GDB (API)
/home/stan/gdb/src/gdb/dwarf2read.c
Go to the documentation of this file.
00001 /* DWARF 2 debugging format support for GDB.
00002 
00003    Copyright (C) 1994-2013 Free Software Foundation, Inc.
00004 
00005    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
00006    Inc.  with support from Florida State University (under contract
00007    with the Ada Joint Program Office), and Silicon Graphics, Inc.
00008    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
00009    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
00010    support.
00011 
00012    This file is part of GDB.
00013 
00014    This program is free software; you can redistribute it and/or modify
00015    it under the terms of the GNU General Public License as published by
00016    the Free Software Foundation; either version 3 of the License, or
00017    (at your option) any later version.
00018 
00019    This program is distributed in the hope that it will be useful,
00020    but WITHOUT ANY WARRANTY; without even the implied warranty of
00021    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00022    GNU General Public License for more details.
00023 
00024    You should have received a copy of the GNU General Public License
00025    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
00026 
00027 /* FIXME: Various die-reading functions need to be more careful with
00028    reading off the end of the section.
00029    E.g., load_partial_dies, read_partial_die.  */
00030 
00031 #include "defs.h"
00032 #include "bfd.h"
00033 #include "elf-bfd.h"
00034 #include "symtab.h"
00035 #include "gdbtypes.h"
00036 #include "objfiles.h"
00037 #include "dwarf2.h"
00038 #include "buildsym.h"
00039 #include "demangle.h"
00040 #include "gdb-demangle.h"
00041 #include "expression.h"
00042 #include "filenames.h"  /* for DOSish file names */
00043 #include "macrotab.h"
00044 #include "language.h"
00045 #include "complaints.h"
00046 #include "bcache.h"
00047 #include "dwarf2expr.h"
00048 #include "dwarf2loc.h"
00049 #include "cp-support.h"
00050 #include "hashtab.h"
00051 #include "command.h"
00052 #include "gdbcmd.h"
00053 #include "block.h"
00054 #include "addrmap.h"
00055 #include "typeprint.h"
00056 #include "jv-lang.h"
00057 #include "psympriv.h"
00058 #include "exceptions.h"
00059 #include "gdb_stat.h"
00060 #include "completer.h"
00061 #include "vec.h"
00062 #include "c-lang.h"
00063 #include "go-lang.h"
00064 #include "valprint.h"
00065 #include "gdbcore.h" /* for gnutarget */
00066 #include "gdb/gdb-index.h"
00067 #include <ctype.h>
00068 #include "gdb_bfd.h"
00069 #include "f-lang.h"
00070 #include "source.h"
00071 #include "filestuff.h"
00072 #include "build-id.h"
00073 
00074 #include <fcntl.h>
00075 #include "gdb_string.h"
00076 #include "gdb_assert.h"
00077 #include <sys/types.h>
00078 
00079 typedef struct symbol *symbolp;
00080 DEF_VEC_P (symbolp);
00081 
00082 /* When non-zero, print basic high level tracing messages.
00083    This is in contrast to the low level DIE reading of dwarf2_die_debug.  */
00084 static int dwarf2_read_debug = 0;
00085 
00086 /* When non-zero, dump DIEs after they are read in.  */
00087 static unsigned int dwarf2_die_debug = 0;
00088 
00089 /* When non-zero, cross-check physname against demangler.  */
00090 static int check_physname = 0;
00091 
00092 /* When non-zero, do not reject deprecated .gdb_index sections.  */
00093 static int use_deprecated_index_sections = 0;
00094 
00095 static const struct objfile_data *dwarf2_objfile_data_key;
00096 
00097 /* The "aclass" indices for various kinds of computed DWARF symbols.  */
00098 
00099 static int dwarf2_locexpr_index;
00100 static int dwarf2_loclist_index;
00101 static int dwarf2_locexpr_block_index;
00102 static int dwarf2_loclist_block_index;
00103 
00104 /* A descriptor for dwarf sections.
00105 
00106    S.ASECTION, SIZE are typically initialized when the objfile is first
00107    scanned.  BUFFER, READIN are filled in later when the section is read.
00108    If the section contained compressed data then SIZE is updated to record
00109    the uncompressed size of the section.
00110 
00111    DWP file format V2 introduces a wrinkle that is easiest to handle by
00112    creating the concept of virtual sections contained within a real section.
00113    In DWP V2 the sections of the input DWO files are concatenated together
00114    into one section, but section offsets are kept relative to the original
00115    input section.
00116    If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
00117    the real section this "virtual" section is contained in, and BUFFER,SIZE
00118    describe the virtual section.  */
00119 
00120 struct dwarf2_section_info
00121 {
00122   union
00123   {
00124     /* If this is a real section, the bfd section.  */
00125     asection *asection;
00126     /* If this is a virtual section, pointer to the containing ("real")
00127        section.  */
00128     struct dwarf2_section_info *containing_section;
00129   } s;
00130   /* Pointer to section data, only valid if readin.  */
00131   const gdb_byte *buffer;
00132   /* The size of the section, real or virtual.  */
00133   bfd_size_type size;
00134   /* If this is a virtual section, the offset in the real section.
00135      Only valid if is_virtual.  */
00136   bfd_size_type virtual_offset;
00137   /* True if we have tried to read this section.  */
00138   char readin;
00139   /* True if this is a virtual section, False otherwise.
00140      This specifies which of s.asection and s.containing_section to use.  */
00141   char is_virtual;
00142 };
00143 
00144 typedef struct dwarf2_section_info dwarf2_section_info_def;
00145 DEF_VEC_O (dwarf2_section_info_def);
00146 
00147 /* All offsets in the index are of this type.  It must be
00148    architecture-independent.  */
00149 typedef uint32_t offset_type;
00150 
00151 DEF_VEC_I (offset_type);
00152 
00153 /* Ensure only legit values are used.  */
00154 #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
00155   do { \
00156     gdb_assert ((unsigned int) (value) <= 1); \
00157     GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
00158   } while (0)
00159 
00160 /* Ensure only legit values are used.  */
00161 #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
00162   do { \
00163     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
00164                 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
00165     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
00166   } while (0)
00167 
00168 /* Ensure we don't use more than the alloted nuber of bits for the CU.  */
00169 #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
00170   do { \
00171     gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
00172     GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
00173   } while (0)
00174 
00175 /* A description of the mapped index.  The file format is described in
00176    a comment by the code that writes the index.  */
00177 struct mapped_index
00178 {
00179   /* Index data format version.  */
00180   int version;
00181 
00182   /* The total length of the buffer.  */
00183   off_t total_size;
00184 
00185   /* A pointer to the address table data.  */
00186   const gdb_byte *address_table;
00187 
00188   /* Size of the address table data in bytes.  */
00189   offset_type address_table_size;
00190 
00191   /* The symbol table, implemented as a hash table.  */
00192   const offset_type *symbol_table;
00193 
00194   /* Size in slots, each slot is 2 offset_types.  */
00195   offset_type symbol_table_slots;
00196 
00197   /* A pointer to the constant pool.  */
00198   const char *constant_pool;
00199 };
00200 
00201 typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
00202 DEF_VEC_P (dwarf2_per_cu_ptr);
00203 
00204 /* Collection of data recorded per objfile.
00205    This hangs off of dwarf2_objfile_data_key.  */
00206 
00207 struct dwarf2_per_objfile
00208 {
00209   struct dwarf2_section_info info;
00210   struct dwarf2_section_info abbrev;
00211   struct dwarf2_section_info line;
00212   struct dwarf2_section_info loc;
00213   struct dwarf2_section_info macinfo;
00214   struct dwarf2_section_info macro;
00215   struct dwarf2_section_info str;
00216   struct dwarf2_section_info ranges;
00217   struct dwarf2_section_info addr;
00218   struct dwarf2_section_info frame;
00219   struct dwarf2_section_info eh_frame;
00220   struct dwarf2_section_info gdb_index;
00221 
00222   VEC (dwarf2_section_info_def) *types;
00223 
00224   /* Back link.  */
00225   struct objfile *objfile;
00226 
00227   /* Table of all the compilation units.  This is used to locate
00228      the target compilation unit of a particular reference.  */
00229   struct dwarf2_per_cu_data **all_comp_units;
00230 
00231   /* The number of compilation units in ALL_COMP_UNITS.  */
00232   int n_comp_units;
00233 
00234   /* The number of .debug_types-related CUs.  */
00235   int n_type_units;
00236 
00237   /* The .debug_types-related CUs (TUs).
00238      This is stored in malloc space because we may realloc it.  */
00239   struct signatured_type **all_type_units;
00240 
00241   /* The number of entries in all_type_unit_groups.  */
00242   int n_type_unit_groups;
00243 
00244   /* Table of type unit groups.
00245      This exists to make it easy to iterate over all CUs and TU groups.  */
00246   struct type_unit_group **all_type_unit_groups;
00247 
00248   /* Table of struct type_unit_group objects.
00249      The hash key is the DW_AT_stmt_list value.  */
00250   htab_t type_unit_groups;
00251 
00252   /* A table mapping .debug_types signatures to its signatured_type entry.
00253      This is NULL if the .debug_types section hasn't been read in yet.  */
00254   htab_t signatured_types;
00255 
00256   /* Type unit statistics, to see how well the scaling improvements
00257      are doing.  */
00258   struct tu_stats
00259   {
00260     int nr_uniq_abbrev_tables;
00261     int nr_symtabs;
00262     int nr_symtab_sharers;
00263     int nr_stmt_less_type_units;
00264   } tu_stats;
00265 
00266   /* A chain of compilation units that are currently read in, so that
00267      they can be freed later.  */
00268   struct dwarf2_per_cu_data *read_in_chain;
00269 
00270   /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
00271      This is NULL if the table hasn't been allocated yet.  */
00272   htab_t dwo_files;
00273 
00274   /* Non-zero if we've check for whether there is a DWP file.  */
00275   int dwp_checked;
00276 
00277   /* The DWP file if there is one, or NULL.  */
00278   struct dwp_file *dwp_file;
00279 
00280   /* The shared '.dwz' file, if one exists.  This is used when the
00281      original data was compressed using 'dwz -m'.  */
00282   struct dwz_file *dwz_file;
00283 
00284   /* A flag indicating wether this objfile has a section loaded at a
00285      VMA of 0.  */
00286   int has_section_at_zero;
00287 
00288   /* True if we are using the mapped index,
00289      or we are faking it for OBJF_READNOW's sake.  */
00290   unsigned char using_index;
00291 
00292   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
00293   struct mapped_index *index_table;
00294 
00295   /* When using index_table, this keeps track of all quick_file_names entries.
00296      TUs typically share line table entries with a CU, so we maintain a
00297      separate table of all line table entries to support the sharing.
00298      Note that while there can be way more TUs than CUs, we've already
00299      sorted all the TUs into "type unit groups", grouped by their
00300      DW_AT_stmt_list value.  Therefore the only sharing done here is with a
00301      CU and its associated TU group if there is one.  */
00302   htab_t quick_file_names_table;
00303 
00304   /* Set during partial symbol reading, to prevent queueing of full
00305      symbols.  */
00306   int reading_partial_symbols;
00307 
00308   /* Table mapping type DIEs to their struct type *.
00309      This is NULL if not allocated yet.
00310      The mapping is done via (CU/TU + DIE offset) -> type.  */
00311   htab_t die_type_hash;
00312 
00313   /* The CUs we recently read.  */
00314   VEC (dwarf2_per_cu_ptr) *just_read_cus;
00315 };
00316 
00317 static struct dwarf2_per_objfile *dwarf2_per_objfile;
00318 
00319 /* Default names of the debugging sections.  */
00320 
00321 /* Note that if the debugging section has been compressed, it might
00322    have a name like .zdebug_info.  */
00323 
00324 static const struct dwarf2_debug_sections dwarf2_elf_names =
00325 {
00326   { ".debug_info", ".zdebug_info" },
00327   { ".debug_abbrev", ".zdebug_abbrev" },
00328   { ".debug_line", ".zdebug_line" },
00329   { ".debug_loc", ".zdebug_loc" },
00330   { ".debug_macinfo", ".zdebug_macinfo" },
00331   { ".debug_macro", ".zdebug_macro" },
00332   { ".debug_str", ".zdebug_str" },
00333   { ".debug_ranges", ".zdebug_ranges" },
00334   { ".debug_types", ".zdebug_types" },
00335   { ".debug_addr", ".zdebug_addr" },
00336   { ".debug_frame", ".zdebug_frame" },
00337   { ".eh_frame", NULL },
00338   { ".gdb_index", ".zgdb_index" },
00339   23
00340 };
00341 
00342 /* List of DWO/DWP sections.  */
00343 
00344 static const struct dwop_section_names
00345 {
00346   struct dwarf2_section_names abbrev_dwo;
00347   struct dwarf2_section_names info_dwo;
00348   struct dwarf2_section_names line_dwo;
00349   struct dwarf2_section_names loc_dwo;
00350   struct dwarf2_section_names macinfo_dwo;
00351   struct dwarf2_section_names macro_dwo;
00352   struct dwarf2_section_names str_dwo;
00353   struct dwarf2_section_names str_offsets_dwo;
00354   struct dwarf2_section_names types_dwo;
00355   struct dwarf2_section_names cu_index;
00356   struct dwarf2_section_names tu_index;
00357 }
00358 dwop_section_names =
00359 {
00360   { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
00361   { ".debug_info.dwo", ".zdebug_info.dwo" },
00362   { ".debug_line.dwo", ".zdebug_line.dwo" },
00363   { ".debug_loc.dwo", ".zdebug_loc.dwo" },
00364   { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
00365   { ".debug_macro.dwo", ".zdebug_macro.dwo" },
00366   { ".debug_str.dwo", ".zdebug_str.dwo" },
00367   { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
00368   { ".debug_types.dwo", ".zdebug_types.dwo" },
00369   { ".debug_cu_index", ".zdebug_cu_index" },
00370   { ".debug_tu_index", ".zdebug_tu_index" },
00371 };
00372 
00373 /* local data types */
00374 
00375 /* The data in a compilation unit header, after target2host
00376    translation, looks like this.  */
00377 struct comp_unit_head
00378 {
00379   unsigned int length;
00380   short version;
00381   unsigned char addr_size;
00382   unsigned char signed_addr_p;
00383   sect_offset abbrev_offset;
00384 
00385   /* Size of file offsets; either 4 or 8.  */
00386   unsigned int offset_size;
00387 
00388   /* Size of the length field; either 4 or 12.  */
00389   unsigned int initial_length_size;
00390 
00391   /* Offset to the first byte of this compilation unit header in the
00392      .debug_info section, for resolving relative reference dies.  */
00393   sect_offset offset;
00394 
00395   /* Offset to first die in this cu from the start of the cu.
00396      This will be the first byte following the compilation unit header.  */
00397   cu_offset first_die_offset;
00398 };
00399 
00400 /* Type used for delaying computation of method physnames.
00401    See comments for compute_delayed_physnames.  */
00402 struct delayed_method_info
00403 {
00404   /* The type to which the method is attached, i.e., its parent class.  */
00405   struct type *type;
00406 
00407   /* The index of the method in the type's function fieldlists.  */
00408   int fnfield_index;
00409 
00410   /* The index of the method in the fieldlist.  */
00411   int index;
00412 
00413   /* The name of the DIE.  */
00414   const char *name;
00415 
00416   /*  The DIE associated with this method.  */
00417   struct die_info *die;
00418 };
00419 
00420 typedef struct delayed_method_info delayed_method_info;
00421 DEF_VEC_O (delayed_method_info);
00422 
00423 /* Internal state when decoding a particular compilation unit.  */
00424 struct dwarf2_cu
00425 {
00426   /* The objfile containing this compilation unit.  */
00427   struct objfile *objfile;
00428 
00429   /* The header of the compilation unit.  */
00430   struct comp_unit_head header;
00431 
00432   /* Base address of this compilation unit.  */
00433   CORE_ADDR base_address;
00434 
00435   /* Non-zero if base_address has been set.  */
00436   int base_known;
00437 
00438   /* The language we are debugging.  */
00439   enum language language;
00440   const struct language_defn *language_defn;
00441 
00442   const char *producer;
00443 
00444   /* The generic symbol table building routines have separate lists for
00445      file scope symbols and all all other scopes (local scopes).  So
00446      we need to select the right one to pass to add_symbol_to_list().
00447      We do it by keeping a pointer to the correct list in list_in_scope.
00448 
00449      FIXME: The original dwarf code just treated the file scope as the
00450      first local scope, and all other local scopes as nested local
00451      scopes, and worked fine.  Check to see if we really need to
00452      distinguish these in buildsym.c.  */
00453   struct pending **list_in_scope;
00454 
00455   /* The abbrev table for this CU.
00456      Normally this points to the abbrev table in the objfile.
00457      But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file.  */
00458   struct abbrev_table *abbrev_table;
00459 
00460   /* Hash table holding all the loaded partial DIEs
00461      with partial_die->offset.SECT_OFF as hash.  */
00462   htab_t partial_dies;
00463 
00464   /* Storage for things with the same lifetime as this read-in compilation
00465      unit, including partial DIEs.  */
00466   struct obstack comp_unit_obstack;
00467 
00468   /* When multiple dwarf2_cu structures are living in memory, this field
00469      chains them all together, so that they can be released efficiently.
00470      We will probably also want a generation counter so that most-recently-used
00471      compilation units are cached...  */
00472   struct dwarf2_per_cu_data *read_in_chain;
00473 
00474   /* Backlink to our per_cu entry.  */
00475   struct dwarf2_per_cu_data *per_cu;
00476 
00477   /* How many compilation units ago was this CU last referenced?  */
00478   int last_used;
00479 
00480   /* A hash table of DIE cu_offset for following references with
00481      die_info->offset.sect_off as hash.  */
00482   htab_t die_hash;
00483 
00484   /* Full DIEs if read in.  */
00485   struct die_info *dies;
00486 
00487   /* A set of pointers to dwarf2_per_cu_data objects for compilation
00488      units referenced by this one.  Only set during full symbol processing;
00489      partial symbol tables do not have dependencies.  */
00490   htab_t dependencies;
00491 
00492   /* Header data from the line table, during full symbol processing.  */
00493   struct line_header *line_header;
00494 
00495   /* A list of methods which need to have physnames computed
00496      after all type information has been read.  */
00497   VEC (delayed_method_info) *method_list;
00498 
00499   /* To be copied to symtab->call_site_htab.  */
00500   htab_t call_site_htab;
00501 
00502   /* Non-NULL if this CU came from a DWO file.
00503      There is an invariant here that is important to remember:
00504      Except for attributes copied from the top level DIE in the "main"
00505      (or "stub") file in preparation for reading the DWO file
00506      (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
00507      Either there isn't a DWO file (in which case this is NULL and the point
00508      is moot), or there is and either we're not going to read it (in which
00509      case this is NULL) or there is and we are reading it (in which case this
00510      is non-NULL).  */
00511   struct dwo_unit *dwo_unit;
00512 
00513   /* The DW_AT_addr_base attribute if present, zero otherwise
00514      (zero is a valid value though).
00515      Note this value comes from the stub CU/TU's DIE.  */
00516   ULONGEST addr_base;
00517 
00518   /* The DW_AT_ranges_base attribute if present, zero otherwise
00519      (zero is a valid value though).
00520      Note this value comes from the stub CU/TU's DIE.
00521      Also note that the value is zero in the non-DWO case so this value can
00522      be used without needing to know whether DWO files are in use or not.
00523      N.B. This does not apply to DW_AT_ranges appearing in
00524      DW_TAG_compile_unit dies.  This is a bit of a wart, consider if ever
00525      DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
00526      DW_AT_ranges_base *would* have to be applied, and we'd have to care
00527      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
00528   ULONGEST ranges_base;
00529 
00530   /* Mark used when releasing cached dies.  */
00531   unsigned int mark : 1;
00532 
00533   /* This CU references .debug_loc.  See the symtab->locations_valid field.
00534      This test is imperfect as there may exist optimized debug code not using
00535      any location list and still facing inlining issues if handled as
00536      unoptimized code.  For a future better test see GCC PR other/32998.  */
00537   unsigned int has_loclist : 1;
00538 
00539   /* These cache the results for producer_is_* fields.  CHECKED_PRODUCER is set
00540      if all the producer_is_* fields are valid.  This information is cached
00541      because profiling CU expansion showed excessive time spent in
00542      producer_is_gxx_lt_4_6.  */
00543   unsigned int checked_producer : 1;
00544   unsigned int producer_is_gxx_lt_4_6 : 1;
00545   unsigned int producer_is_gcc_lt_4_3 : 1;
00546   unsigned int producer_is_icc : 1;
00547 
00548   /* When set, the file that we're processing is known to have
00549      debugging info for C++ namespaces.  GCC 3.3.x did not produce
00550      this information, but later versions do.  */
00551 
00552   unsigned int processing_has_namespace_info : 1;
00553 };
00554 
00555 /* Persistent data held for a compilation unit, even when not
00556    processing it.  We put a pointer to this structure in the
00557    read_symtab_private field of the psymtab.  */
00558 
00559 struct dwarf2_per_cu_data
00560 {
00561   /* The start offset and length of this compilation unit.
00562      NOTE: Unlike comp_unit_head.length, this length includes
00563      initial_length_size.
00564      If the DIE refers to a DWO file, this is always of the original die,
00565      not the DWO file.  */
00566   sect_offset offset;
00567   unsigned int length;
00568 
00569   /* Flag indicating this compilation unit will be read in before
00570      any of the current compilation units are processed.  */
00571   unsigned int queued : 1;
00572 
00573   /* This flag will be set when reading partial DIEs if we need to load
00574      absolutely all DIEs for this compilation unit, instead of just the ones
00575      we think are interesting.  It gets set if we look for a DIE in the
00576      hash table and don't find it.  */
00577   unsigned int load_all_dies : 1;
00578 
00579   /* Non-zero if this CU is from .debug_types.
00580      Struct dwarf2_per_cu_data is contained in struct signatured_type iff
00581      this is non-zero.  */
00582   unsigned int is_debug_types : 1;
00583 
00584   /* Non-zero if this CU is from the .dwz file.  */
00585   unsigned int is_dwz : 1;
00586 
00587   /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
00588      This flag is only valid if is_debug_types is true.
00589      We can't read a CU directly from a DWO file: There are required
00590      attributes in the stub.  */
00591   unsigned int reading_dwo_directly : 1;
00592 
00593   /* Non-zero if the TU has been read.
00594      This is used to assist the "Stay in DWO Optimization" for Fission:
00595      When reading a DWO, it's faster to read TUs from the DWO instead of
00596      fetching them from random other DWOs (due to comdat folding).
00597      If the TU has already been read, the optimization is unnecessary
00598      (and unwise - we don't want to change where gdb thinks the TU lives
00599      "midflight").
00600      This flag is only valid if is_debug_types is true.  */
00601   unsigned int tu_read : 1;
00602 
00603   /* The section this CU/TU lives in.
00604      If the DIE refers to a DWO file, this is always the original die,
00605      not the DWO file.  */
00606   struct dwarf2_section_info *section;
00607 
00608   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
00609      of the CU cache it gets reset to NULL again.  */
00610   struct dwarf2_cu *cu;
00611 
00612   /* The corresponding objfile.
00613      Normally we can get the objfile from dwarf2_per_objfile.
00614      However we can enter this file with just a "per_cu" handle.  */
00615   struct objfile *objfile;
00616 
00617   /* When using partial symbol tables, the 'psymtab' field is active.
00618      Otherwise the 'quick' field is active.  */
00619   union
00620   {
00621     /* The partial symbol table associated with this compilation unit,
00622        or NULL for unread partial units.  */
00623     struct partial_symtab *psymtab;
00624 
00625     /* Data needed by the "quick" functions.  */
00626     struct dwarf2_per_cu_quick_data *quick;
00627   } v;
00628 
00629   /* The CUs we import using DW_TAG_imported_unit.  This is filled in
00630      while reading psymtabs, used to compute the psymtab dependencies,
00631      and then cleared.  Then it is filled in again while reading full
00632      symbols, and only deleted when the objfile is destroyed.
00633 
00634      This is also used to work around a difference between the way gold
00635      generates .gdb_index version <=7 and the way gdb does.  Arguably this
00636      is a gold bug.  For symbols coming from TUs, gold records in the index
00637      the CU that includes the TU instead of the TU itself.  This breaks
00638      dw2_lookup_symbol: It assumes that if the index says symbol X lives
00639      in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
00640      will find X.  Alas TUs live in their own symtab, so after expanding CU Y
00641      we need to look in TU Z to find X.  Fortunately, this is akin to
00642      DW_TAG_imported_unit, so we just use the same mechanism: For
00643      .gdb_index version <=7 this also records the TUs that the CU referred
00644      to.  Concurrently with this change gdb was modified to emit version 8
00645      indices so we only pay a price for gold generated indices.
00646      http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
00647   VEC (dwarf2_per_cu_ptr) *imported_symtabs;
00648 };
00649 
00650 /* Entry in the signatured_types hash table.  */
00651 
00652 struct signatured_type
00653 {
00654   /* The "per_cu" object of this type.
00655      This struct is used iff per_cu.is_debug_types.
00656      N.B.: This is the first member so that it's easy to convert pointers
00657      between them.  */
00658   struct dwarf2_per_cu_data per_cu;
00659 
00660   /* The type's signature.  */
00661   ULONGEST signature;
00662 
00663   /* Offset in the TU of the type's DIE, as read from the TU header.
00664      If this TU is a DWO stub and the definition lives in a DWO file
00665      (specified by DW_AT_GNU_dwo_name), this value is unusable.  */
00666   cu_offset type_offset_in_tu;
00667 
00668   /* Offset in the section of the type's DIE.
00669      If the definition lives in a DWO file, this is the offset in the
00670      .debug_types.dwo section.
00671      The value is zero until the actual value is known.
00672      Zero is otherwise not a valid section offset.  */
00673   sect_offset type_offset_in_section;
00674 
00675   /* Type units are grouped by their DW_AT_stmt_list entry so that they
00676      can share them.  This points to the containing symtab.  */
00677   struct type_unit_group *type_unit_group;
00678 
00679   /* The type.
00680      The first time we encounter this type we fully read it in and install it
00681      in the symbol tables.  Subsequent times we only need the type.  */
00682   struct type *type;
00683 
00684   /* Containing DWO unit.
00685      This field is valid iff per_cu.reading_dwo_directly.  */
00686   struct dwo_unit *dwo_unit;
00687 };
00688 
00689 typedef struct signatured_type *sig_type_ptr;
00690 DEF_VEC_P (sig_type_ptr);
00691 
00692 /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
00693    This includes type_unit_group and quick_file_names.  */
00694 
00695 struct stmt_list_hash
00696 {
00697   /* The DWO unit this table is from or NULL if there is none.  */
00698   struct dwo_unit *dwo_unit;
00699 
00700   /* Offset in .debug_line or .debug_line.dwo.  */
00701   sect_offset line_offset;
00702 };
00703 
00704 /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
00705    an object of this type.  */
00706 
00707 struct type_unit_group
00708 {
00709   /* dwarf2read.c's main "handle" on a TU symtab.
00710      To simplify things we create an artificial CU that "includes" all the
00711      type units using this stmt_list so that the rest of the code still has
00712      a "per_cu" handle on the symtab.
00713      This PER_CU is recognized by having no section.  */
00714 #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
00715   struct dwarf2_per_cu_data per_cu;
00716 
00717   /* The TUs that share this DW_AT_stmt_list entry.
00718      This is added to while parsing type units to build partial symtabs,
00719      and is deleted afterwards and not used again.  */
00720   VEC (sig_type_ptr) *tus;
00721 
00722   /* The primary symtab.
00723      Type units in a group needn't all be defined in the same source file,
00724      so we create an essentially anonymous symtab as the primary symtab.  */
00725   struct symtab *primary_symtab;
00726 
00727   /* The data used to construct the hash key.  */
00728   struct stmt_list_hash hash;
00729 
00730   /* The number of symtabs from the line header.
00731      The value here must match line_header.num_file_names.  */
00732   unsigned int num_symtabs;
00733 
00734   /* The symbol tables for this TU (obtained from the files listed in
00735      DW_AT_stmt_list).
00736      WARNING: The order of entries here must match the order of entries
00737      in the line header.  After the first TU using this type_unit_group, the
00738      line header for the subsequent TUs is recreated from this.  This is done
00739      because we need to use the same symtabs for each TU using the same
00740      DW_AT_stmt_list value.  Also note that symtabs may be repeated here,
00741      there's no guarantee the line header doesn't have duplicate entries.  */
00742   struct symtab **symtabs;
00743 };
00744 
00745 /* These sections are what may appear in a (real or virtual) DWO file.  */
00746 
00747 struct dwo_sections
00748 {
00749   struct dwarf2_section_info abbrev;
00750   struct dwarf2_section_info line;
00751   struct dwarf2_section_info loc;
00752   struct dwarf2_section_info macinfo;
00753   struct dwarf2_section_info macro;
00754   struct dwarf2_section_info str;
00755   struct dwarf2_section_info str_offsets;
00756   /* In the case of a virtual DWO file, these two are unused.  */
00757   struct dwarf2_section_info info;
00758   VEC (dwarf2_section_info_def) *types;
00759 };
00760 
00761 /* CUs/TUs in DWP/DWO files.  */
00762 
00763 struct dwo_unit
00764 {
00765   /* Backlink to the containing struct dwo_file.  */
00766   struct dwo_file *dwo_file;
00767 
00768   /* The "id" that distinguishes this CU/TU.
00769      .debug_info calls this "dwo_id", .debug_types calls this "signature".
00770      Since signatures came first, we stick with it for consistency.  */
00771   ULONGEST signature;
00772 
00773   /* The section this CU/TU lives in, in the DWO file.  */
00774   struct dwarf2_section_info *section;
00775 
00776   /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section.  */
00777   sect_offset offset;
00778   unsigned int length;
00779 
00780   /* For types, offset in the type's DIE of the type defined by this TU.  */
00781   cu_offset type_offset_in_tu;
00782 };
00783 
00784 /* include/dwarf2.h defines the DWP section codes.
00785    It defines a max value but it doesn't define a min value, which we
00786    use for error checking, so provide one.  */
00787 
00788 enum dwp_v2_section_ids
00789 {
00790   DW_SECT_MIN = 1
00791 };
00792 
00793 /* Data for one DWO file.
00794 
00795    This includes virtual DWO files (a virtual DWO file is a DWO file as it
00796    appears in a DWP file).  DWP files don't really have DWO files per se -
00797    comdat folding of types "loses" the DWO file they came from, and from
00798    a high level view DWP files appear to contain a mass of random types.
00799    However, to maintain consistency with the non-DWP case we pretend DWP
00800    files contain virtual DWO files, and we assign each TU with one virtual
00801    DWO file (generally based on the line and abbrev section offsets -
00802    a heuristic that seems to work in practice).  */
00803 
00804 struct dwo_file
00805 {
00806   /* The DW_AT_GNU_dwo_name attribute.
00807      For virtual DWO files the name is constructed from the section offsets
00808      of abbrev,line,loc,str_offsets so that we combine virtual DWO files
00809      from related CU+TUs.  */
00810   const char *dwo_name;
00811 
00812   /* The DW_AT_comp_dir attribute.  */
00813   const char *comp_dir;
00814 
00815   /* The bfd, when the file is open.  Otherwise this is NULL.
00816      This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd.  */
00817   bfd *dbfd;
00818 
00819   /* The sections that make up this DWO file.
00820      Remember that for virtual DWO files in DWP V2, these are virtual
00821      sections (for lack of a better name).  */
00822   struct dwo_sections sections;
00823 
00824   /* The CU in the file.
00825      We only support one because having more than one requires hacking the
00826      dwo_name of each to match, which is highly unlikely to happen.
00827      Doing this means all TUs can share comp_dir: We also assume that
00828      DW_AT_comp_dir across all TUs in a DWO file will be identical.  */
00829   struct dwo_unit *cu;
00830 
00831   /* Table of TUs in the file.
00832      Each element is a struct dwo_unit.  */
00833   htab_t tus;
00834 };
00835 
00836 /* These sections are what may appear in a DWP file.  */
00837 
00838 struct dwp_sections
00839 {
00840   /* These are used by both DWP version 1 and 2.  */
00841   struct dwarf2_section_info str;
00842   struct dwarf2_section_info cu_index;
00843   struct dwarf2_section_info tu_index;
00844 
00845   /* These are only used by DWP version 2 files.
00846      In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
00847      sections are referenced by section number, and are not recorded here.
00848      In DWP version 2 there is at most one copy of all these sections, each
00849      section being (effectively) comprised of the concatenation of all of the
00850      individual sections that exist in the version 1 format.
00851      To keep the code simple we treat each of these concatenated pieces as a
00852      section itself (a virtual section?).  */
00853   struct dwarf2_section_info abbrev;
00854   struct dwarf2_section_info info;
00855   struct dwarf2_section_info line;
00856   struct dwarf2_section_info loc;
00857   struct dwarf2_section_info macinfo;
00858   struct dwarf2_section_info macro;
00859   struct dwarf2_section_info str_offsets;
00860   struct dwarf2_section_info types;
00861 };
00862 
00863 /* These sections are what may appear in a virtual DWO file in DWP version 1.
00864    A virtual DWO file is a DWO file as it appears in a DWP file.  */
00865 
00866 struct virtual_v1_dwo_sections
00867 {
00868   struct dwarf2_section_info abbrev;
00869   struct dwarf2_section_info line;
00870   struct dwarf2_section_info loc;
00871   struct dwarf2_section_info macinfo;
00872   struct dwarf2_section_info macro;
00873   struct dwarf2_section_info str_offsets;
00874   /* Each DWP hash table entry records one CU or one TU.
00875      That is recorded here, and copied to dwo_unit.section.  */
00876   struct dwarf2_section_info info_or_types;
00877 };
00878 
00879 /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
00880    In version 2, the sections of the DWO files are concatenated together
00881    and stored in one section of that name.  Thus each ELF section contains
00882    several "virtual" sections.  */
00883 
00884 struct virtual_v2_dwo_sections
00885 {
00886   bfd_size_type abbrev_offset;
00887   bfd_size_type abbrev_size;
00888 
00889   bfd_size_type line_offset;
00890   bfd_size_type line_size;
00891 
00892   bfd_size_type loc_offset;
00893   bfd_size_type loc_size;
00894 
00895   bfd_size_type macinfo_offset;
00896   bfd_size_type macinfo_size;
00897 
00898   bfd_size_type macro_offset;
00899   bfd_size_type macro_size;
00900 
00901   bfd_size_type str_offsets_offset;
00902   bfd_size_type str_offsets_size;
00903 
00904   /* Each DWP hash table entry records one CU or one TU.
00905      That is recorded here, and copied to dwo_unit.section.  */
00906   bfd_size_type info_or_types_offset;
00907   bfd_size_type info_or_types_size;
00908 };
00909 
00910 /* Contents of DWP hash tables.  */
00911 
00912 struct dwp_hash_table
00913 {
00914   uint32_t version, nr_columns;
00915   uint32_t nr_units, nr_slots;
00916   const gdb_byte *hash_table, *unit_table;
00917   union
00918   {
00919     struct
00920     {
00921       const gdb_byte *indices;
00922     } v1;
00923     struct
00924     {
00925       /* This is indexed by column number and gives the id of the section
00926          in that column.  */
00927 #define MAX_NR_V2_DWO_SECTIONS \
00928   (1 /* .debug_info or .debug_types */ \
00929    + 1 /* .debug_abbrev */ \
00930    + 1 /* .debug_line */ \
00931    + 1 /* .debug_loc */ \
00932    + 1 /* .debug_str_offsets */ \
00933    + 1 /* .debug_macro or .debug_macinfo */)
00934       int section_ids[MAX_NR_V2_DWO_SECTIONS];
00935       const gdb_byte *offsets;
00936       const gdb_byte *sizes;
00937     } v2;
00938   } section_pool;
00939 };
00940 
00941 /* Data for one DWP file.  */
00942 
00943 struct dwp_file
00944 {
00945   /* Name of the file.  */
00946   const char *name;
00947 
00948   /* File format version.  */
00949   int version;
00950 
00951   /* The bfd.  */
00952   bfd *dbfd;
00953 
00954   /* Section info for this file.  */
00955   struct dwp_sections sections;
00956 
00957   /* Table of CUs in the file.  */
00958   const struct dwp_hash_table *cus;
00959 
00960   /* Table of TUs in the file.  */
00961   const struct dwp_hash_table *tus;
00962 
00963   /* Tables of loaded CUs/TUs.  Each entry is a struct dwo_unit *.  */
00964   htab_t loaded_cus;
00965   htab_t loaded_tus;
00966 
00967   /* Table to map ELF section numbers to their sections.
00968      This is only needed for the DWP V1 file format.  */
00969   unsigned int num_sections;
00970   asection **elf_sections;
00971 };
00972 
00973 /* This represents a '.dwz' file.  */
00974 
00975 struct dwz_file
00976 {
00977   /* A dwz file can only contain a few sections.  */
00978   struct dwarf2_section_info abbrev;
00979   struct dwarf2_section_info info;
00980   struct dwarf2_section_info str;
00981   struct dwarf2_section_info line;
00982   struct dwarf2_section_info macro;
00983   struct dwarf2_section_info gdb_index;
00984 
00985   /* The dwz's BFD.  */
00986   bfd *dwz_bfd;
00987 };
00988 
00989 /* Struct used to pass misc. parameters to read_die_and_children, et
00990    al.  which are used for both .debug_info and .debug_types dies.
00991    All parameters here are unchanging for the life of the call.  This
00992    struct exists to abstract away the constant parameters of die reading.  */
00993 
00994 struct die_reader_specs
00995 {
00996   /* The bfd of die_section.  */
00997   bfd* abfd;
00998 
00999   /* The CU of the DIE we are parsing.  */
01000   struct dwarf2_cu *cu;
01001 
01002   /* Non-NULL if reading a DWO file (including one packaged into a DWP).  */
01003   struct dwo_file *dwo_file;
01004 
01005   /* The section the die comes from.
01006      This is either .debug_info or .debug_types, or the .dwo variants.  */
01007   struct dwarf2_section_info *die_section;
01008 
01009   /* die_section->buffer.  */
01010   const gdb_byte *buffer;
01011 
01012   /* The end of the buffer.  */
01013   const gdb_byte *buffer_end;
01014 
01015   /* The value of the DW_AT_comp_dir attribute.  */
01016   const char *comp_dir;
01017 };
01018 
01019 /* Type of function passed to init_cutu_and_read_dies, et.al.  */
01020 typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
01021                                       const gdb_byte *info_ptr,
01022                                       struct die_info *comp_unit_die,
01023                                       int has_children,
01024                                       void *data);
01025 
01026 /* The line number information for a compilation unit (found in the
01027    .debug_line section) begins with a "statement program header",
01028    which contains the following information.  */
01029 struct line_header
01030 {
01031   unsigned int total_length;
01032   unsigned short version;
01033   unsigned int header_length;
01034   unsigned char minimum_instruction_length;
01035   unsigned char maximum_ops_per_instruction;
01036   unsigned char default_is_stmt;
01037   int line_base;
01038   unsigned char line_range;
01039   unsigned char opcode_base;
01040 
01041   /* standard_opcode_lengths[i] is the number of operands for the
01042      standard opcode whose value is i.  This means that
01043      standard_opcode_lengths[0] is unused, and the last meaningful
01044      element is standard_opcode_lengths[opcode_base - 1].  */
01045   unsigned char *standard_opcode_lengths;
01046 
01047   /* The include_directories table.  NOTE!  These strings are not
01048      allocated with xmalloc; instead, they are pointers into
01049      debug_line_buffer.  If you try to free them, `free' will get
01050      indigestion.  */
01051   unsigned int num_include_dirs, include_dirs_size;
01052   const char **include_dirs;
01053 
01054   /* The file_names table.  NOTE!  These strings are not allocated
01055      with xmalloc; instead, they are pointers into debug_line_buffer.
01056      Don't try to free them directly.  */
01057   unsigned int num_file_names, file_names_size;
01058   struct file_entry
01059   {
01060     const char *name;
01061     unsigned int dir_index;
01062     unsigned int mod_time;
01063     unsigned int length;
01064     int included_p; /* Non-zero if referenced by the Line Number Program.  */
01065     struct symtab *symtab; /* The associated symbol table, if any.  */
01066   } *file_names;
01067 
01068   /* The start and end of the statement program following this
01069      header.  These point into dwarf2_per_objfile->line_buffer.  */
01070   const gdb_byte *statement_program_start, *statement_program_end;
01071 };
01072 
01073 /* When we construct a partial symbol table entry we only
01074    need this much information.  */
01075 struct partial_die_info
01076   {
01077     /* Offset of this DIE.  */
01078     sect_offset offset;
01079 
01080     /* DWARF-2 tag for this DIE.  */
01081     ENUM_BITFIELD(dwarf_tag) tag : 16;
01082 
01083     /* Assorted flags describing the data found in this DIE.  */
01084     unsigned int has_children : 1;
01085     unsigned int is_external : 1;
01086     unsigned int is_declaration : 1;
01087     unsigned int has_type : 1;
01088     unsigned int has_specification : 1;
01089     unsigned int has_pc_info : 1;
01090     unsigned int may_be_inlined : 1;
01091 
01092     /* Flag set if the SCOPE field of this structure has been
01093        computed.  */
01094     unsigned int scope_set : 1;
01095 
01096     /* Flag set if the DIE has a byte_size attribute.  */
01097     unsigned int has_byte_size : 1;
01098 
01099     /* Flag set if any of the DIE's children are template arguments.  */
01100     unsigned int has_template_arguments : 1;
01101 
01102     /* Flag set if fixup_partial_die has been called on this die.  */
01103     unsigned int fixup_called : 1;
01104 
01105     /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
01106     unsigned int is_dwz : 1;
01107 
01108     /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt.  */
01109     unsigned int spec_is_dwz : 1;
01110 
01111     /* The name of this DIE.  Normally the value of DW_AT_name, but
01112        sometimes a default name for unnamed DIEs.  */
01113     const char *name;
01114 
01115     /* The linkage name, if present.  */
01116     const char *linkage_name;
01117 
01118     /* The scope to prepend to our children.  This is generally
01119        allocated on the comp_unit_obstack, so will disappear
01120        when this compilation unit leaves the cache.  */
01121     const char *scope;
01122 
01123     /* Some data associated with the partial DIE.  The tag determines
01124        which field is live.  */
01125     union
01126     {
01127       /* The location description associated with this DIE, if any.  */
01128       struct dwarf_block *locdesc;
01129       /* The offset of an import, for DW_TAG_imported_unit.  */
01130       sect_offset offset;
01131     } d;
01132 
01133     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
01134     CORE_ADDR lowpc;
01135     CORE_ADDR highpc;
01136 
01137     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
01138        DW_AT_sibling, if any.  */
01139     /* NOTE: This member isn't strictly necessary, read_partial_die could
01140        return DW_AT_sibling values to its caller load_partial_dies.  */
01141     const gdb_byte *sibling;
01142 
01143     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
01144        DW_AT_specification (or DW_AT_abstract_origin or
01145        DW_AT_extension).  */
01146     sect_offset spec_offset;
01147 
01148     /* Pointers to this DIE's parent, first child, and next sibling,
01149        if any.  */
01150     struct partial_die_info *die_parent, *die_child, *die_sibling;
01151   };
01152 
01153 /* This data structure holds the information of an abbrev.  */
01154 struct abbrev_info
01155   {
01156     unsigned int number;        /* number identifying abbrev */
01157     enum dwarf_tag tag;         /* dwarf tag */
01158     unsigned short has_children;                /* boolean */
01159     unsigned short num_attrs;   /* number of attributes */
01160     struct attr_abbrev *attrs;  /* an array of attribute descriptions */
01161     struct abbrev_info *next;   /* next in chain */
01162   };
01163 
01164 struct attr_abbrev
01165   {
01166     ENUM_BITFIELD(dwarf_attribute) name : 16;
01167     ENUM_BITFIELD(dwarf_form) form : 16;
01168   };
01169 
01170 /* Size of abbrev_table.abbrev_hash_table.  */
01171 #define ABBREV_HASH_SIZE 121
01172 
01173 /* Top level data structure to contain an abbreviation table.  */
01174 
01175 struct abbrev_table
01176 {
01177   /* Where the abbrev table came from.
01178      This is used as a sanity check when the table is used.  */
01179   sect_offset offset;
01180 
01181   /* Storage for the abbrev table.  */
01182   struct obstack abbrev_obstack;
01183 
01184   /* Hash table of abbrevs.
01185      This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
01186      It could be statically allocated, but the previous code didn't so we
01187      don't either.  */
01188   struct abbrev_info **abbrevs;
01189 };
01190 
01191 /* Attributes have a name and a value.  */
01192 struct attribute
01193   {
01194     ENUM_BITFIELD(dwarf_attribute) name : 16;
01195     ENUM_BITFIELD(dwarf_form) form : 15;
01196 
01197     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
01198        field should be in u.str (existing only for DW_STRING) but it is kept
01199        here for better struct attribute alignment.  */
01200     unsigned int string_is_canonical : 1;
01201 
01202     union
01203       {
01204         const char *str;
01205         struct dwarf_block *blk;
01206         ULONGEST unsnd;
01207         LONGEST snd;
01208         CORE_ADDR addr;
01209         ULONGEST signature;
01210       }
01211     u;
01212   };
01213 
01214 /* This data structure holds a complete die structure.  */
01215 struct die_info
01216   {
01217     /* DWARF-2 tag for this DIE.  */
01218     ENUM_BITFIELD(dwarf_tag) tag : 16;
01219 
01220     /* Number of attributes */
01221     unsigned char num_attrs;
01222 
01223     /* True if we're presently building the full type name for the
01224        type derived from this DIE.  */
01225     unsigned char building_fullname : 1;
01226 
01227     /* Abbrev number */
01228     unsigned int abbrev;
01229 
01230     /* Offset in .debug_info or .debug_types section.  */
01231     sect_offset offset;
01232 
01233     /* The dies in a compilation unit form an n-ary tree.  PARENT
01234        points to this die's parent; CHILD points to the first child of
01235        this node; and all the children of a given node are chained
01236        together via their SIBLING fields.  */
01237     struct die_info *child;     /* Its first child, if any.  */
01238     struct die_info *sibling;   /* Its next sibling, if any.  */
01239     struct die_info *parent;    /* Its parent, if any.  */
01240 
01241     /* An array of attributes, with NUM_ATTRS elements.  There may be
01242        zero, but it's not common and zero-sized arrays are not
01243        sufficiently portable C.  */
01244     struct attribute attrs[1];
01245   };
01246 
01247 /* Get at parts of an attribute structure.  */
01248 
01249 #define DW_STRING(attr)    ((attr)->u.str)
01250 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
01251 #define DW_UNSND(attr)     ((attr)->u.unsnd)
01252 #define DW_BLOCK(attr)     ((attr)->u.blk)
01253 #define DW_SND(attr)       ((attr)->u.snd)
01254 #define DW_ADDR(attr)      ((attr)->u.addr)
01255 #define DW_SIGNATURE(attr) ((attr)->u.signature)
01256 
01257 /* Blocks are a bunch of untyped bytes.  */
01258 struct dwarf_block
01259   {
01260     size_t size;
01261 
01262     /* Valid only if SIZE is not zero.  */
01263     const gdb_byte *data;
01264   };
01265 
01266 #ifndef ATTR_ALLOC_CHUNK
01267 #define ATTR_ALLOC_CHUNK 4
01268 #endif
01269 
01270 /* Allocate fields for structs, unions and enums in this size.  */
01271 #ifndef DW_FIELD_ALLOC_CHUNK
01272 #define DW_FIELD_ALLOC_CHUNK 4
01273 #endif
01274 
01275 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
01276    but this would require a corresponding change in unpack_field_as_long
01277    and friends.  */
01278 static int bits_per_byte = 8;
01279 
01280 /* The routines that read and process dies for a C struct or C++ class
01281    pass lists of data member fields and lists of member function fields
01282    in an instance of a field_info structure, as defined below.  */
01283 struct field_info
01284   {
01285     /* List of data member and baseclasses fields.  */
01286     struct nextfield
01287       {
01288         struct nextfield *next;
01289         int accessibility;
01290         int virtuality;
01291         struct field field;
01292       }
01293      *fields, *baseclasses;
01294 
01295     /* Number of fields (including baseclasses).  */
01296     int nfields;
01297 
01298     /* Number of baseclasses.  */
01299     int nbaseclasses;
01300 
01301     /* Set if the accesibility of one of the fields is not public.  */
01302     int non_public_fields;
01303 
01304     /* Member function fields array, entries are allocated in the order they
01305        are encountered in the object file.  */
01306     struct nextfnfield
01307       {
01308         struct nextfnfield *next;
01309         struct fn_field fnfield;
01310       }
01311      *fnfields;
01312 
01313     /* Member function fieldlist array, contains name of possibly overloaded
01314        member function, number of overloaded member functions and a pointer
01315        to the head of the member function field chain.  */
01316     struct fnfieldlist
01317       {
01318         const char *name;
01319         int length;
01320         struct nextfnfield *head;
01321       }
01322      *fnfieldlists;
01323 
01324     /* Number of entries in the fnfieldlists array.  */
01325     int nfnfields;
01326 
01327     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
01328        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
01329     struct typedef_field_list
01330       {
01331         struct typedef_field field;
01332         struct typedef_field_list *next;
01333       }
01334     *typedef_field_list;
01335     unsigned typedef_field_list_count;
01336   };
01337 
01338 /* One item on the queue of compilation units to read in full symbols
01339    for.  */
01340 struct dwarf2_queue_item
01341 {
01342   struct dwarf2_per_cu_data *per_cu;
01343   enum language pretend_language;
01344   struct dwarf2_queue_item *next;
01345 };
01346 
01347 /* The current queue.  */
01348 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
01349 
01350 /* Loaded secondary compilation units are kept in memory until they
01351    have not been referenced for the processing of this many
01352    compilation units.  Set this to zero to disable caching.  Cache
01353    sizes of up to at least twenty will improve startup time for
01354    typical inter-CU-reference binaries, at an obvious memory cost.  */
01355 static int dwarf2_max_cache_age = 5;
01356 static void
01357 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
01358                            struct cmd_list_element *c, const char *value)
01359 {
01360   fprintf_filtered (file, _("The upper bound on the age of cached "
01361                             "dwarf2 compilation units is %s.\n"),
01362                     value);
01363 }
01364 
01365 /* local function prototypes */
01366 
01367 static const char *get_section_name (const struct dwarf2_section_info *);
01368 
01369 static const char *get_section_file_name (const struct dwarf2_section_info *);
01370 
01371 static void dwarf2_locate_sections (bfd *, asection *, void *);
01372 
01373 static void dwarf2_find_base_address (struct die_info *die,
01374                                       struct dwarf2_cu *cu);
01375 
01376 static struct partial_symtab *create_partial_symtab
01377   (struct dwarf2_per_cu_data *per_cu, const char *name);
01378 
01379 static void dwarf2_build_psymtabs_hard (struct objfile *);
01380 
01381 static void scan_partial_symbols (struct partial_die_info *,
01382                                   CORE_ADDR *, CORE_ADDR *,
01383                                   int, struct dwarf2_cu *);
01384 
01385 static void add_partial_symbol (struct partial_die_info *,
01386                                 struct dwarf2_cu *);
01387 
01388 static void add_partial_namespace (struct partial_die_info *pdi,
01389                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
01390                                    int need_pc, struct dwarf2_cu *cu);
01391 
01392 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
01393                                 CORE_ADDR *highpc, int need_pc,
01394                                 struct dwarf2_cu *cu);
01395 
01396 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
01397                                      struct dwarf2_cu *cu);
01398 
01399 static void add_partial_subprogram (struct partial_die_info *pdi,
01400                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
01401                                     int need_pc, struct dwarf2_cu *cu);
01402 
01403 static void dwarf2_read_symtab (struct partial_symtab *,
01404                                 struct objfile *);
01405 
01406 static void psymtab_to_symtab_1 (struct partial_symtab *);
01407 
01408 static struct abbrev_info *abbrev_table_lookup_abbrev
01409   (const struct abbrev_table *, unsigned int);
01410 
01411 static struct abbrev_table *abbrev_table_read_table
01412   (struct dwarf2_section_info *, sect_offset);
01413 
01414 static void abbrev_table_free (struct abbrev_table *);
01415 
01416 static void abbrev_table_free_cleanup (void *);
01417 
01418 static void dwarf2_read_abbrevs (struct dwarf2_cu *,
01419                                  struct dwarf2_section_info *);
01420 
01421 static void dwarf2_free_abbrev_table (void *);
01422 
01423 static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
01424 
01425 static struct partial_die_info *load_partial_dies
01426   (const struct die_reader_specs *, const gdb_byte *, int);
01427 
01428 static const gdb_byte *read_partial_die (const struct die_reader_specs *,
01429                                          struct partial_die_info *,
01430                                          struct abbrev_info *,
01431                                          unsigned int,
01432                                          const gdb_byte *);
01433 
01434 static struct partial_die_info *find_partial_die (sect_offset, int,
01435                                                   struct dwarf2_cu *);
01436 
01437 static void fixup_partial_die (struct partial_die_info *,
01438                                struct dwarf2_cu *);
01439 
01440 static const gdb_byte *read_attribute (const struct die_reader_specs *,
01441                                        struct attribute *, struct attr_abbrev *,
01442                                        const gdb_byte *);
01443 
01444 static unsigned int read_1_byte (bfd *, const gdb_byte *);
01445 
01446 static int read_1_signed_byte (bfd *, const gdb_byte *);
01447 
01448 static unsigned int read_2_bytes (bfd *, const gdb_byte *);
01449 
01450 static unsigned int read_4_bytes (bfd *, const gdb_byte *);
01451 
01452 static ULONGEST read_8_bytes (bfd *, const gdb_byte *);
01453 
01454 static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
01455                                unsigned int *);
01456 
01457 static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);
01458 
01459 static LONGEST read_checked_initial_length_and_offset
01460   (bfd *, const gdb_byte *, const struct comp_unit_head *,
01461    unsigned int *, unsigned int *);
01462 
01463 static LONGEST read_offset (bfd *, const gdb_byte *,
01464                             const struct comp_unit_head *,
01465                             unsigned int *);
01466 
01467 static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);
01468 
01469 static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
01470                                        sect_offset);
01471 
01472 static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);
01473 
01474 static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);
01475 
01476 static const char *read_indirect_string (bfd *, const gdb_byte *,
01477                                          const struct comp_unit_head *,
01478                                          unsigned int *);
01479 
01480 static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);
01481 
01482 static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);
01483 
01484 static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);
01485 
01486 static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
01487                                               const gdb_byte *,
01488                                               unsigned int *);
01489 
01490 static const char *read_str_index (const struct die_reader_specs *reader,
01491                                    struct dwarf2_cu *cu, ULONGEST str_index);
01492 
01493 static void set_cu_language (unsigned int, struct dwarf2_cu *);
01494 
01495 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
01496                                       struct dwarf2_cu *);
01497 
01498 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
01499                                                 unsigned int);
01500 
01501 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
01502                                struct dwarf2_cu *cu);
01503 
01504 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
01505 
01506 static struct die_info *die_specification (struct die_info *die,
01507                                            struct dwarf2_cu **);
01508 
01509 static void free_line_header (struct line_header *lh);
01510 
01511 static struct line_header *dwarf_decode_line_header (unsigned int offset,
01512                                                      struct dwarf2_cu *cu);
01513 
01514 static void dwarf_decode_lines (struct line_header *, const char *,
01515                                 struct dwarf2_cu *, struct partial_symtab *,
01516                                 int);
01517 
01518 static void dwarf2_start_subfile (const char *, const char *, const char *);
01519 
01520 static void dwarf2_start_symtab (struct dwarf2_cu *,
01521                                  const char *, const char *, CORE_ADDR);
01522 
01523 static struct symbol *new_symbol (struct die_info *, struct type *,
01524                                   struct dwarf2_cu *);
01525 
01526 static struct symbol *new_symbol_full (struct die_info *, struct type *,
01527                                        struct dwarf2_cu *, struct symbol *);
01528 
01529 static void dwarf2_const_value (const struct attribute *, struct symbol *,
01530                                 struct dwarf2_cu *);
01531 
01532 static void dwarf2_const_value_attr (const struct attribute *attr,
01533                                      struct type *type,
01534                                      const char *name,
01535                                      struct obstack *obstack,
01536                                      struct dwarf2_cu *cu, LONGEST *value,
01537                                      const gdb_byte **bytes,
01538                                      struct dwarf2_locexpr_baton **baton);
01539 
01540 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
01541 
01542 static int need_gnat_info (struct dwarf2_cu *);
01543 
01544 static struct type *die_descriptive_type (struct die_info *,
01545                                           struct dwarf2_cu *);
01546 
01547 static void set_descriptive_type (struct type *, struct die_info *,
01548                                   struct dwarf2_cu *);
01549 
01550 static struct type *die_containing_type (struct die_info *,
01551                                          struct dwarf2_cu *);
01552 
01553 static struct type *lookup_die_type (struct die_info *, const struct attribute *,
01554                                      struct dwarf2_cu *);
01555 
01556 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
01557 
01558 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
01559 
01560 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
01561 
01562 static char *typename_concat (struct obstack *obs, const char *prefix,
01563                               const char *suffix, int physname,
01564                               struct dwarf2_cu *cu);
01565 
01566 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
01567 
01568 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
01569 
01570 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
01571 
01572 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
01573 
01574 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
01575 
01576 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
01577                                struct dwarf2_cu *, struct partial_symtab *);
01578 
01579 static int dwarf2_get_pc_bounds (struct die_info *,
01580                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
01581                                  struct partial_symtab *);
01582 
01583 static void get_scope_pc_bounds (struct die_info *,
01584                                  CORE_ADDR *, CORE_ADDR *,
01585                                  struct dwarf2_cu *);
01586 
01587 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
01588                                         CORE_ADDR, struct dwarf2_cu *);
01589 
01590 static void dwarf2_add_field (struct field_info *, struct die_info *,
01591                               struct dwarf2_cu *);
01592 
01593 static void dwarf2_attach_fields_to_type (struct field_info *,
01594                                           struct type *, struct dwarf2_cu *);
01595 
01596 static void dwarf2_add_member_fn (struct field_info *,
01597                                   struct die_info *, struct type *,
01598                                   struct dwarf2_cu *);
01599 
01600 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
01601                                              struct type *,
01602                                              struct dwarf2_cu *);
01603 
01604 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
01605 
01606 static void read_common_block (struct die_info *, struct dwarf2_cu *);
01607 
01608 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
01609 
01610 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
01611 
01612 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
01613 
01614 static struct type *read_module_type (struct die_info *die,
01615                                       struct dwarf2_cu *cu);
01616 
01617 static const char *namespace_name (struct die_info *die,
01618                                    int *is_anonymous, struct dwarf2_cu *);
01619 
01620 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
01621 
01622 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
01623 
01624 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
01625                                                        struct dwarf2_cu *);
01626 
01627 static struct die_info *read_die_and_siblings_1
01628   (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
01629    struct die_info *);
01630 
01631 static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
01632                                                const gdb_byte *info_ptr,
01633                                                const gdb_byte **new_info_ptr,
01634                                                struct die_info *parent);
01635 
01636 static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
01637                                         struct die_info **, const gdb_byte *,
01638                                         int *, int);
01639 
01640 static const gdb_byte *read_full_die (const struct die_reader_specs *,
01641                                       struct die_info **, const gdb_byte *,
01642                                       int *);
01643 
01644 static void process_die (struct die_info *, struct dwarf2_cu *);
01645 
01646 static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
01647                                              struct obstack *);
01648 
01649 static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
01650 
01651 static const char *dwarf2_full_name (const char *name,
01652                                      struct die_info *die,
01653                                      struct dwarf2_cu *cu);
01654 
01655 static const char *dwarf2_physname (const char *name, struct die_info *die,
01656                                     struct dwarf2_cu *cu);
01657 
01658 static struct die_info *dwarf2_extension (struct die_info *die,
01659                                           struct dwarf2_cu **);
01660 
01661 static const char *dwarf_tag_name (unsigned int);
01662 
01663 static const char *dwarf_attr_name (unsigned int);
01664 
01665 static const char *dwarf_form_name (unsigned int);
01666 
01667 static char *dwarf_bool_name (unsigned int);
01668 
01669 static const char *dwarf_type_encoding_name (unsigned int);
01670 
01671 static struct die_info *sibling_die (struct die_info *);
01672 
01673 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
01674 
01675 static void dump_die_for_error (struct die_info *);
01676 
01677 static void dump_die_1 (struct ui_file *, int level, int max_level,
01678                         struct die_info *);
01679 
01680 /*static*/ void dump_die (struct die_info *, int max_level);
01681 
01682 static void store_in_ref_table (struct die_info *,
01683                                 struct dwarf2_cu *);
01684 
01685 static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);
01686 
01687 static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);
01688 
01689 static struct die_info *follow_die_ref_or_sig (struct die_info *,
01690                                                const struct attribute *,
01691                                                struct dwarf2_cu **);
01692 
01693 static struct die_info *follow_die_ref (struct die_info *,
01694                                         const struct attribute *,
01695                                         struct dwarf2_cu **);
01696 
01697 static struct die_info *follow_die_sig (struct die_info *,
01698                                         const struct attribute *,
01699                                         struct dwarf2_cu **);
01700 
01701 static struct type *get_signatured_type (struct die_info *, ULONGEST,
01702                                          struct dwarf2_cu *);
01703 
01704 static struct type *get_DW_AT_signature_type (struct die_info *,
01705                                               const struct attribute *,
01706                                               struct dwarf2_cu *);
01707 
01708 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
01709 
01710 static void read_signatured_type (struct signatured_type *);
01711 
01712 static struct type_unit_group *get_type_unit_group
01713     (struct dwarf2_cu *, const struct attribute *);
01714 
01715 static void build_type_unit_groups (die_reader_func_ftype *, void *);
01716 
01717 /* memory allocation interface */
01718 
01719 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
01720 
01721 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
01722 
01723 static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int,
01724                                  const char *, int);
01725 
01726 static int attr_form_is_block (const struct attribute *);
01727 
01728 static int attr_form_is_section_offset (const struct attribute *);
01729 
01730 static int attr_form_is_constant (const struct attribute *);
01731 
01732 static int attr_form_is_ref (const struct attribute *);
01733 
01734 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
01735                                    struct dwarf2_loclist_baton *baton,
01736                                    const struct attribute *attr);
01737 
01738 static void dwarf2_symbol_mark_computed (const struct attribute *attr,
01739                                          struct symbol *sym,
01740                                          struct dwarf2_cu *cu,
01741                                          int is_block);
01742 
01743 static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
01744                                      const gdb_byte *info_ptr,
01745                                      struct abbrev_info *abbrev);
01746 
01747 static void free_stack_comp_unit (void *);
01748 
01749 static hashval_t partial_die_hash (const void *item);
01750 
01751 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
01752 
01753 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
01754   (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);
01755 
01756 static void init_one_comp_unit (struct dwarf2_cu *cu,
01757                                 struct dwarf2_per_cu_data *per_cu);
01758 
01759 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
01760                                    struct die_info *comp_unit_die,
01761                                    enum language pretend_language);
01762 
01763 static void free_heap_comp_unit (void *);
01764 
01765 static void free_cached_comp_units (void *);
01766 
01767 static void age_cached_comp_units (void);
01768 
01769 static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
01770 
01771 static struct type *set_die_type (struct die_info *, struct type *,
01772                                   struct dwarf2_cu *);
01773 
01774 static void create_all_comp_units (struct objfile *);
01775 
01776 static int create_all_type_units (struct objfile *);
01777 
01778 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
01779                                  enum language);
01780 
01781 static void process_full_comp_unit (struct dwarf2_per_cu_data *,
01782                                     enum language);
01783 
01784 static void process_full_type_unit (struct dwarf2_per_cu_data *,
01785                                     enum language);
01786 
01787 static void dwarf2_add_dependence (struct dwarf2_cu *,
01788                                    struct dwarf2_per_cu_data *);
01789 
01790 static void dwarf2_mark (struct dwarf2_cu *);
01791 
01792 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
01793 
01794 static struct type *get_die_type_at_offset (sect_offset,
01795                                             struct dwarf2_per_cu_data *);
01796 
01797 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
01798 
01799 static void dwarf2_release_queue (void *dummy);
01800 
01801 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
01802                              enum language pretend_language);
01803 
01804 static void process_queue (void);
01805 
01806 static void find_file_and_directory (struct die_info *die,
01807                                      struct dwarf2_cu *cu,
01808                                      const char **name, const char **comp_dir);
01809 
01810 static char *file_full_name (int file, struct line_header *lh,
01811                              const char *comp_dir);
01812 
01813 static const gdb_byte *read_and_check_comp_unit_head
01814   (struct comp_unit_head *header,
01815    struct dwarf2_section_info *section,
01816    struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
01817    int is_debug_types_section);
01818 
01819 static void init_cutu_and_read_dies
01820   (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
01821    int use_existing_cu, int keep,
01822    die_reader_func_ftype *die_reader_func, void *data);
01823 
01824 static void init_cutu_and_read_dies_simple
01825   (struct dwarf2_per_cu_data *this_cu,
01826    die_reader_func_ftype *die_reader_func, void *data);
01827 
01828 static htab_t allocate_signatured_type_table (struct objfile *objfile);
01829 
01830 static htab_t allocate_dwo_unit_table (struct objfile *objfile);
01831 
01832 static struct dwo_unit *lookup_dwo_unit_in_dwp
01833   (struct dwp_file *dwp_file, const char *comp_dir,
01834    ULONGEST signature, int is_debug_types);
01835 
01836 static struct dwp_file *get_dwp_file (void);
01837 
01838 static struct dwo_unit *lookup_dwo_comp_unit
01839   (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
01840 
01841 static struct dwo_unit *lookup_dwo_type_unit
01842   (struct signatured_type *, const char *, const char *);
01843 
01844 static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
01845 
01846 static void free_dwo_file_cleanup (void *);
01847 
01848 static void process_cu_includes (void);
01849 
01850 static void check_producer (struct dwarf2_cu *cu);
01851 
01852 /* Various complaints about symbol reading that don't abort the process.  */
01853 
01854 static void
01855 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
01856 {
01857   complaint (&symfile_complaints,
01858              _("statement list doesn't fit in .debug_line section"));
01859 }
01860 
01861 static void
01862 dwarf2_debug_line_missing_file_complaint (void)
01863 {
01864   complaint (&symfile_complaints,
01865              _(".debug_line section has line data without a file"));
01866 }
01867 
01868 static void
01869 dwarf2_debug_line_missing_end_sequence_complaint (void)
01870 {
01871   complaint (&symfile_complaints,
01872              _(".debug_line section has line "
01873                "program sequence without an end"));
01874 }
01875 
01876 static void
01877 dwarf2_complex_location_expr_complaint (void)
01878 {
01879   complaint (&symfile_complaints, _("location expression too complex"));
01880 }
01881 
01882 static void
01883 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
01884                                               int arg3)
01885 {
01886   complaint (&symfile_complaints,
01887              _("const value length mismatch for '%s', got %d, expected %d"),
01888              arg1, arg2, arg3);
01889 }
01890 
01891 static void
01892 dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
01893 {
01894   complaint (&symfile_complaints,
01895              _("debug info runs off end of %s section"
01896                " [in module %s]"),
01897              get_section_name (section),
01898              get_section_file_name (section));
01899 }
01900 
01901 static void
01902 dwarf2_macro_malformed_definition_complaint (const char *arg1)
01903 {
01904   complaint (&symfile_complaints,
01905              _("macro debug info contains a "
01906                "malformed macro definition:\n`%s'"),
01907              arg1);
01908 }
01909 
01910 static void
01911 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
01912 {
01913   complaint (&symfile_complaints,
01914              _("invalid attribute class or form for '%s' in '%s'"),
01915              arg1, arg2);
01916 }
01917 
01918 #if WORDS_BIGENDIAN
01919 
01920 /* Convert VALUE between big- and little-endian.  */
01921 static offset_type
01922 byte_swap (offset_type value)
01923 {
01924   offset_type result;
01925 
01926   result = (value & 0xff) << 24;
01927   result |= (value & 0xff00) << 8;
01928   result |= (value & 0xff0000) >> 8;
01929   result |= (value & 0xff000000) >> 24;
01930   return result;
01931 }
01932 
01933 #define MAYBE_SWAP(V)  byte_swap (V)
01934 
01935 #else
01936 #define MAYBE_SWAP(V) (V)
01937 #endif /* WORDS_BIGENDIAN */
01938 
01939 /* The suffix for an index file.  */
01940 #define INDEX_SUFFIX ".gdb-index"
01941 
01942 /* Try to locate the sections we need for DWARF 2 debugging
01943    information and return true if we have enough to do something.
01944    NAMES points to the dwarf2 section names, or is NULL if the standard
01945    ELF names are used.  */
01946 
01947 int
01948 dwarf2_has_info (struct objfile *objfile,
01949                  const struct dwarf2_debug_sections *names)
01950 {
01951   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
01952   if (!dwarf2_per_objfile)
01953     {
01954       /* Initialize per-objfile state.  */
01955       struct dwarf2_per_objfile *data
01956         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
01957 
01958       memset (data, 0, sizeof (*data));
01959       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
01960       dwarf2_per_objfile = data;
01961 
01962       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
01963                              (void *) names);
01964       dwarf2_per_objfile->objfile = objfile;
01965     }
01966   return (!dwarf2_per_objfile->info.is_virtual
01967           && dwarf2_per_objfile->info.s.asection != NULL
01968           && !dwarf2_per_objfile->abbrev.is_virtual
01969           && dwarf2_per_objfile->abbrev.s.asection != NULL);
01970 }
01971 
01972 /* Return the containing section of virtual section SECTION.  */
01973 
01974 static struct dwarf2_section_info *
01975 get_containing_section (const struct dwarf2_section_info *section)
01976 {
01977   gdb_assert (section->is_virtual);
01978   return section->s.containing_section;
01979 }
01980 
01981 /* Return the bfd owner of SECTION.  */
01982 
01983 static struct bfd *
01984 get_section_bfd_owner (const struct dwarf2_section_info *section)
01985 {
01986   if (section->is_virtual)
01987     {
01988       section = get_containing_section (section);
01989       gdb_assert (!section->is_virtual);
01990     }
01991   return section->s.asection->owner;
01992 }
01993 
01994 /* Return the bfd section of SECTION.
01995    Returns NULL if the section is not present.  */
01996 
01997 static asection *
01998 get_section_bfd_section (const struct dwarf2_section_info *section)
01999 {
02000   if (section->is_virtual)
02001     {
02002       section = get_containing_section (section);
02003       gdb_assert (!section->is_virtual);
02004     }
02005   return section->s.asection;
02006 }
02007 
02008 /* Return the name of SECTION.  */
02009 
02010 static const char *
02011 get_section_name (const struct dwarf2_section_info *section)
02012 {
02013   asection *sectp = get_section_bfd_section (section);
02014 
02015   gdb_assert (sectp != NULL);
02016   return bfd_section_name (get_section_bfd_owner (section), sectp);
02017 }
02018 
02019 /* Return the name of the file SECTION is in.  */
02020 
02021 static const char *
02022 get_section_file_name (const struct dwarf2_section_info *section)
02023 {
02024   bfd *abfd = get_section_bfd_owner (section);
02025 
02026   return bfd_get_filename (abfd);
02027 }
02028 
02029 /* Return the id of SECTION.
02030    Returns 0 if SECTION doesn't exist.  */
02031 
02032 static int
02033 get_section_id (const struct dwarf2_section_info *section)
02034 {
02035   asection *sectp = get_section_bfd_section (section);
02036 
02037   if (sectp == NULL)
02038     return 0;
02039   return sectp->id;
02040 }
02041 
02042 /* Return the flags of SECTION.
02043    SECTION (or containing section if this is a virtual section) must exist.  */
02044 
02045 static int
02046 get_section_flags (const struct dwarf2_section_info *section)
02047 {
02048   asection *sectp = get_section_bfd_section (section);
02049 
02050   gdb_assert (sectp != NULL);
02051   return bfd_get_section_flags (sectp->owner, sectp);
02052 }
02053 
02054 /* When loading sections, we look either for uncompressed section or for
02055    compressed section names.  */
02056 
02057 static int
02058 section_is_p (const char *section_name,
02059               const struct dwarf2_section_names *names)
02060 {
02061   if (names->normal != NULL
02062       && strcmp (section_name, names->normal) == 0)
02063     return 1;
02064   if (names->compressed != NULL
02065       && strcmp (section_name, names->compressed) == 0)
02066     return 1;
02067   return 0;
02068 }
02069 
02070 /* This function is mapped across the sections and remembers the
02071    offset and size of each of the debugging sections we are interested
02072    in.  */
02073 
02074 static void
02075 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
02076 {
02077   const struct dwarf2_debug_sections *names;
02078   flagword aflag = bfd_get_section_flags (abfd, sectp);
02079 
02080   if (vnames == NULL)
02081     names = &dwarf2_elf_names;
02082   else
02083     names = (const struct dwarf2_debug_sections *) vnames;
02084 
02085   if ((aflag & SEC_HAS_CONTENTS) == 0)
02086     {
02087     }
02088   else if (section_is_p (sectp->name, &names->info))
02089     {
02090       dwarf2_per_objfile->info.s.asection = sectp;
02091       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
02092     }
02093   else if (section_is_p (sectp->name, &names->abbrev))
02094     {
02095       dwarf2_per_objfile->abbrev.s.asection = sectp;
02096       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
02097     }
02098   else if (section_is_p (sectp->name, &names->line))
02099     {
02100       dwarf2_per_objfile->line.s.asection = sectp;
02101       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
02102     }
02103   else if (section_is_p (sectp->name, &names->loc))
02104     {
02105       dwarf2_per_objfile->loc.s.asection = sectp;
02106       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
02107     }
02108   else if (section_is_p (sectp->name, &names->macinfo))
02109     {
02110       dwarf2_per_objfile->macinfo.s.asection = sectp;
02111       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
02112     }
02113   else if (section_is_p (sectp->name, &names->macro))
02114     {
02115       dwarf2_per_objfile->macro.s.asection = sectp;
02116       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
02117     }
02118   else if (section_is_p (sectp->name, &names->str))
02119     {
02120       dwarf2_per_objfile->str.s.asection = sectp;
02121       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
02122     }
02123   else if (section_is_p (sectp->name, &names->addr))
02124     {
02125       dwarf2_per_objfile->addr.s.asection = sectp;
02126       dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
02127     }
02128   else if (section_is_p (sectp->name, &names->frame))
02129     {
02130       dwarf2_per_objfile->frame.s.asection = sectp;
02131       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
02132     }
02133   else if (section_is_p (sectp->name, &names->eh_frame))
02134     {
02135       dwarf2_per_objfile->eh_frame.s.asection = sectp;
02136       dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
02137     }
02138   else if (section_is_p (sectp->name, &names->ranges))
02139     {
02140       dwarf2_per_objfile->ranges.s.asection = sectp;
02141       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
02142     }
02143   else if (section_is_p (sectp->name, &names->types))
02144     {
02145       struct dwarf2_section_info type_section;
02146 
02147       memset (&type_section, 0, sizeof (type_section));
02148       type_section.s.asection = sectp;
02149       type_section.size = bfd_get_section_size (sectp);
02150 
02151       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
02152                      &type_section);
02153     }
02154   else if (section_is_p (sectp->name, &names->gdb_index))
02155     {
02156       dwarf2_per_objfile->gdb_index.s.asection = sectp;
02157       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
02158     }
02159 
02160   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
02161       && bfd_section_vma (abfd, sectp) == 0)
02162     dwarf2_per_objfile->has_section_at_zero = 1;
02163 }
02164 
02165 /* A helper function that decides whether a section is empty,
02166    or not present.  */
02167 
02168 static int
02169 dwarf2_section_empty_p (const struct dwarf2_section_info *section)
02170 {
02171   if (section->is_virtual)
02172     return section->size == 0;
02173   return section->s.asection == NULL || section->size == 0;
02174 }
02175 
02176 /* Read the contents of the section INFO.
02177    OBJFILE is the main object file, but not necessarily the file where
02178    the section comes from.  E.g., for DWO files the bfd of INFO is the bfd
02179    of the DWO file.
02180    If the section is compressed, uncompress it before returning.  */
02181 
02182 static void
02183 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
02184 {
02185   asection *sectp;
02186   bfd *abfd;
02187   gdb_byte *buf, *retbuf;
02188 
02189   if (info->readin)
02190     return;
02191   info->buffer = NULL;
02192   info->readin = 1;
02193 
02194   if (dwarf2_section_empty_p (info))
02195     return;
02196 
02197   sectp = get_section_bfd_section (info);
02198 
02199   /* If this is a virtual section we need to read in the real one first.  */
02200   if (info->is_virtual)
02201     {
02202       struct dwarf2_section_info *containing_section =
02203         get_containing_section (info);
02204 
02205       gdb_assert (sectp != NULL);
02206       if ((sectp->flags & SEC_RELOC) != 0)
02207         {
02208           error (_("Dwarf Error: DWP format V2 with relocations is not"
02209                    " supported in section %s [in module %s]"),
02210                  get_section_name (info), get_section_file_name (info));
02211         }
02212       dwarf2_read_section (objfile, containing_section);
02213       /* Other code should have already caught virtual sections that don't
02214          fit.  */
02215       gdb_assert (info->virtual_offset + info->size
02216                   <= containing_section->size);
02217       /* If the real section is empty or there was a problem reading the
02218          section we shouldn't get here.  */
02219       gdb_assert (containing_section->buffer != NULL);
02220       info->buffer = containing_section->buffer + info->virtual_offset;
02221       return;
02222     }
02223 
02224   /* If the section has relocations, we must read it ourselves.
02225      Otherwise we attach it to the BFD.  */
02226   if ((sectp->flags & SEC_RELOC) == 0)
02227     {
02228       info->buffer = gdb_bfd_map_section (sectp, &info->size);
02229       return;
02230     }
02231 
02232   buf = obstack_alloc (&objfile->objfile_obstack, info->size);
02233   info->buffer = buf;
02234 
02235   /* When debugging .o files, we may need to apply relocations; see
02236      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
02237      We never compress sections in .o files, so we only need to
02238      try this when the section is not compressed.  */
02239   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
02240   if (retbuf != NULL)
02241     {
02242       info->buffer = retbuf;
02243       return;
02244     }
02245 
02246   abfd = get_section_bfd_owner (info);
02247   gdb_assert (abfd != NULL);
02248 
02249   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
02250       || bfd_bread (buf, info->size, abfd) != info->size)
02251     {
02252       error (_("Dwarf Error: Can't read DWARF data"
02253                " in section %s [in module %s]"),
02254              bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
02255     }
02256 }
02257 
02258 /* A helper function that returns the size of a section in a safe way.
02259    If you are positive that the section has been read before using the
02260    size, then it is safe to refer to the dwarf2_section_info object's
02261    "size" field directly.  In other cases, you must call this
02262    function, because for compressed sections the size field is not set
02263    correctly until the section has been read.  */
02264 
02265 static bfd_size_type
02266 dwarf2_section_size (struct objfile *objfile,
02267                      struct dwarf2_section_info *info)
02268 {
02269   if (!info->readin)
02270     dwarf2_read_section (objfile, info);
02271   return info->size;
02272 }
02273 
02274 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
02275    SECTION_NAME.  */
02276 
02277 void
02278 dwarf2_get_section_info (struct objfile *objfile,
02279                          enum dwarf2_section_enum sect,
02280                          asection **sectp, const gdb_byte **bufp,
02281                          bfd_size_type *sizep)
02282 {
02283   struct dwarf2_per_objfile *data
02284     = objfile_data (objfile, dwarf2_objfile_data_key);
02285   struct dwarf2_section_info *info;
02286 
02287   /* We may see an objfile without any DWARF, in which case we just
02288      return nothing.  */
02289   if (data == NULL)
02290     {
02291       *sectp = NULL;
02292       *bufp = NULL;
02293       *sizep = 0;
02294       return;
02295     }
02296   switch (sect)
02297     {
02298     case DWARF2_DEBUG_FRAME:
02299       info = &data->frame;
02300       break;
02301     case DWARF2_EH_FRAME:
02302       info = &data->eh_frame;
02303       break;
02304     default:
02305       gdb_assert_not_reached ("unexpected section");
02306     }
02307 
02308   dwarf2_read_section (objfile, info);
02309 
02310   *sectp = get_section_bfd_section (info);
02311   *bufp = info->buffer;
02312   *sizep = info->size;
02313 }
02314 
02315 /* A helper function to find the sections for a .dwz file.  */
02316 
02317 static void
02318 locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
02319 {
02320   struct dwz_file *dwz_file = arg;
02321 
02322   /* Note that we only support the standard ELF names, because .dwz
02323      is ELF-only (at the time of writing).  */
02324   if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
02325     {
02326       dwz_file->abbrev.s.asection = sectp;
02327       dwz_file->abbrev.size = bfd_get_section_size (sectp);
02328     }
02329   else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
02330     {
02331       dwz_file->info.s.asection = sectp;
02332       dwz_file->info.size = bfd_get_section_size (sectp);
02333     }
02334   else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
02335     {
02336       dwz_file->str.s.asection = sectp;
02337       dwz_file->str.size = bfd_get_section_size (sectp);
02338     }
02339   else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
02340     {
02341       dwz_file->line.s.asection = sectp;
02342       dwz_file->line.size = bfd_get_section_size (sectp);
02343     }
02344   else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
02345     {
02346       dwz_file->macro.s.asection = sectp;
02347       dwz_file->macro.size = bfd_get_section_size (sectp);
02348     }
02349   else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
02350     {
02351       dwz_file->gdb_index.s.asection = sectp;
02352       dwz_file->gdb_index.size = bfd_get_section_size (sectp);
02353     }
02354 }
02355 
02356 /* Open the separate '.dwz' debug file, if needed.  Return NULL if
02357    there is no .gnu_debugaltlink section in the file.  Error if there
02358    is such a section but the file cannot be found.  */
02359 
02360 static struct dwz_file *
02361 dwarf2_get_dwz_file (void)
02362 {
02363   bfd *dwz_bfd;
02364   char *data;
02365   struct cleanup *cleanup;
02366   const char *filename;
02367   struct dwz_file *result;
02368   bfd_size_type buildid_len_arg;
02369   size_t buildid_len;
02370   bfd_byte *buildid;
02371 
02372   if (dwarf2_per_objfile->dwz_file != NULL)
02373     return dwarf2_per_objfile->dwz_file;
02374 
02375   bfd_set_error (bfd_error_no_error);
02376   data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
02377                                       &buildid_len_arg, &buildid);
02378   if (data == NULL)
02379     {
02380       if (bfd_get_error () == bfd_error_no_error)
02381         return NULL;
02382       error (_("could not read '.gnu_debugaltlink' section: %s"),
02383              bfd_errmsg (bfd_get_error ()));
02384     }
02385   cleanup = make_cleanup (xfree, data);
02386   make_cleanup (xfree, buildid);
02387 
02388   buildid_len = (size_t) buildid_len_arg;
02389 
02390   filename = (const char *) data;
02391   if (!IS_ABSOLUTE_PATH (filename))
02392     {
02393       char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
02394       char *rel;
02395 
02396       make_cleanup (xfree, abs);
02397       abs = ldirname (abs);
02398       make_cleanup (xfree, abs);
02399 
02400       rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
02401       make_cleanup (xfree, rel);
02402       filename = rel;
02403     }
02404 
02405   /* First try the file name given in the section.  If that doesn't
02406      work, try to use the build-id instead.  */
02407   dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
02408   if (dwz_bfd != NULL)
02409     {
02410       if (!build_id_verify (dwz_bfd, buildid_len, buildid))
02411         {
02412           gdb_bfd_unref (dwz_bfd);
02413           dwz_bfd = NULL;
02414         }
02415     }
02416 
02417   if (dwz_bfd == NULL)
02418     dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
02419 
02420   if (dwz_bfd == NULL)
02421     error (_("could not find '.gnu_debugaltlink' file for %s"),
02422            objfile_name (dwarf2_per_objfile->objfile));
02423 
02424   result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
02425                            struct dwz_file);
02426   result->dwz_bfd = dwz_bfd;
02427 
02428   bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);
02429 
02430   do_cleanups (cleanup);
02431 
02432   dwarf2_per_objfile->dwz_file = result;
02433   return result;
02434 }
02435 
02436 /* DWARF quick_symbols_functions support.  */
02437 
02438 /* TUs can share .debug_line entries, and there can be a lot more TUs than
02439    unique line tables, so we maintain a separate table of all .debug_line
02440    derived entries to support the sharing.
02441    All the quick functions need is the list of file names.  We discard the
02442    line_header when we're done and don't need to record it here.  */
02443 struct quick_file_names
02444 {
02445   /* The data used to construct the hash key.  */
02446   struct stmt_list_hash hash;
02447 
02448   /* The number of entries in file_names, real_names.  */
02449   unsigned int num_file_names;
02450 
02451   /* The file names from the line table, after being run through
02452      file_full_name.  */
02453   const char **file_names;
02454 
02455   /* The file names from the line table after being run through
02456      gdb_realpath.  These are computed lazily.  */
02457   const char **real_names;
02458 };
02459 
02460 /* When using the index (and thus not using psymtabs), each CU has an
02461    object of this type.  This is used to hold information needed by
02462    the various "quick" methods.  */
02463 struct dwarf2_per_cu_quick_data
02464 {
02465   /* The file table.  This can be NULL if there was no file table
02466      or it's currently not read in.
02467      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
02468   struct quick_file_names *file_names;
02469 
02470   /* The corresponding symbol table.  This is NULL if symbols for this
02471      CU have not yet been read.  */
02472   struct symtab *symtab;
02473 
02474   /* A temporary mark bit used when iterating over all CUs in
02475      expand_symtabs_matching.  */
02476   unsigned int mark : 1;
02477 
02478   /* True if we've tried to read the file table and found there isn't one.
02479      There will be no point in trying to read it again next time.  */
02480   unsigned int no_file_data : 1;
02481 };
02482 
02483 /* Utility hash function for a stmt_list_hash.  */
02484 
02485 static hashval_t
02486 hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
02487 {
02488   hashval_t v = 0;
02489 
02490   if (stmt_list_hash->dwo_unit != NULL)
02491     v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
02492   v += stmt_list_hash->line_offset.sect_off;
02493   return v;
02494 }
02495 
02496 /* Utility equality function for a stmt_list_hash.  */
02497 
02498 static int
02499 eq_stmt_list_entry (const struct stmt_list_hash *lhs,
02500                     const struct stmt_list_hash *rhs)
02501 {
02502   if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
02503     return 0;
02504   if (lhs->dwo_unit != NULL
02505       && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
02506     return 0;
02507 
02508   return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
02509 }
02510 
02511 /* Hash function for a quick_file_names.  */
02512 
02513 static hashval_t
02514 hash_file_name_entry (const void *e)
02515 {
02516   const struct quick_file_names *file_data = e;
02517 
02518   return hash_stmt_list_entry (&file_data->hash);
02519 }
02520 
02521 /* Equality function for a quick_file_names.  */
02522 
02523 static int
02524 eq_file_name_entry (const void *a, const void *b)
02525 {
02526   const struct quick_file_names *ea = a;
02527   const struct quick_file_names *eb = b;
02528 
02529   return eq_stmt_list_entry (&ea->hash, &eb->hash);
02530 }
02531 
02532 /* Delete function for a quick_file_names.  */
02533 
02534 static void
02535 delete_file_name_entry (void *e)
02536 {
02537   struct quick_file_names *file_data = e;
02538   int i;
02539 
02540   for (i = 0; i < file_data->num_file_names; ++i)
02541     {
02542       xfree ((void*) file_data->file_names[i]);
02543       if (file_data->real_names)
02544         xfree ((void*) file_data->real_names[i]);
02545     }
02546 
02547   /* The space for the struct itself lives on objfile_obstack,
02548      so we don't free it here.  */
02549 }
02550 
02551 /* Create a quick_file_names hash table.  */
02552 
02553 static htab_t
02554 create_quick_file_names_table (unsigned int nr_initial_entries)
02555 {
02556   return htab_create_alloc (nr_initial_entries,
02557                             hash_file_name_entry, eq_file_name_entry,
02558                             delete_file_name_entry, xcalloc, xfree);
02559 }
02560 
02561 /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
02562    have to be created afterwards.  You should call age_cached_comp_units after
02563    processing PER_CU->CU.  dw2_setup must have been already called.  */
02564 
02565 static void
02566 load_cu (struct dwarf2_per_cu_data *per_cu)
02567 {
02568   if (per_cu->is_debug_types)
02569     load_full_type_unit (per_cu);
02570   else
02571     load_full_comp_unit (per_cu, language_minimal);
02572 
02573   gdb_assert (per_cu->cu != NULL);
02574 
02575   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
02576 }
02577 
02578 /* Read in the symbols for PER_CU.  */
02579 
02580 static void
02581 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
02582 {
02583   struct cleanup *back_to;
02584 
02585   /* Skip type_unit_groups, reading the type units they contain
02586      is handled elsewhere.  */
02587   if (IS_TYPE_UNIT_GROUP (per_cu))
02588     return;
02589 
02590   back_to = make_cleanup (dwarf2_release_queue, NULL);
02591 
02592   if (dwarf2_per_objfile->using_index
02593       ? per_cu->v.quick->symtab == NULL
02594       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
02595     {
02596       queue_comp_unit (per_cu, language_minimal);
02597       load_cu (per_cu);
02598 
02599       /* If we just loaded a CU from a DWO, and we're working with an index
02600          that may badly handle TUs, load all the TUs in that DWO as well.
02601          http://sourceware.org/bugzilla/show_bug.cgi?id=15021  */
02602       if (!per_cu->is_debug_types
02603           && per_cu->cu->dwo_unit != NULL
02604           && dwarf2_per_objfile->index_table != NULL
02605           && dwarf2_per_objfile->index_table->version <= 7
02606           /* DWP files aren't supported yet.  */
02607           && get_dwp_file () == NULL)
02608         queue_and_load_all_dwo_tus (per_cu);
02609     }
02610 
02611   process_queue ();
02612 
02613   /* Age the cache, releasing compilation units that have not
02614      been used recently.  */
02615   age_cached_comp_units ();
02616 
02617   do_cleanups (back_to);
02618 }
02619 
02620 /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
02621    the objfile from which this CU came.  Returns the resulting symbol
02622    table.  */
02623 
02624 static struct symtab *
02625 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
02626 {
02627   gdb_assert (dwarf2_per_objfile->using_index);
02628   if (!per_cu->v.quick->symtab)
02629     {
02630       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
02631       increment_reading_symtab ();
02632       dw2_do_instantiate_symtab (per_cu);
02633       process_cu_includes ();
02634       do_cleanups (back_to);
02635     }
02636   return per_cu->v.quick->symtab;
02637 }
02638 
02639 /* Return the CU given its index.
02640 
02641    This is intended for loops like:
02642 
02643    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
02644                     + dwarf2_per_objfile->n_type_units); ++i)
02645      {
02646        struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
02647 
02648        ...;
02649      }
02650 */
02651 
02652 static struct dwarf2_per_cu_data *
02653 dw2_get_cu (int index)
02654 {
02655   if (index >= dwarf2_per_objfile->n_comp_units)
02656     {
02657       index -= dwarf2_per_objfile->n_comp_units;
02658       gdb_assert (index < dwarf2_per_objfile->n_type_units);
02659       return &dwarf2_per_objfile->all_type_units[index]->per_cu;
02660     }
02661 
02662   return dwarf2_per_objfile->all_comp_units[index];
02663 }
02664 
02665 /* Return the primary CU given its index.
02666    The difference between this function and dw2_get_cu is in the handling
02667    of type units (TUs).  Here we return the type_unit_group object.
02668 
02669    This is intended for loops like:
02670 
02671    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
02672                     + dwarf2_per_objfile->n_type_unit_groups); ++i)
02673      {
02674        struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
02675 
02676        ...;
02677      }
02678 */
02679 
02680 static struct dwarf2_per_cu_data *
02681 dw2_get_primary_cu (int index)
02682 {
02683   if (index >= dwarf2_per_objfile->n_comp_units)
02684     {
02685       index -= dwarf2_per_objfile->n_comp_units;
02686       gdb_assert (index < dwarf2_per_objfile->n_type_unit_groups);
02687       return &dwarf2_per_objfile->all_type_unit_groups[index]->per_cu;
02688     }
02689 
02690   return dwarf2_per_objfile->all_comp_units[index];
02691 }
02692 
02693 /* A helper for create_cus_from_index that handles a given list of
02694    CUs.  */
02695 
02696 static void
02697 create_cus_from_index_list (struct objfile *objfile,
02698                             const gdb_byte *cu_list, offset_type n_elements,
02699                             struct dwarf2_section_info *section,
02700                             int is_dwz,
02701                             int base_offset)
02702 {
02703   offset_type i;
02704 
02705   for (i = 0; i < n_elements; i += 2)
02706     {
02707       struct dwarf2_per_cu_data *the_cu;
02708       ULONGEST offset, length;
02709 
02710       gdb_static_assert (sizeof (ULONGEST) >= 8);
02711       offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
02712       length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
02713       cu_list += 2 * 8;
02714 
02715       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
02716                                struct dwarf2_per_cu_data);
02717       the_cu->offset.sect_off = offset;
02718       the_cu->length = length;
02719       the_cu->objfile = objfile;
02720       the_cu->section = section;
02721       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
02722                                         struct dwarf2_per_cu_quick_data);
02723       the_cu->is_dwz = is_dwz;
02724       dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
02725     }
02726 }
02727 
02728 /* Read the CU list from the mapped index, and use it to create all
02729    the CU objects for this objfile.  */
02730 
02731 static void
02732 create_cus_from_index (struct objfile *objfile,
02733                        const gdb_byte *cu_list, offset_type cu_list_elements,
02734                        const gdb_byte *dwz_list, offset_type dwz_elements)
02735 {
02736   struct dwz_file *dwz;
02737 
02738   dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
02739   dwarf2_per_objfile->all_comp_units
02740     = obstack_alloc (&objfile->objfile_obstack,
02741                      dwarf2_per_objfile->n_comp_units
02742                      * sizeof (struct dwarf2_per_cu_data *));
02743 
02744   create_cus_from_index_list (objfile, cu_list, cu_list_elements,
02745                               &dwarf2_per_objfile->info, 0, 0);
02746 
02747   if (dwz_elements == 0)
02748     return;
02749 
02750   dwz = dwarf2_get_dwz_file ();
02751   create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
02752                               cu_list_elements / 2);
02753 }
02754 
02755 /* Create the signatured type hash table from the index.  */
02756 
02757 static void
02758 create_signatured_type_table_from_index (struct objfile *objfile,
02759                                          struct dwarf2_section_info *section,
02760                                          const gdb_byte *bytes,
02761                                          offset_type elements)
02762 {
02763   offset_type i;
02764   htab_t sig_types_hash;
02765 
02766   dwarf2_per_objfile->n_type_units = elements / 3;
02767   dwarf2_per_objfile->all_type_units
02768     = xmalloc (dwarf2_per_objfile->n_type_units
02769                * sizeof (struct signatured_type *));
02770 
02771   sig_types_hash = allocate_signatured_type_table (objfile);
02772 
02773   for (i = 0; i < elements; i += 3)
02774     {
02775       struct signatured_type *sig_type;
02776       ULONGEST offset, type_offset_in_tu, signature;
02777       void **slot;
02778 
02779       gdb_static_assert (sizeof (ULONGEST) >= 8);
02780       offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
02781       type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
02782                                                     BFD_ENDIAN_LITTLE);
02783       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
02784       bytes += 3 * 8;
02785 
02786       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
02787                                  struct signatured_type);
02788       sig_type->signature = signature;
02789       sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
02790       sig_type->per_cu.is_debug_types = 1;
02791       sig_type->per_cu.section = section;
02792       sig_type->per_cu.offset.sect_off = offset;
02793       sig_type->per_cu.objfile = objfile;
02794       sig_type->per_cu.v.quick
02795         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
02796                           struct dwarf2_per_cu_quick_data);
02797 
02798       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
02799       *slot = sig_type;
02800 
02801       dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
02802     }
02803 
02804   dwarf2_per_objfile->signatured_types = sig_types_hash;
02805 }
02806 
02807 /* Read the address map data from the mapped index, and use it to
02808    populate the objfile's psymtabs_addrmap.  */
02809 
02810 static void
02811 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
02812 {
02813   const gdb_byte *iter, *end;
02814   struct obstack temp_obstack;
02815   struct addrmap *mutable_map;
02816   struct cleanup *cleanup;
02817   CORE_ADDR baseaddr;
02818 
02819   obstack_init (&temp_obstack);
02820   cleanup = make_cleanup_obstack_free (&temp_obstack);
02821   mutable_map = addrmap_create_mutable (&temp_obstack);
02822 
02823   iter = index->address_table;
02824   end = iter + index->address_table_size;
02825 
02826   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
02827 
02828   while (iter < end)
02829     {
02830       ULONGEST hi, lo, cu_index;
02831       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
02832       iter += 8;
02833       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
02834       iter += 8;
02835       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
02836       iter += 4;
02837 
02838       if (lo > hi)
02839         {
02840           complaint (&symfile_complaints,
02841                      _(".gdb_index address table has invalid range (%s - %s)"),
02842                      hex_string (lo), hex_string (hi));
02843           continue;
02844         }
02845 
02846       if (cu_index >= dwarf2_per_objfile->n_comp_units)
02847         {
02848           complaint (&symfile_complaints,
02849                      _(".gdb_index address table has invalid CU number %u"),
02850                      (unsigned) cu_index);
02851           continue;
02852         }
02853 
02854       addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
02855                          dw2_get_cu (cu_index));
02856     }
02857 
02858   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
02859                                                     &objfile->objfile_obstack);
02860   do_cleanups (cleanup);
02861 }
02862 
02863 /* The hash function for strings in the mapped index.  This is the same as
02864    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
02865    implementation.  This is necessary because the hash function is tied to the
02866    format of the mapped index file.  The hash values do not have to match with
02867    SYMBOL_HASH_NEXT.
02868    
02869    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */
02870 
02871 static hashval_t
02872 mapped_index_string_hash (int index_version, const void *p)
02873 {
02874   const unsigned char *str = (const unsigned char *) p;
02875   hashval_t r = 0;
02876   unsigned char c;
02877 
02878   while ((c = *str++) != 0)
02879     {
02880       if (index_version >= 5)
02881         c = tolower (c);
02882       r = r * 67 + c - 113;
02883     }
02884 
02885   return r;
02886 }
02887 
02888 /* Find a slot in the mapped index INDEX for the object named NAME.
02889    If NAME is found, set *VEC_OUT to point to the CU vector in the
02890    constant pool and return 1.  If NAME cannot be found, return 0.  */
02891 
02892 static int
02893 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
02894                           offset_type **vec_out)
02895 {
02896   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
02897   offset_type hash;
02898   offset_type slot, step;
02899   int (*cmp) (const char *, const char *);
02900 
02901   if (current_language->la_language == language_cplus
02902       || current_language->la_language == language_java
02903       || current_language->la_language == language_fortran)
02904     {
02905       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
02906          not contain any.  */
02907       const char *paren = strchr (name, '(');
02908 
02909       if (paren)
02910         {
02911           char *dup;
02912 
02913           dup = xmalloc (paren - name + 1);
02914           memcpy (dup, name, paren - name);
02915           dup[paren - name] = 0;
02916 
02917           make_cleanup (xfree, dup);
02918           name = dup;
02919         }
02920     }
02921 
02922   /* Index version 4 did not support case insensitive searches.  But the
02923      indices for case insensitive languages are built in lowercase, therefore
02924      simulate our NAME being searched is also lowercased.  */
02925   hash = mapped_index_string_hash ((index->version == 4
02926                                     && case_sensitivity == case_sensitive_off
02927                                     ? 5 : index->version),
02928                                    name);
02929 
02930   slot = hash & (index->symbol_table_slots - 1);
02931   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
02932   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
02933 
02934   for (;;)
02935     {
02936       /* Convert a slot number to an offset into the table.  */
02937       offset_type i = 2 * slot;
02938       const char *str;
02939       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
02940         {
02941           do_cleanups (back_to);
02942           return 0;
02943         }
02944 
02945       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
02946       if (!cmp (name, str))
02947         {
02948           *vec_out = (offset_type *) (index->constant_pool
02949                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
02950           do_cleanups (back_to);
02951           return 1;
02952         }
02953 
02954       slot = (slot + step) & (index->symbol_table_slots - 1);
02955     }
02956 }
02957 
02958 /* A helper function that reads the .gdb_index from SECTION and fills
02959    in MAP.  FILENAME is the name of the file containing the section;
02960    it is used for error reporting.  DEPRECATED_OK is nonzero if it is
02961    ok to use deprecated sections.
02962 
02963    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
02964    out parameters that are filled in with information about the CU and
02965    TU lists in the section.
02966 
02967    Returns 1 if all went well, 0 otherwise.  */
02968 
02969 static int
02970 read_index_from_section (struct objfile *objfile,
02971                          const char *filename,
02972                          int deprecated_ok,
02973                          struct dwarf2_section_info *section,
02974                          struct mapped_index *map,
02975                          const gdb_byte **cu_list,
02976                          offset_type *cu_list_elements,
02977                          const gdb_byte **types_list,
02978                          offset_type *types_list_elements)
02979 {
02980   const gdb_byte *addr;
02981   offset_type version;
02982   offset_type *metadata;
02983   int i;
02984 
02985   if (dwarf2_section_empty_p (section))
02986     return 0;
02987 
02988   /* Older elfutils strip versions could keep the section in the main
02989      executable while splitting it for the separate debug info file.  */
02990   if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
02991     return 0;
02992 
02993   dwarf2_read_section (objfile, section);
02994 
02995   addr = section->buffer;
02996   /* Version check.  */
02997   version = MAYBE_SWAP (*(offset_type *) addr);
02998   /* Versions earlier than 3 emitted every copy of a psymbol.  This
02999      causes the index to behave very poorly for certain requests.  Version 3
03000      contained incomplete addrmap.  So, it seems better to just ignore such
03001      indices.  */
03002   if (version < 4)
03003     {
03004       static int warning_printed = 0;
03005       if (!warning_printed)
03006         {
03007           warning (_("Skipping obsolete .gdb_index section in %s."),
03008                    filename);
03009           warning_printed = 1;
03010         }
03011       return 0;
03012     }
03013   /* Index version 4 uses a different hash function than index version
03014      5 and later.
03015 
03016      Versions earlier than 6 did not emit psymbols for inlined
03017      functions.  Using these files will cause GDB not to be able to
03018      set breakpoints on inlined functions by name, so we ignore these
03019      indices unless the user has done
03020      "set use-deprecated-index-sections on".  */
03021   if (version < 6 && !deprecated_ok)
03022     {
03023       static int warning_printed = 0;
03024       if (!warning_printed)
03025         {
03026           warning (_("\
03027 Skipping deprecated .gdb_index section in %s.\n\
03028 Do \"set use-deprecated-index-sections on\" before the file is read\n\
03029 to use the section anyway."),
03030                    filename);
03031           warning_printed = 1;
03032         }
03033       return 0;
03034     }
03035   /* Version 7 indices generated by gold refer to the CU for a symbol instead
03036      of the TU (for symbols coming from TUs).  It's just a performance bug, and
03037      we can't distinguish gdb-generated indices from gold-generated ones, so
03038      nothing to do here.  */
03039 
03040   /* Indexes with higher version than the one supported by GDB may be no
03041      longer backward compatible.  */
03042   if (version > 8)
03043     return 0;
03044 
03045   map->version = version;
03046   map->total_size = section->size;
03047 
03048   metadata = (offset_type *) (addr + sizeof (offset_type));
03049 
03050   i = 0;
03051   *cu_list = addr + MAYBE_SWAP (metadata[i]);
03052   *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
03053                        / 8);
03054   ++i;
03055 
03056   *types_list = addr + MAYBE_SWAP (metadata[i]);
03057   *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
03058                            - MAYBE_SWAP (metadata[i]))
03059                           / 8);
03060   ++i;
03061 
03062   map->address_table = addr + MAYBE_SWAP (metadata[i]);
03063   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
03064                              - MAYBE_SWAP (metadata[i]));
03065   ++i;
03066 
03067   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
03068   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
03069                               - MAYBE_SWAP (metadata[i]))
03070                              / (2 * sizeof (offset_type)));
03071   ++i;
03072 
03073   map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
03074 
03075   return 1;
03076 }
03077 
03078 
03079 /* Read the index file.  If everything went ok, initialize the "quick"
03080    elements of all the CUs and return 1.  Otherwise, return 0.  */
03081 
03082 static int
03083 dwarf2_read_index (struct objfile *objfile)
03084 {
03085   struct mapped_index local_map, *map;
03086   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
03087   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
03088   struct dwz_file *dwz;
03089 
03090   if (!read_index_from_section (objfile, objfile_name (objfile),
03091                                 use_deprecated_index_sections,
03092                                 &dwarf2_per_objfile->gdb_index, &local_map,
03093                                 &cu_list, &cu_list_elements,
03094                                 &types_list, &types_list_elements))
03095     return 0;
03096 
03097   /* Don't use the index if it's empty.  */
03098   if (local_map.symbol_table_slots == 0)
03099     return 0;
03100 
03101   /* If there is a .dwz file, read it so we can get its CU list as
03102      well.  */
03103   dwz = dwarf2_get_dwz_file ();
03104   if (dwz != NULL)
03105     {
03106       struct mapped_index dwz_map;
03107       const gdb_byte *dwz_types_ignore;
03108       offset_type dwz_types_elements_ignore;
03109 
03110       if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
03111                                     1,
03112                                     &dwz->gdb_index, &dwz_map,
03113                                     &dwz_list, &dwz_list_elements,
03114                                     &dwz_types_ignore,
03115                                     &dwz_types_elements_ignore))
03116         {
03117           warning (_("could not read '.gdb_index' section from %s; skipping"),
03118                    bfd_get_filename (dwz->dwz_bfd));
03119           return 0;
03120         }
03121     }
03122 
03123   create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
03124                          dwz_list_elements);
03125 
03126   if (types_list_elements)
03127     {
03128       struct dwarf2_section_info *section;
03129 
03130       /* We can only handle a single .debug_types when we have an
03131          index.  */
03132       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
03133         return 0;
03134 
03135       section = VEC_index (dwarf2_section_info_def,
03136                            dwarf2_per_objfile->types, 0);
03137 
03138       create_signatured_type_table_from_index (objfile, section, types_list,
03139                                                types_list_elements);
03140     }
03141 
03142   create_addrmap_from_index (objfile, &local_map);
03143 
03144   map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
03145   *map = local_map;
03146 
03147   dwarf2_per_objfile->index_table = map;
03148   dwarf2_per_objfile->using_index = 1;
03149   dwarf2_per_objfile->quick_file_names_table =
03150     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
03151 
03152   return 1;
03153 }
03154 
03155 /* A helper for the "quick" functions which sets the global
03156    dwarf2_per_objfile according to OBJFILE.  */
03157 
03158 static void
03159 dw2_setup (struct objfile *objfile)
03160 {
03161   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
03162   gdb_assert (dwarf2_per_objfile);
03163 }
03164 
03165 /* die_reader_func for dw2_get_file_names.  */
03166 
03167 static void
03168 dw2_get_file_names_reader (const struct die_reader_specs *reader,
03169                            const gdb_byte *info_ptr,
03170                            struct die_info *comp_unit_die,
03171                            int has_children,
03172                            void *data)
03173 {
03174   struct dwarf2_cu *cu = reader->cu;
03175   struct dwarf2_per_cu_data *this_cu = cu->per_cu;  
03176   struct objfile *objfile = dwarf2_per_objfile->objfile;
03177   struct dwarf2_per_cu_data *lh_cu;
03178   struct line_header *lh;
03179   struct attribute *attr;
03180   int i;
03181   const char *name, *comp_dir;
03182   void **slot;
03183   struct quick_file_names *qfn;
03184   unsigned int line_offset;
03185 
03186   gdb_assert (! this_cu->is_debug_types);
03187 
03188   /* Our callers never want to match partial units -- instead they
03189      will match the enclosing full CU.  */
03190   if (comp_unit_die->tag == DW_TAG_partial_unit)
03191     {
03192       this_cu->v.quick->no_file_data = 1;
03193       return;
03194     }
03195 
03196   lh_cu = this_cu;
03197   lh = NULL;
03198   slot = NULL;
03199   line_offset = 0;
03200 
03201   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
03202   if (attr)
03203     {
03204       struct quick_file_names find_entry;
03205 
03206       line_offset = DW_UNSND (attr);
03207 
03208       /* We may have already read in this line header (TU line header sharing).
03209          If we have we're done.  */
03210       find_entry.hash.dwo_unit = cu->dwo_unit;
03211       find_entry.hash.line_offset.sect_off = line_offset;
03212       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
03213                              &find_entry, INSERT);
03214       if (*slot != NULL)
03215         {
03216           lh_cu->v.quick->file_names = *slot;
03217           return;
03218         }
03219 
03220       lh = dwarf_decode_line_header (line_offset, cu);
03221     }
03222   if (lh == NULL)
03223     {
03224       lh_cu->v.quick->no_file_data = 1;
03225       return;
03226     }
03227 
03228   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
03229   qfn->hash.dwo_unit = cu->dwo_unit;
03230   qfn->hash.line_offset.sect_off = line_offset;
03231   gdb_assert (slot != NULL);
03232   *slot = qfn;
03233 
03234   find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);
03235 
03236   qfn->num_file_names = lh->num_file_names;
03237   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
03238                                    lh->num_file_names * sizeof (char *));
03239   for (i = 0; i < lh->num_file_names; ++i)
03240     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
03241   qfn->real_names = NULL;
03242 
03243   free_line_header (lh);
03244 
03245   lh_cu->v.quick->file_names = qfn;
03246 }
03247 
03248 /* A helper for the "quick" functions which attempts to read the line
03249    table for THIS_CU.  */
03250 
03251 static struct quick_file_names *
03252 dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
03253 {
03254   /* This should never be called for TUs.  */
03255   gdb_assert (! this_cu->is_debug_types);
03256   /* Nor type unit groups.  */
03257   gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));
03258 
03259   if (this_cu->v.quick->file_names != NULL)
03260     return this_cu->v.quick->file_names;
03261   /* If we know there is no line data, no point in looking again.  */
03262   if (this_cu->v.quick->no_file_data)
03263     return NULL;
03264 
03265   init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);
03266 
03267   if (this_cu->v.quick->no_file_data)
03268     return NULL;
03269   return this_cu->v.quick->file_names;
03270 }
03271 
03272 /* A helper for the "quick" functions which computes and caches the
03273    real path for a given file name from the line table.  */
03274 
03275 static const char *
03276 dw2_get_real_path (struct objfile *objfile,
03277                    struct quick_file_names *qfn, int index)
03278 {
03279   if (qfn->real_names == NULL)
03280     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
03281                                       qfn->num_file_names, sizeof (char *));
03282 
03283   if (qfn->real_names[index] == NULL)
03284     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
03285 
03286   return qfn->real_names[index];
03287 }
03288 
03289 static struct symtab *
03290 dw2_find_last_source_symtab (struct objfile *objfile)
03291 {
03292   int index;
03293 
03294   dw2_setup (objfile);
03295   index = dwarf2_per_objfile->n_comp_units - 1;
03296   return dw2_instantiate_symtab (dw2_get_cu (index));
03297 }
03298 
03299 /* Traversal function for dw2_forget_cached_source_info.  */
03300 
03301 static int
03302 dw2_free_cached_file_names (void **slot, void *info)
03303 {
03304   struct quick_file_names *file_data = (struct quick_file_names *) *slot;
03305 
03306   if (file_data->real_names)
03307     {
03308       int i;
03309 
03310       for (i = 0; i < file_data->num_file_names; ++i)
03311         {
03312           xfree ((void*) file_data->real_names[i]);
03313           file_data->real_names[i] = NULL;
03314         }
03315     }
03316 
03317   return 1;
03318 }
03319 
03320 static void
03321 dw2_forget_cached_source_info (struct objfile *objfile)
03322 {
03323   dw2_setup (objfile);
03324 
03325   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
03326                           dw2_free_cached_file_names, NULL);
03327 }
03328 
03329 /* Helper function for dw2_map_symtabs_matching_filename that expands
03330    the symtabs and calls the iterator.  */
03331 
03332 static int
03333 dw2_map_expand_apply (struct objfile *objfile,
03334                       struct dwarf2_per_cu_data *per_cu,
03335                       const char *name, const char *real_path,
03336                       int (*callback) (struct symtab *, void *),
03337                       void *data)
03338 {
03339   struct symtab *last_made = objfile->symtabs;
03340 
03341   /* Don't visit already-expanded CUs.  */
03342   if (per_cu->v.quick->symtab)
03343     return 0;
03344 
03345   /* This may expand more than one symtab, and we want to iterate over
03346      all of them.  */
03347   dw2_instantiate_symtab (per_cu);
03348 
03349   return iterate_over_some_symtabs (name, real_path, callback, data,
03350                                     objfile->symtabs, last_made);
03351 }
03352 
03353 /* Implementation of the map_symtabs_matching_filename method.  */
03354 
03355 static int
03356 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
03357                                    const char *real_path,
03358                                    int (*callback) (struct symtab *, void *),
03359                                    void *data)
03360 {
03361   int i;
03362   const char *name_basename = lbasename (name);
03363 
03364   dw2_setup (objfile);
03365 
03366   /* The rule is CUs specify all the files, including those used by
03367      any TU, so there's no need to scan TUs here.  */
03368 
03369   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
03370     {
03371       int j;
03372       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
03373       struct quick_file_names *file_data;
03374 
03375       /* We only need to look at symtabs not already expanded.  */
03376       if (per_cu->v.quick->symtab)
03377         continue;
03378 
03379       file_data = dw2_get_file_names (per_cu);
03380       if (file_data == NULL)
03381         continue;
03382 
03383       for (j = 0; j < file_data->num_file_names; ++j)
03384         {
03385           const char *this_name = file_data->file_names[j];
03386           const char *this_real_name;
03387 
03388           if (compare_filenames_for_search (this_name, name))
03389             {
03390               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
03391                                         callback, data))
03392                 return 1;
03393               continue;
03394             }
03395 
03396           /* Before we invoke realpath, which can get expensive when many
03397              files are involved, do a quick comparison of the basenames.  */
03398           if (! basenames_may_differ
03399               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
03400             continue;
03401 
03402           this_real_name = dw2_get_real_path (objfile, file_data, j);
03403           if (compare_filenames_for_search (this_real_name, name))
03404             {
03405               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
03406                                         callback, data))
03407                 return 1;
03408               continue;
03409             }
03410 
03411           if (real_path != NULL)
03412             {
03413               gdb_assert (IS_ABSOLUTE_PATH (real_path));
03414               gdb_assert (IS_ABSOLUTE_PATH (name));
03415               if (this_real_name != NULL
03416                   && FILENAME_CMP (real_path, this_real_name) == 0)
03417                 {
03418                   if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
03419                                             callback, data))
03420                     return 1;
03421                   continue;
03422                 }
03423             }
03424         }
03425     }
03426 
03427   return 0;
03428 }
03429 
03430 /* Struct used to manage iterating over all CUs looking for a symbol.  */
03431 
03432 struct dw2_symtab_iterator
03433 {
03434   /* The internalized form of .gdb_index.  */
03435   struct mapped_index *index;
03436   /* If non-zero, only look for symbols that match BLOCK_INDEX.  */
03437   int want_specific_block;
03438   /* One of GLOBAL_BLOCK or STATIC_BLOCK.
03439      Unused if !WANT_SPECIFIC_BLOCK.  */
03440   int block_index;
03441   /* The kind of symbol we're looking for.  */
03442   domain_enum domain;
03443   /* The list of CUs from the index entry of the symbol,
03444      or NULL if not found.  */
03445   offset_type *vec;
03446   /* The next element in VEC to look at.  */
03447   int next;
03448   /* The number of elements in VEC, or zero if there is no match.  */
03449   int length;
03450 };
03451 
03452 /* Initialize the index symtab iterator ITER.
03453    If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
03454    in block BLOCK_INDEX.  Otherwise BLOCK_INDEX is ignored.  */
03455 
03456 static void
03457 dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
03458                       struct mapped_index *index,
03459                       int want_specific_block,
03460                       int block_index,
03461                       domain_enum domain,
03462                       const char *name)
03463 {
03464   iter->index = index;
03465   iter->want_specific_block = want_specific_block;
03466   iter->block_index = block_index;
03467   iter->domain = domain;
03468   iter->next = 0;
03469 
03470   if (find_slot_in_mapped_hash (index, name, &iter->vec))
03471     iter->length = MAYBE_SWAP (*iter->vec);
03472   else
03473     {
03474       iter->vec = NULL;
03475       iter->length = 0;
03476     }
03477 }
03478 
03479 /* Return the next matching CU or NULL if there are no more.  */
03480 
03481 static struct dwarf2_per_cu_data *
03482 dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
03483 {
03484   for ( ; iter->next < iter->length; ++iter->next)
03485     {
03486       offset_type cu_index_and_attrs =
03487         MAYBE_SWAP (iter->vec[iter->next + 1]);
03488       offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
03489       struct dwarf2_per_cu_data *per_cu;
03490       int want_static = iter->block_index != GLOBAL_BLOCK;
03491       /* This value is only valid for index versions >= 7.  */
03492       int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
03493       gdb_index_symbol_kind symbol_kind =
03494         GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
03495       /* Only check the symbol attributes if they're present.
03496          Indices prior to version 7 don't record them,
03497          and indices >= 7 may elide them for certain symbols
03498          (gold does this).  */
03499       int attrs_valid =
03500         (iter->index->version >= 7
03501          && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
03502 
03503       /* Don't crash on bad data.  */
03504       if (cu_index >= (dwarf2_per_objfile->n_comp_units
03505                        + dwarf2_per_objfile->n_type_units))
03506         {
03507           complaint (&symfile_complaints,
03508                      _(".gdb_index entry has bad CU index"
03509                        " [in module %s]"),
03510                      objfile_name (dwarf2_per_objfile->objfile));
03511           continue;
03512         }
03513 
03514       per_cu = dw2_get_cu (cu_index);
03515 
03516       /* Skip if already read in.  */
03517       if (per_cu->v.quick->symtab)
03518         continue;
03519 
03520       if (attrs_valid
03521           && iter->want_specific_block
03522           && want_static != is_static)
03523         continue;
03524 
03525       /* Only check the symbol's kind if it has one.  */
03526       if (attrs_valid)
03527         {
03528           switch (iter->domain)
03529             {
03530             case VAR_DOMAIN:
03531               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
03532                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
03533                   /* Some types are also in VAR_DOMAIN.  */
03534                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
03535                 continue;
03536               break;
03537             case STRUCT_DOMAIN:
03538               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
03539                 continue;
03540               break;
03541             case LABEL_DOMAIN:
03542               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
03543                 continue;
03544               break;
03545             default:
03546               break;
03547             }
03548         }
03549 
03550       ++iter->next;
03551       return per_cu;
03552     }
03553 
03554   return NULL;
03555 }
03556 
03557 static struct symtab *
03558 dw2_lookup_symbol (struct objfile *objfile, int block_index,
03559                    const char *name, domain_enum domain)
03560 {
03561   struct symtab *stab_best = NULL;
03562   struct mapped_index *index;
03563 
03564   dw2_setup (objfile);
03565 
03566   index = dwarf2_per_objfile->index_table;
03567 
03568   /* index is NULL if OBJF_READNOW.  */
03569   if (index)
03570     {
03571       struct dw2_symtab_iterator iter;
03572       struct dwarf2_per_cu_data *per_cu;
03573 
03574       dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);
03575 
03576       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
03577         {
03578           struct symbol *sym = NULL;
03579           struct symtab *stab = dw2_instantiate_symtab (per_cu);
03580 
03581           /* Some caution must be observed with overloaded functions
03582              and methods, since the index will not contain any overload
03583              information (but NAME might contain it).  */
03584           if (stab->primary)
03585             {
03586               struct blockvector *bv = BLOCKVECTOR (stab);
03587               struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
03588 
03589               sym = lookup_block_symbol (block, name, domain);
03590             }
03591 
03592           if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
03593             {
03594               if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
03595                 return stab;
03596 
03597               stab_best = stab;
03598             }
03599 
03600           /* Keep looking through other CUs.  */
03601         }
03602     }
03603 
03604   return stab_best;
03605 }
03606 
03607 static void
03608 dw2_print_stats (struct objfile *objfile)
03609 {
03610   int i, total, count;
03611 
03612   dw2_setup (objfile);
03613   total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
03614   count = 0;
03615   for (i = 0; i < total; ++i)
03616     {
03617       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
03618 
03619       if (!per_cu->v.quick->symtab)
03620         ++count;
03621     }
03622   printf_filtered (_("  Number of read CUs: %d\n"), total - count);
03623   printf_filtered (_("  Number of unread CUs: %d\n"), count);
03624 }
03625 
03626 /* This dumps minimal information about the index.
03627    It is called via "mt print objfiles".
03628    One use is to verify .gdb_index has been loaded by the
03629    gdb.dwarf2/gdb-index.exp testcase.  */
03630 
03631 static void
03632 dw2_dump (struct objfile *objfile)
03633 {
03634   dw2_setup (objfile);
03635   gdb_assert (dwarf2_per_objfile->using_index);
03636   printf_filtered (".gdb_index:");
03637   if (dwarf2_per_objfile->index_table != NULL)
03638     {
03639       printf_filtered (" version %d\n",
03640                        dwarf2_per_objfile->index_table->version);
03641     }
03642   else
03643     printf_filtered (" faked for \"readnow\"\n");
03644   printf_filtered ("\n");
03645 }
03646 
03647 static void
03648 dw2_relocate (struct objfile *objfile,
03649               const struct section_offsets *new_offsets,
03650               const struct section_offsets *delta)
03651 {
03652   /* There's nothing to relocate here.  */
03653 }
03654 
03655 static void
03656 dw2_expand_symtabs_for_function (struct objfile *objfile,
03657                                  const char *func_name)
03658 {
03659   struct mapped_index *index;
03660 
03661   dw2_setup (objfile);
03662 
03663   index = dwarf2_per_objfile->index_table;
03664 
03665   /* index is NULL if OBJF_READNOW.  */
03666   if (index)
03667     {
03668       struct dw2_symtab_iterator iter;
03669       struct dwarf2_per_cu_data *per_cu;
03670 
03671       /* Note: It doesn't matter what we pass for block_index here.  */
03672       dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
03673                             func_name);
03674 
03675       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
03676         dw2_instantiate_symtab (per_cu);
03677     }
03678 }
03679 
03680 static void
03681 dw2_expand_all_symtabs (struct objfile *objfile)
03682 {
03683   int i;
03684 
03685   dw2_setup (objfile);
03686 
03687   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
03688                    + dwarf2_per_objfile->n_type_units); ++i)
03689     {
03690       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
03691 
03692       dw2_instantiate_symtab (per_cu);
03693     }
03694 }
03695 
03696 static void
03697 dw2_expand_symtabs_with_fullname (struct objfile *objfile,
03698                                   const char *fullname)
03699 {
03700   int i;
03701 
03702   dw2_setup (objfile);
03703 
03704   /* We don't need to consider type units here.
03705      This is only called for examining code, e.g. expand_line_sal.
03706      There can be an order of magnitude (or more) more type units
03707      than comp units, and we avoid them if we can.  */
03708 
03709   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
03710     {
03711       int j;
03712       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
03713       struct quick_file_names *file_data;
03714 
03715       /* We only need to look at symtabs not already expanded.  */
03716       if (per_cu->v.quick->symtab)
03717         continue;
03718 
03719       file_data = dw2_get_file_names (per_cu);
03720       if (file_data == NULL)
03721         continue;
03722 
03723       for (j = 0; j < file_data->num_file_names; ++j)
03724         {
03725           const char *this_fullname = file_data->file_names[j];
03726 
03727           if (filename_cmp (this_fullname, fullname) == 0)
03728             {
03729               dw2_instantiate_symtab (per_cu);
03730               break;
03731             }
03732         }
03733     }
03734 }
03735 
03736 static void
03737 dw2_map_matching_symbols (struct objfile *objfile,
03738                           const char * name, domain_enum namespace,
03739                           int global,
03740                           int (*callback) (struct block *,
03741                                            struct symbol *, void *),
03742                           void *data, symbol_compare_ftype *match,
03743                           symbol_compare_ftype *ordered_compare)
03744 {
03745   /* Currently unimplemented; used for Ada.  The function can be called if the
03746      current language is Ada for a non-Ada objfile using GNU index.  As Ada
03747      does not look for non-Ada symbols this function should just return.  */
03748 }
03749 
03750 static void
03751 dw2_expand_symtabs_matching
03752   (struct objfile *objfile,
03753    int (*file_matcher) (const char *, void *, int basenames),
03754    int (*name_matcher) (const char *, void *),
03755    enum search_domain kind,
03756    void *data)
03757 {
03758   int i;
03759   offset_type iter;
03760   struct mapped_index *index;
03761 
03762   dw2_setup (objfile);
03763 
03764   /* index_table is NULL if OBJF_READNOW.  */
03765   if (!dwarf2_per_objfile->index_table)
03766     return;
03767   index = dwarf2_per_objfile->index_table;
03768 
03769   if (file_matcher != NULL)
03770     {
03771       struct cleanup *cleanup;
03772       htab_t visited_found, visited_not_found;
03773 
03774       visited_found = htab_create_alloc (10,
03775                                          htab_hash_pointer, htab_eq_pointer,
03776                                          NULL, xcalloc, xfree);
03777       cleanup = make_cleanup_htab_delete (visited_found);
03778       visited_not_found = htab_create_alloc (10,
03779                                              htab_hash_pointer, htab_eq_pointer,
03780                                              NULL, xcalloc, xfree);
03781       make_cleanup_htab_delete (visited_not_found);
03782 
03783       /* The rule is CUs specify all the files, including those used by
03784          any TU, so there's no need to scan TUs here.  */
03785 
03786       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
03787         {
03788           int j;
03789           struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
03790           struct quick_file_names *file_data;
03791           void **slot;
03792 
03793           per_cu->v.quick->mark = 0;
03794 
03795           /* We only need to look at symtabs not already expanded.  */
03796           if (per_cu->v.quick->symtab)
03797             continue;
03798 
03799           file_data = dw2_get_file_names (per_cu);
03800           if (file_data == NULL)
03801             continue;
03802 
03803           if (htab_find (visited_not_found, file_data) != NULL)
03804             continue;
03805           else if (htab_find (visited_found, file_data) != NULL)
03806             {
03807               per_cu->v.quick->mark = 1;
03808               continue;
03809             }
03810 
03811           for (j = 0; j < file_data->num_file_names; ++j)
03812             {
03813               const char *this_real_name;
03814 
03815               if (file_matcher (file_data->file_names[j], data, 0))
03816                 {
03817                   per_cu->v.quick->mark = 1;
03818                   break;
03819                 }
03820 
03821               /* Before we invoke realpath, which can get expensive when many
03822                  files are involved, do a quick comparison of the basenames.  */
03823               if (!basenames_may_differ
03824                   && !file_matcher (lbasename (file_data->file_names[j]),
03825                                     data, 1))
03826                 continue;
03827 
03828               this_real_name = dw2_get_real_path (objfile, file_data, j);
03829               if (file_matcher (this_real_name, data, 0))
03830                 {
03831                   per_cu->v.quick->mark = 1;
03832                   break;
03833                 }
03834             }
03835 
03836           slot = htab_find_slot (per_cu->v.quick->mark
03837                                  ? visited_found
03838                                  : visited_not_found,
03839                                  file_data, INSERT);
03840           *slot = file_data;
03841         }
03842 
03843       do_cleanups (cleanup);
03844     }
03845 
03846   for (iter = 0; iter < index->symbol_table_slots; ++iter)
03847     {
03848       offset_type idx = 2 * iter;
03849       const char *name;
03850       offset_type *vec, vec_len, vec_idx;
03851 
03852       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
03853         continue;
03854 
03855       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
03856 
03857       if (! (*name_matcher) (name, data))
03858         continue;
03859 
03860       /* The name was matched, now expand corresponding CUs that were
03861          marked.  */
03862       vec = (offset_type *) (index->constant_pool
03863                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
03864       vec_len = MAYBE_SWAP (vec[0]);
03865       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
03866         {
03867           struct dwarf2_per_cu_data *per_cu;
03868           offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
03869           gdb_index_symbol_kind symbol_kind =
03870             GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
03871           int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
03872           /* Only check the symbol attributes if they're present.
03873              Indices prior to version 7 don't record them,
03874              and indices >= 7 may elide them for certain symbols
03875              (gold does this).  */
03876           int attrs_valid =
03877             (index->version >= 7
03878              && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
03879 
03880           /* Only check the symbol's kind if it has one.  */
03881           if (attrs_valid)
03882             {
03883               switch (kind)
03884                 {
03885                 case VARIABLES_DOMAIN:
03886                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
03887                     continue;
03888                   break;
03889                 case FUNCTIONS_DOMAIN:
03890                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
03891                     continue;
03892                   break;
03893                 case TYPES_DOMAIN:
03894                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
03895                     continue;
03896                   break;
03897                 default:
03898                   break;
03899                 }
03900             }
03901 
03902           /* Don't crash on bad data.  */
03903           if (cu_index >= (dwarf2_per_objfile->n_comp_units
03904                            + dwarf2_per_objfile->n_type_units))
03905             {
03906               complaint (&symfile_complaints,
03907                          _(".gdb_index entry has bad CU index"
03908                            " [in module %s]"), objfile_name (objfile));
03909               continue;
03910             }
03911 
03912           per_cu = dw2_get_cu (cu_index);
03913           if (file_matcher == NULL || per_cu->v.quick->mark)
03914             dw2_instantiate_symtab (per_cu);
03915         }
03916     }
03917 }
03918 
03919 /* A helper for dw2_find_pc_sect_symtab which finds the most specific
03920    symtab.  */
03921 
03922 static struct symtab *
03923 recursively_find_pc_sect_symtab (struct symtab *symtab, CORE_ADDR pc)
03924 {
03925   int i;
03926 
03927   if (BLOCKVECTOR (symtab) != NULL
03928       && blockvector_contains_pc (BLOCKVECTOR (symtab), pc))
03929     return symtab;
03930 
03931   if (symtab->includes == NULL)
03932     return NULL;
03933 
03934   for (i = 0; symtab->includes[i]; ++i)
03935     {
03936       struct symtab *s = symtab->includes[i];
03937 
03938       s = recursively_find_pc_sect_symtab (s, pc);
03939       if (s != NULL)
03940         return s;
03941     }
03942 
03943   return NULL;
03944 }
03945 
03946 static struct symtab *
03947 dw2_find_pc_sect_symtab (struct objfile *objfile,
03948                          struct minimal_symbol *msymbol,
03949                          CORE_ADDR pc,
03950                          struct obj_section *section,
03951                          int warn_if_readin)
03952 {
03953   struct dwarf2_per_cu_data *data;
03954   struct symtab *result;
03955 
03956   dw2_setup (objfile);
03957 
03958   if (!objfile->psymtabs_addrmap)
03959     return NULL;
03960 
03961   data = addrmap_find (objfile->psymtabs_addrmap, pc);
03962   if (!data)
03963     return NULL;
03964 
03965   if (warn_if_readin && data->v.quick->symtab)
03966     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
03967              paddress (get_objfile_arch (objfile), pc));
03968 
03969   result = recursively_find_pc_sect_symtab (dw2_instantiate_symtab (data), pc);
03970   gdb_assert (result != NULL);
03971   return result;
03972 }
03973 
03974 static void
03975 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
03976                           void *data, int need_fullname)
03977 {
03978   int i;
03979   struct cleanup *cleanup;
03980   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
03981                                       NULL, xcalloc, xfree);
03982 
03983   cleanup = make_cleanup_htab_delete (visited);
03984   dw2_setup (objfile);
03985 
03986   /* The rule is CUs specify all the files, including those used by
03987      any TU, so there's no need to scan TUs here.
03988      We can ignore file names coming from already-expanded CUs.  */
03989 
03990   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
03991     {
03992       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
03993 
03994       if (per_cu->v.quick->symtab)
03995         {
03996           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
03997                                         INSERT);
03998 
03999           *slot = per_cu->v.quick->file_names;
04000         }
04001     }
04002 
04003   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
04004     {
04005       int j;
04006       struct dwarf2_per_cu_data *per_cu = dw2_get_primary_cu (i);
04007       struct quick_file_names *file_data;
04008       void **slot;
04009 
04010       /* We only need to look at symtabs not already expanded.  */
04011       if (per_cu->v.quick->symtab)
04012         continue;
04013 
04014       file_data = dw2_get_file_names (per_cu);
04015       if (file_data == NULL)
04016         continue;
04017 
04018       slot = htab_find_slot (visited, file_data, INSERT);
04019       if (*slot)
04020         {
04021           /* Already visited.  */
04022           continue;
04023         }
04024       *slot = file_data;
04025 
04026       for (j = 0; j < file_data->num_file_names; ++j)
04027         {
04028           const char *this_real_name;
04029 
04030           if (need_fullname)
04031             this_real_name = dw2_get_real_path (objfile, file_data, j);
04032           else
04033             this_real_name = NULL;
04034           (*fun) (file_data->file_names[j], this_real_name, data);
04035         }
04036     }
04037 
04038   do_cleanups (cleanup);
04039 }
04040 
04041 static int
04042 dw2_has_symbols (struct objfile *objfile)
04043 {
04044   return 1;
04045 }
04046 
04047 const struct quick_symbol_functions dwarf2_gdb_index_functions =
04048 {
04049   dw2_has_symbols,
04050   dw2_find_last_source_symtab,
04051   dw2_forget_cached_source_info,
04052   dw2_map_symtabs_matching_filename,
04053   dw2_lookup_symbol,
04054   dw2_print_stats,
04055   dw2_dump,
04056   dw2_relocate,
04057   dw2_expand_symtabs_for_function,
04058   dw2_expand_all_symtabs,
04059   dw2_expand_symtabs_with_fullname,
04060   dw2_map_matching_symbols,
04061   dw2_expand_symtabs_matching,
04062   dw2_find_pc_sect_symtab,
04063   dw2_map_symbol_filenames
04064 };
04065 
04066 /* Initialize for reading DWARF for this objfile.  Return 0 if this
04067    file will use psymtabs, or 1 if using the GNU index.  */
04068 
04069 int
04070 dwarf2_initialize_objfile (struct objfile *objfile)
04071 {
04072   /* If we're about to read full symbols, don't bother with the
04073      indices.  In this case we also don't care if some other debug
04074      format is making psymtabs, because they are all about to be
04075      expanded anyway.  */
04076   if ((objfile->flags & OBJF_READNOW))
04077     {
04078       int i;
04079 
04080       dwarf2_per_objfile->using_index = 1;
04081       create_all_comp_units (objfile);
04082       create_all_type_units (objfile);
04083       dwarf2_per_objfile->quick_file_names_table =
04084         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
04085 
04086       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
04087                        + dwarf2_per_objfile->n_type_units); ++i)
04088         {
04089           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
04090 
04091           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
04092                                             struct dwarf2_per_cu_quick_data);
04093         }
04094 
04095       /* Return 1 so that gdb sees the "quick" functions.  However,
04096          these functions will be no-ops because we will have expanded
04097          all symtabs.  */
04098       return 1;
04099     }
04100 
04101   if (dwarf2_read_index (objfile))
04102     return 1;
04103 
04104   return 0;
04105 }
04106 
04107 
04108 
04109 /* Build a partial symbol table.  */
04110 
04111 void
04112 dwarf2_build_psymtabs (struct objfile *objfile)
04113 {
04114   volatile struct gdb_exception except;
04115 
04116   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
04117     {
04118       init_psymbol_list (objfile, 1024);
04119     }
04120 
04121   TRY_CATCH (except, RETURN_MASK_ERROR)
04122     {
04123       /* This isn't really ideal: all the data we allocate on the
04124          objfile's obstack is still uselessly kept around.  However,
04125          freeing it seems unsafe.  */
04126       struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);
04127 
04128       dwarf2_build_psymtabs_hard (objfile);
04129       discard_cleanups (cleanups);
04130     }
04131   if (except.reason < 0)
04132     exception_print (gdb_stderr, except);
04133 }
04134 
04135 /* Return the total length of the CU described by HEADER.  */
04136 
04137 static unsigned int
04138 get_cu_length (const struct comp_unit_head *header)
04139 {
04140   return header->initial_length_size + header->length;
04141 }
04142 
04143 /* Return TRUE if OFFSET is within CU_HEADER.  */
04144 
04145 static inline int
04146 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
04147 {
04148   sect_offset bottom = { cu_header->offset.sect_off };
04149   sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };
04150 
04151   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
04152 }
04153 
04154 /* Find the base address of the compilation unit for range lists and
04155    location lists.  It will normally be specified by DW_AT_low_pc.
04156    In DWARF-3 draft 4, the base address could be overridden by
04157    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
04158    compilation units with discontinuous ranges.  */
04159 
04160 static void
04161 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
04162 {
04163   struct attribute *attr;
04164 
04165   cu->base_known = 0;
04166   cu->base_address = 0;
04167 
04168   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
04169   if (attr)
04170     {
04171       cu->base_address = DW_ADDR (attr);
04172       cu->base_known = 1;
04173     }
04174   else
04175     {
04176       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
04177       if (attr)
04178         {
04179           cu->base_address = DW_ADDR (attr);
04180           cu->base_known = 1;
04181         }
04182     }
04183 }
04184 
04185 /* Read in the comp unit header information from the debug_info at info_ptr.
04186    NOTE: This leaves members offset, first_die_offset to be filled in
04187    by the caller.  */
04188 
04189 static const gdb_byte *
04190 read_comp_unit_head (struct comp_unit_head *cu_header,
04191                      const gdb_byte *info_ptr, bfd *abfd)
04192 {
04193   int signed_addr;
04194   unsigned int bytes_read;
04195 
04196   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
04197   cu_header->initial_length_size = bytes_read;
04198   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
04199   info_ptr += bytes_read;
04200   cu_header->version = read_2_bytes (abfd, info_ptr);
04201   info_ptr += 2;
04202   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
04203                                              &bytes_read);
04204   info_ptr += bytes_read;
04205   cu_header->addr_size = read_1_byte (abfd, info_ptr);
04206   info_ptr += 1;
04207   signed_addr = bfd_get_sign_extend_vma (abfd);
04208   if (signed_addr < 0)
04209     internal_error (__FILE__, __LINE__,
04210                     _("read_comp_unit_head: dwarf from non elf file"));
04211   cu_header->signed_addr_p = signed_addr;
04212 
04213   return info_ptr;
04214 }
04215 
04216 /* Helper function that returns the proper abbrev section for
04217    THIS_CU.  */
04218 
04219 static struct dwarf2_section_info *
04220 get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
04221 {
04222   struct dwarf2_section_info *abbrev;
04223 
04224   if (this_cu->is_dwz)
04225     abbrev = &dwarf2_get_dwz_file ()->abbrev;
04226   else
04227     abbrev = &dwarf2_per_objfile->abbrev;
04228 
04229   return abbrev;
04230 }
04231 
04232 /* Subroutine of read_and_check_comp_unit_head and
04233    read_and_check_type_unit_head to simplify them.
04234    Perform various error checking on the header.  */
04235 
04236 static void
04237 error_check_comp_unit_head (struct comp_unit_head *header,
04238                             struct dwarf2_section_info *section,
04239                             struct dwarf2_section_info *abbrev_section)
04240 {
04241   bfd *abfd = get_section_bfd_owner (section);
04242   const char *filename = get_section_file_name (section);
04243 
04244   if (header->version != 2 && header->version != 3 && header->version != 4)
04245     error (_("Dwarf Error: wrong version in compilation unit header "
04246            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
04247            filename);
04248 
04249   if (header->abbrev_offset.sect_off
04250       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
04251     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
04252            "(offset 0x%lx + 6) [in module %s]"),
04253            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
04254            filename);
04255 
04256   /* Cast to unsigned long to use 64-bit arithmetic when possible to
04257      avoid potential 32-bit overflow.  */
04258   if (((unsigned long) header->offset.sect_off + get_cu_length (header))
04259       > section->size)
04260     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
04261            "(offset 0x%lx + 0) [in module %s]"),
04262            (long) header->length, (long) header->offset.sect_off,
04263            filename);
04264 }
04265 
04266 /* Read in a CU/TU header and perform some basic error checking.
04267    The contents of the header are stored in HEADER.
04268    The result is a pointer to the start of the first DIE.  */
04269 
04270 static const gdb_byte *
04271 read_and_check_comp_unit_head (struct comp_unit_head *header,
04272                                struct dwarf2_section_info *section,
04273                                struct dwarf2_section_info *abbrev_section,
04274                                const gdb_byte *info_ptr,
04275                                int is_debug_types_section)
04276 {
04277   const gdb_byte *beg_of_comp_unit = info_ptr;
04278   bfd *abfd = get_section_bfd_owner (section);
04279 
04280   header->offset.sect_off = beg_of_comp_unit - section->buffer;
04281 
04282   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
04283 
04284   /* If we're reading a type unit, skip over the signature and
04285      type_offset fields.  */
04286   if (is_debug_types_section)
04287     info_ptr += 8 /*signature*/ + header->offset_size;
04288 
04289   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
04290 
04291   error_check_comp_unit_head (header, section, abbrev_section);
04292 
04293   return info_ptr;
04294 }
04295 
04296 /* Read in the types comp unit header information from .debug_types entry at
04297    types_ptr.  The result is a pointer to one past the end of the header.  */
04298 
04299 static const gdb_byte *
04300 read_and_check_type_unit_head (struct comp_unit_head *header,
04301                                struct dwarf2_section_info *section,
04302                                struct dwarf2_section_info *abbrev_section,
04303                                const gdb_byte *info_ptr,
04304                                ULONGEST *signature,
04305                                cu_offset *type_offset_in_tu)
04306 {
04307   const gdb_byte *beg_of_comp_unit = info_ptr;
04308   bfd *abfd = get_section_bfd_owner (section);
04309 
04310   header->offset.sect_off = beg_of_comp_unit - section->buffer;
04311 
04312   info_ptr = read_comp_unit_head (header, info_ptr, abfd);
04313 
04314   /* If we're reading a type unit, skip over the signature and
04315      type_offset fields.  */
04316   if (signature != NULL)
04317     *signature = read_8_bytes (abfd, info_ptr);
04318   info_ptr += 8;
04319   if (type_offset_in_tu != NULL)
04320     type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
04321                                                header->offset_size);
04322   info_ptr += header->offset_size;
04323 
04324   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
04325 
04326   error_check_comp_unit_head (header, section, abbrev_section);
04327 
04328   return info_ptr;
04329 }
04330 
04331 /* Fetch the abbreviation table offset from a comp or type unit header.  */
04332 
04333 static sect_offset
04334 read_abbrev_offset (struct dwarf2_section_info *section,
04335                     sect_offset offset)
04336 {
04337   bfd *abfd = get_section_bfd_owner (section);
04338   const gdb_byte *info_ptr;
04339   unsigned int length, initial_length_size, offset_size;
04340   sect_offset abbrev_offset;
04341 
04342   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
04343   info_ptr = section->buffer + offset.sect_off;
04344   length = read_initial_length (abfd, info_ptr, &initial_length_size);
04345   offset_size = initial_length_size == 4 ? 4 : 8;
04346   info_ptr += initial_length_size + 2 /*version*/;
04347   abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
04348   return abbrev_offset;
04349 }
04350 
04351 /* Allocate a new partial symtab for file named NAME and mark this new
04352    partial symtab as being an include of PST.  */
04353 
04354 static void
04355 dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
04356                                struct objfile *objfile)
04357 {
04358   struct partial_symtab *subpst = allocate_psymtab (name, objfile);
04359 
04360   if (!IS_ABSOLUTE_PATH (subpst->filename))
04361     {
04362       /* It shares objfile->objfile_obstack.  */
04363       subpst->dirname = pst->dirname;
04364     }
04365 
04366   subpst->section_offsets = pst->section_offsets;
04367   subpst->textlow = 0;
04368   subpst->texthigh = 0;
04369 
04370   subpst->dependencies = (struct partial_symtab **)
04371     obstack_alloc (&objfile->objfile_obstack,
04372                    sizeof (struct partial_symtab *));
04373   subpst->dependencies[0] = pst;
04374   subpst->number_of_dependencies = 1;
04375 
04376   subpst->globals_offset = 0;
04377   subpst->n_global_syms = 0;
04378   subpst->statics_offset = 0;
04379   subpst->n_static_syms = 0;
04380   subpst->symtab = NULL;
04381   subpst->read_symtab = pst->read_symtab;
04382   subpst->readin = 0;
04383 
04384   /* No private part is necessary for include psymtabs.  This property
04385      can be used to differentiate between such include psymtabs and
04386      the regular ones.  */
04387   subpst->read_symtab_private = NULL;
04388 }
04389 
04390 /* Read the Line Number Program data and extract the list of files
04391    included by the source file represented by PST.  Build an include
04392    partial symtab for each of these included files.  */
04393 
04394 static void
04395 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
04396                                struct die_info *die,
04397                                struct partial_symtab *pst)
04398 {
04399   struct line_header *lh = NULL;
04400   struct attribute *attr;
04401 
04402   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
04403   if (attr)
04404     lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
04405   if (lh == NULL)
04406     return;  /* No linetable, so no includes.  */
04407 
04408   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
04409   dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
04410 
04411   free_line_header (lh);
04412 }
04413 
04414 static hashval_t
04415 hash_signatured_type (const void *item)
04416 {
04417   const struct signatured_type *sig_type = item;
04418 
04419   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
04420   return sig_type->signature;
04421 }
04422 
04423 static int
04424 eq_signatured_type (const void *item_lhs, const void *item_rhs)
04425 {
04426   const struct signatured_type *lhs = item_lhs;
04427   const struct signatured_type *rhs = item_rhs;
04428 
04429   return lhs->signature == rhs->signature;
04430 }
04431 
04432 /* Allocate a hash table for signatured types.  */
04433 
04434 static htab_t
04435 allocate_signatured_type_table (struct objfile *objfile)
04436 {
04437   return htab_create_alloc_ex (41,
04438                                hash_signatured_type,
04439                                eq_signatured_type,
04440                                NULL,
04441                                &objfile->objfile_obstack,
04442                                hashtab_obstack_allocate,
04443                                dummy_obstack_deallocate);
04444 }
04445 
04446 /* A helper function to add a signatured type CU to a table.  */
04447 
04448 static int
04449 add_signatured_type_cu_to_table (void **slot, void *datum)
04450 {
04451   struct signatured_type *sigt = *slot;
04452   struct signatured_type ***datap = datum;
04453 
04454   **datap = sigt;
04455   ++*datap;
04456 
04457   return 1;
04458 }
04459 
04460 /* Create the hash table of all entries in the .debug_types
04461    (or .debug_types.dwo) section(s).
04462    If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
04463    otherwise it is NULL.
04464 
04465    The result is a pointer to the hash table or NULL if there are no types.
04466 
04467    Note: This function processes DWO files only, not DWP files.  */
04468 
04469 static htab_t
04470 create_debug_types_hash_table (struct dwo_file *dwo_file,
04471                                VEC (dwarf2_section_info_def) *types)
04472 {
04473   struct objfile *objfile = dwarf2_per_objfile->objfile;
04474   htab_t types_htab = NULL;
04475   int ix;
04476   struct dwarf2_section_info *section;
04477   struct dwarf2_section_info *abbrev_section;
04478 
04479   if (VEC_empty (dwarf2_section_info_def, types))
04480     return NULL;
04481 
04482   abbrev_section = (dwo_file != NULL
04483                     ? &dwo_file->sections.abbrev
04484                     : &dwarf2_per_objfile->abbrev);
04485 
04486   if (dwarf2_read_debug)
04487     fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
04488                         dwo_file ? ".dwo" : "",
04489                         get_section_file_name (abbrev_section));
04490 
04491   for (ix = 0;
04492        VEC_iterate (dwarf2_section_info_def, types, ix, section);
04493        ++ix)
04494     {
04495       bfd *abfd;
04496       const gdb_byte *info_ptr, *end_ptr;
04497 
04498       dwarf2_read_section (objfile, section);
04499       info_ptr = section->buffer;
04500 
04501       if (info_ptr == NULL)
04502         continue;
04503 
04504       /* We can't set abfd until now because the section may be empty or
04505          not present, in which case the bfd is unknown.  */
04506       abfd = get_section_bfd_owner (section);
04507 
04508       /* We don't use init_cutu_and_read_dies_simple, or some such, here
04509          because we don't need to read any dies: the signature is in the
04510          header.  */
04511 
04512       end_ptr = info_ptr + section->size;
04513       while (info_ptr < end_ptr)
04514         {
04515           sect_offset offset;
04516           cu_offset type_offset_in_tu;
04517           ULONGEST signature;
04518           struct signatured_type *sig_type;
04519           struct dwo_unit *dwo_tu;
04520           void **slot;
04521           const gdb_byte *ptr = info_ptr;
04522           struct comp_unit_head header;
04523           unsigned int length;
04524 
04525           offset.sect_off = ptr - section->buffer;
04526 
04527           /* We need to read the type's signature in order to build the hash
04528              table, but we don't need anything else just yet.  */
04529 
04530           ptr = read_and_check_type_unit_head (&header, section,
04531                                                abbrev_section, ptr,
04532                                                &signature, &type_offset_in_tu);
04533 
04534           length = get_cu_length (&header);
04535 
04536           /* Skip dummy type units.  */
04537           if (ptr >= info_ptr + length
04538               || peek_abbrev_code (abfd, ptr) == 0)
04539             {
04540               info_ptr += length;
04541               continue;
04542             }
04543 
04544           if (types_htab == NULL)
04545             {
04546               if (dwo_file)
04547                 types_htab = allocate_dwo_unit_table (objfile);
04548               else
04549                 types_htab = allocate_signatured_type_table (objfile);
04550             }
04551 
04552           if (dwo_file)
04553             {
04554               sig_type = NULL;
04555               dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
04556                                        struct dwo_unit);
04557               dwo_tu->dwo_file = dwo_file;
04558               dwo_tu->signature = signature;
04559               dwo_tu->type_offset_in_tu = type_offset_in_tu;
04560               dwo_tu->section = section;
04561               dwo_tu->offset = offset;
04562               dwo_tu->length = length;
04563             }
04564           else
04565             {
04566               /* N.B.: type_offset is not usable if this type uses a DWO file.
04567                  The real type_offset is in the DWO file.  */
04568               dwo_tu = NULL;
04569               sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
04570                                          struct signatured_type);
04571               sig_type->signature = signature;
04572               sig_type->type_offset_in_tu = type_offset_in_tu;
04573               sig_type->per_cu.objfile = objfile;
04574               sig_type->per_cu.is_debug_types = 1;
04575               sig_type->per_cu.section = section;
04576               sig_type->per_cu.offset = offset;
04577               sig_type->per_cu.length = length;
04578             }
04579 
04580           slot = htab_find_slot (types_htab,
04581                                  dwo_file ? (void*) dwo_tu : (void *) sig_type,
04582                                  INSERT);
04583           gdb_assert (slot != NULL);
04584           if (*slot != NULL)
04585             {
04586               sect_offset dup_offset;
04587 
04588               if (dwo_file)
04589                 {
04590                   const struct dwo_unit *dup_tu = *slot;
04591 
04592                   dup_offset = dup_tu->offset;
04593                 }
04594               else
04595                 {
04596                   const struct signatured_type *dup_tu = *slot;
04597 
04598                   dup_offset = dup_tu->per_cu.offset;
04599                 }
04600 
04601               complaint (&symfile_complaints,
04602                          _("debug type entry at offset 0x%x is duplicate to"
04603                            " the entry at offset 0x%x, signature %s"),
04604                          offset.sect_off, dup_offset.sect_off,
04605                          hex_string (signature));
04606             }
04607           *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
04608 
04609           if (dwarf2_read_debug)
04610             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature %s\n",
04611                                 offset.sect_off,
04612                                 hex_string (signature));
04613 
04614           info_ptr += length;
04615         }
04616     }
04617 
04618   return types_htab;
04619 }
04620 
04621 /* Create the hash table of all entries in the .debug_types section,
04622    and initialize all_type_units.
04623    The result is zero if there is an error (e.g. missing .debug_types section),
04624    otherwise non-zero.  */
04625 
04626 static int
04627 create_all_type_units (struct objfile *objfile)
04628 {
04629   htab_t types_htab;
04630   struct signatured_type **iter;
04631 
04632   types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
04633   if (types_htab == NULL)
04634     {
04635       dwarf2_per_objfile->signatured_types = NULL;
04636       return 0;
04637     }
04638 
04639   dwarf2_per_objfile->signatured_types = types_htab;
04640 
04641   dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
04642   dwarf2_per_objfile->all_type_units
04643     = xmalloc (dwarf2_per_objfile->n_type_units
04644                * sizeof (struct signatured_type *));
04645   iter = &dwarf2_per_objfile->all_type_units[0];
04646   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
04647   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
04648               == dwarf2_per_objfile->n_type_units);
04649 
04650   return 1;
04651 }
04652 
04653 /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
04654    Fill in SIG_ENTRY with DWO_ENTRY.  */
04655 
04656 static void
04657 fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
04658                                   struct signatured_type *sig_entry,
04659                                   struct dwo_unit *dwo_entry)
04660 {
04661   /* Make sure we're not clobbering something we don't expect to.  */
04662   gdb_assert (! sig_entry->per_cu.queued);
04663   gdb_assert (sig_entry->per_cu.cu == NULL);
04664   gdb_assert (sig_entry->per_cu.v.quick != NULL);
04665   gdb_assert (sig_entry->per_cu.v.quick->symtab == NULL);
04666   gdb_assert (sig_entry->signature == dwo_entry->signature);
04667   gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
04668   gdb_assert (sig_entry->type_unit_group == NULL);
04669   gdb_assert (sig_entry->dwo_unit == NULL);
04670 
04671   sig_entry->per_cu.section = dwo_entry->section;
04672   sig_entry->per_cu.offset = dwo_entry->offset;
04673   sig_entry->per_cu.length = dwo_entry->length;
04674   sig_entry->per_cu.reading_dwo_directly = 1;
04675   sig_entry->per_cu.objfile = objfile;
04676   sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
04677   sig_entry->dwo_unit = dwo_entry;
04678 }
04679 
04680 /* Subroutine of lookup_signatured_type.
04681    If we haven't read the TU yet, create the signatured_type data structure
04682    for a TU to be read in directly from a DWO file, bypassing the stub.
04683    This is the "Stay in DWO Optimization": When there is no DWP file and we're
04684    using .gdb_index, then when reading a CU we want to stay in the DWO file
04685    containing that CU.  Otherwise we could end up reading several other DWO
04686    files (due to comdat folding) to process the transitive closure of all the
04687    mentioned TUs, and that can be slow.  The current DWO file will have every
04688    type signature that it needs.
04689    We only do this for .gdb_index because in the psymtab case we already have
04690    to read all the DWOs to build the type unit groups.  */
04691 
04692 static struct signatured_type *
04693 lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
04694 {
04695   struct objfile *objfile = dwarf2_per_objfile->objfile;
04696   struct dwo_file *dwo_file;
04697   struct dwo_unit find_dwo_entry, *dwo_entry;
04698   struct signatured_type find_sig_entry, *sig_entry;
04699 
04700   gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
04701 
04702   /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
04703      dwo_unit of the TU itself.  */
04704   dwo_file = cu->dwo_unit->dwo_file;
04705 
04706   /* We only ever need to read in one copy of a signatured type.
04707      Just use the global signatured_types array.  If this is the first time
04708      we're reading this type, replace the recorded data from .gdb_index with
04709      this TU.  */
04710 
04711   if (dwarf2_per_objfile->signatured_types == NULL)
04712     return NULL;
04713   find_sig_entry.signature = sig;
04714   sig_entry = htab_find (dwarf2_per_objfile->signatured_types, &find_sig_entry);
04715   if (sig_entry == NULL)
04716     return NULL;
04717 
04718   /* We can get here with the TU already read, *or* in the process of being
04719      read.  Don't reassign it if that's the case.  Also note that if the TU is
04720      already being read, it may not have come from a DWO, the program may be
04721      a mix of Fission-compiled code and non-Fission-compiled code.  */
04722   /* Have we already tried to read this TU?  */
04723   if (sig_entry->per_cu.tu_read)
04724     return sig_entry;
04725 
04726   /* Ok, this is the first time we're reading this TU.  */
04727   if (dwo_file->tus == NULL)
04728     return NULL;
04729   find_dwo_entry.signature = sig;
04730   dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
04731   if (dwo_entry == NULL)
04732     return NULL;
04733 
04734   fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
04735   sig_entry->per_cu.tu_read = 1;
04736   return sig_entry;
04737 }
04738 
04739 /* Subroutine of lookup_dwp_signatured_type.
04740    Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.  */
04741 
04742 static struct signatured_type *
04743 add_type_unit (ULONGEST sig)
04744 {
04745   struct objfile *objfile = dwarf2_per_objfile->objfile;
04746   int n_type_units = dwarf2_per_objfile->n_type_units;
04747   struct signatured_type *sig_type;
04748   void **slot;
04749 
04750   ++n_type_units;
04751   dwarf2_per_objfile->all_type_units =
04752     xrealloc (dwarf2_per_objfile->all_type_units,
04753               n_type_units * sizeof (struct signatured_type *));
04754   dwarf2_per_objfile->n_type_units = n_type_units;
04755   sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
04756                              struct signatured_type);
04757   dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
04758   sig_type->signature = sig;
04759   sig_type->per_cu.is_debug_types = 1;
04760   sig_type->per_cu.v.quick =
04761     OBSTACK_ZALLOC (&objfile->objfile_obstack,
04762                     struct dwarf2_per_cu_quick_data);
04763   slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
04764                          sig_type, INSERT);
04765   gdb_assert (*slot == NULL);
04766   *slot = sig_type;
04767   /* The rest of sig_type must be filled in by the caller.  */
04768   return sig_type;
04769 }
04770 
04771 /* Subroutine of lookup_signatured_type.
04772    Look up the type for signature SIG, and if we can't find SIG in .gdb_index
04773    then try the DWP file.
04774    Normally this "can't happen", but if there's a bug in signature
04775    generation and/or the DWP file is built incorrectly, it can happen.
04776    Using the type directly from the DWP file means we don't have the stub
04777    which has some useful attributes (e.g., DW_AT_comp_dir), but they're
04778    not critical.  [Eventually the stub may go away for type units anyway.]  */
04779 
04780 static struct signatured_type *
04781 lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
04782 {
04783   struct objfile *objfile = dwarf2_per_objfile->objfile;
04784   struct dwp_file *dwp_file = get_dwp_file ();
04785   struct dwo_unit *dwo_entry;
04786   struct signatured_type find_sig_entry, *sig_entry;
04787 
04788   gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
04789   gdb_assert (dwp_file != NULL);
04790 
04791   if (dwarf2_per_objfile->signatured_types != NULL)
04792     {
04793       find_sig_entry.signature = sig;
04794       sig_entry = htab_find (dwarf2_per_objfile->signatured_types,
04795                              &find_sig_entry);
04796       if (sig_entry != NULL)
04797         return sig_entry;
04798     }
04799 
04800   /* This is the "shouldn't happen" case.
04801      Try the DWP file and hope for the best.  */
04802   if (dwp_file->tus == NULL)
04803     return NULL;
04804   dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
04805                                       sig, 1 /* is_debug_types */);
04806   if (dwo_entry == NULL)
04807     return NULL;
04808 
04809   sig_entry = add_type_unit (sig);
04810   fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
04811 
04812   /* The caller will signal a complaint if we return NULL.
04813      Here we don't return NULL but we still want to complain.  */
04814   complaint (&symfile_complaints,
04815              _("Bad type signature %s referenced by %s at 0x%x,"
04816                " coping by using copy in DWP [in module %s]"),
04817              hex_string (sig),
04818              cu->per_cu->is_debug_types ? "TU" : "CU",
04819              cu->per_cu->offset.sect_off,
04820              objfile_name (objfile));
04821 
04822   return sig_entry;
04823 }
04824 
04825 /* Lookup a signature based type for DW_FORM_ref_sig8.
04826    Returns NULL if signature SIG is not present in the table.
04827    It is up to the caller to complain about this.  */
04828 
04829 static struct signatured_type *
04830 lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
04831 {
04832   if (cu->dwo_unit
04833       && dwarf2_per_objfile->using_index)
04834     {
04835       /* We're in a DWO/DWP file, and we're using .gdb_index.
04836          These cases require special processing.  */
04837       if (get_dwp_file () == NULL)
04838         return lookup_dwo_signatured_type (cu, sig);
04839       else
04840         return lookup_dwp_signatured_type (cu, sig);
04841     }
04842   else
04843     {
04844       struct signatured_type find_entry, *entry;
04845 
04846       if (dwarf2_per_objfile->signatured_types == NULL)
04847         return NULL;
04848       find_entry.signature = sig;
04849       entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
04850       return entry;
04851     }
04852 }
04853 
04854 /* Low level DIE reading support.  */
04855 
04856 /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */
04857 
04858 static void
04859 init_cu_die_reader (struct die_reader_specs *reader,
04860                     struct dwarf2_cu *cu,
04861                     struct dwarf2_section_info *section,
04862                     struct dwo_file *dwo_file)
04863 {
04864   gdb_assert (section->readin && section->buffer != NULL);
04865   reader->abfd = get_section_bfd_owner (section);
04866   reader->cu = cu;
04867   reader->dwo_file = dwo_file;
04868   reader->die_section = section;
04869   reader->buffer = section->buffer;
04870   reader->buffer_end = section->buffer + section->size;
04871   reader->comp_dir = NULL;
04872 }
04873 
04874 /* Subroutine of init_cutu_and_read_dies to simplify it.
04875    Read in the rest of a CU/TU top level DIE from DWO_UNIT.
04876    There's just a lot of work to do, and init_cutu_and_read_dies is big enough
04877    already.
04878 
04879    STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
04880    from it to the DIE in the DWO.  If NULL we are skipping the stub.
04881    STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
04882    from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
04883    attribute of the referencing CU.  Exactly one of STUB_COMP_UNIT_DIE and
04884    COMP_DIR must be non-NULL.
04885    *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
04886    are filled in with the info of the DIE from the DWO file.
04887    ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
04888    provided an abbrev table to use.
04889    The result is non-zero if a valid (non-dummy) DIE was found.  */
04890 
04891 static int
04892 read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
04893                         struct dwo_unit *dwo_unit,
04894                         int abbrev_table_provided,
04895                         struct die_info *stub_comp_unit_die,
04896                         const char *stub_comp_dir,
04897                         struct die_reader_specs *result_reader,
04898                         const gdb_byte **result_info_ptr,
04899                         struct die_info **result_comp_unit_die,
04900                         int *result_has_children)
04901 {
04902   struct objfile *objfile = dwarf2_per_objfile->objfile;
04903   struct dwarf2_cu *cu = this_cu->cu;
04904   struct dwarf2_section_info *section;
04905   bfd *abfd;
04906   const gdb_byte *begin_info_ptr, *info_ptr;
04907   const char *comp_dir_string;
04908   ULONGEST signature; /* Or dwo_id.  */
04909   struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
04910   int i,num_extra_attrs;
04911   struct dwarf2_section_info *dwo_abbrev_section;
04912   struct attribute *attr;
04913   struct attribute comp_dir_attr;
04914   struct die_info *comp_unit_die;
04915 
04916   /* Both can't be provided.  */
04917   gdb_assert (! (stub_comp_unit_die && stub_comp_dir));
04918 
04919   /* These attributes aren't processed until later:
04920      DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
04921      However, the attribute is found in the stub which we won't have later.
04922      In order to not impose this complication on the rest of the code,
04923      we read them here and copy them to the DWO CU/TU die.  */
04924 
04925   stmt_list = NULL;
04926   low_pc = NULL;
04927   high_pc = NULL;
04928   ranges = NULL;
04929   comp_dir = NULL;
04930 
04931   if (stub_comp_unit_die != NULL)
04932     {
04933       /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
04934          DWO file.  */
04935       if (! this_cu->is_debug_types)
04936         stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
04937       low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
04938       high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
04939       ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
04940       comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
04941 
04942       /* There should be a DW_AT_addr_base attribute here (if needed).
04943          We need the value before we can process DW_FORM_GNU_addr_index.  */
04944       cu->addr_base = 0;
04945       attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
04946       if (attr)
04947         cu->addr_base = DW_UNSND (attr);
04948 
04949       /* There should be a DW_AT_ranges_base attribute here (if needed).
04950          We need the value before we can process DW_AT_ranges.  */
04951       cu->ranges_base = 0;
04952       attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
04953       if (attr)
04954         cu->ranges_base = DW_UNSND (attr);
04955     }
04956   else if (stub_comp_dir != NULL)
04957     {
04958       /* Reconstruct the comp_dir attribute to simplify the code below.  */
04959       comp_dir = (struct attribute *)
04960         obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
04961       comp_dir->name = DW_AT_comp_dir;
04962       comp_dir->form = DW_FORM_string;
04963       DW_STRING_IS_CANONICAL (comp_dir) = 0;
04964       DW_STRING (comp_dir) = stub_comp_dir;
04965     }
04966 
04967   /* Set up for reading the DWO CU/TU.  */
04968   cu->dwo_unit = dwo_unit;
04969   section = dwo_unit->section;
04970   dwarf2_read_section (objfile, section);
04971   abfd = get_section_bfd_owner (section);
04972   begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
04973   dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
04974   init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);
04975 
04976   if (this_cu->is_debug_types)
04977     {
04978       ULONGEST header_signature;
04979       cu_offset type_offset_in_tu;
04980       struct signatured_type *sig_type = (struct signatured_type *) this_cu;
04981 
04982       info_ptr = read_and_check_type_unit_head (&cu->header, section,
04983                                                 dwo_abbrev_section,
04984                                                 info_ptr,
04985                                                 &header_signature,
04986                                                 &type_offset_in_tu);
04987       /* This is not an assert because it can be caused by bad debug info.  */
04988       if (sig_type->signature != header_signature)
04989         {
04990           error (_("Dwarf Error: signature mismatch %s vs %s while reading"
04991                    " TU at offset 0x%x [in module %s]"),
04992                  hex_string (sig_type->signature),
04993                  hex_string (header_signature),
04994                  dwo_unit->offset.sect_off,
04995                  bfd_get_filename (abfd));
04996         }
04997       gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
04998       /* For DWOs coming from DWP files, we don't know the CU length
04999          nor the type's offset in the TU until now.  */
05000       dwo_unit->length = get_cu_length (&cu->header);
05001       dwo_unit->type_offset_in_tu = type_offset_in_tu;
05002 
05003       /* Establish the type offset that can be used to lookup the type.
05004          For DWO files, we don't know it until now.  */
05005       sig_type->type_offset_in_section.sect_off =
05006         dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
05007     }
05008   else
05009     {
05010       info_ptr = read_and_check_comp_unit_head (&cu->header, section,
05011                                                 dwo_abbrev_section,
05012                                                 info_ptr, 0);
05013       gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
05014       /* For DWOs coming from DWP files, we don't know the CU length
05015          until now.  */
05016       dwo_unit->length = get_cu_length (&cu->header);
05017     }
05018 
05019   /* Replace the CU's original abbrev table with the DWO's.
05020      Reminder: We can't read the abbrev table until we've read the header.  */
05021   if (abbrev_table_provided)
05022     {
05023       /* Don't free the provided abbrev table, the caller of
05024          init_cutu_and_read_dies owns it.  */
05025       dwarf2_read_abbrevs (cu, dwo_abbrev_section);
05026       /* Ensure the DWO abbrev table gets freed.  */
05027       make_cleanup (dwarf2_free_abbrev_table, cu);
05028     }
05029   else
05030     {
05031       dwarf2_free_abbrev_table (cu);
05032       dwarf2_read_abbrevs (cu, dwo_abbrev_section);
05033       /* Leave any existing abbrev table cleanup as is.  */
05034     }
05035 
05036   /* Read in the die, but leave space to copy over the attributes
05037      from the stub.  This has the benefit of simplifying the rest of
05038      the code - all the work to maintain the illusion of a single
05039      DW_TAG_{compile,type}_unit DIE is done here.  */
05040   num_extra_attrs = ((stmt_list != NULL)
05041                      + (low_pc != NULL)
05042                      + (high_pc != NULL)
05043                      + (ranges != NULL)
05044                      + (comp_dir != NULL));
05045   info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
05046                               result_has_children, num_extra_attrs);
05047 
05048   /* Copy over the attributes from the stub to the DIE we just read in.  */
05049   comp_unit_die = *result_comp_unit_die;
05050   i = comp_unit_die->num_attrs;
05051   if (stmt_list != NULL)
05052     comp_unit_die->attrs[i++] = *stmt_list;
05053   if (low_pc != NULL)
05054     comp_unit_die->attrs[i++] = *low_pc;
05055   if (high_pc != NULL)
05056     comp_unit_die->attrs[i++] = *high_pc;
05057   if (ranges != NULL)
05058     comp_unit_die->attrs[i++] = *ranges;
05059   if (comp_dir != NULL)
05060     comp_unit_die->attrs[i++] = *comp_dir;
05061   comp_unit_die->num_attrs += num_extra_attrs;
05062 
05063   if (dwarf2_die_debug)
05064     {
05065       fprintf_unfiltered (gdb_stdlog,
05066                           "Read die from %s@0x%x of %s:\n",
05067                           get_section_name (section),
05068                           (unsigned) (begin_info_ptr - section->buffer),
05069                           bfd_get_filename (abfd));
05070       dump_die (comp_unit_die, dwarf2_die_debug);
05071     }
05072 
05073   /* Save the comp_dir attribute.  If there is no DWP file then we'll read
05074      TUs by skipping the stub and going directly to the entry in the DWO file.
05075      However, skipping the stub means we won't get DW_AT_comp_dir, so we have
05076      to get it via circuitous means.  Blech.  */
05077   if (comp_dir != NULL)
05078     result_reader->comp_dir = DW_STRING (comp_dir);
05079 
05080   /* Skip dummy compilation units.  */
05081   if (info_ptr >= begin_info_ptr + dwo_unit->length
05082       || peek_abbrev_code (abfd, info_ptr) == 0)
05083     return 0;
05084 
05085   *result_info_ptr = info_ptr;
05086   return 1;
05087 }
05088 
05089 /* Subroutine of init_cutu_and_read_dies to simplify it.
05090    Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
05091    Returns NULL if the specified DWO unit cannot be found.  */
05092 
05093 static struct dwo_unit *
05094 lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
05095                  struct die_info *comp_unit_die)
05096 {
05097   struct dwarf2_cu *cu = this_cu->cu;
05098   struct attribute *attr;
05099   ULONGEST signature;
05100   struct dwo_unit *dwo_unit;
05101   const char *comp_dir, *dwo_name;
05102 
05103   gdb_assert (cu != NULL);
05104 
05105   /* Yeah, we look dwo_name up again, but it simplifies the code.  */
05106   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
05107   gdb_assert (attr != NULL);
05108   dwo_name = DW_STRING (attr);
05109   comp_dir = NULL;
05110   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
05111   if (attr)
05112     comp_dir = DW_STRING (attr);
05113 
05114   if (this_cu->is_debug_types)
05115     {
05116       struct signatured_type *sig_type;
05117 
05118       /* Since this_cu is the first member of struct signatured_type,
05119          we can go from a pointer to one to a pointer to the other.  */
05120       sig_type = (struct signatured_type *) this_cu;
05121       signature = sig_type->signature;
05122       dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
05123     }
05124   else
05125     {
05126       struct attribute *attr;
05127 
05128       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
05129       if (! attr)
05130         error (_("Dwarf Error: missing dwo_id for dwo_name %s"
05131                  " [in module %s]"),
05132                dwo_name, objfile_name (this_cu->objfile));
05133       signature = DW_UNSND (attr);
05134       dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
05135                                        signature);
05136     }
05137 
05138   return dwo_unit;
05139 }
05140 
05141 /* Subroutine of init_cutu_and_read_dies to simplify it.
05142    Read a TU directly from a DWO file, bypassing the stub.  */
05143 
05144 static void
05145 init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu, int keep,
05146                            die_reader_func_ftype *die_reader_func,
05147                            void *data)
05148 {
05149   struct dwarf2_cu *cu;
05150   struct signatured_type *sig_type;
05151   struct cleanup *cleanups, *free_cu_cleanup;
05152   struct die_reader_specs reader;
05153   const gdb_byte *info_ptr;
05154   struct die_info *comp_unit_die;
05155   int has_children;
05156 
05157   /* Verify we can do the following downcast, and that we have the
05158      data we need.  */
05159   gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
05160   sig_type = (struct signatured_type *) this_cu;
05161   gdb_assert (sig_type->dwo_unit != NULL);
05162 
05163   cleanups = make_cleanup (null_cleanup, NULL);
05164 
05165   gdb_assert (this_cu->cu == NULL);
05166   cu = xmalloc (sizeof (*cu));
05167   init_one_comp_unit (cu, this_cu);
05168   /* If an error occurs while loading, release our storage.  */
05169   free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
05170 
05171   if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
05172                               0 /* abbrev_table_provided */,
05173                               NULL /* stub_comp_unit_die */,
05174                               sig_type->dwo_unit->dwo_file->comp_dir,
05175                               &reader, &info_ptr,
05176                               &comp_unit_die, &has_children) == 0)
05177     {
05178       /* Dummy die.  */
05179       do_cleanups (cleanups);
05180       return;
05181     }
05182 
05183   /* All the "real" work is done here.  */
05184   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
05185 
05186   /* This duplicates some code in init_cutu_and_read_dies,
05187      but the alternative is making the latter more complex.
05188      This function is only for the special case of using DWO files directly:
05189      no point in overly complicating the general case just to handle this.  */
05190   if (keep)
05191     {
05192       /* We've successfully allocated this compilation unit.  Let our
05193          caller clean it up when finished with it.  */
05194       discard_cleanups (free_cu_cleanup);
05195 
05196       /* We can only discard free_cu_cleanup and all subsequent cleanups.
05197          So we have to manually free the abbrev table.  */
05198       dwarf2_free_abbrev_table (cu);
05199 
05200       /* Link this CU into read_in_chain.  */
05201       this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
05202       dwarf2_per_objfile->read_in_chain = this_cu;
05203     }
05204   else
05205     do_cleanups (free_cu_cleanup);
05206 
05207   do_cleanups (cleanups);
05208 }
05209 
05210 /* Initialize a CU (or TU) and read its DIEs.
05211    If the CU defers to a DWO file, read the DWO file as well.
05212 
05213    ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
05214    Otherwise the table specified in the comp unit header is read in and used.
05215    This is an optimization for when we already have the abbrev table.
05216 
05217    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
05218    Otherwise, a new CU is allocated with xmalloc.
05219 
05220    If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
05221    read_in_chain.  Otherwise the dwarf2_cu data is freed at the end.
05222 
05223    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
05224    linker) then DIE_READER_FUNC will not get called.  */
05225 
05226 static void
05227 init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
05228                          struct abbrev_table *abbrev_table,
05229                          int use_existing_cu, int keep,
05230                          die_reader_func_ftype *die_reader_func,
05231                          void *data)
05232 {
05233   struct objfile *objfile = dwarf2_per_objfile->objfile;
05234   struct dwarf2_section_info *section = this_cu->section;
05235   bfd *abfd = get_section_bfd_owner (section);
05236   struct dwarf2_cu *cu;
05237   const gdb_byte *begin_info_ptr, *info_ptr;
05238   struct die_reader_specs reader;
05239   struct die_info *comp_unit_die;
05240   int has_children;
05241   struct attribute *attr;
05242   struct cleanup *cleanups, *free_cu_cleanup = NULL;
05243   struct signatured_type *sig_type = NULL;
05244   struct dwarf2_section_info *abbrev_section;
05245   /* Non-zero if CU currently points to a DWO file and we need to
05246      reread it.  When this happens we need to reread the skeleton die
05247      before we can reread the DWO file (this only applies to CUs, not TUs).  */
05248   int rereading_dwo_cu = 0;
05249 
05250   if (dwarf2_die_debug)
05251     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
05252                         this_cu->is_debug_types ? "type" : "comp",
05253                         this_cu->offset.sect_off);
05254 
05255   if (use_existing_cu)
05256     gdb_assert (keep);
05257 
05258   /* If we're reading a TU directly from a DWO file, including a virtual DWO
05259      file (instead of going through the stub), short-circuit all of this.  */
05260   if (this_cu->reading_dwo_directly)
05261     {
05262       /* Narrow down the scope of possibilities to have to understand.  */
05263       gdb_assert (this_cu->is_debug_types);
05264       gdb_assert (abbrev_table == NULL);
05265       gdb_assert (!use_existing_cu);
05266       init_tu_and_read_dwo_dies (this_cu, keep, die_reader_func, data);
05267       return;
05268     }
05269 
05270   cleanups = make_cleanup (null_cleanup, NULL);
05271 
05272   /* This is cheap if the section is already read in.  */
05273   dwarf2_read_section (objfile, section);
05274 
05275   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
05276 
05277   abbrev_section = get_abbrev_section_for_cu (this_cu);
05278 
05279   if (use_existing_cu && this_cu->cu != NULL)
05280     {
05281       cu = this_cu->cu;
05282 
05283       /* If this CU is from a DWO file we need to start over, we need to
05284          refetch the attributes from the skeleton CU.
05285          This could be optimized by retrieving those attributes from when we
05286          were here the first time: the previous comp_unit_die was stored in
05287          comp_unit_obstack.  But there's no data yet that we need this
05288          optimization.  */
05289       if (cu->dwo_unit != NULL)
05290         rereading_dwo_cu = 1;
05291     }
05292   else
05293     {
05294       /* If !use_existing_cu, this_cu->cu must be NULL.  */
05295       gdb_assert (this_cu->cu == NULL);
05296 
05297       cu = xmalloc (sizeof (*cu));
05298       init_one_comp_unit (cu, this_cu);
05299 
05300       /* If an error occurs while loading, release our storage.  */
05301       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
05302     }
05303 
05304   /* Get the header.  */
05305   if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
05306     {
05307       /* We already have the header, there's no need to read it in again.  */
05308       info_ptr += cu->header.first_die_offset.cu_off;
05309     }
05310   else
05311     {
05312       if (this_cu->is_debug_types)
05313         {
05314           ULONGEST signature;
05315           cu_offset type_offset_in_tu;
05316 
05317           info_ptr = read_and_check_type_unit_head (&cu->header, section,
05318                                                     abbrev_section, info_ptr,
05319                                                     &signature,
05320                                                     &type_offset_in_tu);
05321 
05322           /* Since per_cu is the first member of struct signatured_type,
05323              we can go from a pointer to one to a pointer to the other.  */
05324           sig_type = (struct signatured_type *) this_cu;
05325           gdb_assert (sig_type->signature == signature);
05326           gdb_assert (sig_type->type_offset_in_tu.cu_off
05327                       == type_offset_in_tu.cu_off);
05328           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
05329 
05330           /* LENGTH has not been set yet for type units if we're
05331              using .gdb_index.  */
05332           this_cu->length = get_cu_length (&cu->header);
05333 
05334           /* Establish the type offset that can be used to lookup the type.  */
05335           sig_type->type_offset_in_section.sect_off =
05336             this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
05337         }
05338       else
05339         {
05340           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
05341                                                     abbrev_section,
05342                                                     info_ptr, 0);
05343 
05344           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
05345           gdb_assert (this_cu->length == get_cu_length (&cu->header));
05346         }
05347     }
05348 
05349   /* Skip dummy compilation units.  */
05350   if (info_ptr >= begin_info_ptr + this_cu->length
05351       || peek_abbrev_code (abfd, info_ptr) == 0)
05352     {
05353       do_cleanups (cleanups);
05354       return;
05355     }
05356 
05357   /* If we don't have them yet, read the abbrevs for this compilation unit.
05358      And if we need to read them now, make sure they're freed when we're
05359      done.  Note that it's important that if the CU had an abbrev table
05360      on entry we don't free it when we're done: Somewhere up the call stack
05361      it may be in use.  */
05362   if (abbrev_table != NULL)
05363     {
05364       gdb_assert (cu->abbrev_table == NULL);
05365       gdb_assert (cu->header.abbrev_offset.sect_off
05366                   == abbrev_table->offset.sect_off);
05367       cu->abbrev_table = abbrev_table;
05368     }
05369   else if (cu->abbrev_table == NULL)
05370     {
05371       dwarf2_read_abbrevs (cu, abbrev_section);
05372       make_cleanup (dwarf2_free_abbrev_table, cu);
05373     }
05374   else if (rereading_dwo_cu)
05375     {
05376       dwarf2_free_abbrev_table (cu);
05377       dwarf2_read_abbrevs (cu, abbrev_section);
05378     }
05379 
05380   /* Read the top level CU/TU die.  */
05381   init_cu_die_reader (&reader, cu, section, NULL);
05382   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
05383 
05384   /* If we are in a DWO stub, process it and then read in the "real" CU/TU
05385      from the DWO file.
05386      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
05387      DWO CU, that this test will fail (the attribute will not be present).  */
05388   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
05389   if (attr)
05390     {
05391       struct dwo_unit *dwo_unit;
05392       struct die_info *dwo_comp_unit_die;
05393 
05394       if (has_children)
05395         {
05396           complaint (&symfile_complaints,
05397                      _("compilation unit with DW_AT_GNU_dwo_name"
05398                        " has children (offset 0x%x) [in module %s]"),
05399                      this_cu->offset.sect_off, bfd_get_filename (abfd));
05400         }
05401       dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
05402       if (dwo_unit != NULL)
05403         {
05404           if (read_cutu_die_from_dwo (this_cu, dwo_unit,
05405                                       abbrev_table != NULL,
05406                                       comp_unit_die, NULL,
05407                                       &reader, &info_ptr,
05408                                       &dwo_comp_unit_die, &has_children) == 0)
05409             {
05410               /* Dummy die.  */
05411               do_cleanups (cleanups);
05412               return;
05413             }
05414           comp_unit_die = dwo_comp_unit_die;
05415         }
05416       else
05417         {
05418           /* Yikes, we couldn't find the rest of the DIE, we only have
05419              the stub.  A complaint has already been logged.  There's
05420              not much more we can do except pass on the stub DIE to
05421              die_reader_func.  We don't want to throw an error on bad
05422              debug info.  */
05423         }
05424     }
05425 
05426   /* All of the above is setup for this call.  Yikes.  */
05427   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
05428 
05429   /* Done, clean up.  */
05430   if (free_cu_cleanup != NULL)
05431     {
05432       if (keep)
05433         {
05434           /* We've successfully allocated this compilation unit.  Let our
05435              caller clean it up when finished with it.  */
05436           discard_cleanups (free_cu_cleanup);
05437 
05438           /* We can only discard free_cu_cleanup and all subsequent cleanups.
05439              So we have to manually free the abbrev table.  */
05440           dwarf2_free_abbrev_table (cu);
05441 
05442           /* Link this CU into read_in_chain.  */
05443           this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
05444           dwarf2_per_objfile->read_in_chain = this_cu;
05445         }
05446       else
05447         do_cleanups (free_cu_cleanup);
05448     }
05449 
05450   do_cleanups (cleanups);
05451 }
05452 
05453 /* Read CU/TU THIS_CU in section SECTION,
05454    but do not follow DW_AT_GNU_dwo_name if present.
05455    DWOP_FILE, if non-NULL, is the DWO/DWP file to read (the caller is assumed
05456    to have already done the lookup to find the DWO/DWP file).
05457 
05458    The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
05459    THIS_CU->is_debug_types, but nothing else.
05460 
05461    We fill in THIS_CU->length.
05462 
05463    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
05464    linker) then DIE_READER_FUNC will not get called.
05465 
05466    THIS_CU->cu is always freed when done.
05467    This is done in order to not leave THIS_CU->cu in a state where we have
05468    to care whether it refers to the "main" CU or the DWO CU.  */
05469 
05470 static void
05471 init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
05472                                    struct dwarf2_section_info *abbrev_section,
05473                                    struct dwo_file *dwo_file,
05474                                    die_reader_func_ftype *die_reader_func,
05475                                    void *data)
05476 {
05477   struct objfile *objfile = dwarf2_per_objfile->objfile;
05478   struct dwarf2_section_info *section = this_cu->section;
05479   bfd *abfd = get_section_bfd_owner (section);
05480   struct dwarf2_cu cu;
05481   const gdb_byte *begin_info_ptr, *info_ptr;
05482   struct die_reader_specs reader;
05483   struct cleanup *cleanups;
05484   struct die_info *comp_unit_die;
05485   int has_children;
05486 
05487   if (dwarf2_die_debug)
05488     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
05489                         this_cu->is_debug_types ? "type" : "comp",
05490                         this_cu->offset.sect_off);
05491 
05492   gdb_assert (this_cu->cu == NULL);
05493 
05494   /* This is cheap if the section is already read in.  */
05495   dwarf2_read_section (objfile, section);
05496 
05497   init_one_comp_unit (&cu, this_cu);
05498 
05499   cleanups = make_cleanup (free_stack_comp_unit, &cu);
05500 
05501   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
05502   info_ptr = read_and_check_comp_unit_head (&cu.header, section,
05503                                             abbrev_section, info_ptr,
05504                                             this_cu->is_debug_types);
05505 
05506   this_cu->length = get_cu_length (&cu.header);
05507 
05508   /* Skip dummy compilation units.  */
05509   if (info_ptr >= begin_info_ptr + this_cu->length
05510       || peek_abbrev_code (abfd, info_ptr) == 0)
05511     {
05512       do_cleanups (cleanups);
05513       return;
05514     }
05515 
05516   dwarf2_read_abbrevs (&cu, abbrev_section);
05517   make_cleanup (dwarf2_free_abbrev_table, &cu);
05518 
05519   init_cu_die_reader (&reader, &cu, section, dwo_file);
05520   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);
05521 
05522   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);
05523 
05524   do_cleanups (cleanups);
05525 }
05526 
05527 /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
05528    does not lookup the specified DWO file.
05529    This cannot be used to read DWO files.
05530 
05531    THIS_CU->cu is always freed when done.
05532    This is done in order to not leave THIS_CU->cu in a state where we have
05533    to care whether it refers to the "main" CU or the DWO CU.
05534    We can revisit this if the data shows there's a performance issue.  */
05535 
05536 static void
05537 init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
05538                                 die_reader_func_ftype *die_reader_func,
05539                                 void *data)
05540 {
05541   init_cutu_and_read_dies_no_follow (this_cu,
05542                                      get_abbrev_section_for_cu (this_cu),
05543                                      NULL,
05544                                      die_reader_func, data);
05545 }
05546 
05547 /* Type Unit Groups.
05548 
05549    Type Unit Groups are a way to collapse the set of all TUs (type units) into
05550    a more manageable set.  The grouping is done by DW_AT_stmt_list entry
05551    so that all types coming from the same compilation (.o file) are grouped
05552    together.  A future step could be to put the types in the same symtab as
05553    the CU the types ultimately came from.  */
05554 
05555 static hashval_t
05556 hash_type_unit_group (const void *item)
05557 {
05558   const struct type_unit_group *tu_group = item;
05559 
05560   return hash_stmt_list_entry (&tu_group->hash);
05561 }
05562 
05563 static int
05564 eq_type_unit_group (const void *item_lhs, const void *item_rhs)
05565 {
05566   const struct type_unit_group *lhs = item_lhs;
05567   const struct type_unit_group *rhs = item_rhs;
05568 
05569   return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
05570 }
05571 
05572 /* Allocate a hash table for type unit groups.  */
05573 
05574 static htab_t
05575 allocate_type_unit_groups_table (void)
05576 {
05577   return htab_create_alloc_ex (3,
05578                                hash_type_unit_group,
05579                                eq_type_unit_group,
05580                                NULL,
05581                                &dwarf2_per_objfile->objfile->objfile_obstack,
05582                                hashtab_obstack_allocate,
05583                                dummy_obstack_deallocate);
05584 }
05585 
05586 /* Type units that don't have DW_AT_stmt_list are grouped into their own
05587    partial symtabs.  We combine several TUs per psymtab to not let the size
05588    of any one psymtab grow too big.  */
05589 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
05590 #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
05591 
05592 /* Helper routine for get_type_unit_group.
05593    Create the type_unit_group object used to hold one or more TUs.  */
05594 
05595 static struct type_unit_group *
05596 create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
05597 {
05598   struct objfile *objfile = dwarf2_per_objfile->objfile;
05599   struct dwarf2_per_cu_data *per_cu;
05600   struct type_unit_group *tu_group;
05601 
05602   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
05603                              struct type_unit_group);
05604   per_cu = &tu_group->per_cu;
05605   per_cu->objfile = objfile;
05606 
05607   if (dwarf2_per_objfile->using_index)
05608     {
05609       per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
05610                                         struct dwarf2_per_cu_quick_data);
05611     }
05612   else
05613     {
05614       unsigned int line_offset = line_offset_struct.sect_off;
05615       struct partial_symtab *pst;
05616       char *name;
05617 
05618       /* Give the symtab a useful name for debug purposes.  */
05619       if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
05620         name = xstrprintf ("<type_units_%d>",
05621                            (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
05622       else
05623         name = xstrprintf ("<type_units_at_0x%x>", line_offset);
05624 
05625       pst = create_partial_symtab (per_cu, name);
05626       pst->anonymous = 1;
05627 
05628       xfree (name);
05629     }
05630 
05631   tu_group->hash.dwo_unit = cu->dwo_unit;
05632   tu_group->hash.line_offset = line_offset_struct;
05633 
05634   return tu_group;
05635 }
05636 
05637 /* Look up the type_unit_group for type unit CU, and create it if necessary.
05638    STMT_LIST is a DW_AT_stmt_list attribute.  */
05639 
05640 static struct type_unit_group *
05641 get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
05642 {
05643   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
05644   struct type_unit_group *tu_group;
05645   void **slot;
05646   unsigned int line_offset;
05647   struct type_unit_group type_unit_group_for_lookup;
05648 
05649   if (dwarf2_per_objfile->type_unit_groups == NULL)
05650     {
05651       dwarf2_per_objfile->type_unit_groups =
05652         allocate_type_unit_groups_table ();
05653     }
05654 
05655   /* Do we need to create a new group, or can we use an existing one?  */
05656 
05657   if (stmt_list)
05658     {
05659       line_offset = DW_UNSND (stmt_list);
05660       ++tu_stats->nr_symtab_sharers;
05661     }
05662   else
05663     {
05664       /* Ugh, no stmt_list.  Rare, but we have to handle it.
05665          We can do various things here like create one group per TU or
05666          spread them over multiple groups to split up the expansion work.
05667          To avoid worst case scenarios (too many groups or too large groups)
05668          we, umm, group them in bunches.  */
05669       line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
05670                      | (tu_stats->nr_stmt_less_type_units
05671                         / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
05672       ++tu_stats->nr_stmt_less_type_units;
05673     }
05674 
05675   type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
05676   type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
05677   slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
05678                          &type_unit_group_for_lookup, INSERT);
05679   if (*slot != NULL)
05680     {
05681       tu_group = *slot;
05682       gdb_assert (tu_group != NULL);
05683     }
05684   else
05685     {
05686       sect_offset line_offset_struct;
05687 
05688       line_offset_struct.sect_off = line_offset;
05689       tu_group = create_type_unit_group (cu, line_offset_struct);
05690       *slot = tu_group;
05691       ++tu_stats->nr_symtabs;
05692     }
05693 
05694   return tu_group;
05695 }
05696 
05697 /* Struct used to sort TUs by their abbreviation table offset.  */
05698 
05699 struct tu_abbrev_offset
05700 {
05701   struct signatured_type *sig_type;
05702   sect_offset abbrev_offset;
05703 };
05704 
05705 /* Helper routine for build_type_unit_groups, passed to qsort.  */
05706 
05707 static int
05708 sort_tu_by_abbrev_offset (const void *ap, const void *bp)
05709 {
05710   const struct tu_abbrev_offset * const *a = ap;
05711   const struct tu_abbrev_offset * const *b = bp;
05712   unsigned int aoff = (*a)->abbrev_offset.sect_off;
05713   unsigned int boff = (*b)->abbrev_offset.sect_off;
05714 
05715   return (aoff > boff) - (aoff < boff);
05716 }
05717 
05718 /* A helper function to add a type_unit_group to a table.  */
05719 
05720 static int
05721 add_type_unit_group_to_table (void **slot, void *datum)
05722 {
05723   struct type_unit_group *tu_group = *slot;
05724   struct type_unit_group ***datap = datum;
05725 
05726   **datap = tu_group;
05727   ++*datap;
05728 
05729   return 1;
05730 }
05731 
05732 /* Efficiently read all the type units, calling init_cutu_and_read_dies on
05733    each one passing FUNC,DATA.
05734 
05735    The efficiency is because we sort TUs by the abbrev table they use and
05736    only read each abbrev table once.  In one program there are 200K TUs
05737    sharing 8K abbrev tables.
05738 
05739    The main purpose of this function is to support building the
05740    dwarf2_per_objfile->type_unit_groups table.
05741    TUs typically share the DW_AT_stmt_list of the CU they came from, so we
05742    can collapse the search space by grouping them by stmt_list.
05743    The savings can be significant, in the same program from above the 200K TUs
05744    share 8K stmt_list tables.
05745 
05746    FUNC is expected to call get_type_unit_group, which will create the
05747    struct type_unit_group if necessary and add it to
05748    dwarf2_per_objfile->type_unit_groups.  */
05749 
05750 static void
05751 build_type_unit_groups (die_reader_func_ftype *func, void *data)
05752 {
05753   struct objfile *objfile = dwarf2_per_objfile->objfile;
05754   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
05755   struct cleanup *cleanups;
05756   struct abbrev_table *abbrev_table;
05757   sect_offset abbrev_offset;
05758   struct tu_abbrev_offset *sorted_by_abbrev;
05759   struct type_unit_group **iter;
05760   int i;
05761 
05762   /* It's up to the caller to not call us multiple times.  */
05763   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
05764 
05765   if (dwarf2_per_objfile->n_type_units == 0)
05766     return;
05767 
05768   /* TUs typically share abbrev tables, and there can be way more TUs than
05769      abbrev tables.  Sort by abbrev table to reduce the number of times we
05770      read each abbrev table in.
05771      Alternatives are to punt or to maintain a cache of abbrev tables.
05772      This is simpler and efficient enough for now.
05773 
05774      Later we group TUs by their DW_AT_stmt_list value (as this defines the
05775      symtab to use).  Typically TUs with the same abbrev offset have the same
05776      stmt_list value too so in practice this should work well.
05777 
05778      The basic algorithm here is:
05779 
05780       sort TUs by abbrev table
05781       for each TU with same abbrev table:
05782         read abbrev table if first user
05783         read TU top level DIE
05784           [IWBN if DWO skeletons had DW_AT_stmt_list]
05785         call FUNC  */
05786 
05787   if (dwarf2_read_debug)
05788     fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
05789 
05790   /* Sort in a separate table to maintain the order of all_type_units
05791      for .gdb_index: TU indices directly index all_type_units.  */
05792   sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
05793                               dwarf2_per_objfile->n_type_units);
05794   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
05795     {
05796       struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];
05797 
05798       sorted_by_abbrev[i].sig_type = sig_type;
05799       sorted_by_abbrev[i].abbrev_offset =
05800         read_abbrev_offset (sig_type->per_cu.section,
05801                             sig_type->per_cu.offset);
05802     }
05803   cleanups = make_cleanup (xfree, sorted_by_abbrev);
05804   qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
05805          sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);
05806 
05807   /* Note: In the .gdb_index case, get_type_unit_group may have already been
05808      called any number of times, so we don't reset tu_stats here.  */
05809 
05810   abbrev_offset.sect_off = ~(unsigned) 0;
05811   abbrev_table = NULL;
05812   make_cleanup (abbrev_table_free_cleanup, &abbrev_table);
05813 
05814   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
05815     {
05816       const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];
05817 
05818       /* Switch to the next abbrev table if necessary.  */
05819       if (abbrev_table == NULL
05820           || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
05821         {
05822           if (abbrev_table != NULL)
05823             {
05824               abbrev_table_free (abbrev_table);
05825               /* Reset to NULL in case abbrev_table_read_table throws
05826                  an error: abbrev_table_free_cleanup will get called.  */
05827               abbrev_table = NULL;
05828             }
05829           abbrev_offset = tu->abbrev_offset;
05830           abbrev_table =
05831             abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
05832                                      abbrev_offset);
05833           ++tu_stats->nr_uniq_abbrev_tables;
05834         }
05835 
05836       init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
05837                                func, data);
05838     }
05839 
05840   /* type_unit_groups can be NULL if there is an error in the debug info.
05841      Just create an empty table so the rest of gdb doesn't have to watch
05842      for this error case.  */
05843   if (dwarf2_per_objfile->type_unit_groups == NULL)
05844     {
05845       dwarf2_per_objfile->type_unit_groups =
05846         allocate_type_unit_groups_table ();
05847       dwarf2_per_objfile->n_type_unit_groups = 0;
05848     }
05849 
05850   /* Create a vector of pointers to primary type units to make it easy to
05851      iterate over them and CUs.  See dw2_get_primary_cu.  */
05852   dwarf2_per_objfile->n_type_unit_groups =
05853     htab_elements (dwarf2_per_objfile->type_unit_groups);
05854   dwarf2_per_objfile->all_type_unit_groups =
05855     obstack_alloc (&objfile->objfile_obstack,
05856                    dwarf2_per_objfile->n_type_unit_groups
05857                    * sizeof (struct type_unit_group *));
05858   iter = &dwarf2_per_objfile->all_type_unit_groups[0];
05859   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
05860                           add_type_unit_group_to_table, &iter);
05861   gdb_assert (iter - &dwarf2_per_objfile->all_type_unit_groups[0]
05862               == dwarf2_per_objfile->n_type_unit_groups);
05863 
05864   do_cleanups (cleanups);
05865 
05866   if (dwarf2_read_debug)
05867     {
05868       fprintf_unfiltered (gdb_stdlog, "Done building type unit groups:\n");
05869       fprintf_unfiltered (gdb_stdlog, "  %d TUs\n",
05870                           dwarf2_per_objfile->n_type_units);
05871       fprintf_unfiltered (gdb_stdlog, "  %d uniq abbrev tables\n",
05872                           tu_stats->nr_uniq_abbrev_tables);
05873       fprintf_unfiltered (gdb_stdlog, "  %d symtabs from stmt_list entries\n",
05874                           tu_stats->nr_symtabs);
05875       fprintf_unfiltered (gdb_stdlog, "  %d symtab sharers\n",
05876                           tu_stats->nr_symtab_sharers);
05877       fprintf_unfiltered (gdb_stdlog, "  %d type units without a stmt_list\n",
05878                           tu_stats->nr_stmt_less_type_units);
05879     }
05880 }
05881 
05882 /* Partial symbol tables.  */
05883 
05884 /* Create a psymtab named NAME and assign it to PER_CU.
05885 
05886    The caller must fill in the following details:
05887    dirname, textlow, texthigh.  */
05888 
05889 static struct partial_symtab *
05890 create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
05891 {
05892   struct objfile *objfile = per_cu->objfile;
05893   struct partial_symtab *pst;
05894 
05895   pst = start_psymtab_common (objfile, objfile->section_offsets,
05896                               name, 0,
05897                               objfile->global_psymbols.next,
05898                               objfile->static_psymbols.next);
05899 
05900   pst->psymtabs_addrmap_supported = 1;
05901 
05902   /* This is the glue that links PST into GDB's symbol API.  */
05903   pst->read_symtab_private = per_cu;
05904   pst->read_symtab = dwarf2_read_symtab;
05905   per_cu->v.psymtab = pst;
05906 
05907   return pst;
05908 }
05909 
05910 /* The DATA object passed to process_psymtab_comp_unit_reader has this
05911    type.  */
05912 
05913 struct process_psymtab_comp_unit_data
05914 {
05915   /* True if we are reading a DW_TAG_partial_unit.  */
05916 
05917   int want_partial_unit;
05918 
05919   /* The "pretend" language that is used if the CU doesn't declare a
05920      language.  */
05921 
05922   enum language pretend_language;
05923 };
05924 
05925 /* die_reader_func for process_psymtab_comp_unit.  */
05926 
05927 static void
05928 process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
05929                                   const gdb_byte *info_ptr,
05930                                   struct die_info *comp_unit_die,
05931                                   int has_children,
05932                                   void *data)
05933 {
05934   struct dwarf2_cu *cu = reader->cu;
05935   struct objfile *objfile = cu->objfile;
05936   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
05937   struct attribute *attr;
05938   CORE_ADDR baseaddr;
05939   CORE_ADDR best_lowpc = 0, best_highpc = 0;
05940   struct partial_symtab *pst;
05941   int has_pc_info;
05942   const char *filename;
05943   struct process_psymtab_comp_unit_data *info = data;
05944 
05945   if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
05946     return;
05947 
05948   gdb_assert (! per_cu->is_debug_types);
05949 
05950   prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);
05951 
05952   cu->list_in_scope = &file_symbols;
05953 
05954   /* Allocate a new partial symbol table structure.  */
05955   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
05956   if (attr == NULL || !DW_STRING (attr))
05957     filename = "";
05958   else
05959     filename = DW_STRING (attr);
05960 
05961   pst = create_partial_symtab (per_cu, filename);
05962 
05963   /* This must be done before calling dwarf2_build_include_psymtabs.  */
05964   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
05965   if (attr != NULL)
05966     pst->dirname = DW_STRING (attr);
05967 
05968   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
05969 
05970   dwarf2_find_base_address (comp_unit_die, cu);
05971 
05972   /* Possibly set the default values of LOWPC and HIGHPC from
05973      `DW_AT_ranges'.  */
05974   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
05975                                       &best_highpc, cu, pst);
05976   if (has_pc_info == 1 && best_lowpc < best_highpc)
05977     /* Store the contiguous range if it is not empty; it can be empty for
05978        CUs with no code.  */
05979     addrmap_set_empty (objfile->psymtabs_addrmap,
05980                        best_lowpc + baseaddr,
05981                        best_highpc + baseaddr - 1, pst);
05982 
05983   /* Check if comp unit has_children.
05984      If so, read the rest of the partial symbols from this comp unit.
05985      If not, there's no more debug_info for this comp unit.  */
05986   if (has_children)
05987     {
05988       struct partial_die_info *first_die;
05989       CORE_ADDR lowpc, highpc;
05990 
05991       lowpc = ((CORE_ADDR) -1);
05992       highpc = ((CORE_ADDR) 0);
05993 
05994       first_die = load_partial_dies (reader, info_ptr, 1);
05995 
05996       scan_partial_symbols (first_die, &lowpc, &highpc,
05997                             ! has_pc_info, cu);
05998 
05999       /* If we didn't find a lowpc, set it to highpc to avoid
06000          complaints from `maint check'.  */
06001       if (lowpc == ((CORE_ADDR) -1))
06002         lowpc = highpc;
06003 
06004       /* If the compilation unit didn't have an explicit address range,
06005          then use the information extracted from its child dies.  */
06006       if (! has_pc_info)
06007         {
06008           best_lowpc = lowpc;
06009           best_highpc = highpc;
06010         }
06011     }
06012   pst->textlow = best_lowpc + baseaddr;
06013   pst->texthigh = best_highpc + baseaddr;
06014 
06015   pst->n_global_syms = objfile->global_psymbols.next -
06016     (objfile->global_psymbols.list + pst->globals_offset);
06017   pst->n_static_syms = objfile->static_psymbols.next -
06018     (objfile->static_psymbols.list + pst->statics_offset);
06019   sort_pst_symbols (objfile, pst);
06020 
06021   if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
06022     {
06023       int i;
06024       int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
06025       struct dwarf2_per_cu_data *iter;
06026 
06027       /* Fill in 'dependencies' here; we fill in 'users' in a
06028          post-pass.  */
06029       pst->number_of_dependencies = len;
06030       pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
06031                                          len * sizeof (struct symtab *));
06032       for (i = 0;
06033            VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
06034                         i, iter);
06035            ++i)
06036         pst->dependencies[i] = iter->v.psymtab;
06037 
06038       VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
06039     }
06040 
06041   /* Get the list of files included in the current compilation unit,
06042      and build a psymtab for each of them.  */
06043   dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
06044 
06045   if (dwarf2_read_debug)
06046     {
06047       struct gdbarch *gdbarch = get_objfile_arch (objfile);
06048 
06049       fprintf_unfiltered (gdb_stdlog,
06050                           "Psymtab for %s unit @0x%x: %s - %s"
06051                           ", %d global, %d static syms\n",
06052                           per_cu->is_debug_types ? "type" : "comp",
06053                           per_cu->offset.sect_off,
06054                           paddress (gdbarch, pst->textlow),
06055                           paddress (gdbarch, pst->texthigh),
06056                           pst->n_global_syms, pst->n_static_syms);
06057     }
06058 }
06059 
06060 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
06061    Process compilation unit THIS_CU for a psymtab.  */
06062 
06063 static void
06064 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
06065                            int want_partial_unit,
06066                            enum language pretend_language)
06067 {
06068   struct process_psymtab_comp_unit_data info;
06069 
06070   /* If this compilation unit was already read in, free the
06071      cached copy in order to read it in again.  This is
06072      necessary because we skipped some symbols when we first
06073      read in the compilation unit (see load_partial_dies).
06074      This problem could be avoided, but the benefit is unclear.  */
06075   if (this_cu->cu != NULL)
06076     free_one_cached_comp_unit (this_cu);
06077 
06078   gdb_assert (! this_cu->is_debug_types);
06079   info.want_partial_unit = want_partial_unit;
06080   info.pretend_language = pretend_language;
06081   init_cutu_and_read_dies (this_cu, NULL, 0, 0,
06082                            process_psymtab_comp_unit_reader,
06083                            &info);
06084 
06085   /* Age out any secondary CUs.  */
06086   age_cached_comp_units ();
06087 }
06088 
06089 /* Reader function for build_type_psymtabs.  */
06090 
06091 static void
06092 build_type_psymtabs_reader (const struct die_reader_specs *reader,
06093                             const gdb_byte *info_ptr,
06094                             struct die_info *type_unit_die,
06095                             int has_children,
06096                             void *data)
06097 {
06098   struct objfile *objfile = dwarf2_per_objfile->objfile;
06099   struct dwarf2_cu *cu = reader->cu;
06100   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
06101   struct signatured_type *sig_type;
06102   struct type_unit_group *tu_group;
06103   struct attribute *attr;
06104   struct partial_die_info *first_die;
06105   CORE_ADDR lowpc, highpc;
06106   struct partial_symtab *pst;
06107 
06108   gdb_assert (data == NULL);
06109   gdb_assert (per_cu->is_debug_types);
06110   sig_type = (struct signatured_type *) per_cu;
06111 
06112   if (! has_children)
06113     return;
06114 
06115   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
06116   tu_group = get_type_unit_group (cu, attr);
06117 
06118   VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);
06119 
06120   prepare_one_comp_unit (cu, type_unit_die, language_minimal);
06121   cu->list_in_scope = &file_symbols;
06122   pst = create_partial_symtab (per_cu, "");
06123   pst->anonymous = 1;
06124 
06125   first_die = load_partial_dies (reader, info_ptr, 1);
06126 
06127   lowpc = (CORE_ADDR) -1;
06128   highpc = (CORE_ADDR) 0;
06129   scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
06130 
06131   pst->n_global_syms = objfile->global_psymbols.next -
06132     (objfile->global_psymbols.list + pst->globals_offset);
06133   pst->n_static_syms = objfile->static_psymbols.next -
06134     (objfile->static_psymbols.list + pst->statics_offset);
06135   sort_pst_symbols (objfile, pst);
06136 }
06137 
06138 /* Traversal function for build_type_psymtabs.  */
06139 
06140 static int
06141 build_type_psymtab_dependencies (void **slot, void *info)
06142 {
06143   struct objfile *objfile = dwarf2_per_objfile->objfile;
06144   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
06145   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
06146   struct partial_symtab *pst = per_cu->v.psymtab;
06147   int len = VEC_length (sig_type_ptr, tu_group->tus);
06148   struct signatured_type *iter;
06149   int i;
06150 
06151   gdb_assert (len > 0);
06152   gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));
06153 
06154   pst->number_of_dependencies = len;
06155   pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
06156                                      len * sizeof (struct psymtab *));
06157   for (i = 0;
06158        VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
06159        ++i)
06160     {
06161       gdb_assert (iter->per_cu.is_debug_types);
06162       pst->dependencies[i] = iter->per_cu.v.psymtab;
06163       iter->type_unit_group = tu_group;
06164     }
06165 
06166   VEC_free (sig_type_ptr, tu_group->tus);
06167 
06168   return 1;
06169 }
06170 
06171 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
06172    Build partial symbol tables for the .debug_types comp-units.  */
06173 
06174 static void
06175 build_type_psymtabs (struct objfile *objfile)
06176 {
06177   if (! create_all_type_units (objfile))
06178     return;
06179 
06180   build_type_unit_groups (build_type_psymtabs_reader, NULL);
06181 
06182   /* Now that all TUs have been processed we can fill in the dependencies.  */
06183   htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
06184                           build_type_psymtab_dependencies, NULL);
06185 }
06186 
06187 /* A cleanup function that clears objfile's psymtabs_addrmap field.  */
06188 
06189 static void
06190 psymtabs_addrmap_cleanup (void *o)
06191 {
06192   struct objfile *objfile = o;
06193 
06194   objfile->psymtabs_addrmap = NULL;
06195 }
06196 
06197 /* Compute the 'user' field for each psymtab in OBJFILE.  */
06198 
06199 static void
06200 set_partial_user (struct objfile *objfile)
06201 {
06202   int i;
06203 
06204   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
06205     {
06206       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
06207       struct partial_symtab *pst = per_cu->v.psymtab;
06208       int j;
06209 
06210       if (pst == NULL)
06211         continue;
06212 
06213       for (j = 0; j < pst->number_of_dependencies; ++j)
06214         {
06215           /* Set the 'user' field only if it is not already set.  */
06216           if (pst->dependencies[j]->user == NULL)
06217             pst->dependencies[j]->user = pst;
06218         }
06219     }
06220 }
06221 
06222 /* Build the partial symbol table by doing a quick pass through the
06223    .debug_info and .debug_abbrev sections.  */
06224 
06225 static void
06226 dwarf2_build_psymtabs_hard (struct objfile *objfile)
06227 {
06228   struct cleanup *back_to, *addrmap_cleanup;
06229   struct obstack temp_obstack;
06230   int i;
06231 
06232   if (dwarf2_read_debug)
06233     {
06234       fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
06235                           objfile_name (objfile));
06236     }
06237 
06238   dwarf2_per_objfile->reading_partial_symbols = 1;
06239 
06240   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
06241 
06242   /* Any cached compilation units will be linked by the per-objfile
06243      read_in_chain.  Make sure to free them when we're done.  */
06244   back_to = make_cleanup (free_cached_comp_units, NULL);
06245 
06246   build_type_psymtabs (objfile);
06247 
06248   create_all_comp_units (objfile);
06249 
06250   /* Create a temporary address map on a temporary obstack.  We later
06251      copy this to the final obstack.  */
06252   obstack_init (&temp_obstack);
06253   make_cleanup_obstack_free (&temp_obstack);
06254   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
06255   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
06256 
06257   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
06258     {
06259       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
06260 
06261       process_psymtab_comp_unit (per_cu, 0, language_minimal);
06262     }
06263 
06264   set_partial_user (objfile);
06265 
06266   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
06267                                                     &objfile->objfile_obstack);
06268   discard_cleanups (addrmap_cleanup);
06269 
06270   do_cleanups (back_to);
06271 
06272   if (dwarf2_read_debug)
06273     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
06274                         objfile_name (objfile));
06275 }
06276 
06277 /* die_reader_func for load_partial_comp_unit.  */
06278 
06279 static void
06280 load_partial_comp_unit_reader (const struct die_reader_specs *reader,
06281                                const gdb_byte *info_ptr,
06282                                struct die_info *comp_unit_die,
06283                                int has_children,
06284                                void *data)
06285 {
06286   struct dwarf2_cu *cu = reader->cu;
06287 
06288   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);
06289 
06290   /* Check if comp unit has_children.
06291      If so, read the rest of the partial symbols from this comp unit.
06292      If not, there's no more debug_info for this comp unit.  */
06293   if (has_children)
06294     load_partial_dies (reader, info_ptr, 0);
06295 }
06296 
06297 /* Load the partial DIEs for a secondary CU into memory.
06298    This is also used when rereading a primary CU with load_all_dies.  */
06299 
06300 static void
06301 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
06302 {
06303   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
06304                            load_partial_comp_unit_reader, NULL);
06305 }
06306 
06307 static void
06308 read_comp_units_from_section (struct objfile *objfile,
06309                               struct dwarf2_section_info *section,
06310                               unsigned int is_dwz,
06311                               int *n_allocated,
06312                               int *n_comp_units,
06313                               struct dwarf2_per_cu_data ***all_comp_units)
06314 {
06315   const gdb_byte *info_ptr;
06316   bfd *abfd = get_section_bfd_owner (section);
06317 
06318   if (dwarf2_read_debug)
06319     fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
06320                         get_section_name (section),
06321                         get_section_file_name (section));
06322 
06323   dwarf2_read_section (objfile, section);
06324 
06325   info_ptr = section->buffer;
06326 
06327   while (info_ptr < section->buffer + section->size)
06328     {
06329       unsigned int length, initial_length_size;
06330       struct dwarf2_per_cu_data *this_cu;
06331       sect_offset offset;
06332 
06333       offset.sect_off = info_ptr - section->buffer;
06334 
06335       /* Read just enough information to find out where the next
06336          compilation unit is.  */
06337       length = read_initial_length (abfd, info_ptr, &initial_length_size);
06338 
06339       /* Save the compilation unit for later lookup.  */
06340       this_cu = obstack_alloc (&objfile->objfile_obstack,
06341                                sizeof (struct dwarf2_per_cu_data));
06342       memset (this_cu, 0, sizeof (*this_cu));
06343       this_cu->offset = offset;
06344       this_cu->length = length + initial_length_size;
06345       this_cu->is_dwz = is_dwz;
06346       this_cu->objfile = objfile;
06347       this_cu->section = section;
06348 
06349       if (*n_comp_units == *n_allocated)
06350         {
06351           *n_allocated *= 2;
06352           *all_comp_units = xrealloc (*all_comp_units,
06353                                       *n_allocated
06354                                       * sizeof (struct dwarf2_per_cu_data *));
06355         }
06356       (*all_comp_units)[*n_comp_units] = this_cu;
06357       ++*n_comp_units;
06358 
06359       info_ptr = info_ptr + this_cu->length;
06360     }
06361 }
06362 
06363 /* Create a list of all compilation units in OBJFILE.
06364    This is only done for -readnow and building partial symtabs.  */
06365 
06366 static void
06367 create_all_comp_units (struct objfile *objfile)
06368 {
06369   int n_allocated;
06370   int n_comp_units;
06371   struct dwarf2_per_cu_data **all_comp_units;
06372   struct dwz_file *dwz;
06373 
06374   n_comp_units = 0;
06375   n_allocated = 10;
06376   all_comp_units = xmalloc (n_allocated
06377                             * sizeof (struct dwarf2_per_cu_data *));
06378 
06379   read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
06380                                 &n_allocated, &n_comp_units, &all_comp_units);
06381 
06382   dwz = dwarf2_get_dwz_file ();
06383   if (dwz != NULL)
06384     read_comp_units_from_section (objfile, &dwz->info, 1,
06385                                   &n_allocated, &n_comp_units,
06386                                   &all_comp_units);
06387 
06388   dwarf2_per_objfile->all_comp_units
06389     = obstack_alloc (&objfile->objfile_obstack,
06390                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
06391   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
06392           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
06393   xfree (all_comp_units);
06394   dwarf2_per_objfile->n_comp_units = n_comp_units;
06395 }
06396 
06397 /* Process all loaded DIEs for compilation unit CU, starting at
06398    FIRST_DIE.  The caller should pass NEED_PC == 1 if the compilation
06399    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
06400    DW_AT_ranges).  If NEED_PC is set, then this function will set
06401    *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
06402    and record the covered ranges in the addrmap.  */
06403 
06404 static void
06405 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
06406                       CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
06407 {
06408   struct partial_die_info *pdi;
06409 
06410   /* Now, march along the PDI's, descending into ones which have
06411      interesting children but skipping the children of the other ones,
06412      until we reach the end of the compilation unit.  */
06413 
06414   pdi = first_die;
06415 
06416   while (pdi != NULL)
06417     {
06418       fixup_partial_die (pdi, cu);
06419 
06420       /* Anonymous namespaces or modules have no name but have interesting
06421          children, so we need to look at them.  Ditto for anonymous
06422          enums.  */
06423 
06424       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
06425           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
06426           || pdi->tag == DW_TAG_imported_unit)
06427         {
06428           switch (pdi->tag)
06429             {
06430             case DW_TAG_subprogram:
06431               add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
06432               break;
06433             case DW_TAG_constant:
06434             case DW_TAG_variable:
06435             case DW_TAG_typedef:
06436             case DW_TAG_union_type:
06437               if (!pdi->is_declaration)
06438                 {
06439                   add_partial_symbol (pdi, cu);
06440                 }
06441               break;
06442             case DW_TAG_class_type:
06443             case DW_TAG_interface_type:
06444             case DW_TAG_structure_type:
06445               if (!pdi->is_declaration)
06446                 {
06447                   add_partial_symbol (pdi, cu);
06448                 }
06449               break;
06450             case DW_TAG_enumeration_type:
06451               if (!pdi->is_declaration)
06452                 add_partial_enumeration (pdi, cu);
06453               break;
06454             case DW_TAG_base_type:
06455             case DW_TAG_subrange_type:
06456               /* File scope base type definitions are added to the partial
06457                  symbol table.  */
06458               add_partial_symbol (pdi, cu);
06459               break;
06460             case DW_TAG_namespace:
06461               add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
06462               break;
06463             case DW_TAG_module:
06464               add_partial_module (pdi, lowpc, highpc, need_pc, cu);
06465               break;
06466             case DW_TAG_imported_unit:
06467               {
06468                 struct dwarf2_per_cu_data *per_cu;
06469 
06470                 /* For now we don't handle imported units in type units.  */
06471                 if (cu->per_cu->is_debug_types)
06472                   {
06473                     error (_("Dwarf Error: DW_TAG_imported_unit is not"
06474                              " supported in type units [in module %s]"),
06475                            objfile_name (cu->objfile));
06476                   }
06477 
06478                 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
06479                                                            pdi->is_dwz,
06480                                                            cu->objfile);
06481 
06482                 /* Go read the partial unit, if needed.  */
06483                 if (per_cu->v.psymtab == NULL)
06484                   process_psymtab_comp_unit (per_cu, 1, cu->language);
06485 
06486                 VEC_safe_push (dwarf2_per_cu_ptr,
06487                                cu->per_cu->imported_symtabs, per_cu);
06488               }
06489               break;
06490             default:
06491               break;
06492             }
06493         }
06494 
06495       /* If the die has a sibling, skip to the sibling.  */
06496 
06497       pdi = pdi->die_sibling;
06498     }
06499 }
06500 
06501 /* Functions used to compute the fully scoped name of a partial DIE.
06502 
06503    Normally, this is simple.  For C++, the parent DIE's fully scoped
06504    name is concatenated with "::" and the partial DIE's name.  For
06505    Java, the same thing occurs except that "." is used instead of "::".
06506    Enumerators are an exception; they use the scope of their parent
06507    enumeration type, i.e. the name of the enumeration type is not
06508    prepended to the enumerator.
06509 
06510    There are two complexities.  One is DW_AT_specification; in this
06511    case "parent" means the parent of the target of the specification,
06512    instead of the direct parent of the DIE.  The other is compilers
06513    which do not emit DW_TAG_namespace; in this case we try to guess
06514    the fully qualified name of structure types from their members'
06515    linkage names.  This must be done using the DIE's children rather
06516    than the children of any DW_AT_specification target.  We only need
06517    to do this for structures at the top level, i.e. if the target of
06518    any DW_AT_specification (if any; otherwise the DIE itself) does not
06519    have a parent.  */
06520 
06521 /* Compute the scope prefix associated with PDI's parent, in
06522    compilation unit CU.  The result will be allocated on CU's
06523    comp_unit_obstack, or a copy of the already allocated PDI->NAME
06524    field.  NULL is returned if no prefix is necessary.  */
06525 static const char *
06526 partial_die_parent_scope (struct partial_die_info *pdi,
06527                           struct dwarf2_cu *cu)
06528 {
06529   const char *grandparent_scope;
06530   struct partial_die_info *parent, *real_pdi;
06531 
06532   /* We need to look at our parent DIE; if we have a DW_AT_specification,
06533      then this means the parent of the specification DIE.  */
06534 
06535   real_pdi = pdi;
06536   while (real_pdi->has_specification)
06537     real_pdi = find_partial_die (real_pdi->spec_offset,
06538                                  real_pdi->spec_is_dwz, cu);
06539 
06540   parent = real_pdi->die_parent;
06541   if (parent == NULL)
06542     return NULL;
06543 
06544   if (parent->scope_set)
06545     return parent->scope;
06546 
06547   fixup_partial_die (parent, cu);
06548 
06549   grandparent_scope = partial_die_parent_scope (parent, cu);
06550 
06551   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
06552      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
06553      Work around this problem here.  */
06554   if (cu->language == language_cplus
06555       && parent->tag == DW_TAG_namespace
06556       && strcmp (parent->name, "::") == 0
06557       && grandparent_scope == NULL)
06558     {
06559       parent->scope = NULL;
06560       parent->scope_set = 1;
06561       return NULL;
06562     }
06563 
06564   if (pdi->tag == DW_TAG_enumerator)
06565     /* Enumerators should not get the name of the enumeration as a prefix.  */
06566     parent->scope = grandparent_scope;
06567   else if (parent->tag == DW_TAG_namespace
06568       || parent->tag == DW_TAG_module
06569       || parent->tag == DW_TAG_structure_type
06570       || parent->tag == DW_TAG_class_type
06571       || parent->tag == DW_TAG_interface_type
06572       || parent->tag == DW_TAG_union_type
06573       || parent->tag == DW_TAG_enumeration_type)
06574     {
06575       if (grandparent_scope == NULL)
06576         parent->scope = parent->name;
06577       else
06578         parent->scope = typename_concat (&cu->comp_unit_obstack,
06579                                          grandparent_scope,
06580                                          parent->name, 0, cu);
06581     }
06582   else
06583     {
06584       /* FIXME drow/2004-04-01: What should we be doing with
06585          function-local names?  For partial symbols, we should probably be
06586          ignoring them.  */
06587       complaint (&symfile_complaints,
06588                  _("unhandled containing DIE tag %d for DIE at %d"),
06589                  parent->tag, pdi->offset.sect_off);
06590       parent->scope = grandparent_scope;
06591     }
06592 
06593   parent->scope_set = 1;
06594   return parent->scope;
06595 }
06596 
06597 /* Return the fully scoped name associated with PDI, from compilation unit
06598    CU.  The result will be allocated with malloc.  */
06599 
06600 static char *
06601 partial_die_full_name (struct partial_die_info *pdi,
06602                        struct dwarf2_cu *cu)
06603 {
06604   const char *parent_scope;
06605 
06606   /* If this is a template instantiation, we can not work out the
06607      template arguments from partial DIEs.  So, unfortunately, we have
06608      to go through the full DIEs.  At least any work we do building
06609      types here will be reused if full symbols are loaded later.  */
06610   if (pdi->has_template_arguments)
06611     {
06612       fixup_partial_die (pdi, cu);
06613 
06614       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
06615         {
06616           struct die_info *die;
06617           struct attribute attr;
06618           struct dwarf2_cu *ref_cu = cu;
06619 
06620           /* DW_FORM_ref_addr is using section offset.  */
06621           attr.name = 0;
06622           attr.form = DW_FORM_ref_addr;
06623           attr.u.unsnd = pdi->offset.sect_off;
06624           die = follow_die_ref (NULL, &attr, &ref_cu);
06625 
06626           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
06627         }
06628     }
06629 
06630   parent_scope = partial_die_parent_scope (pdi, cu);
06631   if (parent_scope == NULL)
06632     return NULL;
06633   else
06634     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
06635 }
06636 
06637 static void
06638 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
06639 {
06640   struct objfile *objfile = cu->objfile;
06641   CORE_ADDR addr = 0;
06642   const char *actual_name = NULL;
06643   CORE_ADDR baseaddr;
06644   char *built_actual_name;
06645 
06646   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
06647 
06648   built_actual_name = partial_die_full_name (pdi, cu);
06649   if (built_actual_name != NULL)
06650     actual_name = built_actual_name;
06651 
06652   if (actual_name == NULL)
06653     actual_name = pdi->name;
06654 
06655   switch (pdi->tag)
06656     {
06657     case DW_TAG_subprogram:
06658       if (pdi->is_external || cu->language == language_ada)
06659         {
06660           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
06661              of the global scope.  But in Ada, we want to be able to access
06662              nested procedures globally.  So all Ada subprograms are stored
06663              in the global scope.  */
06664           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
06665              mst_text, objfile); */
06666           add_psymbol_to_list (actual_name, strlen (actual_name),
06667                                built_actual_name != NULL,
06668                                VAR_DOMAIN, LOC_BLOCK,
06669                                &objfile->global_psymbols,
06670                                0, pdi->lowpc + baseaddr,
06671                                cu->language, objfile);
06672         }
06673       else
06674         {
06675           /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
06676              mst_file_text, objfile); */
06677           add_psymbol_to_list (actual_name, strlen (actual_name),
06678                                built_actual_name != NULL,
06679                                VAR_DOMAIN, LOC_BLOCK,
06680                                &objfile->static_psymbols,
06681                                0, pdi->lowpc + baseaddr,
06682                                cu->language, objfile);
06683         }
06684       break;
06685     case DW_TAG_constant:
06686       {
06687         struct psymbol_allocation_list *list;
06688 
06689         if (pdi->is_external)
06690           list = &objfile->global_psymbols;
06691         else
06692           list = &objfile->static_psymbols;
06693         add_psymbol_to_list (actual_name, strlen (actual_name),
06694                              built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
06695                              list, 0, 0, cu->language, objfile);
06696       }
06697       break;
06698     case DW_TAG_variable:
06699       if (pdi->d.locdesc)
06700         addr = decode_locdesc (pdi->d.locdesc, cu);
06701 
06702       if (pdi->d.locdesc
06703           && addr == 0
06704           && !dwarf2_per_objfile->has_section_at_zero)
06705         {
06706           /* A global or static variable may also have been stripped
06707              out by the linker if unused, in which case its address
06708              will be nullified; do not add such variables into partial
06709              symbol table then.  */
06710         }
06711       else if (pdi->is_external)
06712         {
06713           /* Global Variable.
06714              Don't enter into the minimal symbol tables as there is
06715              a minimal symbol table entry from the ELF symbols already.
06716              Enter into partial symbol table if it has a location
06717              descriptor or a type.
06718              If the location descriptor is missing, new_symbol will create
06719              a LOC_UNRESOLVED symbol, the address of the variable will then
06720              be determined from the minimal symbol table whenever the variable
06721              is referenced.
06722              The address for the partial symbol table entry is not
06723              used by GDB, but it comes in handy for debugging partial symbol
06724              table building.  */
06725 
06726           if (pdi->d.locdesc || pdi->has_type)
06727             add_psymbol_to_list (actual_name, strlen (actual_name),
06728                                  built_actual_name != NULL,
06729                                  VAR_DOMAIN, LOC_STATIC,
06730                                  &objfile->global_psymbols,
06731                                  0, addr + baseaddr,
06732                                  cu->language, objfile);
06733         }
06734       else
06735         {
06736           /* Static Variable.  Skip symbols without location descriptors.  */
06737           if (pdi->d.locdesc == NULL)
06738             {
06739               xfree (built_actual_name);
06740               return;
06741             }
06742           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
06743              mst_file_data, objfile); */
06744           add_psymbol_to_list (actual_name, strlen (actual_name),
06745                                built_actual_name != NULL,
06746                                VAR_DOMAIN, LOC_STATIC,
06747                                &objfile->static_psymbols,
06748                                0, addr + baseaddr,
06749                                cu->language, objfile);
06750         }
06751       break;
06752     case DW_TAG_typedef:
06753     case DW_TAG_base_type:
06754     case DW_TAG_subrange_type:
06755       add_psymbol_to_list (actual_name, strlen (actual_name),
06756                            built_actual_name != NULL,
06757                            VAR_DOMAIN, LOC_TYPEDEF,
06758                            &objfile->static_psymbols,
06759                            0, (CORE_ADDR) 0, cu->language, objfile);
06760       break;
06761     case DW_TAG_namespace:
06762       add_psymbol_to_list (actual_name, strlen (actual_name),
06763                            built_actual_name != NULL,
06764                            VAR_DOMAIN, LOC_TYPEDEF,
06765                            &objfile->global_psymbols,
06766                            0, (CORE_ADDR) 0, cu->language, objfile);
06767       break;
06768     case DW_TAG_class_type:
06769     case DW_TAG_interface_type:
06770     case DW_TAG_structure_type:
06771     case DW_TAG_union_type:
06772     case DW_TAG_enumeration_type:
06773       /* Skip external references.  The DWARF standard says in the section
06774          about "Structure, Union, and Class Type Entries": "An incomplete
06775          structure, union or class type is represented by a structure,
06776          union or class entry that does not have a byte size attribute
06777          and that has a DW_AT_declaration attribute."  */
06778       if (!pdi->has_byte_size && pdi->is_declaration)
06779         {
06780           xfree (built_actual_name);
06781           return;
06782         }
06783 
06784       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
06785          static vs. global.  */
06786       add_psymbol_to_list (actual_name, strlen (actual_name),
06787                            built_actual_name != NULL,
06788                            STRUCT_DOMAIN, LOC_TYPEDEF,
06789                            (cu->language == language_cplus
06790                             || cu->language == language_java)
06791                            ? &objfile->global_psymbols
06792                            : &objfile->static_psymbols,
06793                            0, (CORE_ADDR) 0, cu->language, objfile);
06794 
06795       break;
06796     case DW_TAG_enumerator:
06797       add_psymbol_to_list (actual_name, strlen (actual_name),
06798                            built_actual_name != NULL,
06799                            VAR_DOMAIN, LOC_CONST,
06800                            (cu->language == language_cplus
06801                             || cu->language == language_java)
06802                            ? &objfile->global_psymbols
06803                            : &objfile->static_psymbols,
06804                            0, (CORE_ADDR) 0, cu->language, objfile);
06805       break;
06806     default:
06807       break;
06808     }
06809 
06810   xfree (built_actual_name);
06811 }
06812 
06813 /* Read a partial die corresponding to a namespace; also, add a symbol
06814    corresponding to that namespace to the symbol table.  NAMESPACE is
06815    the name of the enclosing namespace.  */
06816 
06817 static void
06818 add_partial_namespace (struct partial_die_info *pdi,
06819                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
06820                        int need_pc, struct dwarf2_cu *cu)
06821 {
06822   /* Add a symbol for the namespace.  */
06823 
06824   add_partial_symbol (pdi, cu);
06825 
06826   /* Now scan partial symbols in that namespace.  */
06827 
06828   if (pdi->has_children)
06829     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
06830 }
06831 
06832 /* Read a partial die corresponding to a Fortran module.  */
06833 
06834 static void
06835 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
06836                     CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
06837 {
06838   /* Now scan partial symbols in that module.  */
06839 
06840   if (pdi->has_children)
06841     scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
06842 }
06843 
06844 /* Read a partial die corresponding to a subprogram and create a partial
06845    symbol for that subprogram.  When the CU language allows it, this
06846    routine also defines a partial symbol for each nested subprogram
06847    that this subprogram contains.
06848 
06849    DIE my also be a lexical block, in which case we simply search
06850    recursively for suprograms defined inside that lexical block.
06851    Again, this is only performed when the CU language allows this
06852    type of definitions.  */
06853 
06854 static void
06855 add_partial_subprogram (struct partial_die_info *pdi,
06856                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
06857                         int need_pc, struct dwarf2_cu *cu)
06858 {
06859   if (pdi->tag == DW_TAG_subprogram)
06860     {
06861       if (pdi->has_pc_info)
06862         {
06863           if (pdi->lowpc < *lowpc)
06864             *lowpc = pdi->lowpc;
06865           if (pdi->highpc > *highpc)
06866             *highpc = pdi->highpc;
06867           if (need_pc)
06868             {
06869               CORE_ADDR baseaddr;
06870               struct objfile *objfile = cu->objfile;
06871 
06872               baseaddr = ANOFFSET (objfile->section_offsets,
06873                                    SECT_OFF_TEXT (objfile));
06874               addrmap_set_empty (objfile->psymtabs_addrmap,
06875                                  pdi->lowpc + baseaddr,
06876                                  pdi->highpc - 1 + baseaddr,
06877                                  cu->per_cu->v.psymtab);
06878             }
06879         }
06880 
06881       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
06882         {
06883           if (!pdi->is_declaration)
06884             /* Ignore subprogram DIEs that do not have a name, they are
06885                illegal.  Do not emit a complaint at this point, we will
06886                do so when we convert this psymtab into a symtab.  */
06887             if (pdi->name)
06888               add_partial_symbol (pdi, cu);
06889         }
06890     }
06891 
06892   if (! pdi->has_children)
06893     return;
06894 
06895   if (cu->language == language_ada)
06896     {
06897       pdi = pdi->die_child;
06898       while (pdi != NULL)
06899         {
06900           fixup_partial_die (pdi, cu);
06901           if (pdi->tag == DW_TAG_subprogram
06902               || pdi->tag == DW_TAG_lexical_block)
06903             add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
06904           pdi = pdi->die_sibling;
06905         }
06906     }
06907 }
06908 
06909 /* Read a partial die corresponding to an enumeration type.  */
06910 
06911 static void
06912 add_partial_enumeration (struct partial_die_info *enum_pdi,
06913                          struct dwarf2_cu *cu)
06914 {
06915   struct partial_die_info *pdi;
06916 
06917   if (enum_pdi->name != NULL)
06918     add_partial_symbol (enum_pdi, cu);
06919 
06920   pdi = enum_pdi->die_child;
06921   while (pdi)
06922     {
06923       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
06924         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
06925       else
06926         add_partial_symbol (pdi, cu);
06927       pdi = pdi->die_sibling;
06928     }
06929 }
06930 
06931 /* Return the initial uleb128 in the die at INFO_PTR.  */
06932 
06933 static unsigned int
06934 peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
06935 {
06936   unsigned int bytes_read;
06937 
06938   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
06939 }
06940 
06941 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
06942    Return the corresponding abbrev, or NULL if the number is zero (indicating
06943    an empty DIE).  In either case *BYTES_READ will be set to the length of
06944    the initial number.  */
06945 
06946 static struct abbrev_info *
06947 peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
06948                  struct dwarf2_cu *cu)
06949 {
06950   bfd *abfd = cu->objfile->obfd;
06951   unsigned int abbrev_number;
06952   struct abbrev_info *abbrev;
06953 
06954   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
06955 
06956   if (abbrev_number == 0)
06957     return NULL;
06958 
06959   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
06960   if (!abbrev)
06961     {
06962       error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
06963              abbrev_number, bfd_get_filename (abfd));
06964     }
06965 
06966   return abbrev;
06967 }
06968 
06969 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
06970    Returns a pointer to the end of a series of DIEs, terminated by an empty
06971    DIE.  Any children of the skipped DIEs will also be skipped.  */
06972 
06973 static const gdb_byte *
06974 skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
06975 {
06976   struct dwarf2_cu *cu = reader->cu;
06977   struct abbrev_info *abbrev;
06978   unsigned int bytes_read;
06979 
06980   while (1)
06981     {
06982       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
06983       if (abbrev == NULL)
06984         return info_ptr + bytes_read;
06985       else
06986         info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
06987     }
06988 }
06989 
06990 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
06991    INFO_PTR should point just after the initial uleb128 of a DIE, and the
06992    abbrev corresponding to that skipped uleb128 should be passed in
06993    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
06994    children.  */
06995 
06996 static const gdb_byte *
06997 skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
06998               struct abbrev_info *abbrev)
06999 {
07000   unsigned int bytes_read;
07001   struct attribute attr;
07002   bfd *abfd = reader->abfd;
07003   struct dwarf2_cu *cu = reader->cu;
07004   const gdb_byte *buffer = reader->buffer;
07005   const gdb_byte *buffer_end = reader->buffer_end;
07006   const gdb_byte *start_info_ptr = info_ptr;
07007   unsigned int form, i;
07008 
07009   for (i = 0; i < abbrev->num_attrs; i++)
07010     {
07011       /* The only abbrev we care about is DW_AT_sibling.  */
07012       if (abbrev->attrs[i].name == DW_AT_sibling)
07013         {
07014           read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
07015           if (attr.form == DW_FORM_ref_addr)
07016             complaint (&symfile_complaints,
07017                        _("ignoring absolute DW_AT_sibling"));
07018           else
07019             return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
07020         }
07021 
07022       /* If it isn't DW_AT_sibling, skip this attribute.  */
07023       form = abbrev->attrs[i].form;
07024     skip_attribute:
07025       switch (form)
07026         {
07027         case DW_FORM_ref_addr:
07028           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
07029              and later it is offset sized.  */
07030           if (cu->header.version == 2)
07031             info_ptr += cu->header.addr_size;
07032           else
07033             info_ptr += cu->header.offset_size;
07034           break;
07035         case DW_FORM_GNU_ref_alt:
07036           info_ptr += cu->header.offset_size;
07037           break;
07038         case DW_FORM_addr:
07039           info_ptr += cu->header.addr_size;
07040           break;
07041         case DW_FORM_data1:
07042         case DW_FORM_ref1:
07043         case DW_FORM_flag:
07044           info_ptr += 1;
07045           break;
07046         case DW_FORM_flag_present:
07047           break;
07048         case DW_FORM_data2:
07049         case DW_FORM_ref2:
07050           info_ptr += 2;
07051           break;
07052         case DW_FORM_data4:
07053         case DW_FORM_ref4:
07054           info_ptr += 4;
07055           break;
07056         case DW_FORM_data8:
07057         case DW_FORM_ref8:
07058         case DW_FORM_ref_sig8:
07059           info_ptr += 8;
07060           break;
07061         case DW_FORM_string:
07062           read_direct_string (abfd, info_ptr, &bytes_read);
07063           info_ptr += bytes_read;
07064           break;
07065         case DW_FORM_sec_offset:
07066         case DW_FORM_strp:
07067         case DW_FORM_GNU_strp_alt:
07068           info_ptr += cu->header.offset_size;
07069           break;
07070         case DW_FORM_exprloc:
07071         case DW_FORM_block:
07072           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
07073           info_ptr += bytes_read;
07074           break;
07075         case DW_FORM_block1:
07076           info_ptr += 1 + read_1_byte (abfd, info_ptr);
07077           break;
07078         case DW_FORM_block2:
07079           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
07080           break;
07081         case DW_FORM_block4:
07082           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
07083           break;
07084         case DW_FORM_sdata:
07085         case DW_FORM_udata:
07086         case DW_FORM_ref_udata:
07087         case DW_FORM_GNU_addr_index:
07088         case DW_FORM_GNU_str_index:
07089           info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
07090           break;
07091         case DW_FORM_indirect:
07092           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
07093           info_ptr += bytes_read;
07094           /* We need to continue parsing from here, so just go back to
07095              the top.  */
07096           goto skip_attribute;
07097 
07098         default:
07099           error (_("Dwarf Error: Cannot handle %s "
07100                    "in DWARF reader [in module %s]"),
07101                  dwarf_form_name (form),
07102                  bfd_get_filename (abfd));
07103         }
07104     }
07105 
07106   if (abbrev->has_children)
07107     return skip_children (reader, info_ptr);
07108   else
07109     return info_ptr;
07110 }
07111 
07112 /* Locate ORIG_PDI's sibling.
07113    INFO_PTR should point to the start of the next DIE after ORIG_PDI.  */
07114 
07115 static const gdb_byte *
07116 locate_pdi_sibling (const struct die_reader_specs *reader,
07117                     struct partial_die_info *orig_pdi,
07118                     const gdb_byte *info_ptr)
07119 {
07120   /* Do we know the sibling already?  */
07121 
07122   if (orig_pdi->sibling)
07123     return orig_pdi->sibling;
07124 
07125   /* Are there any children to deal with?  */
07126 
07127   if (!orig_pdi->has_children)
07128     return info_ptr;
07129 
07130   /* Skip the children the long way.  */
07131 
07132   return skip_children (reader, info_ptr);
07133 }
07134 
07135 /* Expand this partial symbol table into a full symbol table.  SELF is
07136    not NULL.  */
07137 
07138 static void
07139 dwarf2_read_symtab (struct partial_symtab *self,
07140                     struct objfile *objfile)
07141 {
07142   if (self->readin)
07143     {
07144       warning (_("bug: psymtab for %s is already read in."),
07145                self->filename);
07146     }
07147   else
07148     {
07149       if (info_verbose)
07150         {
07151           printf_filtered (_("Reading in symbols for %s..."),
07152                            self->filename);
07153           gdb_flush (gdb_stdout);
07154         }
07155 
07156       /* Restore our global data.  */
07157       dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
07158 
07159       /* If this psymtab is constructed from a debug-only objfile, the
07160          has_section_at_zero flag will not necessarily be correct.  We
07161          can get the correct value for this flag by looking at the data
07162          associated with the (presumably stripped) associated objfile.  */
07163       if (objfile->separate_debug_objfile_backlink)
07164         {
07165           struct dwarf2_per_objfile *dpo_backlink
07166             = objfile_data (objfile->separate_debug_objfile_backlink,
07167                             dwarf2_objfile_data_key);
07168 
07169           dwarf2_per_objfile->has_section_at_zero
07170             = dpo_backlink->has_section_at_zero;
07171         }
07172 
07173       dwarf2_per_objfile->reading_partial_symbols = 0;
07174 
07175       psymtab_to_symtab_1 (self);
07176 
07177       /* Finish up the debug error message.  */
07178       if (info_verbose)
07179         printf_filtered (_("done.\n"));
07180     }
07181 
07182   process_cu_includes ();
07183 }
07184 
07185 /* Reading in full CUs.  */
07186 
07187 /* Add PER_CU to the queue.  */
07188 
07189 static void
07190 queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
07191                  enum language pretend_language)
07192 {
07193   struct dwarf2_queue_item *item;
07194 
07195   per_cu->queued = 1;
07196   item = xmalloc (sizeof (*item));
07197   item->per_cu = per_cu;
07198   item->pretend_language = pretend_language;
07199   item->next = NULL;
07200 
07201   if (dwarf2_queue == NULL)
07202     dwarf2_queue = item;
07203   else
07204     dwarf2_queue_tail->next = item;
07205 
07206   dwarf2_queue_tail = item;
07207 }
07208 
07209 /* If PER_CU is not yet queued, add it to the queue.
07210    If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
07211    dependency.
07212    The result is non-zero if PER_CU was queued, otherwise the result is zero
07213    meaning either PER_CU is already queued or it is already loaded.
07214 
07215    N.B. There is an invariant here that if a CU is queued then it is loaded.
07216    The caller is required to load PER_CU if we return non-zero.  */
07217 
07218 static int
07219 maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
07220                        struct dwarf2_per_cu_data *per_cu,
07221                        enum language pretend_language)
07222 {
07223   /* We may arrive here during partial symbol reading, if we need full
07224      DIEs to process an unusual case (e.g. template arguments).  Do
07225      not queue PER_CU, just tell our caller to load its DIEs.  */
07226   if (dwarf2_per_objfile->reading_partial_symbols)
07227     {
07228       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
07229         return 1;
07230       return 0;
07231     }
07232 
07233   /* Mark the dependence relation so that we don't flush PER_CU
07234      too early.  */
07235   if (dependent_cu != NULL)
07236     dwarf2_add_dependence (dependent_cu, per_cu);
07237 
07238   /* If it's already on the queue, we have nothing to do.  */
07239   if (per_cu->queued)
07240     return 0;
07241 
07242   /* If the compilation unit is already loaded, just mark it as
07243      used.  */
07244   if (per_cu->cu != NULL)
07245     {
07246       per_cu->cu->last_used = 0;
07247       return 0;
07248     }
07249 
07250   /* Add it to the queue.  */
07251   queue_comp_unit (per_cu, pretend_language);
07252 
07253   return 1;
07254 }
07255 
07256 /* Process the queue.  */
07257 
07258 static void
07259 process_queue (void)
07260 {
07261   struct dwarf2_queue_item *item, *next_item;
07262 
07263   if (dwarf2_read_debug)
07264     {
07265       fprintf_unfiltered (gdb_stdlog,
07266                           "Expanding one or more symtabs of objfile %s ...\n",
07267                           objfile_name (dwarf2_per_objfile->objfile));
07268     }
07269 
07270   /* The queue starts out with one item, but following a DIE reference
07271      may load a new CU, adding it to the end of the queue.  */
07272   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
07273     {
07274       if (dwarf2_per_objfile->using_index
07275           ? !item->per_cu->v.quick->symtab
07276           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
07277         {
07278           struct dwarf2_per_cu_data *per_cu = item->per_cu;
07279           char buf[100];
07280 
07281           if (per_cu->is_debug_types)
07282             {
07283               struct signatured_type *sig_type =
07284                 (struct signatured_type *) per_cu;
07285 
07286               sprintf (buf, "TU %s at offset 0x%x",
07287                        hex_string (sig_type->signature), per_cu->offset.sect_off);
07288             }
07289           else
07290             sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
07291 
07292           if (dwarf2_read_debug)
07293             fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
07294 
07295           if (per_cu->is_debug_types)
07296             process_full_type_unit (per_cu, item->pretend_language);
07297           else
07298             process_full_comp_unit (per_cu, item->pretend_language);
07299 
07300           if (dwarf2_read_debug)
07301             fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
07302         }
07303 
07304       item->per_cu->queued = 0;
07305       next_item = item->next;
07306       xfree (item);
07307     }
07308 
07309   dwarf2_queue_tail = NULL;
07310 
07311   if (dwarf2_read_debug)
07312     {
07313       fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
07314                           objfile_name (dwarf2_per_objfile->objfile));
07315     }
07316 }
07317 
07318 /* Free all allocated queue entries.  This function only releases anything if
07319    an error was thrown; if the queue was processed then it would have been
07320    freed as we went along.  */
07321 
07322 static void
07323 dwarf2_release_queue (void *dummy)
07324 {
07325   struct dwarf2_queue_item *item, *last;
07326 
07327   item = dwarf2_queue;
07328   while (item)
07329     {
07330       /* Anything still marked queued is likely to be in an
07331          inconsistent state, so discard it.  */
07332       if (item->per_cu->queued)
07333         {
07334           if (item->per_cu->cu != NULL)
07335             free_one_cached_comp_unit (item->per_cu);
07336           item->per_cu->queued = 0;
07337         }
07338 
07339       last = item;
07340       item = item->next;
07341       xfree (last);
07342     }
07343 
07344   dwarf2_queue = dwarf2_queue_tail = NULL;
07345 }
07346 
07347 /* Read in full symbols for PST, and anything it depends on.  */
07348 
07349 static void
07350 psymtab_to_symtab_1 (struct partial_symtab *pst)
07351 {
07352   struct dwarf2_per_cu_data *per_cu;
07353   int i;
07354 
07355   if (pst->readin)
07356     return;
07357 
07358   for (i = 0; i < pst->number_of_dependencies; i++)
07359     if (!pst->dependencies[i]->readin
07360         && pst->dependencies[i]->user == NULL)
07361       {
07362         /* Inform about additional files that need to be read in.  */
07363         if (info_verbose)
07364           {
07365             /* FIXME: i18n: Need to make this a single string.  */
07366             fputs_filtered (" ", gdb_stdout);
07367             wrap_here ("");
07368             fputs_filtered ("and ", gdb_stdout);
07369             wrap_here ("");
07370             printf_filtered ("%s...", pst->dependencies[i]->filename);
07371             wrap_here ("");     /* Flush output.  */
07372             gdb_flush (gdb_stdout);
07373           }
07374         psymtab_to_symtab_1 (pst->dependencies[i]);
07375       }
07376 
07377   per_cu = pst->read_symtab_private;
07378 
07379   if (per_cu == NULL)
07380     {
07381       /* It's an include file, no symbols to read for it.
07382          Everything is in the parent symtab.  */
07383       pst->readin = 1;
07384       return;
07385     }
07386 
07387   dw2_do_instantiate_symtab (per_cu);
07388 }
07389 
07390 /* Trivial hash function for die_info: the hash value of a DIE
07391    is its offset in .debug_info for this objfile.  */
07392 
07393 static hashval_t
07394 die_hash (const void *item)
07395 {
07396   const struct die_info *die = item;
07397 
07398   return die->offset.sect_off;
07399 }
07400 
07401 /* Trivial comparison function for die_info structures: two DIEs
07402    are equal if they have the same offset.  */
07403 
07404 static int
07405 die_eq (const void *item_lhs, const void *item_rhs)
07406 {
07407   const struct die_info *die_lhs = item_lhs;
07408   const struct die_info *die_rhs = item_rhs;
07409 
07410   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
07411 }
07412 
07413 /* die_reader_func for load_full_comp_unit.
07414    This is identical to read_signatured_type_reader,
07415    but is kept separate for now.  */
07416 
07417 static void
07418 load_full_comp_unit_reader (const struct die_reader_specs *reader,
07419                             const gdb_byte *info_ptr,
07420                             struct die_info *comp_unit_die,
07421                             int has_children,
07422                             void *data)
07423 {
07424   struct dwarf2_cu *cu = reader->cu;
07425   enum language *language_ptr = data;
07426 
07427   gdb_assert (cu->die_hash == NULL);
07428   cu->die_hash =
07429     htab_create_alloc_ex (cu->header.length / 12,
07430                           die_hash,
07431                           die_eq,
07432                           NULL,
07433                           &cu->comp_unit_obstack,
07434                           hashtab_obstack_allocate,
07435                           dummy_obstack_deallocate);
07436 
07437   if (has_children)
07438     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
07439                                                   &info_ptr, comp_unit_die);
07440   cu->dies = comp_unit_die;
07441   /* comp_unit_die is not stored in die_hash, no need.  */
07442 
07443   /* We try not to read any attributes in this function, because not
07444      all CUs needed for references have been loaded yet, and symbol
07445      table processing isn't initialized.  But we have to set the CU language,
07446      or we won't be able to build types correctly.
07447      Similarly, if we do not read the producer, we can not apply
07448      producer-specific interpretation.  */
07449   prepare_one_comp_unit (cu, cu->dies, *language_ptr);
07450 }
07451 
07452 /* Load the DIEs associated with PER_CU into memory.  */
07453 
07454 static void
07455 load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
07456                      enum language pretend_language)
07457 {
07458   gdb_assert (! this_cu->is_debug_types);
07459 
07460   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
07461                            load_full_comp_unit_reader, &pretend_language);
07462 }
07463 
07464 /* Add a DIE to the delayed physname list.  */
07465 
07466 static void
07467 add_to_method_list (struct type *type, int fnfield_index, int index,
07468                     const char *name, struct die_info *die,
07469                     struct dwarf2_cu *cu)
07470 {
07471   struct delayed_method_info mi;
07472   mi.type = type;
07473   mi.fnfield_index = fnfield_index;
07474   mi.index = index;
07475   mi.name = name;
07476   mi.die = die;
07477   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
07478 }
07479 
07480 /* A cleanup for freeing the delayed method list.  */
07481 
07482 static void
07483 free_delayed_list (void *ptr)
07484 {
07485   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
07486   if (cu->method_list != NULL)
07487     {
07488       VEC_free (delayed_method_info, cu->method_list);
07489       cu->method_list = NULL;
07490     }
07491 }
07492 
07493 /* Compute the physnames of any methods on the CU's method list.
07494 
07495    The computation of method physnames is delayed in order to avoid the
07496    (bad) condition that one of the method's formal parameters is of an as yet
07497    incomplete type.  */
07498 
07499 static void
07500 compute_delayed_physnames (struct dwarf2_cu *cu)
07501 {
07502   int i;
07503   struct delayed_method_info *mi;
07504   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
07505     {
07506       const char *physname;
07507       struct fn_fieldlist *fn_flp
07508         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
07509       physname = dwarf2_physname (mi->name, mi->die, cu);
07510       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
07511     }
07512 }
07513 
07514 /* Go objects should be embedded in a DW_TAG_module DIE,
07515    and it's not clear if/how imported objects will appear.
07516    To keep Go support simple until that's worked out,
07517    go back through what we've read and create something usable.
07518    We could do this while processing each DIE, and feels kinda cleaner,
07519    but that way is more invasive.
07520    This is to, for example, allow the user to type "p var" or "b main"
07521    without having to specify the package name, and allow lookups
07522    of module.object to work in contexts that use the expression
07523    parser.  */
07524 
07525 static void
07526 fixup_go_packaging (struct dwarf2_cu *cu)
07527 {
07528   char *package_name = NULL;
07529   struct pending *list;
07530   int i;
07531 
07532   for (list = global_symbols; list != NULL; list = list->next)
07533     {
07534       for (i = 0; i < list->nsyms; ++i)
07535         {
07536           struct symbol *sym = list->symbol[i];
07537 
07538           if (SYMBOL_LANGUAGE (sym) == language_go
07539               && SYMBOL_CLASS (sym) == LOC_BLOCK)
07540             {
07541               char *this_package_name = go_symbol_package_name (sym);
07542 
07543               if (this_package_name == NULL)
07544                 continue;
07545               if (package_name == NULL)
07546                 package_name = this_package_name;
07547               else
07548                 {
07549                   if (strcmp (package_name, this_package_name) != 0)
07550                     complaint (&symfile_complaints,
07551                                _("Symtab %s has objects from two different Go packages: %s and %s"),
07552                                (SYMBOL_SYMTAB (sym)
07553                           ? symtab_to_filename_for_display (SYMBOL_SYMTAB (sym))
07554                                 : objfile_name (cu->objfile)),
07555                                this_package_name, package_name);
07556                   xfree (this_package_name);
07557                 }
07558             }
07559         }
07560     }
07561 
07562   if (package_name != NULL)
07563     {
07564       struct objfile *objfile = cu->objfile;
07565       const char *saved_package_name = obstack_copy0 (&objfile->objfile_obstack,
07566                                                       package_name,
07567                                                       strlen (package_name));
07568       struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
07569                                      saved_package_name, objfile);
07570       struct symbol *sym;
07571 
07572       TYPE_TAG_NAME (type) = TYPE_NAME (type);
07573 
07574       sym = allocate_symbol (objfile);
07575       SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
07576       SYMBOL_SET_NAMES (sym, saved_package_name,
07577                         strlen (saved_package_name), 0, objfile);
07578       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
07579          e.g., "main" finds the "main" module and not C's main().  */
07580       SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
07581       SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
07582       SYMBOL_TYPE (sym) = type;
07583 
07584       add_symbol_to_list (sym, &global_symbols);
07585 
07586       xfree (package_name);
07587     }
07588 }
07589 
07590 /* Return the symtab for PER_CU.  This works properly regardless of
07591    whether we're using the index or psymtabs.  */
07592 
07593 static struct symtab *
07594 get_symtab (struct dwarf2_per_cu_data *per_cu)
07595 {
07596   return (dwarf2_per_objfile->using_index
07597           ? per_cu->v.quick->symtab
07598           : per_cu->v.psymtab->symtab);
07599 }
07600 
07601 /* A helper function for computing the list of all symbol tables
07602    included by PER_CU.  */
07603 
07604 static void
07605 recursively_compute_inclusions (VEC (symtab_ptr) **result,
07606                                 htab_t all_children, htab_t all_type_symtabs,
07607                                 struct dwarf2_per_cu_data *per_cu,
07608                                 struct symtab *immediate_parent)
07609 {
07610   void **slot;
07611   int ix;
07612   struct symtab *symtab;
07613   struct dwarf2_per_cu_data *iter;
07614 
07615   slot = htab_find_slot (all_children, per_cu, INSERT);
07616   if (*slot != NULL)
07617     {
07618       /* This inclusion and its children have been processed.  */
07619       return;
07620     }
07621 
07622   *slot = per_cu;
07623   /* Only add a CU if it has a symbol table.  */
07624   symtab = get_symtab (per_cu);
07625   if (symtab != NULL)
07626     {
07627       /* If this is a type unit only add its symbol table if we haven't
07628          seen it yet (type unit per_cu's can share symtabs).  */
07629       if (per_cu->is_debug_types)
07630         {
07631           slot = htab_find_slot (all_type_symtabs, symtab, INSERT);
07632           if (*slot == NULL)
07633             {
07634               *slot = symtab;
07635               VEC_safe_push (symtab_ptr, *result, symtab);
07636               if (symtab->user == NULL)
07637                 symtab->user = immediate_parent;
07638             }
07639         }
07640       else
07641         {
07642           VEC_safe_push (symtab_ptr, *result, symtab);
07643           if (symtab->user == NULL)
07644             symtab->user = immediate_parent;
07645         }
07646     }
07647 
07648   for (ix = 0;
07649        VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
07650        ++ix)
07651     {
07652       recursively_compute_inclusions (result, all_children,
07653                                       all_type_symtabs, iter, symtab);
07654     }
07655 }
07656 
07657 /* Compute the symtab 'includes' fields for the symtab related to
07658    PER_CU.  */
07659 
07660 static void
07661 compute_symtab_includes (struct dwarf2_per_cu_data *per_cu)
07662 {
07663   gdb_assert (! per_cu->is_debug_types);
07664 
07665   if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
07666     {
07667       int ix, len;
07668       struct dwarf2_per_cu_data *per_cu_iter;
07669       struct symtab *symtab_iter;
07670       VEC (symtab_ptr) *result_symtabs = NULL;
07671       htab_t all_children, all_type_symtabs;
07672       struct symtab *symtab = get_symtab (per_cu);
07673 
07674       /* If we don't have a symtab, we can just skip this case.  */
07675       if (symtab == NULL)
07676         return;
07677 
07678       all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
07679                                         NULL, xcalloc, xfree);
07680       all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
07681                                             NULL, xcalloc, xfree);
07682 
07683       for (ix = 0;
07684            VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
07685                         ix, per_cu_iter);
07686            ++ix)
07687         {
07688           recursively_compute_inclusions (&result_symtabs, all_children,
07689                                           all_type_symtabs, per_cu_iter,
07690                                           symtab);
07691         }
07692 
07693       /* Now we have a transitive closure of all the included symtabs.  */
07694       len = VEC_length (symtab_ptr, result_symtabs);
07695       symtab->includes
07696         = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
07697                          (len + 1) * sizeof (struct symtab *));
07698       for (ix = 0;
07699            VEC_iterate (symtab_ptr, result_symtabs, ix, symtab_iter);
07700            ++ix)
07701         symtab->includes[ix] = symtab_iter;
07702       symtab->includes[len] = NULL;
07703 
07704       VEC_free (symtab_ptr, result_symtabs);
07705       htab_delete (all_children);
07706       htab_delete (all_type_symtabs);
07707     }
07708 }
07709 
07710 /* Compute the 'includes' field for the symtabs of all the CUs we just
07711    read.  */
07712 
07713 static void
07714 process_cu_includes (void)
07715 {
07716   int ix;
07717   struct dwarf2_per_cu_data *iter;
07718 
07719   for (ix = 0;
07720        VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
07721                     ix, iter);
07722        ++ix)
07723     {
07724       if (! iter->is_debug_types)
07725         compute_symtab_includes (iter);
07726     }
07727 
07728   VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
07729 }
07730 
07731 /* Generate full symbol information for PER_CU, whose DIEs have
07732    already been loaded into memory.  */
07733 
07734 static void
07735 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
07736                         enum language pretend_language)
07737 {
07738   struct dwarf2_cu *cu = per_cu->cu;
07739   struct objfile *objfile = per_cu->objfile;
07740   CORE_ADDR lowpc, highpc;
07741   struct symtab *symtab;
07742   struct cleanup *back_to, *delayed_list_cleanup;
07743   CORE_ADDR baseaddr;
07744   struct block *static_block;
07745 
07746   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
07747 
07748   buildsym_init ();
07749   back_to = make_cleanup (really_free_pendings, NULL);
07750   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
07751 
07752   cu->list_in_scope = &file_symbols;
07753 
07754   cu->language = pretend_language;
07755   cu->language_defn = language_def (cu->language);
07756 
07757   /* Do line number decoding in read_file_scope () */
07758   process_die (cu->dies, cu);
07759 
07760   /* For now fudge the Go package.  */
07761   if (cu->language == language_go)
07762     fixup_go_packaging (cu);
07763 
07764   /* Now that we have processed all the DIEs in the CU, all the types 
07765      should be complete, and it should now be safe to compute all of the
07766      physnames.  */
07767   compute_delayed_physnames (cu);
07768   do_cleanups (delayed_list_cleanup);
07769 
07770   /* Some compilers don't define a DW_AT_high_pc attribute for the
07771      compilation unit.  If the DW_AT_high_pc is missing, synthesize
07772      it, by scanning the DIE's below the compilation unit.  */
07773   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
07774 
07775   static_block
07776     = end_symtab_get_static_block (highpc + baseaddr, objfile, 0, 1);
07777 
07778   /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
07779      Also, DW_AT_ranges may record ranges not belonging to any child DIEs
07780      (such as virtual method tables).  Record the ranges in STATIC_BLOCK's
07781      addrmap to help ensure it has an accurate map of pc values belonging to
07782      this comp unit.  */
07783   dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
07784 
07785   symtab = end_symtab_from_static_block (static_block, objfile,
07786                                          SECT_OFF_TEXT (objfile), 0);
07787 
07788   if (symtab != NULL)
07789     {
07790       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
07791 
07792       /* Set symtab language to language from DW_AT_language.  If the
07793          compilation is from a C file generated by language preprocessors, do
07794          not set the language if it was already deduced by start_subfile.  */
07795       if (!(cu->language == language_c && symtab->language != language_c))
07796         symtab->language = cu->language;
07797 
07798       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
07799          produce DW_AT_location with location lists but it can be possibly
07800          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
07801          there were bugs in prologue debug info, fixed later in GCC-4.5
07802          by "unwind info for epilogues" patch (which is not directly related).
07803 
07804          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
07805          needed, it would be wrong due to missing DW_AT_producer there.
07806 
07807          Still one can confuse GDB by using non-standard GCC compilation
07808          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
07809          */ 
07810       if (cu->has_loclist && gcc_4_minor >= 5)
07811         symtab->locations_valid = 1;
07812 
07813       if (gcc_4_minor >= 5)
07814         symtab->epilogue_unwind_valid = 1;
07815 
07816       symtab->call_site_htab = cu->call_site_htab;
07817     }
07818 
07819   if (dwarf2_per_objfile->using_index)
07820     per_cu->v.quick->symtab = symtab;
07821   else
07822     {
07823       struct partial_symtab *pst = per_cu->v.psymtab;
07824       pst->symtab = symtab;
07825       pst->readin = 1;
07826     }
07827 
07828   /* Push it for inclusion processing later.  */
07829   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);
07830 
07831   do_cleanups (back_to);
07832 }
07833 
07834 /* Generate full symbol information for type unit PER_CU, whose DIEs have
07835    already been loaded into memory.  */
07836 
07837 static void
07838 process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
07839                         enum language pretend_language)
07840 {
07841   struct dwarf2_cu *cu = per_cu->cu;
07842   struct objfile *objfile = per_cu->objfile;
07843   struct symtab *symtab;
07844   struct cleanup *back_to, *delayed_list_cleanup;
07845   struct signatured_type *sig_type;
07846 
07847   gdb_assert (per_cu->is_debug_types);
07848   sig_type = (struct signatured_type *) per_cu;
07849 
07850   buildsym_init ();
07851   back_to = make_cleanup (really_free_pendings, NULL);
07852   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
07853 
07854   cu->list_in_scope = &file_symbols;
07855 
07856   cu->language = pretend_language;
07857   cu->language_defn = language_def (cu->language);
07858 
07859   /* The symbol tables are set up in read_type_unit_scope.  */
07860   process_die (cu->dies, cu);
07861 
07862   /* For now fudge the Go package.  */
07863   if (cu->language == language_go)
07864     fixup_go_packaging (cu);
07865 
07866   /* Now that we have processed all the DIEs in the CU, all the types 
07867      should be complete, and it should now be safe to compute all of the
07868      physnames.  */
07869   compute_delayed_physnames (cu);
07870   do_cleanups (delayed_list_cleanup);
07871 
07872   /* TUs share symbol tables.
07873      If this is the first TU to use this symtab, complete the construction
07874      of it with end_expandable_symtab.  Otherwise, complete the addition of
07875      this TU's symbols to the existing symtab.  */
07876   if (sig_type->type_unit_group->primary_symtab == NULL)
07877     {
07878       symtab = end_expandable_symtab (0, objfile, SECT_OFF_TEXT (objfile));
07879       sig_type->type_unit_group->primary_symtab = symtab;
07880 
07881       if (symtab != NULL)
07882         {
07883           /* Set symtab language to language from DW_AT_language.  If the
07884              compilation is from a C file generated by language preprocessors,
07885              do not set the language if it was already deduced by
07886              start_subfile.  */
07887           if (!(cu->language == language_c && symtab->language != language_c))
07888             symtab->language = cu->language;
07889         }
07890     }
07891   else
07892     {
07893       augment_type_symtab (objfile,
07894                            sig_type->type_unit_group->primary_symtab);
07895       symtab = sig_type->type_unit_group->primary_symtab;
07896     }
07897 
07898   if (dwarf2_per_objfile->using_index)
07899     per_cu->v.quick->symtab = symtab;
07900   else
07901     {
07902       struct partial_symtab *pst = per_cu->v.psymtab;
07903       pst->symtab = symtab;
07904       pst->readin = 1;
07905     }
07906 
07907   do_cleanups (back_to);
07908 }
07909 
07910 /* Process an imported unit DIE.  */
07911 
07912 static void
07913 process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
07914 {
07915   struct attribute *attr;
07916 
07917   /* For now we don't handle imported units in type units.  */
07918   if (cu->per_cu->is_debug_types)
07919     {
07920       error (_("Dwarf Error: DW_TAG_imported_unit is not"
07921                " supported in type units [in module %s]"),
07922              objfile_name (cu->objfile));
07923     }
07924 
07925   attr = dwarf2_attr (die, DW_AT_import, cu);
07926   if (attr != NULL)
07927     {
07928       struct dwarf2_per_cu_data *per_cu;
07929       struct symtab *imported_symtab;
07930       sect_offset offset;
07931       int is_dwz;
07932 
07933       offset = dwarf2_get_ref_die_offset (attr);
07934       is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
07935       per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);
07936 
07937       /* If necessary, add it to the queue and load its DIEs.  */
07938       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
07939         load_full_comp_unit (per_cu, cu->language);
07940 
07941       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
07942                      per_cu);
07943     }
07944 }
07945 
07946 /* Process a die and its children.  */
07947 
07948 static void
07949 process_die (struct die_info *die, struct dwarf2_cu *cu)
07950 {
07951   switch (die->tag)
07952     {
07953     case DW_TAG_padding:
07954       break;
07955     case DW_TAG_compile_unit:
07956     case DW_TAG_partial_unit:
07957       read_file_scope (die, cu);
07958       break;
07959     case DW_TAG_type_unit:
07960       read_type_unit_scope (die, cu);
07961       break;
07962     case DW_TAG_subprogram:
07963     case DW_TAG_inlined_subroutine:
07964       read_func_scope (die, cu);
07965       break;
07966     case DW_TAG_lexical_block:
07967     case DW_TAG_try_block:
07968     case DW_TAG_catch_block:
07969       read_lexical_block_scope (die, cu);
07970       break;
07971     case DW_TAG_GNU_call_site:
07972       read_call_site_scope (die, cu);
07973       break;
07974     case DW_TAG_class_type:
07975     case DW_TAG_interface_type:
07976     case DW_TAG_structure_type:
07977     case DW_TAG_union_type:
07978       process_structure_scope (die, cu);
07979       break;
07980     case DW_TAG_enumeration_type:
07981       process_enumeration_scope (die, cu);
07982       break;
07983 
07984     /* These dies have a type, but processing them does not create
07985        a symbol or recurse to process the children.  Therefore we can
07986        read them on-demand through read_type_die.  */
07987     case DW_TAG_subroutine_type:
07988     case DW_TAG_set_type:
07989     case DW_TAG_array_type:
07990     case DW_TAG_pointer_type:
07991     case DW_TAG_ptr_to_member_type:
07992     case DW_TAG_reference_type:
07993     case DW_TAG_string_type:
07994       break;
07995 
07996     case DW_TAG_base_type:
07997     case DW_TAG_subrange_type:
07998     case DW_TAG_typedef:
07999       /* Add a typedef symbol for the type definition, if it has a
08000          DW_AT_name.  */
08001       new_symbol (die, read_type_die (die, cu), cu);
08002       break;
08003     case DW_TAG_common_block:
08004       read_common_block (die, cu);
08005       break;
08006     case DW_TAG_common_inclusion:
08007       break;
08008     case DW_TAG_namespace:
08009       cu->processing_has_namespace_info = 1;
08010       read_namespace (die, cu);
08011       break;
08012     case DW_TAG_module:
08013       cu->processing_has_namespace_info = 1;
08014       read_module (die, cu);
08015       break;
08016     case DW_TAG_imported_declaration:
08017     case DW_TAG_imported_module:
08018       cu->processing_has_namespace_info = 1;
08019       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
08020                                  || cu->language != language_fortran))
08021         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
08022                    dwarf_tag_name (die->tag));
08023       read_import_statement (die, cu);
08024       break;
08025 
08026     case DW_TAG_imported_unit:
08027       process_imported_unit_die (die, cu);
08028       break;
08029 
08030     default:
08031       new_symbol (die, NULL, cu);
08032       break;
08033     }
08034 }
08035 
08036 /* DWARF name computation.  */
08037 
08038 /* A helper function for dwarf2_compute_name which determines whether DIE
08039    needs to have the name of the scope prepended to the name listed in the
08040    die.  */
08041 
08042 static int
08043 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
08044 {
08045   struct attribute *attr;
08046 
08047   switch (die->tag)
08048     {
08049     case DW_TAG_namespace:
08050     case DW_TAG_typedef:
08051     case DW_TAG_class_type:
08052     case DW_TAG_interface_type:
08053     case DW_TAG_structure_type:
08054     case DW_TAG_union_type:
08055     case DW_TAG_enumeration_type:
08056     case DW_TAG_enumerator:
08057     case DW_TAG_subprogram:
08058     case DW_TAG_member:
08059       return 1;
08060 
08061     case DW_TAG_variable:
08062     case DW_TAG_constant:
08063       /* We only need to prefix "globally" visible variables.  These include
08064          any variable marked with DW_AT_external or any variable that
08065          lives in a namespace.  [Variables in anonymous namespaces
08066          require prefixing, but they are not DW_AT_external.]  */
08067 
08068       if (dwarf2_attr (die, DW_AT_specification, cu))
08069         {
08070           struct dwarf2_cu *spec_cu = cu;
08071 
08072           return die_needs_namespace (die_specification (die, &spec_cu),
08073                                       spec_cu);
08074         }
08075 
08076       attr = dwarf2_attr (die, DW_AT_external, cu);
08077       if (attr == NULL && die->parent->tag != DW_TAG_namespace
08078           && die->parent->tag != DW_TAG_module)
08079         return 0;
08080       /* A variable in a lexical block of some kind does not need a
08081          namespace, even though in C++ such variables may be external
08082          and have a mangled name.  */
08083       if (die->parent->tag ==  DW_TAG_lexical_block
08084           || die->parent->tag ==  DW_TAG_try_block
08085           || die->parent->tag ==  DW_TAG_catch_block
08086           || die->parent->tag == DW_TAG_subprogram)
08087         return 0;
08088       return 1;
08089 
08090     default:
08091       return 0;
08092     }
08093 }
08094 
08095 /* Retrieve the last character from a mem_file.  */
08096 
08097 static void
08098 do_ui_file_peek_last (void *object, const char *buffer, long length)
08099 {
08100   char *last_char_p = (char *) object;
08101 
08102   if (length > 0)
08103     *last_char_p = buffer[length - 1];
08104 }
08105 
08106 /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
08107    compute the physname for the object, which include a method's:
08108    - formal parameters (C++/Java),
08109    - receiver type (Go),
08110    - return type (Java).
08111 
08112    The term "physname" is a bit confusing.
08113    For C++, for example, it is the demangled name.
08114    For Go, for example, it's the mangled name.
08115 
08116    For Ada, return the DIE's linkage name rather than the fully qualified
08117    name.  PHYSNAME is ignored..
08118 
08119    The result is allocated on the objfile_obstack and canonicalized.  */
08120 
08121 static const char *
08122 dwarf2_compute_name (const char *name,
08123                      struct die_info *die, struct dwarf2_cu *cu,
08124                      int physname)
08125 {
08126   struct objfile *objfile = cu->objfile;
08127 
08128   if (name == NULL)
08129     name = dwarf2_name (die, cu);
08130 
08131   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
08132      compute it by typename_concat inside GDB.  */
08133   if (cu->language == language_ada
08134       || (cu->language == language_fortran && physname))
08135     {
08136       /* For Ada unit, we prefer the linkage name over the name, as
08137          the former contains the exported name, which the user expects
08138          to be able to reference.  Ideally, we want the user to be able
08139          to reference this entity using either natural or linkage name,
08140          but we haven't started looking at this enhancement yet.  */
08141       struct attribute *attr;
08142 
08143       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
08144       if (attr == NULL)
08145         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
08146       if (attr && DW_STRING (attr))
08147         return DW_STRING (attr);
08148     }
08149 
08150   /* These are the only languages we know how to qualify names in.  */
08151   if (name != NULL
08152       && (cu->language == language_cplus || cu->language == language_java
08153           || cu->language == language_fortran))
08154     {
08155       if (die_needs_namespace (die, cu))
08156         {
08157           long length;
08158           const char *prefix;
08159           struct ui_file *buf;
08160 
08161           prefix = determine_prefix (die, cu);
08162           buf = mem_fileopen ();
08163           if (*prefix != '\0')
08164             {
08165               char *prefixed_name = typename_concat (NULL, prefix, name,
08166                                                      physname, cu);
08167 
08168               fputs_unfiltered (prefixed_name, buf);
08169               xfree (prefixed_name);
08170             }
08171           else
08172             fputs_unfiltered (name, buf);
08173 
08174           /* Template parameters may be specified in the DIE's DW_AT_name, or
08175              as children with DW_TAG_template_type_param or
08176              DW_TAG_value_type_param.  If the latter, add them to the name
08177              here.  If the name already has template parameters, then
08178              skip this step; some versions of GCC emit both, and
08179              it is more efficient to use the pre-computed name.
08180 
08181              Something to keep in mind about this process: it is very
08182              unlikely, or in some cases downright impossible, to produce
08183              something that will match the mangled name of a function.
08184              If the definition of the function has the same debug info,
08185              we should be able to match up with it anyway.  But fallbacks
08186              using the minimal symbol, for instance to find a method
08187              implemented in a stripped copy of libstdc++, will not work.
08188              If we do not have debug info for the definition, we will have to
08189              match them up some other way.
08190 
08191              When we do name matching there is a related problem with function
08192              templates; two instantiated function templates are allowed to
08193              differ only by their return types, which we do not add here.  */
08194 
08195           if (cu->language == language_cplus && strchr (name, '<') == NULL)
08196             {
08197               struct attribute *attr;
08198               struct die_info *child;
08199               int first = 1;
08200 
08201               die->building_fullname = 1;
08202 
08203               for (child = die->child; child != NULL; child = child->sibling)
08204                 {
08205                   struct type *type;
08206                   LONGEST value;
08207                   const gdb_byte *bytes;
08208                   struct dwarf2_locexpr_baton *baton;
08209                   struct value *v;
08210 
08211                   if (child->tag != DW_TAG_template_type_param
08212                       && child->tag != DW_TAG_template_value_param)
08213                     continue;
08214 
08215                   if (first)
08216                     {
08217                       fputs_unfiltered ("<", buf);
08218                       first = 0;
08219                     }
08220                   else
08221                     fputs_unfiltered (", ", buf);
08222 
08223                   attr = dwarf2_attr (child, DW_AT_type, cu);
08224                   if (attr == NULL)
08225                     {
08226                       complaint (&symfile_complaints,
08227                                  _("template parameter missing DW_AT_type"));
08228                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
08229                       continue;
08230                     }
08231                   type = die_type (child, cu);
08232 
08233                   if (child->tag == DW_TAG_template_type_param)
08234                     {
08235                       c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
08236                       continue;
08237                     }
08238 
08239                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
08240                   if (attr == NULL)
08241                     {
08242                       complaint (&symfile_complaints,
08243                                  _("template parameter missing "
08244                                    "DW_AT_const_value"));
08245                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
08246                       continue;
08247                     }
08248 
08249                   dwarf2_const_value_attr (attr, type, name,
08250                                            &cu->comp_unit_obstack, cu,
08251                                            &value, &bytes, &baton);
08252 
08253                   if (TYPE_NOSIGN (type))
08254                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
08255                        changed, this can use value_print instead.  */
08256                     c_printchar (value, type, buf);
08257                   else
08258                     {
08259                       struct value_print_options opts;
08260 
08261                       if (baton != NULL)
08262                         v = dwarf2_evaluate_loc_desc (type, NULL,
08263                                                       baton->data,
08264                                                       baton->size,
08265                                                       baton->per_cu);
08266                       else if (bytes != NULL)
08267                         {
08268                           v = allocate_value (type);
08269                           memcpy (value_contents_writeable (v), bytes,
08270                                   TYPE_LENGTH (type));
08271                         }
08272                       else
08273                         v = value_from_longest (type, value);
08274 
08275                       /* Specify decimal so that we do not depend on
08276                          the radix.  */
08277                       get_formatted_print_options (&opts, 'd');
08278                       opts.raw = 1;
08279                       value_print (v, buf, &opts);
08280                       release_value (v);
08281                       value_free (v);
08282                     }
08283                 }
08284 
08285               die->building_fullname = 0;
08286 
08287               if (!first)
08288                 {
08289                   /* Close the argument list, with a space if necessary
08290                      (nested templates).  */
08291                   char last_char = '\0';
08292                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
08293                   if (last_char == '>')
08294                     fputs_unfiltered (" >", buf);
08295                   else
08296                     fputs_unfiltered (">", buf);
08297                 }
08298             }
08299 
08300           /* For Java and C++ methods, append formal parameter type
08301              information, if PHYSNAME.  */
08302 
08303           if (physname && die->tag == DW_TAG_subprogram
08304               && (cu->language == language_cplus
08305                   || cu->language == language_java))
08306             {
08307               struct type *type = read_type_die (die, cu);
08308 
08309               c_type_print_args (type, buf, 1, cu->language,
08310                                  &type_print_raw_options);
08311 
08312               if (cu->language == language_java)
08313                 {
08314                   /* For java, we must append the return type to method
08315                      names.  */
08316                   if (die->tag == DW_TAG_subprogram)
08317                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
08318                                      0, 0, &type_print_raw_options);
08319                 }
08320               else if (cu->language == language_cplus)
08321                 {
08322                   /* Assume that an artificial first parameter is
08323                      "this", but do not crash if it is not.  RealView
08324                      marks unnamed (and thus unused) parameters as
08325                      artificial; there is no way to differentiate
08326                      the two cases.  */
08327                   if (TYPE_NFIELDS (type) > 0
08328                       && TYPE_FIELD_ARTIFICIAL (type, 0)
08329                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
08330                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
08331                                                                         0))))
08332                     fputs_unfiltered (" const", buf);
08333                 }
08334             }
08335 
08336           name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
08337                                        &length);
08338           ui_file_delete (buf);
08339 
08340           if (cu->language == language_cplus)
08341             {
08342               const char *cname
08343                 = dwarf2_canonicalize_name (name, cu,
08344                                             &objfile->objfile_obstack);
08345 
08346               if (cname != NULL)
08347                 name = cname;
08348             }
08349         }
08350     }
08351 
08352   return name;
08353 }
08354 
08355 /* Return the fully qualified name of DIE, based on its DW_AT_name.
08356    If scope qualifiers are appropriate they will be added.  The result
08357    will be allocated on the objfile_obstack, or NULL if the DIE does
08358    not have a name.  NAME may either be from a previous call to
08359    dwarf2_name or NULL.
08360 
08361    The output string will be canonicalized (if C++/Java).  */
08362 
08363 static const char *
08364 dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
08365 {
08366   return dwarf2_compute_name (name, die, cu, 0);
08367 }
08368 
08369 /* Construct a physname for the given DIE in CU.  NAME may either be
08370    from a previous call to dwarf2_name or NULL.  The result will be
08371    allocated on the objfile_objstack or NULL if the DIE does not have a
08372    name.
08373 
08374    The output string will be canonicalized (if C++/Java).  */
08375 
08376 static const char *
08377 dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
08378 {
08379   struct objfile *objfile = cu->objfile;
08380   struct attribute *attr;
08381   const char *retval, *mangled = NULL, *canon = NULL;
08382   struct cleanup *back_to;
08383   int need_copy = 1;
08384 
08385   /* In this case dwarf2_compute_name is just a shortcut not building anything
08386      on its own.  */
08387   if (!die_needs_namespace (die, cu))
08388     return dwarf2_compute_name (name, die, cu, 1);
08389 
08390   back_to = make_cleanup (null_cleanup, NULL);
08391 
08392   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
08393   if (!attr)
08394     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
08395 
08396   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
08397      has computed.  */
08398   if (attr && DW_STRING (attr))
08399     {
08400       char *demangled;
08401 
08402       mangled = DW_STRING (attr);
08403 
08404       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
08405          type.  It is easier for GDB users to search for such functions as
08406          `name(params)' than `long name(params)'.  In such case the minimal
08407          symbol names do not match the full symbol names but for template
08408          functions there is never a need to look up their definition from their
08409          declaration so the only disadvantage remains the minimal symbol
08410          variant `long name(params)' does not have the proper inferior type.
08411          */
08412 
08413       if (cu->language == language_go)
08414         {
08415           /* This is a lie, but we already lie to the caller new_symbol_full.
08416              new_symbol_full assumes we return the mangled name.
08417              This just undoes that lie until things are cleaned up.  */
08418           demangled = NULL;
08419         }
08420       else
08421         {
08422           demangled = gdb_demangle (mangled,
08423                                     (DMGL_PARAMS | DMGL_ANSI
08424                                      | (cu->language == language_java
08425                                         ? DMGL_JAVA | DMGL_RET_POSTFIX
08426                                         : DMGL_RET_DROP)));
08427         }
08428       if (demangled)
08429         {
08430           make_cleanup (xfree, demangled);
08431           canon = demangled;
08432         }
08433       else
08434         {
08435           canon = mangled;
08436           need_copy = 0;
08437         }
08438     }
08439 
08440   if (canon == NULL || check_physname)
08441     {
08442       const char *physname = dwarf2_compute_name (name, die, cu, 1);
08443 
08444       if (canon != NULL && strcmp (physname, canon) != 0)
08445         {
08446           /* It may not mean a bug in GDB.  The compiler could also
08447              compute DW_AT_linkage_name incorrectly.  But in such case
08448              GDB would need to be bug-to-bug compatible.  */
08449 
08450           complaint (&symfile_complaints,
08451                      _("Computed physname <%s> does not match demangled <%s> "
08452                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
08453                      physname, canon, mangled, die->offset.sect_off,
08454                      objfile_name (objfile));
08455 
08456           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
08457              is available here - over computed PHYSNAME.  It is safer
08458              against both buggy GDB and buggy compilers.  */
08459 
08460           retval = canon;
08461         }
08462       else
08463         {
08464           retval = physname;
08465           need_copy = 0;
08466         }
08467     }
08468   else
08469     retval = canon;
08470 
08471   if (need_copy)
08472     retval = obstack_copy0 (&objfile->objfile_obstack, retval, strlen (retval));
08473 
08474   do_cleanups (back_to);
08475   return retval;
08476 }
08477 
08478 /* Read the import statement specified by the given die and record it.  */
08479 
08480 static void
08481 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
08482 {
08483   struct objfile *objfile = cu->objfile;
08484   struct attribute *import_attr;
08485   struct die_info *imported_die, *child_die;
08486   struct dwarf2_cu *imported_cu;
08487   const char *imported_name;
08488   const char *imported_name_prefix;
08489   const char *canonical_name;
08490   const char *import_alias;
08491   const char *imported_declaration = NULL;
08492   const char *import_prefix;
08493   VEC (const_char_ptr) *excludes = NULL;
08494   struct cleanup *cleanups;
08495 
08496   import_attr = dwarf2_attr (die, DW_AT_import, cu);
08497   if (import_attr == NULL)
08498     {
08499       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
08500                  dwarf_tag_name (die->tag));
08501       return;
08502     }
08503 
08504   imported_cu = cu;
08505   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
08506   imported_name = dwarf2_name (imported_die, imported_cu);
08507   if (imported_name == NULL)
08508     {
08509       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
08510 
08511         The import in the following code:
08512         namespace A
08513           {
08514             typedef int B;
08515           }
08516 
08517         int main ()
08518           {
08519             using A::B;
08520             B b;
08521             return b;
08522           }
08523 
08524         ...
08525          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
08526             <52>   DW_AT_decl_file   : 1
08527             <53>   DW_AT_decl_line   : 6
08528             <54>   DW_AT_import      : <0x75>
08529          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
08530             <59>   DW_AT_name        : B
08531             <5b>   DW_AT_decl_file   : 1
08532             <5c>   DW_AT_decl_line   : 2
08533             <5d>   DW_AT_type        : <0x6e>
08534         ...
08535          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
08536             <76>   DW_AT_byte_size   : 4
08537             <77>   DW_AT_encoding    : 5        (signed)
08538 
08539         imports the wrong die ( 0x75 instead of 0x58 ).
08540         This case will be ignored until the gcc bug is fixed.  */
08541       return;
08542     }
08543 
08544   /* Figure out the local name after import.  */
08545   import_alias = dwarf2_name (die, cu);
08546 
08547   /* Figure out where the statement is being imported to.  */
08548   import_prefix = determine_prefix (die, cu);
08549 
08550   /* Figure out what the scope of the imported die is and prepend it
08551      to the name of the imported die.  */
08552   imported_name_prefix = determine_prefix (imported_die, imported_cu);
08553 
08554   if (imported_die->tag != DW_TAG_namespace
08555       && imported_die->tag != DW_TAG_module)
08556     {
08557       imported_declaration = imported_name;
08558       canonical_name = imported_name_prefix;
08559     }
08560   else if (strlen (imported_name_prefix) > 0)
08561     canonical_name = obconcat (&objfile->objfile_obstack,
08562                                imported_name_prefix, "::", imported_name,
08563                                (char *) NULL);
08564   else
08565     canonical_name = imported_name;
08566 
08567   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
08568 
08569   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
08570     for (child_die = die->child; child_die && child_die->tag;
08571          child_die = sibling_die (child_die))
08572       {
08573         /* DWARF-4: A Fortran use statement with a “rename list” may be
08574            represented by an imported module entry with an import attribute
08575            referring to the module and owned entries corresponding to those
08576            entities that are renamed as part of being imported.  */
08577 
08578         if (child_die->tag != DW_TAG_imported_declaration)
08579           {
08580             complaint (&symfile_complaints,
08581                        _("child DW_TAG_imported_declaration expected "
08582                          "- DIE at 0x%x [in module %s]"),
08583                        child_die->offset.sect_off, objfile_name (objfile));
08584             continue;
08585           }
08586 
08587         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
08588         if (import_attr == NULL)
08589           {
08590             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
08591                        dwarf_tag_name (child_die->tag));
08592             continue;
08593           }
08594 
08595         imported_cu = cu;
08596         imported_die = follow_die_ref_or_sig (child_die, import_attr,
08597                                               &imported_cu);
08598         imported_name = dwarf2_name (imported_die, imported_cu);
08599         if (imported_name == NULL)
08600           {
08601             complaint (&symfile_complaints,
08602                        _("child DW_TAG_imported_declaration has unknown "
08603                          "imported name - DIE at 0x%x [in module %s]"),
08604                        child_die->offset.sect_off, objfile_name (objfile));
08605             continue;
08606           }
08607 
08608         VEC_safe_push (const_char_ptr, excludes, imported_name);
08609 
08610         process_die (child_die, cu);
08611       }
08612 
08613   cp_add_using_directive (import_prefix,
08614                           canonical_name,
08615                           import_alias,
08616                           imported_declaration,
08617                           excludes,
08618                           0,
08619                           &objfile->objfile_obstack);
08620 
08621   do_cleanups (cleanups);
08622 }
08623 
08624 /* Cleanup function for handle_DW_AT_stmt_list.  */
08625 
08626 static void
08627 free_cu_line_header (void *arg)
08628 {
08629   struct dwarf2_cu *cu = arg;
08630 
08631   free_line_header (cu->line_header);
08632   cu->line_header = NULL;
08633 }
08634 
08635 /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
08636    directory paths.  GCC SVN r127613 (new option -fdebug-prefix-map) fixed
08637    this, it was first present in GCC release 4.3.0.  */
08638 
08639 static int
08640 producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
08641 {
08642   if (!cu->checked_producer)
08643     check_producer (cu);
08644 
08645   return cu->producer_is_gcc_lt_4_3;
08646 }
08647 
08648 static void
08649 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
08650                          const char **name, const char **comp_dir)
08651 {
08652   struct attribute *attr;
08653 
08654   *name = NULL;
08655   *comp_dir = NULL;
08656 
08657   /* Find the filename.  Do not use dwarf2_name here, since the filename
08658      is not a source language identifier.  */
08659   attr = dwarf2_attr (die, DW_AT_name, cu);
08660   if (attr)
08661     {
08662       *name = DW_STRING (attr);
08663     }
08664 
08665   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
08666   if (attr)
08667     *comp_dir = DW_STRING (attr);
08668   else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
08669            && IS_ABSOLUTE_PATH (*name))
08670     {
08671       char *d = ldirname (*name);
08672 
08673       *comp_dir = d;
08674       if (d != NULL)
08675         make_cleanup (xfree, d);
08676     }
08677   if (*comp_dir != NULL)
08678     {
08679       /* Irix 6.2 native cc prepends <machine>.: to the compilation
08680          directory, get rid of it.  */
08681       char *cp = strchr (*comp_dir, ':');
08682 
08683       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
08684         *comp_dir = cp + 1;
08685     }
08686 
08687   if (*name == NULL)
08688     *name = "<unknown>";
08689 }
08690 
08691 /* Handle DW_AT_stmt_list for a compilation unit.
08692    DIE is the DW_TAG_compile_unit die for CU.
08693    COMP_DIR is the compilation directory.
08694    WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed.  */
08695 
08696 static void
08697 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
08698                         const char *comp_dir) /* ARI: editCase function */
08699 {
08700   struct attribute *attr;
08701 
08702   gdb_assert (! cu->per_cu->is_debug_types);
08703 
08704   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
08705   if (attr)
08706     {
08707       unsigned int line_offset = DW_UNSND (attr);
08708       struct line_header *line_header
08709         = dwarf_decode_line_header (line_offset, cu);
08710 
08711       if (line_header)
08712         {
08713           cu->line_header = line_header;
08714           make_cleanup (free_cu_line_header, cu);
08715           dwarf_decode_lines (line_header, comp_dir, cu, NULL, 1);
08716         }
08717     }
08718 }
08719 
08720 /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */
08721 
08722 static void
08723 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
08724 {
08725   struct objfile *objfile = dwarf2_per_objfile->objfile;
08726   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
08727   CORE_ADDR lowpc = ((CORE_ADDR) -1);
08728   CORE_ADDR highpc = ((CORE_ADDR) 0);
08729   struct attribute *attr;
08730   const char *name = NULL;
08731   const char *comp_dir = NULL;
08732   struct die_info *child_die;
08733   bfd *abfd = objfile->obfd;
08734   CORE_ADDR baseaddr;
08735 
08736   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
08737 
08738   get_scope_pc_bounds (die, &lowpc, &highpc, cu);
08739 
08740   /* If we didn't find a lowpc, set it to highpc to avoid complaints
08741      from finish_block.  */
08742   if (lowpc == ((CORE_ADDR) -1))
08743     lowpc = highpc;
08744   lowpc += baseaddr;
08745   highpc += baseaddr;
08746 
08747   find_file_and_directory (die, cu, &name, &comp_dir);
08748 
08749   prepare_one_comp_unit (cu, die, cu->language);
08750 
08751   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
08752      standardised yet.  As a workaround for the language detection we fall
08753      back to the DW_AT_producer string.  */
08754   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
08755     cu->language = language_opencl;
08756 
08757   /* Similar hack for Go.  */
08758   if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
08759     set_cu_language (DW_LANG_Go, cu);
08760 
08761   dwarf2_start_symtab (cu, name, comp_dir, lowpc);
08762 
08763   /* Decode line number information if present.  We do this before
08764      processing child DIEs, so that the line header table is available
08765      for DW_AT_decl_file.  */
08766   handle_DW_AT_stmt_list (die, cu, comp_dir);
08767 
08768   /* Process all dies in compilation unit.  */
08769   if (die->child != NULL)
08770     {
08771       child_die = die->child;
08772       while (child_die && child_die->tag)
08773         {
08774           process_die (child_die, cu);
08775           child_die = sibling_die (child_die);
08776         }
08777     }
08778 
08779   /* Decode macro information, if present.  Dwarf 2 macro information
08780      refers to information in the line number info statement program
08781      header, so we can only read it if we've read the header
08782      successfully.  */
08783   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
08784   if (attr && cu->line_header)
08785     {
08786       if (dwarf2_attr (die, DW_AT_macro_info, cu))
08787         complaint (&symfile_complaints,
08788                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
08789 
08790       dwarf_decode_macros (cu, DW_UNSND (attr), comp_dir, 1);
08791     }
08792   else
08793     {
08794       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
08795       if (attr && cu->line_header)
08796         {
08797           unsigned int macro_offset = DW_UNSND (attr);
08798 
08799           dwarf_decode_macros (cu, macro_offset, comp_dir, 0);
08800         }
08801     }
08802 
08803   do_cleanups (back_to);
08804 }
08805 
08806 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
08807    Create the set of symtabs used by this TU, or if this TU is sharing
08808    symtabs with another TU and the symtabs have already been created
08809    then restore those symtabs in the line header.
08810    We don't need the pc/line-number mapping for type units.  */
08811 
08812 static void
08813 setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
08814 {
08815   struct objfile *objfile = dwarf2_per_objfile->objfile;
08816   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
08817   struct type_unit_group *tu_group;
08818   int first_time;
08819   struct line_header *lh;
08820   struct attribute *attr;
08821   unsigned int i, line_offset;
08822   struct signatured_type *sig_type;
08823 
08824   gdb_assert (per_cu->is_debug_types);
08825   sig_type = (struct signatured_type *) per_cu;
08826 
08827   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
08828 
08829   /* If we're using .gdb_index (includes -readnow) then
08830      per_cu->type_unit_group may not have been set up yet.  */
08831   if (sig_type->type_unit_group == NULL)
08832     sig_type->type_unit_group = get_type_unit_group (cu, attr);
08833   tu_group = sig_type->type_unit_group;
08834 
08835   /* If we've already processed this stmt_list there's no real need to
08836      do it again, we could fake it and just recreate the part we need
08837      (file name,index -> symtab mapping).  If data shows this optimization
08838      is useful we can do it then.  */
08839   first_time = tu_group->primary_symtab == NULL;
08840 
08841   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
08842      debug info.  */
08843   lh = NULL;
08844   if (attr != NULL)
08845     {
08846       line_offset = DW_UNSND (attr);
08847       lh = dwarf_decode_line_header (line_offset, cu);
08848     }
08849   if (lh == NULL)
08850     {
08851       if (first_time)
08852         dwarf2_start_symtab (cu, "", NULL, 0);
08853       else
08854         {
08855           gdb_assert (tu_group->symtabs == NULL);
08856           restart_symtab (0);
08857         }
08858       /* Note: The primary symtab will get allocated at the end.  */
08859       return;
08860     }
08861 
08862   cu->line_header = lh;
08863   make_cleanup (free_cu_line_header, cu);
08864 
08865   if (first_time)
08866     {
08867       dwarf2_start_symtab (cu, "", NULL, 0);
08868 
08869       tu_group->num_symtabs = lh->num_file_names;
08870       tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);
08871 
08872       for (i = 0; i < lh->num_file_names; ++i)
08873         {
08874           const char *dir = NULL;
08875           struct file_entry *fe = &lh->file_names[i];
08876 
08877           if (fe->dir_index)
08878             dir = lh->include_dirs[fe->dir_index - 1];
08879           dwarf2_start_subfile (fe->name, dir, NULL);
08880 
08881           /* Note: We don't have to watch for the main subfile here, type units
08882              don't have DW_AT_name.  */
08883 
08884           if (current_subfile->symtab == NULL)
08885             {
08886               /* NOTE: start_subfile will recognize when it's been passed
08887                  a file it has already seen.  So we can't assume there's a
08888                  simple mapping from lh->file_names to subfiles,
08889                  lh->file_names may contain dups.  */
08890               current_subfile->symtab = allocate_symtab (current_subfile->name,
08891                                                          objfile);
08892             }
08893 
08894           fe->symtab = current_subfile->symtab;
08895           tu_group->symtabs[i] = fe->symtab;
08896         }
08897     }
08898   else
08899     {
08900       restart_symtab (0);
08901 
08902       for (i = 0; i < lh->num_file_names; ++i)
08903         {
08904           struct file_entry *fe = &lh->file_names[i];
08905 
08906           fe->symtab = tu_group->symtabs[i];
08907         }
08908     }
08909 
08910   /* The main symtab is allocated last.  Type units don't have DW_AT_name
08911      so they don't have a "real" (so to speak) symtab anyway.
08912      There is later code that will assign the main symtab to all symbols
08913      that don't have one.  We need to handle the case of a symbol with a
08914      missing symtab (DW_AT_decl_file) anyway.  */
08915 }
08916 
08917 /* Process DW_TAG_type_unit.
08918    For TUs we want to skip the first top level sibling if it's not the
08919    actual type being defined by this TU.  In this case the first top
08920    level sibling is there to provide context only.  */
08921 
08922 static void
08923 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
08924 {
08925   struct die_info *child_die;
08926 
08927   prepare_one_comp_unit (cu, die, language_minimal);
08928 
08929   /* Initialize (or reinitialize) the machinery for building symtabs.
08930      We do this before processing child DIEs, so that the line header table
08931      is available for DW_AT_decl_file.  */
08932   setup_type_unit_groups (die, cu);
08933 
08934   if (die->child != NULL)
08935     {
08936       child_die = die->child;
08937       while (child_die && child_die->tag)
08938         {
08939           process_die (child_die, cu);
08940           child_die = sibling_die (child_die);
08941         }
08942     }
08943 }
08944 
08945 /* DWO/DWP files.
08946 
08947    http://gcc.gnu.org/wiki/DebugFission
08948    http://gcc.gnu.org/wiki/DebugFissionDWP
08949 
08950    To simplify handling of both DWO files ("object" files with the DWARF info)
08951    and DWP files (a file with the DWOs packaged up into one file), we treat
08952    DWP files as having a collection of virtual DWO files.  */
08953 
08954 static hashval_t
08955 hash_dwo_file (const void *item)
08956 {
08957   const struct dwo_file *dwo_file = item;
08958   hashval_t hash;
08959 
08960   hash = htab_hash_string (dwo_file->dwo_name);
08961   if (dwo_file->comp_dir != NULL)
08962     hash += htab_hash_string (dwo_file->comp_dir);
08963   return hash;
08964 }
08965 
08966 static int
08967 eq_dwo_file (const void *item_lhs, const void *item_rhs)
08968 {
08969   const struct dwo_file *lhs = item_lhs;
08970   const struct dwo_file *rhs = item_rhs;
08971 
08972   if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
08973     return 0;
08974   if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
08975     return lhs->comp_dir == rhs->comp_dir;
08976   return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
08977 }
08978 
08979 /* Allocate a hash table for DWO files.  */
08980 
08981 static htab_t
08982 allocate_dwo_file_hash_table (void)
08983 {
08984   struct objfile *objfile = dwarf2_per_objfile->objfile;
08985 
08986   return htab_create_alloc_ex (41,
08987                                hash_dwo_file,
08988                                eq_dwo_file,
08989                                NULL,
08990                                &objfile->objfile_obstack,
08991                                hashtab_obstack_allocate,
08992                                dummy_obstack_deallocate);
08993 }
08994 
08995 /* Lookup DWO file DWO_NAME.  */
08996 
08997 static void **
08998 lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
08999 {
09000   struct dwo_file find_entry;
09001   void **slot;
09002 
09003   if (dwarf2_per_objfile->dwo_files == NULL)
09004     dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
09005 
09006   memset (&find_entry, 0, sizeof (find_entry));
09007   find_entry.dwo_name = dwo_name;
09008   find_entry.comp_dir = comp_dir;
09009   slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);
09010 
09011   return slot;
09012 }
09013 
09014 static hashval_t
09015 hash_dwo_unit (const void *item)
09016 {
09017   const struct dwo_unit *dwo_unit = item;
09018 
09019   /* This drops the top 32 bits of the id, but is ok for a hash.  */
09020   return dwo_unit->signature;
09021 }
09022 
09023 static int
09024 eq_dwo_unit (const void *item_lhs, const void *item_rhs)
09025 {
09026   const struct dwo_unit *lhs = item_lhs;
09027   const struct dwo_unit *rhs = item_rhs;
09028 
09029   /* The signature is assumed to be unique within the DWO file.
09030      So while object file CU dwo_id's always have the value zero,
09031      that's OK, assuming each object file DWO file has only one CU,
09032      and that's the rule for now.  */
09033   return lhs->signature == rhs->signature;
09034 }
09035 
09036 /* Allocate a hash table for DWO CUs,TUs.
09037    There is one of these tables for each of CUs,TUs for each DWO file.  */
09038 
09039 static htab_t
09040 allocate_dwo_unit_table (struct objfile *objfile)
09041 {
09042   /* Start out with a pretty small number.
09043      Generally DWO files contain only one CU and maybe some TUs.  */
09044   return htab_create_alloc_ex (3,
09045                                hash_dwo_unit,
09046                                eq_dwo_unit,
09047                                NULL,
09048                                &objfile->objfile_obstack,
09049                                hashtab_obstack_allocate,
09050                                dummy_obstack_deallocate);
09051 }
09052 
09053 /* Structure used to pass data to create_dwo_debug_info_hash_table_reader.  */
09054 
09055 struct create_dwo_cu_data
09056 {
09057   struct dwo_file *dwo_file;
09058   struct dwo_unit dwo_unit;
09059 };
09060 
09061 /* die_reader_func for create_dwo_cu.  */
09062 
09063 static void
09064 create_dwo_cu_reader (const struct die_reader_specs *reader,
09065                       const gdb_byte *info_ptr,
09066                       struct die_info *comp_unit_die,
09067                       int has_children,
09068                       void *datap)
09069 {
09070   struct dwarf2_cu *cu = reader->cu;
09071   struct objfile *objfile = dwarf2_per_objfile->objfile;
09072   sect_offset offset = cu->per_cu->offset;
09073   struct dwarf2_section_info *section = cu->per_cu->section;
09074   struct create_dwo_cu_data *data = datap;
09075   struct dwo_file *dwo_file = data->dwo_file;
09076   struct dwo_unit *dwo_unit = &data->dwo_unit;
09077   struct attribute *attr;
09078 
09079   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
09080   if (attr == NULL)
09081     {
09082       complaint (&symfile_complaints,
09083                  _("Dwarf Error: debug entry at offset 0x%x is missing"
09084                    " its dwo_id [in module %s]"),
09085                  offset.sect_off, dwo_file->dwo_name);
09086       return;
09087     }
09088 
09089   dwo_unit->dwo_file = dwo_file;
09090   dwo_unit->signature = DW_UNSND (attr);
09091   dwo_unit->section = section;
09092   dwo_unit->offset = offset;
09093   dwo_unit->length = cu->per_cu->length;
09094 
09095   if (dwarf2_read_debug)
09096     fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id %s\n",
09097                         offset.sect_off, hex_string (dwo_unit->signature));
09098 }
09099 
09100 /* Create the dwo_unit for the lone CU in DWO_FILE.
09101    Note: This function processes DWO files only, not DWP files.  */
09102 
09103 static struct dwo_unit *
09104 create_dwo_cu (struct dwo_file *dwo_file)
09105 {
09106   struct objfile *objfile = dwarf2_per_objfile->objfile;
09107   struct dwarf2_section_info *section = &dwo_file->sections.info;
09108   bfd *abfd;
09109   htab_t cu_htab;
09110   const gdb_byte *info_ptr, *end_ptr;
09111   struct create_dwo_cu_data create_dwo_cu_data;
09112   struct dwo_unit *dwo_unit;
09113 
09114   dwarf2_read_section (objfile, section);
09115   info_ptr = section->buffer;
09116 
09117   if (info_ptr == NULL)
09118     return NULL;
09119 
09120   /* We can't set abfd until now because the section may be empty or
09121      not present, in which case section->asection will be NULL.  */
09122   abfd = get_section_bfd_owner (section);
09123 
09124   if (dwarf2_read_debug)
09125     {
09126       fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
09127                           get_section_name (section),
09128                           get_section_file_name (section));
09129     }
09130 
09131   create_dwo_cu_data.dwo_file = dwo_file;
09132   dwo_unit = NULL;
09133 
09134   end_ptr = info_ptr + section->size;
09135   while (info_ptr < end_ptr)
09136     {
09137       struct dwarf2_per_cu_data per_cu;
09138 
09139       memset (&create_dwo_cu_data.dwo_unit, 0,
09140               sizeof (create_dwo_cu_data.dwo_unit));
09141       memset (&per_cu, 0, sizeof (per_cu));
09142       per_cu.objfile = objfile;
09143       per_cu.is_debug_types = 0;
09144       per_cu.offset.sect_off = info_ptr - section->buffer;
09145       per_cu.section = section;
09146 
09147       init_cutu_and_read_dies_no_follow (&per_cu,
09148                                          &dwo_file->sections.abbrev,
09149                                          dwo_file,
09150                                          create_dwo_cu_reader,
09151                                          &create_dwo_cu_data);
09152 
09153       if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
09154         {
09155           /* If we've already found one, complain.  We only support one
09156              because having more than one requires hacking the dwo_name of
09157              each to match, which is highly unlikely to happen.  */
09158           if (dwo_unit != NULL)
09159             {
09160               complaint (&symfile_complaints,
09161                          _("Multiple CUs in DWO file %s [in module %s]"),
09162                          dwo_file->dwo_name, objfile_name (objfile));
09163               break;
09164             }
09165 
09166           dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
09167           *dwo_unit = create_dwo_cu_data.dwo_unit;
09168         }
09169 
09170       info_ptr += per_cu.length;
09171     }
09172 
09173   return dwo_unit;
09174 }
09175 
09176 /* DWP file .debug_{cu,tu}_index section format:
09177    [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
09178 
09179    DWP Version 1:
09180 
09181    Both index sections have the same format, and serve to map a 64-bit
09182    signature to a set of section numbers.  Each section begins with a header,
09183    followed by a hash table of 64-bit signatures, a parallel table of 32-bit
09184    indexes, and a pool of 32-bit section numbers.  The index sections will be
09185    aligned at 8-byte boundaries in the file.
09186 
09187    The index section header consists of:
09188 
09189     V, 32 bit version number
09190     -, 32 bits unused
09191     N, 32 bit number of compilation units or type units in the index
09192     M, 32 bit number of slots in the hash table
09193 
09194    Numbers are recorded using the byte order of the application binary.
09195 
09196    The hash table begins at offset 16 in the section, and consists of an array
09197    of M 64-bit slots.  Each slot contains a 64-bit signature (using the byte
09198    order of the application binary).  Unused slots in the hash table are 0.
09199    (We rely on the extreme unlikeliness of a signature being exactly 0.)
09200 
09201    The parallel table begins immediately after the hash table
09202    (at offset 16 + 8 * M from the beginning of the section), and consists of an
09203    array of 32-bit indexes (using the byte order of the application binary),
09204    corresponding 1-1 with slots in the hash table.  Each entry in the parallel
09205    table contains a 32-bit index into the pool of section numbers.  For unused
09206    hash table slots, the corresponding entry in the parallel table will be 0.
09207 
09208    The pool of section numbers begins immediately following the hash table
09209    (at offset 16 + 12 * M from the beginning of the section).  The pool of
09210    section numbers consists of an array of 32-bit words (using the byte order
09211    of the application binary).  Each item in the array is indexed starting
09212    from 0.  The hash table entry provides the index of the first section
09213    number in the set.  Additional section numbers in the set follow, and the
09214    set is terminated by a 0 entry (section number 0 is not used in ELF).
09215 
09216    In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
09217    section must be the first entry in the set, and the .debug_abbrev.dwo must
09218    be the second entry. Other members of the set may follow in any order.
09219 
09220    ---
09221 
09222    DWP Version 2:
09223 
09224    DWP Version 2 combines all the .debug_info, etc. sections into one,
09225    and the entries in the index tables are now offsets into these sections.
09226    CU offsets begin at 0.  TU offsets begin at the size of the .debug_info
09227    section.
09228 
09229    Index Section Contents:
09230     Header
09231     Hash Table of Signatures   dwp_hash_table.hash_table
09232     Parallel Table of Indices  dwp_hash_table.unit_table
09233     Table of Section Offsets   dwp_hash_table.v2.{section_ids,offsets}
09234     Table of Section Sizes     dwp_hash_table.v2.sizes
09235 
09236    The index section header consists of:
09237 
09238     V, 32 bit version number
09239     L, 32 bit number of columns in the table of section offsets
09240     N, 32 bit number of compilation units or type units in the index
09241     M, 32 bit number of slots in the hash table
09242 
09243    Numbers are recorded using the byte order of the application binary.
09244 
09245    The hash table has the same format as version 1.
09246    The parallel table of indices has the same format as version 1,
09247    except that the entries are origin-1 indices into the table of sections
09248    offsets and the table of section sizes.
09249 
09250    The table of offsets begins immediately following the parallel table
09251    (at offset 16 + 12 * M from the beginning of the section).  The table is
09252    a two-dimensional array of 32-bit words (using the byte order of the
09253    application binary), with L columns and N+1 rows, in row-major order.
09254    Each row in the array is indexed starting from 0.  The first row provides
09255    a key to the remaining rows: each column in this row provides an identifier
09256    for a debug section, and the offsets in the same column of subsequent rows
09257    refer to that section.  The section identifiers are:
09258 
09259     DW_SECT_INFO         1  .debug_info.dwo
09260     DW_SECT_TYPES        2  .debug_types.dwo
09261     DW_SECT_ABBREV       3  .debug_abbrev.dwo
09262     DW_SECT_LINE         4  .debug_line.dwo
09263     DW_SECT_LOC          5  .debug_loc.dwo
09264     DW_SECT_STR_OFFSETS  6  .debug_str_offsets.dwo
09265     DW_SECT_MACINFO      7  .debug_macinfo.dwo
09266     DW_SECT_MACRO        8  .debug_macro.dwo
09267 
09268    The offsets provided by the CU and TU index sections are the base offsets
09269    for the contributions made by each CU or TU to the corresponding section
09270    in the package file.  Each CU and TU header contains an abbrev_offset
09271    field, used to find the abbreviations table for that CU or TU within the
09272    contribution to the .debug_abbrev.dwo section for that CU or TU, and should
09273    be interpreted as relative to the base offset given in the index section.
09274    Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
09275    should be interpreted as relative to the base offset for .debug_line.dwo,
09276    and offsets into other debug sections obtained from DWARF attributes should
09277    also be interpreted as relative to the corresponding base offset.
09278 
09279    The table of sizes begins immediately following the table of offsets.
09280    Like the table of offsets, it is a two-dimensional array of 32-bit words,
09281    with L columns and N rows, in row-major order.  Each row in the array is
09282    indexed starting from 1 (row 0 is shared by the two tables).
09283 
09284    ---
09285 
09286    Hash table lookup is handled the same in version 1 and 2:
09287 
09288    We assume that N and M will not exceed 2^32 - 1.
09289    The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
09290 
09291    Given a 64-bit compilation unit signature or a type signature S, an entry
09292    in the hash table is located as follows:
09293 
09294    1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
09295       the low-order k bits all set to 1.
09296 
09297    2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
09298 
09299    3) If the hash table entry at index H matches the signature, use that
09300       entry.  If the hash table entry at index H is unused (all zeroes),
09301       terminate the search: the signature is not present in the table.
09302 
09303    4) Let H = (H + H') modulo M. Repeat at Step 3.
09304 
09305    Because M > N and H' and M are relatively prime, the search is guaranteed
09306    to stop at an unused slot or find the match.  */
09307 
09308 /* Create a hash table to map DWO IDs to their CU/TU entry in
09309    .debug_{info,types}.dwo in DWP_FILE.
09310    Returns NULL if there isn't one.
09311    Note: This function processes DWP files only, not DWO files.  */
09312 
09313 static struct dwp_hash_table *
09314 create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
09315 {
09316   struct objfile *objfile = dwarf2_per_objfile->objfile;
09317   bfd *dbfd = dwp_file->dbfd;
09318   const gdb_byte *index_ptr, *index_end;
09319   struct dwarf2_section_info *index;
09320   uint32_t version, nr_columns, nr_units, nr_slots;
09321   struct dwp_hash_table *htab;
09322 
09323   if (is_debug_types)
09324     index = &dwp_file->sections.tu_index;
09325   else
09326     index = &dwp_file->sections.cu_index;
09327 
09328   if (dwarf2_section_empty_p (index))
09329     return NULL;
09330   dwarf2_read_section (objfile, index);
09331 
09332   index_ptr = index->buffer;
09333   index_end = index_ptr + index->size;
09334 
09335   version = read_4_bytes (dbfd, index_ptr);
09336   index_ptr += 4;
09337   if (version == 2)
09338     nr_columns = read_4_bytes (dbfd, index_ptr);
09339   else
09340     nr_columns = 0;
09341   index_ptr += 4;
09342   nr_units = read_4_bytes (dbfd, index_ptr);
09343   index_ptr += 4;
09344   nr_slots = read_4_bytes (dbfd, index_ptr);
09345   index_ptr += 4;
09346 
09347   if (version != 1 && version != 2)
09348     {
09349       error (_("Dwarf Error: unsupported DWP file version (%s)"
09350                " [in module %s]"),
09351              pulongest (version), dwp_file->name);
09352     }
09353   if (nr_slots != (nr_slots & -nr_slots))
09354     {
09355       error (_("Dwarf Error: number of slots in DWP hash table (%s)"
09356                " is not power of 2 [in module %s]"),
09357              pulongest (nr_slots), dwp_file->name);
09358     }
09359 
09360   htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
09361   htab->version = version;
09362   htab->nr_columns = nr_columns;
09363   htab->nr_units = nr_units;
09364   htab->nr_slots = nr_slots;
09365   htab->hash_table = index_ptr;
09366   htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
09367 
09368   /* Exit early if the table is empty.  */
09369   if (nr_slots == 0 || nr_units == 0
09370       || (version == 2 && nr_columns == 0))
09371     {
09372       /* All must be zero.  */
09373       if (nr_slots != 0 || nr_units != 0
09374           || (version == 2 && nr_columns != 0))
09375         {
09376           complaint (&symfile_complaints,
09377                      _("Empty DWP but nr_slots,nr_units,nr_columns not"
09378                        " all zero [in modules %s]"),
09379                      dwp_file->name);
09380         }
09381       return htab;
09382     }
09383 
09384   if (version == 1)
09385     {
09386       htab->section_pool.v1.indices =
09387         htab->unit_table + sizeof (uint32_t) * nr_slots;
09388       /* It's harder to decide whether the section is too small in v1.
09389          V1 is deprecated anyway so we punt.  */
09390     }
09391   else
09392     {
09393       const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
09394       int *ids = htab->section_pool.v2.section_ids;
09395       /* Reverse map for error checking.  */
09396       int ids_seen[DW_SECT_MAX + 1];
09397       int i;
09398 
09399       if (nr_columns < 2)
09400         {
09401           error (_("Dwarf Error: bad DWP hash table, too few columns"
09402                    " in section table [in module %s]"),
09403                  dwp_file->name);
09404         }
09405       if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
09406         {
09407           error (_("Dwarf Error: bad DWP hash table, too many columns"
09408                    " in section table [in module %s]"),
09409                  dwp_file->name);
09410         }
09411       memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
09412       memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
09413       for (i = 0; i < nr_columns; ++i)
09414         {
09415           int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
09416 
09417           if (id < DW_SECT_MIN || id > DW_SECT_MAX)
09418             {
09419               error (_("Dwarf Error: bad DWP hash table, bad section id %d"
09420                        " in section table [in module %s]"),
09421                      id, dwp_file->name);
09422             }
09423           if (ids_seen[id] != -1)
09424             {
09425               error (_("Dwarf Error: bad DWP hash table, duplicate section"
09426                        " id %d in section table [in module %s]"),
09427                      id, dwp_file->name);
09428             }
09429           ids_seen[id] = i;
09430           ids[i] = id;
09431         }
09432       /* Must have exactly one info or types section.  */
09433       if (((ids_seen[DW_SECT_INFO] != -1)
09434            + (ids_seen[DW_SECT_TYPES] != -1))
09435           != 1)
09436         {
09437           error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
09438                    " DWO info/types section [in module %s]"),
09439                  dwp_file->name);
09440         }
09441       /* Must have an abbrev section.  */
09442       if (ids_seen[DW_SECT_ABBREV] == -1)
09443         {
09444           error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
09445                    " section [in module %s]"),
09446                  dwp_file->name);
09447         }
09448       htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
09449       htab->section_pool.v2.sizes =
09450         htab->section_pool.v2.offsets + (sizeof (uint32_t)
09451                                          * nr_units * nr_columns);
09452       if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
09453                                           * nr_units * nr_columns))
09454           > index_end)
09455         {
09456           error (_("Dwarf Error: DWP index section is corrupt (too small)"
09457                    " [in module %s]"),
09458                  dwp_file->name);
09459         }
09460     }
09461 
09462   return htab;
09463 }
09464 
09465 /* Update SECTIONS with the data from SECTP.
09466 
09467    This function is like the other "locate" section routines that are
09468    passed to bfd_map_over_sections, but in this context the sections to
09469    read comes from the DWP V1 hash table, not the full ELF section table.
09470 
09471    The result is non-zero for success, or zero if an error was found.  */
09472 
09473 static int
09474 locate_v1_virtual_dwo_sections (asection *sectp,
09475                                 struct virtual_v1_dwo_sections *sections)
09476 {
09477   const struct dwop_section_names *names = &dwop_section_names;
09478 
09479   if (section_is_p (sectp->name, &names->abbrev_dwo))
09480     {
09481       /* There can be only one.  */
09482       if (sections->abbrev.s.asection != NULL)
09483         return 0;
09484       sections->abbrev.s.asection = sectp;
09485       sections->abbrev.size = bfd_get_section_size (sectp);
09486     }
09487   else if (section_is_p (sectp->name, &names->info_dwo)
09488            || section_is_p (sectp->name, &names->types_dwo))
09489     {
09490       /* There can be only one.  */
09491       if (sections->info_or_types.s.asection != NULL)
09492         return 0;
09493       sections->info_or_types.s.asection = sectp;
09494       sections->info_or_types.size = bfd_get_section_size (sectp);
09495     }
09496   else if (section_is_p (sectp->name, &names->line_dwo))
09497     {
09498       /* There can be only one.  */
09499       if (sections->line.s.asection != NULL)
09500         return 0;
09501       sections->line.s.asection = sectp;
09502       sections->line.size = bfd_get_section_size (sectp);
09503     }
09504   else if (section_is_p (sectp->name, &names->loc_dwo))
09505     {
09506       /* There can be only one.  */
09507       if (sections->loc.s.asection != NULL)
09508         return 0;
09509       sections->loc.s.asection = sectp;
09510       sections->loc.size = bfd_get_section_size (sectp);
09511     }
09512   else if (section_is_p (sectp->name, &names->macinfo_dwo))
09513     {
09514       /* There can be only one.  */
09515       if (sections->macinfo.s.asection != NULL)
09516         return 0;
09517       sections->macinfo.s.asection = sectp;
09518       sections->macinfo.size = bfd_get_section_size (sectp);
09519     }
09520   else if (section_is_p (sectp->name, &names->macro_dwo))
09521     {
09522       /* There can be only one.  */
09523       if (sections->macro.s.asection != NULL)
09524         return 0;
09525       sections->macro.s.asection = sectp;
09526       sections->macro.size = bfd_get_section_size (sectp);
09527     }
09528   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
09529     {
09530       /* There can be only one.  */
09531       if (sections->str_offsets.s.asection != NULL)
09532         return 0;
09533       sections->str_offsets.s.asection = sectp;
09534       sections->str_offsets.size = bfd_get_section_size (sectp);
09535     }
09536   else
09537     {
09538       /* No other kind of section is valid.  */
09539       return 0;
09540     }
09541 
09542   return 1;
09543 }
09544 
09545 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
09546    UNIT_INDEX is the index of the DWO unit in the DWP hash table.
09547    COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
09548    This is for DWP version 1 files.  */
09549 
09550 static struct dwo_unit *
09551 create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
09552                            uint32_t unit_index,
09553                            const char *comp_dir,
09554                            ULONGEST signature, int is_debug_types)
09555 {
09556   struct objfile *objfile = dwarf2_per_objfile->objfile;
09557   const struct dwp_hash_table *dwp_htab =
09558     is_debug_types ? dwp_file->tus : dwp_file->cus;
09559   bfd *dbfd = dwp_file->dbfd;
09560   const char *kind = is_debug_types ? "TU" : "CU";
09561   struct dwo_file *dwo_file;
09562   struct dwo_unit *dwo_unit;
09563   struct virtual_v1_dwo_sections sections;
09564   void **dwo_file_slot;
09565   char *virtual_dwo_name;
09566   struct dwarf2_section_info *cutu;
09567   struct cleanup *cleanups;
09568   int i;
09569 
09570   gdb_assert (dwp_file->version == 1);
09571 
09572   if (dwarf2_read_debug)
09573     {
09574       fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
09575                           kind,
09576                           pulongest (unit_index), hex_string (signature),
09577                           dwp_file->name);
09578     }
09579 
09580   /* Fetch the sections of this DWO unit.
09581      Put a limit on the number of sections we look for so that bad data
09582      doesn't cause us to loop forever.  */
09583 
09584 #define MAX_NR_V1_DWO_SECTIONS \
09585   (1 /* .debug_info or .debug_types */ \
09586    + 1 /* .debug_abbrev */ \
09587    + 1 /* .debug_line */ \
09588    + 1 /* .debug_loc */ \
09589    + 1 /* .debug_str_offsets */ \
09590    + 1 /* .debug_macro or .debug_macinfo */ \
09591    + 1 /* trailing zero */)
09592 
09593   memset (&sections, 0, sizeof (sections));
09594   cleanups = make_cleanup (null_cleanup, 0);
09595 
09596   for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
09597     {
09598       asection *sectp;
09599       uint32_t section_nr =
09600         read_4_bytes (dbfd,
09601                       dwp_htab->section_pool.v1.indices
09602                       + (unit_index + i) * sizeof (uint32_t));
09603 
09604       if (section_nr == 0)
09605         break;
09606       if (section_nr >= dwp_file->num_sections)
09607         {
09608           error (_("Dwarf Error: bad DWP hash table, section number too large"
09609                    " [in module %s]"),
09610                  dwp_file->name);
09611         }
09612 
09613       sectp = dwp_file->elf_sections[section_nr];
09614       if (! locate_v1_virtual_dwo_sections (sectp, &sections))
09615         {
09616           error (_("Dwarf Error: bad DWP hash table, invalid section found"
09617                    " [in module %s]"),
09618                  dwp_file->name);
09619         }
09620     }
09621 
09622   if (i < 2
09623       || dwarf2_section_empty_p (&sections.info_or_types)
09624       || dwarf2_section_empty_p (&sections.abbrev))
09625     {
09626       error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
09627                " [in module %s]"),
09628              dwp_file->name);
09629     }
09630   if (i == MAX_NR_V1_DWO_SECTIONS)
09631     {
09632       error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
09633                " [in module %s]"),
09634              dwp_file->name);
09635     }
09636 
09637   /* It's easier for the rest of the code if we fake a struct dwo_file and
09638      have dwo_unit "live" in that.  At least for now.
09639 
09640      The DWP file can be made up of a random collection of CUs and TUs.
09641      However, for each CU + set of TUs that came from the same original DWO
09642      file, we can combine them back into a virtual DWO file to save space
09643      (fewer struct dwo_file objects to allocate).  Remember that for really
09644      large apps there can be on the order of 8K CUs and 200K TUs, or more.  */
09645 
09646   virtual_dwo_name =
09647     xstrprintf ("virtual-dwo/%d-%d-%d-%d",
09648                 get_section_id (&sections.abbrev),
09649                 get_section_id (&sections.line),
09650                 get_section_id (&sections.loc),
09651                 get_section_id (&sections.str_offsets));
09652   make_cleanup (xfree, virtual_dwo_name);
09653   /* Can we use an existing virtual DWO file?  */
09654   dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
09655   /* Create one if necessary.  */
09656   if (*dwo_file_slot == NULL)
09657     {
09658       if (dwarf2_read_debug)
09659         {
09660           fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
09661                               virtual_dwo_name);
09662         }
09663       dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
09664       dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
09665                                           virtual_dwo_name,
09666                                           strlen (virtual_dwo_name));
09667       dwo_file->comp_dir = comp_dir;
09668       dwo_file->sections.abbrev = sections.abbrev;
09669       dwo_file->sections.line = sections.line;
09670       dwo_file->sections.loc = sections.loc;
09671       dwo_file->sections.macinfo = sections.macinfo;
09672       dwo_file->sections.macro = sections.macro;
09673       dwo_file->sections.str_offsets = sections.str_offsets;
09674       /* The "str" section is global to the entire DWP file.  */
09675       dwo_file->sections.str = dwp_file->sections.str;
09676       /* The info or types section is assigned below to dwo_unit,
09677          there's no need to record it in dwo_file.
09678          Also, we can't simply record type sections in dwo_file because
09679          we record a pointer into the vector in dwo_unit.  As we collect more
09680          types we'll grow the vector and eventually have to reallocate space
09681          for it, invalidating all copies of pointers into the previous
09682          contents.  */
09683       *dwo_file_slot = dwo_file;
09684     }
09685   else
09686     {
09687       if (dwarf2_read_debug)
09688         {
09689           fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
09690                               virtual_dwo_name);
09691         }
09692       dwo_file = *dwo_file_slot;
09693     }
09694   do_cleanups (cleanups);
09695 
09696   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
09697   dwo_unit->dwo_file = dwo_file;
09698   dwo_unit->signature = signature;
09699   dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
09700                                      sizeof (struct dwarf2_section_info));
09701   *dwo_unit->section = sections.info_or_types;
09702   /* dwo_unit->{offset,length,type_offset_in_tu} are set later.  */
09703 
09704   return dwo_unit;
09705 }
09706 
09707 /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
09708    Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
09709    piece within that section used by a TU/CU, return a virtual section
09710    of just that piece.  */
09711 
09712 static struct dwarf2_section_info
09713 create_dwp_v2_section (struct dwarf2_section_info *section,
09714                        bfd_size_type offset, bfd_size_type size)
09715 {
09716   struct dwarf2_section_info result;
09717   asection *sectp;
09718 
09719   gdb_assert (section != NULL);
09720   gdb_assert (!section->is_virtual);
09721 
09722   memset (&result, 0, sizeof (result));
09723   result.s.containing_section = section;
09724   result.is_virtual = 1;
09725 
09726   if (size == 0)
09727     return result;
09728 
09729   sectp = get_section_bfd_section (section);
09730 
09731   /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
09732      bounds of the real section.  This is a pretty-rare event, so just
09733      flag an error (easier) instead of a warning and trying to cope.  */
09734   if (sectp == NULL
09735       || offset + size > bfd_get_section_size (sectp))
09736     {
09737       bfd *abfd = sectp->owner;
09738 
09739       error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
09740                " in section %s [in module %s]"),
09741              sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
09742              objfile_name (dwarf2_per_objfile->objfile));
09743     }
09744 
09745   result.virtual_offset = offset;
09746   result.size = size;
09747   return result;
09748 }
09749 
09750 /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
09751    UNIT_INDEX is the index of the DWO unit in the DWP hash table.
09752    COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
09753    This is for DWP version 2 files.  */
09754 
09755 static struct dwo_unit *
09756 create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
09757                            uint32_t unit_index,
09758                            const char *comp_dir,
09759                            ULONGEST signature, int is_debug_types)
09760 {
09761   struct objfile *objfile = dwarf2_per_objfile->objfile;
09762   const struct dwp_hash_table *dwp_htab =
09763     is_debug_types ? dwp_file->tus : dwp_file->cus;
09764   bfd *dbfd = dwp_file->dbfd;
09765   const char *kind = is_debug_types ? "TU" : "CU";
09766   struct dwo_file *dwo_file;
09767   struct dwo_unit *dwo_unit;
09768   struct virtual_v2_dwo_sections sections;
09769   void **dwo_file_slot;
09770   char *virtual_dwo_name;
09771   struct dwarf2_section_info *cutu;
09772   struct cleanup *cleanups;
09773   int i;
09774 
09775   gdb_assert (dwp_file->version == 2);
09776 
09777   if (dwarf2_read_debug)
09778     {
09779       fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
09780                           kind,
09781                           pulongest (unit_index), hex_string (signature),
09782                           dwp_file->name);
09783     }
09784 
09785   /* Fetch the section offsets of this DWO unit.  */
09786 
09787   memset (&sections, 0, sizeof (sections));
09788   cleanups = make_cleanup (null_cleanup, 0);
09789 
09790   for (i = 0; i < dwp_htab->nr_columns; ++i)
09791     {
09792       uint32_t offset = read_4_bytes (dbfd,
09793                                       dwp_htab->section_pool.v2.offsets
09794                                       + (((unit_index - 1) * dwp_htab->nr_columns
09795                                           + i)
09796                                          * sizeof (uint32_t)));
09797       uint32_t size = read_4_bytes (dbfd,
09798                                     dwp_htab->section_pool.v2.sizes
09799                                     + (((unit_index - 1) * dwp_htab->nr_columns
09800                                         + i)
09801                                        * sizeof (uint32_t)));
09802 
09803       switch (dwp_htab->section_pool.v2.section_ids[i])
09804         {
09805         case DW_SECT_INFO:
09806         case DW_SECT_TYPES:
09807           sections.info_or_types_offset = offset;
09808           sections.info_or_types_size = size;
09809           break;
09810         case DW_SECT_ABBREV:
09811           sections.abbrev_offset = offset;
09812           sections.abbrev_size = size;
09813           break;
09814         case DW_SECT_LINE:
09815           sections.line_offset = offset;
09816           sections.line_size = size;
09817           break;
09818         case DW_SECT_LOC:
09819           sections.loc_offset = offset;
09820           sections.loc_size = size;
09821           break;
09822         case DW_SECT_STR_OFFSETS:
09823           sections.str_offsets_offset = offset;
09824           sections.str_offsets_size = size;
09825           break;
09826         case DW_SECT_MACINFO:
09827           sections.macinfo_offset = offset;
09828           sections.macinfo_size = size;
09829           break;
09830         case DW_SECT_MACRO:
09831           sections.macro_offset = offset;
09832           sections.macro_size = size;
09833           break;
09834         }
09835     }
09836 
09837   /* It's easier for the rest of the code if we fake a struct dwo_file and
09838      have dwo_unit "live" in that.  At least for now.
09839 
09840      The DWP file can be made up of a random collection of CUs and TUs.
09841      However, for each CU + set of TUs that came from the same original DWO
09842      file, we can combine them back into a virtual DWO file to save space
09843      (fewer struct dwo_file objects to allocate).  Remember that for really
09844      large apps there can be on the order of 8K CUs and 200K TUs, or more.  */
09845 
09846   virtual_dwo_name =
09847     xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
09848                 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
09849                 (long) (sections.line_size ? sections.line_offset : 0),
09850                 (long) (sections.loc_size ? sections.loc_offset : 0),
09851                 (long) (sections.str_offsets_size
09852                         ? sections.str_offsets_offset : 0));
09853   make_cleanup (xfree, virtual_dwo_name);
09854   /* Can we use an existing virtual DWO file?  */
09855   dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
09856   /* Create one if necessary.  */
09857   if (*dwo_file_slot == NULL)
09858     {
09859       if (dwarf2_read_debug)
09860         {
09861           fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
09862                               virtual_dwo_name);
09863         }
09864       dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
09865       dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
09866                                           virtual_dwo_name,
09867                                           strlen (virtual_dwo_name));
09868       dwo_file->comp_dir = comp_dir;
09869       dwo_file->sections.abbrev =
09870         create_dwp_v2_section (&dwp_file->sections.abbrev,
09871                                sections.abbrev_offset, sections.abbrev_size);
09872       dwo_file->sections.line =
09873         create_dwp_v2_section (&dwp_file->sections.line,
09874                                sections.line_offset, sections.line_size);
09875       dwo_file->sections.loc =
09876         create_dwp_v2_section (&dwp_file->sections.loc,
09877                                sections.loc_offset, sections.loc_size);
09878       dwo_file->sections.macinfo =
09879         create_dwp_v2_section (&dwp_file->sections.macinfo,
09880                                sections.macinfo_offset, sections.macinfo_size);
09881       dwo_file->sections.macro =
09882         create_dwp_v2_section (&dwp_file->sections.macro,
09883                                sections.macro_offset, sections.macro_size);
09884       dwo_file->sections.str_offsets =
09885         create_dwp_v2_section (&dwp_file->sections.str_offsets,
09886                                sections.str_offsets_offset,
09887                                sections.str_offsets_size);
09888       /* The "str" section is global to the entire DWP file.  */
09889       dwo_file->sections.str = dwp_file->sections.str;
09890       /* The info or types section is assigned below to dwo_unit,
09891          there's no need to record it in dwo_file.
09892          Also, we can't simply record type sections in dwo_file because
09893          we record a pointer into the vector in dwo_unit.  As we collect more
09894          types we'll grow the vector and eventually have to reallocate space
09895          for it, invalidating all copies of pointers into the previous
09896          contents.  */
09897       *dwo_file_slot = dwo_file;
09898     }
09899   else
09900     {
09901       if (dwarf2_read_debug)
09902         {
09903           fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
09904                               virtual_dwo_name);
09905         }
09906       dwo_file = *dwo_file_slot;
09907     }
09908   do_cleanups (cleanups);
09909 
09910   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
09911   dwo_unit->dwo_file = dwo_file;
09912   dwo_unit->signature = signature;
09913   dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
09914                                      sizeof (struct dwarf2_section_info));
09915   *dwo_unit->section = create_dwp_v2_section (is_debug_types
09916                                               ? &dwp_file->sections.types
09917                                               : &dwp_file->sections.info,
09918                                               sections.info_or_types_offset,
09919                                               sections.info_or_types_size);
09920   /* dwo_unit->{offset,length,type_offset_in_tu} are set later.  */
09921 
09922   return dwo_unit;
09923 }
09924 
09925 /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
09926    Returns NULL if the signature isn't found.  */
09927 
09928 static struct dwo_unit *
09929 lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
09930                         ULONGEST signature, int is_debug_types)
09931 {
09932   const struct dwp_hash_table *dwp_htab =
09933     is_debug_types ? dwp_file->tus : dwp_file->cus;
09934   bfd *dbfd = dwp_file->dbfd;
09935   uint32_t mask = dwp_htab->nr_slots - 1;
09936   uint32_t hash = signature & mask;
09937   uint32_t hash2 = ((signature >> 32) & mask) | 1;
09938   unsigned int i;
09939   void **slot;
09940   struct dwo_unit find_dwo_cu, *dwo_cu;
09941 
09942   memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
09943   find_dwo_cu.signature = signature;
09944   slot = htab_find_slot (is_debug_types
09945                          ? dwp_file->loaded_tus
09946                          : dwp_file->loaded_cus,
09947                          &find_dwo_cu, INSERT);
09948 
09949   if (*slot != NULL)
09950     return *slot;
09951 
09952   /* Use a for loop so that we don't loop forever on bad debug info.  */
09953   for (i = 0; i < dwp_htab->nr_slots; ++i)
09954     {
09955       ULONGEST signature_in_table;
09956 
09957       signature_in_table =
09958         read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
09959       if (signature_in_table == signature)
09960         {
09961           uint32_t unit_index =
09962             read_4_bytes (dbfd,
09963                           dwp_htab->unit_table + hash * sizeof (uint32_t));
09964 
09965           if (dwp_file->version == 1)
09966             {
09967               *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
09968                                                  comp_dir, signature,
09969                                                  is_debug_types);
09970             }
09971           else
09972             {
09973               *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
09974                                                  comp_dir, signature,
09975                                                  is_debug_types);
09976             }
09977           return *slot;
09978         }
09979       if (signature_in_table == 0)
09980         return NULL;
09981       hash = (hash + hash2) & mask;
09982     }
09983 
09984   error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
09985            " [in module %s]"),
09986          dwp_file->name);
09987 }
09988 
09989 /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
09990    Open the file specified by FILE_NAME and hand it off to BFD for
09991    preliminary analysis.  Return a newly initialized bfd *, which
09992    includes a canonicalized copy of FILE_NAME.
09993    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
09994    SEARCH_CWD is true if the current directory is to be searched.
09995    It will be searched before debug-file-directory.
09996    If unable to find/open the file, return NULL.
09997    NOTE: This function is derived from symfile_bfd_open.  */
09998 
09999 static bfd *
10000 try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
10001 {
10002   bfd *sym_bfd;
10003   int desc, flags;
10004   char *absolute_name;
10005   /* Blech.  OPF_TRY_CWD_FIRST also disables searching the path list if
10006      FILE_NAME contains a '/'.  So we can't use it.  Instead prepend "."
10007      to debug_file_directory.  */
10008   char *search_path;
10009   static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
10010 
10011   if (search_cwd)
10012     {
10013       if (*debug_file_directory != '\0')
10014         search_path = concat (".", dirname_separator_string,
10015                               debug_file_directory, NULL);
10016       else
10017         search_path = xstrdup (".");
10018     }
10019   else
10020     search_path = xstrdup (debug_file_directory);
10021 
10022   flags = OPF_RETURN_REALPATH;
10023   if (is_dwp)
10024     flags |= OPF_SEARCH_IN_PATH;
10025   desc = openp (search_path, flags, file_name,
10026                 O_RDONLY | O_BINARY, &absolute_name);
10027   xfree (search_path);
10028   if (desc < 0)
10029     return NULL;
10030 
10031   sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
10032   xfree (absolute_name);
10033   if (sym_bfd == NULL)
10034     return NULL;
10035   bfd_set_cacheable (sym_bfd, 1);
10036 
10037   if (!bfd_check_format (sym_bfd, bfd_object))
10038     {
10039       gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
10040       return NULL;
10041     }
10042 
10043   return sym_bfd;
10044 }
10045 
10046 /* Try to open DWO file FILE_NAME.
10047    COMP_DIR is the DW_AT_comp_dir attribute.
10048    The result is the bfd handle of the file.
10049    If there is a problem finding or opening the file, return NULL.
10050    Upon success, the canonicalized path of the file is stored in the bfd,
10051    same as symfile_bfd_open.  */
10052 
10053 static bfd *
10054 open_dwo_file (const char *file_name, const char *comp_dir)
10055 {
10056   bfd *abfd;
10057 
10058   if (IS_ABSOLUTE_PATH (file_name))
10059     return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);
10060 
10061   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */
10062 
10063   if (comp_dir != NULL)
10064     {
10065       char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);
10066 
10067       /* NOTE: If comp_dir is a relative path, this will also try the
10068          search path, which seems useful.  */
10069       abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
10070       xfree (path_to_try);
10071       if (abfd != NULL)
10072         return abfd;
10073     }
10074 
10075   /* That didn't work, try debug-file-directory, which, despite its name,
10076      is a list of paths.  */
10077 
10078   if (*debug_file_directory == '\0')
10079     return NULL;
10080 
10081   return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
10082 }
10083 
10084 /* This function is mapped across the sections and remembers the offset and
10085    size of each of the DWO debugging sections we are interested in.  */
10086 
10087 static void
10088 dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
10089 {
10090   struct dwo_sections *dwo_sections = dwo_sections_ptr;
10091   const struct dwop_section_names *names = &dwop_section_names;
10092 
10093   if (section_is_p (sectp->name, &names->abbrev_dwo))
10094     {
10095       dwo_sections->abbrev.s.asection = sectp;
10096       dwo_sections->abbrev.size = bfd_get_section_size (sectp);
10097     }
10098   else if (section_is_p (sectp->name, &names->info_dwo))
10099     {
10100       dwo_sections->info.s.asection = sectp;
10101       dwo_sections->info.size = bfd_get_section_size (sectp);
10102     }
10103   else if (section_is_p (sectp->name, &names->line_dwo))
10104     {
10105       dwo_sections->line.s.asection = sectp;
10106       dwo_sections->line.size = bfd_get_section_size (sectp);
10107     }
10108   else if (section_is_p (sectp->name, &names->loc_dwo))
10109     {
10110       dwo_sections->loc.s.asection = sectp;
10111       dwo_sections->loc.size = bfd_get_section_size (sectp);
10112     }
10113   else if (section_is_p (sectp->name, &names->macinfo_dwo))
10114     {
10115       dwo_sections->macinfo.s.asection = sectp;
10116       dwo_sections->macinfo.size = bfd_get_section_size (sectp);
10117     }
10118   else if (section_is_p (sectp->name, &names->macro_dwo))
10119     {
10120       dwo_sections->macro.s.asection = sectp;
10121       dwo_sections->macro.size = bfd_get_section_size (sectp);
10122     }
10123   else if (section_is_p (sectp->name, &names->str_dwo))
10124     {
10125       dwo_sections->str.s.asection = sectp;
10126       dwo_sections->str.size = bfd_get_section_size (sectp);
10127     }
10128   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10129     {
10130       dwo_sections->str_offsets.s.asection = sectp;
10131       dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
10132     }
10133   else if (section_is_p (sectp->name, &names->types_dwo))
10134     {
10135       struct dwarf2_section_info type_section;
10136 
10137       memset (&type_section, 0, sizeof (type_section));
10138       type_section.s.asection = sectp;
10139       type_section.size = bfd_get_section_size (sectp);
10140       VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
10141                      &type_section);
10142     }
10143 }
10144 
10145 /* Initialize the use of the DWO file specified by DWO_NAME and referenced
10146    by PER_CU.  This is for the non-DWP case.
10147    The result is NULL if DWO_NAME can't be found.  */
10148 
10149 static struct dwo_file *
10150 open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
10151                         const char *dwo_name, const char *comp_dir)
10152 {
10153   struct objfile *objfile = dwarf2_per_objfile->objfile;
10154   struct dwo_file *dwo_file;
10155   bfd *dbfd;
10156   struct cleanup *cleanups;
10157 
10158   dbfd = open_dwo_file (dwo_name, comp_dir);
10159   if (dbfd == NULL)
10160     {
10161       if (dwarf2_read_debug)
10162         fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
10163       return NULL;
10164     }
10165   dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
10166   dwo_file->dwo_name = dwo_name;
10167   dwo_file->comp_dir = comp_dir;
10168   dwo_file->dbfd = dbfd;
10169 
10170   cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);
10171 
10172   bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);
10173 
10174   dwo_file->cu = create_dwo_cu (dwo_file);
10175 
10176   dwo_file->tus = create_debug_types_hash_table (dwo_file,
10177                                                  dwo_file->sections.types);
10178 
10179   discard_cleanups (cleanups);
10180 
10181   if (dwarf2_read_debug)
10182     fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
10183 
10184   return dwo_file;
10185 }
10186 
10187 /* This function is mapped across the sections and remembers the offset and
10188    size of each of the DWP debugging sections common to version 1 and 2 that
10189    we are interested in.  */
10190 
10191 static void
10192 dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
10193                                    void *dwp_file_ptr)
10194 {
10195   struct dwp_file *dwp_file = dwp_file_ptr;
10196   const struct dwop_section_names *names = &dwop_section_names;
10197   unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10198 
10199   /* Record the ELF section number for later lookup: this is what the
10200      .debug_cu_index,.debug_tu_index tables use in DWP V1.  */
10201   gdb_assert (elf_section_nr < dwp_file->num_sections);
10202   dwp_file->elf_sections[elf_section_nr] = sectp;
10203 
10204   /* Look for specific sections that we need.  */
10205   if (section_is_p (sectp->name, &names->str_dwo))
10206     {
10207       dwp_file->sections.str.s.asection = sectp;
10208       dwp_file->sections.str.size = bfd_get_section_size (sectp);
10209     }
10210   else if (section_is_p (sectp->name, &names->cu_index))
10211     {
10212       dwp_file->sections.cu_index.s.asection = sectp;
10213       dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
10214     }
10215   else if (section_is_p (sectp->name, &names->tu_index))
10216     {
10217       dwp_file->sections.tu_index.s.asection = sectp;
10218       dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
10219     }
10220 }
10221 
10222 /* This function is mapped across the sections and remembers the offset and
10223    size of each of the DWP version 2 debugging sections that we are interested
10224    in.  This is split into a separate function because we don't know if we
10225    have version 1 or 2 until we parse the cu_index/tu_index sections.  */
10226 
10227 static void
10228 dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
10229 {
10230   struct dwp_file *dwp_file = dwp_file_ptr;
10231   const struct dwop_section_names *names = &dwop_section_names;
10232   unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
10233 
10234   /* Record the ELF section number for later lookup: this is what the
10235      .debug_cu_index,.debug_tu_index tables use in DWP V1.  */
10236   gdb_assert (elf_section_nr < dwp_file->num_sections);
10237   dwp_file->elf_sections[elf_section_nr] = sectp;
10238 
10239   /* Look for specific sections that we need.  */
10240   if (section_is_p (sectp->name, &names->abbrev_dwo))
10241     {
10242       dwp_file->sections.abbrev.s.asection = sectp;
10243       dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
10244     }
10245   else if (section_is_p (sectp->name, &names->info_dwo))
10246     {
10247       dwp_file->sections.info.s.asection = sectp;
10248       dwp_file->sections.info.size = bfd_get_section_size (sectp);
10249     }
10250   else if (section_is_p (sectp->name, &names->line_dwo))
10251     {
10252       dwp_file->sections.line.s.asection = sectp;
10253       dwp_file->sections.line.size = bfd_get_section_size (sectp);
10254     }
10255   else if (section_is_p (sectp->name, &names->loc_dwo))
10256     {
10257       dwp_file->sections.loc.s.asection = sectp;
10258       dwp_file->sections.loc.size = bfd_get_section_size (sectp);
10259     }
10260   else if (section_is_p (sectp->name, &names->macinfo_dwo))
10261     {
10262       dwp_file->sections.macinfo.s.asection = sectp;
10263       dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
10264     }
10265   else if (section_is_p (sectp->name, &names->macro_dwo))
10266     {
10267       dwp_file->sections.macro.s.asection = sectp;
10268       dwp_file->sections.macro.size = bfd_get_section_size (sectp);
10269     }
10270   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
10271     {
10272       dwp_file->sections.str_offsets.s.asection = sectp;
10273       dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
10274     }
10275   else if (section_is_p (sectp->name, &names->types_dwo))
10276     {
10277       dwp_file->sections.types.s.asection = sectp;
10278       dwp_file->sections.types.size = bfd_get_section_size (sectp);
10279     }
10280 }
10281 
10282 /* Hash function for dwp_file loaded CUs/TUs.  */
10283 
10284 static hashval_t
10285 hash_dwp_loaded_cutus (const void *item)
10286 {
10287   const struct dwo_unit *dwo_unit = item;
10288 
10289   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
10290   return dwo_unit->signature;
10291 }
10292 
10293 /* Equality function for dwp_file loaded CUs/TUs.  */
10294 
10295 static int
10296 eq_dwp_loaded_cutus (const void *a, const void *b)
10297 {
10298   const struct dwo_unit *dua = a;
10299   const struct dwo_unit *dub = b;
10300 
10301   return dua->signature == dub->signature;
10302 }
10303 
10304 /* Allocate a hash table for dwp_file loaded CUs/TUs.  */
10305 
10306 static htab_t
10307 allocate_dwp_loaded_cutus_table (struct objfile *objfile)
10308 {
10309   return htab_create_alloc_ex (3,
10310                                hash_dwp_loaded_cutus,
10311                                eq_dwp_loaded_cutus,
10312                                NULL,
10313                                &objfile->objfile_obstack,
10314                                hashtab_obstack_allocate,
10315                                dummy_obstack_deallocate);
10316 }
10317 
10318 /* Try to open DWP file FILE_NAME.
10319    The result is the bfd handle of the file.
10320    If there is a problem finding or opening the file, return NULL.
10321    Upon success, the canonicalized path of the file is stored in the bfd,
10322    same as symfile_bfd_open.  */
10323 
10324 static bfd *
10325 open_dwp_file (const char *file_name)
10326 {
10327   bfd *abfd;
10328 
10329   abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
10330   if (abfd != NULL)
10331     return abfd;
10332 
10333   /* Work around upstream bug 15652.
10334      http://sourceware.org/bugzilla/show_bug.cgi?id=15652
10335      [Whether that's a "bug" is debatable, but it is getting in our way.]
10336      We have no real idea where the dwp file is, because gdb's realpath-ing
10337      of the executable's path may have discarded the needed info.
10338      [IWBN if the dwp file name was recorded in the executable, akin to
10339      .gnu_debuglink, but that doesn't exist yet.]
10340      Strip the directory from FILE_NAME and search again.  */
10341   if (*debug_file_directory != '\0')
10342     {
10343       /* Don't implicitly search the current directory here.
10344          If the user wants to search "." to handle this case,
10345          it must be added to debug-file-directory.  */
10346       return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
10347                                  0 /*search_cwd*/);
10348     }
10349 
10350   return NULL;
10351 }
10352 
10353 /* Initialize the use of the DWP file for the current objfile.
10354    By convention the name of the DWP file is ${objfile}.dwp.
10355    The result is NULL if it can't be found.  */
10356 
10357 static struct dwp_file *
10358 open_and_init_dwp_file (void)
10359 {
10360   struct objfile *objfile = dwarf2_per_objfile->objfile;
10361   struct dwp_file *dwp_file;
10362   char *dwp_name;
10363   bfd *dbfd;
10364   struct cleanup *cleanups;
10365 
10366   /* Try to find first .dwp for the binary file before any symbolic links
10367      resolving.  */
10368   dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
10369   cleanups = make_cleanup (xfree, dwp_name);
10370 
10371   dbfd = open_dwp_file (dwp_name);
10372   if (dbfd == NULL
10373       && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
10374     {
10375       /* Try to find .dwp for the binary file after gdb_realpath resolving.  */
10376       dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
10377       make_cleanup (xfree, dwp_name);
10378       dbfd = open_dwp_file (dwp_name);
10379     }
10380 
10381   if (dbfd == NULL)
10382     {
10383       if (dwarf2_read_debug)
10384         fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
10385       do_cleanups (cleanups);
10386       return NULL;
10387     }
10388   dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
10389   dwp_file->name = bfd_get_filename (dbfd);
10390   dwp_file->dbfd = dbfd;
10391   do_cleanups (cleanups);
10392 
10393   /* +1: section 0 is unused */
10394   dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
10395   dwp_file->elf_sections =
10396     OBSTACK_CALLOC (&objfile->objfile_obstack,
10397                     dwp_file->num_sections, asection *);
10398 
10399   bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);
10400 
10401   dwp_file->cus = create_dwp_hash_table (dwp_file, 0);
10402 
10403   dwp_file->tus = create_dwp_hash_table (dwp_file, 1);
10404 
10405   /* The DWP file version is stored in the hash table.  Oh well.  */
10406   if (dwp_file->cus->version != dwp_file->tus->version)
10407     {
10408       /* Technically speaking, we should try to limp along, but this is
10409          pretty bizarre.  */
10410       error (_("Dwarf Error: DWP file CU version %d doesn't match"
10411                " TU version %d [in DWP file %s]"),
10412              dwp_file->cus->version, dwp_file->tus->version, dwp_name);
10413     }
10414   dwp_file->version = dwp_file->cus->version;
10415 
10416   if (dwp_file->version == 2)
10417     bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);
10418 
10419   dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
10420   dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);
10421 
10422   if (dwarf2_read_debug)
10423     {
10424       fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
10425       fprintf_unfiltered (gdb_stdlog,
10426                           "    %s CUs, %s TUs\n",
10427                           pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
10428                           pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
10429     }
10430 
10431   return dwp_file;
10432 }
10433 
10434 /* Wrapper around open_and_init_dwp_file, only open it once.  */
10435 
10436 static struct dwp_file *
10437 get_dwp_file (void)
10438 {
10439   if (! dwarf2_per_objfile->dwp_checked)
10440     {
10441       dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
10442       dwarf2_per_objfile->dwp_checked = 1;
10443     }
10444   return dwarf2_per_objfile->dwp_file;
10445 }
10446 
10447 /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
10448    Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
10449    or in the DWP file for the objfile, referenced by THIS_UNIT.
10450    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
10451    IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
10452 
10453    This is called, for example, when wanting to read a variable with a
10454    complex location.  Therefore we don't want to do file i/o for every call.
10455    Therefore we don't want to look for a DWO file on every call.
10456    Therefore we first see if we've already seen SIGNATURE in a DWP file,
10457    then we check if we've already seen DWO_NAME, and only THEN do we check
10458    for a DWO file.
10459 
10460    The result is a pointer to the dwo_unit object or NULL if we didn't find it
10461    (dwo_id mismatch or couldn't find the DWO/DWP file).  */
10462 
10463 static struct dwo_unit *
10464 lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
10465                  const char *dwo_name, const char *comp_dir,
10466                  ULONGEST signature, int is_debug_types)
10467 {
10468   struct objfile *objfile = dwarf2_per_objfile->objfile;
10469   const char *kind = is_debug_types ? "TU" : "CU";
10470   void **dwo_file_slot;
10471   struct dwo_file *dwo_file;
10472   struct dwp_file *dwp_file;
10473 
10474   /* First see if there's a DWP file.
10475      If we have a DWP file but didn't find the DWO inside it, don't
10476      look for the original DWO file.  It makes gdb behave differently
10477      depending on whether one is debugging in the build tree.  */
10478 
10479   dwp_file = get_dwp_file ();
10480   if (dwp_file != NULL)
10481     {
10482       const struct dwp_hash_table *dwp_htab =
10483         is_debug_types ? dwp_file->tus : dwp_file->cus;
10484 
10485       if (dwp_htab != NULL)
10486         {
10487           struct dwo_unit *dwo_cutu =
10488             lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
10489                                     signature, is_debug_types);
10490 
10491           if (dwo_cutu != NULL)
10492             {
10493               if (dwarf2_read_debug)
10494                 {
10495                   fprintf_unfiltered (gdb_stdlog,
10496                                       "Virtual DWO %s %s found: @%s\n",
10497                                       kind, hex_string (signature),
10498                                       host_address_to_string (dwo_cutu));
10499                 }
10500               return dwo_cutu;
10501             }
10502         }
10503     }
10504   else
10505     {
10506       /* No DWP file, look for the DWO file.  */
10507 
10508       dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
10509       if (*dwo_file_slot == NULL)
10510         {
10511           /* Read in the file and build a table of the CUs/TUs it contains.  */
10512           *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
10513         }
10514       /* NOTE: This will be NULL if unable to open the file.  */
10515       dwo_file = *dwo_file_slot;
10516 
10517       if (dwo_file != NULL)
10518         {
10519           struct dwo_unit *dwo_cutu = NULL;
10520 
10521           if (is_debug_types && dwo_file->tus)
10522             {
10523               struct dwo_unit find_dwo_cutu;
10524 
10525               memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
10526               find_dwo_cutu.signature = signature;
10527               dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
10528             }
10529           else if (!is_debug_types && dwo_file->cu)
10530             {
10531               if (signature == dwo_file->cu->signature)
10532                 dwo_cutu = dwo_file->cu;
10533             }
10534 
10535           if (dwo_cutu != NULL)
10536             {
10537               if (dwarf2_read_debug)
10538                 {
10539                   fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
10540                                       kind, dwo_name, hex_string (signature),
10541                                       host_address_to_string (dwo_cutu));
10542                 }
10543               return dwo_cutu;
10544             }
10545         }
10546     }
10547 
10548   /* We didn't find it.  This could mean a dwo_id mismatch, or
10549      someone deleted the DWO/DWP file, or the search path isn't set up
10550      correctly to find the file.  */
10551 
10552   if (dwarf2_read_debug)
10553     {
10554       fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
10555                           kind, dwo_name, hex_string (signature));
10556     }
10557 
10558   /* This is a warning and not a complaint because it can be caused by
10559      pilot error (e.g., user accidentally deleting the DWO).  */
10560   warning (_("Could not find DWO %s %s(%s) referenced by %s at offset 0x%x"
10561              " [in module %s]"),
10562            kind, dwo_name, hex_string (signature),
10563            this_unit->is_debug_types ? "TU" : "CU",
10564            this_unit->offset.sect_off, objfile_name (objfile));
10565   return NULL;
10566 }
10567 
10568 /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
10569    See lookup_dwo_cutu_unit for details.  */
10570 
10571 static struct dwo_unit *
10572 lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
10573                       const char *dwo_name, const char *comp_dir,
10574                       ULONGEST signature)
10575 {
10576   return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
10577 }
10578 
10579 /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
10580    See lookup_dwo_cutu_unit for details.  */
10581 
10582 static struct dwo_unit *
10583 lookup_dwo_type_unit (struct signatured_type *this_tu,
10584                       const char *dwo_name, const char *comp_dir)
10585 {
10586   return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
10587 }
10588 
10589 /* Traversal function for queue_and_load_all_dwo_tus.  */
10590 
10591 static int
10592 queue_and_load_dwo_tu (void **slot, void *info)
10593 {
10594   struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
10595   struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
10596   ULONGEST signature = dwo_unit->signature;
10597   struct signatured_type *sig_type =
10598     lookup_dwo_signatured_type (per_cu->cu, signature);
10599 
10600   if (sig_type != NULL)
10601     {
10602       struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
10603 
10604       /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
10605          a real dependency of PER_CU on SIG_TYPE.  That is detected later
10606          while processing PER_CU.  */
10607       if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
10608         load_full_type_unit (sig_cu);
10609       VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
10610     }
10611 
10612   return 1;
10613 }
10614 
10615 /* Queue all TUs contained in the DWO of PER_CU to be read in.
10616    The DWO may have the only definition of the type, though it may not be
10617    referenced anywhere in PER_CU.  Thus we have to load *all* its TUs.
10618    http://sourceware.org/bugzilla/show_bug.cgi?id=15021  */
10619 
10620 static void
10621 queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
10622 {
10623   struct dwo_unit *dwo_unit;
10624   struct dwo_file *dwo_file;
10625 
10626   gdb_assert (!per_cu->is_debug_types);
10627   gdb_assert (get_dwp_file () == NULL);
10628   gdb_assert (per_cu->cu != NULL);
10629 
10630   dwo_unit = per_cu->cu->dwo_unit;
10631   gdb_assert (dwo_unit != NULL);
10632 
10633   dwo_file = dwo_unit->dwo_file;
10634   if (dwo_file->tus != NULL)
10635     htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
10636 }
10637 
10638 /* Free all resources associated with DWO_FILE.
10639    Close the DWO file and munmap the sections.
10640    All memory should be on the objfile obstack.  */
10641 
10642 static void
10643 free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
10644 {
10645   int ix;
10646   struct dwarf2_section_info *section;
10647 
10648   /* Note: dbfd is NULL for virtual DWO files.  */
10649   gdb_bfd_unref (dwo_file->dbfd);
10650 
10651   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
10652 }
10653 
10654 /* Wrapper for free_dwo_file for use in cleanups.  */
10655 
10656 static void
10657 free_dwo_file_cleanup (void *arg)
10658 {
10659   struct dwo_file *dwo_file = (struct dwo_file *) arg;
10660   struct objfile *objfile = dwarf2_per_objfile->objfile;
10661 
10662   free_dwo_file (dwo_file, objfile);
10663 }
10664 
10665 /* Traversal function for free_dwo_files.  */
10666 
10667 static int
10668 free_dwo_file_from_slot (void **slot, void *info)
10669 {
10670   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
10671   struct objfile *objfile = (struct objfile *) info;
10672 
10673   free_dwo_file (dwo_file, objfile);
10674 
10675   return 1;
10676 }
10677 
10678 /* Free all resources associated with DWO_FILES.  */
10679 
10680 static void
10681 free_dwo_files (htab_t dwo_files, struct objfile *objfile)
10682 {
10683   htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
10684 }
10685 
10686 /* Read in various DIEs.  */
10687 
10688 /* qsort helper for inherit_abstract_dies.  */
10689 
10690 static int
10691 unsigned_int_compar (const void *ap, const void *bp)
10692 {
10693   unsigned int a = *(unsigned int *) ap;
10694   unsigned int b = *(unsigned int *) bp;
10695 
10696   return (a > b) - (b > a);
10697 }
10698 
10699 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
10700    Inherit only the children of the DW_AT_abstract_origin DIE not being
10701    already referenced by DW_AT_abstract_origin from the children of the
10702    current DIE.  */
10703 
10704 static void
10705 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
10706 {
10707   struct die_info *child_die;
10708   unsigned die_children_count;
10709   /* CU offsets which were referenced by children of the current DIE.  */
10710   sect_offset *offsets;
10711   sect_offset *offsets_end, *offsetp;
10712   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
10713   struct die_info *origin_die;
10714   /* Iterator of the ORIGIN_DIE children.  */
10715   struct die_info *origin_child_die;
10716   struct cleanup *cleanups;
10717   struct attribute *attr;
10718   struct dwarf2_cu *origin_cu;
10719   struct pending **origin_previous_list_in_scope;
10720 
10721   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
10722   if (!attr)
10723     return;
10724 
10725   /* Note that following die references may follow to a die in a
10726      different cu.  */
10727 
10728   origin_cu = cu;
10729   origin_die = follow_die_ref (die, attr, &origin_cu);
10730 
10731   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
10732      symbols in.  */
10733   origin_previous_list_in_scope = origin_cu->list_in_scope;
10734   origin_cu->list_in_scope = cu->list_in_scope;
10735 
10736   if (die->tag != origin_die->tag
10737       && !(die->tag == DW_TAG_inlined_subroutine
10738            && origin_die->tag == DW_TAG_subprogram))
10739     complaint (&symfile_complaints,
10740                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
10741                die->offset.sect_off, origin_die->offset.sect_off);
10742 
10743   child_die = die->child;
10744   die_children_count = 0;
10745   while (child_die && child_die->tag)
10746     {
10747       child_die = sibling_die (child_die);
10748       die_children_count++;
10749     }
10750   offsets = xmalloc (sizeof (*offsets) * die_children_count);
10751   cleanups = make_cleanup (xfree, offsets);
10752 
10753   offsets_end = offsets;
10754   child_die = die->child;
10755   while (child_die && child_die->tag)
10756     {
10757       /* For each CHILD_DIE, find the corresponding child of
10758          ORIGIN_DIE.  If there is more than one layer of
10759          DW_AT_abstract_origin, follow them all; there shouldn't be,
10760          but GCC versions at least through 4.4 generate this (GCC PR
10761          40573).  */
10762       struct die_info *child_origin_die = child_die;
10763       struct dwarf2_cu *child_origin_cu = cu;
10764 
10765       while (1)
10766         {
10767           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
10768                               child_origin_cu);
10769           if (attr == NULL)
10770             break;
10771           child_origin_die = follow_die_ref (child_origin_die, attr,
10772                                              &child_origin_cu);
10773         }
10774 
10775       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
10776          counterpart may exist.  */
10777       if (child_origin_die != child_die)
10778         {
10779           if (child_die->tag != child_origin_die->tag
10780               && !(child_die->tag == DW_TAG_inlined_subroutine
10781                    && child_origin_die->tag == DW_TAG_subprogram))
10782             complaint (&symfile_complaints,
10783                        _("Child DIE 0x%x and its abstract origin 0x%x have "
10784                          "different tags"), child_die->offset.sect_off,
10785                        child_origin_die->offset.sect_off);
10786           if (child_origin_die->parent != origin_die)
10787             complaint (&symfile_complaints,
10788                        _("Child DIE 0x%x and its abstract origin 0x%x have "
10789                          "different parents"), child_die->offset.sect_off,
10790                        child_origin_die->offset.sect_off);
10791           else
10792             *offsets_end++ = child_origin_die->offset;
10793         }
10794       child_die = sibling_die (child_die);
10795     }
10796   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
10797          unsigned_int_compar);
10798   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
10799     if (offsetp[-1].sect_off == offsetp->sect_off)
10800       complaint (&symfile_complaints,
10801                  _("Multiple children of DIE 0x%x refer "
10802                    "to DIE 0x%x as their abstract origin"),
10803                  die->offset.sect_off, offsetp->sect_off);
10804 
10805   offsetp = offsets;
10806   origin_child_die = origin_die->child;
10807   while (origin_child_die && origin_child_die->tag)
10808     {
10809       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
10810       while (offsetp < offsets_end
10811              && offsetp->sect_off < origin_child_die->offset.sect_off)
10812         offsetp++;
10813       if (offsetp >= offsets_end
10814           || offsetp->sect_off > origin_child_die->offset.sect_off)
10815         {
10816           /* Found that ORIGIN_CHILD_DIE is really not referenced.  */
10817           process_die (origin_child_die, origin_cu);
10818         }
10819       origin_child_die = sibling_die (origin_child_die);
10820     }
10821   origin_cu->list_in_scope = origin_previous_list_in_scope;
10822 
10823   do_cleanups (cleanups);
10824 }
10825 
10826 static void
10827 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
10828 {
10829   struct objfile *objfile = cu->objfile;
10830   struct context_stack *new;
10831   CORE_ADDR lowpc;
10832   CORE_ADDR highpc;
10833   struct die_info *child_die;
10834   struct attribute *attr, *call_line, *call_file;
10835   const char *name;
10836   CORE_ADDR baseaddr;
10837   struct block *block;
10838   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10839   VEC (symbolp) *template_args = NULL;
10840   struct template_symbol *templ_func = NULL;
10841 
10842   if (inlined_func)
10843     {
10844       /* If we do not have call site information, we can't show the
10845          caller of this inlined function.  That's too confusing, so
10846          only use the scope for local variables.  */
10847       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
10848       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
10849       if (call_line == NULL || call_file == NULL)
10850         {
10851           read_lexical_block_scope (die, cu);
10852           return;
10853         }
10854     }
10855 
10856   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10857 
10858   name = dwarf2_name (die, cu);
10859 
10860   /* Ignore functions with missing or empty names.  These are actually
10861      illegal according to the DWARF standard.  */
10862   if (name == NULL)
10863     {
10864       complaint (&symfile_complaints,
10865                  _("missing name for subprogram DIE at %d"),
10866                  die->offset.sect_off);
10867       return;
10868     }
10869 
10870   /* Ignore functions with missing or invalid low and high pc attributes.  */
10871   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
10872     {
10873       attr = dwarf2_attr (die, DW_AT_external, cu);
10874       if (!attr || !DW_UNSND (attr))
10875         complaint (&symfile_complaints,
10876                    _("cannot get low and high bounds "
10877                      "for subprogram DIE at %d"),
10878                    die->offset.sect_off);
10879       return;
10880     }
10881 
10882   lowpc += baseaddr;
10883   highpc += baseaddr;
10884 
10885   /* If we have any template arguments, then we must allocate a
10886      different sort of symbol.  */
10887   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
10888     {
10889       if (child_die->tag == DW_TAG_template_type_param
10890           || child_die->tag == DW_TAG_template_value_param)
10891         {
10892           templ_func = allocate_template_symbol (objfile);
10893           templ_func->base.is_cplus_template_function = 1;
10894           break;
10895         }
10896     }
10897 
10898   new = push_context (0, lowpc);
10899   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
10900                                (struct symbol *) templ_func);
10901 
10902   /* If there is a location expression for DW_AT_frame_base, record
10903      it.  */
10904   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
10905   if (attr)
10906     dwarf2_symbol_mark_computed (attr, new->name, cu, 1);
10907 
10908   cu->list_in_scope = &local_symbols;
10909 
10910   if (die->child != NULL)
10911     {
10912       child_die = die->child;
10913       while (child_die && child_die->tag)
10914         {
10915           if (child_die->tag == DW_TAG_template_type_param
10916               || child_die->tag == DW_TAG_template_value_param)
10917             {
10918               struct symbol *arg = new_symbol (child_die, NULL, cu);
10919 
10920               if (arg != NULL)
10921                 VEC_safe_push (symbolp, template_args, arg);
10922             }
10923           else
10924             process_die (child_die, cu);
10925           child_die = sibling_die (child_die);
10926         }
10927     }
10928 
10929   inherit_abstract_dies (die, cu);
10930 
10931   /* If we have a DW_AT_specification, we might need to import using
10932      directives from the context of the specification DIE.  See the
10933      comment in determine_prefix.  */
10934   if (cu->language == language_cplus
10935       && dwarf2_attr (die, DW_AT_specification, cu))
10936     {
10937       struct dwarf2_cu *spec_cu = cu;
10938       struct die_info *spec_die = die_specification (die, &spec_cu);
10939 
10940       while (spec_die)
10941         {
10942           child_die = spec_die->child;
10943           while (child_die && child_die->tag)
10944             {
10945               if (child_die->tag == DW_TAG_imported_module)
10946                 process_die (child_die, spec_cu);
10947               child_die = sibling_die (child_die);
10948             }
10949 
10950           /* In some cases, GCC generates specification DIEs that
10951              themselves contain DW_AT_specification attributes.  */
10952           spec_die = die_specification (spec_die, &spec_cu);
10953         }
10954     }
10955 
10956   new = pop_context ();
10957   /* Make a block for the local symbols within.  */
10958   block = finish_block (new->name, &local_symbols, new->old_blocks,
10959                         lowpc, highpc, objfile);
10960 
10961   /* For C++, set the block's scope.  */
10962   if ((cu->language == language_cplus || cu->language == language_fortran)
10963       && cu->processing_has_namespace_info)
10964     block_set_scope (block, determine_prefix (die, cu),
10965                      &objfile->objfile_obstack);
10966 
10967   /* If we have address ranges, record them.  */
10968   dwarf2_record_block_ranges (die, block, baseaddr, cu);
10969 
10970   /* Attach template arguments to function.  */
10971   if (! VEC_empty (symbolp, template_args))
10972     {
10973       gdb_assert (templ_func != NULL);
10974 
10975       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
10976       templ_func->template_arguments
10977         = obstack_alloc (&objfile->objfile_obstack,
10978                          (templ_func->n_template_arguments
10979                           * sizeof (struct symbol *)));
10980       memcpy (templ_func->template_arguments,
10981               VEC_address (symbolp, template_args),
10982               (templ_func->n_template_arguments * sizeof (struct symbol *)));
10983       VEC_free (symbolp, template_args);
10984     }
10985 
10986   /* In C++, we can have functions nested inside functions (e.g., when
10987      a function declares a class that has methods).  This means that
10988      when we finish processing a function scope, we may need to go
10989      back to building a containing block's symbol lists.  */
10990   local_symbols = new->locals;
10991   using_directives = new->using_directives;
10992 
10993   /* If we've finished processing a top-level function, subsequent
10994      symbols go in the file symbol list.  */
10995   if (outermost_context_p ())
10996     cu->list_in_scope = &file_symbols;
10997 }
10998 
10999 /* Process all the DIES contained within a lexical block scope.  Start
11000    a new scope, process the dies, and then close the scope.  */
11001 
11002 static void
11003 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
11004 {
11005   struct objfile *objfile = cu->objfile;
11006   struct context_stack *new;
11007   CORE_ADDR lowpc, highpc;
11008   struct die_info *child_die;
11009   CORE_ADDR baseaddr;
11010 
11011   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11012 
11013   /* Ignore blocks with missing or invalid low and high pc attributes.  */
11014   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
11015      as multiple lexical blocks?  Handling children in a sane way would
11016      be nasty.  Might be easier to properly extend generic blocks to
11017      describe ranges.  */
11018   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
11019     return;
11020   lowpc += baseaddr;
11021   highpc += baseaddr;
11022 
11023   push_context (0, lowpc);
11024   if (die->child != NULL)
11025     {
11026       child_die = die->child;
11027       while (child_die && child_die->tag)
11028         {
11029           process_die (child_die, cu);
11030           child_die = sibling_die (child_die);
11031         }
11032     }
11033   new = pop_context ();
11034 
11035   if (local_symbols != NULL || using_directives != NULL)
11036     {
11037       struct block *block
11038         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
11039                         highpc, objfile);
11040 
11041       /* Note that recording ranges after traversing children, as we
11042          do here, means that recording a parent's ranges entails
11043          walking across all its children's ranges as they appear in
11044          the address map, which is quadratic behavior.
11045 
11046          It would be nicer to record the parent's ranges before
11047          traversing its children, simply overriding whatever you find
11048          there.  But since we don't even decide whether to create a
11049          block until after we've traversed its children, that's hard
11050          to do.  */
11051       dwarf2_record_block_ranges (die, block, baseaddr, cu);
11052     }
11053   local_symbols = new->locals;
11054   using_directives = new->using_directives;
11055 }
11056 
11057 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */
11058 
11059 static void
11060 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
11061 {
11062   struct objfile *objfile = cu->objfile;
11063   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11064   CORE_ADDR pc, baseaddr;
11065   struct attribute *attr;
11066   struct call_site *call_site, call_site_local;
11067   void **slot;
11068   int nparams;
11069   struct die_info *child_die;
11070 
11071   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11072 
11073   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11074   if (!attr)
11075     {
11076       complaint (&symfile_complaints,
11077                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
11078                    "DIE 0x%x [in module %s]"),
11079                  die->offset.sect_off, objfile_name (objfile));
11080       return;
11081     }
11082   pc = DW_ADDR (attr) + baseaddr;
11083 
11084   if (cu->call_site_htab == NULL)
11085     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
11086                                                NULL, &objfile->objfile_obstack,
11087                                                hashtab_obstack_allocate, NULL);
11088   call_site_local.pc = pc;
11089   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
11090   if (*slot != NULL)
11091     {
11092       complaint (&symfile_complaints,
11093                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
11094                    "DIE 0x%x [in module %s]"),
11095                  paddress (gdbarch, pc), die->offset.sect_off,
11096                  objfile_name (objfile));
11097       return;
11098     }
11099 
11100   /* Count parameters at the caller.  */
11101 
11102   nparams = 0;
11103   for (child_die = die->child; child_die && child_die->tag;
11104        child_die = sibling_die (child_die))
11105     {
11106       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11107         {
11108           complaint (&symfile_complaints,
11109                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
11110                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11111                      child_die->tag, child_die->offset.sect_off,
11112                      objfile_name (objfile));
11113           continue;
11114         }
11115 
11116       nparams++;
11117     }
11118 
11119   call_site = obstack_alloc (&objfile->objfile_obstack,
11120                              (sizeof (*call_site)
11121                               + (sizeof (*call_site->parameter)
11122                                  * (nparams - 1))));
11123   *slot = call_site;
11124   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
11125   call_site->pc = pc;
11126 
11127   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
11128     {
11129       struct die_info *func_die;
11130 
11131       /* Skip also over DW_TAG_inlined_subroutine.  */
11132       for (func_die = die->parent;
11133            func_die && func_die->tag != DW_TAG_subprogram
11134            && func_die->tag != DW_TAG_subroutine_type;
11135            func_die = func_die->parent);
11136 
11137       /* DW_AT_GNU_all_call_sites is a superset
11138          of DW_AT_GNU_all_tail_call_sites.  */
11139       if (func_die
11140           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
11141           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
11142         {
11143           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
11144              not complete.  But keep CALL_SITE for look ups via call_site_htab,
11145              both the initial caller containing the real return address PC and
11146              the final callee containing the current PC of a chain of tail
11147              calls do not need to have the tail call list complete.  But any
11148              function candidate for a virtual tail call frame searched via
11149              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
11150              determined unambiguously.  */
11151         }
11152       else
11153         {
11154           struct type *func_type = NULL;
11155 
11156           if (func_die)
11157             func_type = get_die_type (func_die, cu);
11158           if (func_type != NULL)
11159             {
11160               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
11161 
11162               /* Enlist this call site to the function.  */
11163               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
11164               TYPE_TAIL_CALL_LIST (func_type) = call_site;
11165             }
11166           else
11167             complaint (&symfile_complaints,
11168                        _("Cannot find function owning DW_TAG_GNU_call_site "
11169                          "DIE 0x%x [in module %s]"),
11170                        die->offset.sect_off, objfile_name (objfile));
11171         }
11172     }
11173 
11174   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
11175   if (attr == NULL)
11176     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
11177   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
11178   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
11179     /* Keep NULL DWARF_BLOCK.  */;
11180   else if (attr_form_is_block (attr))
11181     {
11182       struct dwarf2_locexpr_baton *dlbaton;
11183 
11184       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
11185       dlbaton->data = DW_BLOCK (attr)->data;
11186       dlbaton->size = DW_BLOCK (attr)->size;
11187       dlbaton->per_cu = cu->per_cu;
11188 
11189       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
11190     }
11191   else if (attr_form_is_ref (attr))
11192     {
11193       struct dwarf2_cu *target_cu = cu;
11194       struct die_info *target_die;
11195 
11196       target_die = follow_die_ref (die, attr, &target_cu);
11197       gdb_assert (target_cu->objfile == objfile);
11198       if (die_is_declaration (target_die, target_cu))
11199         {
11200           const char *target_physname = NULL;
11201           struct attribute *target_attr;
11202 
11203           /* Prefer the mangled name; otherwise compute the demangled one.  */
11204           target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
11205           if (target_attr == NULL)
11206             target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
11207                                        target_cu);
11208           if (target_attr != NULL && DW_STRING (target_attr) != NULL)
11209             target_physname = DW_STRING (target_attr);
11210           else
11211             target_physname = dwarf2_physname (NULL, target_die, target_cu);
11212           if (target_physname == NULL)
11213             complaint (&symfile_complaints,
11214                        _("DW_AT_GNU_call_site_target target DIE has invalid "
11215                          "physname, for referencing DIE 0x%x [in module %s]"),
11216                        die->offset.sect_off, objfile_name (objfile));
11217           else
11218             SET_FIELD_PHYSNAME (call_site->target, target_physname);
11219         }
11220       else
11221         {
11222           CORE_ADDR lowpc;
11223 
11224           /* DW_AT_entry_pc should be preferred.  */
11225           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
11226             complaint (&symfile_complaints,
11227                        _("DW_AT_GNU_call_site_target target DIE has invalid "
11228                          "low pc, for referencing DIE 0x%x [in module %s]"),
11229                        die->offset.sect_off, objfile_name (objfile));
11230           else
11231             SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
11232         }
11233     }
11234   else
11235     complaint (&symfile_complaints,
11236                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
11237                  "block nor reference, for DIE 0x%x [in module %s]"),
11238                die->offset.sect_off, objfile_name (objfile));
11239 
11240   call_site->per_cu = cu->per_cu;
11241 
11242   for (child_die = die->child;
11243        child_die && child_die->tag;
11244        child_die = sibling_die (child_die))
11245     {
11246       struct call_site_parameter *parameter;
11247       struct attribute *loc, *origin;
11248 
11249       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
11250         {
11251           /* Already printed the complaint above.  */
11252           continue;
11253         }
11254 
11255       gdb_assert (call_site->parameter_count < nparams);
11256       parameter = &call_site->parameter[call_site->parameter_count];
11257 
11258       /* DW_AT_location specifies the register number or DW_AT_abstract_origin
11259          specifies DW_TAG_formal_parameter.  Value of the data assumed for the
11260          register is contained in DW_AT_GNU_call_site_value.  */
11261 
11262       loc = dwarf2_attr (child_die, DW_AT_location, cu);
11263       origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
11264       if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
11265         {
11266           sect_offset offset;
11267 
11268           parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
11269           offset = dwarf2_get_ref_die_offset (origin);
11270           if (!offset_in_cu_p (&cu->header, offset))
11271             {
11272               /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
11273                  binding can be done only inside one CU.  Such referenced DIE
11274                  therefore cannot be even moved to DW_TAG_partial_unit.  */
11275               complaint (&symfile_complaints,
11276                          _("DW_AT_abstract_origin offset is not in CU for "
11277                            "DW_TAG_GNU_call_site child DIE 0x%x "
11278                            "[in module %s]"),
11279                          child_die->offset.sect_off, objfile_name (objfile));
11280               continue;
11281             }
11282           parameter->u.param_offset.cu_off = (offset.sect_off
11283                                               - cu->header.offset.sect_off);
11284         }
11285       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
11286         {
11287           complaint (&symfile_complaints,
11288                      _("No DW_FORM_block* DW_AT_location for "
11289                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11290                      child_die->offset.sect_off, objfile_name (objfile));
11291           continue;
11292         }
11293       else
11294         {
11295           parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
11296             (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
11297           if (parameter->u.dwarf_reg != -1)
11298             parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
11299           else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
11300                                     &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
11301                                              &parameter->u.fb_offset))
11302             parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
11303           else
11304             {
11305               complaint (&symfile_complaints,
11306                          _("Only single DW_OP_reg or DW_OP_fbreg is supported "
11307                            "for DW_FORM_block* DW_AT_location is supported for "
11308                            "DW_TAG_GNU_call_site child DIE 0x%x "
11309                            "[in module %s]"),
11310                          child_die->offset.sect_off, objfile_name (objfile));
11311               continue;
11312             }
11313         }
11314 
11315       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
11316       if (!attr_form_is_block (attr))
11317         {
11318           complaint (&symfile_complaints,
11319                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
11320                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11321                      child_die->offset.sect_off, objfile_name (objfile));
11322           continue;
11323         }
11324       parameter->value = DW_BLOCK (attr)->data;
11325       parameter->value_size = DW_BLOCK (attr)->size;
11326 
11327       /* Parameters are not pre-cleared by memset above.  */
11328       parameter->data_value = NULL;
11329       parameter->data_value_size = 0;
11330       call_site->parameter_count++;
11331 
11332       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
11333       if (attr)
11334         {
11335           if (!attr_form_is_block (attr))
11336             complaint (&symfile_complaints,
11337                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
11338                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
11339                        child_die->offset.sect_off, objfile_name (objfile));
11340           else
11341             {
11342               parameter->data_value = DW_BLOCK (attr)->data;
11343               parameter->data_value_size = DW_BLOCK (attr)->size;
11344             }
11345         }
11346     }
11347 }
11348 
11349 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
11350    Return 1 if the attributes are present and valid, otherwise, return 0.
11351    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */
11352 
11353 static int
11354 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
11355                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
11356                     struct partial_symtab *ranges_pst)
11357 {
11358   struct objfile *objfile = cu->objfile;
11359   struct comp_unit_head *cu_header = &cu->header;
11360   bfd *obfd = objfile->obfd;
11361   unsigned int addr_size = cu_header->addr_size;
11362   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11363   /* Base address selection entry.  */
11364   CORE_ADDR base;
11365   int found_base;
11366   unsigned int dummy;
11367   const gdb_byte *buffer;
11368   CORE_ADDR marker;
11369   int low_set;
11370   CORE_ADDR low = 0;
11371   CORE_ADDR high = 0;
11372   CORE_ADDR baseaddr;
11373 
11374   found_base = cu->base_known;
11375   base = cu->base_address;
11376 
11377   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11378   if (offset >= dwarf2_per_objfile->ranges.size)
11379     {
11380       complaint (&symfile_complaints,
11381                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
11382                  offset);
11383       return 0;
11384     }
11385   buffer = dwarf2_per_objfile->ranges.buffer + offset;
11386 
11387   /* Read in the largest possible address.  */
11388   marker = read_address (obfd, buffer, cu, &dummy);
11389   if ((marker & mask) == mask)
11390     {
11391       /* If we found the largest possible address, then
11392          read the base address.  */
11393       base = read_address (obfd, buffer + addr_size, cu, &dummy);
11394       buffer += 2 * addr_size;
11395       offset += 2 * addr_size;
11396       found_base = 1;
11397     }
11398 
11399   low_set = 0;
11400 
11401   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11402 
11403   while (1)
11404     {
11405       CORE_ADDR range_beginning, range_end;
11406 
11407       range_beginning = read_address (obfd, buffer, cu, &dummy);
11408       buffer += addr_size;
11409       range_end = read_address (obfd, buffer, cu, &dummy);
11410       buffer += addr_size;
11411       offset += 2 * addr_size;
11412 
11413       /* An end of list marker is a pair of zero addresses.  */
11414       if (range_beginning == 0 && range_end == 0)
11415         /* Found the end of list entry.  */
11416         break;
11417 
11418       /* Each base address selection entry is a pair of 2 values.
11419          The first is the largest possible address, the second is
11420          the base address.  Check for a base address here.  */
11421       if ((range_beginning & mask) == mask)
11422         {
11423           /* If we found the largest possible address, then
11424              read the base address.  */
11425           base = read_address (obfd, buffer + addr_size, cu, &dummy);
11426           found_base = 1;
11427           continue;
11428         }
11429 
11430       if (!found_base)
11431         {
11432           /* We have no valid base address for the ranges
11433              data.  */
11434           complaint (&symfile_complaints,
11435                      _("Invalid .debug_ranges data (no base address)"));
11436           return 0;
11437         }
11438 
11439       if (range_beginning > range_end)
11440         {
11441           /* Inverted range entries are invalid.  */
11442           complaint (&symfile_complaints,
11443                      _("Invalid .debug_ranges data (inverted range)"));
11444           return 0;
11445         }
11446 
11447       /* Empty range entries have no effect.  */
11448       if (range_beginning == range_end)
11449         continue;
11450 
11451       range_beginning += base;
11452       range_end += base;
11453 
11454       /* A not-uncommon case of bad debug info.
11455          Don't pollute the addrmap with bad data.  */
11456       if (range_beginning + baseaddr == 0
11457           && !dwarf2_per_objfile->has_section_at_zero)
11458         {
11459           complaint (&symfile_complaints,
11460                      _(".debug_ranges entry has start address of zero"
11461                        " [in module %s]"), objfile_name (objfile));
11462           continue;
11463         }
11464 
11465       if (ranges_pst != NULL)
11466         addrmap_set_empty (objfile->psymtabs_addrmap,
11467                            range_beginning + baseaddr,
11468                            range_end - 1 + baseaddr,
11469                            ranges_pst);
11470 
11471       /* FIXME: This is recording everything as a low-high
11472          segment of consecutive addresses.  We should have a
11473          data structure for discontiguous block ranges
11474          instead.  */
11475       if (! low_set)
11476         {
11477           low = range_beginning;
11478           high = range_end;
11479           low_set = 1;
11480         }
11481       else
11482         {
11483           if (range_beginning < low)
11484             low = range_beginning;
11485           if (range_end > high)
11486             high = range_end;
11487         }
11488     }
11489 
11490   if (! low_set)
11491     /* If the first entry is an end-of-list marker, the range
11492        describes an empty scope, i.e. no instructions.  */
11493     return 0;
11494 
11495   if (low_return)
11496     *low_return = low;
11497   if (high_return)
11498     *high_return = high;
11499   return 1;
11500 }
11501 
11502 /* Get low and high pc attributes from a die.  Return 1 if the attributes
11503    are present and valid, otherwise, return 0.  Return -1 if the range is
11504    discontinuous, i.e. derived from DW_AT_ranges information.  */
11505 
11506 static int
11507 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
11508                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
11509                       struct partial_symtab *pst)
11510 {
11511   struct attribute *attr;
11512   struct attribute *attr_high;
11513   CORE_ADDR low = 0;
11514   CORE_ADDR high = 0;
11515   int ret = 0;
11516 
11517   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11518   if (attr_high)
11519     {
11520       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11521       if (attr)
11522         {
11523           low = DW_ADDR (attr);
11524           if (attr_high->form == DW_FORM_addr
11525               || attr_high->form == DW_FORM_GNU_addr_index)
11526             high = DW_ADDR (attr_high);
11527           else
11528             high = low + DW_UNSND (attr_high);
11529         }
11530       else
11531         /* Found high w/o low attribute.  */
11532         return 0;
11533 
11534       /* Found consecutive range of addresses.  */
11535       ret = 1;
11536     }
11537   else
11538     {
11539       attr = dwarf2_attr (die, DW_AT_ranges, cu);
11540       if (attr != NULL)
11541         {
11542           /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
11543              We take advantage of the fact that DW_AT_ranges does not appear
11544              in DW_TAG_compile_unit of DWO files.  */
11545           int need_ranges_base = die->tag != DW_TAG_compile_unit;
11546           unsigned int ranges_offset = (DW_UNSND (attr)
11547                                         + (need_ranges_base
11548                                            ? cu->ranges_base
11549                                            : 0));
11550 
11551           /* Value of the DW_AT_ranges attribute is the offset in the
11552              .debug_ranges section.  */
11553           if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
11554             return 0;
11555           /* Found discontinuous range of addresses.  */
11556           ret = -1;
11557         }
11558     }
11559 
11560   /* read_partial_die has also the strict LOW < HIGH requirement.  */
11561   if (high <= low)
11562     return 0;
11563 
11564   /* When using the GNU linker, .gnu.linkonce. sections are used to
11565      eliminate duplicate copies of functions and vtables and such.
11566      The linker will arbitrarily choose one and discard the others.
11567      The AT_*_pc values for such functions refer to local labels in
11568      these sections.  If the section from that file was discarded, the
11569      labels are not in the output, so the relocs get a value of 0.
11570      If this is a discarded function, mark the pc bounds as invalid,
11571      so that GDB will ignore it.  */
11572   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
11573     return 0;
11574 
11575   *lowpc = low;
11576   if (highpc)
11577     *highpc = high;
11578   return ret;
11579 }
11580 
11581 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
11582    its low and high PC addresses.  Do nothing if these addresses could not
11583    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
11584    and HIGHPC to the high address if greater than HIGHPC.  */
11585 
11586 static void
11587 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
11588                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
11589                                  struct dwarf2_cu *cu)
11590 {
11591   CORE_ADDR low, high;
11592   struct die_info *child = die->child;
11593 
11594   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
11595     {
11596       *lowpc = min (*lowpc, low);
11597       *highpc = max (*highpc, high);
11598     }
11599 
11600   /* If the language does not allow nested subprograms (either inside
11601      subprograms or lexical blocks), we're done.  */
11602   if (cu->language != language_ada)
11603     return;
11604 
11605   /* Check all the children of the given DIE.  If it contains nested
11606      subprograms, then check their pc bounds.  Likewise, we need to
11607      check lexical blocks as well, as they may also contain subprogram
11608      definitions.  */
11609   while (child && child->tag)
11610     {
11611       if (child->tag == DW_TAG_subprogram
11612           || child->tag == DW_TAG_lexical_block)
11613         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
11614       child = sibling_die (child);
11615     }
11616 }
11617 
11618 /* Get the low and high pc's represented by the scope DIE, and store
11619    them in *LOWPC and *HIGHPC.  If the correct values can't be
11620    determined, set *LOWPC to -1 and *HIGHPC to 0.  */
11621 
11622 static void
11623 get_scope_pc_bounds (struct die_info *die,
11624                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
11625                      struct dwarf2_cu *cu)
11626 {
11627   CORE_ADDR best_low = (CORE_ADDR) -1;
11628   CORE_ADDR best_high = (CORE_ADDR) 0;
11629   CORE_ADDR current_low, current_high;
11630 
11631   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
11632     {
11633       best_low = current_low;
11634       best_high = current_high;
11635     }
11636   else
11637     {
11638       struct die_info *child = die->child;
11639 
11640       while (child && child->tag)
11641         {
11642           switch (child->tag) {
11643           case DW_TAG_subprogram:
11644             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
11645             break;
11646           case DW_TAG_namespace:
11647           case DW_TAG_module:
11648             /* FIXME: carlton/2004-01-16: Should we do this for
11649                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
11650                that current GCC's always emit the DIEs corresponding
11651                to definitions of methods of classes as children of a
11652                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
11653                the DIEs giving the declarations, which could be
11654                anywhere).  But I don't see any reason why the
11655                standards says that they have to be there.  */
11656             get_scope_pc_bounds (child, &current_low, &current_high, cu);
11657 
11658             if (current_low != ((CORE_ADDR) -1))
11659               {
11660                 best_low = min (best_low, current_low);
11661                 best_high = max (best_high, current_high);
11662               }
11663             break;
11664           default:
11665             /* Ignore.  */
11666             break;
11667           }
11668 
11669           child = sibling_die (child);
11670         }
11671     }
11672 
11673   *lowpc = best_low;
11674   *highpc = best_high;
11675 }
11676 
11677 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
11678    in DIE.  */
11679 
11680 static void
11681 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
11682                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
11683 {
11684   struct objfile *objfile = cu->objfile;
11685   struct attribute *attr;
11686   struct attribute *attr_high;
11687 
11688   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
11689   if (attr_high)
11690     {
11691       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11692       if (attr)
11693         {
11694           CORE_ADDR low = DW_ADDR (attr);
11695           CORE_ADDR high;
11696           if (attr_high->form == DW_FORM_addr
11697               || attr_high->form == DW_FORM_GNU_addr_index)
11698             high = DW_ADDR (attr_high);
11699           else
11700             high = low + DW_UNSND (attr_high);
11701 
11702           record_block_range (block, baseaddr + low, baseaddr + high - 1);
11703         }
11704     }
11705 
11706   attr = dwarf2_attr (die, DW_AT_ranges, cu);
11707   if (attr)
11708     {
11709       bfd *obfd = objfile->obfd;
11710       /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
11711          We take advantage of the fact that DW_AT_ranges does not appear
11712          in DW_TAG_compile_unit of DWO files.  */
11713       int need_ranges_base = die->tag != DW_TAG_compile_unit;
11714 
11715       /* The value of the DW_AT_ranges attribute is the offset of the
11716          address range list in the .debug_ranges section.  */
11717       unsigned long offset = (DW_UNSND (attr)
11718                               + (need_ranges_base ? cu->ranges_base : 0));
11719       const gdb_byte *buffer;
11720 
11721       /* For some target architectures, but not others, the
11722          read_address function sign-extends the addresses it returns.
11723          To recognize base address selection entries, we need a
11724          mask.  */
11725       unsigned int addr_size = cu->header.addr_size;
11726       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
11727 
11728       /* The base address, to which the next pair is relative.  Note
11729          that this 'base' is a DWARF concept: most entries in a range
11730          list are relative, to reduce the number of relocs against the
11731          debugging information.  This is separate from this function's
11732          'baseaddr' argument, which GDB uses to relocate debugging
11733          information from a shared library based on the address at
11734          which the library was loaded.  */
11735       CORE_ADDR base = cu->base_address;
11736       int base_known = cu->base_known;
11737 
11738       dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
11739       if (offset >= dwarf2_per_objfile->ranges.size)
11740         {
11741           complaint (&symfile_complaints,
11742                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
11743                      offset);
11744           return;
11745         }
11746       buffer = dwarf2_per_objfile->ranges.buffer + offset;
11747 
11748       for (;;)
11749         {
11750           unsigned int bytes_read;
11751           CORE_ADDR start, end;
11752 
11753           start = read_address (obfd, buffer, cu, &bytes_read);
11754           buffer += bytes_read;
11755           end = read_address (obfd, buffer, cu, &bytes_read);
11756           buffer += bytes_read;
11757 
11758           /* Did we find the end of the range list?  */
11759           if (start == 0 && end == 0)
11760             break;
11761 
11762           /* Did we find a base address selection entry?  */
11763           else if ((start & base_select_mask) == base_select_mask)
11764             {
11765               base = end;
11766               base_known = 1;
11767             }
11768 
11769           /* We found an ordinary address range.  */
11770           else
11771             {
11772               if (!base_known)
11773                 {
11774                   complaint (&symfile_complaints,
11775                              _("Invalid .debug_ranges data "
11776                                "(no base address)"));
11777                   return;
11778                 }
11779 
11780               if (start > end)
11781                 {
11782                   /* Inverted range entries are invalid.  */
11783                   complaint (&symfile_complaints,
11784                              _("Invalid .debug_ranges data "
11785                                "(inverted range)"));
11786                   return;
11787                 }
11788 
11789               /* Empty range entries have no effect.  */
11790               if (start == end)
11791                 continue;
11792 
11793               start += base + baseaddr;
11794               end += base + baseaddr;
11795 
11796               /* A not-uncommon case of bad debug info.
11797                  Don't pollute the addrmap with bad data.  */
11798               if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
11799                 {
11800                   complaint (&symfile_complaints,
11801                              _(".debug_ranges entry has start address of zero"
11802                                " [in module %s]"), objfile_name (objfile));
11803                   continue;
11804                 }
11805 
11806               record_block_range (block, start, end - 1);
11807             }
11808         }
11809     }
11810 }
11811 
11812 /* Check whether the producer field indicates either of GCC < 4.6, or the
11813    Intel C/C++ compiler, and cache the result in CU.  */
11814 
11815 static void
11816 check_producer (struct dwarf2_cu *cu)
11817 {
11818   const char *cs;
11819   int major, minor, release;
11820 
11821   if (cu->producer == NULL)
11822     {
11823       /* For unknown compilers expect their behavior is DWARF version
11824          compliant.
11825 
11826          GCC started to support .debug_types sections by -gdwarf-4 since
11827          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
11828          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
11829          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
11830          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
11831     }
11832   else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
11833     {
11834       /* Skip any identifier after "GNU " - such as "C++" or "Java".  */
11835 
11836       cs = &cu->producer[strlen ("GNU ")];
11837       while (*cs && !isdigit (*cs))
11838         cs++;
11839       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
11840         {
11841           /* Not recognized as GCC.  */
11842         }
11843       else
11844         {
11845           cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
11846           cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
11847         }
11848     }
11849   else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
11850     cu->producer_is_icc = 1;
11851   else
11852     {
11853       /* For other non-GCC compilers, expect their behavior is DWARF version
11854          compliant.  */
11855     }
11856 
11857   cu->checked_producer = 1;
11858 }
11859 
11860 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
11861    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
11862    during 4.6.0 experimental.  */
11863 
11864 static int
11865 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
11866 {
11867   if (!cu->checked_producer)
11868     check_producer (cu);
11869 
11870   return cu->producer_is_gxx_lt_4_6;
11871 }
11872 
11873 /* Return the default accessibility type if it is not overriden by
11874    DW_AT_accessibility.  */
11875 
11876 static enum dwarf_access_attribute
11877 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
11878 {
11879   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
11880     {
11881       /* The default DWARF 2 accessibility for members is public, the default
11882          accessibility for inheritance is private.  */
11883 
11884       if (die->tag != DW_TAG_inheritance)
11885         return DW_ACCESS_public;
11886       else
11887         return DW_ACCESS_private;
11888     }
11889   else
11890     {
11891       /* DWARF 3+ defines the default accessibility a different way.  The same
11892          rules apply now for DW_TAG_inheritance as for the members and it only
11893          depends on the container kind.  */
11894 
11895       if (die->parent->tag == DW_TAG_class_type)
11896         return DW_ACCESS_private;
11897       else
11898         return DW_ACCESS_public;
11899     }
11900 }
11901 
11902 /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
11903    offset.  If the attribute was not found return 0, otherwise return
11904    1.  If it was found but could not properly be handled, set *OFFSET
11905    to 0.  */
11906 
11907 static int
11908 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
11909                              LONGEST *offset)
11910 {
11911   struct attribute *attr;
11912 
11913   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
11914   if (attr != NULL)
11915     {
11916       *offset = 0;
11917 
11918       /* Note that we do not check for a section offset first here.
11919          This is because DW_AT_data_member_location is new in DWARF 4,
11920          so if we see it, we can assume that a constant form is really
11921          a constant and not a section offset.  */
11922       if (attr_form_is_constant (attr))
11923         *offset = dwarf2_get_attr_constant_value (attr, 0);
11924       else if (attr_form_is_section_offset (attr))
11925         dwarf2_complex_location_expr_complaint ();
11926       else if (attr_form_is_block (attr))
11927         *offset = decode_locdesc (DW_BLOCK (attr), cu);
11928       else
11929         dwarf2_complex_location_expr_complaint ();
11930 
11931       return 1;
11932     }
11933 
11934   return 0;
11935 }
11936 
11937 /* Add an aggregate field to the field list.  */
11938 
11939 static void
11940 dwarf2_add_field (struct field_info *fip, struct die_info *die,
11941                   struct dwarf2_cu *cu)
11942 {
11943   struct objfile *objfile = cu->objfile;
11944   struct gdbarch *gdbarch = get_objfile_arch (objfile);
11945   struct nextfield *new_field;
11946   struct attribute *attr;
11947   struct field *fp;
11948   const char *fieldname = "";
11949 
11950   /* Allocate a new field list entry and link it in.  */
11951   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
11952   make_cleanup (xfree, new_field);
11953   memset (new_field, 0, sizeof (struct nextfield));
11954 
11955   if (die->tag == DW_TAG_inheritance)
11956     {
11957       new_field->next = fip->baseclasses;
11958       fip->baseclasses = new_field;
11959     }
11960   else
11961     {
11962       new_field->next = fip->fields;
11963       fip->fields = new_field;
11964     }
11965   fip->nfields++;
11966 
11967   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
11968   if (attr)
11969     new_field->accessibility = DW_UNSND (attr);
11970   else
11971     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
11972   if (new_field->accessibility != DW_ACCESS_public)
11973     fip->non_public_fields = 1;
11974 
11975   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
11976   if (attr)
11977     new_field->virtuality = DW_UNSND (attr);
11978   else
11979     new_field->virtuality = DW_VIRTUALITY_none;
11980 
11981   fp = &new_field->field;
11982 
11983   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
11984     {
11985       LONGEST offset;
11986 
11987       /* Data member other than a C++ static data member.  */
11988 
11989       /* Get type of field.  */
11990       fp->type = die_type (die, cu);
11991 
11992       SET_FIELD_BITPOS (*fp, 0);
11993 
11994       /* Get bit size of field (zero if none).  */
11995       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
11996       if (attr)
11997         {
11998           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
11999         }
12000       else
12001         {
12002           FIELD_BITSIZE (*fp) = 0;
12003         }
12004 
12005       /* Get bit offset of field.  */
12006       if (handle_data_member_location (die, cu, &offset))
12007         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12008       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
12009       if (attr)
12010         {
12011           if (gdbarch_bits_big_endian (gdbarch))
12012             {
12013               /* For big endian bits, the DW_AT_bit_offset gives the
12014                  additional bit offset from the MSB of the containing
12015                  anonymous object to the MSB of the field.  We don't
12016                  have to do anything special since we don't need to
12017                  know the size of the anonymous object.  */
12018               SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
12019             }
12020           else
12021             {
12022               /* For little endian bits, compute the bit offset to the
12023                  MSB of the anonymous object, subtract off the number of
12024                  bits from the MSB of the field to the MSB of the
12025                  object, and then subtract off the number of bits of
12026                  the field itself.  The result is the bit offset of
12027                  the LSB of the field.  */
12028               int anonymous_size;
12029               int bit_offset = DW_UNSND (attr);
12030 
12031               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12032               if (attr)
12033                 {
12034                   /* The size of the anonymous object containing
12035                      the bit field is explicit, so use the
12036                      indicated size (in bytes).  */
12037                   anonymous_size = DW_UNSND (attr);
12038                 }
12039               else
12040                 {
12041                   /* The size of the anonymous object containing
12042                      the bit field must be inferred from the type
12043                      attribute of the data member containing the
12044                      bit field.  */
12045                   anonymous_size = TYPE_LENGTH (fp->type);
12046                 }
12047               SET_FIELD_BITPOS (*fp,
12048                                 (FIELD_BITPOS (*fp)
12049                                  + anonymous_size * bits_per_byte
12050                                  - bit_offset - FIELD_BITSIZE (*fp)));
12051             }
12052         }
12053 
12054       /* Get name of field.  */
12055       fieldname = dwarf2_name (die, cu);
12056       if (fieldname == NULL)
12057         fieldname = "";
12058 
12059       /* The name is already allocated along with this objfile, so we don't
12060          need to duplicate it for the type.  */
12061       fp->name = fieldname;
12062 
12063       /* Change accessibility for artificial fields (e.g. virtual table
12064          pointer or virtual base class pointer) to private.  */
12065       if (dwarf2_attr (die, DW_AT_artificial, cu))
12066         {
12067           FIELD_ARTIFICIAL (*fp) = 1;
12068           new_field->accessibility = DW_ACCESS_private;
12069           fip->non_public_fields = 1;
12070         }
12071     }
12072   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
12073     {
12074       /* C++ static member.  */
12075 
12076       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
12077          is a declaration, but all versions of G++ as of this writing
12078          (so through at least 3.2.1) incorrectly generate
12079          DW_TAG_variable tags.  */
12080 
12081       const char *physname;
12082 
12083       /* Get name of field.  */
12084       fieldname = dwarf2_name (die, cu);
12085       if (fieldname == NULL)
12086         return;
12087 
12088       attr = dwarf2_attr (die, DW_AT_const_value, cu);
12089       if (attr
12090           /* Only create a symbol if this is an external value.
12091              new_symbol checks this and puts the value in the global symbol
12092              table, which we want.  If it is not external, new_symbol
12093              will try to put the value in cu->list_in_scope which is wrong.  */
12094           && dwarf2_flag_true_p (die, DW_AT_external, cu))
12095         {
12096           /* A static const member, not much different than an enum as far as
12097              we're concerned, except that we can support more types.  */
12098           new_symbol (die, NULL, cu);
12099         }
12100 
12101       /* Get physical name.  */
12102       physname = dwarf2_physname (fieldname, die, cu);
12103 
12104       /* The name is already allocated along with this objfile, so we don't
12105          need to duplicate it for the type.  */
12106       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
12107       FIELD_TYPE (*fp) = die_type (die, cu);
12108       FIELD_NAME (*fp) = fieldname;
12109     }
12110   else if (die->tag == DW_TAG_inheritance)
12111     {
12112       LONGEST offset;
12113 
12114       /* C++ base class field.  */
12115       if (handle_data_member_location (die, cu, &offset))
12116         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
12117       FIELD_BITSIZE (*fp) = 0;
12118       FIELD_TYPE (*fp) = die_type (die, cu);
12119       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
12120       fip->nbaseclasses++;
12121     }
12122 }
12123 
12124 /* Add a typedef defined in the scope of the FIP's class.  */
12125 
12126 static void
12127 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
12128                     struct dwarf2_cu *cu)
12129 {
12130   struct objfile *objfile = cu->objfile;
12131   struct typedef_field_list *new_field;
12132   struct attribute *attr;
12133   struct typedef_field *fp;
12134   char *fieldname = "";
12135 
12136   /* Allocate a new field list entry and link it in.  */
12137   new_field = xzalloc (sizeof (*new_field));
12138   make_cleanup (xfree, new_field);
12139 
12140   gdb_assert (die->tag == DW_TAG_typedef);
12141 
12142   fp = &new_field->field;
12143 
12144   /* Get name of field.  */
12145   fp->name = dwarf2_name (die, cu);
12146   if (fp->name == NULL)
12147     return;
12148 
12149   fp->type = read_type_die (die, cu);
12150 
12151   new_field->next = fip->typedef_field_list;
12152   fip->typedef_field_list = new_field;
12153   fip->typedef_field_list_count++;
12154 }
12155 
12156 /* Create the vector of fields, and attach it to the type.  */
12157 
12158 static void
12159 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
12160                               struct dwarf2_cu *cu)
12161 {
12162   int nfields = fip->nfields;
12163 
12164   /* Record the field count, allocate space for the array of fields,
12165      and create blank accessibility bitfields if necessary.  */
12166   TYPE_NFIELDS (type) = nfields;
12167   TYPE_FIELDS (type) = (struct field *)
12168     TYPE_ALLOC (type, sizeof (struct field) * nfields);
12169   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
12170 
12171   if (fip->non_public_fields && cu->language != language_ada)
12172     {
12173       ALLOCATE_CPLUS_STRUCT_TYPE (type);
12174 
12175       TYPE_FIELD_PRIVATE_BITS (type) =
12176         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12177       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
12178 
12179       TYPE_FIELD_PROTECTED_BITS (type) =
12180         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12181       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
12182 
12183       TYPE_FIELD_IGNORE_BITS (type) =
12184         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
12185       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
12186     }
12187 
12188   /* If the type has baseclasses, allocate and clear a bit vector for
12189      TYPE_FIELD_VIRTUAL_BITS.  */
12190   if (fip->nbaseclasses && cu->language != language_ada)
12191     {
12192       int num_bytes = B_BYTES (fip->nbaseclasses);
12193       unsigned char *pointer;
12194 
12195       ALLOCATE_CPLUS_STRUCT_TYPE (type);
12196       pointer = TYPE_ALLOC (type, num_bytes);
12197       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
12198       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
12199       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
12200     }
12201 
12202   /* Copy the saved-up fields into the field vector.  Start from the head of
12203      the list, adding to the tail of the field array, so that they end up in
12204      the same order in the array in which they were added to the list.  */
12205   while (nfields-- > 0)
12206     {
12207       struct nextfield *fieldp;
12208 
12209       if (fip->fields)
12210         {
12211           fieldp = fip->fields;
12212           fip->fields = fieldp->next;
12213         }
12214       else
12215         {
12216           fieldp = fip->baseclasses;
12217           fip->baseclasses = fieldp->next;
12218         }
12219 
12220       TYPE_FIELD (type, nfields) = fieldp->field;
12221       switch (fieldp->accessibility)
12222         {
12223         case DW_ACCESS_private:
12224           if (cu->language != language_ada)
12225             SET_TYPE_FIELD_PRIVATE (type, nfields);
12226           break;
12227 
12228         case DW_ACCESS_protected:
12229           if (cu->language != language_ada)
12230             SET_TYPE_FIELD_PROTECTED (type, nfields);
12231           break;
12232 
12233         case DW_ACCESS_public:
12234           break;
12235 
12236         default:
12237           /* Unknown accessibility.  Complain and treat it as public.  */
12238           {
12239             complaint (&symfile_complaints, _("unsupported accessibility %d"),
12240                        fieldp->accessibility);
12241           }
12242           break;
12243         }
12244       if (nfields < fip->nbaseclasses)
12245         {
12246           switch (fieldp->virtuality)
12247             {
12248             case DW_VIRTUALITY_virtual:
12249             case DW_VIRTUALITY_pure_virtual:
12250               if (cu->language == language_ada)
12251                 error (_("unexpected virtuality in component of Ada type"));
12252               SET_TYPE_FIELD_VIRTUAL (type, nfields);
12253               break;
12254             }
12255         }
12256     }
12257 }
12258 
12259 /* Return true if this member function is a constructor, false
12260    otherwise.  */
12261 
12262 static int
12263 dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
12264 {
12265   const char *fieldname;
12266   const char *typename;
12267   int len;
12268 
12269   if (die->parent == NULL)
12270     return 0;
12271 
12272   if (die->parent->tag != DW_TAG_structure_type
12273       && die->parent->tag != DW_TAG_union_type
12274       && die->parent->tag != DW_TAG_class_type)
12275     return 0;
12276 
12277   fieldname = dwarf2_name (die, cu);
12278   typename = dwarf2_name (die->parent, cu);
12279   if (fieldname == NULL || typename == NULL)
12280     return 0;
12281 
12282   len = strlen (fieldname);
12283   return (strncmp (fieldname, typename, len) == 0
12284           && (typename[len] == '\0' || typename[len] == '<'));
12285 }
12286 
12287 /* Add a member function to the proper fieldlist.  */
12288 
12289 static void
12290 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
12291                       struct type *type, struct dwarf2_cu *cu)
12292 {
12293   struct objfile *objfile = cu->objfile;
12294   struct attribute *attr;
12295   struct fnfieldlist *flp;
12296   int i;
12297   struct fn_field *fnp;
12298   const char *fieldname;
12299   struct nextfnfield *new_fnfield;
12300   struct type *this_type;
12301   enum dwarf_access_attribute accessibility;
12302 
12303   if (cu->language == language_ada)
12304     error (_("unexpected member function in Ada type"));
12305 
12306   /* Get name of member function.  */
12307   fieldname = dwarf2_name (die, cu);
12308   if (fieldname == NULL)
12309     return;
12310 
12311   /* Look up member function name in fieldlist.  */
12312   for (i = 0; i < fip->nfnfields; i++)
12313     {
12314       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
12315         break;
12316     }
12317 
12318   /* Create new list element if necessary.  */
12319   if (i < fip->nfnfields)
12320     flp = &fip->fnfieldlists[i];
12321   else
12322     {
12323       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
12324         {
12325           fip->fnfieldlists = (struct fnfieldlist *)
12326             xrealloc (fip->fnfieldlists,
12327                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
12328                       * sizeof (struct fnfieldlist));
12329           if (fip->nfnfields == 0)
12330             make_cleanup (free_current_contents, &fip->fnfieldlists);
12331         }
12332       flp = &fip->fnfieldlists[fip->nfnfields];
12333       flp->name = fieldname;
12334       flp->length = 0;
12335       flp->head = NULL;
12336       i = fip->nfnfields++;
12337     }
12338 
12339   /* Create a new member function field and chain it to the field list
12340      entry.  */
12341   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
12342   make_cleanup (xfree, new_fnfield);
12343   memset (new_fnfield, 0, sizeof (struct nextfnfield));
12344   new_fnfield->next = flp->head;
12345   flp->head = new_fnfield;
12346   flp->length++;
12347 
12348   /* Fill in the member function field info.  */
12349   fnp = &new_fnfield->fnfield;
12350 
12351   /* Delay processing of the physname until later.  */
12352   if (cu->language == language_cplus || cu->language == language_java)
12353     {
12354       add_to_method_list (type, i, flp->length - 1, fieldname,
12355                           die, cu);
12356     }
12357   else
12358     {
12359       const char *physname = dwarf2_physname (fieldname, die, cu);
12360       fnp->physname = physname ? physname : "";
12361     }
12362 
12363   fnp->type = alloc_type (objfile);
12364   this_type = read_type_die (die, cu);
12365   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
12366     {
12367       int nparams = TYPE_NFIELDS (this_type);
12368 
12369       /* TYPE is the domain of this method, and THIS_TYPE is the type
12370            of the method itself (TYPE_CODE_METHOD).  */
12371       smash_to_method_type (fnp->type, type,
12372                             TYPE_TARGET_TYPE (this_type),
12373                             TYPE_FIELDS (this_type),
12374                             TYPE_NFIELDS (this_type),
12375                             TYPE_VARARGS (this_type));
12376 
12377       /* Handle static member functions.
12378          Dwarf2 has no clean way to discern C++ static and non-static
12379          member functions.  G++ helps GDB by marking the first
12380          parameter for non-static member functions (which is the this
12381          pointer) as artificial.  We obtain this information from
12382          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
12383       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
12384         fnp->voffset = VOFFSET_STATIC;
12385     }
12386   else
12387     complaint (&symfile_complaints, _("member function type missing for '%s'"),
12388                dwarf2_full_name (fieldname, die, cu));
12389 
12390   /* Get fcontext from DW_AT_containing_type if present.  */
12391   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12392     fnp->fcontext = die_containing_type (die, cu);
12393 
12394   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
12395      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */
12396 
12397   /* Get accessibility.  */
12398   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
12399   if (attr)
12400     accessibility = DW_UNSND (attr);
12401   else
12402     accessibility = dwarf2_default_access_attribute (die, cu);
12403   switch (accessibility)
12404     {
12405     case DW_ACCESS_private:
12406       fnp->is_private = 1;
12407       break;
12408     case DW_ACCESS_protected:
12409       fnp->is_protected = 1;
12410       break;
12411     }
12412 
12413   /* Check for artificial methods.  */
12414   attr = dwarf2_attr (die, DW_AT_artificial, cu);
12415   if (attr && DW_UNSND (attr) != 0)
12416     fnp->is_artificial = 1;
12417 
12418   fnp->is_constructor = dwarf2_is_constructor (die, cu);
12419 
12420   /* Get index in virtual function table if it is a virtual member
12421      function.  For older versions of GCC, this is an offset in the
12422      appropriate virtual table, as specified by DW_AT_containing_type.
12423      For everyone else, it is an expression to be evaluated relative
12424      to the object address.  */
12425 
12426   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
12427   if (attr)
12428     {
12429       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
12430         {
12431           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
12432             {
12433               /* Old-style GCC.  */
12434               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
12435             }
12436           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
12437                    || (DW_BLOCK (attr)->size > 1
12438                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
12439                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
12440             {
12441               struct dwarf_block blk;
12442               int offset;
12443 
12444               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
12445                         ? 1 : 2);
12446               blk.size = DW_BLOCK (attr)->size - offset;
12447               blk.data = DW_BLOCK (attr)->data + offset;
12448               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
12449               if ((fnp->voffset % cu->header.addr_size) != 0)
12450                 dwarf2_complex_location_expr_complaint ();
12451               else
12452                 fnp->voffset /= cu->header.addr_size;
12453               fnp->voffset += 2;
12454             }
12455           else
12456             dwarf2_complex_location_expr_complaint ();
12457 
12458           if (!fnp->fcontext)
12459             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
12460         }
12461       else if (attr_form_is_section_offset (attr))
12462         {
12463           dwarf2_complex_location_expr_complaint ();
12464         }
12465       else
12466         {
12467           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
12468                                                  fieldname);
12469         }
12470     }
12471   else
12472     {
12473       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
12474       if (attr && DW_UNSND (attr))
12475         {
12476           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
12477           complaint (&symfile_complaints,
12478                      _("Member function \"%s\" (offset %d) is virtual "
12479                        "but the vtable offset is not specified"),
12480                      fieldname, die->offset.sect_off);
12481           ALLOCATE_CPLUS_STRUCT_TYPE (type);
12482           TYPE_CPLUS_DYNAMIC (type) = 1;
12483         }
12484     }
12485 }
12486 
12487 /* Create the vector of member function fields, and attach it to the type.  */
12488 
12489 static void
12490 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
12491                                  struct dwarf2_cu *cu)
12492 {
12493   struct fnfieldlist *flp;
12494   int i;
12495 
12496   if (cu->language == language_ada)
12497     error (_("unexpected member functions in Ada type"));
12498 
12499   ALLOCATE_CPLUS_STRUCT_TYPE (type);
12500   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
12501     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
12502 
12503   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
12504     {
12505       struct nextfnfield *nfp = flp->head;
12506       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
12507       int k;
12508 
12509       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
12510       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
12511       fn_flp->fn_fields = (struct fn_field *)
12512         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
12513       for (k = flp->length; (k--, nfp); nfp = nfp->next)
12514         fn_flp->fn_fields[k] = nfp->fnfield;
12515     }
12516 
12517   TYPE_NFN_FIELDS (type) = fip->nfnfields;
12518 }
12519 
12520 /* Returns non-zero if NAME is the name of a vtable member in CU's
12521    language, zero otherwise.  */
12522 static int
12523 is_vtable_name (const char *name, struct dwarf2_cu *cu)
12524 {
12525   static const char vptr[] = "_vptr";
12526   static const char vtable[] = "vtable";
12527 
12528   /* Look for the C++ and Java forms of the vtable.  */
12529   if ((cu->language == language_java
12530        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
12531        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
12532        && is_cplus_marker (name[sizeof (vptr) - 1])))
12533     return 1;
12534 
12535   return 0;
12536 }
12537 
12538 /* GCC outputs unnamed structures that are really pointers to member
12539    functions, with the ABI-specified layout.  If TYPE describes
12540    such a structure, smash it into a member function type.
12541 
12542    GCC shouldn't do this; it should just output pointer to member DIEs.
12543    This is GCC PR debug/28767.  */
12544 
12545 static void
12546 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
12547 {
12548   struct type *pfn_type, *domain_type, *new_type;
12549 
12550   /* Check for a structure with no name and two children.  */
12551   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
12552     return;
12553 
12554   /* Check for __pfn and __delta members.  */
12555   if (TYPE_FIELD_NAME (type, 0) == NULL
12556       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
12557       || TYPE_FIELD_NAME (type, 1) == NULL
12558       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
12559     return;
12560 
12561   /* Find the type of the method.  */
12562   pfn_type = TYPE_FIELD_TYPE (type, 0);
12563   if (pfn_type == NULL
12564       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
12565       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
12566     return;
12567 
12568   /* Look for the "this" argument.  */
12569   pfn_type = TYPE_TARGET_TYPE (pfn_type);
12570   if (TYPE_NFIELDS (pfn_type) == 0
12571       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
12572       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
12573     return;
12574 
12575   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
12576   new_type = alloc_type (objfile);
12577   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
12578                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
12579                         TYPE_VARARGS (pfn_type));
12580   smash_to_methodptr_type (type, new_type);
12581 }
12582 
12583 /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
12584    (icc).  */
12585 
12586 static int
12587 producer_is_icc (struct dwarf2_cu *cu)
12588 {
12589   if (!cu->checked_producer)
12590     check_producer (cu);
12591 
12592   return cu->producer_is_icc;
12593 }
12594 
12595 /* Called when we find the DIE that starts a structure or union scope
12596    (definition) to create a type for the structure or union.  Fill in
12597    the type's name and general properties; the members will not be
12598    processed until process_structure_scope.
12599 
12600    NOTE: we need to call these functions regardless of whether or not the
12601    DIE has a DW_AT_name attribute, since it might be an anonymous
12602    structure or union.  This gets the type entered into our set of
12603    user defined types.
12604 
12605    However, if the structure is incomplete (an opaque struct/union)
12606    then suppress creating a symbol table entry for it since gdb only
12607    wants to find the one with the complete definition.  Note that if
12608    it is complete, we just call new_symbol, which does it's own
12609    checking about whether the struct/union is anonymous or not (and
12610    suppresses creating a symbol table entry itself).  */
12611 
12612 static struct type *
12613 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
12614 {
12615   struct objfile *objfile = cu->objfile;
12616   struct type *type;
12617   struct attribute *attr;
12618   const char *name;
12619 
12620   /* If the definition of this type lives in .debug_types, read that type.
12621      Don't follow DW_AT_specification though, that will take us back up
12622      the chain and we want to go down.  */
12623   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12624   if (attr)
12625     {
12626       type = get_DW_AT_signature_type (die, attr, cu);
12627 
12628       /* The type's CU may not be the same as CU.
12629          Ensure TYPE is recorded with CU in die_type_hash.  */
12630       return set_die_type (die, type, cu);
12631     }
12632 
12633   type = alloc_type (objfile);
12634   INIT_CPLUS_SPECIFIC (type);
12635 
12636   name = dwarf2_name (die, cu);
12637   if (name != NULL)
12638     {
12639       if (cu->language == language_cplus
12640           || cu->language == language_java)
12641         {
12642           const char *full_name = dwarf2_full_name (name, die, cu);
12643 
12644           /* dwarf2_full_name might have already finished building the DIE's
12645              type.  If so, there is no need to continue.  */
12646           if (get_die_type (die, cu) != NULL)
12647             return get_die_type (die, cu);
12648 
12649           TYPE_TAG_NAME (type) = full_name;
12650           if (die->tag == DW_TAG_structure_type
12651               || die->tag == DW_TAG_class_type)
12652             TYPE_NAME (type) = TYPE_TAG_NAME (type);
12653         }
12654       else
12655         {
12656           /* The name is already allocated along with this objfile, so
12657              we don't need to duplicate it for the type.  */
12658           TYPE_TAG_NAME (type) = name;
12659           if (die->tag == DW_TAG_class_type)
12660             TYPE_NAME (type) = TYPE_TAG_NAME (type);
12661         }
12662     }
12663 
12664   if (die->tag == DW_TAG_structure_type)
12665     {
12666       TYPE_CODE (type) = TYPE_CODE_STRUCT;
12667     }
12668   else if (die->tag == DW_TAG_union_type)
12669     {
12670       TYPE_CODE (type) = TYPE_CODE_UNION;
12671     }
12672   else
12673     {
12674       TYPE_CODE (type) = TYPE_CODE_CLASS;
12675     }
12676 
12677   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
12678     TYPE_DECLARED_CLASS (type) = 1;
12679 
12680   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12681   if (attr)
12682     {
12683       TYPE_LENGTH (type) = DW_UNSND (attr);
12684     }
12685   else
12686     {
12687       TYPE_LENGTH (type) = 0;
12688     }
12689 
12690   if (producer_is_icc (cu))
12691     {
12692       /* ICC does not output the required DW_AT_declaration
12693          on incomplete types, but gives them a size of zero.  */
12694     }
12695   else
12696     TYPE_STUB_SUPPORTED (type) = 1;
12697 
12698   if (die_is_declaration (die, cu))
12699     TYPE_STUB (type) = 1;
12700   else if (attr == NULL && die->child == NULL
12701            && producer_is_realview (cu->producer))
12702     /* RealView does not output the required DW_AT_declaration
12703        on incomplete types.  */
12704     TYPE_STUB (type) = 1;
12705 
12706   /* We need to add the type field to the die immediately so we don't
12707      infinitely recurse when dealing with pointers to the structure
12708      type within the structure itself.  */
12709   set_die_type (die, type, cu);
12710 
12711   /* set_die_type should be already done.  */
12712   set_descriptive_type (type, die, cu);
12713 
12714   return type;
12715 }
12716 
12717 /* Finish creating a structure or union type, including filling in
12718    its members and creating a symbol for it.  */
12719 
12720 static void
12721 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
12722 {
12723   struct objfile *objfile = cu->objfile;
12724   struct die_info *child_die = die->child;
12725   struct type *type;
12726 
12727   type = get_die_type (die, cu);
12728   if (type == NULL)
12729     type = read_structure_type (die, cu);
12730 
12731   if (die->child != NULL && ! die_is_declaration (die, cu))
12732     {
12733       struct field_info fi;
12734       struct die_info *child_die;
12735       VEC (symbolp) *template_args = NULL;
12736       struct cleanup *back_to = make_cleanup (null_cleanup, 0);
12737 
12738       memset (&fi, 0, sizeof (struct field_info));
12739 
12740       child_die = die->child;
12741 
12742       while (child_die && child_die->tag)
12743         {
12744           if (child_die->tag == DW_TAG_member
12745               || child_die->tag == DW_TAG_variable)
12746             {
12747               /* NOTE: carlton/2002-11-05: A C++ static data member
12748                  should be a DW_TAG_member that is a declaration, but
12749                  all versions of G++ as of this writing (so through at
12750                  least 3.2.1) incorrectly generate DW_TAG_variable
12751                  tags for them instead.  */
12752               dwarf2_add_field (&fi, child_die, cu);
12753             }
12754           else if (child_die->tag == DW_TAG_subprogram)
12755             {
12756               /* C++ member function.  */
12757               dwarf2_add_member_fn (&fi, child_die, type, cu);
12758             }
12759           else if (child_die->tag == DW_TAG_inheritance)
12760             {
12761               /* C++ base class field.  */
12762               dwarf2_add_field (&fi, child_die, cu);
12763             }
12764           else if (child_die->tag == DW_TAG_typedef)
12765             dwarf2_add_typedef (&fi, child_die, cu);
12766           else if (child_die->tag == DW_TAG_template_type_param
12767                    || child_die->tag == DW_TAG_template_value_param)
12768             {
12769               struct symbol *arg = new_symbol (child_die, NULL, cu);
12770 
12771               if (arg != NULL)
12772                 VEC_safe_push (symbolp, template_args, arg);
12773             }
12774 
12775           child_die = sibling_die (child_die);
12776         }
12777 
12778       /* Attach template arguments to type.  */
12779       if (! VEC_empty (symbolp, template_args))
12780         {
12781           ALLOCATE_CPLUS_STRUCT_TYPE (type);
12782           TYPE_N_TEMPLATE_ARGUMENTS (type)
12783             = VEC_length (symbolp, template_args);
12784           TYPE_TEMPLATE_ARGUMENTS (type)
12785             = obstack_alloc (&objfile->objfile_obstack,
12786                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
12787                               * sizeof (struct symbol *)));
12788           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
12789                   VEC_address (symbolp, template_args),
12790                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
12791                    * sizeof (struct symbol *)));
12792           VEC_free (symbolp, template_args);
12793         }
12794 
12795       /* Attach fields and member functions to the type.  */
12796       if (fi.nfields)
12797         dwarf2_attach_fields_to_type (&fi, type, cu);
12798       if (fi.nfnfields)
12799         {
12800           dwarf2_attach_fn_fields_to_type (&fi, type, cu);
12801 
12802           /* Get the type which refers to the base class (possibly this
12803              class itself) which contains the vtable pointer for the current
12804              class from the DW_AT_containing_type attribute.  This use of
12805              DW_AT_containing_type is a GNU extension.  */
12806 
12807           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
12808             {
12809               struct type *t = die_containing_type (die, cu);
12810 
12811               TYPE_VPTR_BASETYPE (type) = t;
12812               if (type == t)
12813                 {
12814                   int i;
12815 
12816                   /* Our own class provides vtbl ptr.  */
12817                   for (i = TYPE_NFIELDS (t) - 1;
12818                        i >= TYPE_N_BASECLASSES (t);
12819                        --i)
12820                     {
12821                       const char *fieldname = TYPE_FIELD_NAME (t, i);
12822 
12823                       if (is_vtable_name (fieldname, cu))
12824                         {
12825                           TYPE_VPTR_FIELDNO (type) = i;
12826                           break;
12827                         }
12828                     }
12829 
12830                   /* Complain if virtual function table field not found.  */
12831                   if (i < TYPE_N_BASECLASSES (t))
12832                     complaint (&symfile_complaints,
12833                                _("virtual function table pointer "
12834                                  "not found when defining class '%s'"),
12835                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
12836                                "");
12837                 }
12838               else
12839                 {
12840                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
12841                 }
12842             }
12843           else if (cu->producer
12844                    && strncmp (cu->producer,
12845                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
12846             {
12847               /* The IBM XLC compiler does not provide direct indication
12848                  of the containing type, but the vtable pointer is
12849                  always named __vfp.  */
12850 
12851               int i;
12852 
12853               for (i = TYPE_NFIELDS (type) - 1;
12854                    i >= TYPE_N_BASECLASSES (type);
12855                    --i)
12856                 {
12857                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
12858                     {
12859                       TYPE_VPTR_FIELDNO (type) = i;
12860                       TYPE_VPTR_BASETYPE (type) = type;
12861                       break;
12862                     }
12863                 }
12864             }
12865         }
12866 
12867       /* Copy fi.typedef_field_list linked list elements content into the
12868          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
12869       if (fi.typedef_field_list)
12870         {
12871           int i = fi.typedef_field_list_count;
12872 
12873           ALLOCATE_CPLUS_STRUCT_TYPE (type);
12874           TYPE_TYPEDEF_FIELD_ARRAY (type)
12875             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
12876           TYPE_TYPEDEF_FIELD_COUNT (type) = i;
12877 
12878           /* Reverse the list order to keep the debug info elements order.  */
12879           while (--i >= 0)
12880             {
12881               struct typedef_field *dest, *src;
12882 
12883               dest = &TYPE_TYPEDEF_FIELD (type, i);
12884               src = &fi.typedef_field_list->field;
12885               fi.typedef_field_list = fi.typedef_field_list->next;
12886               *dest = *src;
12887             }
12888         }
12889 
12890       do_cleanups (back_to);
12891 
12892       if (HAVE_CPLUS_STRUCT (type))
12893         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
12894     }
12895 
12896   quirk_gcc_member_function_pointer (type, objfile);
12897 
12898   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
12899      snapshots) has been known to create a die giving a declaration
12900      for a class that has, as a child, a die giving a definition for a
12901      nested class.  So we have to process our children even if the
12902      current die is a declaration.  Normally, of course, a declaration
12903      won't have any children at all.  */
12904 
12905   while (child_die != NULL && child_die->tag)
12906     {
12907       if (child_die->tag == DW_TAG_member
12908           || child_die->tag == DW_TAG_variable
12909           || child_die->tag == DW_TAG_inheritance
12910           || child_die->tag == DW_TAG_template_value_param
12911           || child_die->tag == DW_TAG_template_type_param)
12912         {
12913           /* Do nothing.  */
12914         }
12915       else
12916         process_die (child_die, cu);
12917 
12918       child_die = sibling_die (child_die);
12919     }
12920 
12921   /* Do not consider external references.  According to the DWARF standard,
12922      these DIEs are identified by the fact that they have no byte_size
12923      attribute, and a declaration attribute.  */
12924   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
12925       || !die_is_declaration (die, cu))
12926     new_symbol (die, type, cu);
12927 }
12928 
12929 /* Given a DW_AT_enumeration_type die, set its type.  We do not
12930    complete the type's fields yet, or create any symbols.  */
12931 
12932 static struct type *
12933 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
12934 {
12935   struct objfile *objfile = cu->objfile;
12936   struct type *type;
12937   struct attribute *attr;
12938   const char *name;
12939 
12940   /* If the definition of this type lives in .debug_types, read that type.
12941      Don't follow DW_AT_specification though, that will take us back up
12942      the chain and we want to go down.  */
12943   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
12944   if (attr)
12945     {
12946       type = get_DW_AT_signature_type (die, attr, cu);
12947 
12948       /* The type's CU may not be the same as CU.
12949          Ensure TYPE is recorded with CU in die_type_hash.  */
12950       return set_die_type (die, type, cu);
12951     }
12952 
12953   type = alloc_type (objfile);
12954 
12955   TYPE_CODE (type) = TYPE_CODE_ENUM;
12956   name = dwarf2_full_name (NULL, die, cu);
12957   if (name != NULL)
12958     TYPE_TAG_NAME (type) = name;
12959 
12960   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
12961   if (attr)
12962     {
12963       TYPE_LENGTH (type) = DW_UNSND (attr);
12964     }
12965   else
12966     {
12967       TYPE_LENGTH (type) = 0;
12968     }
12969 
12970   /* The enumeration DIE can be incomplete.  In Ada, any type can be
12971      declared as private in the package spec, and then defined only
12972      inside the package body.  Such types are known as Taft Amendment
12973      Types.  When another package uses such a type, an incomplete DIE
12974      may be generated by the compiler.  */
12975   if (die_is_declaration (die, cu))
12976     TYPE_STUB (type) = 1;
12977 
12978   return set_die_type (die, type, cu);
12979 }
12980 
12981 /* Given a pointer to a die which begins an enumeration, process all
12982    the dies that define the members of the enumeration, and create the
12983    symbol for the enumeration type.
12984 
12985    NOTE: We reverse the order of the element list.  */
12986 
12987 static void
12988 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
12989 {
12990   struct type *this_type;
12991 
12992   this_type = get_die_type (die, cu);
12993   if (this_type == NULL)
12994     this_type = read_enumeration_type (die, cu);
12995 
12996   if (die->child != NULL)
12997     {
12998       struct die_info *child_die;
12999       struct symbol *sym;
13000       struct field *fields = NULL;
13001       int num_fields = 0;
13002       int unsigned_enum = 1;
13003       const char *name;
13004       int flag_enum = 1;
13005       ULONGEST mask = 0;
13006 
13007       child_die = die->child;
13008       while (child_die && child_die->tag)
13009         {
13010           if (child_die->tag != DW_TAG_enumerator)
13011             {
13012               process_die (child_die, cu);
13013             }
13014           else
13015             {
13016               name = dwarf2_name (child_die, cu);
13017               if (name)
13018                 {
13019                   sym = new_symbol (child_die, this_type, cu);
13020                   if (SYMBOL_VALUE (sym) < 0)
13021                     {
13022                       unsigned_enum = 0;
13023                       flag_enum = 0;
13024                     }
13025                   else if ((mask & SYMBOL_VALUE (sym)) != 0)
13026                     flag_enum = 0;
13027                   else
13028                     mask |= SYMBOL_VALUE (sym);
13029 
13030                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
13031                     {
13032                       fields = (struct field *)
13033                         xrealloc (fields,
13034                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
13035                                   * sizeof (struct field));
13036                     }
13037 
13038                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
13039                   FIELD_TYPE (fields[num_fields]) = NULL;
13040                   SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
13041                   FIELD_BITSIZE (fields[num_fields]) = 0;
13042 
13043                   num_fields++;
13044                 }
13045             }
13046 
13047           child_die = sibling_die (child_die);
13048         }
13049 
13050       if (num_fields)
13051         {
13052           TYPE_NFIELDS (this_type) = num_fields;
13053           TYPE_FIELDS (this_type) = (struct field *)
13054             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
13055           memcpy (TYPE_FIELDS (this_type), fields,
13056                   sizeof (struct field) * num_fields);
13057           xfree (fields);
13058         }
13059       if (unsigned_enum)
13060         TYPE_UNSIGNED (this_type) = 1;
13061       if (flag_enum)
13062         TYPE_FLAG_ENUM (this_type) = 1;
13063     }
13064 
13065   /* If we are reading an enum from a .debug_types unit, and the enum
13066      is a declaration, and the enum is not the signatured type in the
13067      unit, then we do not want to add a symbol for it.  Adding a
13068      symbol would in some cases obscure the true definition of the
13069      enum, giving users an incomplete type when the definition is
13070      actually available.  Note that we do not want to do this for all
13071      enums which are just declarations, because C++0x allows forward
13072      enum declarations.  */
13073   if (cu->per_cu->is_debug_types
13074       && die_is_declaration (die, cu))
13075     {
13076       struct signatured_type *sig_type;
13077 
13078       sig_type = (struct signatured_type *) cu->per_cu;
13079       gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
13080       if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
13081         return;
13082     }
13083 
13084   new_symbol (die, this_type, cu);
13085 }
13086 
13087 /* Extract all information from a DW_TAG_array_type DIE and put it in
13088    the DIE's type field.  For now, this only handles one dimensional
13089    arrays.  */
13090 
13091 static struct type *
13092 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
13093 {
13094   struct objfile *objfile = cu->objfile;
13095   struct die_info *child_die;
13096   struct type *type;
13097   struct type *element_type, *range_type, *index_type;
13098   struct type **range_types = NULL;
13099   struct attribute *attr;
13100   int ndim = 0;
13101   struct cleanup *back_to;
13102   const char *name;
13103 
13104   element_type = die_type (die, cu);
13105 
13106   /* The die_type call above may have already set the type for this DIE.  */
13107   type = get_die_type (die, cu);
13108   if (type)
13109     return type;
13110 
13111   /* Irix 6.2 native cc creates array types without children for
13112      arrays with unspecified length.  */
13113   if (die->child == NULL)
13114     {
13115       index_type = objfile_type (objfile)->builtin_int;
13116       range_type = create_range_type (NULL, index_type, 0, -1);
13117       type = create_array_type (NULL, element_type, range_type);
13118       return set_die_type (die, type, cu);
13119     }
13120 
13121   back_to = make_cleanup (null_cleanup, NULL);
13122   child_die = die->child;
13123   while (child_die && child_die->tag)
13124     {
13125       if (child_die->tag == DW_TAG_subrange_type)
13126         {
13127           struct type *child_type = read_type_die (child_die, cu);
13128 
13129           if (child_type != NULL)
13130             {
13131               /* The range type was succesfully read.  Save it for the
13132                  array type creation.  */
13133               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
13134                 {
13135                   range_types = (struct type **)
13136                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
13137                               * sizeof (struct type *));
13138                   if (ndim == 0)
13139                     make_cleanup (free_current_contents, &range_types);
13140                 }
13141               range_types[ndim++] = child_type;
13142             }
13143         }
13144       child_die = sibling_die (child_die);
13145     }
13146 
13147   /* Dwarf2 dimensions are output from left to right, create the
13148      necessary array types in backwards order.  */
13149 
13150   type = element_type;
13151 
13152   if (read_array_order (die, cu) == DW_ORD_col_major)
13153     {
13154       int i = 0;
13155 
13156       while (i < ndim)
13157         type = create_array_type (NULL, type, range_types[i++]);
13158     }
13159   else
13160     {
13161       while (ndim-- > 0)
13162         type = create_array_type (NULL, type, range_types[ndim]);
13163     }
13164 
13165   /* Understand Dwarf2 support for vector types (like they occur on
13166      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
13167      array type.  This is not part of the Dwarf2/3 standard yet, but a
13168      custom vendor extension.  The main difference between a regular
13169      array and the vector variant is that vectors are passed by value
13170      to functions.  */
13171   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
13172   if (attr)
13173     make_vector_type (type);
13174 
13175   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
13176      implementation may choose to implement triple vectors using this
13177      attribute.  */
13178   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13179   if (attr)
13180     {
13181       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
13182         TYPE_LENGTH (type) = DW_UNSND (attr);
13183       else
13184         complaint (&symfile_complaints,
13185                    _("DW_AT_byte_size for array type smaller "
13186                      "than the total size of elements"));
13187     }
13188 
13189   name = dwarf2_name (die, cu);
13190   if (name)
13191     TYPE_NAME (type) = name;
13192 
13193   /* Install the type in the die.  */
13194   set_die_type (die, type, cu);
13195 
13196   /* set_die_type should be already done.  */
13197   set_descriptive_type (type, die, cu);
13198 
13199   do_cleanups (back_to);
13200 
13201   return type;
13202 }
13203 
13204 static enum dwarf_array_dim_ordering
13205 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
13206 {
13207   struct attribute *attr;
13208 
13209   attr = dwarf2_attr (die, DW_AT_ordering, cu);
13210 
13211   if (attr) return DW_SND (attr);
13212 
13213   /* GNU F77 is a special case, as at 08/2004 array type info is the
13214      opposite order to the dwarf2 specification, but data is still
13215      laid out as per normal fortran.
13216 
13217      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
13218      version checking.  */
13219 
13220   if (cu->language == language_fortran
13221       && cu->producer && strstr (cu->producer, "GNU F77"))
13222     {
13223       return DW_ORD_row_major;
13224     }
13225 
13226   switch (cu->language_defn->la_array_ordering)
13227     {
13228     case array_column_major:
13229       return DW_ORD_col_major;
13230     case array_row_major:
13231     default:
13232       return DW_ORD_row_major;
13233     };
13234 }
13235 
13236 /* Extract all information from a DW_TAG_set_type DIE and put it in
13237    the DIE's type field.  */
13238 
13239 static struct type *
13240 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
13241 {
13242   struct type *domain_type, *set_type;
13243   struct attribute *attr;
13244 
13245   domain_type = die_type (die, cu);
13246 
13247   /* The die_type call above may have already set the type for this DIE.  */
13248   set_type = get_die_type (die, cu);
13249   if (set_type)
13250     return set_type;
13251 
13252   set_type = create_set_type (NULL, domain_type);
13253 
13254   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13255   if (attr)
13256     TYPE_LENGTH (set_type) = DW_UNSND (attr);
13257 
13258   return set_die_type (die, set_type, cu);
13259 }
13260 
13261 /* A helper for read_common_block that creates a locexpr baton.
13262    SYM is the symbol which we are marking as computed.
13263    COMMON_DIE is the DIE for the common block.
13264    COMMON_LOC is the location expression attribute for the common
13265    block itself.
13266    MEMBER_LOC is the location expression attribute for the particular
13267    member of the common block that we are processing.
13268    CU is the CU from which the above come.  */
13269 
13270 static void
13271 mark_common_block_symbol_computed (struct symbol *sym,
13272                                    struct die_info *common_die,
13273                                    struct attribute *common_loc,
13274                                    struct attribute *member_loc,
13275                                    struct dwarf2_cu *cu)
13276 {
13277   struct objfile *objfile = dwarf2_per_objfile->objfile;
13278   struct dwarf2_locexpr_baton *baton;
13279   gdb_byte *ptr;
13280   unsigned int cu_off;
13281   enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
13282   LONGEST offset = 0;
13283 
13284   gdb_assert (common_loc && member_loc);
13285   gdb_assert (attr_form_is_block (common_loc));
13286   gdb_assert (attr_form_is_block (member_loc)
13287               || attr_form_is_constant (member_loc));
13288 
13289   baton = obstack_alloc (&objfile->objfile_obstack,
13290                          sizeof (struct dwarf2_locexpr_baton));
13291   baton->per_cu = cu->per_cu;
13292   gdb_assert (baton->per_cu);
13293 
13294   baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
13295 
13296   if (attr_form_is_constant (member_loc))
13297     {
13298       offset = dwarf2_get_attr_constant_value (member_loc, 0);
13299       baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
13300     }
13301   else
13302     baton->size += DW_BLOCK (member_loc)->size;
13303 
13304   ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
13305   baton->data = ptr;
13306 
13307   *ptr++ = DW_OP_call4;
13308   cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
13309   store_unsigned_integer (ptr, 4, byte_order, cu_off);
13310   ptr += 4;
13311 
13312   if (attr_form_is_constant (member_loc))
13313     {
13314       *ptr++ = DW_OP_addr;
13315       store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
13316       ptr += cu->header.addr_size;
13317     }
13318   else
13319     {
13320       /* We have to copy the data here, because DW_OP_call4 will only
13321          use a DW_AT_location attribute.  */
13322       memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
13323       ptr += DW_BLOCK (member_loc)->size;
13324     }
13325 
13326   *ptr++ = DW_OP_plus;
13327   gdb_assert (ptr - baton->data == baton->size);
13328 
13329   SYMBOL_LOCATION_BATON (sym) = baton;
13330   SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
13331 }
13332 
13333 /* Create appropriate locally-scoped variables for all the
13334    DW_TAG_common_block entries.  Also create a struct common_block
13335    listing all such variables for `info common'.  COMMON_BLOCK_DOMAIN
13336    is used to sepate the common blocks name namespace from regular
13337    variable names.  */
13338 
13339 static void
13340 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
13341 {
13342   struct attribute *attr;
13343 
13344   attr = dwarf2_attr (die, DW_AT_location, cu);
13345   if (attr)
13346     {
13347       /* Support the .debug_loc offsets.  */
13348       if (attr_form_is_block (attr))
13349         {
13350           /* Ok.  */
13351         }
13352       else if (attr_form_is_section_offset (attr))
13353         {
13354           dwarf2_complex_location_expr_complaint ();
13355           attr = NULL;
13356         }
13357       else
13358         {
13359           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
13360                                                  "common block member");
13361           attr = NULL;
13362         }
13363     }
13364 
13365   if (die->child != NULL)
13366     {
13367       struct objfile *objfile = cu->objfile;
13368       struct die_info *child_die;
13369       size_t n_entries = 0, size;
13370       struct common_block *common_block;
13371       struct symbol *sym;
13372 
13373       for (child_die = die->child;
13374            child_die && child_die->tag;
13375            child_die = sibling_die (child_die))
13376         ++n_entries;
13377 
13378       size = (sizeof (struct common_block)
13379               + (n_entries - 1) * sizeof (struct symbol *));
13380       common_block = obstack_alloc (&objfile->objfile_obstack, size);
13381       memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
13382       common_block->n_entries = 0;
13383 
13384       for (child_die = die->child;
13385            child_die && child_die->tag;
13386            child_die = sibling_die (child_die))
13387         {
13388           /* Create the symbol in the DW_TAG_common_block block in the current
13389              symbol scope.  */
13390           sym = new_symbol (child_die, NULL, cu);
13391           if (sym != NULL)
13392             {
13393               struct attribute *member_loc;
13394 
13395               common_block->contents[common_block->n_entries++] = sym;
13396 
13397               member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
13398                                         cu);
13399               if (member_loc)
13400                 {
13401                   /* GDB has handled this for a long time, but it is
13402                      not specified by DWARF.  It seems to have been
13403                      emitted by gfortran at least as recently as:
13404                      http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057.  */
13405                   complaint (&symfile_complaints,
13406                              _("Variable in common block has "
13407                                "DW_AT_data_member_location "
13408                                "- DIE at 0x%x [in module %s]"),
13409                              child_die->offset.sect_off,
13410                              objfile_name (cu->objfile));
13411 
13412                   if (attr_form_is_section_offset (member_loc))
13413                     dwarf2_complex_location_expr_complaint ();
13414                   else if (attr_form_is_constant (member_loc)
13415                            || attr_form_is_block (member_loc))
13416                     {
13417                       if (attr)
13418                         mark_common_block_symbol_computed (sym, die, attr,
13419                                                            member_loc, cu);
13420                     }
13421                   else
13422                     dwarf2_complex_location_expr_complaint ();
13423                 }
13424             }
13425         }
13426 
13427       sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
13428       SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
13429     }
13430 }
13431 
13432 /* Create a type for a C++ namespace.  */
13433 
13434 static struct type *
13435 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
13436 {
13437   struct objfile *objfile = cu->objfile;
13438   const char *previous_prefix, *name;
13439   int is_anonymous;
13440   struct type *type;
13441 
13442   /* For extensions, reuse the type of the original namespace.  */
13443   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
13444     {
13445       struct die_info *ext_die;
13446       struct dwarf2_cu *ext_cu = cu;
13447 
13448       ext_die = dwarf2_extension (die, &ext_cu);
13449       type = read_type_die (ext_die, ext_cu);
13450 
13451       /* EXT_CU may not be the same as CU.
13452          Ensure TYPE is recorded with CU in die_type_hash.  */
13453       return set_die_type (die, type, cu);
13454     }
13455 
13456   name = namespace_name (die, &is_anonymous, cu);
13457 
13458   /* Now build the name of the current namespace.  */
13459 
13460   previous_prefix = determine_prefix (die, cu);
13461   if (previous_prefix[0] != '\0')
13462     name = typename_concat (&objfile->objfile_obstack,
13463                             previous_prefix, name, 0, cu);
13464 
13465   /* Create the type.  */
13466   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
13467                     objfile);
13468   TYPE_NAME (type) = name;
13469   TYPE_TAG_NAME (type) = TYPE_NAME (type);
13470 
13471   return set_die_type (die, type, cu);
13472 }
13473 
13474 /* Read a C++ namespace.  */
13475 
13476 static void
13477 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
13478 {
13479   struct objfile *objfile = cu->objfile;
13480   int is_anonymous;
13481 
13482   /* Add a symbol associated to this if we haven't seen the namespace
13483      before.  Also, add a using directive if it's an anonymous
13484      namespace.  */
13485 
13486   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
13487     {
13488       struct type *type;
13489 
13490       type = read_type_die (die, cu);
13491       new_symbol (die, type, cu);
13492 
13493       namespace_name (die, &is_anonymous, cu);
13494       if (is_anonymous)
13495         {
13496           const char *previous_prefix = determine_prefix (die, cu);
13497 
13498           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
13499                                   NULL, NULL, 0, &objfile->objfile_obstack);
13500         }
13501     }
13502 
13503   if (die->child != NULL)
13504     {
13505       struct die_info *child_die = die->child;
13506 
13507       while (child_die && child_die->tag)
13508         {
13509           process_die (child_die, cu);
13510           child_die = sibling_die (child_die);
13511         }
13512     }
13513 }
13514 
13515 /* Read a Fortran module as type.  This DIE can be only a declaration used for
13516    imported module.  Still we need that type as local Fortran "use ... only"
13517    declaration imports depend on the created type in determine_prefix.  */
13518 
13519 static struct type *
13520 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
13521 {
13522   struct objfile *objfile = cu->objfile;
13523   const char *module_name;
13524   struct type *type;
13525 
13526   module_name = dwarf2_name (die, cu);
13527   if (!module_name)
13528     complaint (&symfile_complaints,
13529                _("DW_TAG_module has no name, offset 0x%x"),
13530                die->offset.sect_off);
13531   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
13532 
13533   /* determine_prefix uses TYPE_TAG_NAME.  */
13534   TYPE_TAG_NAME (type) = TYPE_NAME (type);
13535 
13536   return set_die_type (die, type, cu);
13537 }
13538 
13539 /* Read a Fortran module.  */
13540 
13541 static void
13542 read_module (struct die_info *die, struct dwarf2_cu *cu)
13543 {
13544   struct die_info *child_die = die->child;
13545 
13546   while (child_die && child_die->tag)
13547     {
13548       process_die (child_die, cu);
13549       child_die = sibling_die (child_die);
13550     }
13551 }
13552 
13553 /* Return the name of the namespace represented by DIE.  Set
13554    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
13555    namespace.  */
13556 
13557 static const char *
13558 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
13559 {
13560   struct die_info *current_die;
13561   const char *name = NULL;
13562 
13563   /* Loop through the extensions until we find a name.  */
13564 
13565   for (current_die = die;
13566        current_die != NULL;
13567        current_die = dwarf2_extension (die, &cu))
13568     {
13569       name = dwarf2_name (current_die, cu);
13570       if (name != NULL)
13571         break;
13572     }
13573 
13574   /* Is it an anonymous namespace?  */
13575 
13576   *is_anonymous = (name == NULL);
13577   if (*is_anonymous)
13578     name = CP_ANONYMOUS_NAMESPACE_STR;
13579 
13580   return name;
13581 }
13582 
13583 /* Extract all information from a DW_TAG_pointer_type DIE and add to
13584    the user defined type vector.  */
13585 
13586 static struct type *
13587 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
13588 {
13589   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
13590   struct comp_unit_head *cu_header = &cu->header;
13591   struct type *type;
13592   struct attribute *attr_byte_size;
13593   struct attribute *attr_address_class;
13594   int byte_size, addr_class;
13595   struct type *target_type;
13596 
13597   target_type = die_type (die, cu);
13598 
13599   /* The die_type call above may have already set the type for this DIE.  */
13600   type = get_die_type (die, cu);
13601   if (type)
13602     return type;
13603 
13604   type = lookup_pointer_type (target_type);
13605 
13606   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
13607   if (attr_byte_size)
13608     byte_size = DW_UNSND (attr_byte_size);
13609   else
13610     byte_size = cu_header->addr_size;
13611 
13612   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
13613   if (attr_address_class)
13614     addr_class = DW_UNSND (attr_address_class);
13615   else
13616     addr_class = DW_ADDR_none;
13617 
13618   /* If the pointer size or address class is different than the
13619      default, create a type variant marked as such and set the
13620      length accordingly.  */
13621   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
13622     {
13623       if (gdbarch_address_class_type_flags_p (gdbarch))
13624         {
13625           int type_flags;
13626 
13627           type_flags = gdbarch_address_class_type_flags
13628                          (gdbarch, byte_size, addr_class);
13629           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
13630                       == 0);
13631           type = make_type_with_address_space (type, type_flags);
13632         }
13633       else if (TYPE_LENGTH (type) != byte_size)
13634         {
13635           complaint (&symfile_complaints,
13636                      _("invalid pointer size %d"), byte_size);
13637         }
13638       else
13639         {
13640           /* Should we also complain about unhandled address classes?  */
13641         }
13642     }
13643 
13644   TYPE_LENGTH (type) = byte_size;
13645   return set_die_type (die, type, cu);
13646 }
13647 
13648 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
13649    the user defined type vector.  */
13650 
13651 static struct type *
13652 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
13653 {
13654   struct type *type;
13655   struct type *to_type;
13656   struct type *domain;
13657 
13658   to_type = die_type (die, cu);
13659   domain = die_containing_type (die, cu);
13660 
13661   /* The calls above may have already set the type for this DIE.  */
13662   type = get_die_type (die, cu);
13663   if (type)
13664     return type;
13665 
13666   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
13667     type = lookup_methodptr_type (to_type);
13668   else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
13669     {
13670       struct type *new_type = alloc_type (cu->objfile);
13671 
13672       smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
13673                             TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
13674                             TYPE_VARARGS (to_type));
13675       type = lookup_methodptr_type (new_type);
13676     }
13677   else
13678     type = lookup_memberptr_type (to_type, domain);
13679 
13680   return set_die_type (die, type, cu);
13681 }
13682 
13683 /* Extract all information from a DW_TAG_reference_type DIE and add to
13684    the user defined type vector.  */
13685 
13686 static struct type *
13687 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
13688 {
13689   struct comp_unit_head *cu_header = &cu->header;
13690   struct type *type, *target_type;
13691   struct attribute *attr;
13692 
13693   target_type = die_type (die, cu);
13694 
13695   /* The die_type call above may have already set the type for this DIE.  */
13696   type = get_die_type (die, cu);
13697   if (type)
13698     return type;
13699 
13700   type = lookup_reference_type (target_type);
13701   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13702   if (attr)
13703     {
13704       TYPE_LENGTH (type) = DW_UNSND (attr);
13705     }
13706   else
13707     {
13708       TYPE_LENGTH (type) = cu_header->addr_size;
13709     }
13710   return set_die_type (die, type, cu);
13711 }
13712 
13713 static struct type *
13714 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
13715 {
13716   struct type *base_type, *cv_type;
13717 
13718   base_type = die_type (die, cu);
13719 
13720   /* The die_type call above may have already set the type for this DIE.  */
13721   cv_type = get_die_type (die, cu);
13722   if (cv_type)
13723     return cv_type;
13724 
13725   /* In case the const qualifier is applied to an array type, the element type
13726      is so qualified, not the array type (section 6.7.3 of C99).  */
13727   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
13728     {
13729       struct type *el_type, *inner_array;
13730 
13731       base_type = copy_type (base_type);
13732       inner_array = base_type;
13733 
13734       while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
13735         {
13736           TYPE_TARGET_TYPE (inner_array) =
13737             copy_type (TYPE_TARGET_TYPE (inner_array));
13738           inner_array = TYPE_TARGET_TYPE (inner_array);
13739         }
13740 
13741       el_type = TYPE_TARGET_TYPE (inner_array);
13742       TYPE_TARGET_TYPE (inner_array) =
13743         make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
13744 
13745       return set_die_type (die, base_type, cu);
13746     }
13747 
13748   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
13749   return set_die_type (die, cv_type, cu);
13750 }
13751 
13752 static struct type *
13753 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
13754 {
13755   struct type *base_type, *cv_type;
13756 
13757   base_type = die_type (die, cu);
13758 
13759   /* The die_type call above may have already set the type for this DIE.  */
13760   cv_type = get_die_type (die, cu);
13761   if (cv_type)
13762     return cv_type;
13763 
13764   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
13765   return set_die_type (die, cv_type, cu);
13766 }
13767 
13768 /* Handle DW_TAG_restrict_type.  */
13769 
13770 static struct type *
13771 read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
13772 {
13773   struct type *base_type, *cv_type;
13774 
13775   base_type = die_type (die, cu);
13776 
13777   /* The die_type call above may have already set the type for this DIE.  */
13778   cv_type = get_die_type (die, cu);
13779   if (cv_type)
13780     return cv_type;
13781 
13782   cv_type = make_restrict_type (base_type);
13783   return set_die_type (die, cv_type, cu);
13784 }
13785 
13786 /* Extract all information from a DW_TAG_string_type DIE and add to
13787    the user defined type vector.  It isn't really a user defined type,
13788    but it behaves like one, with other DIE's using an AT_user_def_type
13789    attribute to reference it.  */
13790 
13791 static struct type *
13792 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
13793 {
13794   struct objfile *objfile = cu->objfile;
13795   struct gdbarch *gdbarch = get_objfile_arch (objfile);
13796   struct type *type, *range_type, *index_type, *char_type;
13797   struct attribute *attr;
13798   unsigned int length;
13799 
13800   attr = dwarf2_attr (die, DW_AT_string_length, cu);
13801   if (attr)
13802     {
13803       length = DW_UNSND (attr);
13804     }
13805   else
13806     {
13807       /* Check for the DW_AT_byte_size attribute.  */
13808       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
13809       if (attr)
13810         {
13811           length = DW_UNSND (attr);
13812         }
13813       else
13814         {
13815           length = 1;
13816         }
13817     }
13818 
13819   index_type = objfile_type (objfile)->builtin_int;
13820   range_type = create_range_type (NULL, index_type, 1, length);
13821   char_type = language_string_char_type (cu->language_defn, gdbarch);
13822   type = create_string_type (NULL, char_type, range_type);
13823 
13824   return set_die_type (die, type, cu);
13825 }
13826 
13827 /* Assuming that DIE corresponds to a function, returns nonzero
13828    if the function is prototyped.  */
13829 
13830 static int
13831 prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
13832 {
13833   struct attribute *attr;
13834 
13835   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
13836   if (attr && (DW_UNSND (attr) != 0))
13837     return 1;
13838 
13839   /* The DWARF standard implies that the DW_AT_prototyped attribute
13840      is only meaninful for C, but the concept also extends to other
13841      languages that allow unprototyped functions (Eg: Objective C).
13842      For all other languages, assume that functions are always
13843      prototyped.  */
13844   if (cu->language != language_c
13845       && cu->language != language_objc
13846       && cu->language != language_opencl)
13847     return 1;
13848 
13849   /* RealView does not emit DW_AT_prototyped.  We can not distinguish
13850      prototyped and unprototyped functions; default to prototyped,
13851      since that is more common in modern code (and RealView warns
13852      about unprototyped functions).  */
13853   if (producer_is_realview (cu->producer))
13854     return 1;
13855 
13856   return 0;
13857 }
13858 
13859 /* Handle DIES due to C code like:
13860 
13861    struct foo
13862    {
13863    int (*funcp)(int a, long l);
13864    int b;
13865    };
13866 
13867    ('funcp' generates a DW_TAG_subroutine_type DIE).  */
13868 
13869 static struct type *
13870 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
13871 {
13872   struct objfile *objfile = cu->objfile;
13873   struct type *type;            /* Type that this function returns.  */
13874   struct type *ftype;           /* Function that returns above type.  */
13875   struct attribute *attr;
13876 
13877   type = die_type (die, cu);
13878 
13879   /* The die_type call above may have already set the type for this DIE.  */
13880   ftype = get_die_type (die, cu);
13881   if (ftype)
13882     return ftype;
13883 
13884   ftype = lookup_function_type (type);
13885 
13886   if (prototyped_function_p (die, cu))
13887     TYPE_PROTOTYPED (ftype) = 1;
13888 
13889   /* Store the calling convention in the type if it's available in
13890      the subroutine die.  Otherwise set the calling convention to
13891      the default value DW_CC_normal.  */
13892   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
13893   if (attr)
13894     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
13895   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
13896     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
13897   else
13898     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
13899 
13900   /* We need to add the subroutine type to the die immediately so
13901      we don't infinitely recurse when dealing with parameters
13902      declared as the same subroutine type.  */
13903   set_die_type (die, ftype, cu);
13904 
13905   if (die->child != NULL)
13906     {
13907       struct type *void_type = objfile_type (objfile)->builtin_void;
13908       struct die_info *child_die;
13909       int nparams, iparams;
13910 
13911       /* Count the number of parameters.
13912          FIXME: GDB currently ignores vararg functions, but knows about
13913          vararg member functions.  */
13914       nparams = 0;
13915       child_die = die->child;
13916       while (child_die && child_die->tag)
13917         {
13918           if (child_die->tag == DW_TAG_formal_parameter)
13919             nparams++;
13920           else if (child_die->tag == DW_TAG_unspecified_parameters)
13921             TYPE_VARARGS (ftype) = 1;
13922           child_die = sibling_die (child_die);
13923         }
13924 
13925       /* Allocate storage for parameters and fill them in.  */
13926       TYPE_NFIELDS (ftype) = nparams;
13927       TYPE_FIELDS (ftype) = (struct field *)
13928         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
13929 
13930       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
13931          even if we error out during the parameters reading below.  */
13932       for (iparams = 0; iparams < nparams; iparams++)
13933         TYPE_FIELD_TYPE (ftype, iparams) = void_type;
13934 
13935       iparams = 0;
13936       child_die = die->child;
13937       while (child_die && child_die->tag)
13938         {
13939           if (child_die->tag == DW_TAG_formal_parameter)
13940             {
13941               struct type *arg_type;
13942 
13943               /* DWARF version 2 has no clean way to discern C++
13944                  static and non-static member functions.  G++ helps
13945                  GDB by marking the first parameter for non-static
13946                  member functions (which is the this pointer) as
13947                  artificial.  We pass this information to
13948                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
13949 
13950                  DWARF version 3 added DW_AT_object_pointer, which GCC
13951                  4.5 does not yet generate.  */
13952               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
13953               if (attr)
13954                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
13955               else
13956                 {
13957                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
13958 
13959                   /* GCC/43521: In java, the formal parameter
13960                      "this" is sometimes not marked with DW_AT_artificial.  */
13961                   if (cu->language == language_java)
13962                     {
13963                       const char *name = dwarf2_name (child_die, cu);
13964 
13965                       if (name && !strcmp (name, "this"))
13966                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
13967                     }
13968                 }
13969               arg_type = die_type (child_die, cu);
13970 
13971               /* RealView does not mark THIS as const, which the testsuite
13972                  expects.  GCC marks THIS as const in method definitions,
13973                  but not in the class specifications (GCC PR 43053).  */
13974               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
13975                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
13976                 {
13977                   int is_this = 0;
13978                   struct dwarf2_cu *arg_cu = cu;
13979                   const char *name = dwarf2_name (child_die, cu);
13980 
13981                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
13982                   if (attr)
13983                     {
13984                       /* If the compiler emits this, use it.  */
13985                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
13986                         is_this = 1;
13987                     }
13988                   else if (name && strcmp (name, "this") == 0)
13989                     /* Function definitions will have the argument names.  */
13990                     is_this = 1;
13991                   else if (name == NULL && iparams == 0)
13992                     /* Declarations may not have the names, so like
13993                        elsewhere in GDB, assume an artificial first
13994                        argument is "this".  */
13995                     is_this = 1;
13996 
13997                   if (is_this)
13998                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
13999                                              arg_type, 0);
14000                 }
14001 
14002               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
14003               iparams++;
14004             }
14005           child_die = sibling_die (child_die);
14006         }
14007     }
14008 
14009   return ftype;
14010 }
14011 
14012 static struct type *
14013 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
14014 {
14015   struct objfile *objfile = cu->objfile;
14016   const char *name = NULL;
14017   struct type *this_type, *target_type;
14018 
14019   name = dwarf2_full_name (NULL, die, cu);
14020   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
14021                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
14022   TYPE_NAME (this_type) = name;
14023   set_die_type (die, this_type, cu);
14024   target_type = die_type (die, cu);
14025   if (target_type != this_type)
14026     TYPE_TARGET_TYPE (this_type) = target_type;
14027   else
14028     {
14029       /* Self-referential typedefs are, it seems, not allowed by the DWARF
14030          spec and cause infinite loops in GDB.  */
14031       complaint (&symfile_complaints,
14032                  _("Self-referential DW_TAG_typedef "
14033                    "- DIE at 0x%x [in module %s]"),
14034                  die->offset.sect_off, objfile_name (objfile));
14035       TYPE_TARGET_TYPE (this_type) = NULL;
14036     }
14037   return this_type;
14038 }
14039 
14040 /* Find a representation of a given base type and install
14041    it in the TYPE field of the die.  */
14042 
14043 static struct type *
14044 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
14045 {
14046   struct objfile *objfile = cu->objfile;
14047   struct type *type;
14048   struct attribute *attr;
14049   int encoding = 0, size = 0;
14050   const char *name;
14051   enum type_code code = TYPE_CODE_INT;
14052   int type_flags = 0;
14053   struct type *target_type = NULL;
14054 
14055   attr = dwarf2_attr (die, DW_AT_encoding, cu);
14056   if (attr)
14057     {
14058       encoding = DW_UNSND (attr);
14059     }
14060   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14061   if (attr)
14062     {
14063       size = DW_UNSND (attr);
14064     }
14065   name = dwarf2_name (die, cu);
14066   if (!name)
14067     {
14068       complaint (&symfile_complaints,
14069                  _("DW_AT_name missing from DW_TAG_base_type"));
14070     }
14071 
14072   switch (encoding)
14073     {
14074       case DW_ATE_address:
14075         /* Turn DW_ATE_address into a void * pointer.  */
14076         code = TYPE_CODE_PTR;
14077         type_flags |= TYPE_FLAG_UNSIGNED;
14078         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
14079         break;
14080       case DW_ATE_boolean:
14081         code = TYPE_CODE_BOOL;
14082         type_flags |= TYPE_FLAG_UNSIGNED;
14083         break;
14084       case DW_ATE_complex_float:
14085         code = TYPE_CODE_COMPLEX;
14086         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
14087         break;
14088       case DW_ATE_decimal_float:
14089         code = TYPE_CODE_DECFLOAT;
14090         break;
14091       case DW_ATE_float:
14092         code = TYPE_CODE_FLT;
14093         break;
14094       case DW_ATE_signed:
14095         break;
14096       case DW_ATE_unsigned:
14097         type_flags |= TYPE_FLAG_UNSIGNED;
14098         if (cu->language == language_fortran
14099             && name
14100             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
14101           code = TYPE_CODE_CHAR;
14102         break;
14103       case DW_ATE_signed_char:
14104         if (cu->language == language_ada || cu->language == language_m2
14105             || cu->language == language_pascal
14106             || cu->language == language_fortran)
14107           code = TYPE_CODE_CHAR;
14108         break;
14109       case DW_ATE_unsigned_char:
14110         if (cu->language == language_ada || cu->language == language_m2
14111             || cu->language == language_pascal
14112             || cu->language == language_fortran)
14113           code = TYPE_CODE_CHAR;
14114         type_flags |= TYPE_FLAG_UNSIGNED;
14115         break;
14116       case DW_ATE_UTF:
14117         /* We just treat this as an integer and then recognize the
14118            type by name elsewhere.  */
14119         break;
14120 
14121       default:
14122         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
14123                    dwarf_type_encoding_name (encoding));
14124         break;
14125     }
14126 
14127   type = init_type (code, size, type_flags, NULL, objfile);
14128   TYPE_NAME (type) = name;
14129   TYPE_TARGET_TYPE (type) = target_type;
14130 
14131   if (name && strcmp (name, "char") == 0)
14132     TYPE_NOSIGN (type) = 1;
14133 
14134   return set_die_type (die, type, cu);
14135 }
14136 
14137 /* Read the given DW_AT_subrange DIE.  */
14138 
14139 static struct type *
14140 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
14141 {
14142   struct type *base_type, *orig_base_type;
14143   struct type *range_type;
14144   struct attribute *attr;
14145   LONGEST low, high;
14146   int low_default_is_valid;
14147   const char *name;
14148   LONGEST negative_mask;
14149 
14150   orig_base_type = die_type (die, cu);
14151   /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
14152      whereas the real type might be.  So, we use ORIG_BASE_TYPE when
14153      creating the range type, but we use the result of check_typedef
14154      when examining properties of the type.  */
14155   base_type = check_typedef (orig_base_type);
14156 
14157   /* The die_type call above may have already set the type for this DIE.  */
14158   range_type = get_die_type (die, cu);
14159   if (range_type)
14160     return range_type;
14161 
14162   /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
14163      omitting DW_AT_lower_bound.  */
14164   switch (cu->language)
14165     {
14166     case language_c:
14167     case language_cplus:
14168       low = 0;
14169       low_default_is_valid = 1;
14170       break;
14171     case language_fortran:
14172       low = 1;
14173       low_default_is_valid = 1;
14174       break;
14175     case language_d:
14176     case language_java:
14177     case language_objc:
14178       low = 0;
14179       low_default_is_valid = (cu->header.version >= 4);
14180       break;
14181     case language_ada:
14182     case language_m2:
14183     case language_pascal:
14184       low = 1;
14185       low_default_is_valid = (cu->header.version >= 4);
14186       break;
14187     default:
14188       low = 0;
14189       low_default_is_valid = 0;
14190       break;
14191     }
14192 
14193   /* FIXME: For variable sized arrays either of these could be
14194      a variable rather than a constant value.  We'll allow it,
14195      but we don't know how to handle it.  */
14196   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
14197   if (attr)
14198     low = dwarf2_get_attr_constant_value (attr, low);
14199   else if (!low_default_is_valid)
14200     complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
14201                                       "- DIE at 0x%x [in module %s]"),
14202                die->offset.sect_off, objfile_name (cu->objfile));
14203 
14204   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
14205   if (attr)
14206     {
14207       if (attr_form_is_block (attr) || attr_form_is_ref (attr))
14208         {
14209           /* GCC encodes arrays with unspecified or dynamic length
14210              with a DW_FORM_block1 attribute or a reference attribute.
14211              FIXME: GDB does not yet know how to handle dynamic
14212              arrays properly, treat them as arrays with unspecified
14213              length for now.
14214 
14215              FIXME: jimb/2003-09-22: GDB does not really know
14216              how to handle arrays of unspecified length
14217              either; we just represent them as zero-length
14218              arrays.  Choose an appropriate upper bound given
14219              the lower bound we've computed above.  */
14220           high = low - 1;
14221         }
14222       else
14223         high = dwarf2_get_attr_constant_value (attr, 1);
14224     }
14225   else
14226     {
14227       attr = dwarf2_attr (die, DW_AT_count, cu);
14228       if (attr)
14229         {
14230           int count = dwarf2_get_attr_constant_value (attr, 1);
14231           high = low + count - 1;
14232         }
14233       else
14234         {
14235           /* Unspecified array length.  */
14236           high = low - 1;
14237         }
14238     }
14239 
14240   /* Dwarf-2 specifications explicitly allows to create subrange types
14241      without specifying a base type.
14242      In that case, the base type must be set to the type of
14243      the lower bound, upper bound or count, in that order, if any of these
14244      three attributes references an object that has a type.
14245      If no base type is found, the Dwarf-2 specifications say that
14246      a signed integer type of size equal to the size of an address should
14247      be used.
14248      For the following C code: `extern char gdb_int [];'
14249      GCC produces an empty range DIE.
14250      FIXME: muller/2010-05-28: Possible references to object for low bound,
14251      high bound or count are not yet handled by this code.  */
14252   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
14253     {
14254       struct objfile *objfile = cu->objfile;
14255       struct gdbarch *gdbarch = get_objfile_arch (objfile);
14256       int addr_size = gdbarch_addr_bit (gdbarch) /8;
14257       struct type *int_type = objfile_type (objfile)->builtin_int;
14258 
14259       /* Test "int", "long int", and "long long int" objfile types,
14260          and select the first one having a size above or equal to the
14261          architecture address size.  */
14262       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14263         base_type = int_type;
14264       else
14265         {
14266           int_type = objfile_type (objfile)->builtin_long;
14267           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14268             base_type = int_type;
14269           else
14270             {
14271               int_type = objfile_type (objfile)->builtin_long_long;
14272               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
14273                 base_type = int_type;
14274             }
14275         }
14276     }
14277 
14278   negative_mask =
14279     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
14280   if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
14281     low |= negative_mask;
14282   if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
14283     high |= negative_mask;
14284 
14285   range_type = create_range_type (NULL, orig_base_type, low, high);
14286 
14287   /* Mark arrays with dynamic length at least as an array of unspecified
14288      length.  GDB could check the boundary but before it gets implemented at
14289      least allow accessing the array elements.  */
14290   if (attr && attr_form_is_block (attr))
14291     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
14292 
14293   /* Ada expects an empty array on no boundary attributes.  */
14294   if (attr == NULL && cu->language != language_ada)
14295     TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
14296 
14297   name = dwarf2_name (die, cu);
14298   if (name)
14299     TYPE_NAME (range_type) = name;
14300 
14301   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
14302   if (attr)
14303     TYPE_LENGTH (range_type) = DW_UNSND (attr);
14304 
14305   set_die_type (die, range_type, cu);
14306 
14307   /* set_die_type should be already done.  */
14308   set_descriptive_type (range_type, die, cu);
14309 
14310   return range_type;
14311 }
14312 
14313 static struct type *
14314 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
14315 {
14316   struct type *type;
14317 
14318   /* For now, we only support the C meaning of an unspecified type: void.  */
14319 
14320   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
14321   TYPE_NAME (type) = dwarf2_name (die, cu);
14322 
14323   return set_die_type (die, type, cu);
14324 }
14325 
14326 /* Read a single die and all its descendents.  Set the die's sibling
14327    field to NULL; set other fields in the die correctly, and set all
14328    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
14329    location of the info_ptr after reading all of those dies.  PARENT
14330    is the parent of the die in question.  */
14331 
14332 static struct die_info *
14333 read_die_and_children (const struct die_reader_specs *reader,
14334                        const gdb_byte *info_ptr,
14335                        const gdb_byte **new_info_ptr,
14336                        struct die_info *parent)
14337 {
14338   struct die_info *die;
14339   const gdb_byte *cur_ptr;
14340   int has_children;
14341 
14342   cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
14343   if (die == NULL)
14344     {
14345       *new_info_ptr = cur_ptr;
14346       return NULL;
14347     }
14348   store_in_ref_table (die, reader->cu);
14349 
14350   if (has_children)
14351     die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
14352   else
14353     {
14354       die->child = NULL;
14355       *new_info_ptr = cur_ptr;
14356     }
14357 
14358   die->sibling = NULL;
14359   die->parent = parent;
14360   return die;
14361 }
14362 
14363 /* Read a die, all of its descendents, and all of its siblings; set
14364    all of the fields of all of the dies correctly.  Arguments are as
14365    in read_die_and_children.  */
14366 
14367 static struct die_info *
14368 read_die_and_siblings_1 (const struct die_reader_specs *reader,
14369                          const gdb_byte *info_ptr,
14370                          const gdb_byte **new_info_ptr,
14371                          struct die_info *parent)
14372 {
14373   struct die_info *first_die, *last_sibling;
14374   const gdb_byte *cur_ptr;
14375 
14376   cur_ptr = info_ptr;
14377   first_die = last_sibling = NULL;
14378 
14379   while (1)
14380     {
14381       struct die_info *die
14382         = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
14383 
14384       if (die == NULL)
14385         {
14386           *new_info_ptr = cur_ptr;
14387           return first_die;
14388         }
14389 
14390       if (!first_die)
14391         first_die = die;
14392       else
14393         last_sibling->sibling = die;
14394 
14395       last_sibling = die;
14396     }
14397 }
14398 
14399 /* Read a die, all of its descendents, and all of its siblings; set
14400    all of the fields of all of the dies correctly.  Arguments are as
14401    in read_die_and_children.
14402    This the main entry point for reading a DIE and all its children.  */
14403 
14404 static struct die_info *
14405 read_die_and_siblings (const struct die_reader_specs *reader,
14406                        const gdb_byte *info_ptr,
14407                        const gdb_byte **new_info_ptr,
14408                        struct die_info *parent)
14409 {
14410   struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
14411                                                   new_info_ptr, parent);
14412 
14413   if (dwarf2_die_debug)
14414     {
14415       fprintf_unfiltered (gdb_stdlog,
14416                           "Read die from %s@0x%x of %s:\n",
14417                           get_section_name (reader->die_section),
14418                           (unsigned) (info_ptr - reader->die_section->buffer),
14419                           bfd_get_filename (reader->abfd));
14420       dump_die (die, dwarf2_die_debug);
14421     }
14422 
14423   return die;
14424 }
14425 
14426 /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
14427    attributes.
14428    The caller is responsible for filling in the extra attributes
14429    and updating (*DIEP)->num_attrs.
14430    Set DIEP to point to a newly allocated die with its information,
14431    except for its child, sibling, and parent fields.
14432    Set HAS_CHILDREN to tell whether the die has children or not.  */
14433 
14434 static const gdb_byte *
14435 read_full_die_1 (const struct die_reader_specs *reader,
14436                  struct die_info **diep, const gdb_byte *info_ptr,
14437                  int *has_children, int num_extra_attrs)
14438 {
14439   unsigned int abbrev_number, bytes_read, i;
14440   sect_offset offset;
14441   struct abbrev_info *abbrev;
14442   struct die_info *die;
14443   struct dwarf2_cu *cu = reader->cu;
14444   bfd *abfd = reader->abfd;
14445 
14446   offset.sect_off = info_ptr - reader->buffer;
14447   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
14448   info_ptr += bytes_read;
14449   if (!abbrev_number)
14450     {
14451       *diep = NULL;
14452       *has_children = 0;
14453       return info_ptr;
14454     }
14455 
14456   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
14457   if (!abbrev)
14458     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
14459            abbrev_number,
14460            bfd_get_filename (abfd));
14461 
14462   die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
14463   die->offset = offset;
14464   die->tag = abbrev->tag;
14465   die->abbrev = abbrev_number;
14466 
14467   /* Make the result usable.
14468      The caller needs to update num_attrs after adding the extra
14469      attributes.  */
14470   die->num_attrs = abbrev->num_attrs;
14471 
14472   for (i = 0; i < abbrev->num_attrs; ++i)
14473     info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
14474                                info_ptr);
14475 
14476   *diep = die;
14477   *has_children = abbrev->has_children;
14478   return info_ptr;
14479 }
14480 
14481 /* Read a die and all its attributes.
14482    Set DIEP to point to a newly allocated die with its information,
14483    except for its child, sibling, and parent fields.
14484    Set HAS_CHILDREN to tell whether the die has children or not.  */
14485 
14486 static const gdb_byte *
14487 read_full_die (const struct die_reader_specs *reader,
14488                struct die_info **diep, const gdb_byte *info_ptr,
14489                int *has_children)
14490 {
14491   const gdb_byte *result;
14492 
14493   result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);
14494 
14495   if (dwarf2_die_debug)
14496     {
14497       fprintf_unfiltered (gdb_stdlog,
14498                           "Read die from %s@0x%x of %s:\n",
14499                           get_section_name (reader->die_section),
14500                           (unsigned) (info_ptr - reader->die_section->buffer),
14501                           bfd_get_filename (reader->abfd));
14502       dump_die (*diep, dwarf2_die_debug);
14503     }
14504 
14505   return result;
14506 }
14507 
14508 /* Abbreviation tables.
14509 
14510    In DWARF version 2, the description of the debugging information is
14511    stored in a separate .debug_abbrev section.  Before we read any
14512    dies from a section we read in all abbreviations and install them
14513    in a hash table.  */
14514 
14515 /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */
14516 
14517 static struct abbrev_info *
14518 abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
14519 {
14520   struct abbrev_info *abbrev;
14521 
14522   abbrev = (struct abbrev_info *)
14523     obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
14524   memset (abbrev, 0, sizeof (struct abbrev_info));
14525   return abbrev;
14526 }
14527 
14528 /* Add an abbreviation to the table.  */
14529 
14530 static void
14531 abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
14532                          unsigned int abbrev_number,
14533                          struct abbrev_info *abbrev)
14534 {
14535   unsigned int hash_number;
14536 
14537   hash_number = abbrev_number % ABBREV_HASH_SIZE;
14538   abbrev->next = abbrev_table->abbrevs[hash_number];
14539   abbrev_table->abbrevs[hash_number] = abbrev;
14540 }
14541 
14542 /* Look up an abbrev in the table.
14543    Returns NULL if the abbrev is not found.  */
14544 
14545 static struct abbrev_info *
14546 abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
14547                             unsigned int abbrev_number)
14548 {
14549   unsigned int hash_number;
14550   struct abbrev_info *abbrev;
14551 
14552   hash_number = abbrev_number % ABBREV_HASH_SIZE;
14553   abbrev = abbrev_table->abbrevs[hash_number];
14554 
14555   while (abbrev)
14556     {
14557       if (abbrev->number == abbrev_number)
14558         return abbrev;
14559       abbrev = abbrev->next;
14560     }
14561   return NULL;
14562 }
14563 
14564 /* Read in an abbrev table.  */
14565 
14566 static struct abbrev_table *
14567 abbrev_table_read_table (struct dwarf2_section_info *section,
14568                          sect_offset offset)
14569 {
14570   struct objfile *objfile = dwarf2_per_objfile->objfile;
14571   bfd *abfd = get_section_bfd_owner (section);
14572   struct abbrev_table *abbrev_table;
14573   const gdb_byte *abbrev_ptr;
14574   struct abbrev_info *cur_abbrev;
14575   unsigned int abbrev_number, bytes_read, abbrev_name;
14576   unsigned int abbrev_form;
14577   struct attr_abbrev *cur_attrs;
14578   unsigned int allocated_attrs;
14579 
14580   abbrev_table = XMALLOC (struct abbrev_table);
14581   abbrev_table->offset = offset;
14582   obstack_init (&abbrev_table->abbrev_obstack);
14583   abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
14584                                          (ABBREV_HASH_SIZE
14585                                           * sizeof (struct abbrev_info *)));
14586   memset (abbrev_table->abbrevs, 0,
14587           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
14588 
14589   dwarf2_read_section (objfile, section);
14590   abbrev_ptr = section->buffer + offset.sect_off;
14591   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14592   abbrev_ptr += bytes_read;
14593 
14594   allocated_attrs = ATTR_ALLOC_CHUNK;
14595   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
14596 
14597   /* Loop until we reach an abbrev number of 0.  */
14598   while (abbrev_number)
14599     {
14600       cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);
14601 
14602       /* read in abbrev header */
14603       cur_abbrev->number = abbrev_number;
14604       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14605       abbrev_ptr += bytes_read;
14606       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
14607       abbrev_ptr += 1;
14608 
14609       /* now read in declarations */
14610       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14611       abbrev_ptr += bytes_read;
14612       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14613       abbrev_ptr += bytes_read;
14614       while (abbrev_name)
14615         {
14616           if (cur_abbrev->num_attrs == allocated_attrs)
14617             {
14618               allocated_attrs += ATTR_ALLOC_CHUNK;
14619               cur_attrs
14620                 = xrealloc (cur_attrs, (allocated_attrs
14621                                         * sizeof (struct attr_abbrev)));
14622             }
14623 
14624           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
14625           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
14626           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14627           abbrev_ptr += bytes_read;
14628           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14629           abbrev_ptr += bytes_read;
14630         }
14631 
14632       cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
14633                                          (cur_abbrev->num_attrs
14634                                           * sizeof (struct attr_abbrev)));
14635       memcpy (cur_abbrev->attrs, cur_attrs,
14636               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
14637 
14638       abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);
14639 
14640       /* Get next abbreviation.
14641          Under Irix6 the abbreviations for a compilation unit are not
14642          always properly terminated with an abbrev number of 0.
14643          Exit loop if we encounter an abbreviation which we have
14644          already read (which means we are about to read the abbreviations
14645          for the next compile unit) or if the end of the abbreviation
14646          table is reached.  */
14647       if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
14648         break;
14649       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
14650       abbrev_ptr += bytes_read;
14651       if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
14652         break;
14653     }
14654 
14655   xfree (cur_attrs);
14656   return abbrev_table;
14657 }
14658 
14659 /* Free the resources held by ABBREV_TABLE.  */
14660 
14661 static void
14662 abbrev_table_free (struct abbrev_table *abbrev_table)
14663 {
14664   obstack_free (&abbrev_table->abbrev_obstack, NULL);
14665   xfree (abbrev_table);
14666 }
14667 
14668 /* Same as abbrev_table_free but as a cleanup.
14669    We pass in a pointer to the pointer to the table so that we can
14670    set the pointer to NULL when we're done.  It also simplifies
14671    build_type_unit_groups.  */
14672 
14673 static void
14674 abbrev_table_free_cleanup (void *table_ptr)
14675 {
14676   struct abbrev_table **abbrev_table_ptr = table_ptr;
14677 
14678   if (*abbrev_table_ptr != NULL)
14679     abbrev_table_free (*abbrev_table_ptr);
14680   *abbrev_table_ptr = NULL;
14681 }
14682 
14683 /* Read the abbrev table for CU from ABBREV_SECTION.  */
14684 
14685 static void
14686 dwarf2_read_abbrevs (struct dwarf2_cu *cu,
14687                      struct dwarf2_section_info *abbrev_section)
14688 {
14689   cu->abbrev_table =
14690     abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
14691 }
14692 
14693 /* Release the memory used by the abbrev table for a compilation unit.  */
14694 
14695 static void
14696 dwarf2_free_abbrev_table (void *ptr_to_cu)
14697 {
14698   struct dwarf2_cu *cu = ptr_to_cu;
14699 
14700   if (cu->abbrev_table != NULL)
14701     abbrev_table_free (cu->abbrev_table);
14702   /* Set this to NULL so that we SEGV if we try to read it later,
14703      and also because free_comp_unit verifies this is NULL.  */
14704   cu->abbrev_table = NULL;
14705 }
14706 
14707 /* Returns nonzero if TAG represents a type that we might generate a partial
14708    symbol for.  */
14709 
14710 static int
14711 is_type_tag_for_partial (int tag)
14712 {
14713   switch (tag)
14714     {
14715 #if 0
14716     /* Some types that would be reasonable to generate partial symbols for,
14717        that we don't at present.  */
14718     case DW_TAG_array_type:
14719     case DW_TAG_file_type:
14720     case DW_TAG_ptr_to_member_type:
14721     case DW_TAG_set_type:
14722     case DW_TAG_string_type:
14723     case DW_TAG_subroutine_type:
14724 #endif
14725     case DW_TAG_base_type:
14726     case DW_TAG_class_type:
14727     case DW_TAG_interface_type:
14728     case DW_TAG_enumeration_type:
14729     case DW_TAG_structure_type:
14730     case DW_TAG_subrange_type:
14731     case DW_TAG_typedef:
14732     case DW_TAG_union_type:
14733       return 1;
14734     default:
14735       return 0;
14736     }
14737 }
14738 
14739 /* Load all DIEs that are interesting for partial symbols into memory.  */
14740 
14741 static struct partial_die_info *
14742 load_partial_dies (const struct die_reader_specs *reader,
14743                    const gdb_byte *info_ptr, int building_psymtab)
14744 {
14745   struct dwarf2_cu *cu = reader->cu;
14746   struct objfile *objfile = cu->objfile;
14747   struct partial_die_info *part_die;
14748   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
14749   struct abbrev_info *abbrev;
14750   unsigned int bytes_read;
14751   unsigned int load_all = 0;
14752   int nesting_level = 1;
14753 
14754   parent_die = NULL;
14755   last_die = NULL;
14756 
14757   gdb_assert (cu->per_cu != NULL);
14758   if (cu->per_cu->load_all_dies)
14759     load_all = 1;
14760 
14761   cu->partial_dies
14762     = htab_create_alloc_ex (cu->header.length / 12,
14763                             partial_die_hash,
14764                             partial_die_eq,
14765                             NULL,
14766                             &cu->comp_unit_obstack,
14767                             hashtab_obstack_allocate,
14768                             dummy_obstack_deallocate);
14769 
14770   part_die = obstack_alloc (&cu->comp_unit_obstack,
14771                             sizeof (struct partial_die_info));
14772 
14773   while (1)
14774     {
14775       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
14776 
14777       /* A NULL abbrev means the end of a series of children.  */
14778       if (abbrev == NULL)
14779         {
14780           if (--nesting_level == 0)
14781             {
14782               /* PART_DIE was probably the last thing allocated on the
14783                  comp_unit_obstack, so we could call obstack_free
14784                  here.  We don't do that because the waste is small,
14785                  and will be cleaned up when we're done with this
14786                  compilation unit.  This way, we're also more robust
14787                  against other users of the comp_unit_obstack.  */
14788               return first_die;
14789             }
14790           info_ptr += bytes_read;
14791           last_die = parent_die;
14792           parent_die = parent_die->die_parent;
14793           continue;
14794         }
14795 
14796       /* Check for template arguments.  We never save these; if
14797          they're seen, we just mark the parent, and go on our way.  */
14798       if (parent_die != NULL
14799           && cu->language == language_cplus
14800           && (abbrev->tag == DW_TAG_template_type_param
14801               || abbrev->tag == DW_TAG_template_value_param))
14802         {
14803           parent_die->has_template_arguments = 1;
14804 
14805           if (!load_all)
14806             {
14807               /* We don't need a partial DIE for the template argument.  */
14808               info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14809               continue;
14810             }
14811         }
14812 
14813       /* We only recurse into c++ subprograms looking for template arguments.
14814          Skip their other children.  */
14815       if (!load_all
14816           && cu->language == language_cplus
14817           && parent_die != NULL
14818           && parent_die->tag == DW_TAG_subprogram)
14819         {
14820           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14821           continue;
14822         }
14823 
14824       /* Check whether this DIE is interesting enough to save.  Normally
14825          we would not be interested in members here, but there may be
14826          later variables referencing them via DW_AT_specification (for
14827          static members).  */
14828       if (!load_all
14829           && !is_type_tag_for_partial (abbrev->tag)
14830           && abbrev->tag != DW_TAG_constant
14831           && abbrev->tag != DW_TAG_enumerator
14832           && abbrev->tag != DW_TAG_subprogram
14833           && abbrev->tag != DW_TAG_lexical_block
14834           && abbrev->tag != DW_TAG_variable
14835           && abbrev->tag != DW_TAG_namespace
14836           && abbrev->tag != DW_TAG_module
14837           && abbrev->tag != DW_TAG_member
14838           && abbrev->tag != DW_TAG_imported_unit)
14839         {
14840           /* Otherwise we skip to the next sibling, if any.  */
14841           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
14842           continue;
14843         }
14844 
14845       info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
14846                                    info_ptr);
14847 
14848       /* This two-pass algorithm for processing partial symbols has a
14849          high cost in cache pressure.  Thus, handle some simple cases
14850          here which cover the majority of C partial symbols.  DIEs
14851          which neither have specification tags in them, nor could have
14852          specification tags elsewhere pointing at them, can simply be
14853          processed and discarded.
14854 
14855          This segment is also optional; scan_partial_symbols and
14856          add_partial_symbol will handle these DIEs if we chain
14857          them in normally.  When compilers which do not emit large
14858          quantities of duplicate debug information are more common,
14859          this code can probably be removed.  */
14860 
14861       /* Any complete simple types at the top level (pretty much all
14862          of them, for a language without namespaces), can be processed
14863          directly.  */
14864       if (parent_die == NULL
14865           && part_die->has_specification == 0
14866           && part_die->is_declaration == 0
14867           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
14868               || part_die->tag == DW_TAG_base_type
14869               || part_die->tag == DW_TAG_subrange_type))
14870         {
14871           if (building_psymtab && part_die->name != NULL)
14872             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14873                                  VAR_DOMAIN, LOC_TYPEDEF,
14874                                  &objfile->static_psymbols,
14875                                  0, (CORE_ADDR) 0, cu->language, objfile);
14876           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14877           continue;
14878         }
14879 
14880       /* The exception for DW_TAG_typedef with has_children above is
14881          a workaround of GCC PR debug/47510.  In the case of this complaint
14882          type_name_no_tag_or_error will error on such types later.
14883 
14884          GDB skipped children of DW_TAG_typedef by the shortcut above and then
14885          it could not find the child DIEs referenced later, this is checked
14886          above.  In correct DWARF DW_TAG_typedef should have no children.  */
14887 
14888       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
14889         complaint (&symfile_complaints,
14890                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
14891                      "- DIE at 0x%x [in module %s]"),
14892                    part_die->offset.sect_off, objfile_name (objfile));
14893 
14894       /* If we're at the second level, and we're an enumerator, and
14895          our parent has no specification (meaning possibly lives in a
14896          namespace elsewhere), then we can add the partial symbol now
14897          instead of queueing it.  */
14898       if (part_die->tag == DW_TAG_enumerator
14899           && parent_die != NULL
14900           && parent_die->die_parent == NULL
14901           && parent_die->tag == DW_TAG_enumeration_type
14902           && parent_die->has_specification == 0)
14903         {
14904           if (part_die->name == NULL)
14905             complaint (&symfile_complaints,
14906                        _("malformed enumerator DIE ignored"));
14907           else if (building_psymtab)
14908             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
14909                                  VAR_DOMAIN, LOC_CONST,
14910                                  (cu->language == language_cplus
14911                                   || cu->language == language_java)
14912                                  ? &objfile->global_psymbols
14913                                  : &objfile->static_psymbols,
14914                                  0, (CORE_ADDR) 0, cu->language, objfile);
14915 
14916           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
14917           continue;
14918         }
14919 
14920       /* We'll save this DIE so link it in.  */
14921       part_die->die_parent = parent_die;
14922       part_die->die_sibling = NULL;
14923       part_die->die_child = NULL;
14924 
14925       if (last_die && last_die == parent_die)
14926         last_die->die_child = part_die;
14927       else if (last_die)
14928         last_die->die_sibling = part_die;
14929 
14930       last_die = part_die;
14931 
14932       if (first_die == NULL)
14933         first_die = part_die;
14934 
14935       /* Maybe add the DIE to the hash table.  Not all DIEs that we
14936          find interesting need to be in the hash table, because we
14937          also have the parent/sibling/child chains; only those that we
14938          might refer to by offset later during partial symbol reading.
14939 
14940          For now this means things that might have be the target of a
14941          DW_AT_specification, DW_AT_abstract_origin, or
14942          DW_AT_extension.  DW_AT_extension will refer only to
14943          namespaces; DW_AT_abstract_origin refers to functions (and
14944          many things under the function DIE, but we do not recurse
14945          into function DIEs during partial symbol reading) and
14946          possibly variables as well; DW_AT_specification refers to
14947          declarations.  Declarations ought to have the DW_AT_declaration
14948          flag.  It happens that GCC forgets to put it in sometimes, but
14949          only for functions, not for types.
14950 
14951          Adding more things than necessary to the hash table is harmless
14952          except for the performance cost.  Adding too few will result in
14953          wasted time in find_partial_die, when we reread the compilation
14954          unit with load_all_dies set.  */
14955 
14956       if (load_all
14957           || abbrev->tag == DW_TAG_constant
14958           || abbrev->tag == DW_TAG_subprogram
14959           || abbrev->tag == DW_TAG_variable
14960           || abbrev->tag == DW_TAG_namespace
14961           || part_die->is_declaration)
14962         {
14963           void **slot;
14964 
14965           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
14966                                            part_die->offset.sect_off, INSERT);
14967           *slot = part_die;
14968         }
14969 
14970       part_die = obstack_alloc (&cu->comp_unit_obstack,
14971                                 sizeof (struct partial_die_info));
14972 
14973       /* For some DIEs we want to follow their children (if any).  For C
14974          we have no reason to follow the children of structures; for other
14975          languages we have to, so that we can get at method physnames
14976          to infer fully qualified class names, for DW_AT_specification,
14977          and for C++ template arguments.  For C++, we also look one level
14978          inside functions to find template arguments (if the name of the
14979          function does not already contain the template arguments).
14980 
14981          For Ada, we need to scan the children of subprograms and lexical
14982          blocks as well because Ada allows the definition of nested
14983          entities that could be interesting for the debugger, such as
14984          nested subprograms for instance.  */
14985       if (last_die->has_children
14986           && (load_all
14987               || last_die->tag == DW_TAG_namespace
14988               || last_die->tag == DW_TAG_module
14989               || last_die->tag == DW_TAG_enumeration_type
14990               || (cu->language == language_cplus
14991                   && last_die->tag == DW_TAG_subprogram
14992                   && (last_die->name == NULL
14993                       || strchr (last_die->name, '<') == NULL))
14994               || (cu->language != language_c
14995                   && (last_die->tag == DW_TAG_class_type
14996                       || last_die->tag == DW_TAG_interface_type
14997                       || last_die->tag == DW_TAG_structure_type
14998                       || last_die->tag == DW_TAG_union_type))
14999               || (cu->language == language_ada
15000                   && (last_die->tag == DW_TAG_subprogram
15001                       || last_die->tag == DW_TAG_lexical_block))))
15002         {
15003           nesting_level++;
15004           parent_die = last_die;
15005           continue;
15006         }
15007 
15008       /* Otherwise we skip to the next sibling, if any.  */
15009       info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
15010 
15011       /* Back to the top, do it again.  */
15012     }
15013 }
15014 
15015 /* Read a minimal amount of information into the minimal die structure.  */
15016 
15017 static const gdb_byte *
15018 read_partial_die (const struct die_reader_specs *reader,
15019                   struct partial_die_info *part_die,
15020                   struct abbrev_info *abbrev, unsigned int abbrev_len,
15021                   const gdb_byte *info_ptr)
15022 {
15023   struct dwarf2_cu *cu = reader->cu;
15024   struct objfile *objfile = cu->objfile;
15025   const gdb_byte *buffer = reader->buffer;
15026   unsigned int i;
15027   struct attribute attr;
15028   int has_low_pc_attr = 0;
15029   int has_high_pc_attr = 0;
15030   int high_pc_relative = 0;
15031 
15032   memset (part_die, 0, sizeof (struct partial_die_info));
15033 
15034   part_die->offset.sect_off = info_ptr - buffer;
15035 
15036   info_ptr += abbrev_len;
15037 
15038   if (abbrev == NULL)
15039     return info_ptr;
15040 
15041   part_die->tag = abbrev->tag;
15042   part_die->has_children = abbrev->has_children;
15043 
15044   for (i = 0; i < abbrev->num_attrs; ++i)
15045     {
15046       info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
15047 
15048       /* Store the data if it is of an attribute we want to keep in a
15049          partial symbol table.  */
15050       switch (attr.name)
15051         {
15052         case DW_AT_name:
15053           switch (part_die->tag)
15054             {
15055             case DW_TAG_compile_unit:
15056             case DW_TAG_partial_unit:
15057             case DW_TAG_type_unit:
15058               /* Compilation units have a DW_AT_name that is a filename, not
15059                  a source language identifier.  */
15060             case DW_TAG_enumeration_type:
15061             case DW_TAG_enumerator:
15062               /* These tags always have simple identifiers already; no need
15063                  to canonicalize them.  */
15064               part_die->name = DW_STRING (&attr);
15065               break;
15066             default:
15067               part_die->name
15068                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
15069                                             &objfile->objfile_obstack);
15070               break;
15071             }
15072           break;
15073         case DW_AT_linkage_name:
15074         case DW_AT_MIPS_linkage_name:
15075           /* Note that both forms of linkage name might appear.  We
15076              assume they will be the same, and we only store the last
15077              one we see.  */
15078           if (cu->language == language_ada)
15079             part_die->name = DW_STRING (&attr);
15080           part_die->linkage_name = DW_STRING (&attr);
15081           break;
15082         case DW_AT_low_pc:
15083           has_low_pc_attr = 1;
15084           part_die->lowpc = DW_ADDR (&attr);
15085           break;
15086         case DW_AT_high_pc:
15087           has_high_pc_attr = 1;
15088           if (attr.form == DW_FORM_addr
15089               || attr.form == DW_FORM_GNU_addr_index)
15090             part_die->highpc = DW_ADDR (&attr);
15091           else
15092             {
15093               high_pc_relative = 1;
15094               part_die->highpc = DW_UNSND (&attr);
15095             }
15096           break;
15097         case DW_AT_location:
15098           /* Support the .debug_loc offsets.  */
15099           if (attr_form_is_block (&attr))
15100             {
15101                part_die->d.locdesc = DW_BLOCK (&attr);
15102             }
15103           else if (attr_form_is_section_offset (&attr))
15104             {
15105               dwarf2_complex_location_expr_complaint ();
15106             }
15107           else
15108             {
15109               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15110                                                      "partial symbol information");
15111             }
15112           break;
15113         case DW_AT_external:
15114           part_die->is_external = DW_UNSND (&attr);
15115           break;
15116         case DW_AT_declaration:
15117           part_die->is_declaration = DW_UNSND (&attr);
15118           break;
15119         case DW_AT_type:
15120           part_die->has_type = 1;
15121           break;
15122         case DW_AT_abstract_origin:
15123         case DW_AT_specification:
15124         case DW_AT_extension:
15125           part_die->has_specification = 1;
15126           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
15127           part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15128                                    || cu->per_cu->is_dwz);
15129           break;
15130         case DW_AT_sibling:
15131           /* Ignore absolute siblings, they might point outside of
15132              the current compile unit.  */
15133           if (attr.form == DW_FORM_ref_addr)
15134             complaint (&symfile_complaints,
15135                        _("ignoring absolute DW_AT_sibling"));
15136           else
15137             part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
15138           break;
15139         case DW_AT_byte_size:
15140           part_die->has_byte_size = 1;
15141           break;
15142         case DW_AT_calling_convention:
15143           /* DWARF doesn't provide a way to identify a program's source-level
15144              entry point.  DW_AT_calling_convention attributes are only meant
15145              to describe functions' calling conventions.
15146 
15147              However, because it's a necessary piece of information in
15148              Fortran, and because DW_CC_program is the only piece of debugging
15149              information whose definition refers to a 'main program' at all,
15150              several compilers have begun marking Fortran main programs with
15151              DW_CC_program --- even when those functions use the standard
15152              calling conventions.
15153 
15154              So until DWARF specifies a way to provide this information and
15155              compilers pick up the new representation, we'll support this
15156              practice.  */
15157           if (DW_UNSND (&attr) == DW_CC_program
15158               && cu->language == language_fortran)
15159             {
15160               set_main_name (part_die->name);
15161 
15162               /* As this DIE has a static linkage the name would be difficult
15163                  to look up later.  */
15164               language_of_main = language_fortran;
15165             }
15166           break;
15167         case DW_AT_inline:
15168           if (DW_UNSND (&attr) == DW_INL_inlined
15169               || DW_UNSND (&attr) == DW_INL_declared_inlined)
15170             part_die->may_be_inlined = 1;
15171           break;
15172 
15173         case DW_AT_import:
15174           if (part_die->tag == DW_TAG_imported_unit)
15175             {
15176               part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
15177               part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
15178                                   || cu->per_cu->is_dwz);
15179             }
15180           break;
15181 
15182         default:
15183           break;
15184         }
15185     }
15186 
15187   if (high_pc_relative)
15188     part_die->highpc += part_die->lowpc;
15189 
15190   if (has_low_pc_attr && has_high_pc_attr)
15191     {
15192       /* When using the GNU linker, .gnu.linkonce. sections are used to
15193          eliminate duplicate copies of functions and vtables and such.
15194          The linker will arbitrarily choose one and discard the others.
15195          The AT_*_pc values for such functions refer to local labels in
15196          these sections.  If the section from that file was discarded, the
15197          labels are not in the output, so the relocs get a value of 0.
15198          If this is a discarded function, mark the pc bounds as invalid,
15199          so that GDB will ignore it.  */
15200       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
15201         {
15202           struct gdbarch *gdbarch = get_objfile_arch (objfile);
15203 
15204           complaint (&symfile_complaints,
15205                      _("DW_AT_low_pc %s is zero "
15206                        "for DIE at 0x%x [in module %s]"),
15207                      paddress (gdbarch, part_die->lowpc),
15208                      part_die->offset.sect_off, objfile_name (objfile));
15209         }
15210       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
15211       else if (part_die->lowpc >= part_die->highpc)
15212         {
15213           struct gdbarch *gdbarch = get_objfile_arch (objfile);
15214 
15215           complaint (&symfile_complaints,
15216                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
15217                        "for DIE at 0x%x [in module %s]"),
15218                      paddress (gdbarch, part_die->lowpc),
15219                      paddress (gdbarch, part_die->highpc),
15220                      part_die->offset.sect_off, objfile_name (objfile));
15221         }
15222       else
15223         part_die->has_pc_info = 1;
15224     }
15225 
15226   return info_ptr;
15227 }
15228 
15229 /* Find a cached partial DIE at OFFSET in CU.  */
15230 
15231 static struct partial_die_info *
15232 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
15233 {
15234   struct partial_die_info *lookup_die = NULL;
15235   struct partial_die_info part_die;
15236 
15237   part_die.offset = offset;
15238   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
15239                                     offset.sect_off);
15240 
15241   return lookup_die;
15242 }
15243 
15244 /* Find a partial DIE at OFFSET, which may or may not be in CU,
15245    except in the case of .debug_types DIEs which do not reference
15246    outside their CU (they do however referencing other types via
15247    DW_FORM_ref_sig8).  */
15248 
15249 static struct partial_die_info *
15250 find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
15251 {
15252   struct objfile *objfile = cu->objfile;
15253   struct dwarf2_per_cu_data *per_cu = NULL;
15254   struct partial_die_info *pd = NULL;
15255 
15256   if (offset_in_dwz == cu->per_cu->is_dwz
15257       && offset_in_cu_p (&cu->header, offset))
15258     {
15259       pd = find_partial_die_in_comp_unit (offset, cu);
15260       if (pd != NULL)
15261         return pd;
15262       /* We missed recording what we needed.
15263          Load all dies and try again.  */
15264       per_cu = cu->per_cu;
15265     }
15266   else
15267     {
15268       /* TUs don't reference other CUs/TUs (except via type signatures).  */
15269       if (cu->per_cu->is_debug_types)
15270         {
15271           error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
15272                    " external reference to offset 0x%lx [in module %s].\n"),
15273                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
15274                  bfd_get_filename (objfile->obfd));
15275         }
15276       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
15277                                                  objfile);
15278 
15279       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
15280         load_partial_comp_unit (per_cu);
15281 
15282       per_cu->cu->last_used = 0;
15283       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
15284     }
15285 
15286   /* If we didn't find it, and not all dies have been loaded,
15287      load them all and try again.  */
15288 
15289   if (pd == NULL && per_cu->load_all_dies == 0)
15290     {
15291       per_cu->load_all_dies = 1;
15292 
15293       /* This is nasty.  When we reread the DIEs, somewhere up the call chain
15294          THIS_CU->cu may already be in use.  So we can't just free it and
15295          replace its DIEs with the ones we read in.  Instead, we leave those
15296          DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
15297          and clobber THIS_CU->cu->partial_dies with the hash table for the new
15298          set.  */
15299       load_partial_comp_unit (per_cu);
15300 
15301       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
15302     }
15303 
15304   if (pd == NULL)
15305     internal_error (__FILE__, __LINE__,
15306                     _("could not find partial DIE 0x%x "
15307                       "in cache [from module %s]\n"),
15308                     offset.sect_off, bfd_get_filename (objfile->obfd));
15309   return pd;
15310 }
15311 
15312 /* See if we can figure out if the class lives in a namespace.  We do
15313    this by looking for a member function; its demangled name will
15314    contain namespace info, if there is any.  */
15315 
15316 static void
15317 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
15318                                   struct dwarf2_cu *cu)
15319 {
15320   /* NOTE: carlton/2003-10-07: Getting the info this way changes
15321      what template types look like, because the demangler
15322      frequently doesn't give the same name as the debug info.  We
15323      could fix this by only using the demangled name to get the
15324      prefix (but see comment in read_structure_type).  */
15325 
15326   struct partial_die_info *real_pdi;
15327   struct partial_die_info *child_pdi;
15328 
15329   /* If this DIE (this DIE's specification, if any) has a parent, then
15330      we should not do this.  We'll prepend the parent's fully qualified
15331      name when we create the partial symbol.  */
15332 
15333   real_pdi = struct_pdi;
15334   while (real_pdi->has_specification)
15335     real_pdi = find_partial_die (real_pdi->spec_offset,
15336                                  real_pdi->spec_is_dwz, cu);
15337 
15338   if (real_pdi->die_parent != NULL)
15339     return;
15340 
15341   for (child_pdi = struct_pdi->die_child;
15342        child_pdi != NULL;
15343        child_pdi = child_pdi->die_sibling)
15344     {
15345       if (child_pdi->tag == DW_TAG_subprogram
15346           && child_pdi->linkage_name != NULL)
15347         {
15348           char *actual_class_name
15349             = language_class_name_from_physname (cu->language_defn,
15350                                                  child_pdi->linkage_name);
15351           if (actual_class_name != NULL)
15352             {
15353               struct_pdi->name
15354                 = obstack_copy0 (&cu->objfile->objfile_obstack,
15355                                  actual_class_name,
15356                                  strlen (actual_class_name));
15357               xfree (actual_class_name);
15358             }
15359           break;
15360         }
15361     }
15362 }
15363 
15364 /* Adjust PART_DIE before generating a symbol for it.  This function
15365    may set the is_external flag or change the DIE's name.  */
15366 
15367 static void
15368 fixup_partial_die (struct partial_die_info *part_die,
15369                    struct dwarf2_cu *cu)
15370 {
15371   /* Once we've fixed up a die, there's no point in doing so again.
15372      This also avoids a memory leak if we were to call
15373      guess_partial_die_structure_name multiple times.  */
15374   if (part_die->fixup_called)
15375     return;
15376 
15377   /* If we found a reference attribute and the DIE has no name, try
15378      to find a name in the referred to DIE.  */
15379 
15380   if (part_die->name == NULL && part_die->has_specification)
15381     {
15382       struct partial_die_info *spec_die;
15383 
15384       spec_die = find_partial_die (part_die->spec_offset,
15385                                    part_die->spec_is_dwz, cu);
15386 
15387       fixup_partial_die (spec_die, cu);
15388 
15389       if (spec_die->name)
15390         {
15391           part_die->name = spec_die->name;
15392 
15393           /* Copy DW_AT_external attribute if it is set.  */
15394           if (spec_die->is_external)
15395             part_die->is_external = spec_die->is_external;
15396         }
15397     }
15398 
15399   /* Set default names for some unnamed DIEs.  */
15400 
15401   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
15402     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
15403 
15404   /* If there is no parent die to provide a namespace, and there are
15405      children, see if we can determine the namespace from their linkage
15406      name.  */
15407   if (cu->language == language_cplus
15408       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
15409       && part_die->die_parent == NULL
15410       && part_die->has_children
15411       && (part_die->tag == DW_TAG_class_type
15412           || part_die->tag == DW_TAG_structure_type
15413           || part_die->tag == DW_TAG_union_type))
15414     guess_partial_die_structure_name (part_die, cu);
15415 
15416   /* GCC might emit a nameless struct or union that has a linkage
15417      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
15418   if (part_die->name == NULL
15419       && (part_die->tag == DW_TAG_class_type
15420           || part_die->tag == DW_TAG_interface_type
15421           || part_die->tag == DW_TAG_structure_type
15422           || part_die->tag == DW_TAG_union_type)
15423       && part_die->linkage_name != NULL)
15424     {
15425       char *demangled;
15426 
15427       demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
15428       if (demangled)
15429         {
15430           const char *base;
15431 
15432           /* Strip any leading namespaces/classes, keep only the base name.
15433              DW_AT_name for named DIEs does not contain the prefixes.  */
15434           base = strrchr (demangled, ':');
15435           if (base && base > demangled && base[-1] == ':')
15436             base++;
15437           else
15438             base = demangled;
15439 
15440           part_die->name = obstack_copy0 (&cu->objfile->objfile_obstack,
15441                                           base, strlen (base));
15442           xfree (demangled);
15443         }
15444     }
15445 
15446   part_die->fixup_called = 1;
15447 }
15448 
15449 /* Read an attribute value described by an attribute form.  */
15450 
15451 static const gdb_byte *
15452 read_attribute_value (const struct die_reader_specs *reader,
15453                       struct attribute *attr, unsigned form,
15454                       const gdb_byte *info_ptr)
15455 {
15456   struct dwarf2_cu *cu = reader->cu;
15457   bfd *abfd = reader->abfd;
15458   struct comp_unit_head *cu_header = &cu->header;
15459   unsigned int bytes_read;
15460   struct dwarf_block *blk;
15461 
15462   attr->form = form;
15463   switch (form)
15464     {
15465     case DW_FORM_ref_addr:
15466       if (cu->header.version == 2)
15467         DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
15468       else
15469         DW_UNSND (attr) = read_offset (abfd, info_ptr,
15470                                        &cu->header, &bytes_read);
15471       info_ptr += bytes_read;
15472       break;
15473     case DW_FORM_GNU_ref_alt:
15474       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
15475       info_ptr += bytes_read;
15476       break;
15477     case DW_FORM_addr:
15478       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
15479       info_ptr += bytes_read;
15480       break;
15481     case DW_FORM_block2:
15482       blk = dwarf_alloc_block (cu);
15483       blk->size = read_2_bytes (abfd, info_ptr);
15484       info_ptr += 2;
15485       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15486       info_ptr += blk->size;
15487       DW_BLOCK (attr) = blk;
15488       break;
15489     case DW_FORM_block4:
15490       blk = dwarf_alloc_block (cu);
15491       blk->size = read_4_bytes (abfd, info_ptr);
15492       info_ptr += 4;
15493       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15494       info_ptr += blk->size;
15495       DW_BLOCK (attr) = blk;
15496       break;
15497     case DW_FORM_data2:
15498       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
15499       info_ptr += 2;
15500       break;
15501     case DW_FORM_data4:
15502       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
15503       info_ptr += 4;
15504       break;
15505     case DW_FORM_data8:
15506       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
15507       info_ptr += 8;
15508       break;
15509     case DW_FORM_sec_offset:
15510       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
15511       info_ptr += bytes_read;
15512       break;
15513     case DW_FORM_string:
15514       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
15515       DW_STRING_IS_CANONICAL (attr) = 0;
15516       info_ptr += bytes_read;
15517       break;
15518     case DW_FORM_strp:
15519       if (!cu->per_cu->is_dwz)
15520         {
15521           DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
15522                                                    &bytes_read);
15523           DW_STRING_IS_CANONICAL (attr) = 0;
15524           info_ptr += bytes_read;
15525           break;
15526         }
15527       /* FALLTHROUGH */
15528     case DW_FORM_GNU_strp_alt:
15529       {
15530         struct dwz_file *dwz = dwarf2_get_dwz_file ();
15531         LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
15532                                           &bytes_read);
15533 
15534         DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
15535         DW_STRING_IS_CANONICAL (attr) = 0;
15536         info_ptr += bytes_read;
15537       }
15538       break;
15539     case DW_FORM_exprloc:
15540     case DW_FORM_block:
15541       blk = dwarf_alloc_block (cu);
15542       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15543       info_ptr += bytes_read;
15544       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15545       info_ptr += blk->size;
15546       DW_BLOCK (attr) = blk;
15547       break;
15548     case DW_FORM_block1:
15549       blk = dwarf_alloc_block (cu);
15550       blk->size = read_1_byte (abfd, info_ptr);
15551       info_ptr += 1;
15552       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
15553       info_ptr += blk->size;
15554       DW_BLOCK (attr) = blk;
15555       break;
15556     case DW_FORM_data1:
15557       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
15558       info_ptr += 1;
15559       break;
15560     case DW_FORM_flag:
15561       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
15562       info_ptr += 1;
15563       break;
15564     case DW_FORM_flag_present:
15565       DW_UNSND (attr) = 1;
15566       break;
15567     case DW_FORM_sdata:
15568       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
15569       info_ptr += bytes_read;
15570       break;
15571     case DW_FORM_udata:
15572       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15573       info_ptr += bytes_read;
15574       break;
15575     case DW_FORM_ref1:
15576       DW_UNSND (attr) = (cu->header.offset.sect_off
15577                          + read_1_byte (abfd, info_ptr));
15578       info_ptr += 1;
15579       break;
15580     case DW_FORM_ref2:
15581       DW_UNSND (attr) = (cu->header.offset.sect_off
15582                          + read_2_bytes (abfd, info_ptr));
15583       info_ptr += 2;
15584       break;
15585     case DW_FORM_ref4:
15586       DW_UNSND (attr) = (cu->header.offset.sect_off
15587                          + read_4_bytes (abfd, info_ptr));
15588       info_ptr += 4;
15589       break;
15590     case DW_FORM_ref8:
15591       DW_UNSND (attr) = (cu->header.offset.sect_off
15592                          + read_8_bytes (abfd, info_ptr));
15593       info_ptr += 8;
15594       break;
15595     case DW_FORM_ref_sig8:
15596       DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
15597       info_ptr += 8;
15598       break;
15599     case DW_FORM_ref_udata:
15600       DW_UNSND (attr) = (cu->header.offset.sect_off
15601                          + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
15602       info_ptr += bytes_read;
15603       break;
15604     case DW_FORM_indirect:
15605       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15606       info_ptr += bytes_read;
15607       info_ptr = read_attribute_value (reader, attr, form, info_ptr);
15608       break;
15609     case DW_FORM_GNU_addr_index:
15610       if (reader->dwo_file == NULL)
15611         {
15612           /* For now flag a hard error.
15613              Later we can turn this into a complaint.  */
15614           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
15615                  dwarf_form_name (form),
15616                  bfd_get_filename (abfd));
15617         }
15618       DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
15619       info_ptr += bytes_read;
15620       break;
15621     case DW_FORM_GNU_str_index:
15622       if (reader->dwo_file == NULL)
15623         {
15624           /* For now flag a hard error.
15625              Later we can turn this into a complaint if warranted.  */
15626           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
15627                  dwarf_form_name (form),
15628                  bfd_get_filename (abfd));
15629         }
15630       {
15631         ULONGEST str_index =
15632           read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
15633 
15634         DW_STRING (attr) = read_str_index (reader, cu, str_index);
15635         DW_STRING_IS_CANONICAL (attr) = 0;
15636         info_ptr += bytes_read;
15637       }
15638       break;
15639     default:
15640       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
15641              dwarf_form_name (form),
15642              bfd_get_filename (abfd));
15643     }
15644 
15645   /* Super hack.  */
15646   if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
15647     attr->form = DW_FORM_GNU_ref_alt;
15648 
15649   /* We have seen instances where the compiler tried to emit a byte
15650      size attribute of -1 which ended up being encoded as an unsigned
15651      0xffffffff.  Although 0xffffffff is technically a valid size value,
15652      an object of this size seems pretty unlikely so we can relatively
15653      safely treat these cases as if the size attribute was invalid and
15654      treat them as zero by default.  */
15655   if (attr->name == DW_AT_byte_size
15656       && form == DW_FORM_data4
15657       && DW_UNSND (attr) >= 0xffffffff)
15658     {
15659       complaint
15660         (&symfile_complaints,
15661          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
15662          hex_string (DW_UNSND (attr)));
15663       DW_UNSND (attr) = 0;
15664     }
15665 
15666   return info_ptr;
15667 }
15668 
15669 /* Read an attribute described by an abbreviated attribute.  */
15670 
15671 static const gdb_byte *
15672 read_attribute (const struct die_reader_specs *reader,
15673                 struct attribute *attr, struct attr_abbrev *abbrev,
15674                 const gdb_byte *info_ptr)
15675 {
15676   attr->name = abbrev->name;
15677   return read_attribute_value (reader, attr, abbrev->form, info_ptr);
15678 }
15679 
15680 /* Read dwarf information from a buffer.  */
15681 
15682 static unsigned int
15683 read_1_byte (bfd *abfd, const gdb_byte *buf)
15684 {
15685   return bfd_get_8 (abfd, buf);
15686 }
15687 
15688 static int
15689 read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
15690 {
15691   return bfd_get_signed_8 (abfd, buf);
15692 }
15693 
15694 static unsigned int
15695 read_2_bytes (bfd *abfd, const gdb_byte *buf)
15696 {
15697   return bfd_get_16 (abfd, buf);
15698 }
15699 
15700 static int
15701 read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
15702 {
15703   return bfd_get_signed_16 (abfd, buf);
15704 }
15705 
15706 static unsigned int
15707 read_4_bytes (bfd *abfd, const gdb_byte *buf)
15708 {
15709   return bfd_get_32 (abfd, buf);
15710 }
15711 
15712 static int
15713 read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
15714 {
15715   return bfd_get_signed_32 (abfd, buf);
15716 }
15717 
15718 static ULONGEST
15719 read_8_bytes (bfd *abfd, const gdb_byte *buf)
15720 {
15721   return bfd_get_64 (abfd, buf);
15722 }
15723 
15724 static CORE_ADDR
15725 read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
15726               unsigned int *bytes_read)
15727 {
15728   struct comp_unit_head *cu_header = &cu->header;
15729   CORE_ADDR retval = 0;
15730 
15731   if (cu_header->signed_addr_p)
15732     {
15733       switch (cu_header->addr_size)
15734         {
15735         case 2:
15736           retval = bfd_get_signed_16 (abfd, buf);
15737           break;
15738         case 4:
15739           retval = bfd_get_signed_32 (abfd, buf);
15740           break;
15741         case 8:
15742           retval = bfd_get_signed_64 (abfd, buf);
15743           break;
15744         default:
15745           internal_error (__FILE__, __LINE__,
15746                           _("read_address: bad switch, signed [in module %s]"),
15747                           bfd_get_filename (abfd));
15748         }
15749     }
15750   else
15751     {
15752       switch (cu_header->addr_size)
15753         {
15754         case 2:
15755           retval = bfd_get_16 (abfd, buf);
15756           break;
15757         case 4:
15758           retval = bfd_get_32 (abfd, buf);
15759           break;
15760         case 8:
15761           retval = bfd_get_64 (abfd, buf);
15762           break;
15763         default:
15764           internal_error (__FILE__, __LINE__,
15765                           _("read_address: bad switch, "
15766                             "unsigned [in module %s]"),
15767                           bfd_get_filename (abfd));
15768         }
15769     }
15770 
15771   *bytes_read = cu_header->addr_size;
15772   return retval;
15773 }
15774 
15775 /* Read the initial length from a section.  The (draft) DWARF 3
15776    specification allows the initial length to take up either 4 bytes
15777    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
15778    bytes describe the length and all offsets will be 8 bytes in length
15779    instead of 4.
15780 
15781    An older, non-standard 64-bit format is also handled by this
15782    function.  The older format in question stores the initial length
15783    as an 8-byte quantity without an escape value.  Lengths greater
15784    than 2^32 aren't very common which means that the initial 4 bytes
15785    is almost always zero.  Since a length value of zero doesn't make
15786    sense for the 32-bit format, this initial zero can be considered to
15787    be an escape value which indicates the presence of the older 64-bit
15788    format.  As written, the code can't detect (old format) lengths
15789    greater than 4GB.  If it becomes necessary to handle lengths
15790    somewhat larger than 4GB, we could allow other small values (such
15791    as the non-sensical values of 1, 2, and 3) to also be used as
15792    escape values indicating the presence of the old format.
15793 
15794    The value returned via bytes_read should be used to increment the
15795    relevant pointer after calling read_initial_length().
15796 
15797    [ Note:  read_initial_length() and read_offset() are based on the
15798      document entitled "DWARF Debugging Information Format", revision
15799      3, draft 8, dated November 19, 2001.  This document was obtained
15800      from:
15801 
15802         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
15803 
15804      This document is only a draft and is subject to change.  (So beware.)
15805 
15806      Details regarding the older, non-standard 64-bit format were
15807      determined empirically by examining 64-bit ELF files produced by
15808      the SGI toolchain on an IRIX 6.5 machine.
15809 
15810      - Kevin, July 16, 2002
15811    ] */
15812 
15813 static LONGEST
15814 read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
15815 {
15816   LONGEST length = bfd_get_32 (abfd, buf);
15817 
15818   if (length == 0xffffffff)
15819     {
15820       length = bfd_get_64 (abfd, buf + 4);
15821       *bytes_read = 12;
15822     }
15823   else if (length == 0)
15824     {
15825       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
15826       length = bfd_get_64 (abfd, buf);
15827       *bytes_read = 8;
15828     }
15829   else
15830     {
15831       *bytes_read = 4;
15832     }
15833 
15834   return length;
15835 }
15836 
15837 /* Cover function for read_initial_length.
15838    Returns the length of the object at BUF, and stores the size of the
15839    initial length in *BYTES_READ and stores the size that offsets will be in
15840    *OFFSET_SIZE.
15841    If the initial length size is not equivalent to that specified in
15842    CU_HEADER then issue a complaint.
15843    This is useful when reading non-comp-unit headers.  */
15844 
15845 static LONGEST
15846 read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
15847                                         const struct comp_unit_head *cu_header,
15848                                         unsigned int *bytes_read,
15849                                         unsigned int *offset_size)
15850 {
15851   LONGEST length = read_initial_length (abfd, buf, bytes_read);
15852 
15853   gdb_assert (cu_header->initial_length_size == 4
15854               || cu_header->initial_length_size == 8
15855               || cu_header->initial_length_size == 12);
15856 
15857   if (cu_header->initial_length_size != *bytes_read)
15858     complaint (&symfile_complaints,
15859                _("intermixed 32-bit and 64-bit DWARF sections"));
15860 
15861   *offset_size = (*bytes_read == 4) ? 4 : 8;
15862   return length;
15863 }
15864 
15865 /* Read an offset from the data stream.  The size of the offset is
15866    given by cu_header->offset_size.  */
15867 
15868 static LONGEST
15869 read_offset (bfd *abfd, const gdb_byte *buf,
15870              const struct comp_unit_head *cu_header,
15871              unsigned int *bytes_read)
15872 {
15873   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
15874 
15875   *bytes_read = cu_header->offset_size;
15876   return offset;
15877 }
15878 
15879 /* Read an offset from the data stream.  */
15880 
15881 static LONGEST
15882 read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
15883 {
15884   LONGEST retval = 0;
15885 
15886   switch (offset_size)
15887     {
15888     case 4:
15889       retval = bfd_get_32 (abfd, buf);
15890       break;
15891     case 8:
15892       retval = bfd_get_64 (abfd, buf);
15893       break;
15894     default:
15895       internal_error (__FILE__, __LINE__,
15896                       _("read_offset_1: bad switch [in module %s]"),
15897                       bfd_get_filename (abfd));
15898     }
15899 
15900   return retval;
15901 }
15902 
15903 static const gdb_byte *
15904 read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
15905 {
15906   /* If the size of a host char is 8 bits, we can return a pointer
15907      to the buffer, otherwise we have to copy the data to a buffer
15908      allocated on the temporary obstack.  */
15909   gdb_assert (HOST_CHAR_BIT == 8);
15910   return buf;
15911 }
15912 
15913 static const char *
15914 read_direct_string (bfd *abfd, const gdb_byte *buf,
15915                     unsigned int *bytes_read_ptr)
15916 {
15917   /* If the size of a host char is 8 bits, we can return a pointer
15918      to the string, otherwise we have to copy the string to a buffer
15919      allocated on the temporary obstack.  */
15920   gdb_assert (HOST_CHAR_BIT == 8);
15921   if (*buf == '\0')
15922     {
15923       *bytes_read_ptr = 1;
15924       return NULL;
15925     }
15926   *bytes_read_ptr = strlen ((const char *) buf) + 1;
15927   return (const char *) buf;
15928 }
15929 
15930 static const char *
15931 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
15932 {
15933   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
15934   if (dwarf2_per_objfile->str.buffer == NULL)
15935     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
15936            bfd_get_filename (abfd));
15937   if (str_offset >= dwarf2_per_objfile->str.size)
15938     error (_("DW_FORM_strp pointing outside of "
15939              ".debug_str section [in module %s]"),
15940            bfd_get_filename (abfd));
15941   gdb_assert (HOST_CHAR_BIT == 8);
15942   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
15943     return NULL;
15944   return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
15945 }
15946 
15947 /* Read a string at offset STR_OFFSET in the .debug_str section from
15948    the .dwz file DWZ.  Throw an error if the offset is too large.  If
15949    the string consists of a single NUL byte, return NULL; otherwise
15950    return a pointer to the string.  */
15951 
15952 static const char *
15953 read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
15954 {
15955   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);
15956 
15957   if (dwz->str.buffer == NULL)
15958     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
15959              "section [in module %s]"),
15960            bfd_get_filename (dwz->dwz_bfd));
15961   if (str_offset >= dwz->str.size)
15962     error (_("DW_FORM_GNU_strp_alt pointing outside of "
15963              ".debug_str section [in module %s]"),
15964            bfd_get_filename (dwz->dwz_bfd));
15965   gdb_assert (HOST_CHAR_BIT == 8);
15966   if (dwz->str.buffer[str_offset] == '\0')
15967     return NULL;
15968   return (const char *) (dwz->str.buffer + str_offset);
15969 }
15970 
15971 static const char *
15972 read_indirect_string (bfd *abfd, const gdb_byte *buf,
15973                       const struct comp_unit_head *cu_header,
15974                       unsigned int *bytes_read_ptr)
15975 {
15976   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
15977 
15978   return read_indirect_string_at_offset (abfd, str_offset);
15979 }
15980 
15981 static ULONGEST
15982 read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
15983                       unsigned int *bytes_read_ptr)
15984 {
15985   ULONGEST result;
15986   unsigned int num_read;
15987   int i, shift;
15988   unsigned char byte;
15989 
15990   result = 0;
15991   shift = 0;
15992   num_read = 0;
15993   i = 0;
15994   while (1)
15995     {
15996       byte = bfd_get_8 (abfd, buf);
15997       buf++;
15998       num_read++;
15999       result |= ((ULONGEST) (byte & 127) << shift);
16000       if ((byte & 128) == 0)
16001         {
16002           break;
16003         }
16004       shift += 7;
16005     }
16006   *bytes_read_ptr = num_read;
16007   return result;
16008 }
16009 
16010 static LONGEST
16011 read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
16012                     unsigned int *bytes_read_ptr)
16013 {
16014   LONGEST result;
16015   int i, shift, num_read;
16016   unsigned char byte;
16017 
16018   result = 0;
16019   shift = 0;
16020   num_read = 0;
16021   i = 0;
16022   while (1)
16023     {
16024       byte = bfd_get_8 (abfd, buf);
16025       buf++;
16026       num_read++;
16027       result |= ((LONGEST) (byte & 127) << shift);
16028       shift += 7;
16029       if ((byte & 128) == 0)
16030         {
16031           break;
16032         }
16033     }
16034   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
16035     result |= -(((LONGEST) 1) << shift);
16036   *bytes_read_ptr = num_read;
16037   return result;
16038 }
16039 
16040 /* Given index ADDR_INDEX in .debug_addr, fetch the value.
16041    ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
16042    ADDR_SIZE is the size of addresses from the CU header.  */
16043 
16044 static CORE_ADDR
16045 read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
16046 {
16047   struct objfile *objfile = dwarf2_per_objfile->objfile;
16048   bfd *abfd = objfile->obfd;
16049   const gdb_byte *info_ptr;
16050 
16051   dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
16052   if (dwarf2_per_objfile->addr.buffer == NULL)
16053     error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
16054            objfile_name (objfile));
16055   if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
16056     error (_("DW_FORM_addr_index pointing outside of "
16057              ".debug_addr section [in module %s]"),
16058            objfile_name (objfile));
16059   info_ptr = (dwarf2_per_objfile->addr.buffer
16060               + addr_base + addr_index * addr_size);
16061   if (addr_size == 4)
16062     return bfd_get_32 (abfd, info_ptr);
16063   else
16064     return bfd_get_64 (abfd, info_ptr);
16065 }
16066 
16067 /* Given index ADDR_INDEX in .debug_addr, fetch the value.  */
16068 
16069 static CORE_ADDR
16070 read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
16071 {
16072   return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
16073 }
16074 
16075 /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */
16076 
16077 static CORE_ADDR
16078 read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
16079                              unsigned int *bytes_read)
16080 {
16081   bfd *abfd = cu->objfile->obfd;
16082   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
16083 
16084   return read_addr_index (cu, addr_index);
16085 }
16086 
16087 /* Data structure to pass results from dwarf2_read_addr_index_reader
16088    back to dwarf2_read_addr_index.  */
16089 
16090 struct dwarf2_read_addr_index_data
16091 {
16092   ULONGEST addr_base;
16093   int addr_size;
16094 };
16095 
16096 /* die_reader_func for dwarf2_read_addr_index.  */
16097 
16098 static void
16099 dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
16100                                const gdb_byte *info_ptr,
16101                                struct die_info *comp_unit_die,
16102                                int has_children,
16103                                void *data)
16104 {
16105   struct dwarf2_cu *cu = reader->cu;
16106   struct dwarf2_read_addr_index_data *aidata =
16107     (struct dwarf2_read_addr_index_data *) data;
16108 
16109   aidata->addr_base = cu->addr_base;
16110   aidata->addr_size = cu->header.addr_size;
16111 }
16112 
16113 /* Given an index in .debug_addr, fetch the value.
16114    NOTE: This can be called during dwarf expression evaluation,
16115    long after the debug information has been read, and thus per_cu->cu
16116    may no longer exist.  */
16117 
16118 CORE_ADDR
16119 dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
16120                         unsigned int addr_index)
16121 {
16122   struct objfile *objfile = per_cu->objfile;
16123   struct dwarf2_cu *cu = per_cu->cu;
16124   ULONGEST addr_base;
16125   int addr_size;
16126 
16127   /* This is intended to be called from outside this file.  */
16128   dw2_setup (objfile);
16129 
16130   /* We need addr_base and addr_size.
16131      If we don't have PER_CU->cu, we have to get it.
16132      Nasty, but the alternative is storing the needed info in PER_CU,
16133      which at this point doesn't seem justified: it's not clear how frequently
16134      it would get used and it would increase the size of every PER_CU.
16135      Entry points like dwarf2_per_cu_addr_size do a similar thing
16136      so we're not in uncharted territory here.
16137      Alas we need to be a bit more complicated as addr_base is contained
16138      in the DIE.
16139 
16140      We don't need to read the entire CU(/TU).
16141      We just need the header and top level die.
16142 
16143      IWBN to use the aging mechanism to let us lazily later discard the CU.
16144      For now we skip this optimization.  */
16145 
16146   if (cu != NULL)
16147     {
16148       addr_base = cu->addr_base;
16149       addr_size = cu->header.addr_size;
16150     }
16151   else
16152     {
16153       struct dwarf2_read_addr_index_data aidata;
16154 
16155       /* Note: We can't use init_cutu_and_read_dies_simple here,
16156          we need addr_base.  */
16157       init_cutu_and_read_dies (per_cu, NULL, 0, 0,
16158                                dwarf2_read_addr_index_reader, &aidata);
16159       addr_base = aidata.addr_base;
16160       addr_size = aidata.addr_size;
16161     }
16162 
16163   return read_addr_index_1 (addr_index, addr_base, addr_size);
16164 }
16165 
16166 /* Given a DW_FORM_GNU_str_index, fetch the string.
16167    This is only used by the Fission support.  */
16168 
16169 static const char *
16170 read_str_index (const struct die_reader_specs *reader,
16171                 struct dwarf2_cu *cu, ULONGEST str_index)
16172 {
16173   struct objfile *objfile = dwarf2_per_objfile->objfile;
16174   const char *dwo_name = objfile_name (objfile);
16175   bfd *abfd = objfile->obfd;
16176   struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
16177   struct dwarf2_section_info *str_offsets_section =
16178     &reader->dwo_file->sections.str_offsets;
16179   const gdb_byte *info_ptr;
16180   ULONGEST str_offset;
16181   static const char form_name[] = "DW_FORM_GNU_str_index";
16182 
16183   dwarf2_read_section (objfile, str_section);
16184   dwarf2_read_section (objfile, str_offsets_section);
16185   if (str_section->buffer == NULL)
16186     error (_("%s used without .debug_str.dwo section"
16187              " in CU at offset 0x%lx [in module %s]"),
16188            form_name, (long) cu->header.offset.sect_off, dwo_name);
16189   if (str_offsets_section->buffer == NULL)
16190     error (_("%s used without .debug_str_offsets.dwo section"
16191              " in CU at offset 0x%lx [in module %s]"),
16192            form_name, (long) cu->header.offset.sect_off, dwo_name);
16193   if (str_index * cu->header.offset_size >= str_offsets_section->size)
16194     error (_("%s pointing outside of .debug_str_offsets.dwo"
16195              " section in CU at offset 0x%lx [in module %s]"),
16196            form_name, (long) cu->header.offset.sect_off, dwo_name);
16197   info_ptr = (str_offsets_section->buffer
16198               + str_index * cu->header.offset_size);
16199   if (cu->header.offset_size == 4)
16200     str_offset = bfd_get_32 (abfd, info_ptr);
16201   else
16202     str_offset = bfd_get_64 (abfd, info_ptr);
16203   if (str_offset >= str_section->size)
16204     error (_("Offset from %s pointing outside of"
16205              " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
16206            form_name, (long) cu->header.offset.sect_off, dwo_name);
16207   return (const char *) (str_section->buffer + str_offset);
16208 }
16209 
16210 /* Return the length of an LEB128 number in BUF.  */
16211 
16212 static int
16213 leb128_size (const gdb_byte *buf)
16214 {
16215   const gdb_byte *begin = buf;
16216   gdb_byte byte;
16217 
16218   while (1)
16219     {
16220       byte = *buf++;
16221       if ((byte & 128) == 0)
16222         return buf - begin;
16223     }
16224 }
16225 
16226 static void
16227 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
16228 {
16229   switch (lang)
16230     {
16231     case DW_LANG_C89:
16232     case DW_LANG_C99:
16233     case DW_LANG_C:
16234     case DW_LANG_UPC:
16235       cu->language = language_c;
16236       break;
16237     case DW_LANG_C_plus_plus:
16238       cu->language = language_cplus;
16239       break;
16240     case DW_LANG_D:
16241       cu->language = language_d;
16242       break;
16243     case DW_LANG_Fortran77:
16244     case DW_LANG_Fortran90:
16245     case DW_LANG_Fortran95:
16246       cu->language = language_fortran;
16247       break;
16248     case DW_LANG_Go:
16249       cu->language = language_go;
16250       break;
16251     case DW_LANG_Mips_Assembler:
16252       cu->language = language_asm;
16253       break;
16254     case DW_LANG_Java:
16255       cu->language = language_java;
16256       break;
16257     case DW_LANG_Ada83:
16258     case DW_LANG_Ada95:
16259       cu->language = language_ada;
16260       break;
16261     case DW_LANG_Modula2:
16262       cu->language = language_m2;
16263       break;
16264     case DW_LANG_Pascal83:
16265       cu->language = language_pascal;
16266       break;
16267     case DW_LANG_ObjC:
16268       cu->language = language_objc;
16269       break;
16270     case DW_LANG_Cobol74:
16271     case DW_LANG_Cobol85:
16272     default:
16273       cu->language = language_minimal;
16274       break;
16275     }
16276   cu->language_defn = language_def (cu->language);
16277 }
16278 
16279 /* Return the named attribute or NULL if not there.  */
16280 
16281 static struct attribute *
16282 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
16283 {
16284   for (;;)
16285     {
16286       unsigned int i;
16287       struct attribute *spec = NULL;
16288 
16289       for (i = 0; i < die->num_attrs; ++i)
16290         {
16291           if (die->attrs[i].name == name)
16292             return &die->attrs[i];
16293           if (die->attrs[i].name == DW_AT_specification
16294               || die->attrs[i].name == DW_AT_abstract_origin)
16295             spec = &die->attrs[i];
16296         }
16297 
16298       if (!spec)
16299         break;
16300 
16301       die = follow_die_ref (die, spec, &cu);
16302     }
16303 
16304   return NULL;
16305 }
16306 
16307 /* Return the named attribute or NULL if not there,
16308    but do not follow DW_AT_specification, etc.
16309    This is for use in contexts where we're reading .debug_types dies.
16310    Following DW_AT_specification, DW_AT_abstract_origin will take us
16311    back up the chain, and we want to go down.  */
16312 
16313 static struct attribute *
16314 dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
16315 {
16316   unsigned int i;
16317 
16318   for (i = 0; i < die->num_attrs; ++i)
16319     if (die->attrs[i].name == name)
16320       return &die->attrs[i];
16321 
16322   return NULL;
16323 }
16324 
16325 /* Return non-zero iff the attribute NAME is defined for the given DIE,
16326    and holds a non-zero value.  This function should only be used for
16327    DW_FORM_flag or DW_FORM_flag_present attributes.  */
16328 
16329 static int
16330 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
16331 {
16332   struct attribute *attr = dwarf2_attr (die, name, cu);
16333 
16334   return (attr && DW_UNSND (attr));
16335 }
16336 
16337 static int
16338 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
16339 {
16340   /* A DIE is a declaration if it has a DW_AT_declaration attribute
16341      which value is non-zero.  However, we have to be careful with
16342      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
16343      (via dwarf2_flag_true_p) follows this attribute.  So we may
16344      end up accidently finding a declaration attribute that belongs
16345      to a different DIE referenced by the specification attribute,
16346      even though the given DIE does not have a declaration attribute.  */
16347   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
16348           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
16349 }
16350 
16351 /* Return the die giving the specification for DIE, if there is
16352    one.  *SPEC_CU is the CU containing DIE on input, and the CU
16353    containing the return value on output.  If there is no
16354    specification, but there is an abstract origin, that is
16355    returned.  */
16356 
16357 static struct die_info *
16358 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
16359 {
16360   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
16361                                              *spec_cu);
16362 
16363   if (spec_attr == NULL)
16364     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
16365 
16366   if (spec_attr == NULL)
16367     return NULL;
16368   else
16369     return follow_die_ref (die, spec_attr, spec_cu);
16370 }
16371 
16372 /* Free the line_header structure *LH, and any arrays and strings it
16373    refers to.
16374    NOTE: This is also used as a "cleanup" function.  */
16375 
16376 static void
16377 free_line_header (struct line_header *lh)
16378 {
16379   if (lh->standard_opcode_lengths)
16380     xfree (lh->standard_opcode_lengths);
16381 
16382   /* Remember that all the lh->file_names[i].name pointers are
16383      pointers into debug_line_buffer, and don't need to be freed.  */
16384   if (lh->file_names)
16385     xfree (lh->file_names);
16386 
16387   /* Similarly for the include directory names.  */
16388   if (lh->include_dirs)
16389     xfree (lh->include_dirs);
16390 
16391   xfree (lh);
16392 }
16393 
16394 /* Add an entry to LH's include directory table.  */
16395 
16396 static void
16397 add_include_dir (struct line_header *lh, const char *include_dir)
16398 {
16399   /* Grow the array if necessary.  */
16400   if (lh->include_dirs_size == 0)
16401     {
16402       lh->include_dirs_size = 1; /* for testing */
16403       lh->include_dirs = xmalloc (lh->include_dirs_size
16404                                   * sizeof (*lh->include_dirs));
16405     }
16406   else if (lh->num_include_dirs >= lh->include_dirs_size)
16407     {
16408       lh->include_dirs_size *= 2;
16409       lh->include_dirs = xrealloc (lh->include_dirs,
16410                                    (lh->include_dirs_size
16411                                     * sizeof (*lh->include_dirs)));
16412     }
16413 
16414   lh->include_dirs[lh->num_include_dirs++] = include_dir;
16415 }
16416 
16417 /* Add an entry to LH's file name table.  */
16418 
16419 static void
16420 add_file_name (struct line_header *lh,
16421                const char *name,
16422                unsigned int dir_index,
16423                unsigned int mod_time,
16424                unsigned int length)
16425 {
16426   struct file_entry *fe;
16427 
16428   /* Grow the array if necessary.  */
16429   if (lh->file_names_size == 0)
16430     {
16431       lh->file_names_size = 1; /* for testing */
16432       lh->file_names = xmalloc (lh->file_names_size
16433                                 * sizeof (*lh->file_names));
16434     }
16435   else if (lh->num_file_names >= lh->file_names_size)
16436     {
16437       lh->file_names_size *= 2;
16438       lh->file_names = xrealloc (lh->file_names,
16439                                  (lh->file_names_size
16440                                   * sizeof (*lh->file_names)));
16441     }
16442 
16443   fe = &lh->file_names[lh->num_file_names++];
16444   fe->name = name;
16445   fe->dir_index = dir_index;
16446   fe->mod_time = mod_time;
16447   fe->length = length;
16448   fe->included_p = 0;
16449   fe->symtab = NULL;
16450 }
16451 
16452 /* A convenience function to find the proper .debug_line section for a
16453    CU.  */
16454 
16455 static struct dwarf2_section_info *
16456 get_debug_line_section (struct dwarf2_cu *cu)
16457 {
16458   struct dwarf2_section_info *section;
16459 
16460   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
16461      DWO file.  */
16462   if (cu->dwo_unit && cu->per_cu->is_debug_types)
16463     section = &cu->dwo_unit->dwo_file->sections.line;
16464   else if (cu->per_cu->is_dwz)
16465     {
16466       struct dwz_file *dwz = dwarf2_get_dwz_file ();
16467 
16468       section = &dwz->line;
16469     }
16470   else
16471     section = &dwarf2_per_objfile->line;
16472 
16473   return section;
16474 }
16475 
16476 /* Read the statement program header starting at OFFSET in
16477    .debug_line, or .debug_line.dwo.  Return a pointer
16478    to a struct line_header, allocated using xmalloc.
16479 
16480    NOTE: the strings in the include directory and file name tables of
16481    the returned object point into the dwarf line section buffer,
16482    and must not be freed.  */
16483 
16484 static struct line_header *
16485 dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
16486 {
16487   struct cleanup *back_to;
16488   struct line_header *lh;
16489   const gdb_byte *line_ptr;
16490   unsigned int bytes_read, offset_size;
16491   int i;
16492   const char *cur_dir, *cur_file;
16493   struct dwarf2_section_info *section;
16494   bfd *abfd;
16495 
16496   section = get_debug_line_section (cu);
16497   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
16498   if (section->buffer == NULL)
16499     {
16500       if (cu->dwo_unit && cu->per_cu->is_debug_types)
16501         complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
16502       else
16503         complaint (&symfile_complaints, _("missing .debug_line section"));
16504       return 0;
16505     }
16506 
16507   /* We can't do this until we know the section is non-empty.
16508      Only then do we know we have such a section.  */
16509   abfd = get_section_bfd_owner (section);
16510 
16511   /* Make sure that at least there's room for the total_length field.
16512      That could be 12 bytes long, but we're just going to fudge that.  */
16513   if (offset + 4 >= section->size)
16514     {
16515       dwarf2_statement_list_fits_in_line_number_section_complaint ();
16516       return 0;
16517     }
16518 
16519   lh = xmalloc (sizeof (*lh));
16520   memset (lh, 0, sizeof (*lh));
16521   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
16522                           (void *) lh);
16523 
16524   line_ptr = section->buffer + offset;
16525 
16526   /* Read in the header.  */
16527   lh->total_length =
16528     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
16529                                             &bytes_read, &offset_size);
16530   line_ptr += bytes_read;
16531   if (line_ptr + lh->total_length > (section->buffer + section->size))
16532     {
16533       dwarf2_statement_list_fits_in_line_number_section_complaint ();
16534       do_cleanups (back_to);
16535       return 0;
16536     }
16537   lh->statement_program_end = line_ptr + lh->total_length;
16538   lh->version = read_2_bytes (abfd, line_ptr);
16539   line_ptr += 2;
16540   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
16541   line_ptr += offset_size;
16542   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
16543   line_ptr += 1;
16544   if (lh->version >= 4)
16545     {
16546       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
16547       line_ptr += 1;
16548     }
16549   else
16550     lh->maximum_ops_per_instruction = 1;
16551 
16552   if (lh->maximum_ops_per_instruction == 0)
16553     {
16554       lh->maximum_ops_per_instruction = 1;
16555       complaint (&symfile_complaints,
16556                  _("invalid maximum_ops_per_instruction "
16557                    "in `.debug_line' section"));
16558     }
16559 
16560   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
16561   line_ptr += 1;
16562   lh->line_base = read_1_signed_byte (abfd, line_ptr);
16563   line_ptr += 1;
16564   lh->line_range = read_1_byte (abfd, line_ptr);
16565   line_ptr += 1;
16566   lh->opcode_base = read_1_byte (abfd, line_ptr);
16567   line_ptr += 1;
16568   lh->standard_opcode_lengths
16569     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
16570 
16571   lh->standard_opcode_lengths[0] = 1;  /* This should never be used anyway.  */
16572   for (i = 1; i < lh->opcode_base; ++i)
16573     {
16574       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
16575       line_ptr += 1;
16576     }
16577 
16578   /* Read directory table.  */
16579   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
16580     {
16581       line_ptr += bytes_read;
16582       add_include_dir (lh, cur_dir);
16583     }
16584   line_ptr += bytes_read;
16585 
16586   /* Read file name table.  */
16587   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
16588     {
16589       unsigned int dir_index, mod_time, length;
16590 
16591       line_ptr += bytes_read;
16592       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16593       line_ptr += bytes_read;
16594       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16595       line_ptr += bytes_read;
16596       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16597       line_ptr += bytes_read;
16598 
16599       add_file_name (lh, cur_file, dir_index, mod_time, length);
16600     }
16601   line_ptr += bytes_read;
16602   lh->statement_program_start = line_ptr;
16603 
16604   if (line_ptr > (section->buffer + section->size))
16605     complaint (&symfile_complaints,
16606                _("line number info header doesn't "
16607                  "fit in `.debug_line' section"));
16608 
16609   discard_cleanups (back_to);
16610   return lh;
16611 }
16612 
16613 /* Subroutine of dwarf_decode_lines to simplify it.
16614    Return the file name of the psymtab for included file FILE_INDEX
16615    in line header LH of PST.
16616    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
16617    If space for the result is malloc'd, it will be freed by a cleanup.
16618    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.
16619 
16620    The function creates dangling cleanup registration.  */
16621 
16622 static const char *
16623 psymtab_include_file_name (const struct line_header *lh, int file_index,
16624                            const struct partial_symtab *pst,
16625                            const char *comp_dir)
16626 {
16627   const struct file_entry fe = lh->file_names [file_index];
16628   const char *include_name = fe.name;
16629   const char *include_name_to_compare = include_name;
16630   const char *dir_name = NULL;
16631   const char *pst_filename;
16632   char *copied_name = NULL;
16633   int file_is_pst;
16634 
16635   if (fe.dir_index)
16636     dir_name = lh->include_dirs[fe.dir_index - 1];
16637 
16638   if (!IS_ABSOLUTE_PATH (include_name)
16639       && (dir_name != NULL || comp_dir != NULL))
16640     {
16641       /* Avoid creating a duplicate psymtab for PST.
16642          We do this by comparing INCLUDE_NAME and PST_FILENAME.
16643          Before we do the comparison, however, we need to account
16644          for DIR_NAME and COMP_DIR.
16645          First prepend dir_name (if non-NULL).  If we still don't
16646          have an absolute path prepend comp_dir (if non-NULL).
16647          However, the directory we record in the include-file's
16648          psymtab does not contain COMP_DIR (to match the
16649          corresponding symtab(s)).
16650 
16651          Example:
16652 
16653          bash$ cd /tmp
16654          bash$ gcc -g ./hello.c
16655          include_name = "hello.c"
16656          dir_name = "."
16657          DW_AT_comp_dir = comp_dir = "/tmp"
16658          DW_AT_name = "./hello.c"  */
16659 
16660       if (dir_name != NULL)
16661         {
16662           char *tem = concat (dir_name, SLASH_STRING,
16663                               include_name, (char *)NULL);
16664 
16665           make_cleanup (xfree, tem);
16666           include_name = tem;
16667           include_name_to_compare = include_name;
16668         }
16669       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
16670         {
16671           char *tem = concat (comp_dir, SLASH_STRING,
16672                               include_name, (char *)NULL);
16673 
16674           make_cleanup (xfree, tem);
16675           include_name_to_compare = tem;
16676         }
16677     }
16678 
16679   pst_filename = pst->filename;
16680   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
16681     {
16682       copied_name = concat (pst->dirname, SLASH_STRING,
16683                             pst_filename, (char *)NULL);
16684       pst_filename = copied_name;
16685     }
16686 
16687   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
16688 
16689   if (copied_name != NULL)
16690     xfree (copied_name);
16691 
16692   if (file_is_pst)
16693     return NULL;
16694   return include_name;
16695 }
16696 
16697 /* Ignore this record_line request.  */
16698 
16699 static void
16700 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
16701 {
16702   return;
16703 }
16704 
16705 /* Subroutine of dwarf_decode_lines to simplify it.
16706    Process the line number information in LH.  */
16707 
16708 static void
16709 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
16710                       struct dwarf2_cu *cu, struct partial_symtab *pst)
16711 {
16712   const gdb_byte *line_ptr, *extended_end;
16713   const gdb_byte *line_end;
16714   unsigned int bytes_read, extended_len;
16715   unsigned char op_code, extended_op, adj_opcode;
16716   CORE_ADDR baseaddr;
16717   struct objfile *objfile = cu->objfile;
16718   bfd *abfd = objfile->obfd;
16719   struct gdbarch *gdbarch = get_objfile_arch (objfile);
16720   const int decode_for_pst_p = (pst != NULL);
16721   struct subfile *last_subfile = NULL;
16722   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
16723     = record_line;
16724 
16725   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16726 
16727   line_ptr = lh->statement_program_start;
16728   line_end = lh->statement_program_end;
16729 
16730   /* Read the statement sequences until there's nothing left.  */
16731   while (line_ptr < line_end)
16732     {
16733       /* state machine registers  */
16734       CORE_ADDR address = 0;
16735       unsigned int file = 1;
16736       unsigned int line = 1;
16737       unsigned int column = 0;
16738       int is_stmt = lh->default_is_stmt;
16739       int basic_block = 0;
16740       int end_sequence = 0;
16741       CORE_ADDR addr;
16742       unsigned char op_index = 0;
16743 
16744       if (!decode_for_pst_p && lh->num_file_names >= file)
16745         {
16746           /* Start a subfile for the current file of the state machine.  */
16747           /* lh->include_dirs and lh->file_names are 0-based, but the
16748              directory and file name numbers in the statement program
16749              are 1-based.  */
16750           struct file_entry *fe = &lh->file_names[file - 1];
16751           const char *dir = NULL;
16752 
16753           if (fe->dir_index)
16754             dir = lh->include_dirs[fe->dir_index - 1];
16755 
16756           dwarf2_start_subfile (fe->name, dir, comp_dir);
16757         }
16758 
16759       /* Decode the table.  */
16760       while (!end_sequence)
16761         {
16762           op_code = read_1_byte (abfd, line_ptr);
16763           line_ptr += 1;
16764           if (line_ptr > line_end)
16765             {
16766               dwarf2_debug_line_missing_end_sequence_complaint ();
16767               break;
16768             }
16769 
16770           if (op_code >= lh->opcode_base)
16771             {
16772               /* Special operand.  */
16773               adj_opcode = op_code - lh->opcode_base;
16774               address += (((op_index + (adj_opcode / lh->line_range))
16775                            / lh->maximum_ops_per_instruction)
16776                           * lh->minimum_instruction_length);
16777               op_index = ((op_index + (adj_opcode / lh->line_range))
16778                           % lh->maximum_ops_per_instruction);
16779               line += lh->line_base + (adj_opcode % lh->line_range);
16780               if (lh->num_file_names < file || file == 0)
16781                 dwarf2_debug_line_missing_file_complaint ();
16782               /* For now we ignore lines not starting on an
16783                  instruction boundary.  */
16784               else if (op_index == 0)
16785                 {
16786                   lh->file_names[file - 1].included_p = 1;
16787                   if (!decode_for_pst_p && is_stmt)
16788                     {
16789                       if (last_subfile != current_subfile)
16790                         {
16791                           addr = gdbarch_addr_bits_remove (gdbarch, address);
16792                           if (last_subfile)
16793                             (*p_record_line) (last_subfile, 0, addr);
16794                           last_subfile = current_subfile;
16795                         }
16796                       /* Append row to matrix using current values.  */
16797                       addr = gdbarch_addr_bits_remove (gdbarch, address);
16798                       (*p_record_line) (current_subfile, line, addr);
16799                     }
16800                 }
16801               basic_block = 0;
16802             }
16803           else switch (op_code)
16804             {
16805             case DW_LNS_extended_op:
16806               extended_len = read_unsigned_leb128 (abfd, line_ptr,
16807                                                    &bytes_read);
16808               line_ptr += bytes_read;
16809               extended_end = line_ptr + extended_len;
16810               extended_op = read_1_byte (abfd, line_ptr);
16811               line_ptr += 1;
16812               switch (extended_op)
16813                 {
16814                 case DW_LNE_end_sequence:
16815                   p_record_line = record_line;
16816                   end_sequence = 1;
16817                   break;
16818                 case DW_LNE_set_address:
16819                   address = read_address (abfd, line_ptr, cu, &bytes_read);
16820 
16821                   if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
16822                     {
16823                       /* This line table is for a function which has been
16824                          GCd by the linker.  Ignore it.  PR gdb/12528 */
16825 
16826                       long line_offset
16827                         = line_ptr - get_debug_line_section (cu)->buffer;
16828 
16829                       complaint (&symfile_complaints,
16830                                  _(".debug_line address at offset 0x%lx is 0 "
16831                                    "[in module %s]"),
16832                                  line_offset, objfile_name (objfile));
16833                       p_record_line = noop_record_line;
16834                     }
16835 
16836                   op_index = 0;
16837                   line_ptr += bytes_read;
16838                   address += baseaddr;
16839                   break;
16840                 case DW_LNE_define_file:
16841                   {
16842                     const char *cur_file;
16843                     unsigned int dir_index, mod_time, length;
16844 
16845                     cur_file = read_direct_string (abfd, line_ptr,
16846                                                    &bytes_read);
16847                     line_ptr += bytes_read;
16848                     dir_index =
16849                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16850                     line_ptr += bytes_read;
16851                     mod_time =
16852                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16853                     line_ptr += bytes_read;
16854                     length =
16855                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16856                     line_ptr += bytes_read;
16857                     add_file_name (lh, cur_file, dir_index, mod_time, length);
16858                   }
16859                   break;
16860                 case DW_LNE_set_discriminator:
16861                   /* The discriminator is not interesting to the debugger;
16862                      just ignore it.  */
16863                   line_ptr = extended_end;
16864                   break;
16865                 default:
16866                   complaint (&symfile_complaints,
16867                              _("mangled .debug_line section"));
16868                   return;
16869                 }
16870               /* Make sure that we parsed the extended op correctly.  If e.g.
16871                  we expected a different address size than the producer used,
16872                  we may have read the wrong number of bytes.  */
16873               if (line_ptr != extended_end)
16874                 {
16875                   complaint (&symfile_complaints,
16876                              _("mangled .debug_line section"));
16877                   return;
16878                 }
16879               break;
16880             case DW_LNS_copy:
16881               if (lh->num_file_names < file || file == 0)
16882                 dwarf2_debug_line_missing_file_complaint ();
16883               else
16884                 {
16885                   lh->file_names[file - 1].included_p = 1;
16886                   if (!decode_for_pst_p && is_stmt)
16887                     {
16888                       if (last_subfile != current_subfile)
16889                         {
16890                           addr = gdbarch_addr_bits_remove (gdbarch, address);
16891                           if (last_subfile)
16892                             (*p_record_line) (last_subfile, 0, addr);
16893                           last_subfile = current_subfile;
16894                         }
16895                       addr = gdbarch_addr_bits_remove (gdbarch, address);
16896                       (*p_record_line) (current_subfile, line, addr);
16897                     }
16898                 }
16899               basic_block = 0;
16900               break;
16901             case DW_LNS_advance_pc:
16902               {
16903                 CORE_ADDR adjust
16904                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16905 
16906                 address += (((op_index + adjust)
16907                              / lh->maximum_ops_per_instruction)
16908                             * lh->minimum_instruction_length);
16909                 op_index = ((op_index + adjust)
16910                             % lh->maximum_ops_per_instruction);
16911                 line_ptr += bytes_read;
16912               }
16913               break;
16914             case DW_LNS_advance_line:
16915               line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
16916               line_ptr += bytes_read;
16917               break;
16918             case DW_LNS_set_file:
16919               {
16920                 /* The arrays lh->include_dirs and lh->file_names are
16921                    0-based, but the directory and file name numbers in
16922                    the statement program are 1-based.  */
16923                 struct file_entry *fe;
16924                 const char *dir = NULL;
16925 
16926                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16927                 line_ptr += bytes_read;
16928                 if (lh->num_file_names < file || file == 0)
16929                   dwarf2_debug_line_missing_file_complaint ();
16930                 else
16931                   {
16932                     fe = &lh->file_names[file - 1];
16933                     if (fe->dir_index)
16934                       dir = lh->include_dirs[fe->dir_index - 1];
16935                     if (!decode_for_pst_p)
16936                       {
16937                         last_subfile = current_subfile;
16938                         dwarf2_start_subfile (fe->name, dir, comp_dir);
16939                       }
16940                   }
16941               }
16942               break;
16943             case DW_LNS_set_column:
16944               column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16945               line_ptr += bytes_read;
16946               break;
16947             case DW_LNS_negate_stmt:
16948               is_stmt = (!is_stmt);
16949               break;
16950             case DW_LNS_set_basic_block:
16951               basic_block = 1;
16952               break;
16953             /* Add to the address register of the state machine the
16954                address increment value corresponding to special opcode
16955                255.  I.e., this value is scaled by the minimum
16956                instruction length since special opcode 255 would have
16957                scaled the increment.  */
16958             case DW_LNS_const_add_pc:
16959               {
16960                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
16961 
16962                 address += (((op_index + adjust)
16963                              / lh->maximum_ops_per_instruction)
16964                             * lh->minimum_instruction_length);
16965                 op_index = ((op_index + adjust)
16966                             % lh->maximum_ops_per_instruction);
16967               }
16968               break;
16969             case DW_LNS_fixed_advance_pc:
16970               address += read_2_bytes (abfd, line_ptr);
16971               op_index = 0;
16972               line_ptr += 2;
16973               break;
16974             default:
16975               {
16976                 /* Unknown standard opcode, ignore it.  */
16977                 int i;
16978 
16979                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
16980                   {
16981                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
16982                     line_ptr += bytes_read;
16983                   }
16984               }
16985             }
16986         }
16987       if (lh->num_file_names < file || file == 0)
16988         dwarf2_debug_line_missing_file_complaint ();
16989       else
16990         {
16991           lh->file_names[file - 1].included_p = 1;
16992           if (!decode_for_pst_p)
16993             {
16994               addr = gdbarch_addr_bits_remove (gdbarch, address);
16995               (*p_record_line) (current_subfile, 0, addr);
16996             }
16997         }
16998     }
16999 }
17000 
17001 /* Decode the Line Number Program (LNP) for the given line_header
17002    structure and CU.  The actual information extracted and the type
17003    of structures created from the LNP depends on the value of PST.
17004 
17005    1. If PST is NULL, then this procedure uses the data from the program
17006       to create all necessary symbol tables, and their linetables.
17007 
17008    2. If PST is not NULL, this procedure reads the program to determine
17009       the list of files included by the unit represented by PST, and
17010       builds all the associated partial symbol tables.
17011 
17012    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
17013    It is used for relative paths in the line table.
17014    NOTE: When processing partial symtabs (pst != NULL),
17015    comp_dir == pst->dirname.
17016 
17017    NOTE: It is important that psymtabs have the same file name (via strcmp)
17018    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
17019    symtab we don't use it in the name of the psymtabs we create.
17020    E.g. expand_line_sal requires this when finding psymtabs to expand.
17021    A good testcase for this is mb-inline.exp.  */
17022 
17023 static void
17024 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
17025                     struct dwarf2_cu *cu, struct partial_symtab *pst,
17026                     int want_line_info)
17027 {
17028   struct objfile *objfile = cu->objfile;
17029   const int decode_for_pst_p = (pst != NULL);
17030   struct subfile *first_subfile = current_subfile;
17031 
17032   if (want_line_info)
17033     dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
17034 
17035   if (decode_for_pst_p)
17036     {
17037       int file_index;
17038 
17039       /* Now that we're done scanning the Line Header Program, we can
17040          create the psymtab of each included file.  */
17041       for (file_index = 0; file_index < lh->num_file_names; file_index++)
17042         if (lh->file_names[file_index].included_p == 1)
17043           {
17044             const char *include_name =
17045               psymtab_include_file_name (lh, file_index, pst, comp_dir);
17046             if (include_name != NULL)
17047               dwarf2_create_include_psymtab (include_name, pst, objfile);
17048           }
17049     }
17050   else
17051     {
17052       /* Make sure a symtab is created for every file, even files
17053          which contain only variables (i.e. no code with associated
17054          line numbers).  */
17055       int i;
17056 
17057       for (i = 0; i < lh->num_file_names; i++)
17058         {
17059           const char *dir = NULL;
17060           struct file_entry *fe;
17061 
17062           fe = &lh->file_names[i];
17063           if (fe->dir_index)
17064             dir = lh->include_dirs[fe->dir_index - 1];
17065           dwarf2_start_subfile (fe->name, dir, comp_dir);
17066 
17067           /* Skip the main file; we don't need it, and it must be
17068              allocated last, so that it will show up before the
17069              non-primary symtabs in the objfile's symtab list.  */
17070           if (current_subfile == first_subfile)
17071             continue;
17072 
17073           if (current_subfile->symtab == NULL)
17074             current_subfile->symtab = allocate_symtab (current_subfile->name,
17075                                                        objfile);
17076           fe->symtab = current_subfile->symtab;
17077         }
17078     }
17079 }
17080 
17081 /* Start a subfile for DWARF.  FILENAME is the name of the file and
17082    DIRNAME the name of the source directory which contains FILENAME
17083    or NULL if not known.  COMP_DIR is the compilation directory for the
17084    linetable's compilation unit or NULL if not known.
17085    This routine tries to keep line numbers from identical absolute and
17086    relative file names in a common subfile.
17087 
17088    Using the `list' example from the GDB testsuite, which resides in
17089    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
17090    of /srcdir/list0.c yields the following debugging information for list0.c:
17091 
17092    DW_AT_name:          /srcdir/list0.c
17093    DW_AT_comp_dir:              /compdir
17094    files.files[0].name: list0.h
17095    files.files[0].dir:  /srcdir
17096    files.files[1].name: list0.c
17097    files.files[1].dir:  /srcdir
17098 
17099    The line number information for list0.c has to end up in a single
17100    subfile, so that `break /srcdir/list0.c:1' works as expected.
17101    start_subfile will ensure that this happens provided that we pass the
17102    concatenation of files.files[1].dir and files.files[1].name as the
17103    subfile's name.  */
17104 
17105 static void
17106 dwarf2_start_subfile (const char *filename, const char *dirname,
17107                       const char *comp_dir)
17108 {
17109   char *copy = NULL;
17110 
17111   /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
17112      `start_symtab' will always pass the contents of DW_AT_comp_dir as
17113      second argument to start_subfile.  To be consistent, we do the
17114      same here.  In order not to lose the line information directory,
17115      we concatenate it to the filename when it makes sense.
17116      Note that the Dwarf3 standard says (speaking of filenames in line
17117      information): ``The directory index is ignored for file names
17118      that represent full path names''.  Thus ignoring dirname in the
17119      `else' branch below isn't an issue.  */
17120 
17121   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
17122     {
17123       copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
17124       filename = copy;
17125     }
17126 
17127   start_subfile (filename, comp_dir);
17128 
17129   if (copy != NULL)
17130     xfree (copy);
17131 }
17132 
17133 /* Start a symtab for DWARF.
17134    NAME, COMP_DIR, LOW_PC are passed to start_symtab.  */
17135 
17136 static void
17137 dwarf2_start_symtab (struct dwarf2_cu *cu,
17138                      const char *name, const char *comp_dir, CORE_ADDR low_pc)
17139 {
17140   start_symtab (name, comp_dir, low_pc);
17141   record_debugformat ("DWARF 2");
17142   record_producer (cu->producer);
17143 
17144   /* We assume that we're processing GCC output.  */
17145   processing_gcc_compilation = 2;
17146 
17147   cu->processing_has_namespace_info = 0;
17148 }
17149 
17150 static void
17151 var_decode_location (struct attribute *attr, struct symbol *sym,
17152                      struct dwarf2_cu *cu)
17153 {
17154   struct objfile *objfile = cu->objfile;
17155   struct comp_unit_head *cu_header = &cu->header;
17156 
17157   /* NOTE drow/2003-01-30: There used to be a comment and some special
17158      code here to turn a symbol with DW_AT_external and a
17159      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
17160      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
17161      with some versions of binutils) where shared libraries could have
17162      relocations against symbols in their debug information - the
17163      minimal symbol would have the right address, but the debug info
17164      would not.  It's no longer necessary, because we will explicitly
17165      apply relocations when we read in the debug information now.  */
17166 
17167   /* A DW_AT_location attribute with no contents indicates that a
17168      variable has been optimized away.  */
17169   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
17170     {
17171       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
17172       return;
17173     }
17174 
17175   /* Handle one degenerate form of location expression specially, to
17176      preserve GDB's previous behavior when section offsets are
17177      specified.  If this is just a DW_OP_addr or DW_OP_GNU_addr_index
17178      then mark this symbol as LOC_STATIC.  */
17179 
17180   if (attr_form_is_block (attr)
17181       && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
17182            && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
17183           || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
17184               && (DW_BLOCK (attr)->size
17185                   == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
17186     {
17187       unsigned int dummy;
17188 
17189       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
17190         SYMBOL_VALUE_ADDRESS (sym) =
17191           read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
17192       else
17193         SYMBOL_VALUE_ADDRESS (sym) =
17194           read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
17195       SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
17196       fixup_symbol_section (sym, objfile);
17197       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
17198                                               SYMBOL_SECTION (sym));
17199       return;
17200     }
17201 
17202   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
17203      expression evaluator, and use LOC_COMPUTED only when necessary
17204      (i.e. when the value of a register or memory location is
17205      referenced, or a thread-local block, etc.).  Then again, it might
17206      not be worthwhile.  I'm assuming that it isn't unless performance
17207      or memory numbers show me otherwise.  */
17208 
17209   dwarf2_symbol_mark_computed (attr, sym, cu, 0);
17210 
17211   if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
17212     cu->has_loclist = 1;
17213 }
17214 
17215 /* Given a pointer to a DWARF information entry, figure out if we need
17216    to make a symbol table entry for it, and if so, create a new entry
17217    and return a pointer to it.
17218    If TYPE is NULL, determine symbol type from the die, otherwise
17219    used the passed type.
17220    If SPACE is not NULL, use it to hold the new symbol.  If it is
17221    NULL, allocate a new symbol on the objfile's obstack.  */
17222 
17223 static struct symbol *
17224 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
17225                  struct symbol *space)
17226 {
17227   struct objfile *objfile = cu->objfile;
17228   struct symbol *sym = NULL;
17229   const char *name;
17230   struct attribute *attr = NULL;
17231   struct attribute *attr2 = NULL;
17232   CORE_ADDR baseaddr;
17233   struct pending **list_to_add = NULL;
17234 
17235   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
17236 
17237   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
17238 
17239   name = dwarf2_name (die, cu);
17240   if (name)
17241     {
17242       const char *linkagename;
17243       int suppress_add = 0;
17244 
17245       if (space)
17246         sym = space;
17247       else
17248         sym = allocate_symbol (objfile);
17249       OBJSTAT (objfile, n_syms++);
17250 
17251       /* Cache this symbol's name and the name's demangled form (if any).  */
17252       SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
17253       linkagename = dwarf2_physname (name, die, cu);
17254       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
17255 
17256       /* Fortran does not have mangling standard and the mangling does differ
17257          between gfortran, iFort etc.  */
17258       if (cu->language == language_fortran
17259           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
17260         symbol_set_demangled_name (&(sym->ginfo),
17261                                    dwarf2_full_name (name, die, cu),
17262                                    NULL);
17263 
17264       /* Default assumptions.
17265          Use the passed type or decode it from the die.  */
17266       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
17267       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
17268       if (type != NULL)
17269         SYMBOL_TYPE (sym) = type;
17270       else
17271         SYMBOL_TYPE (sym) = die_type (die, cu);
17272       attr = dwarf2_attr (die,
17273                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
17274                           cu);
17275       if (attr)
17276         {
17277           SYMBOL_LINE (sym) = DW_UNSND (attr);
17278         }
17279 
17280       attr = dwarf2_attr (die,
17281                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
17282                           cu);
17283       if (attr)
17284         {
17285           int file_index = DW_UNSND (attr);
17286 
17287           if (cu->line_header == NULL
17288               || file_index > cu->line_header->num_file_names)
17289             complaint (&symfile_complaints,
17290                        _("file index out of range"));
17291           else if (file_index > 0)
17292             {
17293               struct file_entry *fe;
17294 
17295               fe = &cu->line_header->file_names[file_index - 1];
17296               SYMBOL_SYMTAB (sym) = fe->symtab;
17297             }
17298         }
17299 
17300       switch (die->tag)
17301         {
17302         case DW_TAG_label:
17303           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
17304           if (attr)
17305             {
17306               SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
17307             }
17308           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
17309           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
17310           SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
17311           add_symbol_to_list (sym, cu->list_in_scope);
17312           break;
17313         case DW_TAG_subprogram:
17314           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
17315              finish_block.  */
17316           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
17317           attr2 = dwarf2_attr (die, DW_AT_external, cu);
17318           if ((attr2 && (DW_UNSND (attr2) != 0))
17319               || cu->language == language_ada)
17320             {
17321               /* Subprograms marked external are stored as a global symbol.
17322                  Ada subprograms, whether marked external or not, are always
17323                  stored as a global symbol, because we want to be able to
17324                  access them globally.  For instance, we want to be able
17325                  to break on a nested subprogram without having to
17326                  specify the context.  */
17327               list_to_add = &global_symbols;
17328             }
17329           else
17330             {
17331               list_to_add = cu->list_in_scope;
17332             }
17333           break;
17334         case DW_TAG_inlined_subroutine:
17335           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
17336              finish_block.  */
17337           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
17338           SYMBOL_INLINED (sym) = 1;
17339           list_to_add = cu->list_in_scope;
17340           break;
17341         case DW_TAG_template_value_param:
17342           suppress_add = 1;
17343           /* Fall through.  */
17344         case DW_TAG_constant:
17345         case DW_TAG_variable:
17346         case DW_TAG_member:
17347           /* Compilation with minimal debug info may result in
17348              variables with missing type entries.  Change the
17349              misleading `void' type to something sensible.  */
17350           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
17351             SYMBOL_TYPE (sym)
17352               = objfile_type (objfile)->nodebug_data_symbol;
17353 
17354           attr = dwarf2_attr (die, DW_AT_const_value, cu);
17355           /* In the case of DW_TAG_member, we should only be called for
17356              static const members.  */
17357           if (die->tag == DW_TAG_member)
17358             {
17359               /* dwarf2_add_field uses die_is_declaration,
17360                  so we do the same.  */
17361               gdb_assert (die_is_declaration (die, cu));
17362               gdb_assert (attr);
17363             }
17364           if (attr)
17365             {
17366               dwarf2_const_value (attr, sym, cu);
17367               attr2 = dwarf2_attr (die, DW_AT_external, cu);
17368               if (!suppress_add)
17369                 {
17370                   if (attr2 && (DW_UNSND (attr2) != 0))
17371                     list_to_add = &global_symbols;
17372                   else
17373                     list_to_add = cu->list_in_scope;
17374                 }
17375               break;
17376             }
17377           attr = dwarf2_attr (die, DW_AT_location, cu);
17378           if (attr)
17379             {
17380               var_decode_location (attr, sym, cu);
17381               attr2 = dwarf2_attr (die, DW_AT_external, cu);
17382 
17383               /* Fortran explicitly imports any global symbols to the local
17384                  scope by DW_TAG_common_block.  */
17385               if (cu->language == language_fortran && die->parent
17386                   && die->parent->tag == DW_TAG_common_block)
17387                 attr2 = NULL;
17388 
17389               if (SYMBOL_CLASS (sym) == LOC_STATIC
17390                   && SYMBOL_VALUE_ADDRESS (sym) == 0
17391                   && !dwarf2_per_objfile->has_section_at_zero)
17392                 {
17393                   /* When a static variable is eliminated by the linker,
17394                      the corresponding debug information is not stripped
17395                      out, but the variable address is set to null;
17396                      do not add such variables into symbol table.  */
17397                 }
17398               else if (attr2 && (DW_UNSND (attr2) != 0))
17399                 {
17400                   /* Workaround gfortran PR debug/40040 - it uses
17401                      DW_AT_location for variables in -fPIC libraries which may
17402                      get overriden by other libraries/executable and get
17403                      a different address.  Resolve it by the minimal symbol
17404                      which may come from inferior's executable using copy
17405                      relocation.  Make this workaround only for gfortran as for
17406                      other compilers GDB cannot guess the minimal symbol
17407                      Fortran mangling kind.  */
17408                   if (cu->language == language_fortran && die->parent
17409                       && die->parent->tag == DW_TAG_module
17410                       && cu->producer
17411                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
17412                     SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
17413 
17414                   /* A variable with DW_AT_external is never static,
17415                      but it may be block-scoped.  */
17416                   list_to_add = (cu->list_in_scope == &file_symbols
17417                                  ? &global_symbols : cu->list_in_scope);
17418                 }
17419               else
17420                 list_to_add = cu->list_in_scope;
17421             }
17422           else
17423             {
17424               /* We do not know the address of this symbol.
17425                  If it is an external symbol and we have type information
17426                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
17427                  The address of the variable will then be determined from
17428                  the minimal symbol table whenever the variable is
17429                  referenced.  */
17430               attr2 = dwarf2_attr (die, DW_AT_external, cu);
17431 
17432               /* Fortran explicitly imports any global symbols to the local
17433                  scope by DW_TAG_common_block.  */
17434               if (cu->language == language_fortran && die->parent
17435                   && die->parent->tag == DW_TAG_common_block)
17436                 {
17437                   /* SYMBOL_CLASS doesn't matter here because
17438                      read_common_block is going to reset it.  */
17439                   if (!suppress_add)
17440                     list_to_add = cu->list_in_scope;
17441                 }
17442               else if (attr2 && (DW_UNSND (attr2) != 0)
17443                        && dwarf2_attr (die, DW_AT_type, cu) != NULL)
17444                 {
17445                   /* A variable with DW_AT_external is never static, but it
17446                      may be block-scoped.  */
17447                   list_to_add = (cu->list_in_scope == &file_symbols
17448                                  ? &global_symbols : cu->list_in_scope);
17449 
17450                   SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
17451                 }
17452               else if (!die_is_declaration (die, cu))
17453                 {
17454                   /* Use the default LOC_OPTIMIZED_OUT class.  */
17455                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
17456                   if (!suppress_add)
17457                     list_to_add = cu->list_in_scope;
17458                 }
17459             }
17460           break;
17461         case DW_TAG_formal_parameter:
17462           /* If we are inside a function, mark this as an argument.  If
17463              not, we might be looking at an argument to an inlined function
17464              when we do not have enough information to show inlined frames;
17465              pretend it's a local variable in that case so that the user can
17466              still see it.  */
17467           if (context_stack_depth > 0
17468               && context_stack[context_stack_depth - 1].name != NULL)
17469             SYMBOL_IS_ARGUMENT (sym) = 1;
17470           attr = dwarf2_attr (die, DW_AT_location, cu);
17471           if (attr)
17472             {
17473               var_decode_location (attr, sym, cu);
17474             }
17475           attr = dwarf2_attr (die, DW_AT_const_value, cu);
17476           if (attr)
17477             {
17478               dwarf2_const_value (attr, sym, cu);
17479             }
17480 
17481           list_to_add = cu->list_in_scope;
17482           break;
17483         case DW_TAG_unspecified_parameters:
17484           /* From varargs functions; gdb doesn't seem to have any
17485              interest in this information, so just ignore it for now.
17486              (FIXME?) */
17487           break;
17488         case DW_TAG_template_type_param:
17489           suppress_add = 1;
17490           /* Fall through.  */
17491         case DW_TAG_class_type:
17492         case DW_TAG_interface_type:
17493         case DW_TAG_structure_type:
17494         case DW_TAG_union_type:
17495         case DW_TAG_set_type:
17496         case DW_TAG_enumeration_type:
17497           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17498           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
17499 
17500           {
17501             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
17502                really ever be static objects: otherwise, if you try
17503                to, say, break of a class's method and you're in a file
17504                which doesn't mention that class, it won't work unless
17505                the check for all static symbols in lookup_symbol_aux
17506                saves you.  See the OtherFileClass tests in
17507                gdb.c++/namespace.exp.  */
17508 
17509             if (!suppress_add)
17510               {
17511                 list_to_add = (cu->list_in_scope == &file_symbols
17512                                && (cu->language == language_cplus
17513                                    || cu->language == language_java)
17514                                ? &global_symbols : cu->list_in_scope);
17515 
17516                 /* The semantics of C++ state that "struct foo {
17517                    ... }" also defines a typedef for "foo".  A Java
17518                    class declaration also defines a typedef for the
17519                    class.  */
17520                 if (cu->language == language_cplus
17521                     || cu->language == language_java
17522                     || cu->language == language_ada)
17523                   {
17524                     /* The symbol's name is already allocated along
17525                        with this objfile, so we don't need to
17526                        duplicate it for the type.  */
17527                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
17528                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
17529                   }
17530               }
17531           }
17532           break;
17533         case DW_TAG_typedef:
17534           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17535           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
17536           list_to_add = cu->list_in_scope;
17537           break;
17538         case DW_TAG_base_type:
17539         case DW_TAG_subrange_type:
17540           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17541           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
17542           list_to_add = cu->list_in_scope;
17543           break;
17544         case DW_TAG_enumerator:
17545           attr = dwarf2_attr (die, DW_AT_const_value, cu);
17546           if (attr)
17547             {
17548               dwarf2_const_value (attr, sym, cu);
17549             }
17550           {
17551             /* NOTE: carlton/2003-11-10: See comment above in the
17552                DW_TAG_class_type, etc. block.  */
17553 
17554             list_to_add = (cu->list_in_scope == &file_symbols
17555                            && (cu->language == language_cplus
17556                                || cu->language == language_java)
17557                            ? &global_symbols : cu->list_in_scope);
17558           }
17559           break;
17560         case DW_TAG_namespace:
17561           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
17562           list_to_add = &global_symbols;
17563           break;
17564         case DW_TAG_common_block:
17565           SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
17566           SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
17567           add_symbol_to_list (sym, cu->list_in_scope);
17568           break;
17569         default:
17570           /* Not a tag we recognize.  Hopefully we aren't processing
17571              trash data, but since we must specifically ignore things
17572              we don't recognize, there is nothing else we should do at
17573              this point.  */
17574           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
17575                      dwarf_tag_name (die->tag));
17576           break;
17577         }
17578 
17579       if (suppress_add)
17580         {
17581           sym->hash_next = objfile->template_symbols;
17582           objfile->template_symbols = sym;
17583           list_to_add = NULL;
17584         }
17585 
17586       if (list_to_add != NULL)
17587         add_symbol_to_list (sym, list_to_add);
17588 
17589       /* For the benefit of old versions of GCC, check for anonymous
17590          namespaces based on the demangled name.  */
17591       if (!cu->processing_has_namespace_info
17592           && cu->language == language_cplus)
17593         cp_scan_for_anonymous_namespaces (sym, objfile);
17594     }
17595   return (sym);
17596 }
17597 
17598 /* A wrapper for new_symbol_full that always allocates a new symbol.  */
17599 
17600 static struct symbol *
17601 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
17602 {
17603   return new_symbol_full (die, type, cu, NULL);
17604 }
17605 
17606 /* Given an attr with a DW_FORM_dataN value in host byte order,
17607    zero-extend it as appropriate for the symbol's type.  The DWARF
17608    standard (v4) is not entirely clear about the meaning of using
17609    DW_FORM_dataN for a constant with a signed type, where the type is
17610    wider than the data.  The conclusion of a discussion on the DWARF
17611    list was that this is unspecified.  We choose to always zero-extend
17612    because that is the interpretation long in use by GCC.  */
17613 
17614 static gdb_byte *
17615 dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
17616                          struct dwarf2_cu *cu, LONGEST *value, int bits)
17617 {
17618   struct objfile *objfile = cu->objfile;
17619   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
17620                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
17621   LONGEST l = DW_UNSND (attr);
17622 
17623   if (bits < sizeof (*value) * 8)
17624     {
17625       l &= ((LONGEST) 1 << bits) - 1;
17626       *value = l;
17627     }
17628   else if (bits == sizeof (*value) * 8)
17629     *value = l;
17630   else
17631     {
17632       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
17633       store_unsigned_integer (bytes, bits / 8, byte_order, l);
17634       return bytes;
17635     }
17636 
17637   return NULL;
17638 }
17639 
17640 /* Read a constant value from an attribute.  Either set *VALUE, or if
17641    the value does not fit in *VALUE, set *BYTES - either already
17642    allocated on the objfile obstack, or newly allocated on OBSTACK,
17643    or, set *BATON, if we translated the constant to a location
17644    expression.  */
17645 
17646 static void
17647 dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
17648                          const char *name, struct obstack *obstack,
17649                          struct dwarf2_cu *cu,
17650                          LONGEST *value, const gdb_byte **bytes,
17651                          struct dwarf2_locexpr_baton **baton)
17652 {
17653   struct objfile *objfile = cu->objfile;
17654   struct comp_unit_head *cu_header = &cu->header;
17655   struct dwarf_block *blk;
17656   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
17657                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
17658 
17659   *value = 0;
17660   *bytes = NULL;
17661   *baton = NULL;
17662 
17663   switch (attr->form)
17664     {
17665     case DW_FORM_addr:
17666     case DW_FORM_GNU_addr_index:
17667       {
17668         gdb_byte *data;
17669 
17670         if (TYPE_LENGTH (type) != cu_header->addr_size)
17671           dwarf2_const_value_length_mismatch_complaint (name,
17672                                                         cu_header->addr_size,
17673                                                         TYPE_LENGTH (type));
17674         /* Symbols of this form are reasonably rare, so we just
17675            piggyback on the existing location code rather than writing
17676            a new implementation of symbol_computed_ops.  */
17677         *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
17678         (*baton)->per_cu = cu->per_cu;
17679         gdb_assert ((*baton)->per_cu);
17680 
17681         (*baton)->size = 2 + cu_header->addr_size;
17682         data = obstack_alloc (obstack, (*baton)->size);
17683         (*baton)->data = data;
17684 
17685         data[0] = DW_OP_addr;
17686         store_unsigned_integer (&data[1], cu_header->addr_size,
17687                                 byte_order, DW_ADDR (attr));
17688         data[cu_header->addr_size + 1] = DW_OP_stack_value;
17689       }
17690       break;
17691     case DW_FORM_string:
17692     case DW_FORM_strp:
17693     case DW_FORM_GNU_str_index:
17694     case DW_FORM_GNU_strp_alt:
17695       /* DW_STRING is already allocated on the objfile obstack, point
17696          directly to it.  */
17697       *bytes = (const gdb_byte *) DW_STRING (attr);
17698       break;
17699     case DW_FORM_block1:
17700     case DW_FORM_block2:
17701     case DW_FORM_block4:
17702     case DW_FORM_block:
17703     case DW_FORM_exprloc:
17704       blk = DW_BLOCK (attr);
17705       if (TYPE_LENGTH (type) != blk->size)
17706         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
17707                                                       TYPE_LENGTH (type));
17708       *bytes = blk->data;
17709       break;
17710 
17711       /* The DW_AT_const_value attributes are supposed to carry the
17712          symbol's value "represented as it would be on the target
17713          architecture."  By the time we get here, it's already been
17714          converted to host endianness, so we just need to sign- or
17715          zero-extend it as appropriate.  */
17716     case DW_FORM_data1:
17717       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
17718       break;
17719     case DW_FORM_data2:
17720       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
17721       break;
17722     case DW_FORM_data4:
17723       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
17724       break;
17725     case DW_FORM_data8:
17726       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
17727       break;
17728 
17729     case DW_FORM_sdata:
17730       *value = DW_SND (attr);
17731       break;
17732 
17733     case DW_FORM_udata:
17734       *value = DW_UNSND (attr);
17735       break;
17736 
17737     default:
17738       complaint (&symfile_complaints,
17739                  _("unsupported const value attribute form: '%s'"),
17740                  dwarf_form_name (attr->form));
17741       *value = 0;
17742       break;
17743     }
17744 }
17745 
17746 
17747 /* Copy constant value from an attribute to a symbol.  */
17748 
17749 static void
17750 dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
17751                     struct dwarf2_cu *cu)
17752 {
17753   struct objfile *objfile = cu->objfile;
17754   struct comp_unit_head *cu_header = &cu->header;
17755   LONGEST value;
17756   const gdb_byte *bytes;
17757   struct dwarf2_locexpr_baton *baton;
17758 
17759   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
17760                            SYMBOL_PRINT_NAME (sym),
17761                            &objfile->objfile_obstack, cu,
17762                            &value, &bytes, &baton);
17763 
17764   if (baton != NULL)
17765     {
17766       SYMBOL_LOCATION_BATON (sym) = baton;
17767       SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
17768     }
17769   else if (bytes != NULL)
17770      {
17771       SYMBOL_VALUE_BYTES (sym) = bytes;
17772       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
17773     }
17774   else
17775     {
17776       SYMBOL_VALUE (sym) = value;
17777       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
17778     }
17779 }
17780 
17781 /* Return the type of the die in question using its DW_AT_type attribute.  */
17782 
17783 static struct type *
17784 die_type (struct die_info *die, struct dwarf2_cu *cu)
17785 {
17786   struct attribute *type_attr;
17787 
17788   type_attr = dwarf2_attr (die, DW_AT_type, cu);
17789   if (!type_attr)
17790     {
17791       /* A missing DW_AT_type represents a void type.  */
17792       return objfile_type (cu->objfile)->builtin_void;
17793     }
17794 
17795   return lookup_die_type (die, type_attr, cu);
17796 }
17797 
17798 /* True iff CU's producer generates GNAT Ada auxiliary information
17799    that allows to find parallel types through that information instead
17800    of having to do expensive parallel lookups by type name.  */
17801 
17802 static int
17803 need_gnat_info (struct dwarf2_cu *cu)
17804 {
17805   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
17806      of GNAT produces this auxiliary information, without any indication
17807      that it is produced.  Part of enhancing the FSF version of GNAT
17808      to produce that information will be to put in place an indicator
17809      that we can use in order to determine whether the descriptive type
17810      info is available or not.  One suggestion that has been made is
17811      to use a new attribute, attached to the CU die.  For now, assume
17812      that the descriptive type info is not available.  */
17813   return 0;
17814 }
17815 
17816 /* Return the auxiliary type of the die in question using its
17817    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
17818    attribute is not present.  */
17819 
17820 static struct type *
17821 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
17822 {
17823   struct attribute *type_attr;
17824 
17825   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
17826   if (!type_attr)
17827     return NULL;
17828 
17829   return lookup_die_type (die, type_attr, cu);
17830 }
17831 
17832 /* If DIE has a descriptive_type attribute, then set the TYPE's
17833    descriptive type accordingly.  */
17834 
17835 static void
17836 set_descriptive_type (struct type *type, struct die_info *die,
17837                       struct dwarf2_cu *cu)
17838 {
17839   struct type *descriptive_type = die_descriptive_type (die, cu);
17840 
17841   if (descriptive_type)
17842     {
17843       ALLOCATE_GNAT_AUX_TYPE (type);
17844       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
17845     }
17846 }
17847 
17848 /* Return the containing type of the die in question using its
17849    DW_AT_containing_type attribute.  */
17850 
17851 static struct type *
17852 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
17853 {
17854   struct attribute *type_attr;
17855 
17856   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
17857   if (!type_attr)
17858     error (_("Dwarf Error: Problem turning containing type into gdb type "
17859              "[in module %s]"), objfile_name (cu->objfile));
17860 
17861   return lookup_die_type (die, type_attr, cu);
17862 }
17863 
17864 /* Return an error marker type to use for the ill formed type in DIE/CU.  */
17865 
17866 static struct type *
17867 build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
17868 {
17869   struct objfile *objfile = dwarf2_per_objfile->objfile;
17870   char *message, *saved;
17871 
17872   message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
17873                         objfile_name (objfile),
17874                         cu->header.offset.sect_off,
17875                         die->offset.sect_off);
17876   saved = obstack_copy0 (&objfile->objfile_obstack,
17877                          message, strlen (message));
17878   xfree (message);
17879 
17880   return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
17881 }
17882 
17883 /* Look up the type of DIE in CU using its type attribute ATTR.
17884    ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
17885    DW_AT_containing_type.
17886    If there is no type substitute an error marker.  */
17887 
17888 static struct type *
17889 lookup_die_type (struct die_info *die, const struct attribute *attr,
17890                  struct dwarf2_cu *cu)
17891 {
17892   struct objfile *objfile = cu->objfile;
17893   struct type *this_type;
17894 
17895   gdb_assert (attr->name == DW_AT_type
17896               || attr->name == DW_AT_GNAT_descriptive_type
17897               || attr->name == DW_AT_containing_type);
17898 
17899   /* First see if we have it cached.  */
17900 
17901   if (attr->form == DW_FORM_GNU_ref_alt)
17902     {
17903       struct dwarf2_per_cu_data *per_cu;
17904       sect_offset offset = dwarf2_get_ref_die_offset (attr);
17905 
17906       per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
17907       this_type = get_die_type_at_offset (offset, per_cu);
17908     }
17909   else if (attr_form_is_ref (attr))
17910     {
17911       sect_offset offset = dwarf2_get_ref_die_offset (attr);
17912 
17913       this_type = get_die_type_at_offset (offset, cu->per_cu);
17914     }
17915   else if (attr->form == DW_FORM_ref_sig8)
17916     {
17917       ULONGEST signature = DW_SIGNATURE (attr);
17918 
17919       return get_signatured_type (die, signature, cu);
17920     }
17921   else
17922     {
17923       complaint (&symfile_complaints,
17924                  _("Dwarf Error: Bad type attribute %s in DIE"
17925                    " at 0x%x [in module %s]"),
17926                  dwarf_attr_name (attr->name), die->offset.sect_off,
17927                  objfile_name (objfile));
17928       return build_error_marker_type (cu, die);
17929     }
17930 
17931   /* If not cached we need to read it in.  */
17932 
17933   if (this_type == NULL)
17934     {
17935       struct die_info *type_die = NULL;
17936       struct dwarf2_cu *type_cu = cu;
17937 
17938       if (attr_form_is_ref (attr))
17939         type_die = follow_die_ref (die, attr, &type_cu);
17940       if (type_die == NULL)
17941         return build_error_marker_type (cu, die);
17942       /* If we find the type now, it's probably because the type came
17943          from an inter-CU reference and the type's CU got expanded before
17944          ours.  */
17945       this_type = read_type_die (type_die, type_cu);
17946     }
17947 
17948   /* If we still don't have a type use an error marker.  */
17949 
17950   if (this_type == NULL)
17951     return build_error_marker_type (cu, die);
17952 
17953   return this_type;
17954 }
17955 
17956 /* Return the type in DIE, CU.
17957    Returns NULL for invalid types.
17958 
17959    This first does a lookup in die_type_hash,
17960    and only reads the die in if necessary.
17961 
17962    NOTE: This can be called when reading in partial or full symbols.  */
17963 
17964 static struct type *
17965 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
17966 {
17967   struct type *this_type;
17968 
17969   this_type = get_die_type (die, cu);
17970   if (this_type)
17971     return this_type;
17972 
17973   return read_type_die_1 (die, cu);
17974 }
17975 
17976 /* Read the type in DIE, CU.
17977    Returns NULL for invalid types.  */
17978 
17979 static struct type *
17980 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
17981 {
17982   struct type *this_type = NULL;
17983 
17984   switch (die->tag)
17985     {
17986     case DW_TAG_class_type:
17987     case DW_TAG_interface_type:
17988     case DW_TAG_structure_type:
17989     case DW_TAG_union_type:
17990       this_type = read_structure_type (die, cu);
17991       break;
17992     case DW_TAG_enumeration_type:
17993       this_type = read_enumeration_type (die, cu);
17994       break;
17995     case DW_TAG_subprogram:
17996     case DW_TAG_subroutine_type:
17997     case DW_TAG_inlined_subroutine:
17998       this_type = read_subroutine_type (die, cu);
17999       break;
18000     case DW_TAG_array_type:
18001       this_type = read_array_type (die, cu);
18002       break;
18003     case DW_TAG_set_type:
18004       this_type = read_set_type (die, cu);
18005       break;
18006     case DW_TAG_pointer_type:
18007       this_type = read_tag_pointer_type (die, cu);
18008       break;
18009     case DW_TAG_ptr_to_member_type:
18010       this_type = read_tag_ptr_to_member_type (die, cu);
18011       break;
18012     case DW_TAG_reference_type:
18013       this_type = read_tag_reference_type (die, cu);
18014       break;
18015     case DW_TAG_const_type:
18016       this_type = read_tag_const_type (die, cu);
18017       break;
18018     case DW_TAG_volatile_type:
18019       this_type = read_tag_volatile_type (die, cu);
18020       break;
18021     case DW_TAG_restrict_type:
18022       this_type = read_tag_restrict_type (die, cu);
18023       break;
18024     case DW_TAG_string_type:
18025       this_type = read_tag_string_type (die, cu);
18026       break;
18027     case DW_TAG_typedef:
18028       this_type = read_typedef (die, cu);
18029       break;
18030     case DW_TAG_subrange_type:
18031       this_type = read_subrange_type (die, cu);
18032       break;
18033     case DW_TAG_base_type:
18034       this_type = read_base_type (die, cu);
18035       break;
18036     case DW_TAG_unspecified_type:
18037       this_type = read_unspecified_type (die, cu);
18038       break;
18039     case DW_TAG_namespace:
18040       this_type = read_namespace_type (die, cu);
18041       break;
18042     case DW_TAG_module:
18043       this_type = read_module_type (die, cu);
18044       break;
18045     default:
18046       complaint (&symfile_complaints,
18047                  _("unexpected tag in read_type_die: '%s'"),
18048                  dwarf_tag_name (die->tag));
18049       break;
18050     }
18051 
18052   return this_type;
18053 }
18054 
18055 /* See if we can figure out if the class lives in a namespace.  We do
18056    this by looking for a member function; its demangled name will
18057    contain namespace info, if there is any.
18058    Return the computed name or NULL.
18059    Space for the result is allocated on the objfile's obstack.
18060    This is the full-die version of guess_partial_die_structure_name.
18061    In this case we know DIE has no useful parent.  */
18062 
18063 static char *
18064 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
18065 {
18066   struct die_info *spec_die;
18067   struct dwarf2_cu *spec_cu;
18068   struct die_info *child;
18069 
18070   spec_cu = cu;
18071   spec_die = die_specification (die, &spec_cu);
18072   if (spec_die != NULL)
18073     {
18074       die = spec_die;
18075       cu = spec_cu;
18076     }
18077 
18078   for (child = die->child;
18079        child != NULL;
18080        child = child->sibling)
18081     {
18082       if (child->tag == DW_TAG_subprogram)
18083         {
18084           struct attribute *attr;
18085 
18086           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
18087           if (attr == NULL)
18088             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
18089           if (attr != NULL)
18090             {
18091               char *actual_name
18092                 = language_class_name_from_physname (cu->language_defn,
18093                                                      DW_STRING (attr));
18094               char *name = NULL;
18095 
18096               if (actual_name != NULL)
18097                 {
18098                   const char *die_name = dwarf2_name (die, cu);
18099 
18100                   if (die_name != NULL
18101                       && strcmp (die_name, actual_name) != 0)
18102                     {
18103                       /* Strip off the class name from the full name.
18104                          We want the prefix.  */
18105                       int die_name_len = strlen (die_name);
18106                       int actual_name_len = strlen (actual_name);
18107 
18108                       /* Test for '::' as a sanity check.  */
18109                       if (actual_name_len > die_name_len + 2
18110                           && actual_name[actual_name_len
18111                                          - die_name_len - 1] == ':')
18112                         name =
18113                           obstack_copy0 (&cu->objfile->objfile_obstack,
18114                                          actual_name,
18115                                          actual_name_len - die_name_len - 2);
18116                     }
18117                 }
18118               xfree (actual_name);
18119               return name;
18120             }
18121         }
18122     }
18123 
18124   return NULL;
18125 }
18126 
18127 /* GCC might emit a nameless typedef that has a linkage name.  Determine the
18128    prefix part in such case.  See
18129    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
18130 
18131 static char *
18132 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
18133 {
18134   struct attribute *attr;
18135   char *base;
18136 
18137   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
18138       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
18139     return NULL;
18140 
18141   attr = dwarf2_attr (die, DW_AT_name, cu);
18142   if (attr != NULL && DW_STRING (attr) != NULL)
18143     return NULL;
18144 
18145   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
18146   if (attr == NULL)
18147     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
18148   if (attr == NULL || DW_STRING (attr) == NULL)
18149     return NULL;
18150 
18151   /* dwarf2_name had to be already called.  */
18152   gdb_assert (DW_STRING_IS_CANONICAL (attr));
18153 
18154   /* Strip the base name, keep any leading namespaces/classes.  */
18155   base = strrchr (DW_STRING (attr), ':');
18156   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
18157     return "";
18158 
18159   return obstack_copy0 (&cu->objfile->objfile_obstack,
18160                         DW_STRING (attr), &base[-1] - DW_STRING (attr));
18161 }
18162 
18163 /* Return the name of the namespace/class that DIE is defined within,
18164    or "" if we can't tell.  The caller should not xfree the result.
18165 
18166    For example, if we're within the method foo() in the following
18167    code:
18168 
18169    namespace N {
18170      class C {
18171        void foo () {
18172        }
18173      };
18174    }
18175 
18176    then determine_prefix on foo's die will return "N::C".  */
18177 
18178 static const char *
18179 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
18180 {
18181   struct die_info *parent, *spec_die;
18182   struct dwarf2_cu *spec_cu;
18183   struct type *parent_type;
18184   char *retval;
18185 
18186   if (cu->language != language_cplus && cu->language != language_java
18187       && cu->language != language_fortran)
18188     return "";
18189 
18190   retval = anonymous_struct_prefix (die, cu);
18191   if (retval)
18192     return retval;
18193 
18194   /* We have to be careful in the presence of DW_AT_specification.
18195      For example, with GCC 3.4, given the code
18196 
18197      namespace N {
18198        void foo() {
18199          // Definition of N::foo.
18200        }
18201      }
18202 
18203      then we'll have a tree of DIEs like this:
18204 
18205      1: DW_TAG_compile_unit
18206        2: DW_TAG_namespace        // N
18207          3: DW_TAG_subprogram     // declaration of N::foo
18208        4: DW_TAG_subprogram       // definition of N::foo
18209             DW_AT_specification   // refers to die #3
18210 
18211      Thus, when processing die #4, we have to pretend that we're in
18212      the context of its DW_AT_specification, namely the contex of die
18213      #3.  */
18214   spec_cu = cu;
18215   spec_die = die_specification (die, &spec_cu);
18216   if (spec_die == NULL)
18217     parent = die->parent;
18218   else
18219     {
18220       parent = spec_die->parent;
18221       cu = spec_cu;
18222     }
18223 
18224   if (parent == NULL)
18225     return "";
18226   else if (parent->building_fullname)
18227     {
18228       const char *name;
18229       const char *parent_name;
18230 
18231       /* It has been seen on RealView 2.2 built binaries,
18232          DW_TAG_template_type_param types actually _defined_ as
18233          children of the parent class:
18234 
18235          enum E {};
18236          template class <class Enum> Class{};
18237          Class<enum E> class_e;
18238 
18239          1: DW_TAG_class_type (Class)
18240            2: DW_TAG_enumeration_type (E)
18241              3: DW_TAG_enumerator (enum1:0)
18242              3: DW_TAG_enumerator (enum2:1)
18243              ...
18244            2: DW_TAG_template_type_param
18245               DW_AT_type  DW_FORM_ref_udata (E)
18246 
18247          Besides being broken debug info, it can put GDB into an
18248          infinite loop.  Consider:
18249 
18250          When we're building the full name for Class<E>, we'll start
18251          at Class, and go look over its template type parameters,
18252          finding E.  We'll then try to build the full name of E, and
18253          reach here.  We're now trying to build the full name of E,
18254          and look over the parent DIE for containing scope.  In the
18255          broken case, if we followed the parent DIE of E, we'd again
18256          find Class, and once again go look at its template type
18257          arguments, etc., etc.  Simply don't consider such parent die
18258          as source-level parent of this die (it can't be, the language
18259          doesn't allow it), and break the loop here.  */
18260       name = dwarf2_name (die, cu);
18261       parent_name = dwarf2_name (parent, cu);
18262       complaint (&symfile_complaints,
18263                  _("template param type '%s' defined within parent '%s'"),
18264                  name ? name : "<unknown>",
18265                  parent_name ? parent_name : "<unknown>");
18266       return "";
18267     }
18268   else
18269     switch (parent->tag)
18270       {
18271       case DW_TAG_namespace:
18272         parent_type = read_type_die (parent, cu);
18273         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
18274            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
18275            Work around this problem here.  */
18276         if (cu->language == language_cplus
18277             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
18278           return "";
18279         /* We give a name to even anonymous namespaces.  */
18280         return TYPE_TAG_NAME (parent_type);
18281       case DW_TAG_class_type:
18282       case DW_TAG_interface_type:
18283       case DW_TAG_structure_type:
18284       case DW_TAG_union_type:
18285       case DW_TAG_module:
18286         parent_type = read_type_die (parent, cu);
18287         if (TYPE_TAG_NAME (parent_type) != NULL)
18288           return TYPE_TAG_NAME (parent_type);
18289         else
18290           /* An anonymous structure is only allowed non-static data
18291              members; no typedefs, no member functions, et cetera.
18292              So it does not need a prefix.  */
18293           return "";
18294       case DW_TAG_compile_unit:
18295       case DW_TAG_partial_unit:
18296         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
18297         if (cu->language == language_cplus
18298             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
18299             && die->child != NULL
18300             && (die->tag == DW_TAG_class_type
18301                 || die->tag == DW_TAG_structure_type
18302                 || die->tag == DW_TAG_union_type))
18303           {
18304             char *name = guess_full_die_structure_name (die, cu);
18305             if (name != NULL)
18306               return name;
18307           }
18308         return "";
18309       default:
18310         return determine_prefix (parent, cu);
18311       }
18312 }
18313 
18314 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
18315    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
18316    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
18317    an obconcat, otherwise allocate storage for the result.  The CU argument is
18318    used to determine the language and hence, the appropriate separator.  */
18319 
18320 #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */
18321 
18322 static char *
18323 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
18324                  int physname, struct dwarf2_cu *cu)
18325 {
18326   const char *lead = "";
18327   const char *sep;
18328 
18329   if (suffix == NULL || suffix[0] == '\0'
18330       || prefix == NULL || prefix[0] == '\0')
18331     sep = "";
18332   else if (cu->language == language_java)
18333     sep = ".";
18334   else if (cu->language == language_fortran && physname)
18335     {
18336       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
18337          DW_AT_MIPS_linkage_name is preferred and used instead.  */
18338 
18339       lead = "__";
18340       sep = "_MOD_";
18341     }
18342   else
18343     sep = "::";
18344 
18345   if (prefix == NULL)
18346     prefix = "";
18347   if (suffix == NULL)
18348     suffix = "";
18349 
18350   if (obs == NULL)
18351     {
18352       char *retval
18353         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
18354 
18355       strcpy (retval, lead);
18356       strcat (retval, prefix);
18357       strcat (retval, sep);
18358       strcat (retval, suffix);
18359       return retval;
18360     }
18361   else
18362     {
18363       /* We have an obstack.  */
18364       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
18365     }
18366 }
18367 
18368 /* Return sibling of die, NULL if no sibling.  */
18369 
18370 static struct die_info *
18371 sibling_die (struct die_info *die)
18372 {
18373   return die->sibling;
18374 }
18375 
18376 /* Get name of a die, return NULL if not found.  */
18377 
18378 static const char *
18379 dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
18380                           struct obstack *obstack)
18381 {
18382   if (name && cu->language == language_cplus)
18383     {
18384       char *canon_name = cp_canonicalize_string (name);
18385 
18386       if (canon_name != NULL)
18387         {
18388           if (strcmp (canon_name, name) != 0)
18389             name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
18390           xfree (canon_name);
18391         }
18392     }
18393 
18394   return name;
18395 }
18396 
18397 /* Get name of a die, return NULL if not found.  */
18398 
18399 static const char *
18400 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
18401 {
18402   struct attribute *attr;
18403 
18404   attr = dwarf2_attr (die, DW_AT_name, cu);
18405   if ((!attr || !DW_STRING (attr))
18406       && die->tag != DW_TAG_class_type
18407       && die->tag != DW_TAG_interface_type
18408       && die->tag != DW_TAG_structure_type
18409       && die->tag != DW_TAG_union_type)
18410     return NULL;
18411 
18412   switch (die->tag)
18413     {
18414     case DW_TAG_compile_unit:
18415     case DW_TAG_partial_unit:
18416       /* Compilation units have a DW_AT_name that is a filename, not
18417          a source language identifier.  */
18418     case DW_TAG_enumeration_type:
18419     case DW_TAG_enumerator:
18420       /* These tags always have simple identifiers already; no need
18421          to canonicalize them.  */
18422       return DW_STRING (attr);
18423 
18424     case DW_TAG_subprogram:
18425       /* Java constructors will all be named "<init>", so return
18426          the class name when we see this special case.  */
18427       if (cu->language == language_java
18428           && DW_STRING (attr) != NULL
18429           && strcmp (DW_STRING (attr), "<init>") == 0)
18430         {
18431           struct dwarf2_cu *spec_cu = cu;
18432           struct die_info *spec_die;
18433 
18434           /* GCJ will output '<init>' for Java constructor names.
18435              For this special case, return the name of the parent class.  */
18436 
18437           /* GCJ may output suprogram DIEs with AT_specification set.
18438              If so, use the name of the specified DIE.  */
18439           spec_die = die_specification (die, &spec_cu);
18440           if (spec_die != NULL)
18441             return dwarf2_name (spec_die, spec_cu);
18442 
18443           do
18444             {
18445               die = die->parent;
18446               if (die->tag == DW_TAG_class_type)
18447                 return dwarf2_name (die, cu);
18448             }
18449           while (die->tag != DW_TAG_compile_unit
18450                  && die->tag != DW_TAG_partial_unit);
18451         }
18452       break;
18453 
18454     case DW_TAG_class_type:
18455     case DW_TAG_interface_type:
18456     case DW_TAG_structure_type:
18457     case DW_TAG_union_type:
18458       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
18459          structures or unions.  These were of the form "._%d" in GCC 4.1,
18460          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
18461          and GCC 4.4.  We work around this problem by ignoring these.  */
18462       if (attr && DW_STRING (attr)
18463           && (strncmp (DW_STRING (attr), "._", 2) == 0
18464               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
18465         return NULL;
18466 
18467       /* GCC might emit a nameless typedef that has a linkage name.  See
18468          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
18469       if (!attr || DW_STRING (attr) == NULL)
18470         {
18471           char *demangled = NULL;
18472 
18473           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
18474           if (attr == NULL)
18475             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
18476 
18477           if (attr == NULL || DW_STRING (attr) == NULL)
18478             return NULL;
18479 
18480           /* Avoid demangling DW_STRING (attr) the second time on a second
18481              call for the same DIE.  */
18482           if (!DW_STRING_IS_CANONICAL (attr))
18483             demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);
18484 
18485           if (demangled)
18486             {
18487               char *base;
18488 
18489               /* FIXME: we already did this for the partial symbol... */
18490               DW_STRING (attr) = obstack_copy0 (&cu->objfile->objfile_obstack,
18491                                                 demangled, strlen (demangled));
18492               DW_STRING_IS_CANONICAL (attr) = 1;
18493               xfree (demangled);
18494 
18495               /* Strip any leading namespaces/classes, keep only the base name.
18496                  DW_AT_name for named DIEs does not contain the prefixes.  */
18497               base = strrchr (DW_STRING (attr), ':');
18498               if (base && base > DW_STRING (attr) && base[-1] == ':')
18499                 return &base[1];
18500               else
18501                 return DW_STRING (attr);
18502             }
18503         }
18504       break;
18505 
18506     default:
18507       break;
18508     }
18509 
18510   if (!DW_STRING_IS_CANONICAL (attr))
18511     {
18512       DW_STRING (attr)
18513         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
18514                                     &cu->objfile->objfile_obstack);
18515       DW_STRING_IS_CANONICAL (attr) = 1;
18516     }
18517   return DW_STRING (attr);
18518 }
18519 
18520 /* Return the die that this die in an extension of, or NULL if there
18521    is none.  *EXT_CU is the CU containing DIE on input, and the CU
18522    containing the return value on output.  */
18523 
18524 static struct die_info *
18525 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
18526 {
18527   struct attribute *attr;
18528 
18529   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
18530   if (attr == NULL)
18531     return NULL;
18532 
18533   return follow_die_ref (die, attr, ext_cu);
18534 }
18535 
18536 /* Convert a DIE tag into its string name.  */
18537 
18538 static const char *
18539 dwarf_tag_name (unsigned tag)
18540 {
18541   const char *name = get_DW_TAG_name (tag);
18542 
18543   if (name == NULL)
18544     return "DW_TAG_<unknown>";
18545 
18546   return name;
18547 }
18548 
18549 /* Convert a DWARF attribute code into its string name.  */
18550 
18551 static const char *
18552 dwarf_attr_name (unsigned attr)
18553 {
18554   const char *name;
18555 
18556 #ifdef MIPS /* collides with DW_AT_HP_block_index */
18557   if (attr == DW_AT_MIPS_fde)
18558     return "DW_AT_MIPS_fde";
18559 #else
18560   if (attr == DW_AT_HP_block_index)
18561     return "DW_AT_HP_block_index";
18562 #endif
18563 
18564   name = get_DW_AT_name (attr);
18565 
18566   if (name == NULL)
18567     return "DW_AT_<unknown>";
18568 
18569   return name;
18570 }
18571 
18572 /* Convert a DWARF value form code into its string name.  */
18573 
18574 static const char *
18575 dwarf_form_name (unsigned form)
18576 {
18577   const char *name = get_DW_FORM_name (form);
18578 
18579   if (name == NULL)
18580     return "DW_FORM_<unknown>";
18581 
18582   return name;
18583 }
18584 
18585 static char *
18586 dwarf_bool_name (unsigned mybool)
18587 {
18588   if (mybool)
18589     return "TRUE";
18590   else
18591     return "FALSE";
18592 }
18593 
18594 /* Convert a DWARF type code into its string name.  */
18595 
18596 static const char *
18597 dwarf_type_encoding_name (unsigned enc)
18598 {
18599   const char *name = get_DW_ATE_name (enc);
18600 
18601   if (name == NULL)
18602     return "DW_ATE_<unknown>";
18603 
18604   return name;
18605 }
18606 
18607 static void
18608 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
18609 {
18610   unsigned int i;
18611 
18612   print_spaces (indent, f);
18613   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
18614            dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
18615 
18616   if (die->parent != NULL)
18617     {
18618       print_spaces (indent, f);
18619       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
18620                           die->parent->offset.sect_off);
18621     }
18622 
18623   print_spaces (indent, f);
18624   fprintf_unfiltered (f, "  has children: %s\n",
18625            dwarf_bool_name (die->child != NULL));
18626 
18627   print_spaces (indent, f);
18628   fprintf_unfiltered (f, "  attributes:\n");
18629 
18630   for (i = 0; i < die->num_attrs; ++i)
18631     {
18632       print_spaces (indent, f);
18633       fprintf_unfiltered (f, "    %s (%s) ",
18634                dwarf_attr_name (die->attrs[i].name),
18635                dwarf_form_name (die->attrs[i].form));
18636 
18637       switch (die->attrs[i].form)
18638         {
18639         case DW_FORM_addr:
18640         case DW_FORM_GNU_addr_index:
18641           fprintf_unfiltered (f, "address: ");
18642           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
18643           break;
18644         case DW_FORM_block2:
18645         case DW_FORM_block4:
18646         case DW_FORM_block:
18647         case DW_FORM_block1:
18648           fprintf_unfiltered (f, "block: size %s",
18649                               pulongest (DW_BLOCK (&die->attrs[i])->size));
18650           break;
18651         case DW_FORM_exprloc:
18652           fprintf_unfiltered (f, "expression: size %s",
18653                               pulongest (DW_BLOCK (&die->attrs[i])->size));
18654           break;
18655         case DW_FORM_ref_addr:
18656           fprintf_unfiltered (f, "ref address: ");
18657           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18658           break;
18659         case DW_FORM_GNU_ref_alt:
18660           fprintf_unfiltered (f, "alt ref address: ");
18661           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
18662           break;
18663         case DW_FORM_ref1:
18664         case DW_FORM_ref2:
18665         case DW_FORM_ref4:
18666         case DW_FORM_ref8:
18667         case DW_FORM_ref_udata:
18668           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
18669                               (long) (DW_UNSND (&die->attrs[i])));
18670           break;
18671         case DW_FORM_data1:
18672         case DW_FORM_data2:
18673         case DW_FORM_data4:
18674         case DW_FORM_data8:
18675         case DW_FORM_udata:
18676         case DW_FORM_sdata:
18677           fprintf_unfiltered (f, "constant: %s",
18678                               pulongest (DW_UNSND (&die->attrs[i])));
18679           break;
18680         case DW_FORM_sec_offset:
18681           fprintf_unfiltered (f, "section offset: %s",
18682                               pulongest (DW_UNSND (&die->attrs[i])));
18683           break;
18684         case DW_FORM_ref_sig8:
18685           fprintf_unfiltered (f, "signature: %s",
18686                               hex_string (DW_SIGNATURE (&die->attrs[i])));
18687           break;
18688         case DW_FORM_string:
18689         case DW_FORM_strp:
18690         case DW_FORM_GNU_str_index:
18691         case DW_FORM_GNU_strp_alt:
18692           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
18693                    DW_STRING (&die->attrs[i])
18694                    ? DW_STRING (&die->attrs[i]) : "",
18695                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
18696           break;
18697         case DW_FORM_flag:
18698           if (DW_UNSND (&die->attrs[i]))
18699             fprintf_unfiltered (f, "flag: TRUE");
18700           else
18701             fprintf_unfiltered (f, "flag: FALSE");
18702           break;
18703         case DW_FORM_flag_present:
18704           fprintf_unfiltered (f, "flag: TRUE");
18705           break;
18706         case DW_FORM_indirect:
18707           /* The reader will have reduced the indirect form to
18708              the "base form" so this form should not occur.  */
18709           fprintf_unfiltered (f, 
18710                               "unexpected attribute form: DW_FORM_indirect");
18711           break;
18712         default:
18713           fprintf_unfiltered (f, "unsupported attribute form: %d.",
18714                    die->attrs[i].form);
18715           break;
18716         }
18717       fprintf_unfiltered (f, "\n");
18718     }
18719 }
18720 
18721 static void
18722 dump_die_for_error (struct die_info *die)
18723 {
18724   dump_die_shallow (gdb_stderr, 0, die);
18725 }
18726 
18727 static void
18728 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
18729 {
18730   int indent = level * 4;
18731 
18732   gdb_assert (die != NULL);
18733 
18734   if (level >= max_level)
18735     return;
18736 
18737   dump_die_shallow (f, indent, die);
18738 
18739   if (die->child != NULL)
18740     {
18741       print_spaces (indent, f);
18742       fprintf_unfiltered (f, "  Children:");
18743       if (level + 1 < max_level)
18744         {
18745           fprintf_unfiltered (f, "\n");
18746           dump_die_1 (f, level + 1, max_level, die->child);
18747         }
18748       else
18749         {
18750           fprintf_unfiltered (f,
18751                               " [not printed, max nesting level reached]\n");
18752         }
18753     }
18754 
18755   if (die->sibling != NULL && level > 0)
18756     {
18757       dump_die_1 (f, level, max_level, die->sibling);
18758     }
18759 }
18760 
18761 /* This is called from the pdie macro in gdbinit.in.
18762    It's not static so gcc will keep a copy callable from gdb.  */
18763 
18764 void
18765 dump_die (struct die_info *die, int max_level)
18766 {
18767   dump_die_1 (gdb_stdlog, 0, max_level, die);
18768 }
18769 
18770 static void
18771 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
18772 {
18773   void **slot;
18774 
18775   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
18776                                    INSERT);
18777 
18778   *slot = die;
18779 }
18780 
18781 /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
18782    required kind.  */
18783 
18784 static sect_offset
18785 dwarf2_get_ref_die_offset (const struct attribute *attr)
18786 {
18787   sect_offset retval = { DW_UNSND (attr) };
18788 
18789   if (attr_form_is_ref (attr))
18790     return retval;
18791 
18792   retval.sect_off = 0;
18793   complaint (&symfile_complaints,
18794              _("unsupported die ref attribute form: '%s'"),
18795              dwarf_form_name (attr->form));
18796   return retval;
18797 }
18798 
18799 /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
18800  * the value held by the attribute is not constant.  */
18801 
18802 static LONGEST
18803 dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
18804 {
18805   if (attr->form == DW_FORM_sdata)
18806     return DW_SND (attr);
18807   else if (attr->form == DW_FORM_udata
18808            || attr->form == DW_FORM_data1
18809            || attr->form == DW_FORM_data2
18810            || attr->form == DW_FORM_data4
18811            || attr->form == DW_FORM_data8)
18812     return DW_UNSND (attr);
18813   else
18814     {
18815       complaint (&symfile_complaints,
18816                  _("Attribute value is not a constant (%s)"),
18817                  dwarf_form_name (attr->form));
18818       return default_value;
18819     }
18820 }
18821 
18822 /* Follow reference or signature attribute ATTR of SRC_DIE.
18823    On entry *REF_CU is the CU of SRC_DIE.
18824    On exit *REF_CU is the CU of the result.  */
18825 
18826 static struct die_info *
18827 follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
18828                        struct dwarf2_cu **ref_cu)
18829 {
18830   struct die_info *die;
18831 
18832   if (attr_form_is_ref (attr))
18833     die = follow_die_ref (src_die, attr, ref_cu);
18834   else if (attr->form == DW_FORM_ref_sig8)
18835     die = follow_die_sig (src_die, attr, ref_cu);
18836   else
18837     {
18838       dump_die_for_error (src_die);
18839       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
18840              objfile_name ((*ref_cu)->objfile));
18841     }
18842 
18843   return die;
18844 }
18845 
18846 /* Follow reference OFFSET.
18847    On entry *REF_CU is the CU of the source die referencing OFFSET.
18848    On exit *REF_CU is the CU of the result.
18849    Returns NULL if OFFSET is invalid.  */
18850 
18851 static struct die_info *
18852 follow_die_offset (sect_offset offset, int offset_in_dwz,
18853                    struct dwarf2_cu **ref_cu)
18854 {
18855   struct die_info temp_die;
18856   struct dwarf2_cu *target_cu, *cu = *ref_cu;
18857 
18858   gdb_assert (cu->per_cu != NULL);
18859 
18860   target_cu = cu;
18861 
18862   if (cu->per_cu->is_debug_types)
18863     {
18864       /* .debug_types CUs cannot reference anything outside their CU.
18865          If they need to, they have to reference a signatured type via
18866          DW_FORM_ref_sig8.  */
18867       if (! offset_in_cu_p (&cu->header, offset))
18868         return NULL;
18869     }
18870   else if (offset_in_dwz != cu->per_cu->is_dwz
18871            || ! offset_in_cu_p (&cu->header, offset))
18872     {
18873       struct dwarf2_per_cu_data *per_cu;
18874 
18875       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
18876                                                  cu->objfile);
18877 
18878       /* If necessary, add it to the queue and load its DIEs.  */
18879       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
18880         load_full_comp_unit (per_cu, cu->language);
18881 
18882       target_cu = per_cu->cu;
18883     }
18884   else if (cu->dies == NULL)
18885     {
18886       /* We're loading full DIEs during partial symbol reading.  */
18887       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
18888       load_full_comp_unit (cu->per_cu, language_minimal);
18889     }
18890 
18891   *ref_cu = target_cu;
18892   temp_die.offset = offset;
18893   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
18894 }
18895 
18896 /* Follow reference attribute ATTR of SRC_DIE.
18897    On entry *REF_CU is the CU of SRC_DIE.
18898    On exit *REF_CU is the CU of the result.  */
18899 
18900 static struct die_info *
18901 follow_die_ref (struct die_info *src_die, const struct attribute *attr,
18902                 struct dwarf2_cu **ref_cu)
18903 {
18904   sect_offset offset = dwarf2_get_ref_die_offset (attr);
18905   struct dwarf2_cu *cu = *ref_cu;
18906   struct die_info *die;
18907 
18908   die = follow_die_offset (offset,
18909                            (attr->form == DW_FORM_GNU_ref_alt
18910                             || cu->per_cu->is_dwz),
18911                            ref_cu);
18912   if (!die)
18913     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
18914            "at 0x%x [in module %s]"),
18915            offset.sect_off, src_die->offset.sect_off,
18916            objfile_name (cu->objfile));
18917 
18918   return die;
18919 }
18920 
18921 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
18922    Returned value is intended for DW_OP_call*.  Returned
18923    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */
18924 
18925 struct dwarf2_locexpr_baton
18926 dwarf2_fetch_die_loc_sect_off (sect_offset offset,
18927                                struct dwarf2_per_cu_data *per_cu,
18928                                CORE_ADDR (*get_frame_pc) (void *baton),
18929                                void *baton)
18930 {
18931   struct dwarf2_cu *cu;
18932   struct die_info *die;
18933   struct attribute *attr;
18934   struct dwarf2_locexpr_baton retval;
18935 
18936   dw2_setup (per_cu->objfile);
18937 
18938   if (per_cu->cu == NULL)
18939     load_cu (per_cu);
18940   cu = per_cu->cu;
18941 
18942   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
18943   if (!die)
18944     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
18945            offset.sect_off, objfile_name (per_cu->objfile));
18946 
18947   attr = dwarf2_attr (die, DW_AT_location, cu);
18948   if (!attr)
18949     {
18950       /* DWARF: "If there is no such attribute, then there is no effect.".
18951          DATA is ignored if SIZE is 0.  */
18952 
18953       retval.data = NULL;
18954       retval.size = 0;
18955     }
18956   else if (attr_form_is_section_offset (attr))
18957     {
18958       struct dwarf2_loclist_baton loclist_baton;
18959       CORE_ADDR pc = (*get_frame_pc) (baton);
18960       size_t size;
18961 
18962       fill_in_loclist_baton (cu, &loclist_baton, attr);
18963 
18964       retval.data = dwarf2_find_location_expression (&loclist_baton,
18965                                                      &size, pc);
18966       retval.size = size;
18967     }
18968   else
18969     {
18970       if (!attr_form_is_block (attr))
18971         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
18972                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
18973                offset.sect_off, objfile_name (per_cu->objfile));
18974 
18975       retval.data = DW_BLOCK (attr)->data;
18976       retval.size = DW_BLOCK (attr)->size;
18977     }
18978   retval.per_cu = cu->per_cu;
18979 
18980   age_cached_comp_units ();
18981 
18982   return retval;
18983 }
18984 
18985 /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
18986    offset.  */
18987 
18988 struct dwarf2_locexpr_baton
18989 dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
18990                              struct dwarf2_per_cu_data *per_cu,
18991                              CORE_ADDR (*get_frame_pc) (void *baton),
18992                              void *baton)
18993 {
18994   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
18995 
18996   return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
18997 }
18998 
18999 /* Write a constant of a given type as target-ordered bytes into
19000    OBSTACK.  */
19001 
19002 static const gdb_byte *
19003 write_constant_as_bytes (struct obstack *obstack,
19004                          enum bfd_endian byte_order,
19005                          struct type *type,
19006                          ULONGEST value,
19007                          LONGEST *len)
19008 {
19009   gdb_byte *result;
19010 
19011   *len = TYPE_LENGTH (type);
19012   result = obstack_alloc (obstack, *len);
19013   store_unsigned_integer (result, *len, byte_order, value);
19014 
19015   return result;
19016 }
19017 
19018 /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
19019    pointer to the constant bytes and set LEN to the length of the
19020    data.  If memory is needed, allocate it on OBSTACK.  If the DIE
19021    does not have a DW_AT_const_value, return NULL.  */
19022 
19023 const gdb_byte *
19024 dwarf2_fetch_constant_bytes (sect_offset offset,
19025                              struct dwarf2_per_cu_data *per_cu,
19026                              struct obstack *obstack,
19027                              LONGEST *len)
19028 {
19029   struct dwarf2_cu *cu;
19030   struct die_info *die;
19031   struct attribute *attr;
19032   const gdb_byte *result = NULL;
19033   struct type *type;
19034   LONGEST value;
19035   enum bfd_endian byte_order;
19036 
19037   dw2_setup (per_cu->objfile);
19038 
19039   if (per_cu->cu == NULL)
19040     load_cu (per_cu);
19041   cu = per_cu->cu;
19042 
19043   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
19044   if (!die)
19045     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
19046            offset.sect_off, objfile_name (per_cu->objfile));
19047 
19048 
19049   attr = dwarf2_attr (die, DW_AT_const_value, cu);
19050   if (attr == NULL)
19051     return NULL;
19052 
19053   byte_order = (bfd_big_endian (per_cu->objfile->obfd)
19054                 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
19055 
19056   switch (attr->form)
19057     {
19058     case DW_FORM_addr:
19059     case DW_FORM_GNU_addr_index:
19060       {
19061         gdb_byte *tem;
19062 
19063         *len = cu->header.addr_size;
19064         tem = obstack_alloc (obstack, *len);
19065         store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
19066         result = tem;
19067       }
19068       break;
19069     case DW_FORM_string:
19070     case DW_FORM_strp:
19071     case DW_FORM_GNU_str_index:
19072     case DW_FORM_GNU_strp_alt:
19073       /* DW_STRING is already allocated on the objfile obstack, point
19074          directly to it.  */
19075       result = (const gdb_byte *) DW_STRING (attr);
19076       *len = strlen (DW_STRING (attr));
19077       break;
19078     case DW_FORM_block1:
19079     case DW_FORM_block2:
19080     case DW_FORM_block4:
19081     case DW_FORM_block:
19082     case DW_FORM_exprloc:
19083       result = DW_BLOCK (attr)->data;
19084       *len = DW_BLOCK (attr)->size;
19085       break;
19086 
19087       /* The DW_AT_const_value attributes are supposed to carry the
19088          symbol's value "represented as it would be on the target
19089          architecture."  By the time we get here, it's already been
19090          converted to host endianness, so we just need to sign- or
19091          zero-extend it as appropriate.  */
19092     case DW_FORM_data1:
19093       type = die_type (die, cu);
19094       result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
19095       if (result == NULL)
19096         result = write_constant_as_bytes (obstack, byte_order,
19097                                           type, value, len);
19098       break;
19099     case DW_FORM_data2:
19100       type = die_type (die, cu);
19101       result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
19102       if (result == NULL)
19103         result = write_constant_as_bytes (obstack, byte_order,
19104                                           type, value, len);
19105       break;
19106     case DW_FORM_data4:
19107       type = die_type (die, cu);
19108       result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
19109       if (result == NULL)
19110         result = write_constant_as_bytes (obstack, byte_order,
19111                                           type, value, len);
19112       break;
19113     case DW_FORM_data8:
19114       type = die_type (die, cu);
19115       result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
19116       if (result == NULL)
19117         result = write_constant_as_bytes (obstack, byte_order,
19118                                           type, value, len);
19119       break;
19120 
19121     case DW_FORM_sdata:
19122       type = die_type (die, cu);
19123       result = write_constant_as_bytes (obstack, byte_order,
19124                                         type, DW_SND (attr), len);
19125       break;
19126 
19127     case DW_FORM_udata:
19128       type = die_type (die, cu);
19129       result = write_constant_as_bytes (obstack, byte_order,
19130                                         type, DW_UNSND (attr), len);
19131       break;
19132 
19133     default:
19134       complaint (&symfile_complaints,
19135                  _("unsupported const value attribute form: '%s'"),
19136                  dwarf_form_name (attr->form));
19137       break;
19138     }
19139 
19140   return result;
19141 }
19142 
19143 /* Return the type of the DIE at DIE_OFFSET in the CU named by
19144    PER_CU.  */
19145 
19146 struct type *
19147 dwarf2_get_die_type (cu_offset die_offset,
19148                      struct dwarf2_per_cu_data *per_cu)
19149 {
19150   sect_offset die_offset_sect;
19151 
19152   dw2_setup (per_cu->objfile);
19153 
19154   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
19155   return get_die_type_at_offset (die_offset_sect, per_cu);
19156 }
19157 
19158 /* Follow type unit SIG_TYPE referenced by SRC_DIE.
19159    On entry *REF_CU is the CU of SRC_DIE.
19160    On exit *REF_CU is the CU of the result.
19161    Returns NULL if the referenced DIE isn't found.  */
19162 
19163 static struct die_info *
19164 follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
19165                   struct dwarf2_cu **ref_cu)
19166 {
19167   struct objfile *objfile = (*ref_cu)->objfile;
19168   struct die_info temp_die;
19169   struct dwarf2_cu *sig_cu;
19170   struct die_info *die;
19171 
19172   /* While it might be nice to assert sig_type->type == NULL here,
19173      we can get here for DW_AT_imported_declaration where we need
19174      the DIE not the type.  */
19175 
19176   /* If necessary, add it to the queue and load its DIEs.  */
19177 
19178   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
19179     read_signatured_type (sig_type);
19180 
19181   sig_cu = sig_type->per_cu.cu;
19182   gdb_assert (sig_cu != NULL);
19183   gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
19184   temp_die.offset = sig_type->type_offset_in_section;
19185   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
19186                              temp_die.offset.sect_off);
19187   if (die)
19188     {
19189       /* For .gdb_index version 7 keep track of included TUs.
19190          http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
19191       if (dwarf2_per_objfile->index_table != NULL
19192           && dwarf2_per_objfile->index_table->version <= 7)
19193         {
19194           VEC_safe_push (dwarf2_per_cu_ptr,
19195                          (*ref_cu)->per_cu->imported_symtabs,
19196                          sig_cu->per_cu);
19197         }
19198 
19199       *ref_cu = sig_cu;
19200       return die;
19201     }
19202 
19203   return NULL;
19204 }
19205 
19206 /* Follow signatured type referenced by ATTR in SRC_DIE.
19207    On entry *REF_CU is the CU of SRC_DIE.
19208    On exit *REF_CU is the CU of the result.
19209    The result is the DIE of the type.
19210    If the referenced type cannot be found an error is thrown.  */
19211 
19212 static struct die_info *
19213 follow_die_sig (struct die_info *src_die, const struct attribute *attr,
19214                 struct dwarf2_cu **ref_cu)
19215 {
19216   ULONGEST signature = DW_SIGNATURE (attr);
19217   struct signatured_type *sig_type;
19218   struct die_info *die;
19219 
19220   gdb_assert (attr->form == DW_FORM_ref_sig8);
19221 
19222   sig_type = lookup_signatured_type (*ref_cu, signature);
19223   /* sig_type will be NULL if the signatured type is missing from
19224      the debug info.  */
19225   if (sig_type == NULL)
19226     {
19227       error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
19228                " from DIE at 0x%x [in module %s]"),
19229              hex_string (signature), src_die->offset.sect_off,
19230              objfile_name ((*ref_cu)->objfile));
19231     }
19232 
19233   die = follow_die_sig_1 (src_die, sig_type, ref_cu);
19234   if (die == NULL)
19235     {
19236       dump_die_for_error (src_die);
19237       error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
19238                " from DIE at 0x%x [in module %s]"),
19239              hex_string (signature), src_die->offset.sect_off,
19240              objfile_name ((*ref_cu)->objfile));
19241     }
19242 
19243   return die;
19244 }
19245 
19246 /* Get the type specified by SIGNATURE referenced in DIE/CU,
19247    reading in and processing the type unit if necessary.  */
19248 
19249 static struct type *
19250 get_signatured_type (struct die_info *die, ULONGEST signature,
19251                      struct dwarf2_cu *cu)
19252 {
19253   struct signatured_type *sig_type;
19254   struct dwarf2_cu *type_cu;
19255   struct die_info *type_die;
19256   struct type *type;
19257 
19258   sig_type = lookup_signatured_type (cu, signature);
19259   /* sig_type will be NULL if the signatured type is missing from
19260      the debug info.  */
19261   if (sig_type == NULL)
19262     {
19263       complaint (&symfile_complaints,
19264                  _("Dwarf Error: Cannot find signatured DIE %s referenced"
19265                    " from DIE at 0x%x [in module %s]"),
19266                  hex_string (signature), die->offset.sect_off,
19267                  objfile_name (dwarf2_per_objfile->objfile));
19268       return build_error_marker_type (cu, die);
19269     }
19270 
19271   /* If we already know the type we're done.  */
19272   if (sig_type->type != NULL)
19273     return sig_type->type;
19274 
19275   type_cu = cu;
19276   type_die = follow_die_sig_1 (die, sig_type, &type_cu);
19277   if (type_die != NULL)
19278     {
19279       /* N.B. We need to call get_die_type to ensure only one type for this DIE
19280          is created.  This is important, for example, because for c++ classes
19281          we need TYPE_NAME set which is only done by new_symbol.  Blech.  */
19282       type = read_type_die (type_die, type_cu);
19283       if (type == NULL)
19284         {
19285           complaint (&symfile_complaints,
19286                      _("Dwarf Error: Cannot build signatured type %s"
19287                        " referenced from DIE at 0x%x [in module %s]"),
19288                      hex_string (signature), die->offset.sect_off,
19289                      objfile_name (dwarf2_per_objfile->objfile));
19290           type = build_error_marker_type (cu, die);
19291         }
19292     }
19293   else
19294     {
19295       complaint (&symfile_complaints,
19296                  _("Dwarf Error: Problem reading signatured DIE %s referenced"
19297                    " from DIE at 0x%x [in module %s]"),
19298                  hex_string (signature), die->offset.sect_off,
19299                  objfile_name (dwarf2_per_objfile->objfile));
19300       type = build_error_marker_type (cu, die);
19301     }
19302   sig_type->type = type;
19303 
19304   return type;
19305 }
19306 
19307 /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
19308    reading in and processing the type unit if necessary.  */
19309 
19310 static struct type *
19311 get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
19312                           struct dwarf2_cu *cu) /* ARI: editCase function */
19313 {
19314   /* Yes, DW_AT_signature can use a non-ref_sig8 reference.  */
19315   if (attr_form_is_ref (attr))
19316     {
19317       struct dwarf2_cu *type_cu = cu;
19318       struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
19319 
19320       return read_type_die (type_die, type_cu);
19321     }
19322   else if (attr->form == DW_FORM_ref_sig8)
19323     {
19324       return get_signatured_type (die, DW_SIGNATURE (attr), cu);
19325     }
19326   else
19327     {
19328       complaint (&symfile_complaints,
19329                  _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
19330                    " at 0x%x [in module %s]"),
19331                  dwarf_form_name (attr->form), die->offset.sect_off,
19332                  objfile_name (dwarf2_per_objfile->objfile));
19333       return build_error_marker_type (cu, die);
19334     }
19335 }
19336 
19337 /* Load the DIEs associated with type unit PER_CU into memory.  */
19338 
19339 static void
19340 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
19341 {
19342   struct signatured_type *sig_type;
19343 
19344   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
19345   gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));
19346 
19347   /* We have the per_cu, but we need the signatured_type.
19348      Fortunately this is an easy translation.  */
19349   gdb_assert (per_cu->is_debug_types);
19350   sig_type = (struct signatured_type *) per_cu;
19351 
19352   gdb_assert (per_cu->cu == NULL);
19353 
19354   read_signatured_type (sig_type);
19355 
19356   gdb_assert (per_cu->cu != NULL);
19357 }
19358 
19359 /* die_reader_func for read_signatured_type.
19360    This is identical to load_full_comp_unit_reader,
19361    but is kept separate for now.  */
19362 
19363 static void
19364 read_signatured_type_reader (const struct die_reader_specs *reader,
19365                              const gdb_byte *info_ptr,
19366                              struct die_info *comp_unit_die,
19367                              int has_children,
19368                              void *data)
19369 {
19370   struct dwarf2_cu *cu = reader->cu;
19371 
19372   gdb_assert (cu->die_hash == NULL);
19373   cu->die_hash =
19374     htab_create_alloc_ex (cu->header.length / 12,
19375                           die_hash,
19376                           die_eq,
19377                           NULL,
19378                           &cu->comp_unit_obstack,
19379                           hashtab_obstack_allocate,
19380                           dummy_obstack_deallocate);
19381 
19382   if (has_children)
19383     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
19384                                                   &info_ptr, comp_unit_die);
19385   cu->dies = comp_unit_die;
19386   /* comp_unit_die is not stored in die_hash, no need.  */
19387 
19388   /* We try not to read any attributes in this function, because not
19389      all CUs needed for references have been loaded yet, and symbol
19390      table processing isn't initialized.  But we have to set the CU language,
19391      or we won't be able to build types correctly.
19392      Similarly, if we do not read the producer, we can not apply
19393      producer-specific interpretation.  */
19394   prepare_one_comp_unit (cu, cu->dies, language_minimal);
19395 }
19396 
19397 /* Read in a signatured type and build its CU and DIEs.
19398    If the type is a stub for the real type in a DWO file,
19399    read in the real type from the DWO file as well.  */
19400 
19401 static void
19402 read_signatured_type (struct signatured_type *sig_type)
19403 {
19404   struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
19405 
19406   gdb_assert (per_cu->is_debug_types);
19407   gdb_assert (per_cu->cu == NULL);
19408 
19409   init_cutu_and_read_dies (per_cu, NULL, 0, 1,
19410                            read_signatured_type_reader, NULL);
19411   sig_type->per_cu.tu_read = 1;
19412 }
19413 
19414 /* Decode simple location descriptions.
19415    Given a pointer to a dwarf block that defines a location, compute
19416    the location and return the value.
19417 
19418    NOTE drow/2003-11-18: This function is called in two situations
19419    now: for the address of static or global variables (partial symbols
19420    only) and for offsets into structures which are expected to be
19421    (more or less) constant.  The partial symbol case should go away,
19422    and only the constant case should remain.  That will let this
19423    function complain more accurately.  A few special modes are allowed
19424    without complaint for global variables (for instance, global
19425    register values and thread-local values).
19426 
19427    A location description containing no operations indicates that the
19428    object is optimized out.  The return value is 0 for that case.
19429    FIXME drow/2003-11-16: No callers check for this case any more; soon all
19430    callers will only want a very basic result and this can become a
19431    complaint.
19432 
19433    Note that stack[0] is unused except as a default error return.  */
19434 
19435 static CORE_ADDR
19436 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
19437 {
19438   struct objfile *objfile = cu->objfile;
19439   size_t i;
19440   size_t size = blk->size;
19441   const gdb_byte *data = blk->data;
19442   CORE_ADDR stack[64];
19443   int stacki;
19444   unsigned int bytes_read, unsnd;
19445   gdb_byte op;
19446 
19447   i = 0;
19448   stacki = 0;
19449   stack[stacki] = 0;
19450   stack[++stacki] = 0;
19451 
19452   while (i < size)
19453     {
19454       op = data[i++];
19455       switch (op)
19456         {
19457         case DW_OP_lit0:
19458         case DW_OP_lit1:
19459         case DW_OP_lit2:
19460         case DW_OP_lit3:
19461         case DW_OP_lit4:
19462         case DW_OP_lit5:
19463         case DW_OP_lit6:
19464         case DW_OP_lit7:
19465         case DW_OP_lit8:
19466         case DW_OP_lit9:
19467         case DW_OP_lit10:
19468         case DW_OP_lit11:
19469         case DW_OP_lit12:
19470         case DW_OP_lit13:
19471         case DW_OP_lit14:
19472         case DW_OP_lit15:
19473         case DW_OP_lit16:
19474         case DW_OP_lit17:
19475         case DW_OP_lit18:
19476         case DW_OP_lit19:
19477         case DW_OP_lit20:
19478         case DW_OP_lit21:
19479         case DW_OP_lit22:
19480         case DW_OP_lit23:
19481         case DW_OP_lit24:
19482         case DW_OP_lit25:
19483         case DW_OP_lit26:
19484         case DW_OP_lit27:
19485         case DW_OP_lit28:
19486         case DW_OP_lit29:
19487         case DW_OP_lit30:
19488         case DW_OP_lit31:
19489           stack[++stacki] = op - DW_OP_lit0;
19490           break;
19491 
19492         case DW_OP_reg0:
19493         case DW_OP_reg1:
19494         case DW_OP_reg2:
19495         case DW_OP_reg3:
19496         case DW_OP_reg4:
19497         case DW_OP_reg5:
19498         case DW_OP_reg6:
19499         case DW_OP_reg7:
19500         case DW_OP_reg8:
19501         case DW_OP_reg9:
19502         case DW_OP_reg10:
19503         case DW_OP_reg11:
19504         case DW_OP_reg12:
19505         case DW_OP_reg13:
19506         case DW_OP_reg14:
19507         case DW_OP_reg15:
19508         case DW_OP_reg16:
19509         case DW_OP_reg17:
19510         case DW_OP_reg18:
19511         case DW_OP_reg19:
19512         case DW_OP_reg20:
19513         case DW_OP_reg21:
19514         case DW_OP_reg22:
19515         case DW_OP_reg23:
19516         case DW_OP_reg24:
19517         case DW_OP_reg25:
19518         case DW_OP_reg26:
19519         case DW_OP_reg27:
19520         case DW_OP_reg28:
19521         case DW_OP_reg29:
19522         case DW_OP_reg30:
19523         case DW_OP_reg31:
19524           stack[++stacki] = op - DW_OP_reg0;
19525           if (i < size)
19526             dwarf2_complex_location_expr_complaint ();
19527           break;
19528 
19529         case DW_OP_regx:
19530           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
19531           i += bytes_read;
19532           stack[++stacki] = unsnd;
19533           if (i < size)
19534             dwarf2_complex_location_expr_complaint ();
19535           break;
19536 
19537         case DW_OP_addr:
19538           stack[++stacki] = read_address (objfile->obfd, &data[i],
19539                                           cu, &bytes_read);
19540           i += bytes_read;
19541           break;
19542 
19543         case DW_OP_const1u:
19544           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
19545           i += 1;
19546           break;
19547 
19548         case DW_OP_const1s:
19549           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
19550           i += 1;
19551           break;
19552 
19553         case DW_OP_const2u:
19554           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
19555           i += 2;
19556           break;
19557 
19558         case DW_OP_const2s:
19559           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
19560           i += 2;
19561           break;
19562 
19563         case DW_OP_const4u:
19564           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
19565           i += 4;
19566           break;
19567 
19568         case DW_OP_const4s:
19569           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
19570           i += 4;
19571           break;
19572 
19573         case DW_OP_const8u:
19574           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
19575           i += 8;
19576           break;
19577 
19578         case DW_OP_constu:
19579           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
19580                                                   &bytes_read);
19581           i += bytes_read;
19582           break;
19583 
19584         case DW_OP_consts:
19585           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
19586           i += bytes_read;
19587           break;
19588 
19589         case DW_OP_dup:
19590           stack[stacki + 1] = stack[stacki];
19591           stacki++;
19592           break;
19593 
19594         case DW_OP_plus:
19595           stack[stacki - 1] += stack[stacki];
19596           stacki--;
19597           break;
19598 
19599         case DW_OP_plus_uconst:
19600           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
19601                                                  &bytes_read);
19602           i += bytes_read;
19603           break;
19604 
19605         case DW_OP_minus:
19606           stack[stacki - 1] -= stack[stacki];
19607           stacki--;
19608           break;
19609 
19610         case DW_OP_deref:
19611           /* If we're not the last op, then we definitely can't encode
19612              this using GDB's address_class enum.  This is valid for partial
19613              global symbols, although the variable's address will be bogus
19614              in the psymtab.  */
19615           if (i < size)
19616             dwarf2_complex_location_expr_complaint ();
19617           break;
19618 
19619         case DW_OP_GNU_push_tls_address:
19620           /* The top of the stack has the offset from the beginning
19621              of the thread control block at which the variable is located.  */
19622           /* Nothing should follow this operator, so the top of stack would
19623              be returned.  */
19624           /* This is valid for partial global symbols, but the variable's
19625              address will be bogus in the psymtab.  Make it always at least
19626              non-zero to not look as a variable garbage collected by linker
19627              which have DW_OP_addr 0.  */
19628           if (i < size)
19629             dwarf2_complex_location_expr_complaint ();
19630           stack[stacki]++;
19631           break;
19632 
19633         case DW_OP_GNU_uninit:
19634           break;
19635 
19636         case DW_OP_GNU_addr_index:
19637         case DW_OP_GNU_const_index:
19638           stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
19639                                                          &bytes_read);
19640           i += bytes_read;
19641           break;
19642 
19643         default:
19644           {
19645             const char *name = get_DW_OP_name (op);
19646 
19647             if (name)
19648               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
19649                          name);
19650             else
19651               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
19652                          op);
19653           }
19654 
19655           return (stack[stacki]);
19656         }
19657 
19658       /* Enforce maximum stack depth of SIZE-1 to avoid writing
19659          outside of the allocated space.  Also enforce minimum>0.  */
19660       if (stacki >= ARRAY_SIZE (stack) - 1)
19661         {
19662           complaint (&symfile_complaints,
19663                      _("location description stack overflow"));
19664           return 0;
19665         }
19666 
19667       if (stacki <= 0)
19668         {
19669           complaint (&symfile_complaints,
19670                      _("location description stack underflow"));
19671           return 0;
19672         }
19673     }
19674   return (stack[stacki]);
19675 }
19676 
19677 /* memory allocation interface */
19678 
19679 static struct dwarf_block *
19680 dwarf_alloc_block (struct dwarf2_cu *cu)
19681 {
19682   struct dwarf_block *blk;
19683 
19684   blk = (struct dwarf_block *)
19685     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
19686   return (blk);
19687 }
19688 
19689 static struct die_info *
19690 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
19691 {
19692   struct die_info *die;
19693   size_t size = sizeof (struct die_info);
19694 
19695   if (num_attrs > 1)
19696     size += (num_attrs - 1) * sizeof (struct attribute);
19697 
19698   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
19699   memset (die, 0, sizeof (struct die_info));
19700   return (die);
19701 }
19702 
19703 
19704 /* Macro support.  */
19705 
19706 /* Return file name relative to the compilation directory of file number I in
19707    *LH's file name table.  The result is allocated using xmalloc; the caller is
19708    responsible for freeing it.  */
19709 
19710 static char *
19711 file_file_name (int file, struct line_header *lh)
19712 {
19713   /* Is the file number a valid index into the line header's file name
19714      table?  Remember that file numbers start with one, not zero.  */
19715   if (1 <= file && file <= lh->num_file_names)
19716     {
19717       struct file_entry *fe = &lh->file_names[file - 1];
19718 
19719       if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
19720         return xstrdup (fe->name);
19721       return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
19722                      fe->name, NULL);
19723     }
19724   else
19725     {
19726       /* The compiler produced a bogus file number.  We can at least
19727          record the macro definitions made in the file, even if we
19728          won't be able to find the file by name.  */
19729       char fake_name[80];
19730 
19731       xsnprintf (fake_name, sizeof (fake_name),
19732                  "<bad macro file number %d>", file);
19733 
19734       complaint (&symfile_complaints,
19735                  _("bad file number in macro information (%d)"),
19736                  file);
19737 
19738       return xstrdup (fake_name);
19739     }
19740 }
19741 
19742 /* Return the full name of file number I in *LH's file name table.
19743    Use COMP_DIR as the name of the current directory of the
19744    compilation.  The result is allocated using xmalloc; the caller is
19745    responsible for freeing it.  */
19746 static char *
19747 file_full_name (int file, struct line_header *lh, const char *comp_dir)
19748 {
19749   /* Is the file number a valid index into the line header's file name
19750      table?  Remember that file numbers start with one, not zero.  */
19751   if (1 <= file && file <= lh->num_file_names)
19752     {
19753       char *relative = file_file_name (file, lh);
19754 
19755       if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
19756         return relative;
19757       return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
19758     }
19759   else
19760     return file_file_name (file, lh);
19761 }
19762 
19763 
19764 static struct macro_source_file *
19765 macro_start_file (int file, int line,
19766                   struct macro_source_file *current_file,
19767                   const char *comp_dir,
19768                   struct line_header *lh, struct objfile *objfile)
19769 {
19770   /* File name relative to the compilation directory of this source file.  */
19771   char *file_name = file_file_name (file, lh);
19772 
19773   if (! current_file)
19774     {
19775       /* Note: We don't create a macro table for this compilation unit
19776          at all until we actually get a filename.  */
19777       struct macro_table *macro_table = get_macro_table (objfile, comp_dir);
19778 
19779       /* If we have no current file, then this must be the start_file
19780          directive for the compilation unit's main source file.  */
19781       current_file = macro_set_main (macro_table, file_name);
19782       macro_define_special (macro_table);
19783     }
19784   else
19785     current_file = macro_include (current_file, line, file_name);
19786 
19787   xfree (file_name);
19788 
19789   return current_file;
19790 }
19791 
19792 
19793 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
19794    followed by a null byte.  */
19795 static char *
19796 copy_string (const char *buf, int len)
19797 {
19798   char *s = xmalloc (len + 1);
19799 
19800   memcpy (s, buf, len);
19801   s[len] = '\0';
19802   return s;
19803 }
19804 
19805 
19806 static const char *
19807 consume_improper_spaces (const char *p, const char *body)
19808 {
19809   if (*p == ' ')
19810     {
19811       complaint (&symfile_complaints,
19812                  _("macro definition contains spaces "
19813                    "in formal argument list:\n`%s'"),
19814                  body);
19815 
19816       while (*p == ' ')
19817         p++;
19818     }
19819 
19820   return p;
19821 }
19822 
19823 
19824 static void
19825 parse_macro_definition (struct macro_source_file *file, int line,
19826                         const char *body)
19827 {
19828   const char *p;
19829 
19830   /* The body string takes one of two forms.  For object-like macro
19831      definitions, it should be:
19832 
19833         <macro name> " " <definition>
19834 
19835      For function-like macro definitions, it should be:
19836 
19837         <macro name> "() " <definition>
19838      or
19839         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
19840 
19841      Spaces may appear only where explicitly indicated, and in the
19842      <definition>.
19843 
19844      The Dwarf 2 spec says that an object-like macro's name is always
19845      followed by a space, but versions of GCC around March 2002 omit
19846      the space when the macro's definition is the empty string.
19847 
19848      The Dwarf 2 spec says that there should be no spaces between the
19849      formal arguments in a function-like macro's formal argument list,
19850      but versions of GCC around March 2002 include spaces after the
19851      commas.  */
19852 
19853 
19854   /* Find the extent of the macro name.  The macro name is terminated
19855      by either a space or null character (for an object-like macro) or
19856      an opening paren (for a function-like macro).  */
19857   for (p = body; *p; p++)
19858     if (*p == ' ' || *p == '(')
19859       break;
19860 
19861   if (*p == ' ' || *p == '\0')
19862     {
19863       /* It's an object-like macro.  */
19864       int name_len = p - body;
19865       char *name = copy_string (body, name_len);
19866       const char *replacement;
19867 
19868       if (*p == ' ')
19869         replacement = body + name_len + 1;
19870       else
19871         {
19872           dwarf2_macro_malformed_definition_complaint (body);
19873           replacement = body + name_len;
19874         }
19875 
19876       macro_define_object (file, line, name, replacement);
19877 
19878       xfree (name);
19879     }
19880   else if (*p == '(')
19881     {
19882       /* It's a function-like macro.  */
19883       char *name = copy_string (body, p - body);
19884       int argc = 0;
19885       int argv_size = 1;
19886       char **argv = xmalloc (argv_size * sizeof (*argv));
19887 
19888       p++;
19889 
19890       p = consume_improper_spaces (p, body);
19891 
19892       /* Parse the formal argument list.  */
19893       while (*p && *p != ')')
19894         {
19895           /* Find the extent of the current argument name.  */
19896           const char *arg_start = p;
19897 
19898           while (*p && *p != ',' && *p != ')' && *p != ' ')
19899             p++;
19900 
19901           if (! *p || p == arg_start)
19902             dwarf2_macro_malformed_definition_complaint (body);
19903           else
19904             {
19905               /* Make sure argv has room for the new argument.  */
19906               if (argc >= argv_size)
19907                 {
19908                   argv_size *= 2;
19909                   argv = xrealloc (argv, argv_size * sizeof (*argv));
19910                 }
19911 
19912               argv[argc++] = copy_string (arg_start, p - arg_start);
19913             }
19914 
19915           p = consume_improper_spaces (p, body);
19916 
19917           /* Consume the comma, if present.  */
19918           if (*p == ',')
19919             {
19920               p++;
19921 
19922               p = consume_improper_spaces (p, body);
19923             }
19924         }
19925 
19926       if (*p == ')')
19927         {
19928           p++;
19929 
19930           if (*p == ' ')
19931             /* Perfectly formed definition, no complaints.  */
19932             macro_define_function (file, line, name,
19933                                    argc, (const char **) argv,
19934                                    p + 1);
19935           else if (*p == '\0')
19936             {
19937               /* Complain, but do define it.  */
19938               dwarf2_macro_malformed_definition_complaint (body);
19939               macro_define_function (file, line, name,
19940                                      argc, (const char **) argv,
19941                                      p);
19942             }
19943           else
19944             /* Just complain.  */
19945             dwarf2_macro_malformed_definition_complaint (body);
19946         }
19947       else
19948         /* Just complain.  */
19949         dwarf2_macro_malformed_definition_complaint (body);
19950 
19951       xfree (name);
19952       {
19953         int i;
19954 
19955         for (i = 0; i < argc; i++)
19956           xfree (argv[i]);
19957       }
19958       xfree (argv);
19959     }
19960   else
19961     dwarf2_macro_malformed_definition_complaint (body);
19962 }
19963 
19964 /* Skip some bytes from BYTES according to the form given in FORM.
19965    Returns the new pointer.  */
19966 
19967 static const gdb_byte *
19968 skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
19969                  enum dwarf_form form,
19970                  unsigned int offset_size,
19971                  struct dwarf2_section_info *section)
19972 {
19973   unsigned int bytes_read;
19974 
19975   switch (form)
19976     {
19977     case DW_FORM_data1:
19978     case DW_FORM_flag:
19979       ++bytes;
19980       break;
19981 
19982     case DW_FORM_data2:
19983       bytes += 2;
19984       break;
19985 
19986     case DW_FORM_data4:
19987       bytes += 4;
19988       break;
19989 
19990     case DW_FORM_data8:
19991       bytes += 8;
19992       break;
19993 
19994     case DW_FORM_string:
19995       read_direct_string (abfd, bytes, &bytes_read);
19996       bytes += bytes_read;
19997       break;
19998 
19999     case DW_FORM_sec_offset:
20000     case DW_FORM_strp:
20001     case DW_FORM_GNU_strp_alt:
20002       bytes += offset_size;
20003       break;
20004 
20005     case DW_FORM_block:
20006       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
20007       bytes += bytes_read;
20008       break;
20009 
20010     case DW_FORM_block1:
20011       bytes += 1 + read_1_byte (abfd, bytes);
20012       break;
20013     case DW_FORM_block2:
20014       bytes += 2 + read_2_bytes (abfd, bytes);
20015       break;
20016     case DW_FORM_block4:
20017       bytes += 4 + read_4_bytes (abfd, bytes);
20018       break;
20019 
20020     case DW_FORM_sdata:
20021     case DW_FORM_udata:
20022     case DW_FORM_GNU_addr_index:
20023     case DW_FORM_GNU_str_index:
20024       bytes = gdb_skip_leb128 (bytes, buffer_end);
20025       if (bytes == NULL)
20026         {
20027           dwarf2_section_buffer_overflow_complaint (section);
20028           return NULL;
20029         }
20030       break;
20031 
20032     default:
20033       {
20034       complain:
20035         complaint (&symfile_complaints,
20036                    _("invalid form 0x%x in `%s'"),
20037                    form, get_section_name (section));
20038         return NULL;
20039       }
20040     }
20041 
20042   return bytes;
20043 }
20044 
20045 /* A helper for dwarf_decode_macros that handles skipping an unknown
20046    opcode.  Returns an updated pointer to the macro data buffer; or,
20047    on error, issues a complaint and returns NULL.  */
20048 
20049 static const gdb_byte *
20050 skip_unknown_opcode (unsigned int opcode,
20051                      const gdb_byte **opcode_definitions,
20052                      const gdb_byte *mac_ptr, const gdb_byte *mac_end,
20053                      bfd *abfd,
20054                      unsigned int offset_size,
20055                      struct dwarf2_section_info *section)
20056 {
20057   unsigned int bytes_read, i;
20058   unsigned long arg;
20059   const gdb_byte *defn;
20060 
20061   if (opcode_definitions[opcode] == NULL)
20062     {
20063       complaint (&symfile_complaints,
20064                  _("unrecognized DW_MACFINO opcode 0x%x"),
20065                  opcode);
20066       return NULL;
20067     }
20068 
20069   defn = opcode_definitions[opcode];
20070   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
20071   defn += bytes_read;
20072 
20073   for (i = 0; i < arg; ++i)
20074     {
20075       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
20076                                  section);
20077       if (mac_ptr == NULL)
20078         {
20079           /* skip_form_bytes already issued the complaint.  */
20080           return NULL;
20081         }
20082     }
20083 
20084   return mac_ptr;
20085 }
20086 
20087 /* A helper function which parses the header of a macro section.
20088    If the macro section is the extended (for now called "GNU") type,
20089    then this updates *OFFSET_SIZE.  Returns a pointer to just after
20090    the header, or issues a complaint and returns NULL on error.  */
20091 
20092 static const gdb_byte *
20093 dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
20094                           bfd *abfd,
20095                           const gdb_byte *mac_ptr,
20096                           unsigned int *offset_size,
20097                           int section_is_gnu)
20098 {
20099   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
20100 
20101   if (section_is_gnu)
20102     {
20103       unsigned int version, flags;
20104 
20105       version = read_2_bytes (abfd, mac_ptr);
20106       if (version != 4)
20107         {
20108           complaint (&symfile_complaints,
20109                      _("unrecognized version `%d' in .debug_macro section"),
20110                      version);
20111           return NULL;
20112         }
20113       mac_ptr += 2;
20114 
20115       flags = read_1_byte (abfd, mac_ptr);
20116       ++mac_ptr;
20117       *offset_size = (flags & 1) ? 8 : 4;
20118 
20119       if ((flags & 2) != 0)
20120         /* We don't need the line table offset.  */
20121         mac_ptr += *offset_size;
20122 
20123       /* Vendor opcode descriptions.  */
20124       if ((flags & 4) != 0)
20125         {
20126           unsigned int i, count;
20127 
20128           count = read_1_byte (abfd, mac_ptr);
20129           ++mac_ptr;
20130           for (i = 0; i < count; ++i)
20131             {
20132               unsigned int opcode, bytes_read;
20133               unsigned long arg;
20134 
20135               opcode = read_1_byte (abfd, mac_ptr);
20136               ++mac_ptr;
20137               opcode_definitions[opcode] = mac_ptr;
20138               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20139               mac_ptr += bytes_read;
20140               mac_ptr += arg;
20141             }
20142         }
20143     }
20144 
20145   return mac_ptr;
20146 }
20147 
20148 /* A helper for dwarf_decode_macros that handles the GNU extensions,
20149    including DW_MACRO_GNU_transparent_include.  */
20150 
20151 static void
20152 dwarf_decode_macro_bytes (bfd *abfd,
20153                           const gdb_byte *mac_ptr, const gdb_byte *mac_end,
20154                           struct macro_source_file *current_file,
20155                           struct line_header *lh, const char *comp_dir,
20156                           struct dwarf2_section_info *section,
20157                           int section_is_gnu, int section_is_dwz,
20158                           unsigned int offset_size,
20159                           struct objfile *objfile,
20160                           htab_t include_hash)
20161 {
20162   enum dwarf_macro_record_type macinfo_type;
20163   int at_commandline;
20164   const gdb_byte *opcode_definitions[256];
20165 
20166   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
20167                                       &offset_size, section_is_gnu);
20168   if (mac_ptr == NULL)
20169     {
20170       /* We already issued a complaint.  */
20171       return;
20172     }
20173 
20174   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
20175      GDB is still reading the definitions from command line.  First
20176      DW_MACINFO_start_file will need to be ignored as it was already executed
20177      to create CURRENT_FILE for the main source holding also the command line
20178      definitions.  On first met DW_MACINFO_start_file this flag is reset to
20179      normally execute all the remaining DW_MACINFO_start_file macinfos.  */
20180 
20181   at_commandline = 1;
20182 
20183   do
20184     {
20185       /* Do we at least have room for a macinfo type byte?  */
20186       if (mac_ptr >= mac_end)
20187         {
20188           dwarf2_section_buffer_overflow_complaint (section);
20189           break;
20190         }
20191 
20192       macinfo_type = read_1_byte (abfd, mac_ptr);
20193       mac_ptr++;
20194 
20195       /* Note that we rely on the fact that the corresponding GNU and
20196          DWARF constants are the same.  */
20197       switch (macinfo_type)
20198         {
20199           /* A zero macinfo type indicates the end of the macro
20200              information.  */
20201         case 0:
20202           break;
20203 
20204         case DW_MACRO_GNU_define:
20205         case DW_MACRO_GNU_undef:
20206         case DW_MACRO_GNU_define_indirect:
20207         case DW_MACRO_GNU_undef_indirect:
20208         case DW_MACRO_GNU_define_indirect_alt:
20209         case DW_MACRO_GNU_undef_indirect_alt:
20210           {
20211             unsigned int bytes_read;
20212             int line;
20213             const char *body;
20214             int is_define;
20215 
20216             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20217             mac_ptr += bytes_read;
20218 
20219             if (macinfo_type == DW_MACRO_GNU_define
20220                 || macinfo_type == DW_MACRO_GNU_undef)
20221               {
20222                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
20223                 mac_ptr += bytes_read;
20224               }
20225             else
20226               {
20227                 LONGEST str_offset;
20228 
20229                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
20230                 mac_ptr += offset_size;
20231 
20232                 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
20233                     || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
20234                     || section_is_dwz)
20235                   {
20236                     struct dwz_file *dwz = dwarf2_get_dwz_file ();
20237 
20238                     body = read_indirect_string_from_dwz (dwz, str_offset);
20239                   }
20240                 else
20241                   body = read_indirect_string_at_offset (abfd, str_offset);
20242               }
20243 
20244             is_define = (macinfo_type == DW_MACRO_GNU_define
20245                          || macinfo_type == DW_MACRO_GNU_define_indirect
20246                          || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
20247             if (! current_file)
20248               {
20249                 /* DWARF violation as no main source is present.  */
20250                 complaint (&symfile_complaints,
20251                            _("debug info with no main source gives macro %s "
20252                              "on line %d: %s"),
20253                            is_define ? _("definition") : _("undefinition"),
20254                            line, body);
20255                 break;
20256               }
20257             if ((line == 0 && !at_commandline)
20258                 || (line != 0 && at_commandline))
20259               complaint (&symfile_complaints,
20260                          _("debug info gives %s macro %s with %s line %d: %s"),
20261                          at_commandline ? _("command-line") : _("in-file"),
20262                          is_define ? _("definition") : _("undefinition"),
20263                          line == 0 ? _("zero") : _("non-zero"), line, body);
20264 
20265             if (is_define)
20266               parse_macro_definition (current_file, line, body);
20267             else
20268               {
20269                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
20270                             || macinfo_type == DW_MACRO_GNU_undef_indirect
20271                             || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
20272                 macro_undef (current_file, line, body);
20273               }
20274           }
20275           break;
20276 
20277         case DW_MACRO_GNU_start_file:
20278           {
20279             unsigned int bytes_read;
20280             int line, file;
20281 
20282             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20283             mac_ptr += bytes_read;
20284             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20285             mac_ptr += bytes_read;
20286 
20287             if ((line == 0 && !at_commandline)
20288                 || (line != 0 && at_commandline))
20289               complaint (&symfile_complaints,
20290                          _("debug info gives source %d included "
20291                            "from %s at %s line %d"),
20292                          file, at_commandline ? _("command-line") : _("file"),
20293                          line == 0 ? _("zero") : _("non-zero"), line);
20294 
20295             if (at_commandline)
20296               {
20297                 /* This DW_MACRO_GNU_start_file was executed in the
20298                    pass one.  */
20299                 at_commandline = 0;
20300               }
20301             else
20302               current_file = macro_start_file (file, line,
20303                                                current_file, comp_dir,
20304                                                lh, objfile);
20305           }
20306           break;
20307 
20308         case DW_MACRO_GNU_end_file:
20309           if (! current_file)
20310             complaint (&symfile_complaints,
20311                        _("macro debug info has an unmatched "
20312                          "`close_file' directive"));
20313           else
20314             {
20315               current_file = current_file->included_by;
20316               if (! current_file)
20317                 {
20318                   enum dwarf_macro_record_type next_type;
20319 
20320                   /* GCC circa March 2002 doesn't produce the zero
20321                      type byte marking the end of the compilation
20322                      unit.  Complain if it's not there, but exit no
20323                      matter what.  */
20324 
20325                   /* Do we at least have room for a macinfo type byte?  */
20326                   if (mac_ptr >= mac_end)
20327                     {
20328                       dwarf2_section_buffer_overflow_complaint (section);
20329                       return;
20330                     }
20331 
20332                   /* We don't increment mac_ptr here, so this is just
20333                      a look-ahead.  */
20334                   next_type = read_1_byte (abfd, mac_ptr);
20335                   if (next_type != 0)
20336                     complaint (&symfile_complaints,
20337                                _("no terminating 0-type entry for "
20338                                  "macros in `.debug_macinfo' section"));
20339 
20340                   return;
20341                 }
20342             }
20343           break;
20344 
20345         case DW_MACRO_GNU_transparent_include:
20346         case DW_MACRO_GNU_transparent_include_alt:
20347           {
20348             LONGEST offset;
20349             void **slot;
20350             bfd *include_bfd = abfd;
20351             struct dwarf2_section_info *include_section = section;
20352             struct dwarf2_section_info alt_section;
20353             const gdb_byte *include_mac_end = mac_end;
20354             int is_dwz = section_is_dwz;
20355             const gdb_byte *new_mac_ptr;
20356 
20357             offset = read_offset_1 (abfd, mac_ptr, offset_size);
20358             mac_ptr += offset_size;
20359 
20360             if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
20361               {
20362                 struct dwz_file *dwz = dwarf2_get_dwz_file ();
20363 
20364                 dwarf2_read_section (dwarf2_per_objfile->objfile,
20365                                      &dwz->macro);
20366 
20367                 include_section = &dwz->macro;
20368                 include_bfd = get_section_bfd_owner (include_section);
20369                 include_mac_end = dwz->macro.buffer + dwz->macro.size;
20370                 is_dwz = 1;
20371               }
20372 
20373             new_mac_ptr = include_section->buffer + offset;
20374             slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);
20375 
20376             if (*slot != NULL)
20377               {
20378                 /* This has actually happened; see
20379                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
20380                 complaint (&symfile_complaints,
20381                            _("recursive DW_MACRO_GNU_transparent_include in "
20382                              ".debug_macro section"));
20383               }
20384             else
20385               {
20386                 *slot = (void *) new_mac_ptr;
20387 
20388                 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
20389                                           include_mac_end, current_file,
20390                                           lh, comp_dir,
20391                                           section, section_is_gnu, is_dwz,
20392                                           offset_size, objfile, include_hash);
20393 
20394                 htab_remove_elt (include_hash, (void *) new_mac_ptr);
20395               }
20396           }
20397           break;
20398 
20399         case DW_MACINFO_vendor_ext:
20400           if (!section_is_gnu)
20401             {
20402               unsigned int bytes_read;
20403               int constant;
20404 
20405               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20406               mac_ptr += bytes_read;
20407               read_direct_string (abfd, mac_ptr, &bytes_read);
20408               mac_ptr += bytes_read;
20409 
20410               /* We don't recognize any vendor extensions.  */
20411               break;
20412             }
20413           /* FALLTHROUGH */
20414 
20415         default:
20416           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
20417                                          mac_ptr, mac_end, abfd, offset_size,
20418                                          section);
20419           if (mac_ptr == NULL)
20420             return;
20421           break;
20422         }
20423     } while (macinfo_type != 0);
20424 }
20425 
20426 static void
20427 dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
20428                      const char *comp_dir, int section_is_gnu)
20429 {
20430   struct objfile *objfile = dwarf2_per_objfile->objfile;
20431   struct line_header *lh = cu->line_header;
20432   bfd *abfd;
20433   const gdb_byte *mac_ptr, *mac_end;
20434   struct macro_source_file *current_file = 0;
20435   enum dwarf_macro_record_type macinfo_type;
20436   unsigned int offset_size = cu->header.offset_size;
20437   const gdb_byte *opcode_definitions[256];
20438   struct cleanup *cleanup;
20439   htab_t include_hash;
20440   void **slot;
20441   struct dwarf2_section_info *section;
20442   const char *section_name;
20443 
20444   if (cu->dwo_unit != NULL)
20445     {
20446       if (section_is_gnu)
20447         {
20448           section = &cu->dwo_unit->dwo_file->sections.macro;
20449           section_name = ".debug_macro.dwo";
20450         }
20451       else
20452         {
20453           section = &cu->dwo_unit->dwo_file->sections.macinfo;
20454           section_name = ".debug_macinfo.dwo";
20455         }
20456     }
20457   else
20458     {
20459       if (section_is_gnu)
20460         {
20461           section = &dwarf2_per_objfile->macro;
20462           section_name = ".debug_macro";
20463         }
20464       else
20465         {
20466           section = &dwarf2_per_objfile->macinfo;
20467           section_name = ".debug_macinfo";
20468         }
20469     }
20470 
20471   dwarf2_read_section (objfile, section);
20472   if (section->buffer == NULL)
20473     {
20474       complaint (&symfile_complaints, _("missing %s section"), section_name);
20475       return;
20476     }
20477   abfd = get_section_bfd_owner (section);
20478 
20479   /* First pass: Find the name of the base filename.
20480      This filename is needed in order to process all macros whose definition
20481      (or undefinition) comes from the command line.  These macros are defined
20482      before the first DW_MACINFO_start_file entry, and yet still need to be
20483      associated to the base file.
20484 
20485      To determine the base file name, we scan the macro definitions until we
20486      reach the first DW_MACINFO_start_file entry.  We then initialize
20487      CURRENT_FILE accordingly so that any macro definition found before the
20488      first DW_MACINFO_start_file can still be associated to the base file.  */
20489 
20490   mac_ptr = section->buffer + offset;
20491   mac_end = section->buffer + section->size;
20492 
20493   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
20494                                       &offset_size, section_is_gnu);
20495   if (mac_ptr == NULL)
20496     {
20497       /* We already issued a complaint.  */
20498       return;
20499     }
20500 
20501   do
20502     {
20503       /* Do we at least have room for a macinfo type byte?  */
20504       if (mac_ptr >= mac_end)
20505         {
20506           /* Complaint is printed during the second pass as GDB will probably
20507              stop the first pass earlier upon finding
20508              DW_MACINFO_start_file.  */
20509           break;
20510         }
20511 
20512       macinfo_type = read_1_byte (abfd, mac_ptr);
20513       mac_ptr++;
20514 
20515       /* Note that we rely on the fact that the corresponding GNU and
20516          DWARF constants are the same.  */
20517       switch (macinfo_type)
20518         {
20519           /* A zero macinfo type indicates the end of the macro
20520              information.  */
20521         case 0:
20522           break;
20523 
20524         case DW_MACRO_GNU_define:
20525         case DW_MACRO_GNU_undef:
20526           /* Only skip the data by MAC_PTR.  */
20527           {
20528             unsigned int bytes_read;
20529 
20530             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20531             mac_ptr += bytes_read;
20532             read_direct_string (abfd, mac_ptr, &bytes_read);
20533             mac_ptr += bytes_read;
20534           }
20535           break;
20536 
20537         case DW_MACRO_GNU_start_file:
20538           {
20539             unsigned int bytes_read;
20540             int line, file;
20541 
20542             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20543             mac_ptr += bytes_read;
20544             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20545             mac_ptr += bytes_read;
20546 
20547             current_file = macro_start_file (file, line, current_file,
20548                                              comp_dir, lh, objfile);
20549           }
20550           break;
20551 
20552         case DW_MACRO_GNU_end_file:
20553           /* No data to skip by MAC_PTR.  */
20554           break;
20555 
20556         case DW_MACRO_GNU_define_indirect:
20557         case DW_MACRO_GNU_undef_indirect:
20558         case DW_MACRO_GNU_define_indirect_alt:
20559         case DW_MACRO_GNU_undef_indirect_alt:
20560           {
20561             unsigned int bytes_read;
20562 
20563             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20564             mac_ptr += bytes_read;
20565             mac_ptr += offset_size;
20566           }
20567           break;
20568 
20569         case DW_MACRO_GNU_transparent_include:
20570         case DW_MACRO_GNU_transparent_include_alt:
20571           /* Note that, according to the spec, a transparent include
20572              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
20573              skip this opcode.  */
20574           mac_ptr += offset_size;
20575           break;
20576 
20577         case DW_MACINFO_vendor_ext:
20578           /* Only skip the data by MAC_PTR.  */
20579           if (!section_is_gnu)
20580             {
20581               unsigned int bytes_read;
20582 
20583               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
20584               mac_ptr += bytes_read;
20585               read_direct_string (abfd, mac_ptr, &bytes_read);
20586               mac_ptr += bytes_read;
20587             }
20588           /* FALLTHROUGH */
20589 
20590         default:
20591           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
20592                                          mac_ptr, mac_end, abfd, offset_size,
20593                                          section);
20594           if (mac_ptr == NULL)
20595             return;
20596           break;
20597         }
20598     } while (macinfo_type != 0 && current_file == NULL);
20599 
20600   /* Second pass: Process all entries.
20601 
20602      Use the AT_COMMAND_LINE flag to determine whether we are still processing
20603      command-line macro definitions/undefinitions.  This flag is unset when we
20604      reach the first DW_MACINFO_start_file entry.  */
20605 
20606   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
20607                                     NULL, xcalloc, xfree);
20608   cleanup = make_cleanup_htab_delete (include_hash);
20609   mac_ptr = section->buffer + offset;
20610   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
20611   *slot = (void *) mac_ptr;
20612   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
20613                             current_file, lh, comp_dir, section,
20614                             section_is_gnu, 0,
20615                             offset_size, objfile, include_hash);
20616   do_cleanups (cleanup);
20617 }
20618 
20619 /* Check if the attribute's form is a DW_FORM_block*
20620    if so return true else false.  */
20621 
20622 static int
20623 attr_form_is_block (const struct attribute *attr)
20624 {
20625   return (attr == NULL ? 0 :
20626       attr->form == DW_FORM_block1
20627       || attr->form == DW_FORM_block2
20628       || attr->form == DW_FORM_block4
20629       || attr->form == DW_FORM_block
20630       || attr->form == DW_FORM_exprloc);
20631 }
20632 
20633 /* Return non-zero if ATTR's value is a section offset --- classes
20634    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
20635    You may use DW_UNSND (attr) to retrieve such offsets.
20636 
20637    Section 7.5.4, "Attribute Encodings", explains that no attribute
20638    may have a value that belongs to more than one of these classes; it
20639    would be ambiguous if we did, because we use the same forms for all
20640    of them.  */
20641 
20642 static int
20643 attr_form_is_section_offset (const struct attribute *attr)
20644 {
20645   return (attr->form == DW_FORM_data4
20646           || attr->form == DW_FORM_data8
20647           || attr->form == DW_FORM_sec_offset);
20648 }
20649 
20650 /* Return non-zero if ATTR's value falls in the 'constant' class, or
20651    zero otherwise.  When this function returns true, you can apply
20652    dwarf2_get_attr_constant_value to it.
20653 
20654    However, note that for some attributes you must check
20655    attr_form_is_section_offset before using this test.  DW_FORM_data4
20656    and DW_FORM_data8 are members of both the constant class, and of
20657    the classes that contain offsets into other debug sections
20658    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
20659    that, if an attribute's can be either a constant or one of the
20660    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
20661    taken as section offsets, not constants.  */
20662 
20663 static int
20664 attr_form_is_constant (const struct attribute *attr)
20665 {
20666   switch (attr->form)
20667     {
20668     case DW_FORM_sdata:
20669     case DW_FORM_udata:
20670     case DW_FORM_data1:
20671     case DW_FORM_data2:
20672     case DW_FORM_data4:
20673     case DW_FORM_data8:
20674       return 1;
20675     default:
20676       return 0;
20677     }
20678 }
20679 
20680 
20681 /* DW_ADDR is always stored already as sect_offset; despite for the forms
20682    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */
20683 
20684 static int
20685 attr_form_is_ref (const struct attribute *attr)
20686 {
20687   switch (attr->form)
20688     {
20689     case DW_FORM_ref_addr:
20690     case DW_FORM_ref1:
20691     case DW_FORM_ref2:
20692     case DW_FORM_ref4:
20693     case DW_FORM_ref8:
20694     case DW_FORM_ref_udata:
20695     case DW_FORM_GNU_ref_alt:
20696       return 1;
20697     default:
20698       return 0;
20699     }
20700 }
20701 
20702 /* Return the .debug_loc section to use for CU.
20703    For DWO files use .debug_loc.dwo.  */
20704 
20705 static struct dwarf2_section_info *
20706 cu_debug_loc_section (struct dwarf2_cu *cu)
20707 {
20708   if (cu->dwo_unit)
20709     return &cu->dwo_unit->dwo_file->sections.loc;
20710   return &dwarf2_per_objfile->loc;
20711 }
20712 
20713 /* A helper function that fills in a dwarf2_loclist_baton.  */
20714 
20715 static void
20716 fill_in_loclist_baton (struct dwarf2_cu *cu,
20717                        struct dwarf2_loclist_baton *baton,
20718                        const struct attribute *attr)
20719 {
20720   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20721 
20722   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
20723 
20724   baton->per_cu = cu->per_cu;
20725   gdb_assert (baton->per_cu);
20726   /* We don't know how long the location list is, but make sure we
20727      don't run off the edge of the section.  */
20728   baton->size = section->size - DW_UNSND (attr);
20729   baton->data = section->buffer + DW_UNSND (attr);
20730   baton->base_address = cu->base_address;
20731   baton->from_dwo = cu->dwo_unit != NULL;
20732 }
20733 
20734 static void
20735 dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
20736                              struct dwarf2_cu *cu, int is_block)
20737 {
20738   struct objfile *objfile = dwarf2_per_objfile->objfile;
20739   struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20740 
20741   if (attr_form_is_section_offset (attr)
20742       /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
20743          the section.  If so, fall through to the complaint in the
20744          other branch.  */
20745       && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
20746     {
20747       struct dwarf2_loclist_baton *baton;
20748 
20749       baton = obstack_alloc (&objfile->objfile_obstack,
20750                              sizeof (struct dwarf2_loclist_baton));
20751 
20752       fill_in_loclist_baton (cu, baton, attr);
20753 
20754       if (cu->base_known == 0)
20755         complaint (&symfile_complaints,
20756                    _("Location list used without "
20757                      "specifying the CU base address."));
20758 
20759       SYMBOL_ACLASS_INDEX (sym) = (is_block
20760                                    ? dwarf2_loclist_block_index
20761                                    : dwarf2_loclist_index);
20762       SYMBOL_LOCATION_BATON (sym) = baton;
20763     }
20764   else
20765     {
20766       struct dwarf2_locexpr_baton *baton;
20767 
20768       baton = obstack_alloc (&objfile->objfile_obstack,
20769                              sizeof (struct dwarf2_locexpr_baton));
20770       baton->per_cu = cu->per_cu;
20771       gdb_assert (baton->per_cu);
20772 
20773       if (attr_form_is_block (attr))
20774         {
20775           /* Note that we're just copying the block's data pointer
20776              here, not the actual data.  We're still pointing into the
20777              info_buffer for SYM's objfile; right now we never release
20778              that buffer, but when we do clean up properly this may
20779              need to change.  */
20780           baton->size = DW_BLOCK (attr)->size;
20781           baton->data = DW_BLOCK (attr)->data;
20782         }
20783       else
20784         {
20785           dwarf2_invalid_attrib_class_complaint ("location description",
20786                                                  SYMBOL_NATURAL_NAME (sym));
20787           baton->size = 0;
20788         }
20789 
20790       SYMBOL_ACLASS_INDEX (sym) = (is_block
20791                                    ? dwarf2_locexpr_block_index
20792                                    : dwarf2_locexpr_index);
20793       SYMBOL_LOCATION_BATON (sym) = baton;
20794     }
20795 }
20796 
20797 /* Return the OBJFILE associated with the compilation unit CU.  If CU
20798    came from a separate debuginfo file, then the master objfile is
20799    returned.  */
20800 
20801 struct objfile *
20802 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
20803 {
20804   struct objfile *objfile = per_cu->objfile;
20805 
20806   /* Return the master objfile, so that we can report and look up the
20807      correct file containing this variable.  */
20808   if (objfile->separate_debug_objfile_backlink)
20809     objfile = objfile->separate_debug_objfile_backlink;
20810 
20811   return objfile;
20812 }
20813 
20814 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
20815    (CU_HEADERP is unused in such case) or prepare a temporary copy at
20816    CU_HEADERP first.  */
20817 
20818 static const struct comp_unit_head *
20819 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
20820                        struct dwarf2_per_cu_data *per_cu)
20821 {
20822   const gdb_byte *info_ptr;
20823 
20824   if (per_cu->cu)
20825     return &per_cu->cu->header;
20826 
20827   info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;
20828 
20829   memset (cu_headerp, 0, sizeof (*cu_headerp));
20830   read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);
20831 
20832   return cu_headerp;
20833 }
20834 
20835 /* Return the address size given in the compilation unit header for CU.  */
20836 
20837 int
20838 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
20839 {
20840   struct comp_unit_head cu_header_local;
20841   const struct comp_unit_head *cu_headerp;
20842 
20843   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20844 
20845   return cu_headerp->addr_size;
20846 }
20847 
20848 /* Return the offset size given in the compilation unit header for CU.  */
20849 
20850 int
20851 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
20852 {
20853   struct comp_unit_head cu_header_local;
20854   const struct comp_unit_head *cu_headerp;
20855 
20856   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20857 
20858   return cu_headerp->offset_size;
20859 }
20860 
20861 /* See its dwarf2loc.h declaration.  */
20862 
20863 int
20864 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
20865 {
20866   struct comp_unit_head cu_header_local;
20867   const struct comp_unit_head *cu_headerp;
20868 
20869   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
20870 
20871   if (cu_headerp->version == 2)
20872     return cu_headerp->addr_size;
20873   else
20874     return cu_headerp->offset_size;
20875 }
20876 
20877 /* Return the text offset of the CU.  The returned offset comes from
20878    this CU's objfile.  If this objfile came from a separate debuginfo
20879    file, then the offset may be different from the corresponding
20880    offset in the parent objfile.  */
20881 
20882 CORE_ADDR
20883 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
20884 {
20885   struct objfile *objfile = per_cu->objfile;
20886 
20887   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
20888 }
20889 
20890 /* Locate the .debug_info compilation unit from CU's objfile which contains
20891    the DIE at OFFSET.  Raises an error on failure.  */
20892 
20893 static struct dwarf2_per_cu_data *
20894 dwarf2_find_containing_comp_unit (sect_offset offset,
20895                                   unsigned int offset_in_dwz,
20896                                   struct objfile *objfile)
20897 {
20898   struct dwarf2_per_cu_data *this_cu;
20899   int low, high;
20900   const sect_offset *cu_off;
20901 
20902   low = 0;
20903   high = dwarf2_per_objfile->n_comp_units - 1;
20904   while (high > low)
20905     {
20906       struct dwarf2_per_cu_data *mid_cu;
20907       int mid = low + (high - low) / 2;
20908 
20909       mid_cu = dwarf2_per_objfile->all_comp_units[mid];
20910       cu_off = &mid_cu->offset;
20911       if (mid_cu->is_dwz > offset_in_dwz
20912           || (mid_cu->is_dwz == offset_in_dwz
20913               && cu_off->sect_off >= offset.sect_off))
20914         high = mid;
20915       else
20916         low = mid + 1;
20917     }
20918   gdb_assert (low == high);
20919   this_cu = dwarf2_per_objfile->all_comp_units[low];
20920   cu_off = &this_cu->offset;
20921   if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
20922     {
20923       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
20924         error (_("Dwarf Error: could not find partial DIE containing "
20925                "offset 0x%lx [in module %s]"),
20926                (long) offset.sect_off, bfd_get_filename (objfile->obfd));
20927 
20928       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
20929                   <= offset.sect_off);
20930       return dwarf2_per_objfile->all_comp_units[low-1];
20931     }
20932   else
20933     {
20934       this_cu = dwarf2_per_objfile->all_comp_units[low];
20935       if (low == dwarf2_per_objfile->n_comp_units - 1
20936           && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
20937         error (_("invalid dwarf2 offset %u"), offset.sect_off);
20938       gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
20939       return this_cu;
20940     }
20941 }
20942 
20943 /* Initialize dwarf2_cu CU, owned by PER_CU.  */
20944 
20945 static void
20946 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
20947 {
20948   memset (cu, 0, sizeof (*cu));
20949   per_cu->cu = cu;
20950   cu->per_cu = per_cu;
20951   cu->objfile = per_cu->objfile;
20952   obstack_init (&cu->comp_unit_obstack);
20953 }
20954 
20955 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */
20956 
20957 static void
20958 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
20959                        enum language pretend_language)
20960 {
20961   struct attribute *attr;
20962 
20963   /* Set the language we're debugging.  */
20964   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
20965   if (attr)
20966     set_cu_language (DW_UNSND (attr), cu);
20967   else
20968     {
20969       cu->language = pretend_language;
20970       cu->language_defn = language_def (cu->language);
20971     }
20972 
20973   attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
20974   if (attr)
20975     cu->producer = DW_STRING (attr);
20976 }
20977 
20978 /* Release one cached compilation unit, CU.  We unlink it from the tree
20979    of compilation units, but we don't remove it from the read_in_chain;
20980    the caller is responsible for that.
20981    NOTE: DATA is a void * because this function is also used as a
20982    cleanup routine.  */
20983 
20984 static void
20985 free_heap_comp_unit (void *data)
20986 {
20987   struct dwarf2_cu *cu = data;
20988 
20989   gdb_assert (cu->per_cu != NULL);
20990   cu->per_cu->cu = NULL;
20991   cu->per_cu = NULL;
20992 
20993   obstack_free (&cu->comp_unit_obstack, NULL);
20994 
20995   xfree (cu);
20996 }
20997 
20998 /* This cleanup function is passed the address of a dwarf2_cu on the stack
20999    when we're finished with it.  We can't free the pointer itself, but be
21000    sure to unlink it from the cache.  Also release any associated storage.  */
21001 
21002 static void
21003 free_stack_comp_unit (void *data)
21004 {
21005   struct dwarf2_cu *cu = data;
21006 
21007   gdb_assert (cu->per_cu != NULL);
21008   cu->per_cu->cu = NULL;
21009   cu->per_cu = NULL;
21010 
21011   obstack_free (&cu->comp_unit_obstack, NULL);
21012   cu->partial_dies = NULL;
21013 }
21014 
21015 /* Free all cached compilation units.  */
21016 
21017 static void
21018 free_cached_comp_units (void *data)
21019 {
21020   struct dwarf2_per_cu_data *per_cu, **last_chain;
21021 
21022   per_cu = dwarf2_per_objfile->read_in_chain;
21023   last_chain = &dwarf2_per_objfile->read_in_chain;
21024   while (per_cu != NULL)
21025     {
21026       struct dwarf2_per_cu_data *next_cu;
21027 
21028       next_cu = per_cu->cu->read_in_chain;
21029 
21030       free_heap_comp_unit (per_cu->cu);
21031       *last_chain = next_cu;
21032 
21033       per_cu = next_cu;
21034     }
21035 }
21036 
21037 /* Increase the age counter on each cached compilation unit, and free
21038    any that are too old.  */
21039 
21040 static void
21041 age_cached_comp_units (void)
21042 {
21043   struct dwarf2_per_cu_data *per_cu, **last_chain;
21044 
21045   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
21046   per_cu = dwarf2_per_objfile->read_in_chain;
21047   while (per_cu != NULL)
21048     {
21049       per_cu->cu->last_used ++;
21050       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
21051         dwarf2_mark (per_cu->cu);
21052       per_cu = per_cu->cu->read_in_chain;
21053     }
21054 
21055   per_cu = dwarf2_per_objfile->read_in_chain;
21056   last_chain = &dwarf2_per_objfile->read_in_chain;
21057   while (per_cu != NULL)
21058     {
21059       struct dwarf2_per_cu_data *next_cu;
21060 
21061       next_cu = per_cu->cu->read_in_chain;
21062 
21063       if (!per_cu->cu->mark)
21064         {
21065           free_heap_comp_unit (per_cu->cu);
21066           *last_chain = next_cu;
21067         }
21068       else
21069         last_chain = &per_cu->cu->read_in_chain;
21070 
21071       per_cu = next_cu;
21072     }
21073 }
21074 
21075 /* Remove a single compilation unit from the cache.  */
21076 
21077 static void
21078 free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
21079 {
21080   struct dwarf2_per_cu_data *per_cu, **last_chain;
21081 
21082   per_cu = dwarf2_per_objfile->read_in_chain;
21083   last_chain = &dwarf2_per_objfile->read_in_chain;
21084   while (per_cu != NULL)
21085     {
21086       struct dwarf2_per_cu_data *next_cu;
21087 
21088       next_cu = per_cu->cu->read_in_chain;
21089 
21090       if (per_cu == target_per_cu)
21091         {
21092           free_heap_comp_unit (per_cu->cu);
21093           per_cu->cu = NULL;
21094           *last_chain = next_cu;
21095           break;
21096         }
21097       else
21098         last_chain = &per_cu->cu->read_in_chain;
21099 
21100       per_cu = next_cu;
21101     }
21102 }
21103 
21104 /* Release all extra memory associated with OBJFILE.  */
21105 
21106 void
21107 dwarf2_free_objfile (struct objfile *objfile)
21108 {
21109   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
21110 
21111   if (dwarf2_per_objfile == NULL)
21112     return;
21113 
21114   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
21115   free_cached_comp_units (NULL);
21116 
21117   if (dwarf2_per_objfile->quick_file_names_table)
21118     htab_delete (dwarf2_per_objfile->quick_file_names_table);
21119 
21120   /* Everything else should be on the objfile obstack.  */
21121 }
21122 
21123 /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
21124    We store these in a hash table separate from the DIEs, and preserve them
21125    when the DIEs are flushed out of cache.
21126 
21127    The CU "per_cu" pointer is needed because offset alone is not enough to
21128    uniquely identify the type.  A file may have multiple .debug_types sections,
21129    or the type may come from a DWO file.  Furthermore, while it's more logical
21130    to use per_cu->section+offset, with Fission the section with the data is in
21131    the DWO file but we don't know that section at the point we need it.
21132    We have to use something in dwarf2_per_cu_data (or the pointer to it)
21133    because we can enter the lookup routine, get_die_type_at_offset, from
21134    outside this file, and thus won't necessarily have PER_CU->cu.
21135    Fortunately, PER_CU is stable for the life of the objfile.  */
21136 
21137 struct dwarf2_per_cu_offset_and_type
21138 {
21139   const struct dwarf2_per_cu_data *per_cu;
21140   sect_offset offset;
21141   struct type *type;
21142 };
21143 
21144 /* Hash function for a dwarf2_per_cu_offset_and_type.  */
21145 
21146 static hashval_t
21147 per_cu_offset_and_type_hash (const void *item)
21148 {
21149   const struct dwarf2_per_cu_offset_and_type *ofs = item;
21150 
21151   return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
21152 }
21153 
21154 /* Equality function for a dwarf2_per_cu_offset_and_type.  */
21155 
21156 static int
21157 per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
21158 {
21159   const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
21160   const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;
21161 
21162   return (ofs_lhs->per_cu == ofs_rhs->per_cu
21163           && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
21164 }
21165 
21166 /* Set the type associated with DIE to TYPE.  Save it in CU's hash
21167    table if necessary.  For convenience, return TYPE.
21168 
21169    The DIEs reading must have careful ordering to:
21170     * Not cause infite loops trying to read in DIEs as a prerequisite for
21171       reading current DIE.
21172     * Not trying to dereference contents of still incompletely read in types
21173       while reading in other DIEs.
21174     * Enable referencing still incompletely read in types just by a pointer to
21175       the type without accessing its fields.
21176 
21177    Therefore caller should follow these rules:
21178      * Try to fetch any prerequisite types we may need to build this DIE type
21179        before building the type and calling set_die_type.
21180      * After building type call set_die_type for current DIE as soon as
21181        possible before fetching more types to complete the current type.
21182      * Make the type as complete as possible before fetching more types.  */
21183 
21184 static struct type *
21185 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
21186 {
21187   struct dwarf2_per_cu_offset_and_type **slot, ofs;
21188   struct objfile *objfile = cu->objfile;
21189 
21190   /* For Ada types, make sure that the gnat-specific data is always
21191      initialized (if not already set).  There are a few types where
21192      we should not be doing so, because the type-specific area is
21193      already used to hold some other piece of info (eg: TYPE_CODE_FLT
21194      where the type-specific area is used to store the floatformat).
21195      But this is not a problem, because the gnat-specific information
21196      is actually not needed for these types.  */
21197   if (need_gnat_info (cu)
21198       && TYPE_CODE (type) != TYPE_CODE_FUNC
21199       && TYPE_CODE (type) != TYPE_CODE_FLT
21200       && !HAVE_GNAT_AUX_INFO (type))
21201     INIT_GNAT_SPECIFIC (type);
21202 
21203   if (dwarf2_per_objfile->die_type_hash == NULL)
21204     {
21205       dwarf2_per_objfile->die_type_hash =
21206         htab_create_alloc_ex (127,
21207                               per_cu_offset_and_type_hash,
21208                               per_cu_offset_and_type_eq,
21209                               NULL,
21210                               &objfile->objfile_obstack,
21211                               hashtab_obstack_allocate,
21212                               dummy_obstack_deallocate);
21213     }
21214 
21215   ofs.per_cu = cu->per_cu;
21216   ofs.offset = die->offset;
21217   ofs.type = type;
21218   slot = (struct dwarf2_per_cu_offset_and_type **)
21219     htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
21220   if (*slot)
21221     complaint (&symfile_complaints,
21222                _("A problem internal to GDB: DIE 0x%x has type already set"),
21223                die->offset.sect_off);
21224   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
21225   **slot = ofs;
21226   return type;
21227 }
21228 
21229 /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
21230    or return NULL if the die does not have a saved type.  */
21231 
21232 static struct type *
21233 get_die_type_at_offset (sect_offset offset,
21234                         struct dwarf2_per_cu_data *per_cu)
21235 {
21236   struct dwarf2_per_cu_offset_and_type *slot, ofs;
21237 
21238   if (dwarf2_per_objfile->die_type_hash == NULL)
21239     return NULL;
21240 
21241   ofs.per_cu = per_cu;
21242   ofs.offset = offset;
21243   slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
21244   if (slot)
21245     return slot->type;
21246   else
21247     return NULL;
21248 }
21249 
21250 /* Look up the type for DIE in CU in die_type_hash,
21251    or return NULL if DIE does not have a saved type.  */
21252 
21253 static struct type *
21254 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
21255 {
21256   return get_die_type_at_offset (die->offset, cu->per_cu);
21257 }
21258 
21259 /* Add a dependence relationship from CU to REF_PER_CU.  */
21260 
21261 static void
21262 dwarf2_add_dependence (struct dwarf2_cu *cu,
21263                        struct dwarf2_per_cu_data *ref_per_cu)
21264 {
21265   void **slot;
21266 
21267   if (cu->dependencies == NULL)
21268     cu->dependencies
21269       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
21270                               NULL, &cu->comp_unit_obstack,
21271                               hashtab_obstack_allocate,
21272                               dummy_obstack_deallocate);
21273 
21274   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
21275   if (*slot == NULL)
21276     *slot = ref_per_cu;
21277 }
21278 
21279 /* Subroutine of dwarf2_mark to pass to htab_traverse.
21280    Set the mark field in every compilation unit in the
21281    cache that we must keep because we are keeping CU.  */
21282 
21283 static int
21284 dwarf2_mark_helper (void **slot, void *data)
21285 {
21286   struct dwarf2_per_cu_data *per_cu;
21287 
21288   per_cu = (struct dwarf2_per_cu_data *) *slot;
21289 
21290   /* cu->dependencies references may not yet have been ever read if QUIT aborts
21291      reading of the chain.  As such dependencies remain valid it is not much
21292      useful to track and undo them during QUIT cleanups.  */
21293   if (per_cu->cu == NULL)
21294     return 1;
21295 
21296   if (per_cu->cu->mark)
21297     return 1;
21298   per_cu->cu->mark = 1;
21299 
21300   if (per_cu->cu->dependencies != NULL)
21301     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
21302 
21303   return 1;
21304 }
21305 
21306 /* Set the mark field in CU and in every other compilation unit in the
21307    cache that we must keep because we are keeping CU.  */
21308 
21309 static void
21310 dwarf2_mark (struct dwarf2_cu *cu)
21311 {
21312   if (cu->mark)
21313     return;
21314   cu->mark = 1;
21315   if (cu->dependencies != NULL)
21316     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
21317 }
21318 
21319 static void
21320 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
21321 {
21322   while (per_cu)
21323     {
21324       per_cu->cu->mark = 0;
21325       per_cu = per_cu->cu->read_in_chain;
21326     }
21327 }
21328 
21329 /* Trivial hash function for partial_die_info: the hash value of a DIE
21330    is its offset in .debug_info for this objfile.  */
21331 
21332 static hashval_t
21333 partial_die_hash (const void *item)
21334 {
21335   const struct partial_die_info *part_die = item;
21336 
21337   return part_die->offset.sect_off;
21338 }
21339 
21340 /* Trivial comparison function for partial_die_info structures: two DIEs
21341    are equal if they have the same offset.  */
21342 
21343 static int
21344 partial_die_eq (const void *item_lhs, const void *item_rhs)
21345 {
21346   const struct partial_die_info *part_die_lhs = item_lhs;
21347   const struct partial_die_info *part_die_rhs = item_rhs;
21348 
21349   return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
21350 }
21351 
21352 static struct cmd_list_element *set_dwarf2_cmdlist;
21353 static struct cmd_list_element *show_dwarf2_cmdlist;
21354 
21355 static void
21356 set_dwarf2_cmd (char *args, int from_tty)
21357 {
21358   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
21359 }
21360 
21361 static void
21362 show_dwarf2_cmd (char *args, int from_tty)
21363 {
21364   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
21365 }
21366 
21367 /* Free data associated with OBJFILE, if necessary.  */
21368 
21369 static void
21370 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
21371 {
21372   struct dwarf2_per_objfile *data = d;
21373   int ix;
21374 
21375   /* Make sure we don't accidentally use dwarf2_per_objfile while
21376      cleaning up.  */
21377   dwarf2_per_objfile = NULL;
21378 
21379   for (ix = 0; ix < data->n_comp_units; ++ix)
21380    VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);
21381 
21382   for (ix = 0; ix < data->n_type_units; ++ix)
21383     VEC_free (dwarf2_per_cu_ptr,
21384               data->all_type_units[ix]->per_cu.imported_symtabs);
21385   xfree (data->all_type_units);
21386 
21387   VEC_free (dwarf2_section_info_def, data->types);
21388 
21389   if (data->dwo_files)
21390     free_dwo_files (data->dwo_files, objfile);
21391   if (data->dwp_file)
21392     gdb_bfd_unref (data->dwp_file->dbfd);
21393 
21394   if (data->dwz_file && data->dwz_file->dwz_bfd)
21395     gdb_bfd_unref (data->dwz_file->dwz_bfd);
21396 }
21397 
21398 
21399 /* The "save gdb-index" command.  */
21400 
21401 /* The contents of the hash table we create when building the string
21402    table.  */
21403 struct strtab_entry
21404 {
21405   offset_type offset;
21406   const char *str;
21407 };
21408 
21409 /* Hash function for a strtab_entry.
21410 
21411    Function is used only during write_hash_table so no index format backward
21412    compatibility is needed.  */
21413 
21414 static hashval_t
21415 hash_strtab_entry (const void *e)
21416 {
21417   const struct strtab_entry *entry = e;
21418   return mapped_index_string_hash (INT_MAX, entry->str);
21419 }
21420 
21421 /* Equality function for a strtab_entry.  */
21422 
21423 static int
21424 eq_strtab_entry (const void *a, const void *b)
21425 {
21426   const struct strtab_entry *ea = a;
21427   const struct strtab_entry *eb = b;
21428   return !strcmp (ea->str, eb->str);
21429 }
21430 
21431 /* Create a strtab_entry hash table.  */
21432 
21433 static htab_t
21434 create_strtab (void)
21435 {
21436   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
21437                             xfree, xcalloc, xfree);
21438 }
21439 
21440 /* Add a string to the constant pool.  Return the string's offset in
21441    host order.  */
21442 
21443 static offset_type
21444 add_string (htab_t table, struct obstack *cpool, const char *str)
21445 {
21446   void **slot;
21447   struct strtab_entry entry;
21448   struct strtab_entry *result;
21449 
21450   entry.str = str;
21451   slot = htab_find_slot (table, &entry, INSERT);
21452   if (*slot)
21453     result = *slot;
21454   else
21455     {
21456       result = XNEW (struct strtab_entry);
21457       result->offset = obstack_object_size (cpool);
21458       result->str = str;
21459       obstack_grow_str0 (cpool, str);
21460       *slot = result;
21461     }
21462   return result->offset;
21463 }
21464 
21465 /* An entry in the symbol table.  */
21466 struct symtab_index_entry
21467 {
21468   /* The name of the symbol.  */
21469   const char *name;
21470   /* The offset of the name in the constant pool.  */
21471   offset_type index_offset;
21472   /* A sorted vector of the indices of all the CUs that hold an object
21473      of this name.  */
21474   VEC (offset_type) *cu_indices;
21475 };
21476 
21477 /* The symbol table.  This is a power-of-2-sized hash table.  */
21478 struct mapped_symtab
21479 {
21480   offset_type n_elements;
21481   offset_type size;
21482   struct symtab_index_entry **data;
21483 };
21484 
21485 /* Hash function for a symtab_index_entry.  */
21486 
21487 static hashval_t
21488 hash_symtab_entry (const void *e)
21489 {
21490   const struct symtab_index_entry *entry = e;
21491   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
21492                          sizeof (offset_type) * VEC_length (offset_type,
21493                                                             entry->cu_indices),
21494                          0);
21495 }
21496 
21497 /* Equality function for a symtab_index_entry.  */
21498 
21499 static int
21500 eq_symtab_entry (const void *a, const void *b)
21501 {
21502   const struct symtab_index_entry *ea = a;
21503   const struct symtab_index_entry *eb = b;
21504   int len = VEC_length (offset_type, ea->cu_indices);
21505   if (len != VEC_length (offset_type, eb->cu_indices))
21506     return 0;
21507   return !memcmp (VEC_address (offset_type, ea->cu_indices),
21508                   VEC_address (offset_type, eb->cu_indices),
21509                   sizeof (offset_type) * len);
21510 }
21511 
21512 /* Destroy a symtab_index_entry.  */
21513 
21514 static void
21515 delete_symtab_entry (void *p)
21516 {
21517   struct symtab_index_entry *entry = p;
21518   VEC_free (offset_type, entry->cu_indices);
21519   xfree (entry);
21520 }
21521 
21522 /* Create a hash table holding symtab_index_entry objects.  */
21523 
21524 static htab_t
21525 create_symbol_hash_table (void)
21526 {
21527   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
21528                             delete_symtab_entry, xcalloc, xfree);
21529 }
21530 
21531 /* Create a new mapped symtab object.  */
21532 
21533 static struct mapped_symtab *
21534 create_mapped_symtab (void)
21535 {
21536   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
21537   symtab->n_elements = 0;
21538   symtab->size = 1024;
21539   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
21540   return symtab;
21541 }
21542 
21543 /* Destroy a mapped_symtab.  */
21544 
21545 static void
21546 cleanup_mapped_symtab (void *p)
21547 {
21548   struct mapped_symtab *symtab = p;
21549   /* The contents of the array are freed when the other hash table is
21550      destroyed.  */
21551   xfree (symtab->data);
21552   xfree (symtab);
21553 }
21554 
21555 /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
21556    the slot.
21557    
21558    Function is used only during write_hash_table so no index format backward
21559    compatibility is needed.  */
21560 
21561 static struct symtab_index_entry **
21562 find_slot (struct mapped_symtab *symtab, const char *name)
21563 {
21564   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
21565 
21566   index = hash & (symtab->size - 1);
21567   step = ((hash * 17) & (symtab->size - 1)) | 1;
21568 
21569   for (;;)
21570     {
21571       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
21572         return &symtab->data[index];
21573       index = (index + step) & (symtab->size - 1);
21574     }
21575 }
21576 
21577 /* Expand SYMTAB's hash table.  */
21578 
21579 static void
21580 hash_expand (struct mapped_symtab *symtab)
21581 {
21582   offset_type old_size = symtab->size;
21583   offset_type i;
21584   struct symtab_index_entry **old_entries = symtab->data;
21585 
21586   symtab->size *= 2;
21587   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
21588 
21589   for (i = 0; i < old_size; ++i)
21590     {
21591       if (old_entries[i])
21592         {
21593           struct symtab_index_entry **slot = find_slot (symtab,
21594                                                         old_entries[i]->name);
21595           *slot = old_entries[i];
21596         }
21597     }
21598 
21599   xfree (old_entries);
21600 }
21601 
21602 /* Add an entry to SYMTAB.  NAME is the name of the symbol.
21603    CU_INDEX is the index of the CU in which the symbol appears.
21604    IS_STATIC is one if the symbol is static, otherwise zero (global).  */
21605 
21606 static void
21607 add_index_entry (struct mapped_symtab *symtab, const char *name,
21608                  int is_static, gdb_index_symbol_kind kind,
21609                  offset_type cu_index)
21610 {
21611   struct symtab_index_entry **slot;
21612   offset_type cu_index_and_attrs;
21613 
21614   ++symtab->n_elements;
21615   if (4 * symtab->n_elements / 3 >= symtab->size)
21616     hash_expand (symtab);
21617 
21618   slot = find_slot (symtab, name);
21619   if (!*slot)
21620     {
21621       *slot = XNEW (struct symtab_index_entry);
21622       (*slot)->name = name;
21623       /* index_offset is set later.  */
21624       (*slot)->cu_indices = NULL;
21625     }
21626 
21627   cu_index_and_attrs = 0;
21628   DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
21629   DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
21630   DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);
21631 
21632   /* We don't want to record an index value twice as we want to avoid the
21633      duplication.
21634      We process all global symbols and then all static symbols
21635      (which would allow us to avoid the duplication by only having to check
21636      the last entry pushed), but a symbol could have multiple kinds in one CU.
21637      To keep things simple we don't worry about the duplication here and
21638      sort and uniqufy the list after we've processed all symbols.  */
21639   VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
21640 }
21641 
21642 /* qsort helper routine for uniquify_cu_indices.  */
21643 
21644 static int
21645 offset_type_compare (const void *ap, const void *bp)
21646 {
21647   offset_type a = *(offset_type *) ap;
21648   offset_type b = *(offset_type *) bp;
21649 
21650   return (a > b) - (b > a);
21651 }
21652 
21653 /* Sort and remove duplicates of all symbols' cu_indices lists.  */
21654 
21655 static void
21656 uniquify_cu_indices (struct mapped_symtab *symtab)
21657 {
21658   int i;
21659 
21660   for (i = 0; i < symtab->size; ++i)
21661     {
21662       struct symtab_index_entry *entry = symtab->data[i];
21663 
21664       if (entry
21665           && entry->cu_indices != NULL)
21666         {
21667           unsigned int next_to_insert, next_to_check;
21668           offset_type last_value;
21669 
21670           qsort (VEC_address (offset_type, entry->cu_indices),
21671                  VEC_length (offset_type, entry->cu_indices),
21672                  sizeof (offset_type), offset_type_compare);
21673 
21674           last_value = VEC_index (offset_type, entry->cu_indices, 0);
21675           next_to_insert = 1;
21676           for (next_to_check = 1;
21677                next_to_check < VEC_length (offset_type, entry->cu_indices);
21678                ++next_to_check)
21679             {
21680               if (VEC_index (offset_type, entry->cu_indices, next_to_check)
21681                   != last_value)
21682                 {
21683                   last_value = VEC_index (offset_type, entry->cu_indices,
21684                                           next_to_check);
21685                   VEC_replace (offset_type, entry->cu_indices, next_to_insert,
21686                                last_value);
21687                   ++next_to_insert;
21688                 }
21689             }
21690           VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
21691         }
21692     }
21693 }
21694 
21695 /* Add a vector of indices to the constant pool.  */
21696 
21697 static offset_type
21698 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
21699                       struct symtab_index_entry *entry)
21700 {
21701   void **slot;
21702 
21703   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
21704   if (!*slot)
21705     {
21706       offset_type len = VEC_length (offset_type, entry->cu_indices);
21707       offset_type val = MAYBE_SWAP (len);
21708       offset_type iter;
21709       int i;
21710 
21711       *slot = entry;
21712       entry->index_offset = obstack_object_size (cpool);
21713 
21714       obstack_grow (cpool, &val, sizeof (val));
21715       for (i = 0;
21716            VEC_iterate (offset_type, entry->cu_indices, i, iter);
21717            ++i)
21718         {
21719           val = MAYBE_SWAP (iter);
21720           obstack_grow (cpool, &val, sizeof (val));
21721         }
21722     }
21723   else
21724     {
21725       struct symtab_index_entry *old_entry = *slot;
21726       entry->index_offset = old_entry->index_offset;
21727       entry = old_entry;
21728     }
21729   return entry->index_offset;
21730 }
21731 
21732 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
21733    constant pool entries going into the obstack CPOOL.  */
21734 
21735 static void
21736 write_hash_table (struct mapped_symtab *symtab,
21737                   struct obstack *output, struct obstack *cpool)
21738 {
21739   offset_type i;
21740   htab_t symbol_hash_table;
21741   htab_t str_table;
21742 
21743   symbol_hash_table = create_symbol_hash_table ();
21744   str_table = create_strtab ();
21745 
21746   /* We add all the index vectors to the constant pool first, to
21747      ensure alignment is ok.  */
21748   for (i = 0; i < symtab->size; ++i)
21749     {
21750       if (symtab->data[i])
21751         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
21752     }
21753 
21754   /* Now write out the hash table.  */
21755   for (i = 0; i < symtab->size; ++i)
21756     {
21757       offset_type str_off, vec_off;
21758 
21759       if (symtab->data[i])
21760         {
21761           str_off = add_string (str_table, cpool, symtab->data[i]->name);
21762           vec_off = symtab->data[i]->index_offset;
21763         }
21764       else
21765         {
21766           /* While 0 is a valid constant pool index, it is not valid
21767              to have 0 for both offsets.  */
21768           str_off = 0;
21769           vec_off = 0;
21770         }
21771 
21772       str_off = MAYBE_SWAP (str_off);
21773       vec_off = MAYBE_SWAP (vec_off);
21774 
21775       obstack_grow (output, &str_off, sizeof (str_off));
21776       obstack_grow (output, &vec_off, sizeof (vec_off));
21777     }
21778 
21779   htab_delete (str_table);
21780   htab_delete (symbol_hash_table);
21781 }
21782 
21783 /* Struct to map psymtab to CU index in the index file.  */
21784 struct psymtab_cu_index_map
21785 {
21786   struct partial_symtab *psymtab;
21787   unsigned int cu_index;
21788 };
21789 
21790 static hashval_t
21791 hash_psymtab_cu_index (const void *item)
21792 {
21793   const struct psymtab_cu_index_map *map = item;
21794 
21795   return htab_hash_pointer (map->psymtab);
21796 }
21797 
21798 static int
21799 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
21800 {
21801   const struct psymtab_cu_index_map *lhs = item_lhs;
21802   const struct psymtab_cu_index_map *rhs = item_rhs;
21803 
21804   return lhs->psymtab == rhs->psymtab;
21805 }
21806 
21807 /* Helper struct for building the address table.  */
21808 struct addrmap_index_data
21809 {
21810   struct objfile *objfile;
21811   struct obstack *addr_obstack;
21812   htab_t cu_index_htab;
21813 
21814   /* Non-zero if the previous_* fields are valid.
21815      We can't write an entry until we see the next entry (since it is only then
21816      that we know the end of the entry).  */
21817   int previous_valid;
21818   /* Index of the CU in the table of all CUs in the index file.  */
21819   unsigned int previous_cu_index;
21820   /* Start address of the CU.  */
21821   CORE_ADDR previous_cu_start;
21822 };
21823 
21824 /* Write an address entry to OBSTACK.  */
21825 
21826 static void
21827 add_address_entry (struct objfile *objfile, struct obstack *obstack,
21828                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
21829 {
21830   offset_type cu_index_to_write;
21831   gdb_byte addr[8];
21832   CORE_ADDR baseaddr;
21833 
21834   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
21835 
21836   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
21837   obstack_grow (obstack, addr, 8);
21838   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
21839   obstack_grow (obstack, addr, 8);
21840   cu_index_to_write = MAYBE_SWAP (cu_index);
21841   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
21842 }
21843 
21844 /* Worker function for traversing an addrmap to build the address table.  */
21845 
21846 static int
21847 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
21848 {
21849   struct addrmap_index_data *data = datap;
21850   struct partial_symtab *pst = obj;
21851 
21852   if (data->previous_valid)
21853     add_address_entry (data->objfile, data->addr_obstack,
21854                        data->previous_cu_start, start_addr,
21855                        data->previous_cu_index);
21856 
21857   data->previous_cu_start = start_addr;
21858   if (pst != NULL)
21859     {
21860       struct psymtab_cu_index_map find_map, *map;
21861       find_map.psymtab = pst;
21862       map = htab_find (data->cu_index_htab, &find_map);
21863       gdb_assert (map != NULL);
21864       data->previous_cu_index = map->cu_index;
21865       data->previous_valid = 1;
21866     }
21867   else
21868       data->previous_valid = 0;
21869 
21870   return 0;
21871 }
21872 
21873 /* Write OBJFILE's address map to OBSTACK.
21874    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
21875    in the index file.  */
21876 
21877 static void
21878 write_address_map (struct objfile *objfile, struct obstack *obstack,
21879                    htab_t cu_index_htab)
21880 {
21881   struct addrmap_index_data addrmap_index_data;
21882 
21883   /* When writing the address table, we have to cope with the fact that
21884      the addrmap iterator only provides the start of a region; we have to
21885      wait until the next invocation to get the start of the next region.  */
21886 
21887   addrmap_index_data.objfile = objfile;
21888   addrmap_index_data.addr_obstack = obstack;
21889   addrmap_index_data.cu_index_htab = cu_index_htab;
21890   addrmap_index_data.previous_valid = 0;
21891 
21892   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
21893                    &addrmap_index_data);
21894 
21895   /* It's highly unlikely the last entry (end address = 0xff...ff)
21896      is valid, but we should still handle it.
21897      The end address is recorded as the start of the next region, but that
21898      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
21899      anyway.  */
21900   if (addrmap_index_data.previous_valid)
21901     add_address_entry (objfile, obstack,
21902                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
21903                        addrmap_index_data.previous_cu_index);
21904 }
21905 
21906 /* Return the symbol kind of PSYM.  */
21907 
21908 static gdb_index_symbol_kind
21909 symbol_kind (struct partial_symbol *psym)
21910 {
21911   domain_enum domain = PSYMBOL_DOMAIN (psym);
21912   enum address_class aclass = PSYMBOL_CLASS (psym);
21913 
21914   switch (domain)
21915     {
21916     case VAR_DOMAIN:
21917       switch (aclass)
21918         {
21919         case LOC_BLOCK:
21920           return GDB_INDEX_SYMBOL_KIND_FUNCTION;
21921         case LOC_TYPEDEF:
21922           return GDB_INDEX_SYMBOL_KIND_TYPE;
21923         case LOC_COMPUTED:
21924         case LOC_CONST_BYTES:
21925         case LOC_OPTIMIZED_OUT:
21926         case LOC_STATIC:
21927           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21928         case LOC_CONST:
21929           /* Note: It's currently impossible to recognize psyms as enum values
21930              short of reading the type info.  For now punt.  */
21931           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
21932         default:
21933           /* There are other LOC_FOO values that one might want to classify
21934              as variables, but dwarf2read.c doesn't currently use them.  */
21935           return GDB_INDEX_SYMBOL_KIND_OTHER;
21936         }
21937     case STRUCT_DOMAIN:
21938       return GDB_INDEX_SYMBOL_KIND_TYPE;
21939     default:
21940       return GDB_INDEX_SYMBOL_KIND_OTHER;
21941     }
21942 }
21943 
21944 /* Add a list of partial symbols to SYMTAB.  */
21945 
21946 static void
21947 write_psymbols (struct mapped_symtab *symtab,
21948                 htab_t psyms_seen,
21949                 struct partial_symbol **psymp,
21950                 int count,
21951                 offset_type cu_index,
21952                 int is_static)
21953 {
21954   for (; count-- > 0; ++psymp)
21955     {
21956       struct partial_symbol *psym = *psymp;
21957       void **slot;
21958 
21959       if (SYMBOL_LANGUAGE (psym) == language_ada)
21960         error (_("Ada is not currently supported by the index"));
21961 
21962       /* Only add a given psymbol once.  */
21963       slot = htab_find_slot (psyms_seen, psym, INSERT);
21964       if (!*slot)
21965         {
21966           gdb_index_symbol_kind kind = symbol_kind (psym);
21967 
21968           *slot = psym;
21969           add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
21970                            is_static, kind, cu_index);
21971         }
21972     }
21973 }
21974 
21975 /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
21976    exception if there is an error.  */
21977 
21978 static void
21979 write_obstack (FILE *file, struct obstack *obstack)
21980 {
21981   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
21982               file)
21983       != obstack_object_size (obstack))
21984     error (_("couldn't data write to file"));
21985 }
21986 
21987 /* Unlink a file if the argument is not NULL.  */
21988 
21989 static void
21990 unlink_if_set (void *p)
21991 {
21992   char **filename = p;
21993   if (*filename)
21994     unlink (*filename);
21995 }
21996 
21997 /* A helper struct used when iterating over debug_types.  */
21998 struct signatured_type_index_data
21999 {
22000   struct objfile *objfile;
22001   struct mapped_symtab *symtab;
22002   struct obstack *types_list;
22003   htab_t psyms_seen;
22004   int cu_index;
22005 };
22006 
22007 /* A helper function that writes a single signatured_type to an
22008    obstack.  */
22009 
22010 static int
22011 write_one_signatured_type (void **slot, void *d)
22012 {
22013   struct signatured_type_index_data *info = d;
22014   struct signatured_type *entry = (struct signatured_type *) *slot;
22015   struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
22016   gdb_byte val[8];
22017 
22018   write_psymbols (info->symtab,
22019                   info->psyms_seen,
22020                   info->objfile->global_psymbols.list
22021                   + psymtab->globals_offset,
22022                   psymtab->n_global_syms, info->cu_index,
22023                   0);
22024   write_psymbols (info->symtab,
22025                   info->psyms_seen,
22026                   info->objfile->static_psymbols.list
22027                   + psymtab->statics_offset,
22028                   psymtab->n_static_syms, info->cu_index,
22029                   1);
22030 
22031   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22032                           entry->per_cu.offset.sect_off);
22033   obstack_grow (info->types_list, val, 8);
22034   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22035                           entry->type_offset_in_tu.cu_off);
22036   obstack_grow (info->types_list, val, 8);
22037   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
22038   obstack_grow (info->types_list, val, 8);
22039 
22040   ++info->cu_index;
22041 
22042   return 1;
22043 }
22044 
22045 /* Recurse into all "included" dependencies and write their symbols as
22046    if they appeared in this psymtab.  */
22047 
22048 static void
22049 recursively_write_psymbols (struct objfile *objfile,
22050                             struct partial_symtab *psymtab,
22051                             struct mapped_symtab *symtab,
22052                             htab_t psyms_seen,
22053                             offset_type cu_index)
22054 {
22055   int i;
22056 
22057   for (i = 0; i < psymtab->number_of_dependencies; ++i)
22058     if (psymtab->dependencies[i]->user != NULL)
22059       recursively_write_psymbols (objfile, psymtab->dependencies[i],
22060                                   symtab, psyms_seen, cu_index);
22061 
22062   write_psymbols (symtab,
22063                   psyms_seen,
22064                   objfile->global_psymbols.list + psymtab->globals_offset,
22065                   psymtab->n_global_syms, cu_index,
22066                   0);
22067   write_psymbols (symtab,
22068                   psyms_seen,
22069                   objfile->static_psymbols.list + psymtab->statics_offset,
22070                   psymtab->n_static_syms, cu_index,
22071                   1);
22072 }
22073 
22074 /* Create an index file for OBJFILE in the directory DIR.  */
22075 
22076 static void
22077 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
22078 {
22079   struct cleanup *cleanup;
22080   char *filename, *cleanup_filename;
22081   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
22082   struct obstack cu_list, types_cu_list;
22083   int i;
22084   FILE *out_file;
22085   struct mapped_symtab *symtab;
22086   offset_type val, size_of_contents, total_len;
22087   struct stat st;
22088   htab_t psyms_seen;
22089   htab_t cu_index_htab;
22090   struct psymtab_cu_index_map *psymtab_cu_index_map;
22091 
22092   if (dwarf2_per_objfile->using_index)
22093     error (_("Cannot use an index to create the index"));
22094 
22095   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
22096     error (_("Cannot make an index when the file has multiple .debug_types sections"));
22097 
22098   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
22099     return;
22100 
22101   if (stat (objfile_name (objfile), &st) < 0)
22102     perror_with_name (objfile_name (objfile));
22103 
22104   filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
22105                      INDEX_SUFFIX, (char *) NULL);
22106   cleanup = make_cleanup (xfree, filename);
22107 
22108   out_file = gdb_fopen_cloexec (filename, "wb");
22109   if (!out_file)
22110     error (_("Can't open `%s' for writing"), filename);
22111 
22112   cleanup_filename = filename;
22113   make_cleanup (unlink_if_set, &cleanup_filename);
22114 
22115   symtab = create_mapped_symtab ();
22116   make_cleanup (cleanup_mapped_symtab, symtab);
22117 
22118   obstack_init (&addr_obstack);
22119   make_cleanup_obstack_free (&addr_obstack);
22120 
22121   obstack_init (&cu_list);
22122   make_cleanup_obstack_free (&cu_list);
22123 
22124   obstack_init (&types_cu_list);
22125   make_cleanup_obstack_free (&types_cu_list);
22126 
22127   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
22128                                   NULL, xcalloc, xfree);
22129   make_cleanup_htab_delete (psyms_seen);
22130 
22131   /* While we're scanning CU's create a table that maps a psymtab pointer
22132      (which is what addrmap records) to its index (which is what is recorded
22133      in the index file).  This will later be needed to write the address
22134      table.  */
22135   cu_index_htab = htab_create_alloc (100,
22136                                      hash_psymtab_cu_index,
22137                                      eq_psymtab_cu_index,
22138                                      NULL, xcalloc, xfree);
22139   make_cleanup_htab_delete (cu_index_htab);
22140   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
22141     xmalloc (sizeof (struct psymtab_cu_index_map)
22142              * dwarf2_per_objfile->n_comp_units);
22143   make_cleanup (xfree, psymtab_cu_index_map);
22144 
22145   /* The CU list is already sorted, so we don't need to do additional
22146      work here.  Also, the debug_types entries do not appear in
22147      all_comp_units, but only in their own hash table.  */
22148   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
22149     {
22150       struct dwarf2_per_cu_data *per_cu
22151         = dwarf2_per_objfile->all_comp_units[i];
22152       struct partial_symtab *psymtab = per_cu->v.psymtab;
22153       gdb_byte val[8];
22154       struct psymtab_cu_index_map *map;
22155       void **slot;
22156 
22157       /* CU of a shared file from 'dwz -m' may be unused by this main file.
22158          It may be referenced from a local scope but in such case it does not
22159          need to be present in .gdb_index.  */
22160       if (psymtab == NULL)
22161         continue;
22162 
22163       if (psymtab->user == NULL)
22164         recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);
22165 
22166       map = &psymtab_cu_index_map[i];
22167       map->psymtab = psymtab;
22168       map->cu_index = i;
22169       slot = htab_find_slot (cu_index_htab, map, INSERT);
22170       gdb_assert (slot != NULL);
22171       gdb_assert (*slot == NULL);
22172       *slot = map;
22173 
22174       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
22175                               per_cu->offset.sect_off);
22176       obstack_grow (&cu_list, val, 8);
22177       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
22178       obstack_grow (&cu_list, val, 8);
22179     }
22180 
22181   /* Dump the address map.  */
22182   write_address_map (objfile, &addr_obstack, cu_index_htab);
22183 
22184   /* Write out the .debug_type entries, if any.  */
22185   if (dwarf2_per_objfile->signatured_types)
22186     {
22187       struct signatured_type_index_data sig_data;
22188 
22189       sig_data.objfile = objfile;
22190       sig_data.symtab = symtab;
22191       sig_data.types_list = &types_cu_list;
22192       sig_data.psyms_seen = psyms_seen;
22193       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
22194       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
22195                               write_one_signatured_type, &sig_data);
22196     }
22197 
22198   /* Now that we've processed all symbols we can shrink their cu_indices
22199      lists.  */
22200   uniquify_cu_indices (symtab);
22201 
22202   obstack_init (&constant_pool);
22203   make_cleanup_obstack_free (&constant_pool);
22204   obstack_init (&symtab_obstack);
22205   make_cleanup_obstack_free (&symtab_obstack);
22206   write_hash_table (symtab, &symtab_obstack, &constant_pool);
22207 
22208   obstack_init (&contents);
22209   make_cleanup_obstack_free (&contents);
22210   size_of_contents = 6 * sizeof (offset_type);
22211   total_len = size_of_contents;
22212 
22213   /* The version number.  */
22214   val = MAYBE_SWAP (8);
22215   obstack_grow (&contents, &val, sizeof (val));
22216 
22217   /* The offset of the CU list from the start of the file.  */
22218   val = MAYBE_SWAP (total_len);
22219   obstack_grow (&contents, &val, sizeof (val));
22220   total_len += obstack_object_size (&cu_list);
22221 
22222   /* The offset of the types CU list from the start of the file.  */
22223   val = MAYBE_SWAP (total_len);
22224   obstack_grow (&contents, &val, sizeof (val));
22225   total_len += obstack_object_size (&types_cu_list);
22226 
22227   /* The offset of the address table from the start of the file.  */
22228   val = MAYBE_SWAP (total_len);
22229   obstack_grow (&contents, &val, sizeof (val));
22230   total_len += obstack_object_size (&addr_obstack);
22231 
22232   /* The offset of the symbol table from the start of the file.  */
22233   val = MAYBE_SWAP (total_len);
22234   obstack_grow (&contents, &val, sizeof (val));
22235   total_len += obstack_object_size (&symtab_obstack);
22236 
22237   /* The offset of the constant pool from the start of the file.  */
22238   val = MAYBE_SWAP (total_len);
22239   obstack_grow (&contents, &val, sizeof (val));
22240   total_len += obstack_object_size (&constant_pool);
22241 
22242   gdb_assert (obstack_object_size (&contents) == size_of_contents);
22243 
22244   write_obstack (out_file, &contents);
22245   write_obstack (out_file, &cu_list);
22246   write_obstack (out_file, &types_cu_list);
22247   write_obstack (out_file, &addr_obstack);
22248   write_obstack (out_file, &symtab_obstack);
22249   write_obstack (out_file, &constant_pool);
22250 
22251   fclose (out_file);
22252 
22253   /* We want to keep the file, so we set cleanup_filename to NULL
22254      here.  See unlink_if_set.  */
22255   cleanup_filename = NULL;
22256 
22257   do_cleanups (cleanup);
22258 }
22259 
22260 /* Implementation of the `save gdb-index' command.
22261    
22262    Note that the file format used by this command is documented in the
22263    GDB manual.  Any changes here must be documented there.  */
22264 
22265 static void
22266 save_gdb_index_command (char *arg, int from_tty)
22267 {
22268   struct objfile *objfile;
22269 
22270   if (!arg || !*arg)
22271     error (_("usage: save gdb-index DIRECTORY"));
22272 
22273   ALL_OBJFILES (objfile)
22274   {
22275     struct stat st;
22276 
22277     /* If the objfile does not correspond to an actual file, skip it.  */
22278     if (stat (objfile_name (objfile), &st) < 0)
22279       continue;
22280 
22281     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
22282     if (dwarf2_per_objfile)
22283       {
22284         volatile struct gdb_exception except;
22285 
22286         TRY_CATCH (except, RETURN_MASK_ERROR)
22287           {
22288             write_psymtabs_to_index (objfile, arg);
22289           }
22290         if (except.reason < 0)
22291           exception_fprintf (gdb_stderr, except,
22292                              _("Error while writing index for `%s': "),
22293                              objfile_name (objfile));
22294       }
22295   }
22296 }
22297 
22298 
22299 
22300 int dwarf2_always_disassemble;
22301 
22302 static void
22303 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
22304                                 struct cmd_list_element *c, const char *value)
22305 {
22306   fprintf_filtered (file,
22307                     _("Whether to always disassemble "
22308                       "DWARF expressions is %s.\n"),
22309                     value);
22310 }
22311 
22312 static void
22313 show_check_physname (struct ui_file *file, int from_tty,
22314                      struct cmd_list_element *c, const char *value)
22315 {
22316   fprintf_filtered (file,
22317                     _("Whether to check \"physname\" is %s.\n"),
22318                     value);
22319 }
22320 
22321 void _initialize_dwarf2_read (void);
22322 
22323 void
22324 _initialize_dwarf2_read (void)
22325 {
22326   struct cmd_list_element *c;
22327 
22328   dwarf2_objfile_data_key
22329     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
22330 
22331   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
22332 Set DWARF 2 specific variables.\n\
22333 Configure DWARF 2 variables such as the cache size"),
22334                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
22335                   0/*allow-unknown*/, &maintenance_set_cmdlist);
22336 
22337   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
22338 Show DWARF 2 specific variables\n\
22339 Show DWARF 2 variables such as the cache size"),
22340                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
22341                   0/*allow-unknown*/, &maintenance_show_cmdlist);
22342 
22343   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
22344                             &dwarf2_max_cache_age, _("\
22345 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
22346 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
22347 A higher limit means that cached compilation units will be stored\n\
22348 in memory longer, and more total memory will be used.  Zero disables\n\
22349 caching, which can slow down startup."),
22350                             NULL,
22351                             show_dwarf2_max_cache_age,
22352                             &set_dwarf2_cmdlist,
22353                             &show_dwarf2_cmdlist);
22354 
22355   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
22356                            &dwarf2_always_disassemble, _("\
22357 Set whether `info address' always disassembles DWARF expressions."), _("\
22358 Show whether `info address' always disassembles DWARF expressions."), _("\
22359 When enabled, DWARF expressions are always printed in an assembly-like\n\
22360 syntax.  When disabled, expressions will be printed in a more\n\
22361 conversational style, when possible."),
22362                            NULL,
22363                            show_dwarf2_always_disassemble,
22364                            &set_dwarf2_cmdlist,
22365                            &show_dwarf2_cmdlist);
22366 
22367   add_setshow_boolean_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
22368 Set debugging of the dwarf2 reader."), _("\
22369 Show debugging of the dwarf2 reader."), _("\
22370 When enabled, debugging messages are printed during dwarf2 reading\n\
22371 and symtab expansion."),
22372                             NULL,
22373                             NULL,
22374                             &setdebuglist, &showdebuglist);
22375 
22376   add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
22377 Set debugging of the dwarf2 DIE reader."), _("\
22378 Show debugging of the dwarf2 DIE reader."), _("\
22379 When enabled (non-zero), DIEs are dumped after they are read in.\n\
22380 The value is the maximum depth to print."),
22381                              NULL,
22382                              NULL,
22383                              &setdebuglist, &showdebuglist);
22384 
22385   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
22386 Set cross-checking of \"physname\" code against demangler."), _("\
22387 Show cross-checking of \"physname\" code against demangler."), _("\
22388 When enabled, GDB's internal \"physname\" code is checked against\n\
22389 the demangler."),
22390                            NULL, show_check_physname,
22391                            &setdebuglist, &showdebuglist);
22392 
22393   add_setshow_boolean_cmd ("use-deprecated-index-sections",
22394                            no_class, &use_deprecated_index_sections, _("\
22395 Set whether to use deprecated gdb_index sections."), _("\
22396 Show whether to use deprecated gdb_index sections."), _("\
22397 When enabled, deprecated .gdb_index sections are used anyway.\n\
22398 Normally they are ignored either because of a missing feature or\n\
22399 performance issue.\n\
22400 Warning: This option must be enabled before gdb reads the file."),
22401                            NULL,
22402                            NULL,
22403                            &setlist, &showlist);
22404 
22405   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
22406                _("\
22407 Save a gdb-index file.\n\
22408 Usage: save gdb-index DIRECTORY"),
22409                &save_cmdlist);
22410   set_cmd_completer (c, filename_completer);
22411 
22412   dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
22413                                                         &dwarf2_locexpr_funcs);
22414   dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
22415                                                         &dwarf2_loclist_funcs);
22416 
22417   dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
22418                                         &dwarf2_block_frame_base_locexpr_funcs);
22419   dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
22420                                         &dwarf2_block_frame_base_loclist_funcs);
22421 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Defines