gdb/dwarf2read.c - gdb

Global variables defined

Data types defined

Functions defined

Macros defined

Source code

  1. /* DWARF 2 debugging format support for GDB.

  2.    Copyright (C) 1994-2015 Free Software Foundation, Inc.

  3.    Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
  4.    Inc.  with support from Florida State University (under contract
  5.    with the Ada Joint Program Office), and Silicon Graphics, Inc.
  6.    Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
  7.    based on Fred Fish's (Cygnus Support) implementation of DWARF 1
  8.    support.

  9.    This file is part of GDB.

  10.    This program is free software; you can redistribute it and/or modify
  11.    it under the terms of the GNU General Public License as published by
  12.    the Free Software Foundation; either version 3 of the License, or
  13.    (at your option) any later version.

  14.    This program is distributed in the hope that it will be useful,
  15.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.    GNU General Public License for more details.

  18.    You should have received a copy of the GNU General Public License
  19.    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

  20. /* FIXME: Various die-reading functions need to be more careful with
  21.    reading off the end of the section.
  22.    E.g., load_partial_dies, read_partial_die.  */

  23. #include "defs.h"
  24. #include "bfd.h"
  25. #include "elf-bfd.h"
  26. #include "symtab.h"
  27. #include "gdbtypes.h"
  28. #include "objfiles.h"
  29. #include "dwarf2.h"
  30. #include "buildsym.h"
  31. #include "demangle.h"
  32. #include "gdb-demangle.h"
  33. #include "expression.h"
  34. #include "filenames.h"        /* for DOSish file names */
  35. #include "macrotab.h"
  36. #include "language.h"
  37. #include "complaints.h"
  38. #include "bcache.h"
  39. #include "dwarf2expr.h"
  40. #include "dwarf2loc.h"
  41. #include "cp-support.h"
  42. #include "hashtab.h"
  43. #include "command.h"
  44. #include "gdbcmd.h"
  45. #include "block.h"
  46. #include "addrmap.h"
  47. #include "typeprint.h"
  48. #include "jv-lang.h"
  49. #include "psympriv.h"
  50. #include <sys/stat.h>
  51. #include "completer.h"
  52. #include "vec.h"
  53. #include "c-lang.h"
  54. #include "go-lang.h"
  55. #include "valprint.h"
  56. #include "gdbcore.h" /* for gnutarget */
  57. #include "gdb/gdb-index.h"
  58. #include <ctype.h>
  59. #include "gdb_bfd.h"
  60. #include "f-lang.h"
  61. #include "source.h"
  62. #include "filestuff.h"
  63. #include "build-id.h"

  64. #include <fcntl.h>
  65. #include <sys/types.h>

  66. typedef struct symbol *symbolp;
  67. DEF_VEC_P (symbolp);

  68. /* When == 1, print basic high level tracing messages.
  69.    When > 1, be more verbose.
  70.    This is in contrast to the low level DIE reading of dwarf2_die_debug.  */
  71. static unsigned int dwarf2_read_debug = 0;

  72. /* When non-zero, dump DIEs after they are read in.  */
  73. static unsigned int dwarf2_die_debug = 0;

  74. /* When non-zero, cross-check physname against demangler.  */
  75. static int check_physname = 0;

  76. /* When non-zero, do not reject deprecated .gdb_index sections.  */
  77. static int use_deprecated_index_sections = 0;

  78. static const struct objfile_data *dwarf2_objfile_data_key;

  79. /* The "aclass" indices for various kinds of computed DWARF symbols.  */

  80. static int dwarf2_locexpr_index;
  81. static int dwarf2_loclist_index;
  82. static int dwarf2_locexpr_block_index;
  83. static int dwarf2_loclist_block_index;

  84. /* A descriptor for dwarf sections.

  85.    S.ASECTION, SIZE are typically initialized when the objfile is first
  86.    scanned.  BUFFER, READIN are filled in later when the section is read.
  87.    If the section contained compressed data then SIZE is updated to record
  88.    the uncompressed size of the section.

  89.    DWP file format V2 introduces a wrinkle that is easiest to handle by
  90.    creating the concept of virtual sections contained within a real section.
  91.    In DWP V2 the sections of the input DWO files are concatenated together
  92.    into one section, but section offsets are kept relative to the original
  93.    input section.
  94.    If this is a virtual dwp-v2 section, S.CONTAINING_SECTION is a backlink to
  95.    the real section this "virtual" section is contained in, and BUFFER,SIZE
  96.    describe the virtual section.  */

  97. struct dwarf2_section_info
  98. {
  99.   union
  100.   {
  101.     /* If this is a real section, the bfd section.  */
  102.     asection *asection;
  103.     /* If this is a virtual section, pointer to the containing ("real")
  104.        section.  */
  105.     struct dwarf2_section_info *containing_section;
  106.   } s;
  107.   /* Pointer to section data, only valid if readin.  */
  108.   const gdb_byte *buffer;
  109.   /* The size of the section, real or virtual.  */
  110.   bfd_size_type size;
  111.   /* If this is a virtual section, the offset in the real section.
  112.      Only valid if is_virtual.  */
  113.   bfd_size_type virtual_offset;
  114.   /* True if we have tried to read this section.  */
  115.   char readin;
  116.   /* True if this is a virtual section, False otherwise.
  117.      This specifies which of s.asection and s.containing_section to use.  */
  118.   char is_virtual;
  119. };

  120. typedef struct dwarf2_section_info dwarf2_section_info_def;
  121. DEF_VEC_O (dwarf2_section_info_def);

  122. /* All offsets in the index are of this type.  It must be
  123.    architecture-independent.  */
  124. typedef uint32_t offset_type;

  125. DEF_VEC_I (offset_type);

  126. /* Ensure only legit values are used.  */
  127. #define DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE(cu_index, value) \
  128.   do { \
  129.     gdb_assert ((unsigned int) (value) <= 1); \
  130.     GDB_INDEX_SYMBOL_STATIC_SET_VALUE((cu_index), (value)); \
  131.   } while (0)

  132. /* Ensure only legit values are used.  */
  133. #define DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE(cu_index, value) \
  134.   do { \
  135.     gdb_assert ((value) >= GDB_INDEX_SYMBOL_KIND_TYPE \
  136.                 && (value) <= GDB_INDEX_SYMBOL_KIND_OTHER); \
  137.     GDB_INDEX_SYMBOL_KIND_SET_VALUE((cu_index), (value)); \
  138.   } while (0)

  139. /* Ensure we don't use more than the alloted nuber of bits for the CU.  */
  140. #define DW2_GDB_INDEX_CU_SET_VALUE(cu_index, value) \
  141.   do { \
  142.     gdb_assert (((value) & ~GDB_INDEX_CU_MASK) == 0); \
  143.     GDB_INDEX_CU_SET_VALUE((cu_index), (value)); \
  144.   } while (0)

  145. /* A description of the mapped index.  The file format is described in
  146.    a comment by the code that writes the index.  */
  147. struct mapped_index
  148. {
  149.   /* Index data format version.  */
  150.   int version;

  151.   /* The total length of the buffer.  */
  152.   off_t total_size;

  153.   /* A pointer to the address table data.  */
  154.   const gdb_byte *address_table;

  155.   /* Size of the address table data in bytes.  */
  156.   offset_type address_table_size;

  157.   /* The symbol table, implemented as a hash table.  */
  158.   const offset_type *symbol_table;

  159.   /* Size in slots, each slot is 2 offset_types.  */
  160.   offset_type symbol_table_slots;

  161.   /* A pointer to the constant pool.  */
  162.   const char *constant_pool;
  163. };

  164. typedef struct dwarf2_per_cu_data *dwarf2_per_cu_ptr;
  165. DEF_VEC_P (dwarf2_per_cu_ptr);

  166. /* Collection of data recorded per objfile.
  167.    This hangs off of dwarf2_objfile_data_key.  */

  168. struct dwarf2_per_objfile
  169. {
  170.   struct dwarf2_section_info info;
  171.   struct dwarf2_section_info abbrev;
  172.   struct dwarf2_section_info line;
  173.   struct dwarf2_section_info loc;
  174.   struct dwarf2_section_info macinfo;
  175.   struct dwarf2_section_info macro;
  176.   struct dwarf2_section_info str;
  177.   struct dwarf2_section_info ranges;
  178.   struct dwarf2_section_info addr;
  179.   struct dwarf2_section_info frame;
  180.   struct dwarf2_section_info eh_frame;
  181.   struct dwarf2_section_info gdb_index;

  182.   VEC (dwarf2_section_info_def) *types;

  183.   /* Back link.  */
  184.   struct objfile *objfile;

  185.   /* Table of all the compilation units.  This is used to locate
  186.      the target compilation unit of a particular reference.  */
  187.   struct dwarf2_per_cu_data **all_comp_units;

  188.   /* The number of compilation units in ALL_COMP_UNITS.  */
  189.   int n_comp_units;

  190.   /* The number of .debug_types-related CUs.  */
  191.   int n_type_units;

  192.   /* The number of elements allocated in all_type_units.
  193.      If there are skeleton-less TUs, we add them to all_type_units lazily.  */
  194.   int n_allocated_type_units;

  195.   /* The .debug_types-related CUs (TUs).
  196.      This is stored in malloc space because we may realloc it.  */
  197.   struct signatured_type **all_type_units;

  198.   /* Table of struct type_unit_group objects.
  199.      The hash key is the DW_AT_stmt_list value.  */
  200.   htab_t type_unit_groups;

  201.   /* A table mapping .debug_types signatures to its signatured_type entry.
  202.      This is NULL if the .debug_types section hasn't been read in yet.  */
  203.   htab_t signatured_types;

  204.   /* Type unit statistics, to see how well the scaling improvements
  205.      are doing.  */
  206.   struct tu_stats
  207.   {
  208.     int nr_uniq_abbrev_tables;
  209.     int nr_symtabs;
  210.     int nr_symtab_sharers;
  211.     int nr_stmt_less_type_units;
  212.     int nr_all_type_units_reallocs;
  213.   } tu_stats;

  214.   /* A chain of compilation units that are currently read in, so that
  215.      they can be freed later.  */
  216.   struct dwarf2_per_cu_data *read_in_chain;

  217.   /* A table mapping DW_AT_dwo_name values to struct dwo_file objects.
  218.      This is NULL if the table hasn't been allocated yet.  */
  219.   htab_t dwo_files;

  220.   /* Non-zero if we've check for whether there is a DWP file.  */
  221.   int dwp_checked;

  222.   /* The DWP file if there is one, or NULL.  */
  223.   struct dwp_file *dwp_file;

  224.   /* The shared '.dwz' file, if one exists.  This is used when the
  225.      original data was compressed using 'dwz -m'.  */
  226.   struct dwz_file *dwz_file;

  227.   /* A flag indicating wether this objfile has a section loaded at a
  228.      VMA of 0.  */
  229.   int has_section_at_zero;

  230.   /* True if we are using the mapped index,
  231.      or we are faking it for OBJF_READNOW's sake.  */
  232.   unsigned char using_index;

  233.   /* The mapped index, or NULL if .gdb_index is missing or not being used.  */
  234.   struct mapped_index *index_table;

  235.   /* When using index_table, this keeps track of all quick_file_names entries.
  236.      TUs typically share line table entries with a CU, so we maintain a
  237.      separate table of all line table entries to support the sharing.
  238.      Note that while there can be way more TUs than CUs, we've already
  239.      sorted all the TUs into "type unit groups", grouped by their
  240.      DW_AT_stmt_list value.  Therefore the only sharing done here is with a
  241.      CU and its associated TU group if there is one.  */
  242.   htab_t quick_file_names_table;

  243.   /* Set during partial symbol reading, to prevent queueing of full
  244.      symbols.  */
  245.   int reading_partial_symbols;

  246.   /* Table mapping type DIEs to their struct type *.
  247.      This is NULL if not allocated yet.
  248.      The mapping is done via (CU/TU + DIE offset) -> type.  */
  249.   htab_t die_type_hash;

  250.   /* The CUs we recently read.  */
  251.   VEC (dwarf2_per_cu_ptr) *just_read_cus;
  252. };

  253. static struct dwarf2_per_objfile *dwarf2_per_objfile;

  254. /* Default names of the debugging sections.  */

  255. /* Note that if the debugging section has been compressed, it might
  256.    have a name like .zdebug_info.  */

  257. static const struct dwarf2_debug_sections dwarf2_elf_names =
  258. {
  259.   { ".debug_info", ".zdebug_info" },
  260.   { ".debug_abbrev", ".zdebug_abbrev" },
  261.   { ".debug_line", ".zdebug_line" },
  262.   { ".debug_loc", ".zdebug_loc" },
  263.   { ".debug_macinfo", ".zdebug_macinfo" },
  264.   { ".debug_macro", ".zdebug_macro" },
  265.   { ".debug_str", ".zdebug_str" },
  266.   { ".debug_ranges", ".zdebug_ranges" },
  267.   { ".debug_types", ".zdebug_types" },
  268.   { ".debug_addr", ".zdebug_addr" },
  269.   { ".debug_frame", ".zdebug_frame" },
  270.   { ".eh_frame", NULL },
  271.   { ".gdb_index", ".zgdb_index" },
  272.   23
  273. };

  274. /* List of DWO/DWP sections.  */

  275. static const struct dwop_section_names
  276. {
  277.   struct dwarf2_section_names abbrev_dwo;
  278.   struct dwarf2_section_names info_dwo;
  279.   struct dwarf2_section_names line_dwo;
  280.   struct dwarf2_section_names loc_dwo;
  281.   struct dwarf2_section_names macinfo_dwo;
  282.   struct dwarf2_section_names macro_dwo;
  283.   struct dwarf2_section_names str_dwo;
  284.   struct dwarf2_section_names str_offsets_dwo;
  285.   struct dwarf2_section_names types_dwo;
  286.   struct dwarf2_section_names cu_index;
  287.   struct dwarf2_section_names tu_index;
  288. }
  289. dwop_section_names =
  290. {
  291.   { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
  292.   { ".debug_info.dwo", ".zdebug_info.dwo" },
  293.   { ".debug_line.dwo", ".zdebug_line.dwo" },
  294.   { ".debug_loc.dwo", ".zdebug_loc.dwo" },
  295.   { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
  296.   { ".debug_macro.dwo", ".zdebug_macro.dwo" },
  297.   { ".debug_str.dwo", ".zdebug_str.dwo" },
  298.   { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
  299.   { ".debug_types.dwo", ".zdebug_types.dwo" },
  300.   { ".debug_cu_index", ".zdebug_cu_index" },
  301.   { ".debug_tu_index", ".zdebug_tu_index" },
  302. };

  303. /* local data types */

  304. /* The data in a compilation unit header, after target2host
  305.    translation, looks like this.  */
  306. struct comp_unit_head
  307. {
  308.   unsigned int length;
  309.   short version;
  310.   unsigned char addr_size;
  311.   unsigned char signed_addr_p;
  312.   sect_offset abbrev_offset;

  313.   /* Size of file offsets; either 4 or 8.  */
  314.   unsigned int offset_size;

  315.   /* Size of the length field; either 4 or 12.  */
  316.   unsigned int initial_length_size;

  317.   /* Offset to the first byte of this compilation unit header in the
  318.      .debug_info section, for resolving relative reference dies.  */
  319.   sect_offset offset;

  320.   /* Offset to first die in this cu from the start of the cu.
  321.      This will be the first byte following the compilation unit header.  */
  322.   cu_offset first_die_offset;
  323. };

  324. /* Type used for delaying computation of method physnames.
  325.    See comments for compute_delayed_physnames.  */
  326. struct delayed_method_info
  327. {
  328.   /* The type to which the method is attached, i.e., its parent class.  */
  329.   struct type *type;

  330.   /* The index of the method in the type's function fieldlists.  */
  331.   int fnfield_index;

  332.   /* The index of the method in the fieldlist.  */
  333.   int index;

  334.   /* The name of the DIE.  */
  335.   const char *name;

  336.   /*  The DIE associated with this method.  */
  337.   struct die_info *die;
  338. };

  339. typedef struct delayed_method_info delayed_method_info;
  340. DEF_VEC_O (delayed_method_info);

  341. /* Internal state when decoding a particular compilation unit.  */
  342. struct dwarf2_cu
  343. {
  344.   /* The objfile containing this compilation unit.  */
  345.   struct objfile *objfile;

  346.   /* The header of the compilation unit.  */
  347.   struct comp_unit_head header;

  348.   /* Base address of this compilation unit.  */
  349.   CORE_ADDR base_address;

  350.   /* Non-zero if base_address has been set.  */
  351.   int base_known;

  352.   /* The language we are debugging.  */
  353.   enum language language;
  354.   const struct language_defn *language_defn;

  355.   const char *producer;

  356.   /* The generic symbol table building routines have separate lists for
  357.      file scope symbols and all all other scopes (local scopes).  So
  358.      we need to select the right one to pass to add_symbol_to_list().
  359.      We do it by keeping a pointer to the correct list in list_in_scope.

  360.      FIXME: The original dwarf code just treated the file scope as the
  361.      first local scope, and all other local scopes as nested local
  362.      scopes, and worked fine.  Check to see if we really need to
  363.      distinguish these in buildsym.c.  */
  364.   struct pending **list_in_scope;

  365.   /* The abbrev table for this CU.
  366.      Normally this points to the abbrev table in the objfile.
  367.      But if DWO_UNIT is non-NULL this is the abbrev table in the DWO file.  */
  368.   struct abbrev_table *abbrev_table;

  369.   /* Hash table holding all the loaded partial DIEs
  370.      with partial_die->offset.SECT_OFF as hash.  */
  371.   htab_t partial_dies;

  372.   /* Storage for things with the same lifetime as this read-in compilation
  373.      unit, including partial DIEs.  */
  374.   struct obstack comp_unit_obstack;

  375.   /* When multiple dwarf2_cu structures are living in memory, this field
  376.      chains them all together, so that they can be released efficiently.
  377.      We will probably also want a generation counter so that most-recently-used
  378.      compilation units are cached...  */
  379.   struct dwarf2_per_cu_data *read_in_chain;

  380.   /* Backlink to our per_cu entry.  */
  381.   struct dwarf2_per_cu_data *per_cu;

  382.   /* How many compilation units ago was this CU last referenced?  */
  383.   int last_used;

  384.   /* A hash table of DIE cu_offset for following references with
  385.      die_info->offset.sect_off as hash.  */
  386.   htab_t die_hash;

  387.   /* Full DIEs if read in.  */
  388.   struct die_info *dies;

  389.   /* A set of pointers to dwarf2_per_cu_data objects for compilation
  390.      units referenced by this one.  Only set during full symbol processing;
  391.      partial symbol tables do not have dependencies.  */
  392.   htab_t dependencies;

  393.   /* Header data from the line table, during full symbol processing.  */
  394.   struct line_header *line_header;

  395.   /* A list of methods which need to have physnames computed
  396.      after all type information has been read.  */
  397.   VEC (delayed_method_info) *method_list;

  398.   /* To be copied to symtab->call_site_htab.  */
  399.   htab_t call_site_htab;

  400.   /* Non-NULL if this CU came from a DWO file.
  401.      There is an invariant here that is important to remember:
  402.      Except for attributes copied from the top level DIE in the "main"
  403.      (or "stub") file in preparation for reading the DWO file
  404.      (e.g., DW_AT_GNU_addr_base), we KISS: there is only *one* CU.
  405.      Either there isn't a DWO file (in which case this is NULL and the point
  406.      is moot), or there is and either we're not going to read it (in which
  407.      case this is NULL) or there is and we are reading it (in which case this
  408.      is non-NULL).  */
  409.   struct dwo_unit *dwo_unit;

  410.   /* The DW_AT_addr_base attribute if present, zero otherwise
  411.      (zero is a valid value though).
  412.      Note this value comes from the Fission stub CU/TU's DIE.  */
  413.   ULONGEST addr_base;

  414.   /* The DW_AT_ranges_base attribute if present, zero otherwise
  415.      (zero is a valid value though).
  416.      Note this value comes from the Fission stub CU/TU's DIE.
  417.      Also note that the value is zero in the non-DWO case so this value can
  418.      be used without needing to know whether DWO files are in use or not.
  419.      N.B. This does not apply to DW_AT_ranges appearing in
  420.      DW_TAG_compile_unit dies.  This is a bit of a wart, consider if ever
  421.      DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
  422.      DW_AT_ranges_base *would* have to be applied, and we'd have to care
  423.      whether the DW_AT_ranges attribute came from the skeleton or DWO.  */
  424.   ULONGEST ranges_base;

  425.   /* Mark used when releasing cached dies.  */
  426.   unsigned int mark : 1;

  427.   /* This CU references .debug_loc.  See the symtab->locations_valid field.
  428.      This test is imperfect as there may exist optimized debug code not using
  429.      any location list and still facing inlining issues if handled as
  430.      unoptimized code.  For a future better test see GCC PR other/32998.  */
  431.   unsigned int has_loclist : 1;

  432.   /* These cache the results for producer_is_* fields.  CHECKED_PRODUCER is set
  433.      if all the producer_is_* fields are valid.  This information is cached
  434.      because profiling CU expansion showed excessive time spent in
  435.      producer_is_gxx_lt_4_6.  */
  436.   unsigned int checked_producer : 1;
  437.   unsigned int producer_is_gxx_lt_4_6 : 1;
  438.   unsigned int producer_is_gcc_lt_4_3 : 1;
  439.   unsigned int producer_is_icc : 1;

  440.   /* When set, the file that we're processing is known to have
  441.      debugging info for C++ namespaces.  GCC 3.3.x did not produce
  442.      this information, but later versions do.  */

  443.   unsigned int processing_has_namespace_info : 1;
  444. };

  445. /* Persistent data held for a compilation unit, even when not
  446.    processing it.  We put a pointer to this structure in the
  447.    read_symtab_private field of the psymtab.  */

  448. struct dwarf2_per_cu_data
  449. {
  450.   /* The start offset and length of this compilation unit.
  451.      NOTE: Unlike comp_unit_head.length, this length includes
  452.      initial_length_size.
  453.      If the DIE refers to a DWO file, this is always of the original die,
  454.      not the DWO file.  */
  455.   sect_offset offset;
  456.   unsigned int length;

  457.   /* Flag indicating this compilation unit will be read in before
  458.      any of the current compilation units are processed.  */
  459.   unsigned int queued : 1;

  460.   /* This flag will be set when reading partial DIEs if we need to load
  461.      absolutely all DIEs for this compilation unit, instead of just the ones
  462.      we think are interesting.  It gets set if we look for a DIE in the
  463.      hash table and don't find it.  */
  464.   unsigned int load_all_dies : 1;

  465.   /* Non-zero if this CU is from .debug_types.
  466.      Struct dwarf2_per_cu_data is contained in struct signatured_type iff
  467.      this is non-zero.  */
  468.   unsigned int is_debug_types : 1;

  469.   /* Non-zero if this CU is from the .dwz file.  */
  470.   unsigned int is_dwz : 1;

  471.   /* Non-zero if reading a TU directly from a DWO file, bypassing the stub.
  472.      This flag is only valid if is_debug_types is true.
  473.      We can't read a CU directly from a DWO file: There are required
  474.      attributes in the stub.  */
  475.   unsigned int reading_dwo_directly : 1;

  476.   /* Non-zero if the TU has been read.
  477.      This is used to assist the "Stay in DWO Optimization" for Fission:
  478.      When reading a DWO, it's faster to read TUs from the DWO instead of
  479.      fetching them from random other DWOs (due to comdat folding).
  480.      If the TU has already been read, the optimization is unnecessary
  481.      (and unwise - we don't want to change where gdb thinks the TU lives
  482.      "midflight").
  483.      This flag is only valid if is_debug_types is true.  */
  484.   unsigned int tu_read : 1;

  485.   /* The section this CU/TU lives in.
  486.      If the DIE refers to a DWO file, this is always the original die,
  487.      not the DWO file.  */
  488.   struct dwarf2_section_info *section;

  489.   /* Set to non-NULL iff this CU is currently loaded.  When it gets freed out
  490.      of the CU cache it gets reset to NULL again.  */
  491.   struct dwarf2_cu *cu;

  492.   /* The corresponding objfile.
  493.      Normally we can get the objfile from dwarf2_per_objfile.
  494.      However we can enter this file with just a "per_cu" handle.  */
  495.   struct objfile *objfile;

  496.   /* When dwarf2_per_objfile->using_index is true, the 'quick' field
  497.      is active.  Otherwise, the 'psymtab' field is active.  */
  498.   union
  499.   {
  500.     /* The partial symbol table associated with this compilation unit,
  501.        or NULL for unread partial units.  */
  502.     struct partial_symtab *psymtab;

  503.     /* Data needed by the "quick" functions.  */
  504.     struct dwarf2_per_cu_quick_data *quick;
  505.   } v;

  506.   /* The CUs we import using DW_TAG_imported_unit.  This is filled in
  507.      while reading psymtabs, used to compute the psymtab dependencies,
  508.      and then cleared.  Then it is filled in again while reading full
  509.      symbols, and only deleted when the objfile is destroyed.

  510.      This is also used to work around a difference between the way gold
  511.      generates .gdb_index version <=7 and the way gdb does.  Arguably this
  512.      is a gold bug.  For symbols coming from TUs, gold records in the index
  513.      the CU that includes the TU instead of the TU itself.  This breaks
  514.      dw2_lookup_symbol: It assumes that if the index says symbol X lives
  515.      in CU/TU Y, then one need only expand Y and a subsequent lookup in Y
  516.      will find X.  Alas TUs live in their own symtab, so after expanding CU Y
  517.      we need to look in TU Z to find X.  Fortunately, this is akin to
  518.      DW_TAG_imported_unit, so we just use the same mechanism: For
  519.      .gdb_index version <=7 this also records the TUs that the CU referred
  520.      to.  Concurrently with this change gdb was modified to emit version 8
  521.      indices so we only pay a price for gold generated indices.
  522.      http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
  523.   VEC (dwarf2_per_cu_ptr) *imported_symtabs;
  524. };

  525. /* Entry in the signatured_types hash table.  */

  526. struct signatured_type
  527. {
  528.   /* The "per_cu" object of this type.
  529.      This struct is used iff per_cu.is_debug_types.
  530.      N.B.: This is the first member so that it's easy to convert pointers
  531.      between them.  */
  532.   struct dwarf2_per_cu_data per_cu;

  533.   /* The type's signature.  */
  534.   ULONGEST signature;

  535.   /* Offset in the TU of the type's DIE, as read from the TU header.
  536.      If this TU is a DWO stub and the definition lives in a DWO file
  537.      (specified by DW_AT_GNU_dwo_name), this value is unusable.  */
  538.   cu_offset type_offset_in_tu;

  539.   /* Offset in the section of the type's DIE.
  540.      If the definition lives in a DWO file, this is the offset in the
  541.      .debug_types.dwo section.
  542.      The value is zero until the actual value is known.
  543.      Zero is otherwise not a valid section offset.  */
  544.   sect_offset type_offset_in_section;

  545.   /* Type units are grouped by their DW_AT_stmt_list entry so that they
  546.      can share them.  This points to the containing symtab.  */
  547.   struct type_unit_group *type_unit_group;

  548.   /* The type.
  549.      The first time we encounter this type we fully read it in and install it
  550.      in the symbol tables.  Subsequent times we only need the type.  */
  551.   struct type *type;

  552.   /* Containing DWO unit.
  553.      This field is valid iff per_cu.reading_dwo_directly.  */
  554.   struct dwo_unit *dwo_unit;
  555. };

  556. typedef struct signatured_type *sig_type_ptr;
  557. DEF_VEC_P (sig_type_ptr);

  558. /* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
  559.    This includes type_unit_group and quick_file_names.  */

  560. struct stmt_list_hash
  561. {
  562.   /* The DWO unit this table is from or NULL if there is none.  */
  563.   struct dwo_unit *dwo_unit;

  564.   /* Offset in .debug_line or .debug_line.dwo.  */
  565.   sect_offset line_offset;
  566. };

  567. /* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
  568.    an object of this type.  */

  569. struct type_unit_group
  570. {
  571.   /* dwarf2read.c's main "handle" on a TU symtab.
  572.      To simplify things we create an artificial CU that "includes" all the
  573.      type units using this stmt_list so that the rest of the code still has
  574.      a "per_cu" handle on the symtab.
  575.      This PER_CU is recognized by having no section.  */
  576. #define IS_TYPE_UNIT_GROUP(per_cu) ((per_cu)->section == NULL)
  577.   struct dwarf2_per_cu_data per_cu;

  578.   /* The TUs that share this DW_AT_stmt_list entry.
  579.      This is added to while parsing type units to build partial symtabs,
  580.      and is deleted afterwards and not used again.  */
  581.   VEC (sig_type_ptr) *tus;

  582.   /* The compunit symtab.
  583.      Type units in a group needn't all be defined in the same source file,
  584.      so we create an essentially anonymous symtab as the compunit symtab.  */
  585.   struct compunit_symtab *compunit_symtab;

  586.   /* The data used to construct the hash key.  */
  587.   struct stmt_list_hash hash;

  588.   /* The number of symtabs from the line header.
  589.      The value here must match line_header.num_file_names.  */
  590.   unsigned int num_symtabs;

  591.   /* The symbol tables for this TU (obtained from the files listed in
  592.      DW_AT_stmt_list).
  593.      WARNING: The order of entries here must match the order of entries
  594.      in the line header.  After the first TU using this type_unit_group, the
  595.      line header for the subsequent TUs is recreated from this.  This is done
  596.      because we need to use the same symtabs for each TU using the same
  597.      DW_AT_stmt_list value.  Also note that symtabs may be repeated here,
  598.      there's no guarantee the line header doesn't have duplicate entries.  */
  599.   struct symtab **symtabs;
  600. };

  601. /* These sections are what may appear in a (real or virtual) DWO file.  */

  602. struct dwo_sections
  603. {
  604.   struct dwarf2_section_info abbrev;
  605.   struct dwarf2_section_info line;
  606.   struct dwarf2_section_info loc;
  607.   struct dwarf2_section_info macinfo;
  608.   struct dwarf2_section_info macro;
  609.   struct dwarf2_section_info str;
  610.   struct dwarf2_section_info str_offsets;
  611.   /* In the case of a virtual DWO file, these two are unused.  */
  612.   struct dwarf2_section_info info;
  613.   VEC (dwarf2_section_info_def) *types;
  614. };

  615. /* CUs/TUs in DWP/DWO files.  */

  616. struct dwo_unit
  617. {
  618.   /* Backlink to the containing struct dwo_file.  */
  619.   struct dwo_file *dwo_file;

  620.   /* The "id" that distinguishes this CU/TU.
  621.      .debug_info calls this "dwo_id", .debug_types calls this "signature".
  622.      Since signatures came first, we stick with it for consistency.  */
  623.   ULONGEST signature;

  624.   /* The section this CU/TU lives in, in the DWO file.  */
  625.   struct dwarf2_section_info *section;

  626.   /* Same as dwarf2_per_cu_data:{offset,length} but in the DWO section.  */
  627.   sect_offset offset;
  628.   unsigned int length;

  629.   /* For types, offset in the type's DIE of the type defined by this TU.  */
  630.   cu_offset type_offset_in_tu;
  631. };

  632. /* include/dwarf2.h defines the DWP section codes.
  633.    It defines a max value but it doesn't define a min value, which we
  634.    use for error checking, so provide one.  */

  635. enum dwp_v2_section_ids
  636. {
  637.   DW_SECT_MIN = 1
  638. };

  639. /* Data for one DWO file.

  640.    This includes virtual DWO files (a virtual DWO file is a DWO file as it
  641.    appears in a DWP file).  DWP files don't really have DWO files per se -
  642.    comdat folding of types "loses" the DWO file they came from, and from
  643.    a high level view DWP files appear to contain a mass of random types.
  644.    However, to maintain consistency with the non-DWP case we pretend DWP
  645.    files contain virtual DWO files, and we assign each TU with one virtual
  646.    DWO file (generally based on the line and abbrev section offsets -
  647.    a heuristic that seems to work in practice).  */

  648. struct dwo_file
  649. {
  650.   /* The DW_AT_GNU_dwo_name attribute.
  651.      For virtual DWO files the name is constructed from the section offsets
  652.      of abbrev,line,loc,str_offsets so that we combine virtual DWO files
  653.      from related CU+TUs.  */
  654.   const char *dwo_name;

  655.   /* The DW_AT_comp_dir attribute.  */
  656.   const char *comp_dir;

  657.   /* The bfd, when the file is open.  Otherwise this is NULL.
  658.      This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd.  */
  659.   bfd *dbfd;

  660.   /* The sections that make up this DWO file.
  661.      Remember that for virtual DWO files in DWP V2, these are virtual
  662.      sections (for lack of a better name).  */
  663.   struct dwo_sections sections;

  664.   /* The CU in the file.
  665.      We only support one because having more than one requires hacking the
  666.      dwo_name of each to match, which is highly unlikely to happen.
  667.      Doing this means all TUs can share comp_dir: We also assume that
  668.      DW_AT_comp_dir across all TUs in a DWO file will be identical.  */
  669.   struct dwo_unit *cu;

  670.   /* Table of TUs in the file.
  671.      Each element is a struct dwo_unit.  */
  672.   htab_t tus;
  673. };

  674. /* These sections are what may appear in a DWP file.  */

  675. struct dwp_sections
  676. {
  677.   /* These are used by both DWP version 1 and 2.  */
  678.   struct dwarf2_section_info str;
  679.   struct dwarf2_section_info cu_index;
  680.   struct dwarf2_section_info tu_index;

  681.   /* These are only used by DWP version 2 files.
  682.      In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
  683.      sections are referenced by section number, and are not recorded here.
  684.      In DWP version 2 there is at most one copy of all these sections, each
  685.      section being (effectively) comprised of the concatenation of all of the
  686.      individual sections that exist in the version 1 format.
  687.      To keep the code simple we treat each of these concatenated pieces as a
  688.      section itself (a virtual section?).  */
  689.   struct dwarf2_section_info abbrev;
  690.   struct dwarf2_section_info info;
  691.   struct dwarf2_section_info line;
  692.   struct dwarf2_section_info loc;
  693.   struct dwarf2_section_info macinfo;
  694.   struct dwarf2_section_info macro;
  695.   struct dwarf2_section_info str_offsets;
  696.   struct dwarf2_section_info types;
  697. };

  698. /* These sections are what may appear in a virtual DWO file in DWP version 1.
  699.    A virtual DWO file is a DWO file as it appears in a DWP file.  */

  700. struct virtual_v1_dwo_sections
  701. {
  702.   struct dwarf2_section_info abbrev;
  703.   struct dwarf2_section_info line;
  704.   struct dwarf2_section_info loc;
  705.   struct dwarf2_section_info macinfo;
  706.   struct dwarf2_section_info macro;
  707.   struct dwarf2_section_info str_offsets;
  708.   /* Each DWP hash table entry records one CU or one TU.
  709.      That is recorded here, and copied to dwo_unit.section.  */
  710.   struct dwarf2_section_info info_or_types;
  711. };

  712. /* Similar to virtual_v1_dwo_sections, but for DWP version 2.
  713.    In version 2, the sections of the DWO files are concatenated together
  714.    and stored in one section of that name.  Thus each ELF section contains
  715.    several "virtual" sections.  */

  716. struct virtual_v2_dwo_sections
  717. {
  718.   bfd_size_type abbrev_offset;
  719.   bfd_size_type abbrev_size;

  720.   bfd_size_type line_offset;
  721.   bfd_size_type line_size;

  722.   bfd_size_type loc_offset;
  723.   bfd_size_type loc_size;

  724.   bfd_size_type macinfo_offset;
  725.   bfd_size_type macinfo_size;

  726.   bfd_size_type macro_offset;
  727.   bfd_size_type macro_size;

  728.   bfd_size_type str_offsets_offset;
  729.   bfd_size_type str_offsets_size;

  730.   /* Each DWP hash table entry records one CU or one TU.
  731.      That is recorded here, and copied to dwo_unit.section.  */
  732.   bfd_size_type info_or_types_offset;
  733.   bfd_size_type info_or_types_size;
  734. };

  735. /* Contents of DWP hash tables.  */

  736. struct dwp_hash_table
  737. {
  738.   uint32_t version, nr_columns;
  739.   uint32_t nr_units, nr_slots;
  740.   const gdb_byte *hash_table, *unit_table;
  741.   union
  742.   {
  743.     struct
  744.     {
  745.       const gdb_byte *indices;
  746.     } v1;
  747.     struct
  748.     {
  749.       /* This is indexed by column number and gives the id of the section
  750.          in that column.  */
  751. #define MAX_NR_V2_DWO_SECTIONS \
  752.   (1 /* .debug_info or .debug_types */ \
  753.    + 1 /* .debug_abbrev */ \
  754.    + 1 /* .debug_line */ \
  755.    + 1 /* .debug_loc */ \
  756.    + 1 /* .debug_str_offsets */ \
  757.    + 1 /* .debug_macro or .debug_macinfo */)
  758.       int section_ids[MAX_NR_V2_DWO_SECTIONS];
  759.       const gdb_byte *offsets;
  760.       const gdb_byte *sizes;
  761.     } v2;
  762.   } section_pool;
  763. };

  764. /* Data for one DWP file.  */

  765. struct dwp_file
  766. {
  767.   /* Name of the file.  */
  768.   const char *name;

  769.   /* File format version.  */
  770.   int version;

  771.   /* The bfd.  */
  772.   bfd *dbfd;

  773.   /* Section info for this file.  */
  774.   struct dwp_sections sections;

  775.   /* Table of CUs in the file.  */
  776.   const struct dwp_hash_table *cus;

  777.   /* Table of TUs in the file.  */
  778.   const struct dwp_hash_table *tus;

  779.   /* Tables of loaded CUs/TUs.  Each entry is a struct dwo_unit *.  */
  780.   htab_t loaded_cus;
  781.   htab_t loaded_tus;

  782.   /* Table to map ELF section numbers to their sections.
  783.      This is only needed for the DWP V1 file format.  */
  784.   unsigned int num_sections;
  785.   asection **elf_sections;
  786. };

  787. /* This represents a '.dwz' file.  */

  788. struct dwz_file
  789. {
  790.   /* A dwz file can only contain a few sections.  */
  791.   struct dwarf2_section_info abbrev;
  792.   struct dwarf2_section_info info;
  793.   struct dwarf2_section_info str;
  794.   struct dwarf2_section_info line;
  795.   struct dwarf2_section_info macro;
  796.   struct dwarf2_section_info gdb_index;

  797.   /* The dwz's BFD.  */
  798.   bfd *dwz_bfd;
  799. };

  800. /* Struct used to pass misc. parameters to read_die_and_children, et
  801.    al.  which are used for both .debug_info and .debug_types dies.
  802.    All parameters here are unchanging for the life of the call.  This
  803.    struct exists to abstract away the constant parameters of die reading.  */

  804. struct die_reader_specs
  805. {
  806.   /* The bfd of die_section.  */
  807.   bfd* abfd;

  808.   /* The CU of the DIE we are parsing.  */
  809.   struct dwarf2_cu *cu;

  810.   /* Non-NULL if reading a DWO file (including one packaged into a DWP).  */
  811.   struct dwo_file *dwo_file;

  812.   /* The section the die comes from.
  813.      This is either .debug_info or .debug_types, or the .dwo variants.  */
  814.   struct dwarf2_section_info *die_section;

  815.   /* die_section->buffer.  */
  816.   const gdb_byte *buffer;

  817.   /* The end of the buffer.  */
  818.   const gdb_byte *buffer_end;

  819.   /* The value of the DW_AT_comp_dir attribute.  */
  820.   const char *comp_dir;
  821. };

  822. /* Type of function passed to init_cutu_and_read_dies, et.al.  */
  823. typedef void (die_reader_func_ftype) (const struct die_reader_specs *reader,
  824.                                       const gdb_byte *info_ptr,
  825.                                       struct die_info *comp_unit_die,
  826.                                       int has_children,
  827.                                       void *data);

  828. /* The line number information for a compilation unit (found in the
  829.    .debug_line section) begins with a "statement program header",
  830.    which contains the following information.  */
  831. struct line_header
  832. {
  833.   unsigned int total_length;
  834.   unsigned short version;
  835.   unsigned int header_length;
  836.   unsigned char minimum_instruction_length;
  837.   unsigned char maximum_ops_per_instruction;
  838.   unsigned char default_is_stmt;
  839.   int line_base;
  840.   unsigned char line_range;
  841.   unsigned char opcode_base;

  842.   /* standard_opcode_lengths[i] is the number of operands for the
  843.      standard opcode whose value is i.  This means that
  844.      standard_opcode_lengths[0] is unused, and the last meaningful
  845.      element is standard_opcode_lengths[opcode_base - 1].  */
  846.   unsigned char *standard_opcode_lengths;

  847.   /* The include_directories table.  NOTE!  These strings are not
  848.      allocated with xmalloc; instead, they are pointers into
  849.      debug_line_buffer.  If you try to free them, `free' will get
  850.      indigestion.  */
  851.   unsigned int num_include_dirs, include_dirs_size;
  852.   const char **include_dirs;

  853.   /* The file_names table.  NOTE!  These strings are not allocated
  854.      with xmalloc; instead, they are pointers into debug_line_buffer.
  855.      Don't try to free them directly.  */
  856.   unsigned int num_file_names, file_names_size;
  857.   struct file_entry
  858.   {
  859.     const char *name;
  860.     unsigned int dir_index;
  861.     unsigned int mod_time;
  862.     unsigned int length;
  863.     int included_p; /* Non-zero if referenced by the Line Number Program.  */
  864.     struct symtab *symtab; /* The associated symbol table, if any.  */
  865.   } *file_names;

  866.   /* The start and end of the statement program following this
  867.      header.  These point into dwarf2_per_objfile->line_buffer.  */
  868.   const gdb_byte *statement_program_start, *statement_program_end;
  869. };

  870. /* When we construct a partial symbol table entry we only
  871.    need this much information.  */
  872. struct partial_die_info
  873.   {
  874.     /* Offset of this DIE.  */
  875.     sect_offset offset;

  876.     /* DWARF-2 tag for this DIE.  */
  877.     ENUM_BITFIELD(dwarf_tag) tag : 16;

  878.     /* Assorted flags describing the data found in this DIE.  */
  879.     unsigned int has_children : 1;
  880.     unsigned int is_external : 1;
  881.     unsigned int is_declaration : 1;
  882.     unsigned int has_type : 1;
  883.     unsigned int has_specification : 1;
  884.     unsigned int has_pc_info : 1;
  885.     unsigned int may_be_inlined : 1;

  886.     /* Flag set if the SCOPE field of this structure has been
  887.        computed.  */
  888.     unsigned int scope_set : 1;

  889.     /* Flag set if the DIE has a byte_size attribute.  */
  890.     unsigned int has_byte_size : 1;

  891.     /* Flag set if any of the DIE's children are template arguments.  */
  892.     unsigned int has_template_arguments : 1;

  893.     /* Flag set if fixup_partial_die has been called on this die.  */
  894.     unsigned int fixup_called : 1;

  895.     /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt.  */
  896.     unsigned int is_dwz : 1;

  897.     /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt.  */
  898.     unsigned int spec_is_dwz : 1;

  899.     /* The name of this DIE.  Normally the value of DW_AT_name, but
  900.        sometimes a default name for unnamed DIEs.  */
  901.     const char *name;

  902.     /* The linkage name, if present.  */
  903.     const char *linkage_name;

  904.     /* The scope to prepend to our children.  This is generally
  905.        allocated on the comp_unit_obstack, so will disappear
  906.        when this compilation unit leaves the cache.  */
  907.     const char *scope;

  908.     /* Some data associated with the partial DIE.  The tag determines
  909.        which field is live.  */
  910.     union
  911.     {
  912.       /* The location description associated with this DIE, if any.  */
  913.       struct dwarf_block *locdesc;
  914.       /* The offset of an import, for DW_TAG_imported_unit.  */
  915.       sect_offset offset;
  916.     } d;

  917.     /* If HAS_PC_INFO, the PC range associated with this DIE.  */
  918.     CORE_ADDR lowpc;
  919.     CORE_ADDR highpc;

  920.     /* Pointer into the info_buffer (or types_buffer) pointing at the target of
  921.        DW_AT_sibling, if any.  */
  922.     /* NOTE: This member isn't strictly necessary, read_partial_die could
  923.        return DW_AT_sibling values to its caller load_partial_dies.  */
  924.     const gdb_byte *sibling;

  925.     /* If HAS_SPECIFICATION, the offset of the DIE referred to by
  926.        DW_AT_specification (or DW_AT_abstract_origin or
  927.        DW_AT_extension).  */
  928.     sect_offset spec_offset;

  929.     /* Pointers to this DIE's parent, first child, and next sibling,
  930.        if any.  */
  931.     struct partial_die_info *die_parent, *die_child, *die_sibling;
  932.   };

  933. /* This data structure holds the information of an abbrev.  */
  934. struct abbrev_info
  935.   {
  936.     unsigned int number;        /* number identifying abbrev */
  937.     enum dwarf_tag tag;                /* dwarf tag */
  938.     unsigned short has_children;                /* boolean */
  939.     unsigned short num_attrs;        /* number of attributes */
  940.     struct attr_abbrev *attrs;        /* an array of attribute descriptions */
  941.     struct abbrev_info *next;        /* next in chain */
  942.   };

  943. struct attr_abbrev
  944.   {
  945.     ENUM_BITFIELD(dwarf_attribute) name : 16;
  946.     ENUM_BITFIELD(dwarf_form) form : 16;
  947.   };

  948. /* Size of abbrev_table.abbrev_hash_table.  */
  949. #define ABBREV_HASH_SIZE 121

  950. /* Top level data structure to contain an abbreviation table.  */

  951. struct abbrev_table
  952. {
  953.   /* Where the abbrev table came from.
  954.      This is used as a sanity check when the table is used.  */
  955.   sect_offset offset;

  956.   /* Storage for the abbrev table.  */
  957.   struct obstack abbrev_obstack;

  958.   /* Hash table of abbrevs.
  959.      This is an array of size ABBREV_HASH_SIZE allocated in abbrev_obstack.
  960.      It could be statically allocated, but the previous code didn't so we
  961.      don't either.  */
  962.   struct abbrev_info **abbrevs;
  963. };

  964. /* Attributes have a name and a value.  */
  965. struct attribute
  966.   {
  967.     ENUM_BITFIELD(dwarf_attribute) name : 16;
  968.     ENUM_BITFIELD(dwarf_form) form : 15;

  969.     /* Has DW_STRING already been updated by dwarf2_canonicalize_name?  This
  970.        field should be in u.str (existing only for DW_STRING) but it is kept
  971.        here for better struct attribute alignment.  */
  972.     unsigned int string_is_canonical : 1;

  973.     union
  974.       {
  975.         const char *str;
  976.         struct dwarf_block *blk;
  977.         ULONGEST unsnd;
  978.         LONGEST snd;
  979.         CORE_ADDR addr;
  980.         ULONGEST signature;
  981.       }
  982.     u;
  983.   };

  984. /* This data structure holds a complete die structure.  */
  985. struct die_info
  986.   {
  987.     /* DWARF-2 tag for this DIE.  */
  988.     ENUM_BITFIELD(dwarf_tag) tag : 16;

  989.     /* Number of attributes */
  990.     unsigned char num_attrs;

  991.     /* True if we're presently building the full type name for the
  992.        type derived from this DIE.  */
  993.     unsigned char building_fullname : 1;

  994.     /* True if this die is in process.  PR 16581.  */
  995.     unsigned char in_process : 1;

  996.     /* Abbrev number */
  997.     unsigned int abbrev;

  998.     /* Offset in .debug_info or .debug_types section.  */
  999.     sect_offset offset;

  1000.     /* The dies in a compilation unit form an n-ary tree.  PARENT
  1001.        points to this die's parent; CHILD points to the first child of
  1002.        this node; and all the children of a given node are chained
  1003.        together via their SIBLING fields.  */
  1004.     struct die_info *child;        /* Its first child, if any.  */
  1005.     struct die_info *sibling;        /* Its next sibling, if any.  */
  1006.     struct die_info *parent;        /* Its parent, if any.  */

  1007.     /* An array of attributes, with NUM_ATTRS elements.  There may be
  1008.        zero, but it's not common and zero-sized arrays are not
  1009.        sufficiently portable C.  */
  1010.     struct attribute attrs[1];
  1011.   };

  1012. /* Get at parts of an attribute structure.  */

  1013. #define DW_STRING(attr)    ((attr)->u.str)
  1014. #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
  1015. #define DW_UNSND(attr)     ((attr)->u.unsnd)
  1016. #define DW_BLOCK(attr)     ((attr)->u.blk)
  1017. #define DW_SND(attr)       ((attr)->u.snd)
  1018. #define DW_ADDR(attr)           ((attr)->u.addr)
  1019. #define DW_SIGNATURE(attr) ((attr)->u.signature)

  1020. /* Blocks are a bunch of untyped bytes.  */
  1021. struct dwarf_block
  1022.   {
  1023.     size_t size;

  1024.     /* Valid only if SIZE is not zero.  */
  1025.     const gdb_byte *data;
  1026.   };

  1027. #ifndef ATTR_ALLOC_CHUNK
  1028. #define ATTR_ALLOC_CHUNK 4
  1029. #endif

  1030. /* Allocate fields for structs, unions and enums in this size.  */
  1031. #ifndef DW_FIELD_ALLOC_CHUNK
  1032. #define DW_FIELD_ALLOC_CHUNK 4
  1033. #endif

  1034. /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
  1035.    but this would require a corresponding change in unpack_field_as_long
  1036.    and friends.  */
  1037. static int bits_per_byte = 8;

  1038. /* The routines that read and process dies for a C struct or C++ class
  1039.    pass lists of data member fields and lists of member function fields
  1040.    in an instance of a field_info structure, as defined below.  */
  1041. struct field_info
  1042.   {
  1043.     /* List of data member and baseclasses fields.  */
  1044.     struct nextfield
  1045.       {
  1046.         struct nextfield *next;
  1047.         int accessibility;
  1048.         int virtuality;
  1049.         struct field field;
  1050.       }
  1051.      *fields, *baseclasses;

  1052.     /* Number of fields (including baseclasses).  */
  1053.     int nfields;

  1054.     /* Number of baseclasses.  */
  1055.     int nbaseclasses;

  1056.     /* Set if the accesibility of one of the fields is not public.  */
  1057.     int non_public_fields;

  1058.     /* Member function fields array, entries are allocated in the order they
  1059.        are encountered in the object file.  */
  1060.     struct nextfnfield
  1061.       {
  1062.         struct nextfnfield *next;
  1063.         struct fn_field fnfield;
  1064.       }
  1065.      *fnfields;

  1066.     /* Member function fieldlist array, contains name of possibly overloaded
  1067.        member function, number of overloaded member functions and a pointer
  1068.        to the head of the member function field chain.  */
  1069.     struct fnfieldlist
  1070.       {
  1071.         const char *name;
  1072.         int length;
  1073.         struct nextfnfield *head;
  1074.       }
  1075.      *fnfieldlists;

  1076.     /* Number of entries in the fnfieldlists array.  */
  1077.     int nfnfields;

  1078.     /* typedefs defined inside this class.  TYPEDEF_FIELD_LIST contains head of
  1079.        a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements.  */
  1080.     struct typedef_field_list
  1081.       {
  1082.         struct typedef_field field;
  1083.         struct typedef_field_list *next;
  1084.       }
  1085.     *typedef_field_list;
  1086.     unsigned typedef_field_list_count;
  1087.   };

  1088. /* One item on the queue of compilation units to read in full symbols
  1089.    for.  */
  1090. struct dwarf2_queue_item
  1091. {
  1092.   struct dwarf2_per_cu_data *per_cu;
  1093.   enum language pretend_language;
  1094.   struct dwarf2_queue_item *next;
  1095. };

  1096. /* The current queue.  */
  1097. static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;

  1098. /* Loaded secondary compilation units are kept in memory until they
  1099.    have not been referenced for the processing of this many
  1100.    compilation units.  Set this to zero to disable caching.  Cache
  1101.    sizes of up to at least twenty will improve startup time for
  1102.    typical inter-CU-reference binaries, at an obvious memory cost.  */
  1103. static int dwarf2_max_cache_age = 5;
  1104. static void
  1105. show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
  1106.                            struct cmd_list_element *c, const char *value)
  1107. {
  1108.   fprintf_filtered (file, _("The upper bound on the age of cached "
  1109.                             "dwarf2 compilation units is %s.\n"),
  1110.                     value);
  1111. }

  1112. /* local function prototypes */

  1113. static const char *get_section_name (const struct dwarf2_section_info *);

  1114. static const char *get_section_file_name (const struct dwarf2_section_info *);

  1115. static void dwarf2_locate_sections (bfd *, asection *, void *);

  1116. static void dwarf2_find_base_address (struct die_info *die,
  1117.                                       struct dwarf2_cu *cu);

  1118. static struct partial_symtab *create_partial_symtab
  1119.   (struct dwarf2_per_cu_data *per_cu, const char *name);

  1120. static void dwarf2_build_psymtabs_hard (struct objfile *);

  1121. static void scan_partial_symbols (struct partial_die_info *,
  1122.                                   CORE_ADDR *, CORE_ADDR *,
  1123.                                   int, struct dwarf2_cu *);

  1124. static void add_partial_symbol (struct partial_die_info *,
  1125.                                 struct dwarf2_cu *);

  1126. static void add_partial_namespace (struct partial_die_info *pdi,
  1127.                                    CORE_ADDR *lowpc, CORE_ADDR *highpc,
  1128.                                    int set_addrmap, struct dwarf2_cu *cu);

  1129. static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
  1130.                                 CORE_ADDR *highpc, int set_addrmap,
  1131.                                 struct dwarf2_cu *cu);

  1132. static void add_partial_enumeration (struct partial_die_info *enum_pdi,
  1133.                                      struct dwarf2_cu *cu);

  1134. static void add_partial_subprogram (struct partial_die_info *pdi,
  1135.                                     CORE_ADDR *lowpc, CORE_ADDR *highpc,
  1136.                                     int need_pc, struct dwarf2_cu *cu);

  1137. static void dwarf2_read_symtab (struct partial_symtab *,
  1138.                                 struct objfile *);

  1139. static void psymtab_to_symtab_1 (struct partial_symtab *);

  1140. static struct abbrev_info *abbrev_table_lookup_abbrev
  1141.   (const struct abbrev_table *, unsigned int);

  1142. static struct abbrev_table *abbrev_table_read_table
  1143.   (struct dwarf2_section_info *, sect_offset);

  1144. static void abbrev_table_free (struct abbrev_table *);

  1145. static void abbrev_table_free_cleanup (void *);

  1146. static void dwarf2_read_abbrevs (struct dwarf2_cu *,
  1147.                                  struct dwarf2_section_info *);

  1148. static void dwarf2_free_abbrev_table (void *);

  1149. static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);

  1150. static struct partial_die_info *load_partial_dies
  1151.   (const struct die_reader_specs *, const gdb_byte *, int);

  1152. static const gdb_byte *read_partial_die (const struct die_reader_specs *,
  1153.                                          struct partial_die_info *,
  1154.                                          struct abbrev_info *,
  1155.                                          unsigned int,
  1156.                                          const gdb_byte *);

  1157. static struct partial_die_info *find_partial_die (sect_offset, int,
  1158.                                                   struct dwarf2_cu *);

  1159. static void fixup_partial_die (struct partial_die_info *,
  1160.                                struct dwarf2_cu *);

  1161. static const gdb_byte *read_attribute (const struct die_reader_specs *,
  1162.                                        struct attribute *, struct attr_abbrev *,
  1163.                                        const gdb_byte *);

  1164. static unsigned int read_1_byte (bfd *, const gdb_byte *);

  1165. static int read_1_signed_byte (bfd *, const gdb_byte *);

  1166. static unsigned int read_2_bytes (bfd *, const gdb_byte *);

  1167. static unsigned int read_4_bytes (bfd *, const gdb_byte *);

  1168. static ULONGEST read_8_bytes (bfd *, const gdb_byte *);

  1169. static CORE_ADDR read_address (bfd *, const gdb_byte *ptr, struct dwarf2_cu *,
  1170.                                unsigned int *);

  1171. static LONGEST read_initial_length (bfd *, const gdb_byte *, unsigned int *);

  1172. static LONGEST read_checked_initial_length_and_offset
  1173.   (bfd *, const gdb_byte *, const struct comp_unit_head *,
  1174.    unsigned int *, unsigned int *);

  1175. static LONGEST read_offset (bfd *, const gdb_byte *,
  1176.                             const struct comp_unit_head *,
  1177.                             unsigned int *);

  1178. static LONGEST read_offset_1 (bfd *, const gdb_byte *, unsigned int);

  1179. static sect_offset read_abbrev_offset (struct dwarf2_section_info *,
  1180.                                        sect_offset);

  1181. static const gdb_byte *read_n_bytes (bfd *, const gdb_byte *, unsigned int);

  1182. static const char *read_direct_string (bfd *, const gdb_byte *, unsigned int *);

  1183. static const char *read_indirect_string (bfd *, const gdb_byte *,
  1184.                                          const struct comp_unit_head *,
  1185.                                          unsigned int *);

  1186. static const char *read_indirect_string_from_dwz (struct dwz_file *, LONGEST);

  1187. static ULONGEST read_unsigned_leb128 (bfd *, const gdb_byte *, unsigned int *);

  1188. static LONGEST read_signed_leb128 (bfd *, const gdb_byte *, unsigned int *);

  1189. static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
  1190.                                               const gdb_byte *,
  1191.                                               unsigned int *);

  1192. static const char *read_str_index (const struct die_reader_specs *reader,
  1193.                                    ULONGEST str_index);

  1194. static void set_cu_language (unsigned int, struct dwarf2_cu *);

  1195. static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
  1196.                                       struct dwarf2_cu *);

  1197. static struct attribute *dwarf2_attr_no_follow (struct die_info *,
  1198.                                                 unsigned int);

  1199. static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
  1200.                                struct dwarf2_cu *cu);

  1201. static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);

  1202. static struct die_info *die_specification (struct die_info *die,
  1203.                                            struct dwarf2_cu **);

  1204. static void free_line_header (struct line_header *lh);

  1205. static struct line_header *dwarf_decode_line_header (unsigned int offset,
  1206.                                                      struct dwarf2_cu *cu);

  1207. static void dwarf_decode_lines (struct line_header *, const char *,
  1208.                                 struct dwarf2_cu *, struct partial_symtab *,
  1209.                                 CORE_ADDR);

  1210. static void dwarf2_start_subfile (const char *, const char *);

  1211. static struct compunit_symtab *dwarf2_start_symtab (struct dwarf2_cu *,
  1212.                                                     const char *, const char *,
  1213.                                                     CORE_ADDR);

  1214. static struct symbol *new_symbol (struct die_info *, struct type *,
  1215.                                   struct dwarf2_cu *);

  1216. static struct symbol *new_symbol_full (struct die_info *, struct type *,
  1217.                                        struct dwarf2_cu *, struct symbol *);

  1218. static void dwarf2_const_value (const struct attribute *, struct symbol *,
  1219.                                 struct dwarf2_cu *);

  1220. static void dwarf2_const_value_attr (const struct attribute *attr,
  1221.                                      struct type *type,
  1222.                                      const char *name,
  1223.                                      struct obstack *obstack,
  1224.                                      struct dwarf2_cu *cu, LONGEST *value,
  1225.                                      const gdb_byte **bytes,
  1226.                                      struct dwarf2_locexpr_baton **baton);

  1227. static struct type *die_type (struct die_info *, struct dwarf2_cu *);

  1228. static int need_gnat_info (struct dwarf2_cu *);

  1229. static struct type *die_descriptive_type (struct die_info *,
  1230.                                           struct dwarf2_cu *);

  1231. static void set_descriptive_type (struct type *, struct die_info *,
  1232.                                   struct dwarf2_cu *);

  1233. static struct type *die_containing_type (struct die_info *,
  1234.                                          struct dwarf2_cu *);

  1235. static struct type *lookup_die_type (struct die_info *, const struct attribute *,
  1236.                                      struct dwarf2_cu *);

  1237. static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);

  1238. static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);

  1239. static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);

  1240. static char *typename_concat (struct obstack *obs, const char *prefix,
  1241.                               const char *suffix, int physname,
  1242.                               struct dwarf2_cu *cu);

  1243. static void read_file_scope (struct die_info *, struct dwarf2_cu *);

  1244. static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);

  1245. static void read_func_scope (struct die_info *, struct dwarf2_cu *);

  1246. static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);

  1247. static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);

  1248. static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
  1249.                                struct dwarf2_cu *, struct partial_symtab *);

  1250. static int dwarf2_get_pc_bounds (struct die_info *,
  1251.                                  CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
  1252.                                  struct partial_symtab *);

  1253. static void get_scope_pc_bounds (struct die_info *,
  1254.                                  CORE_ADDR *, CORE_ADDR *,
  1255.                                  struct dwarf2_cu *);

  1256. static void dwarf2_record_block_ranges (struct die_info *, struct block *,
  1257.                                         CORE_ADDR, struct dwarf2_cu *);

  1258. static void dwarf2_add_field (struct field_info *, struct die_info *,
  1259.                               struct dwarf2_cu *);

  1260. static void dwarf2_attach_fields_to_type (struct field_info *,
  1261.                                           struct type *, struct dwarf2_cu *);

  1262. static void dwarf2_add_member_fn (struct field_info *,
  1263.                                   struct die_info *, struct type *,
  1264.                                   struct dwarf2_cu *);

  1265. static void dwarf2_attach_fn_fields_to_type (struct field_info *,
  1266.                                              struct type *,
  1267.                                              struct dwarf2_cu *);

  1268. static void process_structure_scope (struct die_info *, struct dwarf2_cu *);

  1269. static void read_common_block (struct die_info *, struct dwarf2_cu *);

  1270. static void read_namespace (struct die_info *die, struct dwarf2_cu *);

  1271. static void read_module (struct die_info *die, struct dwarf2_cu *cu);

  1272. static void read_import_statement (struct die_info *die, struct dwarf2_cu *);

  1273. static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);

  1274. static struct type *read_module_type (struct die_info *die,
  1275.                                       struct dwarf2_cu *cu);

  1276. static const char *namespace_name (struct die_info *die,
  1277.                                    int *is_anonymous, struct dwarf2_cu *);

  1278. static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);

  1279. static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);

  1280. static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
  1281.                                                        struct dwarf2_cu *);

  1282. static struct die_info *read_die_and_siblings_1
  1283.   (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
  1284.    struct die_info *);

  1285. static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
  1286.                                                const gdb_byte *info_ptr,
  1287.                                                const gdb_byte **new_info_ptr,
  1288.                                                struct die_info *parent);

  1289. static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
  1290.                                         struct die_info **, const gdb_byte *,
  1291.                                         int *, int);

  1292. static const gdb_byte *read_full_die (const struct die_reader_specs *,
  1293.                                       struct die_info **, const gdb_byte *,
  1294.                                       int *);

  1295. static void process_die (struct die_info *, struct dwarf2_cu *);

  1296. static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
  1297.                                              struct obstack *);

  1298. static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);

  1299. static const char *dwarf2_full_name (const char *name,
  1300.                                      struct die_info *die,
  1301.                                      struct dwarf2_cu *cu);

  1302. static const char *dwarf2_physname (const char *name, struct die_info *die,
  1303.                                     struct dwarf2_cu *cu);

  1304. static struct die_info *dwarf2_extension (struct die_info *die,
  1305.                                           struct dwarf2_cu **);

  1306. static const char *dwarf_tag_name (unsigned int);

  1307. static const char *dwarf_attr_name (unsigned int);

  1308. static const char *dwarf_form_name (unsigned int);

  1309. static char *dwarf_bool_name (unsigned int);

  1310. static const char *dwarf_type_encoding_name (unsigned int);

  1311. static struct die_info *sibling_die (struct die_info *);

  1312. static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);

  1313. static void dump_die_for_error (struct die_info *);

  1314. static void dump_die_1 (struct ui_file *, int level, int max_level,
  1315.                         struct die_info *);

  1316. /*static*/ void dump_die (struct die_info *, int max_level);

  1317. static void store_in_ref_table (struct die_info *,
  1318.                                 struct dwarf2_cu *);

  1319. static sect_offset dwarf2_get_ref_die_offset (const struct attribute *);

  1320. static LONGEST dwarf2_get_attr_constant_value (const struct attribute *, int);

  1321. static struct die_info *follow_die_ref_or_sig (struct die_info *,
  1322.                                                const struct attribute *,
  1323.                                                struct dwarf2_cu **);

  1324. static struct die_info *follow_die_ref (struct die_info *,
  1325.                                         const struct attribute *,
  1326.                                         struct dwarf2_cu **);

  1327. static struct die_info *follow_die_sig (struct die_info *,
  1328.                                         const struct attribute *,
  1329.                                         struct dwarf2_cu **);

  1330. static struct type *get_signatured_type (struct die_info *, ULONGEST,
  1331.                                          struct dwarf2_cu *);

  1332. static struct type *get_DW_AT_signature_type (struct die_info *,
  1333.                                               const struct attribute *,
  1334.                                               struct dwarf2_cu *);

  1335. static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);

  1336. static void read_signatured_type (struct signatured_type *);

  1337. /* memory allocation interface */

  1338. static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);

  1339. static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);

  1340. static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);

  1341. static int attr_form_is_block (const struct attribute *);

  1342. static int attr_form_is_section_offset (const struct attribute *);

  1343. static int attr_form_is_constant (const struct attribute *);

  1344. static int attr_form_is_ref (const struct attribute *);

  1345. static void fill_in_loclist_baton (struct dwarf2_cu *cu,
  1346.                                    struct dwarf2_loclist_baton *baton,
  1347.                                    const struct attribute *attr);

  1348. static void dwarf2_symbol_mark_computed (const struct attribute *attr,
  1349.                                          struct symbol *sym,
  1350.                                          struct dwarf2_cu *cu,
  1351.                                          int is_block);

  1352. static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
  1353.                                      const gdb_byte *info_ptr,
  1354.                                      struct abbrev_info *abbrev);

  1355. static void free_stack_comp_unit (void *);

  1356. static hashval_t partial_die_hash (const void *item);

  1357. static int partial_die_eq (const void *item_lhs, const void *item_rhs);

  1358. static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
  1359.   (sect_offset offset, unsigned int offset_in_dwz, struct objfile *objfile);

  1360. static void init_one_comp_unit (struct dwarf2_cu *cu,
  1361.                                 struct dwarf2_per_cu_data *per_cu);

  1362. static void prepare_one_comp_unit (struct dwarf2_cu *cu,
  1363.                                    struct die_info *comp_unit_die,
  1364.                                    enum language pretend_language);

  1365. static void free_heap_comp_unit (void *);

  1366. static void free_cached_comp_units (void *);

  1367. static void age_cached_comp_units (void);

  1368. static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);

  1369. static struct type *set_die_type (struct die_info *, struct type *,
  1370.                                   struct dwarf2_cu *);

  1371. static void create_all_comp_units (struct objfile *);

  1372. static int create_all_type_units (struct objfile *);

  1373. static void load_full_comp_unit (struct dwarf2_per_cu_data *,
  1374.                                  enum language);

  1375. static void process_full_comp_unit (struct dwarf2_per_cu_data *,
  1376.                                     enum language);

  1377. static void process_full_type_unit (struct dwarf2_per_cu_data *,
  1378.                                     enum language);

  1379. static void dwarf2_add_dependence (struct dwarf2_cu *,
  1380.                                    struct dwarf2_per_cu_data *);

  1381. static void dwarf2_mark (struct dwarf2_cu *);

  1382. static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);

  1383. static struct type *get_die_type_at_offset (sect_offset,
  1384.                                             struct dwarf2_per_cu_data *);

  1385. static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);

  1386. static void dwarf2_release_queue (void *dummy);

  1387. static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
  1388.                              enum language pretend_language);

  1389. static void process_queue (void);

  1390. static void find_file_and_directory (struct die_info *die,
  1391.                                      struct dwarf2_cu *cu,
  1392.                                      const char **name, const char **comp_dir);

  1393. static char *file_full_name (int file, struct line_header *lh,
  1394.                              const char *comp_dir);

  1395. static const gdb_byte *read_and_check_comp_unit_head
  1396.   (struct comp_unit_head *header,
  1397.    struct dwarf2_section_info *section,
  1398.    struct dwarf2_section_info *abbrev_section, const gdb_byte *info_ptr,
  1399.    int is_debug_types_section);

  1400. static void init_cutu_and_read_dies
  1401.   (struct dwarf2_per_cu_data *this_cu, struct abbrev_table *abbrev_table,
  1402.    int use_existing_cu, int keep,
  1403.    die_reader_func_ftype *die_reader_func, void *data);

  1404. static void init_cutu_and_read_dies_simple
  1405.   (struct dwarf2_per_cu_data *this_cu,
  1406.    die_reader_func_ftype *die_reader_func, void *data);

  1407. static htab_t allocate_signatured_type_table (struct objfile *objfile);

  1408. static htab_t allocate_dwo_unit_table (struct objfile *objfile);

  1409. static struct dwo_unit *lookup_dwo_unit_in_dwp
  1410.   (struct dwp_file *dwp_file, const char *comp_dir,
  1411.    ULONGEST signature, int is_debug_types);

  1412. static struct dwp_file *get_dwp_file (void);

  1413. static struct dwo_unit *lookup_dwo_comp_unit
  1414.   (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);

  1415. static struct dwo_unit *lookup_dwo_type_unit
  1416.   (struct signatured_type *, const char *, const char *);

  1417. static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);

  1418. static void free_dwo_file_cleanup (void *);

  1419. static void process_cu_includes (void);

  1420. static void check_producer (struct dwarf2_cu *cu);

  1421. /* Various complaints about symbol reading that don't abort the process.  */

  1422. static void
  1423. dwarf2_statement_list_fits_in_line_number_section_complaint (void)
  1424. {
  1425.   complaint (&symfile_complaints,
  1426.              _("statement list doesn't fit in .debug_line section"));
  1427. }

  1428. static void
  1429. dwarf2_debug_line_missing_file_complaint (void)
  1430. {
  1431.   complaint (&symfile_complaints,
  1432.              _(".debug_line section has line data without a file"));
  1433. }

  1434. static void
  1435. dwarf2_debug_line_missing_end_sequence_complaint (void)
  1436. {
  1437.   complaint (&symfile_complaints,
  1438.              _(".debug_line section has line "
  1439.                "program sequence without an end"));
  1440. }

  1441. static void
  1442. dwarf2_complex_location_expr_complaint (void)
  1443. {
  1444.   complaint (&symfile_complaints, _("location expression too complex"));
  1445. }

  1446. static void
  1447. dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
  1448.                                               int arg3)
  1449. {
  1450.   complaint (&symfile_complaints,
  1451.              _("const value length mismatch for '%s', got %d, expected %d"),
  1452.              arg1, arg2, arg3);
  1453. }

  1454. static void
  1455. dwarf2_section_buffer_overflow_complaint (struct dwarf2_section_info *section)
  1456. {
  1457.   complaint (&symfile_complaints,
  1458.              _("debug info runs off end of %s section"
  1459.                " [in module %s]"),
  1460.              get_section_name (section),
  1461.              get_section_file_name (section));
  1462. }

  1463. static void
  1464. dwarf2_macro_malformed_definition_complaint (const char *arg1)
  1465. {
  1466.   complaint (&symfile_complaints,
  1467.              _("macro debug info contains a "
  1468.                "malformed macro definition:\n`%s'"),
  1469.              arg1);
  1470. }

  1471. static void
  1472. dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
  1473. {
  1474.   complaint (&symfile_complaints,
  1475.              _("invalid attribute class or form for '%s' in '%s'"),
  1476.              arg1, arg2);
  1477. }

  1478. #if WORDS_BIGENDIAN

  1479. /* Convert VALUE between big- and little-endian.  */
  1480. static offset_type
  1481. byte_swap (offset_type value)
  1482. {
  1483.   offset_type result;

  1484.   result = (value & 0xff) << 24;
  1485.   result |= (value & 0xff00) << 8;
  1486.   result |= (value & 0xff0000) >> 8;
  1487.   result |= (value & 0xff000000) >> 24;
  1488.   return result;
  1489. }

  1490. #define MAYBE_SWAP(Vbyte_swap (V)

  1491. #else
  1492. #define MAYBE_SWAP(V) (V)
  1493. #endif /* WORDS_BIGENDIAN */

  1494. /* Read the given attribute value as an address, taking the attribute's
  1495.    form into account.  */

  1496. static CORE_ADDR
  1497. attr_value_as_address (struct attribute *attr)
  1498. {
  1499.   CORE_ADDR addr;

  1500.   if (attr->form != DW_FORM_addr && attr->form != DW_FORM_GNU_addr_index)
  1501.     {
  1502.       /* Aside from a few clearly defined exceptions, attributes that
  1503.          contain an address must always be in DW_FORM_addr form.
  1504.          Unfortunately, some compilers happen to be violating this
  1505.          requirement by encoding addresses using other forms, such
  1506.          as DW_FORM_data4 for example.  For those broken compilers,
  1507.          we try to do our best, without any guarantee of success,
  1508.          to interpret the address correctly.  It would also be nice
  1509.          to generate a complaint, but that would require us to maintain
  1510.          a list of legitimate cases where a non-address form is allowed,
  1511.          as well as update callers to pass in at least the CU's DWARF
  1512.          version.  This is more overhead than what we're willing to
  1513.          expand for a pretty rare case.  */
  1514.       addr = DW_UNSND (attr);
  1515.     }
  1516.   else
  1517.     addr = DW_ADDR (attr);

  1518.   return addr;
  1519. }

  1520. /* The suffix for an index file.  */
  1521. #define INDEX_SUFFIX ".gdb-index"

  1522. /* Try to locate the sections we need for DWARF 2 debugging
  1523.    information and return true if we have enough to do something.
  1524.    NAMES points to the dwarf2 section names, or is NULL if the standard
  1525.    ELF names are used.  */

  1526. int
  1527. dwarf2_has_info (struct objfile *objfile,
  1528.                  const struct dwarf2_debug_sections *names)
  1529. {
  1530.   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
  1531.   if (!dwarf2_per_objfile)
  1532.     {
  1533.       /* Initialize per-objfile state.  */
  1534.       struct dwarf2_per_objfile *data
  1535.         = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));

  1536.       memset (data, 0, sizeof (*data));
  1537.       set_objfile_data (objfile, dwarf2_objfile_data_key, data);
  1538.       dwarf2_per_objfile = data;

  1539.       bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
  1540.                              (void *) names);
  1541.       dwarf2_per_objfile->objfile = objfile;
  1542.     }
  1543.   return (!dwarf2_per_objfile->info.is_virtual
  1544.           && dwarf2_per_objfile->info.s.asection != NULL
  1545.           && !dwarf2_per_objfile->abbrev.is_virtual
  1546.           && dwarf2_per_objfile->abbrev.s.asection != NULL);
  1547. }

  1548. /* Return the containing section of virtual section SECTION.  */

  1549. static struct dwarf2_section_info *
  1550. get_containing_section (const struct dwarf2_section_info *section)
  1551. {
  1552.   gdb_assert (section->is_virtual);
  1553.   return section->s.containing_section;
  1554. }

  1555. /* Return the bfd owner of SECTION.  */

  1556. static struct bfd *
  1557. get_section_bfd_owner (const struct dwarf2_section_info *section)
  1558. {
  1559.   if (section->is_virtual)
  1560.     {
  1561.       section = get_containing_section (section);
  1562.       gdb_assert (!section->is_virtual);
  1563.     }
  1564.   return section->s.asection->owner;
  1565. }

  1566. /* Return the bfd section of SECTION.
  1567.    Returns NULL if the section is not present.  */

  1568. static asection *
  1569. get_section_bfd_section (const struct dwarf2_section_info *section)
  1570. {
  1571.   if (section->is_virtual)
  1572.     {
  1573.       section = get_containing_section (section);
  1574.       gdb_assert (!section->is_virtual);
  1575.     }
  1576.   return section->s.asection;
  1577. }

  1578. /* Return the name of SECTION.  */

  1579. static const char *
  1580. get_section_name (const struct dwarf2_section_info *section)
  1581. {
  1582.   asection *sectp = get_section_bfd_section (section);

  1583.   gdb_assert (sectp != NULL);
  1584.   return bfd_section_name (get_section_bfd_owner (section), sectp);
  1585. }

  1586. /* Return the name of the file SECTION is in.  */

  1587. static const char *
  1588. get_section_file_name (const struct dwarf2_section_info *section)
  1589. {
  1590.   bfd *abfd = get_section_bfd_owner (section);

  1591.   return bfd_get_filename (abfd);
  1592. }

  1593. /* Return the id of SECTION.
  1594.    Returns 0 if SECTION doesn't exist.  */

  1595. static int
  1596. get_section_id (const struct dwarf2_section_info *section)
  1597. {
  1598.   asection *sectp = get_section_bfd_section (section);

  1599.   if (sectp == NULL)
  1600.     return 0;
  1601.   return sectp->id;
  1602. }

  1603. /* Return the flags of SECTION.
  1604.    SECTION (or containing section if this is a virtual section) must exist.  */

  1605. static int
  1606. get_section_flags (const struct dwarf2_section_info *section)
  1607. {
  1608.   asection *sectp = get_section_bfd_section (section);

  1609.   gdb_assert (sectp != NULL);
  1610.   return bfd_get_section_flags (sectp->owner, sectp);
  1611. }

  1612. /* When loading sections, we look either for uncompressed section or for
  1613.    compressed section names.  */

  1614. static int
  1615. section_is_p (const char *section_name,
  1616.               const struct dwarf2_section_names *names)
  1617. {
  1618.   if (names->normal != NULL
  1619.       && strcmp (section_name, names->normal) == 0)
  1620.     return 1;
  1621.   if (names->compressed != NULL
  1622.       && strcmp (section_name, names->compressed) == 0)
  1623.     return 1;
  1624.   return 0;
  1625. }

  1626. /* This function is mapped across the sections and remembers the
  1627.    offset and size of each of the debugging sections we are interested
  1628.    in.  */

  1629. static void
  1630. dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
  1631. {
  1632.   const struct dwarf2_debug_sections *names;
  1633.   flagword aflag = bfd_get_section_flags (abfd, sectp);

  1634.   if (vnames == NULL)
  1635.     names = &dwarf2_elf_names;
  1636.   else
  1637.     names = (const struct dwarf2_debug_sections *) vnames;

  1638.   if ((aflag & SEC_HAS_CONTENTS) == 0)
  1639.     {
  1640.     }
  1641.   else if (section_is_p (sectp->name, &names->info))
  1642.     {
  1643.       dwarf2_per_objfile->info.s.asection = sectp;
  1644.       dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
  1645.     }
  1646.   else if (section_is_p (sectp->name, &names->abbrev))
  1647.     {
  1648.       dwarf2_per_objfile->abbrev.s.asection = sectp;
  1649.       dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
  1650.     }
  1651.   else if (section_is_p (sectp->name, &names->line))
  1652.     {
  1653.       dwarf2_per_objfile->line.s.asection = sectp;
  1654.       dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
  1655.     }
  1656.   else if (section_is_p (sectp->name, &names->loc))
  1657.     {
  1658.       dwarf2_per_objfile->loc.s.asection = sectp;
  1659.       dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
  1660.     }
  1661.   else if (section_is_p (sectp->name, &names->macinfo))
  1662.     {
  1663.       dwarf2_per_objfile->macinfo.s.asection = sectp;
  1664.       dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
  1665.     }
  1666.   else if (section_is_p (sectp->name, &names->macro))
  1667.     {
  1668.       dwarf2_per_objfile->macro.s.asection = sectp;
  1669.       dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
  1670.     }
  1671.   else if (section_is_p (sectp->name, &names->str))
  1672.     {
  1673.       dwarf2_per_objfile->str.s.asection = sectp;
  1674.       dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
  1675.     }
  1676.   else if (section_is_p (sectp->name, &names->addr))
  1677.     {
  1678.       dwarf2_per_objfile->addr.s.asection = sectp;
  1679.       dwarf2_per_objfile->addr.size = bfd_get_section_size (sectp);
  1680.     }
  1681.   else if (section_is_p (sectp->name, &names->frame))
  1682.     {
  1683.       dwarf2_per_objfile->frame.s.asection = sectp;
  1684.       dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
  1685.     }
  1686.   else if (section_is_p (sectp->name, &names->eh_frame))
  1687.     {
  1688.       dwarf2_per_objfile->eh_frame.s.asection = sectp;
  1689.       dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
  1690.     }
  1691.   else if (section_is_p (sectp->name, &names->ranges))
  1692.     {
  1693.       dwarf2_per_objfile->ranges.s.asection = sectp;
  1694.       dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
  1695.     }
  1696.   else if (section_is_p (sectp->name, &names->types))
  1697.     {
  1698.       struct dwarf2_section_info type_section;

  1699.       memset (&type_section, 0, sizeof (type_section));
  1700.       type_section.s.asection = sectp;
  1701.       type_section.size = bfd_get_section_size (sectp);

  1702.       VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
  1703.                      &type_section);
  1704.     }
  1705.   else if (section_is_p (sectp->name, &names->gdb_index))
  1706.     {
  1707.       dwarf2_per_objfile->gdb_index.s.asection = sectp;
  1708.       dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
  1709.     }

  1710.   if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
  1711.       && bfd_section_vma (abfd, sectp) == 0)
  1712.     dwarf2_per_objfile->has_section_at_zero = 1;
  1713. }

  1714. /* A helper function that decides whether a section is empty,
  1715.    or not present.  */

  1716. static int
  1717. dwarf2_section_empty_p (const struct dwarf2_section_info *section)
  1718. {
  1719.   if (section->is_virtual)
  1720.     return section->size == 0;
  1721.   return section->s.asection == NULL || section->size == 0;
  1722. }

  1723. /* Read the contents of the section INFO.
  1724.    OBJFILE is the main object file, but not necessarily the file where
  1725.    the section comes fromE.g., for DWO files the bfd of INFO is the bfd
  1726.    of the DWO file.
  1727.    If the section is compressed, uncompress it before returning.  */

  1728. static void
  1729. dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
  1730. {
  1731.   asection *sectp;
  1732.   bfd *abfd;
  1733.   gdb_byte *buf, *retbuf;

  1734.   if (info->readin)
  1735.     return;
  1736.   info->buffer = NULL;
  1737.   info->readin = 1;

  1738.   if (dwarf2_section_empty_p (info))
  1739.     return;

  1740.   sectp = get_section_bfd_section (info);

  1741.   /* If this is a virtual section we need to read in the real one first.  */
  1742.   if (info->is_virtual)
  1743.     {
  1744.       struct dwarf2_section_info *containing_section =
  1745.         get_containing_section (info);

  1746.       gdb_assert (sectp != NULL);
  1747.       if ((sectp->flags & SEC_RELOC) != 0)
  1748.         {
  1749.           error (_("Dwarf Error: DWP format V2 with relocations is not"
  1750.                    " supported in section %s [in module %s]"),
  1751.                  get_section_name (info), get_section_file_name (info));
  1752.         }
  1753.       dwarf2_read_section (objfile, containing_section);
  1754.       /* Other code should have already caught virtual sections that don't
  1755.          fit.  */
  1756.       gdb_assert (info->virtual_offset + info->size
  1757.                   <= containing_section->size);
  1758.       /* If the real section is empty or there was a problem reading the
  1759.          section we shouldn't get here.  */
  1760.       gdb_assert (containing_section->buffer != NULL);
  1761.       info->buffer = containing_section->buffer + info->virtual_offset;
  1762.       return;
  1763.     }

  1764.   /* If the section has relocations, we must read it ourselves.
  1765.      Otherwise we attach it to the BFD.  */
  1766.   if ((sectp->flags & SEC_RELOC) == 0)
  1767.     {
  1768.       info->buffer = gdb_bfd_map_section (sectp, &info->size);
  1769.       return;
  1770.     }

  1771.   buf = obstack_alloc (&objfile->objfile_obstack, info->size);
  1772.   info->buffer = buf;

  1773.   /* When debugging .o files, we may need to apply relocations; see
  1774.      http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
  1775.      We never compress sections in .o files, so we only need to
  1776.      try this when the section is not compressed.  */
  1777.   retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
  1778.   if (retbuf != NULL)
  1779.     {
  1780.       info->buffer = retbuf;
  1781.       return;
  1782.     }

  1783.   abfd = get_section_bfd_owner (info);
  1784.   gdb_assert (abfd != NULL);

  1785.   if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
  1786.       || bfd_bread (buf, info->size, abfd) != info->size)
  1787.     {
  1788.       error (_("Dwarf Error: Can't read DWARF data"
  1789.                " in section %s [in module %s]"),
  1790.              bfd_section_name (abfd, sectp), bfd_get_filename (abfd));
  1791.     }
  1792. }

  1793. /* A helper function that returns the size of a section in a safe way.
  1794.    If you are positive that the section has been read before using the
  1795.    size, then it is safe to refer to the dwarf2_section_info object's
  1796.    "size" field directly.  In other cases, you must call this
  1797.    function, because for compressed sections the size field is not set
  1798.    correctly until the section has been read.  */

  1799. static bfd_size_type
  1800. dwarf2_section_size (struct objfile *objfile,
  1801.                      struct dwarf2_section_info *info)
  1802. {
  1803.   if (!info->readin)
  1804.     dwarf2_read_section (objfile, info);
  1805.   return info->size;
  1806. }

  1807. /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
  1808.    SECTION_NAME.  */

  1809. void
  1810. dwarf2_get_section_info (struct objfile *objfile,
  1811.                          enum dwarf2_section_enum sect,
  1812.                          asection **sectp, const gdb_byte **bufp,
  1813.                          bfd_size_type *sizep)
  1814. {
  1815.   struct dwarf2_per_objfile *data
  1816.     = objfile_data (objfile, dwarf2_objfile_data_key);
  1817.   struct dwarf2_section_info *info;

  1818.   /* We may see an objfile without any DWARF, in which case we just
  1819.      return nothing.  */
  1820.   if (data == NULL)
  1821.     {
  1822.       *sectp = NULL;
  1823.       *bufp = NULL;
  1824.       *sizep = 0;
  1825.       return;
  1826.     }
  1827.   switch (sect)
  1828.     {
  1829.     case DWARF2_DEBUG_FRAME:
  1830.       info = &data->frame;
  1831.       break;
  1832.     case DWARF2_EH_FRAME:
  1833.       info = &data->eh_frame;
  1834.       break;
  1835.     default:
  1836.       gdb_assert_not_reached ("unexpected section");
  1837.     }

  1838.   dwarf2_read_section (objfile, info);

  1839.   *sectp = get_section_bfd_section (info);
  1840.   *bufp = info->buffer;
  1841.   *sizep = info->size;
  1842. }

  1843. /* A helper function to find the sections for a .dwz file.  */

  1844. static void
  1845. locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
  1846. {
  1847.   struct dwz_file *dwz_file = arg;

  1848.   /* Note that we only support the standard ELF names, because .dwz
  1849.      is ELF-only (at the time of writing).  */
  1850.   if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
  1851.     {
  1852.       dwz_file->abbrev.s.asection = sectp;
  1853.       dwz_file->abbrev.size = bfd_get_section_size (sectp);
  1854.     }
  1855.   else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
  1856.     {
  1857.       dwz_file->info.s.asection = sectp;
  1858.       dwz_file->info.size = bfd_get_section_size (sectp);
  1859.     }
  1860.   else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
  1861.     {
  1862.       dwz_file->str.s.asection = sectp;
  1863.       dwz_file->str.size = bfd_get_section_size (sectp);
  1864.     }
  1865.   else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
  1866.     {
  1867.       dwz_file->line.s.asection = sectp;
  1868.       dwz_file->line.size = bfd_get_section_size (sectp);
  1869.     }
  1870.   else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
  1871.     {
  1872.       dwz_file->macro.s.asection = sectp;
  1873.       dwz_file->macro.size = bfd_get_section_size (sectp);
  1874.     }
  1875.   else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
  1876.     {
  1877.       dwz_file->gdb_index.s.asection = sectp;
  1878.       dwz_file->gdb_index.size = bfd_get_section_size (sectp);
  1879.     }
  1880. }

  1881. /* Open the separate '.dwz' debug file, if needed.  Return NULL if
  1882.    there is no .gnu_debugaltlink section in the file.  Error if there
  1883.    is such a section but the file cannot be found.  */

  1884. static struct dwz_file *
  1885. dwarf2_get_dwz_file (void)
  1886. {
  1887.   bfd *dwz_bfd;
  1888.   char *data;
  1889.   struct cleanup *cleanup;
  1890.   const char *filename;
  1891.   struct dwz_file *result;
  1892.   bfd_size_type buildid_len_arg;
  1893.   size_t buildid_len;
  1894.   bfd_byte *buildid;

  1895.   if (dwarf2_per_objfile->dwz_file != NULL)
  1896.     return dwarf2_per_objfile->dwz_file;

  1897.   bfd_set_error (bfd_error_no_error);
  1898.   data = bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
  1899.                                       &buildid_len_arg, &buildid);
  1900.   if (data == NULL)
  1901.     {
  1902.       if (bfd_get_error () == bfd_error_no_error)
  1903.         return NULL;
  1904.       error (_("could not read '.gnu_debugaltlink' section: %s"),
  1905.              bfd_errmsg (bfd_get_error ()));
  1906.     }
  1907.   cleanup = make_cleanup (xfree, data);
  1908.   make_cleanup (xfree, buildid);

  1909.   buildid_len = (size_t) buildid_len_arg;

  1910.   filename = (const char *) data;
  1911.   if (!IS_ABSOLUTE_PATH (filename))
  1912.     {
  1913.       char *abs = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
  1914.       char *rel;

  1915.       make_cleanup (xfree, abs);
  1916.       abs = ldirname (abs);
  1917.       make_cleanup (xfree, abs);

  1918.       rel = concat (abs, SLASH_STRING, filename, (char *) NULL);
  1919.       make_cleanup (xfree, rel);
  1920.       filename = rel;
  1921.     }

  1922.   /* First try the file name given in the section.  If that doesn't
  1923.      work, try to use the build-id instead.  */
  1924.   dwz_bfd = gdb_bfd_open (filename, gnutarget, -1);
  1925.   if (dwz_bfd != NULL)
  1926.     {
  1927.       if (!build_id_verify (dwz_bfd, buildid_len, buildid))
  1928.         {
  1929.           gdb_bfd_unref (dwz_bfd);
  1930.           dwz_bfd = NULL;
  1931.         }
  1932.     }

  1933.   if (dwz_bfd == NULL)
  1934.     dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);

  1935.   if (dwz_bfd == NULL)
  1936.     error (_("could not find '.gnu_debugaltlink' file for %s"),
  1937.            objfile_name (dwarf2_per_objfile->objfile));

  1938.   result = OBSTACK_ZALLOC (&dwarf2_per_objfile->objfile->objfile_obstack,
  1939.                            struct dwz_file);
  1940.   result->dwz_bfd = dwz_bfd;

  1941.   bfd_map_over_sections (dwz_bfd, locate_dwz_sections, result);

  1942.   do_cleanups (cleanup);

  1943.   gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, dwz_bfd);
  1944.   dwarf2_per_objfile->dwz_file = result;
  1945.   return result;
  1946. }

  1947. /* DWARF quick_symbols_functions support.  */

  1948. /* TUs can share .debug_line entries, and there can be a lot more TUs than
  1949.    unique line tables, so we maintain a separate table of all .debug_line
  1950.    derived entries to support the sharing.
  1951.    All the quick functions need is the list of file names.  We discard the
  1952.    line_header when we're done and don't need to record it here.  */
  1953. struct quick_file_names
  1954. {
  1955.   /* The data used to construct the hash key.  */
  1956.   struct stmt_list_hash hash;

  1957.   /* The number of entries in file_names, real_names.  */
  1958.   unsigned int num_file_names;

  1959.   /* The file names from the line table, after being run through
  1960.      file_full_name.  */
  1961.   const char **file_names;

  1962.   /* The file names from the line table after being run through
  1963.      gdb_realpath.  These are computed lazily.  */
  1964.   const char **real_names;
  1965. };

  1966. /* When using the index (and thus not using psymtabs), each CU has an
  1967.    object of this type.  This is used to hold information needed by
  1968.    the various "quick" methods.  */
  1969. struct dwarf2_per_cu_quick_data
  1970. {
  1971.   /* The file table.  This can be NULL if there was no file table
  1972.      or it's currently not read in.
  1973.      NOTE: This points into dwarf2_per_objfile->quick_file_names_table.  */
  1974.   struct quick_file_names *file_names;

  1975.   /* The corresponding symbol table.  This is NULL if symbols for this
  1976.      CU have not yet been read.  */
  1977.   struct compunit_symtab *compunit_symtab;

  1978.   /* A temporary mark bit used when iterating over all CUs in
  1979.      expand_symtabs_matching.  */
  1980.   unsigned int mark : 1;

  1981.   /* True if we've tried to read the file table and found there isn't one.
  1982.      There will be no point in trying to read it again next time.  */
  1983.   unsigned int no_file_data : 1;
  1984. };

  1985. /* Utility hash function for a stmt_list_hash.  */

  1986. static hashval_t
  1987. hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
  1988. {
  1989.   hashval_t v = 0;

  1990.   if (stmt_list_hash->dwo_unit != NULL)
  1991.     v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
  1992.   v += stmt_list_hash->line_offset.sect_off;
  1993.   return v;
  1994. }

  1995. /* Utility equality function for a stmt_list_hash.  */

  1996. static int
  1997. eq_stmt_list_entry (const struct stmt_list_hash *lhs,
  1998.                     const struct stmt_list_hash *rhs)
  1999. {
  2000.   if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
  2001.     return 0;
  2002.   if (lhs->dwo_unit != NULL
  2003.       && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
  2004.     return 0;

  2005.   return lhs->line_offset.sect_off == rhs->line_offset.sect_off;
  2006. }

  2007. /* Hash function for a quick_file_names.  */

  2008. static hashval_t
  2009. hash_file_name_entry (const void *e)
  2010. {
  2011.   const struct quick_file_names *file_data = e;

  2012.   return hash_stmt_list_entry (&file_data->hash);
  2013. }

  2014. /* Equality function for a quick_file_names.  */

  2015. static int
  2016. eq_file_name_entry (const void *a, const void *b)
  2017. {
  2018.   const struct quick_file_names *ea = a;
  2019.   const struct quick_file_names *eb = b;

  2020.   return eq_stmt_list_entry (&ea->hash, &eb->hash);
  2021. }

  2022. /* Delete function for a quick_file_names.  */

  2023. static void
  2024. delete_file_name_entry (void *e)
  2025. {
  2026.   struct quick_file_names *file_data = e;
  2027.   int i;

  2028.   for (i = 0; i < file_data->num_file_names; ++i)
  2029.     {
  2030.       xfree ((void*) file_data->file_names[i]);
  2031.       if (file_data->real_names)
  2032.         xfree ((void*) file_data->real_names[i]);
  2033.     }

  2034.   /* The space for the struct itself lives on objfile_obstack,
  2035.      so we don't free it here.  */
  2036. }

  2037. /* Create a quick_file_names hash table.  */

  2038. static htab_t
  2039. create_quick_file_names_table (unsigned int nr_initial_entries)
  2040. {
  2041.   return htab_create_alloc (nr_initial_entries,
  2042.                             hash_file_name_entry, eq_file_name_entry,
  2043.                             delete_file_name_entry, xcalloc, xfree);
  2044. }

  2045. /* Read in PER_CU->CU.  This function is unrelated to symtabs, symtab would
  2046.    have to be created afterwards.  You should call age_cached_comp_units after
  2047.    processing PER_CU->CU.  dw2_setup must have been already called.  */

  2048. static void
  2049. load_cu (struct dwarf2_per_cu_data *per_cu)
  2050. {
  2051.   if (per_cu->is_debug_types)
  2052.     load_full_type_unit (per_cu);
  2053.   else
  2054.     load_full_comp_unit (per_cu, language_minimal);

  2055.   gdb_assert (per_cu->cu != NULL);

  2056.   dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
  2057. }

  2058. /* Read in the symbols for PER_CU.  */

  2059. static void
  2060. dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
  2061. {
  2062.   struct cleanup *back_to;

  2063.   /* Skip type_unit_groups, reading the type units they contain
  2064.      is handled elsewhere.  */
  2065.   if (IS_TYPE_UNIT_GROUP (per_cu))
  2066.     return;

  2067.   back_to = make_cleanup (dwarf2_release_queue, NULL);

  2068.   if (dwarf2_per_objfile->using_index
  2069.       ? per_cu->v.quick->compunit_symtab == NULL
  2070.       : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
  2071.     {
  2072.       queue_comp_unit (per_cu, language_minimal);
  2073.       load_cu (per_cu);

  2074.       /* If we just loaded a CU from a DWO, and we're working with an index
  2075.          that may badly handle TUs, load all the TUs in that DWO as well.
  2076.          http://sourceware.org/bugzilla/show_bug.cgi?id=15021  */
  2077.       if (!per_cu->is_debug_types
  2078.           && per_cu->cu->dwo_unit != NULL
  2079.           && dwarf2_per_objfile->index_table != NULL
  2080.           && dwarf2_per_objfile->index_table->version <= 7
  2081.           /* DWP files aren't supported yet.  */
  2082.           && get_dwp_file () == NULL)
  2083.         queue_and_load_all_dwo_tus (per_cu);
  2084.     }

  2085.   process_queue ();

  2086.   /* Age the cache, releasing compilation units that have not
  2087.      been used recently.  */
  2088.   age_cached_comp_units ();

  2089.   do_cleanups (back_to);
  2090. }

  2091. /* Ensure that the symbols for PER_CU have been read in.  OBJFILE is
  2092.    the objfile from which this CU came.  Returns the resulting symbol
  2093.    table.  */

  2094. static struct compunit_symtab *
  2095. dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
  2096. {
  2097.   gdb_assert (dwarf2_per_objfile->using_index);
  2098.   if (!per_cu->v.quick->compunit_symtab)
  2099.     {
  2100.       struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
  2101.       increment_reading_symtab ();
  2102.       dw2_do_instantiate_symtab (per_cu);
  2103.       process_cu_includes ();
  2104.       do_cleanups (back_to);
  2105.     }

  2106.   return per_cu->v.quick->compunit_symtab;
  2107. }

  2108. /* Return the CU/TU given its index.

  2109.    This is intended for loops like:

  2110.    for (i = 0; i < (dwarf2_per_objfile->n_comp_units
  2111.                     + dwarf2_per_objfile->n_type_units); ++i)
  2112.      {
  2113.        struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);

  2114.        ...;
  2115.      }
  2116. */

  2117. static struct dwarf2_per_cu_data *
  2118. dw2_get_cutu (int index)
  2119. {
  2120.   if (index >= dwarf2_per_objfile->n_comp_units)
  2121.     {
  2122.       index -= dwarf2_per_objfile->n_comp_units;
  2123.       gdb_assert (index < dwarf2_per_objfile->n_type_units);
  2124.       return &dwarf2_per_objfile->all_type_units[index]->per_cu;
  2125.     }

  2126.   return dwarf2_per_objfile->all_comp_units[index];
  2127. }

  2128. /* Return the CU given its index.
  2129.    This differs from dw2_get_cutu in that it's for when you know INDEX
  2130.    refers to a CU.  */

  2131. static struct dwarf2_per_cu_data *
  2132. dw2_get_cu (int index)
  2133. {
  2134.   gdb_assert (index >= 0 && index < dwarf2_per_objfile->n_comp_units);

  2135.   return dwarf2_per_objfile->all_comp_units[index];
  2136. }

  2137. /* A helper for create_cus_from_index that handles a given list of
  2138.    CUs.  */

  2139. static void
  2140. create_cus_from_index_list (struct objfile *objfile,
  2141.                             const gdb_byte *cu_list, offset_type n_elements,
  2142.                             struct dwarf2_section_info *section,
  2143.                             int is_dwz,
  2144.                             int base_offset)
  2145. {
  2146.   offset_type i;

  2147.   for (i = 0; i < n_elements; i += 2)
  2148.     {
  2149.       struct dwarf2_per_cu_data *the_cu;
  2150.       ULONGEST offset, length;

  2151.       gdb_static_assert (sizeof (ULONGEST) >= 8);
  2152.       offset = extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
  2153.       length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
  2154.       cu_list += 2 * 8;

  2155.       the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  2156.                                struct dwarf2_per_cu_data);
  2157.       the_cu->offset.sect_off = offset;
  2158.       the_cu->length = length;
  2159.       the_cu->objfile = objfile;
  2160.       the_cu->section = section;
  2161.       the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  2162.                                         struct dwarf2_per_cu_quick_data);
  2163.       the_cu->is_dwz = is_dwz;
  2164.       dwarf2_per_objfile->all_comp_units[base_offset + i / 2] = the_cu;
  2165.     }
  2166. }

  2167. /* Read the CU list from the mapped index, and use it to create all
  2168.    the CU objects for this objfile.  */

  2169. static void
  2170. create_cus_from_index (struct objfile *objfile,
  2171.                        const gdb_byte *cu_list, offset_type cu_list_elements,
  2172.                        const gdb_byte *dwz_list, offset_type dwz_elements)
  2173. {
  2174.   struct dwz_file *dwz;

  2175.   dwarf2_per_objfile->n_comp_units = (cu_list_elements + dwz_elements) / 2;
  2176.   dwarf2_per_objfile->all_comp_units
  2177.     = obstack_alloc (&objfile->objfile_obstack,
  2178.                      dwarf2_per_objfile->n_comp_units
  2179.                      * sizeof (struct dwarf2_per_cu_data *));

  2180.   create_cus_from_index_list (objfile, cu_list, cu_list_elements,
  2181.                               &dwarf2_per_objfile->info, 0, 0);

  2182.   if (dwz_elements == 0)
  2183.     return;

  2184.   dwz = dwarf2_get_dwz_file ();
  2185.   create_cus_from_index_list (objfile, dwz_list, dwz_elements, &dwz->info, 1,
  2186.                               cu_list_elements / 2);
  2187. }

  2188. /* Create the signatured type hash table from the index.  */

  2189. static void
  2190. create_signatured_type_table_from_index (struct objfile *objfile,
  2191.                                          struct dwarf2_section_info *section,
  2192.                                          const gdb_byte *bytes,
  2193.                                          offset_type elements)
  2194. {
  2195.   offset_type i;
  2196.   htab_t sig_types_hash;

  2197.   dwarf2_per_objfile->n_type_units
  2198.     = dwarf2_per_objfile->n_allocated_type_units
  2199.     = elements / 3;
  2200.   dwarf2_per_objfile->all_type_units
  2201.     = xmalloc (dwarf2_per_objfile->n_type_units
  2202.                * sizeof (struct signatured_type *));

  2203.   sig_types_hash = allocate_signatured_type_table (objfile);

  2204.   for (i = 0; i < elements; i += 3)
  2205.     {
  2206.       struct signatured_type *sig_type;
  2207.       ULONGEST offset, type_offset_in_tu, signature;
  2208.       void **slot;

  2209.       gdb_static_assert (sizeof (ULONGEST) >= 8);
  2210.       offset = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
  2211.       type_offset_in_tu = extract_unsigned_integer (bytes + 8, 8,
  2212.                                                     BFD_ENDIAN_LITTLE);
  2213.       signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
  2214.       bytes += 3 * 8;

  2215.       sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  2216.                                  struct signatured_type);
  2217.       sig_type->signature = signature;
  2218.       sig_type->type_offset_in_tu.cu_off = type_offset_in_tu;
  2219.       sig_type->per_cu.is_debug_types = 1;
  2220.       sig_type->per_cu.section = section;
  2221.       sig_type->per_cu.offset.sect_off = offset;
  2222.       sig_type->per_cu.objfile = objfile;
  2223.       sig_type->per_cu.v.quick
  2224.         = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  2225.                           struct dwarf2_per_cu_quick_data);

  2226.       slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
  2227.       *slot = sig_type;

  2228.       dwarf2_per_objfile->all_type_units[i / 3] = sig_type;
  2229.     }

  2230.   dwarf2_per_objfile->signatured_types = sig_types_hash;
  2231. }

  2232. /* Read the address map data from the mapped index, and use it to
  2233.    populate the objfile's psymtabs_addrmap.  */

  2234. static void
  2235. create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
  2236. {
  2237.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  2238.   const gdb_byte *iter, *end;
  2239.   struct obstack temp_obstack;
  2240.   struct addrmap *mutable_map;
  2241.   struct cleanup *cleanup;
  2242.   CORE_ADDR baseaddr;

  2243.   obstack_init (&temp_obstack);
  2244.   cleanup = make_cleanup_obstack_free (&temp_obstack);
  2245.   mutable_map = addrmap_create_mutable (&temp_obstack);

  2246.   iter = index->address_table;
  2247.   end = iter + index->address_table_size;

  2248.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  2249.   while (iter < end)
  2250.     {
  2251.       ULONGEST hi, lo, cu_index;
  2252.       lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
  2253.       iter += 8;
  2254.       hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
  2255.       iter += 8;
  2256.       cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
  2257.       iter += 4;

  2258.       if (lo > hi)
  2259.         {
  2260.           complaint (&symfile_complaints,
  2261.                      _(".gdb_index address table has invalid range (%s - %s)"),
  2262.                      hex_string (lo), hex_string (hi));
  2263.           continue;
  2264.         }

  2265.       if (cu_index >= dwarf2_per_objfile->n_comp_units)
  2266.         {
  2267.           complaint (&symfile_complaints,
  2268.                      _(".gdb_index address table has invalid CU number %u"),
  2269.                      (unsigned) cu_index);
  2270.           continue;
  2271.         }

  2272.       lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr);
  2273.       hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr);
  2274.       addrmap_set_empty (mutable_map, lo, hi - 1, dw2_get_cutu (cu_index));
  2275.     }

  2276.   objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
  2277.                                                     &objfile->objfile_obstack);
  2278.   do_cleanups (cleanup);
  2279. }

  2280. /* The hash function for strings in the mapped index.  This is the same as
  2281.    SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
  2282.    implementation.  This is necessary because the hash function is tied to the
  2283.    format of the mapped index file.  The hash values do not have to match with
  2284.    SYMBOL_HASH_NEXT.

  2285.    Use INT_MAX for INDEX_VERSION if you generate the current index format.  */

  2286. static hashval_t
  2287. mapped_index_string_hash (int index_version, const void *p)
  2288. {
  2289.   const unsigned char *str = (const unsigned char *) p;
  2290.   hashval_t r = 0;
  2291.   unsigned char c;

  2292.   while ((c = *str++) != 0)
  2293.     {
  2294.       if (index_version >= 5)
  2295.         c = tolower (c);
  2296.       r = r * 67 + c - 113;
  2297.     }

  2298.   return r;
  2299. }

  2300. /* Find a slot in the mapped index INDEX for the object named NAME.
  2301.    If NAME is found, set *VEC_OUT to point to the CU vector in the
  2302.    constant pool and return 1.  If NAME cannot be found, return 0.  */

  2303. static int
  2304. find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
  2305.                           offset_type **vec_out)
  2306. {
  2307.   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
  2308.   offset_type hash;
  2309.   offset_type slot, step;
  2310.   int (*cmp) (const char *, const char *);

  2311.   if (current_language->la_language == language_cplus
  2312.       || current_language->la_language == language_java
  2313.       || current_language->la_language == language_fortran)
  2314.     {
  2315.       /* NAME is already canonical.  Drop any qualifiers as .gdb_index does
  2316.          not contain any.  */

  2317.       if (strchr (name, '(') != NULL)
  2318.         {
  2319.           char *without_params = cp_remove_params (name);

  2320.           if (without_params != NULL)
  2321.             {
  2322.               make_cleanup (xfree, without_params);
  2323.               name = without_params;
  2324.             }
  2325.         }
  2326.     }

  2327.   /* Index version 4 did not support case insensitive searches.  But the
  2328.      indices for case insensitive languages are built in lowercase, therefore
  2329.      simulate our NAME being searched is also lowercased.  */
  2330.   hash = mapped_index_string_hash ((index->version == 4
  2331.                                     && case_sensitivity == case_sensitive_off
  2332.                                     ? 5 : index->version),
  2333.                                    name);

  2334.   slot = hash & (index->symbol_table_slots - 1);
  2335.   step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
  2336.   cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);

  2337.   for (;;)
  2338.     {
  2339.       /* Convert a slot number to an offset into the table.  */
  2340.       offset_type i = 2 * slot;
  2341.       const char *str;
  2342.       if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
  2343.         {
  2344.           do_cleanups (back_to);
  2345.           return 0;
  2346.         }

  2347.       str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
  2348.       if (!cmp (name, str))
  2349.         {
  2350.           *vec_out = (offset_type *) (index->constant_pool
  2351.                                       + MAYBE_SWAP (index->symbol_table[i + 1]));
  2352.           do_cleanups (back_to);
  2353.           return 1;
  2354.         }

  2355.       slot = (slot + step) & (index->symbol_table_slots - 1);
  2356.     }
  2357. }

  2358. /* A helper function that reads the .gdb_index from SECTION and fills
  2359.    in MAP.  FILENAME is the name of the file containing the section;
  2360.    it is used for error reporting.  DEPRECATED_OK is nonzero if it is
  2361.    ok to use deprecated sections.

  2362.    CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
  2363.    out parameters that are filled in with information about the CU and
  2364.    TU lists in the section.

  2365.    Returns 1 if all went well, 0 otherwise.  */

  2366. static int
  2367. read_index_from_section (struct objfile *objfile,
  2368.                          const char *filename,
  2369.                          int deprecated_ok,
  2370.                          struct dwarf2_section_info *section,
  2371.                          struct mapped_index *map,
  2372.                          const gdb_byte **cu_list,
  2373.                          offset_type *cu_list_elements,
  2374.                          const gdb_byte **types_list,
  2375.                          offset_type *types_list_elements)
  2376. {
  2377.   const gdb_byte *addr;
  2378.   offset_type version;
  2379.   offset_type *metadata;
  2380.   int i;

  2381.   if (dwarf2_section_empty_p (section))
  2382.     return 0;

  2383.   /* Older elfutils strip versions could keep the section in the main
  2384.      executable while splitting it for the separate debug info file.  */
  2385.   if ((get_section_flags (section) & SEC_HAS_CONTENTS) == 0)
  2386.     return 0;

  2387.   dwarf2_read_section (objfile, section);

  2388.   addr = section->buffer;
  2389.   /* Version check.  */
  2390.   version = MAYBE_SWAP (*(offset_type *) addr);
  2391.   /* Versions earlier than 3 emitted every copy of a psymbol.  This
  2392.      causes the index to behave very poorly for certain requests.  Version 3
  2393.      contained incomplete addrmap.  So, it seems better to just ignore such
  2394.      indices.  */
  2395.   if (version < 4)
  2396.     {
  2397.       static int warning_printed = 0;
  2398.       if (!warning_printed)
  2399.         {
  2400.           warning (_("Skipping obsolete .gdb_index section in %s."),
  2401.                    filename);
  2402.           warning_printed = 1;
  2403.         }
  2404.       return 0;
  2405.     }
  2406.   /* Index version 4 uses a different hash function than index version
  2407.      5 and later.

  2408.      Versions earlier than 6 did not emit psymbols for inlined
  2409.      functions.  Using these files will cause GDB not to be able to
  2410.      set breakpoints on inlined functions by name, so we ignore these
  2411.      indices unless the user has done
  2412.      "set use-deprecated-index-sections on".  */
  2413.   if (version < 6 && !deprecated_ok)
  2414.     {
  2415.       static int warning_printed = 0;
  2416.       if (!warning_printed)
  2417.         {
  2418.           warning (_("\
  2419. Skipping deprecated .gdb_index section in %s.\n\
  2420. Do \"set use-deprecated-index-sections on\" before the file is read\n\
  2421. to use the section anyway."),
  2422.                    filename);
  2423.           warning_printed = 1;
  2424.         }
  2425.       return 0;
  2426.     }
  2427.   /* Version 7 indices generated by gold refer to the CU for a symbol instead
  2428.      of the TU (for symbols coming from TUs),
  2429.      http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
  2430.      Plus gold-generated indices can have duplicate entries for global symbols,
  2431.      http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
  2432.      These are just performance bugs, and we can't distinguish gdb-generated
  2433.      indices from gold-generated ones, so issue no warning here.  */

  2434.   /* Indexes with higher version than the one supported by GDB may be no
  2435.      longer backward compatible.  */
  2436.   if (version > 8)
  2437.     return 0;

  2438.   map->version = version;
  2439.   map->total_size = section->size;

  2440.   metadata = (offset_type *) (addr + sizeof (offset_type));

  2441.   i = 0;
  2442.   *cu_list = addr + MAYBE_SWAP (metadata[i]);
  2443.   *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
  2444.                        / 8);
  2445.   ++i;

  2446.   *types_list = addr + MAYBE_SWAP (metadata[i]);
  2447.   *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
  2448.                            - MAYBE_SWAP (metadata[i]))
  2449.                           / 8);
  2450.   ++i;

  2451.   map->address_table = addr + MAYBE_SWAP (metadata[i]);
  2452.   map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
  2453.                              - MAYBE_SWAP (metadata[i]));
  2454.   ++i;

  2455.   map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
  2456.   map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
  2457.                               - MAYBE_SWAP (metadata[i]))
  2458.                              / (2 * sizeof (offset_type)));
  2459.   ++i;

  2460.   map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));

  2461.   return 1;
  2462. }


  2463. /* Read the index file.  If everything went ok, initialize the "quick"
  2464.    elements of all the CUs and return 1.  Otherwise, return 0.  */

  2465. static int
  2466. dwarf2_read_index (struct objfile *objfile)
  2467. {
  2468.   struct mapped_index local_map, *map;
  2469.   const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
  2470.   offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
  2471.   struct dwz_file *dwz;

  2472.   if (!read_index_from_section (objfile, objfile_name (objfile),
  2473.                                 use_deprecated_index_sections,
  2474.                                 &dwarf2_per_objfile->gdb_index, &local_map,
  2475.                                 &cu_list, &cu_list_elements,
  2476.                                 &types_list, &types_list_elements))
  2477.     return 0;

  2478.   /* Don't use the index if it's empty.  */
  2479.   if (local_map.symbol_table_slots == 0)
  2480.     return 0;

  2481.   /* If there is a .dwz file, read it so we can get its CU list as
  2482.      well.  */
  2483.   dwz = dwarf2_get_dwz_file ();
  2484.   if (dwz != NULL)
  2485.     {
  2486.       struct mapped_index dwz_map;
  2487.       const gdb_byte *dwz_types_ignore;
  2488.       offset_type dwz_types_elements_ignore;

  2489.       if (!read_index_from_section (objfile, bfd_get_filename (dwz->dwz_bfd),
  2490.                                     1,
  2491.                                     &dwz->gdb_index, &dwz_map,
  2492.                                     &dwz_list, &dwz_list_elements,
  2493.                                     &dwz_types_ignore,
  2494.                                     &dwz_types_elements_ignore))
  2495.         {
  2496.           warning (_("could not read '.gdb_index' section from %s; skipping"),
  2497.                    bfd_get_filename (dwz->dwz_bfd));
  2498.           return 0;
  2499.         }
  2500.     }

  2501.   create_cus_from_index (objfile, cu_list, cu_list_elements, dwz_list,
  2502.                          dwz_list_elements);

  2503.   if (types_list_elements)
  2504.     {
  2505.       struct dwarf2_section_info *section;

  2506.       /* We can only handle a single .debug_types when we have an
  2507.          index.  */
  2508.       if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
  2509.         return 0;

  2510.       section = VEC_index (dwarf2_section_info_def,
  2511.                            dwarf2_per_objfile->types, 0);

  2512.       create_signatured_type_table_from_index (objfile, section, types_list,
  2513.                                                types_list_elements);
  2514.     }

  2515.   create_addrmap_from_index (objfile, &local_map);

  2516.   map = obstack_alloc (&objfile->objfile_obstack, sizeof (struct mapped_index));
  2517.   *map = local_map;

  2518.   dwarf2_per_objfile->index_table = map;
  2519.   dwarf2_per_objfile->using_index = 1;
  2520.   dwarf2_per_objfile->quick_file_names_table =
  2521.     create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);

  2522.   return 1;
  2523. }

  2524. /* A helper for the "quick" functions which sets the global
  2525.    dwarf2_per_objfile according to OBJFILE.  */

  2526. static void
  2527. dw2_setup (struct objfile *objfile)
  2528. {
  2529.   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
  2530.   gdb_assert (dwarf2_per_objfile);
  2531. }

  2532. /* die_reader_func for dw2_get_file_names.  */

  2533. static void
  2534. dw2_get_file_names_reader (const struct die_reader_specs *reader,
  2535.                            const gdb_byte *info_ptr,
  2536.                            struct die_info *comp_unit_die,
  2537.                            int has_children,
  2538.                            void *data)
  2539. {
  2540.   struct dwarf2_cu *cu = reader->cu;
  2541.   struct dwarf2_per_cu_data *this_cu = cu->per_cu;
  2542.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  2543.   struct dwarf2_per_cu_data *lh_cu;
  2544.   struct line_header *lh;
  2545.   struct attribute *attr;
  2546.   int i;
  2547.   const char *name, *comp_dir;
  2548.   void **slot;
  2549.   struct quick_file_names *qfn;
  2550.   unsigned int line_offset;

  2551.   gdb_assert (! this_cu->is_debug_types);

  2552.   /* Our callers never want to match partial units -- instead they
  2553.      will match the enclosing full CU.  */
  2554.   if (comp_unit_die->tag == DW_TAG_partial_unit)
  2555.     {
  2556.       this_cu->v.quick->no_file_data = 1;
  2557.       return;
  2558.     }

  2559.   lh_cu = this_cu;
  2560.   lh = NULL;
  2561.   slot = NULL;
  2562.   line_offset = 0;

  2563.   attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
  2564.   if (attr)
  2565.     {
  2566.       struct quick_file_names find_entry;

  2567.       line_offset = DW_UNSND (attr);

  2568.       /* We may have already read in this line header (TU line header sharing).
  2569.          If we have we're done.  */
  2570.       find_entry.hash.dwo_unit = cu->dwo_unit;
  2571.       find_entry.hash.line_offset.sect_off = line_offset;
  2572.       slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
  2573.                              &find_entry, INSERT);
  2574.       if (*slot != NULL)
  2575.         {
  2576.           lh_cu->v.quick->file_names = *slot;
  2577.           return;
  2578.         }

  2579.       lh = dwarf_decode_line_header (line_offset, cu);
  2580.     }
  2581.   if (lh == NULL)
  2582.     {
  2583.       lh_cu->v.quick->no_file_data = 1;
  2584.       return;
  2585.     }

  2586.   qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
  2587.   qfn->hash.dwo_unit = cu->dwo_unit;
  2588.   qfn->hash.line_offset.sect_off = line_offset;
  2589.   gdb_assert (slot != NULL);
  2590.   *slot = qfn;

  2591.   find_file_and_directory (comp_unit_die, cu, &name, &comp_dir);

  2592.   qfn->num_file_names = lh->num_file_names;
  2593.   qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
  2594.                                    lh->num_file_names * sizeof (char *));
  2595.   for (i = 0; i < lh->num_file_names; ++i)
  2596.     qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
  2597.   qfn->real_names = NULL;

  2598.   free_line_header (lh);

  2599.   lh_cu->v.quick->file_names = qfn;
  2600. }

  2601. /* A helper for the "quick" functions which attempts to read the line
  2602.    table for THIS_CU.  */

  2603. static struct quick_file_names *
  2604. dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
  2605. {
  2606.   /* This should never be called for TUs.  */
  2607.   gdb_assert (! this_cu->is_debug_types);
  2608.   /* Nor type unit groups.  */
  2609.   gdb_assert (! IS_TYPE_UNIT_GROUP (this_cu));

  2610.   if (this_cu->v.quick->file_names != NULL)
  2611.     return this_cu->v.quick->file_names;
  2612.   /* If we know there is no line data, no point in looking again.  */
  2613.   if (this_cu->v.quick->no_file_data)
  2614.     return NULL;

  2615.   init_cutu_and_read_dies_simple (this_cu, dw2_get_file_names_reader, NULL);

  2616.   if (this_cu->v.quick->no_file_data)
  2617.     return NULL;
  2618.   return this_cu->v.quick->file_names;
  2619. }

  2620. /* A helper for the "quick" functions which computes and caches the
  2621.    real path for a given file name from the line table.  */

  2622. static const char *
  2623. dw2_get_real_path (struct objfile *objfile,
  2624.                    struct quick_file_names *qfn, int index)
  2625. {
  2626.   if (qfn->real_names == NULL)
  2627.     qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
  2628.                                       qfn->num_file_names, const char *);

  2629.   if (qfn->real_names[index] == NULL)
  2630.     qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);

  2631.   return qfn->real_names[index];
  2632. }

  2633. static struct symtab *
  2634. dw2_find_last_source_symtab (struct objfile *objfile)
  2635. {
  2636.   struct compunit_symtab *cust;
  2637.   int index;

  2638.   dw2_setup (objfile);
  2639.   index = dwarf2_per_objfile->n_comp_units - 1;
  2640.   cust = dw2_instantiate_symtab (dw2_get_cutu (index));
  2641.   if (cust == NULL)
  2642.     return NULL;
  2643.   return compunit_primary_filetab (cust);
  2644. }

  2645. /* Traversal function for dw2_forget_cached_source_info.  */

  2646. static int
  2647. dw2_free_cached_file_names (void **slot, void *info)
  2648. {
  2649.   struct quick_file_names *file_data = (struct quick_file_names *) *slot;

  2650.   if (file_data->real_names)
  2651.     {
  2652.       int i;

  2653.       for (i = 0; i < file_data->num_file_names; ++i)
  2654.         {
  2655.           xfree ((void*) file_data->real_names[i]);
  2656.           file_data->real_names[i] = NULL;
  2657.         }
  2658.     }

  2659.   return 1;
  2660. }

  2661. static void
  2662. dw2_forget_cached_source_info (struct objfile *objfile)
  2663. {
  2664.   dw2_setup (objfile);

  2665.   htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
  2666.                           dw2_free_cached_file_names, NULL);
  2667. }

  2668. /* Helper function for dw2_map_symtabs_matching_filename that expands
  2669.    the symtabs and calls the iterator.  */

  2670. static int
  2671. dw2_map_expand_apply (struct objfile *objfile,
  2672.                       struct dwarf2_per_cu_data *per_cu,
  2673.                       const char *name, const char *real_path,
  2674.                       int (*callback) (struct symtab *, void *),
  2675.                       void *data)
  2676. {
  2677.   struct compunit_symtab *last_made = objfile->compunit_symtabs;

  2678.   /* Don't visit already-expanded CUs.  */
  2679.   if (per_cu->v.quick->compunit_symtab)
  2680.     return 0;

  2681.   /* This may expand more than one symtab, and we want to iterate over
  2682.      all of them.  */
  2683.   dw2_instantiate_symtab (per_cu);

  2684.   return iterate_over_some_symtabs (name, real_path, callback, data,
  2685.                                     objfile->compunit_symtabs, last_made);
  2686. }

  2687. /* Implementation of the map_symtabs_matching_filename method.  */

  2688. static int
  2689. dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
  2690.                                    const char *real_path,
  2691.                                    int (*callback) (struct symtab *, void *),
  2692.                                    void *data)
  2693. {
  2694.   int i;
  2695.   const char *name_basename = lbasename (name);

  2696.   dw2_setup (objfile);

  2697.   /* The rule is CUs specify all the files, including those used by
  2698.      any TU, so there's no need to scan TUs here.  */

  2699.   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  2700.     {
  2701.       int j;
  2702.       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
  2703.       struct quick_file_names *file_data;

  2704.       /* We only need to look at symtabs not already expanded.  */
  2705.       if (per_cu->v.quick->compunit_symtab)
  2706.         continue;

  2707.       file_data = dw2_get_file_names (per_cu);
  2708.       if (file_data == NULL)
  2709.         continue;

  2710.       for (j = 0; j < file_data->num_file_names; ++j)
  2711.         {
  2712.           const char *this_name = file_data->file_names[j];
  2713.           const char *this_real_name;

  2714.           if (compare_filenames_for_search (this_name, name))
  2715.             {
  2716.               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
  2717.                                         callback, data))
  2718.                 return 1;
  2719.               continue;
  2720.             }

  2721.           /* Before we invoke realpath, which can get expensive when many
  2722.              files are involved, do a quick comparison of the basenames.  */
  2723.           if (! basenames_may_differ
  2724.               && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
  2725.             continue;

  2726.           this_real_name = dw2_get_real_path (objfile, file_data, j);
  2727.           if (compare_filenames_for_search (this_real_name, name))
  2728.             {
  2729.               if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
  2730.                                         callback, data))
  2731.                 return 1;
  2732.               continue;
  2733.             }

  2734.           if (real_path != NULL)
  2735.             {
  2736.               gdb_assert (IS_ABSOLUTE_PATH (real_path));
  2737.               gdb_assert (IS_ABSOLUTE_PATH (name));
  2738.               if (this_real_name != NULL
  2739.                   && FILENAME_CMP (real_path, this_real_name) == 0)
  2740.                 {
  2741.                   if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
  2742.                                             callback, data))
  2743.                     return 1;
  2744.                   continue;
  2745.                 }
  2746.             }
  2747.         }
  2748.     }

  2749.   return 0;
  2750. }

  2751. /* Struct used to manage iterating over all CUs looking for a symbol.  */

  2752. struct dw2_symtab_iterator
  2753. {
  2754.   /* The internalized form of .gdb_index.  */
  2755.   struct mapped_index *index;
  2756.   /* If non-zero, only look for symbols that match BLOCK_INDEX.  */
  2757.   int want_specific_block;
  2758.   /* One of GLOBAL_BLOCK or STATIC_BLOCK.
  2759.      Unused if !WANT_SPECIFIC_BLOCK.  */
  2760.   int block_index;
  2761.   /* The kind of symbol we're looking for.  */
  2762.   domain_enum domain;
  2763.   /* The list of CUs from the index entry of the symbol,
  2764.      or NULL if not found.  */
  2765.   offset_type *vec;
  2766.   /* The next element in VEC to look at.  */
  2767.   int next;
  2768.   /* The number of elements in VEC, or zero if there is no match.  */
  2769.   int length;
  2770.   /* Have we seen a global version of the symbol?
  2771.      If so we can ignore all further global instances.
  2772.      This is to work around gold/15646, inefficient gold-generated
  2773.      indices.  */
  2774.   int global_seen;
  2775. };

  2776. /* Initialize the index symtab iterator ITER.
  2777.    If WANT_SPECIFIC_BLOCK is non-zero, only look for symbols
  2778.    in block BLOCK_INDEX.  Otherwise BLOCK_INDEX is ignored.  */

  2779. static void
  2780. dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
  2781.                       struct mapped_index *index,
  2782.                       int want_specific_block,
  2783.                       int block_index,
  2784.                       domain_enum domain,
  2785.                       const char *name)
  2786. {
  2787.   iter->index = index;
  2788.   iter->want_specific_block = want_specific_block;
  2789.   iter->block_index = block_index;
  2790.   iter->domain = domain;
  2791.   iter->next = 0;
  2792.   iter->global_seen = 0;

  2793.   if (find_slot_in_mapped_hash (index, name, &iter->vec))
  2794.     iter->length = MAYBE_SWAP (*iter->vec);
  2795.   else
  2796.     {
  2797.       iter->vec = NULL;
  2798.       iter->length = 0;
  2799.     }
  2800. }

  2801. /* Return the next matching CU or NULL if there are no more.  */

  2802. static struct dwarf2_per_cu_data *
  2803. dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
  2804. {
  2805.   for ( ; iter->next < iter->length; ++iter->next)
  2806.     {
  2807.       offset_type cu_index_and_attrs =
  2808.         MAYBE_SWAP (iter->vec[iter->next + 1]);
  2809.       offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
  2810.       struct dwarf2_per_cu_data *per_cu;
  2811.       int want_static = iter->block_index != GLOBAL_BLOCK;
  2812.       /* This value is only valid for index versions >= 7.  */
  2813.       int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
  2814.       gdb_index_symbol_kind symbol_kind =
  2815.         GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
  2816.       /* Only check the symbol attributes if they're present.
  2817.          Indices prior to version 7 don't record them,
  2818.          and indices >= 7 may elide them for certain symbols
  2819.          (gold does this).  */
  2820.       int attrs_valid =
  2821.         (iter->index->version >= 7
  2822.          && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);

  2823.       /* Don't crash on bad data.  */
  2824.       if (cu_index >= (dwarf2_per_objfile->n_comp_units
  2825.                        + dwarf2_per_objfile->n_type_units))
  2826.         {
  2827.           complaint (&symfile_complaints,
  2828.                      _(".gdb_index entry has bad CU index"
  2829.                        " [in module %s]"),
  2830.                      objfile_name (dwarf2_per_objfile->objfile));
  2831.           continue;
  2832.         }

  2833.       per_cu = dw2_get_cutu (cu_index);

  2834.       /* Skip if already read in.  */
  2835.       if (per_cu->v.quick->compunit_symtab)
  2836.         continue;

  2837.       /* Check static vs global.  */
  2838.       if (attrs_valid)
  2839.         {
  2840.           if (iter->want_specific_block
  2841.               && want_static != is_static)
  2842.             continue;
  2843.           /* Work around gold/15646.  */
  2844.           if (!is_static && iter->global_seen)
  2845.             continue;
  2846.           if (!is_static)
  2847.             iter->global_seen = 1;
  2848.         }

  2849.       /* Only check the symbol's kind if it has one.  */
  2850.       if (attrs_valid)
  2851.         {
  2852.           switch (iter->domain)
  2853.             {
  2854.             case VAR_DOMAIN:
  2855.               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
  2856.                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
  2857.                   /* Some types are also in VAR_DOMAIN.  */
  2858.                   && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
  2859.                 continue;
  2860.               break;
  2861.             case STRUCT_DOMAIN:
  2862.               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
  2863.                 continue;
  2864.               break;
  2865.             case LABEL_DOMAIN:
  2866.               if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
  2867.                 continue;
  2868.               break;
  2869.             default:
  2870.               break;
  2871.             }
  2872.         }

  2873.       ++iter->next;
  2874.       return per_cu;
  2875.     }

  2876.   return NULL;
  2877. }

  2878. static struct compunit_symtab *
  2879. dw2_lookup_symbol (struct objfile *objfile, int block_index,
  2880.                    const char *name, domain_enum domain)
  2881. {
  2882.   struct compunit_symtab *stab_best = NULL;
  2883.   struct mapped_index *index;

  2884.   dw2_setup (objfile);

  2885.   index = dwarf2_per_objfile->index_table;

  2886.   /* index is NULL if OBJF_READNOW.  */
  2887.   if (index)
  2888.     {
  2889.       struct dw2_symtab_iterator iter;
  2890.       struct dwarf2_per_cu_data *per_cu;

  2891.       dw2_symtab_iter_init (&iter, index, 1, block_index, domain, name);

  2892.       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
  2893.         {
  2894.           struct symbol *sym = NULL;
  2895.           struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu);
  2896.           const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
  2897.           struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);

  2898.           /* Some caution must be observed with overloaded functions
  2899.              and methods, since the index will not contain any overload
  2900.              information (but NAME might contain it).  */
  2901.           sym = block_lookup_symbol (block, name, domain);

  2902.           if (sym && strcmp_iw (SYMBOL_SEARCH_NAME (sym), name) == 0)
  2903.             {
  2904.               if (!TYPE_IS_OPAQUE (SYMBOL_TYPE (sym)))
  2905.                 return stab;

  2906.               stab_best = stab;
  2907.             }

  2908.           /* Keep looking through other CUs.  */
  2909.         }
  2910.     }

  2911.   return stab_best;
  2912. }

  2913. static void
  2914. dw2_print_stats (struct objfile *objfile)
  2915. {
  2916.   int i, total, count;

  2917.   dw2_setup (objfile);
  2918.   total = dwarf2_per_objfile->n_comp_units + dwarf2_per_objfile->n_type_units;
  2919.   count = 0;
  2920.   for (i = 0; i < total; ++i)
  2921.     {
  2922.       struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);

  2923.       if (!per_cu->v.quick->compunit_symtab)
  2924.         ++count;
  2925.     }
  2926.   printf_filtered (_("  Number of read CUs: %d\n"), total - count);
  2927.   printf_filtered (_("  Number of unread CUs: %d\n"), count);
  2928. }

  2929. /* This dumps minimal information about the index.
  2930.    It is called via "mt print objfiles".
  2931.    One use is to verify .gdb_index has been loaded by the
  2932.    gdb.dwarf2/gdb-index.exp testcase.  */

  2933. static void
  2934. dw2_dump (struct objfile *objfile)
  2935. {
  2936.   dw2_setup (objfile);
  2937.   gdb_assert (dwarf2_per_objfile->using_index);
  2938.   printf_filtered (".gdb_index:");
  2939.   if (dwarf2_per_objfile->index_table != NULL)
  2940.     {
  2941.       printf_filtered (" version %d\n",
  2942.                        dwarf2_per_objfile->index_table->version);
  2943.     }
  2944.   else
  2945.     printf_filtered (" faked for \"readnow\"\n");
  2946.   printf_filtered ("\n");
  2947. }

  2948. static void
  2949. dw2_relocate (struct objfile *objfile,
  2950.               const struct section_offsets *new_offsets,
  2951.               const struct section_offsets *delta)
  2952. {
  2953.   /* There's nothing to relocate here.  */
  2954. }

  2955. static void
  2956. dw2_expand_symtabs_for_function (struct objfile *objfile,
  2957.                                  const char *func_name)
  2958. {
  2959.   struct mapped_index *index;

  2960.   dw2_setup (objfile);

  2961.   index = dwarf2_per_objfile->index_table;

  2962.   /* index is NULL if OBJF_READNOW.  */
  2963.   if (index)
  2964.     {
  2965.       struct dw2_symtab_iterator iter;
  2966.       struct dwarf2_per_cu_data *per_cu;

  2967.       /* Note: It doesn't matter what we pass for block_index here.  */
  2968.       dw2_symtab_iter_init (&iter, index, 0, GLOBAL_BLOCK, VAR_DOMAIN,
  2969.                             func_name);

  2970.       while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
  2971.         dw2_instantiate_symtab (per_cu);
  2972.     }
  2973. }

  2974. static void
  2975. dw2_expand_all_symtabs (struct objfile *objfile)
  2976. {
  2977.   int i;

  2978.   dw2_setup (objfile);

  2979.   for (i = 0; i < (dwarf2_per_objfile->n_comp_units
  2980.                    + dwarf2_per_objfile->n_type_units); ++i)
  2981.     {
  2982.       struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);

  2983.       dw2_instantiate_symtab (per_cu);
  2984.     }
  2985. }

  2986. static void
  2987. dw2_expand_symtabs_with_fullname (struct objfile *objfile,
  2988.                                   const char *fullname)
  2989. {
  2990.   int i;

  2991.   dw2_setup (objfile);

  2992.   /* We don't need to consider type units here.
  2993.      This is only called for examining code, e.g. expand_line_sal.
  2994.      There can be an order of magnitude (or more) more type units
  2995.      than comp units, and we avoid them if we can.  */

  2996.   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  2997.     {
  2998.       int j;
  2999.       struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
  3000.       struct quick_file_names *file_data;

  3001.       /* We only need to look at symtabs not already expanded.  */
  3002.       if (per_cu->v.quick->compunit_symtab)
  3003.         continue;

  3004.       file_data = dw2_get_file_names (per_cu);
  3005.       if (file_data == NULL)
  3006.         continue;

  3007.       for (j = 0; j < file_data->num_file_names; ++j)
  3008.         {
  3009.           const char *this_fullname = file_data->file_names[j];

  3010.           if (filename_cmp (this_fullname, fullname) == 0)
  3011.             {
  3012.               dw2_instantiate_symtab (per_cu);
  3013.               break;
  3014.             }
  3015.         }
  3016.     }
  3017. }

  3018. static void
  3019. dw2_map_matching_symbols (struct objfile *objfile,
  3020.                           const char * name, domain_enum namespace,
  3021.                           int global,
  3022.                           int (*callback) (struct block *,
  3023.                                            struct symbol *, void *),
  3024.                           void *data, symbol_compare_ftype *match,
  3025.                           symbol_compare_ftype *ordered_compare)
  3026. {
  3027.   /* Currently unimplemented; used for Ada.  The function can be called if the
  3028.      current language is Ada for a non-Ada objfile using GNU index.  As Ada
  3029.      does not look for non-Ada symbols this function should just return.  */
  3030. }

  3031. static void
  3032. dw2_expand_symtabs_matching
  3033.   (struct objfile *objfile,
  3034.    expand_symtabs_file_matcher_ftype *file_matcher,
  3035.    expand_symtabs_symbol_matcher_ftype *symbol_matcher,
  3036.    enum search_domain kind,
  3037.    void *data)
  3038. {
  3039.   int i;
  3040.   offset_type iter;
  3041.   struct mapped_index *index;

  3042.   dw2_setup (objfile);

  3043.   /* index_table is NULL if OBJF_READNOW.  */
  3044.   if (!dwarf2_per_objfile->index_table)
  3045.     return;
  3046.   index = dwarf2_per_objfile->index_table;

  3047.   if (file_matcher != NULL)
  3048.     {
  3049.       struct cleanup *cleanup;
  3050.       htab_t visited_found, visited_not_found;

  3051.       visited_found = htab_create_alloc (10,
  3052.                                          htab_hash_pointer, htab_eq_pointer,
  3053.                                          NULL, xcalloc, xfree);
  3054.       cleanup = make_cleanup_htab_delete (visited_found);
  3055.       visited_not_found = htab_create_alloc (10,
  3056.                                              htab_hash_pointer, htab_eq_pointer,
  3057.                                              NULL, xcalloc, xfree);
  3058.       make_cleanup_htab_delete (visited_not_found);

  3059.       /* The rule is CUs specify all the files, including those used by
  3060.          any TU, so there's no need to scan TUs here.  */

  3061.       for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  3062.         {
  3063.           int j;
  3064.           struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
  3065.           struct quick_file_names *file_data;
  3066.           void **slot;

  3067.           per_cu->v.quick->mark = 0;

  3068.           /* We only need to look at symtabs not already expanded.  */
  3069.           if (per_cu->v.quick->compunit_symtab)
  3070.             continue;

  3071.           file_data = dw2_get_file_names (per_cu);
  3072.           if (file_data == NULL)
  3073.             continue;

  3074.           if (htab_find (visited_not_found, file_data) != NULL)
  3075.             continue;
  3076.           else if (htab_find (visited_found, file_data) != NULL)
  3077.             {
  3078.               per_cu->v.quick->mark = 1;
  3079.               continue;
  3080.             }

  3081.           for (j = 0; j < file_data->num_file_names; ++j)
  3082.             {
  3083.               const char *this_real_name;

  3084.               if (file_matcher (file_data->file_names[j], data, 0))
  3085.                 {
  3086.                   per_cu->v.quick->mark = 1;
  3087.                   break;
  3088.                 }

  3089.               /* Before we invoke realpath, which can get expensive when many
  3090.                  files are involved, do a quick comparison of the basenames.  */
  3091.               if (!basenames_may_differ
  3092.                   && !file_matcher (lbasename (file_data->file_names[j]),
  3093.                                     data, 1))
  3094.                 continue;

  3095.               this_real_name = dw2_get_real_path (objfile, file_data, j);
  3096.               if (file_matcher (this_real_name, data, 0))
  3097.                 {
  3098.                   per_cu->v.quick->mark = 1;
  3099.                   break;
  3100.                 }
  3101.             }

  3102.           slot = htab_find_slot (per_cu->v.quick->mark
  3103.                                  ? visited_found
  3104.                                  : visited_not_found,
  3105.                                  file_data, INSERT);
  3106.           *slot = file_data;
  3107.         }

  3108.       do_cleanups (cleanup);
  3109.     }

  3110.   for (iter = 0; iter < index->symbol_table_slots; ++iter)
  3111.     {
  3112.       offset_type idx = 2 * iter;
  3113.       const char *name;
  3114.       offset_type *vec, vec_len, vec_idx;
  3115.       int global_seen = 0;

  3116.       if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
  3117.         continue;

  3118.       name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);

  3119.       if (! (*symbol_matcher) (name, data))
  3120.         continue;

  3121.       /* The name was matched, now expand corresponding CUs that were
  3122.          marked.  */
  3123.       vec = (offset_type *) (index->constant_pool
  3124.                              + MAYBE_SWAP (index->symbol_table[idx + 1]));
  3125.       vec_len = MAYBE_SWAP (vec[0]);
  3126.       for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
  3127.         {
  3128.           struct dwarf2_per_cu_data *per_cu;
  3129.           offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
  3130.           /* This value is only valid for index versions >= 7.  */
  3131.           int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
  3132.           gdb_index_symbol_kind symbol_kind =
  3133.             GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
  3134.           int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
  3135.           /* Only check the symbol attributes if they're present.
  3136.              Indices prior to version 7 don't record them,
  3137.              and indices >= 7 may elide them for certain symbols
  3138.              (gold does this).  */
  3139.           int attrs_valid =
  3140.             (index->version >= 7
  3141.              && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);

  3142.           /* Work around gold/15646.  */
  3143.           if (attrs_valid)
  3144.             {
  3145.               if (!is_static && global_seen)
  3146.                 continue;
  3147.               if (!is_static)
  3148.                 global_seen = 1;
  3149.             }

  3150.           /* Only check the symbol's kind if it has one.  */
  3151.           if (attrs_valid)
  3152.             {
  3153.               switch (kind)
  3154.                 {
  3155.                 case VARIABLES_DOMAIN:
  3156.                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
  3157.                     continue;
  3158.                   break;
  3159.                 case FUNCTIONS_DOMAIN:
  3160.                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
  3161.                     continue;
  3162.                   break;
  3163.                 case TYPES_DOMAIN:
  3164.                   if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
  3165.                     continue;
  3166.                   break;
  3167.                 default:
  3168.                   break;
  3169.                 }
  3170.             }

  3171.           /* Don't crash on bad data.  */
  3172.           if (cu_index >= (dwarf2_per_objfile->n_comp_units
  3173.                            + dwarf2_per_objfile->n_type_units))
  3174.             {
  3175.               complaint (&symfile_complaints,
  3176.                          _(".gdb_index entry has bad CU index"
  3177.                            " [in module %s]"), objfile_name (objfile));
  3178.               continue;
  3179.             }

  3180.           per_cu = dw2_get_cutu (cu_index);
  3181.           if (file_matcher == NULL || per_cu->v.quick->mark)
  3182.             dw2_instantiate_symtab (per_cu);
  3183.         }
  3184.     }
  3185. }

  3186. /* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
  3187.    symtab.  */

  3188. static struct compunit_symtab *
  3189. recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
  3190.                                           CORE_ADDR pc)
  3191. {
  3192.   int i;

  3193.   if (COMPUNIT_BLOCKVECTOR (cust) != NULL
  3194.       && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
  3195.     return cust;

  3196.   if (cust->includes == NULL)
  3197.     return NULL;

  3198.   for (i = 0; cust->includes[i]; ++i)
  3199.     {
  3200.       struct compunit_symtab *s = cust->includes[i];

  3201.       s = recursively_find_pc_sect_compunit_symtab (s, pc);
  3202.       if (s != NULL)
  3203.         return s;
  3204.     }

  3205.   return NULL;
  3206. }

  3207. static struct compunit_symtab *
  3208. dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
  3209.                                   struct bound_minimal_symbol msymbol,
  3210.                                   CORE_ADDR pc,
  3211.                                   struct obj_section *section,
  3212.                                   int warn_if_readin)
  3213. {
  3214.   struct dwarf2_per_cu_data *data;
  3215.   struct compunit_symtab *result;

  3216.   dw2_setup (objfile);

  3217.   if (!objfile->psymtabs_addrmap)
  3218.     return NULL;

  3219.   data = addrmap_find (objfile->psymtabs_addrmap, pc);
  3220.   if (!data)
  3221.     return NULL;

  3222.   if (warn_if_readin && data->v.quick->compunit_symtab)
  3223.     warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
  3224.              paddress (get_objfile_arch (objfile), pc));

  3225.   result
  3226.     = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data),
  3227.                                                 pc);
  3228.   gdb_assert (result != NULL);
  3229.   return result;
  3230. }

  3231. static void
  3232. dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
  3233.                           void *data, int need_fullname)
  3234. {
  3235.   int i;
  3236.   struct cleanup *cleanup;
  3237.   htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
  3238.                                       NULL, xcalloc, xfree);

  3239.   cleanup = make_cleanup_htab_delete (visited);
  3240.   dw2_setup (objfile);

  3241.   /* The rule is CUs specify all the files, including those used by
  3242.      any TU, so there's no need to scan TUs here.
  3243.      We can ignore file names coming from already-expanded CUs.  */

  3244.   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  3245.     {
  3246.       struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);

  3247.       if (per_cu->v.quick->compunit_symtab)
  3248.         {
  3249.           void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
  3250.                                         INSERT);

  3251.           *slot = per_cu->v.quick->file_names;
  3252.         }
  3253.     }

  3254.   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  3255.     {
  3256.       int j;
  3257.       struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
  3258.       struct quick_file_names *file_data;
  3259.       void **slot;

  3260.       /* We only need to look at symtabs not already expanded.  */
  3261.       if (per_cu->v.quick->compunit_symtab)
  3262.         continue;

  3263.       file_data = dw2_get_file_names (per_cu);
  3264.       if (file_data == NULL)
  3265.         continue;

  3266.       slot = htab_find_slot (visited, file_data, INSERT);
  3267.       if (*slot)
  3268.         {
  3269.           /* Already visited.  */
  3270.           continue;
  3271.         }
  3272.       *slot = file_data;

  3273.       for (j = 0; j < file_data->num_file_names; ++j)
  3274.         {
  3275.           const char *this_real_name;

  3276.           if (need_fullname)
  3277.             this_real_name = dw2_get_real_path (objfile, file_data, j);
  3278.           else
  3279.             this_real_name = NULL;
  3280.           (*fun) (file_data->file_names[j], this_real_name, data);
  3281.         }
  3282.     }

  3283.   do_cleanups (cleanup);
  3284. }

  3285. static int
  3286. dw2_has_symbols (struct objfile *objfile)
  3287. {
  3288.   return 1;
  3289. }

  3290. const struct quick_symbol_functions dwarf2_gdb_index_functions =
  3291. {
  3292.   dw2_has_symbols,
  3293.   dw2_find_last_source_symtab,
  3294.   dw2_forget_cached_source_info,
  3295.   dw2_map_symtabs_matching_filename,
  3296.   dw2_lookup_symbol,
  3297.   dw2_print_stats,
  3298.   dw2_dump,
  3299.   dw2_relocate,
  3300.   dw2_expand_symtabs_for_function,
  3301.   dw2_expand_all_symtabs,
  3302.   dw2_expand_symtabs_with_fullname,
  3303.   dw2_map_matching_symbols,
  3304.   dw2_expand_symtabs_matching,
  3305.   dw2_find_pc_sect_compunit_symtab,
  3306.   dw2_map_symbol_filenames
  3307. };

  3308. /* Initialize for reading DWARF for this objfile.  Return 0 if this
  3309.    file will use psymtabs, or 1 if using the GNU index.  */

  3310. int
  3311. dwarf2_initialize_objfile (struct objfile *objfile)
  3312. {
  3313.   /* If we're about to read full symbols, don't bother with the
  3314.      indices.  In this case we also don't care if some other debug
  3315.      format is making psymtabs, because they are all about to be
  3316.      expanded anyway.  */
  3317.   if ((objfile->flags & OBJF_READNOW))
  3318.     {
  3319.       int i;

  3320.       dwarf2_per_objfile->using_index = 1;
  3321.       create_all_comp_units (objfile);
  3322.       create_all_type_units (objfile);
  3323.       dwarf2_per_objfile->quick_file_names_table =
  3324.         create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);

  3325.       for (i = 0; i < (dwarf2_per_objfile->n_comp_units
  3326.                        + dwarf2_per_objfile->n_type_units); ++i)
  3327.         {
  3328.           struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);

  3329.           per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  3330.                                             struct dwarf2_per_cu_quick_data);
  3331.         }

  3332.       /* Return 1 so that gdb sees the "quick" functions.  However,
  3333.          these functions will be no-ops because we will have expanded
  3334.          all symtabs.  */
  3335.       return 1;
  3336.     }

  3337.   if (dwarf2_read_index (objfile))
  3338.     return 1;

  3339.   return 0;
  3340. }



  3341. /* Build a partial symbol table.  */

  3342. void
  3343. dwarf2_build_psymtabs (struct objfile *objfile)
  3344. {
  3345.   volatile struct gdb_exception except;

  3346.   if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
  3347.     {
  3348.       init_psymbol_list (objfile, 1024);
  3349.     }

  3350.   TRY_CATCH (except, RETURN_MASK_ERROR)
  3351.     {
  3352.       /* This isn't really ideal: all the data we allocate on the
  3353.          objfile's obstack is still uselessly kept around.  However,
  3354.          freeing it seems unsafe.  */
  3355.       struct cleanup *cleanups = make_cleanup_discard_psymtabs (objfile);

  3356.       dwarf2_build_psymtabs_hard (objfile);
  3357.       discard_cleanups (cleanups);
  3358.     }
  3359.   if (except.reason < 0)
  3360.     exception_print (gdb_stderr, except);
  3361. }

  3362. /* Return the total length of the CU described by HEADER.  */

  3363. static unsigned int
  3364. get_cu_length (const struct comp_unit_head *header)
  3365. {
  3366.   return header->initial_length_size + header->length;
  3367. }

  3368. /* Return TRUE if OFFSET is within CU_HEADER.  */

  3369. static inline int
  3370. offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
  3371. {
  3372.   sect_offset bottom = { cu_header->offset.sect_off };
  3373.   sect_offset top = { cu_header->offset.sect_off + get_cu_length (cu_header) };

  3374.   return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
  3375. }

  3376. /* Find the base address of the compilation unit for range lists and
  3377.    location lists.  It will normally be specified by DW_AT_low_pc.
  3378.    In DWARF-3 draft 4, the base address could be overridden by
  3379.    DW_AT_entry_pc.  It's been removed, but GCC still uses this for
  3380.    compilation units with discontinuous ranges.  */

  3381. static void
  3382. dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
  3383. {
  3384.   struct attribute *attr;

  3385.   cu->base_known = 0;
  3386.   cu->base_address = 0;

  3387.   attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
  3388.   if (attr)
  3389.     {
  3390.       cu->base_address = attr_value_as_address (attr);
  3391.       cu->base_known = 1;
  3392.     }
  3393.   else
  3394.     {
  3395.       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
  3396.       if (attr)
  3397.         {
  3398.           cu->base_address = attr_value_as_address (attr);
  3399.           cu->base_known = 1;
  3400.         }
  3401.     }
  3402. }

  3403. /* Read in the comp unit header information from the debug_info at info_ptr.
  3404.    NOTE: This leaves members offset, first_die_offset to be filled in
  3405.    by the caller.  */

  3406. static const gdb_byte *
  3407. read_comp_unit_head (struct comp_unit_head *cu_header,
  3408.                      const gdb_byte *info_ptr, bfd *abfd)
  3409. {
  3410.   int signed_addr;
  3411.   unsigned int bytes_read;

  3412.   cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
  3413.   cu_header->initial_length_size = bytes_read;
  3414.   cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
  3415.   info_ptr += bytes_read;
  3416.   cu_header->version = read_2_bytes (abfd, info_ptr);
  3417.   info_ptr += 2;
  3418.   cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
  3419.                                              &bytes_read);
  3420.   info_ptr += bytes_read;
  3421.   cu_header->addr_size = read_1_byte (abfd, info_ptr);
  3422.   info_ptr += 1;
  3423.   signed_addr = bfd_get_sign_extend_vma (abfd);
  3424.   if (signed_addr < 0)
  3425.     internal_error (__FILE__, __LINE__,
  3426.                     _("read_comp_unit_head: dwarf from non elf file"));
  3427.   cu_header->signed_addr_p = signed_addr;

  3428.   return info_ptr;
  3429. }

  3430. /* Helper function that returns the proper abbrev section for
  3431.    THIS_CU.  */

  3432. static struct dwarf2_section_info *
  3433. get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
  3434. {
  3435.   struct dwarf2_section_info *abbrev;

  3436.   if (this_cu->is_dwz)
  3437.     abbrev = &dwarf2_get_dwz_file ()->abbrev;
  3438.   else
  3439.     abbrev = &dwarf2_per_objfile->abbrev;

  3440.   return abbrev;
  3441. }

  3442. /* Subroutine of read_and_check_comp_unit_head and
  3443.    read_and_check_type_unit_head to simplify them.
  3444.    Perform various error checking on the header.  */

  3445. static void
  3446. error_check_comp_unit_head (struct comp_unit_head *header,
  3447.                             struct dwarf2_section_info *section,
  3448.                             struct dwarf2_section_info *abbrev_section)
  3449. {
  3450.   bfd *abfd = get_section_bfd_owner (section);
  3451.   const char *filename = get_section_file_name (section);

  3452.   if (header->version != 2 && header->version != 3 && header->version != 4)
  3453.     error (_("Dwarf Error: wrong version in compilation unit header "
  3454.            "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
  3455.            filename);

  3456.   if (header->abbrev_offset.sect_off
  3457.       >= dwarf2_section_size (dwarf2_per_objfile->objfile, abbrev_section))
  3458.     error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
  3459.            "(offset 0x%lx + 6) [in module %s]"),
  3460.            (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
  3461.            filename);

  3462.   /* Cast to unsigned long to use 64-bit arithmetic when possible to
  3463.      avoid potential 32-bit overflow.  */
  3464.   if (((unsigned long) header->offset.sect_off + get_cu_length (header))
  3465.       > section->size)
  3466.     error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
  3467.            "(offset 0x%lx + 0) [in module %s]"),
  3468.            (long) header->length, (long) header->offset.sect_off,
  3469.            filename);
  3470. }

  3471. /* Read in a CU/TU header and perform some basic error checking.
  3472.    The contents of the header are stored in HEADER.
  3473.    The result is a pointer to the start of the first DIE.  */

  3474. static const gdb_byte *
  3475. read_and_check_comp_unit_head (struct comp_unit_head *header,
  3476.                                struct dwarf2_section_info *section,
  3477.                                struct dwarf2_section_info *abbrev_section,
  3478.                                const gdb_byte *info_ptr,
  3479.                                int is_debug_types_section)
  3480. {
  3481.   const gdb_byte *beg_of_comp_unit = info_ptr;
  3482.   bfd *abfd = get_section_bfd_owner (section);

  3483.   header->offset.sect_off = beg_of_comp_unit - section->buffer;

  3484.   info_ptr = read_comp_unit_head (header, info_ptr, abfd);

  3485.   /* If we're reading a type unit, skip over the signature and
  3486.      type_offset fields.  */
  3487.   if (is_debug_types_section)
  3488.     info_ptr += 8 /*signature*/ + header->offset_size;

  3489.   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;

  3490.   error_check_comp_unit_head (header, section, abbrev_section);

  3491.   return info_ptr;
  3492. }

  3493. /* Read in the types comp unit header information from .debug_types entry at
  3494.    types_ptr.  The result is a pointer to one past the end of the header.  */

  3495. static const gdb_byte *
  3496. read_and_check_type_unit_head (struct comp_unit_head *header,
  3497.                                struct dwarf2_section_info *section,
  3498.                                struct dwarf2_section_info *abbrev_section,
  3499.                                const gdb_byte *info_ptr,
  3500.                                ULONGEST *signature,
  3501.                                cu_offset *type_offset_in_tu)
  3502. {
  3503.   const gdb_byte *beg_of_comp_unit = info_ptr;
  3504.   bfd *abfd = get_section_bfd_owner (section);

  3505.   header->offset.sect_off = beg_of_comp_unit - section->buffer;

  3506.   info_ptr = read_comp_unit_head (header, info_ptr, abfd);

  3507.   /* If we're reading a type unit, skip over the signature and
  3508.      type_offset fields.  */
  3509.   if (signature != NULL)
  3510.     *signature = read_8_bytes (abfd, info_ptr);
  3511.   info_ptr += 8;
  3512.   if (type_offset_in_tu != NULL)
  3513.     type_offset_in_tu->cu_off = read_offset_1 (abfd, info_ptr,
  3514.                                                header->offset_size);
  3515.   info_ptr += header->offset_size;

  3516.   header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;

  3517.   error_check_comp_unit_head (header, section, abbrev_section);

  3518.   return info_ptr;
  3519. }

  3520. /* Fetch the abbreviation table offset from a comp or type unit header.  */

  3521. static sect_offset
  3522. read_abbrev_offset (struct dwarf2_section_info *section,
  3523.                     sect_offset offset)
  3524. {
  3525.   bfd *abfd = get_section_bfd_owner (section);
  3526.   const gdb_byte *info_ptr;
  3527.   unsigned int length, initial_length_size, offset_size;
  3528.   sect_offset abbrev_offset;

  3529.   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
  3530.   info_ptr = section->buffer + offset.sect_off;
  3531.   length = read_initial_length (abfd, info_ptr, &initial_length_size);
  3532.   offset_size = initial_length_size == 4 ? 4 : 8;
  3533.   info_ptr += initial_length_size + 2 /*version*/;
  3534.   abbrev_offset.sect_off = read_offset_1 (abfd, info_ptr, offset_size);
  3535.   return abbrev_offset;
  3536. }

  3537. /* Allocate a new partial symtab for file named NAME and mark this new
  3538.    partial symtab as being an include of PST.  */

  3539. static void
  3540. dwarf2_create_include_psymtab (const char *name, struct partial_symtab *pst,
  3541.                                struct objfile *objfile)
  3542. {
  3543.   struct partial_symtab *subpst = allocate_psymtab (name, objfile);

  3544.   if (!IS_ABSOLUTE_PATH (subpst->filename))
  3545.     {
  3546.       /* It shares objfile->objfile_obstack.  */
  3547.       subpst->dirname = pst->dirname;
  3548.     }

  3549.   subpst->section_offsets = pst->section_offsets;
  3550.   subpst->textlow = 0;
  3551.   subpst->texthigh = 0;

  3552.   subpst->dependencies = (struct partial_symtab **)
  3553.     obstack_alloc (&objfile->objfile_obstack,
  3554.                    sizeof (struct partial_symtab *));
  3555.   subpst->dependencies[0] = pst;
  3556.   subpst->number_of_dependencies = 1;

  3557.   subpst->globals_offset = 0;
  3558.   subpst->n_global_syms = 0;
  3559.   subpst->statics_offset = 0;
  3560.   subpst->n_static_syms = 0;
  3561.   subpst->compunit_symtab = NULL;
  3562.   subpst->read_symtab = pst->read_symtab;
  3563.   subpst->readin = 0;

  3564.   /* No private part is necessary for include psymtabs.  This property
  3565.      can be used to differentiate between such include psymtabs and
  3566.      the regular ones.  */
  3567.   subpst->read_symtab_private = NULL;
  3568. }

  3569. /* Read the Line Number Program data and extract the list of files
  3570.    included by the source file represented by PST.  Build an include
  3571.    partial symtab for each of these included files.  */

  3572. static void
  3573. dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
  3574.                                struct die_info *die,
  3575.                                struct partial_symtab *pst)
  3576. {
  3577.   struct line_header *lh = NULL;
  3578.   struct attribute *attr;

  3579.   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
  3580.   if (attr)
  3581.     lh = dwarf_decode_line_header (DW_UNSND (attr), cu);
  3582.   if (lh == NULL)
  3583.     return/* No linetable, so no includes.  */

  3584.   /* NOTE: pst->dirname is DW_AT_comp_dir (if present).  */
  3585.   dwarf_decode_lines (lh, pst->dirname, cu, pst, pst->textlow);

  3586.   free_line_header (lh);
  3587. }

  3588. static hashval_t
  3589. hash_signatured_type (const void *item)
  3590. {
  3591.   const struct signatured_type *sig_type = item;

  3592.   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
  3593.   return sig_type->signature;
  3594. }

  3595. static int
  3596. eq_signatured_type (const void *item_lhs, const void *item_rhs)
  3597. {
  3598.   const struct signatured_type *lhs = item_lhs;
  3599.   const struct signatured_type *rhs = item_rhs;

  3600.   return lhs->signature == rhs->signature;
  3601. }

  3602. /* Allocate a hash table for signatured types.  */

  3603. static htab_t
  3604. allocate_signatured_type_table (struct objfile *objfile)
  3605. {
  3606.   return htab_create_alloc_ex (41,
  3607.                                hash_signatured_type,
  3608.                                eq_signatured_type,
  3609.                                NULL,
  3610.                                &objfile->objfile_obstack,
  3611.                                hashtab_obstack_allocate,
  3612.                                dummy_obstack_deallocate);
  3613. }

  3614. /* A helper function to add a signatured type CU to a table.  */

  3615. static int
  3616. add_signatured_type_cu_to_table (void **slot, void *datum)
  3617. {
  3618.   struct signatured_type *sigt = *slot;
  3619.   struct signatured_type ***datap = datum;

  3620.   **datap = sigt;
  3621.   ++*datap;

  3622.   return 1;
  3623. }

  3624. /* Create the hash table of all entries in the .debug_types
  3625.    (or .debug_types.dwo) section(s).
  3626.    If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
  3627.    otherwise it is NULL.

  3628.    The result is a pointer to the hash table or NULL if there are no types.

  3629.    Note: This function processes DWO files only, not DWP files.  */

  3630. static htab_t
  3631. create_debug_types_hash_table (struct dwo_file *dwo_file,
  3632.                                VEC (dwarf2_section_info_def) *types)
  3633. {
  3634.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  3635.   htab_t types_htab = NULL;
  3636.   int ix;
  3637.   struct dwarf2_section_info *section;
  3638.   struct dwarf2_section_info *abbrev_section;

  3639.   if (VEC_empty (dwarf2_section_info_def, types))
  3640.     return NULL;

  3641.   abbrev_section = (dwo_file != NULL
  3642.                     ? &dwo_file->sections.abbrev
  3643.                     : &dwarf2_per_objfile->abbrev);

  3644.   if (dwarf2_read_debug)
  3645.     fprintf_unfiltered (gdb_stdlog, "Reading .debug_types%s for %s:\n",
  3646.                         dwo_file ? ".dwo" : "",
  3647.                         get_section_file_name (abbrev_section));

  3648.   for (ix = 0;
  3649.        VEC_iterate (dwarf2_section_info_def, types, ix, section);
  3650.        ++ix)
  3651.     {
  3652.       bfd *abfd;
  3653.       const gdb_byte *info_ptr, *end_ptr;

  3654.       dwarf2_read_section (objfile, section);
  3655.       info_ptr = section->buffer;

  3656.       if (info_ptr == NULL)
  3657.         continue;

  3658.       /* We can't set abfd until now because the section may be empty or
  3659.          not present, in which case the bfd is unknown.  */
  3660.       abfd = get_section_bfd_owner (section);

  3661.       /* We don't use init_cutu_and_read_dies_simple, or some such, here
  3662.          because we don't need to read any dies: the signature is in the
  3663.          header.  */

  3664.       end_ptr = info_ptr + section->size;
  3665.       while (info_ptr < end_ptr)
  3666.         {
  3667.           sect_offset offset;
  3668.           cu_offset type_offset_in_tu;
  3669.           ULONGEST signature;
  3670.           struct signatured_type *sig_type;
  3671.           struct dwo_unit *dwo_tu;
  3672.           void **slot;
  3673.           const gdb_byte *ptr = info_ptr;
  3674.           struct comp_unit_head header;
  3675.           unsigned int length;

  3676.           offset.sect_off = ptr - section->buffer;

  3677.           /* We need to read the type's signature in order to build the hash
  3678.              table, but we don't need anything else just yet.  */

  3679.           ptr = read_and_check_type_unit_head (&header, section,
  3680.                                                abbrev_section, ptr,
  3681.                                                &signature, &type_offset_in_tu);

  3682.           length = get_cu_length (&header);

  3683.           /* Skip dummy type units.  */
  3684.           if (ptr >= info_ptr + length
  3685.               || peek_abbrev_code (abfd, ptr) == 0)
  3686.             {
  3687.               info_ptr += length;
  3688.               continue;
  3689.             }

  3690.           if (types_htab == NULL)
  3691.             {
  3692.               if (dwo_file)
  3693.                 types_htab = allocate_dwo_unit_table (objfile);
  3694.               else
  3695.                 types_htab = allocate_signatured_type_table (objfile);
  3696.             }

  3697.           if (dwo_file)
  3698.             {
  3699.               sig_type = NULL;
  3700.               dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  3701.                                        struct dwo_unit);
  3702.               dwo_tu->dwo_file = dwo_file;
  3703.               dwo_tu->signature = signature;
  3704.               dwo_tu->type_offset_in_tu = type_offset_in_tu;
  3705.               dwo_tu->section = section;
  3706.               dwo_tu->offset = offset;
  3707.               dwo_tu->length = length;
  3708.             }
  3709.           else
  3710.             {
  3711.               /* N.B.: type_offset is not usable if this type uses a DWO file.
  3712.                  The real type_offset is in the DWO file.  */
  3713.               dwo_tu = NULL;
  3714.               sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  3715.                                          struct signatured_type);
  3716.               sig_type->signature = signature;
  3717.               sig_type->type_offset_in_tu = type_offset_in_tu;
  3718.               sig_type->per_cu.objfile = objfile;
  3719.               sig_type->per_cu.is_debug_types = 1;
  3720.               sig_type->per_cu.section = section;
  3721.               sig_type->per_cu.offset = offset;
  3722.               sig_type->per_cu.length = length;
  3723.             }

  3724.           slot = htab_find_slot (types_htab,
  3725.                                  dwo_file ? (void*) dwo_tu : (void *) sig_type,
  3726.                                  INSERT);
  3727.           gdb_assert (slot != NULL);
  3728.           if (*slot != NULL)
  3729.             {
  3730.               sect_offset dup_offset;

  3731.               if (dwo_file)
  3732.                 {
  3733.                   const struct dwo_unit *dup_tu = *slot;

  3734.                   dup_offset = dup_tu->offset;
  3735.                 }
  3736.               else
  3737.                 {
  3738.                   const struct signatured_type *dup_tu = *slot;

  3739.                   dup_offset = dup_tu->per_cu.offset;
  3740.                 }

  3741.               complaint (&symfile_complaints,
  3742.                          _("debug type entry at offset 0x%x is duplicate to"
  3743.                            " the entry at offset 0x%x, signature %s"),
  3744.                          offset.sect_off, dup_offset.sect_off,
  3745.                          hex_string (signature));
  3746.             }
  3747.           *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;

  3748.           if (dwarf2_read_debug > 1)
  3749.             fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, signature %s\n",
  3750.                                 offset.sect_off,
  3751.                                 hex_string (signature));

  3752.           info_ptr += length;
  3753.         }
  3754.     }

  3755.   return types_htab;
  3756. }

  3757. /* Create the hash table of all entries in the .debug_types section,
  3758.    and initialize all_type_units.
  3759.    The result is zero if there is an error (e.g. missing .debug_types section),
  3760.    otherwise non-zero.        */

  3761. static int
  3762. create_all_type_units (struct objfile *objfile)
  3763. {
  3764.   htab_t types_htab;
  3765.   struct signatured_type **iter;

  3766.   types_htab = create_debug_types_hash_table (NULL, dwarf2_per_objfile->types);
  3767.   if (types_htab == NULL)
  3768.     {
  3769.       dwarf2_per_objfile->signatured_types = NULL;
  3770.       return 0;
  3771.     }

  3772.   dwarf2_per_objfile->signatured_types = types_htab;

  3773.   dwarf2_per_objfile->n_type_units
  3774.     = dwarf2_per_objfile->n_allocated_type_units
  3775.     = htab_elements (types_htab);
  3776.   dwarf2_per_objfile->all_type_units
  3777.     = xmalloc (dwarf2_per_objfile->n_type_units
  3778.                * sizeof (struct signatured_type *));
  3779.   iter = &dwarf2_per_objfile->all_type_units[0];
  3780.   htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
  3781.   gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
  3782.               == dwarf2_per_objfile->n_type_units);

  3783.   return 1;
  3784. }

  3785. /* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
  3786.    If SLOT is non-NULL, it is the entry to use in the hash table.
  3787.    Otherwise we find one.  */

  3788. static struct signatured_type *
  3789. add_type_unit (ULONGEST sig, void **slot)
  3790. {
  3791.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  3792.   int n_type_units = dwarf2_per_objfile->n_type_units;
  3793.   struct signatured_type *sig_type;

  3794.   gdb_assert (n_type_units <= dwarf2_per_objfile->n_allocated_type_units);
  3795.   ++n_type_units;
  3796.   if (n_type_units > dwarf2_per_objfile->n_allocated_type_units)
  3797.     {
  3798.       if (dwarf2_per_objfile->n_allocated_type_units == 0)
  3799.         dwarf2_per_objfile->n_allocated_type_units = 1;
  3800.       dwarf2_per_objfile->n_allocated_type_units *= 2;
  3801.       dwarf2_per_objfile->all_type_units
  3802.         = xrealloc (dwarf2_per_objfile->all_type_units,
  3803.                     dwarf2_per_objfile->n_allocated_type_units
  3804.                     * sizeof (struct signatured_type *));
  3805.       ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
  3806.     }
  3807.   dwarf2_per_objfile->n_type_units = n_type_units;

  3808.   sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  3809.                              struct signatured_type);
  3810.   dwarf2_per_objfile->all_type_units[n_type_units - 1] = sig_type;
  3811.   sig_type->signature = sig;
  3812.   sig_type->per_cu.is_debug_types = 1;
  3813.   if (dwarf2_per_objfile->using_index)
  3814.     {
  3815.       sig_type->per_cu.v.quick =
  3816.         OBSTACK_ZALLOC (&objfile->objfile_obstack,
  3817.                         struct dwarf2_per_cu_quick_data);
  3818.     }

  3819.   if (slot == NULL)
  3820.     {
  3821.       slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
  3822.                              sig_type, INSERT);
  3823.     }
  3824.   gdb_assert (*slot == NULL);
  3825.   *slot = sig_type;
  3826.   /* The rest of sig_type must be filled in by the caller.  */
  3827.   return sig_type;
  3828. }

  3829. /* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
  3830.    Fill in SIG_ENTRY with DWO_ENTRY.  */

  3831. static void
  3832. fill_in_sig_entry_from_dwo_entry (struct objfile *objfile,
  3833.                                   struct signatured_type *sig_entry,
  3834.                                   struct dwo_unit *dwo_entry)
  3835. {
  3836.   /* Make sure we're not clobbering something we don't expect to.  */
  3837.   gdb_assert (! sig_entry->per_cu.queued);
  3838.   gdb_assert (sig_entry->per_cu.cu == NULL);
  3839.   if (dwarf2_per_objfile->using_index)
  3840.     {
  3841.       gdb_assert (sig_entry->per_cu.v.quick != NULL);
  3842.       gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
  3843.     }
  3844.   else
  3845.       gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
  3846.   gdb_assert (sig_entry->signature == dwo_entry->signature);
  3847.   gdb_assert (sig_entry->type_offset_in_section.sect_off == 0);
  3848.   gdb_assert (sig_entry->type_unit_group == NULL);
  3849.   gdb_assert (sig_entry->dwo_unit == NULL);

  3850.   sig_entry->per_cu.section = dwo_entry->section;
  3851.   sig_entry->per_cu.offset = dwo_entry->offset;
  3852.   sig_entry->per_cu.length = dwo_entry->length;
  3853.   sig_entry->per_cu.reading_dwo_directly = 1;
  3854.   sig_entry->per_cu.objfile = objfile;
  3855.   sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
  3856.   sig_entry->dwo_unit = dwo_entry;
  3857. }

  3858. /* Subroutine of lookup_signatured_type.
  3859.    If we haven't read the TU yet, create the signatured_type data structure
  3860.    for a TU to be read in directly from a DWO file, bypassing the stub.
  3861.    This is the "Stay in DWO Optimization": When there is no DWP file and we're
  3862.    using .gdb_index, then when reading a CU we want to stay in the DWO file
  3863.    containing that CU.  Otherwise we could end up reading several other DWO
  3864.    files (due to comdat folding) to process the transitive closure of all the
  3865.    mentioned TUs, and that can be slow.  The current DWO file will have every
  3866.    type signature that it needs.
  3867.    We only do this for .gdb_index because in the psymtab case we already have
  3868.    to read all the DWOs to build the type unit groups.  */

  3869. static struct signatured_type *
  3870. lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
  3871. {
  3872.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  3873.   struct dwo_file *dwo_file;
  3874.   struct dwo_unit find_dwo_entry, *dwo_entry;
  3875.   struct signatured_type find_sig_entry, *sig_entry;
  3876.   void **slot;

  3877.   gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);

  3878.   /* If TU skeletons have been removed then we may not have read in any
  3879.      TUs yet.  */
  3880.   if (dwarf2_per_objfile->signatured_types == NULL)
  3881.     {
  3882.       dwarf2_per_objfile->signatured_types
  3883.         = allocate_signatured_type_table (objfile);
  3884.     }

  3885.   /* We only ever need to read in one copy of a signatured type.
  3886.      Use the global signatured_types array to do our own comdat-folding
  3887.      of types.  If this is the first time we're reading this TU, and
  3888.      the TU has an entry in .gdb_index, replace the recorded data from
  3889.      .gdb_index with this TU.  */

  3890.   find_sig_entry.signature = sig;
  3891.   slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
  3892.                          &find_sig_entry, INSERT);
  3893.   sig_entry = *slot;

  3894.   /* We can get here with the TU already read, *or* in the process of being
  3895.      read.  Don't reassign the global entry to point to this DWO if that's
  3896.      the case.  Also note that if the TU is already being read, it may not
  3897.      have come from a DWO, the program may be a mix of Fission-compiled
  3898.      code and non-Fission-compiled code.  */

  3899.   /* Have we already tried to read this TU?
  3900.      Note: sig_entry can be NULL if the skeleton TU was removed (thus it
  3901.      needn't exist in the global table yet).  */
  3902.   if (sig_entry != NULL && sig_entry->per_cu.tu_read)
  3903.     return sig_entry;

  3904.   /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
  3905.      dwo_unit of the TU itself.  */
  3906.   dwo_file = cu->dwo_unit->dwo_file;

  3907.   /* Ok, this is the first time we're reading this TU.  */
  3908.   if (dwo_file->tus == NULL)
  3909.     return NULL;
  3910.   find_dwo_entry.signature = sig;
  3911.   dwo_entry = htab_find (dwo_file->tus, &find_dwo_entry);
  3912.   if (dwo_entry == NULL)
  3913.     return NULL;

  3914.   /* If the global table doesn't have an entry for this TU, add one.  */
  3915.   if (sig_entry == NULL)
  3916.     sig_entry = add_type_unit (sig, slot);

  3917.   fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);
  3918.   sig_entry->per_cu.tu_read = 1;
  3919.   return sig_entry;
  3920. }

  3921. /* Subroutine of lookup_signatured_type.
  3922.    Look up the type for signature SIG, and if we can't find SIG in .gdb_index
  3923.    then try the DWP file.  If the TU stub (skeleton) has been removed then
  3924.    it won't be in .gdb_index.  */

  3925. static struct signatured_type *
  3926. lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
  3927. {
  3928.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  3929.   struct dwp_file *dwp_file = get_dwp_file ();
  3930.   struct dwo_unit *dwo_entry;
  3931.   struct signatured_type find_sig_entry, *sig_entry;
  3932.   void **slot;

  3933.   gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
  3934.   gdb_assert (dwp_file != NULL);

  3935.   /* If TU skeletons have been removed then we may not have read in any
  3936.      TUs yet.  */
  3937.   if (dwarf2_per_objfile->signatured_types == NULL)
  3938.     {
  3939.       dwarf2_per_objfile->signatured_types
  3940.         = allocate_signatured_type_table (objfile);
  3941.     }

  3942.   find_sig_entry.signature = sig;
  3943.   slot = htab_find_slot (dwarf2_per_objfile->signatured_types,
  3944.                          &find_sig_entry, INSERT);
  3945.   sig_entry = *slot;

  3946.   /* Have we already tried to read this TU?
  3947.      Note: sig_entry can be NULL if the skeleton TU was removed (thus it
  3948.      needn't exist in the global table yet).  */
  3949.   if (sig_entry != NULL)
  3950.     return sig_entry;

  3951.   if (dwp_file->tus == NULL)
  3952.     return NULL;
  3953.   dwo_entry = lookup_dwo_unit_in_dwp (dwp_file, NULL,
  3954.                                       sig, 1 /* is_debug_types */);
  3955.   if (dwo_entry == NULL)
  3956.     return NULL;

  3957.   sig_entry = add_type_unit (sig, slot);
  3958.   fill_in_sig_entry_from_dwo_entry (objfile, sig_entry, dwo_entry);

  3959.   return sig_entry;
  3960. }

  3961. /* Lookup a signature based type for DW_FORM_ref_sig8.
  3962.    Returns NULL if signature SIG is not present in the table.
  3963.    It is up to the caller to complain about this.  */

  3964. static struct signatured_type *
  3965. lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
  3966. {
  3967.   if (cu->dwo_unit
  3968.       && dwarf2_per_objfile->using_index)
  3969.     {
  3970.       /* We're in a DWO/DWP file, and we're using .gdb_index.
  3971.          These cases require special processing.  */
  3972.       if (get_dwp_file () == NULL)
  3973.         return lookup_dwo_signatured_type (cu, sig);
  3974.       else
  3975.         return lookup_dwp_signatured_type (cu, sig);
  3976.     }
  3977.   else
  3978.     {
  3979.       struct signatured_type find_entry, *entry;

  3980.       if (dwarf2_per_objfile->signatured_types == NULL)
  3981.         return NULL;
  3982.       find_entry.signature = sig;
  3983.       entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
  3984.       return entry;
  3985.     }
  3986. }

  3987. /* Low level DIE reading support.  */

  3988. /* Initialize a die_reader_specs struct from a dwarf2_cu struct.  */

  3989. static void
  3990. init_cu_die_reader (struct die_reader_specs *reader,
  3991.                     struct dwarf2_cu *cu,
  3992.                     struct dwarf2_section_info *section,
  3993.                     struct dwo_file *dwo_file)
  3994. {
  3995.   gdb_assert (section->readin && section->buffer != NULL);
  3996.   reader->abfd = get_section_bfd_owner (section);
  3997.   reader->cu = cu;
  3998.   reader->dwo_file = dwo_file;
  3999.   reader->die_section = section;
  4000.   reader->buffer = section->buffer;
  4001.   reader->buffer_end = section->buffer + section->size;
  4002.   reader->comp_dir = NULL;
  4003. }

  4004. /* Subroutine of init_cutu_and_read_dies to simplify it.
  4005.    Read in the rest of a CU/TU top level DIE from DWO_UNIT.
  4006.    There's just a lot of work to do, and init_cutu_and_read_dies is big enough
  4007.    already.

  4008.    STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
  4009.    from it to the DIE in the DWO.  If NULL we are skipping the stub.
  4010.    STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
  4011.    from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
  4012.    attribute of the referencing CU.  At most one of STUB_COMP_UNIT_DIE and
  4013.    STUB_COMP_DIR may be non-NULL.
  4014.    *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE,*RESULT_HAS_CHILDREN
  4015.    are filled in with the info of the DIE from the DWO file.
  4016.    ABBREV_TABLE_PROVIDED is non-zero if the caller of init_cutu_and_read_dies
  4017.    provided an abbrev table to use.
  4018.    The result is non-zero if a valid (non-dummy) DIE was found.  */

  4019. static int
  4020. read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
  4021.                         struct dwo_unit *dwo_unit,
  4022.                         int abbrev_table_provided,
  4023.                         struct die_info *stub_comp_unit_die,
  4024.                         const char *stub_comp_dir,
  4025.                         struct die_reader_specs *result_reader,
  4026.                         const gdb_byte **result_info_ptr,
  4027.                         struct die_info **result_comp_unit_die,
  4028.                         int *result_has_children)
  4029. {
  4030.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  4031.   struct dwarf2_cu *cu = this_cu->cu;
  4032.   struct dwarf2_section_info *section;
  4033.   bfd *abfd;
  4034.   const gdb_byte *begin_info_ptr, *info_ptr;
  4035.   ULONGEST signature; /* Or dwo_id.  */
  4036.   struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
  4037.   int i,num_extra_attrs;
  4038.   struct dwarf2_section_info *dwo_abbrev_section;
  4039.   struct attribute *attr;
  4040.   struct die_info *comp_unit_die;

  4041.   /* At most one of these may be provided.  */
  4042.   gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);

  4043.   /* These attributes aren't processed until later:
  4044.      DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
  4045.      DW_AT_comp_dir is used now, to find the DWO file, but it is also
  4046.      referenced later.  However, these attributes are found in the stub
  4047.      which we won't have later.  In order to not impose this complication
  4048.      on the rest of the code, we read them here and copy them to the
  4049.      DWO CU/TU die.  */

  4050.   stmt_list = NULL;
  4051.   low_pc = NULL;
  4052.   high_pc = NULL;
  4053.   ranges = NULL;
  4054.   comp_dir = NULL;

  4055.   if (stub_comp_unit_die != NULL)
  4056.     {
  4057.       /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
  4058.          DWO file.  */
  4059.       if (! this_cu->is_debug_types)
  4060.         stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
  4061.       low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
  4062.       high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
  4063.       ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
  4064.       comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);

  4065.       /* There should be a DW_AT_addr_base attribute here (if needed).
  4066.          We need the value before we can process DW_FORM_GNU_addr_index.  */
  4067.       cu->addr_base = 0;
  4068.       attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_addr_base, cu);
  4069.       if (attr)
  4070.         cu->addr_base = DW_UNSND (attr);

  4071.       /* There should be a DW_AT_ranges_base attribute here (if needed).
  4072.          We need the value before we can process DW_AT_ranges.  */
  4073.       cu->ranges_base = 0;
  4074.       attr = dwarf2_attr (stub_comp_unit_die, DW_AT_GNU_ranges_base, cu);
  4075.       if (attr)
  4076.         cu->ranges_base = DW_UNSND (attr);
  4077.     }
  4078.   else if (stub_comp_dir != NULL)
  4079.     {
  4080.       /* Reconstruct the comp_dir attribute to simplify the code below.  */
  4081.       comp_dir = (struct attribute *)
  4082.         obstack_alloc (&cu->comp_unit_obstack, sizeof (*comp_dir));
  4083.       comp_dir->name = DW_AT_comp_dir;
  4084.       comp_dir->form = DW_FORM_string;
  4085.       DW_STRING_IS_CANONICAL (comp_dir) = 0;
  4086.       DW_STRING (comp_dir) = stub_comp_dir;
  4087.     }

  4088.   /* Set up for reading the DWO CU/TU.  */
  4089.   cu->dwo_unit = dwo_unit;
  4090.   section = dwo_unit->section;
  4091.   dwarf2_read_section (objfile, section);
  4092.   abfd = get_section_bfd_owner (section);
  4093.   begin_info_ptr = info_ptr = section->buffer + dwo_unit->offset.sect_off;
  4094.   dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
  4095.   init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file);

  4096.   if (this_cu->is_debug_types)
  4097.     {
  4098.       ULONGEST header_signature;
  4099.       cu_offset type_offset_in_tu;
  4100.       struct signatured_type *sig_type = (struct signatured_type *) this_cu;

  4101.       info_ptr = read_and_check_type_unit_head (&cu->header, section,
  4102.                                                 dwo_abbrev_section,
  4103.                                                 info_ptr,
  4104.                                                 &header_signature,
  4105.                                                 &type_offset_in_tu);
  4106.       /* This is not an assert because it can be caused by bad debug info.  */
  4107.       if (sig_type->signature != header_signature)
  4108.         {
  4109.           error (_("Dwarf Error: signature mismatch %s vs %s while reading"
  4110.                    " TU at offset 0x%x [in module %s]"),
  4111.                  hex_string (sig_type->signature),
  4112.                  hex_string (header_signature),
  4113.                  dwo_unit->offset.sect_off,
  4114.                  bfd_get_filename (abfd));
  4115.         }
  4116.       gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
  4117.       /* For DWOs coming from DWP files, we don't know the CU length
  4118.          nor the type's offset in the TU until now.  */
  4119.       dwo_unit->length = get_cu_length (&cu->header);
  4120.       dwo_unit->type_offset_in_tu = type_offset_in_tu;

  4121.       /* Establish the type offset that can be used to lookup the type.
  4122.          For DWO files, we don't know it until now.  */
  4123.       sig_type->type_offset_in_section.sect_off =
  4124.         dwo_unit->offset.sect_off + dwo_unit->type_offset_in_tu.cu_off;
  4125.     }
  4126.   else
  4127.     {
  4128.       info_ptr = read_and_check_comp_unit_head (&cu->header, section,
  4129.                                                 dwo_abbrev_section,
  4130.                                                 info_ptr, 0);
  4131.       gdb_assert (dwo_unit->offset.sect_off == cu->header.offset.sect_off);
  4132.       /* For DWOs coming from DWP files, we don't know the CU length
  4133.          until now.  */
  4134.       dwo_unit->length = get_cu_length (&cu->header);
  4135.     }

  4136.   /* Replace the CU's original abbrev table with the DWO's.
  4137.      Reminder: We can't read the abbrev table until we've read the header.  */
  4138.   if (abbrev_table_provided)
  4139.     {
  4140.       /* Don't free the provided abbrev table, the caller of
  4141.          init_cutu_and_read_dies owns it.  */
  4142.       dwarf2_read_abbrevs (cu, dwo_abbrev_section);
  4143.       /* Ensure the DWO abbrev table gets freed.  */
  4144.       make_cleanup (dwarf2_free_abbrev_table, cu);
  4145.     }
  4146.   else
  4147.     {
  4148.       dwarf2_free_abbrev_table (cu);
  4149.       dwarf2_read_abbrevs (cu, dwo_abbrev_section);
  4150.       /* Leave any existing abbrev table cleanup as is.  */
  4151.     }

  4152.   /* Read in the die, but leave space to copy over the attributes
  4153.      from the stub.  This has the benefit of simplifying the rest of
  4154.      the code - all the work to maintain the illusion of a single
  4155.      DW_TAG_{compile,type}_unit DIE is done here.  */
  4156.   num_extra_attrs = ((stmt_list != NULL)
  4157.                      + (low_pc != NULL)
  4158.                      + (high_pc != NULL)
  4159.                      + (ranges != NULL)
  4160.                      + (comp_dir != NULL));
  4161.   info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
  4162.                               result_has_children, num_extra_attrs);

  4163.   /* Copy over the attributes from the stub to the DIE we just read in.  */
  4164.   comp_unit_die = *result_comp_unit_die;
  4165.   i = comp_unit_die->num_attrs;
  4166.   if (stmt_list != NULL)
  4167.     comp_unit_die->attrs[i++] = *stmt_list;
  4168.   if (low_pc != NULL)
  4169.     comp_unit_die->attrs[i++] = *low_pc;
  4170.   if (high_pc != NULL)
  4171.     comp_unit_die->attrs[i++] = *high_pc;
  4172.   if (ranges != NULL)
  4173.     comp_unit_die->attrs[i++] = *ranges;
  4174.   if (comp_dir != NULL)
  4175.     comp_unit_die->attrs[i++] = *comp_dir;
  4176.   comp_unit_die->num_attrs += num_extra_attrs;

  4177.   if (dwarf2_die_debug)
  4178.     {
  4179.       fprintf_unfiltered (gdb_stdlog,
  4180.                           "Read die from %s@0x%x of %s:\n",
  4181.                           get_section_name (section),
  4182.                           (unsigned) (begin_info_ptr - section->buffer),
  4183.                           bfd_get_filename (abfd));
  4184.       dump_die (comp_unit_die, dwarf2_die_debug);
  4185.     }

  4186.   /* Save the comp_dir attribute.  If there is no DWP file then we'll read
  4187.      TUs by skipping the stub and going directly to the entry in the DWO file.
  4188.      However, skipping the stub means we won't get DW_AT_comp_dir, so we have
  4189.      to get it via circuitous means.  Blech.  */
  4190.   if (comp_dir != NULL)
  4191.     result_reader->comp_dir = DW_STRING (comp_dir);

  4192.   /* Skip dummy compilation units.  */
  4193.   if (info_ptr >= begin_info_ptr + dwo_unit->length
  4194.       || peek_abbrev_code (abfd, info_ptr) == 0)
  4195.     return 0;

  4196.   *result_info_ptr = info_ptr;
  4197.   return 1;
  4198. }

  4199. /* Subroutine of init_cutu_and_read_dies to simplify it.
  4200.    Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
  4201.    Returns NULL if the specified DWO unit cannot be found.  */

  4202. static struct dwo_unit *
  4203. lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
  4204.                  struct die_info *comp_unit_die)
  4205. {
  4206.   struct dwarf2_cu *cu = this_cu->cu;
  4207.   struct attribute *attr;
  4208.   ULONGEST signature;
  4209.   struct dwo_unit *dwo_unit;
  4210.   const char *comp_dir, *dwo_name;

  4211.   gdb_assert (cu != NULL);

  4212.   /* Yeah, we look dwo_name up again, but it simplifies the code.  */
  4213.   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
  4214.   gdb_assert (attr != NULL);
  4215.   dwo_name = DW_STRING (attr);
  4216.   comp_dir = NULL;
  4217.   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
  4218.   if (attr)
  4219.     comp_dir = DW_STRING (attr);

  4220.   if (this_cu->is_debug_types)
  4221.     {
  4222.       struct signatured_type *sig_type;

  4223.       /* Since this_cu is the first member of struct signatured_type,
  4224.          we can go from a pointer to one to a pointer to the other.  */
  4225.       sig_type = (struct signatured_type *) this_cu;
  4226.       signature = sig_type->signature;
  4227.       dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
  4228.     }
  4229.   else
  4230.     {
  4231.       struct attribute *attr;

  4232.       attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
  4233.       if (! attr)
  4234.         error (_("Dwarf Error: missing dwo_id for dwo_name %s"
  4235.                  " [in module %s]"),
  4236.                dwo_name, objfile_name (this_cu->objfile));
  4237.       signature = DW_UNSND (attr);
  4238.       dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
  4239.                                        signature);
  4240.     }

  4241.   return dwo_unit;
  4242. }

  4243. /* Subroutine of init_cutu_and_read_dies to simplify it.
  4244.    See it for a description of the parameters.
  4245.    Read a TU directly from a DWO file, bypassing the stub.

  4246.    Note: This function could be a little bit simpler if we shared cleanups
  4247.    with our caller, init_cutu_and_read_dies.  That's generally a fragile thing
  4248.    to do, so we keep this function self-contained.  Or we could move this
  4249.    into our caller, but it's complex enough already.  */

  4250. static void
  4251. init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
  4252.                            int use_existing_cu, int keep,
  4253.                            die_reader_func_ftype *die_reader_func,
  4254.                            void *data)
  4255. {
  4256.   struct dwarf2_cu *cu;
  4257.   struct signatured_type *sig_type;
  4258.   struct cleanup *cleanups, *free_cu_cleanup = NULL;
  4259.   struct die_reader_specs reader;
  4260.   const gdb_byte *info_ptr;
  4261.   struct die_info *comp_unit_die;
  4262.   int has_children;

  4263.   /* Verify we can do the following downcast, and that we have the
  4264.      data we need.  */
  4265.   gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
  4266.   sig_type = (struct signatured_type *) this_cu;
  4267.   gdb_assert (sig_type->dwo_unit != NULL);

  4268.   cleanups = make_cleanup (null_cleanup, NULL);

  4269.   if (use_existing_cu && this_cu->cu != NULL)
  4270.     {
  4271.       gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
  4272.       cu = this_cu->cu;
  4273.       /* There's no need to do the rereading_dwo_cu handling that
  4274.          init_cutu_and_read_dies does since we don't read the stub.  */
  4275.     }
  4276.   else
  4277.     {
  4278.       /* If !use_existing_cu, this_cu->cu must be NULL.  */
  4279.       gdb_assert (this_cu->cu == NULL);
  4280.       cu = xmalloc (sizeof (*cu));
  4281.       init_one_comp_unit (cu, this_cu);
  4282.       /* If an error occurs while loading, release our storage.  */
  4283.       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
  4284.     }

  4285.   /* A future optimization, if needed, would be to use an existing
  4286.      abbrev table.  When reading DWOs with skeletonless TUs, all the TUs
  4287.      could share abbrev tables.  */

  4288.   if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
  4289.                               0 /* abbrev_table_provided */,
  4290.                               NULL /* stub_comp_unit_die */,
  4291.                               sig_type->dwo_unit->dwo_file->comp_dir,
  4292.                               &reader, &info_ptr,
  4293.                               &comp_unit_die, &has_children) == 0)
  4294.     {
  4295.       /* Dummy die.  */
  4296.       do_cleanups (cleanups);
  4297.       return;
  4298.     }

  4299.   /* All the "real" work is done here.  */
  4300.   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);

  4301.   /* This duplicates the code in init_cutu_and_read_dies,
  4302.      but the alternative is making the latter more complex.
  4303.      This function is only for the special case of using DWO files directly:
  4304.      no point in overly complicating the general case just to handle this.  */
  4305.   if (free_cu_cleanup != NULL)
  4306.     {
  4307.       if (keep)
  4308.         {
  4309.           /* We've successfully allocated this compilation unit.  Let our
  4310.              caller clean it up when finished with it.  */
  4311.           discard_cleanups (free_cu_cleanup);

  4312.           /* We can only discard free_cu_cleanup and all subsequent cleanups.
  4313.              So we have to manually free the abbrev table.  */
  4314.           dwarf2_free_abbrev_table (cu);

  4315.           /* Link this CU into read_in_chain.  */
  4316.           this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
  4317.           dwarf2_per_objfile->read_in_chain = this_cu;
  4318.         }
  4319.       else
  4320.         do_cleanups (free_cu_cleanup);
  4321.     }

  4322.   do_cleanups (cleanups);
  4323. }

  4324. /* Initialize a CU (or TU) and read its DIEs.
  4325.    If the CU defers to a DWO file, read the DWO file as well.

  4326.    ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
  4327.    Otherwise the table specified in the comp unit header is read in and used.
  4328.    This is an optimization for when we already have the abbrev table.

  4329.    If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
  4330.    Otherwise, a new CU is allocated with xmalloc.

  4331.    If KEEP is non-zero, then if we allocated a dwarf2_cu we add it to
  4332.    read_in_chain.  Otherwise the dwarf2_cu data is freed at the end.

  4333.    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
  4334.    linker) then DIE_READER_FUNC will not get called.  */

  4335. static void
  4336. init_cutu_and_read_dies (struct dwarf2_per_cu_data *this_cu,
  4337.                          struct abbrev_table *abbrev_table,
  4338.                          int use_existing_cu, int keep,
  4339.                          die_reader_func_ftype *die_reader_func,
  4340.                          void *data)
  4341. {
  4342.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  4343.   struct dwarf2_section_info *section = this_cu->section;
  4344.   bfd *abfd = get_section_bfd_owner (section);
  4345.   struct dwarf2_cu *cu;
  4346.   const gdb_byte *begin_info_ptr, *info_ptr;
  4347.   struct die_reader_specs reader;
  4348.   struct die_info *comp_unit_die;
  4349.   int has_children;
  4350.   struct attribute *attr;
  4351.   struct cleanup *cleanups, *free_cu_cleanup = NULL;
  4352.   struct signatured_type *sig_type = NULL;
  4353.   struct dwarf2_section_info *abbrev_section;
  4354.   /* Non-zero if CU currently points to a DWO file and we need to
  4355.      reread it.  When this happens we need to reread the skeleton die
  4356.      before we can reread the DWO file (this only applies to CUs, not TUs).  */
  4357.   int rereading_dwo_cu = 0;

  4358.   if (dwarf2_die_debug)
  4359.     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
  4360.                         this_cu->is_debug_types ? "type" : "comp",
  4361.                         this_cu->offset.sect_off);

  4362.   if (use_existing_cu)
  4363.     gdb_assert (keep);

  4364.   /* If we're reading a TU directly from a DWO file, including a virtual DWO
  4365.      file (instead of going through the stub), short-circuit all of this.  */
  4366.   if (this_cu->reading_dwo_directly)
  4367.     {
  4368.       /* Narrow down the scope of possibilities to have to understand.  */
  4369.       gdb_assert (this_cu->is_debug_types);
  4370.       gdb_assert (abbrev_table == NULL);
  4371.       init_tu_and_read_dwo_dies (this_cu, use_existing_cu, keep,
  4372.                                  die_reader_func, data);
  4373.       return;
  4374.     }

  4375.   cleanups = make_cleanup (null_cleanup, NULL);

  4376.   /* This is cheap if the section is already read in.  */
  4377.   dwarf2_read_section (objfile, section);

  4378.   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;

  4379.   abbrev_section = get_abbrev_section_for_cu (this_cu);

  4380.   if (use_existing_cu && this_cu->cu != NULL)
  4381.     {
  4382.       cu = this_cu->cu;
  4383.       /* If this CU is from a DWO file we need to start over, we need to
  4384.          refetch the attributes from the skeleton CU.
  4385.          This could be optimized by retrieving those attributes from when we
  4386.          were here the first time: the previous comp_unit_die was stored in
  4387.          comp_unit_obstack.  But there's no data yet that we need this
  4388.          optimization.  */
  4389.       if (cu->dwo_unit != NULL)
  4390.         rereading_dwo_cu = 1;
  4391.     }
  4392.   else
  4393.     {
  4394.       /* If !use_existing_cu, this_cu->cu must be NULL.  */
  4395.       gdb_assert (this_cu->cu == NULL);
  4396.       cu = xmalloc (sizeof (*cu));
  4397.       init_one_comp_unit (cu, this_cu);
  4398.       /* If an error occurs while loading, release our storage.  */
  4399.       free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
  4400.     }

  4401.   /* Get the header.  */
  4402.   if (cu->header.first_die_offset.cu_off != 0 && ! rereading_dwo_cu)
  4403.     {
  4404.       /* We already have the header, there's no need to read it in again.  */
  4405.       info_ptr += cu->header.first_die_offset.cu_off;
  4406.     }
  4407.   else
  4408.     {
  4409.       if (this_cu->is_debug_types)
  4410.         {
  4411.           ULONGEST signature;
  4412.           cu_offset type_offset_in_tu;

  4413.           info_ptr = read_and_check_type_unit_head (&cu->header, section,
  4414.                                                     abbrev_section, info_ptr,
  4415.                                                     &signature,
  4416.                                                     &type_offset_in_tu);

  4417.           /* Since per_cu is the first member of struct signatured_type,
  4418.              we can go from a pointer to one to a pointer to the other.  */
  4419.           sig_type = (struct signatured_type *) this_cu;
  4420.           gdb_assert (sig_type->signature == signature);
  4421.           gdb_assert (sig_type->type_offset_in_tu.cu_off
  4422.                       == type_offset_in_tu.cu_off);
  4423.           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);

  4424.           /* LENGTH has not been set yet for type units if we're
  4425.              using .gdb_index.  */
  4426.           this_cu->length = get_cu_length (&cu->header);

  4427.           /* Establish the type offset that can be used to lookup the type.  */
  4428.           sig_type->type_offset_in_section.sect_off =
  4429.             this_cu->offset.sect_off + sig_type->type_offset_in_tu.cu_off;
  4430.         }
  4431.       else
  4432.         {
  4433.           info_ptr = read_and_check_comp_unit_head (&cu->header, section,
  4434.                                                     abbrev_section,
  4435.                                                     info_ptr, 0);

  4436.           gdb_assert (this_cu->offset.sect_off == cu->header.offset.sect_off);
  4437.           gdb_assert (this_cu->length == get_cu_length (&cu->header));
  4438.         }
  4439.     }

  4440.   /* Skip dummy compilation units.  */
  4441.   if (info_ptr >= begin_info_ptr + this_cu->length
  4442.       || peek_abbrev_code (abfd, info_ptr) == 0)
  4443.     {
  4444.       do_cleanups (cleanups);
  4445.       return;
  4446.     }

  4447.   /* If we don't have them yet, read the abbrevs for this compilation unit.
  4448.      And if we need to read them now, make sure they're freed when we're
  4449.      done.  Note that it's important that if the CU had an abbrev table
  4450.      on entry we don't free it when we're done: Somewhere up the call stack
  4451.      it may be in use.  */
  4452.   if (abbrev_table != NULL)
  4453.     {
  4454.       gdb_assert (cu->abbrev_table == NULL);
  4455.       gdb_assert (cu->header.abbrev_offset.sect_off
  4456.                   == abbrev_table->offset.sect_off);
  4457.       cu->abbrev_table = abbrev_table;
  4458.     }
  4459.   else if (cu->abbrev_table == NULL)
  4460.     {
  4461.       dwarf2_read_abbrevs (cu, abbrev_section);
  4462.       make_cleanup (dwarf2_free_abbrev_table, cu);
  4463.     }
  4464.   else if (rereading_dwo_cu)
  4465.     {
  4466.       dwarf2_free_abbrev_table (cu);
  4467.       dwarf2_read_abbrevs (cu, abbrev_section);
  4468.     }

  4469.   /* Read the top level CU/TU die.  */
  4470.   init_cu_die_reader (&reader, cu, section, NULL);
  4471.   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);

  4472.   /* If we are in a DWO stub, process it and then read in the "real" CU/TU
  4473.      from the DWO file.
  4474.      Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
  4475.      DWO CU, that this test will fail (the attribute will not be present).  */
  4476.   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_name, cu);
  4477.   if (attr)
  4478.     {
  4479.       struct dwo_unit *dwo_unit;
  4480.       struct die_info *dwo_comp_unit_die;

  4481.       if (has_children)
  4482.         {
  4483.           complaint (&symfile_complaints,
  4484.                      _("compilation unit with DW_AT_GNU_dwo_name"
  4485.                        " has children (offset 0x%x) [in module %s]"),
  4486.                      this_cu->offset.sect_off, bfd_get_filename (abfd));
  4487.         }
  4488.       dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die);
  4489.       if (dwo_unit != NULL)
  4490.         {
  4491.           if (read_cutu_die_from_dwo (this_cu, dwo_unit,
  4492.                                       abbrev_table != NULL,
  4493.                                       comp_unit_die, NULL,
  4494.                                       &reader, &info_ptr,
  4495.                                       &dwo_comp_unit_die, &has_children) == 0)
  4496.             {
  4497.               /* Dummy die.  */
  4498.               do_cleanups (cleanups);
  4499.               return;
  4500.             }
  4501.           comp_unit_die = dwo_comp_unit_die;
  4502.         }
  4503.       else
  4504.         {
  4505.           /* Yikes, we couldn't find the rest of the DIE, we only have
  4506.              the stubA complaint has already been logged.  There's
  4507.              not much more we can do except pass on the stub DIE to
  4508.              die_reader_func.  We don't want to throw an error on bad
  4509.              debug info.  */
  4510.         }
  4511.     }

  4512.   /* All of the above is setup for this call.  Yikes.  */
  4513.   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);

  4514.   /* Done, clean up.  */
  4515.   if (free_cu_cleanup != NULL)
  4516.     {
  4517.       if (keep)
  4518.         {
  4519.           /* We've successfully allocated this compilation unit.  Let our
  4520.              caller clean it up when finished with it.  */
  4521.           discard_cleanups (free_cu_cleanup);

  4522.           /* We can only discard free_cu_cleanup and all subsequent cleanups.
  4523.              So we have to manually free the abbrev table.  */
  4524.           dwarf2_free_abbrev_table (cu);

  4525.           /* Link this CU into read_in_chain.  */
  4526.           this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
  4527.           dwarf2_per_objfile->read_in_chain = this_cu;
  4528.         }
  4529.       else
  4530.         do_cleanups (free_cu_cleanup);
  4531.     }

  4532.   do_cleanups (cleanups);
  4533. }

  4534. /* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name if present.
  4535.    DWO_FILE, if non-NULL, is the DWO file to read (the caller is assumed
  4536.    to have already done the lookup to find the DWO file).

  4537.    The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
  4538.    THIS_CU->is_debug_types, but nothing else.

  4539.    We fill in THIS_CU->length.

  4540.    WARNING: If THIS_CU is a "dummy CU" (used as filler by the incremental
  4541.    linker) then DIE_READER_FUNC will not get called.

  4542.    THIS_CU->cu is always freed when done.
  4543.    This is done in order to not leave THIS_CU->cu in a state where we have
  4544.    to care whether it refers to the "main" CU or the DWO CU.  */

  4545. static void
  4546. init_cutu_and_read_dies_no_follow (struct dwarf2_per_cu_data *this_cu,
  4547.                                    struct dwo_file *dwo_file,
  4548.                                    die_reader_func_ftype *die_reader_func,
  4549.                                    void *data)
  4550. {
  4551.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  4552.   struct dwarf2_section_info *section = this_cu->section;
  4553.   bfd *abfd = get_section_bfd_owner (section);
  4554.   struct dwarf2_section_info *abbrev_section;
  4555.   struct dwarf2_cu cu;
  4556.   const gdb_byte *begin_info_ptr, *info_ptr;
  4557.   struct die_reader_specs reader;
  4558.   struct cleanup *cleanups;
  4559.   struct die_info *comp_unit_die;
  4560.   int has_children;

  4561.   if (dwarf2_die_debug)
  4562.     fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset 0x%x\n",
  4563.                         this_cu->is_debug_types ? "type" : "comp",
  4564.                         this_cu->offset.sect_off);

  4565.   gdb_assert (this_cu->cu == NULL);

  4566.   abbrev_section = (dwo_file != NULL
  4567.                     ? &dwo_file->sections.abbrev
  4568.                     : get_abbrev_section_for_cu (this_cu));

  4569.   /* This is cheap if the section is already read in.  */
  4570.   dwarf2_read_section (objfile, section);

  4571.   init_one_comp_unit (&cu, this_cu);

  4572.   cleanups = make_cleanup (free_stack_comp_unit, &cu);

  4573.   begin_info_ptr = info_ptr = section->buffer + this_cu->offset.sect_off;
  4574.   info_ptr = read_and_check_comp_unit_head (&cu.header, section,
  4575.                                             abbrev_section, info_ptr,
  4576.                                             this_cu->is_debug_types);

  4577.   this_cu->length = get_cu_length (&cu.header);

  4578.   /* Skip dummy compilation units.  */
  4579.   if (info_ptr >= begin_info_ptr + this_cu->length
  4580.       || peek_abbrev_code (abfd, info_ptr) == 0)
  4581.     {
  4582.       do_cleanups (cleanups);
  4583.       return;
  4584.     }

  4585.   dwarf2_read_abbrevs (&cu, abbrev_section);
  4586.   make_cleanup (dwarf2_free_abbrev_table, &cu);

  4587.   init_cu_die_reader (&reader, &cu, section, dwo_file);
  4588.   info_ptr = read_full_die (&reader, &comp_unit_die, info_ptr, &has_children);

  4589.   die_reader_func (&reader, info_ptr, comp_unit_die, has_children, data);

  4590.   do_cleanups (cleanups);
  4591. }

  4592. /* Read a CU/TU, except that this does not look for DW_AT_GNU_dwo_name and
  4593.    does not lookup the specified DWO file.
  4594.    This cannot be used to read DWO files.

  4595.    THIS_CU->cu is always freed when done.
  4596.    This is done in order to not leave THIS_CU->cu in a state where we have
  4597.    to care whether it refers to the "main" CU or the DWO CU.
  4598.    We can revisit this if the data shows there's a performance issue.  */

  4599. static void
  4600. init_cutu_and_read_dies_simple (struct dwarf2_per_cu_data *this_cu,
  4601.                                 die_reader_func_ftype *die_reader_func,
  4602.                                 void *data)
  4603. {
  4604.   init_cutu_and_read_dies_no_follow (this_cu, NULL, die_reader_func, data);
  4605. }

  4606. /* Type Unit Groups.

  4607.    Type Unit Groups are a way to collapse the set of all TUs (type units) into
  4608.    a more manageable set.  The grouping is done by DW_AT_stmt_list entry
  4609.    so that all types coming from the same compilation (.o file) are grouped
  4610.    together.  A future step could be to put the types in the same symtab as
  4611.    the CU the types ultimately came from.  */

  4612. static hashval_t
  4613. hash_type_unit_group (const void *item)
  4614. {
  4615.   const struct type_unit_group *tu_group = item;

  4616.   return hash_stmt_list_entry (&tu_group->hash);
  4617. }

  4618. static int
  4619. eq_type_unit_group (const void *item_lhs, const void *item_rhs)
  4620. {
  4621.   const struct type_unit_group *lhs = item_lhs;
  4622.   const struct type_unit_group *rhs = item_rhs;

  4623.   return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
  4624. }

  4625. /* Allocate a hash table for type unit groups.  */

  4626. static htab_t
  4627. allocate_type_unit_groups_table (void)
  4628. {
  4629.   return htab_create_alloc_ex (3,
  4630.                                hash_type_unit_group,
  4631.                                eq_type_unit_group,
  4632.                                NULL,
  4633.                                &dwarf2_per_objfile->objfile->objfile_obstack,
  4634.                                hashtab_obstack_allocate,
  4635.                                dummy_obstack_deallocate);
  4636. }

  4637. /* Type units that don't have DW_AT_stmt_list are grouped into their own
  4638.    partial symtabs.  We combine several TUs per psymtab to not let the size
  4639.    of any one psymtab grow too big.  */
  4640. #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
  4641. #define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10

  4642. /* Helper routine for get_type_unit_group.
  4643.    Create the type_unit_group object used to hold one or more TUs.  */

  4644. static struct type_unit_group *
  4645. create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
  4646. {
  4647.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  4648.   struct dwarf2_per_cu_data *per_cu;
  4649.   struct type_unit_group *tu_group;

  4650.   tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  4651.                              struct type_unit_group);
  4652.   per_cu = &tu_group->per_cu;
  4653.   per_cu->objfile = objfile;

  4654.   if (dwarf2_per_objfile->using_index)
  4655.     {
  4656.       per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
  4657.                                         struct dwarf2_per_cu_quick_data);
  4658.     }
  4659.   else
  4660.     {
  4661.       unsigned int line_offset = line_offset_struct.sect_off;
  4662.       struct partial_symtab *pst;
  4663.       char *name;

  4664.       /* Give the symtab a useful name for debug purposes.  */
  4665.       if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
  4666.         name = xstrprintf ("<type_units_%d>",
  4667.                            (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
  4668.       else
  4669.         name = xstrprintf ("<type_units_at_0x%x>", line_offset);

  4670.       pst = create_partial_symtab (per_cu, name);
  4671.       pst->anonymous = 1;

  4672.       xfree (name);
  4673.     }

  4674.   tu_group->hash.dwo_unit = cu->dwo_unit;
  4675.   tu_group->hash.line_offset = line_offset_struct;

  4676.   return tu_group;
  4677. }

  4678. /* Look up the type_unit_group for type unit CU, and create it if necessary.
  4679.    STMT_LIST is a DW_AT_stmt_list attribute.  */

  4680. static struct type_unit_group *
  4681. get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
  4682. {
  4683.   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
  4684.   struct type_unit_group *tu_group;
  4685.   void **slot;
  4686.   unsigned int line_offset;
  4687.   struct type_unit_group type_unit_group_for_lookup;

  4688.   if (dwarf2_per_objfile->type_unit_groups == NULL)
  4689.     {
  4690.       dwarf2_per_objfile->type_unit_groups =
  4691.         allocate_type_unit_groups_table ();
  4692.     }

  4693.   /* Do we need to create a new group, or can we use an existing one?  */

  4694.   if (stmt_list)
  4695.     {
  4696.       line_offset = DW_UNSND (stmt_list);
  4697.       ++tu_stats->nr_symtab_sharers;
  4698.     }
  4699.   else
  4700.     {
  4701.       /* Ugh, no stmt_list.  Rare, but we have to handle it.
  4702.          We can do various things here like create one group per TU or
  4703.          spread them over multiple groups to split up the expansion work.
  4704.          To avoid worst case scenarios (too many groups or too large groups)
  4705.          we, umm, group them in bunches.  */
  4706.       line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
  4707.                      | (tu_stats->nr_stmt_less_type_units
  4708.                         / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
  4709.       ++tu_stats->nr_stmt_less_type_units;
  4710.     }

  4711.   type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
  4712.   type_unit_group_for_lookup.hash.line_offset.sect_off = line_offset;
  4713.   slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups,
  4714.                          &type_unit_group_for_lookup, INSERT);
  4715.   if (*slot != NULL)
  4716.     {
  4717.       tu_group = *slot;
  4718.       gdb_assert (tu_group != NULL);
  4719.     }
  4720.   else
  4721.     {
  4722.       sect_offset line_offset_struct;

  4723.       line_offset_struct.sect_off = line_offset;
  4724.       tu_group = create_type_unit_group (cu, line_offset_struct);
  4725.       *slot = tu_group;
  4726.       ++tu_stats->nr_symtabs;
  4727.     }

  4728.   return tu_group;
  4729. }

  4730. /* Partial symbol tables.  */

  4731. /* Create a psymtab named NAME and assign it to PER_CU.

  4732.    The caller must fill in the following details:
  4733.    dirname, textlow, texthigh.  */

  4734. static struct partial_symtab *
  4735. create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
  4736. {
  4737.   struct objfile *objfile = per_cu->objfile;
  4738.   struct partial_symtab *pst;

  4739.   pst = start_psymtab_common (objfile, objfile->section_offsets,
  4740.                               name, 0,
  4741.                               objfile->global_psymbols.next,
  4742.                               objfile->static_psymbols.next);

  4743.   pst->psymtabs_addrmap_supported = 1;

  4744.   /* This is the glue that links PST into GDB's symbol API.  */
  4745.   pst->read_symtab_private = per_cu;
  4746.   pst->read_symtab = dwarf2_read_symtab;
  4747.   per_cu->v.psymtab = pst;

  4748.   return pst;
  4749. }

  4750. /* The DATA object passed to process_psymtab_comp_unit_reader has this
  4751.    type.  */

  4752. struct process_psymtab_comp_unit_data
  4753. {
  4754.   /* True if we are reading a DW_TAG_partial_unit.  */

  4755.   int want_partial_unit;

  4756.   /* The "pretend" language that is used if the CU doesn't declare a
  4757.      language.  */

  4758.   enum language pretend_language;
  4759. };

  4760. /* die_reader_func for process_psymtab_comp_unit.  */

  4761. static void
  4762. process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
  4763.                                   const gdb_byte *info_ptr,
  4764.                                   struct die_info *comp_unit_die,
  4765.                                   int has_children,
  4766.                                   void *data)
  4767. {
  4768.   struct dwarf2_cu *cu = reader->cu;
  4769.   struct objfile *objfile = cu->objfile;
  4770.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  4771.   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
  4772.   struct attribute *attr;
  4773.   CORE_ADDR baseaddr;
  4774.   CORE_ADDR best_lowpc = 0, best_highpc = 0;
  4775.   struct partial_symtab *pst;
  4776.   int has_pc_info;
  4777.   const char *filename;
  4778.   struct process_psymtab_comp_unit_data *info = data;

  4779.   if (comp_unit_die->tag == DW_TAG_partial_unit && !info->want_partial_unit)
  4780.     return;

  4781.   gdb_assert (! per_cu->is_debug_types);

  4782.   prepare_one_comp_unit (cu, comp_unit_die, info->pretend_language);

  4783.   cu->list_in_scope = &file_symbols;

  4784.   /* Allocate a new partial symbol table structure.  */
  4785.   attr = dwarf2_attr (comp_unit_die, DW_AT_name, cu);
  4786.   if (attr == NULL || !DW_STRING (attr))
  4787.     filename = "";
  4788.   else
  4789.     filename = DW_STRING (attr);

  4790.   pst = create_partial_symtab (per_cu, filename);

  4791.   /* This must be done before calling dwarf2_build_include_psymtabs.  */
  4792.   attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, cu);
  4793.   if (attr != NULL)
  4794.     pst->dirname = DW_STRING (attr);

  4795.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  4796.   dwarf2_find_base_address (comp_unit_die, cu);

  4797.   /* Possibly set the default values of LOWPC and HIGHPC from
  4798.      `DW_AT_ranges'.  */
  4799.   has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
  4800.                                       &best_highpc, cu, pst);
  4801.   if (has_pc_info == 1 && best_lowpc < best_highpc)
  4802.     /* Store the contiguous range if it is not empty; it can be empty for
  4803.        CUs with no code.  */
  4804.     addrmap_set_empty (objfile->psymtabs_addrmap,
  4805.                        gdbarch_adjust_dwarf2_addr (gdbarch,
  4806.                                                    best_lowpc + baseaddr),
  4807.                        gdbarch_adjust_dwarf2_addr (gdbarch,
  4808.                                                    best_highpc + baseaddr) - 1,
  4809.                        pst);

  4810.   /* Check if comp unit has_children.
  4811.      If so, read the rest of the partial symbols from this comp unit.
  4812.      If not, there's no more debug_info for this comp unit.  */
  4813.   if (has_children)
  4814.     {
  4815.       struct partial_die_info *first_die;
  4816.       CORE_ADDR lowpc, highpc;

  4817.       lowpc = ((CORE_ADDR) -1);
  4818.       highpc = ((CORE_ADDR) 0);

  4819.       first_die = load_partial_dies (reader, info_ptr, 1);

  4820.       scan_partial_symbols (first_die, &lowpc, &highpc,
  4821.                             ! has_pc_info, cu);

  4822.       /* If we didn't find a lowpc, set it to highpc to avoid
  4823.          complaints from `maint check'.         */
  4824.       if (lowpc == ((CORE_ADDR) -1))
  4825.         lowpc = highpc;

  4826.       /* If the compilation unit didn't have an explicit address range,
  4827.          then use the information extracted from its child dies.  */
  4828.       if (! has_pc_info)
  4829.         {
  4830.           best_lowpc = lowpc;
  4831.           best_highpc = highpc;
  4832.         }
  4833.     }
  4834.   pst->textlow = gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr);
  4835.   pst->texthigh = gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr);

  4836.   pst->n_global_syms = objfile->global_psymbols.next -
  4837.     (objfile->global_psymbols.list + pst->globals_offset);
  4838.   pst->n_static_syms = objfile->static_psymbols.next -
  4839.     (objfile->static_psymbols.list + pst->statics_offset);
  4840.   sort_pst_symbols (objfile, pst);

  4841.   if (!VEC_empty (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs))
  4842.     {
  4843.       int i;
  4844.       int len = VEC_length (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
  4845.       struct dwarf2_per_cu_data *iter;

  4846.       /* Fill in 'dependencies' here; we fill in 'users' in a
  4847.          post-pass.  */
  4848.       pst->number_of_dependencies = len;
  4849.       pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
  4850.                                          len * sizeof (struct symtab *));
  4851.       for (i = 0;
  4852.            VEC_iterate (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
  4853.                         i, iter);
  4854.            ++i)
  4855.         pst->dependencies[i] = iter->v.psymtab;

  4856.       VEC_free (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs);
  4857.     }

  4858.   /* Get the list of files included in the current compilation unit,
  4859.      and build a psymtab for each of them.  */
  4860.   dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);

  4861.   if (dwarf2_read_debug)
  4862.     {
  4863.       struct gdbarch *gdbarch = get_objfile_arch (objfile);

  4864.       fprintf_unfiltered (gdb_stdlog,
  4865.                           "Psymtab for %s unit @0x%x: %s - %s"
  4866.                           ", %d global, %d static syms\n",
  4867.                           per_cu->is_debug_types ? "type" : "comp",
  4868.                           per_cu->offset.sect_off,
  4869.                           paddress (gdbarch, pst->textlow),
  4870.                           paddress (gdbarch, pst->texthigh),
  4871.                           pst->n_global_syms, pst->n_static_syms);
  4872.     }
  4873. }

  4874. /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
  4875.    Process compilation unit THIS_CU for a psymtab.  */

  4876. static void
  4877. process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
  4878.                            int want_partial_unit,
  4879.                            enum language pretend_language)
  4880. {
  4881.   struct process_psymtab_comp_unit_data info;

  4882.   /* If this compilation unit was already read in, free the
  4883.      cached copy in order to read it in again.        This is
  4884.      necessary because we skipped some symbols when we first
  4885.      read in the compilation unit (see load_partial_dies).
  4886.      This problem could be avoided, but the benefit is unclear.  */
  4887.   if (this_cu->cu != NULL)
  4888.     free_one_cached_comp_unit (this_cu);

  4889.   gdb_assert (! this_cu->is_debug_types);
  4890.   info.want_partial_unit = want_partial_unit;
  4891.   info.pretend_language = pretend_language;
  4892.   init_cutu_and_read_dies (this_cu, NULL, 0, 0,
  4893.                            process_psymtab_comp_unit_reader,
  4894.                            &info);

  4895.   /* Age out any secondary CUs.  */
  4896.   age_cached_comp_units ();
  4897. }

  4898. /* Reader function for build_type_psymtabs.  */

  4899. static void
  4900. build_type_psymtabs_reader (const struct die_reader_specs *reader,
  4901.                             const gdb_byte *info_ptr,
  4902.                             struct die_info *type_unit_die,
  4903.                             int has_children,
  4904.                             void *data)
  4905. {
  4906.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  4907.   struct dwarf2_cu *cu = reader->cu;
  4908.   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
  4909.   struct signatured_type *sig_type;
  4910.   struct type_unit_group *tu_group;
  4911.   struct attribute *attr;
  4912.   struct partial_die_info *first_die;
  4913.   CORE_ADDR lowpc, highpc;
  4914.   struct partial_symtab *pst;

  4915.   gdb_assert (data == NULL);
  4916.   gdb_assert (per_cu->is_debug_types);
  4917.   sig_type = (struct signatured_type *) per_cu;

  4918.   if (! has_children)
  4919.     return;

  4920.   attr = dwarf2_attr_no_follow (type_unit_die, DW_AT_stmt_list);
  4921.   tu_group = get_type_unit_group (cu, attr);

  4922.   VEC_safe_push (sig_type_ptr, tu_group->tus, sig_type);

  4923.   prepare_one_comp_unit (cu, type_unit_die, language_minimal);
  4924.   cu->list_in_scope = &file_symbols;
  4925.   pst = create_partial_symtab (per_cu, "");
  4926.   pst->anonymous = 1;

  4927.   first_die = load_partial_dies (reader, info_ptr, 1);

  4928.   lowpc = (CORE_ADDR) -1;
  4929.   highpc = (CORE_ADDR) 0;
  4930.   scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);

  4931.   pst->n_global_syms = objfile->global_psymbols.next -
  4932.     (objfile->global_psymbols.list + pst->globals_offset);
  4933.   pst->n_static_syms = objfile->static_psymbols.next -
  4934.     (objfile->static_psymbols.list + pst->statics_offset);
  4935.   sort_pst_symbols (objfile, pst);
  4936. }

  4937. /* Struct used to sort TUs by their abbreviation table offset.  */

  4938. struct tu_abbrev_offset
  4939. {
  4940.   struct signatured_type *sig_type;
  4941.   sect_offset abbrev_offset;
  4942. };

  4943. /* Helper routine for build_type_psymtabs_1, passed to qsort.  */

  4944. static int
  4945. sort_tu_by_abbrev_offset (const void *ap, const void *bp)
  4946. {
  4947.   const struct tu_abbrev_offset * const *a = ap;
  4948.   const struct tu_abbrev_offset * const *b = bp;
  4949.   unsigned int aoff = (*a)->abbrev_offset.sect_off;
  4950.   unsigned int boff = (*b)->abbrev_offset.sect_off;

  4951.   return (aoff > boff) - (aoff < boff);
  4952. }

  4953. /* Efficiently read all the type units.
  4954.    This does the bulk of the work for build_type_psymtabs.

  4955.    The efficiency is because we sort TUs by the abbrev table they use and
  4956.    only read each abbrev table once.  In one program there are 200K TUs
  4957.    sharing 8K abbrev tables.

  4958.    The main purpose of this function is to support building the
  4959.    dwarf2_per_objfile->type_unit_groups table.
  4960.    TUs typically share the DW_AT_stmt_list of the CU they came from, so we
  4961.    can collapse the search space by grouping them by stmt_list.
  4962.    The savings can be significant, in the same program from above the 200K TUs
  4963.    share 8K stmt_list tables.

  4964.    FUNC is expected to call get_type_unit_group, which will create the
  4965.    struct type_unit_group if necessary and add it to
  4966.    dwarf2_per_objfile->type_unit_groups.  */

  4967. static void
  4968. build_type_psymtabs_1 (void)
  4969. {
  4970.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  4971.   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
  4972.   struct cleanup *cleanups;
  4973.   struct abbrev_table *abbrev_table;
  4974.   sect_offset abbrev_offset;
  4975.   struct tu_abbrev_offset *sorted_by_abbrev;
  4976.   struct type_unit_group **iter;
  4977.   int i;

  4978.   /* It's up to the caller to not call us multiple times.  */
  4979.   gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);

  4980.   if (dwarf2_per_objfile->n_type_units == 0)
  4981.     return;

  4982.   /* TUs typically share abbrev tables, and there can be way more TUs than
  4983.      abbrev tables.  Sort by abbrev table to reduce the number of times we
  4984.      read each abbrev table in.
  4985.      Alternatives are to punt or to maintain a cache of abbrev tables.
  4986.      This is simpler and efficient enough for now.

  4987.      Later we group TUs by their DW_AT_stmt_list value (as this defines the
  4988.      symtab to use).  Typically TUs with the same abbrev offset have the same
  4989.      stmt_list value too so in practice this should work well.

  4990.      The basic algorithm here is:

  4991.       sort TUs by abbrev table
  4992.       for each TU with same abbrev table:
  4993.         read abbrev table if first user
  4994.         read TU top level DIE
  4995.           [IWBN if DWO skeletons had DW_AT_stmt_list]
  4996.         call FUNC  */

  4997.   if (dwarf2_read_debug)
  4998.     fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");

  4999.   /* Sort in a separate table to maintain the order of all_type_units
  5000.      for .gdb_index: TU indices directly index all_type_units.  */
  5001.   sorted_by_abbrev = XNEWVEC (struct tu_abbrev_offset,
  5002.                               dwarf2_per_objfile->n_type_units);
  5003.   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
  5004.     {
  5005.       struct signatured_type *sig_type = dwarf2_per_objfile->all_type_units[i];

  5006.       sorted_by_abbrev[i].sig_type = sig_type;
  5007.       sorted_by_abbrev[i].abbrev_offset =
  5008.         read_abbrev_offset (sig_type->per_cu.section,
  5009.                             sig_type->per_cu.offset);
  5010.     }
  5011.   cleanups = make_cleanup (xfree, sorted_by_abbrev);
  5012.   qsort (sorted_by_abbrev, dwarf2_per_objfile->n_type_units,
  5013.          sizeof (struct tu_abbrev_offset), sort_tu_by_abbrev_offset);

  5014.   abbrev_offset.sect_off = ~(unsigned) 0;
  5015.   abbrev_table = NULL;
  5016.   make_cleanup (abbrev_table_free_cleanup, &abbrev_table);

  5017.   for (i = 0; i < dwarf2_per_objfile->n_type_units; ++i)
  5018.     {
  5019.       const struct tu_abbrev_offset *tu = &sorted_by_abbrev[i];

  5020.       /* Switch to the next abbrev table if necessary.  */
  5021.       if (abbrev_table == NULL
  5022.           || tu->abbrev_offset.sect_off != abbrev_offset.sect_off)
  5023.         {
  5024.           if (abbrev_table != NULL)
  5025.             {
  5026.               abbrev_table_free (abbrev_table);
  5027.               /* Reset to NULL in case abbrev_table_read_table throws
  5028.                  an error: abbrev_table_free_cleanup will get called.  */
  5029.               abbrev_table = NULL;
  5030.             }
  5031.           abbrev_offset = tu->abbrev_offset;
  5032.           abbrev_table =
  5033.             abbrev_table_read_table (&dwarf2_per_objfile->abbrev,
  5034.                                      abbrev_offset);
  5035.           ++tu_stats->nr_uniq_abbrev_tables;
  5036.         }

  5037.       init_cutu_and_read_dies (&tu->sig_type->per_cu, abbrev_table, 0, 0,
  5038.                                build_type_psymtabs_reader, NULL);
  5039.     }

  5040.   do_cleanups (cleanups);
  5041. }

  5042. /* Print collected type unit statistics.  */

  5043. static void
  5044. print_tu_stats (void)
  5045. {
  5046.   struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;

  5047.   fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
  5048.   fprintf_unfiltered (gdb_stdlog, %d TUs\n",
  5049.                       dwarf2_per_objfile->n_type_units);
  5050.   fprintf_unfiltered (gdb_stdlog, %d uniq abbrev tables\n",
  5051.                       tu_stats->nr_uniq_abbrev_tables);
  5052.   fprintf_unfiltered (gdb_stdlog, %d symtabs from stmt_list entries\n",
  5053.                       tu_stats->nr_symtabs);
  5054.   fprintf_unfiltered (gdb_stdlog, %d symtab sharers\n",
  5055.                       tu_stats->nr_symtab_sharers);
  5056.   fprintf_unfiltered (gdb_stdlog, %d type units without a stmt_list\n",
  5057.                       tu_stats->nr_stmt_less_type_units);
  5058.   fprintf_unfiltered (gdb_stdlog, %d all_type_units reallocs\n",
  5059.                       tu_stats->nr_all_type_units_reallocs);
  5060. }

  5061. /* Traversal function for build_type_psymtabs.  */

  5062. static int
  5063. build_type_psymtab_dependencies (void **slot, void *info)
  5064. {
  5065.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  5066.   struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
  5067.   struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
  5068.   struct partial_symtab *pst = per_cu->v.psymtab;
  5069.   int len = VEC_length (sig_type_ptr, tu_group->tus);
  5070.   struct signatured_type *iter;
  5071.   int i;

  5072.   gdb_assert (len > 0);
  5073.   gdb_assert (IS_TYPE_UNIT_GROUP (per_cu));

  5074.   pst->number_of_dependencies = len;
  5075.   pst->dependencies = obstack_alloc (&objfile->objfile_obstack,
  5076.                                      len * sizeof (struct psymtab *));
  5077.   for (i = 0;
  5078.        VEC_iterate (sig_type_ptr, tu_group->tus, i, iter);
  5079.        ++i)
  5080.     {
  5081.       gdb_assert (iter->per_cu.is_debug_types);
  5082.       pst->dependencies[i] = iter->per_cu.v.psymtab;
  5083.       iter->type_unit_group = tu_group;
  5084.     }

  5085.   VEC_free (sig_type_ptr, tu_group->tus);

  5086.   return 1;
  5087. }

  5088. /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
  5089.    Build partial symbol tables for the .debug_types comp-units.  */

  5090. static void
  5091. build_type_psymtabs (struct objfile *objfile)
  5092. {
  5093.   if (! create_all_type_units (objfile))
  5094.     return;

  5095.   build_type_psymtabs_1 ();
  5096. }

  5097. /* Traversal function for process_skeletonless_type_unit.
  5098.    Read a TU in a DWO file and build partial symbols for it.  */

  5099. static int
  5100. process_skeletonless_type_unit (void **slot, void *info)
  5101. {
  5102.   struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
  5103.   struct objfile *objfile = info;
  5104.   struct signatured_type find_entry, *entry;

  5105.   /* If this TU doesn't exist in the global table, add it and read it in.  */

  5106.   if (dwarf2_per_objfile->signatured_types == NULL)
  5107.     {
  5108.       dwarf2_per_objfile->signatured_types
  5109.         = allocate_signatured_type_table (objfile);
  5110.     }

  5111.   find_entry.signature = dwo_unit->signature;
  5112.   slot = htab_find_slot (dwarf2_per_objfile->signatured_types, &find_entry,
  5113.                          INSERT);
  5114.   /* If we've already seen this type there's nothing to do.  What's happening
  5115.      is we're doing our own version of comdat-folding here.  */
  5116.   if (*slot != NULL)
  5117.     return 1;

  5118.   /* This does the job that create_all_type_units would have done for
  5119.      this TU.  */
  5120.   entry = add_type_unit (dwo_unit->signature, slot);
  5121.   fill_in_sig_entry_from_dwo_entry (objfile, entry, dwo_unit);
  5122.   *slot = entry;

  5123.   /* This does the job that build_type_psymtabs_1 would have done.  */
  5124.   init_cutu_and_read_dies (&entry->per_cu, NULL, 0, 0,
  5125.                            build_type_psymtabs_reader, NULL);

  5126.   return 1;
  5127. }

  5128. /* Traversal function for process_skeletonless_type_units.  */

  5129. static int
  5130. process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
  5131. {
  5132.   struct dwo_file *dwo_file = (struct dwo_file *) *slot;

  5133.   if (dwo_file->tus != NULL)
  5134.     {
  5135.       htab_traverse_noresize (dwo_file->tus,
  5136.                               process_skeletonless_type_unit, info);
  5137.     }

  5138.   return 1;
  5139. }

  5140. /* Scan all TUs of DWO files, verifying we've processed them.
  5141.    This is needed in case a TU was emitted without its skeleton.
  5142.    Note: This can't be done until we know what all the DWO files are.  */

  5143. static void
  5144. process_skeletonless_type_units (struct objfile *objfile)
  5145. {
  5146.   /* Skeletonless TUs in DWP files without .gdb_index is not supported yet.  */
  5147.   if (get_dwp_file () == NULL
  5148.       && dwarf2_per_objfile->dwo_files != NULL)
  5149.     {
  5150.       htab_traverse_noresize (dwarf2_per_objfile->dwo_files,
  5151.                               process_dwo_file_for_skeletonless_type_units,
  5152.                               objfile);
  5153.     }
  5154. }

  5155. /* A cleanup function that clears objfile's psymtabs_addrmap field.  */

  5156. static void
  5157. psymtabs_addrmap_cleanup (void *o)
  5158. {
  5159.   struct objfile *objfile = o;

  5160.   objfile->psymtabs_addrmap = NULL;
  5161. }

  5162. /* Compute the 'user' field for each psymtab in OBJFILE.  */

  5163. static void
  5164. set_partial_user (struct objfile *objfile)
  5165. {
  5166.   int i;

  5167.   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  5168.     {
  5169.       struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);
  5170.       struct partial_symtab *pst = per_cu->v.psymtab;
  5171.       int j;

  5172.       if (pst == NULL)
  5173.         continue;

  5174.       for (j = 0; j < pst->number_of_dependencies; ++j)
  5175.         {
  5176.           /* Set the 'user' field only if it is not already set.  */
  5177.           if (pst->dependencies[j]->user == NULL)
  5178.             pst->dependencies[j]->user = pst;
  5179.         }
  5180.     }
  5181. }

  5182. /* Build the partial symbol table by doing a quick pass through the
  5183.    .debug_info and .debug_abbrev sections.  */

  5184. static void
  5185. dwarf2_build_psymtabs_hard (struct objfile *objfile)
  5186. {
  5187.   struct cleanup *back_to, *addrmap_cleanup;
  5188.   struct obstack temp_obstack;
  5189.   int i;

  5190.   if (dwarf2_read_debug)
  5191.     {
  5192.       fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
  5193.                           objfile_name (objfile));
  5194.     }

  5195.   dwarf2_per_objfile->reading_partial_symbols = 1;

  5196.   dwarf2_read_section (objfile, &dwarf2_per_objfile->info);

  5197.   /* Any cached compilation units will be linked by the per-objfile
  5198.      read_in_chain.  Make sure to free them when we're done.  */
  5199.   back_to = make_cleanup (free_cached_comp_units, NULL);

  5200.   build_type_psymtabs (objfile);

  5201.   create_all_comp_units (objfile);

  5202.   /* Create a temporary address map on a temporary obstack.  We later
  5203.      copy this to the final obstack.  */
  5204.   obstack_init (&temp_obstack);
  5205.   make_cleanup_obstack_free (&temp_obstack);
  5206.   objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
  5207.   addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);

  5208.   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  5209.     {
  5210.       struct dwarf2_per_cu_data *per_cu = dw2_get_cutu (i);

  5211.       process_psymtab_comp_unit (per_cu, 0, language_minimal);
  5212.     }

  5213.   /* This has to wait until we read the CUs, we need the list of DWOs.  */
  5214.   process_skeletonless_type_units (objfile);

  5215.   /* Now that all TUs have been processed we can fill in the dependencies.  */
  5216.   if (dwarf2_per_objfile->type_unit_groups != NULL)
  5217.     {
  5218.       htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups,
  5219.                               build_type_psymtab_dependencies, NULL);
  5220.     }

  5221.   if (dwarf2_read_debug)
  5222.     print_tu_stats ();

  5223.   set_partial_user (objfile);

  5224.   objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
  5225.                                                     &objfile->objfile_obstack);
  5226.   discard_cleanups (addrmap_cleanup);

  5227.   do_cleanups (back_to);

  5228.   if (dwarf2_read_debug)
  5229.     fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
  5230.                         objfile_name (objfile));
  5231. }

  5232. /* die_reader_func for load_partial_comp_unit.  */

  5233. static void
  5234. load_partial_comp_unit_reader (const struct die_reader_specs *reader,
  5235.                                const gdb_byte *info_ptr,
  5236.                                struct die_info *comp_unit_die,
  5237.                                int has_children,
  5238.                                void *data)
  5239. {
  5240.   struct dwarf2_cu *cu = reader->cu;

  5241.   prepare_one_comp_unit (cu, comp_unit_die, language_minimal);

  5242.   /* Check if comp unit has_children.
  5243.      If so, read the rest of the partial symbols from this comp unit.
  5244.      If not, there's no more debug_info for this comp unit.  */
  5245.   if (has_children)
  5246.     load_partial_dies (reader, info_ptr, 0);
  5247. }

  5248. /* Load the partial DIEs for a secondary CU into memory.
  5249.    This is also used when rereading a primary CU with load_all_dies.  */

  5250. static void
  5251. load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
  5252. {
  5253.   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
  5254.                            load_partial_comp_unit_reader, NULL);
  5255. }

  5256. static void
  5257. read_comp_units_from_section (struct objfile *objfile,
  5258.                               struct dwarf2_section_info *section,
  5259.                               unsigned int is_dwz,
  5260.                               int *n_allocated,
  5261.                               int *n_comp_units,
  5262.                               struct dwarf2_per_cu_data ***all_comp_units)
  5263. {
  5264.   const gdb_byte *info_ptr;
  5265.   bfd *abfd = get_section_bfd_owner (section);

  5266.   if (dwarf2_read_debug)
  5267.     fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
  5268.                         get_section_name (section),
  5269.                         get_section_file_name (section));

  5270.   dwarf2_read_section (objfile, section);

  5271.   info_ptr = section->buffer;

  5272.   while (info_ptr < section->buffer + section->size)
  5273.     {
  5274.       unsigned int length, initial_length_size;
  5275.       struct dwarf2_per_cu_data *this_cu;
  5276.       sect_offset offset;

  5277.       offset.sect_off = info_ptr - section->buffer;

  5278.       /* Read just enough information to find out where the next
  5279.          compilation unit is.  */
  5280.       length = read_initial_length (abfd, info_ptr, &initial_length_size);

  5281.       /* Save the compilation unit for later lookup.  */
  5282.       this_cu = obstack_alloc (&objfile->objfile_obstack,
  5283.                                sizeof (struct dwarf2_per_cu_data));
  5284.       memset (this_cu, 0, sizeof (*this_cu));
  5285.       this_cu->offset = offset;
  5286.       this_cu->length = length + initial_length_size;
  5287.       this_cu->is_dwz = is_dwz;
  5288.       this_cu->objfile = objfile;
  5289.       this_cu->section = section;

  5290.       if (*n_comp_units == *n_allocated)
  5291.         {
  5292.           *n_allocated *= 2;
  5293.           *all_comp_units = xrealloc (*all_comp_units,
  5294.                                       *n_allocated
  5295.                                       * sizeof (struct dwarf2_per_cu_data *));
  5296.         }
  5297.       (*all_comp_units)[*n_comp_units] = this_cu;
  5298.       ++*n_comp_units;

  5299.       info_ptr = info_ptr + this_cu->length;
  5300.     }
  5301. }

  5302. /* Create a list of all compilation units in OBJFILE.
  5303.    This is only done for -readnow and building partial symtabs.  */

  5304. static void
  5305. create_all_comp_units (struct objfile *objfile)
  5306. {
  5307.   int n_allocated;
  5308.   int n_comp_units;
  5309.   struct dwarf2_per_cu_data **all_comp_units;
  5310.   struct dwz_file *dwz;

  5311.   n_comp_units = 0;
  5312.   n_allocated = 10;
  5313.   all_comp_units = xmalloc (n_allocated
  5314.                             * sizeof (struct dwarf2_per_cu_data *));

  5315.   read_comp_units_from_section (objfile, &dwarf2_per_objfile->info, 0,
  5316.                                 &n_allocated, &n_comp_units, &all_comp_units);

  5317.   dwz = dwarf2_get_dwz_file ();
  5318.   if (dwz != NULL)
  5319.     read_comp_units_from_section (objfile, &dwz->info, 1,
  5320.                                   &n_allocated, &n_comp_units,
  5321.                                   &all_comp_units);

  5322.   dwarf2_per_objfile->all_comp_units
  5323.     = obstack_alloc (&objfile->objfile_obstack,
  5324.                      n_comp_units * sizeof (struct dwarf2_per_cu_data *));
  5325.   memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
  5326.           n_comp_units * sizeof (struct dwarf2_per_cu_data *));
  5327.   xfree (all_comp_units);
  5328.   dwarf2_per_objfile->n_comp_units = n_comp_units;
  5329. }

  5330. /* Process all loaded DIEs for compilation unit CU, starting at
  5331.    FIRST_DIE.  The caller should pass SET_ADDRMAP == 1 if the compilation
  5332.    unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
  5333.    DW_AT_ranges).  See the comments of add_partial_subprogram on how
  5334.    SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated.  */

  5335. static void
  5336. scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
  5337.                       CORE_ADDR *highpc, int set_addrmap,
  5338.                       struct dwarf2_cu *cu)
  5339. {
  5340.   struct partial_die_info *pdi;

  5341.   /* Now, march along the PDI's, descending into ones which have
  5342.      interesting children but skipping the children of the other ones,
  5343.      until we reach the end of the compilation unit.  */

  5344.   pdi = first_die;

  5345.   while (pdi != NULL)
  5346.     {
  5347.       fixup_partial_die (pdi, cu);

  5348.       /* Anonymous namespaces or modules have no name but have interesting
  5349.          children, so we need to look at them.  Ditto for anonymous
  5350.          enums.  */

  5351.       if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
  5352.           || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
  5353.           || pdi->tag == DW_TAG_imported_unit)
  5354.         {
  5355.           switch (pdi->tag)
  5356.             {
  5357.             case DW_TAG_subprogram:
  5358.               add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
  5359.               break;
  5360.             case DW_TAG_constant:
  5361.             case DW_TAG_variable:
  5362.             case DW_TAG_typedef:
  5363.             case DW_TAG_union_type:
  5364.               if (!pdi->is_declaration)
  5365.                 {
  5366.                   add_partial_symbol (pdi, cu);
  5367.                 }
  5368.               break;
  5369.             case DW_TAG_class_type:
  5370.             case DW_TAG_interface_type:
  5371.             case DW_TAG_structure_type:
  5372.               if (!pdi->is_declaration)
  5373.                 {
  5374.                   add_partial_symbol (pdi, cu);
  5375.                 }
  5376.               break;
  5377.             case DW_TAG_enumeration_type:
  5378.               if (!pdi->is_declaration)
  5379.                 add_partial_enumeration (pdi, cu);
  5380.               break;
  5381.             case DW_TAG_base_type:
  5382.             case DW_TAG_subrange_type:
  5383.               /* File scope base type definitions are added to the partial
  5384.                  symbol table.  */
  5385.               add_partial_symbol (pdi, cu);
  5386.               break;
  5387.             case DW_TAG_namespace:
  5388.               add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
  5389.               break;
  5390.             case DW_TAG_module:
  5391.               add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
  5392.               break;
  5393.             case DW_TAG_imported_unit:
  5394.               {
  5395.                 struct dwarf2_per_cu_data *per_cu;

  5396.                 /* For now we don't handle imported units in type units.  */
  5397.                 if (cu->per_cu->is_debug_types)
  5398.                   {
  5399.                     error (_("Dwarf Error: DW_TAG_imported_unit is not"
  5400.                              " supported in type units [in module %s]"),
  5401.                            objfile_name (cu->objfile));
  5402.                   }

  5403.                 per_cu = dwarf2_find_containing_comp_unit (pdi->d.offset,
  5404.                                                            pdi->is_dwz,
  5405.                                                            cu->objfile);

  5406.                 /* Go read the partial unit, if needed.  */
  5407.                 if (per_cu->v.psymtab == NULL)
  5408.                   process_psymtab_comp_unit (per_cu, 1, cu->language);

  5409.                 VEC_safe_push (dwarf2_per_cu_ptr,
  5410.                                cu->per_cu->imported_symtabs, per_cu);
  5411.               }
  5412.               break;
  5413.             case DW_TAG_imported_declaration:
  5414.               add_partial_symbol (pdi, cu);
  5415.               break;
  5416.             default:
  5417.               break;
  5418.             }
  5419.         }

  5420.       /* If the die has a sibling, skip to the sibling.  */

  5421.       pdi = pdi->die_sibling;
  5422.     }
  5423. }

  5424. /* Functions used to compute the fully scoped name of a partial DIE.

  5425.    Normally, this is simple.  For C++, the parent DIE's fully scoped
  5426.    name is concatenated with "::" and the partial DIE's name.  For
  5427.    Java, the same thing occurs except that "." is used instead of "::".
  5428.    Enumerators are an exception; they use the scope of their parent
  5429.    enumeration type, i.e. the name of the enumeration type is not
  5430.    prepended to the enumerator.

  5431.    There are two complexities.  One is DW_AT_specification; in this
  5432.    case "parent" means the parent of the target of the specification,
  5433.    instead of the direct parent of the DIE.  The other is compilers
  5434.    which do not emit DW_TAG_namespace; in this case we try to guess
  5435.    the fully qualified name of structure types from their members'
  5436.    linkage names.  This must be done using the DIE's children rather
  5437.    than the children of any DW_AT_specification target.  We only need
  5438.    to do this for structures at the top level, i.e. if the target of
  5439.    any DW_AT_specification (if any; otherwise the DIE itself) does not
  5440.    have a parent.  */

  5441. /* Compute the scope prefix associated with PDI's parent, in
  5442.    compilation unit CU.  The result will be allocated on CU's
  5443.    comp_unit_obstack, or a copy of the already allocated PDI->NAME
  5444.    field.  NULL is returned if no prefix is necessary.  */
  5445. static const char *
  5446. partial_die_parent_scope (struct partial_die_info *pdi,
  5447.                           struct dwarf2_cu *cu)
  5448. {
  5449.   const char *grandparent_scope;
  5450.   struct partial_die_info *parent, *real_pdi;

  5451.   /* We need to look at our parent DIE; if we have a DW_AT_specification,
  5452.      then this means the parent of the specification DIE.  */

  5453.   real_pdi = pdi;
  5454.   while (real_pdi->has_specification)
  5455.     real_pdi = find_partial_die (real_pdi->spec_offset,
  5456.                                  real_pdi->spec_is_dwz, cu);

  5457.   parent = real_pdi->die_parent;
  5458.   if (parent == NULL)
  5459.     return NULL;

  5460.   if (parent->scope_set)
  5461.     return parent->scope;

  5462.   fixup_partial_die (parent, cu);

  5463.   grandparent_scope = partial_die_parent_scope (parent, cu);

  5464.   /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
  5465.      DW_TAG_namespace DIEs with a name of "::" for the global namespace.
  5466.      Work around this problem here.  */
  5467.   if (cu->language == language_cplus
  5468.       && parent->tag == DW_TAG_namespace
  5469.       && strcmp (parent->name, "::") == 0
  5470.       && grandparent_scope == NULL)
  5471.     {
  5472.       parent->scope = NULL;
  5473.       parent->scope_set = 1;
  5474.       return NULL;
  5475.     }

  5476.   if (pdi->tag == DW_TAG_enumerator)
  5477.     /* Enumerators should not get the name of the enumeration as a prefix.  */
  5478.     parent->scope = grandparent_scope;
  5479.   else if (parent->tag == DW_TAG_namespace
  5480.       || parent->tag == DW_TAG_module
  5481.       || parent->tag == DW_TAG_structure_type
  5482.       || parent->tag == DW_TAG_class_type
  5483.       || parent->tag == DW_TAG_interface_type
  5484.       || parent->tag == DW_TAG_union_type
  5485.       || parent->tag == DW_TAG_enumeration_type)
  5486.     {
  5487.       if (grandparent_scope == NULL)
  5488.         parent->scope = parent->name;
  5489.       else
  5490.         parent->scope = typename_concat (&cu->comp_unit_obstack,
  5491.                                          grandparent_scope,
  5492.                                          parent->name, 0, cu);
  5493.     }
  5494.   else
  5495.     {
  5496.       /* FIXME drow/2004-04-01: What should we be doing with
  5497.          function-local names?  For partial symbols, we should probably be
  5498.          ignoring them.  */
  5499.       complaint (&symfile_complaints,
  5500.                  _("unhandled containing DIE tag %d for DIE at %d"),
  5501.                  parent->tag, pdi->offset.sect_off);
  5502.       parent->scope = grandparent_scope;
  5503.     }

  5504.   parent->scope_set = 1;
  5505.   return parent->scope;
  5506. }

  5507. /* Return the fully scoped name associated with PDI, from compilation unit
  5508.    CU.  The result will be allocated with malloc.  */

  5509. static char *
  5510. partial_die_full_name (struct partial_die_info *pdi,
  5511.                        struct dwarf2_cu *cu)
  5512. {
  5513.   const char *parent_scope;

  5514.   /* If this is a template instantiation, we can not work out the
  5515.      template arguments from partial DIEs.  So, unfortunately, we have
  5516.      to go through the full DIEs.  At least any work we do building
  5517.      types here will be reused if full symbols are loaded later.  */
  5518.   if (pdi->has_template_arguments)
  5519.     {
  5520.       fixup_partial_die (pdi, cu);

  5521.       if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
  5522.         {
  5523.           struct die_info *die;
  5524.           struct attribute attr;
  5525.           struct dwarf2_cu *ref_cu = cu;

  5526.           /* DW_FORM_ref_addr is using section offset.  */
  5527.           attr.name = 0;
  5528.           attr.form = DW_FORM_ref_addr;
  5529.           attr.u.unsnd = pdi->offset.sect_off;
  5530.           die = follow_die_ref (NULL, &attr, &ref_cu);

  5531.           return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
  5532.         }
  5533.     }

  5534.   parent_scope = partial_die_parent_scope (pdi, cu);
  5535.   if (parent_scope == NULL)
  5536.     return NULL;
  5537.   else
  5538.     return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
  5539. }

  5540. static void
  5541. add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
  5542. {
  5543.   struct objfile *objfile = cu->objfile;
  5544.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  5545.   CORE_ADDR addr = 0;
  5546.   const char *actual_name = NULL;
  5547.   CORE_ADDR baseaddr;
  5548.   char *built_actual_name;

  5549.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  5550.   built_actual_name = partial_die_full_name (pdi, cu);
  5551.   if (built_actual_name != NULL)
  5552.     actual_name = built_actual_name;

  5553.   if (actual_name == NULL)
  5554.     actual_name = pdi->name;

  5555.   switch (pdi->tag)
  5556.     {
  5557.     case DW_TAG_subprogram:
  5558.       addr = gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr);
  5559.       if (pdi->is_external || cu->language == language_ada)
  5560.         {
  5561.           /* brobecker/2007-12-26: Normally, only "external" DIEs are part
  5562.              of the global scope.  But in Ada, we want to be able to access
  5563.              nested procedures globally.  So all Ada subprograms are stored
  5564.              in the global scope.  */
  5565.           /* prim_record_minimal_symbol (actual_name, addr, mst_text,
  5566.              objfile); */
  5567.           add_psymbol_to_list (actual_name, strlen (actual_name),
  5568.                                built_actual_name != NULL,
  5569.                                VAR_DOMAIN, LOC_BLOCK,
  5570.                                &objfile->global_psymbols,
  5571.                                0, addr, cu->language, objfile);
  5572.         }
  5573.       else
  5574.         {
  5575.           /* prim_record_minimal_symbol (actual_name, addr, mst_file_text,
  5576.              objfile); */
  5577.           add_psymbol_to_list (actual_name, strlen (actual_name),
  5578.                                built_actual_name != NULL,
  5579.                                VAR_DOMAIN, LOC_BLOCK,
  5580.                                &objfile->static_psymbols,
  5581.                                0, addr, cu->language, objfile);
  5582.         }
  5583.       break;
  5584.     case DW_TAG_constant:
  5585.       {
  5586.         struct psymbol_allocation_list *list;

  5587.         if (pdi->is_external)
  5588.           list = &objfile->global_psymbols;
  5589.         else
  5590.           list = &objfile->static_psymbols;
  5591.         add_psymbol_to_list (actual_name, strlen (actual_name),
  5592.                              built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
  5593.                              list, 0, 0, cu->language, objfile);
  5594.       }
  5595.       break;
  5596.     case DW_TAG_variable:
  5597.       if (pdi->d.locdesc)
  5598.         addr = decode_locdesc (pdi->d.locdesc, cu);

  5599.       if (pdi->d.locdesc
  5600.           && addr == 0
  5601.           && !dwarf2_per_objfile->has_section_at_zero)
  5602.         {
  5603.           /* A global or static variable may also have been stripped
  5604.              out by the linker if unused, in which case its address
  5605.              will be nullified; do not add such variables into partial
  5606.              symbol table then.  */
  5607.         }
  5608.       else if (pdi->is_external)
  5609.         {
  5610.           /* Global Variable.
  5611.              Don't enter into the minimal symbol tables as there is
  5612.              a minimal symbol table entry from the ELF symbols already.
  5613.              Enter into partial symbol table if it has a location
  5614.              descriptor or a type.
  5615.              If the location descriptor is missing, new_symbol will create
  5616.              a LOC_UNRESOLVED symbol, the address of the variable will then
  5617.              be determined from the minimal symbol table whenever the variable
  5618.              is referenced.
  5619.              The address for the partial symbol table entry is not
  5620.              used by GDB, but it comes in handy for debugging partial symbol
  5621.              table building.  */

  5622.           if (pdi->d.locdesc || pdi->has_type)
  5623.             add_psymbol_to_list (actual_name, strlen (actual_name),
  5624.                                  built_actual_name != NULL,
  5625.                                  VAR_DOMAIN, LOC_STATIC,
  5626.                                  &objfile->global_psymbols,
  5627.                                  0, addr + baseaddr,
  5628.                                  cu->language, objfile);
  5629.         }
  5630.       else
  5631.         {
  5632.           /* Static Variable.  Skip symbols without location descriptors.  */
  5633.           if (pdi->d.locdesc == NULL)
  5634.             {
  5635.               xfree (built_actual_name);
  5636.               return;
  5637.             }
  5638.           /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
  5639.              mst_file_data, objfile); */
  5640.           add_psymbol_to_list (actual_name, strlen (actual_name),
  5641.                                built_actual_name != NULL,
  5642.                                VAR_DOMAIN, LOC_STATIC,
  5643.                                &objfile->static_psymbols,
  5644.                                0, addr + baseaddr,
  5645.                                cu->language, objfile);
  5646.         }
  5647.       break;
  5648.     case DW_TAG_typedef:
  5649.     case DW_TAG_base_type:
  5650.     case DW_TAG_subrange_type:
  5651.       add_psymbol_to_list (actual_name, strlen (actual_name),
  5652.                            built_actual_name != NULL,
  5653.                            VAR_DOMAIN, LOC_TYPEDEF,
  5654.                            &objfile->static_psymbols,
  5655.                            0, (CORE_ADDR) 0, cu->language, objfile);
  5656.       break;
  5657.     case DW_TAG_imported_declaration:
  5658.     case DW_TAG_namespace:
  5659.       add_psymbol_to_list (actual_name, strlen (actual_name),
  5660.                            built_actual_name != NULL,
  5661.                            VAR_DOMAIN, LOC_TYPEDEF,
  5662.                            &objfile->global_psymbols,
  5663.                            0, (CORE_ADDR) 0, cu->language, objfile);
  5664.       break;
  5665.     case DW_TAG_module:
  5666.       add_psymbol_to_list (actual_name, strlen (actual_name),
  5667.                            built_actual_name != NULL,
  5668.                            MODULE_DOMAIN, LOC_TYPEDEF,
  5669.                            &objfile->global_psymbols,
  5670.                            0, (CORE_ADDR) 0, cu->language, objfile);
  5671.       break;
  5672.     case DW_TAG_class_type:
  5673.     case DW_TAG_interface_type:
  5674.     case DW_TAG_structure_type:
  5675.     case DW_TAG_union_type:
  5676.     case DW_TAG_enumeration_type:
  5677.       /* Skip external references.  The DWARF standard says in the section
  5678.          about "Structure, Union, and Class Type Entries": "An incomplete
  5679.          structure, union or class type is represented by a structure,
  5680.          union or class entry that does not have a byte size attribute
  5681.          and that has a DW_AT_declaration attribute."  */
  5682.       if (!pdi->has_byte_size && pdi->is_declaration)
  5683.         {
  5684.           xfree (built_actual_name);
  5685.           return;
  5686.         }

  5687.       /* NOTE: carlton/2003-10-07: See comment in new_symbol about
  5688.          static vs. global.  */
  5689.       add_psymbol_to_list (actual_name, strlen (actual_name),
  5690.                            built_actual_name != NULL,
  5691.                            STRUCT_DOMAIN, LOC_TYPEDEF,
  5692.                            (cu->language == language_cplus
  5693.                             || cu->language == language_java)
  5694.                            ? &objfile->global_psymbols
  5695.                            : &objfile->static_psymbols,
  5696.                            0, (CORE_ADDR) 0, cu->language, objfile);

  5697.       break;
  5698.     case DW_TAG_enumerator:
  5699.       add_psymbol_to_list (actual_name, strlen (actual_name),
  5700.                            built_actual_name != NULL,
  5701.                            VAR_DOMAIN, LOC_CONST,
  5702.                            (cu->language == language_cplus
  5703.                             || cu->language == language_java)
  5704.                            ? &objfile->global_psymbols
  5705.                            : &objfile->static_psymbols,
  5706.                            0, (CORE_ADDR) 0, cu->language, objfile);
  5707.       break;
  5708.     default:
  5709.       break;
  5710.     }

  5711.   xfree (built_actual_name);
  5712. }

  5713. /* Read a partial die corresponding to a namespace; also, add a symbol
  5714.    corresponding to that namespace to the symbol table.  NAMESPACE is
  5715.    the name of the enclosing namespace.  */

  5716. static void
  5717. add_partial_namespace (struct partial_die_info *pdi,
  5718.                        CORE_ADDR *lowpc, CORE_ADDR *highpc,
  5719.                        int set_addrmap, struct dwarf2_cu *cu)
  5720. {
  5721.   /* Add a symbol for the namespace.  */

  5722.   add_partial_symbol (pdi, cu);

  5723.   /* Now scan partial symbols in that namespace.  */

  5724.   if (pdi->has_children)
  5725.     scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
  5726. }

  5727. /* Read a partial die corresponding to a Fortran module.  */

  5728. static void
  5729. add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
  5730.                     CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
  5731. {
  5732.   /* Add a symbol for the namespace.  */

  5733.   add_partial_symbol (pdi, cu);

  5734.   /* Now scan partial symbols in that module.  */

  5735.   if (pdi->has_children)
  5736.     scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
  5737. }

  5738. /* Read a partial die corresponding to a subprogram and create a partial
  5739.    symbol for that subprogram.  When the CU language allows it, this
  5740.    routine also defines a partial symbol for each nested subprogram
  5741.    that this subprogram contains.  If SET_ADDRMAP is true, record the
  5742.    covered ranges in the addrmap.  Set *LOWPC and *HIGHPC to the lowest
  5743.    and highest PC values found in PDI.

  5744.    PDI may also be a lexical block, in which case we simply search
  5745.    recursively for subprograms defined inside that lexical block.
  5746.    Again, this is only performed when the CU language allows this
  5747.    type of definitions.  */

  5748. static void
  5749. add_partial_subprogram (struct partial_die_info *pdi,
  5750.                         CORE_ADDR *lowpc, CORE_ADDR *highpc,
  5751.                         int set_addrmap, struct dwarf2_cu *cu)
  5752. {
  5753.   if (pdi->tag == DW_TAG_subprogram)
  5754.     {
  5755.       if (pdi->has_pc_info)
  5756.         {
  5757.           if (pdi->lowpc < *lowpc)
  5758.             *lowpc = pdi->lowpc;
  5759.           if (pdi->highpc > *highpc)
  5760.             *highpc = pdi->highpc;
  5761.           if (set_addrmap)
  5762.             {
  5763.               struct objfile *objfile = cu->objfile;
  5764.               struct gdbarch *gdbarch = get_objfile_arch (objfile);
  5765.               CORE_ADDR baseaddr;
  5766.               CORE_ADDR highpc;
  5767.               CORE_ADDR lowpc;

  5768.               baseaddr = ANOFFSET (objfile->section_offsets,
  5769.                                    SECT_OFF_TEXT (objfile));
  5770.               lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
  5771.                                                   pdi->lowpc + baseaddr);
  5772.               highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
  5773.                                                    pdi->highpc + baseaddr);
  5774.               addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
  5775.                                  cu->per_cu->v.psymtab);
  5776.             }
  5777.         }

  5778.       if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
  5779.         {
  5780.           if (!pdi->is_declaration)
  5781.             /* Ignore subprogram DIEs that do not have a name, they are
  5782.                illegal.  Do not emit a complaint at this point, we will
  5783.                do so when we convert this psymtab into a symtab.  */
  5784.             if (pdi->name)
  5785.               add_partial_symbol (pdi, cu);
  5786.         }
  5787.     }

  5788.   if (! pdi->has_children)
  5789.     return;

  5790.   if (cu->language == language_ada)
  5791.     {
  5792.       pdi = pdi->die_child;
  5793.       while (pdi != NULL)
  5794.         {
  5795.           fixup_partial_die (pdi, cu);
  5796.           if (pdi->tag == DW_TAG_subprogram
  5797.               || pdi->tag == DW_TAG_lexical_block)
  5798.             add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
  5799.           pdi = pdi->die_sibling;
  5800.         }
  5801.     }
  5802. }

  5803. /* Read a partial die corresponding to an enumeration type.  */

  5804. static void
  5805. add_partial_enumeration (struct partial_die_info *enum_pdi,
  5806.                          struct dwarf2_cu *cu)
  5807. {
  5808.   struct partial_die_info *pdi;

  5809.   if (enum_pdi->name != NULL)
  5810.     add_partial_symbol (enum_pdi, cu);

  5811.   pdi = enum_pdi->die_child;
  5812.   while (pdi)
  5813.     {
  5814.       if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
  5815.         complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
  5816.       else
  5817.         add_partial_symbol (pdi, cu);
  5818.       pdi = pdi->die_sibling;
  5819.     }
  5820. }

  5821. /* Return the initial uleb128 in the die at INFO_PTR.  */

  5822. static unsigned int
  5823. peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
  5824. {
  5825.   unsigned int bytes_read;

  5826.   return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
  5827. }

  5828. /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
  5829.    Return the corresponding abbrev, or NULL if the number is zero (indicating
  5830.    an empty DIE).  In either case *BYTES_READ will be set to the length of
  5831.    the initial number.  */

  5832. static struct abbrev_info *
  5833. peek_die_abbrev (const gdb_byte *info_ptr, unsigned int *bytes_read,
  5834.                  struct dwarf2_cu *cu)
  5835. {
  5836.   bfd *abfd = cu->objfile->obfd;
  5837.   unsigned int abbrev_number;
  5838.   struct abbrev_info *abbrev;

  5839.   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);

  5840.   if (abbrev_number == 0)
  5841.     return NULL;

  5842.   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
  5843.   if (!abbrev)
  5844.     {
  5845.       error (_("Dwarf Error: Could not find abbrev number %d in %s"
  5846.                " at offset 0x%x [in module %s]"),
  5847.              abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
  5848.              cu->header.offset.sect_off, bfd_get_filename (abfd));
  5849.     }

  5850.   return abbrev;
  5851. }

  5852. /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
  5853.    Returns a pointer to the end of a series of DIEs, terminated by an empty
  5854.    DIE.  Any children of the skipped DIEs will also be skipped.  */

  5855. static const gdb_byte *
  5856. skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
  5857. {
  5858.   struct dwarf2_cu *cu = reader->cu;
  5859.   struct abbrev_info *abbrev;
  5860.   unsigned int bytes_read;

  5861.   while (1)
  5862.     {
  5863.       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
  5864.       if (abbrev == NULL)
  5865.         return info_ptr + bytes_read;
  5866.       else
  5867.         info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
  5868.     }
  5869. }

  5870. /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
  5871.    INFO_PTR should point just after the initial uleb128 of a DIE, and the
  5872.    abbrev corresponding to that skipped uleb128 should be passed in
  5873.    ABBREV.  Returns a pointer to this DIE's sibling, skipping any
  5874.    children.  */

  5875. static const gdb_byte *
  5876. skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
  5877.               struct abbrev_info *abbrev)
  5878. {
  5879.   unsigned int bytes_read;
  5880.   struct attribute attr;
  5881.   bfd *abfd = reader->abfd;
  5882.   struct dwarf2_cu *cu = reader->cu;
  5883.   const gdb_byte *buffer = reader->buffer;
  5884.   const gdb_byte *buffer_end = reader->buffer_end;
  5885.   const gdb_byte *start_info_ptr = info_ptr;
  5886.   unsigned int form, i;

  5887.   for (i = 0; i < abbrev->num_attrs; i++)
  5888.     {
  5889.       /* The only abbrev we care about is DW_AT_sibling.  */
  5890.       if (abbrev->attrs[i].name == DW_AT_sibling)
  5891.         {
  5892.           read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
  5893.           if (attr.form == DW_FORM_ref_addr)
  5894.             complaint (&symfile_complaints,
  5895.                        _("ignoring absolute DW_AT_sibling"));
  5896.           else
  5897.             {
  5898.               unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
  5899.               const gdb_byte *sibling_ptr = buffer + off;

  5900.               if (sibling_ptr < info_ptr)
  5901.                 complaint (&symfile_complaints,
  5902.                            _("DW_AT_sibling points backwards"));
  5903.               else if (sibling_ptr > reader->buffer_end)
  5904.                 dwarf2_section_buffer_overflow_complaint (reader->die_section);
  5905.               else
  5906.                 return sibling_ptr;
  5907.             }
  5908.         }

  5909.       /* If it isn't DW_AT_sibling, skip this attribute.  */
  5910.       form = abbrev->attrs[i].form;
  5911.     skip_attribute:
  5912.       switch (form)
  5913.         {
  5914.         case DW_FORM_ref_addr:
  5915.           /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
  5916.              and later it is offset sized.  */
  5917.           if (cu->header.version == 2)
  5918.             info_ptr += cu->header.addr_size;
  5919.           else
  5920.             info_ptr += cu->header.offset_size;
  5921.           break;
  5922.         case DW_FORM_GNU_ref_alt:
  5923.           info_ptr += cu->header.offset_size;
  5924.           break;
  5925.         case DW_FORM_addr:
  5926.           info_ptr += cu->header.addr_size;
  5927.           break;
  5928.         case DW_FORM_data1:
  5929.         case DW_FORM_ref1:
  5930.         case DW_FORM_flag:
  5931.           info_ptr += 1;
  5932.           break;
  5933.         case DW_FORM_flag_present:
  5934.           break;
  5935.         case DW_FORM_data2:
  5936.         case DW_FORM_ref2:
  5937.           info_ptr += 2;
  5938.           break;
  5939.         case DW_FORM_data4:
  5940.         case DW_FORM_ref4:
  5941.           info_ptr += 4;
  5942.           break;
  5943.         case DW_FORM_data8:
  5944.         case DW_FORM_ref8:
  5945.         case DW_FORM_ref_sig8:
  5946.           info_ptr += 8;
  5947.           break;
  5948.         case DW_FORM_string:
  5949.           read_direct_string (abfd, info_ptr, &bytes_read);
  5950.           info_ptr += bytes_read;
  5951.           break;
  5952.         case DW_FORM_sec_offset:
  5953.         case DW_FORM_strp:
  5954.         case DW_FORM_GNU_strp_alt:
  5955.           info_ptr += cu->header.offset_size;
  5956.           break;
  5957.         case DW_FORM_exprloc:
  5958.         case DW_FORM_block:
  5959.           info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
  5960.           info_ptr += bytes_read;
  5961.           break;
  5962.         case DW_FORM_block1:
  5963.           info_ptr += 1 + read_1_byte (abfd, info_ptr);
  5964.           break;
  5965.         case DW_FORM_block2:
  5966.           info_ptr += 2 + read_2_bytes (abfd, info_ptr);
  5967.           break;
  5968.         case DW_FORM_block4:
  5969.           info_ptr += 4 + read_4_bytes (abfd, info_ptr);
  5970.           break;
  5971.         case DW_FORM_sdata:
  5972.         case DW_FORM_udata:
  5973.         case DW_FORM_ref_udata:
  5974.         case DW_FORM_GNU_addr_index:
  5975.         case DW_FORM_GNU_str_index:
  5976.           info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
  5977.           break;
  5978.         case DW_FORM_indirect:
  5979.           form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
  5980.           info_ptr += bytes_read;
  5981.           /* We need to continue parsing from here, so just go back to
  5982.              the top.  */
  5983.           goto skip_attribute;

  5984.         default:
  5985.           error (_("Dwarf Error: Cannot handle %s "
  5986.                    "in DWARF reader [in module %s]"),
  5987.                  dwarf_form_name (form),
  5988.                  bfd_get_filename (abfd));
  5989.         }
  5990.     }

  5991.   if (abbrev->has_children)
  5992.     return skip_children (reader, info_ptr);
  5993.   else
  5994.     return info_ptr;
  5995. }

  5996. /* Locate ORIG_PDI's sibling.
  5997.    INFO_PTR should point to the start of the next DIE after ORIG_PDI.  */

  5998. static const gdb_byte *
  5999. locate_pdi_sibling (const struct die_reader_specs *reader,
  6000.                     struct partial_die_info *orig_pdi,
  6001.                     const gdb_byte *info_ptr)
  6002. {
  6003.   /* Do we know the sibling already?  */

  6004.   if (orig_pdi->sibling)
  6005.     return orig_pdi->sibling;

  6006.   /* Are there any children to deal with?  */

  6007.   if (!orig_pdi->has_children)
  6008.     return info_ptr;

  6009.   /* Skip the children the long way.  */

  6010.   return skip_children (reader, info_ptr);
  6011. }

  6012. /* Expand this partial symbol table into a full symbol table.  SELF is
  6013.    not NULL.  */

  6014. static void
  6015. dwarf2_read_symtab (struct partial_symtab *self,
  6016.                     struct objfile *objfile)
  6017. {
  6018.   if (self->readin)
  6019.     {
  6020.       warning (_("bug: psymtab for %s is already read in."),
  6021.                self->filename);
  6022.     }
  6023.   else
  6024.     {
  6025.       if (info_verbose)
  6026.         {
  6027.           printf_filtered (_("Reading in symbols for %s..."),
  6028.                            self->filename);
  6029.           gdb_flush (gdb_stdout);
  6030.         }

  6031.       /* Restore our global data.  */
  6032.       dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);

  6033.       /* If this psymtab is constructed from a debug-only objfile, the
  6034.          has_section_at_zero flag will not necessarily be correct.  We
  6035.          can get the correct value for this flag by looking at the data
  6036.          associated with the (presumably stripped) associated objfile.  */
  6037.       if (objfile->separate_debug_objfile_backlink)
  6038.         {
  6039.           struct dwarf2_per_objfile *dpo_backlink
  6040.             = objfile_data (objfile->separate_debug_objfile_backlink,
  6041.                             dwarf2_objfile_data_key);

  6042.           dwarf2_per_objfile->has_section_at_zero
  6043.             = dpo_backlink->has_section_at_zero;
  6044.         }

  6045.       dwarf2_per_objfile->reading_partial_symbols = 0;

  6046.       psymtab_to_symtab_1 (self);

  6047.       /* Finish up the debug error message.  */
  6048.       if (info_verbose)
  6049.         printf_filtered (_("done.\n"));
  6050.     }

  6051.   process_cu_includes ();
  6052. }

  6053. /* Reading in full CUs.  */

  6054. /* Add PER_CU to the queue.  */

  6055. static void
  6056. queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
  6057.                  enum language pretend_language)
  6058. {
  6059.   struct dwarf2_queue_item *item;

  6060.   per_cu->queued = 1;
  6061.   item = xmalloc (sizeof (*item));
  6062.   item->per_cu = per_cu;
  6063.   item->pretend_language = pretend_language;
  6064.   item->next = NULL;

  6065.   if (dwarf2_queue == NULL)
  6066.     dwarf2_queue = item;
  6067.   else
  6068.     dwarf2_queue_tail->next = item;

  6069.   dwarf2_queue_tail = item;
  6070. }

  6071. /* If PER_CU is not yet queued, add it to the queue.
  6072.    If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
  6073.    dependency.
  6074.    The result is non-zero if PER_CU was queued, otherwise the result is zero
  6075.    meaning either PER_CU is already queued or it is already loaded.

  6076.    N.B. There is an invariant here that if a CU is queued then it is loaded.
  6077.    The caller is required to load PER_CU if we return non-zero.  */

  6078. static int
  6079. maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
  6080.                        struct dwarf2_per_cu_data *per_cu,
  6081.                        enum language pretend_language)
  6082. {
  6083.   /* We may arrive here during partial symbol reading, if we need full
  6084.      DIEs to process an unusual case (e.g. template arguments).  Do
  6085.      not queue PER_CU, just tell our caller to load its DIEs.  */
  6086.   if (dwarf2_per_objfile->reading_partial_symbols)
  6087.     {
  6088.       if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
  6089.         return 1;
  6090.       return 0;
  6091.     }

  6092.   /* Mark the dependence relation so that we don't flush PER_CU
  6093.      too early.  */
  6094.   if (dependent_cu != NULL)
  6095.     dwarf2_add_dependence (dependent_cu, per_cu);

  6096.   /* If it's already on the queue, we have nothing to do.  */
  6097.   if (per_cu->queued)
  6098.     return 0;

  6099.   /* If the compilation unit is already loaded, just mark it as
  6100.      used.  */
  6101.   if (per_cu->cu != NULL)
  6102.     {
  6103.       per_cu->cu->last_used = 0;
  6104.       return 0;
  6105.     }

  6106.   /* Add it to the queue.  */
  6107.   queue_comp_unit (per_cu, pretend_language);

  6108.   return 1;
  6109. }

  6110. /* Process the queue.  */

  6111. static void
  6112. process_queue (void)
  6113. {
  6114.   struct dwarf2_queue_item *item, *next_item;

  6115.   if (dwarf2_read_debug)
  6116.     {
  6117.       fprintf_unfiltered (gdb_stdlog,
  6118.                           "Expanding one or more symtabs of objfile %s ...\n",
  6119.                           objfile_name (dwarf2_per_objfile->objfile));
  6120.     }

  6121.   /* The queue starts out with one item, but following a DIE reference
  6122.      may load a new CU, adding it to the end of the queue.  */
  6123.   for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
  6124.     {
  6125.       if (dwarf2_per_objfile->using_index
  6126.           ? !item->per_cu->v.quick->compunit_symtab
  6127.           : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
  6128.         {
  6129.           struct dwarf2_per_cu_data *per_cu = item->per_cu;
  6130.           unsigned int debug_print_threshold;
  6131.           char buf[100];

  6132.           if (per_cu->is_debug_types)
  6133.             {
  6134.               struct signatured_type *sig_type =
  6135.                 (struct signatured_type *) per_cu;

  6136.               sprintf (buf, "TU %s at offset 0x%x",
  6137.                        hex_string (sig_type->signature),
  6138.                        per_cu->offset.sect_off);
  6139.               /* There can be 100s of TUs.
  6140.                  Only print them in verbose mode.  */
  6141.               debug_print_threshold = 2;
  6142.             }
  6143.           else
  6144.             {
  6145.               sprintf (buf, "CU at offset 0x%x", per_cu->offset.sect_off);
  6146.               debug_print_threshold = 1;
  6147.             }

  6148.           if (dwarf2_read_debug >= debug_print_threshold)
  6149.             fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);

  6150.           if (per_cu->is_debug_types)
  6151.             process_full_type_unit (per_cu, item->pretend_language);
  6152.           else
  6153.             process_full_comp_unit (per_cu, item->pretend_language);

  6154.           if (dwarf2_read_debug >= debug_print_threshold)
  6155.             fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
  6156.         }

  6157.       item->per_cu->queued = 0;
  6158.       next_item = item->next;
  6159.       xfree (item);
  6160.     }

  6161.   dwarf2_queue_tail = NULL;

  6162.   if (dwarf2_read_debug)
  6163.     {
  6164.       fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
  6165.                           objfile_name (dwarf2_per_objfile->objfile));
  6166.     }
  6167. }

  6168. /* Free all allocated queue entries.  This function only releases anything if
  6169.    an error was thrown; if the queue was processed then it would have been
  6170.    freed as we went along.  */

  6171. static void
  6172. dwarf2_release_queue (void *dummy)
  6173. {
  6174.   struct dwarf2_queue_item *item, *last;

  6175.   item = dwarf2_queue;
  6176.   while (item)
  6177.     {
  6178.       /* Anything still marked queued is likely to be in an
  6179.          inconsistent state, so discard it.  */
  6180.       if (item->per_cu->queued)
  6181.         {
  6182.           if (item->per_cu->cu != NULL)
  6183.             free_one_cached_comp_unit (item->per_cu);
  6184.           item->per_cu->queued = 0;
  6185.         }

  6186.       last = item;
  6187.       item = item->next;
  6188.       xfree (last);
  6189.     }

  6190.   dwarf2_queue = dwarf2_queue_tail = NULL;
  6191. }

  6192. /* Read in full symbols for PST, and anything it depends on.  */

  6193. static void
  6194. psymtab_to_symtab_1 (struct partial_symtab *pst)
  6195. {
  6196.   struct dwarf2_per_cu_data *per_cu;
  6197.   int i;

  6198.   if (pst->readin)
  6199.     return;

  6200.   for (i = 0; i < pst->number_of_dependencies; i++)
  6201.     if (!pst->dependencies[i]->readin
  6202.         && pst->dependencies[i]->user == NULL)
  6203.       {
  6204.         /* Inform about additional files that need to be read in.  */
  6205.         if (info_verbose)
  6206.           {
  6207.             /* FIXME: i18n: Need to make this a single string.  */
  6208.             fputs_filtered (" ", gdb_stdout);
  6209.             wrap_here ("");
  6210.             fputs_filtered ("and ", gdb_stdout);
  6211.             wrap_here ("");
  6212.             printf_filtered ("%s...", pst->dependencies[i]->filename);
  6213.             wrap_here ("");     /* Flush output.  */
  6214.             gdb_flush (gdb_stdout);
  6215.           }
  6216.         psymtab_to_symtab_1 (pst->dependencies[i]);
  6217.       }

  6218.   per_cu = pst->read_symtab_private;

  6219.   if (per_cu == NULL)
  6220.     {
  6221.       /* It's an include file, no symbols to read for it.
  6222.          Everything is in the parent symtab.  */
  6223.       pst->readin = 1;
  6224.       return;
  6225.     }

  6226.   dw2_do_instantiate_symtab (per_cu);
  6227. }

  6228. /* Trivial hash function for die_info: the hash value of a DIE
  6229.    is its offset in .debug_info for this objfile.  */

  6230. static hashval_t
  6231. die_hash (const void *item)
  6232. {
  6233.   const struct die_info *die = item;

  6234.   return die->offset.sect_off;
  6235. }

  6236. /* Trivial comparison function for die_info structures: two DIEs
  6237.    are equal if they have the same offset.  */

  6238. static int
  6239. die_eq (const void *item_lhs, const void *item_rhs)
  6240. {
  6241.   const struct die_info *die_lhs = item_lhs;
  6242.   const struct die_info *die_rhs = item_rhs;

  6243.   return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
  6244. }

  6245. /* die_reader_func for load_full_comp_unit.
  6246.    This is identical to read_signatured_type_reader,
  6247.    but is kept separate for now.  */

  6248. static void
  6249. load_full_comp_unit_reader (const struct die_reader_specs *reader,
  6250.                             const gdb_byte *info_ptr,
  6251.                             struct die_info *comp_unit_die,
  6252.                             int has_children,
  6253.                             void *data)
  6254. {
  6255.   struct dwarf2_cu *cu = reader->cu;
  6256.   enum language *language_ptr = data;

  6257.   gdb_assert (cu->die_hash == NULL);
  6258.   cu->die_hash =
  6259.     htab_create_alloc_ex (cu->header.length / 12,
  6260.                           die_hash,
  6261.                           die_eq,
  6262.                           NULL,
  6263.                           &cu->comp_unit_obstack,
  6264.                           hashtab_obstack_allocate,
  6265.                           dummy_obstack_deallocate);

  6266.   if (has_children)
  6267.     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
  6268.                                                   &info_ptr, comp_unit_die);
  6269.   cu->dies = comp_unit_die;
  6270.   /* comp_unit_die is not stored in die_hash, no need.  */

  6271.   /* We try not to read any attributes in this function, because not
  6272.      all CUs needed for references have been loaded yet, and symbol
  6273.      table processing isn't initialized.  But we have to set the CU language,
  6274.      or we won't be able to build types correctly.
  6275.      Similarly, if we do not read the producer, we can not apply
  6276.      producer-specific interpretation.  */
  6277.   prepare_one_comp_unit (cu, cu->dies, *language_ptr);
  6278. }

  6279. /* Load the DIEs associated with PER_CU into memory.  */

  6280. static void
  6281. load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
  6282.                      enum language pretend_language)
  6283. {
  6284.   gdb_assert (! this_cu->is_debug_types);

  6285.   init_cutu_and_read_dies (this_cu, NULL, 1, 1,
  6286.                            load_full_comp_unit_reader, &pretend_language);
  6287. }

  6288. /* Add a DIE to the delayed physname list.  */

  6289. static void
  6290. add_to_method_list (struct type *type, int fnfield_index, int index,
  6291.                     const char *name, struct die_info *die,
  6292.                     struct dwarf2_cu *cu)
  6293. {
  6294.   struct delayed_method_info mi;
  6295.   mi.type = type;
  6296.   mi.fnfield_index = fnfield_index;
  6297.   mi.index = index;
  6298.   mi.name = name;
  6299.   mi.die = die;
  6300.   VEC_safe_push (delayed_method_info, cu->method_list, &mi);
  6301. }

  6302. /* A cleanup for freeing the delayed method list.  */

  6303. static void
  6304. free_delayed_list (void *ptr)
  6305. {
  6306.   struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
  6307.   if (cu->method_list != NULL)
  6308.     {
  6309.       VEC_free (delayed_method_info, cu->method_list);
  6310.       cu->method_list = NULL;
  6311.     }
  6312. }

  6313. /* Compute the physnames of any methods on the CU's method list.

  6314.    The computation of method physnames is delayed in order to avoid the
  6315.    (bad) condition that one of the method's formal parameters is of an as yet
  6316.    incomplete type.  */

  6317. static void
  6318. compute_delayed_physnames (struct dwarf2_cu *cu)
  6319. {
  6320.   int i;
  6321.   struct delayed_method_info *mi;
  6322.   for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
  6323.     {
  6324.       const char *physname;
  6325.       struct fn_fieldlist *fn_flp
  6326.         = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
  6327.       physname = dwarf2_physname (mi->name, mi->die, cu);
  6328.       fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
  6329.     }
  6330. }

  6331. /* Go objects should be embedded in a DW_TAG_module DIE,
  6332.    and it's not clear if/how imported objects will appear.
  6333.    To keep Go support simple until that's worked out,
  6334.    go back through what we've read and create something usable.
  6335.    We could do this while processing each DIE, and feels kinda cleaner,
  6336.    but that way is more invasive.
  6337.    This is to, for example, allow the user to type "p var" or "b main"
  6338.    without having to specify the package name, and allow lookups
  6339.    of module.object to work in contexts that use the expression
  6340.    parser.  */

  6341. static void
  6342. fixup_go_packaging (struct dwarf2_cu *cu)
  6343. {
  6344.   char *package_name = NULL;
  6345.   struct pending *list;
  6346.   int i;

  6347.   for (list = global_symbols; list != NULL; list = list->next)
  6348.     {
  6349.       for (i = 0; i < list->nsyms; ++i)
  6350.         {
  6351.           struct symbol *sym = list->symbol[i];

  6352.           if (SYMBOL_LANGUAGE (sym) == language_go
  6353.               && SYMBOL_CLASS (sym) == LOC_BLOCK)
  6354.             {
  6355.               char *this_package_name = go_symbol_package_name (sym);

  6356.               if (this_package_name == NULL)
  6357.                 continue;
  6358.               if (package_name == NULL)
  6359.                 package_name = this_package_name;
  6360.               else
  6361.                 {
  6362.                   if (strcmp (package_name, this_package_name) != 0)
  6363.                     complaint (&symfile_complaints,
  6364.                                _("Symtab %s has objects from two different Go packages: %s and %s"),
  6365.                                (symbol_symtab (sym) != NULL
  6366.                                 ? symtab_to_filename_for_display
  6367.                                     (symbol_symtab (sym))
  6368.                                 : objfile_name (cu->objfile)),
  6369.                                this_package_name, package_name);
  6370.                   xfree (this_package_name);
  6371.                 }
  6372.             }
  6373.         }
  6374.     }

  6375.   if (package_name != NULL)
  6376.     {
  6377.       struct objfile *objfile = cu->objfile;
  6378.       const char *saved_package_name
  6379.         = obstack_copy0 (&objfile->per_bfd->storage_obstack,
  6380.                          package_name,
  6381.                          strlen (package_name));
  6382.       struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
  6383.                                      saved_package_name, objfile);
  6384.       struct symbol *sym;

  6385.       TYPE_TAG_NAME (type) = TYPE_NAME (type);

  6386.       sym = allocate_symbol (objfile);
  6387.       SYMBOL_SET_LANGUAGE (sym, language_go, &objfile->objfile_obstack);
  6388.       SYMBOL_SET_NAMES (sym, saved_package_name,
  6389.                         strlen (saved_package_name), 0, objfile);
  6390.       /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
  6391.          e.g., "main" finds the "main" module and not C's main().  */
  6392.       SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
  6393.       SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  6394.       SYMBOL_TYPE (sym) = type;

  6395.       add_symbol_to_list (sym, &global_symbols);

  6396.       xfree (package_name);
  6397.     }
  6398. }

  6399. /* Return the symtab for PER_CU.  This works properly regardless of
  6400.    whether we're using the index or psymtabs.  */

  6401. static struct compunit_symtab *
  6402. get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
  6403. {
  6404.   return (dwarf2_per_objfile->using_index
  6405.           ? per_cu->v.quick->compunit_symtab
  6406.           : per_cu->v.psymtab->compunit_symtab);
  6407. }

  6408. /* A helper function for computing the list of all symbol tables
  6409.    included by PER_CU.  */

  6410. static void
  6411. recursively_compute_inclusions (VEC (compunit_symtab_ptr) **result,
  6412.                                 htab_t all_children, htab_t all_type_symtabs,
  6413.                                 struct dwarf2_per_cu_data *per_cu,
  6414.                                 struct compunit_symtab *immediate_parent)
  6415. {
  6416.   void **slot;
  6417.   int ix;
  6418.   struct compunit_symtab *cust;
  6419.   struct dwarf2_per_cu_data *iter;

  6420.   slot = htab_find_slot (all_children, per_cu, INSERT);
  6421.   if (*slot != NULL)
  6422.     {
  6423.       /* This inclusion and its children have been processed.  */
  6424.       return;
  6425.     }

  6426.   *slot = per_cu;
  6427.   /* Only add a CU if it has a symbol table.  */
  6428.   cust = get_compunit_symtab (per_cu);
  6429.   if (cust != NULL)
  6430.     {
  6431.       /* If this is a type unit only add its symbol table if we haven't
  6432.          seen it yet (type unit per_cu's can share symtabs).  */
  6433.       if (per_cu->is_debug_types)
  6434.         {
  6435.           slot = htab_find_slot (all_type_symtabs, cust, INSERT);
  6436.           if (*slot == NULL)
  6437.             {
  6438.               *slot = cust;
  6439.               VEC_safe_push (compunit_symtab_ptr, *result, cust);
  6440.               if (cust->user == NULL)
  6441.                 cust->user = immediate_parent;
  6442.             }
  6443.         }
  6444.       else
  6445.         {
  6446.           VEC_safe_push (compunit_symtab_ptr, *result, cust);
  6447.           if (cust->user == NULL)
  6448.             cust->user = immediate_parent;
  6449.         }
  6450.     }

  6451.   for (ix = 0;
  6452.        VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs, ix, iter);
  6453.        ++ix)
  6454.     {
  6455.       recursively_compute_inclusions (result, all_children,
  6456.                                       all_type_symtabs, iter, cust);
  6457.     }
  6458. }

  6459. /* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
  6460.    PER_CU.  */

  6461. static void
  6462. compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
  6463. {
  6464.   gdb_assert (! per_cu->is_debug_types);

  6465.   if (!VEC_empty (dwarf2_per_cu_ptr, per_cu->imported_symtabs))
  6466.     {
  6467.       int ix, len;
  6468.       struct dwarf2_per_cu_data *per_cu_iter;
  6469.       struct compunit_symtab *compunit_symtab_iter;
  6470.       VEC (compunit_symtab_ptr) *result_symtabs = NULL;
  6471.       htab_t all_children, all_type_symtabs;
  6472.       struct compunit_symtab *cust = get_compunit_symtab (per_cu);

  6473.       /* If we don't have a symtab, we can just skip this case.  */
  6474.       if (cust == NULL)
  6475.         return;

  6476.       all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
  6477.                                         NULL, xcalloc, xfree);
  6478.       all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
  6479.                                             NULL, xcalloc, xfree);

  6480.       for (ix = 0;
  6481.            VEC_iterate (dwarf2_per_cu_ptr, per_cu->imported_symtabs,
  6482.                         ix, per_cu_iter);
  6483.            ++ix)
  6484.         {
  6485.           recursively_compute_inclusions (&result_symtabs, all_children,
  6486.                                           all_type_symtabs, per_cu_iter,
  6487.                                           cust);
  6488.         }

  6489.       /* Now we have a transitive closure of all the included symtabs.  */
  6490.       len = VEC_length (compunit_symtab_ptr, result_symtabs);
  6491.       cust->includes
  6492.         = obstack_alloc (&dwarf2_per_objfile->objfile->objfile_obstack,
  6493.                          (len + 1) * sizeof (struct symtab *));
  6494.       for (ix = 0;
  6495.            VEC_iterate (compunit_symtab_ptr, result_symtabs, ix,
  6496.                         compunit_symtab_iter);
  6497.            ++ix)
  6498.         cust->includes[ix] = compunit_symtab_iter;
  6499.       cust->includes[len] = NULL;

  6500.       VEC_free (compunit_symtab_ptr, result_symtabs);
  6501.       htab_delete (all_children);
  6502.       htab_delete (all_type_symtabs);
  6503.     }
  6504. }

  6505. /* Compute the 'includes' field for the symtabs of all the CUs we just
  6506.    read.  */

  6507. static void
  6508. process_cu_includes (void)
  6509. {
  6510.   int ix;
  6511.   struct dwarf2_per_cu_data *iter;

  6512.   for (ix = 0;
  6513.        VEC_iterate (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus,
  6514.                     ix, iter);
  6515.        ++ix)
  6516.     {
  6517.       if (! iter->is_debug_types)
  6518.         compute_compunit_symtab_includes (iter);
  6519.     }

  6520.   VEC_free (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus);
  6521. }

  6522. /* Generate full symbol information for PER_CU, whose DIEs have
  6523.    already been loaded into memory.  */

  6524. static void
  6525. process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
  6526.                         enum language pretend_language)
  6527. {
  6528.   struct dwarf2_cu *cu = per_cu->cu;
  6529.   struct objfile *objfile = per_cu->objfile;
  6530.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  6531.   CORE_ADDR lowpc, highpc;
  6532.   struct compunit_symtab *cust;
  6533.   struct cleanup *back_to, *delayed_list_cleanup;
  6534.   CORE_ADDR baseaddr;
  6535.   struct block *static_block;
  6536.   CORE_ADDR addr;

  6537.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  6538.   buildsym_init ();
  6539.   back_to = make_cleanup (really_free_pendings, NULL);
  6540.   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);

  6541.   cu->list_in_scope = &file_symbols;

  6542.   cu->language = pretend_language;
  6543.   cu->language_defn = language_def (cu->language);

  6544.   /* Do line number decoding in read_file_scope () */
  6545.   process_die (cu->dies, cu);

  6546.   /* For now fudge the Go package.  */
  6547.   if (cu->language == language_go)
  6548.     fixup_go_packaging (cu);

  6549.   /* Now that we have processed all the DIEs in the CU, all the types
  6550.      should be complete, and it should now be safe to compute all of the
  6551.      physnames.  */
  6552.   compute_delayed_physnames (cu);
  6553.   do_cleanups (delayed_list_cleanup);

  6554.   /* Some compilers don't define a DW_AT_high_pc attribute for the
  6555.      compilation unit.  If the DW_AT_high_pc is missing, synthesize
  6556.      it, by scanning the DIE's below the compilation unit.  */
  6557.   get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);

  6558.   addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
  6559.   static_block = end_symtab_get_static_block (addr, 0, 1);

  6560.   /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
  6561.      Also, DW_AT_ranges may record ranges not belonging to any child DIEs
  6562.      (such as virtual method tables).  Record the ranges in STATIC_BLOCK's
  6563.      addrmap to help ensure it has an accurate map of pc values belonging to
  6564.      this comp unit.  */
  6565.   dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);

  6566.   cust = end_symtab_from_static_block (static_block,
  6567.                                        SECT_OFF_TEXT (objfile), 0);

  6568.   if (cust != NULL)
  6569.     {
  6570.       int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);

  6571.       /* Set symtab language to language from DW_AT_language.  If the
  6572.          compilation is from a C file generated by language preprocessors, do
  6573.          not set the language if it was already deduced by start_subfile.  */
  6574.       if (!(cu->language == language_c
  6575.             && COMPUNIT_FILETABS (cust)->language != language_c))
  6576.         COMPUNIT_FILETABS (cust)->language = cu->language;

  6577.       /* GCC-4.0 has started to support -fvar-tracking.  GCC-3.x still can
  6578.          produce DW_AT_location with location lists but it can be possibly
  6579.          invalid without -fvar-tracking.  Still up to GCC-4.4.x incl. 4.4.0
  6580.          there were bugs in prologue debug info, fixed later in GCC-4.5
  6581.          by "unwind info for epilogues" patch (which is not directly related).

  6582.          For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
  6583.          needed, it would be wrong due to missing DW_AT_producer there.

  6584.          Still one can confuse GDB by using non-standard GCC compilation
  6585.          options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
  6586.          */
  6587.       if (cu->has_loclist && gcc_4_minor >= 5)
  6588.         cust->locations_valid = 1;

  6589.       if (gcc_4_minor >= 5)
  6590.         cust->epilogue_unwind_valid = 1;

  6591.       cust->call_site_htab = cu->call_site_htab;
  6592.     }

  6593.   if (dwarf2_per_objfile->using_index)
  6594.     per_cu->v.quick->compunit_symtab = cust;
  6595.   else
  6596.     {
  6597.       struct partial_symtab *pst = per_cu->v.psymtab;
  6598.       pst->compunit_symtab = cust;
  6599.       pst->readin = 1;
  6600.     }

  6601.   /* Push it for inclusion processing later.  */
  6602.   VEC_safe_push (dwarf2_per_cu_ptr, dwarf2_per_objfile->just_read_cus, per_cu);

  6603.   do_cleanups (back_to);
  6604. }

  6605. /* Generate full symbol information for type unit PER_CU, whose DIEs have
  6606.    already been loaded into memory.  */

  6607. static void
  6608. process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
  6609.                         enum language pretend_language)
  6610. {
  6611.   struct dwarf2_cu *cu = per_cu->cu;
  6612.   struct objfile *objfile = per_cu->objfile;
  6613.   struct compunit_symtab *cust;
  6614.   struct cleanup *back_to, *delayed_list_cleanup;
  6615.   struct signatured_type *sig_type;

  6616.   gdb_assert (per_cu->is_debug_types);
  6617.   sig_type = (struct signatured_type *) per_cu;

  6618.   buildsym_init ();
  6619.   back_to = make_cleanup (really_free_pendings, NULL);
  6620.   delayed_list_cleanup = make_cleanup (free_delayed_list, cu);

  6621.   cu->list_in_scope = &file_symbols;

  6622.   cu->language = pretend_language;
  6623.   cu->language_defn = language_def (cu->language);

  6624.   /* The symbol tables are set up in read_type_unit_scope.  */
  6625.   process_die (cu->dies, cu);

  6626.   /* For now fudge the Go package.  */
  6627.   if (cu->language == language_go)
  6628.     fixup_go_packaging (cu);

  6629.   /* Now that we have processed all the DIEs in the CU, all the types
  6630.      should be complete, and it should now be safe to compute all of the
  6631.      physnames.  */
  6632.   compute_delayed_physnames (cu);
  6633.   do_cleanups (delayed_list_cleanup);

  6634.   /* TUs share symbol tables.
  6635.      If this is the first TU to use this symtab, complete the construction
  6636.      of it with end_expandable_symtab.  Otherwise, complete the addition of
  6637.      this TU's symbols to the existing symtab.  */
  6638.   if (sig_type->type_unit_group->compunit_symtab == NULL)
  6639.     {
  6640.       cust = end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
  6641.       sig_type->type_unit_group->compunit_symtab = cust;

  6642.       if (cust != NULL)
  6643.         {
  6644.           /* Set symtab language to language from DW_AT_language.  If the
  6645.              compilation is from a C file generated by language preprocessors,
  6646.              do not set the language if it was already deduced by
  6647.              start_subfile.  */
  6648.           if (!(cu->language == language_c
  6649.                 && COMPUNIT_FILETABS (cust)->language != language_c))
  6650.             COMPUNIT_FILETABS (cust)->language = cu->language;
  6651.         }
  6652.     }
  6653.   else
  6654.     {
  6655.       augment_type_symtab ();
  6656.       cust = sig_type->type_unit_group->compunit_symtab;
  6657.     }

  6658.   if (dwarf2_per_objfile->using_index)
  6659.     per_cu->v.quick->compunit_symtab = cust;
  6660.   else
  6661.     {
  6662.       struct partial_symtab *pst = per_cu->v.psymtab;
  6663.       pst->compunit_symtab = cust;
  6664.       pst->readin = 1;
  6665.     }

  6666.   do_cleanups (back_to);
  6667. }

  6668. /* Process an imported unit DIE.  */

  6669. static void
  6670. process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
  6671. {
  6672.   struct attribute *attr;

  6673.   /* For now we don't handle imported units in type units.  */
  6674.   if (cu->per_cu->is_debug_types)
  6675.     {
  6676.       error (_("Dwarf Error: DW_TAG_imported_unit is not"
  6677.                " supported in type units [in module %s]"),
  6678.              objfile_name (cu->objfile));
  6679.     }

  6680.   attr = dwarf2_attr (die, DW_AT_import, cu);
  6681.   if (attr != NULL)
  6682.     {
  6683.       struct dwarf2_per_cu_data *per_cu;
  6684.       struct symtab *imported_symtab;
  6685.       sect_offset offset;
  6686.       int is_dwz;

  6687.       offset = dwarf2_get_ref_die_offset (attr);
  6688.       is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
  6689.       per_cu = dwarf2_find_containing_comp_unit (offset, is_dwz, cu->objfile);

  6690.       /* If necessary, add it to the queue and load its DIEs.  */
  6691.       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
  6692.         load_full_comp_unit (per_cu, cu->language);

  6693.       VEC_safe_push (dwarf2_per_cu_ptr, cu->per_cu->imported_symtabs,
  6694.                      per_cu);
  6695.     }
  6696. }

  6697. /* Reset the in_process bit of a die.  */

  6698. static void
  6699. reset_die_in_process (void *arg)
  6700. {
  6701.   struct die_info *die = arg;

  6702.   die->in_process = 0;
  6703. }

  6704. /* Process a die and its children.  */

  6705. static void
  6706. process_die (struct die_info *die, struct dwarf2_cu *cu)
  6707. {
  6708.   struct cleanup *in_process;

  6709.   /* We should only be processing those not already in process.  */
  6710.   gdb_assert (!die->in_process);

  6711.   die->in_process = 1;
  6712.   in_process = make_cleanup (reset_die_in_process,die);

  6713.   switch (die->tag)
  6714.     {
  6715.     case DW_TAG_padding:
  6716.       break;
  6717.     case DW_TAG_compile_unit:
  6718.     case DW_TAG_partial_unit:
  6719.       read_file_scope (die, cu);
  6720.       break;
  6721.     case DW_TAG_type_unit:
  6722.       read_type_unit_scope (die, cu);
  6723.       break;
  6724.     case DW_TAG_subprogram:
  6725.     case DW_TAG_inlined_subroutine:
  6726.       read_func_scope (die, cu);
  6727.       break;
  6728.     case DW_TAG_lexical_block:
  6729.     case DW_TAG_try_block:
  6730.     case DW_TAG_catch_block:
  6731.       read_lexical_block_scope (die, cu);
  6732.       break;
  6733.     case DW_TAG_GNU_call_site:
  6734.       read_call_site_scope (die, cu);
  6735.       break;
  6736.     case DW_TAG_class_type:
  6737.     case DW_TAG_interface_type:
  6738.     case DW_TAG_structure_type:
  6739.     case DW_TAG_union_type:
  6740.       process_structure_scope (die, cu);
  6741.       break;
  6742.     case DW_TAG_enumeration_type:
  6743.       process_enumeration_scope (die, cu);
  6744.       break;

  6745.     /* These dies have a type, but processing them does not create
  6746.        a symbol or recurse to process the children.  Therefore we can
  6747.        read them on-demand through read_type_die.  */
  6748.     case DW_TAG_subroutine_type:
  6749.     case DW_TAG_set_type:
  6750.     case DW_TAG_array_type:
  6751.     case DW_TAG_pointer_type:
  6752.     case DW_TAG_ptr_to_member_type:
  6753.     case DW_TAG_reference_type:
  6754.     case DW_TAG_string_type:
  6755.       break;

  6756.     case DW_TAG_base_type:
  6757.     case DW_TAG_subrange_type:
  6758.     case DW_TAG_typedef:
  6759.       /* Add a typedef symbol for the type definition, if it has a
  6760.          DW_AT_name.  */
  6761.       new_symbol (die, read_type_die (die, cu), cu);
  6762.       break;
  6763.     case DW_TAG_common_block:
  6764.       read_common_block (die, cu);
  6765.       break;
  6766.     case DW_TAG_common_inclusion:
  6767.       break;
  6768.     case DW_TAG_namespace:
  6769.       cu->processing_has_namespace_info = 1;
  6770.       read_namespace (die, cu);
  6771.       break;
  6772.     case DW_TAG_module:
  6773.       cu->processing_has_namespace_info = 1;
  6774.       read_module (die, cu);
  6775.       break;
  6776.     case DW_TAG_imported_declaration:
  6777.       cu->processing_has_namespace_info = 1;
  6778.       if (read_namespace_alias (die, cu))
  6779.         break;
  6780.       /* The declaration is not a global namespace alias: fall through.  */
  6781.     case DW_TAG_imported_module:
  6782.       cu->processing_has_namespace_info = 1;
  6783.       if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
  6784.                                  || cu->language != language_fortran))
  6785.         complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
  6786.                    dwarf_tag_name (die->tag));
  6787.       read_import_statement (die, cu);
  6788.       break;

  6789.     case DW_TAG_imported_unit:
  6790.       process_imported_unit_die (die, cu);
  6791.       break;

  6792.     default:
  6793.       new_symbol (die, NULL, cu);
  6794.       break;
  6795.     }

  6796.   do_cleanups (in_process);
  6797. }

  6798. /* DWARF name computation.  */

  6799. /* A helper function for dwarf2_compute_name which determines whether DIE
  6800.    needs to have the name of the scope prepended to the name listed in the
  6801.    die.  */

  6802. static int
  6803. die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
  6804. {
  6805.   struct attribute *attr;

  6806.   switch (die->tag)
  6807.     {
  6808.     case DW_TAG_namespace:
  6809.     case DW_TAG_typedef:
  6810.     case DW_TAG_class_type:
  6811.     case DW_TAG_interface_type:
  6812.     case DW_TAG_structure_type:
  6813.     case DW_TAG_union_type:
  6814.     case DW_TAG_enumeration_type:
  6815.     case DW_TAG_enumerator:
  6816.     case DW_TAG_subprogram:
  6817.     case DW_TAG_member:
  6818.     case DW_TAG_imported_declaration:
  6819.       return 1;

  6820.     case DW_TAG_variable:
  6821.     case DW_TAG_constant:
  6822.       /* We only need to prefix "globally" visible variables.  These include
  6823.          any variable marked with DW_AT_external or any variable that
  6824.          lives in a namespace.  [Variables in anonymous namespaces
  6825.          require prefixing, but they are not DW_AT_external.]  */

  6826.       if (dwarf2_attr (die, DW_AT_specification, cu))
  6827.         {
  6828.           struct dwarf2_cu *spec_cu = cu;

  6829.           return die_needs_namespace (die_specification (die, &spec_cu),
  6830.                                       spec_cu);
  6831.         }

  6832.       attr = dwarf2_attr (die, DW_AT_external, cu);
  6833.       if (attr == NULL && die->parent->tag != DW_TAG_namespace
  6834.           && die->parent->tag != DW_TAG_module)
  6835.         return 0;
  6836.       /* A variable in a lexical block of some kind does not need a
  6837.          namespace, even though in C++ such variables may be external
  6838.          and have a mangled name.  */
  6839.       if (die->parent->tag ==  DW_TAG_lexical_block
  6840.           || die->parent->tag ==  DW_TAG_try_block
  6841.           || die->parent->tag ==  DW_TAG_catch_block
  6842.           || die->parent->tag == DW_TAG_subprogram)
  6843.         return 0;
  6844.       return 1;

  6845.     default:
  6846.       return 0;
  6847.     }
  6848. }

  6849. /* Retrieve the last character from a mem_file.  */

  6850. static void
  6851. do_ui_file_peek_last (void *object, const char *buffer, long length)
  6852. {
  6853.   char *last_char_p = (char *) object;

  6854.   if (length > 0)
  6855.     *last_char_p = buffer[length - 1];
  6856. }

  6857. /* Compute the fully qualified name of DIE in CU.  If PHYSNAME is nonzero,
  6858.    compute the physname for the object, which include a method's:
  6859.    - formal parameters (C++/Java),
  6860.    - receiver type (Go),
  6861.    - return type (Java).

  6862.    The term "physname" is a bit confusing.
  6863.    For C++, for example, it is the demangled name.
  6864.    For Go, for example, it's the mangled name.

  6865.    For Ada, return the DIE's linkage name rather than the fully qualified
  6866.    name.  PHYSNAME is ignored..

  6867.    The result is allocated on the objfile_obstack and canonicalized.  */

  6868. static const char *
  6869. dwarf2_compute_name (const char *name,
  6870.                      struct die_info *die, struct dwarf2_cu *cu,
  6871.                      int physname)
  6872. {
  6873.   struct objfile *objfile = cu->objfile;

  6874.   if (name == NULL)
  6875.     name = dwarf2_name (die, cu);

  6876.   /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
  6877.      compute it by typename_concat inside GDB.  */
  6878.   if (cu->language == language_ada
  6879.       || (cu->language == language_fortran && physname))
  6880.     {
  6881.       /* For Ada unit, we prefer the linkage name over the name, as
  6882.          the former contains the exported name, which the user expects
  6883.          to be able to reference.  Ideally, we want the user to be able
  6884.          to reference this entity using either natural or linkage name,
  6885.          but we haven't started looking at this enhancement yet.  */
  6886.       struct attribute *attr;

  6887.       attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
  6888.       if (attr == NULL)
  6889.         attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
  6890.       if (attr && DW_STRING (attr))
  6891.         return DW_STRING (attr);
  6892.     }

  6893.   /* These are the only languages we know how to qualify names in.  */
  6894.   if (name != NULL
  6895.       && (cu->language == language_cplus || cu->language == language_java
  6896.           || cu->language == language_fortran))
  6897.     {
  6898.       if (die_needs_namespace (die, cu))
  6899.         {
  6900.           long length;
  6901.           const char *prefix;
  6902.           struct ui_file *buf;
  6903.           char *intermediate_name;
  6904.           const char *canonical_name = NULL;

  6905.           prefix = determine_prefix (die, cu);
  6906.           buf = mem_fileopen ();
  6907.           if (*prefix != '\0')
  6908.             {
  6909.               char *prefixed_name = typename_concat (NULL, prefix, name,
  6910.                                                      physname, cu);

  6911.               fputs_unfiltered (prefixed_name, buf);
  6912.               xfree (prefixed_name);
  6913.             }
  6914.           else
  6915.             fputs_unfiltered (name, buf);

  6916.           /* Template parameters may be specified in the DIE's DW_AT_name, or
  6917.              as children with DW_TAG_template_type_param or
  6918.              DW_TAG_value_type_param.  If the latter, add them to the name
  6919.              here.  If the name already has template parameters, then
  6920.              skip this step; some versions of GCC emit both, and
  6921.              it is more efficient to use the pre-computed name.

  6922.              Something to keep in mind about this process: it is very
  6923.              unlikely, or in some cases downright impossible, to produce
  6924.              something that will match the mangled name of a function.
  6925.              If the definition of the function has the same debug info,
  6926.              we should be able to match up with it anyway.  But fallbacks
  6927.              using the minimal symbol, for instance to find a method
  6928.              implemented in a stripped copy of libstdc++, will not work.
  6929.              If we do not have debug info for the definition, we will have to
  6930.              match them up some other way.

  6931.              When we do name matching there is a related problem with function
  6932.              templates; two instantiated function templates are allowed to
  6933.              differ only by their return types, which we do not add here.  */

  6934.           if (cu->language == language_cplus && strchr (name, '<') == NULL)
  6935.             {
  6936.               struct attribute *attr;
  6937.               struct die_info *child;
  6938.               int first = 1;

  6939.               die->building_fullname = 1;

  6940.               for (child = die->child; child != NULL; child = child->sibling)
  6941.                 {
  6942.                   struct type *type;
  6943.                   LONGEST value;
  6944.                   const gdb_byte *bytes;
  6945.                   struct dwarf2_locexpr_baton *baton;
  6946.                   struct value *v;

  6947.                   if (child->tag != DW_TAG_template_type_param
  6948.                       && child->tag != DW_TAG_template_value_param)
  6949.                     continue;

  6950.                   if (first)
  6951.                     {
  6952.                       fputs_unfiltered ("<", buf);
  6953.                       first = 0;
  6954.                     }
  6955.                   else
  6956.                     fputs_unfiltered (", ", buf);

  6957.                   attr = dwarf2_attr (child, DW_AT_type, cu);
  6958.                   if (attr == NULL)
  6959.                     {
  6960.                       complaint (&symfile_complaints,
  6961.                                  _("template parameter missing DW_AT_type"));
  6962.                       fputs_unfiltered ("UNKNOWN_TYPE", buf);
  6963.                       continue;
  6964.                     }
  6965.                   type = die_type (child, cu);

  6966.                   if (child->tag == DW_TAG_template_type_param)
  6967.                     {
  6968.                       c_print_type (type, "", buf, -1, 0, &type_print_raw_options);
  6969.                       continue;
  6970.                     }

  6971.                   attr = dwarf2_attr (child, DW_AT_const_value, cu);
  6972.                   if (attr == NULL)
  6973.                     {
  6974.                       complaint (&symfile_complaints,
  6975.                                  _("template parameter missing "
  6976.                                    "DW_AT_const_value"));
  6977.                       fputs_unfiltered ("UNKNOWN_VALUE", buf);
  6978.                       continue;
  6979.                     }

  6980.                   dwarf2_const_value_attr (attr, type, name,
  6981.                                            &cu->comp_unit_obstack, cu,
  6982.                                            &value, &bytes, &baton);

  6983.                   if (TYPE_NOSIGN (type))
  6984.                     /* GDB prints characters as NUMBER 'CHAR'.  If that's
  6985.                        changed, this can use value_print instead.  */
  6986.                     c_printchar (value, type, buf);
  6987.                   else
  6988.                     {
  6989.                       struct value_print_options opts;

  6990.                       if (baton != NULL)
  6991.                         v = dwarf2_evaluate_loc_desc (type, NULL,
  6992.                                                       baton->data,
  6993.                                                       baton->size,
  6994.                                                       baton->per_cu);
  6995.                       else if (bytes != NULL)
  6996.                         {
  6997.                           v = allocate_value (type);
  6998.                           memcpy (value_contents_writeable (v), bytes,
  6999.                                   TYPE_LENGTH (type));
  7000.                         }
  7001.                       else
  7002.                         v = value_from_longest (type, value);

  7003.                       /* Specify decimal so that we do not depend on
  7004.                          the radix.  */
  7005.                       get_formatted_print_options (&opts, 'd');
  7006.                       opts.raw = 1;
  7007.                       value_print (v, buf, &opts);
  7008.                       release_value (v);
  7009.                       value_free (v);
  7010.                     }
  7011.                 }

  7012.               die->building_fullname = 0;

  7013.               if (!first)
  7014.                 {
  7015.                   /* Close the argument list, with a space if necessary
  7016.                      (nested templates).  */
  7017.                   char last_char = '\0';
  7018.                   ui_file_put (buf, do_ui_file_peek_last, &last_char);
  7019.                   if (last_char == '>')
  7020.                     fputs_unfiltered (" >", buf);
  7021.                   else
  7022.                     fputs_unfiltered (">", buf);
  7023.                 }
  7024.             }

  7025.           /* For Java and C++ methods, append formal parameter type
  7026.              information, if PHYSNAME.  */

  7027.           if (physname && die->tag == DW_TAG_subprogram
  7028.               && (cu->language == language_cplus
  7029.                   || cu->language == language_java))
  7030.             {
  7031.               struct type *type = read_type_die (die, cu);

  7032.               c_type_print_args (type, buf, 1, cu->language,
  7033.                                  &type_print_raw_options);

  7034.               if (cu->language == language_java)
  7035.                 {
  7036.                   /* For java, we must append the return type to method
  7037.                      names.  */
  7038.                   if (die->tag == DW_TAG_subprogram)
  7039.                     java_print_type (TYPE_TARGET_TYPE (type), "", buf,
  7040.                                      0, 0, &type_print_raw_options);
  7041.                 }
  7042.               else if (cu->language == language_cplus)
  7043.                 {
  7044.                   /* Assume that an artificial first parameter is
  7045.                      "this", but do not crash if it is not.  RealView
  7046.                      marks unnamed (and thus unused) parameters as
  7047.                      artificial; there is no way to differentiate
  7048.                      the two cases.  */
  7049.                   if (TYPE_NFIELDS (type) > 0
  7050.                       && TYPE_FIELD_ARTIFICIAL (type, 0)
  7051.                       && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
  7052.                       && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
  7053.                                                                         0))))
  7054.                     fputs_unfiltered (" const", buf);
  7055.                 }
  7056.             }

  7057.           intermediate_name = ui_file_xstrdup (buf, &length);
  7058.           ui_file_delete (buf);

  7059.           if (cu->language == language_cplus)
  7060.             canonical_name
  7061.               = dwarf2_canonicalize_name (intermediate_name, cu,
  7062.                                           &objfile->per_bfd->storage_obstack);

  7063.           /* If we only computed INTERMEDIATE_NAME, or if
  7064.              INTERMEDIATE_NAME is already canonical, then we need to
  7065.              copy it to the appropriate obstack.  */
  7066.           if (canonical_name == NULL || canonical_name == intermediate_name)
  7067.             name = obstack_copy0 (&objfile->per_bfd->storage_obstack,
  7068.                                   intermediate_name,
  7069.                                   strlen (intermediate_name));
  7070.           else
  7071.             name = canonical_name;

  7072.           xfree (intermediate_name);
  7073.         }
  7074.     }

  7075.   return name;
  7076. }

  7077. /* Return the fully qualified name of DIE, based on its DW_AT_name.
  7078.    If scope qualifiers are appropriate they will be added.  The result
  7079.    will be allocated on the storage_obstack, or NULL if the DIE does
  7080.    not have a nameNAME may either be from a previous call to
  7081.    dwarf2_name or NULL.

  7082.    The output string will be canonicalized (if C++/Java).  */

  7083. static const char *
  7084. dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
  7085. {
  7086.   return dwarf2_compute_name (name, die, cu, 0);
  7087. }

  7088. /* Construct a physname for the given DIE in CU.  NAME may either be
  7089.    from a previous call to dwarf2_name or NULL.  The result will be
  7090.    allocated on the objfile_objstack or NULL if the DIE does not have a
  7091.    name.

  7092.    The output string will be canonicalized (if C++/Java).  */

  7093. static const char *
  7094. dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
  7095. {
  7096.   struct objfile *objfile = cu->objfile;
  7097.   struct attribute *attr;
  7098.   const char *retval, *mangled = NULL, *canon = NULL;
  7099.   struct cleanup *back_to;
  7100.   int need_copy = 1;

  7101.   /* In this case dwarf2_compute_name is just a shortcut not building anything
  7102.      on its own.  */
  7103.   if (!die_needs_namespace (die, cu))
  7104.     return dwarf2_compute_name (name, die, cu, 1);

  7105.   back_to = make_cleanup (null_cleanup, NULL);

  7106.   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
  7107.   if (!attr)
  7108.     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);

  7109.   /* DW_AT_linkage_name is missing in some cases - depend on what GDB
  7110.      has computed.  */
  7111.   if (attr && DW_STRING (attr))
  7112.     {
  7113.       char *demangled;

  7114.       mangled = DW_STRING (attr);

  7115.       /* Use DMGL_RET_DROP for C++ template functions to suppress their return
  7116.          type.  It is easier for GDB users to search for such functions as
  7117.          `name(params)' than `long name(params)'.  In such case the minimal
  7118.          symbol names do not match the full symbol names but for template
  7119.          functions there is never a need to look up their definition from their
  7120.          declaration so the only disadvantage remains the minimal symbol
  7121.          variant `long name(params)' does not have the proper inferior type.
  7122.          */

  7123.       if (cu->language == language_go)
  7124.         {
  7125.           /* This is a lie, but we already lie to the caller new_symbol_full.
  7126.              new_symbol_full assumes we return the mangled name.
  7127.              This just undoes that lie until things are cleaned up.  */
  7128.           demangled = NULL;
  7129.         }
  7130.       else
  7131.         {
  7132.           demangled = gdb_demangle (mangled,
  7133.                                     (DMGL_PARAMS | DMGL_ANSI
  7134.                                      | (cu->language == language_java
  7135.                                         ? DMGL_JAVA | DMGL_RET_POSTFIX
  7136.                                         : DMGL_RET_DROP)));
  7137.         }
  7138.       if (demangled)
  7139.         {
  7140.           make_cleanup (xfree, demangled);
  7141.           canon = demangled;
  7142.         }
  7143.       else
  7144.         {
  7145.           canon = mangled;
  7146.           need_copy = 0;
  7147.         }
  7148.     }

  7149.   if (canon == NULL || check_physname)
  7150.     {
  7151.       const char *physname = dwarf2_compute_name (name, die, cu, 1);

  7152.       if (canon != NULL && strcmp (physname, canon) != 0)
  7153.         {
  7154.           /* It may not mean a bug in GDB.  The compiler could also
  7155.              compute DW_AT_linkage_name incorrectly.  But in such case
  7156.              GDB would need to be bug-to-bug compatible.  */

  7157.           complaint (&symfile_complaints,
  7158.                      _("Computed physname <%s> does not match demangled <%s> "
  7159.                        "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
  7160.                      physname, canon, mangled, die->offset.sect_off,
  7161.                      objfile_name (objfile));

  7162.           /* Prefer DW_AT_linkage_name (in the CANON form) - when it
  7163.              is available here - over computed PHYSNAME.  It is safer
  7164.              against both buggy GDB and buggy compilers.  */

  7165.           retval = canon;
  7166.         }
  7167.       else
  7168.         {
  7169.           retval = physname;
  7170.           need_copy = 0;
  7171.         }
  7172.     }
  7173.   else
  7174.     retval = canon;

  7175.   if (need_copy)
  7176.     retval = obstack_copy0 (&objfile->per_bfd->storage_obstack,
  7177.                             retval, strlen (retval));

  7178.   do_cleanups (back_to);
  7179.   return retval;
  7180. }

  7181. /* Inspect DIE in CU for a namespace alias.  If one exists, record
  7182.    a new symbol for it.

  7183.    Returns 1 if a namespace alias was recorded, 0 otherwise.  */

  7184. static int
  7185. read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
  7186. {
  7187.   struct attribute *attr;

  7188.   /* If the die does not have a name, this is not a namespace
  7189.      alias.  */
  7190.   attr = dwarf2_attr (die, DW_AT_name, cu);
  7191.   if (attr != NULL)
  7192.     {
  7193.       int num;
  7194.       struct die_info *d = die;
  7195.       struct dwarf2_cu *imported_cu = cu;

  7196.       /* If the compiler has nested DW_AT_imported_declaration DIEs,
  7197.          keep inspecting DIEs until we hit the underlying import.  */
  7198. #define MAX_NESTED_IMPORTED_DECLARATIONS 100
  7199.       for (num = 0; num  < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
  7200.         {
  7201.           attr = dwarf2_attr (d, DW_AT_import, cu);
  7202.           if (attr == NULL)
  7203.             break;

  7204.           d = follow_die_ref (d, attr, &imported_cu);
  7205.           if (d->tag != DW_TAG_imported_declaration)
  7206.             break;
  7207.         }

  7208.       if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
  7209.         {
  7210.           complaint (&symfile_complaints,
  7211.                      _("DIE at 0x%x has too many recursively imported "
  7212.                        "declarations"), d->offset.sect_off);
  7213.           return 0;
  7214.         }

  7215.       if (attr != NULL)
  7216.         {
  7217.           struct type *type;
  7218.           sect_offset offset = dwarf2_get_ref_die_offset (attr);

  7219.           type = get_die_type_at_offset (offset, cu->per_cu);
  7220.           if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
  7221.             {
  7222.               /* This declaration is a global namespace alias.  Add
  7223.                  a symbol for it whose type is the aliased namespace.  */
  7224.               new_symbol (die, type, cu);
  7225.               return 1;
  7226.             }
  7227.         }
  7228.     }

  7229.   return 0;
  7230. }

  7231. /* Read the import statement specified by the given die and record it.  */

  7232. static void
  7233. read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
  7234. {
  7235.   struct objfile *objfile = cu->objfile;
  7236.   struct attribute *import_attr;
  7237.   struct die_info *imported_die, *child_die;
  7238.   struct dwarf2_cu *imported_cu;
  7239.   const char *imported_name;
  7240.   const char *imported_name_prefix;
  7241.   const char *canonical_name;
  7242.   const char *import_alias;
  7243.   const char *imported_declaration = NULL;
  7244.   const char *import_prefix;
  7245.   VEC (const_char_ptr) *excludes = NULL;
  7246.   struct cleanup *cleanups;

  7247.   import_attr = dwarf2_attr (die, DW_AT_import, cu);
  7248.   if (import_attr == NULL)
  7249.     {
  7250.       complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
  7251.                  dwarf_tag_name (die->tag));
  7252.       return;
  7253.     }

  7254.   imported_cu = cu;
  7255.   imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
  7256.   imported_name = dwarf2_name (imported_die, imported_cu);
  7257.   if (imported_name == NULL)
  7258.     {
  7259.       /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524

  7260.         The import in the following code:
  7261.         namespace A
  7262.           {
  7263.             typedef int B;
  7264.           }

  7265.         int main ()
  7266.           {
  7267.             using A::B;
  7268.             B b;
  7269.             return b;
  7270.           }

  7271.         ...
  7272.          <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
  7273.             <52>   DW_AT_decl_file   : 1
  7274.             <53>   DW_AT_decl_line   : 6
  7275.             <54>   DW_AT_import      : <0x75>
  7276.          <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
  7277.             <59>   DW_AT_name        : B
  7278.             <5b>   DW_AT_decl_file   : 1
  7279.             <5c>   DW_AT_decl_line   : 2
  7280.             <5d>   DW_AT_type        : <0x6e>
  7281.         ...
  7282.          <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
  7283.             <76>   DW_AT_byte_size   : 4
  7284.             <77>   DW_AT_encoding    : 5        (signed)

  7285.         imports the wrong die ( 0x75 instead of 0x58 ).
  7286.         This case will be ignored until the gcc bug is fixed.  */
  7287.       return;
  7288.     }

  7289.   /* Figure out the local name after import.  */
  7290.   import_alias = dwarf2_name (die, cu);

  7291.   /* Figure out where the statement is being imported to.  */
  7292.   import_prefix = determine_prefix (die, cu);

  7293.   /* Figure out what the scope of the imported die is and prepend it
  7294.      to the name of the imported die.  */
  7295.   imported_name_prefix = determine_prefix (imported_die, imported_cu);

  7296.   if (imported_die->tag != DW_TAG_namespace
  7297.       && imported_die->tag != DW_TAG_module)
  7298.     {
  7299.       imported_declaration = imported_name;
  7300.       canonical_name = imported_name_prefix;
  7301.     }
  7302.   else if (strlen (imported_name_prefix) > 0)
  7303.     canonical_name = obconcat (&objfile->objfile_obstack,
  7304.                                imported_name_prefix, "::", imported_name,
  7305.                                (char *) NULL);
  7306.   else
  7307.     canonical_name = imported_name;

  7308.   cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);

  7309.   if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
  7310.     for (child_die = die->child; child_die && child_die->tag;
  7311.          child_die = sibling_die (child_die))
  7312.       {
  7313.         /* DWARF-4: A Fortran use statement with arename list” may be
  7314.            represented by an imported module entry with an import attribute
  7315.            referring to the module and owned entries corresponding to those
  7316.            entities that are renamed as part of being imported.  */

  7317.         if (child_die->tag != DW_TAG_imported_declaration)
  7318.           {
  7319.             complaint (&symfile_complaints,
  7320.                        _("child DW_TAG_imported_declaration expected "
  7321.                          "- DIE at 0x%x [in module %s]"),
  7322.                        child_die->offset.sect_off, objfile_name (objfile));
  7323.             continue;
  7324.           }

  7325.         import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
  7326.         if (import_attr == NULL)
  7327.           {
  7328.             complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
  7329.                        dwarf_tag_name (child_die->tag));
  7330.             continue;
  7331.           }

  7332.         imported_cu = cu;
  7333.         imported_die = follow_die_ref_or_sig (child_die, import_attr,
  7334.                                               &imported_cu);
  7335.         imported_name = dwarf2_name (imported_die, imported_cu);
  7336.         if (imported_name == NULL)
  7337.           {
  7338.             complaint (&symfile_complaints,
  7339.                        _("child DW_TAG_imported_declaration has unknown "
  7340.                          "imported name - DIE at 0x%x [in module %s]"),
  7341.                        child_die->offset.sect_off, objfile_name (objfile));
  7342.             continue;
  7343.           }

  7344.         VEC_safe_push (const_char_ptr, excludes, imported_name);

  7345.         process_die (child_die, cu);
  7346.       }

  7347.   cp_add_using_directive (import_prefix,
  7348.                           canonical_name,
  7349.                           import_alias,
  7350.                           imported_declaration,
  7351.                           excludes,
  7352.                           0,
  7353.                           &objfile->objfile_obstack);

  7354.   do_cleanups (cleanups);
  7355. }

  7356. /* Cleanup function for handle_DW_AT_stmt_list.  */

  7357. static void
  7358. free_cu_line_header (void *arg)
  7359. {
  7360.   struct dwarf2_cu *cu = arg;

  7361.   free_line_header (cu->line_header);
  7362.   cu->line_header = NULL;
  7363. }

  7364. /* Check for possibly missing DW_AT_comp_dir with relative .debug_line
  7365.    directory paths.  GCC SVN r127613 (new option -fdebug-prefix-map) fixed
  7366.    this, it was first present in GCC release 4.3.0.  */

  7367. static int
  7368. producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
  7369. {
  7370.   if (!cu->checked_producer)
  7371.     check_producer (cu);

  7372.   return cu->producer_is_gcc_lt_4_3;
  7373. }

  7374. static void
  7375. find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
  7376.                          const char **name, const char **comp_dir)
  7377. {
  7378.   struct attribute *attr;

  7379.   *name = NULL;
  7380.   *comp_dir = NULL;

  7381.   /* Find the filename.  Do not use dwarf2_name here, since the filename
  7382.      is not a source language identifier.  */
  7383.   attr = dwarf2_attr (die, DW_AT_name, cu);
  7384.   if (attr)
  7385.     {
  7386.       *name = DW_STRING (attr);
  7387.     }

  7388.   attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
  7389.   if (attr)
  7390.     *comp_dir = DW_STRING (attr);
  7391.   else if (producer_is_gcc_lt_4_3 (cu) && *name != NULL
  7392.            && IS_ABSOLUTE_PATH (*name))
  7393.     {
  7394.       char *d = ldirname (*name);

  7395.       *comp_dir = d;
  7396.       if (d != NULL)
  7397.         make_cleanup (xfree, d);
  7398.     }
  7399.   if (*comp_dir != NULL)
  7400.     {
  7401.       /* Irix 6.2 native cc prepends <machine>.: to the compilation
  7402.          directory, get rid of it.  */
  7403.       char *cp = strchr (*comp_dir, ':');

  7404.       if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
  7405.         *comp_dir = cp + 1;
  7406.     }

  7407.   if (*name == NULL)
  7408.     *name = "<unknown>";
  7409. }

  7410. /* Handle DW_AT_stmt_list for a compilation unit.
  7411.    DIE is the DW_TAG_compile_unit die for CU.
  7412.    COMP_DIR is the compilation directory.  LOWPC is passed to
  7413.    dwarf_decode_lines.  See dwarf_decode_lines comments about it.  */

  7414. static void
  7415. handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
  7416.                         const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
  7417. {
  7418.   struct attribute *attr;

  7419.   gdb_assert (! cu->per_cu->is_debug_types);

  7420.   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
  7421.   if (attr)
  7422.     {
  7423.       unsigned int line_offset = DW_UNSND (attr);
  7424.       struct line_header *line_header
  7425.         = dwarf_decode_line_header (line_offset, cu);

  7426.       if (line_header)
  7427.         {
  7428.           cu->line_header = line_header;
  7429.           make_cleanup (free_cu_line_header, cu);
  7430.           dwarf_decode_lines (line_header, comp_dir, cu, NULL, lowpc);
  7431.         }
  7432.     }
  7433. }

  7434. /* Process DW_TAG_compile_unit or DW_TAG_partial_unit.  */

  7435. static void
  7436. read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
  7437. {
  7438.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  7439.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  7440.   struct cleanup *back_to = make_cleanup (null_cleanup, 0);
  7441.   CORE_ADDR lowpc = ((CORE_ADDR) -1);
  7442.   CORE_ADDR highpc = ((CORE_ADDR) 0);
  7443.   struct attribute *attr;
  7444.   const char *name = NULL;
  7445.   const char *comp_dir = NULL;
  7446.   struct die_info *child_die;
  7447.   bfd *abfd = objfile->obfd;
  7448.   CORE_ADDR baseaddr;

  7449.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  7450.   get_scope_pc_bounds (die, &lowpc, &highpc, cu);

  7451.   /* If we didn't find a lowpc, set it to highpc to avoid complaints
  7452.      from finish_block.  */
  7453.   if (lowpc == ((CORE_ADDR) -1))
  7454.     lowpc = highpc;
  7455.   lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);

  7456.   find_file_and_directory (die, cu, &name, &comp_dir);

  7457.   prepare_one_comp_unit (cu, die, cu->language);

  7458.   /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
  7459.      standardised yet.  As a workaround for the language detection we fall
  7460.      back to the DW_AT_producer string.  */
  7461.   if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
  7462.     cu->language = language_opencl;

  7463.   /* Similar hack for Go.  */
  7464.   if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
  7465.     set_cu_language (DW_LANG_Go, cu);

  7466.   dwarf2_start_symtab (cu, name, comp_dir, lowpc);

  7467.   /* Decode line number information if present.  We do this before
  7468.      processing child DIEs, so that the line header table is available
  7469.      for DW_AT_decl_file.  */
  7470.   handle_DW_AT_stmt_list (die, cu, comp_dir, lowpc);

  7471.   /* Process all dies in compilation unit.  */
  7472.   if (die->child != NULL)
  7473.     {
  7474.       child_die = die->child;
  7475.       while (child_die && child_die->tag)
  7476.         {
  7477.           process_die (child_die, cu);
  7478.           child_die = sibling_die (child_die);
  7479.         }
  7480.     }

  7481.   /* Decode macro information, if present.  Dwarf 2 macro information
  7482.      refers to information in the line number info statement program
  7483.      header, so we can only read it if we've read the header
  7484.      successfully.  */
  7485.   attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
  7486.   if (attr && cu->line_header)
  7487.     {
  7488.       if (dwarf2_attr (die, DW_AT_macro_info, cu))
  7489.         complaint (&symfile_complaints,
  7490.                    _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));

  7491.       dwarf_decode_macros (cu, DW_UNSND (attr), 1);
  7492.     }
  7493.   else
  7494.     {
  7495.       attr = dwarf2_attr (die, DW_AT_macro_info, cu);
  7496.       if (attr && cu->line_header)
  7497.         {
  7498.           unsigned int macro_offset = DW_UNSND (attr);

  7499.           dwarf_decode_macros (cu, macro_offset, 0);
  7500.         }
  7501.     }

  7502.   do_cleanups (back_to);
  7503. }

  7504. /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
  7505.    Create the set of symtabs used by this TU, or if this TU is sharing
  7506.    symtabs with another TU and the symtabs have already been created
  7507.    then restore those symtabs in the line header.
  7508.    We don't need the pc/line-number mapping for type units.  */

  7509. static void
  7510. setup_type_unit_groups (struct die_info *die, struct dwarf2_cu *cu)
  7511. {
  7512.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  7513.   struct dwarf2_per_cu_data *per_cu = cu->per_cu;
  7514.   struct type_unit_group *tu_group;
  7515.   int first_time;
  7516.   struct line_header *lh;
  7517.   struct attribute *attr;
  7518.   unsigned int i, line_offset;
  7519.   struct signatured_type *sig_type;

  7520.   gdb_assert (per_cu->is_debug_types);
  7521.   sig_type = (struct signatured_type *) per_cu;

  7522.   attr = dwarf2_attr (die, DW_AT_stmt_list, cu);

  7523.   /* If we're using .gdb_index (includes -readnow) then
  7524.      per_cu->type_unit_group may not have been set up yet.  */
  7525.   if (sig_type->type_unit_group == NULL)
  7526.     sig_type->type_unit_group = get_type_unit_group (cu, attr);
  7527.   tu_group = sig_type->type_unit_group;

  7528.   /* If we've already processed this stmt_list there's no real need to
  7529.      do it again, we could fake it and just recreate the part we need
  7530.      (file name,index -> symtab mapping).  If data shows this optimization
  7531.      is useful we can do it then.  */
  7532.   first_time = tu_group->compunit_symtab == NULL;

  7533.   /* We have to handle the case of both a missing DW_AT_stmt_list or bad
  7534.      debug info.  */
  7535.   lh = NULL;
  7536.   if (attr != NULL)
  7537.     {
  7538.       line_offset = DW_UNSND (attr);
  7539.       lh = dwarf_decode_line_header (line_offset, cu);
  7540.     }
  7541.   if (lh == NULL)
  7542.     {
  7543.       if (first_time)
  7544.         dwarf2_start_symtab (cu, "", NULL, 0);
  7545.       else
  7546.         {
  7547.           gdb_assert (tu_group->symtabs == NULL);
  7548.           restart_symtab (tu_group->compunit_symtab, "", 0);
  7549.         }
  7550.       return;
  7551.     }

  7552.   cu->line_header = lh;
  7553.   make_cleanup (free_cu_line_header, cu);

  7554.   if (first_time)
  7555.     {
  7556.       struct compunit_symtab *cust = dwarf2_start_symtab (cu, "", NULL, 0);

  7557.       tu_group->num_symtabs = lh->num_file_names;
  7558.       tu_group->symtabs = XNEWVEC (struct symtab *, lh->num_file_names);

  7559.       for (i = 0; i < lh->num_file_names; ++i)
  7560.         {
  7561.           const char *dir = NULL;
  7562.           struct file_entry *fe = &lh->file_names[i];

  7563.           if (fe->dir_index)
  7564.             dir = lh->include_dirs[fe->dir_index - 1];
  7565.           dwarf2_start_subfile (fe->name, dir);

  7566.           if (current_subfile->symtab == NULL)
  7567.             {
  7568.               /* NOTE: start_subfile will recognize when it's been passed
  7569.                  a file it has already seen.  So we can't assume there's a
  7570.                  simple mapping from lh->file_names to subfiles, plus
  7571.                  lh->file_names may contain dups.  */
  7572.               current_subfile->symtab
  7573.                 = allocate_symtab (cust, current_subfile->name);
  7574.             }

  7575.           fe->symtab = current_subfile->symtab;
  7576.           tu_group->symtabs[i] = fe->symtab;
  7577.         }
  7578.     }
  7579.   else
  7580.     {
  7581.       restart_symtab (tu_group->compunit_symtab, "", 0);

  7582.       for (i = 0; i < lh->num_file_names; ++i)
  7583.         {
  7584.           struct file_entry *fe = &lh->file_names[i];

  7585.           fe->symtab = tu_group->symtabs[i];
  7586.         }
  7587.     }

  7588.   /* The main symtab is allocated last.  Type units don't have DW_AT_name
  7589.      so they don't have a "real" (so to speak) symtab anyway.
  7590.      There is later code that will assign the main symtab to all symbols
  7591.      that don't have one.  We need to handle the case of a symbol with a
  7592.      missing symtab (DW_AT_decl_file) anyway.  */
  7593. }

  7594. /* Process DW_TAG_type_unit.
  7595.    For TUs we want to skip the first top level sibling if it's not the
  7596.    actual type being defined by this TU.  In this case the first top
  7597.    level sibling is there to provide context only.  */

  7598. static void
  7599. read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
  7600. {
  7601.   struct die_info *child_die;

  7602.   prepare_one_comp_unit (cu, die, language_minimal);

  7603.   /* Initialize (or reinitialize) the machinery for building symtabs.
  7604.      We do this before processing child DIEs, so that the line header table
  7605.      is available for DW_AT_decl_file.  */
  7606.   setup_type_unit_groups (die, cu);

  7607.   if (die->child != NULL)
  7608.     {
  7609.       child_die = die->child;
  7610.       while (child_die && child_die->tag)
  7611.         {
  7612.           process_die (child_die, cu);
  7613.           child_die = sibling_die (child_die);
  7614.         }
  7615.     }
  7616. }

  7617. /* DWO/DWP files.

  7618.    http://gcc.gnu.org/wiki/DebugFission
  7619.    http://gcc.gnu.org/wiki/DebugFissionDWP

  7620.    To simplify handling of both DWO files ("object" files with the DWARF info)
  7621.    and DWP files (a file with the DWOs packaged up into one file), we treat
  7622.    DWP files as having a collection of virtual DWO files.  */

  7623. static hashval_t
  7624. hash_dwo_file (const void *item)
  7625. {
  7626.   const struct dwo_file *dwo_file = item;
  7627.   hashval_t hash;

  7628.   hash = htab_hash_string (dwo_file->dwo_name);
  7629.   if (dwo_file->comp_dir != NULL)
  7630.     hash += htab_hash_string (dwo_file->comp_dir);
  7631.   return hash;
  7632. }

  7633. static int
  7634. eq_dwo_file (const void *item_lhs, const void *item_rhs)
  7635. {
  7636.   const struct dwo_file *lhs = item_lhs;
  7637.   const struct dwo_file *rhs = item_rhs;

  7638.   if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
  7639.     return 0;
  7640.   if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
  7641.     return lhs->comp_dir == rhs->comp_dir;
  7642.   return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
  7643. }

  7644. /* Allocate a hash table for DWO files.  */

  7645. static htab_t
  7646. allocate_dwo_file_hash_table (void)
  7647. {
  7648.   struct objfile *objfile = dwarf2_per_objfile->objfile;

  7649.   return htab_create_alloc_ex (41,
  7650.                                hash_dwo_file,
  7651.                                eq_dwo_file,
  7652.                                NULL,
  7653.                                &objfile->objfile_obstack,
  7654.                                hashtab_obstack_allocate,
  7655.                                dummy_obstack_deallocate);
  7656. }

  7657. /* Lookup DWO file DWO_NAME.  */

  7658. static void **
  7659. lookup_dwo_file_slot (const char *dwo_name, const char *comp_dir)
  7660. {
  7661.   struct dwo_file find_entry;
  7662.   void **slot;

  7663.   if (dwarf2_per_objfile->dwo_files == NULL)
  7664.     dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();

  7665.   memset (&find_entry, 0, sizeof (find_entry));
  7666.   find_entry.dwo_name = dwo_name;
  7667.   find_entry.comp_dir = comp_dir;
  7668.   slot = htab_find_slot (dwarf2_per_objfile->dwo_files, &find_entry, INSERT);

  7669.   return slot;
  7670. }

  7671. static hashval_t
  7672. hash_dwo_unit (const void *item)
  7673. {
  7674.   const struct dwo_unit *dwo_unit = item;

  7675.   /* This drops the top 32 bits of the id, but is ok for a hash.  */
  7676.   return dwo_unit->signature;
  7677. }

  7678. static int
  7679. eq_dwo_unit (const void *item_lhs, const void *item_rhs)
  7680. {
  7681.   const struct dwo_unit *lhs = item_lhs;
  7682.   const struct dwo_unit *rhs = item_rhs;

  7683.   /* The signature is assumed to be unique within the DWO file.
  7684.      So while object file CU dwo_id's always have the value zero,
  7685.      that's OK, assuming each object file DWO file has only one CU,
  7686.      and that's the rule for now.  */
  7687.   return lhs->signature == rhs->signature;
  7688. }

  7689. /* Allocate a hash table for DWO CUs,TUs.
  7690.    There is one of these tables for each of CUs,TUs for each DWO file.  */

  7691. static htab_t
  7692. allocate_dwo_unit_table (struct objfile *objfile)
  7693. {
  7694.   /* Start out with a pretty small number.
  7695.      Generally DWO files contain only one CU and maybe some TUs.  */
  7696.   return htab_create_alloc_ex (3,
  7697.                                hash_dwo_unit,
  7698.                                eq_dwo_unit,
  7699.                                NULL,
  7700.                                &objfile->objfile_obstack,
  7701.                                hashtab_obstack_allocate,
  7702.                                dummy_obstack_deallocate);
  7703. }

  7704. /* Structure used to pass data to create_dwo_debug_info_hash_table_reader.  */

  7705. struct create_dwo_cu_data
  7706. {
  7707.   struct dwo_file *dwo_file;
  7708.   struct dwo_unit dwo_unit;
  7709. };

  7710. /* die_reader_func for create_dwo_cu.  */

  7711. static void
  7712. create_dwo_cu_reader (const struct die_reader_specs *reader,
  7713.                       const gdb_byte *info_ptr,
  7714.                       struct die_info *comp_unit_die,
  7715.                       int has_children,
  7716.                       void *datap)
  7717. {
  7718.   struct dwarf2_cu *cu = reader->cu;
  7719.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  7720.   sect_offset offset = cu->per_cu->offset;
  7721.   struct dwarf2_section_info *section = cu->per_cu->section;
  7722.   struct create_dwo_cu_data *data = datap;
  7723.   struct dwo_file *dwo_file = data->dwo_file;
  7724.   struct dwo_unit *dwo_unit = &data->dwo_unit;
  7725.   struct attribute *attr;

  7726.   attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
  7727.   if (attr == NULL)
  7728.     {
  7729.       complaint (&symfile_complaints,
  7730.                  _("Dwarf Error: debug entry at offset 0x%x is missing"
  7731.                    " its dwo_id [in module %s]"),
  7732.                  offset.sect_off, dwo_file->dwo_name);
  7733.       return;
  7734.     }

  7735.   dwo_unit->dwo_file = dwo_file;
  7736.   dwo_unit->signature = DW_UNSND (attr);
  7737.   dwo_unit->section = section;
  7738.   dwo_unit->offset = offset;
  7739.   dwo_unit->length = cu->per_cu->length;

  7740.   if (dwarf2_read_debug)
  7741.     fprintf_unfiltered (gdb_stdlog, "  offset 0x%x, dwo_id %s\n",
  7742.                         offset.sect_off, hex_string (dwo_unit->signature));
  7743. }

  7744. /* Create the dwo_unit for the lone CU in DWO_FILE.
  7745.    Note: This function processes DWO files only, not DWP files.  */

  7746. static struct dwo_unit *
  7747. create_dwo_cu (struct dwo_file *dwo_file)
  7748. {
  7749.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  7750.   struct dwarf2_section_info *section = &dwo_file->sections.info;
  7751.   bfd *abfd;
  7752.   htab_t cu_htab;
  7753.   const gdb_byte *info_ptr, *end_ptr;
  7754.   struct create_dwo_cu_data create_dwo_cu_data;
  7755.   struct dwo_unit *dwo_unit;

  7756.   dwarf2_read_section (objfile, section);
  7757.   info_ptr = section->buffer;

  7758.   if (info_ptr == NULL)
  7759.     return NULL;

  7760.   /* We can't set abfd until now because the section may be empty or
  7761.      not present, in which case section->asection will be NULL.  */
  7762.   abfd = get_section_bfd_owner (section);

  7763.   if (dwarf2_read_debug)
  7764.     {
  7765.       fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
  7766.                           get_section_name (section),
  7767.                           get_section_file_name (section));
  7768.     }

  7769.   create_dwo_cu_data.dwo_file = dwo_file;
  7770.   dwo_unit = NULL;

  7771.   end_ptr = info_ptr + section->size;
  7772.   while (info_ptr < end_ptr)
  7773.     {
  7774.       struct dwarf2_per_cu_data per_cu;

  7775.       memset (&create_dwo_cu_data.dwo_unit, 0,
  7776.               sizeof (create_dwo_cu_data.dwo_unit));
  7777.       memset (&per_cu, 0, sizeof (per_cu));
  7778.       per_cu.objfile = objfile;
  7779.       per_cu.is_debug_types = 0;
  7780.       per_cu.offset.sect_off = info_ptr - section->buffer;
  7781.       per_cu.section = section;

  7782.       init_cutu_and_read_dies_no_follow (&per_cu, dwo_file,
  7783.                                          create_dwo_cu_reader,
  7784.                                          &create_dwo_cu_data);

  7785.       if (create_dwo_cu_data.dwo_unit.dwo_file != NULL)
  7786.         {
  7787.           /* If we've already found one, complain.  We only support one
  7788.              because having more than one requires hacking the dwo_name of
  7789.              each to match, which is highly unlikely to happen.  */
  7790.           if (dwo_unit != NULL)
  7791.             {
  7792.               complaint (&symfile_complaints,
  7793.                          _("Multiple CUs in DWO file %s [in module %s]"),
  7794.                          dwo_file->dwo_name, objfile_name (objfile));
  7795.               break;
  7796.             }

  7797.           dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
  7798.           *dwo_unit = create_dwo_cu_data.dwo_unit;
  7799.         }

  7800.       info_ptr += per_cu.length;
  7801.     }

  7802.   return dwo_unit;
  7803. }

  7804. /* DWP file .debug_{cu,tu}_index section format:
  7805.    [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]

  7806.    DWP Version 1:

  7807.    Both index sections have the same format, and serve to map a 64-bit
  7808.    signature to a set of section numbers.  Each section begins with a header,
  7809.    followed by a hash table of 64-bit signatures, a parallel table of 32-bit
  7810.    indexes, and a pool of 32-bit section numbers.  The index sections will be
  7811.    aligned at 8-byte boundaries in the file.

  7812.    The index section header consists of:

  7813.     V, 32 bit version number
  7814.     -, 32 bits unused
  7815.     N, 32 bit number of compilation units or type units in the index
  7816.     M, 32 bit number of slots in the hash table

  7817.    Numbers are recorded using the byte order of the application binary.

  7818.    The hash table begins at offset 16 in the section, and consists of an array
  7819.    of M 64-bit slots.  Each slot contains a 64-bit signature (using the byte
  7820.    order of the application binary).  Unused slots in the hash table are 0.
  7821.    (We rely on the extreme unlikeliness of a signature being exactly 0.)

  7822.    The parallel table begins immediately after the hash table
  7823.    (at offset 16 + 8 * M from the beginning of the section), and consists of an
  7824.    array of 32-bit indexes (using the byte order of the application binary),
  7825.    corresponding 1-1 with slots in the hash table.  Each entry in the parallel
  7826.    table contains a 32-bit index into the pool of section numbers.  For unused
  7827.    hash table slots, the corresponding entry in the parallel table will be 0.

  7828.    The pool of section numbers begins immediately following the hash table
  7829.    (at offset 16 + 12 * M from the beginning of the section).  The pool of
  7830.    section numbers consists of an array of 32-bit words (using the byte order
  7831.    of the application binary).  Each item in the array is indexed starting
  7832.    from 0.  The hash table entry provides the index of the first section
  7833.    number in the set.  Additional section numbers in the set follow, and the
  7834.    set is terminated by a 0 entry (section number 0 is not used in ELF).

  7835.    In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
  7836.    section must be the first entry in the set, and the .debug_abbrev.dwo must
  7837.    be the second entry. Other members of the set may follow in any order.

  7838.    ---

  7839.    DWP Version 2:

  7840.    DWP Version 2 combines all the .debug_info, etc. sections into one,
  7841.    and the entries in the index tables are now offsets into these sections.
  7842.    CU offsets begin at 0.  TU offsets begin at the size of the .debug_info
  7843.    section.

  7844.    Index Section Contents:
  7845.     Header
  7846.     Hash Table of Signatures   dwp_hash_table.hash_table
  7847.     Parallel Table of Indices  dwp_hash_table.unit_table
  7848.     Table of Section Offsets   dwp_hash_table.v2.{section_ids,offsets}
  7849.     Table of Section Sizes     dwp_hash_table.v2.sizes

  7850.    The index section header consists of:

  7851.     V, 32 bit version number
  7852.     L, 32 bit number of columns in the table of section offsets
  7853.     N, 32 bit number of compilation units or type units in the index
  7854.     M, 32 bit number of slots in the hash table

  7855.    Numbers are recorded using the byte order of the application binary.

  7856.    The hash table has the same format as version 1.
  7857.    The parallel table of indices has the same format as version 1,
  7858.    except that the entries are origin-1 indices into the table of sections
  7859.    offsets and the table of section sizes.

  7860.    The table of offsets begins immediately following the parallel table
  7861.    (at offset 16 + 12 * M from the beginning of the section).  The table is
  7862.    a two-dimensional array of 32-bit words (using the byte order of the
  7863.    application binary), with L columns and N+1 rows, in row-major order.
  7864.    Each row in the array is indexed starting from 0.  The first row provides
  7865.    a key to the remaining rows: each column in this row provides an identifier
  7866.    for a debug section, and the offsets in the same column of subsequent rows
  7867.    refer to that section.  The section identifiers are:

  7868.     DW_SECT_INFO         1  .debug_info.dwo
  7869.     DW_SECT_TYPES        2  .debug_types.dwo
  7870.     DW_SECT_ABBREV       3  .debug_abbrev.dwo
  7871.     DW_SECT_LINE         4  .debug_line.dwo
  7872.     DW_SECT_LOC          5  .debug_loc.dwo
  7873.     DW_SECT_STR_OFFSETS  6  .debug_str_offsets.dwo
  7874.     DW_SECT_MACINFO      7  .debug_macinfo.dwo
  7875.     DW_SECT_MACRO        8  .debug_macro.dwo

  7876.    The offsets provided by the CU and TU index sections are the base offsets
  7877.    for the contributions made by each CU or TU to the corresponding section
  7878.    in the package file.  Each CU and TU header contains an abbrev_offset
  7879.    field, used to find the abbreviations table for that CU or TU within the
  7880.    contribution to the .debug_abbrev.dwo section for that CU or TU, and should
  7881.    be interpreted as relative to the base offset given in the index section.
  7882.    Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
  7883.    should be interpreted as relative to the base offset for .debug_line.dwo,
  7884.    and offsets into other debug sections obtained from DWARF attributes should
  7885.    also be interpreted as relative to the corresponding base offset.

  7886.    The table of sizes begins immediately following the table of offsets.
  7887.    Like the table of offsets, it is a two-dimensional array of 32-bit words,
  7888.    with L columns and N rows, in row-major order.  Each row in the array is
  7889.    indexed starting from 1 (row 0 is shared by the two tables).

  7890.    ---

  7891.    Hash table lookup is handled the same in version 1 and 2:

  7892.    We assume that N and M will not exceed 2^32 - 1.
  7893.    The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.

  7894.    Given a 64-bit compilation unit signature or a type signature S, an entry
  7895.    in the hash table is located as follows:

  7896.    1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
  7897.       the low-order k bits all set to 1.

  7898.    2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).

  7899.    3) If the hash table entry at index H matches the signature, use that
  7900.       entry.  If the hash table entry at index H is unused (all zeroes),
  7901.       terminate the search: the signature is not present in the table.

  7902.    4) Let H = (H + H') modulo M. Repeat at Step 3.

  7903.    Because M > N and H' and M are relatively prime, the search is guaranteed
  7904.    to stop at an unused slot or find the match.  */

  7905. /* Create a hash table to map DWO IDs to their CU/TU entry in
  7906.    .debug_{info,types}.dwo in DWP_FILE.
  7907.    Returns NULL if there isn't one.
  7908.    Note: This function processes DWP files only, not DWO files.  */

  7909. static struct dwp_hash_table *
  7910. create_dwp_hash_table (struct dwp_file *dwp_file, int is_debug_types)
  7911. {
  7912.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  7913.   bfd *dbfd = dwp_file->dbfd;
  7914.   const gdb_byte *index_ptr, *index_end;
  7915.   struct dwarf2_section_info *index;
  7916.   uint32_t version, nr_columns, nr_units, nr_slots;
  7917.   struct dwp_hash_table *htab;

  7918.   if (is_debug_types)
  7919.     index = &dwp_file->sections.tu_index;
  7920.   else
  7921.     index = &dwp_file->sections.cu_index;

  7922.   if (dwarf2_section_empty_p (index))
  7923.     return NULL;
  7924.   dwarf2_read_section (objfile, index);

  7925.   index_ptr = index->buffer;
  7926.   index_end = index_ptr + index->size;

  7927.   version = read_4_bytes (dbfd, index_ptr);
  7928.   index_ptr += 4;
  7929.   if (version == 2)
  7930.     nr_columns = read_4_bytes (dbfd, index_ptr);
  7931.   else
  7932.     nr_columns = 0;
  7933.   index_ptr += 4;
  7934.   nr_units = read_4_bytes (dbfd, index_ptr);
  7935.   index_ptr += 4;
  7936.   nr_slots = read_4_bytes (dbfd, index_ptr);
  7937.   index_ptr += 4;

  7938.   if (version != 1 && version != 2)
  7939.     {
  7940.       error (_("Dwarf Error: unsupported DWP file version (%s)"
  7941.                " [in module %s]"),
  7942.              pulongest (version), dwp_file->name);
  7943.     }
  7944.   if (nr_slots != (nr_slots & -nr_slots))
  7945.     {
  7946.       error (_("Dwarf Error: number of slots in DWP hash table (%s)"
  7947.                " is not power of 2 [in module %s]"),
  7948.              pulongest (nr_slots), dwp_file->name);
  7949.     }

  7950.   htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
  7951.   htab->version = version;
  7952.   htab->nr_columns = nr_columns;
  7953.   htab->nr_units = nr_units;
  7954.   htab->nr_slots = nr_slots;
  7955.   htab->hash_table = index_ptr;
  7956.   htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;

  7957.   /* Exit early if the table is empty.  */
  7958.   if (nr_slots == 0 || nr_units == 0
  7959.       || (version == 2 && nr_columns == 0))
  7960.     {
  7961.       /* All must be zero.  */
  7962.       if (nr_slots != 0 || nr_units != 0
  7963.           || (version == 2 && nr_columns != 0))
  7964.         {
  7965.           complaint (&symfile_complaints,
  7966.                      _("Empty DWP but nr_slots,nr_units,nr_columns not"
  7967.                        " all zero [in modules %s]"),
  7968.                      dwp_file->name);
  7969.         }
  7970.       return htab;
  7971.     }

  7972.   if (version == 1)
  7973.     {
  7974.       htab->section_pool.v1.indices =
  7975.         htab->unit_table + sizeof (uint32_t) * nr_slots;
  7976.       /* It's harder to decide whether the section is too small in v1.
  7977.          V1 is deprecated anyway so we punt.  */
  7978.     }
  7979.   else
  7980.     {
  7981.       const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
  7982.       int *ids = htab->section_pool.v2.section_ids;
  7983.       /* Reverse map for error checking.  */
  7984.       int ids_seen[DW_SECT_MAX + 1];
  7985.       int i;

  7986.       if (nr_columns < 2)
  7987.         {
  7988.           error (_("Dwarf Error: bad DWP hash table, too few columns"
  7989.                    " in section table [in module %s]"),
  7990.                  dwp_file->name);
  7991.         }
  7992.       if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
  7993.         {
  7994.           error (_("Dwarf Error: bad DWP hash table, too many columns"
  7995.                    " in section table [in module %s]"),
  7996.                  dwp_file->name);
  7997.         }
  7998.       memset (ids, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
  7999.       memset (ids_seen, 255, (DW_SECT_MAX + 1) * sizeof (int32_t));
  8000.       for (i = 0; i < nr_columns; ++i)
  8001.         {
  8002.           int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));

  8003.           if (id < DW_SECT_MIN || id > DW_SECT_MAX)
  8004.             {
  8005.               error (_("Dwarf Error: bad DWP hash table, bad section id %d"
  8006.                        " in section table [in module %s]"),
  8007.                      id, dwp_file->name);
  8008.             }
  8009.           if (ids_seen[id] != -1)
  8010.             {
  8011.               error (_("Dwarf Error: bad DWP hash table, duplicate section"
  8012.                        " id %d in section table [in module %s]"),
  8013.                      id, dwp_file->name);
  8014.             }
  8015.           ids_seen[id] = i;
  8016.           ids[i] = id;
  8017.         }
  8018.       /* Must have exactly one info or types section.  */
  8019.       if (((ids_seen[DW_SECT_INFO] != -1)
  8020.            + (ids_seen[DW_SECT_TYPES] != -1))
  8021.           != 1)
  8022.         {
  8023.           error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
  8024.                    " DWO info/types section [in module %s]"),
  8025.                  dwp_file->name);
  8026.         }
  8027.       /* Must have an abbrev section.  */
  8028.       if (ids_seen[DW_SECT_ABBREV] == -1)
  8029.         {
  8030.           error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
  8031.                    " section [in module %s]"),
  8032.                  dwp_file->name);
  8033.         }
  8034.       htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
  8035.       htab->section_pool.v2.sizes =
  8036.         htab->section_pool.v2.offsets + (sizeof (uint32_t)
  8037.                                          * nr_units * nr_columns);
  8038.       if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
  8039.                                           * nr_units * nr_columns))
  8040.           > index_end)
  8041.         {
  8042.           error (_("Dwarf Error: DWP index section is corrupt (too small)"
  8043.                    " [in module %s]"),
  8044.                  dwp_file->name);
  8045.         }
  8046.     }

  8047.   return htab;
  8048. }

  8049. /* Update SECTIONS with the data from SECTP.

  8050.    This function is like the other "locate" section routines that are
  8051.    passed to bfd_map_over_sections, but in this context the sections to
  8052.    read comes from the DWP V1 hash table, not the full ELF section table.

  8053.    The result is non-zero for success, or zero if an error was found.  */

  8054. static int
  8055. locate_v1_virtual_dwo_sections (asection *sectp,
  8056.                                 struct virtual_v1_dwo_sections *sections)
  8057. {
  8058.   const struct dwop_section_names *names = &dwop_section_names;

  8059.   if (section_is_p (sectp->name, &names->abbrev_dwo))
  8060.     {
  8061.       /* There can be only one.  */
  8062.       if (sections->abbrev.s.asection != NULL)
  8063.         return 0;
  8064.       sections->abbrev.s.asection = sectp;
  8065.       sections->abbrev.size = bfd_get_section_size (sectp);
  8066.     }
  8067.   else if (section_is_p (sectp->name, &names->info_dwo)
  8068.            || section_is_p (sectp->name, &names->types_dwo))
  8069.     {
  8070.       /* There can be only one.  */
  8071.       if (sections->info_or_types.s.asection != NULL)
  8072.         return 0;
  8073.       sections->info_or_types.s.asection = sectp;
  8074.       sections->info_or_types.size = bfd_get_section_size (sectp);
  8075.     }
  8076.   else if (section_is_p (sectp->name, &names->line_dwo))
  8077.     {
  8078.       /* There can be only one.  */
  8079.       if (sections->line.s.asection != NULL)
  8080.         return 0;
  8081.       sections->line.s.asection = sectp;
  8082.       sections->line.size = bfd_get_section_size (sectp);
  8083.     }
  8084.   else if (section_is_p (sectp->name, &names->loc_dwo))
  8085.     {
  8086.       /* There can be only one.  */
  8087.       if (sections->loc.s.asection != NULL)
  8088.         return 0;
  8089.       sections->loc.s.asection = sectp;
  8090.       sections->loc.size = bfd_get_section_size (sectp);
  8091.     }
  8092.   else if (section_is_p (sectp->name, &names->macinfo_dwo))
  8093.     {
  8094.       /* There can be only one.  */
  8095.       if (sections->macinfo.s.asection != NULL)
  8096.         return 0;
  8097.       sections->macinfo.s.asection = sectp;
  8098.       sections->macinfo.size = bfd_get_section_size (sectp);
  8099.     }
  8100.   else if (section_is_p (sectp->name, &names->macro_dwo))
  8101.     {
  8102.       /* There can be only one.  */
  8103.       if (sections->macro.s.asection != NULL)
  8104.         return 0;
  8105.       sections->macro.s.asection = sectp;
  8106.       sections->macro.size = bfd_get_section_size (sectp);
  8107.     }
  8108.   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
  8109.     {
  8110.       /* There can be only one.  */
  8111.       if (sections->str_offsets.s.asection != NULL)
  8112.         return 0;
  8113.       sections->str_offsets.s.asection = sectp;
  8114.       sections->str_offsets.size = bfd_get_section_size (sectp);
  8115.     }
  8116.   else
  8117.     {
  8118.       /* No other kind of section is valid.  */
  8119.       return 0;
  8120.     }

  8121.   return 1;
  8122. }

  8123. /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
  8124.    UNIT_INDEX is the index of the DWO unit in the DWP hash table.
  8125.    COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
  8126.    This is for DWP version 1 files.  */

  8127. static struct dwo_unit *
  8128. create_dwo_unit_in_dwp_v1 (struct dwp_file *dwp_file,
  8129.                            uint32_t unit_index,
  8130.                            const char *comp_dir,
  8131.                            ULONGEST signature, int is_debug_types)
  8132. {
  8133.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  8134.   const struct dwp_hash_table *dwp_htab =
  8135.     is_debug_types ? dwp_file->tus : dwp_file->cus;
  8136.   bfd *dbfd = dwp_file->dbfd;
  8137.   const char *kind = is_debug_types ? "TU" : "CU";
  8138.   struct dwo_file *dwo_file;
  8139.   struct dwo_unit *dwo_unit;
  8140.   struct virtual_v1_dwo_sections sections;
  8141.   void **dwo_file_slot;
  8142.   char *virtual_dwo_name;
  8143.   struct dwarf2_section_info *cutu;
  8144.   struct cleanup *cleanups;
  8145.   int i;

  8146.   gdb_assert (dwp_file->version == 1);

  8147.   if (dwarf2_read_debug)
  8148.     {
  8149.       fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
  8150.                           kind,
  8151.                           pulongest (unit_index), hex_string (signature),
  8152.                           dwp_file->name);
  8153.     }

  8154.   /* Fetch the sections of this DWO unit.
  8155.      Put a limit on the number of sections we look for so that bad data
  8156.      doesn't cause us to loop forever.  */

  8157. #define MAX_NR_V1_DWO_SECTIONS \
  8158.   (1 /* .debug_info or .debug_types */ \
  8159.    + 1 /* .debug_abbrev */ \
  8160.    + 1 /* .debug_line */ \
  8161.    + 1 /* .debug_loc */ \
  8162.    + 1 /* .debug_str_offsets */ \
  8163.    + 1 /* .debug_macro or .debug_macinfo */ \
  8164.    + 1 /* trailing zero */)

  8165.   memset (&sections, 0, sizeof (sections));
  8166.   cleanups = make_cleanup (null_cleanup, 0);

  8167.   for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
  8168.     {
  8169.       asection *sectp;
  8170.       uint32_t section_nr =
  8171.         read_4_bytes (dbfd,
  8172.                       dwp_htab->section_pool.v1.indices
  8173.                       + (unit_index + i) * sizeof (uint32_t));

  8174.       if (section_nr == 0)
  8175.         break;
  8176.       if (section_nr >= dwp_file->num_sections)
  8177.         {
  8178.           error (_("Dwarf Error: bad DWP hash table, section number too large"
  8179.                    " [in module %s]"),
  8180.                  dwp_file->name);
  8181.         }

  8182.       sectp = dwp_file->elf_sections[section_nr];
  8183.       if (! locate_v1_virtual_dwo_sections (sectp, &sections))
  8184.         {
  8185.           error (_("Dwarf Error: bad DWP hash table, invalid section found"
  8186.                    " [in module %s]"),
  8187.                  dwp_file->name);
  8188.         }
  8189.     }

  8190.   if (i < 2
  8191.       || dwarf2_section_empty_p (&sections.info_or_types)
  8192.       || dwarf2_section_empty_p (&sections.abbrev))
  8193.     {
  8194.       error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
  8195.                " [in module %s]"),
  8196.              dwp_file->name);
  8197.     }
  8198.   if (i == MAX_NR_V1_DWO_SECTIONS)
  8199.     {
  8200.       error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
  8201.                " [in module %s]"),
  8202.              dwp_file->name);
  8203.     }

  8204.   /* It's easier for the rest of the code if we fake a struct dwo_file and
  8205.      have dwo_unit "live" in that.  At least for now.

  8206.      The DWP file can be made up of a random collection of CUs and TUs.
  8207.      However, for each CU + set of TUs that came from the same original DWO
  8208.      file, we can combine them back into a virtual DWO file to save space
  8209.      (fewer struct dwo_file objects to allocate).  Remember that for really
  8210.      large apps there can be on the order of 8K CUs and 200K TUs, or more.  */

  8211.   virtual_dwo_name =
  8212.     xstrprintf ("virtual-dwo/%d-%d-%d-%d",
  8213.                 get_section_id (&sections.abbrev),
  8214.                 get_section_id (&sections.line),
  8215.                 get_section_id (&sections.loc),
  8216.                 get_section_id (&sections.str_offsets));
  8217.   make_cleanup (xfree, virtual_dwo_name);
  8218.   /* Can we use an existing virtual DWO file?  */
  8219.   dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
  8220.   /* Create one if necessary.  */
  8221.   if (*dwo_file_slot == NULL)
  8222.     {
  8223.       if (dwarf2_read_debug)
  8224.         {
  8225.           fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
  8226.                               virtual_dwo_name);
  8227.         }
  8228.       dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
  8229.       dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
  8230.                                           virtual_dwo_name,
  8231.                                           strlen (virtual_dwo_name));
  8232.       dwo_file->comp_dir = comp_dir;
  8233.       dwo_file->sections.abbrev = sections.abbrev;
  8234.       dwo_file->sections.line = sections.line;
  8235.       dwo_file->sections.loc = sections.loc;
  8236.       dwo_file->sections.macinfo = sections.macinfo;
  8237.       dwo_file->sections.macro = sections.macro;
  8238.       dwo_file->sections.str_offsets = sections.str_offsets;
  8239.       /* The "str" section is global to the entire DWP file.  */
  8240.       dwo_file->sections.str = dwp_file->sections.str;
  8241.       /* The info or types section is assigned below to dwo_unit,
  8242.          there's no need to record it in dwo_file.
  8243.          Also, we can't simply record type sections in dwo_file because
  8244.          we record a pointer into the vector in dwo_unit.  As we collect more
  8245.          types we'll grow the vector and eventually have to reallocate space
  8246.          for it, invalidating all copies of pointers into the previous
  8247.          contents.  */
  8248.       *dwo_file_slot = dwo_file;
  8249.     }
  8250.   else
  8251.     {
  8252.       if (dwarf2_read_debug)
  8253.         {
  8254.           fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
  8255.                               virtual_dwo_name);
  8256.         }
  8257.       dwo_file = *dwo_file_slot;
  8258.     }
  8259.   do_cleanups (cleanups);

  8260.   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
  8261.   dwo_unit->dwo_file = dwo_file;
  8262.   dwo_unit->signature = signature;
  8263.   dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
  8264.                                      sizeof (struct dwarf2_section_info));
  8265.   *dwo_unit->section = sections.info_or_types;
  8266.   /* dwo_unit->{offset,length,type_offset_in_tu} are set later.  */

  8267.   return dwo_unit;
  8268. }

  8269. /* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
  8270.    Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
  8271.    piece within that section used by a TU/CU, return a virtual section
  8272.    of just that piece.  */

  8273. static struct dwarf2_section_info
  8274. create_dwp_v2_section (struct dwarf2_section_info *section,
  8275.                        bfd_size_type offset, bfd_size_type size)
  8276. {
  8277.   struct dwarf2_section_info result;
  8278.   asection *sectp;

  8279.   gdb_assert (section != NULL);
  8280.   gdb_assert (!section->is_virtual);

  8281.   memset (&result, 0, sizeof (result));
  8282.   result.s.containing_section = section;
  8283.   result.is_virtual = 1;

  8284.   if (size == 0)
  8285.     return result;

  8286.   sectp = get_section_bfd_section (section);

  8287.   /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
  8288.      bounds of the real section.  This is a pretty-rare event, so just
  8289.      flag an error (easier) instead of a warning and trying to cope.  */
  8290.   if (sectp == NULL
  8291.       || offset + size > bfd_get_section_size (sectp))
  8292.     {
  8293.       bfd *abfd = sectp->owner;

  8294.       error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
  8295.                " in section %s [in module %s]"),
  8296.              sectp ? bfd_section_name (abfd, sectp) : "<unknown>",
  8297.              objfile_name (dwarf2_per_objfile->objfile));
  8298.     }

  8299.   result.virtual_offset = offset;
  8300.   result.size = size;
  8301.   return result;
  8302. }

  8303. /* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
  8304.    UNIT_INDEX is the index of the DWO unit in the DWP hash table.
  8305.    COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
  8306.    This is for DWP version 2 files.  */

  8307. static struct dwo_unit *
  8308. create_dwo_unit_in_dwp_v2 (struct dwp_file *dwp_file,
  8309.                            uint32_t unit_index,
  8310.                            const char *comp_dir,
  8311.                            ULONGEST signature, int is_debug_types)
  8312. {
  8313.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  8314.   const struct dwp_hash_table *dwp_htab =
  8315.     is_debug_types ? dwp_file->tus : dwp_file->cus;
  8316.   bfd *dbfd = dwp_file->dbfd;
  8317.   const char *kind = is_debug_types ? "TU" : "CU";
  8318.   struct dwo_file *dwo_file;
  8319.   struct dwo_unit *dwo_unit;
  8320.   struct virtual_v2_dwo_sections sections;
  8321.   void **dwo_file_slot;
  8322.   char *virtual_dwo_name;
  8323.   struct dwarf2_section_info *cutu;
  8324.   struct cleanup *cleanups;
  8325.   int i;

  8326.   gdb_assert (dwp_file->version == 2);

  8327.   if (dwarf2_read_debug)
  8328.     {
  8329.       fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
  8330.                           kind,
  8331.                           pulongest (unit_index), hex_string (signature),
  8332.                           dwp_file->name);
  8333.     }

  8334.   /* Fetch the section offsets of this DWO unit.  */

  8335.   memset (&sections, 0, sizeof (sections));
  8336.   cleanups = make_cleanup (null_cleanup, 0);

  8337.   for (i = 0; i < dwp_htab->nr_columns; ++i)
  8338.     {
  8339.       uint32_t offset = read_4_bytes (dbfd,
  8340.                                       dwp_htab->section_pool.v2.offsets
  8341.                                       + (((unit_index - 1) * dwp_htab->nr_columns
  8342.                                           + i)
  8343.                                          * sizeof (uint32_t)));
  8344.       uint32_t size = read_4_bytes (dbfd,
  8345.                                     dwp_htab->section_pool.v2.sizes
  8346.                                     + (((unit_index - 1) * dwp_htab->nr_columns
  8347.                                         + i)
  8348.                                        * sizeof (uint32_t)));

  8349.       switch (dwp_htab->section_pool.v2.section_ids[i])
  8350.         {
  8351.         case DW_SECT_INFO:
  8352.         case DW_SECT_TYPES:
  8353.           sections.info_or_types_offset = offset;
  8354.           sections.info_or_types_size = size;
  8355.           break;
  8356.         case DW_SECT_ABBREV:
  8357.           sections.abbrev_offset = offset;
  8358.           sections.abbrev_size = size;
  8359.           break;
  8360.         case DW_SECT_LINE:
  8361.           sections.line_offset = offset;
  8362.           sections.line_size = size;
  8363.           break;
  8364.         case DW_SECT_LOC:
  8365.           sections.loc_offset = offset;
  8366.           sections.loc_size = size;
  8367.           break;
  8368.         case DW_SECT_STR_OFFSETS:
  8369.           sections.str_offsets_offset = offset;
  8370.           sections.str_offsets_size = size;
  8371.           break;
  8372.         case DW_SECT_MACINFO:
  8373.           sections.macinfo_offset = offset;
  8374.           sections.macinfo_size = size;
  8375.           break;
  8376.         case DW_SECT_MACRO:
  8377.           sections.macro_offset = offset;
  8378.           sections.macro_size = size;
  8379.           break;
  8380.         }
  8381.     }

  8382.   /* It's easier for the rest of the code if we fake a struct dwo_file and
  8383.      have dwo_unit "live" in that.  At least for now.

  8384.      The DWP file can be made up of a random collection of CUs and TUs.
  8385.      However, for each CU + set of TUs that came from the same original DWO
  8386.      file, we can combine them back into a virtual DWO file to save space
  8387.      (fewer struct dwo_file objects to allocate).  Remember that for really
  8388.      large apps there can be on the order of 8K CUs and 200K TUs, or more.  */

  8389.   virtual_dwo_name =
  8390.     xstrprintf ("virtual-dwo/%ld-%ld-%ld-%ld",
  8391.                 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
  8392.                 (long) (sections.line_size ? sections.line_offset : 0),
  8393.                 (long) (sections.loc_size ? sections.loc_offset : 0),
  8394.                 (long) (sections.str_offsets_size
  8395.                         ? sections.str_offsets_offset : 0));
  8396.   make_cleanup (xfree, virtual_dwo_name);
  8397.   /* Can we use an existing virtual DWO file?  */
  8398.   dwo_file_slot = lookup_dwo_file_slot (virtual_dwo_name, comp_dir);
  8399.   /* Create one if necessary.  */
  8400.   if (*dwo_file_slot == NULL)
  8401.     {
  8402.       if (dwarf2_read_debug)
  8403.         {
  8404.           fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
  8405.                               virtual_dwo_name);
  8406.         }
  8407.       dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
  8408.       dwo_file->dwo_name = obstack_copy0 (&objfile->objfile_obstack,
  8409.                                           virtual_dwo_name,
  8410.                                           strlen (virtual_dwo_name));
  8411.       dwo_file->comp_dir = comp_dir;
  8412.       dwo_file->sections.abbrev =
  8413.         create_dwp_v2_section (&dwp_file->sections.abbrev,
  8414.                                sections.abbrev_offset, sections.abbrev_size);
  8415.       dwo_file->sections.line =
  8416.         create_dwp_v2_section (&dwp_file->sections.line,
  8417.                                sections.line_offset, sections.line_size);
  8418.       dwo_file->sections.loc =
  8419.         create_dwp_v2_section (&dwp_file->sections.loc,
  8420.                                sections.loc_offset, sections.loc_size);
  8421.       dwo_file->sections.macinfo =
  8422.         create_dwp_v2_section (&dwp_file->sections.macinfo,
  8423.                                sections.macinfo_offset, sections.macinfo_size);
  8424.       dwo_file->sections.macro =
  8425.         create_dwp_v2_section (&dwp_file->sections.macro,
  8426.                                sections.macro_offset, sections.macro_size);
  8427.       dwo_file->sections.str_offsets =
  8428.         create_dwp_v2_section (&dwp_file->sections.str_offsets,
  8429.                                sections.str_offsets_offset,
  8430.                                sections.str_offsets_size);
  8431.       /* The "str" section is global to the entire DWP file.  */
  8432.       dwo_file->sections.str = dwp_file->sections.str;
  8433.       /* The info or types section is assigned below to dwo_unit,
  8434.          there's no need to record it in dwo_file.
  8435.          Also, we can't simply record type sections in dwo_file because
  8436.          we record a pointer into the vector in dwo_unit.  As we collect more
  8437.          types we'll grow the vector and eventually have to reallocate space
  8438.          for it, invalidating all copies of pointers into the previous
  8439.          contents.  */
  8440.       *dwo_file_slot = dwo_file;
  8441.     }
  8442.   else
  8443.     {
  8444.       if (dwarf2_read_debug)
  8445.         {
  8446.           fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
  8447.                               virtual_dwo_name);
  8448.         }
  8449.       dwo_file = *dwo_file_slot;
  8450.     }
  8451.   do_cleanups (cleanups);

  8452.   dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
  8453.   dwo_unit->dwo_file = dwo_file;
  8454.   dwo_unit->signature = signature;
  8455.   dwo_unit->section = obstack_alloc (&objfile->objfile_obstack,
  8456.                                      sizeof (struct dwarf2_section_info));
  8457.   *dwo_unit->section = create_dwp_v2_section (is_debug_types
  8458.                                               ? &dwp_file->sections.types
  8459.                                               : &dwp_file->sections.info,
  8460.                                               sections.info_or_types_offset,
  8461.                                               sections.info_or_types_size);
  8462.   /* dwo_unit->{offset,length,type_offset_in_tu} are set later.  */

  8463.   return dwo_unit;
  8464. }

  8465. /* Lookup the DWO unit with SIGNATURE in DWP_FILE.
  8466.    Returns NULL if the signature isn't found.  */

  8467. static struct dwo_unit *
  8468. lookup_dwo_unit_in_dwp (struct dwp_file *dwp_file, const char *comp_dir,
  8469.                         ULONGEST signature, int is_debug_types)
  8470. {
  8471.   const struct dwp_hash_table *dwp_htab =
  8472.     is_debug_types ? dwp_file->tus : dwp_file->cus;
  8473.   bfd *dbfd = dwp_file->dbfd;
  8474.   uint32_t mask = dwp_htab->nr_slots - 1;
  8475.   uint32_t hash = signature & mask;
  8476.   uint32_t hash2 = ((signature >> 32) & mask) | 1;
  8477.   unsigned int i;
  8478.   void **slot;
  8479.   struct dwo_unit find_dwo_cu, *dwo_cu;

  8480.   memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
  8481.   find_dwo_cu.signature = signature;
  8482.   slot = htab_find_slot (is_debug_types
  8483.                          ? dwp_file->loaded_tus
  8484.                          : dwp_file->loaded_cus,
  8485.                          &find_dwo_cu, INSERT);

  8486.   if (*slot != NULL)
  8487.     return *slot;

  8488.   /* Use a for loop so that we don't loop forever on bad debug info.  */
  8489.   for (i = 0; i < dwp_htab->nr_slots; ++i)
  8490.     {
  8491.       ULONGEST signature_in_table;

  8492.       signature_in_table =
  8493.         read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
  8494.       if (signature_in_table == signature)
  8495.         {
  8496.           uint32_t unit_index =
  8497.             read_4_bytes (dbfd,
  8498.                           dwp_htab->unit_table + hash * sizeof (uint32_t));

  8499.           if (dwp_file->version == 1)
  8500.             {
  8501.               *slot = create_dwo_unit_in_dwp_v1 (dwp_file, unit_index,
  8502.                                                  comp_dir, signature,
  8503.                                                  is_debug_types);
  8504.             }
  8505.           else
  8506.             {
  8507.               *slot = create_dwo_unit_in_dwp_v2 (dwp_file, unit_index,
  8508.                                                  comp_dir, signature,
  8509.                                                  is_debug_types);
  8510.             }
  8511.           return *slot;
  8512.         }
  8513.       if (signature_in_table == 0)
  8514.         return NULL;
  8515.       hash = (hash + hash2) & mask;
  8516.     }

  8517.   error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
  8518.            " [in module %s]"),
  8519.          dwp_file->name);
  8520. }

  8521. /* Subroutine of open_dwo_file,open_dwp_file to simplify them.
  8522.    Open the file specified by FILE_NAME and hand it off to BFD for
  8523.    preliminary analysis.  Return a newly initialized bfd *, which
  8524.    includes a canonicalized copy of FILE_NAME.
  8525.    If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
  8526.    SEARCH_CWD is true if the current directory is to be searched.
  8527.    It will be searched before debug-file-directory.
  8528.    If successful, the file is added to the bfd include table of the
  8529.    objfile's bfd (see gdb_bfd_record_inclusion).
  8530.    If unable to find/open the file, return NULL.
  8531.    NOTE: This function is derived from symfile_bfd_open.  */

  8532. static bfd *
  8533. try_open_dwop_file (const char *file_name, int is_dwp, int search_cwd)
  8534. {
  8535.   bfd *sym_bfd;
  8536.   int desc, flags;
  8537.   char *absolute_name;
  8538.   /* Blech.  OPF_TRY_CWD_FIRST also disables searching the path list if
  8539.      FILE_NAME contains a '/'.  So we can't use it.  Instead prepend "."
  8540.      to debug_file_directory.  */
  8541.   char *search_path;
  8542.   static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };

  8543.   if (search_cwd)
  8544.     {
  8545.       if (*debug_file_directory != '\0')
  8546.         search_path = concat (".", dirname_separator_string,
  8547.                               debug_file_directory, NULL);
  8548.       else
  8549.         search_path = xstrdup (".");
  8550.     }
  8551.   else
  8552.     search_path = xstrdup (debug_file_directory);

  8553.   flags = OPF_RETURN_REALPATH;
  8554.   if (is_dwp)
  8555.     flags |= OPF_SEARCH_IN_PATH;
  8556.   desc = openp (search_path, flags, file_name,
  8557.                 O_RDONLY | O_BINARY, &absolute_name);
  8558.   xfree (search_path);
  8559.   if (desc < 0)
  8560.     return NULL;

  8561.   sym_bfd = gdb_bfd_open (absolute_name, gnutarget, desc);
  8562.   xfree (absolute_name);
  8563.   if (sym_bfd == NULL)
  8564.     return NULL;
  8565.   bfd_set_cacheable (sym_bfd, 1);

  8566.   if (!bfd_check_format (sym_bfd, bfd_object))
  8567.     {
  8568.       gdb_bfd_unref (sym_bfd); /* This also closes desc.  */
  8569.       return NULL;
  8570.     }

  8571.   /* Success.  Record the bfd as having been included by the objfile's bfd.
  8572.      This is important because things like demangled_names_hash lives in the
  8573.      objfile's per_bfd space and may have references to things like symbol
  8574.      names that live in the DWO/DWP file's per_bfd space.  PR 16426.  */
  8575.   gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd);

  8576.   return sym_bfd;
  8577. }

  8578. /* Try to open DWO file FILE_NAME.
  8579.    COMP_DIR is the DW_AT_comp_dir attribute.
  8580.    The result is the bfd handle of the file.
  8581.    If there is a problem finding or opening the file, return NULL.
  8582.    Upon success, the canonicalized path of the file is stored in the bfd,
  8583.    same as symfile_bfd_open.  */

  8584. static bfd *
  8585. open_dwo_file (const char *file_name, const char *comp_dir)
  8586. {
  8587.   bfd *abfd;

  8588.   if (IS_ABSOLUTE_PATH (file_name))
  8589.     return try_open_dwop_file (file_name, 0 /*is_dwp*/, 0 /*search_cwd*/);

  8590.   /* Before trying the search path, try DWO_NAME in COMP_DIR.  */

  8591.   if (comp_dir != NULL)
  8592.     {
  8593.       char *path_to_try = concat (comp_dir, SLASH_STRING, file_name, NULL);

  8594.       /* NOTE: If comp_dir is a relative path, this will also try the
  8595.          search path, which seems useful.  */
  8596.       abfd = try_open_dwop_file (path_to_try, 0 /*is_dwp*/, 1 /*search_cwd*/);
  8597.       xfree (path_to_try);
  8598.       if (abfd != NULL)
  8599.         return abfd;
  8600.     }

  8601.   /* That didn't work, try debug-file-directory, which, despite its name,
  8602.      is a list of paths.  */

  8603.   if (*debug_file_directory == '\0')
  8604.     return NULL;

  8605.   return try_open_dwop_file (file_name, 0 /*is_dwp*/, 1 /*search_cwd*/);
  8606. }

  8607. /* This function is mapped across the sections and remembers the offset and
  8608.    size of each of the DWO debugging sections we are interested in.  */

  8609. static void
  8610. dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
  8611. {
  8612.   struct dwo_sections *dwo_sections = dwo_sections_ptr;
  8613.   const struct dwop_section_names *names = &dwop_section_names;

  8614.   if (section_is_p (sectp->name, &names->abbrev_dwo))
  8615.     {
  8616.       dwo_sections->abbrev.s.asection = sectp;
  8617.       dwo_sections->abbrev.size = bfd_get_section_size (sectp);
  8618.     }
  8619.   else if (section_is_p (sectp->name, &names->info_dwo))
  8620.     {
  8621.       dwo_sections->info.s.asection = sectp;
  8622.       dwo_sections->info.size = bfd_get_section_size (sectp);
  8623.     }
  8624.   else if (section_is_p (sectp->name, &names->line_dwo))
  8625.     {
  8626.       dwo_sections->line.s.asection = sectp;
  8627.       dwo_sections->line.size = bfd_get_section_size (sectp);
  8628.     }
  8629.   else if (section_is_p (sectp->name, &names->loc_dwo))
  8630.     {
  8631.       dwo_sections->loc.s.asection = sectp;
  8632.       dwo_sections->loc.size = bfd_get_section_size (sectp);
  8633.     }
  8634.   else if (section_is_p (sectp->name, &names->macinfo_dwo))
  8635.     {
  8636.       dwo_sections->macinfo.s.asection = sectp;
  8637.       dwo_sections->macinfo.size = bfd_get_section_size (sectp);
  8638.     }
  8639.   else if (section_is_p (sectp->name, &names->macro_dwo))
  8640.     {
  8641.       dwo_sections->macro.s.asection = sectp;
  8642.       dwo_sections->macro.size = bfd_get_section_size (sectp);
  8643.     }
  8644.   else if (section_is_p (sectp->name, &names->str_dwo))
  8645.     {
  8646.       dwo_sections->str.s.asection = sectp;
  8647.       dwo_sections->str.size = bfd_get_section_size (sectp);
  8648.     }
  8649.   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
  8650.     {
  8651.       dwo_sections->str_offsets.s.asection = sectp;
  8652.       dwo_sections->str_offsets.size = bfd_get_section_size (sectp);
  8653.     }
  8654.   else if (section_is_p (sectp->name, &names->types_dwo))
  8655.     {
  8656.       struct dwarf2_section_info type_section;

  8657.       memset (&type_section, 0, sizeof (type_section));
  8658.       type_section.s.asection = sectp;
  8659.       type_section.size = bfd_get_section_size (sectp);
  8660.       VEC_safe_push (dwarf2_section_info_def, dwo_sections->types,
  8661.                      &type_section);
  8662.     }
  8663. }

  8664. /* Initialize the use of the DWO file specified by DWO_NAME and referenced
  8665.    by PER_CU.  This is for the non-DWP case.
  8666.    The result is NULL if DWO_NAME can't be found.  */

  8667. static struct dwo_file *
  8668. open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
  8669.                         const char *dwo_name, const char *comp_dir)
  8670. {
  8671.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  8672.   struct dwo_file *dwo_file;
  8673.   bfd *dbfd;
  8674.   struct cleanup *cleanups;

  8675.   dbfd = open_dwo_file (dwo_name, comp_dir);
  8676.   if (dbfd == NULL)
  8677.     {
  8678.       if (dwarf2_read_debug)
  8679.         fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
  8680.       return NULL;
  8681.     }
  8682.   dwo_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_file);
  8683.   dwo_file->dwo_name = dwo_name;
  8684.   dwo_file->comp_dir = comp_dir;
  8685.   dwo_file->dbfd = dbfd;

  8686.   cleanups = make_cleanup (free_dwo_file_cleanup, dwo_file);

  8687.   bfd_map_over_sections (dbfd, dwarf2_locate_dwo_sections, &dwo_file->sections);

  8688.   dwo_file->cu = create_dwo_cu (dwo_file);

  8689.   dwo_file->tus = create_debug_types_hash_table (dwo_file,
  8690.                                                  dwo_file->sections.types);

  8691.   discard_cleanups (cleanups);

  8692.   if (dwarf2_read_debug)
  8693.     fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);

  8694.   return dwo_file;
  8695. }

  8696. /* This function is mapped across the sections and remembers the offset and
  8697.    size of each of the DWP debugging sections common to version 1 and 2 that
  8698.    we are interested in.  */

  8699. static void
  8700. dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
  8701.                                    void *dwp_file_ptr)
  8702. {
  8703.   struct dwp_file *dwp_file = dwp_file_ptr;
  8704.   const struct dwop_section_names *names = &dwop_section_names;
  8705.   unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;

  8706.   /* Record the ELF section number for later lookup: this is what the
  8707.      .debug_cu_index,.debug_tu_index tables use in DWP V1.  */
  8708.   gdb_assert (elf_section_nr < dwp_file->num_sections);
  8709.   dwp_file->elf_sections[elf_section_nr] = sectp;

  8710.   /* Look for specific sections that we need.  */
  8711.   if (section_is_p (sectp->name, &names->str_dwo))
  8712.     {
  8713.       dwp_file->sections.str.s.asection = sectp;
  8714.       dwp_file->sections.str.size = bfd_get_section_size (sectp);
  8715.     }
  8716.   else if (section_is_p (sectp->name, &names->cu_index))
  8717.     {
  8718.       dwp_file->sections.cu_index.s.asection = sectp;
  8719.       dwp_file->sections.cu_index.size = bfd_get_section_size (sectp);
  8720.     }
  8721.   else if (section_is_p (sectp->name, &names->tu_index))
  8722.     {
  8723.       dwp_file->sections.tu_index.s.asection = sectp;
  8724.       dwp_file->sections.tu_index.size = bfd_get_section_size (sectp);
  8725.     }
  8726. }

  8727. /* This function is mapped across the sections and remembers the offset and
  8728.    size of each of the DWP version 2 debugging sections that we are interested
  8729.    in.  This is split into a separate function because we don't know if we
  8730.    have version 1 or 2 until we parse the cu_index/tu_index sections.  */

  8731. static void
  8732. dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
  8733. {
  8734.   struct dwp_file *dwp_file = dwp_file_ptr;
  8735.   const struct dwop_section_names *names = &dwop_section_names;
  8736.   unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;

  8737.   /* Record the ELF section number for later lookup: this is what the
  8738.      .debug_cu_index,.debug_tu_index tables use in DWP V1.  */
  8739.   gdb_assert (elf_section_nr < dwp_file->num_sections);
  8740.   dwp_file->elf_sections[elf_section_nr] = sectp;

  8741.   /* Look for specific sections that we need.  */
  8742.   if (section_is_p (sectp->name, &names->abbrev_dwo))
  8743.     {
  8744.       dwp_file->sections.abbrev.s.asection = sectp;
  8745.       dwp_file->sections.abbrev.size = bfd_get_section_size (sectp);
  8746.     }
  8747.   else if (section_is_p (sectp->name, &names->info_dwo))
  8748.     {
  8749.       dwp_file->sections.info.s.asection = sectp;
  8750.       dwp_file->sections.info.size = bfd_get_section_size (sectp);
  8751.     }
  8752.   else if (section_is_p (sectp->name, &names->line_dwo))
  8753.     {
  8754.       dwp_file->sections.line.s.asection = sectp;
  8755.       dwp_file->sections.line.size = bfd_get_section_size (sectp);
  8756.     }
  8757.   else if (section_is_p (sectp->name, &names->loc_dwo))
  8758.     {
  8759.       dwp_file->sections.loc.s.asection = sectp;
  8760.       dwp_file->sections.loc.size = bfd_get_section_size (sectp);
  8761.     }
  8762.   else if (section_is_p (sectp->name, &names->macinfo_dwo))
  8763.     {
  8764.       dwp_file->sections.macinfo.s.asection = sectp;
  8765.       dwp_file->sections.macinfo.size = bfd_get_section_size (sectp);
  8766.     }
  8767.   else if (section_is_p (sectp->name, &names->macro_dwo))
  8768.     {
  8769.       dwp_file->sections.macro.s.asection = sectp;
  8770.       dwp_file->sections.macro.size = bfd_get_section_size (sectp);
  8771.     }
  8772.   else if (section_is_p (sectp->name, &names->str_offsets_dwo))
  8773.     {
  8774.       dwp_file->sections.str_offsets.s.asection = sectp;
  8775.       dwp_file->sections.str_offsets.size = bfd_get_section_size (sectp);
  8776.     }
  8777.   else if (section_is_p (sectp->name, &names->types_dwo))
  8778.     {
  8779.       dwp_file->sections.types.s.asection = sectp;
  8780.       dwp_file->sections.types.size = bfd_get_section_size (sectp);
  8781.     }
  8782. }

  8783. /* Hash function for dwp_file loaded CUs/TUs.  */

  8784. static hashval_t
  8785. hash_dwp_loaded_cutus (const void *item)
  8786. {
  8787.   const struct dwo_unit *dwo_unit = item;

  8788.   /* This drops the top 32 bits of the signature, but is ok for a hash.  */
  8789.   return dwo_unit->signature;
  8790. }

  8791. /* Equality function for dwp_file loaded CUs/TUs.  */

  8792. static int
  8793. eq_dwp_loaded_cutus (const void *a, const void *b)
  8794. {
  8795.   const struct dwo_unit *dua = a;
  8796.   const struct dwo_unit *dub = b;

  8797.   return dua->signature == dub->signature;
  8798. }

  8799. /* Allocate a hash table for dwp_file loaded CUs/TUs.  */

  8800. static htab_t
  8801. allocate_dwp_loaded_cutus_table (struct objfile *objfile)
  8802. {
  8803.   return htab_create_alloc_ex (3,
  8804.                                hash_dwp_loaded_cutus,
  8805.                                eq_dwp_loaded_cutus,
  8806.                                NULL,
  8807.                                &objfile->objfile_obstack,
  8808.                                hashtab_obstack_allocate,
  8809.                                dummy_obstack_deallocate);
  8810. }

  8811. /* Try to open DWP file FILE_NAME.
  8812.    The result is the bfd handle of the file.
  8813.    If there is a problem finding or opening the file, return NULL.
  8814.    Upon success, the canonicalized path of the file is stored in the bfd,
  8815.    same as symfile_bfd_open.  */

  8816. static bfd *
  8817. open_dwp_file (const char *file_name)
  8818. {
  8819.   bfd *abfd;

  8820.   abfd = try_open_dwop_file (file_name, 1 /*is_dwp*/, 1 /*search_cwd*/);
  8821.   if (abfd != NULL)
  8822.     return abfd;

  8823.   /* Work around upstream bug 15652.
  8824.      http://sourceware.org/bugzilla/show_bug.cgi?id=15652
  8825.      [Whether that's a "bug" is debatable, but it is getting in our way.]
  8826.      We have no real idea where the dwp file is, because gdb's realpath-ing
  8827.      of the executable's path may have discarded the needed info.
  8828.      [IWBN if the dwp file name was recorded in the executable, akin to
  8829.      .gnu_debuglink, but that doesn't exist yet.]
  8830.      Strip the directory from FILE_NAME and search again.  */
  8831.   if (*debug_file_directory != '\0')
  8832.     {
  8833.       /* Don't implicitly search the current directory here.
  8834.          If the user wants to search "." to handle this case,
  8835.          it must be added to debug-file-directory.  */
  8836.       return try_open_dwop_file (lbasename (file_name), 1 /*is_dwp*/,
  8837.                                  0 /*search_cwd*/);
  8838.     }

  8839.   return NULL;
  8840. }

  8841. /* Initialize the use of the DWP file for the current objfile.
  8842.    By convention the name of the DWP file is ${objfile}.dwp.
  8843.    The result is NULL if it can't be found.  */

  8844. static struct dwp_file *
  8845. open_and_init_dwp_file (void)
  8846. {
  8847.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  8848.   struct dwp_file *dwp_file;
  8849.   char *dwp_name;
  8850.   bfd *dbfd;
  8851.   struct cleanup *cleanups;

  8852.   /* Try to find first .dwp for the binary file before any symbolic links
  8853.      resolving.  */
  8854.   dwp_name = xstrprintf ("%s.dwp", objfile->original_name);
  8855.   cleanups = make_cleanup (xfree, dwp_name);

  8856.   dbfd = open_dwp_file (dwp_name);
  8857.   if (dbfd == NULL
  8858.       && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
  8859.     {
  8860.       /* Try to find .dwp for the binary file after gdb_realpath resolving.  */
  8861.       dwp_name = xstrprintf ("%s.dwp", objfile_name (objfile));
  8862.       make_cleanup (xfree, dwp_name);
  8863.       dbfd = open_dwp_file (dwp_name);
  8864.     }

  8865.   if (dbfd == NULL)
  8866.     {
  8867.       if (dwarf2_read_debug)
  8868.         fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name);
  8869.       do_cleanups (cleanups);
  8870.       return NULL;
  8871.     }
  8872.   dwp_file = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_file);
  8873.   dwp_file->name = bfd_get_filename (dbfd);
  8874.   dwp_file->dbfd = dbfd;
  8875.   do_cleanups (cleanups);

  8876.   /* +1: section 0 is unused */
  8877.   dwp_file->num_sections = bfd_count_sections (dbfd) + 1;
  8878.   dwp_file->elf_sections =
  8879.     OBSTACK_CALLOC (&objfile->objfile_obstack,
  8880.                     dwp_file->num_sections, asection *);

  8881.   bfd_map_over_sections (dbfd, dwarf2_locate_common_dwp_sections, dwp_file);

  8882.   dwp_file->cus = create_dwp_hash_table (dwp_file, 0);

  8883.   dwp_file->tus = create_dwp_hash_table (dwp_file, 1);

  8884.   /* The DWP file version is stored in the hash table.  Oh well.  */
  8885.   if (dwp_file->cus->version != dwp_file->tus->version)
  8886.     {
  8887.       /* Technically speaking, we should try to limp along, but this is
  8888.          pretty bizarre.  We use pulongest here because that's the established
  8889.          portability solution (e.g, we cannot use %u for uint32_t).  */
  8890.       error (_("Dwarf Error: DWP file CU version %s doesn't match"
  8891.                " TU version %s [in DWP file %s]"),
  8892.              pulongest (dwp_file->cus->version),
  8893.              pulongest (dwp_file->tus->version), dwp_name);
  8894.     }
  8895.   dwp_file->version = dwp_file->cus->version;

  8896.   if (dwp_file->version == 2)
  8897.     bfd_map_over_sections (dbfd, dwarf2_locate_v2_dwp_sections, dwp_file);

  8898.   dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table (objfile);
  8899.   dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table (objfile);

  8900.   if (dwarf2_read_debug)
  8901.     {
  8902.       fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
  8903.       fprintf_unfiltered (gdb_stdlog,
  8904.                           "    %s CUs, %s TUs\n",
  8905.                           pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
  8906.                           pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
  8907.     }

  8908.   return dwp_file;
  8909. }

  8910. /* Wrapper around open_and_init_dwp_file, only open it once.  */

  8911. static struct dwp_file *
  8912. get_dwp_file (void)
  8913. {
  8914.   if (! dwarf2_per_objfile->dwp_checked)
  8915.     {
  8916.       dwarf2_per_objfile->dwp_file = open_and_init_dwp_file ();
  8917.       dwarf2_per_objfile->dwp_checked = 1;
  8918.     }
  8919.   return dwarf2_per_objfile->dwp_file;
  8920. }

  8921. /* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
  8922.    Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
  8923.    or in the DWP file for the objfile, referenced by THIS_UNIT.
  8924.    If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
  8925.    IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.

  8926.    This is called, for example, when wanting to read a variable with a
  8927.    complex location.  Therefore we don't want to do file i/o for every call.
  8928.    Therefore we don't want to look for a DWO file on every call.
  8929.    Therefore we first see if we've already seen SIGNATURE in a DWP file,
  8930.    then we check if we've already seen DWO_NAME, and only THEN do we check
  8931.    for a DWO file.

  8932.    The result is a pointer to the dwo_unit object or NULL if we didn't find it
  8933.    (dwo_id mismatch or couldn't find the DWO/DWP file).  */

  8934. static struct dwo_unit *
  8935. lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
  8936.                  const char *dwo_name, const char *comp_dir,
  8937.                  ULONGEST signature, int is_debug_types)
  8938. {
  8939.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  8940.   const char *kind = is_debug_types ? "TU" : "CU";
  8941.   void **dwo_file_slot;
  8942.   struct dwo_file *dwo_file;
  8943.   struct dwp_file *dwp_file;

  8944.   /* First see if there's a DWP file.
  8945.      If we have a DWP file but didn't find the DWO inside it, don't
  8946.      look for the original DWO file.  It makes gdb behave differently
  8947.      depending on whether one is debugging in the build tree.  */

  8948.   dwp_file = get_dwp_file ();
  8949.   if (dwp_file != NULL)
  8950.     {
  8951.       const struct dwp_hash_table *dwp_htab =
  8952.         is_debug_types ? dwp_file->tus : dwp_file->cus;

  8953.       if (dwp_htab != NULL)
  8954.         {
  8955.           struct dwo_unit *dwo_cutu =
  8956.             lookup_dwo_unit_in_dwp (dwp_file, comp_dir,
  8957.                                     signature, is_debug_types);

  8958.           if (dwo_cutu != NULL)
  8959.             {
  8960.               if (dwarf2_read_debug)
  8961.                 {
  8962.                   fprintf_unfiltered (gdb_stdlog,
  8963.                                       "Virtual DWO %s %s found: @%s\n",
  8964.                                       kind, hex_string (signature),
  8965.                                       host_address_to_string (dwo_cutu));
  8966.                 }
  8967.               return dwo_cutu;
  8968.             }
  8969.         }
  8970.     }
  8971.   else
  8972.     {
  8973.       /* No DWP file, look for the DWO file.  */

  8974.       dwo_file_slot = lookup_dwo_file_slot (dwo_name, comp_dir);
  8975.       if (*dwo_file_slot == NULL)
  8976.         {
  8977.           /* Read in the file and build a table of the CUs/TUs it contains.  */
  8978.           *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
  8979.         }
  8980.       /* NOTE: This will be NULL if unable to open the file.  */
  8981.       dwo_file = *dwo_file_slot;

  8982.       if (dwo_file != NULL)
  8983.         {
  8984.           struct dwo_unit *dwo_cutu = NULL;

  8985.           if (is_debug_types && dwo_file->tus)
  8986.             {
  8987.               struct dwo_unit find_dwo_cutu;

  8988.               memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
  8989.               find_dwo_cutu.signature = signature;
  8990.               dwo_cutu = htab_find (dwo_file->tus, &find_dwo_cutu);
  8991.             }
  8992.           else if (!is_debug_types && dwo_file->cu)
  8993.             {
  8994.               if (signature == dwo_file->cu->signature)
  8995.                 dwo_cutu = dwo_file->cu;
  8996.             }

  8997.           if (dwo_cutu != NULL)
  8998.             {
  8999.               if (dwarf2_read_debug)
  9000.                 {
  9001.                   fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
  9002.                                       kind, dwo_name, hex_string (signature),
  9003.                                       host_address_to_string (dwo_cutu));
  9004.                 }
  9005.               return dwo_cutu;
  9006.             }
  9007.         }
  9008.     }

  9009.   /* We didn't find it.  This could mean a dwo_id mismatch, or
  9010.      someone deleted the DWO/DWP file, or the search path isn't set up
  9011.      correctly to find the file.  */

  9012.   if (dwarf2_read_debug)
  9013.     {
  9014.       fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
  9015.                           kind, dwo_name, hex_string (signature));
  9016.     }

  9017.   /* This is a warning and not a complaint because it can be caused by
  9018.      pilot error (e.g., user accidentally deleting the DWO).  */
  9019.   {
  9020.     /* Print the name of the DWP file if we looked there, helps the user
  9021.        better diagnose the problem.  */
  9022.     char *dwp_text = NULL;
  9023.     struct cleanup *cleanups;

  9024.     if (dwp_file != NULL)
  9025.       dwp_text = xstrprintf (" [in DWP file %s]", lbasename (dwp_file->name));
  9026.     cleanups = make_cleanup (xfree, dwp_text);

  9027.     warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset 0x%x"
  9028.                " [in module %s]"),
  9029.              kind, dwo_name, hex_string (signature),
  9030.              dwp_text != NULL ? dwp_text : "",
  9031.              this_unit->is_debug_types ? "TU" : "CU",
  9032.              this_unit->offset.sect_off, objfile_name (objfile));

  9033.     do_cleanups (cleanups);
  9034.   }
  9035.   return NULL;
  9036. }

  9037. /* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
  9038.    See lookup_dwo_cutu_unit for details.  */

  9039. static struct dwo_unit *
  9040. lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
  9041.                       const char *dwo_name, const char *comp_dir,
  9042.                       ULONGEST signature)
  9043. {
  9044.   return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
  9045. }

  9046. /* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
  9047.    See lookup_dwo_cutu_unit for details.  */

  9048. static struct dwo_unit *
  9049. lookup_dwo_type_unit (struct signatured_type *this_tu,
  9050.                       const char *dwo_name, const char *comp_dir)
  9051. {
  9052.   return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
  9053. }

  9054. /* Traversal function for queue_and_load_all_dwo_tus.  */

  9055. static int
  9056. queue_and_load_dwo_tu (void **slot, void *info)
  9057. {
  9058.   struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
  9059.   struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
  9060.   ULONGEST signature = dwo_unit->signature;
  9061.   struct signatured_type *sig_type =
  9062.     lookup_dwo_signatured_type (per_cu->cu, signature);

  9063.   if (sig_type != NULL)
  9064.     {
  9065.       struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;

  9066.       /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
  9067.          a real dependency of PER_CU on SIG_TYPE.  That is detected later
  9068.          while processing PER_CU.  */
  9069.       if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
  9070.         load_full_type_unit (sig_cu);
  9071.       VEC_safe_push (dwarf2_per_cu_ptr, per_cu->imported_symtabs, sig_cu);
  9072.     }

  9073.   return 1;
  9074. }

  9075. /* Queue all TUs contained in the DWO of PER_CU to be read in.
  9076.    The DWO may have the only definition of the type, though it may not be
  9077.    referenced anywhere in PER_CU.  Thus we have to load *all* its TUs.
  9078.    http://sourceware.org/bugzilla/show_bug.cgi?id=15021  */

  9079. static void
  9080. queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
  9081. {
  9082.   struct dwo_unit *dwo_unit;
  9083.   struct dwo_file *dwo_file;

  9084.   gdb_assert (!per_cu->is_debug_types);
  9085.   gdb_assert (get_dwp_file () == NULL);
  9086.   gdb_assert (per_cu->cu != NULL);

  9087.   dwo_unit = per_cu->cu->dwo_unit;
  9088.   gdb_assert (dwo_unit != NULL);

  9089.   dwo_file = dwo_unit->dwo_file;
  9090.   if (dwo_file->tus != NULL)
  9091.     htab_traverse_noresize (dwo_file->tus, queue_and_load_dwo_tu, per_cu);
  9092. }

  9093. /* Free all resources associated with DWO_FILE.
  9094.    Close the DWO file and munmap the sections.
  9095.    All memory should be on the objfile obstack.  */

  9096. static void
  9097. free_dwo_file (struct dwo_file *dwo_file, struct objfile *objfile)
  9098. {
  9099.   int ix;
  9100.   struct dwarf2_section_info *section;

  9101.   /* Note: dbfd is NULL for virtual DWO files.  */
  9102.   gdb_bfd_unref (dwo_file->dbfd);

  9103.   VEC_free (dwarf2_section_info_def, dwo_file->sections.types);
  9104. }

  9105. /* Wrapper for free_dwo_file for use in cleanups.  */

  9106. static void
  9107. free_dwo_file_cleanup (void *arg)
  9108. {
  9109.   struct dwo_file *dwo_file = (struct dwo_file *) arg;
  9110.   struct objfile *objfile = dwarf2_per_objfile->objfile;

  9111.   free_dwo_file (dwo_file, objfile);
  9112. }

  9113. /* Traversal function for free_dwo_files.  */

  9114. static int
  9115. free_dwo_file_from_slot (void **slot, void *info)
  9116. {
  9117.   struct dwo_file *dwo_file = (struct dwo_file *) *slot;
  9118.   struct objfile *objfile = (struct objfile *) info;

  9119.   free_dwo_file (dwo_file, objfile);

  9120.   return 1;
  9121. }

  9122. /* Free all resources associated with DWO_FILES.  */

  9123. static void
  9124. free_dwo_files (htab_t dwo_files, struct objfile *objfile)
  9125. {
  9126.   htab_traverse_noresize (dwo_files, free_dwo_file_from_slot, objfile);
  9127. }

  9128. /* Read in various DIEs.  */

  9129. /* qsort helper for inherit_abstract_dies.  */

  9130. static int
  9131. unsigned_int_compar (const void *ap, const void *bp)
  9132. {
  9133.   unsigned int a = *(unsigned int *) ap;
  9134.   unsigned int b = *(unsigned int *) bp;

  9135.   return (a > b) - (b > a);
  9136. }

  9137. /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
  9138.    Inherit only the children of the DW_AT_abstract_origin DIE not being
  9139.    already referenced by DW_AT_abstract_origin from the children of the
  9140.    current DIE.  */

  9141. static void
  9142. inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
  9143. {
  9144.   struct die_info *child_die;
  9145.   unsigned die_children_count;
  9146.   /* CU offsets which were referenced by children of the current DIE.  */
  9147.   sect_offset *offsets;
  9148.   sect_offset *offsets_end, *offsetp;
  9149.   /* Parent of DIE - referenced by DW_AT_abstract_origin.  */
  9150.   struct die_info *origin_die;
  9151.   /* Iterator of the ORIGIN_DIE children.  */
  9152.   struct die_info *origin_child_die;
  9153.   struct cleanup *cleanups;
  9154.   struct attribute *attr;
  9155.   struct dwarf2_cu *origin_cu;
  9156.   struct pending **origin_previous_list_in_scope;

  9157.   attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
  9158.   if (!attr)
  9159.     return;

  9160.   /* Note that following die references may follow to a die in a
  9161.      different cu.  */

  9162.   origin_cu = cu;
  9163.   origin_die = follow_die_ref (die, attr, &origin_cu);

  9164.   /* We're inheriting ORIGIN's children into the scope we'd put DIE's
  9165.      symbols in.  */
  9166.   origin_previous_list_in_scope = origin_cu->list_in_scope;
  9167.   origin_cu->list_in_scope = cu->list_in_scope;

  9168.   if (die->tag != origin_die->tag
  9169.       && !(die->tag == DW_TAG_inlined_subroutine
  9170.            && origin_die->tag == DW_TAG_subprogram))
  9171.     complaint (&symfile_complaints,
  9172.                _("DIE 0x%x and its abstract origin 0x%x have different tags"),
  9173.                die->offset.sect_off, origin_die->offset.sect_off);

  9174.   child_die = die->child;
  9175.   die_children_count = 0;
  9176.   while (child_die && child_die->tag)
  9177.     {
  9178.       child_die = sibling_die (child_die);
  9179.       die_children_count++;
  9180.     }
  9181.   offsets = xmalloc (sizeof (*offsets) * die_children_count);
  9182.   cleanups = make_cleanup (xfree, offsets);

  9183.   offsets_end = offsets;
  9184.   child_die = die->child;
  9185.   while (child_die && child_die->tag)
  9186.     {
  9187.       /* For each CHILD_DIE, find the corresponding child of
  9188.          ORIGIN_DIE.  If there is more than one layer of
  9189.          DW_AT_abstract_origin, follow them all; there shouldn't be,
  9190.          but GCC versions at least through 4.4 generate this (GCC PR
  9191.          40573).  */
  9192.       struct die_info *child_origin_die = child_die;
  9193.       struct dwarf2_cu *child_origin_cu = cu;

  9194.       while (1)
  9195.         {
  9196.           attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
  9197.                               child_origin_cu);
  9198.           if (attr == NULL)
  9199.             break;
  9200.           child_origin_die = follow_die_ref (child_origin_die, attr,
  9201.                                              &child_origin_cu);
  9202.         }

  9203.       /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
  9204.          counterpart may exist.  */
  9205.       if (child_origin_die != child_die)
  9206.         {
  9207.           if (child_die->tag != child_origin_die->tag
  9208.               && !(child_die->tag == DW_TAG_inlined_subroutine
  9209.                    && child_origin_die->tag == DW_TAG_subprogram))
  9210.             complaint (&symfile_complaints,
  9211.                        _("Child DIE 0x%x and its abstract origin 0x%x have "
  9212.                          "different tags"), child_die->offset.sect_off,
  9213.                        child_origin_die->offset.sect_off);
  9214.           if (child_origin_die->parent != origin_die)
  9215.             complaint (&symfile_complaints,
  9216.                        _("Child DIE 0x%x and its abstract origin 0x%x have "
  9217.                          "different parents"), child_die->offset.sect_off,
  9218.                        child_origin_die->offset.sect_off);
  9219.           else
  9220.             *offsets_end++ = child_origin_die->offset;
  9221.         }
  9222.       child_die = sibling_die (child_die);
  9223.     }
  9224.   qsort (offsets, offsets_end - offsets, sizeof (*offsets),
  9225.          unsigned_int_compar);
  9226.   for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
  9227.     if (offsetp[-1].sect_off == offsetp->sect_off)
  9228.       complaint (&symfile_complaints,
  9229.                  _("Multiple children of DIE 0x%x refer "
  9230.                    "to DIE 0x%x as their abstract origin"),
  9231.                  die->offset.sect_off, offsetp->sect_off);

  9232.   offsetp = offsets;
  9233.   origin_child_die = origin_die->child;
  9234.   while (origin_child_die && origin_child_die->tag)
  9235.     {
  9236.       /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children?  */
  9237.       while (offsetp < offsets_end
  9238.              && offsetp->sect_off < origin_child_die->offset.sect_off)
  9239.         offsetp++;
  9240.       if (offsetp >= offsets_end
  9241.           || offsetp->sect_off > origin_child_die->offset.sect_off)
  9242.         {
  9243.           /* Found that ORIGIN_CHILD_DIE is really not referenced.
  9244.              Check whether we're already processing ORIGIN_CHILD_DIE.
  9245.              This can happen with mutually referenced abstract_origins.
  9246.              PR 16581.  */
  9247.           if (!origin_child_die->in_process)
  9248.             process_die (origin_child_die, origin_cu);
  9249.         }
  9250.       origin_child_die = sibling_die (origin_child_die);
  9251.     }
  9252.   origin_cu->list_in_scope = origin_previous_list_in_scope;

  9253.   do_cleanups (cleanups);
  9254. }

  9255. static void
  9256. read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
  9257. {
  9258.   struct objfile *objfile = cu->objfile;
  9259.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  9260.   struct context_stack *new;
  9261.   CORE_ADDR lowpc;
  9262.   CORE_ADDR highpc;
  9263.   struct die_info *child_die;
  9264.   struct attribute *attr, *call_line, *call_file;
  9265.   const char *name;
  9266.   CORE_ADDR baseaddr;
  9267.   struct block *block;
  9268.   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
  9269.   VEC (symbolp) *template_args = NULL;
  9270.   struct template_symbol *templ_func = NULL;

  9271.   if (inlined_func)
  9272.     {
  9273.       /* If we do not have call site information, we can't show the
  9274.          caller of this inlined function.  That's too confusing, so
  9275.          only use the scope for local variables.  */
  9276.       call_line = dwarf2_attr (die, DW_AT_call_line, cu);
  9277.       call_file = dwarf2_attr (die, DW_AT_call_file, cu);
  9278.       if (call_line == NULL || call_file == NULL)
  9279.         {
  9280.           read_lexical_block_scope (die, cu);
  9281.           return;
  9282.         }
  9283.     }

  9284.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  9285.   name = dwarf2_name (die, cu);

  9286.   /* Ignore functions with missing or empty names.  These are actually
  9287.      illegal according to the DWARF standard.  */
  9288.   if (name == NULL)
  9289.     {
  9290.       complaint (&symfile_complaints,
  9291.                  _("missing name for subprogram DIE at %d"),
  9292.                  die->offset.sect_off);
  9293.       return;
  9294.     }

  9295.   /* Ignore functions with missing or invalid low and high pc attributes.  */
  9296.   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
  9297.     {
  9298.       attr = dwarf2_attr (die, DW_AT_external, cu);
  9299.       if (!attr || !DW_UNSND (attr))
  9300.         complaint (&symfile_complaints,
  9301.                    _("cannot get low and high bounds "
  9302.                      "for subprogram DIE at %d"),
  9303.                    die->offset.sect_off);
  9304.       return;
  9305.     }

  9306.   lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
  9307.   highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);

  9308.   /* If we have any template arguments, then we must allocate a
  9309.      different sort of symbol.  */
  9310.   for (child_die = die->child; child_die; child_die = sibling_die (child_die))
  9311.     {
  9312.       if (child_die->tag == DW_TAG_template_type_param
  9313.           || child_die->tag == DW_TAG_template_value_param)
  9314.         {
  9315.           templ_func = allocate_template_symbol (objfile);
  9316.           templ_func->base.is_cplus_template_function = 1;
  9317.           break;
  9318.         }
  9319.     }

  9320.   new = push_context (0, lowpc);
  9321.   new->name = new_symbol_full (die, read_type_die (die, cu), cu,
  9322.                                (struct symbol *) templ_func);

  9323.   /* If there is a location expression for DW_AT_frame_base, record
  9324.      it.  */
  9325.   attr = dwarf2_attr (die, DW_AT_frame_base, cu);
  9326.   if (attr)
  9327.     dwarf2_symbol_mark_computed (attr, new->name, cu, 1);

  9328.   cu->list_in_scope = &local_symbols;

  9329.   if (die->child != NULL)
  9330.     {
  9331.       child_die = die->child;
  9332.       while (child_die && child_die->tag)
  9333.         {
  9334.           if (child_die->tag == DW_TAG_template_type_param
  9335.               || child_die->tag == DW_TAG_template_value_param)
  9336.             {
  9337.               struct symbol *arg = new_symbol (child_die, NULL, cu);

  9338.               if (arg != NULL)
  9339.                 VEC_safe_push (symbolp, template_args, arg);
  9340.             }
  9341.           else
  9342.             process_die (child_die, cu);
  9343.           child_die = sibling_die (child_die);
  9344.         }
  9345.     }

  9346.   inherit_abstract_dies (die, cu);

  9347.   /* If we have a DW_AT_specification, we might need to import using
  9348.      directives from the context of the specification DIE.  See the
  9349.      comment in determine_prefix.  */
  9350.   if (cu->language == language_cplus
  9351.       && dwarf2_attr (die, DW_AT_specification, cu))
  9352.     {
  9353.       struct dwarf2_cu *spec_cu = cu;
  9354.       struct die_info *spec_die = die_specification (die, &spec_cu);

  9355.       while (spec_die)
  9356.         {
  9357.           child_die = spec_die->child;
  9358.           while (child_die && child_die->tag)
  9359.             {
  9360.               if (child_die->tag == DW_TAG_imported_module)
  9361.                 process_die (child_die, spec_cu);
  9362.               child_die = sibling_die (child_die);
  9363.             }

  9364.           /* In some cases, GCC generates specification DIEs that
  9365.              themselves contain DW_AT_specification attributes.  */
  9366.           spec_die = die_specification (spec_die, &spec_cu);
  9367.         }
  9368.     }

  9369.   new = pop_context ();
  9370.   /* Make a block for the local symbols within.  */
  9371.   block = finish_block (new->name, &local_symbols, new->old_blocks,
  9372.                         lowpc, highpc);

  9373.   /* For C++, set the block's scope.  */
  9374.   if ((cu->language == language_cplus || cu->language == language_fortran)
  9375.       && cu->processing_has_namespace_info)
  9376.     block_set_scope (block, determine_prefix (die, cu),
  9377.                      &objfile->objfile_obstack);

  9378.   /* If we have address ranges, record them.  */
  9379.   dwarf2_record_block_ranges (die, block, baseaddr, cu);

  9380.   gdbarch_make_symbol_special (gdbarch, new->name, objfile);

  9381.   /* Attach template arguments to function.  */
  9382.   if (! VEC_empty (symbolp, template_args))
  9383.     {
  9384.       gdb_assert (templ_func != NULL);

  9385.       templ_func->n_template_arguments = VEC_length (symbolp, template_args);
  9386.       templ_func->template_arguments
  9387.         = obstack_alloc (&objfile->objfile_obstack,
  9388.                          (templ_func->n_template_arguments
  9389.                           * sizeof (struct symbol *)));
  9390.       memcpy (templ_func->template_arguments,
  9391.               VEC_address (symbolp, template_args),
  9392.               (templ_func->n_template_arguments * sizeof (struct symbol *)));
  9393.       VEC_free (symbolp, template_args);
  9394.     }

  9395.   /* In C++, we can have functions nested inside functions (e.g., when
  9396.      a function declares a class that has methods).  This means that
  9397.      when we finish processing a function scope, we may need to go
  9398.      back to building a containing block's symbol lists.  */
  9399.   local_symbols = new->locals;
  9400.   using_directives = new->using_directives;

  9401.   /* If we've finished processing a top-level function, subsequent
  9402.      symbols go in the file symbol list.  */
  9403.   if (outermost_context_p ())
  9404.     cu->list_in_scope = &file_symbols;
  9405. }

  9406. /* Process all the DIES contained within a lexical block scope.  Start
  9407.    a new scope, process the dies, and then close the scope.  */

  9408. static void
  9409. read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
  9410. {
  9411.   struct objfile *objfile = cu->objfile;
  9412.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  9413.   struct context_stack *new;
  9414.   CORE_ADDR lowpc, highpc;
  9415.   struct die_info *child_die;
  9416.   CORE_ADDR baseaddr;

  9417.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  9418.   /* Ignore blocks with missing or invalid low and high pc attributes.  */
  9419.   /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
  9420.      as multiple lexical blocks?  Handling children in a sane way would
  9421.      be nasty.  Might be easier to properly extend generic blocks to
  9422.      describe ranges.  */
  9423.   if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
  9424.     return;
  9425.   lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
  9426.   highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);

  9427.   push_context (0, lowpc);
  9428.   if (die->child != NULL)
  9429.     {
  9430.       child_die = die->child;
  9431.       while (child_die && child_die->tag)
  9432.         {
  9433.           process_die (child_die, cu);
  9434.           child_die = sibling_die (child_die);
  9435.         }
  9436.     }
  9437.   new = pop_context ();

  9438.   if (local_symbols != NULL || using_directives != NULL)
  9439.     {
  9440.       struct block *block
  9441.         = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
  9442.                         highpc);

  9443.       /* Note that recording ranges after traversing children, as we
  9444.          do here, means that recording a parent's ranges entails
  9445.          walking across all its children's ranges as they appear in
  9446.          the address map, which is quadratic behavior.

  9447.          It would be nicer to record the parent's ranges before
  9448.          traversing its children, simply overriding whatever you find
  9449.          there.  But since we don't even decide whether to create a
  9450.          block until after we've traversed its children, that's hard
  9451.          to do.  */
  9452.       dwarf2_record_block_ranges (die, block, baseaddr, cu);
  9453.     }
  9454.   local_symbols = new->locals;
  9455.   using_directives = new->using_directives;
  9456. }

  9457. /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab.  */

  9458. static void
  9459. read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
  9460. {
  9461.   struct objfile *objfile = cu->objfile;
  9462.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  9463.   CORE_ADDR pc, baseaddr;
  9464.   struct attribute *attr;
  9465.   struct call_site *call_site, call_site_local;
  9466.   void **slot;
  9467.   int nparams;
  9468.   struct die_info *child_die;

  9469.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  9470.   attr = dwarf2_attr (die, DW_AT_low_pc, cu);
  9471.   if (!attr)
  9472.     {
  9473.       complaint (&symfile_complaints,
  9474.                  _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
  9475.                    "DIE 0x%x [in module %s]"),
  9476.                  die->offset.sect_off, objfile_name (objfile));
  9477.       return;
  9478.     }
  9479.   pc = attr_value_as_address (attr) + baseaddr;
  9480.   pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);

  9481.   if (cu->call_site_htab == NULL)
  9482.     cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
  9483.                                                NULL, &objfile->objfile_obstack,
  9484.                                                hashtab_obstack_allocate, NULL);
  9485.   call_site_local.pc = pc;
  9486.   slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
  9487.   if (*slot != NULL)
  9488.     {
  9489.       complaint (&symfile_complaints,
  9490.                  _("Duplicate PC %s for DW_TAG_GNU_call_site "
  9491.                    "DIE 0x%x [in module %s]"),
  9492.                  paddress (gdbarch, pc), die->offset.sect_off,
  9493.                  objfile_name (objfile));
  9494.       return;
  9495.     }

  9496.   /* Count parameters at the caller.  */

  9497.   nparams = 0;
  9498.   for (child_die = die->child; child_die && child_die->tag;
  9499.        child_die = sibling_die (child_die))
  9500.     {
  9501.       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
  9502.         {
  9503.           complaint (&symfile_complaints,
  9504.                      _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
  9505.                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
  9506.                      child_die->tag, child_die->offset.sect_off,
  9507.                      objfile_name (objfile));
  9508.           continue;
  9509.         }

  9510.       nparams++;
  9511.     }

  9512.   call_site = obstack_alloc (&objfile->objfile_obstack,
  9513.                              (sizeof (*call_site)
  9514.                               + (sizeof (*call_site->parameter)
  9515.                                  * (nparams - 1))));
  9516.   *slot = call_site;
  9517.   memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
  9518.   call_site->pc = pc;

  9519.   if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
  9520.     {
  9521.       struct die_info *func_die;

  9522.       /* Skip also over DW_TAG_inlined_subroutine.  */
  9523.       for (func_die = die->parent;
  9524.            func_die && func_die->tag != DW_TAG_subprogram
  9525.            && func_die->tag != DW_TAG_subroutine_type;
  9526.            func_die = func_die->parent);

  9527.       /* DW_AT_GNU_all_call_sites is a superset
  9528.          of DW_AT_GNU_all_tail_call_sites.  */
  9529.       if (func_die
  9530.           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
  9531.           && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
  9532.         {
  9533.           /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
  9534.              not complete.  But keep CALL_SITE for look ups via call_site_htab,
  9535.              both the initial caller containing the real return address PC and
  9536.              the final callee containing the current PC of a chain of tail
  9537.              calls do not need to have the tail call list complete.  But any
  9538.              function candidate for a virtual tail call frame searched via
  9539.              TYPE_TAIL_CALL_LIST must have the tail call list complete to be
  9540.              determined unambiguously.  */
  9541.         }
  9542.       else
  9543.         {
  9544.           struct type *func_type = NULL;

  9545.           if (func_die)
  9546.             func_type = get_die_type (func_die, cu);
  9547.           if (func_type != NULL)
  9548.             {
  9549.               gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);

  9550.               /* Enlist this call site to the function.  */
  9551.               call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
  9552.               TYPE_TAIL_CALL_LIST (func_type) = call_site;
  9553.             }
  9554.           else
  9555.             complaint (&symfile_complaints,
  9556.                        _("Cannot find function owning DW_TAG_GNU_call_site "
  9557.                          "DIE 0x%x [in module %s]"),
  9558.                        die->offset.sect_off, objfile_name (objfile));
  9559.         }
  9560.     }

  9561.   attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
  9562.   if (attr == NULL)
  9563.     attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
  9564.   SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
  9565.   if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
  9566.     /* Keep NULL DWARF_BLOCK.  */;
  9567.   else if (attr_form_is_block (attr))
  9568.     {
  9569.       struct dwarf2_locexpr_baton *dlbaton;

  9570.       dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
  9571.       dlbaton->data = DW_BLOCK (attr)->data;
  9572.       dlbaton->size = DW_BLOCK (attr)->size;
  9573.       dlbaton->per_cu = cu->per_cu;

  9574.       SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
  9575.     }
  9576.   else if (attr_form_is_ref (attr))
  9577.     {
  9578.       struct dwarf2_cu *target_cu = cu;
  9579.       struct die_info *target_die;

  9580.       target_die = follow_die_ref (die, attr, &target_cu);
  9581.       gdb_assert (target_cu->objfile == objfile);
  9582.       if (die_is_declaration (target_die, target_cu))
  9583.         {
  9584.           const char *target_physname = NULL;
  9585.           struct attribute *target_attr;

  9586.           /* Prefer the mangled name; otherwise compute the demangled one.  */
  9587.           target_attr = dwarf2_attr (target_die, DW_AT_linkage_name, target_cu);
  9588.           if (target_attr == NULL)
  9589.             target_attr = dwarf2_attr (target_die, DW_AT_MIPS_linkage_name,
  9590.                                        target_cu);
  9591.           if (target_attr != NULL && DW_STRING (target_attr) != NULL)
  9592.             target_physname = DW_STRING (target_attr);
  9593.           else
  9594.             target_physname = dwarf2_physname (NULL, target_die, target_cu);
  9595.           if (target_physname == NULL)
  9596.             complaint (&symfile_complaints,
  9597.                        _("DW_AT_GNU_call_site_target target DIE has invalid "
  9598.                          "physname, for referencing DIE 0x%x [in module %s]"),
  9599.                        die->offset.sect_off, objfile_name (objfile));
  9600.           else
  9601.             SET_FIELD_PHYSNAME (call_site->target, target_physname);
  9602.         }
  9603.       else
  9604.         {
  9605.           CORE_ADDR lowpc;

  9606.           /* DW_AT_entry_pc should be preferred.  */
  9607.           if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
  9608.             complaint (&symfile_complaints,
  9609.                        _("DW_AT_GNU_call_site_target target DIE has invalid "
  9610.                          "low pc, for referencing DIE 0x%x [in module %s]"),
  9611.                        die->offset.sect_off, objfile_name (objfile));
  9612.           else
  9613.             {
  9614.               lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
  9615.               SET_FIELD_PHYSADDR (call_site->target, lowpc);
  9616.             }
  9617.         }
  9618.     }
  9619.   else
  9620.     complaint (&symfile_complaints,
  9621.                _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
  9622.                  "block nor reference, for DIE 0x%x [in module %s]"),
  9623.                die->offset.sect_off, objfile_name (objfile));

  9624.   call_site->per_cu = cu->per_cu;

  9625.   for (child_die = die->child;
  9626.        child_die && child_die->tag;
  9627.        child_die = sibling_die (child_die))
  9628.     {
  9629.       struct call_site_parameter *parameter;
  9630.       struct attribute *loc, *origin;

  9631.       if (child_die->tag != DW_TAG_GNU_call_site_parameter)
  9632.         {
  9633.           /* Already printed the complaint above.  */
  9634.           continue;
  9635.         }

  9636.       gdb_assert (call_site->parameter_count < nparams);
  9637.       parameter = &call_site->parameter[call_site->parameter_count];

  9638.       /* DW_AT_location specifies the register number or DW_AT_abstract_origin
  9639.          specifies DW_TAG_formal_parameter.  Value of the data assumed for the
  9640.          register is contained in DW_AT_GNU_call_site_value.  */

  9641.       loc = dwarf2_attr (child_die, DW_AT_location, cu);
  9642.       origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
  9643.       if (loc == NULL && origin != NULL && attr_form_is_ref (origin))
  9644.         {
  9645.           sect_offset offset;

  9646.           parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
  9647.           offset = dwarf2_get_ref_die_offset (origin);
  9648.           if (!offset_in_cu_p (&cu->header, offset))
  9649.             {
  9650.               /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
  9651.                  binding can be done only inside one CU.  Such referenced DIE
  9652.                  therefore cannot be even moved to DW_TAG_partial_unit.  */
  9653.               complaint (&symfile_complaints,
  9654.                          _("DW_AT_abstract_origin offset is not in CU for "
  9655.                            "DW_TAG_GNU_call_site child DIE 0x%x "
  9656.                            "[in module %s]"),
  9657.                          child_die->offset.sect_off, objfile_name (objfile));
  9658.               continue;
  9659.             }
  9660.           parameter->u.param_offset.cu_off = (offset.sect_off
  9661.                                               - cu->header.offset.sect_off);
  9662.         }
  9663.       else if (loc == NULL || origin != NULL || !attr_form_is_block (loc))
  9664.         {
  9665.           complaint (&symfile_complaints,
  9666.                      _("No DW_FORM_block* DW_AT_location for "
  9667.                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
  9668.                      child_die->offset.sect_off, objfile_name (objfile));
  9669.           continue;
  9670.         }
  9671.       else
  9672.         {
  9673.           parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
  9674.             (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
  9675.           if (parameter->u.dwarf_reg != -1)
  9676.             parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
  9677.           else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
  9678.                                     &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
  9679.                                              &parameter->u.fb_offset))
  9680.             parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
  9681.           else
  9682.             {
  9683.               complaint (&symfile_complaints,
  9684.                          _("Only single DW_OP_reg or DW_OP_fbreg is supported "
  9685.                            "for DW_FORM_block* DW_AT_location is supported for "
  9686.                            "DW_TAG_GNU_call_site child DIE 0x%x "
  9687.                            "[in module %s]"),
  9688.                          child_die->offset.sect_off, objfile_name (objfile));
  9689.               continue;
  9690.             }
  9691.         }

  9692.       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
  9693.       if (!attr_form_is_block (attr))
  9694.         {
  9695.           complaint (&symfile_complaints,
  9696.                      _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
  9697.                        "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
  9698.                      child_die->offset.sect_off, objfile_name (objfile));
  9699.           continue;
  9700.         }
  9701.       parameter->value = DW_BLOCK (attr)->data;
  9702.       parameter->value_size = DW_BLOCK (attr)->size;

  9703.       /* Parameters are not pre-cleared by memset above.  */
  9704.       parameter->data_value = NULL;
  9705.       parameter->data_value_size = 0;
  9706.       call_site->parameter_count++;

  9707.       attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
  9708.       if (attr)
  9709.         {
  9710.           if (!attr_form_is_block (attr))
  9711.             complaint (&symfile_complaints,
  9712.                        _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
  9713.                          "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
  9714.                        child_die->offset.sect_off, objfile_name (objfile));
  9715.           else
  9716.             {
  9717.               parameter->data_value = DW_BLOCK (attr)->data;
  9718.               parameter->data_value_size = DW_BLOCK (attr)->size;
  9719.             }
  9720.         }
  9721.     }
  9722. }

  9723. /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
  9724.    Return 1 if the attributes are present and valid, otherwise, return 0.
  9725.    If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'.  */

  9726. static int
  9727. dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
  9728.                     CORE_ADDR *high_return, struct dwarf2_cu *cu,
  9729.                     struct partial_symtab *ranges_pst)
  9730. {
  9731.   struct objfile *objfile = cu->objfile;
  9732.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  9733.   struct comp_unit_head *cu_header = &cu->header;
  9734.   bfd *obfd = objfile->obfd;
  9735.   unsigned int addr_size = cu_header->addr_size;
  9736.   CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
  9737.   /* Base address selection entry.  */
  9738.   CORE_ADDR base;
  9739.   int found_base;
  9740.   unsigned int dummy;
  9741.   const gdb_byte *buffer;
  9742.   CORE_ADDR marker;
  9743.   int low_set;
  9744.   CORE_ADDR low = 0;
  9745.   CORE_ADDR high = 0;
  9746.   CORE_ADDR baseaddr;

  9747.   found_base = cu->base_known;
  9748.   base = cu->base_address;

  9749.   dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
  9750.   if (offset >= dwarf2_per_objfile->ranges.size)
  9751.     {
  9752.       complaint (&symfile_complaints,
  9753.                  _("Offset %d out of bounds for DW_AT_ranges attribute"),
  9754.                  offset);
  9755.       return 0;
  9756.     }
  9757.   buffer = dwarf2_per_objfile->ranges.buffer + offset;

  9758.   /* Read in the largest possible address.  */
  9759.   marker = read_address (obfd, buffer, cu, &dummy);
  9760.   if ((marker & mask) == mask)
  9761.     {
  9762.       /* If we found the largest possible address, then
  9763.          read the base address.  */
  9764.       base = read_address (obfd, buffer + addr_size, cu, &dummy);
  9765.       buffer += 2 * addr_size;
  9766.       offset += 2 * addr_size;
  9767.       found_base = 1;
  9768.     }

  9769.   low_set = 0;

  9770.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  9771.   while (1)
  9772.     {
  9773.       CORE_ADDR range_beginning, range_end;

  9774.       range_beginning = read_address (obfd, buffer, cu, &dummy);
  9775.       buffer += addr_size;
  9776.       range_end = read_address (obfd, buffer, cu, &dummy);
  9777.       buffer += addr_size;
  9778.       offset += 2 * addr_size;

  9779.       /* An end of list marker is a pair of zero addresses.  */
  9780.       if (range_beginning == 0 && range_end == 0)
  9781.         /* Found the end of list entry.  */
  9782.         break;

  9783.       /* Each base address selection entry is a pair of 2 values.
  9784.          The first is the largest possible address, the second is
  9785.          the base address.  Check for a base address here.  */
  9786.       if ((range_beginning & mask) == mask)
  9787.         {
  9788.           /* If we found the largest possible address, then
  9789.              read the base address.  */
  9790.           base = read_address (obfd, buffer + addr_size, cu, &dummy);
  9791.           found_base = 1;
  9792.           continue;
  9793.         }

  9794.       if (!found_base)
  9795.         {
  9796.           /* We have no valid base address for the ranges
  9797.              data.  */
  9798.           complaint (&symfile_complaints,
  9799.                      _("Invalid .debug_ranges data (no base address)"));
  9800.           return 0;
  9801.         }

  9802.       if (range_beginning > range_end)
  9803.         {
  9804.           /* Inverted range entries are invalid.  */
  9805.           complaint (&symfile_complaints,
  9806.                      _("Invalid .debug_ranges data (inverted range)"));
  9807.           return 0;
  9808.         }

  9809.       /* Empty range entries have no effect.  */
  9810.       if (range_beginning == range_end)
  9811.         continue;

  9812.       range_beginning += base;
  9813.       range_end += base;

  9814.       /* A not-uncommon case of bad debug info.
  9815.          Don't pollute the addrmap with bad data.  */
  9816.       if (range_beginning + baseaddr == 0
  9817.           && !dwarf2_per_objfile->has_section_at_zero)
  9818.         {
  9819.           complaint (&symfile_complaints,
  9820.                      _(".debug_ranges entry has start address of zero"
  9821.                        " [in module %s]"), objfile_name (objfile));
  9822.           continue;
  9823.         }

  9824.       if (ranges_pst != NULL)
  9825.         {
  9826.           CORE_ADDR lowpc;
  9827.           CORE_ADDR highpc;

  9828.           lowpc = gdbarch_adjust_dwarf2_addr (gdbarch,
  9829.                                               range_beginning + baseaddr);
  9830.           highpc = gdbarch_adjust_dwarf2_addr (gdbarch,
  9831.                                                range_end + baseaddr);
  9832.           addrmap_set_empty (objfile->psymtabs_addrmap, lowpc, highpc - 1,
  9833.                              ranges_pst);
  9834.         }

  9835.       /* FIXME: This is recording everything as a low-high
  9836.          segment of consecutive addresses.  We should have a
  9837.          data structure for discontiguous block ranges
  9838.          instead.  */
  9839.       if (! low_set)
  9840.         {
  9841.           low = range_beginning;
  9842.           high = range_end;
  9843.           low_set = 1;
  9844.         }
  9845.       else
  9846.         {
  9847.           if (range_beginning < low)
  9848.             low = range_beginning;
  9849.           if (range_end > high)
  9850.             high = range_end;
  9851.         }
  9852.     }

  9853.   if (! low_set)
  9854.     /* If the first entry is an end-of-list marker, the range
  9855.        describes an empty scope, i.e. no instructions.  */
  9856.     return 0;

  9857.   if (low_return)
  9858.     *low_return = low;
  9859.   if (high_return)
  9860.     *high_return = high;
  9861.   return 1;
  9862. }

  9863. /* Get low and high pc attributes from a die.  Return 1 if the attributes
  9864.    are present and valid, otherwise, return 0.  Return -1 if the range is
  9865.    discontinuous, i.e. derived from DW_AT_ranges information.  */

  9866. static int
  9867. dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
  9868.                       CORE_ADDR *highpc, struct dwarf2_cu *cu,
  9869.                       struct partial_symtab *pst)
  9870. {
  9871.   struct attribute *attr;
  9872.   struct attribute *attr_high;
  9873.   CORE_ADDR low = 0;
  9874.   CORE_ADDR high = 0;
  9875.   int ret = 0;

  9876.   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
  9877.   if (attr_high)
  9878.     {
  9879.       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
  9880.       if (attr)
  9881.         {
  9882.           low = attr_value_as_address (attr);
  9883.           high = attr_value_as_address (attr_high);
  9884.           if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
  9885.             high += low;
  9886.         }
  9887.       else
  9888.         /* Found high w/o low attribute.  */
  9889.         return 0;

  9890.       /* Found consecutive range of addresses.  */
  9891.       ret = 1;
  9892.     }
  9893.   else
  9894.     {
  9895.       attr = dwarf2_attr (die, DW_AT_ranges, cu);
  9896.       if (attr != NULL)
  9897.         {
  9898.           /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
  9899.              We take advantage of the fact that DW_AT_ranges does not appear
  9900.              in DW_TAG_compile_unit of DWO files.  */
  9901.           int need_ranges_base = die->tag != DW_TAG_compile_unit;
  9902.           unsigned int ranges_offset = (DW_UNSND (attr)
  9903.                                         + (need_ranges_base
  9904.                                            ? cu->ranges_base
  9905.                                            : 0));

  9906.           /* Value of the DW_AT_ranges attribute is the offset in the
  9907.              .debug_ranges section.  */
  9908.           if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
  9909.             return 0;
  9910.           /* Found discontinuous range of addresses.  */
  9911.           ret = -1;
  9912.         }
  9913.     }

  9914.   /* read_partial_die has also the strict LOW < HIGH requirement.  */
  9915.   if (high <= low)
  9916.     return 0;

  9917.   /* When using the GNU linker, .gnu.linkonce. sections are used to
  9918.      eliminate duplicate copies of functions and vtables and such.
  9919.      The linker will arbitrarily choose one and discard the others.
  9920.      The AT_*_pc values for such functions refer to local labels in
  9921.      these sections.  If the section from that file was discarded, the
  9922.      labels are not in the output, so the relocs get a value of 0.
  9923.      If this is a discarded function, mark the pc bounds as invalid,
  9924.      so that GDB will ignore it.  */
  9925.   if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
  9926.     return 0;

  9927.   *lowpc = low;
  9928.   if (highpc)
  9929.     *highpc = high;
  9930.   return ret;
  9931. }

  9932. /* Assuming that DIE represents a subprogram DIE or a lexical block, get
  9933.    its low and high PC addresses.  Do nothing if these addresses could not
  9934.    be determined.  Otherwise, set LOWPC to the low address if it is smaller,
  9935.    and HIGHPC to the high address if greater than HIGHPC.  */

  9936. static void
  9937. dwarf2_get_subprogram_pc_bounds (struct die_info *die,
  9938.                                  CORE_ADDR *lowpc, CORE_ADDR *highpc,
  9939.                                  struct dwarf2_cu *cu)
  9940. {
  9941.   CORE_ADDR low, high;
  9942.   struct die_info *child = die->child;

  9943.   if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
  9944.     {
  9945.       *lowpc = min (*lowpc, low);
  9946.       *highpc = max (*highpc, high);
  9947.     }

  9948.   /* If the language does not allow nested subprograms (either inside
  9949.      subprograms or lexical blocks), we're done.  */
  9950.   if (cu->language != language_ada)
  9951.     return;

  9952.   /* Check all the children of the given DIE.  If it contains nested
  9953.      subprograms, then check their pc bounds.  Likewise, we need to
  9954.      check lexical blocks as well, as they may also contain subprogram
  9955.      definitions.  */
  9956.   while (child && child->tag)
  9957.     {
  9958.       if (child->tag == DW_TAG_subprogram
  9959.           || child->tag == DW_TAG_lexical_block)
  9960.         dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
  9961.       child = sibling_die (child);
  9962.     }
  9963. }

  9964. /* Get the low and high pc's represented by the scope DIE, and store
  9965.    them in *LOWPC and *HIGHPC.  If the correct values can't be
  9966.    determined, set *LOWPC to -1 and *HIGHPC to 0.  */

  9967. static void
  9968. get_scope_pc_bounds (struct die_info *die,
  9969.                      CORE_ADDR *lowpc, CORE_ADDR *highpc,
  9970.                      struct dwarf2_cu *cu)
  9971. {
  9972.   CORE_ADDR best_low = (CORE_ADDR) -1;
  9973.   CORE_ADDR best_high = (CORE_ADDR) 0;
  9974.   CORE_ADDR current_low, current_high;

  9975.   if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
  9976.     {
  9977.       best_low = current_low;
  9978.       best_high = current_high;
  9979.     }
  9980.   else
  9981.     {
  9982.       struct die_info *child = die->child;

  9983.       while (child && child->tag)
  9984.         {
  9985.           switch (child->tag) {
  9986.           case DW_TAG_subprogram:
  9987.             dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
  9988.             break;
  9989.           case DW_TAG_namespace:
  9990.           case DW_TAG_module:
  9991.             /* FIXME: carlton/2004-01-16: Should we do this for
  9992.                DW_TAG_class_type/DW_TAG_structure_type, too?  I think
  9993.                that current GCC's always emit the DIEs corresponding
  9994.                to definitions of methods of classes as children of a
  9995.                DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
  9996.                the DIEs giving the declarations, which could be
  9997.                anywhere).  But I don't see any reason why the
  9998.                standards says that they have to be there.  */
  9999.             get_scope_pc_bounds (child, &current_low, &current_high, cu);

  10000.             if (current_low != ((CORE_ADDR) -1))
  10001.               {
  10002.                 best_low = min (best_low, current_low);
  10003.                 best_high = max (best_high, current_high);
  10004.               }
  10005.             break;
  10006.           default:
  10007.             /* Ignore.  */
  10008.             break;
  10009.           }

  10010.           child = sibling_die (child);
  10011.         }
  10012.     }

  10013.   *lowpc = best_low;
  10014.   *highpc = best_high;
  10015. }

  10016. /* Record the address ranges for BLOCK, offset by BASEADDR, as given
  10017.    in DIE.  */

  10018. static void
  10019. dwarf2_record_block_ranges (struct die_info *die, struct block *block,
  10020.                             CORE_ADDR baseaddr, struct dwarf2_cu *cu)
  10021. {
  10022.   struct objfile *objfile = cu->objfile;
  10023.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  10024.   struct attribute *attr;
  10025.   struct attribute *attr_high;

  10026.   attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
  10027.   if (attr_high)
  10028.     {
  10029.       attr = dwarf2_attr (die, DW_AT_low_pc, cu);
  10030.       if (attr)
  10031.         {
  10032.           CORE_ADDR low = attr_value_as_address (attr);
  10033.           CORE_ADDR high = attr_value_as_address (attr_high);

  10034.           if (cu->header.version >= 4 && attr_form_is_constant (attr_high))
  10035.             high += low;

  10036.           low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
  10037.           high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
  10038.           record_block_range (block, low, high - 1);
  10039.         }
  10040.     }

  10041.   attr = dwarf2_attr (die, DW_AT_ranges, cu);
  10042.   if (attr)
  10043.     {
  10044.       bfd *obfd = objfile->obfd;
  10045.       /* DW_AT_ranges_base does not apply to DIEs from the DWO skeleton.
  10046.          We take advantage of the fact that DW_AT_ranges does not appear
  10047.          in DW_TAG_compile_unit of DWO files.  */
  10048.       int need_ranges_base = die->tag != DW_TAG_compile_unit;

  10049.       /* The value of the DW_AT_ranges attribute is the offset of the
  10050.          address range list in the .debug_ranges section.  */
  10051.       unsigned long offset = (DW_UNSND (attr)
  10052.                               + (need_ranges_base ? cu->ranges_base : 0));
  10053.       const gdb_byte *buffer;

  10054.       /* For some target architectures, but not others, the
  10055.          read_address function sign-extends the addresses it returns.
  10056.          To recognize base address selection entries, we need a
  10057.          mask.  */
  10058.       unsigned int addr_size = cu->header.addr_size;
  10059.       CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));

  10060.       /* The base address, to which the next pair is relative.  Note
  10061.          that this 'base' is a DWARF concept: most entries in a range
  10062.          list are relative, to reduce the number of relocs against the
  10063.          debugging information.  This is separate from this function's
  10064.          'baseaddr' argument, which GDB uses to relocate debugging
  10065.          information from a shared library based on the address at
  10066.          which the library was loaded.  */
  10067.       CORE_ADDR base = cu->base_address;
  10068.       int base_known = cu->base_known;

  10069.       dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
  10070.       if (offset >= dwarf2_per_objfile->ranges.size)
  10071.         {
  10072.           complaint (&symfile_complaints,
  10073.                      _("Offset %lu out of bounds for DW_AT_ranges attribute"),
  10074.                      offset);
  10075.           return;
  10076.         }
  10077.       buffer = dwarf2_per_objfile->ranges.buffer + offset;

  10078.       for (;;)
  10079.         {
  10080.           unsigned int bytes_read;
  10081.           CORE_ADDR start, end;

  10082.           start = read_address (obfd, buffer, cu, &bytes_read);
  10083.           buffer += bytes_read;
  10084.           end = read_address (obfd, buffer, cu, &bytes_read);
  10085.           buffer += bytes_read;

  10086.           /* Did we find the end of the range list?  */
  10087.           if (start == 0 && end == 0)
  10088.             break;

  10089.           /* Did we find a base address selection entry?  */
  10090.           else if ((start & base_select_mask) == base_select_mask)
  10091.             {
  10092.               base = end;
  10093.               base_known = 1;
  10094.             }

  10095.           /* We found an ordinary address range.  */
  10096.           else
  10097.             {
  10098.               if (!base_known)
  10099.                 {
  10100.                   complaint (&symfile_complaints,
  10101.                              _("Invalid .debug_ranges data "
  10102.                                "(no base address)"));
  10103.                   return;
  10104.                 }

  10105.               if (start > end)
  10106.                 {
  10107.                   /* Inverted range entries are invalid.  */
  10108.                   complaint (&symfile_complaints,
  10109.                              _("Invalid .debug_ranges data "
  10110.                                "(inverted range)"));
  10111.                   return;
  10112.                 }

  10113.               /* Empty range entries have no effect.  */
  10114.               if (start == end)
  10115.                 continue;

  10116.               start += base + baseaddr;
  10117.               end += base + baseaddr;

  10118.               /* A not-uncommon case of bad debug info.
  10119.                  Don't pollute the addrmap with bad data.  */
  10120.               if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
  10121.                 {
  10122.                   complaint (&symfile_complaints,
  10123.                              _(".debug_ranges entry has start address of zero"
  10124.                                " [in module %s]"), objfile_name (objfile));
  10125.                   continue;
  10126.                 }

  10127.               start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
  10128.               end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
  10129.               record_block_range (block, start, end - 1);
  10130.             }
  10131.         }
  10132.     }
  10133. }

  10134. /* Check whether the producer field indicates either of GCC < 4.6, or the
  10135.    Intel C/C++ compiler, and cache the result in CU.  */

  10136. static void
  10137. check_producer (struct dwarf2_cu *cu)
  10138. {
  10139.   const char *cs;
  10140.   int major, minor, release;

  10141.   if (cu->producer == NULL)
  10142.     {
  10143.       /* For unknown compilers expect their behavior is DWARF version
  10144.          compliant.

  10145.          GCC started to support .debug_types sections by -gdwarf-4 since
  10146.          gcc-4.5.x.  As the .debug_types sections are missing DW_AT_producer
  10147.          for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
  10148.          combination.  gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
  10149.          interpreted incorrectly by GDB now - GCC PR debug/48229.  */
  10150.     }
  10151.   else if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) == 0)
  10152.     {
  10153.       /* Skip any identifier after "GNU " - such as "C++" or "Java".  */

  10154.       cs = &cu->producer[strlen ("GNU ")];
  10155.       while (*cs && !isdigit (*cs))
  10156.         cs++;
  10157.       if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
  10158.         {
  10159.           /* Not recognized as GCC.  */
  10160.         }
  10161.       else
  10162.         {
  10163.           cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
  10164.           cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
  10165.         }
  10166.     }
  10167.   else if (strncmp (cu->producer, "Intel(R) C", strlen ("Intel(R) C")) == 0)
  10168.     cu->producer_is_icc = 1;
  10169.   else
  10170.     {
  10171.       /* For other non-GCC compilers, expect their behavior is DWARF version
  10172.          compliant.  */
  10173.     }

  10174.   cu->checked_producer = 1;
  10175. }

  10176. /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
  10177.    to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
  10178.    during 4.6.0 experimental.  */

  10179. static int
  10180. producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
  10181. {
  10182.   if (!cu->checked_producer)
  10183.     check_producer (cu);

  10184.   return cu->producer_is_gxx_lt_4_6;
  10185. }

  10186. /* Return the default accessibility type if it is not overriden by
  10187.    DW_AT_accessibility.  */

  10188. static enum dwarf_access_attribute
  10189. dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
  10190. {
  10191.   if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
  10192.     {
  10193.       /* The default DWARF 2 accessibility for members is public, the default
  10194.          accessibility for inheritance is private.  */

  10195.       if (die->tag != DW_TAG_inheritance)
  10196.         return DW_ACCESS_public;
  10197.       else
  10198.         return DW_ACCESS_private;
  10199.     }
  10200.   else
  10201.     {
  10202.       /* DWARF 3+ defines the default accessibility a different way.  The same
  10203.          rules apply now for DW_TAG_inheritance as for the members and it only
  10204.          depends on the container kind.  */

  10205.       if (die->parent->tag == DW_TAG_class_type)
  10206.         return DW_ACCESS_private;
  10207.       else
  10208.         return DW_ACCESS_public;
  10209.     }
  10210. }

  10211. /* Look for DW_AT_data_member_location.  Set *OFFSET to the byte
  10212.    offset.  If the attribute was not found return 0, otherwise return
  10213.    1.  If it was found but could not properly be handled, set *OFFSET
  10214.    to 0.  */

  10215. static int
  10216. handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
  10217.                              LONGEST *offset)
  10218. {
  10219.   struct attribute *attr;

  10220.   attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
  10221.   if (attr != NULL)
  10222.     {
  10223.       *offset = 0;

  10224.       /* Note that we do not check for a section offset first here.
  10225.          This is because DW_AT_data_member_location is new in DWARF 4,
  10226.          so if we see it, we can assume that a constant form is really
  10227.          a constant and not a section offset.  */
  10228.       if (attr_form_is_constant (attr))
  10229.         *offset = dwarf2_get_attr_constant_value (attr, 0);
  10230.       else if (attr_form_is_section_offset (attr))
  10231.         dwarf2_complex_location_expr_complaint ();
  10232.       else if (attr_form_is_block (attr))
  10233.         *offset = decode_locdesc (DW_BLOCK (attr), cu);
  10234.       else
  10235.         dwarf2_complex_location_expr_complaint ();

  10236.       return 1;
  10237.     }

  10238.   return 0;
  10239. }

  10240. /* Add an aggregate field to the field list.  */

  10241. static void
  10242. dwarf2_add_field (struct field_info *fip, struct die_info *die,
  10243.                   struct dwarf2_cu *cu)
  10244. {
  10245.   struct objfile *objfile = cu->objfile;
  10246.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  10247.   struct nextfield *new_field;
  10248.   struct attribute *attr;
  10249.   struct field *fp;
  10250.   const char *fieldname = "";

  10251.   /* Allocate a new field list entry and link it in.  */
  10252.   new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
  10253.   make_cleanup (xfree, new_field);
  10254.   memset (new_field, 0, sizeof (struct nextfield));

  10255.   if (die->tag == DW_TAG_inheritance)
  10256.     {
  10257.       new_field->next = fip->baseclasses;
  10258.       fip->baseclasses = new_field;
  10259.     }
  10260.   else
  10261.     {
  10262.       new_field->next = fip->fields;
  10263.       fip->fields = new_field;
  10264.     }
  10265.   fip->nfields++;

  10266.   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
  10267.   if (attr)
  10268.     new_field->accessibility = DW_UNSND (attr);
  10269.   else
  10270.     new_field->accessibility = dwarf2_default_access_attribute (die, cu);
  10271.   if (new_field->accessibility != DW_ACCESS_public)
  10272.     fip->non_public_fields = 1;

  10273.   attr = dwarf2_attr (die, DW_AT_virtuality, cu);
  10274.   if (attr)
  10275.     new_field->virtuality = DW_UNSND (attr);
  10276.   else
  10277.     new_field->virtuality = DW_VIRTUALITY_none;

  10278.   fp = &new_field->field;

  10279.   if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
  10280.     {
  10281.       LONGEST offset;

  10282.       /* Data member other than a C++ static data member.  */

  10283.       /* Get type of field.  */
  10284.       fp->type = die_type (die, cu);

  10285.       SET_FIELD_BITPOS (*fp, 0);

  10286.       /* Get bit size of field (zero if none).  */
  10287.       attr = dwarf2_attr (die, DW_AT_bit_size, cu);
  10288.       if (attr)
  10289.         {
  10290.           FIELD_BITSIZE (*fp) = DW_UNSND (attr);
  10291.         }
  10292.       else
  10293.         {
  10294.           FIELD_BITSIZE (*fp) = 0;
  10295.         }

  10296.       /* Get bit offset of field.  */
  10297.       if (handle_data_member_location (die, cu, &offset))
  10298.         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
  10299.       attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
  10300.       if (attr)
  10301.         {
  10302.           if (gdbarch_bits_big_endian (gdbarch))
  10303.             {
  10304.               /* For big endian bits, the DW_AT_bit_offset gives the
  10305.                  additional bit offset from the MSB of the containing
  10306.                  anonymous object to the MSB of the field.  We don't
  10307.                  have to do anything special since we don't need to
  10308.                  know the size of the anonymous object.  */
  10309.               SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
  10310.             }
  10311.           else
  10312.             {
  10313.               /* For little endian bits, compute the bit offset to the
  10314.                  MSB of the anonymous object, subtract off the number of
  10315.                  bits from the MSB of the field to the MSB of the
  10316.                  object, and then subtract off the number of bits of
  10317.                  the field itself.  The result is the bit offset of
  10318.                  the LSB of the field.  */
  10319.               int anonymous_size;
  10320.               int bit_offset = DW_UNSND (attr);

  10321.               attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  10322.               if (attr)
  10323.                 {
  10324.                   /* The size of the anonymous object containing
  10325.                      the bit field is explicit, so use the
  10326.                      indicated size (in bytes).  */
  10327.                   anonymous_size = DW_UNSND (attr);
  10328.                 }
  10329.               else
  10330.                 {
  10331.                   /* The size of the anonymous object containing
  10332.                      the bit field must be inferred from the type
  10333.                      attribute of the data member containing the
  10334.                      bit field.  */
  10335.                   anonymous_size = TYPE_LENGTH (fp->type);
  10336.                 }
  10337.               SET_FIELD_BITPOS (*fp,
  10338.                                 (FIELD_BITPOS (*fp)
  10339.                                  + anonymous_size * bits_per_byte
  10340.                                  - bit_offset - FIELD_BITSIZE (*fp)));
  10341.             }
  10342.         }

  10343.       /* Get name of field.  */
  10344.       fieldname = dwarf2_name (die, cu);
  10345.       if (fieldname == NULL)
  10346.         fieldname = "";

  10347.       /* The name is already allocated along with this objfile, so we don't
  10348.          need to duplicate it for the type.  */
  10349.       fp->name = fieldname;

  10350.       /* Change accessibility for artificial fields (e.g. virtual table
  10351.          pointer or virtual base class pointer) to private.  */
  10352.       if (dwarf2_attr (die, DW_AT_artificial, cu))
  10353.         {
  10354.           FIELD_ARTIFICIAL (*fp) = 1;
  10355.           new_field->accessibility = DW_ACCESS_private;
  10356.           fip->non_public_fields = 1;
  10357.         }
  10358.     }
  10359.   else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
  10360.     {
  10361.       /* C++ static member.  */

  10362.       /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
  10363.          is a declaration, but all versions of G++ as of this writing
  10364.          (so through at least 3.2.1) incorrectly generate
  10365.          DW_TAG_variable tags.  */

  10366.       const char *physname;

  10367.       /* Get name of field.  */
  10368.       fieldname = dwarf2_name (die, cu);
  10369.       if (fieldname == NULL)
  10370.         return;

  10371.       attr = dwarf2_attr (die, DW_AT_const_value, cu);
  10372.       if (attr
  10373.           /* Only create a symbol if this is an external value.
  10374.              new_symbol checks this and puts the value in the global symbol
  10375.              table, which we want.  If it is not external, new_symbol
  10376.              will try to put the value in cu->list_in_scope which is wrong.  */
  10377.           && dwarf2_flag_true_p (die, DW_AT_external, cu))
  10378.         {
  10379.           /* A static const member, not much different than an enum as far as
  10380.              we're concerned, except that we can support more types.  */
  10381.           new_symbol (die, NULL, cu);
  10382.         }

  10383.       /* Get physical name.  */
  10384.       physname = dwarf2_physname (fieldname, die, cu);

  10385.       /* The name is already allocated along with this objfile, so we don't
  10386.          need to duplicate it for the type.  */
  10387.       SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
  10388.       FIELD_TYPE (*fp) = die_type (die, cu);
  10389.       FIELD_NAME (*fp) = fieldname;
  10390.     }
  10391.   else if (die->tag == DW_TAG_inheritance)
  10392.     {
  10393.       LONGEST offset;

  10394.       /* C++ base class field.  */
  10395.       if (handle_data_member_location (die, cu, &offset))
  10396.         SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
  10397.       FIELD_BITSIZE (*fp) = 0;
  10398.       FIELD_TYPE (*fp) = die_type (die, cu);
  10399.       FIELD_NAME (*fp) = type_name_no_tag (fp->type);
  10400.       fip->nbaseclasses++;
  10401.     }
  10402. }

  10403. /* Add a typedef defined in the scope of the FIP's class.  */

  10404. static void
  10405. dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
  10406.                     struct dwarf2_cu *cu)
  10407. {
  10408.   struct objfile *objfile = cu->objfile;
  10409.   struct typedef_field_list *new_field;
  10410.   struct attribute *attr;
  10411.   struct typedef_field *fp;
  10412.   char *fieldname = "";

  10413.   /* Allocate a new field list entry and link it in.  */
  10414.   new_field = xzalloc (sizeof (*new_field));
  10415.   make_cleanup (xfree, new_field);

  10416.   gdb_assert (die->tag == DW_TAG_typedef);

  10417.   fp = &new_field->field;

  10418.   /* Get name of field.  */
  10419.   fp->name = dwarf2_name (die, cu);
  10420.   if (fp->name == NULL)
  10421.     return;

  10422.   fp->type = read_type_die (die, cu);

  10423.   new_field->next = fip->typedef_field_list;
  10424.   fip->typedef_field_list = new_field;
  10425.   fip->typedef_field_list_count++;
  10426. }

  10427. /* Create the vector of fields, and attach it to the type.  */

  10428. static void
  10429. dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
  10430.                               struct dwarf2_cu *cu)
  10431. {
  10432.   int nfields = fip->nfields;

  10433.   /* Record the field count, allocate space for the array of fields,
  10434.      and create blank accessibility bitfields if necessary.  */
  10435.   TYPE_NFIELDS (type) = nfields;
  10436.   TYPE_FIELDS (type) = (struct field *)
  10437.     TYPE_ALLOC (type, sizeof (struct field) * nfields);
  10438.   memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);

  10439.   if (fip->non_public_fields && cu->language != language_ada)
  10440.     {
  10441.       ALLOCATE_CPLUS_STRUCT_TYPE (type);

  10442.       TYPE_FIELD_PRIVATE_BITS (type) =
  10443.         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
  10444.       B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);

  10445.       TYPE_FIELD_PROTECTED_BITS (type) =
  10446.         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
  10447.       B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);

  10448.       TYPE_FIELD_IGNORE_BITS (type) =
  10449.         (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
  10450.       B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
  10451.     }

  10452.   /* If the type has baseclasses, allocate and clear a bit vector for
  10453.      TYPE_FIELD_VIRTUAL_BITS.  */
  10454.   if (fip->nbaseclasses && cu->language != language_ada)
  10455.     {
  10456.       int num_bytes = B_BYTES (fip->nbaseclasses);
  10457.       unsigned char *pointer;

  10458.       ALLOCATE_CPLUS_STRUCT_TYPE (type);
  10459.       pointer = TYPE_ALLOC (type, num_bytes);
  10460.       TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
  10461.       B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
  10462.       TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
  10463.     }

  10464.   /* Copy the saved-up fields into the field vector.  Start from the head of
  10465.      the list, adding to the tail of the field array, so that they end up in
  10466.      the same order in the array in which they were added to the list.  */
  10467.   while (nfields-- > 0)
  10468.     {
  10469.       struct nextfield *fieldp;

  10470.       if (fip->fields)
  10471.         {
  10472.           fieldp = fip->fields;
  10473.           fip->fields = fieldp->next;
  10474.         }
  10475.       else
  10476.         {
  10477.           fieldp = fip->baseclasses;
  10478.           fip->baseclasses = fieldp->next;
  10479.         }

  10480.       TYPE_FIELD (type, nfields) = fieldp->field;
  10481.       switch (fieldp->accessibility)
  10482.         {
  10483.         case DW_ACCESS_private:
  10484.           if (cu->language != language_ada)
  10485.             SET_TYPE_FIELD_PRIVATE (type, nfields);
  10486.           break;

  10487.         case DW_ACCESS_protected:
  10488.           if (cu->language != language_ada)
  10489.             SET_TYPE_FIELD_PROTECTED (type, nfields);
  10490.           break;

  10491.         case DW_ACCESS_public:
  10492.           break;

  10493.         default:
  10494.           /* Unknown accessibility.  Complain and treat it as public.  */
  10495.           {
  10496.             complaint (&symfile_complaints, _("unsupported accessibility %d"),
  10497.                        fieldp->accessibility);
  10498.           }
  10499.           break;
  10500.         }
  10501.       if (nfields < fip->nbaseclasses)
  10502.         {
  10503.           switch (fieldp->virtuality)
  10504.             {
  10505.             case DW_VIRTUALITY_virtual:
  10506.             case DW_VIRTUALITY_pure_virtual:
  10507.               if (cu->language == language_ada)
  10508.                 error (_("unexpected virtuality in component of Ada type"));
  10509.               SET_TYPE_FIELD_VIRTUAL (type, nfields);
  10510.               break;
  10511.             }
  10512.         }
  10513.     }
  10514. }

  10515. /* Return true if this member function is a constructor, false
  10516.    otherwise.  */

  10517. static int
  10518. dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
  10519. {
  10520.   const char *fieldname;
  10521.   const char *typename;
  10522.   int len;

  10523.   if (die->parent == NULL)
  10524.     return 0;

  10525.   if (die->parent->tag != DW_TAG_structure_type
  10526.       && die->parent->tag != DW_TAG_union_type
  10527.       && die->parent->tag != DW_TAG_class_type)
  10528.     return 0;

  10529.   fieldname = dwarf2_name (die, cu);
  10530.   typename = dwarf2_name (die->parent, cu);
  10531.   if (fieldname == NULL || typename == NULL)
  10532.     return 0;

  10533.   len = strlen (fieldname);
  10534.   return (strncmp (fieldname, typename, len) == 0
  10535.           && (typename[len] == '\0' || typename[len] == '<'));
  10536. }

  10537. /* Add a member function to the proper fieldlist.  */

  10538. static void
  10539. dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
  10540.                       struct type *type, struct dwarf2_cu *cu)
  10541. {
  10542.   struct objfile *objfile = cu->objfile;
  10543.   struct attribute *attr;
  10544.   struct fnfieldlist *flp;
  10545.   int i;
  10546.   struct fn_field *fnp;
  10547.   const char *fieldname;
  10548.   struct nextfnfield *new_fnfield;
  10549.   struct type *this_type;
  10550.   enum dwarf_access_attribute accessibility;

  10551.   if (cu->language == language_ada)
  10552.     error (_("unexpected member function in Ada type"));

  10553.   /* Get name of member function.  */
  10554.   fieldname = dwarf2_name (die, cu);
  10555.   if (fieldname == NULL)
  10556.     return;

  10557.   /* Look up member function name in fieldlist.  */
  10558.   for (i = 0; i < fip->nfnfields; i++)
  10559.     {
  10560.       if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
  10561.         break;
  10562.     }

  10563.   /* Create new list element if necessary.  */
  10564.   if (i < fip->nfnfields)
  10565.     flp = &fip->fnfieldlists[i];
  10566.   else
  10567.     {
  10568.       if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
  10569.         {
  10570.           fip->fnfieldlists = (struct fnfieldlist *)
  10571.             xrealloc (fip->fnfieldlists,
  10572.                       (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
  10573.                       * sizeof (struct fnfieldlist));
  10574.           if (fip->nfnfields == 0)
  10575.             make_cleanup (free_current_contents, &fip->fnfieldlists);
  10576.         }
  10577.       flp = &fip->fnfieldlists[fip->nfnfields];
  10578.       flp->name = fieldname;
  10579.       flp->length = 0;
  10580.       flp->head = NULL;
  10581.       i = fip->nfnfields++;
  10582.     }

  10583.   /* Create a new member function field and chain it to the field list
  10584.      entry.  */
  10585.   new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
  10586.   make_cleanup (xfree, new_fnfield);
  10587.   memset (new_fnfield, 0, sizeof (struct nextfnfield));
  10588.   new_fnfield->next = flp->head;
  10589.   flp->head = new_fnfield;
  10590.   flp->length++;

  10591.   /* Fill in the member function field info.  */
  10592.   fnp = &new_fnfield->fnfield;

  10593.   /* Delay processing of the physname until later.  */
  10594.   if (cu->language == language_cplus || cu->language == language_java)
  10595.     {
  10596.       add_to_method_list (type, i, flp->length - 1, fieldname,
  10597.                           die, cu);
  10598.     }
  10599.   else
  10600.     {
  10601.       const char *physname = dwarf2_physname (fieldname, die, cu);
  10602.       fnp->physname = physname ? physname : "";
  10603.     }

  10604.   fnp->type = alloc_type (objfile);
  10605.   this_type = read_type_die (die, cu);
  10606.   if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
  10607.     {
  10608.       int nparams = TYPE_NFIELDS (this_type);

  10609.       /* TYPE is the domain of this method, and THIS_TYPE is the type
  10610.            of the method itself (TYPE_CODE_METHOD).  */
  10611.       smash_to_method_type (fnp->type, type,
  10612.                             TYPE_TARGET_TYPE (this_type),
  10613.                             TYPE_FIELDS (this_type),
  10614.                             TYPE_NFIELDS (this_type),
  10615.                             TYPE_VARARGS (this_type));

  10616.       /* Handle static member functions.
  10617.          Dwarf2 has no clean way to discern C++ static and non-static
  10618.          member functions.  G++ helps GDB by marking the first
  10619.          parameter for non-static member functions (which is the this
  10620.          pointer) as artificial.  We obtain this information from
  10621.          read_subroutine_type via TYPE_FIELD_ARTIFICIAL.  */
  10622.       if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
  10623.         fnp->voffset = VOFFSET_STATIC;
  10624.     }
  10625.   else
  10626.     complaint (&symfile_complaints, _("member function type missing for '%s'"),
  10627.                dwarf2_full_name (fieldname, die, cu));

  10628.   /* Get fcontext from DW_AT_containing_type if present.  */
  10629.   if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
  10630.     fnp->fcontext = die_containing_type (die, cu);

  10631.   /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
  10632.      is_volatile is irrelevant, as it is needed by gdb_mangle_name only.  */

  10633.   /* Get accessibility.  */
  10634.   attr = dwarf2_attr (die, DW_AT_accessibility, cu);
  10635.   if (attr)
  10636.     accessibility = DW_UNSND (attr);
  10637.   else
  10638.     accessibility = dwarf2_default_access_attribute (die, cu);
  10639.   switch (accessibility)
  10640.     {
  10641.     case DW_ACCESS_private:
  10642.       fnp->is_private = 1;
  10643.       break;
  10644.     case DW_ACCESS_protected:
  10645.       fnp->is_protected = 1;
  10646.       break;
  10647.     }

  10648.   /* Check for artificial methods.  */
  10649.   attr = dwarf2_attr (die, DW_AT_artificial, cu);
  10650.   if (attr && DW_UNSND (attr) != 0)
  10651.     fnp->is_artificial = 1;

  10652.   fnp->is_constructor = dwarf2_is_constructor (die, cu);

  10653.   /* Get index in virtual function table if it is a virtual member
  10654.      function.  For older versions of GCC, this is an offset in the
  10655.      appropriate virtual table, as specified by DW_AT_containing_type.
  10656.      For everyone else, it is an expression to be evaluated relative
  10657.      to the object address.  */

  10658.   attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
  10659.   if (attr)
  10660.     {
  10661.       if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
  10662.         {
  10663.           if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
  10664.             {
  10665.               /* Old-style GCC.  */
  10666.               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
  10667.             }
  10668.           else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
  10669.                    || (DW_BLOCK (attr)->size > 1
  10670.                        && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
  10671.                        && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
  10672.             {
  10673.               struct dwarf_block blk;
  10674.               int offset;

  10675.               offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
  10676.                         ? 1 : 2);
  10677.               blk.size = DW_BLOCK (attr)->size - offset;
  10678.               blk.data = DW_BLOCK (attr)->data + offset;
  10679.               fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
  10680.               if ((fnp->voffset % cu->header.addr_size) != 0)
  10681.                 dwarf2_complex_location_expr_complaint ();
  10682.               else
  10683.                 fnp->voffset /= cu->header.addr_size;
  10684.               fnp->voffset += 2;
  10685.             }
  10686.           else
  10687.             dwarf2_complex_location_expr_complaint ();

  10688.           if (!fnp->fcontext)
  10689.             fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
  10690.         }
  10691.       else if (attr_form_is_section_offset (attr))
  10692.         {
  10693.           dwarf2_complex_location_expr_complaint ();
  10694.         }
  10695.       else
  10696.         {
  10697.           dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
  10698.                                                  fieldname);
  10699.         }
  10700.     }
  10701.   else
  10702.     {
  10703.       attr = dwarf2_attr (die, DW_AT_virtuality, cu);
  10704.       if (attr && DW_UNSND (attr))
  10705.         {
  10706.           /* GCC does this, as of 2008-08-25; PR debug/37237.  */
  10707.           complaint (&symfile_complaints,
  10708.                      _("Member function \"%s\" (offset %d) is virtual "
  10709.                        "but the vtable offset is not specified"),
  10710.                      fieldname, die->offset.sect_off);
  10711.           ALLOCATE_CPLUS_STRUCT_TYPE (type);
  10712.           TYPE_CPLUS_DYNAMIC (type) = 1;
  10713.         }
  10714.     }
  10715. }

  10716. /* Create the vector of member function fields, and attach it to the type.  */

  10717. static void
  10718. dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
  10719.                                  struct dwarf2_cu *cu)
  10720. {
  10721.   struct fnfieldlist *flp;
  10722.   int i;

  10723.   if (cu->language == language_ada)
  10724.     error (_("unexpected member functions in Ada type"));

  10725.   ALLOCATE_CPLUS_STRUCT_TYPE (type);
  10726.   TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
  10727.     TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);

  10728.   for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
  10729.     {
  10730.       struct nextfnfield *nfp = flp->head;
  10731.       struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
  10732.       int k;

  10733.       TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
  10734.       TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
  10735.       fn_flp->fn_fields = (struct fn_field *)
  10736.         TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
  10737.       for (k = flp->length; (k--, nfp); nfp = nfp->next)
  10738.         fn_flp->fn_fields[k] = nfp->fnfield;
  10739.     }

  10740.   TYPE_NFN_FIELDS (type) = fip->nfnfields;
  10741. }

  10742. /* Returns non-zero if NAME is the name of a vtable member in CU's
  10743.    language, zero otherwise.  */
  10744. static int
  10745. is_vtable_name (const char *name, struct dwarf2_cu *cu)
  10746. {
  10747.   static const char vptr[] = "_vptr";
  10748.   static const char vtable[] = "vtable";

  10749.   /* Look for the C++ and Java forms of the vtable.  */
  10750.   if ((cu->language == language_java
  10751.        && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
  10752.        || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
  10753.        && is_cplus_marker (name[sizeof (vptr) - 1])))
  10754.     return 1;

  10755.   return 0;
  10756. }

  10757. /* GCC outputs unnamed structures that are really pointers to member
  10758.    functions, with the ABI-specified layout.  If TYPE describes
  10759.    such a structure, smash it into a member function type.

  10760.    GCC shouldn't do this; it should just output pointer to member DIEs.
  10761.    This is GCC PR debug/28767.  */

  10762. static void
  10763. quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
  10764. {
  10765.   struct type *pfn_type, *domain_type, *new_type;

  10766.   /* Check for a structure with no name and two children.  */
  10767.   if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
  10768.     return;

  10769.   /* Check for __pfn and __delta members.  */
  10770.   if (TYPE_FIELD_NAME (type, 0) == NULL
  10771.       || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
  10772.       || TYPE_FIELD_NAME (type, 1) == NULL
  10773.       || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
  10774.     return;

  10775.   /* Find the type of the method.  */
  10776.   pfn_type = TYPE_FIELD_TYPE (type, 0);
  10777.   if (pfn_type == NULL
  10778.       || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
  10779.       || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
  10780.     return;

  10781.   /* Look for the "this" argument.  */
  10782.   pfn_type = TYPE_TARGET_TYPE (pfn_type);
  10783.   if (TYPE_NFIELDS (pfn_type) == 0
  10784.       /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
  10785.       || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
  10786.     return;

  10787.   domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
  10788.   new_type = alloc_type (objfile);
  10789.   smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
  10790.                         TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
  10791.                         TYPE_VARARGS (pfn_type));
  10792.   smash_to_methodptr_type (type, new_type);
  10793. }

  10794. /* Return non-zero if the CU's PRODUCER string matches the Intel C/C++ compiler
  10795.    (icc).  */

  10796. static int
  10797. producer_is_icc (struct dwarf2_cu *cu)
  10798. {
  10799.   if (!cu->checked_producer)
  10800.     check_producer (cu);

  10801.   return cu->producer_is_icc;
  10802. }

  10803. /* Called when we find the DIE that starts a structure or union scope
  10804.    (definition) to create a type for the structure or union.  Fill in
  10805.    the type's name and general properties; the members will not be
  10806.    processed until process_structure_scopeA symbol table entry for
  10807.    the type will also not be done until process_structure_scope (assuming
  10808.    the type has a name).

  10809.    NOTE: we need to call these functions regardless of whether or not the
  10810.    DIE has a DW_AT_name attribute, since it might be an anonymous
  10811.    structure or union.  This gets the type entered into our set of
  10812.    user defined types.  */

  10813. static struct type *
  10814. read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
  10815. {
  10816.   struct objfile *objfile = cu->objfile;
  10817.   struct type *type;
  10818.   struct attribute *attr;
  10819.   const char *name;

  10820.   /* If the definition of this type lives in .debug_types, read that type.
  10821.      Don't follow DW_AT_specification though, that will take us back up
  10822.      the chain and we want to go down.  */
  10823.   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
  10824.   if (attr)
  10825.     {
  10826.       type = get_DW_AT_signature_type (die, attr, cu);

  10827.       /* The type's CU may not be the same as CU.
  10828.          Ensure TYPE is recorded with CU in die_type_hash.  */
  10829.       return set_die_type (die, type, cu);
  10830.     }

  10831.   type = alloc_type (objfile);
  10832.   INIT_CPLUS_SPECIFIC (type);

  10833.   name = dwarf2_name (die, cu);
  10834.   if (name != NULL)
  10835.     {
  10836.       if (cu->language == language_cplus
  10837.           || cu->language == language_java)
  10838.         {
  10839.           const char *full_name = dwarf2_full_name (name, die, cu);

  10840.           /* dwarf2_full_name might have already finished building the DIE's
  10841.              type.  If so, there is no need to continue.  */
  10842.           if (get_die_type (die, cu) != NULL)
  10843.             return get_die_type (die, cu);

  10844.           TYPE_TAG_NAME (type) = full_name;
  10845.           if (die->tag == DW_TAG_structure_type
  10846.               || die->tag == DW_TAG_class_type)
  10847.             TYPE_NAME (type) = TYPE_TAG_NAME (type);
  10848.         }
  10849.       else
  10850.         {
  10851.           /* The name is already allocated along with this objfile, so
  10852.              we don't need to duplicate it for the type.  */
  10853.           TYPE_TAG_NAME (type) = name;
  10854.           if (die->tag == DW_TAG_class_type)
  10855.             TYPE_NAME (type) = TYPE_TAG_NAME (type);
  10856.         }
  10857.     }

  10858.   if (die->tag == DW_TAG_structure_type)
  10859.     {
  10860.       TYPE_CODE (type) = TYPE_CODE_STRUCT;
  10861.     }
  10862.   else if (die->tag == DW_TAG_union_type)
  10863.     {
  10864.       TYPE_CODE (type) = TYPE_CODE_UNION;
  10865.     }
  10866.   else
  10867.     {
  10868.       TYPE_CODE (type) = TYPE_CODE_STRUCT;
  10869.     }

  10870.   if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
  10871.     TYPE_DECLARED_CLASS (type) = 1;

  10872.   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  10873.   if (attr)
  10874.     {
  10875.       TYPE_LENGTH (type) = DW_UNSND (attr);
  10876.     }
  10877.   else
  10878.     {
  10879.       TYPE_LENGTH (type) = 0;
  10880.     }

  10881.   if (producer_is_icc (cu) && (TYPE_LENGTH (type) == 0))
  10882.     {
  10883.       /* ICC does not output the required DW_AT_declaration
  10884.          on incomplete types, but gives them a size of zero.  */
  10885.       TYPE_STUB (type) = 1;
  10886.     }
  10887.   else
  10888.     TYPE_STUB_SUPPORTED (type) = 1;

  10889.   if (die_is_declaration (die, cu))
  10890.     TYPE_STUB (type) = 1;
  10891.   else if (attr == NULL && die->child == NULL
  10892.            && producer_is_realview (cu->producer))
  10893.     /* RealView does not output the required DW_AT_declaration
  10894.        on incomplete types.  */
  10895.     TYPE_STUB (type) = 1;

  10896.   /* We need to add the type field to the die immediately so we don't
  10897.      infinitely recurse when dealing with pointers to the structure
  10898.      type within the structure itself.  */
  10899.   set_die_type (die, type, cu);

  10900.   /* set_die_type should be already done.  */
  10901.   set_descriptive_type (type, die, cu);

  10902.   return type;
  10903. }

  10904. /* Finish creating a structure or union type, including filling in
  10905.    its members and creating a symbol for it.  */

  10906. static void
  10907. process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
  10908. {
  10909.   struct objfile *objfile = cu->objfile;
  10910.   struct die_info *child_die;
  10911.   struct type *type;

  10912.   type = get_die_type (die, cu);
  10913.   if (type == NULL)
  10914.     type = read_structure_type (die, cu);

  10915.   if (die->child != NULL && ! die_is_declaration (die, cu))
  10916.     {
  10917.       struct field_info fi;
  10918.       VEC (symbolp) *template_args = NULL;
  10919.       struct cleanup *back_to = make_cleanup (null_cleanup, 0);

  10920.       memset (&fi, 0, sizeof (struct field_info));

  10921.       child_die = die->child;

  10922.       while (child_die && child_die->tag)
  10923.         {
  10924.           if (child_die->tag == DW_TAG_member
  10925.               || child_die->tag == DW_TAG_variable)
  10926.             {
  10927.               /* NOTE: carlton/2002-11-05: A C++ static data member
  10928.                  should be a DW_TAG_member that is a declaration, but
  10929.                  all versions of G++ as of this writing (so through at
  10930.                  least 3.2.1) incorrectly generate DW_TAG_variable
  10931.                  tags for them instead.  */
  10932.               dwarf2_add_field (&fi, child_die, cu);
  10933.             }
  10934.           else if (child_die->tag == DW_TAG_subprogram)
  10935.             {
  10936.               /* C++ member function.  */
  10937.               dwarf2_add_member_fn (&fi, child_die, type, cu);
  10938.             }
  10939.           else if (child_die->tag == DW_TAG_inheritance)
  10940.             {
  10941.               /* C++ base class field.  */
  10942.               dwarf2_add_field (&fi, child_die, cu);
  10943.             }
  10944.           else if (child_die->tag == DW_TAG_typedef)
  10945.             dwarf2_add_typedef (&fi, child_die, cu);
  10946.           else if (child_die->tag == DW_TAG_template_type_param
  10947.                    || child_die->tag == DW_TAG_template_value_param)
  10948.             {
  10949.               struct symbol *arg = new_symbol (child_die, NULL, cu);

  10950.               if (arg != NULL)
  10951.                 VEC_safe_push (symbolp, template_args, arg);
  10952.             }

  10953.           child_die = sibling_die (child_die);
  10954.         }

  10955.       /* Attach template arguments to type.  */
  10956.       if (! VEC_empty (symbolp, template_args))
  10957.         {
  10958.           ALLOCATE_CPLUS_STRUCT_TYPE (type);
  10959.           TYPE_N_TEMPLATE_ARGUMENTS (type)
  10960.             = VEC_length (symbolp, template_args);
  10961.           TYPE_TEMPLATE_ARGUMENTS (type)
  10962.             = obstack_alloc (&objfile->objfile_obstack,
  10963.                              (TYPE_N_TEMPLATE_ARGUMENTS (type)
  10964.                               * sizeof (struct symbol *)));
  10965.           memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
  10966.                   VEC_address (symbolp, template_args),
  10967.                   (TYPE_N_TEMPLATE_ARGUMENTS (type)
  10968.                    * sizeof (struct symbol *)));
  10969.           VEC_free (symbolp, template_args);
  10970.         }

  10971.       /* Attach fields and member functions to the type.  */
  10972.       if (fi.nfields)
  10973.         dwarf2_attach_fields_to_type (&fi, type, cu);
  10974.       if (fi.nfnfields)
  10975.         {
  10976.           dwarf2_attach_fn_fields_to_type (&fi, type, cu);

  10977.           /* Get the type which refers to the base class (possibly this
  10978.              class itself) which contains the vtable pointer for the current
  10979.              class from the DW_AT_containing_type attribute.  This use of
  10980.              DW_AT_containing_type is a GNU extension.  */

  10981.           if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
  10982.             {
  10983.               struct type *t = die_containing_type (die, cu);

  10984.               TYPE_VPTR_BASETYPE (type) = t;
  10985.               if (type == t)
  10986.                 {
  10987.                   int i;

  10988.                   /* Our own class provides vtbl ptr.  */
  10989.                   for (i = TYPE_NFIELDS (t) - 1;
  10990.                        i >= TYPE_N_BASECLASSES (t);
  10991.                        --i)
  10992.                     {
  10993.                       const char *fieldname = TYPE_FIELD_NAME (t, i);

  10994.                       if (is_vtable_name (fieldname, cu))
  10995.                         {
  10996.                           TYPE_VPTR_FIELDNO (type) = i;
  10997.                           break;
  10998.                         }
  10999.                     }

  11000.                   /* Complain if virtual function table field not found.  */
  11001.                   if (i < TYPE_N_BASECLASSES (t))
  11002.                     complaint (&symfile_complaints,
  11003.                                _("virtual function table pointer "
  11004.                                  "not found when defining class '%s'"),
  11005.                                TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
  11006.                                "");
  11007.                 }
  11008.               else
  11009.                 {
  11010.                   TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
  11011.                 }
  11012.             }
  11013.           else if (cu->producer
  11014.                    && strncmp (cu->producer,
  11015.                                "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
  11016.             {
  11017.               /* The IBM XLC compiler does not provide direct indication
  11018.                  of the containing type, but the vtable pointer is
  11019.                  always named __vfp.  */

  11020.               int i;

  11021.               for (i = TYPE_NFIELDS (type) - 1;
  11022.                    i >= TYPE_N_BASECLASSES (type);
  11023.                    --i)
  11024.                 {
  11025.                   if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
  11026.                     {
  11027.                       TYPE_VPTR_FIELDNO (type) = i;
  11028.                       TYPE_VPTR_BASETYPE (type) = type;
  11029.                       break;
  11030.                     }
  11031.                 }
  11032.             }
  11033.         }

  11034.       /* Copy fi.typedef_field_list linked list elements content into the
  11035.          allocated array TYPE_TYPEDEF_FIELD_ARRAY (type).  */
  11036.       if (fi.typedef_field_list)
  11037.         {
  11038.           int i = fi.typedef_field_list_count;

  11039.           ALLOCATE_CPLUS_STRUCT_TYPE (type);
  11040.           TYPE_TYPEDEF_FIELD_ARRAY (type)
  11041.             = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
  11042.           TYPE_TYPEDEF_FIELD_COUNT (type) = i;

  11043.           /* Reverse the list order to keep the debug info elements order.  */
  11044.           while (--i >= 0)
  11045.             {
  11046.               struct typedef_field *dest, *src;

  11047.               dest = &TYPE_TYPEDEF_FIELD (type, i);
  11048.               src = &fi.typedef_field_list->field;
  11049.               fi.typedef_field_list = fi.typedef_field_list->next;
  11050.               *dest = *src;
  11051.             }
  11052.         }

  11053.       do_cleanups (back_to);

  11054.       if (HAVE_CPLUS_STRUCT (type))
  11055.         TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
  11056.     }

  11057.   quirk_gcc_member_function_pointer (type, objfile);

  11058.   /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
  11059.      snapshots) has been known to create a die giving a declaration
  11060.      for a class that has, as a child, a die giving a definition for a
  11061.      nested class.  So we have to process our children even if the
  11062.      current die is a declaration.  Normally, of course, a declaration
  11063.      won't have any children at all.  */

  11064.   child_die = die->child;

  11065.   while (child_die != NULL && child_die->tag)
  11066.     {
  11067.       if (child_die->tag == DW_TAG_member
  11068.           || child_die->tag == DW_TAG_variable
  11069.           || child_die->tag == DW_TAG_inheritance
  11070.           || child_die->tag == DW_TAG_template_value_param
  11071.           || child_die->tag == DW_TAG_template_type_param)
  11072.         {
  11073.           /* Do nothing.  */
  11074.         }
  11075.       else
  11076.         process_die (child_die, cu);

  11077.       child_die = sibling_die (child_die);
  11078.     }

  11079.   /* Do not consider external references.  According to the DWARF standard,
  11080.      these DIEs are identified by the fact that they have no byte_size
  11081.      attribute, and a declaration attribute.  */
  11082.   if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
  11083.       || !die_is_declaration (die, cu))
  11084.     new_symbol (die, type, cu);
  11085. }

  11086. /* Assuming DIE is an enumeration type, and TYPE is its associated type,
  11087.    update TYPE using some information only available in DIE's children.  */

  11088. static void
  11089. update_enumeration_type_from_children (struct die_info *die,
  11090.                                        struct type *type,
  11091.                                        struct dwarf2_cu *cu)
  11092. {
  11093.   struct obstack obstack;
  11094.   struct die_info *child_die;
  11095.   int unsigned_enum = 1;
  11096.   int flag_enum = 1;
  11097.   ULONGEST mask = 0;
  11098.   struct cleanup *old_chain;

  11099.   obstack_init (&obstack);
  11100.   old_chain = make_cleanup_obstack_free (&obstack);

  11101.   for (child_die = die->child;
  11102.        child_die != NULL && child_die->tag;
  11103.        child_die = sibling_die (child_die))
  11104.     {
  11105.       struct attribute *attr;
  11106.       LONGEST value;
  11107.       const gdb_byte *bytes;
  11108.       struct dwarf2_locexpr_baton *baton;
  11109.       const char *name;

  11110.       if (child_die->tag != DW_TAG_enumerator)
  11111.         continue;

  11112.       attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
  11113.       if (attr == NULL)
  11114.         continue;

  11115.       name = dwarf2_name (child_die, cu);
  11116.       if (name == NULL)
  11117.         name = "<anonymous enumerator>";

  11118.       dwarf2_const_value_attr (attr, type, name, &obstack, cu,
  11119.                                &value, &bytes, &baton);
  11120.       if (value < 0)
  11121.         {
  11122.           unsigned_enum = 0;
  11123.           flag_enum = 0;
  11124.         }
  11125.       else if ((mask & value) != 0)
  11126.         flag_enum = 0;
  11127.       else
  11128.         mask |= value;

  11129.       /* If we already know that the enum type is neither unsigned, nor
  11130.          a flag type, no need to look at the rest of the enumerates.  */
  11131.       if (!unsigned_enum && !flag_enum)
  11132.         break;
  11133.     }

  11134.   if (unsigned_enum)
  11135.     TYPE_UNSIGNED (type) = 1;
  11136.   if (flag_enum)
  11137.     TYPE_FLAG_ENUM (type) = 1;

  11138.   do_cleanups (old_chain);
  11139. }

  11140. /* Given a DW_AT_enumeration_type die, set its type.  We do not
  11141.    complete the type's fields yet, or create any symbols.  */

  11142. static struct type *
  11143. read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
  11144. {
  11145.   struct objfile *objfile = cu->objfile;
  11146.   struct type *type;
  11147.   struct attribute *attr;
  11148.   const char *name;

  11149.   /* If the definition of this type lives in .debug_types, read that type.
  11150.      Don't follow DW_AT_specification though, that will take us back up
  11151.      the chain and we want to go down.  */
  11152.   attr = dwarf2_attr_no_follow (die, DW_AT_signature);
  11153.   if (attr)
  11154.     {
  11155.       type = get_DW_AT_signature_type (die, attr, cu);

  11156.       /* The type's CU may not be the same as CU.
  11157.          Ensure TYPE is recorded with CU in die_type_hash.  */
  11158.       return set_die_type (die, type, cu);
  11159.     }

  11160.   type = alloc_type (objfile);

  11161.   TYPE_CODE (type) = TYPE_CODE_ENUM;
  11162.   name = dwarf2_full_name (NULL, die, cu);
  11163.   if (name != NULL)
  11164.     TYPE_TAG_NAME (type) = name;

  11165.   attr = dwarf2_attr (die, DW_AT_type, cu);
  11166.   if (attr != NULL)
  11167.     {
  11168.       struct type *underlying_type = die_type (die, cu);

  11169.       TYPE_TARGET_TYPE (type) = underlying_type;
  11170.     }

  11171.   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  11172.   if (attr)
  11173.     {
  11174.       TYPE_LENGTH (type) = DW_UNSND (attr);
  11175.     }
  11176.   else
  11177.     {
  11178.       TYPE_LENGTH (type) = 0;
  11179.     }

  11180.   /* The enumeration DIE can be incomplete.  In Ada, any type can be
  11181.      declared as private in the package spec, and then defined only
  11182.      inside the package body.  Such types are known as Taft Amendment
  11183.      Types.  When another package uses such a type, an incomplete DIE
  11184.      may be generated by the compiler.  */
  11185.   if (die_is_declaration (die, cu))
  11186.     TYPE_STUB (type) = 1;

  11187.   /* Finish the creation of this type by using the enum's children.
  11188.      We must call this even when the underlying type has been provided
  11189.      so that we can determine if we're looking at a "flag" enum.  */
  11190.   update_enumeration_type_from_children (die, type, cu);

  11191.   /* If this type has an underlying type that is not a stub, then we
  11192.      may use its attributes.  We always use the "unsigned" attribute
  11193.      in this situation, because ordinarily we guess whether the type
  11194.      is unsigned -- but the guess can be wrong and the underlying type
  11195.      can tell us the reality.  However, we defer to a local size
  11196.      attribute if one exists, because this lets the compiler override
  11197.      the underlying type if needed.  */
  11198.   if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
  11199.     {
  11200.       TYPE_UNSIGNED (type) = TYPE_UNSIGNED (TYPE_TARGET_TYPE (type));
  11201.       if (TYPE_LENGTH (type) == 0)
  11202.         TYPE_LENGTH (type) = TYPE_LENGTH (TYPE_TARGET_TYPE (type));
  11203.     }

  11204.   TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);

  11205.   return set_die_type (die, type, cu);
  11206. }

  11207. /* Given a pointer to a die which begins an enumeration, process all
  11208.    the dies that define the members of the enumeration, and create the
  11209.    symbol for the enumeration type.

  11210.    NOTE: We reverse the order of the element list.  */

  11211. static void
  11212. process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
  11213. {
  11214.   struct type *this_type;

  11215.   this_type = get_die_type (die, cu);
  11216.   if (this_type == NULL)
  11217.     this_type = read_enumeration_type (die, cu);

  11218.   if (die->child != NULL)
  11219.     {
  11220.       struct die_info *child_die;
  11221.       struct symbol *sym;
  11222.       struct field *fields = NULL;
  11223.       int num_fields = 0;
  11224.       const char *name;

  11225.       child_die = die->child;
  11226.       while (child_die && child_die->tag)
  11227.         {
  11228.           if (child_die->tag != DW_TAG_enumerator)
  11229.             {
  11230.               process_die (child_die, cu);
  11231.             }
  11232.           else
  11233.             {
  11234.               name = dwarf2_name (child_die, cu);
  11235.               if (name)
  11236.                 {
  11237.                   sym = new_symbol (child_die, this_type, cu);

  11238.                   if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
  11239.                     {
  11240.                       fields = (struct field *)
  11241.                         xrealloc (fields,
  11242.                                   (num_fields + DW_FIELD_ALLOC_CHUNK)
  11243.                                   * sizeof (struct field));
  11244.                     }

  11245.                   FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
  11246.                   FIELD_TYPE (fields[num_fields]) = NULL;
  11247.                   SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
  11248.                   FIELD_BITSIZE (fields[num_fields]) = 0;

  11249.                   num_fields++;
  11250.                 }
  11251.             }

  11252.           child_die = sibling_die (child_die);
  11253.         }

  11254.       if (num_fields)
  11255.         {
  11256.           TYPE_NFIELDS (this_type) = num_fields;
  11257.           TYPE_FIELDS (this_type) = (struct field *)
  11258.             TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
  11259.           memcpy (TYPE_FIELDS (this_type), fields,
  11260.                   sizeof (struct field) * num_fields);
  11261.           xfree (fields);
  11262.         }
  11263.     }

  11264.   /* If we are reading an enum from a .debug_types unit, and the enum
  11265.      is a declaration, and the enum is not the signatured type in the
  11266.      unit, then we do not want to add a symbol for it.  Adding a
  11267.      symbol would in some cases obscure the true definition of the
  11268.      enum, giving users an incomplete type when the definition is
  11269.      actually available.  Note that we do not want to do this for all
  11270.      enums which are just declarations, because C++0x allows forward
  11271.      enum declarations.  */
  11272.   if (cu->per_cu->is_debug_types
  11273.       && die_is_declaration (die, cu))
  11274.     {
  11275.       struct signatured_type *sig_type;

  11276.       sig_type = (struct signatured_type *) cu->per_cu;
  11277.       gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
  11278.       if (sig_type->type_offset_in_section.sect_off != die->offset.sect_off)
  11279.         return;
  11280.     }

  11281.   new_symbol (die, this_type, cu);
  11282. }

  11283. /* Extract all information from a DW_TAG_array_type DIE and put it in
  11284.    the DIE's type field.  For now, this only handles one dimensional
  11285.    arrays.  */

  11286. static struct type *
  11287. read_array_type (struct die_info *die, struct dwarf2_cu *cu)
  11288. {
  11289.   struct objfile *objfile = cu->objfile;
  11290.   struct die_info *child_die;
  11291.   struct type *type;
  11292.   struct type *element_type, *range_type, *index_type;
  11293.   struct type **range_types = NULL;
  11294.   struct attribute *attr;
  11295.   int ndim = 0;
  11296.   struct cleanup *back_to;
  11297.   const char *name;
  11298.   unsigned int bit_stride = 0;

  11299.   element_type = die_type (die, cu);

  11300.   /* The die_type call above may have already set the type for this DIE.  */
  11301.   type = get_die_type (die, cu);
  11302.   if (type)
  11303.     return type;

  11304.   attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
  11305.   if (attr != NULL)
  11306.     bit_stride = DW_UNSND (attr) * 8;

  11307.   attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
  11308.   if (attr != NULL)
  11309.     bit_stride = DW_UNSND (attr);

  11310.   /* Irix 6.2 native cc creates array types without children for
  11311.      arrays with unspecified length.  */
  11312.   if (die->child == NULL)
  11313.     {
  11314.       index_type = objfile_type (objfile)->builtin_int;
  11315.       range_type = create_static_range_type (NULL, index_type, 0, -1);
  11316.       type = create_array_type_with_stride (NULL, element_type, range_type,
  11317.                                             bit_stride);
  11318.       return set_die_type (die, type, cu);
  11319.     }

  11320.   back_to = make_cleanup (null_cleanup, NULL);
  11321.   child_die = die->child;
  11322.   while (child_die && child_die->tag)
  11323.     {
  11324.       if (child_die->tag == DW_TAG_subrange_type)
  11325.         {
  11326.           struct type *child_type = read_type_die (child_die, cu);

  11327.           if (child_type != NULL)
  11328.             {
  11329.               /* The range type was succesfully read.  Save it for the
  11330.                  array type creation.  */
  11331.               if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
  11332.                 {
  11333.                   range_types = (struct type **)
  11334.                     xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
  11335.                               * sizeof (struct type *));
  11336.                   if (ndim == 0)
  11337.                     make_cleanup (free_current_contents, &range_types);
  11338.                 }
  11339.               range_types[ndim++] = child_type;
  11340.             }
  11341.         }
  11342.       child_die = sibling_die (child_die);
  11343.     }

  11344.   /* Dwarf2 dimensions are output from left to right, create the
  11345.      necessary array types in backwards order.  */

  11346.   type = element_type;

  11347.   if (read_array_order (die, cu) == DW_ORD_col_major)
  11348.     {
  11349.       int i = 0;

  11350.       while (i < ndim)
  11351.         type = create_array_type_with_stride (NULL, type, range_types[i++],
  11352.                                               bit_stride);
  11353.     }
  11354.   else
  11355.     {
  11356.       while (ndim-- > 0)
  11357.         type = create_array_type_with_stride (NULL, type, range_types[ndim],
  11358.                                               bit_stride);
  11359.     }

  11360.   /* Understand Dwarf2 support for vector types (like they occur on
  11361.      the PowerPC w/ AltiVec).  Gcc just adds another attribute to the
  11362.      array type.  This is not part of the Dwarf2/3 standard yet, but a
  11363.      custom vendor extension.  The main difference between a regular
  11364.      array and the vector variant is that vectors are passed by value
  11365.      to functions.  */
  11366.   attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
  11367.   if (attr)
  11368.     make_vector_type (type);

  11369.   /* The DIE may have DW_AT_byte_size set.  For example an OpenCL
  11370.      implementation may choose to implement triple vectors using this
  11371.      attribute.  */
  11372.   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  11373.   if (attr)
  11374.     {
  11375.       if (DW_UNSND (attr) >= TYPE_LENGTH (type))
  11376.         TYPE_LENGTH (type) = DW_UNSND (attr);
  11377.       else
  11378.         complaint (&symfile_complaints,
  11379.                    _("DW_AT_byte_size for array type smaller "
  11380.                      "than the total size of elements"));
  11381.     }

  11382.   name = dwarf2_name (die, cu);
  11383.   if (name)
  11384.     TYPE_NAME (type) = name;

  11385.   /* Install the type in the die.  */
  11386.   set_die_type (die, type, cu);

  11387.   /* set_die_type should be already done.  */
  11388.   set_descriptive_type (type, die, cu);

  11389.   do_cleanups (back_to);

  11390.   return type;
  11391. }

  11392. static enum dwarf_array_dim_ordering
  11393. read_array_order (struct die_info *die, struct dwarf2_cu *cu)
  11394. {
  11395.   struct attribute *attr;

  11396.   attr = dwarf2_attr (die, DW_AT_ordering, cu);

  11397.   if (attr) return DW_SND (attr);

  11398.   /* GNU F77 is a special case, as at 08/2004 array type info is the
  11399.      opposite order to the dwarf2 specification, but data is still
  11400.      laid out as per normal fortran.

  11401.      FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
  11402.      version checking.  */

  11403.   if (cu->language == language_fortran
  11404.       && cu->producer && strstr (cu->producer, "GNU F77"))
  11405.     {
  11406.       return DW_ORD_row_major;
  11407.     }

  11408.   switch (cu->language_defn->la_array_ordering)
  11409.     {
  11410.     case array_column_major:
  11411.       return DW_ORD_col_major;
  11412.     case array_row_major:
  11413.     default:
  11414.       return DW_ORD_row_major;
  11415.     };
  11416. }

  11417. /* Extract all information from a DW_TAG_set_type DIE and put it in
  11418.    the DIE's type field.  */

  11419. static struct type *
  11420. read_set_type (struct die_info *die, struct dwarf2_cu *cu)
  11421. {
  11422.   struct type *domain_type, *set_type;
  11423.   struct attribute *attr;

  11424.   domain_type = die_type (die, cu);

  11425.   /* The die_type call above may have already set the type for this DIE.  */
  11426.   set_type = get_die_type (die, cu);
  11427.   if (set_type)
  11428.     return set_type;

  11429.   set_type = create_set_type (NULL, domain_type);

  11430.   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  11431.   if (attr)
  11432.     TYPE_LENGTH (set_type) = DW_UNSND (attr);

  11433.   return set_die_type (die, set_type, cu);
  11434. }

  11435. /* A helper for read_common_block that creates a locexpr baton.
  11436.    SYM is the symbol which we are marking as computed.
  11437.    COMMON_DIE is the DIE for the common block.
  11438.    COMMON_LOC is the location expression attribute for the common
  11439.    block itself.
  11440.    MEMBER_LOC is the location expression attribute for the particular
  11441.    member of the common block that we are processing.
  11442.    CU is the CU from which the above come.  */

  11443. static void
  11444. mark_common_block_symbol_computed (struct symbol *sym,
  11445.                                    struct die_info *common_die,
  11446.                                    struct attribute *common_loc,
  11447.                                    struct attribute *member_loc,
  11448.                                    struct dwarf2_cu *cu)
  11449. {
  11450.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  11451.   struct dwarf2_locexpr_baton *baton;
  11452.   gdb_byte *ptr;
  11453.   unsigned int cu_off;
  11454.   enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
  11455.   LONGEST offset = 0;

  11456.   gdb_assert (common_loc && member_loc);
  11457.   gdb_assert (attr_form_is_block (common_loc));
  11458.   gdb_assert (attr_form_is_block (member_loc)
  11459.               || attr_form_is_constant (member_loc));

  11460.   baton = obstack_alloc (&objfile->objfile_obstack,
  11461.                          sizeof (struct dwarf2_locexpr_baton));
  11462.   baton->per_cu = cu->per_cu;
  11463.   gdb_assert (baton->per_cu);

  11464.   baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;

  11465.   if (attr_form_is_constant (member_loc))
  11466.     {
  11467.       offset = dwarf2_get_attr_constant_value (member_loc, 0);
  11468.       baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
  11469.     }
  11470.   else
  11471.     baton->size += DW_BLOCK (member_loc)->size;

  11472.   ptr = obstack_alloc (&objfile->objfile_obstack, baton->size);
  11473.   baton->data = ptr;

  11474.   *ptr++ = DW_OP_call4;
  11475.   cu_off = common_die->offset.sect_off - cu->per_cu->offset.sect_off;
  11476.   store_unsigned_integer (ptr, 4, byte_order, cu_off);
  11477.   ptr += 4;

  11478.   if (attr_form_is_constant (member_loc))
  11479.     {
  11480.       *ptr++ = DW_OP_addr;
  11481.       store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
  11482.       ptr += cu->header.addr_size;
  11483.     }
  11484.   else
  11485.     {
  11486.       /* We have to copy the data here, because DW_OP_call4 will only
  11487.          use a DW_AT_location attribute.  */
  11488.       memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
  11489.       ptr += DW_BLOCK (member_loc)->size;
  11490.     }

  11491.   *ptr++ = DW_OP_plus;
  11492.   gdb_assert (ptr - baton->data == baton->size);

  11493.   SYMBOL_LOCATION_BATON (sym) = baton;
  11494.   SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
  11495. }

  11496. /* Create appropriate locally-scoped variables for all the
  11497.    DW_TAG_common_block entries.  Also create a struct common_block
  11498.    listing all such variables for `info common'.  COMMON_BLOCK_DOMAIN
  11499.    is used to sepate the common blocks name namespace from regular
  11500.    variable names.  */

  11501. static void
  11502. read_common_block (struct die_info *die, struct dwarf2_cu *cu)
  11503. {
  11504.   struct attribute *attr;

  11505.   attr = dwarf2_attr (die, DW_AT_location, cu);
  11506.   if (attr)
  11507.     {
  11508.       /* Support the .debug_loc offsets.  */
  11509.       if (attr_form_is_block (attr))
  11510.         {
  11511.           /* Ok.  */
  11512.         }
  11513.       else if (attr_form_is_section_offset (attr))
  11514.         {
  11515.           dwarf2_complex_location_expr_complaint ();
  11516.           attr = NULL;
  11517.         }
  11518.       else
  11519.         {
  11520.           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
  11521.                                                  "common block member");
  11522.           attr = NULL;
  11523.         }
  11524.     }

  11525.   if (die->child != NULL)
  11526.     {
  11527.       struct objfile *objfile = cu->objfile;
  11528.       struct die_info *child_die;
  11529.       size_t n_entries = 0, size;
  11530.       struct common_block *common_block;
  11531.       struct symbol *sym;

  11532.       for (child_die = die->child;
  11533.            child_die && child_die->tag;
  11534.            child_die = sibling_die (child_die))
  11535.         ++n_entries;

  11536.       size = (sizeof (struct common_block)
  11537.               + (n_entries - 1) * sizeof (struct symbol *));
  11538.       common_block = obstack_alloc (&objfile->objfile_obstack, size);
  11539.       memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
  11540.       common_block->n_entries = 0;

  11541.       for (child_die = die->child;
  11542.            child_die && child_die->tag;
  11543.            child_die = sibling_die (child_die))
  11544.         {
  11545.           /* Create the symbol in the DW_TAG_common_block block in the current
  11546.              symbol scope.  */
  11547.           sym = new_symbol (child_die, NULL, cu);
  11548.           if (sym != NULL)
  11549.             {
  11550.               struct attribute *member_loc;

  11551.               common_block->contents[common_block->n_entries++] = sym;

  11552.               member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
  11553.                                         cu);
  11554.               if (member_loc)
  11555.                 {
  11556.                   /* GDB has handled this for a long time, but it is
  11557.                      not specified by DWARF.  It seems to have been
  11558.                      emitted by gfortran at least as recently as:
  11559.                      http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057.  */
  11560.                   complaint (&symfile_complaints,
  11561.                              _("Variable in common block has "
  11562.                                "DW_AT_data_member_location "
  11563.                                "- DIE at 0x%x [in module %s]"),
  11564.                              child_die->offset.sect_off,
  11565.                              objfile_name (cu->objfile));

  11566.                   if (attr_form_is_section_offset (member_loc))
  11567.                     dwarf2_complex_location_expr_complaint ();
  11568.                   else if (attr_form_is_constant (member_loc)
  11569.                            || attr_form_is_block (member_loc))
  11570.                     {
  11571.                       if (attr)
  11572.                         mark_common_block_symbol_computed (sym, die, attr,
  11573.                                                            member_loc, cu);
  11574.                     }
  11575.                   else
  11576.                     dwarf2_complex_location_expr_complaint ();
  11577.                 }
  11578.             }
  11579.         }

  11580.       sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
  11581.       SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
  11582.     }
  11583. }

  11584. /* Create a type for a C++ namespace.  */

  11585. static struct type *
  11586. read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
  11587. {
  11588.   struct objfile *objfile = cu->objfile;
  11589.   const char *previous_prefix, *name;
  11590.   int is_anonymous;
  11591.   struct type *type;

  11592.   /* For extensions, reuse the type of the original namespace.  */
  11593.   if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
  11594.     {
  11595.       struct die_info *ext_die;
  11596.       struct dwarf2_cu *ext_cu = cu;

  11597.       ext_die = dwarf2_extension (die, &ext_cu);
  11598.       type = read_type_die (ext_die, ext_cu);

  11599.       /* EXT_CU may not be the same as CU.
  11600.          Ensure TYPE is recorded with CU in die_type_hash.  */
  11601.       return set_die_type (die, type, cu);
  11602.     }

  11603.   name = namespace_name (die, &is_anonymous, cu);

  11604.   /* Now build the name of the current namespace.  */

  11605.   previous_prefix = determine_prefix (die, cu);
  11606.   if (previous_prefix[0] != '\0')
  11607.     name = typename_concat (&objfile->objfile_obstack,
  11608.                             previous_prefix, name, 0, cu);

  11609.   /* Create the type.  */
  11610.   type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
  11611.                     objfile);
  11612.   TYPE_NAME (type) = name;
  11613.   TYPE_TAG_NAME (type) = TYPE_NAME (type);

  11614.   return set_die_type (die, type, cu);
  11615. }

  11616. /* Read a C++ namespace.  */

  11617. static void
  11618. read_namespace (struct die_info *die, struct dwarf2_cu *cu)
  11619. {
  11620.   struct objfile *objfile = cu->objfile;
  11621.   int is_anonymous;

  11622.   /* Add a symbol associated to this if we haven't seen the namespace
  11623.      before.  Also, add a using directive if it's an anonymous
  11624.      namespace.  */

  11625.   if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
  11626.     {
  11627.       struct type *type;

  11628.       type = read_type_die (die, cu);
  11629.       new_symbol (die, type, cu);

  11630.       namespace_name (die, &is_anonymous, cu);
  11631.       if (is_anonymous)
  11632.         {
  11633.           const char *previous_prefix = determine_prefix (die, cu);

  11634.           cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
  11635.                                   NULL, NULL, 0, &objfile->objfile_obstack);
  11636.         }
  11637.     }

  11638.   if (die->child != NULL)
  11639.     {
  11640.       struct die_info *child_die = die->child;

  11641.       while (child_die && child_die->tag)
  11642.         {
  11643.           process_die (child_die, cu);
  11644.           child_die = sibling_die (child_die);
  11645.         }
  11646.     }
  11647. }

  11648. /* Read a Fortran module as type.  This DIE can be only a declaration used for
  11649.    imported module.  Still we need that type as local Fortran "use ... only"
  11650.    declaration imports depend on the created type in determine_prefix.  */

  11651. static struct type *
  11652. read_module_type (struct die_info *die, struct dwarf2_cu *cu)
  11653. {
  11654.   struct objfile *objfile = cu->objfile;
  11655.   const char *module_name;
  11656.   struct type *type;

  11657.   module_name = dwarf2_name (die, cu);
  11658.   if (!module_name)
  11659.     complaint (&symfile_complaints,
  11660.                _("DW_TAG_module has no name, offset 0x%x"),
  11661.                die->offset.sect_off);
  11662.   type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);

  11663.   /* determine_prefix uses TYPE_TAG_NAME.  */
  11664.   TYPE_TAG_NAME (type) = TYPE_NAME (type);

  11665.   return set_die_type (die, type, cu);
  11666. }

  11667. /* Read a Fortran module.  */

  11668. static void
  11669. read_module (struct die_info *die, struct dwarf2_cu *cu)
  11670. {
  11671.   struct die_info *child_die = die->child;
  11672.   struct type *type;

  11673.   type = read_type_die (die, cu);
  11674.   new_symbol (die, type, cu);

  11675.   while (child_die && child_die->tag)
  11676.     {
  11677.       process_die (child_die, cu);
  11678.       child_die = sibling_die (child_die);
  11679.     }
  11680. }

  11681. /* Return the name of the namespace represented by DIE.  Set
  11682.    *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
  11683.    namespace.  */

  11684. static const char *
  11685. namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
  11686. {
  11687.   struct die_info *current_die;
  11688.   const char *name = NULL;

  11689.   /* Loop through the extensions until we find a name.  */

  11690.   for (current_die = die;
  11691.        current_die != NULL;
  11692.        current_die = dwarf2_extension (die, &cu))
  11693.     {
  11694.       name = dwarf2_name (current_die, cu);
  11695.       if (name != NULL)
  11696.         break;
  11697.     }

  11698.   /* Is it an anonymous namespace?  */

  11699.   *is_anonymous = (name == NULL);
  11700.   if (*is_anonymous)
  11701.     name = CP_ANONYMOUS_NAMESPACE_STR;

  11702.   return name;
  11703. }

  11704. /* Extract all information from a DW_TAG_pointer_type DIE and add to
  11705.    the user defined type vector.  */

  11706. static struct type *
  11707. read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
  11708. {
  11709.   struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
  11710.   struct comp_unit_head *cu_header = &cu->header;
  11711.   struct type *type;
  11712.   struct attribute *attr_byte_size;
  11713.   struct attribute *attr_address_class;
  11714.   int byte_size, addr_class;
  11715.   struct type *target_type;

  11716.   target_type = die_type (die, cu);

  11717.   /* The die_type call above may have already set the type for this DIE.  */
  11718.   type = get_die_type (die, cu);
  11719.   if (type)
  11720.     return type;

  11721.   type = lookup_pointer_type (target_type);

  11722.   attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
  11723.   if (attr_byte_size)
  11724.     byte_size = DW_UNSND (attr_byte_size);
  11725.   else
  11726.     byte_size = cu_header->addr_size;

  11727.   attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
  11728.   if (attr_address_class)
  11729.     addr_class = DW_UNSND (attr_address_class);
  11730.   else
  11731.     addr_class = DW_ADDR_none;

  11732.   /* If the pointer size or address class is different than the
  11733.      default, create a type variant marked as such and set the
  11734.      length accordingly.  */
  11735.   if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
  11736.     {
  11737.       if (gdbarch_address_class_type_flags_p (gdbarch))
  11738.         {
  11739.           int type_flags;

  11740.           type_flags = gdbarch_address_class_type_flags
  11741.                          (gdbarch, byte_size, addr_class);
  11742.           gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
  11743.                       == 0);
  11744.           type = make_type_with_address_space (type, type_flags);
  11745.         }
  11746.       else if (TYPE_LENGTH (type) != byte_size)
  11747.         {
  11748.           complaint (&symfile_complaints,
  11749.                      _("invalid pointer size %d"), byte_size);
  11750.         }
  11751.       else
  11752.         {
  11753.           /* Should we also complain about unhandled address classes?  */
  11754.         }
  11755.     }

  11756.   TYPE_LENGTH (type) = byte_size;
  11757.   return set_die_type (die, type, cu);
  11758. }

  11759. /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
  11760.    the user defined type vector.  */

  11761. static struct type *
  11762. read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
  11763. {
  11764.   struct type *type;
  11765.   struct type *to_type;
  11766.   struct type *domain;

  11767.   to_type = die_type (die, cu);
  11768.   domain = die_containing_type (die, cu);

  11769.   /* The calls above may have already set the type for this DIE.  */
  11770.   type = get_die_type (die, cu);
  11771.   if (type)
  11772.     return type;

  11773.   if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
  11774.     type = lookup_methodptr_type (to_type);
  11775.   else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
  11776.     {
  11777.       struct type *new_type = alloc_type (cu->objfile);

  11778.       smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
  11779.                             TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
  11780.                             TYPE_VARARGS (to_type));
  11781.       type = lookup_methodptr_type (new_type);
  11782.     }
  11783.   else
  11784.     type = lookup_memberptr_type (to_type, domain);

  11785.   return set_die_type (die, type, cu);
  11786. }

  11787. /* Extract all information from a DW_TAG_reference_type DIE and add to
  11788.    the user defined type vector.  */

  11789. static struct type *
  11790. read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
  11791. {
  11792.   struct comp_unit_head *cu_header = &cu->header;
  11793.   struct type *type, *target_type;
  11794.   struct attribute *attr;

  11795.   target_type = die_type (die, cu);

  11796.   /* The die_type call above may have already set the type for this DIE.  */
  11797.   type = get_die_type (die, cu);
  11798.   if (type)
  11799.     return type;

  11800.   type = lookup_reference_type (target_type);
  11801.   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  11802.   if (attr)
  11803.     {
  11804.       TYPE_LENGTH (type) = DW_UNSND (attr);
  11805.     }
  11806.   else
  11807.     {
  11808.       TYPE_LENGTH (type) = cu_header->addr_size;
  11809.     }
  11810.   return set_die_type (die, type, cu);
  11811. }

  11812. /* Add the given cv-qualifiers to the element type of the array.  GCC
  11813.    outputs DWARF type qualifiers that apply to an array, not the
  11814.    element type.  But GDB relies on the array element type to carry
  11815.    the cv-qualifiers.  This mimics section 6.7.3 of the C99
  11816.    specification.  */

  11817. static struct type *
  11818. add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
  11819.                    struct type *base_type, int cnst, int voltl)
  11820. {
  11821.   struct type *el_type, *inner_array;

  11822.   base_type = copy_type (base_type);
  11823.   inner_array = base_type;

  11824.   while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
  11825.     {
  11826.       TYPE_TARGET_TYPE (inner_array) =
  11827.         copy_type (TYPE_TARGET_TYPE (inner_array));
  11828.       inner_array = TYPE_TARGET_TYPE (inner_array);
  11829.     }

  11830.   el_type = TYPE_TARGET_TYPE (inner_array);
  11831.   cnst |= TYPE_CONST (el_type);
  11832.   voltl |= TYPE_VOLATILE (el_type);
  11833.   TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);

  11834.   return set_die_type (die, base_type, cu);
  11835. }

  11836. static struct type *
  11837. read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
  11838. {
  11839.   struct type *base_type, *cv_type;

  11840.   base_type = die_type (die, cu);

  11841.   /* The die_type call above may have already set the type for this DIE.  */
  11842.   cv_type = get_die_type (die, cu);
  11843.   if (cv_type)
  11844.     return cv_type;

  11845.   /* In case the const qualifier is applied to an array type, the element type
  11846.      is so qualified, not the array type (section 6.7.3 of C99).  */
  11847.   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
  11848.     return add_array_cv_type (die, cu, base_type, 1, 0);

  11849.   cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
  11850.   return set_die_type (die, cv_type, cu);
  11851. }

  11852. static struct type *
  11853. read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
  11854. {
  11855.   struct type *base_type, *cv_type;

  11856.   base_type = die_type (die, cu);

  11857.   /* The die_type call above may have already set the type for this DIE.  */
  11858.   cv_type = get_die_type (die, cu);
  11859.   if (cv_type)
  11860.     return cv_type;

  11861.   /* In case the volatile qualifier is applied to an array type, the
  11862.      element type is so qualified, not the array type (section 6.7.3
  11863.      of C99).  */
  11864.   if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
  11865.     return add_array_cv_type (die, cu, base_type, 0, 1);

  11866.   cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
  11867.   return set_die_type (die, cv_type, cu);
  11868. }

  11869. /* Handle DW_TAG_restrict_type.  */

  11870. static struct type *
  11871. read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
  11872. {
  11873.   struct type *base_type, *cv_type;

  11874.   base_type = die_type (die, cu);

  11875.   /* The die_type call above may have already set the type for this DIE.  */
  11876.   cv_type = get_die_type (die, cu);
  11877.   if (cv_type)
  11878.     return cv_type;

  11879.   cv_type = make_restrict_type (base_type);
  11880.   return set_die_type (die, cv_type, cu);
  11881. }

  11882. /* Extract all information from a DW_TAG_string_type DIE and add to
  11883.    the user defined type vector.  It isn't really a user defined type,
  11884.    but it behaves like one, with other DIE's using an AT_user_def_type
  11885.    attribute to reference it.  */

  11886. static struct type *
  11887. read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
  11888. {
  11889.   struct objfile *objfile = cu->objfile;
  11890.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  11891.   struct type *type, *range_type, *index_type, *char_type;
  11892.   struct attribute *attr;
  11893.   unsigned int length;

  11894.   attr = dwarf2_attr (die, DW_AT_string_length, cu);
  11895.   if (attr)
  11896.     {
  11897.       length = DW_UNSND (attr);
  11898.     }
  11899.   else
  11900.     {
  11901.       /* Check for the DW_AT_byte_size attribute.  */
  11902.       attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  11903.       if (attr)
  11904.         {
  11905.           length = DW_UNSND (attr);
  11906.         }
  11907.       else
  11908.         {
  11909.           length = 1;
  11910.         }
  11911.     }

  11912.   index_type = objfile_type (objfile)->builtin_int;
  11913.   range_type = create_static_range_type (NULL, index_type, 1, length);
  11914.   char_type = language_string_char_type (cu->language_defn, gdbarch);
  11915.   type = create_string_type (NULL, char_type, range_type);

  11916.   return set_die_type (die, type, cu);
  11917. }

  11918. /* Assuming that DIE corresponds to a function, returns nonzero
  11919.    if the function is prototyped.  */

  11920. static int
  11921. prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
  11922. {
  11923.   struct attribute *attr;

  11924.   attr = dwarf2_attr (die, DW_AT_prototyped, cu);
  11925.   if (attr && (DW_UNSND (attr) != 0))
  11926.     return 1;

  11927.   /* The DWARF standard implies that the DW_AT_prototyped attribute
  11928.      is only meaninful for C, but the concept also extends to other
  11929.      languages that allow unprototyped functions (Eg: Objective C).
  11930.      For all other languages, assume that functions are always
  11931.      prototyped.  */
  11932.   if (cu->language != language_c
  11933.       && cu->language != language_objc
  11934.       && cu->language != language_opencl)
  11935.     return 1;

  11936.   /* RealView does not emit DW_AT_prototyped.  We can not distinguish
  11937.      prototyped and unprototyped functions; default to prototyped,
  11938.      since that is more common in modern code (and RealView warns
  11939.      about unprototyped functions).  */
  11940.   if (producer_is_realview (cu->producer))
  11941.     return 1;

  11942.   return 0;
  11943. }

  11944. /* Handle DIES due to C code like:

  11945.    struct foo
  11946.    {
  11947.    int (*funcp)(int a, long l);
  11948.    int b;
  11949.    };

  11950.    ('funcp' generates a DW_TAG_subroutine_type DIE).  */

  11951. static struct type *
  11952. read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
  11953. {
  11954.   struct objfile *objfile = cu->objfile;
  11955.   struct type *type;                /* Type that this function returns.  */
  11956.   struct type *ftype;                /* Function that returns above type.  */
  11957.   struct attribute *attr;

  11958.   type = die_type (die, cu);

  11959.   /* The die_type call above may have already set the type for this DIE.  */
  11960.   ftype = get_die_type (die, cu);
  11961.   if (ftype)
  11962.     return ftype;

  11963.   ftype = lookup_function_type (type);

  11964.   if (prototyped_function_p (die, cu))
  11965.     TYPE_PROTOTYPED (ftype) = 1;

  11966.   /* Store the calling convention in the type if it's available in
  11967.      the subroutine die.  Otherwise set the calling convention to
  11968.      the default value DW_CC_normal.  */
  11969.   attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
  11970.   if (attr)
  11971.     TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
  11972.   else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
  11973.     TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
  11974.   else
  11975.     TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;

  11976.   /* We need to add the subroutine type to the die immediately so
  11977.      we don't infinitely recurse when dealing with parameters
  11978.      declared as the same subroutine type.  */
  11979.   set_die_type (die, ftype, cu);

  11980.   if (die->child != NULL)
  11981.     {
  11982.       struct type *void_type = objfile_type (objfile)->builtin_void;
  11983.       struct die_info *child_die;
  11984.       int nparams, iparams;

  11985.       /* Count the number of parameters.
  11986.          FIXME: GDB currently ignores vararg functions, but knows about
  11987.          vararg member functions.  */
  11988.       nparams = 0;
  11989.       child_die = die->child;
  11990.       while (child_die && child_die->tag)
  11991.         {
  11992.           if (child_die->tag == DW_TAG_formal_parameter)
  11993.             nparams++;
  11994.           else if (child_die->tag == DW_TAG_unspecified_parameters)
  11995.             TYPE_VARARGS (ftype) = 1;
  11996.           child_die = sibling_die (child_die);
  11997.         }

  11998.       /* Allocate storage for parameters and fill them in.  */
  11999.       TYPE_NFIELDS (ftype) = nparams;
  12000.       TYPE_FIELDS (ftype) = (struct field *)
  12001.         TYPE_ZALLOC (ftype, nparams * sizeof (struct field));

  12002.       /* TYPE_FIELD_TYPE must never be NULL.  Pre-fill the array to ensure it
  12003.          even if we error out during the parameters reading below.  */
  12004.       for (iparams = 0; iparams < nparams; iparams++)
  12005.         TYPE_FIELD_TYPE (ftype, iparams) = void_type;

  12006.       iparams = 0;
  12007.       child_die = die->child;
  12008.       while (child_die && child_die->tag)
  12009.         {
  12010.           if (child_die->tag == DW_TAG_formal_parameter)
  12011.             {
  12012.               struct type *arg_type;

  12013.               /* DWARF version 2 has no clean way to discern C++
  12014.                  static and non-static member functions.  G++ helps
  12015.                  GDB by marking the first parameter for non-static
  12016.                  member functions (which is the this pointer) as
  12017.                  artificial.  We pass this information to
  12018.                  dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.

  12019.                  DWARF version 3 added DW_AT_object_pointer, which GCC
  12020.                  4.5 does not yet generate.  */
  12021.               attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
  12022.               if (attr)
  12023.                 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
  12024.               else
  12025.                 {
  12026.                   TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;

  12027.                   /* GCC/43521: In java, the formal parameter
  12028.                      "this" is sometimes not marked with DW_AT_artificial.  */
  12029.                   if (cu->language == language_java)
  12030.                     {
  12031.                       const char *name = dwarf2_name (child_die, cu);

  12032.                       if (name && !strcmp (name, "this"))
  12033.                         TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
  12034.                     }
  12035.                 }
  12036.               arg_type = die_type (child_die, cu);

  12037.               /* RealView does not mark THIS as const, which the testsuite
  12038.                  expects.  GCC marks THIS as const in method definitions,
  12039.                  but not in the class specifications (GCC PR 43053).  */
  12040.               if (cu->language == language_cplus && !TYPE_CONST (arg_type)
  12041.                   && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
  12042.                 {
  12043.                   int is_this = 0;
  12044.                   struct dwarf2_cu *arg_cu = cu;
  12045.                   const char *name = dwarf2_name (child_die, cu);

  12046.                   attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
  12047.                   if (attr)
  12048.                     {
  12049.                       /* If the compiler emits this, use it.  */
  12050.                       if (follow_die_ref (die, attr, &arg_cu) == child_die)
  12051.                         is_this = 1;
  12052.                     }
  12053.                   else if (name && strcmp (name, "this") == 0)
  12054.                     /* Function definitions will have the argument names.  */
  12055.                     is_this = 1;
  12056.                   else if (name == NULL && iparams == 0)
  12057.                     /* Declarations may not have the names, so like
  12058.                        elsewhere in GDB, assume an artificial first
  12059.                        argument is "this".  */
  12060.                     is_this = 1;

  12061.                   if (is_this)
  12062.                     arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
  12063.                                              arg_type, 0);
  12064.                 }

  12065.               TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
  12066.               iparams++;
  12067.             }
  12068.           child_die = sibling_die (child_die);
  12069.         }
  12070.     }

  12071.   return ftype;
  12072. }

  12073. static struct type *
  12074. read_typedef (struct die_info *die, struct dwarf2_cu *cu)
  12075. {
  12076.   struct objfile *objfile = cu->objfile;
  12077.   const char *name = NULL;
  12078.   struct type *this_type, *target_type;

  12079.   name = dwarf2_full_name (NULL, die, cu);
  12080.   this_type = init_type (TYPE_CODE_TYPEDEF, 0,
  12081.                          TYPE_FLAG_TARGET_STUB, NULL, objfile);
  12082.   TYPE_NAME (this_type) = name;
  12083.   set_die_type (die, this_type, cu);
  12084.   target_type = die_type (die, cu);
  12085.   if (target_type != this_type)
  12086.     TYPE_TARGET_TYPE (this_type) = target_type;
  12087.   else
  12088.     {
  12089.       /* Self-referential typedefs are, it seems, not allowed by the DWARF
  12090.          spec and cause infinite loops in GDB.  */
  12091.       complaint (&symfile_complaints,
  12092.                  _("Self-referential DW_TAG_typedef "
  12093.                    "- DIE at 0x%x [in module %s]"),
  12094.                  die->offset.sect_off, objfile_name (objfile));
  12095.       TYPE_TARGET_TYPE (this_type) = NULL;
  12096.     }
  12097.   return this_type;
  12098. }

  12099. /* Find a representation of a given base type and install
  12100.    it in the TYPE field of the die.  */

  12101. static struct type *
  12102. read_base_type (struct die_info *die, struct dwarf2_cu *cu)
  12103. {
  12104.   struct objfile *objfile = cu->objfile;
  12105.   struct type *type;
  12106.   struct attribute *attr;
  12107.   int encoding = 0, size = 0;
  12108.   const char *name;
  12109.   enum type_code code = TYPE_CODE_INT;
  12110.   int type_flags = 0;
  12111.   struct type *target_type = NULL;

  12112.   attr = dwarf2_attr (die, DW_AT_encoding, cu);
  12113.   if (attr)
  12114.     {
  12115.       encoding = DW_UNSND (attr);
  12116.     }
  12117.   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  12118.   if (attr)
  12119.     {
  12120.       size = DW_UNSND (attr);
  12121.     }
  12122.   name = dwarf2_name (die, cu);
  12123.   if (!name)
  12124.     {
  12125.       complaint (&symfile_complaints,
  12126.                  _("DW_AT_name missing from DW_TAG_base_type"));
  12127.     }

  12128.   switch (encoding)
  12129.     {
  12130.       case DW_ATE_address:
  12131.         /* Turn DW_ATE_address into a void * pointer.  */
  12132.         code = TYPE_CODE_PTR;
  12133.         type_flags |= TYPE_FLAG_UNSIGNED;
  12134.         target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
  12135.         break;
  12136.       case DW_ATE_boolean:
  12137.         code = TYPE_CODE_BOOL;
  12138.         type_flags |= TYPE_FLAG_UNSIGNED;
  12139.         break;
  12140.       case DW_ATE_complex_float:
  12141.         code = TYPE_CODE_COMPLEX;
  12142.         target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
  12143.         break;
  12144.       case DW_ATE_decimal_float:
  12145.         code = TYPE_CODE_DECFLOAT;
  12146.         break;
  12147.       case DW_ATE_float:
  12148.         code = TYPE_CODE_FLT;
  12149.         break;
  12150.       case DW_ATE_signed:
  12151.         break;
  12152.       case DW_ATE_unsigned:
  12153.         type_flags |= TYPE_FLAG_UNSIGNED;
  12154.         if (cu->language == language_fortran
  12155.             && name
  12156.             && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
  12157.           code = TYPE_CODE_CHAR;
  12158.         break;
  12159.       case DW_ATE_signed_char:
  12160.         if (cu->language == language_ada || cu->language == language_m2
  12161.             || cu->language == language_pascal
  12162.             || cu->language == language_fortran)
  12163.           code = TYPE_CODE_CHAR;
  12164.         break;
  12165.       case DW_ATE_unsigned_char:
  12166.         if (cu->language == language_ada || cu->language == language_m2
  12167.             || cu->language == language_pascal
  12168.             || cu->language == language_fortran)
  12169.           code = TYPE_CODE_CHAR;
  12170.         type_flags |= TYPE_FLAG_UNSIGNED;
  12171.         break;
  12172.       case DW_ATE_UTF:
  12173.         /* We just treat this as an integer and then recognize the
  12174.            type by name elsewhere.  */
  12175.         break;

  12176.       default:
  12177.         complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
  12178.                    dwarf_type_encoding_name (encoding));
  12179.         break;
  12180.     }

  12181.   type = init_type (code, size, type_flags, NULL, objfile);
  12182.   TYPE_NAME (type) = name;
  12183.   TYPE_TARGET_TYPE (type) = target_type;

  12184.   if (name && strcmp (name, "char") == 0)
  12185.     TYPE_NOSIGN (type) = 1;

  12186.   return set_die_type (die, type, cu);
  12187. }

  12188. /* Parse dwarf attribute if it's a block, reference or constant and put the
  12189.    resulting value of the attribute into struct bound_prop.
  12190.    Returns 1 if ATTR could be resolved into PROP, 0 otherwise.  */

  12191. static int
  12192. attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
  12193.                       struct dwarf2_cu *cu, struct dynamic_prop *prop)
  12194. {
  12195.   struct dwarf2_property_baton *baton;
  12196.   struct obstack *obstack = &cu->objfile->objfile_obstack;

  12197.   if (attr == NULL || prop == NULL)
  12198.     return 0;

  12199.   if (attr_form_is_block (attr))
  12200.     {
  12201.       baton = obstack_alloc (obstack, sizeof (*baton));
  12202.       baton->referenced_type = NULL;
  12203.       baton->locexpr.per_cu = cu->per_cu;
  12204.       baton->locexpr.size = DW_BLOCK (attr)->size;
  12205.       baton->locexpr.data = DW_BLOCK (attr)->data;
  12206.       prop->data.baton = baton;
  12207.       prop->kind = PROP_LOCEXPR;
  12208.       gdb_assert (prop->data.baton != NULL);
  12209.     }
  12210.   else if (attr_form_is_ref (attr))
  12211.     {
  12212.       struct dwarf2_cu *target_cu = cu;
  12213.       struct die_info *target_die;
  12214.       struct attribute *target_attr;

  12215.       target_die = follow_die_ref (die, attr, &target_cu);
  12216.       target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
  12217.       if (target_attr == NULL)
  12218.         return 0;

  12219.       if (attr_form_is_section_offset (target_attr))
  12220.         {
  12221.           baton = obstack_alloc (obstack, sizeof (*baton));
  12222.           baton->referenced_type = die_type (target_die, target_cu);
  12223.           fill_in_loclist_baton (cu, &baton->loclist, target_attr);
  12224.           prop->data.baton = baton;
  12225.           prop->kind = PROP_LOCLIST;
  12226.           gdb_assert (prop->data.baton != NULL);
  12227.         }
  12228.       else if (attr_form_is_block (target_attr))
  12229.         {
  12230.           baton = obstack_alloc (obstack, sizeof (*baton));
  12231.           baton->referenced_type = die_type (target_die, target_cu);
  12232.           baton->locexpr.per_cu = cu->per_cu;
  12233.           baton->locexpr.size = DW_BLOCK (target_attr)->size;
  12234.           baton->locexpr.data = DW_BLOCK (target_attr)->data;
  12235.           prop->data.baton = baton;
  12236.           prop->kind = PROP_LOCEXPR;
  12237.           gdb_assert (prop->data.baton != NULL);
  12238.         }
  12239.       else
  12240.         {
  12241.           dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
  12242.                                                  "dynamic property");
  12243.           return 0;
  12244.         }
  12245.     }
  12246.   else if (attr_form_is_constant (attr))
  12247.     {
  12248.       prop->data.const_val = dwarf2_get_attr_constant_value (attr, 0);
  12249.       prop->kind = PROP_CONST;
  12250.     }
  12251.   else
  12252.     {
  12253.       dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
  12254.                                              dwarf2_name (die, cu));
  12255.       return 0;
  12256.     }

  12257.   return 1;
  12258. }

  12259. /* Read the given DW_AT_subrange DIE.  */

  12260. static struct type *
  12261. read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
  12262. {
  12263.   struct type *base_type, *orig_base_type;
  12264.   struct type *range_type;
  12265.   struct attribute *attr;
  12266.   struct dynamic_prop low, high;
  12267.   int low_default_is_valid;
  12268.   int high_bound_is_count = 0;
  12269.   const char *name;
  12270.   LONGEST negative_mask;

  12271.   orig_base_type = die_type (die, cu);
  12272.   /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
  12273.      whereas the real type might be.  So, we use ORIG_BASE_TYPE when
  12274.      creating the range type, but we use the result of check_typedef
  12275.      when examining properties of the type.  */
  12276.   base_type = check_typedef (orig_base_type);

  12277.   /* The die_type call above may have already set the type for this DIE.  */
  12278.   range_type = get_die_type (die, cu);
  12279.   if (range_type)
  12280.     return range_type;

  12281.   low.kind = PROP_CONST;
  12282.   high.kind = PROP_CONST;
  12283.   high.data.const_val = 0;

  12284.   /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
  12285.      omitting DW_AT_lower_bound.  */
  12286.   switch (cu->language)
  12287.     {
  12288.     case language_c:
  12289.     case language_cplus:
  12290.       low.data.const_val = 0;
  12291.       low_default_is_valid = 1;
  12292.       break;
  12293.     case language_fortran:
  12294.       low.data.const_val = 1;
  12295.       low_default_is_valid = 1;
  12296.       break;
  12297.     case language_d:
  12298.     case language_java:
  12299.     case language_objc:
  12300.       low.data.const_val = 0;
  12301.       low_default_is_valid = (cu->header.version >= 4);
  12302.       break;
  12303.     case language_ada:
  12304.     case language_m2:
  12305.     case language_pascal:
  12306.       low.data.const_val = 1;
  12307.       low_default_is_valid = (cu->header.version >= 4);
  12308.       break;
  12309.     default:
  12310.       low.data.const_val = 0;
  12311.       low_default_is_valid = 0;
  12312.       break;
  12313.     }

  12314.   attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
  12315.   if (attr)
  12316.     attr_to_dynamic_prop (attr, die, cu, &low);
  12317.   else if (!low_default_is_valid)
  12318.     complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
  12319.                                       "- DIE at 0x%x [in module %s]"),
  12320.                die->offset.sect_off, objfile_name (cu->objfile));

  12321.   attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
  12322.   if (!attr_to_dynamic_prop (attr, die, cu, &high))
  12323.     {
  12324.       attr = dwarf2_attr (die, DW_AT_count, cu);
  12325.       if (attr_to_dynamic_prop (attr, die, cu, &high))
  12326.         {
  12327.           /* If bounds are constant do the final calculation here.  */
  12328.           if (low.kind == PROP_CONST && high.kind == PROP_CONST)
  12329.             high.data.const_val = low.data.const_val + high.data.const_val - 1;
  12330.           else
  12331.             high_bound_is_count = 1;
  12332.         }
  12333.     }

  12334.   /* Dwarf-2 specifications explicitly allows to create subrange types
  12335.      without specifying a base type.
  12336.      In that case, the base type must be set to the type of
  12337.      the lower bound, upper bound or count, in that order, if any of these
  12338.      three attributes references an object that has a type.
  12339.      If no base type is found, the Dwarf-2 specifications say that
  12340.      a signed integer type of size equal to the size of an address should
  12341.      be used.
  12342.      For the following C code: `extern char gdb_int [];'
  12343.      GCC produces an empty range DIE.
  12344.      FIXME: muller/2010-05-28: Possible references to object for low bound,
  12345.      high bound or count are not yet handled by this code.  */
  12346.   if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
  12347.     {
  12348.       struct objfile *objfile = cu->objfile;
  12349.       struct gdbarch *gdbarch = get_objfile_arch (objfile);
  12350.       int addr_size = gdbarch_addr_bit (gdbarch) /8;
  12351.       struct type *int_type = objfile_type (objfile)->builtin_int;

  12352.       /* Test "int", "long int", and "long long int" objfile types,
  12353.          and select the first one having a size above or equal to the
  12354.          architecture address size.  */
  12355.       if (int_type && TYPE_LENGTH (int_type) >= addr_size)
  12356.         base_type = int_type;
  12357.       else
  12358.         {
  12359.           int_type = objfile_type (objfile)->builtin_long;
  12360.           if (int_type && TYPE_LENGTH (int_type) >= addr_size)
  12361.             base_type = int_type;
  12362.           else
  12363.             {
  12364.               int_type = objfile_type (objfile)->builtin_long_long;
  12365.               if (int_type && TYPE_LENGTH (int_type) >= addr_size)
  12366.                 base_type = int_type;
  12367.             }
  12368.         }
  12369.     }

  12370.   /* Normally, the DWARF producers are expected to use a signed
  12371.      constant form (Eg. DW_FORM_sdata) to express negative bounds.
  12372.      But this is unfortunately not always the case, as witnessed
  12373.      with GCC, for instance, where the ambiguous DW_FORM_dataN form
  12374.      is used instead.  To work around that ambiguity, we treat
  12375.      the bounds as signed, and thus sign-extend their values, when
  12376.      the base type is signed.  */
  12377.   negative_mask =
  12378.     (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
  12379.   if (low.kind == PROP_CONST
  12380.       && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
  12381.     low.data.const_val |= negative_mask;
  12382.   if (high.kind == PROP_CONST
  12383.       && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
  12384.     high.data.const_val |= negative_mask;

  12385.   range_type = create_range_type (NULL, orig_base_type, &low, &high);

  12386.   if (high_bound_is_count)
  12387.     TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;

  12388.   /* Ada expects an empty array on no boundary attributes.  */
  12389.   if (attr == NULL && cu->language != language_ada)
  12390.     TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;

  12391.   name = dwarf2_name (die, cu);
  12392.   if (name)
  12393.     TYPE_NAME (range_type) = name;

  12394.   attr = dwarf2_attr (die, DW_AT_byte_size, cu);
  12395.   if (attr)
  12396.     TYPE_LENGTH (range_type) = DW_UNSND (attr);

  12397.   set_die_type (die, range_type, cu);

  12398.   /* set_die_type should be already done.  */
  12399.   set_descriptive_type (range_type, die, cu);

  12400.   return range_type;
  12401. }

  12402. static struct type *
  12403. read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
  12404. {
  12405.   struct type *type;

  12406.   /* For now, we only support the C meaning of an unspecified type: void.  */

  12407.   type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
  12408.   TYPE_NAME (type) = dwarf2_name (die, cu);

  12409.   return set_die_type (die, type, cu);
  12410. }

  12411. /* Read a single die and all its descendents.  Set the die's sibling
  12412.    field to NULL; set other fields in the die correctly, and set all
  12413.    of the descendents' fields correctly.  Set *NEW_INFO_PTR to the
  12414.    location of the info_ptr after reading all of those dies.  PARENT
  12415.    is the parent of the die in question.  */

  12416. static struct die_info *
  12417. read_die_and_children (const struct die_reader_specs *reader,
  12418.                        const gdb_byte *info_ptr,
  12419.                        const gdb_byte **new_info_ptr,
  12420.                        struct die_info *parent)
  12421. {
  12422.   struct die_info *die;
  12423.   const gdb_byte *cur_ptr;
  12424.   int has_children;

  12425.   cur_ptr = read_full_die_1 (reader, &die, info_ptr, &has_children, 0);
  12426.   if (die == NULL)
  12427.     {
  12428.       *new_info_ptr = cur_ptr;
  12429.       return NULL;
  12430.     }
  12431.   store_in_ref_table (die, reader->cu);

  12432.   if (has_children)
  12433.     die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
  12434.   else
  12435.     {
  12436.       die->child = NULL;
  12437.       *new_info_ptr = cur_ptr;
  12438.     }

  12439.   die->sibling = NULL;
  12440.   die->parent = parent;
  12441.   return die;
  12442. }

  12443. /* Read a die, all of its descendents, and all of its siblings; set
  12444.    all of the fields of all of the dies correctly.  Arguments are as
  12445.    in read_die_and_children.  */

  12446. static struct die_info *
  12447. read_die_and_siblings_1 (const struct die_reader_specs *reader,
  12448.                          const gdb_byte *info_ptr,
  12449.                          const gdb_byte **new_info_ptr,
  12450.                          struct die_info *parent)
  12451. {
  12452.   struct die_info *first_die, *last_sibling;
  12453.   const gdb_byte *cur_ptr;

  12454.   cur_ptr = info_ptr;
  12455.   first_die = last_sibling = NULL;

  12456.   while (1)
  12457.     {
  12458.       struct die_info *die
  12459.         = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);

  12460.       if (die == NULL)
  12461.         {
  12462.           *new_info_ptr = cur_ptr;
  12463.           return first_die;
  12464.         }

  12465.       if (!first_die)
  12466.         first_die = die;
  12467.       else
  12468.         last_sibling->sibling = die;

  12469.       last_sibling = die;
  12470.     }
  12471. }

  12472. /* Read a die, all of its descendents, and all of its siblings; set
  12473.    all of the fields of all of the dies correctly.  Arguments are as
  12474.    in read_die_and_children.
  12475.    This the main entry point for reading a DIE and all its children.  */

  12476. static struct die_info *
  12477. read_die_and_siblings (const struct die_reader_specs *reader,
  12478.                        const gdb_byte *info_ptr,
  12479.                        const gdb_byte **new_info_ptr,
  12480.                        struct die_info *parent)
  12481. {
  12482.   struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
  12483.                                                   new_info_ptr, parent);

  12484.   if (dwarf2_die_debug)
  12485.     {
  12486.       fprintf_unfiltered (gdb_stdlog,
  12487.                           "Read die from %s@0x%x of %s:\n",
  12488.                           get_section_name (reader->die_section),
  12489.                           (unsigned) (info_ptr - reader->die_section->buffer),
  12490.                           bfd_get_filename (reader->abfd));
  12491.       dump_die (die, dwarf2_die_debug);
  12492.     }

  12493.   return die;
  12494. }

  12495. /* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
  12496.    attributes.
  12497.    The caller is responsible for filling in the extra attributes
  12498.    and updating (*DIEP)->num_attrs.
  12499.    Set DIEP to point to a newly allocated die with its information,
  12500.    except for its child, sibling, and parent fields.
  12501.    Set HAS_CHILDREN to tell whether the die has children or not.  */

  12502. static const gdb_byte *
  12503. read_full_die_1 (const struct die_reader_specs *reader,
  12504.                  struct die_info **diep, const gdb_byte *info_ptr,
  12505.                  int *has_children, int num_extra_attrs)
  12506. {
  12507.   unsigned int abbrev_number, bytes_read, i;
  12508.   sect_offset offset;
  12509.   struct abbrev_info *abbrev;
  12510.   struct die_info *die;
  12511.   struct dwarf2_cu *cu = reader->cu;
  12512.   bfd *abfd = reader->abfd;

  12513.   offset.sect_off = info_ptr - reader->buffer;
  12514.   abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
  12515.   info_ptr += bytes_read;
  12516.   if (!abbrev_number)
  12517.     {
  12518.       *diep = NULL;
  12519.       *has_children = 0;
  12520.       return info_ptr;
  12521.     }

  12522.   abbrev = abbrev_table_lookup_abbrev (cu->abbrev_table, abbrev_number);
  12523.   if (!abbrev)
  12524.     error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
  12525.            abbrev_number,
  12526.            bfd_get_filename (abfd));

  12527.   die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
  12528.   die->offset = offset;
  12529.   die->tag = abbrev->tag;
  12530.   die->abbrev = abbrev_number;

  12531.   /* Make the result usable.
  12532.      The caller needs to update num_attrs after adding the extra
  12533.      attributes.  */
  12534.   die->num_attrs = abbrev->num_attrs;

  12535.   for (i = 0; i < abbrev->num_attrs; ++i)
  12536.     info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
  12537.                                info_ptr);

  12538.   *diep = die;
  12539.   *has_children = abbrev->has_children;
  12540.   return info_ptr;
  12541. }

  12542. /* Read a die and all its attributes.
  12543.    Set DIEP to point to a newly allocated die with its information,
  12544.    except for its child, sibling, and parent fields.
  12545.    Set HAS_CHILDREN to tell whether the die has children or not.  */

  12546. static const gdb_byte *
  12547. read_full_die (const struct die_reader_specs *reader,
  12548.                struct die_info **diep, const gdb_byte *info_ptr,
  12549.                int *has_children)
  12550. {
  12551.   const gdb_byte *result;

  12552.   result = read_full_die_1 (reader, diep, info_ptr, has_children, 0);

  12553.   if (dwarf2_die_debug)
  12554.     {
  12555.       fprintf_unfiltered (gdb_stdlog,
  12556.                           "Read die from %s@0x%x of %s:\n",
  12557.                           get_section_name (reader->die_section),
  12558.                           (unsigned) (info_ptr - reader->die_section->buffer),
  12559.                           bfd_get_filename (reader->abfd));
  12560.       dump_die (*diep, dwarf2_die_debug);
  12561.     }

  12562.   return result;
  12563. }

  12564. /* Abbreviation tables.

  12565.    In DWARF version 2, the description of the debugging information is
  12566.    stored in a separate .debug_abbrev section.  Before we read any
  12567.    dies from a section we read in all abbreviations and install them
  12568.    in a hash table.  */

  12569. /* Allocate space for a struct abbrev_info object in ABBREV_TABLE.  */

  12570. static struct abbrev_info *
  12571. abbrev_table_alloc_abbrev (struct abbrev_table *abbrev_table)
  12572. {
  12573.   struct abbrev_info *abbrev;

  12574.   abbrev = (struct abbrev_info *)
  12575.     obstack_alloc (&abbrev_table->abbrev_obstack, sizeof (struct abbrev_info));
  12576.   memset (abbrev, 0, sizeof (struct abbrev_info));
  12577.   return abbrev;
  12578. }

  12579. /* Add an abbreviation to the table.  */

  12580. static void
  12581. abbrev_table_add_abbrev (struct abbrev_table *abbrev_table,
  12582.                          unsigned int abbrev_number,
  12583.                          struct abbrev_info *abbrev)
  12584. {
  12585.   unsigned int hash_number;

  12586.   hash_number = abbrev_number % ABBREV_HASH_SIZE;
  12587.   abbrev->next = abbrev_table->abbrevs[hash_number];
  12588.   abbrev_table->abbrevs[hash_number] = abbrev;
  12589. }

  12590. /* Look up an abbrev in the table.
  12591.    Returns NULL if the abbrev is not found.  */

  12592. static struct abbrev_info *
  12593. abbrev_table_lookup_abbrev (const struct abbrev_table *abbrev_table,
  12594.                             unsigned int abbrev_number)
  12595. {
  12596.   unsigned int hash_number;
  12597.   struct abbrev_info *abbrev;

  12598.   hash_number = abbrev_number % ABBREV_HASH_SIZE;
  12599.   abbrev = abbrev_table->abbrevs[hash_number];

  12600.   while (abbrev)
  12601.     {
  12602.       if (abbrev->number == abbrev_number)
  12603.         return abbrev;
  12604.       abbrev = abbrev->next;
  12605.     }
  12606.   return NULL;
  12607. }

  12608. /* Read in an abbrev table.  */

  12609. static struct abbrev_table *
  12610. abbrev_table_read_table (struct dwarf2_section_info *section,
  12611.                          sect_offset offset)
  12612. {
  12613.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  12614.   bfd *abfd = get_section_bfd_owner (section);
  12615.   struct abbrev_table *abbrev_table;
  12616.   const gdb_byte *abbrev_ptr;
  12617.   struct abbrev_info *cur_abbrev;
  12618.   unsigned int abbrev_number, bytes_read, abbrev_name;
  12619.   unsigned int abbrev_form;
  12620.   struct attr_abbrev *cur_attrs;
  12621.   unsigned int allocated_attrs;

  12622.   abbrev_table = XNEW (struct abbrev_table);
  12623.   abbrev_table->offset = offset;
  12624.   obstack_init (&abbrev_table->abbrev_obstack);
  12625.   abbrev_table->abbrevs = obstack_alloc (&abbrev_table->abbrev_obstack,
  12626.                                          (ABBREV_HASH_SIZE
  12627.                                           * sizeof (struct abbrev_info *)));
  12628.   memset (abbrev_table->abbrevs, 0,
  12629.           ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));

  12630.   dwarf2_read_section (objfile, section);
  12631.   abbrev_ptr = section->buffer + offset.sect_off;
  12632.   abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  12633.   abbrev_ptr += bytes_read;

  12634.   allocated_attrs = ATTR_ALLOC_CHUNK;
  12635.   cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));

  12636.   /* Loop until we reach an abbrev number of 0.  */
  12637.   while (abbrev_number)
  12638.     {
  12639.       cur_abbrev = abbrev_table_alloc_abbrev (abbrev_table);

  12640.       /* read in abbrev header */
  12641.       cur_abbrev->number = abbrev_number;
  12642.       cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  12643.       abbrev_ptr += bytes_read;
  12644.       cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
  12645.       abbrev_ptr += 1;

  12646.       /* now read in declarations */
  12647.       abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  12648.       abbrev_ptr += bytes_read;
  12649.       abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  12650.       abbrev_ptr += bytes_read;
  12651.       while (abbrev_name)
  12652.         {
  12653.           if (cur_abbrev->num_attrs == allocated_attrs)
  12654.             {
  12655.               allocated_attrs += ATTR_ALLOC_CHUNK;
  12656.               cur_attrs
  12657.                 = xrealloc (cur_attrs, (allocated_attrs
  12658.                                         * sizeof (struct attr_abbrev)));
  12659.             }

  12660.           cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
  12661.           cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
  12662.           abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  12663.           abbrev_ptr += bytes_read;
  12664.           abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  12665.           abbrev_ptr += bytes_read;
  12666.         }

  12667.       cur_abbrev->attrs = obstack_alloc (&abbrev_table->abbrev_obstack,
  12668.                                          (cur_abbrev->num_attrs
  12669.                                           * sizeof (struct attr_abbrev)));
  12670.       memcpy (cur_abbrev->attrs, cur_attrs,
  12671.               cur_abbrev->num_attrs * sizeof (struct attr_abbrev));

  12672.       abbrev_table_add_abbrev (abbrev_table, abbrev_number, cur_abbrev);

  12673.       /* Get next abbreviation.
  12674.          Under Irix6 the abbreviations for a compilation unit are not
  12675.          always properly terminated with an abbrev number of 0.
  12676.          Exit loop if we encounter an abbreviation which we have
  12677.          already read (which means we are about to read the abbreviations
  12678.          for the next compile unit) or if the end of the abbreviation
  12679.          table is reached.  */
  12680.       if ((unsigned int) (abbrev_ptr - section->buffer) >= section->size)
  12681.         break;
  12682.       abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
  12683.       abbrev_ptr += bytes_read;
  12684.       if (abbrev_table_lookup_abbrev (abbrev_table, abbrev_number) != NULL)
  12685.         break;
  12686.     }

  12687.   xfree (cur_attrs);
  12688.   return abbrev_table;
  12689. }

  12690. /* Free the resources held by ABBREV_TABLE.  */

  12691. static void
  12692. abbrev_table_free (struct abbrev_table *abbrev_table)
  12693. {
  12694.   obstack_free (&abbrev_table->abbrev_obstack, NULL);
  12695.   xfree (abbrev_table);
  12696. }

  12697. /* Same as abbrev_table_free but as a cleanup.
  12698.    We pass in a pointer to the pointer to the table so that we can
  12699.    set the pointer to NULL when we're done.  It also simplifies
  12700.    build_type_psymtabs_1.  */

  12701. static void
  12702. abbrev_table_free_cleanup (void *table_ptr)
  12703. {
  12704.   struct abbrev_table **abbrev_table_ptr = table_ptr;

  12705.   if (*abbrev_table_ptr != NULL)
  12706.     abbrev_table_free (*abbrev_table_ptr);
  12707.   *abbrev_table_ptr = NULL;
  12708. }

  12709. /* Read the abbrev table for CU from ABBREV_SECTION.  */

  12710. static void
  12711. dwarf2_read_abbrevs (struct dwarf2_cu *cu,
  12712.                      struct dwarf2_section_info *abbrev_section)
  12713. {
  12714.   cu->abbrev_table =
  12715.     abbrev_table_read_table (abbrev_section, cu->header.abbrev_offset);
  12716. }

  12717. /* Release the memory used by the abbrev table for a compilation unit.  */

  12718. static void
  12719. dwarf2_free_abbrev_table (void *ptr_to_cu)
  12720. {
  12721.   struct dwarf2_cu *cu = ptr_to_cu;

  12722.   if (cu->abbrev_table != NULL)
  12723.     abbrev_table_free (cu->abbrev_table);
  12724.   /* Set this to NULL so that we SEGV if we try to read it later,
  12725.      and also because free_comp_unit verifies this is NULL.  */
  12726.   cu->abbrev_table = NULL;
  12727. }

  12728. /* Returns nonzero if TAG represents a type that we might generate a partial
  12729.    symbol for.  */

  12730. static int
  12731. is_type_tag_for_partial (int tag)
  12732. {
  12733.   switch (tag)
  12734.     {
  12735. #if 0
  12736.     /* Some types that would be reasonable to generate partial symbols for,
  12737.        that we don't at present.  */
  12738.     case DW_TAG_array_type:
  12739.     case DW_TAG_file_type:
  12740.     case DW_TAG_ptr_to_member_type:
  12741.     case DW_TAG_set_type:
  12742.     case DW_TAG_string_type:
  12743.     case DW_TAG_subroutine_type:
  12744. #endif
  12745.     case DW_TAG_base_type:
  12746.     case DW_TAG_class_type:
  12747.     case DW_TAG_interface_type:
  12748.     case DW_TAG_enumeration_type:
  12749.     case DW_TAG_structure_type:
  12750.     case DW_TAG_subrange_type:
  12751.     case DW_TAG_typedef:
  12752.     case DW_TAG_union_type:
  12753.       return 1;
  12754.     default:
  12755.       return 0;
  12756.     }
  12757. }

  12758. /* Load all DIEs that are interesting for partial symbols into memory.  */

  12759. static struct partial_die_info *
  12760. load_partial_dies (const struct die_reader_specs *reader,
  12761.                    const gdb_byte *info_ptr, int building_psymtab)
  12762. {
  12763.   struct dwarf2_cu *cu = reader->cu;
  12764.   struct objfile *objfile = cu->objfile;
  12765.   struct partial_die_info *part_die;
  12766.   struct partial_die_info *parent_die, *last_die, *first_die = NULL;
  12767.   struct abbrev_info *abbrev;
  12768.   unsigned int bytes_read;
  12769.   unsigned int load_all = 0;
  12770.   int nesting_level = 1;

  12771.   parent_die = NULL;
  12772.   last_die = NULL;

  12773.   gdb_assert (cu->per_cu != NULL);
  12774.   if (cu->per_cu->load_all_dies)
  12775.     load_all = 1;

  12776.   cu->partial_dies
  12777.     = htab_create_alloc_ex (cu->header.length / 12,
  12778.                             partial_die_hash,
  12779.                             partial_die_eq,
  12780.                             NULL,
  12781.                             &cu->comp_unit_obstack,
  12782.                             hashtab_obstack_allocate,
  12783.                             dummy_obstack_deallocate);

  12784.   part_die = obstack_alloc (&cu->comp_unit_obstack,
  12785.                             sizeof (struct partial_die_info));

  12786.   while (1)
  12787.     {
  12788.       abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);

  12789.       /* A NULL abbrev means the end of a series of children.  */
  12790.       if (abbrev == NULL)
  12791.         {
  12792.           if (--nesting_level == 0)
  12793.             {
  12794.               /* PART_DIE was probably the last thing allocated on the
  12795.                  comp_unit_obstack, so we could call obstack_free
  12796.                  here.  We don't do that because the waste is small,
  12797.                  and will be cleaned up when we're done with this
  12798.                  compilation unit.  This way, we're also more robust
  12799.                  against other users of the comp_unit_obstack.  */
  12800.               return first_die;
  12801.             }
  12802.           info_ptr += bytes_read;
  12803.           last_die = parent_die;
  12804.           parent_die = parent_die->die_parent;
  12805.           continue;
  12806.         }

  12807.       /* Check for template arguments.  We never save these; if
  12808.          they're seen, we just mark the parent, and go on our way.  */
  12809.       if (parent_die != NULL
  12810.           && cu->language == language_cplus
  12811.           && (abbrev->tag == DW_TAG_template_type_param
  12812.               || abbrev->tag == DW_TAG_template_value_param))
  12813.         {
  12814.           parent_die->has_template_arguments = 1;

  12815.           if (!load_all)
  12816.             {
  12817.               /* We don't need a partial DIE for the template argument.  */
  12818.               info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
  12819.               continue;
  12820.             }
  12821.         }

  12822.       /* We only recurse into c++ subprograms looking for template arguments.
  12823.          Skip their other children.  */
  12824.       if (!load_all
  12825.           && cu->language == language_cplus
  12826.           && parent_die != NULL
  12827.           && parent_die->tag == DW_TAG_subprogram)
  12828.         {
  12829.           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
  12830.           continue;
  12831.         }

  12832.       /* Check whether this DIE is interesting enough to save.  Normally
  12833.          we would not be interested in members here, but there may be
  12834.          later variables referencing them via DW_AT_specification (for
  12835.          static members).  */
  12836.       if (!load_all
  12837.           && !is_type_tag_for_partial (abbrev->tag)
  12838.           && abbrev->tag != DW_TAG_constant
  12839.           && abbrev->tag != DW_TAG_enumerator
  12840.           && abbrev->tag != DW_TAG_subprogram
  12841.           && abbrev->tag != DW_TAG_lexical_block
  12842.           && abbrev->tag != DW_TAG_variable
  12843.           && abbrev->tag != DW_TAG_namespace
  12844.           && abbrev->tag != DW_TAG_module
  12845.           && abbrev->tag != DW_TAG_member
  12846.           && abbrev->tag != DW_TAG_imported_unit
  12847.           && abbrev->tag != DW_TAG_imported_declaration)
  12848.         {
  12849.           /* Otherwise we skip to the next sibling, if any.  */
  12850.           info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
  12851.           continue;
  12852.         }

  12853.       info_ptr = read_partial_die (reader, part_die, abbrev, bytes_read,
  12854.                                    info_ptr);

  12855.       /* This two-pass algorithm for processing partial symbols has a
  12856.          high cost in cache pressure.  Thus, handle some simple cases
  12857.          here which cover the majority of C partial symbols.  DIEs
  12858.          which neither have specification tags in them, nor could have
  12859.          specification tags elsewhere pointing at them, can simply be
  12860.          processed and discarded.

  12861.          This segment is also optional; scan_partial_symbols and
  12862.          add_partial_symbol will handle these DIEs if we chain
  12863.          them in normally.  When compilers which do not emit large
  12864.          quantities of duplicate debug information are more common,
  12865.          this code can probably be removed.  */

  12866.       /* Any complete simple types at the top level (pretty much all
  12867.          of them, for a language without namespaces), can be processed
  12868.          directly.  */
  12869.       if (parent_die == NULL
  12870.           && part_die->has_specification == 0
  12871.           && part_die->is_declaration == 0
  12872.           && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
  12873.               || part_die->tag == DW_TAG_base_type
  12874.               || part_die->tag == DW_TAG_subrange_type))
  12875.         {
  12876.           if (building_psymtab && part_die->name != NULL)
  12877.             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
  12878.                                  VAR_DOMAIN, LOC_TYPEDEF,
  12879.                                  &objfile->static_psymbols,
  12880.                                  0, (CORE_ADDR) 0, cu->language, objfile);
  12881.           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
  12882.           continue;
  12883.         }

  12884.       /* The exception for DW_TAG_typedef with has_children above is
  12885.          a workaround of GCC PR debug/47510.  In the case of this complaint
  12886.          type_name_no_tag_or_error will error on such types later.

  12887.          GDB skipped children of DW_TAG_typedef by the shortcut above and then
  12888.          it could not find the child DIEs referenced later, this is checked
  12889.          above.  In correct DWARF DW_TAG_typedef should have no children.  */

  12890.       if (part_die->tag == DW_TAG_typedef && part_die->has_children)
  12891.         complaint (&symfile_complaints,
  12892.                    _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
  12893.                      "- DIE at 0x%x [in module %s]"),
  12894.                    part_die->offset.sect_off, objfile_name (objfile));

  12895.       /* If we're at the second level, and we're an enumerator, and
  12896.          our parent has no specification (meaning possibly lives in a
  12897.          namespace elsewhere), then we can add the partial symbol now
  12898.          instead of queueing it.  */
  12899.       if (part_die->tag == DW_TAG_enumerator
  12900.           && parent_die != NULL
  12901.           && parent_die->die_parent == NULL
  12902.           && parent_die->tag == DW_TAG_enumeration_type
  12903.           && parent_die->has_specification == 0)
  12904.         {
  12905.           if (part_die->name == NULL)
  12906.             complaint (&symfile_complaints,
  12907.                        _("malformed enumerator DIE ignored"));
  12908.           else if (building_psymtab)
  12909.             add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
  12910.                                  VAR_DOMAIN, LOC_CONST,
  12911.                                  (cu->language == language_cplus
  12912.                                   || cu->language == language_java)
  12913.                                  ? &objfile->global_psymbols
  12914.                                  : &objfile->static_psymbols,
  12915.                                  0, (CORE_ADDR) 0, cu->language, objfile);

  12916.           info_ptr = locate_pdi_sibling (reader, part_die, info_ptr);
  12917.           continue;
  12918.         }

  12919.       /* We'll save this DIE so link it in.  */
  12920.       part_die->die_parent = parent_die;
  12921.       part_die->die_sibling = NULL;
  12922.       part_die->die_child = NULL;

  12923.       if (last_die && last_die == parent_die)
  12924.         last_die->die_child = part_die;
  12925.       else if (last_die)
  12926.         last_die->die_sibling = part_die;

  12927.       last_die = part_die;

  12928.       if (first_die == NULL)
  12929.         first_die = part_die;

  12930.       /* Maybe add the DIE to the hash table.  Not all DIEs that we
  12931.          find interesting need to be in the hash table, because we
  12932.          also have the parent/sibling/child chains; only those that we
  12933.          might refer to by offset later during partial symbol reading.

  12934.          For now this means things that might have be the target of a
  12935.          DW_AT_specification, DW_AT_abstract_origin, or
  12936.          DW_AT_extension.  DW_AT_extension will refer only to
  12937.          namespaces; DW_AT_abstract_origin refers to functions (and
  12938.          many things under the function DIE, but we do not recurse
  12939.          into function DIEs during partial symbol reading) and
  12940.          possibly variables as well; DW_AT_specification refers to
  12941.          declarations.  Declarations ought to have the DW_AT_declaration
  12942.          flag.  It happens that GCC forgets to put it in sometimes, but
  12943.          only for functions, not for types.

  12944.          Adding more things than necessary to the hash table is harmless
  12945.          except for the performance cost.  Adding too few will result in
  12946.          wasted time in find_partial_die, when we reread the compilation
  12947.          unit with load_all_dies set.  */

  12948.       if (load_all
  12949.           || abbrev->tag == DW_TAG_constant
  12950.           || abbrev->tag == DW_TAG_subprogram
  12951.           || abbrev->tag == DW_TAG_variable
  12952.           || abbrev->tag == DW_TAG_namespace
  12953.           || part_die->is_declaration)
  12954.         {
  12955.           void **slot;

  12956.           slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
  12957.                                            part_die->offset.sect_off, INSERT);
  12958.           *slot = part_die;
  12959.         }

  12960.       part_die = obstack_alloc (&cu->comp_unit_obstack,
  12961.                                 sizeof (struct partial_die_info));

  12962.       /* For some DIEs we want to follow their children (if any).  For C
  12963.          we have no reason to follow the children of structures; for other
  12964.          languages we have to, so that we can get at method physnames
  12965.          to infer fully qualified class names, for DW_AT_specification,
  12966.          and for C++ template arguments.  For C++, we also look one level
  12967.          inside functions to find template arguments (if the name of the
  12968.          function does not already contain the template arguments).

  12969.          For Ada, we need to scan the children of subprograms and lexical
  12970.          blocks as well because Ada allows the definition of nested
  12971.          entities that could be interesting for the debugger, such as
  12972.          nested subprograms for instance.  */
  12973.       if (last_die->has_children
  12974.           && (load_all
  12975.               || last_die->tag == DW_TAG_namespace
  12976.               || last_die->tag == DW_TAG_module
  12977.               || last_die->tag == DW_TAG_enumeration_type
  12978.               || (cu->language == language_cplus
  12979.                   && last_die->tag == DW_TAG_subprogram
  12980.                   && (last_die->name == NULL
  12981.                       || strchr (last_die->name, '<') == NULL))
  12982.               || (cu->language != language_c
  12983.                   && (last_die->tag == DW_TAG_class_type
  12984.                       || last_die->tag == DW_TAG_interface_type
  12985.                       || last_die->tag == DW_TAG_structure_type
  12986.                       || last_die->tag == DW_TAG_union_type))
  12987.               || (cu->language == language_ada
  12988.                   && (last_die->tag == DW_TAG_subprogram
  12989.                       || last_die->tag == DW_TAG_lexical_block))))
  12990.         {
  12991.           nesting_level++;
  12992.           parent_die = last_die;
  12993.           continue;
  12994.         }

  12995.       /* Otherwise we skip to the next sibling, if any.  */
  12996.       info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);

  12997.       /* Back to the top, do it again.  */
  12998.     }
  12999. }

  13000. /* Read a minimal amount of information into the minimal die structure.  */

  13001. static const gdb_byte *
  13002. read_partial_die (const struct die_reader_specs *reader,
  13003.                   struct partial_die_info *part_die,
  13004.                   struct abbrev_info *abbrev, unsigned int abbrev_len,
  13005.                   const gdb_byte *info_ptr)
  13006. {
  13007.   struct dwarf2_cu *cu = reader->cu;
  13008.   struct objfile *objfile = cu->objfile;
  13009.   const gdb_byte *buffer = reader->buffer;
  13010.   unsigned int i;
  13011.   struct attribute attr;
  13012.   int has_low_pc_attr = 0;
  13013.   int has_high_pc_attr = 0;
  13014.   int high_pc_relative = 0;

  13015.   memset (part_die, 0, sizeof (struct partial_die_info));

  13016.   part_die->offset.sect_off = info_ptr - buffer;

  13017.   info_ptr += abbrev_len;

  13018.   if (abbrev == NULL)
  13019.     return info_ptr;

  13020.   part_die->tag = abbrev->tag;
  13021.   part_die->has_children = abbrev->has_children;

  13022.   for (i = 0; i < abbrev->num_attrs; ++i)
  13023.     {
  13024.       info_ptr = read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);

  13025.       /* Store the data if it is of an attribute we want to keep in a
  13026.          partial symbol table.  */
  13027.       switch (attr.name)
  13028.         {
  13029.         case DW_AT_name:
  13030.           switch (part_die->tag)
  13031.             {
  13032.             case DW_TAG_compile_unit:
  13033.             case DW_TAG_partial_unit:
  13034.             case DW_TAG_type_unit:
  13035.               /* Compilation units have a DW_AT_name that is a filename, not
  13036.                  a source language identifier.  */
  13037.             case DW_TAG_enumeration_type:
  13038.             case DW_TAG_enumerator:
  13039.               /* These tags always have simple identifiers already; no need
  13040.                  to canonicalize them.  */
  13041.               part_die->name = DW_STRING (&attr);
  13042.               break;
  13043.             default:
  13044.               part_die->name
  13045.                 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
  13046.                                             &objfile->per_bfd->storage_obstack);
  13047.               break;
  13048.             }
  13049.           break;
  13050.         case DW_AT_linkage_name:
  13051.         case DW_AT_MIPS_linkage_name:
  13052.           /* Note that both forms of linkage name might appear.  We
  13053.              assume they will be the same, and we only store the last
  13054.              one we see.  */
  13055.           if (cu->language == language_ada)
  13056.             part_die->name = DW_STRING (&attr);
  13057.           part_die->linkage_name = DW_STRING (&attr);
  13058.           break;
  13059.         case DW_AT_low_pc:
  13060.           has_low_pc_attr = 1;
  13061.           part_die->lowpc = attr_value_as_address (&attr);
  13062.           break;
  13063.         case DW_AT_high_pc:
  13064.           has_high_pc_attr = 1;
  13065.           part_die->highpc = attr_value_as_address (&attr);
  13066.           if (cu->header.version >= 4 && attr_form_is_constant (&attr))
  13067.                 high_pc_relative = 1;
  13068.           break;
  13069.         case DW_AT_location:
  13070.           /* Support the .debug_loc offsets.  */
  13071.           if (attr_form_is_block (&attr))
  13072.             {
  13073.                part_die->d.locdesc = DW_BLOCK (&attr);
  13074.             }
  13075.           else if (attr_form_is_section_offset (&attr))
  13076.             {
  13077.               dwarf2_complex_location_expr_complaint ();
  13078.             }
  13079.           else
  13080.             {
  13081.               dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
  13082.                                                      "partial symbol information");
  13083.             }
  13084.           break;
  13085.         case DW_AT_external:
  13086.           part_die->is_external = DW_UNSND (&attr);
  13087.           break;
  13088.         case DW_AT_declaration:
  13089.           part_die->is_declaration = DW_UNSND (&attr);
  13090.           break;
  13091.         case DW_AT_type:
  13092.           part_die->has_type = 1;
  13093.           break;
  13094.         case DW_AT_abstract_origin:
  13095.         case DW_AT_specification:
  13096.         case DW_AT_extension:
  13097.           part_die->has_specification = 1;
  13098.           part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
  13099.           part_die->spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
  13100.                                    || cu->per_cu->is_dwz);
  13101.           break;
  13102.         case DW_AT_sibling:
  13103.           /* Ignore absolute siblings, they might point outside of
  13104.              the current compile unit.  */
  13105.           if (attr.form == DW_FORM_ref_addr)
  13106.             complaint (&symfile_complaints,
  13107.                        _("ignoring absolute DW_AT_sibling"));
  13108.           else
  13109.             {
  13110.               unsigned int off = dwarf2_get_ref_die_offset (&attr).sect_off;
  13111.               const gdb_byte *sibling_ptr = buffer + off;

  13112.               if (sibling_ptr < info_ptr)
  13113.                 complaint (&symfile_complaints,
  13114.                            _("DW_AT_sibling points backwards"));
  13115.               else if (sibling_ptr > reader->buffer_end)
  13116.                 dwarf2_section_buffer_overflow_complaint (reader->die_section);
  13117.               else
  13118.                 part_die->sibling = sibling_ptr;
  13119.             }
  13120.           break;
  13121.         case DW_AT_byte_size:
  13122.           part_die->has_byte_size = 1;
  13123.           break;
  13124.         case DW_AT_calling_convention:
  13125.           /* DWARF doesn't provide a way to identify a program's source-level
  13126.              entry point.  DW_AT_calling_convention attributes are only meant
  13127.              to describe functions' calling conventions.

  13128.              However, because it's a necessary piece of information in
  13129.              Fortran, and because DW_CC_program is the only piece of debugging
  13130.              information whose definition refers to a 'main program' at all,
  13131.              several compilers have begun marking Fortran main programs with
  13132.              DW_CC_program --- even when those functions use the standard
  13133.              calling conventions.

  13134.              So until DWARF specifies a way to provide this information and
  13135.              compilers pick up the new representation, we'll support this
  13136.              practice.  */
  13137.           if (DW_UNSND (&attr) == DW_CC_program
  13138.               && cu->language == language_fortran)
  13139.             set_objfile_main_name (objfile, part_die->name, language_fortran);
  13140.           break;
  13141.         case DW_AT_inline:
  13142.           if (DW_UNSND (&attr) == DW_INL_inlined
  13143.               || DW_UNSND (&attr) == DW_INL_declared_inlined)
  13144.             part_die->may_be_inlined = 1;
  13145.           break;

  13146.         case DW_AT_import:
  13147.           if (part_die->tag == DW_TAG_imported_unit)
  13148.             {
  13149.               part_die->d.offset = dwarf2_get_ref_die_offset (&attr);
  13150.               part_die->is_dwz = (attr.form == DW_FORM_GNU_ref_alt
  13151.                                   || cu->per_cu->is_dwz);
  13152.             }
  13153.           break;

  13154.         default:
  13155.           break;
  13156.         }
  13157.     }

  13158.   if (high_pc_relative)
  13159.     part_die->highpc += part_die->lowpc;

  13160.   if (has_low_pc_attr && has_high_pc_attr)
  13161.     {
  13162.       /* When using the GNU linker, .gnu.linkonce. sections are used to
  13163.          eliminate duplicate copies of functions and vtables and such.
  13164.          The linker will arbitrarily choose one and discard the others.
  13165.          The AT_*_pc values for such functions refer to local labels in
  13166.          these sections.  If the section from that file was discarded, the
  13167.          labels are not in the output, so the relocs get a value of 0.
  13168.          If this is a discarded function, mark the pc bounds as invalid,
  13169.          so that GDB will ignore it.  */
  13170.       if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
  13171.         {
  13172.           struct gdbarch *gdbarch = get_objfile_arch (objfile);

  13173.           complaint (&symfile_complaints,
  13174.                      _("DW_AT_low_pc %s is zero "
  13175.                        "for DIE at 0x%x [in module %s]"),
  13176.                      paddress (gdbarch, part_die->lowpc),
  13177.                      part_die->offset.sect_off, objfile_name (objfile));
  13178.         }
  13179.       /* dwarf2_get_pc_bounds has also the strict low < high requirement.  */
  13180.       else if (part_die->lowpc >= part_die->highpc)
  13181.         {
  13182.           struct gdbarch *gdbarch = get_objfile_arch (objfile);

  13183.           complaint (&symfile_complaints,
  13184.                      _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
  13185.                        "for DIE at 0x%x [in module %s]"),
  13186.                      paddress (gdbarch, part_die->lowpc),
  13187.                      paddress (gdbarch, part_die->highpc),
  13188.                      part_die->offset.sect_off, objfile_name (objfile));
  13189.         }
  13190.       else
  13191.         part_die->has_pc_info = 1;
  13192.     }

  13193.   return info_ptr;
  13194. }

  13195. /* Find a cached partial DIE at OFFSET in CU.  */

  13196. static struct partial_die_info *
  13197. find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
  13198. {
  13199.   struct partial_die_info *lookup_die = NULL;
  13200.   struct partial_die_info part_die;

  13201.   part_die.offset = offset;
  13202.   lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
  13203.                                     offset.sect_off);

  13204.   return lookup_die;
  13205. }

  13206. /* Find a partial DIE at OFFSET, which may or may not be in CU,
  13207.    except in the case of .debug_types DIEs which do not reference
  13208.    outside their CU (they do however referencing other types via
  13209.    DW_FORM_ref_sig8).  */

  13210. static struct partial_die_info *
  13211. find_partial_die (sect_offset offset, int offset_in_dwz, struct dwarf2_cu *cu)
  13212. {
  13213.   struct objfile *objfile = cu->objfile;
  13214.   struct dwarf2_per_cu_data *per_cu = NULL;
  13215.   struct partial_die_info *pd = NULL;

  13216.   if (offset_in_dwz == cu->per_cu->is_dwz
  13217.       && offset_in_cu_p (&cu->header, offset))
  13218.     {
  13219.       pd = find_partial_die_in_comp_unit (offset, cu);
  13220.       if (pd != NULL)
  13221.         return pd;
  13222.       /* We missed recording what we needed.
  13223.          Load all dies and try again.  */
  13224.       per_cu = cu->per_cu;
  13225.     }
  13226.   else
  13227.     {
  13228.       /* TUs don't reference other CUs/TUs (except via type signatures).  */
  13229.       if (cu->per_cu->is_debug_types)
  13230.         {
  13231.           error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
  13232.                    " external reference to offset 0x%lx [in module %s].\n"),
  13233.                  (long) cu->header.offset.sect_off, (long) offset.sect_off,
  13234.                  bfd_get_filename (objfile->obfd));
  13235.         }
  13236.       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
  13237.                                                  objfile);

  13238.       if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
  13239.         load_partial_comp_unit (per_cu);

  13240.       per_cu->cu->last_used = 0;
  13241.       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
  13242.     }

  13243.   /* If we didn't find it, and not all dies have been loaded,
  13244.      load them all and try again.  */

  13245.   if (pd == NULL && per_cu->load_all_dies == 0)
  13246.     {
  13247.       per_cu->load_all_dies = 1;

  13248.       /* This is nasty.  When we reread the DIEs, somewhere up the call chain
  13249.          THIS_CU->cu may already be in use.  So we can't just free it and
  13250.          replace its DIEs with the ones we read in.  Instead, we leave those
  13251.          DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
  13252.          and clobber THIS_CU->cu->partial_dies with the hash table for the new
  13253.          set.  */
  13254.       load_partial_comp_unit (per_cu);

  13255.       pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
  13256.     }

  13257.   if (pd == NULL)
  13258.     internal_error (__FILE__, __LINE__,
  13259.                     _("could not find partial DIE 0x%x "
  13260.                       "in cache [from module %s]\n"),
  13261.                     offset.sect_off, bfd_get_filename (objfile->obfd));
  13262.   return pd;
  13263. }

  13264. /* See if we can figure out if the class lives in a namespace.  We do
  13265.    this by looking for a member function; its demangled name will
  13266.    contain namespace info, if there is any.  */

  13267. static void
  13268. guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
  13269.                                   struct dwarf2_cu *cu)
  13270. {
  13271.   /* NOTE: carlton/2003-10-07: Getting the info this way changes
  13272.      what template types look like, because the demangler
  13273.      frequently doesn't give the same name as the debug info.  We
  13274.      could fix this by only using the demangled name to get the
  13275.      prefix (but see comment in read_structure_type).  */

  13276.   struct partial_die_info *real_pdi;
  13277.   struct partial_die_info *child_pdi;

  13278.   /* If this DIE (this DIE's specification, if any) has a parent, then
  13279.      we should not do this.  We'll prepend the parent's fully qualified
  13280.      name when we create the partial symbol.  */

  13281.   real_pdi = struct_pdi;
  13282.   while (real_pdi->has_specification)
  13283.     real_pdi = find_partial_die (real_pdi->spec_offset,
  13284.                                  real_pdi->spec_is_dwz, cu);

  13285.   if (real_pdi->die_parent != NULL)
  13286.     return;

  13287.   for (child_pdi = struct_pdi->die_child;
  13288.        child_pdi != NULL;
  13289.        child_pdi = child_pdi->die_sibling)
  13290.     {
  13291.       if (child_pdi->tag == DW_TAG_subprogram
  13292.           && child_pdi->linkage_name != NULL)
  13293.         {
  13294.           char *actual_class_name
  13295.             = language_class_name_from_physname (cu->language_defn,
  13296.                                                  child_pdi->linkage_name);
  13297.           if (actual_class_name != NULL)
  13298.             {
  13299.               struct_pdi->name
  13300.                 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
  13301.                                  actual_class_name,
  13302.                                  strlen (actual_class_name));
  13303.               xfree (actual_class_name);
  13304.             }
  13305.           break;
  13306.         }
  13307.     }
  13308. }

  13309. /* Adjust PART_DIE before generating a symbol for it.  This function
  13310.    may set the is_external flag or change the DIE's name.  */

  13311. static void
  13312. fixup_partial_die (struct partial_die_info *part_die,
  13313.                    struct dwarf2_cu *cu)
  13314. {
  13315.   /* Once we've fixed up a die, there's no point in doing so again.
  13316.      This also avoids a memory leak if we were to call
  13317.      guess_partial_die_structure_name multiple times.  */
  13318.   if (part_die->fixup_called)
  13319.     return;

  13320.   /* If we found a reference attribute and the DIE has no name, try
  13321.      to find a name in the referred to DIE.  */

  13322.   if (part_die->name == NULL && part_die->has_specification)
  13323.     {
  13324.       struct partial_die_info *spec_die;

  13325.       spec_die = find_partial_die (part_die->spec_offset,
  13326.                                    part_die->spec_is_dwz, cu);

  13327.       fixup_partial_die (spec_die, cu);

  13328.       if (spec_die->name)
  13329.         {
  13330.           part_die->name = spec_die->name;

  13331.           /* Copy DW_AT_external attribute if it is set.  */
  13332.           if (spec_die->is_external)
  13333.             part_die->is_external = spec_die->is_external;
  13334.         }
  13335.     }

  13336.   /* Set default names for some unnamed DIEs.  */

  13337.   if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
  13338.     part_die->name = CP_ANONYMOUS_NAMESPACE_STR;

  13339.   /* If there is no parent die to provide a namespace, and there are
  13340.      children, see if we can determine the namespace from their linkage
  13341.      name.  */
  13342.   if (cu->language == language_cplus
  13343.       && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
  13344.       && part_die->die_parent == NULL
  13345.       && part_die->has_children
  13346.       && (part_die->tag == DW_TAG_class_type
  13347.           || part_die->tag == DW_TAG_structure_type
  13348.           || part_die->tag == DW_TAG_union_type))
  13349.     guess_partial_die_structure_name (part_die, cu);

  13350.   /* GCC might emit a nameless struct or union that has a linkage
  13351.      name.  See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
  13352.   if (part_die->name == NULL
  13353.       && (part_die->tag == DW_TAG_class_type
  13354.           || part_die->tag == DW_TAG_interface_type
  13355.           || part_die->tag == DW_TAG_structure_type
  13356.           || part_die->tag == DW_TAG_union_type)
  13357.       && part_die->linkage_name != NULL)
  13358.     {
  13359.       char *demangled;

  13360.       demangled = gdb_demangle (part_die->linkage_name, DMGL_TYPES);
  13361.       if (demangled)
  13362.         {
  13363.           const char *base;

  13364.           /* Strip any leading namespaces/classes, keep only the base name.
  13365.              DW_AT_name for named DIEs does not contain the prefixes.  */
  13366.           base = strrchr (demangled, ':');
  13367.           if (base && base > demangled && base[-1] == ':')
  13368.             base++;
  13369.           else
  13370.             base = demangled;

  13371.           part_die->name
  13372.             = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
  13373.                              base, strlen (base));
  13374.           xfree (demangled);
  13375.         }
  13376.     }

  13377.   part_die->fixup_called = 1;
  13378. }

  13379. /* Read an attribute value described by an attribute form.  */

  13380. static const gdb_byte *
  13381. read_attribute_value (const struct die_reader_specs *reader,
  13382.                       struct attribute *attr, unsigned form,
  13383.                       const gdb_byte *info_ptr)
  13384. {
  13385.   struct dwarf2_cu *cu = reader->cu;
  13386.   struct objfile *objfile = cu->objfile;
  13387.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  13388.   bfd *abfd = reader->abfd;
  13389.   struct comp_unit_head *cu_header = &cu->header;
  13390.   unsigned int bytes_read;
  13391.   struct dwarf_block *blk;

  13392.   attr->form = form;
  13393.   switch (form)
  13394.     {
  13395.     case DW_FORM_ref_addr:
  13396.       if (cu->header.version == 2)
  13397.         DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
  13398.       else
  13399.         DW_UNSND (attr) = read_offset (abfd, info_ptr,
  13400.                                        &cu->header, &bytes_read);
  13401.       info_ptr += bytes_read;
  13402.       break;
  13403.     case DW_FORM_GNU_ref_alt:
  13404.       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
  13405.       info_ptr += bytes_read;
  13406.       break;
  13407.     case DW_FORM_addr:
  13408.       DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
  13409.       DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
  13410.       info_ptr += bytes_read;
  13411.       break;
  13412.     case DW_FORM_block2:
  13413.       blk = dwarf_alloc_block (cu);
  13414.       blk->size = read_2_bytes (abfd, info_ptr);
  13415.       info_ptr += 2;
  13416.       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
  13417.       info_ptr += blk->size;
  13418.       DW_BLOCK (attr) = blk;
  13419.       break;
  13420.     case DW_FORM_block4:
  13421.       blk = dwarf_alloc_block (cu);
  13422.       blk->size = read_4_bytes (abfd, info_ptr);
  13423.       info_ptr += 4;
  13424.       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
  13425.       info_ptr += blk->size;
  13426.       DW_BLOCK (attr) = blk;
  13427.       break;
  13428.     case DW_FORM_data2:
  13429.       DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
  13430.       info_ptr += 2;
  13431.       break;
  13432.     case DW_FORM_data4:
  13433.       DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
  13434.       info_ptr += 4;
  13435.       break;
  13436.     case DW_FORM_data8:
  13437.       DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
  13438.       info_ptr += 8;
  13439.       break;
  13440.     case DW_FORM_sec_offset:
  13441.       DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
  13442.       info_ptr += bytes_read;
  13443.       break;
  13444.     case DW_FORM_string:
  13445.       DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
  13446.       DW_STRING_IS_CANONICAL (attr) = 0;
  13447.       info_ptr += bytes_read;
  13448.       break;
  13449.     case DW_FORM_strp:
  13450.       if (!cu->per_cu->is_dwz)
  13451.         {
  13452.           DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
  13453.                                                    &bytes_read);
  13454.           DW_STRING_IS_CANONICAL (attr) = 0;
  13455.           info_ptr += bytes_read;
  13456.           break;
  13457.         }
  13458.       /* FALLTHROUGH */
  13459.     case DW_FORM_GNU_strp_alt:
  13460.       {
  13461.         struct dwz_file *dwz = dwarf2_get_dwz_file ();
  13462.         LONGEST str_offset = read_offset (abfd, info_ptr, cu_header,
  13463.                                           &bytes_read);

  13464.         DW_STRING (attr) = read_indirect_string_from_dwz (dwz, str_offset);
  13465.         DW_STRING_IS_CANONICAL (attr) = 0;
  13466.         info_ptr += bytes_read;
  13467.       }
  13468.       break;
  13469.     case DW_FORM_exprloc:
  13470.     case DW_FORM_block:
  13471.       blk = dwarf_alloc_block (cu);
  13472.       blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
  13473.       info_ptr += bytes_read;
  13474.       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
  13475.       info_ptr += blk->size;
  13476.       DW_BLOCK (attr) = blk;
  13477.       break;
  13478.     case DW_FORM_block1:
  13479.       blk = dwarf_alloc_block (cu);
  13480.       blk->size = read_1_byte (abfd, info_ptr);
  13481.       info_ptr += 1;
  13482.       blk->data = read_n_bytes (abfd, info_ptr, blk->size);
  13483.       info_ptr += blk->size;
  13484.       DW_BLOCK (attr) = blk;
  13485.       break;
  13486.     case DW_FORM_data1:
  13487.       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
  13488.       info_ptr += 1;
  13489.       break;
  13490.     case DW_FORM_flag:
  13491.       DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
  13492.       info_ptr += 1;
  13493.       break;
  13494.     case DW_FORM_flag_present:
  13495.       DW_UNSND (attr) = 1;
  13496.       break;
  13497.     case DW_FORM_sdata:
  13498.       DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
  13499.       info_ptr += bytes_read;
  13500.       break;
  13501.     case DW_FORM_udata:
  13502.       DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
  13503.       info_ptr += bytes_read;
  13504.       break;
  13505.     case DW_FORM_ref1:
  13506.       DW_UNSND (attr) = (cu->header.offset.sect_off
  13507.                          + read_1_byte (abfd, info_ptr));
  13508.       info_ptr += 1;
  13509.       break;
  13510.     case DW_FORM_ref2:
  13511.       DW_UNSND (attr) = (cu->header.offset.sect_off
  13512.                          + read_2_bytes (abfd, info_ptr));
  13513.       info_ptr += 2;
  13514.       break;
  13515.     case DW_FORM_ref4:
  13516.       DW_UNSND (attr) = (cu->header.offset.sect_off
  13517.                          + read_4_bytes (abfd, info_ptr));
  13518.       info_ptr += 4;
  13519.       break;
  13520.     case DW_FORM_ref8:
  13521.       DW_UNSND (attr) = (cu->header.offset.sect_off
  13522.                          + read_8_bytes (abfd, info_ptr));
  13523.       info_ptr += 8;
  13524.       break;
  13525.     case DW_FORM_ref_sig8:
  13526.       DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
  13527.       info_ptr += 8;
  13528.       break;
  13529.     case DW_FORM_ref_udata:
  13530.       DW_UNSND (attr) = (cu->header.offset.sect_off
  13531.                          + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
  13532.       info_ptr += bytes_read;
  13533.       break;
  13534.     case DW_FORM_indirect:
  13535.       form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
  13536.       info_ptr += bytes_read;
  13537.       info_ptr = read_attribute_value (reader, attr, form, info_ptr);
  13538.       break;
  13539.     case DW_FORM_GNU_addr_index:
  13540.       if (reader->dwo_file == NULL)
  13541.         {
  13542.           /* For now flag a hard error.
  13543.              Later we can turn this into a complaint.  */
  13544.           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
  13545.                  dwarf_form_name (form),
  13546.                  bfd_get_filename (abfd));
  13547.         }
  13548.       DW_ADDR (attr) = read_addr_index_from_leb128 (cu, info_ptr, &bytes_read);
  13549.       info_ptr += bytes_read;
  13550.       break;
  13551.     case DW_FORM_GNU_str_index:
  13552.       if (reader->dwo_file == NULL)
  13553.         {
  13554.           /* For now flag a hard error.
  13555.              Later we can turn this into a complaint if warranted.  */
  13556.           error (_("Dwarf Error: %s found in non-DWO CU [in module %s]"),
  13557.                  dwarf_form_name (form),
  13558.                  bfd_get_filename (abfd));
  13559.         }
  13560.       {
  13561.         ULONGEST str_index =
  13562.           read_unsigned_leb128 (abfd, info_ptr, &bytes_read);

  13563.         DW_STRING (attr) = read_str_index (reader, str_index);
  13564.         DW_STRING_IS_CANONICAL (attr) = 0;
  13565.         info_ptr += bytes_read;
  13566.       }
  13567.       break;
  13568.     default:
  13569.       error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
  13570.              dwarf_form_name (form),
  13571.              bfd_get_filename (abfd));
  13572.     }

  13573.   /* Super hack.  */
  13574.   if (cu->per_cu->is_dwz && attr_form_is_ref (attr))
  13575.     attr->form = DW_FORM_GNU_ref_alt;

  13576.   /* We have seen instances where the compiler tried to emit a byte
  13577.      size attribute of -1 which ended up being encoded as an unsigned
  13578.      0xffffffff.  Although 0xffffffff is technically a valid size value,
  13579.      an object of this size seems pretty unlikely so we can relatively
  13580.      safely treat these cases as if the size attribute was invalid and
  13581.      treat them as zero by default.  */
  13582.   if (attr->name == DW_AT_byte_size
  13583.       && form == DW_FORM_data4
  13584.       && DW_UNSND (attr) >= 0xffffffff)
  13585.     {
  13586.       complaint
  13587.         (&symfile_complaints,
  13588.          _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
  13589.          hex_string (DW_UNSND (attr)));
  13590.       DW_UNSND (attr) = 0;
  13591.     }

  13592.   return info_ptr;
  13593. }

  13594. /* Read an attribute described by an abbreviated attribute.  */

  13595. static const gdb_byte *
  13596. read_attribute (const struct die_reader_specs *reader,
  13597.                 struct attribute *attr, struct attr_abbrev *abbrev,
  13598.                 const gdb_byte *info_ptr)
  13599. {
  13600.   attr->name = abbrev->name;
  13601.   return read_attribute_value (reader, attr, abbrev->form, info_ptr);
  13602. }

  13603. /* Read dwarf information from a buffer.  */

  13604. static unsigned int
  13605. read_1_byte (bfd *abfd, const gdb_byte *buf)
  13606. {
  13607.   return bfd_get_8 (abfd, buf);
  13608. }

  13609. static int
  13610. read_1_signed_byte (bfd *abfd, const gdb_byte *buf)
  13611. {
  13612.   return bfd_get_signed_8 (abfd, buf);
  13613. }

  13614. static unsigned int
  13615. read_2_bytes (bfd *abfd, const gdb_byte *buf)
  13616. {
  13617.   return bfd_get_16 (abfd, buf);
  13618. }

  13619. static int
  13620. read_2_signed_bytes (bfd *abfd, const gdb_byte *buf)
  13621. {
  13622.   return bfd_get_signed_16 (abfd, buf);
  13623. }

  13624. static unsigned int
  13625. read_4_bytes (bfd *abfd, const gdb_byte *buf)
  13626. {
  13627.   return bfd_get_32 (abfd, buf);
  13628. }

  13629. static int
  13630. read_4_signed_bytes (bfd *abfd, const gdb_byte *buf)
  13631. {
  13632.   return bfd_get_signed_32 (abfd, buf);
  13633. }

  13634. static ULONGEST
  13635. read_8_bytes (bfd *abfd, const gdb_byte *buf)
  13636. {
  13637.   return bfd_get_64 (abfd, buf);
  13638. }

  13639. static CORE_ADDR
  13640. read_address (bfd *abfd, const gdb_byte *buf, struct dwarf2_cu *cu,
  13641.               unsigned int *bytes_read)
  13642. {
  13643.   struct comp_unit_head *cu_header = &cu->header;
  13644.   CORE_ADDR retval = 0;

  13645.   if (cu_header->signed_addr_p)
  13646.     {
  13647.       switch (cu_header->addr_size)
  13648.         {
  13649.         case 2:
  13650.           retval = bfd_get_signed_16 (abfd, buf);
  13651.           break;
  13652.         case 4:
  13653.           retval = bfd_get_signed_32 (abfd, buf);
  13654.           break;
  13655.         case 8:
  13656.           retval = bfd_get_signed_64 (abfd, buf);
  13657.           break;
  13658.         default:
  13659.           internal_error (__FILE__, __LINE__,
  13660.                           _("read_address: bad switch, signed [in module %s]"),
  13661.                           bfd_get_filename (abfd));
  13662.         }
  13663.     }
  13664.   else
  13665.     {
  13666.       switch (cu_header->addr_size)
  13667.         {
  13668.         case 2:
  13669.           retval = bfd_get_16 (abfd, buf);
  13670.           break;
  13671.         case 4:
  13672.           retval = bfd_get_32 (abfd, buf);
  13673.           break;
  13674.         case 8:
  13675.           retval = bfd_get_64 (abfd, buf);
  13676.           break;
  13677.         default:
  13678.           internal_error (__FILE__, __LINE__,
  13679.                           _("read_address: bad switch, "
  13680.                             "unsigned [in module %s]"),
  13681.                           bfd_get_filename (abfd));
  13682.         }
  13683.     }

  13684.   *bytes_read = cu_header->addr_size;
  13685.   return retval;
  13686. }

  13687. /* Read the initial length from a section.  The (draft) DWARF 3
  13688.    specification allows the initial length to take up either 4 bytes
  13689.    or 12 bytes.  If the first 4 bytes are 0xffffffff, then the next 8
  13690.    bytes describe the length and all offsets will be 8 bytes in length
  13691.    instead of 4.

  13692.    An older, non-standard 64-bit format is also handled by this
  13693.    function.  The older format in question stores the initial length
  13694.    as an 8-byte quantity without an escape value.  Lengths greater
  13695.    than 2^32 aren't very common which means that the initial 4 bytes
  13696.    is almost always zero.  Since a length value of zero doesn't make
  13697.    sense for the 32-bit format, this initial zero can be considered to
  13698.    be an escape value which indicates the presence of the older 64-bit
  13699.    format.  As written, the code can't detect (old format) lengths
  13700.    greater than 4GB.  If it becomes necessary to handle lengths
  13701.    somewhat larger than 4GB, we could allow other small values (such
  13702.    as the non-sensical values of 1, 2, and 3) to also be used as
  13703.    escape values indicating the presence of the old format.

  13704.    The value returned via bytes_read should be used to increment the
  13705.    relevant pointer after calling read_initial_length().

  13706.    [ Note:  read_initial_length() and read_offset() are based on the
  13707.      document entitled "DWARF Debugging Information Format", revision
  13708.      3, draft 8, dated November 19, 2001.  This document was obtained
  13709.      from:

  13710.         http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf

  13711.      This document is only a draft and is subject to change.  (So beware.)

  13712.      Details regarding the older, non-standard 64-bit format were
  13713.      determined empirically by examining 64-bit ELF files produced by
  13714.      the SGI toolchain on an IRIX 6.5 machine.

  13715.      - Kevin, July 16, 2002
  13716.    ] */

  13717. static LONGEST
  13718. read_initial_length (bfd *abfd, const gdb_byte *buf, unsigned int *bytes_read)
  13719. {
  13720.   LONGEST length = bfd_get_32 (abfd, buf);

  13721.   if (length == 0xffffffff)
  13722.     {
  13723.       length = bfd_get_64 (abfd, buf + 4);
  13724.       *bytes_read = 12;
  13725.     }
  13726.   else if (length == 0)
  13727.     {
  13728.       /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX.  */
  13729.       length = bfd_get_64 (abfd, buf);
  13730.       *bytes_read = 8;
  13731.     }
  13732.   else
  13733.     {
  13734.       *bytes_read = 4;
  13735.     }

  13736.   return length;
  13737. }

  13738. /* Cover function for read_initial_length.
  13739.    Returns the length of the object at BUF, and stores the size of the
  13740.    initial length in *BYTES_READ and stores the size that offsets will be in
  13741.    *OFFSET_SIZE.
  13742.    If the initial length size is not equivalent to that specified in
  13743.    CU_HEADER then issue a complaint.
  13744.    This is useful when reading non-comp-unit headers.  */

  13745. static LONGEST
  13746. read_checked_initial_length_and_offset (bfd *abfd, const gdb_byte *buf,
  13747.                                         const struct comp_unit_head *cu_header,
  13748.                                         unsigned int *bytes_read,
  13749.                                         unsigned int *offset_size)
  13750. {
  13751.   LONGEST length = read_initial_length (abfd, buf, bytes_read);

  13752.   gdb_assert (cu_header->initial_length_size == 4
  13753.               || cu_header->initial_length_size == 8
  13754.               || cu_header->initial_length_size == 12);

  13755.   if (cu_header->initial_length_size != *bytes_read)
  13756.     complaint (&symfile_complaints,
  13757.                _("intermixed 32-bit and 64-bit DWARF sections"));

  13758.   *offset_size = (*bytes_read == 4) ? 4 : 8;
  13759.   return length;
  13760. }

  13761. /* Read an offset from the data stream.  The size of the offset is
  13762.    given by cu_header->offset_size.  */

  13763. static LONGEST
  13764. read_offset (bfd *abfd, const gdb_byte *buf,
  13765.              const struct comp_unit_head *cu_header,
  13766.              unsigned int *bytes_read)
  13767. {
  13768.   LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);

  13769.   *bytes_read = cu_header->offset_size;
  13770.   return offset;
  13771. }

  13772. /* Read an offset from the data stream.  */

  13773. static LONGEST
  13774. read_offset_1 (bfd *abfd, const gdb_byte *buf, unsigned int offset_size)
  13775. {
  13776.   LONGEST retval = 0;

  13777.   switch (offset_size)
  13778.     {
  13779.     case 4:
  13780.       retval = bfd_get_32 (abfd, buf);
  13781.       break;
  13782.     case 8:
  13783.       retval = bfd_get_64 (abfd, buf);
  13784.       break;
  13785.     default:
  13786.       internal_error (__FILE__, __LINE__,
  13787.                       _("read_offset_1: bad switch [in module %s]"),
  13788.                       bfd_get_filename (abfd));
  13789.     }

  13790.   return retval;
  13791. }

  13792. static const gdb_byte *
  13793. read_n_bytes (bfd *abfd, const gdb_byte *buf, unsigned int size)
  13794. {
  13795.   /* If the size of a host char is 8 bits, we can return a pointer
  13796.      to the buffer, otherwise we have to copy the data to a buffer
  13797.      allocated on the temporary obstack.  */
  13798.   gdb_assert (HOST_CHAR_BIT == 8);
  13799.   return buf;
  13800. }

  13801. static const char *
  13802. read_direct_string (bfd *abfd, const gdb_byte *buf,
  13803.                     unsigned int *bytes_read_ptr)
  13804. {
  13805.   /* If the size of a host char is 8 bits, we can return a pointer
  13806.      to the string, otherwise we have to copy the string to a buffer
  13807.      allocated on the temporary obstack.  */
  13808.   gdb_assert (HOST_CHAR_BIT == 8);
  13809.   if (*buf == '\0')
  13810.     {
  13811.       *bytes_read_ptr = 1;
  13812.       return NULL;
  13813.     }
  13814.   *bytes_read_ptr = strlen ((const char *) buf) + 1;
  13815.   return (const char *) buf;
  13816. }

  13817. static const char *
  13818. read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
  13819. {
  13820.   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
  13821.   if (dwarf2_per_objfile->str.buffer == NULL)
  13822.     error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
  13823.            bfd_get_filename (abfd));
  13824.   if (str_offset >= dwarf2_per_objfile->str.size)
  13825.     error (_("DW_FORM_strp pointing outside of "
  13826.              ".debug_str section [in module %s]"),
  13827.            bfd_get_filename (abfd));
  13828.   gdb_assert (HOST_CHAR_BIT == 8);
  13829.   if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
  13830.     return NULL;
  13831.   return (const char *) (dwarf2_per_objfile->str.buffer + str_offset);
  13832. }

  13833. /* Read a string at offset STR_OFFSET in the .debug_str section from
  13834.    the .dwz file DWZ.  Throw an error if the offset is too large.  If
  13835.    the string consists of a single NUL byte, return NULL; otherwise
  13836.    return a pointer to the string.  */

  13837. static const char *
  13838. read_indirect_string_from_dwz (struct dwz_file *dwz, LONGEST str_offset)
  13839. {
  13840.   dwarf2_read_section (dwarf2_per_objfile->objfile, &dwz->str);

  13841.   if (dwz->str.buffer == NULL)
  13842.     error (_("DW_FORM_GNU_strp_alt used without .debug_str "
  13843.              "section [in module %s]"),
  13844.            bfd_get_filename (dwz->dwz_bfd));
  13845.   if (str_offset >= dwz->str.size)
  13846.     error (_("DW_FORM_GNU_strp_alt pointing outside of "
  13847.              ".debug_str section [in module %s]"),
  13848.            bfd_get_filename (dwz->dwz_bfd));
  13849.   gdb_assert (HOST_CHAR_BIT == 8);
  13850.   if (dwz->str.buffer[str_offset] == '\0')
  13851.     return NULL;
  13852.   return (const char *) (dwz->str.buffer + str_offset);
  13853. }

  13854. static const char *
  13855. read_indirect_string (bfd *abfd, const gdb_byte *buf,
  13856.                       const struct comp_unit_head *cu_header,
  13857.                       unsigned int *bytes_read_ptr)
  13858. {
  13859.   LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);

  13860.   return read_indirect_string_at_offset (abfd, str_offset);
  13861. }

  13862. static ULONGEST
  13863. read_unsigned_leb128 (bfd *abfd, const gdb_byte *buf,
  13864.                       unsigned int *bytes_read_ptr)
  13865. {
  13866.   ULONGEST result;
  13867.   unsigned int num_read;
  13868.   int i, shift;
  13869.   unsigned char byte;

  13870.   result = 0;
  13871.   shift = 0;
  13872.   num_read = 0;
  13873.   i = 0;
  13874.   while (1)
  13875.     {
  13876.       byte = bfd_get_8 (abfd, buf);
  13877.       buf++;
  13878.       num_read++;
  13879.       result |= ((ULONGEST) (byte & 127) << shift);
  13880.       if ((byte & 128) == 0)
  13881.         {
  13882.           break;
  13883.         }
  13884.       shift += 7;
  13885.     }
  13886.   *bytes_read_ptr = num_read;
  13887.   return result;
  13888. }

  13889. static LONGEST
  13890. read_signed_leb128 (bfd *abfd, const gdb_byte *buf,
  13891.                     unsigned int *bytes_read_ptr)
  13892. {
  13893.   LONGEST result;
  13894.   int i, shift, num_read;
  13895.   unsigned char byte;

  13896.   result = 0;
  13897.   shift = 0;
  13898.   num_read = 0;
  13899.   i = 0;
  13900.   while (1)
  13901.     {
  13902.       byte = bfd_get_8 (abfd, buf);
  13903.       buf++;
  13904.       num_read++;
  13905.       result |= ((LONGEST) (byte & 127) << shift);
  13906.       shift += 7;
  13907.       if ((byte & 128) == 0)
  13908.         {
  13909.           break;
  13910.         }
  13911.     }
  13912.   if ((shift < 8 * sizeof (result)) && (byte & 0x40))
  13913.     result |= -(((LONGEST) 1) << shift);
  13914.   *bytes_read_ptr = num_read;
  13915.   return result;
  13916. }

  13917. /* Given index ADDR_INDEX in .debug_addr, fetch the value.
  13918.    ADDR_BASE is the DW_AT_GNU_addr_base attribute or zero.
  13919.    ADDR_SIZE is the size of addresses from the CU header.  */

  13920. static CORE_ADDR
  13921. read_addr_index_1 (unsigned int addr_index, ULONGEST addr_base, int addr_size)
  13922. {
  13923.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  13924.   bfd *abfd = objfile->obfd;
  13925.   const gdb_byte *info_ptr;

  13926.   dwarf2_read_section (objfile, &dwarf2_per_objfile->addr);
  13927.   if (dwarf2_per_objfile->addr.buffer == NULL)
  13928.     error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
  13929.            objfile_name (objfile));
  13930.   if (addr_base + addr_index * addr_size >= dwarf2_per_objfile->addr.size)
  13931.     error (_("DW_FORM_addr_index pointing outside of "
  13932.              ".debug_addr section [in module %s]"),
  13933.            objfile_name (objfile));
  13934.   info_ptr = (dwarf2_per_objfile->addr.buffer
  13935.               + addr_base + addr_index * addr_size);
  13936.   if (addr_size == 4)
  13937.     return bfd_get_32 (abfd, info_ptr);
  13938.   else
  13939.     return bfd_get_64 (abfd, info_ptr);
  13940. }

  13941. /* Given index ADDR_INDEX in .debug_addr, fetch the value.  */

  13942. static CORE_ADDR
  13943. read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
  13944. {
  13945.   return read_addr_index_1 (addr_index, cu->addr_base, cu->header.addr_size);
  13946. }

  13947. /* Given a pointer to an leb128 value, fetch the value from .debug_addr.  */

  13948. static CORE_ADDR
  13949. read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
  13950.                              unsigned int *bytes_read)
  13951. {
  13952.   bfd *abfd = cu->objfile->obfd;
  13953.   unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);

  13954.   return read_addr_index (cu, addr_index);
  13955. }

  13956. /* Data structure to pass results from dwarf2_read_addr_index_reader
  13957.    back to dwarf2_read_addr_index.  */

  13958. struct dwarf2_read_addr_index_data
  13959. {
  13960.   ULONGEST addr_base;
  13961.   int addr_size;
  13962. };

  13963. /* die_reader_func for dwarf2_read_addr_index.  */

  13964. static void
  13965. dwarf2_read_addr_index_reader (const struct die_reader_specs *reader,
  13966.                                const gdb_byte *info_ptr,
  13967.                                struct die_info *comp_unit_die,
  13968.                                int has_children,
  13969.                                void *data)
  13970. {
  13971.   struct dwarf2_cu *cu = reader->cu;
  13972.   struct dwarf2_read_addr_index_data *aidata =
  13973.     (struct dwarf2_read_addr_index_data *) data;

  13974.   aidata->addr_base = cu->addr_base;
  13975.   aidata->addr_size = cu->header.addr_size;
  13976. }

  13977. /* Given an index in .debug_addr, fetch the value.
  13978.    NOTE: This can be called during dwarf expression evaluation,
  13979.    long after the debug information has been read, and thus per_cu->cu
  13980.    may no longer exist.  */

  13981. CORE_ADDR
  13982. dwarf2_read_addr_index (struct dwarf2_per_cu_data *per_cu,
  13983.                         unsigned int addr_index)
  13984. {
  13985.   struct objfile *objfile = per_cu->objfile;
  13986.   struct dwarf2_cu *cu = per_cu->cu;
  13987.   ULONGEST addr_base;
  13988.   int addr_size;

  13989.   /* This is intended to be called from outside this file.  */
  13990.   dw2_setup (objfile);

  13991.   /* We need addr_base and addr_size.
  13992.      If we don't have PER_CU->cu, we have to get it.
  13993.      Nasty, but the alternative is storing the needed info in PER_CU,
  13994.      which at this point doesn't seem justified: it's not clear how frequently
  13995.      it would get used and it would increase the size of every PER_CU.
  13996.      Entry points like dwarf2_per_cu_addr_size do a similar thing
  13997.      so we're not in uncharted territory here.
  13998.      Alas we need to be a bit more complicated as addr_base is contained
  13999.      in the DIE.

  14000.      We don't need to read the entire CU(/TU).
  14001.      We just need the header and top level die.

  14002.      IWBN to use the aging mechanism to let us lazily later discard the CU.
  14003.      For now we skip this optimization.  */

  14004.   if (cu != NULL)
  14005.     {
  14006.       addr_base = cu->addr_base;
  14007.       addr_size = cu->header.addr_size;
  14008.     }
  14009.   else
  14010.     {
  14011.       struct dwarf2_read_addr_index_data aidata;

  14012.       /* Note: We can't use init_cutu_and_read_dies_simple here,
  14013.          we need addr_base.  */
  14014.       init_cutu_and_read_dies (per_cu, NULL, 0, 0,
  14015.                                dwarf2_read_addr_index_reader, &aidata);
  14016.       addr_base = aidata.addr_base;
  14017.       addr_size = aidata.addr_size;
  14018.     }

  14019.   return read_addr_index_1 (addr_index, addr_base, addr_size);
  14020. }

  14021. /* Given a DW_FORM_GNU_str_index, fetch the string.
  14022.    This is only used by the Fission support.  */

  14023. static const char *
  14024. read_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
  14025. {
  14026.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  14027.   const char *objf_name = objfile_name (objfile);
  14028.   bfd *abfd = objfile->obfd;
  14029.   struct dwarf2_cu *cu = reader->cu;
  14030.   struct dwarf2_section_info *str_section = &reader->dwo_file->sections.str;
  14031.   struct dwarf2_section_info *str_offsets_section =
  14032.     &reader->dwo_file->sections.str_offsets;
  14033.   const gdb_byte *info_ptr;
  14034.   ULONGEST str_offset;
  14035.   static const char form_name[] = "DW_FORM_GNU_str_index";

  14036.   dwarf2_read_section (objfile, str_section);
  14037.   dwarf2_read_section (objfile, str_offsets_section);
  14038.   if (str_section->buffer == NULL)
  14039.     error (_("%s used without .debug_str.dwo section"
  14040.              " in CU at offset 0x%lx [in module %s]"),
  14041.            form_name, (long) cu->header.offset.sect_off, objf_name);
  14042.   if (str_offsets_section->buffer == NULL)
  14043.     error (_("%s used without .debug_str_offsets.dwo section"
  14044.              " in CU at offset 0x%lx [in module %s]"),
  14045.            form_name, (long) cu->header.offset.sect_off, objf_name);
  14046.   if (str_index * cu->header.offset_size >= str_offsets_section->size)
  14047.     error (_("%s pointing outside of .debug_str_offsets.dwo"
  14048.              " section in CU at offset 0x%lx [in module %s]"),
  14049.            form_name, (long) cu->header.offset.sect_off, objf_name);
  14050.   info_ptr = (str_offsets_section->buffer
  14051.               + str_index * cu->header.offset_size);
  14052.   if (cu->header.offset_size == 4)
  14053.     str_offset = bfd_get_32 (abfd, info_ptr);
  14054.   else
  14055.     str_offset = bfd_get_64 (abfd, info_ptr);
  14056.   if (str_offset >= str_section->size)
  14057.     error (_("Offset from %s pointing outside of"
  14058.              " .debug_str.dwo section in CU at offset 0x%lx [in module %s]"),
  14059.            form_name, (long) cu->header.offset.sect_off, objf_name);
  14060.   return (const char *) (str_section->buffer + str_offset);
  14061. }

  14062. /* Return the length of an LEB128 number in BUF.  */

  14063. static int
  14064. leb128_size (const gdb_byte *buf)
  14065. {
  14066.   const gdb_byte *begin = buf;
  14067.   gdb_byte byte;

  14068.   while (1)
  14069.     {
  14070.       byte = *buf++;
  14071.       if ((byte & 128) == 0)
  14072.         return buf - begin;
  14073.     }
  14074. }

  14075. static void
  14076. set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
  14077. {
  14078.   switch (lang)
  14079.     {
  14080.     case DW_LANG_C89:
  14081.     case DW_LANG_C99:
  14082.     case DW_LANG_C11:
  14083.     case DW_LANG_C:
  14084.     case DW_LANG_UPC:
  14085.       cu->language = language_c;
  14086.       break;
  14087.     case DW_LANG_C_plus_plus:
  14088.     case DW_LANG_C_plus_plus_11:
  14089.     case DW_LANG_C_plus_plus_14:
  14090.       cu->language = language_cplus;
  14091.       break;
  14092.     case DW_LANG_D:
  14093.       cu->language = language_d;
  14094.       break;
  14095.     case DW_LANG_Fortran77:
  14096.     case DW_LANG_Fortran90:
  14097.     case DW_LANG_Fortran95:
  14098.       cu->language = language_fortran;
  14099.       break;
  14100.     case DW_LANG_Go:
  14101.       cu->language = language_go;
  14102.       break;
  14103.     case DW_LANG_Mips_Assembler:
  14104.       cu->language = language_asm;
  14105.       break;
  14106.     case DW_LANG_Java:
  14107.       cu->language = language_java;
  14108.       break;
  14109.     case DW_LANG_Ada83:
  14110.     case DW_LANG_Ada95:
  14111.       cu->language = language_ada;
  14112.       break;
  14113.     case DW_LANG_Modula2:
  14114.       cu->language = language_m2;
  14115.       break;
  14116.     case DW_LANG_Pascal83:
  14117.       cu->language = language_pascal;
  14118.       break;
  14119.     case DW_LANG_ObjC:
  14120.       cu->language = language_objc;
  14121.       break;
  14122.     case DW_LANG_Cobol74:
  14123.     case DW_LANG_Cobol85:
  14124.     default:
  14125.       cu->language = language_minimal;
  14126.       break;
  14127.     }
  14128.   cu->language_defn = language_def (cu->language);
  14129. }

  14130. /* Return the named attribute or NULL if not there.  */

  14131. static struct attribute *
  14132. dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
  14133. {
  14134.   for (;;)
  14135.     {
  14136.       unsigned int i;
  14137.       struct attribute *spec = NULL;

  14138.       for (i = 0; i < die->num_attrs; ++i)
  14139.         {
  14140.           if (die->attrs[i].name == name)
  14141.             return &die->attrs[i];
  14142.           if (die->attrs[i].name == DW_AT_specification
  14143.               || die->attrs[i].name == DW_AT_abstract_origin)
  14144.             spec = &die->attrs[i];
  14145.         }

  14146.       if (!spec)
  14147.         break;

  14148.       die = follow_die_ref (die, spec, &cu);
  14149.     }

  14150.   return NULL;
  14151. }

  14152. /* Return the named attribute or NULL if not there,
  14153.    but do not follow DW_AT_specification, etc.
  14154.    This is for use in contexts where we're reading .debug_types dies.
  14155.    Following DW_AT_specification, DW_AT_abstract_origin will take us
  14156.    back up the chain, and we want to go down.  */

  14157. static struct attribute *
  14158. dwarf2_attr_no_follow (struct die_info *die, unsigned int name)
  14159. {
  14160.   unsigned int i;

  14161.   for (i = 0; i < die->num_attrs; ++i)
  14162.     if (die->attrs[i].name == name)
  14163.       return &die->attrs[i];

  14164.   return NULL;
  14165. }

  14166. /* Return non-zero iff the attribute NAME is defined for the given DIE,
  14167.    and holds a non-zero value.  This function should only be used for
  14168.    DW_FORM_flag or DW_FORM_flag_present attributes.  */

  14169. static int
  14170. dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
  14171. {
  14172.   struct attribute *attr = dwarf2_attr (die, name, cu);

  14173.   return (attr && DW_UNSND (attr));
  14174. }

  14175. static int
  14176. die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
  14177. {
  14178.   /* A DIE is a declaration if it has a DW_AT_declaration attribute
  14179.      which value is non-zero.  However, we have to be careful with
  14180.      DIEs having a DW_AT_specification attribute, because dwarf2_attr()
  14181.      (via dwarf2_flag_true_p) follows this attribute.  So we may
  14182.      end up accidently finding a declaration attribute that belongs
  14183.      to a different DIE referenced by the specification attribute,
  14184.      even though the given DIE does not have a declaration attribute.  */
  14185.   return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
  14186.           && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
  14187. }

  14188. /* Return the die giving the specification for DIE, if there is
  14189.    one.  *SPEC_CU is the CU containing DIE on input, and the CU
  14190.    containing the return value on output.  If there is no
  14191.    specification, but there is an abstract origin, that is
  14192.    returned.  */

  14193. static struct die_info *
  14194. die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
  14195. {
  14196.   struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
  14197.                                              *spec_cu);

  14198.   if (spec_attr == NULL)
  14199.     spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);

  14200.   if (spec_attr == NULL)
  14201.     return NULL;
  14202.   else
  14203.     return follow_die_ref (die, spec_attr, spec_cu);
  14204. }

  14205. /* Free the line_header structure *LH, and any arrays and strings it
  14206.    refers to.
  14207.    NOTE: This is also used as a "cleanup" function.  */

  14208. static void
  14209. free_line_header (struct line_header *lh)
  14210. {
  14211.   if (lh->standard_opcode_lengths)
  14212.     xfree (lh->standard_opcode_lengths);

  14213.   /* Remember that all the lh->file_names[i].name pointers are
  14214.      pointers into debug_line_buffer, and don't need to be freed.  */
  14215.   if (lh->file_names)
  14216.     xfree (lh->file_names);

  14217.   /* Similarly for the include directory names.  */
  14218.   if (lh->include_dirs)
  14219.     xfree (lh->include_dirs);

  14220.   xfree (lh);
  14221. }

  14222. /* Add an entry to LH's include directory table.  */

  14223. static void
  14224. add_include_dir (struct line_header *lh, const char *include_dir)
  14225. {
  14226.   /* Grow the array if necessary.  */
  14227.   if (lh->include_dirs_size == 0)
  14228.     {
  14229.       lh->include_dirs_size = 1; /* for testing */
  14230.       lh->include_dirs = xmalloc (lh->include_dirs_size
  14231.                                   * sizeof (*lh->include_dirs));
  14232.     }
  14233.   else if (lh->num_include_dirs >= lh->include_dirs_size)
  14234.     {
  14235.       lh->include_dirs_size *= 2;
  14236.       lh->include_dirs = xrealloc (lh->include_dirs,
  14237.                                    (lh->include_dirs_size
  14238.                                     * sizeof (*lh->include_dirs)));
  14239.     }

  14240.   lh->include_dirs[lh->num_include_dirs++] = include_dir;
  14241. }

  14242. /* Add an entry to LH's file name table.  */

  14243. static void
  14244. add_file_name (struct line_header *lh,
  14245.                const char *name,
  14246.                unsigned int dir_index,
  14247.                unsigned int mod_time,
  14248.                unsigned int length)
  14249. {
  14250.   struct file_entry *fe;

  14251.   /* Grow the array if necessary.  */
  14252.   if (lh->file_names_size == 0)
  14253.     {
  14254.       lh->file_names_size = 1; /* for testing */
  14255.       lh->file_names = xmalloc (lh->file_names_size
  14256.                                 * sizeof (*lh->file_names));
  14257.     }
  14258.   else if (lh->num_file_names >= lh->file_names_size)
  14259.     {
  14260.       lh->file_names_size *= 2;
  14261.       lh->file_names = xrealloc (lh->file_names,
  14262.                                  (lh->file_names_size
  14263.                                   * sizeof (*lh->file_names)));
  14264.     }

  14265.   fe = &lh->file_names[lh->num_file_names++];
  14266.   fe->name = name;
  14267.   fe->dir_index = dir_index;
  14268.   fe->mod_time = mod_time;
  14269.   fe->length = length;
  14270.   fe->included_p = 0;
  14271.   fe->symtab = NULL;
  14272. }

  14273. /* A convenience function to find the proper .debug_line section for a
  14274.    CU.  */

  14275. static struct dwarf2_section_info *
  14276. get_debug_line_section (struct dwarf2_cu *cu)
  14277. {
  14278.   struct dwarf2_section_info *section;

  14279.   /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
  14280.      DWO file.  */
  14281.   if (cu->dwo_unit && cu->per_cu->is_debug_types)
  14282.     section = &cu->dwo_unit->dwo_file->sections.line;
  14283.   else if (cu->per_cu->is_dwz)
  14284.     {
  14285.       struct dwz_file *dwz = dwarf2_get_dwz_file ();

  14286.       section = &dwz->line;
  14287.     }
  14288.   else
  14289.     section = &dwarf2_per_objfile->line;

  14290.   return section;
  14291. }

  14292. /* Read the statement program header starting at OFFSET in
  14293.    .debug_line, or .debug_line.dwo.  Return a pointer
  14294.    to a struct line_header, allocated using xmalloc.

  14295.    NOTE: the strings in the include directory and file name tables of
  14296.    the returned object point into the dwarf line section buffer,
  14297.    and must not be freed.  */

  14298. static struct line_header *
  14299. dwarf_decode_line_header (unsigned int offset, struct dwarf2_cu *cu)
  14300. {
  14301.   struct cleanup *back_to;
  14302.   struct line_header *lh;
  14303.   const gdb_byte *line_ptr;
  14304.   unsigned int bytes_read, offset_size;
  14305.   int i;
  14306.   const char *cur_dir, *cur_file;
  14307.   struct dwarf2_section_info *section;
  14308.   bfd *abfd;

  14309.   section = get_debug_line_section (cu);
  14310.   dwarf2_read_section (dwarf2_per_objfile->objfile, section);
  14311.   if (section->buffer == NULL)
  14312.     {
  14313.       if (cu->dwo_unit && cu->per_cu->is_debug_types)
  14314.         complaint (&symfile_complaints, _("missing .debug_line.dwo section"));
  14315.       else
  14316.         complaint (&symfile_complaints, _("missing .debug_line section"));
  14317.       return 0;
  14318.     }

  14319.   /* We can't do this until we know the section is non-empty.
  14320.      Only then do we know we have such a section.  */
  14321.   abfd = get_section_bfd_owner (section);

  14322.   /* Make sure that at least there's room for the total_length field.
  14323.      That could be 12 bytes long, but we're just going to fudge that.  */
  14324.   if (offset + 4 >= section->size)
  14325.     {
  14326.       dwarf2_statement_list_fits_in_line_number_section_complaint ();
  14327.       return 0;
  14328.     }

  14329.   lh = xmalloc (sizeof (*lh));
  14330.   memset (lh, 0, sizeof (*lh));
  14331.   back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
  14332.                           (void *) lh);

  14333.   line_ptr = section->buffer + offset;

  14334.   /* Read in the header.  */
  14335.   lh->total_length =
  14336.     read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
  14337.                                             &bytes_read, &offset_size);
  14338.   line_ptr += bytes_read;
  14339.   if (line_ptr + lh->total_length > (section->buffer + section->size))
  14340.     {
  14341.       dwarf2_statement_list_fits_in_line_number_section_complaint ();
  14342.       do_cleanups (back_to);
  14343.       return 0;
  14344.     }
  14345.   lh->statement_program_end = line_ptr + lh->total_length;
  14346.   lh->version = read_2_bytes (abfd, line_ptr);
  14347.   line_ptr += 2;
  14348.   lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
  14349.   line_ptr += offset_size;
  14350.   lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
  14351.   line_ptr += 1;
  14352.   if (lh->version >= 4)
  14353.     {
  14354.       lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
  14355.       line_ptr += 1;
  14356.     }
  14357.   else
  14358.     lh->maximum_ops_per_instruction = 1;

  14359.   if (lh->maximum_ops_per_instruction == 0)
  14360.     {
  14361.       lh->maximum_ops_per_instruction = 1;
  14362.       complaint (&symfile_complaints,
  14363.                  _("invalid maximum_ops_per_instruction "
  14364.                    "in `.debug_line' section"));
  14365.     }

  14366.   lh->default_is_stmt = read_1_byte (abfd, line_ptr);
  14367.   line_ptr += 1;
  14368.   lh->line_base = read_1_signed_byte (abfd, line_ptr);
  14369.   line_ptr += 1;
  14370.   lh->line_range = read_1_byte (abfd, line_ptr);
  14371.   line_ptr += 1;
  14372.   lh->opcode_base = read_1_byte (abfd, line_ptr);
  14373.   line_ptr += 1;
  14374.   lh->standard_opcode_lengths
  14375.     = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));

  14376.   lh->standard_opcode_lengths[0] = 1/* This should never be used anyway.  */
  14377.   for (i = 1; i < lh->opcode_base; ++i)
  14378.     {
  14379.       lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
  14380.       line_ptr += 1;
  14381.     }

  14382.   /* Read directory table.  */
  14383.   while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
  14384.     {
  14385.       line_ptr += bytes_read;
  14386.       add_include_dir (lh, cur_dir);
  14387.     }
  14388.   line_ptr += bytes_read;

  14389.   /* Read file name table.  */
  14390.   while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
  14391.     {
  14392.       unsigned int dir_index, mod_time, length;

  14393.       line_ptr += bytes_read;
  14394.       dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14395.       line_ptr += bytes_read;
  14396.       mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14397.       line_ptr += bytes_read;
  14398.       length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14399.       line_ptr += bytes_read;

  14400.       add_file_name (lh, cur_file, dir_index, mod_time, length);
  14401.     }
  14402.   line_ptr += bytes_read;
  14403.   lh->statement_program_start = line_ptr;

  14404.   if (line_ptr > (section->buffer + section->size))
  14405.     complaint (&symfile_complaints,
  14406.                _("line number info header doesn't "
  14407.                  "fit in `.debug_line' section"));

  14408.   discard_cleanups (back_to);
  14409.   return lh;
  14410. }

  14411. /* Subroutine of dwarf_decode_lines to simplify it.
  14412.    Return the file name of the psymtab for included file FILE_INDEX
  14413.    in line header LH of PST.
  14414.    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
  14415.    If space for the result is malloc'd, it will be freed by a cleanup.
  14416.    Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename.

  14417.    The function creates dangling cleanup registration.  */

  14418. static const char *
  14419. psymtab_include_file_name (const struct line_header *lh, int file_index,
  14420.                            const struct partial_symtab *pst,
  14421.                            const char *comp_dir)
  14422. {
  14423.   const struct file_entry fe = lh->file_names [file_index];
  14424.   const char *include_name = fe.name;
  14425.   const char *include_name_to_compare = include_name;
  14426.   const char *dir_name = NULL;
  14427.   const char *pst_filename;
  14428.   char *copied_name = NULL;
  14429.   int file_is_pst;

  14430.   if (fe.dir_index)
  14431.     dir_name = lh->include_dirs[fe.dir_index - 1];

  14432.   if (!IS_ABSOLUTE_PATH (include_name)
  14433.       && (dir_name != NULL || comp_dir != NULL))
  14434.     {
  14435.       /* Avoid creating a duplicate psymtab for PST.
  14436.          We do this by comparing INCLUDE_NAME and PST_FILENAME.
  14437.          Before we do the comparison, however, we need to account
  14438.          for DIR_NAME and COMP_DIR.
  14439.          First prepend dir_name (if non-NULL).  If we still don't
  14440.          have an absolute path prepend comp_dir (if non-NULL).
  14441.          However, the directory we record in the include-file's
  14442.          psymtab does not contain COMP_DIR (to match the
  14443.          corresponding symtab(s)).

  14444.          Example:

  14445.          bash$ cd /tmp
  14446.          bash$ gcc -g ./hello.c
  14447.          include_name = "hello.c"
  14448.          dir_name = "."
  14449.          DW_AT_comp_dir = comp_dir = "/tmp"
  14450.          DW_AT_name = "./hello.c"

  14451.       */

  14452.       if (dir_name != NULL)
  14453.         {
  14454.           char *tem = concat (dir_name, SLASH_STRING,
  14455.                               include_name, (char *)NULL);

  14456.           make_cleanup (xfree, tem);
  14457.           include_name = tem;
  14458.           include_name_to_compare = include_name;
  14459.         }
  14460.       if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
  14461.         {
  14462.           char *tem = concat (comp_dir, SLASH_STRING,
  14463.                               include_name, (char *)NULL);

  14464.           make_cleanup (xfree, tem);
  14465.           include_name_to_compare = tem;
  14466.         }
  14467.     }

  14468.   pst_filename = pst->filename;
  14469.   if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
  14470.     {
  14471.       copied_name = concat (pst->dirname, SLASH_STRING,
  14472.                             pst_filename, (char *)NULL);
  14473.       pst_filename = copied_name;
  14474.     }

  14475.   file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;

  14476.   if (copied_name != NULL)
  14477.     xfree (copied_name);

  14478.   if (file_is_pst)
  14479.     return NULL;
  14480.   return include_name;
  14481. }

  14482. /* Ignore this record_line request.  */

  14483. static void
  14484. noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
  14485. {
  14486.   return;
  14487. }

  14488. /* Return non-zero if we should add LINE to the line number table.
  14489.    LINE is the line to add, LAST_LINE is the last line that was added,
  14490.    LAST_SUBFILE is the subfile for LAST_LINE.
  14491.    LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
  14492.    had a non-zero discriminator.

  14493.    We have to be careful in the presence of discriminators.
  14494.    E.g., for this line:

  14495.      for (i = 0; i < 100000; i++);

  14496.    clang can emit four line number entries for that one line,
  14497.    each with a different discriminator.
  14498.    See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.

  14499.    However, we want gdb to coalesce all four entries into one.
  14500.    Otherwise the user could stepi into the middle of the line and
  14501.    gdb would get confused about whether the pc really was in the
  14502.    middle of the line.

  14503.    Things are further complicated by the fact that two consecutive
  14504.    line number entries for the same line is a heuristic used by gcc
  14505.    to denote the end of the prologue.  So we can't just discard duplicate
  14506.    entries, we have to be selective about it.  The heuristic we use is
  14507.    that we only collapse consecutive entries for the same line if at least
  14508.    one of those entries has a non-zero discriminator.  PR 17276.

  14509.    Note: Addresses in the line number state machine can never go backwards
  14510.    within one sequence, thus this coalescing is ok.  */

  14511. static int
  14512. dwarf_record_line_p (unsigned int line, unsigned int last_line,
  14513.                      int line_has_non_zero_discriminator,
  14514.                      struct subfile *last_subfile)
  14515. {
  14516.   if (current_subfile != last_subfile)
  14517.     return 1;
  14518.   if (line != last_line)
  14519.     return 1;
  14520.   /* Same line for the same file that we've seen already.
  14521.      As a last check, for pr 17276, only record the line if the line
  14522.      has never had a non-zero discriminator.  */
  14523.   if (!line_has_non_zero_discriminator)
  14524.     return 1;
  14525.   return 0;
  14526. }

  14527. /* Use P_RECORD_LINE to record line number LINE beginning at address ADDRESS
  14528.    in the line table of subfile SUBFILE.  */

  14529. static void
  14530. dwarf_record_line (struct gdbarch *gdbarch, struct subfile *subfile,
  14531.                    unsigned int line, CORE_ADDR address,
  14532.                    record_line_ftype p_record_line)
  14533. {
  14534.   CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);

  14535.   (*p_record_line) (subfile, line, addr);
  14536. }

  14537. /* Subroutine of dwarf_decode_lines_1 to simplify it.
  14538.    Mark the end of a set of line number records.
  14539.    The arguments are the same as for dwarf_record_line.
  14540.    If SUBFILE is NULL the request is ignored.  */

  14541. static void
  14542. dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
  14543.                    CORE_ADDR address, record_line_ftype p_record_line)
  14544. {
  14545.   if (subfile != NULL)
  14546.     dwarf_record_line (gdbarch, subfile, 0, address, p_record_line);
  14547. }

  14548. /* Subroutine of dwarf_decode_lines to simplify it.
  14549.    Process the line number information in LH.  */

  14550. static void
  14551. dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
  14552.                       const int decode_for_pst_p, CORE_ADDR lowpc)
  14553. {
  14554.   const gdb_byte *line_ptr, *extended_end;
  14555.   const gdb_byte *line_end;
  14556.   unsigned int bytes_read, extended_len;
  14557.   unsigned char op_code, extended_op;
  14558.   CORE_ADDR baseaddr;
  14559.   struct objfile *objfile = cu->objfile;
  14560.   bfd *abfd = objfile->obfd;
  14561.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  14562.   struct subfile *last_subfile = NULL;
  14563.   void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
  14564.     = record_line;

  14565.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  14566.   line_ptr = lh->statement_program_start;
  14567.   line_end = lh->statement_program_end;

  14568.   /* Read the statement sequences until there's nothing left.  */
  14569.   while (line_ptr < line_end)
  14570.     {
  14571.       /* State machine registers.  Call `gdbarch_adjust_dwarf2_line'
  14572.          on the initial 0 address as if there was a line entry for it
  14573.          so that the backend has a chance to adjust it and also record
  14574.          it in case it needs it.  This is currently used by MIPS code,
  14575.          cf. `mips_adjust_dwarf2_line'.  */
  14576.       CORE_ADDR address = gdbarch_adjust_dwarf2_line (gdbarch, 0, 0);
  14577.       unsigned int file = 1;
  14578.       unsigned int line = 1;
  14579.       int is_stmt = lh->default_is_stmt;
  14580.       int end_sequence = 0;
  14581.       unsigned char op_index = 0;
  14582.       unsigned int discriminator = 0;
  14583.       /* The last line number that was recorded, used to coalesce
  14584.          consecutive entries for the same line.  This can happen, for
  14585.          example, when discriminators are present.  PR 17276.  */
  14586.       unsigned int last_line = 0;
  14587.       int line_has_non_zero_discriminator = 0;

  14588.       if (!decode_for_pst_p && lh->num_file_names >= file)
  14589.         {
  14590.           /* Start a subfile for the current file of the state machine.  */
  14591.           /* lh->include_dirs and lh->file_names are 0-based, but the
  14592.              directory and file name numbers in the statement program
  14593.              are 1-based.  */
  14594.           struct file_entry *fe = &lh->file_names[file - 1];
  14595.           const char *dir = NULL;

  14596.           if (fe->dir_index)
  14597.             dir = lh->include_dirs[fe->dir_index - 1];

  14598.           dwarf2_start_subfile (fe->name, dir);
  14599.         }

  14600.       /* Decode the table.  */
  14601.       while (!end_sequence)
  14602.         {
  14603.           op_code = read_1_byte (abfd, line_ptr);
  14604.           line_ptr += 1;
  14605.           if (line_ptr > line_end)
  14606.             {
  14607.               dwarf2_debug_line_missing_end_sequence_complaint ();
  14608.               break;
  14609.             }

  14610.           if (op_code >= lh->opcode_base)
  14611.             {
  14612.               /* Special opcode.  */
  14613.               unsigned char adj_opcode;
  14614.               CORE_ADDR addr_adj;
  14615.               int line_delta;

  14616.               adj_opcode = op_code - lh->opcode_base;
  14617.               addr_adj = (((op_index + (adj_opcode / lh->line_range))
  14618.                            / lh->maximum_ops_per_instruction)
  14619.                           * lh->minimum_instruction_length);
  14620.               address += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
  14621.               op_index = ((op_index + (adj_opcode / lh->line_range))
  14622.                           % lh->maximum_ops_per_instruction);
  14623.               line_delta = lh->line_base + (adj_opcode % lh->line_range);
  14624.               line += line_delta;
  14625.               if (line_delta != 0)
  14626.                 line_has_non_zero_discriminator = discriminator != 0;
  14627.               if (lh->num_file_names < file || file == 0)
  14628.                 dwarf2_debug_line_missing_file_complaint ();
  14629.               /* For now we ignore lines not starting on an
  14630.                  instruction boundary.  */
  14631.               else if (op_index == 0)
  14632.                 {
  14633.                   lh->file_names[file - 1].included_p = 1;
  14634.                   if (!decode_for_pst_p && is_stmt)
  14635.                     {
  14636.                       if (last_subfile != current_subfile)
  14637.                         {
  14638.                           dwarf_finish_line (gdbarch, last_subfile,
  14639.                                              address, p_record_line);
  14640.                         }
  14641.                       if (dwarf_record_line_p (line, last_line,
  14642.                                                line_has_non_zero_discriminator,
  14643.                                                last_subfile))
  14644.                         {
  14645.                           dwarf_record_line (gdbarch, current_subfile,
  14646.                                              line, address, p_record_line);
  14647.                         }
  14648.                       last_subfile = current_subfile;
  14649.                       last_line = line;
  14650.                     }
  14651.                 }
  14652.               discriminator = 0;
  14653.             }
  14654.           else switch (op_code)
  14655.             {
  14656.             case DW_LNS_extended_op:
  14657.               extended_len = read_unsigned_leb128 (abfd, line_ptr,
  14658.                                                    &bytes_read);
  14659.               line_ptr += bytes_read;
  14660.               extended_end = line_ptr + extended_len;
  14661.               extended_op = read_1_byte (abfd, line_ptr);
  14662.               line_ptr += 1;
  14663.               switch (extended_op)
  14664.                 {
  14665.                 case DW_LNE_end_sequence:
  14666.                   p_record_line = record_line;
  14667.                   end_sequence = 1;
  14668.                   break;
  14669.                 case DW_LNE_set_address:
  14670.                   address = read_address (abfd, line_ptr, cu, &bytes_read);

  14671.                   /* If address < lowpc then it's not a usable value, it's
  14672.                      outside the pc range of the CU.  However, we restrict
  14673.                      the test to only address values of zero to preserve
  14674.                      GDB's previous behaviour which is to handle the specific
  14675.                      case of a function being GC'd by the linker.  */
  14676.                   if (address == 0 && address < lowpc)
  14677.                     {
  14678.                       /* This line table is for a function which has been
  14679.                          GCd by the linker.  Ignore it.  PR gdb/12528 */

  14680.                       long line_offset
  14681.                         = line_ptr - get_debug_line_section (cu)->buffer;

  14682.                       complaint (&symfile_complaints,
  14683.                                  _(".debug_line address at offset 0x%lx is 0 "
  14684.                                    "[in module %s]"),
  14685.                                  line_offset, objfile_name (objfile));
  14686.                       p_record_line = noop_record_line;
  14687.                       /* Note: p_record_line is left as noop_record_line
  14688.                          until we see DW_LNE_end_sequence.  */
  14689.                     }

  14690.                   op_index = 0;
  14691.                   line_ptr += bytes_read;
  14692.                   address += baseaddr;
  14693.                   address = gdbarch_adjust_dwarf2_line (gdbarch, address, 0);
  14694.                   break;
  14695.                 case DW_LNE_define_file:
  14696.                   {
  14697.                     const char *cur_file;
  14698.                     unsigned int dir_index, mod_time, length;

  14699.                     cur_file = read_direct_string (abfd, line_ptr,
  14700.                                                    &bytes_read);
  14701.                     line_ptr += bytes_read;
  14702.                     dir_index =
  14703.                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14704.                     line_ptr += bytes_read;
  14705.                     mod_time =
  14706.                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14707.                     line_ptr += bytes_read;
  14708.                     length =
  14709.                       read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14710.                     line_ptr += bytes_read;
  14711.                     add_file_name (lh, cur_file, dir_index, mod_time, length);
  14712.                   }
  14713.                   break;
  14714.                 case DW_LNE_set_discriminator:
  14715.                   /* The discriminator is not interesting to the debugger;
  14716.                      just ignore it.  We still need to check its value though:
  14717.                      if there are consecutive entries for the same
  14718.                      (non-prologue) line we want to coalesce them.
  14719.                      PR 17276.  */
  14720.                   discriminator = read_unsigned_leb128 (abfd, line_ptr,
  14721.                                                         &bytes_read);
  14722.                   line_has_non_zero_discriminator |= discriminator != 0;
  14723.                   line_ptr += bytes_read;
  14724.                   break;
  14725.                 default:
  14726.                   complaint (&symfile_complaints,
  14727.                              _("mangled .debug_line section"));
  14728.                   return;
  14729.                 }
  14730.               /* Make sure that we parsed the extended op correctly.  If e.g.
  14731.                  we expected a different address size than the producer used,
  14732.                  we may have read the wrong number of bytes.  */
  14733.               if (line_ptr != extended_end)
  14734.                 {
  14735.                   complaint (&symfile_complaints,
  14736.                              _("mangled .debug_line section"));
  14737.                   return;
  14738.                 }
  14739.               break;
  14740.             case DW_LNS_copy:
  14741.               if (lh->num_file_names < file || file == 0)
  14742.                 dwarf2_debug_line_missing_file_complaint ();
  14743.               else
  14744.                 {
  14745.                   lh->file_names[file - 1].included_p = 1;
  14746.                   if (!decode_for_pst_p && is_stmt)
  14747.                     {
  14748.                       if (last_subfile != current_subfile)
  14749.                         {
  14750.                           dwarf_finish_line (gdbarch, last_subfile,
  14751.                                              address, p_record_line);
  14752.                         }
  14753.                       if (dwarf_record_line_p (line, last_line,
  14754.                                                line_has_non_zero_discriminator,
  14755.                                                last_subfile))
  14756.                         {
  14757.                           dwarf_record_line (gdbarch, current_subfile,
  14758.                                              line, address, p_record_line);
  14759.                         }
  14760.                       last_subfile = current_subfile;
  14761.                       last_line = line;
  14762.                     }
  14763.                 }
  14764.               discriminator = 0;
  14765.               break;
  14766.             case DW_LNS_advance_pc:
  14767.               {
  14768.                 CORE_ADDR adjust
  14769.                   = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14770.                 CORE_ADDR addr_adj;

  14771.                 addr_adj = (((op_index + adjust)
  14772.                              / lh->maximum_ops_per_instruction)
  14773.                             * lh->minimum_instruction_length);
  14774.                 address += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
  14775.                 op_index = ((op_index + adjust)
  14776.                             % lh->maximum_ops_per_instruction);
  14777.                 line_ptr += bytes_read;
  14778.               }
  14779.               break;
  14780.             case DW_LNS_advance_line:
  14781.               {
  14782.                 int line_delta
  14783.                   = read_signed_leb128 (abfd, line_ptr, &bytes_read);

  14784.                 line += line_delta;
  14785.                 if (line_delta != 0)
  14786.                   line_has_non_zero_discriminator = discriminator != 0;
  14787.                 line_ptr += bytes_read;
  14788.               }
  14789.               break;
  14790.             case DW_LNS_set_file:
  14791.               {
  14792.                 /* The arrays lh->include_dirs and lh->file_names are
  14793.                    0-based, but the directory and file name numbers in
  14794.                    the statement program are 1-based.  */
  14795.                 struct file_entry *fe;
  14796.                 const char *dir = NULL;

  14797.                 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14798.                 line_ptr += bytes_read;
  14799.                 if (lh->num_file_names < file || file == 0)
  14800.                   dwarf2_debug_line_missing_file_complaint ();
  14801.                 else
  14802.                   {
  14803.                     fe = &lh->file_names[file - 1];
  14804.                     if (fe->dir_index)
  14805.                       dir = lh->include_dirs[fe->dir_index - 1];
  14806.                     if (!decode_for_pst_p)
  14807.                       {
  14808.                         last_subfile = current_subfile;
  14809.                         line_has_non_zero_discriminator = discriminator != 0;
  14810.                         dwarf2_start_subfile (fe->name, dir);
  14811.                       }
  14812.                   }
  14813.               }
  14814.               break;
  14815.             case DW_LNS_set_column:
  14816.               (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14817.               line_ptr += bytes_read;
  14818.               break;
  14819.             case DW_LNS_negate_stmt:
  14820.               is_stmt = (!is_stmt);
  14821.               break;
  14822.             case DW_LNS_set_basic_block:
  14823.               break;
  14824.             /* Add to the address register of the state machine the
  14825.                address increment value corresponding to special opcode
  14826.                255.  I.e., this value is scaled by the minimum
  14827.                instruction length since special opcode 255 would have
  14828.                scaled the increment.  */
  14829.             case DW_LNS_const_add_pc:
  14830.               {
  14831.                 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
  14832.                 CORE_ADDR addr_adj;

  14833.                 addr_adj = (((op_index + adjust)
  14834.                              / lh->maximum_ops_per_instruction)
  14835.                             * lh->minimum_instruction_length);
  14836.                 address += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
  14837.                 op_index = ((op_index + adjust)
  14838.                             % lh->maximum_ops_per_instruction);
  14839.               }
  14840.               break;
  14841.             case DW_LNS_fixed_advance_pc:
  14842.               {
  14843.                 CORE_ADDR addr_adj;

  14844.                 addr_adj = read_2_bytes (abfd, line_ptr);
  14845.                 address += gdbarch_adjust_dwarf2_line (gdbarch, addr_adj, 1);
  14846.                 op_index = 0;
  14847.                 line_ptr += 2;
  14848.               }
  14849.               break;
  14850.             default:
  14851.               {
  14852.                 /* Unknown standard opcode, ignore it.  */
  14853.                 int i;

  14854.                 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
  14855.                   {
  14856.                     (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
  14857.                     line_ptr += bytes_read;
  14858.                   }
  14859.               }
  14860.             }
  14861.         }
  14862.       if (lh->num_file_names < file || file == 0)
  14863.         dwarf2_debug_line_missing_file_complaint ();
  14864.       else
  14865.         {
  14866.           lh->file_names[file - 1].included_p = 1;
  14867.           if (!decode_for_pst_p)
  14868.             {
  14869.               dwarf_finish_line (gdbarch, current_subfile, address,
  14870.                                  p_record_line);
  14871.             }
  14872.         }
  14873.     }
  14874. }

  14875. /* Decode the Line Number Program (LNP) for the given line_header
  14876.    structure and CU.  The actual information extracted and the type
  14877.    of structures created from the LNP depends on the value of PST.

  14878.    1. If PST is NULL, then this procedure uses the data from the program
  14879.       to create all necessary symbol tables, and their linetables.

  14880.    2. If PST is not NULL, this procedure reads the program to determine
  14881.       the list of files included by the unit represented by PST, and
  14882.       builds all the associated partial symbol tables.

  14883.    COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
  14884.    It is used for relative paths in the line table.
  14885.    NOTE: When processing partial symtabs (pst != NULL),
  14886.    comp_dir == pst->dirname.

  14887.    NOTE: It is important that psymtabs have the same file name (via strcmp)
  14888.    as the corresponding symtab.  Since COMP_DIR is not used in the name of the
  14889.    symtab we don't use it in the name of the psymtabs we create.
  14890.    E.g. expand_line_sal requires this when finding psymtabs to expand.
  14891.    A good testcase for this is mb-inline.exp.

  14892.    LOWPC is the lowest address in CU (or 0 if not known).  */

  14893. static void
  14894. dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
  14895.                     struct dwarf2_cu *cu, struct partial_symtab *pst,
  14896.                     CORE_ADDR lowpc)
  14897. {
  14898.   struct objfile *objfile = cu->objfile;
  14899.   const int decode_for_pst_p = (pst != NULL);

  14900.   dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);

  14901.   if (decode_for_pst_p)
  14902.     {
  14903.       int file_index;

  14904.       /* Now that we're done scanning the Line Header Program, we can
  14905.          create the psymtab of each included file.  */
  14906.       for (file_index = 0; file_index < lh->num_file_names; file_index++)
  14907.         if (lh->file_names[file_index].included_p == 1)
  14908.           {
  14909.             const char *include_name =
  14910.               psymtab_include_file_name (lh, file_index, pst, comp_dir);
  14911.             if (include_name != NULL)
  14912.               dwarf2_create_include_psymtab (include_name, pst, objfile);
  14913.           }
  14914.     }
  14915.   else
  14916.     {
  14917.       /* Make sure a symtab is created for every file, even files
  14918.          which contain only variables (i.e. no code with associated
  14919.          line numbers).  */
  14920.       struct compunit_symtab *cust = buildsym_compunit_symtab ();
  14921.       int i;

  14922.       for (i = 0; i < lh->num_file_names; i++)
  14923.         {
  14924.           const char *dir = NULL;
  14925.           struct file_entry *fe;

  14926.           fe = &lh->file_names[i];
  14927.           if (fe->dir_index)
  14928.             dir = lh->include_dirs[fe->dir_index - 1];
  14929.           dwarf2_start_subfile (fe->name, dir);

  14930.           if (current_subfile->symtab == NULL)
  14931.             {
  14932.               current_subfile->symtab
  14933.                 = allocate_symtab (cust, current_subfile->name);
  14934.             }
  14935.           fe->symtab = current_subfile->symtab;
  14936.         }
  14937.     }
  14938. }

  14939. /* Start a subfile for DWARF.  FILENAME is the name of the file and
  14940.    DIRNAME the name of the source directory which contains FILENAME
  14941.    or NULL if not known.
  14942.    This routine tries to keep line numbers from identical absolute and
  14943.    relative file names in a common subfile.

  14944.    Using the `list' example from the GDB testsuite, which resides in
  14945.    /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
  14946.    of /srcdir/list0.c yields the following debugging information for list0.c:

  14947.    DW_AT_name:          /srcdir/list0.c
  14948.    DW_AT_comp_dir:      /compdir
  14949.    files.files[0].name: list0.h
  14950.    files.files[0].dir:  /srcdir
  14951.    files.files[1].name: list0.c
  14952.    files.files[1].dir:  /srcdir

  14953.    The line number information for list0.c has to end up in a single
  14954.    subfile, so that `break /srcdir/list0.c:1' works as expected.
  14955.    start_subfile will ensure that this happens provided that we pass the
  14956.    concatenation of files.files[1].dir and files.files[1].name as the
  14957.    subfile's name.  */

  14958. static void
  14959. dwarf2_start_subfile (const char *filename, const char *dirname)
  14960. {
  14961.   char *copy = NULL;

  14962.   /* In order not to lose the line information directory,
  14963.      we concatenate it to the filename when it makes sense.
  14964.      Note that the Dwarf3 standard says (speaking of filenames in line
  14965.      information): ``The directory index is ignored for file names
  14966.      that represent full path names''.  Thus ignoring dirname in the
  14967.      `else' branch below isn't an issue.  */

  14968.   if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
  14969.     {
  14970.       copy = concat (dirname, SLASH_STRING, filename, (char *)NULL);
  14971.       filename = copy;
  14972.     }

  14973.   start_subfile (filename);

  14974.   if (copy != NULL)
  14975.     xfree (copy);
  14976. }

  14977. /* Start a symtab for DWARF.
  14978.    NAME, COMP_DIR, LOW_PC are passed to start_symtab.  */

  14979. static struct compunit_symtab *
  14980. dwarf2_start_symtab (struct dwarf2_cu *cu,
  14981.                      const char *name, const char *comp_dir, CORE_ADDR low_pc)
  14982. {
  14983.   struct compunit_symtab *cust
  14984.     = start_symtab (cu->objfile, name, comp_dir, low_pc);

  14985.   record_debugformat ("DWARF 2");
  14986.   record_producer (cu->producer);

  14987.   /* We assume that we're processing GCC output.  */
  14988.   processing_gcc_compilation = 2;

  14989.   cu->processing_has_namespace_info = 0;

  14990.   return cust;
  14991. }

  14992. static void
  14993. var_decode_location (struct attribute *attr, struct symbol *sym,
  14994.                      struct dwarf2_cu *cu)
  14995. {
  14996.   struct objfile *objfile = cu->objfile;
  14997.   struct comp_unit_head *cu_header = &cu->header;

  14998.   /* NOTE drow/2003-01-30: There used to be a comment and some special
  14999.      code here to turn a symbol with DW_AT_external and a
  15000.      SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol.  This was
  15001.      necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
  15002.      with some versions of binutils) where shared libraries could have
  15003.      relocations against symbols in their debug information - the
  15004.      minimal symbol would have the right address, but the debug info
  15005.      would not.  It's no longer necessary, because we will explicitly
  15006.      apply relocations when we read in the debug information now.  */

  15007.   /* A DW_AT_location attribute with no contents indicates that a
  15008.      variable has been optimized away.  */
  15009.   if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
  15010.     {
  15011.       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
  15012.       return;
  15013.     }

  15014.   /* Handle one degenerate form of location expression specially, to
  15015.      preserve GDB's previous behavior when section offsets are
  15016.      specified.  If this is just a DW_OP_addr or DW_OP_GNU_addr_index
  15017.      then mark this symbol as LOC_STATIC.  */

  15018.   if (attr_form_is_block (attr)
  15019.       && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
  15020.            && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
  15021.           || (DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
  15022.               && (DW_BLOCK (attr)->size
  15023.                   == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
  15024.     {
  15025.       unsigned int dummy;

  15026.       if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
  15027.         SYMBOL_VALUE_ADDRESS (sym) =
  15028.           read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
  15029.       else
  15030.         SYMBOL_VALUE_ADDRESS (sym) =
  15031.           read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1, &dummy);
  15032.       SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
  15033.       fixup_symbol_section (sym, objfile);
  15034.       SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
  15035.                                               SYMBOL_SECTION (sym));
  15036.       return;
  15037.     }

  15038.   /* NOTE drow/2002-01-30: It might be worthwhile to have a static
  15039.      expression evaluator, and use LOC_COMPUTED only when necessary
  15040.      (i.e. when the value of a register or memory location is
  15041.      referenced, or a thread-local block, etc.).  Then again, it might
  15042.      not be worthwhile.  I'm assuming that it isn't unless performance
  15043.      or memory numbers show me otherwise.  */

  15044.   dwarf2_symbol_mark_computed (attr, sym, cu, 0);

  15045.   if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
  15046.     cu->has_loclist = 1;
  15047. }

  15048. /* Given a pointer to a DWARF information entry, figure out if we need
  15049.    to make a symbol table entry for it, and if so, create a new entry
  15050.    and return a pointer to it.
  15051.    If TYPE is NULL, determine symbol type from the die, otherwise
  15052.    used the passed type.
  15053.    If SPACE is not NULL, use it to hold the new symbol.  If it is
  15054.    NULL, allocate a new symbol on the objfile's obstack.  */

  15055. static struct symbol *
  15056. new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
  15057.                  struct symbol *space)
  15058. {
  15059.   struct objfile *objfile = cu->objfile;
  15060.   struct gdbarch *gdbarch = get_objfile_arch (objfile);
  15061.   struct symbol *sym = NULL;
  15062.   const char *name;
  15063.   struct attribute *attr = NULL;
  15064.   struct attribute *attr2 = NULL;
  15065.   CORE_ADDR baseaddr;
  15066.   struct pending **list_to_add = NULL;

  15067.   int inlined_func = (die->tag == DW_TAG_inlined_subroutine);

  15068.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  15069.   name = dwarf2_name (die, cu);
  15070.   if (name)
  15071.     {
  15072.       const char *linkagename;
  15073.       int suppress_add = 0;

  15074.       if (space)
  15075.         sym = space;
  15076.       else
  15077.         sym = allocate_symbol (objfile);
  15078.       OBJSTAT (objfile, n_syms++);

  15079.       /* Cache this symbol's name and the name's demangled form (if any).  */
  15080.       SYMBOL_SET_LANGUAGE (sym, cu->language, &objfile->objfile_obstack);
  15081.       linkagename = dwarf2_physname (name, die, cu);
  15082.       SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);

  15083.       /* Fortran does not have mangling standard and the mangling does differ
  15084.          between gfortran, iFort etc.  */
  15085.       if (cu->language == language_fortran
  15086.           && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
  15087.         symbol_set_demangled_name (&(sym->ginfo),
  15088.                                    dwarf2_full_name (name, die, cu),
  15089.                                    NULL);

  15090.       /* Default assumptions.
  15091.          Use the passed type or decode it from the die.  */
  15092.       SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
  15093.       SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
  15094.       if (type != NULL)
  15095.         SYMBOL_TYPE (sym) = type;
  15096.       else
  15097.         SYMBOL_TYPE (sym) = die_type (die, cu);
  15098.       attr = dwarf2_attr (die,
  15099.                           inlined_func ? DW_AT_call_line : DW_AT_decl_line,
  15100.                           cu);
  15101.       if (attr)
  15102.         {
  15103.           SYMBOL_LINE (sym) = DW_UNSND (attr);
  15104.         }

  15105.       attr = dwarf2_attr (die,
  15106.                           inlined_func ? DW_AT_call_file : DW_AT_decl_file,
  15107.                           cu);
  15108.       if (attr)
  15109.         {
  15110.           int file_index = DW_UNSND (attr);

  15111.           if (cu->line_header == NULL
  15112.               || file_index > cu->line_header->num_file_names)
  15113.             complaint (&symfile_complaints,
  15114.                        _("file index out of range"));
  15115.           else if (file_index > 0)
  15116.             {
  15117.               struct file_entry *fe;

  15118.               fe = &cu->line_header->file_names[file_index - 1];
  15119.               symbol_set_symtab (sym, fe->symtab);
  15120.             }
  15121.         }

  15122.       switch (die->tag)
  15123.         {
  15124.         case DW_TAG_label:
  15125.           attr = dwarf2_attr (die, DW_AT_low_pc, cu);
  15126.           if (attr)
  15127.             {
  15128.               CORE_ADDR addr;

  15129.               addr = attr_value_as_address (attr);
  15130.               addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
  15131.               SYMBOL_VALUE_ADDRESS (sym) = addr;
  15132.             }
  15133.           SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
  15134.           SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
  15135.           SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
  15136.           add_symbol_to_list (sym, cu->list_in_scope);
  15137.           break;
  15138.         case DW_TAG_subprogram:
  15139.           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
  15140.              finish_block.  */
  15141.           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
  15142.           attr2 = dwarf2_attr (die, DW_AT_external, cu);
  15143.           if ((attr2 && (DW_UNSND (attr2) != 0))
  15144.               || cu->language == language_ada)
  15145.             {
  15146.               /* Subprograms marked external are stored as a global symbol.
  15147.                  Ada subprograms, whether marked external or not, are always
  15148.                  stored as a global symbol, because we want to be able to
  15149.                  access them globally.  For instance, we want to be able
  15150.                  to break on a nested subprogram without having to
  15151.                  specify the context.  */
  15152.               list_to_add = &global_symbols;
  15153.             }
  15154.           else
  15155.             {
  15156.               list_to_add = cu->list_in_scope;
  15157.             }
  15158.           break;
  15159.         case DW_TAG_inlined_subroutine:
  15160.           /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
  15161.              finish_block.  */
  15162.           SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
  15163.           SYMBOL_INLINED (sym) = 1;
  15164.           list_to_add = cu->list_in_scope;
  15165.           break;
  15166.         case DW_TAG_template_value_param:
  15167.           suppress_add = 1;
  15168.           /* Fall through.  */
  15169.         case DW_TAG_constant:
  15170.         case DW_TAG_variable:
  15171.         case DW_TAG_member:
  15172.           /* Compilation with minimal debug info may result in
  15173.              variables with missing type entries.  Change the
  15174.              misleading `void' type to something sensible.  */
  15175.           if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
  15176.             SYMBOL_TYPE (sym)
  15177.               = objfile_type (objfile)->nodebug_data_symbol;

  15178.           attr = dwarf2_attr (die, DW_AT_const_value, cu);
  15179.           /* In the case of DW_TAG_member, we should only be called for
  15180.              static const members.  */
  15181.           if (die->tag == DW_TAG_member)
  15182.             {
  15183.               /* dwarf2_add_field uses die_is_declaration,
  15184.                  so we do the same.  */
  15185.               gdb_assert (die_is_declaration (die, cu));
  15186.               gdb_assert (attr);
  15187.             }
  15188.           if (attr)
  15189.             {
  15190.               dwarf2_const_value (attr, sym, cu);
  15191.               attr2 = dwarf2_attr (die, DW_AT_external, cu);
  15192.               if (!suppress_add)
  15193.                 {
  15194.                   if (attr2 && (DW_UNSND (attr2) != 0))
  15195.                     list_to_add = &global_symbols;
  15196.                   else
  15197.                     list_to_add = cu->list_in_scope;
  15198.                 }
  15199.               break;
  15200.             }
  15201.           attr = dwarf2_attr (die, DW_AT_location, cu);
  15202.           if (attr)
  15203.             {
  15204.               var_decode_location (attr, sym, cu);
  15205.               attr2 = dwarf2_attr (die, DW_AT_external, cu);

  15206.               /* Fortran explicitly imports any global symbols to the local
  15207.                  scope by DW_TAG_common_block.  */
  15208.               if (cu->language == language_fortran && die->parent
  15209.                   && die->parent->tag == DW_TAG_common_block)
  15210.                 attr2 = NULL;

  15211.               if (SYMBOL_CLASS (sym) == LOC_STATIC
  15212.                   && SYMBOL_VALUE_ADDRESS (sym) == 0
  15213.                   && !dwarf2_per_objfile->has_section_at_zero)
  15214.                 {
  15215.                   /* When a static variable is eliminated by the linker,
  15216.                      the corresponding debug information is not stripped
  15217.                      out, but the variable address is set to null;
  15218.                      do not add such variables into symbol table.  */
  15219.                 }
  15220.               else if (attr2 && (DW_UNSND (attr2) != 0))
  15221.                 {
  15222.                   /* Workaround gfortran PR debug/40040 - it uses
  15223.                      DW_AT_location for variables in -fPIC libraries which may
  15224.                      get overriden by other libraries/executable and get
  15225.                      a different address.  Resolve it by the minimal symbol
  15226.                      which may come from inferior's executable using copy
  15227.                      relocation.  Make this workaround only for gfortran as for
  15228.                      other compilers GDB cannot guess the minimal symbol
  15229.                      Fortran mangling kind.  */
  15230.                   if (cu->language == language_fortran && die->parent
  15231.                       && die->parent->tag == DW_TAG_module
  15232.                       && cu->producer
  15233.                       && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
  15234.                     SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;

  15235.                   /* A variable with DW_AT_external is never static,
  15236.                      but it may be block-scoped.  */
  15237.                   list_to_add = (cu->list_in_scope == &file_symbols
  15238.                                  ? &global_symbols : cu->list_in_scope);
  15239.                 }
  15240.               else
  15241.                 list_to_add = cu->list_in_scope;
  15242.             }
  15243.           else
  15244.             {
  15245.               /* We do not know the address of this symbol.
  15246.                  If it is an external symbol and we have type information
  15247.                  for it, enter the symbol as a LOC_UNRESOLVED symbol.
  15248.                  The address of the variable will then be determined from
  15249.                  the minimal symbol table whenever the variable is
  15250.                  referenced.  */
  15251.               attr2 = dwarf2_attr (die, DW_AT_external, cu);

  15252.               /* Fortran explicitly imports any global symbols to the local
  15253.                  scope by DW_TAG_common_block.  */
  15254.               if (cu->language == language_fortran && die->parent
  15255.                   && die->parent->tag == DW_TAG_common_block)
  15256.                 {
  15257.                   /* SYMBOL_CLASS doesn't matter here because
  15258.                      read_common_block is going to reset it.  */
  15259.                   if (!suppress_add)
  15260.                     list_to_add = cu->list_in_scope;
  15261.                 }
  15262.               else if (attr2 && (DW_UNSND (attr2) != 0)
  15263.                        && dwarf2_attr (die, DW_AT_type, cu) != NULL)
  15264.                 {
  15265.                   /* A variable with DW_AT_external is never static, but it
  15266.                      may be block-scoped.  */
  15267.                   list_to_add = (cu->list_in_scope == &file_symbols
  15268.                                  ? &global_symbols : cu->list_in_scope);

  15269.                   SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
  15270.                 }
  15271.               else if (!die_is_declaration (die, cu))
  15272.                 {
  15273.                   /* Use the default LOC_OPTIMIZED_OUT class.  */
  15274.                   gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
  15275.                   if (!suppress_add)
  15276.                     list_to_add = cu->list_in_scope;
  15277.                 }
  15278.             }
  15279.           break;
  15280.         case DW_TAG_formal_parameter:
  15281.           /* If we are inside a function, mark this as an argument.  If
  15282.              not, we might be looking at an argument to an inlined function
  15283.              when we do not have enough information to show inlined frames;
  15284.              pretend it's a local variable in that case so that the user can
  15285.              still see it.  */
  15286.           if (context_stack_depth > 0
  15287.               && context_stack[context_stack_depth - 1].name != NULL)
  15288.             SYMBOL_IS_ARGUMENT (sym) = 1;
  15289.           attr = dwarf2_attr (die, DW_AT_location, cu);
  15290.           if (attr)
  15291.             {
  15292.               var_decode_location (attr, sym, cu);
  15293.             }
  15294.           attr = dwarf2_attr (die, DW_AT_const_value, cu);
  15295.           if (attr)
  15296.             {
  15297.               dwarf2_const_value (attr, sym, cu);
  15298.             }

  15299.           list_to_add = cu->list_in_scope;
  15300.           break;
  15301.         case DW_TAG_unspecified_parameters:
  15302.           /* From varargs functions; gdb doesn't seem to have any
  15303.              interest in this information, so just ignore it for now.
  15304.              (FIXME?) */
  15305.           break;
  15306.         case DW_TAG_template_type_param:
  15307.           suppress_add = 1;
  15308.           /* Fall through.  */
  15309.         case DW_TAG_class_type:
  15310.         case DW_TAG_interface_type:
  15311.         case DW_TAG_structure_type:
  15312.         case DW_TAG_union_type:
  15313.         case DW_TAG_set_type:
  15314.         case DW_TAG_enumeration_type:
  15315.           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  15316.           SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;

  15317.           {
  15318.             /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
  15319.                really ever be static objects: otherwise, if you try
  15320.                to, say, break of a class's method and you're in a file
  15321.                which doesn't mention that class, it won't work unless
  15322.                the check for all static symbols in lookup_symbol_aux
  15323.                saves you.  See the OtherFileClass tests in
  15324.                gdb.c++/namespace.exp.  */

  15325.             if (!suppress_add)
  15326.               {
  15327.                 list_to_add = (cu->list_in_scope == &file_symbols
  15328.                                && (cu->language == language_cplus
  15329.                                    || cu->language == language_java)
  15330.                                ? &global_symbols : cu->list_in_scope);

  15331.                 /* The semantics of C++ state that "struct foo {
  15332.                    ... }" also defines a typedef for "foo".  A Java
  15333.                    class declaration also defines a typedef for the
  15334.                    class.  */
  15335.                 if (cu->language == language_cplus
  15336.                     || cu->language == language_java
  15337.                     || cu->language == language_ada)
  15338.                   {
  15339.                     /* The symbol's name is already allocated along
  15340.                        with this objfile, so we don't need to
  15341.                        duplicate it for the type.  */
  15342.                     if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
  15343.                       TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
  15344.                   }
  15345.               }
  15346.           }
  15347.           break;
  15348.         case DW_TAG_typedef:
  15349.           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  15350.           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
  15351.           list_to_add = cu->list_in_scope;
  15352.           break;
  15353.         case DW_TAG_base_type:
  15354.         case DW_TAG_subrange_type:
  15355.           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  15356.           SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
  15357.           list_to_add = cu->list_in_scope;
  15358.           break;
  15359.         case DW_TAG_enumerator:
  15360.           attr = dwarf2_attr (die, DW_AT_const_value, cu);
  15361.           if (attr)
  15362.             {
  15363.               dwarf2_const_value (attr, sym, cu);
  15364.             }
  15365.           {
  15366.             /* NOTE: carlton/2003-11-10: See comment above in the
  15367.                DW_TAG_class_type, etc. block.  */

  15368.             list_to_add = (cu->list_in_scope == &file_symbols
  15369.                            && (cu->language == language_cplus
  15370.                                || cu->language == language_java)
  15371.                            ? &global_symbols : cu->list_in_scope);
  15372.           }
  15373.           break;
  15374.         case DW_TAG_imported_declaration:
  15375.         case DW_TAG_namespace:
  15376.           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  15377.           list_to_add = &global_symbols;
  15378.           break;
  15379.         case DW_TAG_module:
  15380.           SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
  15381.           SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
  15382.           list_to_add = &global_symbols;
  15383.           break;
  15384.         case DW_TAG_common_block:
  15385.           SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
  15386.           SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
  15387.           add_symbol_to_list (sym, cu->list_in_scope);
  15388.           break;
  15389.         default:
  15390.           /* Not a tag we recognize.  Hopefully we aren't processing
  15391.              trash data, but since we must specifically ignore things
  15392.              we don't recognize, there is nothing else we should do at
  15393.              this point.  */
  15394.           complaint (&symfile_complaints, _("unsupported tag: '%s'"),
  15395.                      dwarf_tag_name (die->tag));
  15396.           break;
  15397.         }

  15398.       if (suppress_add)
  15399.         {
  15400.           sym->hash_next = objfile->template_symbols;
  15401.           objfile->template_symbols = sym;
  15402.           list_to_add = NULL;
  15403.         }

  15404.       if (list_to_add != NULL)
  15405.         add_symbol_to_list (sym, list_to_add);

  15406.       /* For the benefit of old versions of GCC, check for anonymous
  15407.          namespaces based on the demangled name.  */
  15408.       if (!cu->processing_has_namespace_info
  15409.           && cu->language == language_cplus)
  15410.         cp_scan_for_anonymous_namespaces (sym, objfile);
  15411.     }
  15412.   return (sym);
  15413. }

  15414. /* A wrapper for new_symbol_full that always allocates a new symbol.  */

  15415. static struct symbol *
  15416. new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
  15417. {
  15418.   return new_symbol_full (die, type, cu, NULL);
  15419. }

  15420. /* Given an attr with a DW_FORM_dataN value in host byte order,
  15421.    zero-extend it as appropriate for the symbol's type.  The DWARF
  15422.    standard (v4) is not entirely clear about the meaning of using
  15423.    DW_FORM_dataN for a constant with a signed type, where the type is
  15424.    wider than the data.  The conclusion of a discussion on the DWARF
  15425.    list was that this is unspecified.  We choose to always zero-extend
  15426.    because that is the interpretation long in use by GCC.  */

  15427. static gdb_byte *
  15428. dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
  15429.                          struct dwarf2_cu *cu, LONGEST *value, int bits)
  15430. {
  15431.   struct objfile *objfile = cu->objfile;
  15432.   enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
  15433.                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
  15434.   LONGEST l = DW_UNSND (attr);

  15435.   if (bits < sizeof (*value) * 8)
  15436.     {
  15437.       l &= ((LONGEST) 1 << bits) - 1;
  15438.       *value = l;
  15439.     }
  15440.   else if (bits == sizeof (*value) * 8)
  15441.     *value = l;
  15442.   else
  15443.     {
  15444.       gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
  15445.       store_unsigned_integer (bytes, bits / 8, byte_order, l);
  15446.       return bytes;
  15447.     }

  15448.   return NULL;
  15449. }

  15450. /* Read a constant value from an attribute.  Either set *VALUE, or if
  15451.    the value does not fit in *VALUE, set *BYTES - either already
  15452.    allocated on the objfile obstack, or newly allocated on OBSTACK,
  15453.    or, set *BATON, if we translated the constant to a location
  15454.    expression.  */

  15455. static void
  15456. dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
  15457.                          const char *name, struct obstack *obstack,
  15458.                          struct dwarf2_cu *cu,
  15459.                          LONGEST *value, const gdb_byte **bytes,
  15460.                          struct dwarf2_locexpr_baton **baton)
  15461. {
  15462.   struct objfile *objfile = cu->objfile;
  15463.   struct comp_unit_head *cu_header = &cu->header;
  15464.   struct dwarf_block *blk;
  15465.   enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
  15466.                                 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);

  15467.   *value = 0;
  15468.   *bytes = NULL;
  15469.   *baton = NULL;

  15470.   switch (attr->form)
  15471.     {
  15472.     case DW_FORM_addr:
  15473.     case DW_FORM_GNU_addr_index:
  15474.       {
  15475.         gdb_byte *data;

  15476.         if (TYPE_LENGTH (type) != cu_header->addr_size)
  15477.           dwarf2_const_value_length_mismatch_complaint (name,
  15478.                                                         cu_header->addr_size,
  15479.                                                         TYPE_LENGTH (type));
  15480.         /* Symbols of this form are reasonably rare, so we just
  15481.            piggyback on the existing location code rather than writing
  15482.            a new implementation of symbol_computed_ops.  */
  15483.         *baton = obstack_alloc (obstack, sizeof (struct dwarf2_locexpr_baton));
  15484.         (*baton)->per_cu = cu->per_cu;
  15485.         gdb_assert ((*baton)->per_cu);

  15486.         (*baton)->size = 2 + cu_header->addr_size;
  15487.         data = obstack_alloc (obstack, (*baton)->size);
  15488.         (*baton)->data = data;

  15489.         data[0] = DW_OP_addr;
  15490.         store_unsigned_integer (&data[1], cu_header->addr_size,
  15491.                                 byte_order, DW_ADDR (attr));
  15492.         data[cu_header->addr_size + 1] = DW_OP_stack_value;
  15493.       }
  15494.       break;
  15495.     case DW_FORM_string:
  15496.     case DW_FORM_strp:
  15497.     case DW_FORM_GNU_str_index:
  15498.     case DW_FORM_GNU_strp_alt:
  15499.       /* DW_STRING is already allocated on the objfile obstack, point
  15500.          directly to it.  */
  15501.       *bytes = (const gdb_byte *) DW_STRING (attr);
  15502.       break;
  15503.     case DW_FORM_block1:
  15504.     case DW_FORM_block2:
  15505.     case DW_FORM_block4:
  15506.     case DW_FORM_block:
  15507.     case DW_FORM_exprloc:
  15508.       blk = DW_BLOCK (attr);
  15509.       if (TYPE_LENGTH (type) != blk->size)
  15510.         dwarf2_const_value_length_mismatch_complaint (name, blk->size,
  15511.                                                       TYPE_LENGTH (type));
  15512.       *bytes = blk->data;
  15513.       break;

  15514.       /* The DW_AT_const_value attributes are supposed to carry the
  15515.          symbol's value "represented as it would be on the target
  15516.          architecture."  By the time we get here, it's already been
  15517.          converted to host endianness, so we just need to sign- or
  15518.          zero-extend it as appropriate.  */
  15519.     case DW_FORM_data1:
  15520.       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
  15521.       break;
  15522.     case DW_FORM_data2:
  15523.       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
  15524.       break;
  15525.     case DW_FORM_data4:
  15526.       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
  15527.       break;
  15528.     case DW_FORM_data8:
  15529.       *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
  15530.       break;

  15531.     case DW_FORM_sdata:
  15532.       *value = DW_SND (attr);
  15533.       break;

  15534.     case DW_FORM_udata:
  15535.       *value = DW_UNSND (attr);
  15536.       break;

  15537.     default:
  15538.       complaint (&symfile_complaints,
  15539.                  _("unsupported const value attribute form: '%s'"),
  15540.                  dwarf_form_name (attr->form));
  15541.       *value = 0;
  15542.       break;
  15543.     }
  15544. }


  15545. /* Copy constant value from an attribute to a symbol.  */

  15546. static void
  15547. dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
  15548.                     struct dwarf2_cu *cu)
  15549. {
  15550.   struct objfile *objfile = cu->objfile;
  15551.   struct comp_unit_head *cu_header = &cu->header;
  15552.   LONGEST value;
  15553.   const gdb_byte *bytes;
  15554.   struct dwarf2_locexpr_baton *baton;

  15555.   dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
  15556.                            SYMBOL_PRINT_NAME (sym),
  15557.                            &objfile->objfile_obstack, cu,
  15558.                            &value, &bytes, &baton);

  15559.   if (baton != NULL)
  15560.     {
  15561.       SYMBOL_LOCATION_BATON (sym) = baton;
  15562.       SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
  15563.     }
  15564.   else if (bytes != NULL)
  15565.      {
  15566.       SYMBOL_VALUE_BYTES (sym) = bytes;
  15567.       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
  15568.     }
  15569.   else
  15570.     {
  15571.       SYMBOL_VALUE (sym) = value;
  15572.       SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
  15573.     }
  15574. }

  15575. /* Return the type of the die in question using its DW_AT_type attribute.  */

  15576. static struct type *
  15577. die_type (struct die_info *die, struct dwarf2_cu *cu)
  15578. {
  15579.   struct attribute *type_attr;

  15580.   type_attr = dwarf2_attr (die, DW_AT_type, cu);
  15581.   if (!type_attr)
  15582.     {
  15583.       /* A missing DW_AT_type represents a void type.  */
  15584.       return objfile_type (cu->objfile)->builtin_void;
  15585.     }

  15586.   return lookup_die_type (die, type_attr, cu);
  15587. }

  15588. /* True iff CU's producer generates GNAT Ada auxiliary information
  15589.    that allows to find parallel types through that information instead
  15590.    of having to do expensive parallel lookups by type name.  */

  15591. static int
  15592. need_gnat_info (struct dwarf2_cu *cu)
  15593. {
  15594.   /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
  15595.      of GNAT produces this auxiliary information, without any indication
  15596.      that it is produced.  Part of enhancing the FSF version of GNAT
  15597.      to produce that information will be to put in place an indicator
  15598.      that we can use in order to determine whether the descriptive type
  15599.      info is available or not.  One suggestion that has been made is
  15600.      to use a new attribute, attached to the CU die.  For now, assume
  15601.      that the descriptive type info is not available.  */
  15602.   return 0;
  15603. }

  15604. /* Return the auxiliary type of the die in question using its
  15605.    DW_AT_GNAT_descriptive_type attribute.  Returns NULL if the
  15606.    attribute is not present.  */

  15607. static struct type *
  15608. die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
  15609. {
  15610.   struct attribute *type_attr;

  15611.   type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
  15612.   if (!type_attr)
  15613.     return NULL;

  15614.   return lookup_die_type (die, type_attr, cu);
  15615. }

  15616. /* If DIE has a descriptive_type attribute, then set the TYPE's
  15617.    descriptive type accordingly.  */

  15618. static void
  15619. set_descriptive_type (struct type *type, struct die_info *die,
  15620.                       struct dwarf2_cu *cu)
  15621. {
  15622.   struct type *descriptive_type = die_descriptive_type (die, cu);

  15623.   if (descriptive_type)
  15624.     {
  15625.       ALLOCATE_GNAT_AUX_TYPE (type);
  15626.       TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
  15627.     }
  15628. }

  15629. /* Return the containing type of the die in question using its
  15630.    DW_AT_containing_type attribute.  */

  15631. static struct type *
  15632. die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
  15633. {
  15634.   struct attribute *type_attr;

  15635.   type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
  15636.   if (!type_attr)
  15637.     error (_("Dwarf Error: Problem turning containing type into gdb type "
  15638.              "[in module %s]"), objfile_name (cu->objfile));

  15639.   return lookup_die_type (die, type_attr, cu);
  15640. }

  15641. /* Return an error marker type to use for the ill formed type in DIE/CU.  */

  15642. static struct type *
  15643. build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
  15644. {
  15645.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  15646.   char *message, *saved;

  15647.   message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
  15648.                         objfile_name (objfile),
  15649.                         cu->header.offset.sect_off,
  15650.                         die->offset.sect_off);
  15651.   saved = obstack_copy0 (&objfile->objfile_obstack,
  15652.                          message, strlen (message));
  15653.   xfree (message);

  15654.   return init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
  15655. }

  15656. /* Look up the type of DIE in CU using its type attribute ATTR.
  15657.    ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
  15658.    DW_AT_containing_type.
  15659.    If there is no type substitute an error marker.  */

  15660. static struct type *
  15661. lookup_die_type (struct die_info *die, const struct attribute *attr,
  15662.                  struct dwarf2_cu *cu)
  15663. {
  15664.   struct objfile *objfile = cu->objfile;
  15665.   struct type *this_type;

  15666.   gdb_assert (attr->name == DW_AT_type
  15667.               || attr->name == DW_AT_GNAT_descriptive_type
  15668.               || attr->name == DW_AT_containing_type);

  15669.   /* First see if we have it cached.  */

  15670.   if (attr->form == DW_FORM_GNU_ref_alt)
  15671.     {
  15672.       struct dwarf2_per_cu_data *per_cu;
  15673.       sect_offset offset = dwarf2_get_ref_die_offset (attr);

  15674.       per_cu = dwarf2_find_containing_comp_unit (offset, 1, cu->objfile);
  15675.       this_type = get_die_type_at_offset (offset, per_cu);
  15676.     }
  15677.   else if (attr_form_is_ref (attr))
  15678.     {
  15679.       sect_offset offset = dwarf2_get_ref_die_offset (attr);

  15680.       this_type = get_die_type_at_offset (offset, cu->per_cu);
  15681.     }
  15682.   else if (attr->form == DW_FORM_ref_sig8)
  15683.     {
  15684.       ULONGEST signature = DW_SIGNATURE (attr);

  15685.       return get_signatured_type (die, signature, cu);
  15686.     }
  15687.   else
  15688.     {
  15689.       complaint (&symfile_complaints,
  15690.                  _("Dwarf Error: Bad type attribute %s in DIE"
  15691.                    " at 0x%x [in module %s]"),
  15692.                  dwarf_attr_name (attr->name), die->offset.sect_off,
  15693.                  objfile_name (objfile));
  15694.       return build_error_marker_type (cu, die);
  15695.     }

  15696.   /* If not cached we need to read it in.  */

  15697.   if (this_type == NULL)
  15698.     {
  15699.       struct die_info *type_die = NULL;
  15700.       struct dwarf2_cu *type_cu = cu;

  15701.       if (attr_form_is_ref (attr))
  15702.         type_die = follow_die_ref (die, attr, &type_cu);
  15703.       if (type_die == NULL)
  15704.         return build_error_marker_type (cu, die);
  15705.       /* If we find the type now, it's probably because the type came
  15706.          from an inter-CU reference and the type's CU got expanded before
  15707.          ours.  */
  15708.       this_type = read_type_die (type_die, type_cu);
  15709.     }

  15710.   /* If we still don't have a type use an error marker.  */

  15711.   if (this_type == NULL)
  15712.     return build_error_marker_type (cu, die);

  15713.   return this_type;
  15714. }

  15715. /* Return the type in DIE, CU.
  15716.    Returns NULL for invalid types.

  15717.    This first does a lookup in die_type_hash,
  15718.    and only reads the die in if necessary.

  15719.    NOTE: This can be called when reading in partial or full symbols.  */

  15720. static struct type *
  15721. read_type_die (struct die_info *die, struct dwarf2_cu *cu)
  15722. {
  15723.   struct type *this_type;

  15724.   this_type = get_die_type (die, cu);
  15725.   if (this_type)
  15726.     return this_type;

  15727.   return read_type_die_1 (die, cu);
  15728. }

  15729. /* Read the type in DIE, CU.
  15730.    Returns NULL for invalid types.  */

  15731. static struct type *
  15732. read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
  15733. {
  15734.   struct type *this_type = NULL;

  15735.   switch (die->tag)
  15736.     {
  15737.     case DW_TAG_class_type:
  15738.     case DW_TAG_interface_type:
  15739.     case DW_TAG_structure_type:
  15740.     case DW_TAG_union_type:
  15741.       this_type = read_structure_type (die, cu);
  15742.       break;
  15743.     case DW_TAG_enumeration_type:
  15744.       this_type = read_enumeration_type (die, cu);
  15745.       break;
  15746.     case DW_TAG_subprogram:
  15747.     case DW_TAG_subroutine_type:
  15748.     case DW_TAG_inlined_subroutine:
  15749.       this_type = read_subroutine_type (die, cu);
  15750.       break;
  15751.     case DW_TAG_array_type:
  15752.       this_type = read_array_type (die, cu);
  15753.       break;
  15754.     case DW_TAG_set_type:
  15755.       this_type = read_set_type (die, cu);
  15756.       break;
  15757.     case DW_TAG_pointer_type:
  15758.       this_type = read_tag_pointer_type (die, cu);
  15759.       break;
  15760.     case DW_TAG_ptr_to_member_type:
  15761.       this_type = read_tag_ptr_to_member_type (die, cu);
  15762.       break;
  15763.     case DW_TAG_reference_type:
  15764.       this_type = read_tag_reference_type (die, cu);
  15765.       break;
  15766.     case DW_TAG_const_type:
  15767.       this_type = read_tag_const_type (die, cu);
  15768.       break;
  15769.     case DW_TAG_volatile_type:
  15770.       this_type = read_tag_volatile_type (die, cu);
  15771.       break;
  15772.     case DW_TAG_restrict_type:
  15773.       this_type = read_tag_restrict_type (die, cu);
  15774.       break;
  15775.     case DW_TAG_string_type:
  15776.       this_type = read_tag_string_type (die, cu);
  15777.       break;
  15778.     case DW_TAG_typedef:
  15779.       this_type = read_typedef (die, cu);
  15780.       break;
  15781.     case DW_TAG_subrange_type:
  15782.       this_type = read_subrange_type (die, cu);
  15783.       break;
  15784.     case DW_TAG_base_type:
  15785.       this_type = read_base_type (die, cu);
  15786.       break;
  15787.     case DW_TAG_unspecified_type:
  15788.       this_type = read_unspecified_type (die, cu);
  15789.       break;
  15790.     case DW_TAG_namespace:
  15791.       this_type = read_namespace_type (die, cu);
  15792.       break;
  15793.     case DW_TAG_module:
  15794.       this_type = read_module_type (die, cu);
  15795.       break;
  15796.     default:
  15797.       complaint (&symfile_complaints,
  15798.                  _("unexpected tag in read_type_die: '%s'"),
  15799.                  dwarf_tag_name (die->tag));
  15800.       break;
  15801.     }

  15802.   return this_type;
  15803. }

  15804. /* See if we can figure out if the class lives in a namespace.  We do
  15805.    this by looking for a member function; its demangled name will
  15806.    contain namespace info, if there is any.
  15807.    Return the computed name or NULL.
  15808.    Space for the result is allocated on the objfile's obstack.
  15809.    This is the full-die version of guess_partial_die_structure_name.
  15810.    In this case we know DIE has no useful parent.  */

  15811. static char *
  15812. guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
  15813. {
  15814.   struct die_info *spec_die;
  15815.   struct dwarf2_cu *spec_cu;
  15816.   struct die_info *child;

  15817.   spec_cu = cu;
  15818.   spec_die = die_specification (die, &spec_cu);
  15819.   if (spec_die != NULL)
  15820.     {
  15821.       die = spec_die;
  15822.       cu = spec_cu;
  15823.     }

  15824.   for (child = die->child;
  15825.        child != NULL;
  15826.        child = child->sibling)
  15827.     {
  15828.       if (child->tag == DW_TAG_subprogram)
  15829.         {
  15830.           struct attribute *attr;

  15831.           attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
  15832.           if (attr == NULL)
  15833.             attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
  15834.           if (attr != NULL)
  15835.             {
  15836.               char *actual_name
  15837.                 = language_class_name_from_physname (cu->language_defn,
  15838.                                                      DW_STRING (attr));
  15839.               char *name = NULL;

  15840.               if (actual_name != NULL)
  15841.                 {
  15842.                   const char *die_name = dwarf2_name (die, cu);

  15843.                   if (die_name != NULL
  15844.                       && strcmp (die_name, actual_name) != 0)
  15845.                     {
  15846.                       /* Strip off the class name from the full name.
  15847.                          We want the prefix.  */
  15848.                       int die_name_len = strlen (die_name);
  15849.                       int actual_name_len = strlen (actual_name);

  15850.                       /* Test for '::' as a sanity check.  */
  15851.                       if (actual_name_len > die_name_len + 2
  15852.                           && actual_name[actual_name_len
  15853.                                          - die_name_len - 1] == ':')
  15854.                         name =
  15855.                           obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
  15856.                                          actual_name,
  15857.                                          actual_name_len - die_name_len - 2);
  15858.                     }
  15859.                 }
  15860.               xfree (actual_name);
  15861.               return name;
  15862.             }
  15863.         }
  15864.     }

  15865.   return NULL;
  15866. }

  15867. /* GCC might emit a nameless typedef that has a linkage name.  Determine the
  15868.    prefix part in such case.  See
  15869.    http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */

  15870. static char *
  15871. anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
  15872. {
  15873.   struct attribute *attr;
  15874.   char *base;

  15875.   if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
  15876.       && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
  15877.     return NULL;

  15878.   attr = dwarf2_attr (die, DW_AT_name, cu);
  15879.   if (attr != NULL && DW_STRING (attr) != NULL)
  15880.     return NULL;

  15881.   attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
  15882.   if (attr == NULL)
  15883.     attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
  15884.   if (attr == NULL || DW_STRING (attr) == NULL)
  15885.     return NULL;

  15886.   /* dwarf2_name had to be already called.  */
  15887.   gdb_assert (DW_STRING_IS_CANONICAL (attr));

  15888.   /* Strip the base name, keep any leading namespaces/classes.  */
  15889.   base = strrchr (DW_STRING (attr), ':');
  15890.   if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
  15891.     return "";

  15892.   return obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
  15893.                         DW_STRING (attr), &base[-1] - DW_STRING (attr));
  15894. }

  15895. /* Return the name of the namespace/class that DIE is defined within,
  15896.    or "" if we can't tell.  The caller should not xfree the result.

  15897.    For example, if we're within the method foo() in the following
  15898.    code:

  15899.    namespace N {
  15900.      class C {
  15901.        void foo () {
  15902.        }
  15903.      };
  15904.    }

  15905.    then determine_prefix on foo's die will return "N::C".  */

  15906. static const char *
  15907. determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
  15908. {
  15909.   struct die_info *parent, *spec_die;
  15910.   struct dwarf2_cu *spec_cu;
  15911.   struct type *parent_type;
  15912.   char *retval;

  15913.   if (cu->language != language_cplus && cu->language != language_java
  15914.       && cu->language != language_fortran)
  15915.     return "";

  15916.   retval = anonymous_struct_prefix (die, cu);
  15917.   if (retval)
  15918.     return retval;

  15919.   /* We have to be careful in the presence of DW_AT_specification.
  15920.      For example, with GCC 3.4, given the code

  15921.      namespace N {
  15922.        void foo() {
  15923.          // Definition of N::foo.
  15924.        }
  15925.      }

  15926.      then we'll have a tree of DIEs like this:

  15927.      1: DW_TAG_compile_unit
  15928.        2: DW_TAG_namespace        // N
  15929.          3: DW_TAG_subprogram     // declaration of N::foo
  15930.        4: DW_TAG_subprogram       // definition of N::foo
  15931.             DW_AT_specification   // refers to die #3

  15932.      Thus, when processing die #4, we have to pretend that we're in
  15933.      the context of its DW_AT_specification, namely the contex of die
  15934.      #3.  */
  15935.   spec_cu = cu;
  15936.   spec_die = die_specification (die, &spec_cu);
  15937.   if (spec_die == NULL)
  15938.     parent = die->parent;
  15939.   else
  15940.     {
  15941.       parent = spec_die->parent;
  15942.       cu = spec_cu;
  15943.     }

  15944.   if (parent == NULL)
  15945.     return "";
  15946.   else if (parent->building_fullname)
  15947.     {
  15948.       const char *name;
  15949.       const char *parent_name;

  15950.       /* It has been seen on RealView 2.2 built binaries,
  15951.          DW_TAG_template_type_param types actually _defined_ as
  15952.          children of the parent class:

  15953.          enum E {};
  15954.          template class <class Enum> Class{};
  15955.          Class<enum E> class_e;

  15956.          1: DW_TAG_class_type (Class)
  15957.            2: DW_TAG_enumeration_type (E)
  15958.              3: DW_TAG_enumerator (enum1:0)
  15959.              3: DW_TAG_enumerator (enum2:1)
  15960.              ...
  15961.            2: DW_TAG_template_type_param
  15962.               DW_AT_type  DW_FORM_ref_udata (E)

  15963.          Besides being broken debug info, it can put GDB into an
  15964.          infinite loop.  Consider:

  15965.          When we're building the full name for Class<E>, we'll start
  15966.          at Class, and go look over its template type parameters,
  15967.          finding E.  We'll then try to build the full name of E, and
  15968.          reach here.  We're now trying to build the full name of E,
  15969.          and look over the parent DIE for containing scope.  In the
  15970.          broken case, if we followed the parent DIE of E, we'd again
  15971.          find Class, and once again go look at its template type
  15972.          arguments, etc., etc.  Simply don't consider such parent die
  15973.          as source-level parent of this die (it can't be, the language
  15974.          doesn't allow it), and break the loop here.  */
  15975.       name = dwarf2_name (die, cu);
  15976.       parent_name = dwarf2_name (parent, cu);
  15977.       complaint (&symfile_complaints,
  15978.                  _("template param type '%s' defined within parent '%s'"),
  15979.                  name ? name : "<unknown>",
  15980.                  parent_name ? parent_name : "<unknown>");
  15981.       return "";
  15982.     }
  15983.   else
  15984.     switch (parent->tag)
  15985.       {
  15986.       case DW_TAG_namespace:
  15987.         parent_type = read_type_die (parent, cu);
  15988.         /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
  15989.            DW_TAG_namespace DIEs with a name of "::" for the global namespace.
  15990.            Work around this problem here.  */
  15991.         if (cu->language == language_cplus
  15992.             && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
  15993.           return "";
  15994.         /* We give a name to even anonymous namespaces.  */
  15995.         return TYPE_TAG_NAME (parent_type);
  15996.       case DW_TAG_class_type:
  15997.       case DW_TAG_interface_type:
  15998.       case DW_TAG_structure_type:
  15999.       case DW_TAG_union_type:
  16000.       case DW_TAG_module:
  16001.         parent_type = read_type_die (parent, cu);
  16002.         if (TYPE_TAG_NAME (parent_type) != NULL)
  16003.           return TYPE_TAG_NAME (parent_type);
  16004.         else
  16005.           /* An anonymous structure is only allowed non-static data
  16006.              members; no typedefs, no member functions, et cetera.
  16007.              So it does not need a prefix.  */
  16008.           return "";
  16009.       case DW_TAG_compile_unit:
  16010.       case DW_TAG_partial_unit:
  16011.         /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace.  Cope.  */
  16012.         if (cu->language == language_cplus
  16013.             && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
  16014.             && die->child != NULL
  16015.             && (die->tag == DW_TAG_class_type
  16016.                 || die->tag == DW_TAG_structure_type
  16017.                 || die->tag == DW_TAG_union_type))
  16018.           {
  16019.             char *name = guess_full_die_structure_name (die, cu);
  16020.             if (name != NULL)
  16021.               return name;
  16022.           }
  16023.         return "";
  16024.       case DW_TAG_enumeration_type:
  16025.         parent_type = read_type_die (parent, cu);
  16026.         if (TYPE_DECLARED_CLASS (parent_type))
  16027.           {
  16028.             if (TYPE_TAG_NAME (parent_type) != NULL)
  16029.               return TYPE_TAG_NAME (parent_type);
  16030.             return "";
  16031.           }
  16032.         /* Fall through.  */
  16033.       default:
  16034.         return determine_prefix (parent, cu);
  16035.       }
  16036. }

  16037. /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
  16038.    with appropriate separator.  If PREFIX or SUFFIX is NULL or empty, then
  16039.    simply copy the SUFFIX or PREFIX, respectively.  If OBS is non-null, perform
  16040.    an obconcat, otherwise allocate storage for the result.  The CU argument is
  16041.    used to determine the language and hence, the appropriate separator.  */

  16042. #define MAX_SEP_LEN 7  /* strlen ("__") + strlen ("_MOD_")  */

  16043. static char *
  16044. typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
  16045.                  int physname, struct dwarf2_cu *cu)
  16046. {
  16047.   const char *lead = "";
  16048.   const char *sep;

  16049.   if (suffix == NULL || suffix[0] == '\0'
  16050.       || prefix == NULL || prefix[0] == '\0')
  16051.     sep = "";
  16052.   else if (cu->language == language_java)
  16053.     sep = ".";
  16054.   else if (cu->language == language_fortran && physname)
  16055.     {
  16056.       /* This is gfortran specific mangling.  Normally DW_AT_linkage_name or
  16057.          DW_AT_MIPS_linkage_name is preferred and used instead.  */

  16058.       lead = "__";
  16059.       sep = "_MOD_";
  16060.     }
  16061.   else
  16062.     sep = "::";

  16063.   if (prefix == NULL)
  16064.     prefix = "";
  16065.   if (suffix == NULL)
  16066.     suffix = "";

  16067.   if (obs == NULL)
  16068.     {
  16069.       char *retval
  16070.         = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);

  16071.       strcpy (retval, lead);
  16072.       strcat (retval, prefix);
  16073.       strcat (retval, sep);
  16074.       strcat (retval, suffix);
  16075.       return retval;
  16076.     }
  16077.   else
  16078.     {
  16079.       /* We have an obstack.  */
  16080.       return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
  16081.     }
  16082. }

  16083. /* Return sibling of die, NULL if no sibling.  */

  16084. static struct die_info *
  16085. sibling_die (struct die_info *die)
  16086. {
  16087.   return die->sibling;
  16088. }

  16089. /* Get name of a die, return NULL if not found.  */

  16090. static const char *
  16091. dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
  16092.                           struct obstack *obstack)
  16093. {
  16094.   if (name && cu->language == language_cplus)
  16095.     {
  16096.       char *canon_name = cp_canonicalize_string (name);

  16097.       if (canon_name != NULL)
  16098.         {
  16099.           if (strcmp (canon_name, name) != 0)
  16100.             name = obstack_copy0 (obstack, canon_name, strlen (canon_name));
  16101.           xfree (canon_name);
  16102.         }
  16103.     }

  16104.   return name;
  16105. }

  16106. /* Get name of a die, return NULL if not found.  */

  16107. static const char *
  16108. dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
  16109. {
  16110.   struct attribute *attr;

  16111.   attr = dwarf2_attr (die, DW_AT_name, cu);
  16112.   if ((!attr || !DW_STRING (attr))
  16113.       && die->tag != DW_TAG_class_type
  16114.       && die->tag != DW_TAG_interface_type
  16115.       && die->tag != DW_TAG_structure_type
  16116.       && die->tag != DW_TAG_union_type)
  16117.     return NULL;

  16118.   switch (die->tag)
  16119.     {
  16120.     case DW_TAG_compile_unit:
  16121.     case DW_TAG_partial_unit:
  16122.       /* Compilation units have a DW_AT_name that is a filename, not
  16123.          a source language identifier.  */
  16124.     case DW_TAG_enumeration_type:
  16125.     case DW_TAG_enumerator:
  16126.       /* These tags always have simple identifiers already; no need
  16127.          to canonicalize them.  */
  16128.       return DW_STRING (attr);

  16129.     case DW_TAG_subprogram:
  16130.       /* Java constructors will all be named "<init>", so return
  16131.          the class name when we see this special case.  */
  16132.       if (cu->language == language_java
  16133.           && DW_STRING (attr) != NULL
  16134.           && strcmp (DW_STRING (attr), "<init>") == 0)
  16135.         {
  16136.           struct dwarf2_cu *spec_cu = cu;
  16137.           struct die_info *spec_die;

  16138.           /* GCJ will output '<init>' for Java constructor names.
  16139.              For this special case, return the name of the parent class.  */

  16140.           /* GCJ may output subprogram DIEs with AT_specification set.
  16141.              If so, use the name of the specified DIE.  */
  16142.           spec_die = die_specification (die, &spec_cu);
  16143.           if (spec_die != NULL)
  16144.             return dwarf2_name (spec_die, spec_cu);

  16145.           do
  16146.             {
  16147.               die = die->parent;
  16148.               if (die->tag == DW_TAG_class_type)
  16149.                 return dwarf2_name (die, cu);
  16150.             }
  16151.           while (die->tag != DW_TAG_compile_unit
  16152.                  && die->tag != DW_TAG_partial_unit);
  16153.         }
  16154.       break;

  16155.     case DW_TAG_class_type:
  16156.     case DW_TAG_interface_type:
  16157.     case DW_TAG_structure_type:
  16158.     case DW_TAG_union_type:
  16159.       /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
  16160.          structures or unions.  These were of the form "._%d" in GCC 4.1,
  16161.          or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
  16162.          and GCC 4.4.  We work around this problem by ignoring these.  */
  16163.       if (attr && DW_STRING (attr)
  16164.           && (strncmp (DW_STRING (attr), "._", 2) == 0
  16165.               || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
  16166.         return NULL;

  16167.       /* GCC might emit a nameless typedef that has a linkage name.  See
  16168.          http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510.  */
  16169.       if (!attr || DW_STRING (attr) == NULL)
  16170.         {
  16171.           char *demangled = NULL;

  16172.           attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
  16173.           if (attr == NULL)
  16174.             attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);

  16175.           if (attr == NULL || DW_STRING (attr) == NULL)
  16176.             return NULL;

  16177.           /* Avoid demangling DW_STRING (attr) the second time on a second
  16178.              call for the same DIE.  */
  16179.           if (!DW_STRING_IS_CANONICAL (attr))
  16180.             demangled = gdb_demangle (DW_STRING (attr), DMGL_TYPES);

  16181.           if (demangled)
  16182.             {
  16183.               char *base;

  16184.               /* FIXME: we already did this for the partial symbol... */
  16185.               DW_STRING (attr)
  16186.                 = obstack_copy0 (&cu->objfile->per_bfd->storage_obstack,
  16187.                                  demangled, strlen (demangled));
  16188.               DW_STRING_IS_CANONICAL (attr) = 1;
  16189.               xfree (demangled);

  16190.               /* Strip any leading namespaces/classes, keep only the base name.
  16191.                  DW_AT_name for named DIEs does not contain the prefixes.  */
  16192.               base = strrchr (DW_STRING (attr), ':');
  16193.               if (base && base > DW_STRING (attr) && base[-1] == ':')
  16194.                 return &base[1];
  16195.               else
  16196.                 return DW_STRING (attr);
  16197.             }
  16198.         }
  16199.       break;

  16200.     default:
  16201.       break;
  16202.     }

  16203.   if (!DW_STRING_IS_CANONICAL (attr))
  16204.     {
  16205.       DW_STRING (attr)
  16206.         = dwarf2_canonicalize_name (DW_STRING (attr), cu,
  16207.                                     &cu->objfile->per_bfd->storage_obstack);
  16208.       DW_STRING_IS_CANONICAL (attr) = 1;
  16209.     }
  16210.   return DW_STRING (attr);
  16211. }

  16212. /* Return the die that this die in an extension of, or NULL if there
  16213.    is none.  *EXT_CU is the CU containing DIE on input, and the CU
  16214.    containing the return value on output.  */

  16215. static struct die_info *
  16216. dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
  16217. {
  16218.   struct attribute *attr;

  16219.   attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
  16220.   if (attr == NULL)
  16221.     return NULL;

  16222.   return follow_die_ref (die, attr, ext_cu);
  16223. }

  16224. /* Convert a DIE tag into its string name.  */

  16225. static const char *
  16226. dwarf_tag_name (unsigned tag)
  16227. {
  16228.   const char *name = get_DW_TAG_name (tag);

  16229.   if (name == NULL)
  16230.     return "DW_TAG_<unknown>";

  16231.   return name;
  16232. }

  16233. /* Convert a DWARF attribute code into its string name.  */

  16234. static const char *
  16235. dwarf_attr_name (unsigned attr)
  16236. {
  16237.   const char *name;

  16238. #ifdef MIPS /* collides with DW_AT_HP_block_index */
  16239.   if (attr == DW_AT_MIPS_fde)
  16240.     return "DW_AT_MIPS_fde";
  16241. #else
  16242.   if (attr == DW_AT_HP_block_index)
  16243.     return "DW_AT_HP_block_index";
  16244. #endif

  16245.   name = get_DW_AT_name (attr);

  16246.   if (name == NULL)
  16247.     return "DW_AT_<unknown>";

  16248.   return name;
  16249. }

  16250. /* Convert a DWARF value form code into its string name.  */

  16251. static const char *
  16252. dwarf_form_name (unsigned form)
  16253. {
  16254.   const char *name = get_DW_FORM_name (form);

  16255.   if (name == NULL)
  16256.     return "DW_FORM_<unknown>";

  16257.   return name;
  16258. }

  16259. static char *
  16260. dwarf_bool_name (unsigned mybool)
  16261. {
  16262.   if (mybool)
  16263.     return "TRUE";
  16264.   else
  16265.     return "FALSE";
  16266. }

  16267. /* Convert a DWARF type code into its string name.  */

  16268. static const char *
  16269. dwarf_type_encoding_name (unsigned enc)
  16270. {
  16271.   const char *name = get_DW_ATE_name (enc);

  16272.   if (name == NULL)
  16273.     return "DW_ATE_<unknown>";

  16274.   return name;
  16275. }

  16276. static void
  16277. dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
  16278. {
  16279.   unsigned int i;

  16280.   print_spaces (indent, f);
  16281.   fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
  16282.            dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);

  16283.   if (die->parent != NULL)
  16284.     {
  16285.       print_spaces (indent, f);
  16286.       fprintf_unfiltered (f, "  parent at offset: 0x%x\n",
  16287.                           die->parent->offset.sect_off);
  16288.     }

  16289.   print_spaces (indent, f);
  16290.   fprintf_unfiltered (f, "  has children: %s\n",
  16291.            dwarf_bool_name (die->child != NULL));

  16292.   print_spaces (indent, f);
  16293.   fprintf_unfiltered (f, attributes:\n");

  16294.   for (i = 0; i < die->num_attrs; ++i)
  16295.     {
  16296.       print_spaces (indent, f);
  16297.       fprintf_unfiltered (f, "    %s (%s) ",
  16298.                dwarf_attr_name (die->attrs[i].name),
  16299.                dwarf_form_name (die->attrs[i].form));

  16300.       switch (die->attrs[i].form)
  16301.         {
  16302.         case DW_FORM_addr:
  16303.         case DW_FORM_GNU_addr_index:
  16304.           fprintf_unfiltered (f, "address: ");
  16305.           fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
  16306.           break;
  16307.         case DW_FORM_block2:
  16308.         case DW_FORM_block4:
  16309.         case DW_FORM_block:
  16310.         case DW_FORM_block1:
  16311.           fprintf_unfiltered (f, "block: size %s",
  16312.                               pulongest (DW_BLOCK (&die->attrs[i])->size));
  16313.           break;
  16314.         case DW_FORM_exprloc:
  16315.           fprintf_unfiltered (f, "expression: size %s",
  16316.                               pulongest (DW_BLOCK (&die->attrs[i])->size));
  16317.           break;
  16318.         case DW_FORM_ref_addr:
  16319.           fprintf_unfiltered (f, "ref address: ");
  16320.           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
  16321.           break;
  16322.         case DW_FORM_GNU_ref_alt:
  16323.           fprintf_unfiltered (f, "alt ref address: ");
  16324.           fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
  16325.           break;
  16326.         case DW_FORM_ref1:
  16327.         case DW_FORM_ref2:
  16328.         case DW_FORM_ref4:
  16329.         case DW_FORM_ref8:
  16330.         case DW_FORM_ref_udata:
  16331.           fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
  16332.                               (long) (DW_UNSND (&die->attrs[i])));
  16333.           break;
  16334.         case DW_FORM_data1:
  16335.         case DW_FORM_data2:
  16336.         case DW_FORM_data4:
  16337.         case DW_FORM_data8:
  16338.         case DW_FORM_udata:
  16339.         case DW_FORM_sdata:
  16340.           fprintf_unfiltered (f, "constant: %s",
  16341.                               pulongest (DW_UNSND (&die->attrs[i])));
  16342.           break;
  16343.         case DW_FORM_sec_offset:
  16344.           fprintf_unfiltered (f, "section offset: %s",
  16345.                               pulongest (DW_UNSND (&die->attrs[i])));
  16346.           break;
  16347.         case DW_FORM_ref_sig8:
  16348.           fprintf_unfiltered (f, "signature: %s",
  16349.                               hex_string (DW_SIGNATURE (&die->attrs[i])));
  16350.           break;
  16351.         case DW_FORM_string:
  16352.         case DW_FORM_strp:
  16353.         case DW_FORM_GNU_str_index:
  16354.         case DW_FORM_GNU_strp_alt:
  16355.           fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
  16356.                    DW_STRING (&die->attrs[i])
  16357.                    ? DW_STRING (&die->attrs[i]) : "",
  16358.                    DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
  16359.           break;
  16360.         case DW_FORM_flag:
  16361.           if (DW_UNSND (&die->attrs[i]))
  16362.             fprintf_unfiltered (f, "flag: TRUE");
  16363.           else
  16364.             fprintf_unfiltered (f, "flag: FALSE");
  16365.           break;
  16366.         case DW_FORM_flag_present:
  16367.           fprintf_unfiltered (f, "flag: TRUE");
  16368.           break;
  16369.         case DW_FORM_indirect:
  16370.           /* The reader will have reduced the indirect form to
  16371.              the "base form" so this form should not occur.  */
  16372.           fprintf_unfiltered (f,
  16373.                               "unexpected attribute form: DW_FORM_indirect");
  16374.           break;
  16375.         default:
  16376.           fprintf_unfiltered (f, "unsupported attribute form: %d.",
  16377.                    die->attrs[i].form);
  16378.           break;
  16379.         }
  16380.       fprintf_unfiltered (f, "\n");
  16381.     }
  16382. }

  16383. static void
  16384. dump_die_for_error (struct die_info *die)
  16385. {
  16386.   dump_die_shallow (gdb_stderr, 0, die);
  16387. }

  16388. static void
  16389. dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
  16390. {
  16391.   int indent = level * 4;

  16392.   gdb_assert (die != NULL);

  16393.   if (level >= max_level)
  16394.     return;

  16395.   dump_die_shallow (f, indent, die);

  16396.   if (die->child != NULL)
  16397.     {
  16398.       print_spaces (indent, f);
  16399.       fprintf_unfiltered (f, "  Children:");
  16400.       if (level + 1 < max_level)
  16401.         {
  16402.           fprintf_unfiltered (f, "\n");
  16403.           dump_die_1 (f, level + 1, max_level, die->child);
  16404.         }
  16405.       else
  16406.         {
  16407.           fprintf_unfiltered (f,
  16408.                               " [not printed, max nesting level reached]\n");
  16409.         }
  16410.     }

  16411.   if (die->sibling != NULL && level > 0)
  16412.     {
  16413.       dump_die_1 (f, level, max_level, die->sibling);
  16414.     }
  16415. }

  16416. /* This is called from the pdie macro in gdbinit.in.
  16417.    It's not static so gcc will keep a copy callable from gdb.  */

  16418. void
  16419. dump_die (struct die_info *die, int max_level)
  16420. {
  16421.   dump_die_1 (gdb_stdlog, 0, max_level, die);
  16422. }

  16423. static void
  16424. store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
  16425. {
  16426.   void **slot;

  16427.   slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
  16428.                                    INSERT);

  16429.   *slot = die;
  16430. }

  16431. /* Return DIE offset of ATTR.  Return 0 with complaint if ATTR is not of the
  16432.    required kind.  */

  16433. static sect_offset
  16434. dwarf2_get_ref_die_offset (const struct attribute *attr)
  16435. {
  16436.   sect_offset retval = { DW_UNSND (attr) };

  16437.   if (attr_form_is_ref (attr))
  16438.     return retval;

  16439.   retval.sect_off = 0;
  16440.   complaint (&symfile_complaints,
  16441.              _("unsupported die ref attribute form: '%s'"),
  16442.              dwarf_form_name (attr->form));
  16443.   return retval;
  16444. }

  16445. /* Return the constant value held by ATTR.  Return DEFAULT_VALUE if
  16446. * the value held by the attribute is not constant.  */

  16447. static LONGEST
  16448. dwarf2_get_attr_constant_value (const struct attribute *attr, int default_value)
  16449. {
  16450.   if (attr->form == DW_FORM_sdata)
  16451.     return DW_SND (attr);
  16452.   else if (attr->form == DW_FORM_udata
  16453.            || attr->form == DW_FORM_data1
  16454.            || attr->form == DW_FORM_data2
  16455.            || attr->form == DW_FORM_data4
  16456.            || attr->form == DW_FORM_data8)
  16457.     return DW_UNSND (attr);
  16458.   else
  16459.     {
  16460.       complaint (&symfile_complaints,
  16461.                  _("Attribute value is not a constant (%s)"),
  16462.                  dwarf_form_name (attr->form));
  16463.       return default_value;
  16464.     }
  16465. }

  16466. /* Follow reference or signature attribute ATTR of SRC_DIE.
  16467.    On entry *REF_CU is the CU of SRC_DIE.
  16468.    On exit *REF_CU is the CU of the result.  */

  16469. static struct die_info *
  16470. follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
  16471.                        struct dwarf2_cu **ref_cu)
  16472. {
  16473.   struct die_info *die;

  16474.   if (attr_form_is_ref (attr))
  16475.     die = follow_die_ref (src_die, attr, ref_cu);
  16476.   else if (attr->form == DW_FORM_ref_sig8)
  16477.     die = follow_die_sig (src_die, attr, ref_cu);
  16478.   else
  16479.     {
  16480.       dump_die_for_error (src_die);
  16481.       error (_("Dwarf Error: Expected reference attribute [in module %s]"),
  16482.              objfile_name ((*ref_cu)->objfile));
  16483.     }

  16484.   return die;
  16485. }

  16486. /* Follow reference OFFSET.
  16487.    On entry *REF_CU is the CU of the source die referencing OFFSET.
  16488.    On exit *REF_CU is the CU of the result.
  16489.    Returns NULL if OFFSET is invalid.  */

  16490. static struct die_info *
  16491. follow_die_offset (sect_offset offset, int offset_in_dwz,
  16492.                    struct dwarf2_cu **ref_cu)
  16493. {
  16494.   struct die_info temp_die;
  16495.   struct dwarf2_cu *target_cu, *cu = *ref_cu;

  16496.   gdb_assert (cu->per_cu != NULL);

  16497.   target_cu = cu;

  16498.   if (cu->per_cu->is_debug_types)
  16499.     {
  16500.       /* .debug_types CUs cannot reference anything outside their CU.
  16501.          If they need to, they have to reference a signatured type via
  16502.          DW_FORM_ref_sig8.  */
  16503.       if (! offset_in_cu_p (&cu->header, offset))
  16504.         return NULL;
  16505.     }
  16506.   else if (offset_in_dwz != cu->per_cu->is_dwz
  16507.            || ! offset_in_cu_p (&cu->header, offset))
  16508.     {
  16509.       struct dwarf2_per_cu_data *per_cu;

  16510.       per_cu = dwarf2_find_containing_comp_unit (offset, offset_in_dwz,
  16511.                                                  cu->objfile);

  16512.       /* If necessary, add it to the queue and load its DIEs.  */
  16513.       if (maybe_queue_comp_unit (cu, per_cu, cu->language))
  16514.         load_full_comp_unit (per_cu, cu->language);

  16515.       target_cu = per_cu->cu;
  16516.     }
  16517.   else if (cu->dies == NULL)
  16518.     {
  16519.       /* We're loading full DIEs during partial symbol reading.  */
  16520.       gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
  16521.       load_full_comp_unit (cu->per_cu, language_minimal);
  16522.     }

  16523.   *ref_cu = target_cu;
  16524.   temp_die.offset = offset;
  16525.   return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
  16526. }

  16527. /* Follow reference attribute ATTR of SRC_DIE.
  16528.    On entry *REF_CU is the CU of SRC_DIE.
  16529.    On exit *REF_CU is the CU of the result.  */

  16530. static struct die_info *
  16531. follow_die_ref (struct die_info *src_die, const struct attribute *attr,
  16532.                 struct dwarf2_cu **ref_cu)
  16533. {
  16534.   sect_offset offset = dwarf2_get_ref_die_offset (attr);
  16535.   struct dwarf2_cu *cu = *ref_cu;
  16536.   struct die_info *die;

  16537.   die = follow_die_offset (offset,
  16538.                            (attr->form == DW_FORM_GNU_ref_alt
  16539.                             || cu->per_cu->is_dwz),
  16540.                            ref_cu);
  16541.   if (!die)
  16542.     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
  16543.            "at 0x%x [in module %s]"),
  16544.            offset.sect_off, src_die->offset.sect_off,
  16545.            objfile_name (cu->objfile));

  16546.   return die;
  16547. }

  16548. /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
  16549.    Returned value is intended for DW_OP_call*.  Returned
  16550.    dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE.  */

  16551. struct dwarf2_locexpr_baton
  16552. dwarf2_fetch_die_loc_sect_off (sect_offset offset,
  16553.                                struct dwarf2_per_cu_data *per_cu,
  16554.                                CORE_ADDR (*get_frame_pc) (void *baton),
  16555.                                void *baton)
  16556. {
  16557.   struct dwarf2_cu *cu;
  16558.   struct die_info *die;
  16559.   struct attribute *attr;
  16560.   struct dwarf2_locexpr_baton retval;

  16561.   dw2_setup (per_cu->objfile);

  16562.   if (per_cu->cu == NULL)
  16563.     load_cu (per_cu);
  16564.   cu = per_cu->cu;

  16565.   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
  16566.   if (!die)
  16567.     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
  16568.            offset.sect_off, objfile_name (per_cu->objfile));

  16569.   attr = dwarf2_attr (die, DW_AT_location, cu);
  16570.   if (!attr)
  16571.     {
  16572.       /* DWARF: "If there is no such attribute, then there is no effect.".
  16573.          DATA is ignored if SIZE is 0.  */

  16574.       retval.data = NULL;
  16575.       retval.size = 0;
  16576.     }
  16577.   else if (attr_form_is_section_offset (attr))
  16578.     {
  16579.       struct dwarf2_loclist_baton loclist_baton;
  16580.       CORE_ADDR pc = (*get_frame_pc) (baton);
  16581.       size_t size;

  16582.       fill_in_loclist_baton (cu, &loclist_baton, attr);

  16583.       retval.data = dwarf2_find_location_expression (&loclist_baton,
  16584.                                                      &size, pc);
  16585.       retval.size = size;
  16586.     }
  16587.   else
  16588.     {
  16589.       if (!attr_form_is_block (attr))
  16590.         error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
  16591.                  "is neither DW_FORM_block* nor DW_FORM_exprloc"),
  16592.                offset.sect_off, objfile_name (per_cu->objfile));

  16593.       retval.data = DW_BLOCK (attr)->data;
  16594.       retval.size = DW_BLOCK (attr)->size;
  16595.     }
  16596.   retval.per_cu = cu->per_cu;

  16597.   age_cached_comp_units ();

  16598.   return retval;
  16599. }

  16600. /* Like dwarf2_fetch_die_loc_sect_off, but take a CU
  16601.    offset.  */

  16602. struct dwarf2_locexpr_baton
  16603. dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
  16604.                              struct dwarf2_per_cu_data *per_cu,
  16605.                              CORE_ADDR (*get_frame_pc) (void *baton),
  16606.                              void *baton)
  16607. {
  16608.   sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };

  16609.   return dwarf2_fetch_die_loc_sect_off (offset, per_cu, get_frame_pc, baton);
  16610. }

  16611. /* Write a constant of a given type as target-ordered bytes into
  16612.    OBSTACK.  */

  16613. static const gdb_byte *
  16614. write_constant_as_bytes (struct obstack *obstack,
  16615.                          enum bfd_endian byte_order,
  16616.                          struct type *type,
  16617.                          ULONGEST value,
  16618.                          LONGEST *len)
  16619. {
  16620.   gdb_byte *result;

  16621.   *len = TYPE_LENGTH (type);
  16622.   result = obstack_alloc (obstack, *len);
  16623.   store_unsigned_integer (result, *len, byte_order, value);

  16624.   return result;
  16625. }

  16626. /* If the DIE at OFFSET in PER_CU has a DW_AT_const_value, return a
  16627.    pointer to the constant bytes and set LEN to the length of the
  16628.    data.  If memory is needed, allocate it on OBSTACK.  If the DIE
  16629.    does not have a DW_AT_const_value, return NULL.  */

  16630. const gdb_byte *
  16631. dwarf2_fetch_constant_bytes (sect_offset offset,
  16632.                              struct dwarf2_per_cu_data *per_cu,
  16633.                              struct obstack *obstack,
  16634.                              LONGEST *len)
  16635. {
  16636.   struct dwarf2_cu *cu;
  16637.   struct die_info *die;
  16638.   struct attribute *attr;
  16639.   const gdb_byte *result = NULL;
  16640.   struct type *type;
  16641.   LONGEST value;
  16642.   enum bfd_endian byte_order;

  16643.   dw2_setup (per_cu->objfile);

  16644.   if (per_cu->cu == NULL)
  16645.     load_cu (per_cu);
  16646.   cu = per_cu->cu;

  16647.   die = follow_die_offset (offset, per_cu->is_dwz, &cu);
  16648.   if (!die)
  16649.     error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
  16650.            offset.sect_off, objfile_name (per_cu->objfile));


  16651.   attr = dwarf2_attr (die, DW_AT_const_value, cu);
  16652.   if (attr == NULL)
  16653.     return NULL;

  16654.   byte_order = (bfd_big_endian (per_cu->objfile->obfd)
  16655.                 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);

  16656.   switch (attr->form)
  16657.     {
  16658.     case DW_FORM_addr:
  16659.     case DW_FORM_GNU_addr_index:
  16660.       {
  16661.         gdb_byte *tem;

  16662.         *len = cu->header.addr_size;
  16663.         tem = obstack_alloc (obstack, *len);
  16664.         store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
  16665.         result = tem;
  16666.       }
  16667.       break;
  16668.     case DW_FORM_string:
  16669.     case DW_FORM_strp:
  16670.     case DW_FORM_GNU_str_index:
  16671.     case DW_FORM_GNU_strp_alt:
  16672.       /* DW_STRING is already allocated on the objfile obstack, point
  16673.          directly to it.  */
  16674.       result = (const gdb_byte *) DW_STRING (attr);
  16675.       *len = strlen (DW_STRING (attr));
  16676.       break;
  16677.     case DW_FORM_block1:
  16678.     case DW_FORM_block2:
  16679.     case DW_FORM_block4:
  16680.     case DW_FORM_block:
  16681.     case DW_FORM_exprloc:
  16682.       result = DW_BLOCK (attr)->data;
  16683.       *len = DW_BLOCK (attr)->size;
  16684.       break;

  16685.       /* The DW_AT_const_value attributes are supposed to carry the
  16686.          symbol's value "represented as it would be on the target
  16687.          architecture."  By the time we get here, it's already been
  16688.          converted to host endianness, so we just need to sign- or
  16689.          zero-extend it as appropriate.  */
  16690.     case DW_FORM_data1:
  16691.       type = die_type (die, cu);
  16692.       result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
  16693.       if (result == NULL)
  16694.         result = write_constant_as_bytes (obstack, byte_order,
  16695.                                           type, value, len);
  16696.       break;
  16697.     case DW_FORM_data2:
  16698.       type = die_type (die, cu);
  16699.       result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
  16700.       if (result == NULL)
  16701.         result = write_constant_as_bytes (obstack, byte_order,
  16702.                                           type, value, len);
  16703.       break;
  16704.     case DW_FORM_data4:
  16705.       type = die_type (die, cu);
  16706.       result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
  16707.       if (result == NULL)
  16708.         result = write_constant_as_bytes (obstack, byte_order,
  16709.                                           type, value, len);
  16710.       break;
  16711.     case DW_FORM_data8:
  16712.       type = die_type (die, cu);
  16713.       result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
  16714.       if (result == NULL)
  16715.         result = write_constant_as_bytes (obstack, byte_order,
  16716.                                           type, value, len);
  16717.       break;

  16718.     case DW_FORM_sdata:
  16719.       type = die_type (die, cu);
  16720.       result = write_constant_as_bytes (obstack, byte_order,
  16721.                                         type, DW_SND (attr), len);
  16722.       break;

  16723.     case DW_FORM_udata:
  16724.       type = die_type (die, cu);
  16725.       result = write_constant_as_bytes (obstack, byte_order,
  16726.                                         type, DW_UNSND (attr), len);
  16727.       break;

  16728.     default:
  16729.       complaint (&symfile_complaints,
  16730.                  _("unsupported const value attribute form: '%s'"),
  16731.                  dwarf_form_name (attr->form));
  16732.       break;
  16733.     }

  16734.   return result;
  16735. }

  16736. /* Return the type of the DIE at DIE_OFFSET in the CU named by
  16737.    PER_CU.  */

  16738. struct type *
  16739. dwarf2_get_die_type (cu_offset die_offset,
  16740.                      struct dwarf2_per_cu_data *per_cu)
  16741. {
  16742.   sect_offset die_offset_sect;

  16743.   dw2_setup (per_cu->objfile);

  16744.   die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
  16745.   return get_die_type_at_offset (die_offset_sect, per_cu);
  16746. }

  16747. /* Follow type unit SIG_TYPE referenced by SRC_DIE.
  16748.    On entry *REF_CU is the CU of SRC_DIE.
  16749.    On exit *REF_CU is the CU of the result.
  16750.    Returns NULL if the referenced DIE isn't found.  */

  16751. static struct die_info *
  16752. follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
  16753.                   struct dwarf2_cu **ref_cu)
  16754. {
  16755.   struct objfile *objfile = (*ref_cu)->objfile;
  16756.   struct die_info temp_die;
  16757.   struct dwarf2_cu *sig_cu;
  16758.   struct die_info *die;

  16759.   /* While it might be nice to assert sig_type->type == NULL here,
  16760.      we can get here for DW_AT_imported_declaration where we need
  16761.      the DIE not the type.  */

  16762.   /* If necessary, add it to the queue and load its DIEs.  */

  16763.   if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
  16764.     read_signatured_type (sig_type);

  16765.   sig_cu = sig_type->per_cu.cu;
  16766.   gdb_assert (sig_cu != NULL);
  16767.   gdb_assert (sig_type->type_offset_in_section.sect_off != 0);
  16768.   temp_die.offset = sig_type->type_offset_in_section;
  16769.   die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
  16770.                              temp_die.offset.sect_off);
  16771.   if (die)
  16772.     {
  16773.       /* For .gdb_index version 7 keep track of included TUs.
  16774.          http://sourceware.org/bugzilla/show_bug.cgi?id=15021.  */
  16775.       if (dwarf2_per_objfile->index_table != NULL
  16776.           && dwarf2_per_objfile->index_table->version <= 7)
  16777.         {
  16778.           VEC_safe_push (dwarf2_per_cu_ptr,
  16779.                          (*ref_cu)->per_cu->imported_symtabs,
  16780.                          sig_cu->per_cu);
  16781.         }

  16782.       *ref_cu = sig_cu;
  16783.       return die;
  16784.     }

  16785.   return NULL;
  16786. }

  16787. /* Follow signatured type referenced by ATTR in SRC_DIE.
  16788.    On entry *REF_CU is the CU of SRC_DIE.
  16789.    On exit *REF_CU is the CU of the result.
  16790.    The result is the DIE of the type.
  16791.    If the referenced type cannot be found an error is thrown.  */

  16792. static struct die_info *
  16793. follow_die_sig (struct die_info *src_die, const struct attribute *attr,
  16794.                 struct dwarf2_cu **ref_cu)
  16795. {
  16796.   ULONGEST signature = DW_SIGNATURE (attr);
  16797.   struct signatured_type *sig_type;
  16798.   struct die_info *die;

  16799.   gdb_assert (attr->form == DW_FORM_ref_sig8);

  16800.   sig_type = lookup_signatured_type (*ref_cu, signature);
  16801.   /* sig_type will be NULL if the signatured type is missing from
  16802.      the debug info.  */
  16803.   if (sig_type == NULL)
  16804.     {
  16805.       error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
  16806.                " from DIE at 0x%x [in module %s]"),
  16807.              hex_string (signature), src_die->offset.sect_off,
  16808.              objfile_name ((*ref_cu)->objfile));
  16809.     }

  16810.   die = follow_die_sig_1 (src_die, sig_type, ref_cu);
  16811.   if (die == NULL)
  16812.     {
  16813.       dump_die_for_error (src_die);
  16814.       error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
  16815.                " from DIE at 0x%x [in module %s]"),
  16816.              hex_string (signature), src_die->offset.sect_off,
  16817.              objfile_name ((*ref_cu)->objfile));
  16818.     }

  16819.   return die;
  16820. }

  16821. /* Get the type specified by SIGNATURE referenced in DIE/CU,
  16822.    reading in and processing the type unit if necessary.  */

  16823. static struct type *
  16824. get_signatured_type (struct die_info *die, ULONGEST signature,
  16825.                      struct dwarf2_cu *cu)
  16826. {
  16827.   struct signatured_type *sig_type;
  16828.   struct dwarf2_cu *type_cu;
  16829.   struct die_info *type_die;
  16830.   struct type *type;

  16831.   sig_type = lookup_signatured_type (cu, signature);
  16832.   /* sig_type will be NULL if the signatured type is missing from
  16833.      the debug info.  */
  16834.   if (sig_type == NULL)
  16835.     {
  16836.       complaint (&symfile_complaints,
  16837.                  _("Dwarf Error: Cannot find signatured DIE %s referenced"
  16838.                    " from DIE at 0x%x [in module %s]"),
  16839.                  hex_string (signature), die->offset.sect_off,
  16840.                  objfile_name (dwarf2_per_objfile->objfile));
  16841.       return build_error_marker_type (cu, die);
  16842.     }

  16843.   /* If we already know the type we're done.  */
  16844.   if (sig_type->type != NULL)
  16845.     return sig_type->type;

  16846.   type_cu = cu;
  16847.   type_die = follow_die_sig_1 (die, sig_type, &type_cu);
  16848.   if (type_die != NULL)
  16849.     {
  16850.       /* N.B. We need to call get_die_type to ensure only one type for this DIE
  16851.          is created.  This is important, for example, because for c++ classes
  16852.          we need TYPE_NAME set which is only done by new_symbol.  Blech.  */
  16853.       type = read_type_die (type_die, type_cu);
  16854.       if (type == NULL)
  16855.         {
  16856.           complaint (&symfile_complaints,
  16857.                      _("Dwarf Error: Cannot build signatured type %s"
  16858.                        " referenced from DIE at 0x%x [in module %s]"),
  16859.                      hex_string (signature), die->offset.sect_off,
  16860.                      objfile_name (dwarf2_per_objfile->objfile));
  16861.           type = build_error_marker_type (cu, die);
  16862.         }
  16863.     }
  16864.   else
  16865.     {
  16866.       complaint (&symfile_complaints,
  16867.                  _("Dwarf Error: Problem reading signatured DIE %s referenced"
  16868.                    " from DIE at 0x%x [in module %s]"),
  16869.                  hex_string (signature), die->offset.sect_off,
  16870.                  objfile_name (dwarf2_per_objfile->objfile));
  16871.       type = build_error_marker_type (cu, die);
  16872.     }
  16873.   sig_type->type = type;

  16874.   return type;
  16875. }

  16876. /* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
  16877.    reading in and processing the type unit if necessary.  */

  16878. static struct type *
  16879. get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
  16880.                           struct dwarf2_cu *cu) /* ARI: editCase function */
  16881. {
  16882.   /* Yes, DW_AT_signature can use a non-ref_sig8 reference.  */
  16883.   if (attr_form_is_ref (attr))
  16884.     {
  16885.       struct dwarf2_cu *type_cu = cu;
  16886.       struct die_info *type_die = follow_die_ref (die, attr, &type_cu);

  16887.       return read_type_die (type_die, type_cu);
  16888.     }
  16889.   else if (attr->form == DW_FORM_ref_sig8)
  16890.     {
  16891.       return get_signatured_type (die, DW_SIGNATURE (attr), cu);
  16892.     }
  16893.   else
  16894.     {
  16895.       complaint (&symfile_complaints,
  16896.                  _("Dwarf Error: DW_AT_signature has bad form %s in DIE"
  16897.                    " at 0x%x [in module %s]"),
  16898.                  dwarf_form_name (attr->form), die->offset.sect_off,
  16899.                  objfile_name (dwarf2_per_objfile->objfile));
  16900.       return build_error_marker_type (cu, die);
  16901.     }
  16902. }

  16903. /* Load the DIEs associated with type unit PER_CU into memory.  */

  16904. static void
  16905. load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
  16906. {
  16907.   struct signatured_type *sig_type;

  16908.   /* Caller is responsible for ensuring type_unit_groups don't get here.  */
  16909.   gdb_assert (! IS_TYPE_UNIT_GROUP (per_cu));

  16910.   /* We have the per_cu, but we need the signatured_type.
  16911.      Fortunately this is an easy translation.  */
  16912.   gdb_assert (per_cu->is_debug_types);
  16913.   sig_type = (struct signatured_type *) per_cu;

  16914.   gdb_assert (per_cu->cu == NULL);

  16915.   read_signatured_type (sig_type);

  16916.   gdb_assert (per_cu->cu != NULL);
  16917. }

  16918. /* die_reader_func for read_signatured_type.
  16919.    This is identical to load_full_comp_unit_reader,
  16920.    but is kept separate for now.  */

  16921. static void
  16922. read_signatured_type_reader (const struct die_reader_specs *reader,
  16923.                              const gdb_byte *info_ptr,
  16924.                              struct die_info *comp_unit_die,
  16925.                              int has_children,
  16926.                              void *data)
  16927. {
  16928.   struct dwarf2_cu *cu = reader->cu;

  16929.   gdb_assert (cu->die_hash == NULL);
  16930.   cu->die_hash =
  16931.     htab_create_alloc_ex (cu->header.length / 12,
  16932.                           die_hash,
  16933.                           die_eq,
  16934.                           NULL,
  16935.                           &cu->comp_unit_obstack,
  16936.                           hashtab_obstack_allocate,
  16937.                           dummy_obstack_deallocate);

  16938.   if (has_children)
  16939.     comp_unit_die->child = read_die_and_siblings (reader, info_ptr,
  16940.                                                   &info_ptr, comp_unit_die);
  16941.   cu->dies = comp_unit_die;
  16942.   /* comp_unit_die is not stored in die_hash, no need.  */

  16943.   /* We try not to read any attributes in this function, because not
  16944.      all CUs needed for references have been loaded yet, and symbol
  16945.      table processing isn't initialized.  But we have to set the CU language,
  16946.      or we won't be able to build types correctly.
  16947.      Similarly, if we do not read the producer, we can not apply
  16948.      producer-specific interpretation.  */
  16949.   prepare_one_comp_unit (cu, cu->dies, language_minimal);
  16950. }

  16951. /* Read in a signatured type and build its CU and DIEs.
  16952.    If the type is a stub for the real type in a DWO file,
  16953.    read in the real type from the DWO file as well.  */

  16954. static void
  16955. read_signatured_type (struct signatured_type *sig_type)
  16956. {
  16957.   struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;

  16958.   gdb_assert (per_cu->is_debug_types);
  16959.   gdb_assert (per_cu->cu == NULL);

  16960.   init_cutu_and_read_dies (per_cu, NULL, 0, 1,
  16961.                            read_signatured_type_reader, NULL);
  16962.   sig_type->per_cu.tu_read = 1;
  16963. }

  16964. /* Decode simple location descriptions.
  16965.    Given a pointer to a dwarf block that defines a location, compute
  16966.    the location and return the value.

  16967.    NOTE drow/2003-11-18: This function is called in two situations
  16968.    now: for the address of static or global variables (partial symbols
  16969.    only) and for offsets into structures which are expected to be
  16970.    (more or less) constant.  The partial symbol case should go away,
  16971.    and only the constant case should remain.  That will let this
  16972.    function complain more accurately.  A few special modes are allowed
  16973.    without complaint for global variables (for instance, global
  16974.    register values and thread-local values).

  16975.    A location description containing no operations indicates that the
  16976.    object is optimized out.  The return value is 0 for that case.
  16977.    FIXME drow/2003-11-16: No callers check for this case any more; soon all
  16978.    callers will only want a very basic result and this can become a
  16979.    complaint.

  16980.    Note that stack[0] is unused except as a default error return.  */

  16981. static CORE_ADDR
  16982. decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
  16983. {
  16984.   struct objfile *objfile = cu->objfile;
  16985.   size_t i;
  16986.   size_t size = blk->size;
  16987.   const gdb_byte *data = blk->data;
  16988.   CORE_ADDR stack[64];
  16989.   int stacki;
  16990.   unsigned int bytes_read, unsnd;
  16991.   gdb_byte op;

  16992.   i = 0;
  16993.   stacki = 0;
  16994.   stack[stacki] = 0;
  16995.   stack[++stacki] = 0;

  16996.   while (i < size)
  16997.     {
  16998.       op = data[i++];
  16999.       switch (op)
  17000.         {
  17001.         case DW_OP_lit0:
  17002.         case DW_OP_lit1:
  17003.         case DW_OP_lit2:
  17004.         case DW_OP_lit3:
  17005.         case DW_OP_lit4:
  17006.         case DW_OP_lit5:
  17007.         case DW_OP_lit6:
  17008.         case DW_OP_lit7:
  17009.         case DW_OP_lit8:
  17010.         case DW_OP_lit9:
  17011.         case DW_OP_lit10:
  17012.         case DW_OP_lit11:
  17013.         case DW_OP_lit12:
  17014.         case DW_OP_lit13:
  17015.         case DW_OP_lit14:
  17016.         case DW_OP_lit15:
  17017.         case DW_OP_lit16:
  17018.         case DW_OP_lit17:
  17019.         case DW_OP_lit18:
  17020.         case DW_OP_lit19:
  17021.         case DW_OP_lit20:
  17022.         case DW_OP_lit21:
  17023.         case DW_OP_lit22:
  17024.         case DW_OP_lit23:
  17025.         case DW_OP_lit24:
  17026.         case DW_OP_lit25:
  17027.         case DW_OP_lit26:
  17028.         case DW_OP_lit27:
  17029.         case DW_OP_lit28:
  17030.         case DW_OP_lit29:
  17031.         case DW_OP_lit30:
  17032.         case DW_OP_lit31:
  17033.           stack[++stacki] = op - DW_OP_lit0;
  17034.           break;

  17035.         case DW_OP_reg0:
  17036.         case DW_OP_reg1:
  17037.         case DW_OP_reg2:
  17038.         case DW_OP_reg3:
  17039.         case DW_OP_reg4:
  17040.         case DW_OP_reg5:
  17041.         case DW_OP_reg6:
  17042.         case DW_OP_reg7:
  17043.         case DW_OP_reg8:
  17044.         case DW_OP_reg9:
  17045.         case DW_OP_reg10:
  17046.         case DW_OP_reg11:
  17047.         case DW_OP_reg12:
  17048.         case DW_OP_reg13:
  17049.         case DW_OP_reg14:
  17050.         case DW_OP_reg15:
  17051.         case DW_OP_reg16:
  17052.         case DW_OP_reg17:
  17053.         case DW_OP_reg18:
  17054.         case DW_OP_reg19:
  17055.         case DW_OP_reg20:
  17056.         case DW_OP_reg21:
  17057.         case DW_OP_reg22:
  17058.         case DW_OP_reg23:
  17059.         case DW_OP_reg24:
  17060.         case DW_OP_reg25:
  17061.         case DW_OP_reg26:
  17062.         case DW_OP_reg27:
  17063.         case DW_OP_reg28:
  17064.         case DW_OP_reg29:
  17065.         case DW_OP_reg30:
  17066.         case DW_OP_reg31:
  17067.           stack[++stacki] = op - DW_OP_reg0;
  17068.           if (i < size)
  17069.             dwarf2_complex_location_expr_complaint ();
  17070.           break;

  17071.         case DW_OP_regx:
  17072.           unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
  17073.           i += bytes_read;
  17074.           stack[++stacki] = unsnd;
  17075.           if (i < size)
  17076.             dwarf2_complex_location_expr_complaint ();
  17077.           break;

  17078.         case DW_OP_addr:
  17079.           stack[++stacki] = read_address (objfile->obfd, &data[i],
  17080.                                           cu, &bytes_read);
  17081.           i += bytes_read;
  17082.           break;

  17083.         case DW_OP_const1u:
  17084.           stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
  17085.           i += 1;
  17086.           break;

  17087.         case DW_OP_const1s:
  17088.           stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
  17089.           i += 1;
  17090.           break;

  17091.         case DW_OP_const2u:
  17092.           stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
  17093.           i += 2;
  17094.           break;

  17095.         case DW_OP_const2s:
  17096.           stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
  17097.           i += 2;
  17098.           break;

  17099.         case DW_OP_const4u:
  17100.           stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
  17101.           i += 4;
  17102.           break;

  17103.         case DW_OP_const4s:
  17104.           stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
  17105.           i += 4;
  17106.           break;

  17107.         case DW_OP_const8u:
  17108.           stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
  17109.           i += 8;
  17110.           break;

  17111.         case DW_OP_constu:
  17112.           stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
  17113.                                                   &bytes_read);
  17114.           i += bytes_read;
  17115.           break;

  17116.         case DW_OP_consts:
  17117.           stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
  17118.           i += bytes_read;
  17119.           break;

  17120.         case DW_OP_dup:
  17121.           stack[stacki + 1] = stack[stacki];
  17122.           stacki++;
  17123.           break;

  17124.         case DW_OP_plus:
  17125.           stack[stacki - 1] += stack[stacki];
  17126.           stacki--;
  17127.           break;

  17128.         case DW_OP_plus_uconst:
  17129.           stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
  17130.                                                  &bytes_read);
  17131.           i += bytes_read;
  17132.           break;

  17133.         case DW_OP_minus:
  17134.           stack[stacki - 1] -= stack[stacki];
  17135.           stacki--;
  17136.           break;

  17137.         case DW_OP_deref:
  17138.           /* If we're not the last op, then we definitely can't encode
  17139.              this using GDB's address_class enum.  This is valid for partial
  17140.              global symbols, although the variable's address will be bogus
  17141.              in the psymtab.  */
  17142.           if (i < size)
  17143.             dwarf2_complex_location_expr_complaint ();
  17144.           break;

  17145.         case DW_OP_GNU_push_tls_address:
  17146.           /* The top of the stack has the offset from the beginning
  17147.              of the thread control block at which the variable is located.  */
  17148.           /* Nothing should follow this operator, so the top of stack would
  17149.              be returned.  */
  17150.           /* This is valid for partial global symbols, but the variable's
  17151.              address will be bogus in the psymtab.  Make it always at least
  17152.              non-zero to not look as a variable garbage collected by linker
  17153.              which have DW_OP_addr 0.  */
  17154.           if (i < size)
  17155.             dwarf2_complex_location_expr_complaint ();
  17156.           stack[stacki]++;
  17157.           break;

  17158.         case DW_OP_GNU_uninit:
  17159.           break;

  17160.         case DW_OP_GNU_addr_index:
  17161.         case DW_OP_GNU_const_index:
  17162.           stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
  17163.                                                          &bytes_read);
  17164.           i += bytes_read;
  17165.           break;

  17166.         default:
  17167.           {
  17168.             const char *name = get_DW_OP_name (op);

  17169.             if (name)
  17170.               complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
  17171.                          name);
  17172.             else
  17173.               complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
  17174.                          op);
  17175.           }

  17176.           return (stack[stacki]);
  17177.         }

  17178.       /* Enforce maximum stack depth of SIZE-1 to avoid writing
  17179.          outside of the allocated space.  Also enforce minimum>0.  */
  17180.       if (stacki >= ARRAY_SIZE (stack) - 1)
  17181.         {
  17182.           complaint (&symfile_complaints,
  17183.                      _("location description stack overflow"));
  17184.           return 0;
  17185.         }

  17186.       if (stacki <= 0)
  17187.         {
  17188.           complaint (&symfile_complaints,
  17189.                      _("location description stack underflow"));
  17190.           return 0;
  17191.         }
  17192.     }
  17193.   return (stack[stacki]);
  17194. }

  17195. /* memory allocation interface */

  17196. static struct dwarf_block *
  17197. dwarf_alloc_block (struct dwarf2_cu *cu)
  17198. {
  17199.   struct dwarf_block *blk;

  17200.   blk = (struct dwarf_block *)
  17201.     obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
  17202.   return (blk);
  17203. }

  17204. static struct die_info *
  17205. dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
  17206. {
  17207.   struct die_info *die;
  17208.   size_t size = sizeof (struct die_info);

  17209.   if (num_attrs > 1)
  17210.     size += (num_attrs - 1) * sizeof (struct attribute);

  17211.   die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
  17212.   memset (die, 0, sizeof (struct die_info));
  17213.   return (die);
  17214. }


  17215. /* Macro support.  */

  17216. /* Return file name relative to the compilation directory of file number I in
  17217.    *LH's file name table.  The result is allocated using xmalloc; the caller is
  17218.    responsible for freeing it.  */

  17219. static char *
  17220. file_file_name (int file, struct line_header *lh)
  17221. {
  17222.   /* Is the file number a valid index into the line header's file name
  17223.      table?  Remember that file numbers start with one, not zero.  */
  17224.   if (1 <= file && file <= lh->num_file_names)
  17225.     {
  17226.       struct file_entry *fe = &lh->file_names[file - 1];

  17227.       if (IS_ABSOLUTE_PATH (fe->name) || fe->dir_index == 0)
  17228.         return xstrdup (fe->name);
  17229.       return concat (lh->include_dirs[fe->dir_index - 1], SLASH_STRING,
  17230.                      fe->name, NULL);
  17231.     }
  17232.   else
  17233.     {
  17234.       /* The compiler produced a bogus file number.  We can at least
  17235.          record the macro definitions made in the file, even if we
  17236.          won't be able to find the file by name.  */
  17237.       char fake_name[80];

  17238.       xsnprintf (fake_name, sizeof (fake_name),
  17239.                  "<bad macro file number %d>", file);

  17240.       complaint (&symfile_complaints,
  17241.                  _("bad file number in macro information (%d)"),
  17242.                  file);

  17243.       return xstrdup (fake_name);
  17244.     }
  17245. }

  17246. /* Return the full name of file number I in *LH's file name table.
  17247.    Use COMP_DIR as the name of the current directory of the
  17248.    compilation.  The result is allocated using xmalloc; the caller is
  17249.    responsible for freeing it.  */
  17250. static char *
  17251. file_full_name (int file, struct line_header *lh, const char *comp_dir)
  17252. {
  17253.   /* Is the file number a valid index into the line header's file name
  17254.      table?  Remember that file numbers start with one, not zero.  */
  17255.   if (1 <= file && file <= lh->num_file_names)
  17256.     {
  17257.       char *relative = file_file_name (file, lh);

  17258.       if (IS_ABSOLUTE_PATH (relative) || comp_dir == NULL)
  17259.         return relative;
  17260.       return reconcat (relative, comp_dir, SLASH_STRING, relative, NULL);
  17261.     }
  17262.   else
  17263.     return file_file_name (file, lh);
  17264. }


  17265. static struct macro_source_file *
  17266. macro_start_file (int file, int line,
  17267.                   struct macro_source_file *current_file,
  17268.                   struct line_header *lh)
  17269. {
  17270.   /* File name relative to the compilation directory of this source file.  */
  17271.   char *file_name = file_file_name (file, lh);

  17272.   if (! current_file)
  17273.     {
  17274.       /* Note: We don't create a macro table for this compilation unit
  17275.          at all until we actually get a filename.  */
  17276.       struct macro_table *macro_table = get_macro_table ();

  17277.       /* If we have no current file, then this must be the start_file
  17278.          directive for the compilation unit's main source file.  */
  17279.       current_file = macro_set_main (macro_table, file_name);
  17280.       macro_define_special (macro_table);
  17281.     }
  17282.   else
  17283.     current_file = macro_include (current_file, line, file_name);

  17284.   xfree (file_name);

  17285.   return current_file;
  17286. }


  17287. /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
  17288.    followed by a null byte.  */
  17289. static char *
  17290. copy_string (const char *buf, int len)
  17291. {
  17292.   char *s = xmalloc (len + 1);

  17293.   memcpy (s, buf, len);
  17294.   s[len] = '\0';
  17295.   return s;
  17296. }


  17297. static const char *
  17298. consume_improper_spaces (const char *p, const char *body)
  17299. {
  17300.   if (*p == ' ')
  17301.     {
  17302.       complaint (&symfile_complaints,
  17303.                  _("macro definition contains spaces "
  17304.                    "in formal argument list:\n`%s'"),
  17305.                  body);

  17306.       while (*p == ' ')
  17307.         p++;
  17308.     }

  17309.   return p;
  17310. }


  17311. static void
  17312. parse_macro_definition (struct macro_source_file *file, int line,
  17313.                         const char *body)
  17314. {
  17315.   const char *p;

  17316.   /* The body string takes one of two forms.  For object-like macro
  17317.      definitions, it should be:

  17318.         <macro name> " " <definition>

  17319.      For function-like macro definitions, it should be:

  17320.         <macro name> "() " <definition>
  17321.      or
  17322.         <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>

  17323.      Spaces may appear only where explicitly indicated, and in the
  17324.      <definition>.

  17325.      The Dwarf 2 spec says that an object-like macro's name is always
  17326.      followed by a space, but versions of GCC around March 2002 omit
  17327.      the space when the macro's definition is the empty string.

  17328.      The Dwarf 2 spec says that there should be no spaces between the
  17329.      formal arguments in a function-like macro's formal argument list,
  17330.      but versions of GCC around March 2002 include spaces after the
  17331.      commas.  */


  17332.   /* Find the extent of the macro name.  The macro name is terminated
  17333.      by either a space or null character (for an object-like macro) or
  17334.      an opening paren (for a function-like macro).  */
  17335.   for (p = body; *p; p++)
  17336.     if (*p == ' ' || *p == '(')
  17337.       break;

  17338.   if (*p == ' ' || *p == '\0')
  17339.     {
  17340.       /* It's an object-like macro.  */
  17341.       int name_len = p - body;
  17342.       char *name = copy_string (body, name_len);
  17343.       const char *replacement;

  17344.       if (*p == ' ')
  17345.         replacement = body + name_len + 1;
  17346.       else
  17347.         {
  17348.           dwarf2_macro_malformed_definition_complaint (body);
  17349.           replacement = body + name_len;
  17350.         }

  17351.       macro_define_object (file, line, name, replacement);

  17352.       xfree (name);
  17353.     }
  17354.   else if (*p == '(')
  17355.     {
  17356.       /* It's a function-like macro.  */
  17357.       char *name = copy_string (body, p - body);
  17358.       int argc = 0;
  17359.       int argv_size = 1;
  17360.       char **argv = xmalloc (argv_size * sizeof (*argv));

  17361.       p++;

  17362.       p = consume_improper_spaces (p, body);

  17363.       /* Parse the formal argument list.  */
  17364.       while (*p && *p != ')')
  17365.         {
  17366.           /* Find the extent of the current argument name.  */
  17367.           const char *arg_start = p;

  17368.           while (*p && *p != ',' && *p != ')' && *p != ' ')
  17369.             p++;

  17370.           if (! *p || p == arg_start)
  17371.             dwarf2_macro_malformed_definition_complaint (body);
  17372.           else
  17373.             {
  17374.               /* Make sure argv has room for the new argument.  */
  17375.               if (argc >= argv_size)
  17376.                 {
  17377.                   argv_size *= 2;
  17378.                   argv = xrealloc (argv, argv_size * sizeof (*argv));
  17379.                 }

  17380.               argv[argc++] = copy_string (arg_start, p - arg_start);
  17381.             }

  17382.           p = consume_improper_spaces (p, body);

  17383.           /* Consume the comma, if present.  */
  17384.           if (*p == ',')
  17385.             {
  17386.               p++;

  17387.               p = consume_improper_spaces (p, body);
  17388.             }
  17389.         }

  17390.       if (*p == ')')
  17391.         {
  17392.           p++;

  17393.           if (*p == ' ')
  17394.             /* Perfectly formed definition, no complaints.  */
  17395.             macro_define_function (file, line, name,
  17396.                                    argc, (const char **) argv,
  17397.                                    p + 1);
  17398.           else if (*p == '\0')
  17399.             {
  17400.               /* Complain, but do define it.  */
  17401.               dwarf2_macro_malformed_definition_complaint (body);
  17402.               macro_define_function (file, line, name,
  17403.                                      argc, (const char **) argv,
  17404.                                      p);
  17405.             }
  17406.           else
  17407.             /* Just complain.  */
  17408.             dwarf2_macro_malformed_definition_complaint (body);
  17409.         }
  17410.       else
  17411.         /* Just complain.  */
  17412.         dwarf2_macro_malformed_definition_complaint (body);

  17413.       xfree (name);
  17414.       {
  17415.         int i;

  17416.         for (i = 0; i < argc; i++)
  17417.           xfree (argv[i]);
  17418.       }
  17419.       xfree (argv);
  17420.     }
  17421.   else
  17422.     dwarf2_macro_malformed_definition_complaint (body);
  17423. }

  17424. /* Skip some bytes from BYTES according to the form given in FORM.
  17425.    Returns the new pointer.  */

  17426. static const gdb_byte *
  17427. skip_form_bytes (bfd *abfd, const gdb_byte *bytes, const gdb_byte *buffer_end,
  17428.                  enum dwarf_form form,
  17429.                  unsigned int offset_size,
  17430.                  struct dwarf2_section_info *section)
  17431. {
  17432.   unsigned int bytes_read;

  17433.   switch (form)
  17434.     {
  17435.     case DW_FORM_data1:
  17436.     case DW_FORM_flag:
  17437.       ++bytes;
  17438.       break;

  17439.     case DW_FORM_data2:
  17440.       bytes += 2;
  17441.       break;

  17442.     case DW_FORM_data4:
  17443.       bytes += 4;
  17444.       break;

  17445.     case DW_FORM_data8:
  17446.       bytes += 8;
  17447.       break;

  17448.     case DW_FORM_string:
  17449.       read_direct_string (abfd, bytes, &bytes_read);
  17450.       bytes += bytes_read;
  17451.       break;

  17452.     case DW_FORM_sec_offset:
  17453.     case DW_FORM_strp:
  17454.     case DW_FORM_GNU_strp_alt:
  17455.       bytes += offset_size;
  17456.       break;

  17457.     case DW_FORM_block:
  17458.       bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
  17459.       bytes += bytes_read;
  17460.       break;

  17461.     case DW_FORM_block1:
  17462.       bytes += 1 + read_1_byte (abfd, bytes);
  17463.       break;
  17464.     case DW_FORM_block2:
  17465.       bytes += 2 + read_2_bytes (abfd, bytes);
  17466.       break;
  17467.     case DW_FORM_block4:
  17468.       bytes += 4 + read_4_bytes (abfd, bytes);
  17469.       break;

  17470.     case DW_FORM_sdata:
  17471.     case DW_FORM_udata:
  17472.     case DW_FORM_GNU_addr_index:
  17473.     case DW_FORM_GNU_str_index:
  17474.       bytes = gdb_skip_leb128 (bytes, buffer_end);
  17475.       if (bytes == NULL)
  17476.         {
  17477.           dwarf2_section_buffer_overflow_complaint (section);
  17478.           return NULL;
  17479.         }
  17480.       break;

  17481.     default:
  17482.       {
  17483.       complain:
  17484.         complaint (&symfile_complaints,
  17485.                    _("invalid form 0x%x in `%s'"),
  17486.                    form, get_section_name (section));
  17487.         return NULL;
  17488.       }
  17489.     }

  17490.   return bytes;
  17491. }

  17492. /* A helper for dwarf_decode_macros that handles skipping an unknown
  17493.    opcode.  Returns an updated pointer to the macro data buffer; or,
  17494.    on error, issues a complaint and returns NULL.  */

  17495. static const gdb_byte *
  17496. skip_unknown_opcode (unsigned int opcode,
  17497.                      const gdb_byte **opcode_definitions,
  17498.                      const gdb_byte *mac_ptr, const gdb_byte *mac_end,
  17499.                      bfd *abfd,
  17500.                      unsigned int offset_size,
  17501.                      struct dwarf2_section_info *section)
  17502. {
  17503.   unsigned int bytes_read, i;
  17504.   unsigned long arg;
  17505.   const gdb_byte *defn;

  17506.   if (opcode_definitions[opcode] == NULL)
  17507.     {
  17508.       complaint (&symfile_complaints,
  17509.                  _("unrecognized DW_MACFINO opcode 0x%x"),
  17510.                  opcode);
  17511.       return NULL;
  17512.     }

  17513.   defn = opcode_definitions[opcode];
  17514.   arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
  17515.   defn += bytes_read;

  17516.   for (i = 0; i < arg; ++i)
  17517.     {
  17518.       mac_ptr = skip_form_bytes (abfd, mac_ptr, mac_end, defn[i], offset_size,
  17519.                                  section);
  17520.       if (mac_ptr == NULL)
  17521.         {
  17522.           /* skip_form_bytes already issued the complaint.  */
  17523.           return NULL;
  17524.         }
  17525.     }

  17526.   return mac_ptr;
  17527. }

  17528. /* A helper function which parses the header of a macro section.
  17529.    If the macro section is the extended (for now called "GNU") type,
  17530.    then this updates *OFFSET_SIZE.  Returns a pointer to just after
  17531.    the header, or issues a complaint and returns NULL on error.  */

  17532. static const gdb_byte *
  17533. dwarf_parse_macro_header (const gdb_byte **opcode_definitions,
  17534.                           bfd *abfd,
  17535.                           const gdb_byte *mac_ptr,
  17536.                           unsigned int *offset_size,
  17537.                           int section_is_gnu)
  17538. {
  17539.   memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));

  17540.   if (section_is_gnu)
  17541.     {
  17542.       unsigned int version, flags;

  17543.       version = read_2_bytes (abfd, mac_ptr);
  17544.       if (version != 4)
  17545.         {
  17546.           complaint (&symfile_complaints,
  17547.                      _("unrecognized version `%d' in .debug_macro section"),
  17548.                      version);
  17549.           return NULL;
  17550.         }
  17551.       mac_ptr += 2;

  17552.       flags = read_1_byte (abfd, mac_ptr);
  17553.       ++mac_ptr;
  17554.       *offset_size = (flags & 1) ? 8 : 4;

  17555.       if ((flags & 2) != 0)
  17556.         /* We don't need the line table offset.  */
  17557.         mac_ptr += *offset_size;

  17558.       /* Vendor opcode descriptions.  */
  17559.       if ((flags & 4) != 0)
  17560.         {
  17561.           unsigned int i, count;

  17562.           count = read_1_byte (abfd, mac_ptr);
  17563.           ++mac_ptr;
  17564.           for (i = 0; i < count; ++i)
  17565.             {
  17566.               unsigned int opcode, bytes_read;
  17567.               unsigned long arg;

  17568.               opcode = read_1_byte (abfd, mac_ptr);
  17569.               ++mac_ptr;
  17570.               opcode_definitions[opcode] = mac_ptr;
  17571.               arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17572.               mac_ptr += bytes_read;
  17573.               mac_ptr += arg;
  17574.             }
  17575.         }
  17576.     }

  17577.   return mac_ptr;
  17578. }

  17579. /* A helper for dwarf_decode_macros that handles the GNU extensions,
  17580.    including DW_MACRO_GNU_transparent_include.  */

  17581. static void
  17582. dwarf_decode_macro_bytes (bfd *abfd,
  17583.                           const gdb_byte *mac_ptr, const gdb_byte *mac_end,
  17584.                           struct macro_source_file *current_file,
  17585.                           struct line_header *lh,
  17586.                           struct dwarf2_section_info *section,
  17587.                           int section_is_gnu, int section_is_dwz,
  17588.                           unsigned int offset_size,
  17589.                           htab_t include_hash)
  17590. {
  17591.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  17592.   enum dwarf_macro_record_type macinfo_type;
  17593.   int at_commandline;
  17594.   const gdb_byte *opcode_definitions[256];

  17595.   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
  17596.                                       &offset_size, section_is_gnu);
  17597.   if (mac_ptr == NULL)
  17598.     {
  17599.       /* We already issued a complaint.  */
  17600.       return;
  17601.     }

  17602.   /* Determines if GDB is still before first DW_MACINFO_start_file.  If true
  17603.      GDB is still reading the definitions from command line.  First
  17604.      DW_MACINFO_start_file will need to be ignored as it was already executed
  17605.      to create CURRENT_FILE for the main source holding also the command line
  17606.      definitions.  On first met DW_MACINFO_start_file this flag is reset to
  17607.      normally execute all the remaining DW_MACINFO_start_file macinfos.  */

  17608.   at_commandline = 1;

  17609.   do
  17610.     {
  17611.       /* Do we at least have room for a macinfo type byte?  */
  17612.       if (mac_ptr >= mac_end)
  17613.         {
  17614.           dwarf2_section_buffer_overflow_complaint (section);
  17615.           break;
  17616.         }

  17617.       macinfo_type = read_1_byte (abfd, mac_ptr);
  17618.       mac_ptr++;

  17619.       /* Note that we rely on the fact that the corresponding GNU and
  17620.          DWARF constants are the same.  */
  17621.       switch (macinfo_type)
  17622.         {
  17623.           /* A zero macinfo type indicates the end of the macro
  17624.              information.  */
  17625.         case 0:
  17626.           break;

  17627.         case DW_MACRO_GNU_define:
  17628.         case DW_MACRO_GNU_undef:
  17629.         case DW_MACRO_GNU_define_indirect:
  17630.         case DW_MACRO_GNU_undef_indirect:
  17631.         case DW_MACRO_GNU_define_indirect_alt:
  17632.         case DW_MACRO_GNU_undef_indirect_alt:
  17633.           {
  17634.             unsigned int bytes_read;
  17635.             int line;
  17636.             const char *body;
  17637.             int is_define;

  17638.             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17639.             mac_ptr += bytes_read;

  17640.             if (macinfo_type == DW_MACRO_GNU_define
  17641.                 || macinfo_type == DW_MACRO_GNU_undef)
  17642.               {
  17643.                 body = read_direct_string (abfd, mac_ptr, &bytes_read);
  17644.                 mac_ptr += bytes_read;
  17645.               }
  17646.             else
  17647.               {
  17648.                 LONGEST str_offset;

  17649.                 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
  17650.                 mac_ptr += offset_size;

  17651.                 if (macinfo_type == DW_MACRO_GNU_define_indirect_alt
  17652.                     || macinfo_type == DW_MACRO_GNU_undef_indirect_alt
  17653.                     || section_is_dwz)
  17654.                   {
  17655.                     struct dwz_file *dwz = dwarf2_get_dwz_file ();

  17656.                     body = read_indirect_string_from_dwz (dwz, str_offset);
  17657.                   }
  17658.                 else
  17659.                   body = read_indirect_string_at_offset (abfd, str_offset);
  17660.               }

  17661.             is_define = (macinfo_type == DW_MACRO_GNU_define
  17662.                          || macinfo_type == DW_MACRO_GNU_define_indirect
  17663.                          || macinfo_type == DW_MACRO_GNU_define_indirect_alt);
  17664.             if (! current_file)
  17665.               {
  17666.                 /* DWARF violation as no main source is present.  */
  17667.                 complaint (&symfile_complaints,
  17668.                            _("debug info with no main source gives macro %s "
  17669.                              "on line %d: %s"),
  17670.                            is_define ? _("definition") : _("undefinition"),
  17671.                            line, body);
  17672.                 break;
  17673.               }
  17674.             if ((line == 0 && !at_commandline)
  17675.                 || (line != 0 && at_commandline))
  17676.               complaint (&symfile_complaints,
  17677.                          _("debug info gives %s macro %s with %s line %d: %s"),
  17678.                          at_commandline ? _("command-line") : _("in-file"),
  17679.                          is_define ? _("definition") : _("undefinition"),
  17680.                          line == 0 ? _("zero") : _("non-zero"), line, body);

  17681.             if (is_define)
  17682.               parse_macro_definition (current_file, line, body);
  17683.             else
  17684.               {
  17685.                 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
  17686.                             || macinfo_type == DW_MACRO_GNU_undef_indirect
  17687.                             || macinfo_type == DW_MACRO_GNU_undef_indirect_alt);
  17688.                 macro_undef (current_file, line, body);
  17689.               }
  17690.           }
  17691.           break;

  17692.         case DW_MACRO_GNU_start_file:
  17693.           {
  17694.             unsigned int bytes_read;
  17695.             int line, file;

  17696.             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17697.             mac_ptr += bytes_read;
  17698.             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17699.             mac_ptr += bytes_read;

  17700.             if ((line == 0 && !at_commandline)
  17701.                 || (line != 0 && at_commandline))
  17702.               complaint (&symfile_complaints,
  17703.                          _("debug info gives source %d included "
  17704.                            "from %s at %s line %d"),
  17705.                          file, at_commandline ? _("command-line") : _("file"),
  17706.                          line == 0 ? _("zero") : _("non-zero"), line);

  17707.             if (at_commandline)
  17708.               {
  17709.                 /* This DW_MACRO_GNU_start_file was executed in the
  17710.                    pass one.  */
  17711.                 at_commandline = 0;
  17712.               }
  17713.             else
  17714.               current_file = macro_start_file (file, line, current_file, lh);
  17715.           }
  17716.           break;

  17717.         case DW_MACRO_GNU_end_file:
  17718.           if (! current_file)
  17719.             complaint (&symfile_complaints,
  17720.                        _("macro debug info has an unmatched "
  17721.                          "`close_file' directive"));
  17722.           else
  17723.             {
  17724.               current_file = current_file->included_by;
  17725.               if (! current_file)
  17726.                 {
  17727.                   enum dwarf_macro_record_type next_type;

  17728.                   /* GCC circa March 2002 doesn't produce the zero
  17729.                      type byte marking the end of the compilation
  17730.                      unit.  Complain if it's not there, but exit no
  17731.                      matter what.  */

  17732.                   /* Do we at least have room for a macinfo type byte?  */
  17733.                   if (mac_ptr >= mac_end)
  17734.                     {
  17735.                       dwarf2_section_buffer_overflow_complaint (section);
  17736.                       return;
  17737.                     }

  17738.                   /* We don't increment mac_ptr here, so this is just
  17739.                      a look-ahead.  */
  17740.                   next_type = read_1_byte (abfd, mac_ptr);
  17741.                   if (next_type != 0)
  17742.                     complaint (&symfile_complaints,
  17743.                                _("no terminating 0-type entry for "
  17744.                                  "macros in `.debug_macinfo' section"));

  17745.                   return;
  17746.                 }
  17747.             }
  17748.           break;

  17749.         case DW_MACRO_GNU_transparent_include:
  17750.         case DW_MACRO_GNU_transparent_include_alt:
  17751.           {
  17752.             LONGEST offset;
  17753.             void **slot;
  17754.             bfd *include_bfd = abfd;
  17755.             struct dwarf2_section_info *include_section = section;
  17756.             struct dwarf2_section_info alt_section;
  17757.             const gdb_byte *include_mac_end = mac_end;
  17758.             int is_dwz = section_is_dwz;
  17759.             const gdb_byte *new_mac_ptr;

  17760.             offset = read_offset_1 (abfd, mac_ptr, offset_size);
  17761.             mac_ptr += offset_size;

  17762.             if (macinfo_type == DW_MACRO_GNU_transparent_include_alt)
  17763.               {
  17764.                 struct dwz_file *dwz = dwarf2_get_dwz_file ();

  17765.                 dwarf2_read_section (objfile, &dwz->macro);

  17766.                 include_section = &dwz->macro;
  17767.                 include_bfd = get_section_bfd_owner (include_section);
  17768.                 include_mac_end = dwz->macro.buffer + dwz->macro.size;
  17769.                 is_dwz = 1;
  17770.               }

  17771.             new_mac_ptr = include_section->buffer + offset;
  17772.             slot = htab_find_slot (include_hash, new_mac_ptr, INSERT);

  17773.             if (*slot != NULL)
  17774.               {
  17775.                 /* This has actually happened; see
  17776.                    http://sourceware.org/bugzilla/show_bug.cgi?id=13568.  */
  17777.                 complaint (&symfile_complaints,
  17778.                            _("recursive DW_MACRO_GNU_transparent_include in "
  17779.                              ".debug_macro section"));
  17780.               }
  17781.             else
  17782.               {
  17783.                 *slot = (void *) new_mac_ptr;

  17784.                 dwarf_decode_macro_bytes (include_bfd, new_mac_ptr,
  17785.                                           include_mac_end, current_file, lh,
  17786.                                           section, section_is_gnu, is_dwz,
  17787.                                           offset_size, include_hash);

  17788.                 htab_remove_elt (include_hash, (void *) new_mac_ptr);
  17789.               }
  17790.           }
  17791.           break;

  17792.         case DW_MACINFO_vendor_ext:
  17793.           if (!section_is_gnu)
  17794.             {
  17795.               unsigned int bytes_read;
  17796.               int constant;

  17797.               constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17798.               mac_ptr += bytes_read;
  17799.               read_direct_string (abfd, mac_ptr, &bytes_read);
  17800.               mac_ptr += bytes_read;

  17801.               /* We don't recognize any vendor extensions.  */
  17802.               break;
  17803.             }
  17804.           /* FALLTHROUGH */

  17805.         default:
  17806.           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
  17807.                                          mac_ptr, mac_end, abfd, offset_size,
  17808.                                          section);
  17809.           if (mac_ptr == NULL)
  17810.             return;
  17811.           break;
  17812.         }
  17813.     } while (macinfo_type != 0);
  17814. }

  17815. static void
  17816. dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
  17817.                      int section_is_gnu)
  17818. {
  17819.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  17820.   struct line_header *lh = cu->line_header;
  17821.   bfd *abfd;
  17822.   const gdb_byte *mac_ptr, *mac_end;
  17823.   struct macro_source_file *current_file = 0;
  17824.   enum dwarf_macro_record_type macinfo_type;
  17825.   unsigned int offset_size = cu->header.offset_size;
  17826.   const gdb_byte *opcode_definitions[256];
  17827.   struct cleanup *cleanup;
  17828.   htab_t include_hash;
  17829.   void **slot;
  17830.   struct dwarf2_section_info *section;
  17831.   const char *section_name;

  17832.   if (cu->dwo_unit != NULL)
  17833.     {
  17834.       if (section_is_gnu)
  17835.         {
  17836.           section = &cu->dwo_unit->dwo_file->sections.macro;
  17837.           section_name = ".debug_macro.dwo";
  17838.         }
  17839.       else
  17840.         {
  17841.           section = &cu->dwo_unit->dwo_file->sections.macinfo;
  17842.           section_name = ".debug_macinfo.dwo";
  17843.         }
  17844.     }
  17845.   else
  17846.     {
  17847.       if (section_is_gnu)
  17848.         {
  17849.           section = &dwarf2_per_objfile->macro;
  17850.           section_name = ".debug_macro";
  17851.         }
  17852.       else
  17853.         {
  17854.           section = &dwarf2_per_objfile->macinfo;
  17855.           section_name = ".debug_macinfo";
  17856.         }
  17857.     }

  17858.   dwarf2_read_section (objfile, section);
  17859.   if (section->buffer == NULL)
  17860.     {
  17861.       complaint (&symfile_complaints, _("missing %s section"), section_name);
  17862.       return;
  17863.     }
  17864.   abfd = get_section_bfd_owner (section);

  17865.   /* First pass: Find the name of the base filename.
  17866.      This filename is needed in order to process all macros whose definition
  17867.      (or undefinition) comes from the command line.  These macros are defined
  17868.      before the first DW_MACINFO_start_file entry, and yet still need to be
  17869.      associated to the base file.

  17870.      To determine the base file name, we scan the macro definitions until we
  17871.      reach the first DW_MACINFO_start_file entry.  We then initialize
  17872.      CURRENT_FILE accordingly so that any macro definition found before the
  17873.      first DW_MACINFO_start_file can still be associated to the base file.  */

  17874.   mac_ptr = section->buffer + offset;
  17875.   mac_end = section->buffer + section->size;

  17876.   mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
  17877.                                       &offset_size, section_is_gnu);
  17878.   if (mac_ptr == NULL)
  17879.     {
  17880.       /* We already issued a complaint.  */
  17881.       return;
  17882.     }

  17883.   do
  17884.     {
  17885.       /* Do we at least have room for a macinfo type byte?  */
  17886.       if (mac_ptr >= mac_end)
  17887.         {
  17888.           /* Complaint is printed during the second pass as GDB will probably
  17889.              stop the first pass earlier upon finding
  17890.              DW_MACINFO_start_file.  */
  17891.           break;
  17892.         }

  17893.       macinfo_type = read_1_byte (abfd, mac_ptr);
  17894.       mac_ptr++;

  17895.       /* Note that we rely on the fact that the corresponding GNU and
  17896.          DWARF constants are the same.  */
  17897.       switch (macinfo_type)
  17898.         {
  17899.           /* A zero macinfo type indicates the end of the macro
  17900.              information.  */
  17901.         case 0:
  17902.           break;

  17903.         case DW_MACRO_GNU_define:
  17904.         case DW_MACRO_GNU_undef:
  17905.           /* Only skip the data by MAC_PTR.  */
  17906.           {
  17907.             unsigned int bytes_read;

  17908.             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17909.             mac_ptr += bytes_read;
  17910.             read_direct_string (abfd, mac_ptr, &bytes_read);
  17911.             mac_ptr += bytes_read;
  17912.           }
  17913.           break;

  17914.         case DW_MACRO_GNU_start_file:
  17915.           {
  17916.             unsigned int bytes_read;
  17917.             int line, file;

  17918.             line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17919.             mac_ptr += bytes_read;
  17920.             file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17921.             mac_ptr += bytes_read;

  17922.             current_file = macro_start_file (file, line, current_file, lh);
  17923.           }
  17924.           break;

  17925.         case DW_MACRO_GNU_end_file:
  17926.           /* No data to skip by MAC_PTR.  */
  17927.           break;

  17928.         case DW_MACRO_GNU_define_indirect:
  17929.         case DW_MACRO_GNU_undef_indirect:
  17930.         case DW_MACRO_GNU_define_indirect_alt:
  17931.         case DW_MACRO_GNU_undef_indirect_alt:
  17932.           {
  17933.             unsigned int bytes_read;

  17934.             read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17935.             mac_ptr += bytes_read;
  17936.             mac_ptr += offset_size;
  17937.           }
  17938.           break;

  17939.         case DW_MACRO_GNU_transparent_include:
  17940.         case DW_MACRO_GNU_transparent_include_alt:
  17941.           /* Note that, according to the spec, a transparent include
  17942.              chain cannot call DW_MACRO_GNU_start_file.  So, we can just
  17943.              skip this opcode.  */
  17944.           mac_ptr += offset_size;
  17945.           break;

  17946.         case DW_MACINFO_vendor_ext:
  17947.           /* Only skip the data by MAC_PTR.  */
  17948.           if (!section_is_gnu)
  17949.             {
  17950.               unsigned int bytes_read;

  17951.               read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
  17952.               mac_ptr += bytes_read;
  17953.               read_direct_string (abfd, mac_ptr, &bytes_read);
  17954.               mac_ptr += bytes_read;
  17955.             }
  17956.           /* FALLTHROUGH */

  17957.         default:
  17958.           mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
  17959.                                          mac_ptr, mac_end, abfd, offset_size,
  17960.                                          section);
  17961.           if (mac_ptr == NULL)
  17962.             return;
  17963.           break;
  17964.         }
  17965.     } while (macinfo_type != 0 && current_file == NULL);

  17966.   /* Second pass: Process all entries.

  17967.      Use the AT_COMMAND_LINE flag to determine whether we are still processing
  17968.      command-line macro definitions/undefinitions.  This flag is unset when we
  17969.      reach the first DW_MACINFO_start_file entry.  */

  17970.   include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
  17971.                                     NULL, xcalloc, xfree);
  17972.   cleanup = make_cleanup_htab_delete (include_hash);
  17973.   mac_ptr = section->buffer + offset;
  17974.   slot = htab_find_slot (include_hash, mac_ptr, INSERT);
  17975.   *slot = (void *) mac_ptr;
  17976.   dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
  17977.                             current_file, lh, section,
  17978.                             section_is_gnu, 0, offset_size, include_hash);
  17979.   do_cleanups (cleanup);
  17980. }

  17981. /* Check if the attribute's form is a DW_FORM_block*
  17982.    if so return true else false.  */

  17983. static int
  17984. attr_form_is_block (const struct attribute *attr)
  17985. {
  17986.   return (attr == NULL ? 0 :
  17987.       attr->form == DW_FORM_block1
  17988.       || attr->form == DW_FORM_block2
  17989.       || attr->form == DW_FORM_block4
  17990.       || attr->form == DW_FORM_block
  17991.       || attr->form == DW_FORM_exprloc);
  17992. }

  17993. /* Return non-zero if ATTR's value is a section offset --- classes
  17994.    lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
  17995.    You may use DW_UNSND (attr) to retrieve such offsets.

  17996.    Section 7.5.4, "Attribute Encodings", explains that no attribute
  17997.    may have a value that belongs to more than one of these classes; it
  17998.    would be ambiguous if we did, because we use the same forms for all
  17999.    of them.  */

  18000. static int
  18001. attr_form_is_section_offset (const struct attribute *attr)
  18002. {
  18003.   return (attr->form == DW_FORM_data4
  18004.           || attr->form == DW_FORM_data8
  18005.           || attr->form == DW_FORM_sec_offset);
  18006. }

  18007. /* Return non-zero if ATTR's value falls in the 'constant' class, or
  18008.    zero otherwise.  When this function returns true, you can apply
  18009.    dwarf2_get_attr_constant_value to it.

  18010.    However, note that for some attributes you must check
  18011.    attr_form_is_section_offset before using this test.  DW_FORM_data4
  18012.    and DW_FORM_data8 are members of both the constant class, and of
  18013.    the classes that contain offsets into other debug sections
  18014.    (lineptr, loclistptr, macptr or rangelistptr).  The DWARF spec says
  18015.    that, if an attribute's can be either a constant or one of the
  18016.    section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
  18017.    taken as section offsets, not constants.  */

  18018. static int
  18019. attr_form_is_constant (const struct attribute *attr)
  18020. {
  18021.   switch (attr->form)
  18022.     {
  18023.     case DW_FORM_sdata:
  18024.     case DW_FORM_udata:
  18025.     case DW_FORM_data1:
  18026.     case DW_FORM_data2:
  18027.     case DW_FORM_data4:
  18028.     case DW_FORM_data8:
  18029.       return 1;
  18030.     default:
  18031.       return 0;
  18032.     }
  18033. }


  18034. /* DW_ADDR is always stored already as sect_offset; despite for the forms
  18035.    besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file.  */

  18036. static int
  18037. attr_form_is_ref (const struct attribute *attr)
  18038. {
  18039.   switch (attr->form)
  18040.     {
  18041.     case DW_FORM_ref_addr:
  18042.     case DW_FORM_ref1:
  18043.     case DW_FORM_ref2:
  18044.     case DW_FORM_ref4:
  18045.     case DW_FORM_ref8:
  18046.     case DW_FORM_ref_udata:
  18047.     case DW_FORM_GNU_ref_alt:
  18048.       return 1;
  18049.     default:
  18050.       return 0;
  18051.     }
  18052. }

  18053. /* Return the .debug_loc section to use for CU.
  18054.    For DWO files use .debug_loc.dwo.  */

  18055. static struct dwarf2_section_info *
  18056. cu_debug_loc_section (struct dwarf2_cu *cu)
  18057. {
  18058.   if (cu->dwo_unit)
  18059.     return &cu->dwo_unit->dwo_file->sections.loc;
  18060.   return &dwarf2_per_objfile->loc;
  18061. }

  18062. /* A helper function that fills in a dwarf2_loclist_baton.  */

  18063. static void
  18064. fill_in_loclist_baton (struct dwarf2_cu *cu,
  18065.                        struct dwarf2_loclist_baton *baton,
  18066.                        const struct attribute *attr)
  18067. {
  18068.   struct dwarf2_section_info *section = cu_debug_loc_section (cu);

  18069.   dwarf2_read_section (dwarf2_per_objfile->objfile, section);

  18070.   baton->per_cu = cu->per_cu;
  18071.   gdb_assert (baton->per_cu);
  18072.   /* We don't know how long the location list is, but make sure we
  18073.      don't run off the edge of the section.  */
  18074.   baton->size = section->size - DW_UNSND (attr);
  18075.   baton->data = section->buffer + DW_UNSND (attr);
  18076.   baton->base_address = cu->base_address;
  18077.   baton->from_dwo = cu->dwo_unit != NULL;
  18078. }

  18079. static void
  18080. dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
  18081.                              struct dwarf2_cu *cu, int is_block)
  18082. {
  18083.   struct objfile *objfile = dwarf2_per_objfile->objfile;
  18084.   struct dwarf2_section_info *section = cu_debug_loc_section (cu);

  18085.   if (attr_form_is_section_offset (attr)
  18086.       /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
  18087.          the section.  If so, fall through to the complaint in the
  18088.          other branch.  */
  18089.       && DW_UNSND (attr) < dwarf2_section_size (objfile, section))
  18090.     {
  18091.       struct dwarf2_loclist_baton *baton;

  18092.       baton = obstack_alloc (&objfile->objfile_obstack,
  18093.                              sizeof (struct dwarf2_loclist_baton));

  18094.       fill_in_loclist_baton (cu, baton, attr);

  18095.       if (cu->base_known == 0)
  18096.         complaint (&symfile_complaints,
  18097.                    _("Location list used without "
  18098.                      "specifying the CU base address."));

  18099.       SYMBOL_ACLASS_INDEX (sym) = (is_block
  18100.                                    ? dwarf2_loclist_block_index
  18101.                                    : dwarf2_loclist_index);
  18102.       SYMBOL_LOCATION_BATON (sym) = baton;
  18103.     }
  18104.   else
  18105.     {
  18106.       struct dwarf2_locexpr_baton *baton;

  18107.       baton = obstack_alloc (&objfile->objfile_obstack,
  18108.                              sizeof (struct dwarf2_locexpr_baton));
  18109.       baton->per_cu = cu->per_cu;
  18110.       gdb_assert (baton->per_cu);

  18111.       if (attr_form_is_block (attr))
  18112.         {
  18113.           /* Note that we're just copying the block's data pointer
  18114.              here, not the actual data.  We're still pointing into the
  18115.              info_buffer for SYM's objfile; right now we never release
  18116.              that buffer, but when we do clean up properly this may
  18117.              need to change.  */
  18118.           baton->size = DW_BLOCK (attr)->size;
  18119.           baton->data = DW_BLOCK (attr)->data;
  18120.         }
  18121.       else
  18122.         {
  18123.           dwarf2_invalid_attrib_class_complaint ("location description",
  18124.                                                  SYMBOL_NATURAL_NAME (sym));
  18125.           baton->size = 0;
  18126.         }

  18127.       SYMBOL_ACLASS_INDEX (sym) = (is_block
  18128.                                    ? dwarf2_locexpr_block_index
  18129.                                    : dwarf2_locexpr_index);
  18130.       SYMBOL_LOCATION_BATON (sym) = baton;
  18131.     }
  18132. }

  18133. /* Return the OBJFILE associated with the compilation unit CU.  If CU
  18134.    came from a separate debuginfo file, then the master objfile is
  18135.    returned.  */

  18136. struct objfile *
  18137. dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
  18138. {
  18139.   struct objfile *objfile = per_cu->objfile;

  18140.   /* Return the master objfile, so that we can report and look up the
  18141.      correct file containing this variable.  */
  18142.   if (objfile->separate_debug_objfile_backlink)
  18143.     objfile = objfile->separate_debug_objfile_backlink;

  18144.   return objfile;
  18145. }

  18146. /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
  18147.    (CU_HEADERP is unused in such case) or prepare a temporary copy at
  18148.    CU_HEADERP first.  */

  18149. static const struct comp_unit_head *
  18150. per_cu_header_read_in (struct comp_unit_head *cu_headerp,
  18151.                        struct dwarf2_per_cu_data *per_cu)
  18152. {
  18153.   const gdb_byte *info_ptr;

  18154.   if (per_cu->cu)
  18155.     return &per_cu->cu->header;

  18156.   info_ptr = per_cu->section->buffer + per_cu->offset.sect_off;

  18157.   memset (cu_headerp, 0, sizeof (*cu_headerp));
  18158.   read_comp_unit_head (cu_headerp, info_ptr, per_cu->objfile->obfd);

  18159.   return cu_headerp;
  18160. }

  18161. /* Return the address size given in the compilation unit header for CU.  */

  18162. int
  18163. dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
  18164. {
  18165.   struct comp_unit_head cu_header_local;
  18166.   const struct comp_unit_head *cu_headerp;

  18167.   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);

  18168.   return cu_headerp->addr_size;
  18169. }

  18170. /* Return the offset size given in the compilation unit header for CU.  */

  18171. int
  18172. dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
  18173. {
  18174.   struct comp_unit_head cu_header_local;
  18175.   const struct comp_unit_head *cu_headerp;

  18176.   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);

  18177.   return cu_headerp->offset_size;
  18178. }

  18179. /* See its dwarf2loc.h declaration.  */

  18180. int
  18181. dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
  18182. {
  18183.   struct comp_unit_head cu_header_local;
  18184.   const struct comp_unit_head *cu_headerp;

  18185.   cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);

  18186.   if (cu_headerp->version == 2)
  18187.     return cu_headerp->addr_size;
  18188.   else
  18189.     return cu_headerp->offset_size;
  18190. }

  18191. /* Return the text offset of the CU.  The returned offset comes from
  18192.    this CU's objfile.  If this objfile came from a separate debuginfo
  18193.    file, then the offset may be different from the corresponding
  18194.    offset in the parent objfile.  */

  18195. CORE_ADDR
  18196. dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
  18197. {
  18198.   struct objfile *objfile = per_cu->objfile;

  18199.   return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
  18200. }

  18201. /* Locate the .debug_info compilation unit from CU's objfile which contains
  18202.    the DIE at OFFSET.  Raises an error on failure.  */

  18203. static struct dwarf2_per_cu_data *
  18204. dwarf2_find_containing_comp_unit (sect_offset offset,
  18205.                                   unsigned int offset_in_dwz,
  18206.                                   struct objfile *objfile)
  18207. {
  18208.   struct dwarf2_per_cu_data *this_cu;
  18209.   int low, high;
  18210.   const sect_offset *cu_off;

  18211.   low = 0;
  18212.   high = dwarf2_per_objfile->n_comp_units - 1;
  18213.   while (high > low)
  18214.     {
  18215.       struct dwarf2_per_cu_data *mid_cu;
  18216.       int mid = low + (high - low) / 2;

  18217.       mid_cu = dwarf2_per_objfile->all_comp_units[mid];
  18218.       cu_off = &mid_cu->offset;
  18219.       if (mid_cu->is_dwz > offset_in_dwz
  18220.           || (mid_cu->is_dwz == offset_in_dwz
  18221.               && cu_off->sect_off >= offset.sect_off))
  18222.         high = mid;
  18223.       else
  18224.         low = mid + 1;
  18225.     }
  18226.   gdb_assert (low == high);
  18227.   this_cu = dwarf2_per_objfile->all_comp_units[low];
  18228.   cu_off = &this_cu->offset;
  18229.   if (this_cu->is_dwz != offset_in_dwz || cu_off->sect_off > offset.sect_off)
  18230.     {
  18231.       if (low == 0 || this_cu->is_dwz != offset_in_dwz)
  18232.         error (_("Dwarf Error: could not find partial DIE containing "
  18233.                "offset 0x%lx [in module %s]"),
  18234.                (long) offset.sect_off, bfd_get_filename (objfile->obfd));

  18235.       gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
  18236.                   <= offset.sect_off);
  18237.       return dwarf2_per_objfile->all_comp_units[low-1];
  18238.     }
  18239.   else
  18240.     {
  18241.       this_cu = dwarf2_per_objfile->all_comp_units[low];
  18242.       if (low == dwarf2_per_objfile->n_comp_units - 1
  18243.           && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
  18244.         error (_("invalid dwarf2 offset %u"), offset.sect_off);
  18245.       gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
  18246.       return this_cu;
  18247.     }
  18248. }

  18249. /* Initialize dwarf2_cu CU, owned by PER_CU.  */

  18250. static void
  18251. init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
  18252. {
  18253.   memset (cu, 0, sizeof (*cu));
  18254.   per_cu->cu = cu;
  18255.   cu->per_cu = per_cu;
  18256.   cu->objfile = per_cu->objfile;
  18257.   obstack_init (&cu->comp_unit_obstack);
  18258. }

  18259. /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE.  */

  18260. static void
  18261. prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
  18262.                        enum language pretend_language)
  18263. {
  18264.   struct attribute *attr;

  18265.   /* Set the language we're debugging.  */
  18266.   attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
  18267.   if (attr)
  18268.     set_cu_language (DW_UNSND (attr), cu);
  18269.   else
  18270.     {
  18271.       cu->language = pretend_language;
  18272.       cu->language_defn = language_def (cu->language);
  18273.     }

  18274.   attr = dwarf2_attr (comp_unit_die, DW_AT_producer, cu);
  18275.   if (attr)
  18276.     cu->producer = DW_STRING (attr);
  18277. }

  18278. /* Release one cached compilation unit, CU.  We unlink it from the tree
  18279.    of compilation units, but we don't remove it from the read_in_chain;
  18280.    the caller is responsible for that.
  18281.    NOTE: DATA is a void * because this function is also used as a
  18282.    cleanup routine.  */

  18283. static void
  18284. free_heap_comp_unit (void *data)
  18285. {
  18286.   struct dwarf2_cu *cu = data;

  18287.   gdb_assert (cu->per_cu != NULL);
  18288.   cu->per_cu->cu = NULL;
  18289.   cu->per_cu = NULL;

  18290.   obstack_free (&cu->comp_unit_obstack, NULL);

  18291.   xfree (cu);
  18292. }

  18293. /* This cleanup function is passed the address of a dwarf2_cu on the stack
  18294.    when we're finished with it.  We can't free the pointer itself, but be
  18295.    sure to unlink it from the cache.  Also release any associated storage.  */

  18296. static void
  18297. free_stack_comp_unit (void *data)
  18298. {
  18299.   struct dwarf2_cu *cu = data;

  18300.   gdb_assert (cu->per_cu != NULL);
  18301.   cu->per_cu->cu = NULL;
  18302.   cu->per_cu = NULL;

  18303.   obstack_free (&cu->comp_unit_obstack, NULL);
  18304.   cu->partial_dies = NULL;
  18305. }

  18306. /* Free all cached compilation units.  */

  18307. static void
  18308. free_cached_comp_units (void *data)
  18309. {
  18310.   struct dwarf2_per_cu_data *per_cu, **last_chain;

  18311.   per_cu = dwarf2_per_objfile->read_in_chain;
  18312.   last_chain = &dwarf2_per_objfile->read_in_chain;
  18313.   while (per_cu != NULL)
  18314.     {
  18315.       struct dwarf2_per_cu_data *next_cu;

  18316.       next_cu = per_cu->cu->read_in_chain;

  18317.       free_heap_comp_unit (per_cu->cu);
  18318.       *last_chain = next_cu;

  18319.       per_cu = next_cu;
  18320.     }
  18321. }

  18322. /* Increase the age counter on each cached compilation unit, and free
  18323.    any that are too old.  */

  18324. static void
  18325. age_cached_comp_units (void)
  18326. {
  18327.   struct dwarf2_per_cu_data *per_cu, **last_chain;

  18328.   dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
  18329.   per_cu = dwarf2_per_objfile->read_in_chain;
  18330.   while (per_cu != NULL)
  18331.     {
  18332.       per_cu->cu->last_used ++;
  18333.       if (per_cu->cu->last_used <= dwarf2_max_cache_age)
  18334.         dwarf2_mark (per_cu->cu);
  18335.       per_cu = per_cu->cu->read_in_chain;
  18336.     }

  18337.   per_cu = dwarf2_per_objfile->read_in_chain;
  18338.   last_chain = &dwarf2_per_objfile->read_in_chain;
  18339.   while (per_cu != NULL)
  18340.     {
  18341.       struct dwarf2_per_cu_data *next_cu;

  18342.       next_cu = per_cu->cu->read_in_chain;

  18343.       if (!per_cu->cu->mark)
  18344.         {
  18345.           free_heap_comp_unit (per_cu->cu);
  18346.           *last_chain = next_cu;
  18347.         }
  18348.       else
  18349.         last_chain = &per_cu->cu->read_in_chain;

  18350.       per_cu = next_cu;
  18351.     }
  18352. }

  18353. /* Remove a single compilation unit from the cache.  */

  18354. static void
  18355. free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
  18356. {
  18357.   struct dwarf2_per_cu_data *per_cu, **last_chain;

  18358.   per_cu = dwarf2_per_objfile->read_in_chain;
  18359.   last_chain = &dwarf2_per_objfile->read_in_chain;
  18360.   while (per_cu != NULL)
  18361.     {
  18362.       struct dwarf2_per_cu_data *next_cu;

  18363.       next_cu = per_cu->cu->read_in_chain;

  18364.       if (per_cu == target_per_cu)
  18365.         {
  18366.           free_heap_comp_unit (per_cu->cu);
  18367.           per_cu->cu = NULL;
  18368.           *last_chain = next_cu;
  18369.           break;
  18370.         }
  18371.       else
  18372.         last_chain = &per_cu->cu->read_in_chain;

  18373.       per_cu = next_cu;
  18374.     }
  18375. }

  18376. /* Release all extra memory associated with OBJFILE.  */

  18377. void
  18378. dwarf2_free_objfile (struct objfile *objfile)
  18379. {
  18380.   dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);

  18381.   if (dwarf2_per_objfile == NULL)
  18382.     return;

  18383.   /* Cached DIE trees use xmalloc and the comp_unit_obstack.  */
  18384.   free_cached_comp_units (NULL);

  18385.   if (dwarf2_per_objfile->quick_file_names_table)
  18386.     htab_delete (dwarf2_per_objfile->quick_file_names_table);

  18387.   /* Everything else should be on the objfile obstack.  */
  18388. }

  18389. /* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
  18390.    We store these in a hash table separate from the DIEs, and preserve them
  18391.    when the DIEs are flushed out of cache.

  18392.    The CU "per_cu" pointer is needed because offset alone is not enough to
  18393.    uniquely identify the typeA file may have multiple .debug_types sections,
  18394.    or the type may come from a DWO file.  Furthermore, while it's more logical
  18395.    to use per_cu->section+offset, with Fission the section with the data is in
  18396.    the DWO file but we don't know that section at the point we need it.
  18397.    We have to use something in dwarf2_per_cu_data (or the pointer to it)
  18398.    because we can enter the lookup routine, get_die_type_at_offset, from
  18399.    outside this file, and thus won't necessarily have PER_CU->cu.
  18400.    Fortunately, PER_CU is stable for the life of the objfile.  */

  18401. struct dwarf2_per_cu_offset_and_type
  18402. {
  18403.   const struct dwarf2_per_cu_data *per_cu;
  18404.   sect_offset offset;
  18405.   struct type *type;
  18406. };

  18407. /* Hash function for a dwarf2_per_cu_offset_and_type.  */

  18408. static hashval_t
  18409. per_cu_offset_and_type_hash (const void *item)
  18410. {
  18411.   const struct dwarf2_per_cu_offset_and_type *ofs = item;

  18412.   return (uintptr_t) ofs->per_cu + ofs->offset.sect_off;
  18413. }

  18414. /* Equality function for a dwarf2_per_cu_offset_and_type.  */

  18415. static int
  18416. per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
  18417. {
  18418.   const struct dwarf2_per_cu_offset_and_type *ofs_lhs = item_lhs;
  18419.   const struct dwarf2_per_cu_offset_and_type *ofs_rhs = item_rhs;

  18420.   return (ofs_lhs->per_cu == ofs_rhs->per_cu
  18421.           && ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off);
  18422. }

  18423. /* Set the type associated with DIE to TYPE.  Save it in CU's hash
  18424.    table if necessary.  For convenience, return TYPE.

  18425.    The DIEs reading must have careful ordering to:
  18426.     * Not cause infite loops trying to read in DIEs as a prerequisite for
  18427.       reading current DIE.
  18428.     * Not trying to dereference contents of still incompletely read in types
  18429.       while reading in other DIEs.
  18430.     * Enable referencing still incompletely read in types just by a pointer to
  18431.       the type without accessing its fields.

  18432.    Therefore caller should follow these rules:
  18433.      * Try to fetch any prerequisite types we may need to build this DIE type
  18434.        before building the type and calling set_die_type.
  18435.      * After building type call set_die_type for current DIE as soon as
  18436.        possible before fetching more types to complete the current type.
  18437.      * Make the type as complete as possible before fetching more types.  */

  18438. static struct type *
  18439. set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
  18440. {
  18441.   struct dwarf2_per_cu_offset_and_type **slot, ofs;
  18442.   struct objfile *objfile = cu->objfile;
  18443.   struct attribute *attr;
  18444.   struct dynamic_prop prop;

  18445.   /* For Ada types, make sure that the gnat-specific data is always
  18446.      initialized (if not already set).  There are a few types where
  18447.      we should not be doing so, because the type-specific area is
  18448.      already used to hold some other piece of info (eg: TYPE_CODE_FLT
  18449.      where the type-specific area is used to store the floatformat).
  18450.      But this is not a problem, because the gnat-specific information
  18451.      is actually not needed for these types.  */
  18452.   if (need_gnat_info (cu)
  18453.       && TYPE_CODE (type) != TYPE_CODE_FUNC
  18454.       && TYPE_CODE (type) != TYPE_CODE_FLT
  18455.       && !HAVE_GNAT_AUX_INFO (type))
  18456.     INIT_GNAT_SPECIFIC (type);

  18457.   /* Read DW_AT_data_location and set in type.  */
  18458.   attr = dwarf2_attr (die, DW_AT_data_location, cu);
  18459.   if (attr_to_dynamic_prop (attr, die, cu, &prop))
  18460.     {
  18461.       TYPE_DATA_LOCATION (type)
  18462.         = obstack_alloc (&objfile->objfile_obstack, sizeof (prop));
  18463.       *TYPE_DATA_LOCATION (type) = prop;
  18464.     }

  18465.   if (dwarf2_per_objfile->die_type_hash == NULL)
  18466.     {
  18467.       dwarf2_per_objfile->die_type_hash =
  18468.         htab_create_alloc_ex (127,
  18469.                               per_cu_offset_and_type_hash,
  18470.                               per_cu_offset_and_type_eq,
  18471.                               NULL,
  18472.                               &objfile->objfile_obstack,
  18473.                               hashtab_obstack_allocate,
  18474.                               dummy_obstack_deallocate);
  18475.     }

  18476.   ofs.per_cu = cu->per_cu;
  18477.   ofs.offset = die->offset;
  18478.   ofs.type = type;
  18479.   slot = (struct dwarf2_per_cu_offset_and_type **)
  18480.     htab_find_slot (dwarf2_per_objfile->die_type_hash, &ofs, INSERT);
  18481.   if (*slot)
  18482.     complaint (&symfile_complaints,
  18483.                _("A problem internal to GDB: DIE 0x%x has type already set"),
  18484.                die->offset.sect_off);
  18485.   *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
  18486.   **slot = ofs;
  18487.   return type;
  18488. }

  18489. /* Look up the type for the die at OFFSET in PER_CU in die_type_hash,
  18490.    or return NULL if the die does not have a saved type.  */

  18491. static struct type *
  18492. get_die_type_at_offset (sect_offset offset,
  18493.                         struct dwarf2_per_cu_data *per_cu)
  18494. {
  18495.   struct dwarf2_per_cu_offset_and_type *slot, ofs;

  18496.   if (dwarf2_per_objfile->die_type_hash == NULL)
  18497.     return NULL;

  18498.   ofs.per_cu = per_cu;
  18499.   ofs.offset = offset;
  18500.   slot = htab_find (dwarf2_per_objfile->die_type_hash, &ofs);
  18501.   if (slot)
  18502.     return slot->type;
  18503.   else
  18504.     return NULL;
  18505. }

  18506. /* Look up the type for DIE in CU in die_type_hash,
  18507.    or return NULL if DIE does not have a saved type.  */

  18508. static struct type *
  18509. get_die_type (struct die_info *die, struct dwarf2_cu *cu)
  18510. {
  18511.   return get_die_type_at_offset (die->offset, cu->per_cu);
  18512. }

  18513. /* Add a dependence relationship from CU to REF_PER_CU.  */

  18514. static void
  18515. dwarf2_add_dependence (struct dwarf2_cu *cu,
  18516.                        struct dwarf2_per_cu_data *ref_per_cu)
  18517. {
  18518.   void **slot;

  18519.   if (cu->dependencies == NULL)
  18520.     cu->dependencies
  18521.       = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
  18522.                               NULL, &cu->comp_unit_obstack,
  18523.                               hashtab_obstack_allocate,
  18524.                               dummy_obstack_deallocate);

  18525.   slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
  18526.   if (*slot == NULL)
  18527.     *slot = ref_per_cu;
  18528. }

  18529. /* Subroutine of dwarf2_mark to pass to htab_traverse.
  18530.    Set the mark field in every compilation unit in the
  18531.    cache that we must keep because we are keeping CU.  */

  18532. static int
  18533. dwarf2_mark_helper (void **slot, void *data)
  18534. {
  18535.   struct dwarf2_per_cu_data *per_cu;

  18536.   per_cu = (struct dwarf2_per_cu_data *) *slot;

  18537.   /* cu->dependencies references may not yet have been ever read if QUIT aborts
  18538.      reading of the chain.  As such dependencies remain valid it is not much
  18539.      useful to track and undo them during QUIT cleanups.  */
  18540.   if (per_cu->cu == NULL)
  18541.     return 1;

  18542.   if (per_cu->cu->mark)
  18543.     return 1;
  18544.   per_cu->cu->mark = 1;

  18545.   if (per_cu->cu->dependencies != NULL)
  18546.     htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);

  18547.   return 1;
  18548. }

  18549. /* Set the mark field in CU and in every other compilation unit in the
  18550.    cache that we must keep because we are keeping CU.  */

  18551. static void
  18552. dwarf2_mark (struct dwarf2_cu *cu)
  18553. {
  18554.   if (cu->mark)
  18555.     return;
  18556.   cu->mark = 1;
  18557.   if (cu->dependencies != NULL)
  18558.     htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
  18559. }

  18560. static void
  18561. dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
  18562. {
  18563.   while (per_cu)
  18564.     {
  18565.       per_cu->cu->mark = 0;
  18566.       per_cu = per_cu->cu->read_in_chain;
  18567.     }
  18568. }

  18569. /* Trivial hash function for partial_die_info: the hash value of a DIE
  18570.    is its offset in .debug_info for this objfile.  */

  18571. static hashval_t
  18572. partial_die_hash (const void *item)
  18573. {
  18574.   const struct partial_die_info *part_die = item;

  18575.   return part_die->offset.sect_off;
  18576. }

  18577. /* Trivial comparison function for partial_die_info structures: two DIEs
  18578.    are equal if they have the same offset.  */

  18579. static int
  18580. partial_die_eq (const void *item_lhs, const void *item_rhs)
  18581. {
  18582.   const struct partial_die_info *part_die_lhs = item_lhs;
  18583.   const struct partial_die_info *part_die_rhs = item_rhs;

  18584.   return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
  18585. }

  18586. static struct cmd_list_element *set_dwarf2_cmdlist;
  18587. static struct cmd_list_element *show_dwarf2_cmdlist;

  18588. static void
  18589. set_dwarf2_cmd (char *args, int from_tty)
  18590. {
  18591.   help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", all_commands,
  18592.              gdb_stdout);
  18593. }

  18594. static void
  18595. show_dwarf2_cmd (char *args, int from_tty)
  18596. {
  18597.   cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
  18598. }

  18599. /* Free data associated with OBJFILE, if necessary.  */

  18600. static void
  18601. dwarf2_per_objfile_free (struct objfile *objfile, void *d)
  18602. {
  18603.   struct dwarf2_per_objfile *data = d;
  18604.   int ix;

  18605.   /* Make sure we don't accidentally use dwarf2_per_objfile while
  18606.      cleaning up.  */
  18607.   dwarf2_per_objfile = NULL;

  18608.   for (ix = 0; ix < data->n_comp_units; ++ix)
  18609.    VEC_free (dwarf2_per_cu_ptr, data->all_comp_units[ix]->imported_symtabs);

  18610.   for (ix = 0; ix < data->n_type_units; ++ix)
  18611.     VEC_free (dwarf2_per_cu_ptr,
  18612.               data->all_type_units[ix]->per_cu.imported_symtabs);
  18613.   xfree (data->all_type_units);

  18614.   VEC_free (dwarf2_section_info_def, data->types);

  18615.   if (data->dwo_files)
  18616.     free_dwo_files (data->dwo_files, objfile);
  18617.   if (data->dwp_file)
  18618.     gdb_bfd_unref (data->dwp_file->dbfd);

  18619.   if (data->dwz_file && data->dwz_file->dwz_bfd)
  18620.     gdb_bfd_unref (data->dwz_file->dwz_bfd);
  18621. }


  18622. /* The "save gdb-index" command.  */

  18623. /* The contents of the hash table we create when building the string
  18624.    table.  */
  18625. struct strtab_entry
  18626. {
  18627.   offset_type offset;
  18628.   const char *str;
  18629. };

  18630. /* Hash function for a strtab_entry.

  18631.    Function is used only during write_hash_table so no index format backward
  18632.    compatibility is needed.  */

  18633. static hashval_t
  18634. hash_strtab_entry (const void *e)
  18635. {
  18636.   const struct strtab_entry *entry = e;
  18637.   return mapped_index_string_hash (INT_MAX, entry->str);
  18638. }

  18639. /* Equality function for a strtab_entry.  */

  18640. static int
  18641. eq_strtab_entry (const void *a, const void *b)
  18642. {
  18643.   const struct strtab_entry *ea = a;
  18644.   const struct strtab_entry *eb = b;
  18645.   return !strcmp (ea->str, eb->str);
  18646. }

  18647. /* Create a strtab_entry hash table.  */

  18648. static htab_t
  18649. create_strtab (void)
  18650. {
  18651.   return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
  18652.                             xfree, xcalloc, xfree);
  18653. }

  18654. /* Add a string to the constant pool.  Return the string's offset in
  18655.    host order.  */

  18656. static offset_type
  18657. add_string (htab_t table, struct obstack *cpool, const char *str)
  18658. {
  18659.   void **slot;
  18660.   struct strtab_entry entry;
  18661.   struct strtab_entry *result;

  18662.   entry.str = str;
  18663.   slot = htab_find_slot (table, &entry, INSERT);
  18664.   if (*slot)
  18665.     result = *slot;
  18666.   else
  18667.     {
  18668.       result = XNEW (struct strtab_entry);
  18669.       result->offset = obstack_object_size (cpool);
  18670.       result->str = str;
  18671.       obstack_grow_str0 (cpool, str);
  18672.       *slot = result;
  18673.     }
  18674.   return result->offset;
  18675. }

  18676. /* An entry in the symbol table.  */
  18677. struct symtab_index_entry
  18678. {
  18679.   /* The name of the symbol.  */
  18680.   const char *name;
  18681.   /* The offset of the name in the constant pool.  */
  18682.   offset_type index_offset;
  18683.   /* A sorted vector of the indices of all the CUs that hold an object
  18684.      of this name.  */
  18685.   VEC (offset_type) *cu_indices;
  18686. };

  18687. /* The symbol table.  This is a power-of-2-sized hash table.  */
  18688. struct mapped_symtab
  18689. {
  18690.   offset_type n_elements;
  18691.   offset_type size;
  18692.   struct symtab_index_entry **data;
  18693. };

  18694. /* Hash function for a symtab_index_entry.  */

  18695. static hashval_t
  18696. hash_symtab_entry (const void *e)
  18697. {
  18698.   const struct symtab_index_entry *entry = e;
  18699.   return iterative_hash (VEC_address (offset_type, entry->cu_indices),
  18700.                          sizeof (offset_type) * VEC_length (offset_type,
  18701.                                                             entry->cu_indices),
  18702.                          0);
  18703. }

  18704. /* Equality function for a symtab_index_entry.  */

  18705. static int
  18706. eq_symtab_entry (const void *a, const void *b)
  18707. {
  18708.   const struct symtab_index_entry *ea = a;
  18709.   const struct symtab_index_entry *eb = b;
  18710.   int len = VEC_length (offset_type, ea->cu_indices);
  18711.   if (len != VEC_length (offset_type, eb->cu_indices))
  18712.     return 0;
  18713.   return !memcmp (VEC_address (offset_type, ea->cu_indices),
  18714.                   VEC_address (offset_type, eb->cu_indices),
  18715.                   sizeof (offset_type) * len);
  18716. }

  18717. /* Destroy a symtab_index_entry.  */

  18718. static void
  18719. delete_symtab_entry (void *p)
  18720. {
  18721.   struct symtab_index_entry *entry = p;
  18722.   VEC_free (offset_type, entry->cu_indices);
  18723.   xfree (entry);
  18724. }

  18725. /* Create a hash table holding symtab_index_entry objects.  */

  18726. static htab_t
  18727. create_symbol_hash_table (void)
  18728. {
  18729.   return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
  18730.                             delete_symtab_entry, xcalloc, xfree);
  18731. }

  18732. /* Create a new mapped symtab object.  */

  18733. static struct mapped_symtab *
  18734. create_mapped_symtab (void)
  18735. {
  18736.   struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
  18737.   symtab->n_elements = 0;
  18738.   symtab->size = 1024;
  18739.   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
  18740.   return symtab;
  18741. }

  18742. /* Destroy a mapped_symtab.  */

  18743. static void
  18744. cleanup_mapped_symtab (void *p)
  18745. {
  18746.   struct mapped_symtab *symtab = p;
  18747.   /* The contents of the array are freed when the other hash table is
  18748.      destroyed.  */
  18749.   xfree (symtab->data);
  18750.   xfree (symtab);
  18751. }

  18752. /* Find a slot in SYMTAB for the symbol NAME.  Returns a pointer to
  18753.    the slot.

  18754.    Function is used only during write_hash_table so no index format backward
  18755.    compatibility is needed.  */

  18756. static struct symtab_index_entry **
  18757. find_slot (struct mapped_symtab *symtab, const char *name)
  18758. {
  18759.   offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);

  18760.   index = hash & (symtab->size - 1);
  18761.   step = ((hash * 17) & (symtab->size - 1)) | 1;

  18762.   for (;;)
  18763.     {
  18764.       if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
  18765.         return &symtab->data[index];
  18766.       index = (index + step) & (symtab->size - 1);
  18767.     }
  18768. }

  18769. /* Expand SYMTAB's hash table.  */

  18770. static void
  18771. hash_expand (struct mapped_symtab *symtab)
  18772. {
  18773.   offset_type old_size = symtab->size;
  18774.   offset_type i;
  18775.   struct symtab_index_entry **old_entries = symtab->data;

  18776.   symtab->size *= 2;
  18777.   symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);

  18778.   for (i = 0; i < old_size; ++i)
  18779.     {
  18780.       if (old_entries[i])
  18781.         {
  18782.           struct symtab_index_entry **slot = find_slot (symtab,
  18783.                                                         old_entries[i]->name);
  18784.           *slot = old_entries[i];
  18785.         }
  18786.     }

  18787.   xfree (old_entries);
  18788. }

  18789. /* Add an entry to SYMTAB.  NAME is the name of the symbol.
  18790.    CU_INDEX is the index of the CU in which the symbol appears.
  18791.    IS_STATIC is one if the symbol is static, otherwise zero (global).  */

  18792. static void
  18793. add_index_entry (struct mapped_symtab *symtab, const char *name,
  18794.                  int is_static, gdb_index_symbol_kind kind,
  18795.                  offset_type cu_index)
  18796. {
  18797.   struct symtab_index_entry **slot;
  18798.   offset_type cu_index_and_attrs;

  18799.   ++symtab->n_elements;
  18800.   if (4 * symtab->n_elements / 3 >= symtab->size)
  18801.     hash_expand (symtab);

  18802.   slot = find_slot (symtab, name);
  18803.   if (!*slot)
  18804.     {
  18805.       *slot = XNEW (struct symtab_index_entry);
  18806.       (*slot)->name = name;
  18807.       /* index_offset is set later.  */
  18808.       (*slot)->cu_indices = NULL;
  18809.     }

  18810.   cu_index_and_attrs = 0;
  18811.   DW2_GDB_INDEX_CU_SET_VALUE (cu_index_and_attrs, cu_index);
  18812.   DW2_GDB_INDEX_SYMBOL_STATIC_SET_VALUE (cu_index_and_attrs, is_static);
  18813.   DW2_GDB_INDEX_SYMBOL_KIND_SET_VALUE (cu_index_and_attrs, kind);

  18814.   /* We don't want to record an index value twice as we want to avoid the
  18815.      duplication.
  18816.      We process all global symbols and then all static symbols
  18817.      (which would allow us to avoid the duplication by only having to check
  18818.      the last entry pushed), but a symbol could have multiple kinds in one CU.
  18819.      To keep things simple we don't worry about the duplication here and
  18820.      sort and uniqufy the list after we've processed all symbols.  */
  18821.   VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index_and_attrs);
  18822. }

  18823. /* qsort helper routine for uniquify_cu_indices.  */

  18824. static int
  18825. offset_type_compare (const void *ap, const void *bp)
  18826. {
  18827.   offset_type a = *(offset_type *) ap;
  18828.   offset_type b = *(offset_type *) bp;

  18829.   return (a > b) - (b > a);
  18830. }

  18831. /* Sort and remove duplicates of all symbols' cu_indices lists.  */

  18832. static void
  18833. uniquify_cu_indices (struct mapped_symtab *symtab)
  18834. {
  18835.   int i;

  18836.   for (i = 0; i < symtab->size; ++i)
  18837.     {
  18838.       struct symtab_index_entry *entry = symtab->data[i];

  18839.       if (entry
  18840.           && entry->cu_indices != NULL)
  18841.         {
  18842.           unsigned int next_to_insert, next_to_check;
  18843.           offset_type last_value;

  18844.           qsort (VEC_address (offset_type, entry->cu_indices),
  18845.                  VEC_length (offset_type, entry->cu_indices),
  18846.                  sizeof (offset_type), offset_type_compare);

  18847.           last_value = VEC_index (offset_type, entry->cu_indices, 0);
  18848.           next_to_insert = 1;
  18849.           for (next_to_check = 1;
  18850.                next_to_check < VEC_length (offset_type, entry->cu_indices);
  18851.                ++next_to_check)
  18852.             {
  18853.               if (VEC_index (offset_type, entry->cu_indices, next_to_check)
  18854.                   != last_value)
  18855.                 {
  18856.                   last_value = VEC_index (offset_type, entry->cu_indices,
  18857.                                           next_to_check);
  18858.                   VEC_replace (offset_type, entry->cu_indices, next_to_insert,
  18859.                                last_value);
  18860.                   ++next_to_insert;
  18861.                 }
  18862.             }
  18863.           VEC_truncate (offset_type, entry->cu_indices, next_to_insert);
  18864.         }
  18865.     }
  18866. }

  18867. /* Add a vector of indices to the constant pool.  */

  18868. static offset_type
  18869. add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
  18870.                       struct symtab_index_entry *entry)
  18871. {
  18872.   void **slot;

  18873.   slot = htab_find_slot (symbol_hash_table, entry, INSERT);
  18874.   if (!*slot)
  18875.     {
  18876.       offset_type len = VEC_length (offset_type, entry->cu_indices);
  18877.       offset_type val = MAYBE_SWAP (len);
  18878.       offset_type iter;
  18879.       int i;

  18880.       *slot = entry;
  18881.       entry->index_offset = obstack_object_size (cpool);

  18882.       obstack_grow (cpool, &val, sizeof (val));
  18883.       for (i = 0;
  18884.            VEC_iterate (offset_type, entry->cu_indices, i, iter);
  18885.            ++i)
  18886.         {
  18887.           val = MAYBE_SWAP (iter);
  18888.           obstack_grow (cpool, &val, sizeof (val));
  18889.         }
  18890.     }
  18891.   else
  18892.     {
  18893.       struct symtab_index_entry *old_entry = *slot;
  18894.       entry->index_offset = old_entry->index_offset;
  18895.       entry = old_entry;
  18896.     }
  18897.   return entry->index_offset;
  18898. }

  18899. /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
  18900.    constant pool entries going into the obstack CPOOL.  */

  18901. static void
  18902. write_hash_table (struct mapped_symtab *symtab,
  18903.                   struct obstack *output, struct obstack *cpool)
  18904. {
  18905.   offset_type i;
  18906.   htab_t symbol_hash_table;
  18907.   htab_t str_table;

  18908.   symbol_hash_table = create_symbol_hash_table ();
  18909.   str_table = create_strtab ();

  18910.   /* We add all the index vectors to the constant pool first, to
  18911.      ensure alignment is ok.  */
  18912.   for (i = 0; i < symtab->size; ++i)
  18913.     {
  18914.       if (symtab->data[i])
  18915.         add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
  18916.     }

  18917.   /* Now write out the hash table.  */
  18918.   for (i = 0; i < symtab->size; ++i)
  18919.     {
  18920.       offset_type str_off, vec_off;

  18921.       if (symtab->data[i])
  18922.         {
  18923.           str_off = add_string (str_table, cpool, symtab->data[i]->name);
  18924.           vec_off = symtab->data[i]->index_offset;
  18925.         }
  18926.       else
  18927.         {
  18928.           /* While 0 is a valid constant pool index, it is not valid
  18929.              to have 0 for both offsets.  */
  18930.           str_off = 0;
  18931.           vec_off = 0;
  18932.         }

  18933.       str_off = MAYBE_SWAP (str_off);
  18934.       vec_off = MAYBE_SWAP (vec_off);

  18935.       obstack_grow (output, &str_off, sizeof (str_off));
  18936.       obstack_grow (output, &vec_off, sizeof (vec_off));
  18937.     }

  18938.   htab_delete (str_table);
  18939.   htab_delete (symbol_hash_table);
  18940. }

  18941. /* Struct to map psymtab to CU index in the index file.  */
  18942. struct psymtab_cu_index_map
  18943. {
  18944.   struct partial_symtab *psymtab;
  18945.   unsigned int cu_index;
  18946. };

  18947. static hashval_t
  18948. hash_psymtab_cu_index (const void *item)
  18949. {
  18950.   const struct psymtab_cu_index_map *map = item;

  18951.   return htab_hash_pointer (map->psymtab);
  18952. }

  18953. static int
  18954. eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
  18955. {
  18956.   const struct psymtab_cu_index_map *lhs = item_lhs;
  18957.   const struct psymtab_cu_index_map *rhs = item_rhs;

  18958.   return lhs->psymtab == rhs->psymtab;
  18959. }

  18960. /* Helper struct for building the address table.  */
  18961. struct addrmap_index_data
  18962. {
  18963.   struct objfile *objfile;
  18964.   struct obstack *addr_obstack;
  18965.   htab_t cu_index_htab;

  18966.   /* Non-zero if the previous_* fields are valid.
  18967.      We can't write an entry until we see the next entry (since it is only then
  18968.      that we know the end of the entry).  */
  18969.   int previous_valid;
  18970.   /* Index of the CU in the table of all CUs in the index file.  */
  18971.   unsigned int previous_cu_index;
  18972.   /* Start address of the CU.  */
  18973.   CORE_ADDR previous_cu_start;
  18974. };

  18975. /* Write an address entry to OBSTACK.  */

  18976. static void
  18977. add_address_entry (struct objfile *objfile, struct obstack *obstack,
  18978.                    CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
  18979. {
  18980.   offset_type cu_index_to_write;
  18981.   gdb_byte addr[8];
  18982.   CORE_ADDR baseaddr;

  18983.   baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));

  18984.   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
  18985.   obstack_grow (obstack, addr, 8);
  18986.   store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
  18987.   obstack_grow (obstack, addr, 8);
  18988.   cu_index_to_write = MAYBE_SWAP (cu_index);
  18989.   obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
  18990. }

  18991. /* Worker function for traversing an addrmap to build the address table.  */

  18992. static int
  18993. add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
  18994. {
  18995.   struct addrmap_index_data *data = datap;
  18996.   struct partial_symtab *pst = obj;

  18997.   if (data->previous_valid)
  18998.     add_address_entry (data->objfile, data->addr_obstack,
  18999.                        data->previous_cu_start, start_addr,
  19000.                        data->previous_cu_index);

  19001.   data->previous_cu_start = start_addr;
  19002.   if (pst != NULL)
  19003.     {
  19004.       struct psymtab_cu_index_map find_map, *map;
  19005.       find_map.psymtab = pst;
  19006.       map = htab_find (data->cu_index_htab, &find_map);
  19007.       gdb_assert (map != NULL);
  19008.       data->previous_cu_index = map->cu_index;
  19009.       data->previous_valid = 1;
  19010.     }
  19011.   else
  19012.       data->previous_valid = 0;

  19013.   return 0;
  19014. }

  19015. /* Write OBJFILE's address map to OBSTACK.
  19016.    CU_INDEX_HTAB is used to map addrmap entries to their CU indices
  19017.    in the index file.  */

  19018. static void
  19019. write_address_map (struct objfile *objfile, struct obstack *obstack,
  19020.                    htab_t cu_index_htab)
  19021. {
  19022.   struct addrmap_index_data addrmap_index_data;

  19023.   /* When writing the address table, we have to cope with the fact that
  19024.      the addrmap iterator only provides the start of a region; we have to
  19025.      wait until the next invocation to get the start of the next region.  */

  19026.   addrmap_index_data.objfile = objfile;
  19027.   addrmap_index_data.addr_obstack = obstack;
  19028.   addrmap_index_data.cu_index_htab = cu_index_htab;
  19029.   addrmap_index_data.previous_valid = 0;

  19030.   addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
  19031.                    &addrmap_index_data);

  19032.   /* It's highly unlikely the last entry (end address = 0xff...ff)
  19033.      is valid, but we should still handle it.
  19034.      The end address is recorded as the start of the next region, but that
  19035.      doesn't work here.  To cope we pass 0xff...ff, this is a rare situation
  19036.      anyway.  */
  19037.   if (addrmap_index_data.previous_valid)
  19038.     add_address_entry (objfile, obstack,
  19039.                        addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
  19040.                        addrmap_index_data.previous_cu_index);
  19041. }

  19042. /* Return the symbol kind of PSYM.  */

  19043. static gdb_index_symbol_kind
  19044. symbol_kind (struct partial_symbol *psym)
  19045. {
  19046.   domain_enum domain = PSYMBOL_DOMAIN (psym);
  19047.   enum address_class aclass = PSYMBOL_CLASS (psym);

  19048.   switch (domain)
  19049.     {
  19050.     case VAR_DOMAIN:
  19051.       switch (aclass)
  19052.         {
  19053.         case LOC_BLOCK:
  19054.           return GDB_INDEX_SYMBOL_KIND_FUNCTION;
  19055.         case LOC_TYPEDEF:
  19056.           return GDB_INDEX_SYMBOL_KIND_TYPE;
  19057.         case LOC_COMPUTED:
  19058.         case LOC_CONST_BYTES:
  19059.         case LOC_OPTIMIZED_OUT:
  19060.         case LOC_STATIC:
  19061.           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
  19062.         case LOC_CONST:
  19063.           /* Note: It's currently impossible to recognize psyms as enum values
  19064.              short of reading the type info.  For now punt.  */
  19065.           return GDB_INDEX_SYMBOL_KIND_VARIABLE;
  19066.         default:
  19067.           /* There are other LOC_FOO values that one might want to classify
  19068.              as variables, but dwarf2read.c doesn't currently use them.  */
  19069.           return GDB_INDEX_SYMBOL_KIND_OTHER;
  19070.         }
  19071.     case STRUCT_DOMAIN:
  19072.       return GDB_INDEX_SYMBOL_KIND_TYPE;
  19073.     default:
  19074.       return GDB_INDEX_SYMBOL_KIND_OTHER;
  19075.     }
  19076. }

  19077. /* Add a list of partial symbols to SYMTAB.  */

  19078. static void
  19079. write_psymbols (struct mapped_symtab *symtab,
  19080.                 htab_t psyms_seen,
  19081.                 struct partial_symbol **psymp,
  19082.                 int count,
  19083.                 offset_type cu_index,
  19084.                 int is_static)
  19085. {
  19086.   for (; count-- > 0; ++psymp)
  19087.     {
  19088.       struct partial_symbol *psym = *psymp;
  19089.       void **slot;

  19090.       if (SYMBOL_LANGUAGE (psym) == language_ada)
  19091.         error (_("Ada is not currently supported by the index"));

  19092.       /* Only add a given psymbol once.  */
  19093.       slot = htab_find_slot (psyms_seen, psym, INSERT);
  19094.       if (!*slot)
  19095.         {
  19096.           gdb_index_symbol_kind kind = symbol_kind (psym);

  19097.           *slot = psym;
  19098.           add_index_entry (symtab, SYMBOL_SEARCH_NAME (psym),
  19099.                            is_static, kind, cu_index);
  19100.         }
  19101.     }
  19102. }

  19103. /* Write the contents of an ("unfinished") obstack to FILE.  Throw an
  19104.    exception if there is an error.  */

  19105. static void
  19106. write_obstack (FILE *file, struct obstack *obstack)
  19107. {
  19108.   if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
  19109.               file)
  19110.       != obstack_object_size (obstack))
  19111.     error (_("couldn't data write to file"));
  19112. }

  19113. /* Unlink a file if the argument is not NULL.  */

  19114. static void
  19115. unlink_if_set (void *p)
  19116. {
  19117.   char **filename = p;
  19118.   if (*filename)
  19119.     unlink (*filename);
  19120. }

  19121. /* A helper struct used when iterating over debug_types.  */
  19122. struct signatured_type_index_data
  19123. {
  19124.   struct objfile *objfile;
  19125.   struct mapped_symtab *symtab;
  19126.   struct obstack *types_list;
  19127.   htab_t psyms_seen;
  19128.   int cu_index;
  19129. };

  19130. /* A helper function that writes a single signatured_type to an
  19131.    obstack.  */

  19132. static int
  19133. write_one_signatured_type (void **slot, void *d)
  19134. {
  19135.   struct signatured_type_index_data *info = d;
  19136.   struct signatured_type *entry = (struct signatured_type *) *slot;
  19137.   struct partial_symtab *psymtab = entry->per_cu.v.psymtab;
  19138.   gdb_byte val[8];

  19139.   write_psymbols (info->symtab,
  19140.                   info->psyms_seen,
  19141.                   info->objfile->global_psymbols.list
  19142.                   + psymtab->globals_offset,
  19143.                   psymtab->n_global_syms, info->cu_index,
  19144.                   0);
  19145.   write_psymbols (info->symtab,
  19146.                   info->psyms_seen,
  19147.                   info->objfile->static_psymbols.list
  19148.                   + psymtab->statics_offset,
  19149.                   psymtab->n_static_syms, info->cu_index,
  19150.                   1);

  19151.   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
  19152.                           entry->per_cu.offset.sect_off);
  19153.   obstack_grow (info->types_list, val, 8);
  19154.   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
  19155.                           entry->type_offset_in_tu.cu_off);
  19156.   obstack_grow (info->types_list, val, 8);
  19157.   store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
  19158.   obstack_grow (info->types_list, val, 8);

  19159.   ++info->cu_index;

  19160.   return 1;
  19161. }

  19162. /* Recurse into all "included" dependencies and write their symbols as
  19163.    if they appeared in this psymtab.  */

  19164. static void
  19165. recursively_write_psymbols (struct objfile *objfile,
  19166.                             struct partial_symtab *psymtab,
  19167.                             struct mapped_symtab *symtab,
  19168.                             htab_t psyms_seen,
  19169.                             offset_type cu_index)
  19170. {
  19171.   int i;

  19172.   for (i = 0; i < psymtab->number_of_dependencies; ++i)
  19173.     if (psymtab->dependencies[i]->user != NULL)
  19174.       recursively_write_psymbols (objfile, psymtab->dependencies[i],
  19175.                                   symtab, psyms_seen, cu_index);

  19176.   write_psymbols (symtab,
  19177.                   psyms_seen,
  19178.                   objfile->global_psymbols.list + psymtab->globals_offset,
  19179.                   psymtab->n_global_syms, cu_index,
  19180.                   0);
  19181.   write_psymbols (symtab,
  19182.                   psyms_seen,
  19183.                   objfile->static_psymbols.list + psymtab->statics_offset,
  19184.                   psymtab->n_static_syms, cu_index,
  19185.                   1);
  19186. }

  19187. /* Create an index file for OBJFILE in the directory DIR.  */

  19188. static void
  19189. write_psymtabs_to_index (struct objfile *objfile, const char *dir)
  19190. {
  19191.   struct cleanup *cleanup;
  19192.   char *filename, *cleanup_filename;
  19193.   struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
  19194.   struct obstack cu_list, types_cu_list;
  19195.   int i;
  19196.   FILE *out_file;
  19197.   struct mapped_symtab *symtab;
  19198.   offset_type val, size_of_contents, total_len;
  19199.   struct stat st;
  19200.   htab_t psyms_seen;
  19201.   htab_t cu_index_htab;
  19202.   struct psymtab_cu_index_map *psymtab_cu_index_map;

  19203.   if (dwarf2_per_objfile->using_index)
  19204.     error (_("Cannot use an index to create the index"));

  19205.   if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
  19206.     error (_("Cannot make an index when the file has multiple .debug_types sections"));

  19207.   if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
  19208.     return;

  19209.   if (stat (objfile_name (objfile), &st) < 0)
  19210.     perror_with_name (objfile_name (objfile));

  19211.   filename = concat (dir, SLASH_STRING, lbasename (objfile_name (objfile)),
  19212.                      INDEX_SUFFIX, (char *) NULL);
  19213.   cleanup = make_cleanup (xfree, filename);

  19214.   out_file = gdb_fopen_cloexec (filename, "wb");
  19215.   if (!out_file)
  19216.     error (_("Can't open `%s' for writing"), filename);

  19217.   cleanup_filename = filename;
  19218.   make_cleanup (unlink_if_set, &cleanup_filename);

  19219.   symtab = create_mapped_symtab ();
  19220.   make_cleanup (cleanup_mapped_symtab, symtab);

  19221.   obstack_init (&addr_obstack);
  19222.   make_cleanup_obstack_free (&addr_obstack);

  19223.   obstack_init (&cu_list);
  19224.   make_cleanup_obstack_free (&cu_list);

  19225.   obstack_init (&types_cu_list);
  19226.   make_cleanup_obstack_free (&types_cu_list);

  19227.   psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
  19228.                                   NULL, xcalloc, xfree);
  19229.   make_cleanup_htab_delete (psyms_seen);

  19230.   /* While we're scanning CU's create a table that maps a psymtab pointer
  19231.      (which is what addrmap records) to its index (which is what is recorded
  19232.      in the index file).  This will later be needed to write the address
  19233.      table.  */
  19234.   cu_index_htab = htab_create_alloc (100,
  19235.                                      hash_psymtab_cu_index,
  19236.                                      eq_psymtab_cu_index,
  19237.                                      NULL, xcalloc, xfree);
  19238.   make_cleanup_htab_delete (cu_index_htab);
  19239.   psymtab_cu_index_map = (struct psymtab_cu_index_map *)
  19240.     xmalloc (sizeof (struct psymtab_cu_index_map)
  19241.              * dwarf2_per_objfile->n_comp_units);
  19242.   make_cleanup (xfree, psymtab_cu_index_map);

  19243.   /* The CU list is already sorted, so we don't need to do additional
  19244.      work here.  Also, the debug_types entries do not appear in
  19245.      all_comp_units, but only in their own hash table.  */
  19246.   for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
  19247.     {
  19248.       struct dwarf2_per_cu_data *per_cu
  19249.         = dwarf2_per_objfile->all_comp_units[i];
  19250.       struct partial_symtab *psymtab = per_cu->v.psymtab;
  19251.       gdb_byte val[8];
  19252.       struct psymtab_cu_index_map *map;
  19253.       void **slot;

  19254.       /* CU of a shared file from 'dwz -m' may be unused by this main file.
  19255.          It may be referenced from a local scope but in such case it does not
  19256.          need to be present in .gdb_index.  */
  19257.       if (psymtab == NULL)
  19258.         continue;

  19259.       if (psymtab->user == NULL)
  19260.         recursively_write_psymbols (objfile, psymtab, symtab, psyms_seen, i);

  19261.       map = &psymtab_cu_index_map[i];
  19262.       map->psymtab = psymtab;
  19263.       map->cu_index = i;
  19264.       slot = htab_find_slot (cu_index_htab, map, INSERT);
  19265.       gdb_assert (slot != NULL);
  19266.       gdb_assert (*slot == NULL);
  19267.       *slot = map;

  19268.       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
  19269.                               per_cu->offset.sect_off);
  19270.       obstack_grow (&cu_list, val, 8);
  19271.       store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
  19272.       obstack_grow (&cu_list, val, 8);
  19273.     }

  19274.   /* Dump the address map.  */
  19275.   write_address_map (objfile, &addr_obstack, cu_index_htab);

  19276.   /* Write out the .debug_type entries, if any.  */
  19277.   if (dwarf2_per_objfile->signatured_types)
  19278.     {
  19279.       struct signatured_type_index_data sig_data;

  19280.       sig_data.objfile = objfile;
  19281.       sig_data.symtab = symtab;
  19282.       sig_data.types_list = &types_cu_list;
  19283.       sig_data.psyms_seen = psyms_seen;
  19284.       sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
  19285.       htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
  19286.                               write_one_signatured_type, &sig_data);
  19287.     }

  19288.   /* Now that we've processed all symbols we can shrink their cu_indices
  19289.      lists.  */
  19290.   uniquify_cu_indices (symtab);

  19291.   obstack_init (&constant_pool);
  19292.   make_cleanup_obstack_free (&constant_pool);
  19293.   obstack_init (&symtab_obstack);
  19294.   make_cleanup_obstack_free (&symtab_obstack);
  19295.   write_hash_table (symtab, &symtab_obstack, &constant_pool);

  19296.   obstack_init (&contents);
  19297.   make_cleanup_obstack_free (&contents);
  19298.   size_of_contents = 6 * sizeof (offset_type);
  19299.   total_len = size_of_contents;

  19300.   /* The version number.  */
  19301.   val = MAYBE_SWAP (8);
  19302.   obstack_grow (&contents, &val, sizeof (val));

  19303.   /* The offset of the CU list from the start of the file.  */
  19304.   val = MAYBE_SWAP (total_len);
  19305.   obstack_grow (&contents, &val, sizeof (val));
  19306.   total_len += obstack_object_size (&cu_list);

  19307.   /* The offset of the types CU list from the start of the file.  */
  19308.   val = MAYBE_SWAP (total_len);
  19309.   obstack_grow (&contents, &val, sizeof (val));
  19310.   total_len += obstack_object_size (&types_cu_list);

  19311.   /* The offset of the address table from the start of the file.  */
  19312.   val = MAYBE_SWAP (total_len);
  19313.   obstack_grow (&contents, &val, sizeof (val));
  19314.   total_len += obstack_object_size (&addr_obstack);

  19315.   /* The offset of the symbol table from the start of the file.  */
  19316.   val = MAYBE_SWAP (total_len);
  19317.   obstack_grow (&contents, &val, sizeof (val));
  19318.   total_len += obstack_object_size (&symtab_obstack);

  19319.   /* The offset of the constant pool from the start of the file.  */
  19320.   val = MAYBE_SWAP (total_len);
  19321.   obstack_grow (&contents, &val, sizeof (val));
  19322.   total_len += obstack_object_size (&constant_pool);

  19323.   gdb_assert (obstack_object_size (&contents) == size_of_contents);

  19324.   write_obstack (out_file, &contents);
  19325.   write_obstack (out_file, &cu_list);
  19326.   write_obstack (out_file, &types_cu_list);
  19327.   write_obstack (out_file, &addr_obstack);
  19328.   write_obstack (out_file, &symtab_obstack);
  19329.   write_obstack (out_file, &constant_pool);

  19330.   fclose (out_file);

  19331.   /* We want to keep the file, so we set cleanup_filename to NULL
  19332.      here.  See unlink_if_set.  */
  19333.   cleanup_filename = NULL;

  19334.   do_cleanups (cleanup);
  19335. }

  19336. /* Implementation of the `save gdb-index' command.

  19337.    Note that the file format used by this command is documented in the
  19338.    GDB manual.  Any changes here must be documented there.  */

  19339. static void
  19340. save_gdb_index_command (char *arg, int from_tty)
  19341. {
  19342.   struct objfile *objfile;

  19343.   if (!arg || !*arg)
  19344.     error (_("usage: save gdb-index DIRECTORY"));

  19345.   ALL_OBJFILES (objfile)
  19346.   {
  19347.     struct stat st;

  19348.     /* If the objfile does not correspond to an actual file, skip it.  */
  19349.     if (stat (objfile_name (objfile), &st) < 0)
  19350.       continue;

  19351.     dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
  19352.     if (dwarf2_per_objfile)
  19353.       {
  19354.         volatile struct gdb_exception except;

  19355.         TRY_CATCH (except, RETURN_MASK_ERROR)
  19356.           {
  19357.             write_psymtabs_to_index (objfile, arg);
  19358.           }
  19359.         if (except.reason < 0)
  19360.           exception_fprintf (gdb_stderr, except,
  19361.                              _("Error while writing index for `%s': "),
  19362.                              objfile_name (objfile));
  19363.       }
  19364.   }
  19365. }



  19366. int dwarf2_always_disassemble;

  19367. static void
  19368. show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
  19369.                                 struct cmd_list_element *c, const char *value)
  19370. {
  19371.   fprintf_filtered (file,
  19372.                     _("Whether to always disassemble "
  19373.                       "DWARF expressions is %s.\n"),
  19374.                     value);
  19375. }

  19376. static void
  19377. show_check_physname (struct ui_file *file, int from_tty,
  19378.                      struct cmd_list_element *c, const char *value)
  19379. {
  19380.   fprintf_filtered (file,
  19381.                     _("Whether to check \"physname\" is %s.\n"),
  19382.                     value);
  19383. }

  19384. void _initialize_dwarf2_read (void);

  19385. void
  19386. _initialize_dwarf2_read (void)
  19387. {
  19388.   struct cmd_list_element *c;

  19389.   dwarf2_objfile_data_key
  19390.     = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);

  19391.   add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
  19392. Set DWARF 2 specific variables.\n\
  19393. Configure DWARF 2 variables such as the cache size"),
  19394.                   &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
  19395.                   0/*allow-unknown*/, &maintenance_set_cmdlist);

  19396.   add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
  19397. Show DWARF 2 specific variables\n\
  19398. Show DWARF 2 variables such as the cache size"),
  19399.                   &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
  19400.                   0/*allow-unknown*/, &maintenance_show_cmdlist);

  19401.   add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
  19402.                             &dwarf2_max_cache_age, _("\
  19403. Set the upper bound on the age of cached dwarf2 compilation units."), _("\
  19404. Show the upper bound on the age of cached dwarf2 compilation units."), _("\
  19405. A higher limit means that cached compilation units will be stored\n\
  19406. in memory longer, and more total memory will be used.  Zero disables\n\
  19407. caching, which can slow down startup."),
  19408.                             NULL,
  19409.                             show_dwarf2_max_cache_age,
  19410.                             &set_dwarf2_cmdlist,
  19411.                             &show_dwarf2_cmdlist);

  19412.   add_setshow_boolean_cmd ("always-disassemble", class_obscure,
  19413.                            &dwarf2_always_disassemble, _("\
  19414. Set whether `info address' always disassembles DWARF expressions."), _("\
  19415. Show whether `info address' always disassembles DWARF expressions."), _("\
  19416. When enabled, DWARF expressions are always printed in an assembly-like\n\
  19417. syntax.  When disabled, expressions will be printed in a more\n\
  19418. conversational style, when possible."),
  19419.                            NULL,
  19420.                            show_dwarf2_always_disassemble,
  19421.                            &set_dwarf2_cmdlist,
  19422.                            &show_dwarf2_cmdlist);

  19423.   add_setshow_zuinteger_cmd ("dwarf2-read", no_class, &dwarf2_read_debug, _("\
  19424. Set debugging of the dwarf2 reader."), _("\
  19425. Show debugging of the dwarf2 reader."), _("\
  19426. When enabled (non-zero), debugging messages are printed during dwarf2\n\
  19427. reading and symtab expansion.  A value of 1 (one) provides basic\n\
  19428. information.  A value greater than 1 provides more verbose information."),
  19429.                             NULL,
  19430.                             NULL,
  19431.                             &setdebuglist, &showdebuglist);

  19432.   add_setshow_zuinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
  19433. Set debugging of the dwarf2 DIE reader."), _("\
  19434. Show debugging of the dwarf2 DIE reader."), _("\
  19435. When enabled (non-zero), DIEs are dumped after they are read in.\n\
  19436. The value is the maximum depth to print."),
  19437.                              NULL,
  19438.                              NULL,
  19439.                              &setdebuglist, &showdebuglist);

  19440.   add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
  19441. Set cross-checking of \"physname\" code against demangler."), _("\
  19442. Show cross-checking of \"physname\" code against demangler."), _("\
  19443. When enabled, GDB's internal \"physname\" code is checked against\n\
  19444. the demangler."),
  19445.                            NULL, show_check_physname,
  19446.                            &setdebuglist, &showdebuglist);

  19447.   add_setshow_boolean_cmd ("use-deprecated-index-sections",
  19448.                            no_class, &use_deprecated_index_sections, _("\
  19449. Set whether to use deprecated gdb_index sections."), _("\
  19450. Show whether to use deprecated gdb_index sections."), _("\
  19451. When enabled, deprecated .gdb_index sections are used anyway.\n\
  19452. Normally they are ignored either because of a missing feature or\n\
  19453. performance issue.\n\
  19454. Warning: This option must be enabled before gdb reads the file."),
  19455.                            NULL,
  19456.                            NULL,
  19457.                            &setlist, &showlist);

  19458.   c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
  19459.                _("\
  19460. Save a gdb-index file.\n\
  19461. Usage: save gdb-index DIRECTORY"),
  19462.                &save_cmdlist);
  19463.   set_cmd_completer (c, filename_completer);

  19464.   dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
  19465.                                                         &dwarf2_locexpr_funcs);
  19466.   dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
  19467.                                                         &dwarf2_loclist_funcs);

  19468.   dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
  19469.                                         &dwarf2_block_frame_base_locexpr_funcs);
  19470.   dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
  19471.                                         &dwarf2_block_frame_base_loclist_funcs);
  19472. }