[gdb/symtab] Add symbol with inherited DW_AT_const_value to psymtabs
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
b811d2c2 3 Copyright (C) 1994-2020 Free Software Foundation, Inc.
c906108c
SS
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
7ce59000 10 support.
c906108c 11
c5aa993b 12 This file is part of GDB.
c906108c 13
c5aa993b
JM
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
a9762ec7
JB
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
c906108c 18
a9762ec7
JB
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
c906108c 23
c5aa993b 24 You should have received a copy of the GNU General Public License
a9762ec7 25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
c906108c 26
21b2bd31
DE
27/* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
c906108c 31#include "defs.h"
82ca8957 32#include "dwarf2/read.h"
3054dd54 33#include "dwarf2/abbrev.h"
162dce55 34#include "dwarf2/attribute.h"
4057dfde 35#include "dwarf2/comp-unit.h"
82ca8957
TT
36#include "dwarf2/index-cache.h"
37#include "dwarf2/index-common.h"
f4382c45 38#include "dwarf2/leb.h"
8fdd972c 39#include "dwarf2/line-header.h"
9fda78b6 40#include "dwarf2/dwz.h"
c90ec28a 41#include "dwarf2/macro.h"
c2d50fd0 42#include "dwarf2/die.h"
2b2558bf 43#include "dwarf2/stringify.h"
4de283e4
TT
44#include "bfd.h"
45#include "elf-bfd.h"
46#include "symtab.h"
47#include "gdbtypes.h"
48#include "objfiles.h"
d55e5aa6 49#include "dwarf2.h"
4de283e4
TT
50#include "buildsym.h"
51#include "demangle.h"
52#include "gdb-demangle.h"
4de283e4 53#include "filenames.h" /* for DOSish file names */
4de283e4
TT
54#include "language.h"
55#include "complaints.h"
82ca8957
TT
56#include "dwarf2/expr.h"
57#include "dwarf2/loc.h"
4de283e4
TT
58#include "cp-support.h"
59#include "hashtab.h"
60#include "command.h"
d55e5aa6 61#include "gdbcmd.h"
4de283e4
TT
62#include "block.h"
63#include "addrmap.h"
64#include "typeprint.h"
65#include "psympriv.h"
4de283e4 66#include "c-lang.h"
d55e5aa6 67#include "go-lang.h"
4de283e4
TT
68#include "valprint.h"
69#include "gdbcore.h" /* for gnutarget */
70#include "gdb/gdb-index.h"
4de283e4
TT
71#include "gdb_bfd.h"
72#include "f-lang.h"
73#include "source.h"
4de283e4 74#include "build-id.h"
d55e5aa6 75#include "namespace.h"
268a13a5
TT
76#include "gdbsupport/function-view.h"
77#include "gdbsupport/gdb_optional.h"
78#include "gdbsupport/underlying.h"
268a13a5 79#include "gdbsupport/hash_enum.h"
4de283e4 80#include "filename-seen-cache.h"
b32b108a 81#include "producer.h"
4de283e4 82#include <fcntl.h>
4de283e4 83#include <algorithm>
4de283e4 84#include <unordered_map>
268a13a5 85#include "gdbsupport/selftest.h"
c9317f21 86#include "rust-lang.h"
268a13a5 87#include "gdbsupport/pathstuff.h"
edd45eb0 88#include "count-one-bits.h"
0d79cdc4 89#include "debuginfod-support.h"
437afbb8 90
73be47f5
DE
91/* When == 1, print basic high level tracing messages.
92 When > 1, be more verbose.
b4f54984
DE
93 This is in contrast to the low level DIE reading of dwarf_die_debug. */
94static unsigned int dwarf_read_debug = 0;
45cfd468 95
d97bc12b 96/* When non-zero, dump DIEs after they are read in. */
b4f54984 97static unsigned int dwarf_die_debug = 0;
d97bc12b 98
27e0867f 99/* When non-zero, dump line number entries as they are read in. */
8fdd972c 100unsigned int dwarf_line_debug = 0;
27e0867f 101
491144b5
CB
102/* When true, cross-check physname against demangler. */
103static bool check_physname = false;
900e11f9 104
491144b5
CB
105/* When true, do not reject deprecated .gdb_index sections. */
106static bool use_deprecated_index_sections = false;
481860b3 107
5bfd760d 108static const struct objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
6502dd73 109
f1e6e072
TT
110/* The "aclass" indices for various kinds of computed DWARF symbols. */
111
112static int dwarf2_locexpr_index;
113static int dwarf2_loclist_index;
114static int dwarf2_locexpr_block_index;
115static int dwarf2_loclist_block_index;
116
3f563c84
PA
117/* An index into a (C++) symbol name component in a symbol name as
118 recorded in the mapped_index's symbol table. For each C++ symbol
119 in the symbol table, we record one entry for the start of each
120 component in the symbol in a table of name components, and then
121 sort the table, in order to be able to binary search symbol names,
122 ignoring leading namespaces, both completion and regular look up.
123 For example, for symbol "A::B::C", we'll have an entry that points
124 to "A::B::C", another that points to "B::C", and another for "C".
125 Note that function symbols in GDB index have no parameter
126 information, just the function/method names. You can convert a
127 name_component to a "const char *" using the
128 'mapped_index::symbol_name_at(offset_type)' method. */
129
130struct name_component
131{
132 /* Offset in the symbol name where the component starts. Stored as
133 a (32-bit) offset instead of a pointer to save memory and improve
134 locality on 64-bit architectures. */
135 offset_type name_offset;
136
137 /* The symbol's index in the symbol and constant pool tables of a
138 mapped_index. */
139 offset_type idx;
140};
141
44ed8f3e
PA
142/* Base class containing bits shared by both .gdb_index and
143 .debug_name indexes. */
144
145struct mapped_index_base
146{
22ca247e
TT
147 mapped_index_base () = default;
148 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
149
44ed8f3e
PA
150 /* The name_component table (a sorted vector). See name_component's
151 description above. */
152 std::vector<name_component> name_components;
153
154 /* How NAME_COMPONENTS is sorted. */
155 enum case_sensitivity name_components_casing;
156
157 /* Return the number of names in the symbol table. */
158 virtual size_t symbol_name_count () const = 0;
159
160 /* Get the name of the symbol at IDX in the symbol table. */
161 virtual const char *symbol_name_at (offset_type idx) const = 0;
162
163 /* Return whether the name at IDX in the symbol table should be
164 ignored. */
165 virtual bool symbol_name_slot_invalid (offset_type idx) const
166 {
167 return false;
168 }
169
170 /* Build the symbol name component sorted vector, if we haven't
171 yet. */
172 void build_name_components ();
173
174 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
175 possible matches for LN_NO_PARAMS in the name component
176 vector. */
177 std::pair<std::vector<name_component>::const_iterator,
178 std::vector<name_component>::const_iterator>
3b00ef10
TT
179 find_name_components_bounds (const lookup_name_info &ln_no_params,
180 enum language lang) const;
44ed8f3e
PA
181
182 /* Prevent deleting/destroying via a base class pointer. */
183protected:
184 ~mapped_index_base() = default;
185};
186
9291a0cd
TT
187/* A description of the mapped index. The file format is described in
188 a comment by the code that writes the index. */
fc898b42 189struct mapped_index final : public mapped_index_base
9291a0cd 190{
f00a2de2
PA
191 /* A slot/bucket in the symbol table hash. */
192 struct symbol_table_slot
193 {
194 const offset_type name;
195 const offset_type vec;
196 };
197
559a7a62 198 /* Index data format version. */
3063847f 199 int version = 0;
559a7a62 200
f00a2de2
PA
201 /* The address table data. */
202 gdb::array_view<const gdb_byte> address_table;
b11b1f88 203
3876f04e 204 /* The symbol table, implemented as a hash table. */
f00a2de2 205 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 206
9291a0cd 207 /* A pointer to the constant pool. */
3063847f 208 const char *constant_pool = nullptr;
3f563c84 209
44ed8f3e
PA
210 bool symbol_name_slot_invalid (offset_type idx) const override
211 {
212 const auto &bucket = this->symbol_table[idx];
9ab08412 213 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 214 }
5c58de74 215
3f563c84
PA
216 /* Convenience method to get at the name of the symbol at IDX in the
217 symbol table. */
44ed8f3e 218 const char *symbol_name_at (offset_type idx) const override
f00a2de2 219 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 220
44ed8f3e
PA
221 size_t symbol_name_count () const override
222 { return this->symbol_table.size (); }
9291a0cd
TT
223};
224
927aa2e7
JK
225/* A description of the mapped .debug_names.
226 Uninitialized map has CU_COUNT 0. */
fc898b42 227struct mapped_debug_names final : public mapped_index_base
927aa2e7 228{
ed2dc618
SM
229 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
230 : dwarf2_per_objfile (dwarf2_per_objfile_)
231 {}
232
233 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
234 bfd_endian dwarf5_byte_order;
235 bool dwarf5_is_dwarf64;
236 bool augmentation_is_gdb;
237 uint8_t offset_size;
238 uint32_t cu_count = 0;
239 uint32_t tu_count, bucket_count, name_count;
240 const gdb_byte *cu_table_reordered, *tu_table_reordered;
241 const uint32_t *bucket_table_reordered, *hash_table_reordered;
242 const gdb_byte *name_table_string_offs_reordered;
243 const gdb_byte *name_table_entry_offs_reordered;
244 const gdb_byte *entry_pool;
245
246 struct index_val
247 {
248 ULONGEST dwarf_tag;
249 struct attr
250 {
251 /* Attribute name DW_IDX_*. */
252 ULONGEST dw_idx;
253
254 /* Attribute form DW_FORM_*. */
255 ULONGEST form;
256
257 /* Value if FORM is DW_FORM_implicit_const. */
258 LONGEST implicit_const;
259 };
260 std::vector<attr> attr_vec;
261 };
262
263 std::unordered_map<ULONGEST, index_val> abbrev_map;
264
265 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
266
267 /* Implementation of the mapped_index_base virtual interface, for
268 the name_components cache. */
269
270 const char *symbol_name_at (offset_type idx) const override
271 { return namei_to_name (idx); }
272
273 size_t symbol_name_count () const override
274 { return this->name_count; }
927aa2e7
JK
275};
276
cd4fb1b2 277/* See dwarf2read.h. */
ed2dc618 278
cd4fb1b2 279dwarf2_per_objfile *
ed2dc618
SM
280get_dwarf2_per_objfile (struct objfile *objfile)
281{
5bfd760d 282 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 283}
c906108c 284
251d32d9 285/* Default names of the debugging sections. */
c906108c 286
233a11ab
CS
287/* Note that if the debugging section has been compressed, it might
288 have a name like .zdebug_info. */
289
9cdd5dbd
DE
290static const struct dwarf2_debug_sections dwarf2_elf_names =
291{
251d32d9
TG
292 { ".debug_info", ".zdebug_info" },
293 { ".debug_abbrev", ".zdebug_abbrev" },
294 { ".debug_line", ".zdebug_line" },
295 { ".debug_loc", ".zdebug_loc" },
43988095 296 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 297 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 298 { ".debug_macro", ".zdebug_macro" },
251d32d9 299 { ".debug_str", ".zdebug_str" },
18a8505e 300 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 301 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 302 { ".debug_ranges", ".zdebug_ranges" },
43988095 303 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 304 { ".debug_types", ".zdebug_types" },
3019eac3 305 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
306 { ".debug_frame", ".zdebug_frame" },
307 { ".eh_frame", NULL },
24d3216f 308 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
309 { ".debug_names", ".zdebug_names" },
310 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 311 23
251d32d9 312};
c906108c 313
80626a55 314/* List of DWO/DWP sections. */
3019eac3 315
80626a55 316static const struct dwop_section_names
3019eac3
DE
317{
318 struct dwarf2_section_names abbrev_dwo;
319 struct dwarf2_section_names info_dwo;
320 struct dwarf2_section_names line_dwo;
321 struct dwarf2_section_names loc_dwo;
43988095 322 struct dwarf2_section_names loclists_dwo;
09262596
DE
323 struct dwarf2_section_names macinfo_dwo;
324 struct dwarf2_section_names macro_dwo;
3019eac3
DE
325 struct dwarf2_section_names str_dwo;
326 struct dwarf2_section_names str_offsets_dwo;
327 struct dwarf2_section_names types_dwo;
80626a55
DE
328 struct dwarf2_section_names cu_index;
329 struct dwarf2_section_names tu_index;
3019eac3 330}
80626a55 331dwop_section_names =
3019eac3
DE
332{
333 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
334 { ".debug_info.dwo", ".zdebug_info.dwo" },
335 { ".debug_line.dwo", ".zdebug_line.dwo" },
336 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 337 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
338 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
339 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
340 { ".debug_str.dwo", ".zdebug_str.dwo" },
341 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
342 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
343 { ".debug_cu_index", ".zdebug_cu_index" },
344 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
345};
346
c906108c
SS
347/* local data types */
348
3da10d80
KS
349/* Type used for delaying computation of method physnames.
350 See comments for compute_delayed_physnames. */
351struct delayed_method_info
352{
353 /* The type to which the method is attached, i.e., its parent class. */
354 struct type *type;
355
356 /* The index of the method in the type's function fieldlists. */
357 int fnfield_index;
358
359 /* The index of the method in the fieldlist. */
360 int index;
361
362 /* The name of the DIE. */
363 const char *name;
364
365 /* The DIE associated with this method. */
366 struct die_info *die;
367};
368
e7c27a73
DJ
369/* Internal state when decoding a particular compilation unit. */
370struct dwarf2_cu
371{
fcd3b13d
SM
372 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
373 ~dwarf2_cu ();
374
375 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
376
c24bdb02
KS
377 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
378 Create the set of symtabs used by this TU, or if this TU is sharing
379 symtabs with another TU and the symtabs have already been created
380 then restore those symtabs in the line header.
381 We don't need the pc/line-number mapping for type units. */
382 void setup_type_unit_groups (struct die_info *die);
383
384 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
385 buildsym_compunit constructor. */
386 struct compunit_symtab *start_symtab (const char *name,
387 const char *comp_dir,
388 CORE_ADDR low_pc);
389
390 /* Reset the builder. */
391 void reset_builder () { m_builder.reset (); }
392
d00adf39 393 /* The header of the compilation unit. */
fcd3b13d 394 struct comp_unit_head header {};
e142c38c 395
d00adf39 396 /* Base address of this compilation unit. */
2b24b6e4 397 gdb::optional<CORE_ADDR> base_address;
d00adf39 398
e142c38c 399 /* The language we are debugging. */
fcd3b13d
SM
400 enum language language = language_unknown;
401 const struct language_defn *language_defn = nullptr;
e142c38c 402
fcd3b13d 403 const char *producer = nullptr;
b0f35d58 404
c24bdb02 405private:
804d2729
TT
406 /* The symtab builder for this CU. This is only non-NULL when full
407 symbols are being read. */
c24bdb02 408 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 409
c24bdb02 410public:
e142c38c
DJ
411 /* The generic symbol table building routines have separate lists for
412 file scope symbols and all all other scopes (local scopes). So
413 we need to select the right one to pass to add_symbol_to_list().
414 We do it by keeping a pointer to the correct list in list_in_scope.
415
416 FIXME: The original dwarf code just treated the file scope as the
417 first local scope, and all other local scopes as nested local
418 scopes, and worked fine. Check to see if we really need to
419 distinguish these in buildsym.c. */
fcd3b13d 420 struct pending **list_in_scope = nullptr;
e142c38c 421
b64f50a1
JK
422 /* Hash table holding all the loaded partial DIEs
423 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 424 htab_t partial_dies = nullptr;
72bf9492
DJ
425
426 /* Storage for things with the same lifetime as this read-in compilation
427 unit, including partial DIEs. */
fcd3b13d 428 auto_obstack comp_unit_obstack;
72bf9492 429
ae038cb0
DJ
430 /* When multiple dwarf2_cu structures are living in memory, this field
431 chains them all together, so that they can be released efficiently.
432 We will probably also want a generation counter so that most-recently-used
433 compilation units are cached... */
fcd3b13d 434 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 435
69d751e3 436 /* Backlink to our per_cu entry. */
ae038cb0
DJ
437 struct dwarf2_per_cu_data *per_cu;
438
439 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 440 int last_used = 0;
ae038cb0 441
b64f50a1
JK
442 /* A hash table of DIE cu_offset for following references with
443 die_info->offset.sect_off as hash. */
fcd3b13d 444 htab_t die_hash = nullptr;
10b3939b
DJ
445
446 /* Full DIEs if read in. */
fcd3b13d 447 struct die_info *dies = nullptr;
10b3939b
DJ
448
449 /* A set of pointers to dwarf2_per_cu_data objects for compilation
450 units referenced by this one. Only set during full symbol processing;
451 partial symbol tables do not have dependencies. */
fcd3b13d 452 htab_t dependencies = nullptr;
10b3939b 453
cb1df416 454 /* Header data from the line table, during full symbol processing. */
fcd3b13d 455 struct line_header *line_header = nullptr;
4c8aa72d
PA
456 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
457 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
458 this is the DW_TAG_compile_unit die for this CU. We'll hold on
459 to the line header as long as this DIE is being processed. See
460 process_die_scope. */
fcd3b13d 461 die_info *line_header_die_owner = nullptr;
cb1df416 462
3da10d80
KS
463 /* A list of methods which need to have physnames computed
464 after all type information has been read. */
c89b44cd 465 std::vector<delayed_method_info> method_list;
3da10d80 466
96408a79 467 /* To be copied to symtab->call_site_htab. */
fcd3b13d 468 htab_t call_site_htab = nullptr;
96408a79 469
034e5797
DE
470 /* Non-NULL if this CU came from a DWO file.
471 There is an invariant here that is important to remember:
472 Except for attributes copied from the top level DIE in the "main"
473 (or "stub") file in preparation for reading the DWO file
18a8505e 474 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
475 Either there isn't a DWO file (in which case this is NULL and the point
476 is moot), or there is and either we're not going to read it (in which
477 case this is NULL) or there is and we are reading it (in which case this
478 is non-NULL). */
fcd3b13d 479 struct dwo_unit *dwo_unit = nullptr;
3019eac3 480
18a8505e 481 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 482 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 483 gdb::optional<ULONGEST> addr_base;
3019eac3 484
18a8505e 485 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 486 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 487 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
488 be used without needing to know whether DWO files are in use or not.
489 N.B. This does not apply to DW_AT_ranges appearing in
490 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
491 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 492 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 493 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 494 ULONGEST ranges_base = 0;
2e3cf129 495
c9317f21
TT
496 /* When reading debug info generated by older versions of rustc, we
497 have to rewrite some union types to be struct types with a
498 variant part. This rewriting must be done after the CU is fully
499 read in, because otherwise at the point of rewriting some struct
500 type might not have been fully processed. So, we keep a list of
501 all such types here and process them after expansion. */
502 std::vector<struct type *> rust_unions;
503
18a8505e
AT
504 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
505 files, the value is implicitly zero. For DWARF 5 version DWO files, the
506 value is often implicit and is the size of the header of
507 .debug_str_offsets section (8 or 4, depending on the address size). */
508 gdb::optional<ULONGEST> str_offsets_base;
509
ae038cb0 510 /* Mark used when releasing cached dies. */
9068261f 511 bool mark : 1;
ae038cb0 512
8be455d7
JK
513 /* This CU references .debug_loc. See the symtab->locations_valid field.
514 This test is imperfect as there may exist optimized debug code not using
515 any location list and still facing inlining issues if handled as
516 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 517 bool has_loclist : 1;
ba919b58 518
9068261f 519 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
520 if all the producer_is_* fields are valid. This information is cached
521 because profiling CU expansion showed excessive time spent in
522 producer_is_gxx_lt_4_6. */
9068261f
AB
523 bool checked_producer : 1;
524 bool producer_is_gxx_lt_4_6 : 1;
525 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 526 bool producer_is_icc : 1;
9068261f 527 bool producer_is_icc_lt_14 : 1;
c258c396 528 bool producer_is_codewarrior : 1;
4d4ec4e5 529
9068261f 530 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
531 debugging info for C++ namespaces. GCC 3.3.x did not produce
532 this information, but later versions do. */
533
9068261f 534 bool processing_has_namespace_info : 1;
d590ff25
YQ
535
536 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
537
538 /* If this CU was inherited by another CU (via specification,
539 abstract_origin, etc), this is the ancestor CU. */
540 dwarf2_cu *ancestor;
541
542 /* Get the buildsym_compunit for this CU. */
543 buildsym_compunit *get_builder ()
544 {
545 /* If this CU has a builder associated with it, use that. */
546 if (m_builder != nullptr)
547 return m_builder.get ();
548
549 /* Otherwise, search ancestors for a valid builder. */
550 if (ancestor != nullptr)
551 return ancestor->get_builder ();
552
553 return nullptr;
554 }
e7c27a73
DJ
555};
556
094b34ac
DE
557/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
558 This includes type_unit_group and quick_file_names. */
559
560struct 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
565 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 566 sect_offset line_sect_off;
094b34ac
DE
567};
568
f4dc4d17
DE
569/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
570 an object of this type. */
571
572struct type_unit_group
573{
0186c6a7 574 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
575 To simplify things we create an artificial CU that "includes" all the
576 type units using this stmt_list so that the rest of the code still has
197400e8 577 a "per_cu" handle on the symtab. */
094b34ac
DE
578 struct dwarf2_per_cu_data per_cu;
579
0186c6a7
DE
580 /* The TUs that share this DW_AT_stmt_list entry.
581 This is added to while parsing type units to build partial symtabs,
582 and is deleted afterwards and not used again. */
a8b3b8e9 583 std::vector<signatured_type *> *tus;
f4dc4d17 584
43f3e411 585 /* The compunit symtab.
094b34ac 586 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
587 so we create an essentially anonymous symtab as the compunit symtab. */
588 struct compunit_symtab *compunit_symtab;
f4dc4d17 589
094b34ac
DE
590 /* The data used to construct the hash key. */
591 struct stmt_list_hash hash;
f4dc4d17 592
f4dc4d17
DE
593 /* The symbol tables for this TU (obtained from the files listed in
594 DW_AT_stmt_list).
595 WARNING: The order of entries here must match the order of entries
596 in the line header. After the first TU using this type_unit_group, the
597 line header for the subsequent TUs is recreated from this. This is done
598 because we need to use the same symtabs for each TU using the same
599 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
600 there's no guarantee the line header doesn't have duplicate entries. */
601 struct symtab **symtabs;
602};
603
73869dc2 604/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
605
606struct dwo_sections
607{
608 struct dwarf2_section_info abbrev;
3019eac3
DE
609 struct dwarf2_section_info line;
610 struct dwarf2_section_info loc;
43988095 611 struct dwarf2_section_info loclists;
09262596
DE
612 struct dwarf2_section_info macinfo;
613 struct dwarf2_section_info macro;
3019eac3
DE
614 struct dwarf2_section_info str;
615 struct dwarf2_section_info str_offsets;
80626a55
DE
616 /* In the case of a virtual DWO file, these two are unused. */
617 struct dwarf2_section_info info;
fd5866f6 618 std::vector<dwarf2_section_info> types;
3019eac3
DE
619};
620
c88ee1f0 621/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
622
623struct dwo_unit
624{
625 /* Backlink to the containing struct dwo_file. */
626 struct dwo_file *dwo_file;
627
628 /* The "id" that distinguishes this CU/TU.
629 .debug_info calls this "dwo_id", .debug_types calls this "signature".
630 Since signatures came first, we stick with it for consistency. */
631 ULONGEST signature;
632
633 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 634 struct dwarf2_section_info *section;
3019eac3 635
9c541725
PA
636 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
637 sect_offset sect_off;
3019eac3
DE
638 unsigned int length;
639
640 /* For types, offset in the type's DIE of the type defined by this TU. */
641 cu_offset type_offset_in_tu;
642};
643
73869dc2
DE
644/* include/dwarf2.h defines the DWP section codes.
645 It defines a max value but it doesn't define a min value, which we
646 use for error checking, so provide one. */
647
648enum dwp_v2_section_ids
649{
650 DW_SECT_MIN = 1
651};
652
80626a55 653/* Data for one DWO file.
57d63ce2
DE
654
655 This includes virtual DWO files (a virtual DWO file is a DWO file as it
656 appears in a DWP file). DWP files don't really have DWO files per se -
657 comdat folding of types "loses" the DWO file they came from, and from
658 a high level view DWP files appear to contain a mass of random types.
659 However, to maintain consistency with the non-DWP case we pretend DWP
660 files contain virtual DWO files, and we assign each TU with one virtual
661 DWO file (generally based on the line and abbrev section offsets -
662 a heuristic that seems to work in practice). */
3019eac3
DE
663
664struct dwo_file
665{
51ac9db5
SM
666 dwo_file () = default;
667 DISABLE_COPY_AND_ASSIGN (dwo_file);
668
18a8505e 669 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
670 For virtual DWO files the name is constructed from the section offsets
671 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
672 from related CU+TUs. */
51ac9db5 673 const char *dwo_name = nullptr;
0ac5b59e
DE
674
675 /* The DW_AT_comp_dir attribute. */
51ac9db5 676 const char *comp_dir = nullptr;
3019eac3 677
80626a55
DE
678 /* The bfd, when the file is open. Otherwise this is NULL.
679 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 680 gdb_bfd_ref_ptr dbfd;
3019eac3 681
73869dc2
DE
682 /* The sections that make up this DWO file.
683 Remember that for virtual DWO files in DWP V2, these are virtual
684 sections (for lack of a better name). */
51ac9db5 685 struct dwo_sections sections {};
3019eac3 686
33c5cd75
DB
687 /* The CUs in the file.
688 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
689 an extension to handle LLVM's Link Time Optimization output (where
690 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 691 htab_up cus;
3019eac3
DE
692
693 /* Table of TUs in the file.
694 Each element is a struct dwo_unit. */
b0b6a987 695 htab_up tus;
3019eac3
DE
696};
697
80626a55
DE
698/* These sections are what may appear in a DWP file. */
699
700struct dwp_sections
701{
73869dc2 702 /* These are used by both DWP version 1 and 2. */
80626a55
DE
703 struct dwarf2_section_info str;
704 struct dwarf2_section_info cu_index;
705 struct dwarf2_section_info tu_index;
73869dc2
DE
706
707 /* These are only used by DWP version 2 files.
708 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
709 sections are referenced by section number, and are not recorded here.
710 In DWP version 2 there is at most one copy of all these sections, each
711 section being (effectively) comprised of the concatenation of all of the
712 individual sections that exist in the version 1 format.
713 To keep the code simple we treat each of these concatenated pieces as a
714 section itself (a virtual section?). */
715 struct dwarf2_section_info abbrev;
716 struct dwarf2_section_info info;
717 struct dwarf2_section_info line;
718 struct dwarf2_section_info loc;
719 struct dwarf2_section_info macinfo;
720 struct dwarf2_section_info macro;
721 struct dwarf2_section_info str_offsets;
722 struct dwarf2_section_info types;
80626a55
DE
723};
724
73869dc2
DE
725/* These sections are what may appear in a virtual DWO file in DWP version 1.
726 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 727
73869dc2 728struct virtual_v1_dwo_sections
80626a55
DE
729{
730 struct dwarf2_section_info abbrev;
731 struct dwarf2_section_info line;
732 struct dwarf2_section_info loc;
733 struct dwarf2_section_info macinfo;
734 struct dwarf2_section_info macro;
735 struct dwarf2_section_info str_offsets;
736 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 737 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
738 struct dwarf2_section_info info_or_types;
739};
740
73869dc2
DE
741/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
742 In version 2, the sections of the DWO files are concatenated together
743 and stored in one section of that name. Thus each ELF section contains
744 several "virtual" sections. */
745
746struct virtual_v2_dwo_sections
747{
748 bfd_size_type abbrev_offset;
749 bfd_size_type abbrev_size;
750
751 bfd_size_type line_offset;
752 bfd_size_type line_size;
753
754 bfd_size_type loc_offset;
755 bfd_size_type loc_size;
756
757 bfd_size_type macinfo_offset;
758 bfd_size_type macinfo_size;
759
760 bfd_size_type macro_offset;
761 bfd_size_type macro_size;
762
763 bfd_size_type str_offsets_offset;
764 bfd_size_type str_offsets_size;
765
766 /* Each DWP hash table entry records one CU or one TU.
767 That is recorded here, and copied to dwo_unit.section. */
768 bfd_size_type info_or_types_offset;
769 bfd_size_type info_or_types_size;
770};
771
80626a55
DE
772/* Contents of DWP hash tables. */
773
774struct dwp_hash_table
775{
73869dc2 776 uint32_t version, nr_columns;
80626a55 777 uint32_t nr_units, nr_slots;
73869dc2
DE
778 const gdb_byte *hash_table, *unit_table;
779 union
780 {
781 struct
782 {
783 const gdb_byte *indices;
784 } v1;
785 struct
786 {
787 /* This is indexed by column number and gives the id of the section
788 in that column. */
789#define MAX_NR_V2_DWO_SECTIONS \
790 (1 /* .debug_info or .debug_types */ \
791 + 1 /* .debug_abbrev */ \
792 + 1 /* .debug_line */ \
793 + 1 /* .debug_loc */ \
794 + 1 /* .debug_str_offsets */ \
795 + 1 /* .debug_macro or .debug_macinfo */)
796 int section_ids[MAX_NR_V2_DWO_SECTIONS];
797 const gdb_byte *offsets;
798 const gdb_byte *sizes;
799 } v2;
800 } section_pool;
80626a55
DE
801};
802
803/* Data for one DWP file. */
804
805struct dwp_file
806{
400174b1
TT
807 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
808 : name (name_),
809 dbfd (std::move (abfd))
810 {
811 }
812
80626a55
DE
813 /* Name of the file. */
814 const char *name;
815
73869dc2 816 /* File format version. */
400174b1 817 int version = 0;
73869dc2 818
93417882 819 /* The bfd. */
400174b1 820 gdb_bfd_ref_ptr dbfd;
80626a55
DE
821
822 /* Section info for this file. */
400174b1 823 struct dwp_sections sections {};
80626a55 824
57d63ce2 825 /* Table of CUs in the file. */
400174b1 826 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
827
828 /* Table of TUs in the file. */
400174b1 829 const struct dwp_hash_table *tus = nullptr;
80626a55 830
19ac8c2e 831 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
832 htab_up loaded_cus;
833 htab_up loaded_tus;
80626a55 834
73869dc2
DE
835 /* Table to map ELF section numbers to their sections.
836 This is only needed for the DWP V1 file format. */
400174b1
TT
837 unsigned int num_sections = 0;
838 asection **elf_sections = nullptr;
80626a55
DE
839};
840
0963b4bd
MS
841/* Struct used to pass misc. parameters to read_die_and_children, et
842 al. which are used for both .debug_info and .debug_types dies.
843 All parameters here are unchanging for the life of the call. This
dee91e82 844 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
845
846struct die_reader_specs
847{
a32a8923 848 /* The bfd of die_section. */
93311388
DE
849 bfd* abfd;
850
851 /* The CU of the DIE we are parsing. */
852 struct dwarf2_cu *cu;
853
80626a55 854 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
855 struct dwo_file *dwo_file;
856
dee91e82 857 /* The section the die comes from.
3019eac3 858 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
859 struct dwarf2_section_info *die_section;
860
861 /* die_section->buffer. */
d521ce57 862 const gdb_byte *buffer;
f664829e
DE
863
864 /* The end of the buffer. */
865 const gdb_byte *buffer_end;
a2ce51a0 866
685af9cd
TT
867 /* The abbreviation table to use when reading the DIEs. */
868 struct abbrev_table *abbrev_table;
93311388
DE
869};
870
c0ab21c2
TT
871/* A subclass of die_reader_specs that holds storage and has complex
872 constructor and destructor behavior. */
873
874class cutu_reader : public die_reader_specs
875{
876public:
877
878 cutu_reader (struct dwarf2_per_cu_data *this_cu,
879 struct abbrev_table *abbrev_table,
6751ebae 880 int use_existing_cu,
c0ab21c2
TT
881 bool skip_partial);
882
883 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
884 struct dwarf2_cu *parent_cu = nullptr,
885 struct dwo_file *dwo_file = nullptr);
886
c0ab21c2
TT
887 DISABLE_COPY_AND_ASSIGN (cutu_reader);
888
889 const gdb_byte *info_ptr = nullptr;
890 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
891 bool dummy_p = false;
892
6751ebae
TT
893 /* Release the new CU, putting it on the chain. This cannot be done
894 for dummy CUs. */
895 void keep ();
896
c0ab21c2
TT
897private:
898 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 899 int use_existing_cu);
c0ab21c2
TT
900
901 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
902 std::unique_ptr<dwarf2_cu> m_new_cu;
903
904 /* The ordinary abbreviation table. */
905 abbrev_table_up m_abbrev_table_holder;
906
907 /* The DWO abbreviation table. */
908 abbrev_table_up m_dwo_abbrev_table;
909};
dee91e82 910
c906108c 911/* When we construct a partial symbol table entry we only
0963b4bd 912 need this much information. */
6f06d47b 913struct partial_die_info : public allocate_on_obstack
c906108c 914 {
6f06d47b
YQ
915 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
916
917 /* Disable assign but still keep copy ctor, which is needed
918 load_partial_dies. */
919 partial_die_info& operator=(const partial_die_info& rhs) = delete;
920
52356b79
YQ
921 /* Adjust the partial die before generating a symbol for it. This
922 function may set the is_external flag or change the DIE's
923 name. */
924 void fixup (struct dwarf2_cu *cu);
925
48fbe735
YQ
926 /* Read a minimal amount of information into the minimal die
927 structure. */
928 const gdb_byte *read (const struct die_reader_specs *reader,
929 const struct abbrev_info &abbrev,
930 const gdb_byte *info_ptr);
931
72bf9492 932 /* Offset of this DIE. */
6f06d47b 933 const sect_offset sect_off;
72bf9492
DJ
934
935 /* DWARF-2 tag for this DIE. */
6f06d47b 936 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 937
72bf9492 938 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
939 const unsigned int has_children : 1;
940
72bf9492
DJ
941 unsigned int is_external : 1;
942 unsigned int is_declaration : 1;
943 unsigned int has_type : 1;
944 unsigned int has_specification : 1;
945 unsigned int has_pc_info : 1;
481860b3 946 unsigned int may_be_inlined : 1;
72bf9492 947
0c1b455e
TT
948 /* This DIE has been marked DW_AT_main_subprogram. */
949 unsigned int main_subprogram : 1;
950
72bf9492
DJ
951 /* Flag set if the SCOPE field of this structure has been
952 computed. */
953 unsigned int scope_set : 1;
954
fa4028e9
JB
955 /* Flag set if the DIE has a byte_size attribute. */
956 unsigned int has_byte_size : 1;
957
ff908ebf
AW
958 /* Flag set if the DIE has a DW_AT_const_value attribute. */
959 unsigned int has_const_value : 1;
960
98bfdba5
PA
961 /* Flag set if any of the DIE's children are template arguments. */
962 unsigned int has_template_arguments : 1;
963
52356b79 964 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
965 unsigned int fixup_called : 1;
966
36586728
TT
967 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
968 unsigned int is_dwz : 1;
969
970 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
971 unsigned int spec_is_dwz : 1;
972
72bf9492 973 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 974 sometimes a default name for unnamed DIEs. */
6f06d47b 975 const char *name = nullptr;
72bf9492 976
abc72ce4 977 /* The linkage name, if present. */
6f06d47b 978 const char *linkage_name = nullptr;
abc72ce4 979
72bf9492
DJ
980 /* The scope to prepend to our children. This is generally
981 allocated on the comp_unit_obstack, so will disappear
982 when this compilation unit leaves the cache. */
6f06d47b 983 const char *scope = nullptr;
72bf9492 984
95554aad
TT
985 /* Some data associated with the partial DIE. The tag determines
986 which field is live. */
987 union
988 {
989 /* The location description associated with this DIE, if any. */
990 struct dwarf_block *locdesc;
991 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 992 sect_offset sect_off;
6f06d47b 993 } d {};
72bf9492
DJ
994
995 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
996 CORE_ADDR lowpc = 0;
997 CORE_ADDR highpc = 0;
72bf9492 998
93311388 999 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1000 DW_AT_sibling, if any. */
48fbe735
YQ
1001 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1002 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1003 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1004
1005 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1006 DW_AT_specification (or DW_AT_abstract_origin or
1007 DW_AT_extension). */
6f06d47b 1008 sect_offset spec_offset {};
72bf9492
DJ
1009
1010 /* Pointers to this DIE's parent, first child, and next sibling,
1011 if any. */
6f06d47b
YQ
1012 struct partial_die_info *die_parent = nullptr;
1013 struct partial_die_info *die_child = nullptr;
1014 struct partial_die_info *die_sibling = nullptr;
1015
1016 friend struct partial_die_info *
1017 dwarf2_cu::find_partial_die (sect_offset sect_off);
1018
1019 private:
1020 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1021 partial_die_info (sect_offset sect_off)
1022 : partial_die_info (sect_off, DW_TAG_padding, 0)
1023 {
1024 }
1025
1026 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1027 int has_children_)
1028 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1029 {
1030 is_external = 0;
1031 is_declaration = 0;
1032 has_type = 0;
1033 has_specification = 0;
1034 has_pc_info = 0;
1035 may_be_inlined = 0;
1036 main_subprogram = 0;
1037 scope_set = 0;
1038 has_byte_size = 0;
1039 has_const_value = 0;
1040 has_template_arguments = 0;
1041 fixup_called = 0;
1042 is_dwz = 0;
1043 spec_is_dwz = 0;
1044 }
c906108c
SS
1045 };
1046
c906108c
SS
1047/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1048 but this would require a corresponding change in unpack_field_as_long
1049 and friends. */
1050static int bits_per_byte = 8;
1051
2ddeaf8a
TT
1052/* When reading a variant or variant part, we track a bit more
1053 information about the field, and store it in an object of this
1054 type. */
1055
1056struct variant_field
1057{
1058 /* If we see a DW_TAG_variant, then this will be the discriminant
1059 value. */
1060 ULONGEST discriminant_value;
1061 /* If we see a DW_TAG_variant, then this will be set if this is the
1062 default branch. */
1063 bool default_branch;
1064 /* While reading a DW_TAG_variant_part, this will be set if this
1065 field is the discriminant. */
1066 bool is_discriminant;
1067};
1068
52059ffd
TT
1069struct nextfield
1070{
be2daae6
TT
1071 int accessibility = 0;
1072 int virtuality = 0;
2ddeaf8a 1073 /* Extra information to describe a variant or variant part. */
be2daae6
TT
1074 struct variant_field variant {};
1075 struct field field {};
52059ffd
TT
1076};
1077
1078struct fnfieldlist
1079{
be2daae6
TT
1080 const char *name = nullptr;
1081 std::vector<struct fn_field> fnfields;
52059ffd
TT
1082};
1083
c906108c
SS
1084/* The routines that read and process dies for a C struct or C++ class
1085 pass lists of data member fields and lists of member function fields
1086 in an instance of a field_info structure, as defined below. */
1087struct field_info
c5aa993b 1088 {
0963b4bd 1089 /* List of data member and baseclasses fields. */
be2daae6
TT
1090 std::vector<struct nextfield> fields;
1091 std::vector<struct nextfield> baseclasses;
c906108c 1092
85102364 1093 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1094 int non_public_fields = 0;
c906108c 1095
c5aa993b
JM
1096 /* Member function fieldlist array, contains name of possibly overloaded
1097 member function, number of overloaded member functions and a pointer
1098 to the head of the member function field chain. */
be2daae6 1099 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1100
1101 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1102 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1103 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1104
1105 /* Nested types defined by this class and the number of elements in this
1106 list. */
be2daae6 1107 std::vector<struct decl_field> nested_types_list;
317f7127
TT
1108
1109 /* Return the total number of fields (including baseclasses). */
1110 int nfields () const
1111 {
1112 return fields.size () + baseclasses.size ();
1113 }
c5aa993b 1114 };
c906108c 1115
ae038cb0
DJ
1116/* Loaded secondary compilation units are kept in memory until they
1117 have not been referenced for the processing of this many
1118 compilation units. Set this to zero to disable caching. Cache
1119 sizes of up to at least twenty will improve startup time for
1120 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1121static int dwarf_max_cache_age = 5;
920d2a44 1122static void
b4f54984
DE
1123show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1124 struct cmd_list_element *c, const char *value)
920d2a44 1125{
3e43a32a 1126 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1127 "DWARF compilation units is %s.\n"),
920d2a44
AC
1128 value);
1129}
4390d890 1130\f
c906108c
SS
1131/* local function prototypes */
1132
918dd910
JK
1133static void dwarf2_find_base_address (struct die_info *die,
1134 struct dwarf2_cu *cu);
1135
891813be 1136static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1137 (struct dwarf2_per_cu_data *per_cu, const char *name);
1138
f1902523
JK
1139static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1140 const gdb_byte *info_ptr,
3e225074 1141 struct die_info *type_unit_die);
f1902523 1142
ed2dc618
SM
1143static void dwarf2_build_psymtabs_hard
1144 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1145
72bf9492
DJ
1146static void scan_partial_symbols (struct partial_die_info *,
1147 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1148 int, struct dwarf2_cu *);
c906108c 1149
72bf9492
DJ
1150static void add_partial_symbol (struct partial_die_info *,
1151 struct dwarf2_cu *);
63d06c5c 1152
72bf9492
DJ
1153static void add_partial_namespace (struct partial_die_info *pdi,
1154 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1155 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1156
5d7cb8df 1157static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1158 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1159 struct dwarf2_cu *cu);
1160
72bf9492
DJ
1161static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1162 struct dwarf2_cu *cu);
91c24f0a 1163
bc30ff58
JB
1164static void add_partial_subprogram (struct partial_die_info *pdi,
1165 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1166 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1167
d521ce57 1168static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1169
dee91e82 1170static struct partial_die_info *load_partial_dies
d521ce57 1171 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1172
fb816e8b
TV
1173/* A pair of partial_die_info and compilation unit. */
1174struct cu_partial_die_info
1175{
1176 /* The compilation unit of the partial_die_info. */
1177 struct dwarf2_cu *cu;
1178 /* A partial_die_info. */
1179 struct partial_die_info *pdi;
122cf0f2
AB
1180
1181 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1182 : cu (cu),
1183 pdi (pdi)
405feb71 1184 { /* Nothing. */ }
122cf0f2
AB
1185
1186private:
1187 cu_partial_die_info () = delete;
fb816e8b
TV
1188};
1189
122cf0f2
AB
1190static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1191 struct dwarf2_cu *);
72bf9492 1192
d521ce57
TT
1193static const gdb_byte *read_attribute (const struct die_reader_specs *,
1194 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1195 const gdb_byte *, bool *need_reprocess);
1196
1197static void read_attribute_reprocess (const struct die_reader_specs *reader,
1198 struct attribute *attr);
1199
1200static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1201
ed2dc618
SM
1202static sect_offset read_abbrev_offset
1203 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1204 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1205
ed2dc618
SM
1206static const char *read_indirect_string
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1208 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1209
ed2dc618 1210static const char *read_indirect_string_at_offset
4f44ae6c 1211 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1212
d521ce57
TT
1213static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1214 const gdb_byte *,
3019eac3
DE
1215 unsigned int *);
1216
18a8505e
AT
1217static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1218 ULONGEST str_index);
1219
1220static const char *read_stub_str_index (struct dwarf2_cu *cu,
1221 ULONGEST str_index);
3019eac3 1222
e142c38c 1223static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1224
e142c38c
DJ
1225static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1226 struct dwarf2_cu *);
c906108c 1227
7d45c7c3
KB
1228static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1229 struct dwarf2_cu *cu);
1230
a084a2a6
AT
1231static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1232
05cf31d1
JB
1233static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1234 struct dwarf2_cu *cu);
1235
e142c38c 1236static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1237
e142c38c 1238static struct die_info *die_specification (struct die_info *die,
f2f0e013 1239 struct dwarf2_cu **);
63d06c5c 1240
9c541725 1241static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1242 struct dwarf2_cu *cu);
debd256d 1243
f3f5162e 1244static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1245 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1246 CORE_ADDR, int decode_mapping);
c906108c 1247
804d2729
TT
1248static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1249 const char *);
c906108c 1250
a14ed312 1251static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1252 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1253
ff39bb5e 1254static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1255 struct dwarf2_cu *);
c906108c 1256
ff39bb5e 1257static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1258 struct type *type,
1259 const char *name,
1260 struct obstack *obstack,
12df843f 1261 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1262 const gdb_byte **bytes,
98bfdba5 1263 struct dwarf2_locexpr_baton **baton);
2df3850c 1264
e7c27a73 1265static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1266
b4ba55a1
JB
1267static int need_gnat_info (struct dwarf2_cu *);
1268
3e43a32a
MS
1269static struct type *die_descriptive_type (struct die_info *,
1270 struct dwarf2_cu *);
b4ba55a1
JB
1271
1272static void set_descriptive_type (struct type *, struct die_info *,
1273 struct dwarf2_cu *);
1274
e7c27a73
DJ
1275static struct type *die_containing_type (struct die_info *,
1276 struct dwarf2_cu *);
c906108c 1277
ff39bb5e 1278static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1279 struct dwarf2_cu *);
c906108c 1280
f792889a 1281static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1282
673bfd45
DE
1283static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1284
0d5cff50 1285static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1286
6e70227d 1287static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1288 const char *suffix, int physname,
1289 struct dwarf2_cu *cu);
63d06c5c 1290
e7c27a73 1291static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1292
348e048f
DE
1293static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1294
e7c27a73 1295static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1296
e7c27a73 1297static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1298
96408a79
SA
1299static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1300
71a3c369
TT
1301static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1302
ff013f42 1303static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1304 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1305
3a2b436a 1306/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1307 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1308enum pc_bounds_kind
1309{
e385593e 1310 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1311 PC_BOUNDS_NOT_PRESENT,
1312
e385593e
JK
1313 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1314 were present but they do not form a valid range of PC addresses. */
1315 PC_BOUNDS_INVALID,
1316
3a2b436a
JK
1317 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1318 PC_BOUNDS_RANGES,
1319
1320 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1321 PC_BOUNDS_HIGH_LOW,
1322};
1323
1324static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1325 CORE_ADDR *, CORE_ADDR *,
1326 struct dwarf2_cu *,
891813be 1327 dwarf2_psymtab *);
c906108c 1328
fae299cd
DC
1329static void get_scope_pc_bounds (struct die_info *,
1330 CORE_ADDR *, CORE_ADDR *,
1331 struct dwarf2_cu *);
1332
801e3a5b
JB
1333static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1334 CORE_ADDR, struct dwarf2_cu *);
1335
a14ed312 1336static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1337 struct dwarf2_cu *);
c906108c 1338
a14ed312 1339static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1340 struct type *, struct dwarf2_cu *);
c906108c 1341
a14ed312 1342static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1343 struct die_info *, struct type *,
e7c27a73 1344 struct dwarf2_cu *);
c906108c 1345
a14ed312 1346static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1347 struct type *,
1348 struct dwarf2_cu *);
c906108c 1349
134d01f1 1350static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1351
e7c27a73 1352static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1353
e7c27a73 1354static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1355
5d7cb8df
JK
1356static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1357
804d2729 1358static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1359
27aa8d6a
SW
1360static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1361
74921315
KS
1362static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1363
f55ee35c
JK
1364static struct type *read_module_type (struct die_info *die,
1365 struct dwarf2_cu *cu);
1366
38d518c9 1367static const char *namespace_name (struct die_info *die,
e142c38c 1368 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1369
134d01f1 1370static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1371
e7c27a73 1372static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
c906108c 1373
6e70227d 1374static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1375 struct dwarf2_cu *);
1376
bf6af496 1377static struct die_info *read_die_and_siblings_1
d521ce57 1378 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1379 struct die_info *);
639d11d3 1380
dee91e82 1381static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1382 const gdb_byte *info_ptr,
1383 const gdb_byte **new_info_ptr,
639d11d3
DC
1384 struct die_info *parent);
1385
d521ce57
TT
1386static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1387 struct die_info **, const gdb_byte *,
3e225074 1388 int);
3019eac3 1389
d521ce57 1390static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1391 struct die_info **, const gdb_byte *);
93311388 1392
e7c27a73 1393static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1394
15d034d0 1395static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1396 struct objfile *);
71c25dea 1397
15d034d0 1398static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1399
15d034d0 1400static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1401 struct die_info *die,
1402 struct dwarf2_cu *cu);
1403
ca69b9e6
DE
1404static const char *dwarf2_physname (const char *name, struct die_info *die,
1405 struct dwarf2_cu *cu);
1406
e142c38c 1407static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1408 struct dwarf2_cu **);
9219021c 1409
d97bc12b
DE
1410static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1411
1412static void dump_die_for_error (struct die_info *);
1413
1414static void dump_die_1 (struct ui_file *, int level, int max_level,
1415 struct die_info *);
c906108c 1416
d97bc12b 1417/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1418
51545339 1419static void store_in_ref_table (struct die_info *,
10b3939b 1420 struct dwarf2_cu *);
c906108c 1421
348e048f 1422static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1423 const struct attribute *,
348e048f
DE
1424 struct dwarf2_cu **);
1425
10b3939b 1426static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1427 const struct attribute *,
f2f0e013 1428 struct dwarf2_cu **);
c906108c 1429
348e048f 1430static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1431 const struct attribute *,
348e048f
DE
1432 struct dwarf2_cu **);
1433
ac9ec31b
DE
1434static struct type *get_signatured_type (struct die_info *, ULONGEST,
1435 struct dwarf2_cu *);
1436
1437static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1438 const struct attribute *,
ac9ec31b
DE
1439 struct dwarf2_cu *);
1440
e5fe5e75 1441static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1442
52dc124a 1443static void read_signatured_type (struct signatured_type *);
348e048f 1444
63e43d3a
PMR
1445static int attr_to_dynamic_prop (const struct attribute *attr,
1446 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1447 struct dynamic_prop *prop, struct type *type);
63e43d3a 1448
c906108c
SS
1449/* memory allocation interface */
1450
7b5a2f43 1451static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1452
b60c80d6 1453static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1454
43f3e411 1455static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1456
8cf6f0b1
TT
1457static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1458 struct dwarf2_loclist_baton *baton,
ff39bb5e 1459 const struct attribute *attr);
8cf6f0b1 1460
ff39bb5e 1461static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1462 struct symbol *sym,
f1e6e072
TT
1463 struct dwarf2_cu *cu,
1464 int is_block);
4c2df51b 1465
d521ce57
TT
1466static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1467 const gdb_byte *info_ptr,
1468 struct abbrev_info *abbrev);
4bb7a0a7 1469
72bf9492
DJ
1470static hashval_t partial_die_hash (const void *item);
1471
1472static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1473
ae038cb0 1474static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1475 (sect_offset sect_off, unsigned int offset_in_dwz,
1476 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1477
9816fde3 1478static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1479 struct die_info *comp_unit_die,
1480 enum language pretend_language);
93311388 1481
ed2dc618 1482static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1483
dee91e82 1484static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1485
f792889a
DJ
1486static struct type *set_die_type (struct die_info *, struct type *,
1487 struct dwarf2_cu *);
1c379e20 1488
ed2dc618 1489static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1490
ed2dc618 1491static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1492
58f0c718 1493static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1494 enum language);
10b3939b 1495
95554aad
TT
1496static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1497 enum language);
10b3939b 1498
f4dc4d17
DE
1499static void process_full_type_unit (struct dwarf2_per_cu_data *,
1500 enum language);
1501
10b3939b
DJ
1502static void dwarf2_add_dependence (struct dwarf2_cu *,
1503 struct dwarf2_per_cu_data *);
1504
ae038cb0
DJ
1505static void dwarf2_mark (struct dwarf2_cu *);
1506
1507static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1508
b64f50a1 1509static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1510 struct dwarf2_per_cu_data *);
673bfd45 1511
f792889a 1512static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1513
95554aad
TT
1514static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1515 enum language pretend_language);
1516
ed2dc618 1517static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1518
b303c6f6
AB
1519/* Class, the destructor of which frees all allocated queue entries. This
1520 will only have work to do if an error was thrown while processing the
1521 dwarf. If no error was thrown then the queue entries should have all
1522 been processed, and freed, as we went along. */
1523
1524class dwarf2_queue_guard
1525{
1526public:
39856def
TT
1527 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1528 : m_per_objfile (per_objfile)
1529 {
1530 }
b303c6f6
AB
1531
1532 /* Free any entries remaining on the queue. There should only be
1533 entries left if we hit an error while processing the dwarf. */
1534 ~dwarf2_queue_guard ()
1535 {
39856def
TT
1536 /* Ensure that no memory is allocated by the queue. */
1537 std::queue<dwarf2_queue_item> empty;
1538 std::swap (m_per_objfile->queue, empty);
1539 }
b303c6f6 1540
39856def 1541 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1542
39856def
TT
1543private:
1544 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1545};
1546
39856def
TT
1547dwarf2_queue_item::~dwarf2_queue_item ()
1548{
1549 /* Anything still marked queued is likely to be in an
1550 inconsistent state, so discard it. */
1551 if (per_cu->queued)
1552 {
1553 if (per_cu->cu != NULL)
1554 free_one_cached_comp_unit (per_cu);
1555 per_cu->queued = 0;
1556 }
1557}
1558
d721ba37
PA
1559/* The return type of find_file_and_directory. Note, the enclosed
1560 string pointers are only valid while this object is valid. */
1561
1562struct file_and_directory
1563{
1564 /* The filename. This is never NULL. */
1565 const char *name;
1566
1567 /* The compilation directory. NULL if not known. If we needed to
1568 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1569 points directly to the DW_AT_comp_dir string attribute owned by
1570 the obstack that owns the DIE. */
1571 const char *comp_dir;
1572
1573 /* If we needed to build a new string for comp_dir, this is what
1574 owns the storage. */
1575 std::string comp_dir_storage;
1576};
1577
1578static file_and_directory find_file_and_directory (struct die_info *die,
1579 struct dwarf2_cu *cu);
9291a0cd 1580
298e9637 1581static htab_up allocate_signatured_type_table ();
1fd400ff 1582
298e9637 1583static htab_up allocate_dwo_unit_table ();
3019eac3 1584
57d63ce2 1585static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1586 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1587 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1588 ULONGEST signature, int is_debug_types);
a2ce51a0 1589
ed2dc618
SM
1590static struct dwp_file *get_dwp_file
1591 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1592
3019eac3 1593static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1594 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1595
1596static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1597 (struct signatured_type *, const char *, const char *);
3019eac3 1598
89e63ee4
DE
1599static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1600
263db9a1
TT
1601/* A unique pointer to a dwo_file. */
1602
51ac9db5 1603typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1604
ed2dc618 1605static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1606
1b80a9fa 1607static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1608
1609static void free_line_header_voidp (void *arg);
4390d890
DE
1610\f
1611/* Various complaints about symbol reading that don't abort the process. */
1612
4390d890
DE
1613static void
1614dwarf2_debug_line_missing_file_complaint (void)
1615{
b98664d3 1616 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1617}
1618
1619static void
1620dwarf2_debug_line_missing_end_sequence_complaint (void)
1621{
b98664d3 1622 complaint (_(".debug_line section has line "
4390d890
DE
1623 "program sequence without an end"));
1624}
1625
1626static void
1627dwarf2_complex_location_expr_complaint (void)
1628{
b98664d3 1629 complaint (_("location expression too complex"));
4390d890
DE
1630}
1631
1632static void
1633dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1634 int arg3)
1635{
b98664d3 1636 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1637 arg1, arg2, arg3);
1638}
1639
4390d890
DE
1640static void
1641dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1642{
b98664d3 1643 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1644 arg1, arg2);
1645}
527f3840
JK
1646
1647/* Hash function for line_header_hash. */
1648
1649static hashval_t
1650line_header_hash (const struct line_header *ofs)
1651{
9c541725 1652 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1653}
1654
1655/* Hash function for htab_create_alloc_ex for line_header_hash. */
1656
1657static hashval_t
1658line_header_hash_voidp (const void *item)
1659{
9a3c8263 1660 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1661
1662 return line_header_hash (ofs);
1663}
1664
1665/* Equality function for line_header_hash. */
1666
1667static int
1668line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1669{
9a3c8263
SM
1670 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1671 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1672
9c541725 1673 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1674 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1675}
1676
4390d890 1677\f
9291a0cd 1678
330cdd98
PA
1679/* See declaration. */
1680
1681dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1682 const dwarf2_debug_sections *names,
1683 bool can_copy_)
1684 : objfile (objfile_),
1685 can_copy (can_copy_)
330cdd98
PA
1686{
1687 if (names == NULL)
1688 names = &dwarf2_elf_names;
1689
1690 bfd *obfd = objfile->obfd;
1691
1692 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1693 locate_sections (obfd, sec, *names);
1694}
1695
1696dwarf2_per_objfile::~dwarf2_per_objfile ()
1697{
1698 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1699 free_cached_comp_units ();
1700
b76e467d 1701 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1702 per_cu->imported_symtabs_free ();
fc8e7e75 1703
b2bdb8cf 1704 for (signatured_type *sig_type : all_type_units)
ae640021 1705 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1706
330cdd98
PA
1707 /* Everything else should be on the objfile obstack. */
1708}
1709
1710/* See declaration. */
1711
1712void
1713dwarf2_per_objfile::free_cached_comp_units ()
1714{
1715 dwarf2_per_cu_data *per_cu = read_in_chain;
1716 dwarf2_per_cu_data **last_chain = &read_in_chain;
1717 while (per_cu != NULL)
1718 {
1719 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1720
fcd3b13d 1721 delete per_cu->cu;
330cdd98
PA
1722 *last_chain = next_cu;
1723 per_cu = next_cu;
1724 }
1725}
1726
11ed8cad
TT
1727/* A helper class that calls free_cached_comp_units on
1728 destruction. */
1729
1730class free_cached_comp_units
1731{
1732public:
1733
1734 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1735 : m_per_objfile (per_objfile)
1736 {
1737 }
1738
1739 ~free_cached_comp_units ()
1740 {
1741 m_per_objfile->free_cached_comp_units ();
1742 }
1743
1744 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1745
1746private:
1747
1748 dwarf2_per_objfile *m_per_objfile;
1749};
1750
c906108c 1751/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1752 information and return true if we have enough to do something.
1753 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1754 ELF names are used. CAN_COPY is true for formats where symbol
1755 interposition is possible and so symbol values must follow copy
1756 relocation rules. */
c906108c
SS
1757
1758int
251d32d9 1759dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1760 const struct dwarf2_debug_sections *names,
1761 bool can_copy)
c906108c 1762{
97cbe998
SDJ
1763 if (objfile->flags & OBJF_READNEVER)
1764 return 0;
1765
ed2dc618
SM
1766 struct dwarf2_per_objfile *dwarf2_per_objfile
1767 = get_dwarf2_per_objfile (objfile);
1768
1769 if (dwarf2_per_objfile == NULL)
5bfd760d 1770 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1771 names,
1772 can_copy);
5bfd760d 1773
73869dc2 1774 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1775 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1776 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1777 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1778}
1779
251d32d9
TG
1780/* When loading sections, we look either for uncompressed section or for
1781 compressed section names. */
233a11ab
CS
1782
1783static int
251d32d9
TG
1784section_is_p (const char *section_name,
1785 const struct dwarf2_section_names *names)
233a11ab 1786{
251d32d9
TG
1787 if (names->normal != NULL
1788 && strcmp (section_name, names->normal) == 0)
1789 return 1;
1790 if (names->compressed != NULL
1791 && strcmp (section_name, names->compressed) == 0)
1792 return 1;
1793 return 0;
233a11ab
CS
1794}
1795
330cdd98 1796/* See declaration. */
c906108c 1797
330cdd98
PA
1798void
1799dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1800 const dwarf2_debug_sections &names)
c906108c 1801{
fd361982 1802 flagword aflag = bfd_section_flags (sectp);
251d32d9 1803
dc7650b8
JK
1804 if ((aflag & SEC_HAS_CONTENTS) == 0)
1805 {
1806 }
950b7495
KS
1807 else if (elf_section_data (sectp)->this_hdr.sh_size
1808 > bfd_get_file_size (abfd))
1809 {
1810 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1811 warning (_("Discarding section %s which has a section size (%s"
1812 ") larger than the file size [in module %s]"),
1813 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1814 bfd_get_filename (abfd));
1815 }
330cdd98 1816 else if (section_is_p (sectp->name, &names.info))
c906108c 1817 {
330cdd98 1818 this->info.s.section = sectp;
fd361982 1819 this->info.size = bfd_section_size (sectp);
c906108c 1820 }
330cdd98 1821 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1822 {
330cdd98 1823 this->abbrev.s.section = sectp;
fd361982 1824 this->abbrev.size = bfd_section_size (sectp);
c906108c 1825 }
330cdd98 1826 else if (section_is_p (sectp->name, &names.line))
c906108c 1827 {
330cdd98 1828 this->line.s.section = sectp;
fd361982 1829 this->line.size = bfd_section_size (sectp);
c906108c 1830 }
330cdd98 1831 else if (section_is_p (sectp->name, &names.loc))
c906108c 1832 {
330cdd98 1833 this->loc.s.section = sectp;
fd361982 1834 this->loc.size = bfd_section_size (sectp);
c906108c 1835 }
330cdd98 1836 else if (section_is_p (sectp->name, &names.loclists))
43988095 1837 {
330cdd98 1838 this->loclists.s.section = sectp;
fd361982 1839 this->loclists.size = bfd_section_size (sectp);
43988095 1840 }
330cdd98 1841 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1842 {
330cdd98 1843 this->macinfo.s.section = sectp;
fd361982 1844 this->macinfo.size = bfd_section_size (sectp);
c906108c 1845 }
330cdd98 1846 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1847 {
330cdd98 1848 this->macro.s.section = sectp;
fd361982 1849 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1850 }
330cdd98 1851 else if (section_is_p (sectp->name, &names.str))
c906108c 1852 {
330cdd98 1853 this->str.s.section = sectp;
fd361982 1854 this->str.size = bfd_section_size (sectp);
c906108c 1855 }
18a8505e
AT
1856 else if (section_is_p (sectp->name, &names.str_offsets))
1857 {
1858 this->str_offsets.s.section = sectp;
1859 this->str_offsets.size = bfd_section_size (sectp);
1860 }
330cdd98 1861 else if (section_is_p (sectp->name, &names.line_str))
43988095 1862 {
330cdd98 1863 this->line_str.s.section = sectp;
fd361982 1864 this->line_str.size = bfd_section_size (sectp);
43988095 1865 }
330cdd98 1866 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1867 {
330cdd98 1868 this->addr.s.section = sectp;
fd361982 1869 this->addr.size = bfd_section_size (sectp);
3019eac3 1870 }
330cdd98 1871 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1872 {
330cdd98 1873 this->frame.s.section = sectp;
fd361982 1874 this->frame.size = bfd_section_size (sectp);
b6af0555 1875 }
330cdd98 1876 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1877 {
330cdd98 1878 this->eh_frame.s.section = sectp;
fd361982 1879 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1880 }
330cdd98 1881 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1882 {
330cdd98 1883 this->ranges.s.section = sectp;
fd361982 1884 this->ranges.size = bfd_section_size (sectp);
af34e669 1885 }
330cdd98 1886 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1887 {
330cdd98 1888 this->rnglists.s.section = sectp;
fd361982 1889 this->rnglists.size = bfd_section_size (sectp);
43988095 1890 }
330cdd98 1891 else if (section_is_p (sectp->name, &names.types))
348e048f 1892 {
8b70b953
TT
1893 struct dwarf2_section_info type_section;
1894
1895 memset (&type_section, 0, sizeof (type_section));
049412e3 1896 type_section.s.section = sectp;
fd361982 1897 type_section.size = bfd_section_size (sectp);
8b70b953 1898
fd5866f6 1899 this->types.push_back (type_section);
348e048f 1900 }
330cdd98 1901 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1902 {
330cdd98 1903 this->gdb_index.s.section = sectp;
fd361982 1904 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1905 }
927aa2e7
JK
1906 else if (section_is_p (sectp->name, &names.debug_names))
1907 {
1908 this->debug_names.s.section = sectp;
fd361982 1909 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1910 }
1911 else if (section_is_p (sectp->name, &names.debug_aranges))
1912 {
1913 this->debug_aranges.s.section = sectp;
fd361982 1914 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1915 }
dce234bc 1916
fd361982
AM
1917 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1918 && bfd_section_vma (sectp) == 0)
330cdd98 1919 this->has_section_at_zero = true;
c906108c
SS
1920}
1921
dce234bc 1922/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1923 SECTION_NAME. */
af34e669 1924
dce234bc 1925void
3017a003
TG
1926dwarf2_get_section_info (struct objfile *objfile,
1927 enum dwarf2_section_enum sect,
d521ce57 1928 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1929 bfd_size_type *sizep)
1930{
5bfd760d 1931 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1932 struct dwarf2_section_info *info;
a3b2a86b
TT
1933
1934 /* We may see an objfile without any DWARF, in which case we just
1935 return nothing. */
1936 if (data == NULL)
1937 {
1938 *sectp = NULL;
1939 *bufp = NULL;
1940 *sizep = 0;
1941 return;
1942 }
3017a003
TG
1943 switch (sect)
1944 {
1945 case DWARF2_DEBUG_FRAME:
1946 info = &data->frame;
1947 break;
1948 case DWARF2_EH_FRAME:
1949 info = &data->eh_frame;
1950 break;
1951 default:
1952 gdb_assert_not_reached ("unexpected section");
1953 }
dce234bc 1954
96b79293 1955 info->read (objfile);
dce234bc 1956
96b79293 1957 *sectp = info->get_bfd_section ();
dce234bc
PP
1958 *bufp = info->buffer;
1959 *sizep = info->size;
1960}
1961
36586728
TT
1962/* A helper function to find the sections for a .dwz file. */
1963
1964static void
1965locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
1966{
9a3c8263 1967 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
1968
1969 /* Note that we only support the standard ELF names, because .dwz
1970 is ELF-only (at the time of writing). */
1971 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
1972 {
049412e3 1973 dwz_file->abbrev.s.section = sectp;
fd361982 1974 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
1975 }
1976 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
1977 {
049412e3 1978 dwz_file->info.s.section = sectp;
fd361982 1979 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
1980 }
1981 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
1982 {
049412e3 1983 dwz_file->str.s.section = sectp;
fd361982 1984 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
1985 }
1986 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
1987 {
049412e3 1988 dwz_file->line.s.section = sectp;
fd361982 1989 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
1990 }
1991 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
1992 {
049412e3 1993 dwz_file->macro.s.section = sectp;
fd361982 1994 dwz_file->macro.size = bfd_section_size (sectp);
36586728 1995 }
2ec9a5e0
TT
1996 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
1997 {
049412e3 1998 dwz_file->gdb_index.s.section = sectp;
fd361982 1999 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2000 }
927aa2e7
JK
2001 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2002 {
2003 dwz_file->debug_names.s.section = sectp;
fd361982 2004 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2005 }
36586728
TT
2006}
2007
c4973306 2008/* See dwarf2read.h. */
36586728 2009
c4973306 2010struct dwz_file *
ed2dc618 2011dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2012{
36586728 2013 const char *filename;
acd13123 2014 bfd_size_type buildid_len_arg;
dc294be5
TT
2015 size_t buildid_len;
2016 bfd_byte *buildid;
36586728
TT
2017
2018 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2019 return dwarf2_per_objfile->dwz_file.get ();
36586728 2020
4db1a1dc 2021 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2022 gdb::unique_xmalloc_ptr<char> data
2023 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2024 &buildid_len_arg, &buildid));
4db1a1dc
TT
2025 if (data == NULL)
2026 {
2027 if (bfd_get_error () == bfd_error_no_error)
2028 return NULL;
2029 error (_("could not read '.gnu_debugaltlink' section: %s"),
2030 bfd_errmsg (bfd_get_error ()));
2031 }
791afaa2
TT
2032
2033 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2034
acd13123
TT
2035 buildid_len = (size_t) buildid_len_arg;
2036
791afaa2 2037 filename = data.get ();
d721ba37
PA
2038
2039 std::string abs_storage;
36586728
TT
2040 if (!IS_ABSOLUTE_PATH (filename))
2041 {
14278e1f
TT
2042 gdb::unique_xmalloc_ptr<char> abs
2043 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2044
14278e1f 2045 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2046 filename = abs_storage.c_str ();
36586728
TT
2047 }
2048
dc294be5
TT
2049 /* First try the file name given in the section. If that doesn't
2050 work, try to use the build-id instead. */
192b62ce 2051 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2052 if (dwz_bfd != NULL)
36586728 2053 {
192b62ce 2054 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2055 dwz_bfd.reset (nullptr);
36586728
TT
2056 }
2057
dc294be5
TT
2058 if (dwz_bfd == NULL)
2059 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2060
0d79cdc4
AM
2061 if (dwz_bfd == nullptr)
2062 {
2063 gdb::unique_xmalloc_ptr<char> alt_filename;
2064 const char *origname = dwarf2_per_objfile->objfile->original_name;
2065
2066 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2067 buildid_len,
2068 origname,
2069 &alt_filename));
2070
2071 if (fd.get () >= 0)
2072 {
2073 /* File successfully retrieved from server. */
2074 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2075
2076 if (dwz_bfd == nullptr)
2077 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2078 alt_filename.get ());
2079 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2080 dwz_bfd.reset (nullptr);
2081 }
2082 }
2083
dc294be5
TT
2084 if (dwz_bfd == NULL)
2085 error (_("could not find '.gnu_debugaltlink' file for %s"),
2086 objfile_name (dwarf2_per_objfile->objfile));
2087
7ff8cb8c
TT
2088 std::unique_ptr<struct dwz_file> result
2089 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2090
7ff8cb8c
TT
2091 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2092 result.get ());
36586728 2093
7ff8cb8c
TT
2094 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2095 result->dwz_bfd.get ());
2096 dwarf2_per_objfile->dwz_file = std::move (result);
2097 return dwarf2_per_objfile->dwz_file.get ();
36586728 2098}
9291a0cd 2099\f
7b9f3c50
DE
2100/* DWARF quick_symbols_functions support. */
2101
2102/* TUs can share .debug_line entries, and there can be a lot more TUs than
2103 unique line tables, so we maintain a separate table of all .debug_line
2104 derived entries to support the sharing.
2105 All the quick functions need is the list of file names. We discard the
2106 line_header when we're done and don't need to record it here. */
2107struct quick_file_names
2108{
094b34ac
DE
2109 /* The data used to construct the hash key. */
2110 struct stmt_list_hash hash;
7b9f3c50
DE
2111
2112 /* The number of entries in file_names, real_names. */
2113 unsigned int num_file_names;
2114
2115 /* The file names from the line table, after being run through
2116 file_full_name. */
2117 const char **file_names;
2118
2119 /* The file names from the line table after being run through
2120 gdb_realpath. These are computed lazily. */
2121 const char **real_names;
2122};
2123
2124/* When using the index (and thus not using psymtabs), each CU has an
2125 object of this type. This is used to hold information needed by
2126 the various "quick" methods. */
2127struct dwarf2_per_cu_quick_data
2128{
2129 /* The file table. This can be NULL if there was no file table
2130 or it's currently not read in.
2131 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2132 struct quick_file_names *file_names;
2133
2134 /* The corresponding symbol table. This is NULL if symbols for this
2135 CU have not yet been read. */
43f3e411 2136 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2137
2138 /* A temporary mark bit used when iterating over all CUs in
2139 expand_symtabs_matching. */
2140 unsigned int mark : 1;
2141
2142 /* True if we've tried to read the file table and found there isn't one.
2143 There will be no point in trying to read it again next time. */
2144 unsigned int no_file_data : 1;
2145};
2146
094b34ac
DE
2147/* Utility hash function for a stmt_list_hash. */
2148
2149static hashval_t
2150hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2151{
2152 hashval_t v = 0;
2153
2154 if (stmt_list_hash->dwo_unit != NULL)
2155 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2156 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2157 return v;
2158}
2159
2160/* Utility equality function for a stmt_list_hash. */
2161
2162static int
2163eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2164 const struct stmt_list_hash *rhs)
2165{
2166 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2167 return 0;
2168 if (lhs->dwo_unit != NULL
2169 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2170 return 0;
2171
9c541725 2172 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2173}
2174
7b9f3c50
DE
2175/* Hash function for a quick_file_names. */
2176
2177static hashval_t
2178hash_file_name_entry (const void *e)
2179{
9a3c8263
SM
2180 const struct quick_file_names *file_data
2181 = (const struct quick_file_names *) e;
7b9f3c50 2182
094b34ac 2183 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2184}
2185
2186/* Equality function for a quick_file_names. */
2187
2188static int
2189eq_file_name_entry (const void *a, const void *b)
2190{
9a3c8263
SM
2191 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2192 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2193
094b34ac 2194 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2195}
2196
2197/* Delete function for a quick_file_names. */
2198
2199static void
2200delete_file_name_entry (void *e)
2201{
9a3c8263 2202 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2203 int i;
2204
2205 for (i = 0; i < file_data->num_file_names; ++i)
2206 {
2207 xfree ((void*) file_data->file_names[i]);
2208 if (file_data->real_names)
2209 xfree ((void*) file_data->real_names[i]);
2210 }
2211
2212 /* The space for the struct itself lives on objfile_obstack,
2213 so we don't free it here. */
2214}
2215
2216/* Create a quick_file_names hash table. */
2217
5895093f 2218static htab_up
7b9f3c50
DE
2219create_quick_file_names_table (unsigned int nr_initial_entries)
2220{
5895093f
TT
2221 return htab_up (htab_create_alloc (nr_initial_entries,
2222 hash_file_name_entry, eq_file_name_entry,
2223 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2224}
9291a0cd 2225
918dd910
JK
2226/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2227 have to be created afterwards. You should call age_cached_comp_units after
2228 processing PER_CU->CU. dw2_setup must have been already called. */
2229
2230static void
58f0c718 2231load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2232{
3019eac3 2233 if (per_cu->is_debug_types)
e5fe5e75 2234 load_full_type_unit (per_cu);
918dd910 2235 else
58f0c718 2236 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2237
cc12ce38
DE
2238 if (per_cu->cu == NULL)
2239 return; /* Dummy CU. */
2dc860c0
DE
2240
2241 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2242}
2243
a0f42c21 2244/* Read in the symbols for PER_CU. */
2fdf6df6 2245
9291a0cd 2246static void
58f0c718 2247dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2248{
ed2dc618 2249 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2250
f4dc4d17
DE
2251 /* Skip type_unit_groups, reading the type units they contain
2252 is handled elsewhere. */
197400e8 2253 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2254 return;
2255
b303c6f6
AB
2256 /* The destructor of dwarf2_queue_guard frees any entries left on
2257 the queue. After this point we're guaranteed to leave this function
2258 with the dwarf queue empty. */
39856def 2259 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2260
95554aad 2261 if (dwarf2_per_objfile->using_index
43f3e411 2262 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2263 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2264 {
2265 queue_comp_unit (per_cu, language_minimal);
58f0c718 2266 load_cu (per_cu, skip_partial);
89e63ee4
DE
2267
2268 /* If we just loaded a CU from a DWO, and we're working with an index
2269 that may badly handle TUs, load all the TUs in that DWO as well.
2270 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2271 if (!per_cu->is_debug_types
cc12ce38 2272 && per_cu->cu != NULL
89e63ee4
DE
2273 && per_cu->cu->dwo_unit != NULL
2274 && dwarf2_per_objfile->index_table != NULL
2275 && dwarf2_per_objfile->index_table->version <= 7
2276 /* DWP files aren't supported yet. */
ed2dc618 2277 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2278 queue_and_load_all_dwo_tus (per_cu);
95554aad 2279 }
9291a0cd 2280
ed2dc618 2281 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2282
2283 /* Age the cache, releasing compilation units that have not
2284 been used recently. */
ed2dc618 2285 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2286}
2287
2288/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2289 the objfile from which this CU came. Returns the resulting symbol
2290 table. */
2fdf6df6 2291
43f3e411 2292static struct compunit_symtab *
58f0c718 2293dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2294{
ed2dc618
SM
2295 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2296
95554aad 2297 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2298 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2299 {
11ed8cad 2300 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2301 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2302 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2303 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2304 }
f194fefb 2305
43f3e411 2306 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2307}
2308
ff4c9fec 2309/* See declaration. */
f4dc4d17 2310
ff4c9fec
SM
2311dwarf2_per_cu_data *
2312dwarf2_per_objfile::get_cutu (int index)
2313{
b76e467d 2314 if (index >= this->all_comp_units.size ())
ff4c9fec 2315 {
b76e467d 2316 index -= this->all_comp_units.size ();
b2bdb8cf 2317 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2318 return &this->all_type_units[index]->per_cu;
2319 }
f4dc4d17 2320
ff4c9fec
SM
2321 return this->all_comp_units[index];
2322}
f4dc4d17 2323
ff4c9fec 2324/* See declaration. */
2fdf6df6 2325
ff4c9fec
SM
2326dwarf2_per_cu_data *
2327dwarf2_per_objfile::get_cu (int index)
1fd400ff 2328{
b76e467d 2329 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2330
ff4c9fec 2331 return this->all_comp_units[index];
f4dc4d17
DE
2332}
2333
ff4c9fec 2334/* See declaration. */
f4dc4d17 2335
ff4c9fec
SM
2336signatured_type *
2337dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2338{
b2bdb8cf 2339 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2340
ff4c9fec 2341 return this->all_type_units[index];
1fd400ff
TT
2342}
2343
4b514bc8
JK
2344/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2345 objfile_obstack, and constructed with the specified field
2346 values. */
2347
2348static dwarf2_per_cu_data *
ed2dc618 2349create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2350 struct dwarf2_section_info *section,
2351 int is_dwz,
2352 sect_offset sect_off, ULONGEST length)
2353{
ed2dc618 2354 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2355 dwarf2_per_cu_data *the_cu
2356 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2357 struct dwarf2_per_cu_data);
2358 the_cu->sect_off = sect_off;
2359 the_cu->length = length;
e3b94546 2360 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2361 the_cu->section = section;
2362 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2363 struct dwarf2_per_cu_quick_data);
2364 the_cu->is_dwz = is_dwz;
2365 return the_cu;
2366}
2367
2ec9a5e0
TT
2368/* A helper for create_cus_from_index that handles a given list of
2369 CUs. */
2fdf6df6 2370
74a0d9f6 2371static void
12359b5e 2372create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2373 const gdb_byte *cu_list, offset_type n_elements,
2374 struct dwarf2_section_info *section,
b76e467d 2375 int is_dwz)
9291a0cd 2376{
12359b5e 2377 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2378 {
74a0d9f6 2379 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2380
2381 sect_offset sect_off
2382 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2383 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2384 cu_list += 2 * 8;
2385
b76e467d 2386 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2387 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2388 sect_off, length);
b76e467d 2389 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2390 }
9291a0cd
TT
2391}
2392
2ec9a5e0 2393/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2394 the CU objects for this objfile. */
2ec9a5e0 2395
74a0d9f6 2396static void
12359b5e 2397create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2398 const gdb_byte *cu_list, offset_type cu_list_elements,
2399 const gdb_byte *dwz_list, offset_type dwz_elements)
2400{
b76e467d
SM
2401 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2402 dwarf2_per_objfile->all_comp_units.reserve
2403 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2404
12359b5e 2405 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2406 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2407
2408 if (dwz_elements == 0)
74a0d9f6 2409 return;
2ec9a5e0 2410
12359b5e
SM
2411 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2412 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2413 &dwz->info, 1);
2ec9a5e0
TT
2414}
2415
1fd400ff 2416/* Create the signatured type hash table from the index. */
673bfd45 2417
74a0d9f6 2418static void
12359b5e
SM
2419create_signatured_type_table_from_index
2420 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2421 struct dwarf2_section_info *section,
2422 const gdb_byte *bytes,
2423 offset_type elements)
1fd400ff 2424{
12359b5e 2425 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2426
b2bdb8cf
SM
2427 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2428 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2429
298e9637 2430 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2431
12359b5e 2432 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2433 {
52dc124a 2434 struct signatured_type *sig_type;
9c541725 2435 ULONGEST signature;
1fd400ff 2436 void **slot;
9c541725 2437 cu_offset type_offset_in_tu;
1fd400ff 2438
74a0d9f6 2439 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2440 sect_offset sect_off
2441 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2442 type_offset_in_tu
2443 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2444 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2445 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2446 bytes += 3 * 8;
2447
52dc124a 2448 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2449 struct signatured_type);
52dc124a 2450 sig_type->signature = signature;
9c541725 2451 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2452 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2453 sig_type->per_cu.section = section;
9c541725 2454 sig_type->per_cu.sect_off = sect_off;
e3b94546 2455 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2456 sig_type->per_cu.v.quick
1fd400ff
TT
2457 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2458 struct dwarf2_per_cu_quick_data);
2459
b0b6a987 2460 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2461 *slot = sig_type;
1fd400ff 2462
b2bdb8cf 2463 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2464 }
2465
b0b6a987 2466 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2467}
2468
927aa2e7
JK
2469/* Create the signatured type hash table from .debug_names. */
2470
2471static void
2472create_signatured_type_table_from_debug_names
ed2dc618 2473 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2474 const mapped_debug_names &map,
2475 struct dwarf2_section_info *section,
2476 struct dwarf2_section_info *abbrev_section)
2477{
ed2dc618
SM
2478 struct objfile *objfile = dwarf2_per_objfile->objfile;
2479
96b79293
TT
2480 section->read (objfile);
2481 abbrev_section->read (objfile);
927aa2e7 2482
b2bdb8cf
SM
2483 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2484 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2485
298e9637 2486 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2487
2488 for (uint32_t i = 0; i < map.tu_count; ++i)
2489 {
2490 struct signatured_type *sig_type;
927aa2e7 2491 void **slot;
927aa2e7
JK
2492
2493 sect_offset sect_off
2494 = (sect_offset) (extract_unsigned_integer
2495 (map.tu_table_reordered + i * map.offset_size,
2496 map.offset_size,
2497 map.dwarf5_byte_order));
2498
2499 comp_unit_head cu_header;
ed2dc618
SM
2500 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2501 abbrev_section,
927aa2e7
JK
2502 section->buffer + to_underlying (sect_off),
2503 rcuh_kind::TYPE);
2504
2505 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2506 struct signatured_type);
2507 sig_type->signature = cu_header.signature;
2508 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2509 sig_type->per_cu.is_debug_types = 1;
2510 sig_type->per_cu.section = section;
2511 sig_type->per_cu.sect_off = sect_off;
e3b94546 2512 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2513 sig_type->per_cu.v.quick
2514 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2515 struct dwarf2_per_cu_quick_data);
2516
b0b6a987 2517 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2518 *slot = sig_type;
2519
b2bdb8cf 2520 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2521 }
2522
b0b6a987 2523 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2524}
2525
9291a0cd
TT
2526/* Read the address map data from the mapped index, and use it to
2527 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2528
9291a0cd 2529static void
ed2dc618
SM
2530create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2531 struct mapped_index *index)
9291a0cd 2532{
ed2dc618 2533 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 2534 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9291a0cd 2535 const gdb_byte *iter, *end;
9291a0cd 2536 struct addrmap *mutable_map;
9291a0cd
TT
2537 CORE_ADDR baseaddr;
2538
8268c778
PA
2539 auto_obstack temp_obstack;
2540
9291a0cd
TT
2541 mutable_map = addrmap_create_mutable (&temp_obstack);
2542
f00a2de2
PA
2543 iter = index->address_table.data ();
2544 end = iter + index->address_table.size ();
9291a0cd 2545
b3b3bada 2546 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2547
2548 while (iter < end)
2549 {
2550 ULONGEST hi, lo, cu_index;
2551 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2552 iter += 8;
2553 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2554 iter += 8;
2555 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2556 iter += 4;
f652bce2 2557
24a55014 2558 if (lo > hi)
f652bce2 2559 {
b98664d3 2560 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2561 hex_string (lo), hex_string (hi));
24a55014 2562 continue;
f652bce2 2563 }
24a55014 2564
b76e467d 2565 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2566 {
b98664d3 2567 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2568 (unsigned) cu_index);
24a55014 2569 continue;
f652bce2 2570 }
24a55014 2571
79748972
TT
2572 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2573 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2574 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2575 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2576 }
2577
d320c2b5 2578 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2579 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2580}
2581
927aa2e7
JK
2582/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2583 populate the objfile's psymtabs_addrmap. */
2584
2585static void
ed2dc618 2586create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2587 struct dwarf2_section_info *section)
2588{
ed2dc618 2589 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
2590 bfd *abfd = objfile->obfd;
2591 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 2592 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2593
2594 auto_obstack temp_obstack;
2595 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2596
2597 std::unordered_map<sect_offset,
2598 dwarf2_per_cu_data *,
2599 gdb::hash_enum<sect_offset>>
2600 debug_info_offset_to_per_cu;
b76e467d 2601 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2602 {
927aa2e7
JK
2603 const auto insertpair
2604 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2605 if (!insertpair.second)
2606 {
2607 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2608 "debug_info_offset %s, ignoring .debug_aranges."),
2609 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2610 return;
2611 }
2612 }
2613
96b79293 2614 section->read (objfile);
927aa2e7
JK
2615
2616 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2617
2618 const gdb_byte *addr = section->buffer;
2619
2620 while (addr < section->buffer + section->size)
2621 {
2622 const gdb_byte *const entry_addr = addr;
2623 unsigned int bytes_read;
2624
2625 const LONGEST entry_length = read_initial_length (abfd, addr,
2626 &bytes_read);
2627 addr += bytes_read;
2628
2629 const gdb_byte *const entry_end = addr + entry_length;
2630 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2631 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2632 if (addr + entry_length > section->buffer + section->size)
2633 {
47e3f474 2634 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2635 "length %s exceeds section length %s, "
2636 "ignoring .debug_aranges."),
47e3f474
TV
2637 objfile_name (objfile),
2638 plongest (entry_addr - section->buffer),
927aa2e7
JK
2639 plongest (bytes_read + entry_length),
2640 pulongest (section->size));
2641 return;
2642 }
2643
2644 /* The version number. */
2645 const uint16_t version = read_2_bytes (abfd, addr);
2646 addr += 2;
2647 if (version != 2)
2648 {
47e3f474 2649 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2650 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2651 objfile_name (objfile),
2652 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2653 return;
2654 }
2655
2656 const uint64_t debug_info_offset
2657 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2658 addr += offset_size;
2659 const auto per_cu_it
2660 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2661 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2662 {
47e3f474 2663 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2664 "debug_info_offset %s does not exists, "
2665 "ignoring .debug_aranges."),
47e3f474
TV
2666 objfile_name (objfile),
2667 plongest (entry_addr - section->buffer),
927aa2e7
JK
2668 pulongest (debug_info_offset));
2669 return;
2670 }
2671 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2672
2673 const uint8_t address_size = *addr++;
2674 if (address_size < 1 || address_size > 8)
2675 {
47e3f474 2676 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2677 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2678 objfile_name (objfile),
2679 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2680 return;
2681 }
2682
2683 const uint8_t segment_selector_size = *addr++;
2684 if (segment_selector_size != 0)
2685 {
47e3f474 2686 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2687 "segment_selector_size %u is not supported, "
2688 "ignoring .debug_aranges."),
47e3f474
TV
2689 objfile_name (objfile),
2690 plongest (entry_addr - section->buffer),
927aa2e7
JK
2691 segment_selector_size);
2692 return;
2693 }
2694
2695 /* Must pad to an alignment boundary that is twice the address
2696 size. It is undocumented by the DWARF standard but GCC does
2697 use it. */
2698 for (size_t padding = ((-(addr - section->buffer))
2699 & (2 * address_size - 1));
2700 padding > 0; padding--)
2701 if (*addr++ != 0)
2702 {
47e3f474 2703 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2704 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2705 objfile_name (objfile),
2706 plongest (entry_addr - section->buffer));
927aa2e7
JK
2707 return;
2708 }
2709
2710 for (;;)
2711 {
2712 if (addr + 2 * address_size > entry_end)
2713 {
47e3f474 2714 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2715 "address list is not properly terminated, "
2716 "ignoring .debug_aranges."),
47e3f474
TV
2717 objfile_name (objfile),
2718 plongest (entry_addr - section->buffer));
927aa2e7
JK
2719 return;
2720 }
2721 ULONGEST start = extract_unsigned_integer (addr, address_size,
2722 dwarf5_byte_order);
2723 addr += address_size;
2724 ULONGEST length = extract_unsigned_integer (addr, address_size,
2725 dwarf5_byte_order);
2726 addr += address_size;
2727 if (start == 0 && length == 0)
2728 break;
2729 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2730 {
2731 /* Symbol was eliminated due to a COMDAT group. */
2732 continue;
2733 }
2734 ULONGEST end = start + length;
79748972
TT
2735 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2736 - baseaddr);
2737 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2738 - baseaddr);
927aa2e7
JK
2739 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2740 }
2741 }
2742
d320c2b5 2743 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2744 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2745}
2746
9291a0cd
TT
2747/* Find a slot in the mapped index INDEX for the object named NAME.
2748 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2749 constant pool and return true. If NAME cannot be found, return
2750 false. */
2fdf6df6 2751
109483d9 2752static bool
9291a0cd
TT
2753find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2754 offset_type **vec_out)
2755{
0cf03b49 2756 offset_type hash;
9291a0cd 2757 offset_type slot, step;
559a7a62 2758 int (*cmp) (const char *, const char *);
9291a0cd 2759
791afaa2 2760 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2761 if (current_language->la_language == language_cplus
45280282
IB
2762 || current_language->la_language == language_fortran
2763 || current_language->la_language == language_d)
0cf03b49
JK
2764 {
2765 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2766 not contain any. */
a8719064 2767
72998fb3 2768 if (strchr (name, '(') != NULL)
0cf03b49 2769 {
109483d9 2770 without_params = cp_remove_params (name);
0cf03b49 2771
72998fb3 2772 if (without_params != NULL)
791afaa2 2773 name = without_params.get ();
0cf03b49
JK
2774 }
2775 }
2776
559a7a62 2777 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2778 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2779 simulate our NAME being searched is also lowercased. */
2780 hash = mapped_index_string_hash ((index->version == 4
2781 && case_sensitivity == case_sensitive_off
2782 ? 5 : index->version),
2783 name);
2784
f00a2de2
PA
2785 slot = hash & (index->symbol_table.size () - 1);
2786 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2787 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2788
2789 for (;;)
2790 {
9291a0cd 2791 const char *str;
f00a2de2
PA
2792
2793 const auto &bucket = index->symbol_table[slot];
2794 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2795 return false;
9291a0cd 2796
f00a2de2 2797 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2798 if (!cmp (name, str))
9291a0cd
TT
2799 {
2800 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2801 + MAYBE_SWAP (bucket.vec));
109483d9 2802 return true;
9291a0cd
TT
2803 }
2804
f00a2de2 2805 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2806 }
2807}
2808
4485a1c1
SM
2809/* A helper function that reads the .gdb_index from BUFFER and fills
2810 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2811 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2812 ok to use deprecated sections.
2813
2814 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2815 out parameters that are filled in with information about the CU and
2816 TU lists in the section.
2817
4485a1c1 2818 Returns true if all went well, false otherwise. */
2fdf6df6 2819
d33bc52e 2820static bool
4485a1c1
SM
2821read_gdb_index_from_buffer (struct objfile *objfile,
2822 const char *filename,
2823 bool deprecated_ok,
2824 gdb::array_view<const gdb_byte> buffer,
2825 struct mapped_index *map,
2826 const gdb_byte **cu_list,
2827 offset_type *cu_list_elements,
2828 const gdb_byte **types_list,
2829 offset_type *types_list_elements)
2830{
2831 const gdb_byte *addr = &buffer[0];
82430852 2832
9291a0cd 2833 /* Version check. */
4485a1c1 2834 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2835 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2836 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2837 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2838 indices. */
831adc1f 2839 if (version < 4)
481860b3
GB
2840 {
2841 static int warning_printed = 0;
2842 if (!warning_printed)
2843 {
2844 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2845 filename);
481860b3
GB
2846 warning_printed = 1;
2847 }
2848 return 0;
2849 }
2850 /* Index version 4 uses a different hash function than index version
2851 5 and later.
2852
2853 Versions earlier than 6 did not emit psymbols for inlined
2854 functions. Using these files will cause GDB not to be able to
2855 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2856 indices unless the user has done
2857 "set use-deprecated-index-sections on". */
2ec9a5e0 2858 if (version < 6 && !deprecated_ok)
481860b3
GB
2859 {
2860 static int warning_printed = 0;
2861 if (!warning_printed)
2862 {
e615022a
DE
2863 warning (_("\
2864Skipping deprecated .gdb_index section in %s.\n\
2865Do \"set use-deprecated-index-sections on\" before the file is read\n\
2866to use the section anyway."),
2ec9a5e0 2867 filename);
481860b3
GB
2868 warning_printed = 1;
2869 }
2870 return 0;
2871 }
796a7ff8 2872 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2873 of the TU (for symbols coming from TUs),
2874 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2875 Plus gold-generated indices can have duplicate entries for global symbols,
2876 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2877 These are just performance bugs, and we can't distinguish gdb-generated
2878 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2879
481860b3 2880 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2881 longer backward compatible. */
796a7ff8 2882 if (version > 8)
594e8718 2883 return 0;
9291a0cd 2884
559a7a62 2885 map->version = version;
9291a0cd 2886
4485a1c1 2887 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2888
4485a1c1 2889 int i = 0;
2ec9a5e0
TT
2890 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2891 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2892 / 8);
1fd400ff
TT
2893 ++i;
2894
2ec9a5e0
TT
2895 *types_list = addr + MAYBE_SWAP (metadata[i]);
2896 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2897 - MAYBE_SWAP (metadata[i]))
2898 / 8);
987d643c 2899 ++i;
1fd400ff 2900
f00a2de2
PA
2901 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2902 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2903 map->address_table
2904 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2905 ++i;
2906
f00a2de2
PA
2907 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2908 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2909 map->symbol_table
2910 = gdb::array_view<mapped_index::symbol_table_slot>
2911 ((mapped_index::symbol_table_slot *) symbol_table,
2912 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2913
f00a2de2 2914 ++i;
f9d83a0b 2915 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2916
2ec9a5e0
TT
2917 return 1;
2918}
2919
4485a1c1
SM
2920/* Callback types for dwarf2_read_gdb_index. */
2921
2922typedef gdb::function_view
2923 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2924 get_gdb_index_contents_ftype;
2925typedef gdb::function_view
2926 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2927 get_gdb_index_contents_dwz_ftype;
2928
927aa2e7 2929/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2930 elements of all the CUs and return 1. Otherwise, return 0. */
2931
2932static int
4485a1c1
SM
2933dwarf2_read_gdb_index
2934 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2935 get_gdb_index_contents_ftype get_gdb_index_contents,
2936 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 2937{
2ec9a5e0
TT
2938 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
2939 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 2940 struct dwz_file *dwz;
12359b5e 2941 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 2942
4485a1c1
SM
2943 gdb::array_view<const gdb_byte> main_index_contents
2944 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
2945
2946 if (main_index_contents.empty ())
2947 return 0;
2948
3063847f 2949 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
4485a1c1
SM
2950 if (!read_gdb_index_from_buffer (objfile, objfile_name (objfile),
2951 use_deprecated_index_sections,
2952 main_index_contents, map.get (), &cu_list,
2953 &cu_list_elements, &types_list,
2954 &types_list_elements))
2ec9a5e0
TT
2955 return 0;
2956
0fefef59 2957 /* Don't use the index if it's empty. */
3063847f 2958 if (map->symbol_table.empty ())
0fefef59
DE
2959 return 0;
2960
2ec9a5e0
TT
2961 /* If there is a .dwz file, read it so we can get its CU list as
2962 well. */
ed2dc618 2963 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 2964 if (dwz != NULL)
2ec9a5e0 2965 {
2ec9a5e0
TT
2966 struct mapped_index dwz_map;
2967 const gdb_byte *dwz_types_ignore;
2968 offset_type dwz_types_elements_ignore;
2969
4485a1c1
SM
2970 gdb::array_view<const gdb_byte> dwz_index_content
2971 = get_gdb_index_contents_dwz (objfile, dwz);
2972
2973 if (dwz_index_content.empty ())
2974 return 0;
2975
2976 if (!read_gdb_index_from_buffer (objfile,
00f93c44
AM
2977 bfd_get_filename (dwz->dwz_bfd.get ()),
2978 1, dwz_index_content, &dwz_map,
4485a1c1
SM
2979 &dwz_list, &dwz_list_elements,
2980 &dwz_types_ignore,
2981 &dwz_types_elements_ignore))
2ec9a5e0
TT
2982 {
2983 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 2984 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
2985 return 0;
2986 }
2987 }
2988
12359b5e
SM
2989 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
2990 dwz_list, dwz_list_elements);
1fd400ff 2991
8b70b953
TT
2992 if (types_list_elements)
2993 {
8b70b953
TT
2994 /* We can only handle a single .debug_types when we have an
2995 index. */
fd5866f6 2996 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
2997 return 0;
2998
fd5866f6 2999 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3000
12359b5e
SM
3001 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3002 types_list, types_list_elements);
8b70b953 3003 }
9291a0cd 3004
3063847f 3005 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3006
3063847f 3007 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3008 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3009 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3010 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3011
3012 return 1;
3013}
3014
dee91e82 3015/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3016
dee91e82
DE
3017static void
3018dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3019 const gdb_byte *info_ptr,
3e225074 3020 struct die_info *comp_unit_die)
9291a0cd 3021{
dee91e82 3022 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3023 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3024 struct dwarf2_per_objfile *dwarf2_per_objfile
3025 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3026 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3027 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3028 struct attribute *attr;
7b9f3c50
DE
3029 void **slot;
3030 struct quick_file_names *qfn;
9291a0cd 3031
0186c6a7
DE
3032 gdb_assert (! this_cu->is_debug_types);
3033
07261596
TT
3034 /* Our callers never want to match partial units -- instead they
3035 will match the enclosing full CU. */
3036 if (comp_unit_die->tag == DW_TAG_partial_unit)
3037 {
3038 this_cu->v.quick->no_file_data = 1;
3039 return;
3040 }
3041
0186c6a7 3042 lh_cu = this_cu;
7b9f3c50 3043 slot = NULL;
dee91e82 3044
fff8551c 3045 line_header_up lh;
9c541725 3046 sect_offset line_offset {};
fff8551c 3047
dee91e82 3048 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3049 if (attr != nullptr)
9291a0cd 3050 {
7b9f3c50
DE
3051 struct quick_file_names find_entry;
3052
9c541725 3053 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3054
3055 /* We may have already read in this line header (TU line header sharing).
3056 If we have we're done. */
094b34ac 3057 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3058 find_entry.hash.line_sect_off = line_offset;
5895093f 3059 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3060 &find_entry, INSERT);
3061 if (*slot != NULL)
3062 {
9a3c8263 3063 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3064 return;
7b9f3c50
DE
3065 }
3066
3019eac3 3067 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3068 }
3069 if (lh == NULL)
3070 {
094b34ac 3071 lh_cu->v.quick->no_file_data = 1;
dee91e82 3072 return;
9291a0cd
TT
3073 }
3074
8d749320 3075 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3076 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3077 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3078 gdb_assert (slot != NULL);
3079 *slot = qfn;
9291a0cd 3080
d721ba37 3081 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3082
aa391654
TT
3083 int offset = 0;
3084 if (strcmp (fnd.name, "<unknown>") != 0)
3085 ++offset;
3086
7ba99d21 3087 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3088 qfn->file_names =
aa391654
TT
3089 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3090 if (offset != 0)
3091 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3092 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3093 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3094 fnd.comp_dir).release ();
7b9f3c50 3095 qfn->real_names = NULL;
9291a0cd 3096
094b34ac 3097 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3098}
3099
3100/* A helper for the "quick" functions which attempts to read the line
3101 table for THIS_CU. */
3102
3103static struct quick_file_names *
e4a48d9d 3104dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3105{
0186c6a7
DE
3106 /* This should never be called for TUs. */
3107 gdb_assert (! this_cu->is_debug_types);
3108 /* Nor type unit groups. */
197400e8 3109 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3110
dee91e82
DE
3111 if (this_cu->v.quick->file_names != NULL)
3112 return this_cu->v.quick->file_names;
3113 /* If we know there is no line data, no point in looking again. */
3114 if (this_cu->v.quick->no_file_data)
3115 return NULL;
3116
c0ab21c2
TT
3117 cutu_reader reader (this_cu);
3118 if (!reader.dummy_p)
3e225074 3119 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3120
3121 if (this_cu->v.quick->no_file_data)
3122 return NULL;
3123 return this_cu->v.quick->file_names;
9291a0cd
TT
3124}
3125
3126/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3127 real path for a given file name from the line table. */
2fdf6df6 3128
9291a0cd 3129static const char *
7b9f3c50
DE
3130dw2_get_real_path (struct objfile *objfile,
3131 struct quick_file_names *qfn, int index)
9291a0cd 3132{
7b9f3c50
DE
3133 if (qfn->real_names == NULL)
3134 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3135 qfn->num_file_names, const char *);
9291a0cd 3136
7b9f3c50 3137 if (qfn->real_names[index] == NULL)
14278e1f 3138 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3139
7b9f3c50 3140 return qfn->real_names[index];
9291a0cd
TT
3141}
3142
3143static struct symtab *
3144dw2_find_last_source_symtab (struct objfile *objfile)
3145{
ed2dc618
SM
3146 struct dwarf2_per_objfile *dwarf2_per_objfile
3147 = get_dwarf2_per_objfile (objfile);
b76e467d 3148 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3149 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3150
43f3e411
DE
3151 if (cust == NULL)
3152 return NULL;
ed2dc618 3153
43f3e411 3154 return compunit_primary_filetab (cust);
9291a0cd
TT
3155}
3156
7b9f3c50
DE
3157/* Traversal function for dw2_forget_cached_source_info. */
3158
3159static int
3160dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3161{
7b9f3c50 3162 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3163
7b9f3c50 3164 if (file_data->real_names)
9291a0cd 3165 {
7b9f3c50 3166 int i;
9291a0cd 3167
7b9f3c50 3168 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3169 {
7b9f3c50
DE
3170 xfree ((void*) file_data->real_names[i]);
3171 file_data->real_names[i] = NULL;
9291a0cd
TT
3172 }
3173 }
7b9f3c50
DE
3174
3175 return 1;
3176}
3177
3178static void
3179dw2_forget_cached_source_info (struct objfile *objfile)
3180{
ed2dc618
SM
3181 struct dwarf2_per_objfile *dwarf2_per_objfile
3182 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3183
5895093f 3184 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3185 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3186}
3187
f8eba3c6
TT
3188/* Helper function for dw2_map_symtabs_matching_filename that expands
3189 the symtabs and calls the iterator. */
3190
3191static int
3192dw2_map_expand_apply (struct objfile *objfile,
3193 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3194 const char *name, const char *real_path,
14bc53a8 3195 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3196{
43f3e411 3197 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3198
3199 /* Don't visit already-expanded CUs. */
43f3e411 3200 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3201 return 0;
3202
3203 /* This may expand more than one symtab, and we want to iterate over
3204 all of them. */
58f0c718 3205 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3206
14bc53a8
PA
3207 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3208 last_made, callback);
f8eba3c6
TT
3209}
3210
3211/* Implementation of the map_symtabs_matching_filename method. */
3212
14bc53a8
PA
3213static bool
3214dw2_map_symtabs_matching_filename
3215 (struct objfile *objfile, const char *name, const char *real_path,
3216 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3217{
c011a4f4 3218 const char *name_basename = lbasename (name);
ed2dc618
SM
3219 struct dwarf2_per_objfile *dwarf2_per_objfile
3220 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3221
848e3e78
DE
3222 /* The rule is CUs specify all the files, including those used by
3223 any TU, so there's no need to scan TUs here. */
f4dc4d17 3224
b76e467d 3225 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3226 {
3d7bb9d9 3227 /* We only need to look at symtabs not already expanded. */
43f3e411 3228 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3229 continue;
3230
b76e467d 3231 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3232 if (file_data == NULL)
9291a0cd
TT
3233 continue;
3234
b76e467d 3235 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3236 {
7b9f3c50 3237 const char *this_name = file_data->file_names[j];
da235a7c 3238 const char *this_real_name;
9291a0cd 3239
af529f8f 3240 if (compare_filenames_for_search (this_name, name))
9291a0cd 3241 {
f5b95b50 3242 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3243 callback))
3244 return true;
288e77a7 3245 continue;
4aac40c8 3246 }
9291a0cd 3247
c011a4f4
DE
3248 /* Before we invoke realpath, which can get expensive when many
3249 files are involved, do a quick comparison of the basenames. */
3250 if (! basenames_may_differ
3251 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3252 continue;
3253
da235a7c
JK
3254 this_real_name = dw2_get_real_path (objfile, file_data, j);
3255 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3256 {
da235a7c 3257 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3258 callback))
3259 return true;
288e77a7 3260 continue;
da235a7c 3261 }
9291a0cd 3262
da235a7c
JK
3263 if (real_path != NULL)
3264 {
af529f8f
JK
3265 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3266 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3267 if (this_real_name != NULL
af529f8f 3268 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3269 {
f5b95b50 3270 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3271 callback))
3272 return true;
288e77a7 3273 continue;
9291a0cd
TT
3274 }
3275 }
3276 }
3277 }
3278
14bc53a8 3279 return false;
9291a0cd
TT
3280}
3281
da51c347
DE
3282/* Struct used to manage iterating over all CUs looking for a symbol. */
3283
3284struct dw2_symtab_iterator
9291a0cd 3285{
ed2dc618
SM
3286 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3287 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3288 /* If set, only look for symbols that match that block. Valid values are
3289 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3290 gdb::optional<block_enum> block_index;
da51c347
DE
3291 /* The kind of symbol we're looking for. */
3292 domain_enum domain;
3293 /* The list of CUs from the index entry of the symbol,
3294 or NULL if not found. */
3295 offset_type *vec;
3296 /* The next element in VEC to look at. */
3297 int next;
3298 /* The number of elements in VEC, or zero if there is no match. */
3299 int length;
8943b874
DE
3300 /* Have we seen a global version of the symbol?
3301 If so we can ignore all further global instances.
3302 This is to work around gold/15646, inefficient gold-generated
3303 indices. */
3304 int global_seen;
da51c347 3305};
9291a0cd 3306
2b79f376 3307/* Initialize the index symtab iterator ITER. */
2fdf6df6 3308
9291a0cd 3309static void
da51c347 3310dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3311 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3312 gdb::optional<block_enum> block_index,
da51c347
DE
3313 domain_enum domain,
3314 const char *name)
3315{
ed2dc618 3316 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3317 iter->block_index = block_index;
3318 iter->domain = domain;
3319 iter->next = 0;
8943b874 3320 iter->global_seen = 0;
da51c347 3321
3063847f 3322 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3323
3324 /* index is NULL if OBJF_READNOW. */
3325 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3326 iter->length = MAYBE_SWAP (*iter->vec);
3327 else
3328 {
3329 iter->vec = NULL;
3330 iter->length = 0;
3331 }
3332}
3333
3334/* Return the next matching CU or NULL if there are no more. */
3335
3336static struct dwarf2_per_cu_data *
3337dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3338{
ed2dc618
SM
3339 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3340
da51c347
DE
3341 for ( ; iter->next < iter->length; ++iter->next)
3342 {
3343 offset_type cu_index_and_attrs =
3344 MAYBE_SWAP (iter->vec[iter->next + 1]);
3345 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3346 gdb_index_symbol_kind symbol_kind =
3347 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3348 /* Only check the symbol attributes if they're present.
3349 Indices prior to version 7 don't record them,
3350 and indices >= 7 may elide them for certain symbols
3351 (gold does this). */
3352 int attrs_valid =
ed2dc618 3353 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3354 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3355
3190f0c6 3356 /* Don't crash on bad data. */
b76e467d 3357 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3358 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3359 {
b98664d3 3360 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3361 " [in module %s]"),
3362 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3363 continue;
3364 }
3365
ff4c9fec 3366 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3367
da51c347 3368 /* Skip if already read in. */
43f3e411 3369 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3370 continue;
3371
8943b874
DE
3372 /* Check static vs global. */
3373 if (attrs_valid)
3374 {
2b79f376
SM
3375 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3376
3377 if (iter->block_index.has_value ())
3378 {
3379 bool want_static = *iter->block_index == STATIC_BLOCK;
3380
3381 if (is_static != want_static)
3382 continue;
3383 }
3384
8943b874
DE
3385 /* Work around gold/15646. */
3386 if (!is_static && iter->global_seen)
3387 continue;
3388 if (!is_static)
3389 iter->global_seen = 1;
3390 }
da51c347
DE
3391
3392 /* Only check the symbol's kind if it has one. */
3393 if (attrs_valid)
3394 {
3395 switch (iter->domain)
3396 {
3397 case VAR_DOMAIN:
3398 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3399 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3400 /* Some types are also in VAR_DOMAIN. */
3401 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3402 continue;
3403 break;
3404 case STRUCT_DOMAIN:
3405 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3406 continue;
3407 break;
3408 case LABEL_DOMAIN:
3409 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3410 continue;
3411 break;
59c35742
AB
3412 case MODULE_DOMAIN:
3413 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3414 continue;
3415 break;
da51c347
DE
3416 default:
3417 break;
3418 }
3419 }
3420
3421 ++iter->next;
3422 return per_cu;
3423 }
3424
3425 return NULL;
3426}
3427
43f3e411 3428static struct compunit_symtab *
c7f839cb 3429dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3430 const char *name, domain_enum domain)
9291a0cd 3431{
43f3e411 3432 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3433 struct dwarf2_per_objfile *dwarf2_per_objfile
3434 = get_dwarf2_per_objfile (objfile);
9291a0cd 3435
b5ec771e
PA
3436 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3437
ed2dc618
SM
3438 struct dw2_symtab_iterator iter;
3439 struct dwarf2_per_cu_data *per_cu;
da51c347 3440
2b79f376 3441 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3442
ed2dc618
SM
3443 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3444 {
3445 struct symbol *sym, *with_opaque = NULL;
58f0c718 3446 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3447 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3448 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3449
ed2dc618
SM
3450 sym = block_find_symbol (block, name, domain,
3451 block_find_non_opaque_type_preferred,
3452 &with_opaque);
b2e2f908 3453
ed2dc618
SM
3454 /* Some caution must be observed with overloaded functions
3455 and methods, since the index will not contain any overload
3456 information (but NAME might contain it). */
da51c347 3457
ed2dc618
SM
3458 if (sym != NULL
3459 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3460 return stab;
3461 if (with_opaque != NULL
3462 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3463 stab_best = stab;
da51c347 3464
ed2dc618 3465 /* Keep looking through other CUs. */
9291a0cd 3466 }
9291a0cd 3467
da51c347 3468 return stab_best;
9291a0cd
TT
3469}
3470
3471static void
3472dw2_print_stats (struct objfile *objfile)
3473{
ed2dc618
SM
3474 struct dwarf2_per_objfile *dwarf2_per_objfile
3475 = get_dwarf2_per_objfile (objfile);
b76e467d 3476 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3477 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3478 int count = 0;
9291a0cd 3479
ed2dc618 3480 for (int i = 0; i < total; ++i)
9291a0cd 3481 {
ff4c9fec 3482 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3483
43f3e411 3484 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3485 ++count;
3486 }
e4a48d9d 3487 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3488 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3489}
3490
779bd270
DE
3491/* This dumps minimal information about the index.
3492 It is called via "mt print objfiles".
3493 One use is to verify .gdb_index has been loaded by the
3494 gdb.dwarf2/gdb-index.exp testcase. */
3495
9291a0cd
TT
3496static void
3497dw2_dump (struct objfile *objfile)
3498{
ed2dc618
SM
3499 struct dwarf2_per_objfile *dwarf2_per_objfile
3500 = get_dwarf2_per_objfile (objfile);
3501
779bd270
DE
3502 gdb_assert (dwarf2_per_objfile->using_index);
3503 printf_filtered (".gdb_index:");
3504 if (dwarf2_per_objfile->index_table != NULL)
3505 {
3506 printf_filtered (" version %d\n",
3507 dwarf2_per_objfile->index_table->version);
3508 }
3509 else
3510 printf_filtered (" faked for \"readnow\"\n");
3511 printf_filtered ("\n");
9291a0cd
TT
3512}
3513
9291a0cd
TT
3514static void
3515dw2_expand_symtabs_for_function (struct objfile *objfile,
3516 const char *func_name)
3517{
ed2dc618
SM
3518 struct dwarf2_per_objfile *dwarf2_per_objfile
3519 = get_dwarf2_per_objfile (objfile);
da51c347 3520
ed2dc618
SM
3521 struct dw2_symtab_iterator iter;
3522 struct dwarf2_per_cu_data *per_cu;
da51c347 3523
2b79f376 3524 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3525
ed2dc618 3526 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3527 dw2_instantiate_symtab (per_cu, false);
da51c347 3528
9291a0cd
TT
3529}
3530
3531static void
3532dw2_expand_all_symtabs (struct objfile *objfile)
3533{
ed2dc618
SM
3534 struct dwarf2_per_objfile *dwarf2_per_objfile
3535 = get_dwarf2_per_objfile (objfile);
b76e467d 3536 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3537 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3538
ed2dc618 3539 for (int i = 0; i < total_units; ++i)
9291a0cd 3540 {
ff4c9fec 3541 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3542
58f0c718
TT
3543 /* We don't want to directly expand a partial CU, because if we
3544 read it with the wrong language, then assertion failures can
3545 be triggered later on. See PR symtab/23010. So, tell
3546 dw2_instantiate_symtab to skip partial CUs -- any important
3547 partial CU will be read via DW_TAG_imported_unit anyway. */
3548 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3549 }
3550}
3551
3552static void
652a8996
JK
3553dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3554 const char *fullname)
9291a0cd 3555{
ed2dc618
SM
3556 struct dwarf2_per_objfile *dwarf2_per_objfile
3557 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3558
3559 /* We don't need to consider type units here.
3560 This is only called for examining code, e.g. expand_line_sal.
3561 There can be an order of magnitude (or more) more type units
3562 than comp units, and we avoid them if we can. */
3563
b76e467d 3564 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3565 {
3d7bb9d9 3566 /* We only need to look at symtabs not already expanded. */
43f3e411 3567 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3568 continue;
3569
b76e467d 3570 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3571 if (file_data == NULL)
9291a0cd
TT
3572 continue;
3573
b76e467d 3574 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3575 {
652a8996
JK
3576 const char *this_fullname = file_data->file_names[j];
3577
3578 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3579 {
58f0c718 3580 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3581 break;
3582 }
3583 }
3584 }
3585}
3586
9291a0cd 3587static void
199b4314
TT
3588dw2_map_matching_symbols
3589 (struct objfile *objfile,
b054970d 3590 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3591 int global,
3592 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3593 symbol_compare_ftype *ordered_compare)
9291a0cd 3594{
1aa98955
TV
3595 /* Used for Ada. */
3596 struct dwarf2_per_objfile *dwarf2_per_objfile
3597 = get_dwarf2_per_objfile (objfile);
3598
3599 if (dwarf2_per_objfile->index_table != nullptr)
3600 {
3601 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3602 here though if the current language is Ada for a non-Ada objfile
3603 using GNU index. As Ada does not look for non-Ada symbols this
3604 function should just return. */
3605 return;
3606 }
3607
3608 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3609 inline psym_map_matching_symbols here, assuming all partial symtabs have
3610 been read in. */
3611 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3612
3613 for (compunit_symtab *cust : objfile->compunits ())
3614 {
3615 const struct block *block;
3616
3617 if (cust == NULL)
3618 continue;
3619 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3620 if (!iterate_over_symbols_terminated (block, name,
3621 domain, callback))
3622 return;
3623 }
9291a0cd
TT
3624}
3625
e1ef7d7a
PA
3626/* Starting from a search name, return the string that finds the upper
3627 bound of all strings that start with SEARCH_NAME in a sorted name
3628 list. Returns the empty string to indicate that the upper bound is
3629 the end of the list. */
3630
3631static std::string
3632make_sort_after_prefix_name (const char *search_name)
3633{
3634 /* When looking to complete "func", we find the upper bound of all
3635 symbols that start with "func" by looking for where we'd insert
3636 the closest string that would follow "func" in lexicographical
3637 order. Usually, that's "func"-with-last-character-incremented,
3638 i.e. "fund". Mind non-ASCII characters, though. Usually those
3639 will be UTF-8 multi-byte sequences, but we can't be certain.
3640 Especially mind the 0xff character, which is a valid character in
3641 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3642 rule out compilers allowing it in identifiers. Note that
3643 conveniently, strcmp/strcasecmp are specified to compare
3644 characters interpreted as unsigned char. So what we do is treat
3645 the whole string as a base 256 number composed of a sequence of
3646 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3647 to 0, and carries 1 to the following more-significant position.
3648 If the very first character in SEARCH_NAME ends up incremented
3649 and carries/overflows, then the upper bound is the end of the
3650 list. The string after the empty string is also the empty
3651 string.
3652
3653 Some examples of this operation:
3654
3655 SEARCH_NAME => "+1" RESULT
3656
3657 "abc" => "abd"
3658 "ab\xff" => "ac"
3659 "\xff" "a" "\xff" => "\xff" "b"
3660 "\xff" => ""
3661 "\xff\xff" => ""
3662 "" => ""
3663
3664 Then, with these symbols for example:
3665
3666 func
3667 func1
3668 fund
3669
3670 completing "func" looks for symbols between "func" and
3671 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3672 which finds "func" and "func1", but not "fund".
3673
3674 And with:
3675
3676 funcÿ (Latin1 'ÿ' [0xff])
3677 funcÿ1
3678 fund
3679
3680 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3681 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3682
3683 And with:
3684
3685 ÿÿ (Latin1 'ÿ' [0xff])
3686 ÿÿ1
3687
3688 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3689 the end of the list.
3690 */
3691 std::string after = search_name;
3692 while (!after.empty () && (unsigned char) after.back () == 0xff)
3693 after.pop_back ();
3694 if (!after.empty ())
3695 after.back () = (unsigned char) after.back () + 1;
3696 return after;
3697}
3698
5c58de74 3699/* See declaration. */
61d96d7e 3700
5c58de74
PA
3701std::pair<std::vector<name_component>::const_iterator,
3702 std::vector<name_component>::const_iterator>
44ed8f3e 3703mapped_index_base::find_name_components_bounds
3b00ef10 3704 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3705{
5c58de74
PA
3706 auto *name_cmp
3707 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3708
3b00ef10 3709 const char *lang_name
e0802d59 3710 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3711
3f563c84
PA
3712 /* Comparison function object for lower_bound that matches against a
3713 given symbol name. */
3714 auto lookup_compare_lower = [&] (const name_component &elem,
3715 const char *name)
3716 {
5c58de74 3717 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3718 const char *elem_name = elem_qualified + elem.name_offset;
3719 return name_cmp (elem_name, name) < 0;
3720 };
3721
3722 /* Comparison function object for upper_bound that matches against a
3723 given symbol name. */
3724 auto lookup_compare_upper = [&] (const char *name,
3725 const name_component &elem)
3726 {
5c58de74 3727 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3728 const char *elem_name = elem_qualified + elem.name_offset;
3729 return name_cmp (name, elem_name) < 0;
3730 };
3731
5c58de74
PA
3732 auto begin = this->name_components.begin ();
3733 auto end = this->name_components.end ();
3f563c84
PA
3734
3735 /* Find the lower bound. */
3736 auto lower = [&] ()
3737 {
3b00ef10 3738 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3739 return begin;
3740 else
3b00ef10 3741 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3742 } ();
3743
3744 /* Find the upper bound. */
3745 auto upper = [&] ()
3746 {
5c58de74 3747 if (lookup_name_without_params.completion_mode ())
3f563c84 3748 {
e1ef7d7a
PA
3749 /* In completion mode, we want UPPER to point past all
3750 symbols names that have the same prefix. I.e., with
3751 these symbols, and completing "func":
3752
3753 function << lower bound
3754 function1
3755 other_function << upper bound
3756
3757 We find the upper bound by looking for the insertion
3758 point of "func"-with-last-character-incremented,
3759 i.e. "fund". */
3b00ef10 3760 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3761 if (after.empty ())
3f563c84 3762 return end;
e6b2f5ef
PA
3763 return std::lower_bound (lower, end, after.c_str (),
3764 lookup_compare_lower);
3f563c84
PA
3765 }
3766 else
3b00ef10 3767 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3768 } ();
3769
5c58de74
PA
3770 return {lower, upper};
3771}
3772
3773/* See declaration. */
3774
3775void
44ed8f3e 3776mapped_index_base::build_name_components ()
5c58de74
PA
3777{
3778 if (!this->name_components.empty ())
3779 return;
3780
3781 this->name_components_casing = case_sensitivity;
3782 auto *name_cmp
3783 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3784
3785 /* The code below only knows how to break apart components of C++
3786 symbol names (and other languages that use '::' as
3b00ef10 3787 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3788 auto count = this->symbol_name_count ();
3789 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3790 {
44ed8f3e 3791 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3792 continue;
3793
3794 const char *name = this->symbol_name_at (idx);
3795
3796 /* Add each name component to the name component table. */
3797 unsigned int previous_len = 0;
3b00ef10
TT
3798
3799 if (strstr (name, "::") != nullptr)
3800 {
3801 for (unsigned int current_len = cp_find_first_component (name);
3802 name[current_len] != '\0';
3803 current_len += cp_find_first_component (name + current_len))
3804 {
3805 gdb_assert (name[current_len] == ':');
3806 this->name_components.push_back ({previous_len, idx});
3807 /* Skip the '::'. */
3808 current_len += 2;
3809 previous_len = current_len;
3810 }
3811 }
3812 else
5c58de74 3813 {
3b00ef10
TT
3814 /* Handle the Ada encoded (aka mangled) form here. */
3815 for (const char *iter = strstr (name, "__");
3816 iter != nullptr;
3817 iter = strstr (iter, "__"))
3818 {
3819 this->name_components.push_back ({previous_len, idx});
3820 iter += 2;
3821 previous_len = iter - name;
3822 }
5c58de74 3823 }
3b00ef10 3824
5c58de74
PA
3825 this->name_components.push_back ({previous_len, idx});
3826 }
3827
3828 /* Sort name_components elements by name. */
3829 auto name_comp_compare = [&] (const name_component &left,
3830 const name_component &right)
3831 {
3832 const char *left_qualified = this->symbol_name_at (left.idx);
3833 const char *right_qualified = this->symbol_name_at (right.idx);
3834
3835 const char *left_name = left_qualified + left.name_offset;
3836 const char *right_name = right_qualified + right.name_offset;
3837
3838 return name_cmp (left_name, right_name) < 0;
3839 };
3840
3841 std::sort (this->name_components.begin (),
3842 this->name_components.end (),
3843 name_comp_compare);
3844}
3845
3846/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3847 mapped_index_base instead of the containing objfile. This is split
3848 to a separate function in order to be able to unit test the
3849 name_components matching using a mock mapped_index_base. For each
5c58de74 3850 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3851 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3852
3853static void
3854dw2_expand_symtabs_matching_symbol
44ed8f3e 3855 (mapped_index_base &index,
5c58de74
PA
3856 const lookup_name_info &lookup_name_in,
3857 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3858 enum search_domain kind,
3b00ef10 3859 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3860{
3861 lookup_name_info lookup_name_without_params
3862 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3863
3864 /* Build the symbol name component sorted vector, if we haven't
3865 yet. */
3866 index.build_name_components ();
3867
3f563c84
PA
3868 /* The same symbol may appear more than once in the range though.
3869 E.g., if we're looking for symbols that complete "w", and we have
3870 a symbol named "w1::w2", we'll find the two name components for
3871 that same symbol in the range. To be sure we only call the
3872 callback once per symbol, we first collect the symbol name
3873 indexes that matched in a temporary vector and ignore
3874 duplicates. */
3875 std::vector<offset_type> matches;
3f563c84 3876
3b00ef10
TT
3877 struct name_and_matcher
3878 {
3879 symbol_name_matcher_ftype *matcher;
3880 const std::string &name;
3881
3882 bool operator== (const name_and_matcher &other) const
3f563c84 3883 {
3b00ef10
TT
3884 return matcher == other.matcher && name == other.name;
3885 }
3886 };
3887
3888 /* A vector holding all the different symbol name matchers, for all
3889 languages. */
3890 std::vector<name_and_matcher> matchers;
3891
3892 for (int i = 0; i < nr_languages; i++)
3893 {
3894 enum language lang_e = (enum language) i;
3895
3896 const language_defn *lang = language_def (lang_e);
3897 symbol_name_matcher_ftype *name_matcher
3898 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3899
3b00ef10
TT
3900 name_and_matcher key {
3901 name_matcher,
3902 lookup_name_without_params.language_lookup_name (lang_e)
3903 };
3904
3905 /* Don't insert the same comparison routine more than once.
3906 Note that we do this linear walk. This is not a problem in
3907 practice because the number of supported languages is
3908 low. */
3909 if (std::find (matchers.begin (), matchers.end (), key)
3910 != matchers.end ())
9291a0cd 3911 continue;
3b00ef10
TT
3912 matchers.push_back (std::move (key));
3913
3914 auto bounds
3915 = index.find_name_components_bounds (lookup_name_without_params,
3916 lang_e);
3917
3918 /* Now for each symbol name in range, check to see if we have a name
3919 match, and if so, call the MATCH_CALLBACK callback. */
3920
3921 for (; bounds.first != bounds.second; ++bounds.first)
3922 {
3923 const char *qualified = index.symbol_name_at (bounds.first->idx);
3924
3925 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3926 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3927 continue;
9291a0cd 3928
3b00ef10
TT
3929 matches.push_back (bounds.first->idx);
3930 }
3f563c84
PA
3931 }
3932
3933 std::sort (matches.begin (), matches.end ());
3934
3935 /* Finally call the callback, once per match. */
3936 ULONGEST prev = -1;
3937 for (offset_type idx : matches)
3938 {
3939 if (prev != idx)
3940 {
3b00ef10
TT
3941 if (!match_callback (idx))
3942 break;
3f563c84
PA
3943 prev = idx;
3944 }
3945 }
3946
3947 /* Above we use a type wider than idx's for 'prev', since 0 and
3948 (offset_type)-1 are both possible values. */
3949 static_assert (sizeof (prev) > sizeof (offset_type), "");
3950}
3951
c62446b1
PA
3952#if GDB_SELF_TEST
3953
3954namespace selftests { namespace dw2_expand_symtabs_matching {
3955
a3c5fafd
PA
3956/* A mock .gdb_index/.debug_names-like name index table, enough to
3957 exercise dw2_expand_symtabs_matching_symbol, which works with the
3958 mapped_index_base interface. Builds an index from the symbol list
3959 passed as parameter to the constructor. */
3960class mock_mapped_index : public mapped_index_base
c62446b1
PA
3961{
3962public:
a3c5fafd
PA
3963 mock_mapped_index (gdb::array_view<const char *> symbols)
3964 : m_symbol_table (symbols)
c62446b1
PA
3965 {}
3966
a3c5fafd 3967 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 3968
a3c5fafd 3969 /* Return the number of names in the symbol table. */
632e107b 3970 size_t symbol_name_count () const override
c62446b1 3971 {
a3c5fafd 3972 return m_symbol_table.size ();
c62446b1
PA
3973 }
3974
a3c5fafd 3975 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 3976 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
3977 {
3978 return m_symbol_table[idx];
3979 }
c62446b1 3980
a3c5fafd
PA
3981private:
3982 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
3983};
3984
3985/* Convenience function that converts a NULL pointer to a "<null>"
3986 string, to pass to print routines. */
3987
3988static const char *
3989string_or_null (const char *str)
3990{
3991 return str != NULL ? str : "<null>";
3992}
3993
3994/* Check if a lookup_name_info built from
3995 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
3996 index. EXPECTED_LIST is the list of expected matches, in expected
3997 matching order. If no match expected, then an empty list is
3998 specified. Returns true on success. On failure prints a warning
3999 indicating the file:line that failed, and returns false. */
4000
4001static bool
4002check_match (const char *file, int line,
4003 mock_mapped_index &mock_index,
4004 const char *name, symbol_name_match_type match_type,
4005 bool completion_mode,
4006 std::initializer_list<const char *> expected_list)
4007{
4008 lookup_name_info lookup_name (name, match_type, completion_mode);
4009
4010 bool matched = true;
4011
4012 auto mismatch = [&] (const char *expected_str,
4013 const char *got)
4014 {
4015 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4016 "expected=\"%s\", got=\"%s\"\n"),
4017 file, line,
4018 (match_type == symbol_name_match_type::FULL
4019 ? "FULL" : "WILD"),
4020 name, string_or_null (expected_str), string_or_null (got));
4021 matched = false;
4022 };
4023
4024 auto expected_it = expected_list.begin ();
4025 auto expected_end = expected_list.end ();
4026
a3c5fafd 4027 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4028 NULL, ALL_DOMAIN,
4029 [&] (offset_type idx)
4030 {
a3c5fafd 4031 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4032 const char *expected_str
4033 = expected_it == expected_end ? NULL : *expected_it++;
4034
4035 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4036 mismatch (expected_str, matched_name);
3b00ef10 4037 return true;
c62446b1
PA
4038 });
4039
4040 const char *expected_str
4041 = expected_it == expected_end ? NULL : *expected_it++;
4042 if (expected_str != NULL)
4043 mismatch (expected_str, NULL);
4044
4045 return matched;
4046}
4047
4048/* The symbols added to the mock mapped_index for testing (in
4049 canonical form). */
4050static const char *test_symbols[] = {
4051 "function",
4052 "std::bar",
4053 "std::zfunction",
4054 "std::zfunction2",
4055 "w1::w2",
4056 "ns::foo<char*>",
4057 "ns::foo<int>",
4058 "ns::foo<long>",
a20714ff
PA
4059 "ns2::tmpl<int>::foo2",
4060 "(anonymous namespace)::A::B::C",
c62446b1 4061
e1ef7d7a
PA
4062 /* These are used to check that the increment-last-char in the
4063 matching algorithm for completion doesn't match "t1_fund" when
4064 completing "t1_func". */
4065 "t1_func",
4066 "t1_func1",
4067 "t1_fund",
4068 "t1_fund1",
4069
4070 /* A UTF-8 name with multi-byte sequences to make sure that
4071 cp-name-parser understands this as a single identifier ("função"
4072 is "function" in PT). */
4073 u8"u8função",
4074
4075 /* \377 (0xff) is Latin1 'ÿ'. */
4076 "yfunc\377",
4077
4078 /* \377 (0xff) is Latin1 'ÿ'. */
4079 "\377",
4080 "\377\377123",
4081
c62446b1
PA
4082 /* A name with all sorts of complications. Starts with "z" to make
4083 it easier for the completion tests below. */
4084#define Z_SYM_NAME \
4085 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4086 "::tuple<(anonymous namespace)::ui*, " \
4087 "std::default_delete<(anonymous namespace)::ui>, void>"
4088
4089 Z_SYM_NAME
4090};
4091
a3c5fafd
PA
4092/* Returns true if the mapped_index_base::find_name_component_bounds
4093 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4094 in completion mode. */
5c58de74
PA
4095
4096static bool
a3c5fafd 4097check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4098 const char *search_name,
4099 gdb::array_view<const char *> expected_syms)
4100{
4101 lookup_name_info lookup_name (search_name,
4102 symbol_name_match_type::FULL, true);
4103
3b00ef10
TT
4104 auto bounds = index.find_name_components_bounds (lookup_name,
4105 language_cplus);
5c58de74
PA
4106
4107 size_t distance = std::distance (bounds.first, bounds.second);
4108 if (distance != expected_syms.size ())
4109 return false;
4110
4111 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4112 {
4113 auto nc_elem = bounds.first + exp_elem;
4114 const char *qualified = index.symbol_name_at (nc_elem->idx);
4115 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4116 return false;
4117 }
4118
4119 return true;
4120}
4121
4122/* Test the lower-level mapped_index::find_name_component_bounds
4123 method. */
4124
c62446b1 4125static void
5c58de74
PA
4126test_mapped_index_find_name_component_bounds ()
4127{
4128 mock_mapped_index mock_index (test_symbols);
4129
a3c5fafd 4130 mock_index.build_name_components ();
5c58de74
PA
4131
4132 /* Test the lower-level mapped_index::find_name_component_bounds
4133 method in completion mode. */
4134 {
4135 static const char *expected_syms[] = {
4136 "t1_func",
4137 "t1_func1",
5c58de74
PA
4138 };
4139
a3c5fafd 4140 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4141 "t1_func", expected_syms));
4142 }
4143
4144 /* Check that the increment-last-char in the name matching algorithm
4145 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4146 {
4147 static const char *expected_syms1[] = {
4148 "\377",
4149 "\377\377123",
4150 };
a3c5fafd 4151 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4152 "\377", expected_syms1));
4153
4154 static const char *expected_syms2[] = {
4155 "\377\377123",
4156 };
a3c5fafd 4157 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4158 "\377\377", expected_syms2));
4159 }
4160}
4161
4162/* Test dw2_expand_symtabs_matching_symbol. */
4163
4164static void
4165test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4166{
4167 mock_mapped_index mock_index (test_symbols);
4168
4169 /* We let all tests run until the end even if some fails, for debug
4170 convenience. */
4171 bool any_mismatch = false;
4172
4173 /* Create the expected symbols list (an initializer_list). Needed
4174 because lists have commas, and we need to pass them to CHECK,
4175 which is a macro. */
4176#define EXPECT(...) { __VA_ARGS__ }
4177
4178 /* Wrapper for check_match that passes down the current
4179 __FILE__/__LINE__. */
4180#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4181 any_mismatch |= !check_match (__FILE__, __LINE__, \
4182 mock_index, \
4183 NAME, MATCH_TYPE, COMPLETION_MODE, \
4184 EXPECTED_LIST)
4185
4186 /* Identity checks. */
4187 for (const char *sym : test_symbols)
4188 {
4189 /* Should be able to match all existing symbols. */
4190 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4191 EXPECT (sym));
4192
4193 /* Should be able to match all existing symbols with
4194 parameters. */
4195 std::string with_params = std::string (sym) + "(int)";
4196 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4197 EXPECT (sym));
4198
4199 /* Should be able to match all existing symbols with
4200 parameters and qualifiers. */
4201 with_params = std::string (sym) + " ( int ) const";
4202 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4203 EXPECT (sym));
4204
4205 /* This should really find sym, but cp-name-parser.y doesn't
4206 know about lvalue/rvalue qualifiers yet. */
4207 with_params = std::string (sym) + " ( int ) &&";
4208 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4209 {});
4210 }
4211
e1ef7d7a
PA
4212 /* Check that the name matching algorithm for completion doesn't get
4213 confused with Latin1 'ÿ' / 0xff. */
4214 {
4215 static const char str[] = "\377";
4216 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4217 EXPECT ("\377", "\377\377123"));
4218 }
4219
4220 /* Check that the increment-last-char in the matching algorithm for
4221 completion doesn't match "t1_fund" when completing "t1_func". */
4222 {
4223 static const char str[] = "t1_func";
4224 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4225 EXPECT ("t1_func", "t1_func1"));
4226 }
4227
c62446b1
PA
4228 /* Check that completion mode works at each prefix of the expected
4229 symbol name. */
4230 {
4231 static const char str[] = "function(int)";
4232 size_t len = strlen (str);
4233 std::string lookup;
4234
4235 for (size_t i = 1; i < len; i++)
4236 {
4237 lookup.assign (str, i);
4238 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4239 EXPECT ("function"));
4240 }
4241 }
4242
4243 /* While "w" is a prefix of both components, the match function
4244 should still only be called once. */
4245 {
4246 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4247 EXPECT ("w1::w2"));
a20714ff
PA
4248 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4249 EXPECT ("w1::w2"));
c62446b1
PA
4250 }
4251
4252 /* Same, with a "complicated" symbol. */
4253 {
4254 static const char str[] = Z_SYM_NAME;
4255 size_t len = strlen (str);
4256 std::string lookup;
4257
4258 for (size_t i = 1; i < len; i++)
4259 {
4260 lookup.assign (str, i);
4261 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4262 EXPECT (Z_SYM_NAME));
4263 }
4264 }
4265
4266 /* In FULL mode, an incomplete symbol doesn't match. */
4267 {
4268 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4269 {});
4270 }
4271
4272 /* A complete symbol with parameters matches any overload, since the
4273 index has no overload info. */
4274 {
4275 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4276 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4277 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4278 EXPECT ("std::zfunction", "std::zfunction2"));
4279 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4280 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4281 }
4282
4283 /* Check that whitespace is ignored appropriately. A symbol with a
4284 template argument list. */
4285 {
4286 static const char expected[] = "ns::foo<int>";
4287 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4288 EXPECT (expected));
a20714ff
PA
4289 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4290 EXPECT (expected));
c62446b1
PA
4291 }
4292
4293 /* Check that whitespace is ignored appropriately. A symbol with a
4294 template argument list that includes a pointer. */
4295 {
4296 static const char expected[] = "ns::foo<char*>";
4297 /* Try both completion and non-completion modes. */
4298 static const bool completion_mode[2] = {false, true};
4299 for (size_t i = 0; i < 2; i++)
4300 {
4301 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4302 completion_mode[i], EXPECT (expected));
a20714ff
PA
4303 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4304 completion_mode[i], EXPECT (expected));
c62446b1
PA
4305
4306 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4307 completion_mode[i], EXPECT (expected));
a20714ff
PA
4308 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4309 completion_mode[i], EXPECT (expected));
c62446b1
PA
4310 }
4311 }
4312
4313 {
4314 /* Check method qualifiers are ignored. */
4315 static const char expected[] = "ns::foo<char*>";
4316 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4317 symbol_name_match_type::FULL, true, EXPECT (expected));
4318 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4319 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4320 CHECK_MATCH ("foo < char * > ( int ) const",
4321 symbol_name_match_type::WILD, true, EXPECT (expected));
4322 CHECK_MATCH ("foo < char * > ( int ) &&",
4323 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4324 }
4325
4326 /* Test lookup names that don't match anything. */
4327 {
a20714ff
PA
4328 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4329 {});
4330
c62446b1
PA
4331 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4332 {});
4333 }
4334
a20714ff
PA
4335 /* Some wild matching tests, exercising "(anonymous namespace)",
4336 which should not be confused with a parameter list. */
4337 {
4338 static const char *syms[] = {
4339 "A::B::C",
4340 "B::C",
4341 "C",
4342 "A :: B :: C ( int )",
4343 "B :: C ( int )",
4344 "C ( int )",
4345 };
4346
4347 for (const char *s : syms)
4348 {
4349 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4350 EXPECT ("(anonymous namespace)::A::B::C"));
4351 }
4352 }
4353
4354 {
4355 static const char expected[] = "ns2::tmpl<int>::foo2";
4356 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4357 EXPECT (expected));
4358 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4359 EXPECT (expected));
4360 }
4361
c62446b1
PA
4362 SELF_CHECK (!any_mismatch);
4363
4364#undef EXPECT
4365#undef CHECK_MATCH
4366}
4367
5c58de74
PA
4368static void
4369run_test ()
4370{
4371 test_mapped_index_find_name_component_bounds ();
4372 test_dw2_expand_symtabs_matching_symbol ();
4373}
4374
c62446b1
PA
4375}} // namespace selftests::dw2_expand_symtabs_matching
4376
4377#endif /* GDB_SELF_TEST */
4378
4b514bc8
JK
4379/* If FILE_MATCHER is NULL or if PER_CU has
4380 dwarf2_per_cu_quick_data::MARK set (see
4381 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4382 EXPANSION_NOTIFY on it. */
4383
4384static void
4385dw2_expand_symtabs_matching_one
4386 (struct dwarf2_per_cu_data *per_cu,
4387 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4388 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4389{
4390 if (file_matcher == NULL || per_cu->v.quick->mark)
4391 {
4392 bool symtab_was_null
4393 = (per_cu->v.quick->compunit_symtab == NULL);
4394
58f0c718 4395 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4396
4397 if (expansion_notify != NULL
4398 && symtab_was_null
4399 && per_cu->v.quick->compunit_symtab != NULL)
4400 expansion_notify (per_cu->v.quick->compunit_symtab);
4401 }
4402}
4403
3f563c84
PA
4404/* Helper for dw2_expand_matching symtabs. Called on each symbol
4405 matched, to expand corresponding CUs that were marked. IDX is the
4406 index of the symbol name that matched. */
4407
4408static void
4409dw2_expand_marked_cus
ed2dc618 4410 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4411 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4412 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4413 search_domain kind)
4414{
3f563c84
PA
4415 offset_type *vec, vec_len, vec_idx;
4416 bool global_seen = false;
ed2dc618 4417 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4418
61920122 4419 vec = (offset_type *) (index.constant_pool
f00a2de2 4420 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4421 vec_len = MAYBE_SWAP (vec[0]);
4422 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4423 {
61920122
PA
4424 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4425 /* This value is only valid for index versions >= 7. */
4426 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4427 gdb_index_symbol_kind symbol_kind =
4428 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4429 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4430 /* Only check the symbol attributes if they're present.
4431 Indices prior to version 7 don't record them,
4432 and indices >= 7 may elide them for certain symbols
4433 (gold does this). */
4434 int attrs_valid =
4435 (index.version >= 7
4436 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4437
4438 /* Work around gold/15646. */
4439 if (attrs_valid)
9291a0cd 4440 {
61920122
PA
4441 if (!is_static && global_seen)
4442 continue;
4443 if (!is_static)
4444 global_seen = true;
4445 }
3190f0c6 4446
61920122
PA
4447 /* Only check the symbol's kind if it has one. */
4448 if (attrs_valid)
4449 {
4450 switch (kind)
8943b874 4451 {
61920122
PA
4452 case VARIABLES_DOMAIN:
4453 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4454 continue;
4455 break;
4456 case FUNCTIONS_DOMAIN:
4457 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4458 continue;
61920122
PA
4459 break;
4460 case TYPES_DOMAIN:
4461 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4462 continue;
4463 break;
59c35742
AB
4464 case MODULES_DOMAIN:
4465 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4466 continue;
4467 break;
61920122
PA
4468 default:
4469 break;
8943b874 4470 }
61920122 4471 }
8943b874 4472
61920122 4473 /* Don't crash on bad data. */
b76e467d 4474 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4475 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4476 {
b98664d3 4477 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4478 " [in module %s]"),
4479 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4480 continue;
4481 }
4482
ff4c9fec 4483 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4484 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4485 expansion_notify);
61920122
PA
4486 }
4487}
4488
4b514bc8
JK
4489/* If FILE_MATCHER is non-NULL, set all the
4490 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4491 that match FILE_MATCHER. */
4492
61920122 4493static void
4b514bc8 4494dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4495 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4496 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4497{
4b514bc8 4498 if (file_matcher == NULL)
61920122
PA
4499 return;
4500
4b514bc8
JK
4501 objfile *const objfile = dwarf2_per_objfile->objfile;
4502
4503 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4504 htab_eq_pointer,
4505 NULL, xcalloc, xfree));
4506 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4507 htab_eq_pointer,
4508 NULL, xcalloc, xfree));
61920122 4509
4b514bc8
JK
4510 /* The rule is CUs specify all the files, including those used by
4511 any TU, so there's no need to scan TUs here. */
61920122 4512
b76e467d 4513 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4514 {
927aa2e7
JK
4515 QUIT;
4516
4517 per_cu->v.quick->mark = 0;
4518
4519 /* We only need to look at symtabs not already expanded. */
4520 if (per_cu->v.quick->compunit_symtab)
4521 continue;
4522
b76e467d 4523 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4524 if (file_data == NULL)
4525 continue;
4526
4527 if (htab_find (visited_not_found.get (), file_data) != NULL)
4528 continue;
4529 else if (htab_find (visited_found.get (), file_data) != NULL)
4530 {
4531 per_cu->v.quick->mark = 1;
4532 continue;
4533 }
4534
b76e467d 4535 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4536 {
4537 const char *this_real_name;
4538
4539 if (file_matcher (file_data->file_names[j], false))
4540 {
4541 per_cu->v.quick->mark = 1;
4542 break;
4543 }
4544
4545 /* Before we invoke realpath, which can get expensive when many
4546 files are involved, do a quick comparison of the basenames. */
4547 if (!basenames_may_differ
4548 && !file_matcher (lbasename (file_data->file_names[j]),
4549 true))
4550 continue;
4551
4552 this_real_name = dw2_get_real_path (objfile, file_data, j);
4553 if (file_matcher (this_real_name, false))
4554 {
4555 per_cu->v.quick->mark = 1;
4556 break;
4557 }
4558 }
4559
b76e467d
SM
4560 void **slot = htab_find_slot (per_cu->v.quick->mark
4561 ? visited_found.get ()
4562 : visited_not_found.get (),
4563 file_data, INSERT);
927aa2e7
JK
4564 *slot = file_data;
4565 }
4566}
4567
4568static void
4569dw2_expand_symtabs_matching
4570 (struct objfile *objfile,
4571 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4572 const lookup_name_info &lookup_name,
4573 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4574 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4575 enum search_domain kind)
4576{
ed2dc618
SM
4577 struct dwarf2_per_objfile *dwarf2_per_objfile
4578 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4579
4580 /* index_table is NULL if OBJF_READNOW. */
4581 if (!dwarf2_per_objfile->index_table)
4582 return;
4583
ed2dc618 4584 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7
JK
4585
4586 mapped_index &index = *dwarf2_per_objfile->index_table;
4587
4588 dw2_expand_symtabs_matching_symbol (index, lookup_name,
4589 symbol_matcher,
4590 kind, [&] (offset_type idx)
4591 {
ed2dc618 4592 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4593 expansion_notify, kind);
3b00ef10 4594 return true;
927aa2e7
JK
4595 });
4596}
4597
4598/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4599 symtab. */
4600
4601static struct compunit_symtab *
4602recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4603 CORE_ADDR pc)
4604{
4605 int i;
4606
4607 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4608 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4609 return cust;
4610
4611 if (cust->includes == NULL)
4612 return NULL;
4613
4614 for (i = 0; cust->includes[i]; ++i)
4615 {
4616 struct compunit_symtab *s = cust->includes[i];
4617
4618 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4619 if (s != NULL)
4620 return s;
4621 }
4622
4623 return NULL;
4624}
4625
4626static struct compunit_symtab *
4627dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4628 struct bound_minimal_symbol msymbol,
4629 CORE_ADDR pc,
4630 struct obj_section *section,
4631 int warn_if_readin)
4632{
4633 struct dwarf2_per_cu_data *data;
4634 struct compunit_symtab *result;
4635
d320c2b5 4636 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4637 return NULL;
4638
b3b3bada 4639 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4640 data = (struct dwarf2_per_cu_data *) addrmap_find
4641 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4642 if (!data)
4643 return NULL;
4644
4645 if (warn_if_readin && data->v.quick->compunit_symtab)
4646 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
4647 paddress (get_objfile_arch (objfile), pc));
4648
4649 result
58f0c718
TT
4650 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4651 false),
927aa2e7
JK
4652 pc);
4653 gdb_assert (result != NULL);
4654 return result;
4655}
4656
4657static void
4658dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4659 void *data, int need_fullname)
4660{
ed2dc618
SM
4661 struct dwarf2_per_objfile *dwarf2_per_objfile
4662 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4663
4664 if (!dwarf2_per_objfile->filenames_cache)
4665 {
4666 dwarf2_per_objfile->filenames_cache.emplace ();
4667
4668 htab_up visited (htab_create_alloc (10,
4669 htab_hash_pointer, htab_eq_pointer,
4670 NULL, xcalloc, xfree));
4671
4672 /* The rule is CUs specify all the files, including those used
4673 by any TU, so there's no need to scan TUs here. We can
4674 ignore file names coming from already-expanded CUs. */
4675
b76e467d 4676 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4677 {
927aa2e7
JK
4678 if (per_cu->v.quick->compunit_symtab)
4679 {
4680 void **slot = htab_find_slot (visited.get (),
4681 per_cu->v.quick->file_names,
4682 INSERT);
4683
4684 *slot = per_cu->v.quick->file_names;
4685 }
4686 }
4687
b76e467d 4688 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4689 {
927aa2e7
JK
4690 /* We only need to look at symtabs not already expanded. */
4691 if (per_cu->v.quick->compunit_symtab)
4692 continue;
4693
b76e467d 4694 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4695 if (file_data == NULL)
4696 continue;
4697
b76e467d 4698 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4699 if (*slot)
4700 {
4701 /* Already visited. */
4702 continue;
4703 }
4704 *slot = file_data;
4705
4706 for (int j = 0; j < file_data->num_file_names; ++j)
4707 {
4708 const char *filename = file_data->file_names[j];
4709 dwarf2_per_objfile->filenames_cache->seen (filename);
4710 }
4711 }
4712 }
4713
4714 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4715 {
4716 gdb::unique_xmalloc_ptr<char> this_real_name;
4717
4718 if (need_fullname)
4719 this_real_name = gdb_realpath (filename);
4720 (*fun) (filename, this_real_name.get (), data);
4721 });
4722}
4723
4724static int
4725dw2_has_symbols (struct objfile *objfile)
4726{
4727 return 1;
4728}
4729
4730const struct quick_symbol_functions dwarf2_gdb_index_functions =
4731{
4732 dw2_has_symbols,
4733 dw2_find_last_source_symtab,
4734 dw2_forget_cached_source_info,
4735 dw2_map_symtabs_matching_filename,
4736 dw2_lookup_symbol,
d3214198 4737 NULL,
927aa2e7
JK
4738 dw2_print_stats,
4739 dw2_dump,
927aa2e7
JK
4740 dw2_expand_symtabs_for_function,
4741 dw2_expand_all_symtabs,
4742 dw2_expand_symtabs_with_fullname,
4743 dw2_map_matching_symbols,
4744 dw2_expand_symtabs_matching,
4745 dw2_find_pc_sect_compunit_symtab,
4746 NULL,
4747 dw2_map_symbol_filenames
4748};
4749
4750/* DWARF-5 debug_names reader. */
4751
4752/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4753static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4754
4755/* A helper function that reads the .debug_names section in SECTION
4756 and fills in MAP. FILENAME is the name of the file containing the
4757 section; it is used for error reporting.
4758
4759 Returns true if all went well, false otherwise. */
4760
4761static bool
4762read_debug_names_from_section (struct objfile *objfile,
4763 const char *filename,
4764 struct dwarf2_section_info *section,
4765 mapped_debug_names &map)
4766{
96b79293 4767 if (section->empty ())
927aa2e7
JK
4768 return false;
4769
4770 /* Older elfutils strip versions could keep the section in the main
4771 executable while splitting it for the separate debug info file. */
96b79293 4772 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4773 return false;
4774
96b79293 4775 section->read (objfile);
927aa2e7
JK
4776
4777 map.dwarf5_byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
4778
4779 const gdb_byte *addr = section->buffer;
4780
96b79293 4781 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4782
4783 unsigned int bytes_read;
4784 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4785 addr += bytes_read;
4786
4787 map.dwarf5_is_dwarf64 = bytes_read != 4;
4788 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4789 if (bytes_read + length != section->size)
4790 {
4791 /* There may be multiple per-CU indices. */
4792 warning (_("Section .debug_names in %s length %s does not match "
4793 "section length %s, ignoring .debug_names."),
4794 filename, plongest (bytes_read + length),
4795 pulongest (section->size));
4796 return false;
4797 }
4798
4799 /* The version number. */
4800 uint16_t version = read_2_bytes (abfd, addr);
4801 addr += 2;
4802 if (version != 5)
4803 {
4804 warning (_("Section .debug_names in %s has unsupported version %d, "
4805 "ignoring .debug_names."),
4806 filename, version);
4807 return false;
4808 }
4809
4810 /* Padding. */
4811 uint16_t padding = read_2_bytes (abfd, addr);
4812 addr += 2;
4813 if (padding != 0)
4814 {
4815 warning (_("Section .debug_names in %s has unsupported padding %d, "
4816 "ignoring .debug_names."),
4817 filename, padding);
4818 return false;
4819 }
4820
4821 /* comp_unit_count - The number of CUs in the CU list. */
4822 map.cu_count = read_4_bytes (abfd, addr);
4823 addr += 4;
4824
4825 /* local_type_unit_count - The number of TUs in the local TU
4826 list. */
4827 map.tu_count = read_4_bytes (abfd, addr);
4828 addr += 4;
4829
4830 /* foreign_type_unit_count - The number of TUs in the foreign TU
4831 list. */
4832 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4833 addr += 4;
4834 if (foreign_tu_count != 0)
4835 {
4836 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4837 "ignoring .debug_names."),
4838 filename, static_cast<unsigned long> (foreign_tu_count));
4839 return false;
4840 }
4841
4842 /* bucket_count - The number of hash buckets in the hash lookup
4843 table. */
4844 map.bucket_count = read_4_bytes (abfd, addr);
4845 addr += 4;
4846
4847 /* name_count - The number of unique names in the index. */
4848 map.name_count = read_4_bytes (abfd, addr);
4849 addr += 4;
4850
4851 /* abbrev_table_size - The size in bytes of the abbreviations
4852 table. */
4853 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4854 addr += 4;
4855
4856 /* augmentation_string_size - The size in bytes of the augmentation
4857 string. This value is rounded up to a multiple of 4. */
4858 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4859 addr += 4;
4860 map.augmentation_is_gdb = ((augmentation_string_size
4861 == sizeof (dwarf5_augmentation))
4862 && memcmp (addr, dwarf5_augmentation,
4863 sizeof (dwarf5_augmentation)) == 0);
4864 augmentation_string_size += (-augmentation_string_size) & 3;
4865 addr += augmentation_string_size;
4866
4867 /* List of CUs */
4868 map.cu_table_reordered = addr;
4869 addr += map.cu_count * map.offset_size;
4870
4871 /* List of Local TUs */
4872 map.tu_table_reordered = addr;
4873 addr += map.tu_count * map.offset_size;
4874
4875 /* Hash Lookup Table */
4876 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4877 addr += map.bucket_count * 4;
4878 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4879 addr += map.name_count * 4;
4880
4881 /* Name Table */
4882 map.name_table_string_offs_reordered = addr;
4883 addr += map.name_count * map.offset_size;
4884 map.name_table_entry_offs_reordered = addr;
4885 addr += map.name_count * map.offset_size;
4886
4887 const gdb_byte *abbrev_table_start = addr;
4888 for (;;)
4889 {
927aa2e7
JK
4890 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4891 addr += bytes_read;
4892 if (index_num == 0)
4893 break;
4894
4895 const auto insertpair
4896 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4897 if (!insertpair.second)
4898 {
4899 warning (_("Section .debug_names in %s has duplicate index %s, "
4900 "ignoring .debug_names."),
4901 filename, pulongest (index_num));
4902 return false;
4903 }
4904 mapped_debug_names::index_val &indexval = insertpair.first->second;
4905 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4906 addr += bytes_read;
4907
4908 for (;;)
4909 {
4910 mapped_debug_names::index_val::attr attr;
4911 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4912 addr += bytes_read;
4913 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4914 addr += bytes_read;
4915 if (attr.form == DW_FORM_implicit_const)
4916 {
4917 attr.implicit_const = read_signed_leb128 (abfd, addr,
4918 &bytes_read);
4919 addr += bytes_read;
4920 }
4921 if (attr.dw_idx == 0 && attr.form == 0)
4922 break;
4923 indexval.attr_vec.push_back (std::move (attr));
4924 }
4925 }
4926 if (addr != abbrev_table_start + abbrev_table_size)
4927 {
4928 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
4929 "of size %s vs. written as %u, ignoring .debug_names."),
4930 filename, plongest (addr - abbrev_table_start),
4931 abbrev_table_size);
927aa2e7
JK
4932 return false;
4933 }
4934 map.entry_pool = addr;
4935
4936 return true;
4937}
4938
4939/* A helper for create_cus_from_debug_names that handles the MAP's CU
4940 list. */
4941
4942static void
ed2dc618 4943create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4944 const mapped_debug_names &map,
4945 dwarf2_section_info &section,
b76e467d 4946 bool is_dwz)
927aa2e7
JK
4947{
4948 sect_offset sect_off_prev;
4949 for (uint32_t i = 0; i <= map.cu_count; ++i)
4950 {
4951 sect_offset sect_off_next;
4952 if (i < map.cu_count)
4953 {
4954 sect_off_next
4955 = (sect_offset) (extract_unsigned_integer
4956 (map.cu_table_reordered + i * map.offset_size,
4957 map.offset_size,
4958 map.dwarf5_byte_order));
4959 }
4960 else
4961 sect_off_next = (sect_offset) section.size;
4962 if (i >= 1)
4963 {
4964 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 4965 dwarf2_per_cu_data *per_cu
ed2dc618 4966 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 4967 sect_off_prev, length);
b76e467d 4968 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
4969 }
4970 sect_off_prev = sect_off_next;
4971 }
4972}
4973
4974/* Read the CU list from the mapped index, and use it to create all
ed2dc618 4975 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
4976
4977static void
ed2dc618 4978create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
4979 const mapped_debug_names &map,
4980 const mapped_debug_names &dwz_map)
4981{
b76e467d
SM
4982 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
4983 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 4984
ed2dc618
SM
4985 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
4986 dwarf2_per_objfile->info,
b76e467d 4987 false /* is_dwz */);
927aa2e7
JK
4988
4989 if (dwz_map.cu_count == 0)
4990 return;
4991
ed2dc618
SM
4992 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4993 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 4994 true /* is_dwz */);
927aa2e7
JK
4995}
4996
4997/* Read .debug_names. If everything went ok, initialize the "quick"
4998 elements of all the CUs and return true. Otherwise, return false. */
4999
5000static bool
ed2dc618 5001dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5002{
22ca247e
TT
5003 std::unique_ptr<mapped_debug_names> map
5004 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5005 mapped_debug_names dwz_map (dwarf2_per_objfile);
5006 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5007
5008 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5009 &dwarf2_per_objfile->debug_names,
22ca247e 5010 *map))
927aa2e7
JK
5011 return false;
5012
5013 /* Don't use the index if it's empty. */
22ca247e 5014 if (map->name_count == 0)
927aa2e7
JK
5015 return false;
5016
5017 /* If there is a .dwz file, read it so we can get its CU list as
5018 well. */
ed2dc618 5019 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5020 if (dwz != NULL)
5021 {
5022 if (!read_debug_names_from_section (objfile,
00f93c44 5023 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5024 &dwz->debug_names, dwz_map))
5025 {
5026 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5027 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5028 return false;
5029 }
5030 }
5031
22ca247e 5032 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5033
22ca247e 5034 if (map->tu_count != 0)
927aa2e7
JK
5035 {
5036 /* We can only handle a single .debug_types when we have an
5037 index. */
fd5866f6 5038 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5039 return false;
5040
fd5866f6 5041 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5042
5043 create_signatured_type_table_from_debug_names
22ca247e 5044 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5045 }
5046
ed2dc618
SM
5047 create_addrmap_from_aranges (dwarf2_per_objfile,
5048 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5049
22ca247e 5050 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5051 dwarf2_per_objfile->using_index = 1;
5052 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5053 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5054
5055 return true;
5056}
5057
927aa2e7
JK
5058/* Type used to manage iterating over all CUs looking for a symbol for
5059 .debug_names. */
5060
5061class dw2_debug_names_iterator
5062{
5063public:
927aa2e7 5064 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5065 gdb::optional<block_enum> block_index,
5066 domain_enum domain,
927aa2e7 5067 const char *name)
2b79f376 5068 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5069 m_addr (find_vec_in_debug_names (map, name))
5070 {}
5071
5072 dw2_debug_names_iterator (const mapped_debug_names &map,
5073 search_domain search, uint32_t namei)
5074 : m_map (map),
5075 m_search (search),
5076 m_addr (find_vec_in_debug_names (map, namei))
5077 {}
5078
3b00ef10
TT
5079 dw2_debug_names_iterator (const mapped_debug_names &map,
5080 block_enum block_index, domain_enum domain,
5081 uint32_t namei)
5082 : m_map (map), m_block_index (block_index), m_domain (domain),
5083 m_addr (find_vec_in_debug_names (map, namei))
5084 {}
5085
927aa2e7
JK
5086 /* Return the next matching CU or NULL if there are no more. */
5087 dwarf2_per_cu_data *next ();
5088
5089private:
5090 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5091 const char *name);
5092 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5093 uint32_t namei);
5094
5095 /* The internalized form of .debug_names. */
5096 const mapped_debug_names &m_map;
5097
2b79f376
SM
5098 /* If set, only look for symbols that match that block. Valid values are
5099 GLOBAL_BLOCK and STATIC_BLOCK. */
5100 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5101
5102 /* The kind of symbol we're looking for. */
5103 const domain_enum m_domain = UNDEF_DOMAIN;
5104 const search_domain m_search = ALL_DOMAIN;
5105
5106 /* The list of CUs from the index entry of the symbol, or NULL if
5107 not found. */
5108 const gdb_byte *m_addr;
5109};
5110
5111const char *
5112mapped_debug_names::namei_to_name (uint32_t namei) const
5113{
5114 const ULONGEST namei_string_offs
5115 = extract_unsigned_integer ((name_table_string_offs_reordered
5116 + namei * offset_size),
5117 offset_size,
5118 dwarf5_byte_order);
4f44ae6c
TT
5119 return read_indirect_string_at_offset (dwarf2_per_objfile,
5120 namei_string_offs);
927aa2e7
JK
5121}
5122
5123/* Find a slot in .debug_names for the object named NAME. If NAME is
5124 found, return pointer to its pool data. If NAME cannot be found,
5125 return NULL. */
5126
5127const gdb_byte *
5128dw2_debug_names_iterator::find_vec_in_debug_names
5129 (const mapped_debug_names &map, const char *name)
5130{
5131 int (*cmp) (const char *, const char *);
5132
54ee4252 5133 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5134 if (current_language->la_language == language_cplus
5135 || current_language->la_language == language_fortran
5136 || current_language->la_language == language_d)
5137 {
5138 /* NAME is already canonical. Drop any qualifiers as
5139 .debug_names does not contain any. */
5140
5141 if (strchr (name, '(') != NULL)
5142 {
54ee4252 5143 without_params = cp_remove_params (name);
927aa2e7 5144 if (without_params != NULL)
54ee4252 5145 name = without_params.get ();
927aa2e7
JK
5146 }
5147 }
5148
5149 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5150
5151 const uint32_t full_hash = dwarf5_djb_hash (name);
5152 uint32_t namei
5153 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5154 (map.bucket_table_reordered
5155 + (full_hash % map.bucket_count)), 4,
5156 map.dwarf5_byte_order);
5157 if (namei == 0)
5158 return NULL;
5159 --namei;
5160 if (namei >= map.name_count)
5161 {
b98664d3 5162 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5163 "[in module %s]"),
5164 namei, map.name_count,
ed2dc618 5165 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5166 return NULL;
5167 }
5168
5169 for (;;)
5170 {
5171 const uint32_t namei_full_hash
5172 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5173 (map.hash_table_reordered + namei), 4,
5174 map.dwarf5_byte_order);
5175 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5176 return NULL;
5177
5178 if (full_hash == namei_full_hash)
5179 {
5180 const char *const namei_string = map.namei_to_name (namei);
5181
5182#if 0 /* An expensive sanity check. */
5183 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5184 {
b98664d3 5185 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5186 "[in module %s]"),
5187 namei, objfile_name (dwarf2_per_objfile->objfile));
5188 return NULL;
5189 }
5190#endif
5191
5192 if (cmp (namei_string, name) == 0)
5193 {
5194 const ULONGEST namei_entry_offs
5195 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5196 + namei * map.offset_size),
5197 map.offset_size, map.dwarf5_byte_order);
5198 return map.entry_pool + namei_entry_offs;
5199 }
5200 }
5201
5202 ++namei;
5203 if (namei >= map.name_count)
5204 return NULL;
5205 }
5206}
5207
5208const gdb_byte *
5209dw2_debug_names_iterator::find_vec_in_debug_names
5210 (const mapped_debug_names &map, uint32_t namei)
5211{
5212 if (namei >= map.name_count)
5213 {
b98664d3 5214 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5215 "[in module %s]"),
5216 namei, map.name_count,
ed2dc618 5217 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5218 return NULL;
5219 }
5220
5221 const ULONGEST namei_entry_offs
5222 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5223 + namei * map.offset_size),
5224 map.offset_size, map.dwarf5_byte_order);
5225 return map.entry_pool + namei_entry_offs;
5226}
5227
5228/* See dw2_debug_names_iterator. */
5229
5230dwarf2_per_cu_data *
5231dw2_debug_names_iterator::next ()
5232{
5233 if (m_addr == NULL)
5234 return NULL;
5235
ed2dc618
SM
5236 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5237 struct objfile *objfile = dwarf2_per_objfile->objfile;
5238 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5239
5240 again:
5241
5242 unsigned int bytes_read;
5243 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5244 m_addr += bytes_read;
5245 if (abbrev == 0)
5246 return NULL;
5247
5248 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5249 if (indexval_it == m_map.abbrev_map.cend ())
5250 {
b98664d3 5251 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5252 "[in module %s]"),
ed2dc618 5253 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5254 return NULL;
5255 }
5256 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5257 enum class symbol_linkage {
5258 unknown,
5259 static_,
5260 extern_,
23c13d42 5261 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5262 dwarf2_per_cu_data *per_cu = NULL;
5263 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5264 {
5265 ULONGEST ull;
5266 switch (attr.form)
5267 {
5268 case DW_FORM_implicit_const:
5269 ull = attr.implicit_const;
5270 break;
5271 case DW_FORM_flag_present:
5272 ull = 1;
5273 break;
5274 case DW_FORM_udata:
5275 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5276 m_addr += bytes_read;
5277 break;
5278 default:
b98664d3 5279 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5280 dwarf_form_name (attr.form),
ed2dc618 5281 objfile_name (objfile));
927aa2e7
JK
5282 return NULL;
5283 }
5284 switch (attr.dw_idx)
5285 {
5286 case DW_IDX_compile_unit:
5287 /* Don't crash on bad data. */
b76e467d 5288 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5289 {
b98664d3 5290 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5291 " [in module %s]"),
5292 pulongest (ull),
5293 objfile_name (dwarf2_per_objfile->objfile));
5294 continue;
5295 }
ff4c9fec 5296 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5297 break;
8af5c486
JK
5298 case DW_IDX_type_unit:
5299 /* Don't crash on bad data. */
b2bdb8cf 5300 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5301 {
b98664d3 5302 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5303 " [in module %s]"),
5304 pulongest (ull),
5305 objfile_name (dwarf2_per_objfile->objfile));
5306 continue;
5307 }
ff4c9fec 5308 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5309 break;
927aa2e7
JK
5310 case DW_IDX_GNU_internal:
5311 if (!m_map.augmentation_is_gdb)
5312 break;
23c13d42 5313 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5314 break;
5315 case DW_IDX_GNU_external:
5316 if (!m_map.augmentation_is_gdb)
5317 break;
23c13d42 5318 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5319 break;
5320 }
5321 }
5322
5323 /* Skip if already read in. */
5324 if (per_cu->v.quick->compunit_symtab)
5325 goto again;
5326
5327 /* Check static vs global. */
23c13d42 5328 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5329 {
2b79f376 5330 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5331 const bool symbol_is_static =
5332 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5333 if (want_static != symbol_is_static)
2b79f376 5334 goto again;
927aa2e7
JK
5335 }
5336
5337 /* Match dw2_symtab_iter_next, symbol_kind
5338 and debug_names::psymbol_tag. */
5339 switch (m_domain)
5340 {
5341 case VAR_DOMAIN:
5342 switch (indexval.dwarf_tag)
5343 {
5344 case DW_TAG_variable:
5345 case DW_TAG_subprogram:
5346 /* Some types are also in VAR_DOMAIN. */
5347 case DW_TAG_typedef:
5348 case DW_TAG_structure_type:
5349 break;
5350 default:
5351 goto again;
5352 }
5353 break;
5354 case STRUCT_DOMAIN:
5355 switch (indexval.dwarf_tag)
5356 {
5357 case DW_TAG_typedef:
5358 case DW_TAG_structure_type:
5359 break;
5360 default:
5361 goto again;
5362 }
5363 break;
5364 case LABEL_DOMAIN:
5365 switch (indexval.dwarf_tag)
5366 {
5367 case 0:
5368 case DW_TAG_variable:
5369 break;
5370 default:
5371 goto again;
5372 }
5373 break;
59c35742
AB
5374 case MODULE_DOMAIN:
5375 switch (indexval.dwarf_tag)
5376 {
5377 case DW_TAG_module:
5378 break;
5379 default:
5380 goto again;
5381 }
5382 break;
927aa2e7
JK
5383 default:
5384 break;
5385 }
5386
5387 /* Match dw2_expand_symtabs_matching, symbol_kind and
5388 debug_names::psymbol_tag. */
5389 switch (m_search)
4b514bc8 5390 {
927aa2e7
JK
5391 case VARIABLES_DOMAIN:
5392 switch (indexval.dwarf_tag)
4b514bc8 5393 {
927aa2e7
JK
5394 case DW_TAG_variable:
5395 break;
5396 default:
5397 goto again;
4b514bc8 5398 }
927aa2e7
JK
5399 break;
5400 case FUNCTIONS_DOMAIN:
5401 switch (indexval.dwarf_tag)
4b514bc8 5402 {
927aa2e7
JK
5403 case DW_TAG_subprogram:
5404 break;
5405 default:
5406 goto again;
4b514bc8 5407 }
927aa2e7
JK
5408 break;
5409 case TYPES_DOMAIN:
5410 switch (indexval.dwarf_tag)
5411 {
5412 case DW_TAG_typedef:
5413 case DW_TAG_structure_type:
5414 break;
5415 default:
5416 goto again;
5417 }
5418 break;
59c35742
AB
5419 case MODULES_DOMAIN:
5420 switch (indexval.dwarf_tag)
5421 {
5422 case DW_TAG_module:
5423 break;
5424 default:
5425 goto again;
5426 }
927aa2e7
JK
5427 default:
5428 break;
4b514bc8 5429 }
927aa2e7
JK
5430
5431 return per_cu;
4b514bc8 5432}
61920122 5433
927aa2e7 5434static struct compunit_symtab *
c7f839cb 5435dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5436 const char *name, domain_enum domain)
4b514bc8 5437{
ed2dc618
SM
5438 struct dwarf2_per_objfile *dwarf2_per_objfile
5439 = get_dwarf2_per_objfile (objfile);
61920122 5440
927aa2e7
JK
5441 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5442 if (!mapp)
61920122 5443 {
927aa2e7
JK
5444 /* index is NULL if OBJF_READNOW. */
5445 return NULL;
5446 }
5447 const auto &map = *mapp;
9291a0cd 5448
2b79f376 5449 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5450
927aa2e7
JK
5451 struct compunit_symtab *stab_best = NULL;
5452 struct dwarf2_per_cu_data *per_cu;
5453 while ((per_cu = iter.next ()) != NULL)
5454 {
5455 struct symbol *sym, *with_opaque = NULL;
58f0c718 5456 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5457 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5458 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5459
927aa2e7
JK
5460 sym = block_find_symbol (block, name, domain,
5461 block_find_non_opaque_type_preferred,
5462 &with_opaque);
9703b513 5463
927aa2e7
JK
5464 /* Some caution must be observed with overloaded functions and
5465 methods, since the index will not contain any overload
5466 information (but NAME might contain it). */
a3ec0bb1 5467
927aa2e7 5468 if (sym != NULL
987012b8 5469 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5470 return stab;
5471 if (with_opaque != NULL
987012b8 5472 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5473 stab_best = stab;
9703b513 5474
927aa2e7 5475 /* Keep looking through other CUs. */
9703b513
TT
5476 }
5477
927aa2e7 5478 return stab_best;
9703b513
TT
5479}
5480
927aa2e7
JK
5481/* This dumps minimal information about .debug_names. It is called
5482 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5483 uses this to verify that .debug_names has been loaded. */
9291a0cd 5484
927aa2e7
JK
5485static void
5486dw2_debug_names_dump (struct objfile *objfile)
5487{
ed2dc618
SM
5488 struct dwarf2_per_objfile *dwarf2_per_objfile
5489 = get_dwarf2_per_objfile (objfile);
5490
927aa2e7
JK
5491 gdb_assert (dwarf2_per_objfile->using_index);
5492 printf_filtered (".debug_names:");
5493 if (dwarf2_per_objfile->debug_names_table)
5494 printf_filtered (" exists\n");
5495 else
5496 printf_filtered (" faked for \"readnow\"\n");
5497 printf_filtered ("\n");
9291a0cd
TT
5498}
5499
9291a0cd 5500static void
927aa2e7
JK
5501dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5502 const char *func_name)
9291a0cd 5503{
ed2dc618
SM
5504 struct dwarf2_per_objfile *dwarf2_per_objfile
5505 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5506
927aa2e7
JK
5507 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5508 if (dwarf2_per_objfile->debug_names_table)
24c79950 5509 {
927aa2e7 5510 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5511
2b79f376 5512 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5513
927aa2e7
JK
5514 struct dwarf2_per_cu_data *per_cu;
5515 while ((per_cu = iter.next ()) != NULL)
58f0c718 5516 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5517 }
5518}
24c79950 5519
3b00ef10
TT
5520static void
5521dw2_debug_names_map_matching_symbols
5522 (struct objfile *objfile,
5523 const lookup_name_info &name, domain_enum domain,
5524 int global,
5525 gdb::function_view<symbol_found_callback_ftype> callback,
5526 symbol_compare_ftype *ordered_compare)
5527{
5528 struct dwarf2_per_objfile *dwarf2_per_objfile
5529 = get_dwarf2_per_objfile (objfile);
5530
5531 /* debug_names_table is NULL if OBJF_READNOW. */
5532 if (!dwarf2_per_objfile->debug_names_table)
5533 return;
5534
5535 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5536 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5537
5538 const char *match_name = name.ada ().lookup_name ().c_str ();
5539 auto matcher = [&] (const char *symname)
5540 {
5541 if (ordered_compare == nullptr)
5542 return true;
5543 return ordered_compare (symname, match_name) == 0;
5544 };
5545
5546 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5547 [&] (offset_type namei)
5548 {
5549 /* The name was matched, now expand corresponding CUs that were
5550 marked. */
5551 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5552
5553 struct dwarf2_per_cu_data *per_cu;
5554 while ((per_cu = iter.next ()) != NULL)
5555 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5556 return true;
5557 });
5558
5559 /* It's a shame we couldn't do this inside the
5560 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5561 that have already been expanded. Instead, this loop matches what
5562 the psymtab code does. */
5563 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5564 {
5565 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5566 if (cust != nullptr)
5567 {
5568 const struct block *block
5569 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5570 if (!iterate_over_symbols_terminated (block, name,
5571 domain, callback))
5572 break;
5573 }
5574 }
5575}
5576
927aa2e7
JK
5577static void
5578dw2_debug_names_expand_symtabs_matching
5579 (struct objfile *objfile,
5580 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
5581 const lookup_name_info &lookup_name,
5582 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5583 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5584 enum search_domain kind)
5585{
ed2dc618
SM
5586 struct dwarf2_per_objfile *dwarf2_per_objfile
5587 = get_dwarf2_per_objfile (objfile);
9291a0cd 5588
927aa2e7
JK
5589 /* debug_names_table is NULL if OBJF_READNOW. */
5590 if (!dwarf2_per_objfile->debug_names_table)
5591 return;
9291a0cd 5592
ed2dc618 5593 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5594
44ed8f3e 5595 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5596
44ed8f3e
PA
5597 dw2_expand_symtabs_matching_symbol (map, lookup_name,
5598 symbol_matcher,
5599 kind, [&] (offset_type namei)
927aa2e7 5600 {
927aa2e7
JK
5601 /* The name was matched, now expand corresponding CUs that were
5602 marked. */
5603 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5604
927aa2e7
JK
5605 struct dwarf2_per_cu_data *per_cu;
5606 while ((per_cu = iter.next ()) != NULL)
5607 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5608 expansion_notify);
3b00ef10 5609 return true;
44ed8f3e 5610 });
9291a0cd
TT
5611}
5612
927aa2e7 5613const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5614{
5615 dw2_has_symbols,
5616 dw2_find_last_source_symtab,
5617 dw2_forget_cached_source_info,
f8eba3c6 5618 dw2_map_symtabs_matching_filename,
927aa2e7 5619 dw2_debug_names_lookup_symbol,
d3214198 5620 NULL,
9291a0cd 5621 dw2_print_stats,
927aa2e7 5622 dw2_debug_names_dump,
927aa2e7 5623 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5624 dw2_expand_all_symtabs,
652a8996 5625 dw2_expand_symtabs_with_fullname,
3b00ef10 5626 dw2_debug_names_map_matching_symbols,
927aa2e7 5627 dw2_debug_names_expand_symtabs_matching,
43f3e411 5628 dw2_find_pc_sect_compunit_symtab,
71a3c369 5629 NULL,
9291a0cd
TT
5630 dw2_map_symbol_filenames
5631};
5632
4485a1c1
SM
5633/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5634 to either a dwarf2_per_objfile or dwz_file object. */
5635
5636template <typename T>
5637static gdb::array_view<const gdb_byte>
5638get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5639{
5640 dwarf2_section_info *section = &section_owner->gdb_index;
5641
96b79293 5642 if (section->empty ())
4485a1c1
SM
5643 return {};
5644
5645 /* Older elfutils strip versions could keep the section in the main
5646 executable while splitting it for the separate debug info file. */
96b79293 5647 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5648 return {};
5649
96b79293 5650 section->read (obj);
4485a1c1 5651
8bebfcda
PA
5652 /* dwarf2_section_info::size is a bfd_size_type, while
5653 gdb::array_view works with size_t. On 32-bit hosts, with
5654 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5655 is 32-bit. So we need an explicit narrowing conversion here.
5656 This is fine, because it's impossible to allocate or mmap an
5657 array/buffer larger than what size_t can represent. */
5658 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5659}
5660
87d6a7aa
SM
5661/* Lookup the index cache for the contents of the index associated to
5662 DWARF2_OBJ. */
5663
5664static gdb::array_view<const gdb_byte>
5665get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5666{
5667 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5668 if (build_id == nullptr)
5669 return {};
5670
5671 return global_index_cache.lookup_gdb_index (build_id,
5672 &dwarf2_obj->index_cache_res);
5673}
5674
5675/* Same as the above, but for DWZ. */
5676
5677static gdb::array_view<const gdb_byte>
5678get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5679{
5680 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5681 if (build_id == nullptr)
5682 return {};
5683
5684 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5685}
5686
3c0aa29a 5687/* See symfile.h. */
9291a0cd 5688
3c0aa29a
PA
5689bool
5690dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5691{
ed2dc618
SM
5692 struct dwarf2_per_objfile *dwarf2_per_objfile
5693 = get_dwarf2_per_objfile (objfile);
5694
9291a0cd
TT
5695 /* If we're about to read full symbols, don't bother with the
5696 indices. In this case we also don't care if some other debug
5697 format is making psymtabs, because they are all about to be
5698 expanded anyway. */
5699 if ((objfile->flags & OBJF_READNOW))
5700 {
9291a0cd 5701 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5702 create_all_comp_units (dwarf2_per_objfile);
5703 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5704 dwarf2_per_objfile->quick_file_names_table
5705 = create_quick_file_names_table
5706 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5707
b76e467d 5708 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5709 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5710 {
ff4c9fec 5711 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5712
e254ef6a
DE
5713 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5714 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5715 }
5716
5717 /* Return 1 so that gdb sees the "quick" functions. However,
5718 these functions will be no-ops because we will have expanded
5719 all symtabs. */
3c0aa29a
PA
5720 *index_kind = dw_index_kind::GDB_INDEX;
5721 return true;
9291a0cd
TT
5722 }
5723
ed2dc618 5724 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5725 {
5726 *index_kind = dw_index_kind::DEBUG_NAMES;
5727 return true;
5728 }
927aa2e7 5729
4485a1c1
SM
5730 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5731 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5732 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5733 {
5734 *index_kind = dw_index_kind::GDB_INDEX;
5735 return true;
5736 }
9291a0cd 5737
87d6a7aa
SM
5738 /* ... otherwise, try to find the index in the index cache. */
5739 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5740 get_gdb_index_contents_from_cache,
5741 get_gdb_index_contents_from_cache_dwz))
5742 {
5743 global_index_cache.hit ();
5744 *index_kind = dw_index_kind::GDB_INDEX;
5745 return true;
5746 }
5747
5748 global_index_cache.miss ();
3c0aa29a 5749 return false;
9291a0cd
TT
5750}
5751
5752\f
5753
dce234bc
PP
5754/* Build a partial symbol table. */
5755
5756void
f29dff0a 5757dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5758{
ed2dc618
SM
5759 struct dwarf2_per_objfile *dwarf2_per_objfile
5760 = get_dwarf2_per_objfile (objfile);
c9bf0622 5761
6eee24ce 5762 init_psymbol_list (objfile, 1024);
c906108c 5763
a70b8144 5764 try
c9bf0622
TT
5765 {
5766 /* This isn't really ideal: all the data we allocate on the
5767 objfile's obstack is still uselessly kept around. However,
5768 freeing it seems unsafe. */
906768f9 5769 psymtab_discarder psymtabs (objfile);
ed2dc618 5770 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5771 psymtabs.keep ();
87d6a7aa
SM
5772
5773 /* (maybe) store an index in the cache. */
5774 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5775 }
230d2906 5776 catch (const gdb_exception_error &except)
492d29ea
PA
5777 {
5778 exception_print (gdb_stderr, except);
5779 }
c906108c 5780}
c906108c 5781
3b80fe9b
DE
5782/* Find the base address of the compilation unit for range lists and
5783 location lists. It will normally be specified by DW_AT_low_pc.
5784 In DWARF-3 draft 4, the base address could be overridden by
5785 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5786 compilation units with discontinuous ranges. */
5787
5788static void
5789dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5790{
5791 struct attribute *attr;
5792
2b24b6e4 5793 cu->base_address.reset ();
3b80fe9b
DE
5794
5795 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5796 if (attr != nullptr)
2b24b6e4 5797 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5798 else
5799 {
5800 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5801 if (attr != nullptr)
2b24b6e4 5802 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5803 }
5804}
5805
36586728
TT
5806/* Helper function that returns the proper abbrev section for
5807 THIS_CU. */
5808
5809static struct dwarf2_section_info *
5810get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5811{
5812 struct dwarf2_section_info *abbrev;
ed2dc618 5813 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5814
5815 if (this_cu->is_dwz)
ed2dc618 5816 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5817 else
5818 abbrev = &dwarf2_per_objfile->abbrev;
5819
5820 return abbrev;
5821}
5822
f4dc4d17
DE
5823/* Fetch the abbreviation table offset from a comp or type unit header. */
5824
5825static sect_offset
ed2dc618
SM
5826read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5827 struct dwarf2_section_info *section,
9c541725 5828 sect_offset sect_off)
f4dc4d17 5829{
96b79293 5830 bfd *abfd = section->get_bfd_owner ();
d521ce57 5831 const gdb_byte *info_ptr;
ac298888 5832 unsigned int initial_length_size, offset_size;
43988095 5833 uint16_t version;
f4dc4d17 5834
96b79293 5835 section->read (dwarf2_per_objfile->objfile);
9c541725 5836 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5837 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5838 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5839 info_ptr += initial_length_size;
5840
5841 version = read_2_bytes (abfd, info_ptr);
5842 info_ptr += 2;
5843 if (version >= 5)
5844 {
5845 /* Skip unit type and address size. */
5846 info_ptr += 2;
5847 }
5848
24aa364d 5849 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5850}
5851
b83470bf
TT
5852/* A partial symtab that is used only for include files. */
5853struct dwarf2_include_psymtab : public partial_symtab
5854{
5855 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5856 : partial_symtab (filename, objfile)
5857 {
5858 }
5859
5860 void read_symtab (struct objfile *objfile) override
5861 {
5862 expand_psymtab (objfile);
5863 }
5864
5865 void expand_psymtab (struct objfile *objfile) override
5866 {
5867 if (m_readin)
5868 return;
5869 /* It's an include file, no symbols to read for it.
5870 Everything is in the parent symtab. */
48993951 5871 expand_dependencies (objfile);
b83470bf
TT
5872 m_readin = true;
5873 }
5874
5875 bool readin_p () const override
5876 {
5877 return m_readin;
5878 }
5879
5880 struct compunit_symtab *get_compunit_symtab () const override
5881 {
5882 return nullptr;
5883 }
5884
5885private:
5886
5887 bool m_readin = false;
5888};
5889
aaa75496
JB
5890/* Allocate a new partial symtab for file named NAME and mark this new
5891 partial symtab as being an include of PST. */
5892
5893static void
891813be 5894dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5895 struct objfile *objfile)
5896{
b83470bf 5897 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 5898
fbd9ab74
JK
5899 if (!IS_ABSOLUTE_PATH (subpst->filename))
5900 {
5901 /* It shares objfile->objfile_obstack. */
5902 subpst->dirname = pst->dirname;
5903 }
5904
a9342b62 5905 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
5906 subpst->dependencies[0] = pst;
5907 subpst->number_of_dependencies = 1;
aaa75496
JB
5908}
5909
5910/* Read the Line Number Program data and extract the list of files
5911 included by the source file represented by PST. Build an include
d85a05f0 5912 partial symtab for each of these included files. */
aaa75496
JB
5913
5914static void
5915dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 5916 struct die_info *die,
891813be 5917 dwarf2_psymtab *pst)
aaa75496 5918{
fff8551c 5919 line_header_up lh;
d85a05f0 5920 struct attribute *attr;
aaa75496 5921
d85a05f0 5922 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 5923 if (attr != nullptr)
9c541725 5924 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
5925 if (lh == NULL)
5926 return; /* No linetable, so no includes. */
5927
79748972
TT
5928 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
5929 that we pass in the raw text_low here; that is ok because we're
5930 only decoding the line table to make include partial symtabs, and
5931 so the addresses aren't really used. */
4ae976d1 5932 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 5933 pst->raw_text_low (), 1);
aaa75496
JB
5934}
5935
348e048f 5936static hashval_t
52dc124a 5937hash_signatured_type (const void *item)
348e048f 5938{
9a3c8263
SM
5939 const struct signatured_type *sig_type
5940 = (const struct signatured_type *) item;
9a619af0 5941
348e048f 5942 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 5943 return sig_type->signature;
348e048f
DE
5944}
5945
5946static int
52dc124a 5947eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 5948{
9a3c8263
SM
5949 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
5950 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 5951
348e048f
DE
5952 return lhs->signature == rhs->signature;
5953}
5954
1fd400ff
TT
5955/* Allocate a hash table for signatured types. */
5956
b0b6a987 5957static htab_up
298e9637 5958allocate_signatured_type_table ()
1fd400ff 5959{
b0b6a987
TT
5960 return htab_up (htab_create_alloc (41,
5961 hash_signatured_type,
5962 eq_signatured_type,
5963 NULL, xcalloc, xfree));
1fd400ff
TT
5964}
5965
d467dd73 5966/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
5967
5968static int
d467dd73 5969add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 5970{
9a3c8263 5971 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
5972 std::vector<signatured_type *> *all_type_units
5973 = (std::vector<signatured_type *> *) datum;
1fd400ff 5974
b2bdb8cf 5975 all_type_units->push_back (sigt);
1fd400ff
TT
5976
5977 return 1;
5978}
5979
78d4d2c5 5980/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
5981 and fill them into TYPES_HTAB. It will process only type units,
5982 therefore DW_UT_type. */
c88ee1f0 5983
78d4d2c5 5984static void
ed2dc618
SM
5985create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
5986 struct dwo_file *dwo_file,
b0b6a987 5987 dwarf2_section_info *section, htab_up &types_htab,
43988095 5988 rcuh_kind section_kind)
348e048f 5989{
3019eac3 5990 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 5991 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
5992 bfd *abfd;
5993 const gdb_byte *info_ptr, *end_ptr;
348e048f 5994
4bdcc0c1
DE
5995 abbrev_section = (dwo_file != NULL
5996 ? &dwo_file->sections.abbrev
5997 : &dwarf2_per_objfile->abbrev);
5998
b4f54984 5999 if (dwarf_read_debug)
43988095 6000 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6001 section->get_name (),
6002 abbrev_section->get_file_name ());
09406207 6003
96b79293 6004 section->read (objfile);
78d4d2c5 6005 info_ptr = section->buffer;
348e048f 6006
78d4d2c5
JK
6007 if (info_ptr == NULL)
6008 return;
348e048f 6009
78d4d2c5
JK
6010 /* We can't set abfd until now because the section may be empty or
6011 not present, in which case the bfd is unknown. */
96b79293 6012 abfd = section->get_bfd_owner ();
348e048f 6013
c0ab21c2
TT
6014 /* We don't use cutu_reader here because we don't need to read
6015 any dies: the signature is in the header. */
3019eac3 6016
78d4d2c5
JK
6017 end_ptr = info_ptr + section->size;
6018 while (info_ptr < end_ptr)
6019 {
78d4d2c5
JK
6020 struct signatured_type *sig_type;
6021 struct dwo_unit *dwo_tu;
6022 void **slot;
6023 const gdb_byte *ptr = info_ptr;
6024 struct comp_unit_head header;
6025 unsigned int length;
8b70b953 6026
9c541725 6027 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6028
a49dd8dd
JK
6029 /* Initialize it due to a false compiler warning. */
6030 header.signature = -1;
9c541725 6031 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6032
78d4d2c5
JK
6033 /* We need to read the type's signature in order to build the hash
6034 table, but we don't need anything else just yet. */
348e048f 6035
ed2dc618 6036 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6037 abbrev_section, ptr, section_kind);
348e048f 6038
4057dfde 6039 length = header.get_length ();
6caca83c 6040
78d4d2c5
JK
6041 /* Skip dummy type units. */
6042 if (ptr >= info_ptr + length
43988095
JK
6043 || peek_abbrev_code (abfd, ptr) == 0
6044 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6045 {
6046 info_ptr += length;
6047 continue;
6048 }
dee91e82 6049
78d4d2c5
JK
6050 if (types_htab == NULL)
6051 {
6052 if (dwo_file)
298e9637 6053 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6054 else
298e9637 6055 types_htab = allocate_signatured_type_table ();
78d4d2c5 6056 }
8b70b953 6057
78d4d2c5
JK
6058 if (dwo_file)
6059 {
6060 sig_type = NULL;
6061 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6062 struct dwo_unit);
6063 dwo_tu->dwo_file = dwo_file;
43988095 6064 dwo_tu->signature = header.signature;
9c541725 6065 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6066 dwo_tu->section = section;
9c541725 6067 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6068 dwo_tu->length = length;
6069 }
6070 else
6071 {
6072 /* N.B.: type_offset is not usable if this type uses a DWO file.
6073 The real type_offset is in the DWO file. */
6074 dwo_tu = NULL;
6075 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6076 struct signatured_type);
43988095 6077 sig_type->signature = header.signature;
9c541725 6078 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6079 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6080 sig_type->per_cu.is_debug_types = 1;
6081 sig_type->per_cu.section = section;
9c541725 6082 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6083 sig_type->per_cu.length = length;
6084 }
6085
b0b6a987 6086 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6087 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6088 INSERT);
6089 gdb_assert (slot != NULL);
6090 if (*slot != NULL)
6091 {
9c541725 6092 sect_offset dup_sect_off;
0349ea22 6093
3019eac3
DE
6094 if (dwo_file)
6095 {
78d4d2c5
JK
6096 const struct dwo_unit *dup_tu
6097 = (const struct dwo_unit *) *slot;
6098
9c541725 6099 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6100 }
6101 else
6102 {
78d4d2c5
JK
6103 const struct signatured_type *dup_tu
6104 = (const struct signatured_type *) *slot;
6105
9c541725 6106 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6107 }
8b70b953 6108
b98664d3 6109 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6110 " the entry at offset %s, signature %s"),
6111 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6112 hex_string (header.signature));
78d4d2c5
JK
6113 }
6114 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6115
78d4d2c5 6116 if (dwarf_read_debug > 1)
9d8780f0
SM
6117 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6118 sect_offset_str (sect_off),
43988095 6119 hex_string (header.signature));
3019eac3 6120
78d4d2c5
JK
6121 info_ptr += length;
6122 }
6123}
3019eac3 6124
78d4d2c5
JK
6125/* Create the hash table of all entries in the .debug_types
6126 (or .debug_types.dwo) section(s).
6127 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6128 otherwise it is NULL.
b3c8eb43 6129
78d4d2c5 6130 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6131
78d4d2c5 6132 Note: This function processes DWO files only, not DWP files. */
348e048f 6133
78d4d2c5 6134static void
ed2dc618
SM
6135create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6136 struct dwo_file *dwo_file,
fd5866f6 6137 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6138 htab_up &types_htab)
78d4d2c5 6139{
fd5866f6
SM
6140 for (dwarf2_section_info &section : type_sections)
6141 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6142 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6143}
6144
6145/* Create the hash table of all entries in the .debug_types section,
6146 and initialize all_type_units.
6147 The result is zero if there is an error (e.g. missing .debug_types section),
6148 otherwise non-zero. */
6149
6150static int
ed2dc618 6151create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6152{
b0b6a987 6153 htab_up types_htab;
3019eac3 6154
ed2dc618
SM
6155 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6156 &dwarf2_per_objfile->info, types_htab,
43988095 6157 rcuh_kind::COMPILE);
ed2dc618
SM
6158 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6159 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6160 if (types_htab == NULL)
6161 {
6162 dwarf2_per_objfile->signatured_types = NULL;
6163 return 0;
6164 }
6165
b0b6a987 6166 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6167
b2bdb8cf 6168 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6169 dwarf2_per_objfile->all_type_units.reserve
6170 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6171
b0b6a987
TT
6172 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6173 add_signatured_type_cu_to_table,
b2bdb8cf 6174 &dwarf2_per_objfile->all_type_units);
1fd400ff 6175
348e048f
DE
6176 return 1;
6177}
6178
6aa5f3a6
DE
6179/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6180 If SLOT is non-NULL, it is the entry to use in the hash table.
6181 Otherwise we find one. */
6182
6183static struct signatured_type *
ed2dc618
SM
6184add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6185 void **slot)
6aa5f3a6
DE
6186{
6187 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6188
b2bdb8cf
SM
6189 if (dwarf2_per_objfile->all_type_units.size ()
6190 == dwarf2_per_objfile->all_type_units.capacity ())
6191 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6192
b2bdb8cf
SM
6193 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6194 struct signatured_type);
6195
6196 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6197 sig_type->signature = sig;
6198 sig_type->per_cu.is_debug_types = 1;
6199 if (dwarf2_per_objfile->using_index)
6200 {
6201 sig_type->per_cu.v.quick =
6202 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6203 struct dwarf2_per_cu_quick_data);
6204 }
6205
6206 if (slot == NULL)
6207 {
b0b6a987 6208 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6209 sig_type, INSERT);
6210 }
6211 gdb_assert (*slot == NULL);
6212 *slot = sig_type;
6213 /* The rest of sig_type must be filled in by the caller. */
6214 return sig_type;
6215}
6216
a2ce51a0
DE
6217/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6218 Fill in SIG_ENTRY with DWO_ENTRY. */
6219
6220static void
ed2dc618 6221fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6222 struct signatured_type *sig_entry,
6223 struct dwo_unit *dwo_entry)
6224{
7ee85ab1 6225 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6226 gdb_assert (! sig_entry->per_cu.queued);
6227 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6228 if (dwarf2_per_objfile->using_index)
6229 {
6230 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6231 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6232 }
6233 else
6234 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6235 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6236 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6237 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6238 gdb_assert (sig_entry->dwo_unit == NULL);
6239
6240 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6241 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6242 sig_entry->per_cu.length = dwo_entry->length;
6243 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6244 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6245 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6246 sig_entry->dwo_unit = dwo_entry;
6247}
6248
6249/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6250 If we haven't read the TU yet, create the signatured_type data structure
6251 for a TU to be read in directly from a DWO file, bypassing the stub.
6252 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6253 using .gdb_index, then when reading a CU we want to stay in the DWO file
6254 containing that CU. Otherwise we could end up reading several other DWO
6255 files (due to comdat folding) to process the transitive closure of all the
6256 mentioned TUs, and that can be slow. The current DWO file will have every
6257 type signature that it needs.
a2ce51a0
DE
6258 We only do this for .gdb_index because in the psymtab case we already have
6259 to read all the DWOs to build the type unit groups. */
6260
6261static struct signatured_type *
6262lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6263{
518817b3
SM
6264 struct dwarf2_per_objfile *dwarf2_per_objfile
6265 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6266 struct dwo_file *dwo_file;
6267 struct dwo_unit find_dwo_entry, *dwo_entry;
6268 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6269 void **slot;
a2ce51a0
DE
6270
6271 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6272
6aa5f3a6
DE
6273 /* If TU skeletons have been removed then we may not have read in any
6274 TUs yet. */
6275 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6276 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6277
6278 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6279 Use the global signatured_types array to do our own comdat-folding
6280 of types. If this is the first time we're reading this TU, and
6281 the TU has an entry in .gdb_index, replace the recorded data from
6282 .gdb_index with this TU. */
a2ce51a0 6283
a2ce51a0 6284 find_sig_entry.signature = sig;
b0b6a987 6285 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6286 &find_sig_entry, INSERT);
9a3c8263 6287 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6288
6289 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6290 read. Don't reassign the global entry to point to this DWO if that's
6291 the case. Also note that if the TU is already being read, it may not
6292 have come from a DWO, the program may be a mix of Fission-compiled
6293 code and non-Fission-compiled code. */
6294
6295 /* Have we already tried to read this TU?
6296 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6297 needn't exist in the global table yet). */
6298 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6299 return sig_entry;
6300
6aa5f3a6
DE
6301 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6302 dwo_unit of the TU itself. */
6303 dwo_file = cu->dwo_unit->dwo_file;
6304
a2ce51a0
DE
6305 /* Ok, this is the first time we're reading this TU. */
6306 if (dwo_file->tus == NULL)
6307 return NULL;
6308 find_dwo_entry.signature = sig;
b0b6a987
TT
6309 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6310 &find_dwo_entry);
a2ce51a0
DE
6311 if (dwo_entry == NULL)
6312 return NULL;
6313
6aa5f3a6
DE
6314 /* If the global table doesn't have an entry for this TU, add one. */
6315 if (sig_entry == NULL)
ed2dc618 6316 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6317
ed2dc618 6318 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6319 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6320 return sig_entry;
6321}
6322
a2ce51a0
DE
6323/* Subroutine of lookup_signatured_type.
6324 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6325 then try the DWP file. If the TU stub (skeleton) has been removed then
6326 it won't be in .gdb_index. */
a2ce51a0
DE
6327
6328static struct signatured_type *
6329lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6330{
518817b3
SM
6331 struct dwarf2_per_objfile *dwarf2_per_objfile
6332 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6333 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6334 struct dwo_unit *dwo_entry;
6335 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6336 void **slot;
a2ce51a0
DE
6337
6338 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6339 gdb_assert (dwp_file != NULL);
6340
6aa5f3a6
DE
6341 /* If TU skeletons have been removed then we may not have read in any
6342 TUs yet. */
6343 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6344 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6345
6aa5f3a6 6346 find_sig_entry.signature = sig;
b0b6a987 6347 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6348 &find_sig_entry, INSERT);
9a3c8263 6349 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6350
6351 /* Have we already tried to read this TU?
6352 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6353 needn't exist in the global table yet). */
6354 if (sig_entry != NULL)
6355 return sig_entry;
6356
a2ce51a0
DE
6357 if (dwp_file->tus == NULL)
6358 return NULL;
ed2dc618 6359 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6360 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6361 if (dwo_entry == NULL)
6362 return NULL;
6363
ed2dc618
SM
6364 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6365 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6366
a2ce51a0
DE
6367 return sig_entry;
6368}
6369
380bca97 6370/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6371 Returns NULL if signature SIG is not present in the table.
6372 It is up to the caller to complain about this. */
348e048f
DE
6373
6374static struct signatured_type *
a2ce51a0 6375lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6376{
518817b3
SM
6377 struct dwarf2_per_objfile *dwarf2_per_objfile
6378 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6379
a2ce51a0
DE
6380 if (cu->dwo_unit
6381 && dwarf2_per_objfile->using_index)
6382 {
6383 /* We're in a DWO/DWP file, and we're using .gdb_index.
6384 These cases require special processing. */
ed2dc618 6385 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6386 return lookup_dwo_signatured_type (cu, sig);
6387 else
6388 return lookup_dwp_signatured_type (cu, sig);
6389 }
6390 else
6391 {
6392 struct signatured_type find_entry, *entry;
348e048f 6393
a2ce51a0
DE
6394 if (dwarf2_per_objfile->signatured_types == NULL)
6395 return NULL;
6396 find_entry.signature = sig;
9a3c8263 6397 entry = ((struct signatured_type *)
b0b6a987
TT
6398 htab_find (dwarf2_per_objfile->signatured_types.get (),
6399 &find_entry));
a2ce51a0
DE
6400 return entry;
6401 }
348e048f 6402}
18a8505e 6403
42e7ad6c 6404/* Low level DIE reading support. */
348e048f 6405
d85a05f0
DJ
6406/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6407
6408static void
6409init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6410 struct dwarf2_cu *cu,
3019eac3 6411 struct dwarf2_section_info *section,
685af9cd
TT
6412 struct dwo_file *dwo_file,
6413 struct abbrev_table *abbrev_table)
d85a05f0 6414{
fceca515 6415 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6416 reader->abfd = section->get_bfd_owner ();
d85a05f0 6417 reader->cu = cu;
3019eac3 6418 reader->dwo_file = dwo_file;
dee91e82
DE
6419 reader->die_section = section;
6420 reader->buffer = section->buffer;
f664829e 6421 reader->buffer_end = section->buffer + section->size;
685af9cd 6422 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6423}
6424
c0ab21c2 6425/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6426 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6427 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6428 already.
6429
6430 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6431 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6432 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6433 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6434 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6435 STUB_COMP_DIR may be non-NULL.
3e225074 6436 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6437 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6438 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6439 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6440 kept around for at least as long as *RESULT_READER.
6441
b0c7bfa9
DE
6442 The result is non-zero if a valid (non-dummy) DIE was found. */
6443
6444static int
6445read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6446 struct dwo_unit *dwo_unit,
b0c7bfa9 6447 struct die_info *stub_comp_unit_die,
a2ce51a0 6448 const char *stub_comp_dir,
b0c7bfa9 6449 struct die_reader_specs *result_reader,
d521ce57 6450 const gdb_byte **result_info_ptr,
b0c7bfa9 6451 struct die_info **result_comp_unit_die,
685af9cd 6452 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6453{
ed2dc618 6454 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6455 struct objfile *objfile = dwarf2_per_objfile->objfile;
6456 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6457 bfd *abfd;
d521ce57 6458 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6459 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6460 int i,num_extra_attrs;
6461 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6462 struct die_info *comp_unit_die;
6463
b0aeadb3
DE
6464 /* At most one of these may be provided. */
6465 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6466
b0c7bfa9
DE
6467 /* These attributes aren't processed until later:
6468 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6469 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6470 referenced later. However, these attributes are found in the stub
6471 which we won't have later. In order to not impose this complication
6472 on the rest of the code, we read them here and copy them to the
6473 DWO CU/TU die. */
b0c7bfa9
DE
6474
6475 stmt_list = NULL;
6476 low_pc = NULL;
6477 high_pc = NULL;
6478 ranges = NULL;
6479 comp_dir = NULL;
6480
6481 if (stub_comp_unit_die != NULL)
6482 {
6483 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6484 DWO file. */
6485 if (! this_cu->is_debug_types)
6486 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6487 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6488 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6489 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6490 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6491
a39fdb41 6492 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6493
18a8505e
AT
6494 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6495 here (if needed). We need the value before we can process
6496 DW_AT_ranges. */
a39fdb41 6497 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6498 }
a2ce51a0
DE
6499 else if (stub_comp_dir != NULL)
6500 {
6501 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6502 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6503 comp_dir->name = DW_AT_comp_dir;
6504 comp_dir->form = DW_FORM_string;
6505 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6506 DW_STRING (comp_dir) = stub_comp_dir;
6507 }
b0c7bfa9
DE
6508
6509 /* Set up for reading the DWO CU/TU. */
6510 cu->dwo_unit = dwo_unit;
685af9cd 6511 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6512 section->read (objfile);
6513 abfd = section->get_bfd_owner ();
9c541725
PA
6514 begin_info_ptr = info_ptr = (section->buffer
6515 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6516 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6517
6518 if (this_cu->is_debug_types)
6519 {
b0c7bfa9
DE
6520 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6521
ed2dc618
SM
6522 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6523 &cu->header, section,
b0c7bfa9 6524 dwo_abbrev_section,
43988095 6525 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6526 /* This is not an assert because it can be caused by bad debug info. */
43988095 6527 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6528 {
6529 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6530 " TU at offset %s [in module %s]"),
a2ce51a0 6531 hex_string (sig_type->signature),
43988095 6532 hex_string (cu->header.signature),
9d8780f0 6533 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6534 bfd_get_filename (abfd));
6535 }
9c541725 6536 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6537 /* For DWOs coming from DWP files, we don't know the CU length
6538 nor the type's offset in the TU until now. */
4057dfde 6539 dwo_unit->length = cu->header.get_length ();
9c541725 6540 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6541
6542 /* Establish the type offset that can be used to lookup the type.
6543 For DWO files, we don't know it until now. */
9c541725
PA
6544 sig_type->type_offset_in_section
6545 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6546 }
6547 else
6548 {
ed2dc618
SM
6549 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6550 &cu->header, section,
b0c7bfa9 6551 dwo_abbrev_section,
43988095 6552 info_ptr, rcuh_kind::COMPILE);
9c541725 6553 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6554 /* For DWOs coming from DWP files, we don't know the CU length
6555 until now. */
4057dfde 6556 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6557 }
6558
685af9cd 6559 *result_dwo_abbrev_table
86de1d91
TT
6560 = abbrev_table::read (objfile, dwo_abbrev_section,
6561 cu->header.abbrev_sect_off);
685af9cd
TT
6562 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6563 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6564
6565 /* Read in the die, but leave space to copy over the attributes
6566 from the stub. This has the benefit of simplifying the rest of
6567 the code - all the work to maintain the illusion of a single
6568 DW_TAG_{compile,type}_unit DIE is done here. */
6569 num_extra_attrs = ((stmt_list != NULL)
6570 + (low_pc != NULL)
6571 + (high_pc != NULL)
6572 + (ranges != NULL)
6573 + (comp_dir != NULL));
6574 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6575 num_extra_attrs);
b0c7bfa9
DE
6576
6577 /* Copy over the attributes from the stub to the DIE we just read in. */
6578 comp_unit_die = *result_comp_unit_die;
6579 i = comp_unit_die->num_attrs;
6580 if (stmt_list != NULL)
6581 comp_unit_die->attrs[i++] = *stmt_list;
6582 if (low_pc != NULL)
6583 comp_unit_die->attrs[i++] = *low_pc;
6584 if (high_pc != NULL)
6585 comp_unit_die->attrs[i++] = *high_pc;
6586 if (ranges != NULL)
6587 comp_unit_die->attrs[i++] = *ranges;
6588 if (comp_dir != NULL)
6589 comp_unit_die->attrs[i++] = *comp_dir;
6590 comp_unit_die->num_attrs += num_extra_attrs;
6591
b4f54984 6592 if (dwarf_die_debug)
bf6af496
DE
6593 {
6594 fprintf_unfiltered (gdb_stdlog,
6595 "Read die from %s@0x%x of %s:\n",
96b79293 6596 section->get_name (),
bf6af496
DE
6597 (unsigned) (begin_info_ptr - section->buffer),
6598 bfd_get_filename (abfd));
b4f54984 6599 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6600 }
6601
b0c7bfa9
DE
6602 /* Skip dummy compilation units. */
6603 if (info_ptr >= begin_info_ptr + dwo_unit->length
6604 || peek_abbrev_code (abfd, info_ptr) == 0)
6605 return 0;
6606
6607 *result_info_ptr = info_ptr;
6608 return 1;
6609}
6610
a084a2a6
AT
6611/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6612 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6613 signature is part of the header. */
6614static gdb::optional<ULONGEST>
6615lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6616{
6617 if (cu->header.version >= 5)
6618 return cu->header.signature;
6619 struct attribute *attr;
6620 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6621 if (attr == nullptr)
6622 return gdb::optional<ULONGEST> ();
6623 return DW_UNSND (attr);
6624}
6625
c0ab21c2 6626/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6627 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6628 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6629
6630static struct dwo_unit *
6631lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6632 struct die_info *comp_unit_die,
6633 const char *dwo_name)
b0c7bfa9
DE
6634{
6635 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6636 struct dwo_unit *dwo_unit;
c0ab21c2 6637 const char *comp_dir;
b0c7bfa9 6638
a2ce51a0
DE
6639 gdb_assert (cu != NULL);
6640
b0c7bfa9 6641 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6642 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6643 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6644
6645 if (this_cu->is_debug_types)
6646 {
6647 struct signatured_type *sig_type;
6648
6649 /* Since this_cu is the first member of struct signatured_type,
6650 we can go from a pointer to one to a pointer to the other. */
6651 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6652 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6653 }
6654 else
6655 {
a084a2a6
AT
6656 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6657 if (!signature.has_value ())
b0c7bfa9
DE
6658 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6659 " [in module %s]"),
e3b94546 6660 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6661 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6662 *signature);
b0c7bfa9
DE
6663 }
6664
b0c7bfa9
DE
6665 return dwo_unit;
6666}
6667
c0ab21c2 6668/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6669 See it for a description of the parameters.
fcd3b13d 6670 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6671
c0ab21c2
TT
6672void
6673cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6674 int use_existing_cu)
a2ce51a0 6675{
a2ce51a0 6676 struct signatured_type *sig_type;
a2ce51a0
DE
6677
6678 /* Verify we can do the following downcast, and that we have the
6679 data we need. */
6680 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6681 sig_type = (struct signatured_type *) this_cu;
6682 gdb_assert (sig_type->dwo_unit != NULL);
6683
6aa5f3a6
DE
6684 if (use_existing_cu && this_cu->cu != NULL)
6685 {
6686 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6687 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6688 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6689 }
6690 else
6691 {
6692 /* If !use_existing_cu, this_cu->cu must be NULL. */
6693 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6694 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6695 }
6696
6697 /* A future optimization, if needed, would be to use an existing
6698 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6699 could share abbrev tables. */
a2ce51a0
DE
6700
6701 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6702 NULL /* stub_comp_unit_die */,
6703 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6704 this, &info_ptr,
3e225074 6705 &comp_unit_die,
c0ab21c2 6706 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6707 {
6708 /* Dummy die. */
c0ab21c2 6709 dummy_p = true;
a2ce51a0 6710 }
a2ce51a0
DE
6711}
6712
fd820528 6713/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6714 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6715
f4dc4d17
DE
6716 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6717 Otherwise the table specified in the comp unit header is read in and used.
6718 This is an optimization for when we already have the abbrev table.
6719
dee91e82 6720 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6721 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6722
c0ab21c2
TT
6723cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6724 struct abbrev_table *abbrev_table,
6751ebae 6725 int use_existing_cu,
c0ab21c2
TT
6726 bool skip_partial)
6727 : die_reader_specs {},
6751ebae 6728 m_this_cu (this_cu)
c906108c 6729{
ed2dc618 6730 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6731 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6732 struct dwarf2_section_info *section = this_cu->section;
96b79293 6733 bfd *abfd = section->get_bfd_owner ();
dee91e82 6734 struct dwarf2_cu *cu;
c0ab21c2 6735 const gdb_byte *begin_info_ptr;
dee91e82 6736 struct signatured_type *sig_type = NULL;
4bdcc0c1 6737 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6738 /* Non-zero if CU currently points to a DWO file and we need to
6739 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6740 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6741 int rereading_dwo_cu = 0;
c906108c 6742
b4f54984 6743 if (dwarf_die_debug)
9d8780f0 6744 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6745 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6746 sect_offset_str (this_cu->sect_off));
09406207 6747
a2ce51a0
DE
6748 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6749 file (instead of going through the stub), short-circuit all of this. */
6750 if (this_cu->reading_dwo_directly)
6751 {
6752 /* Narrow down the scope of possibilities to have to understand. */
6753 gdb_assert (this_cu->is_debug_types);
6754 gdb_assert (abbrev_table == NULL);
6751ebae 6755 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6756 return;
6757 }
6758
dee91e82 6759 /* This is cheap if the section is already read in. */
96b79293 6760 section->read (objfile);
dee91e82 6761
9c541725 6762 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6763
6764 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6765
6766 if (use_existing_cu && this_cu->cu != NULL)
6767 {
6768 cu = this_cu->cu;
42e7ad6c
DE
6769 /* If this CU is from a DWO file we need to start over, we need to
6770 refetch the attributes from the skeleton CU.
6771 This could be optimized by retrieving those attributes from when we
6772 were here the first time: the previous comp_unit_die was stored in
6773 comp_unit_obstack. But there's no data yet that we need this
6774 optimization. */
6775 if (cu->dwo_unit != NULL)
6776 rereading_dwo_cu = 1;
dee91e82
DE
6777 }
6778 else
6779 {
6780 /* If !use_existing_cu, this_cu->cu must be NULL. */
6781 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6782 m_new_cu.reset (new dwarf2_cu (this_cu));
6783 cu = m_new_cu.get ();
42e7ad6c 6784 }
dee91e82 6785
b0c7bfa9 6786 /* Get the header. */
9c541725 6787 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6788 {
6789 /* We already have the header, there's no need to read it in again. */
9c541725 6790 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6791 }
6792 else
6793 {
3019eac3 6794 if (this_cu->is_debug_types)
dee91e82 6795 {
ed2dc618
SM
6796 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6797 &cu->header, section,
4bdcc0c1 6798 abbrev_section, info_ptr,
43988095 6799 rcuh_kind::TYPE);
dee91e82 6800
42e7ad6c
DE
6801 /* Since per_cu is the first member of struct signatured_type,
6802 we can go from a pointer to one to a pointer to the other. */
6803 sig_type = (struct signatured_type *) this_cu;
43988095 6804 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6805 gdb_assert (sig_type->type_offset_in_tu
6806 == cu->header.type_cu_offset_in_tu);
6807 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6808
42e7ad6c
DE
6809 /* LENGTH has not been set yet for type units if we're
6810 using .gdb_index. */
4057dfde 6811 this_cu->length = cu->header.get_length ();
3019eac3
DE
6812
6813 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6814 sig_type->type_offset_in_section =
6815 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6816
6817 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6818 }
6819 else
6820 {
ed2dc618
SM
6821 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6822 &cu->header, section,
4bdcc0c1 6823 abbrev_section,
43988095
JK
6824 info_ptr,
6825 rcuh_kind::COMPILE);
dee91e82 6826
9c541725 6827 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6828 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6829 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6830 }
6831 }
10b3939b 6832
6caca83c 6833 /* Skip dummy compilation units. */
dee91e82 6834 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6835 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6836 {
6837 dummy_p = true;
6838 return;
6839 }
6caca83c 6840
433df2d4
DE
6841 /* If we don't have them yet, read the abbrevs for this compilation unit.
6842 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6843 done. */
f4dc4d17 6844 if (abbrev_table != NULL)
685af9cd
TT
6845 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6846 else
f4dc4d17 6847 {
c0ab21c2 6848 m_abbrev_table_holder
86de1d91
TT
6849 = abbrev_table::read (objfile, abbrev_section,
6850 cu->header.abbrev_sect_off);
c0ab21c2 6851 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6852 }
af703f96 6853
dee91e82 6854 /* Read the top level CU/TU die. */
c0ab21c2 6855 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6856 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6857
58f0c718 6858 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6859 {
6860 dummy_p = true;
6861 return;
6862 }
58f0c718 6863
b0c7bfa9 6864 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6865 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6866 table from the DWO file and pass the ownership over to us. It will be
6867 referenced from READER, so we must make sure to free it after we're done
6868 with READER.
6869
b0c7bfa9
DE
6870 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6871 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6872 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6873 if (dwo_name != nullptr)
3019eac3 6874 {
3019eac3 6875 struct dwo_unit *dwo_unit;
b0c7bfa9 6876 struct die_info *dwo_comp_unit_die;
3019eac3 6877
3e225074 6878 if (comp_unit_die->has_children)
6a506a2d 6879 {
b98664d3 6880 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6881 " has children (offset %s) [in module %s]"),
6882 sect_offset_str (this_cu->sect_off),
6883 bfd_get_filename (abfd));
6a506a2d 6884 }
c0ab21c2 6885 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6886 if (dwo_unit != NULL)
3019eac3 6887 {
6a506a2d 6888 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6889 comp_unit_die, NULL,
c0ab21c2 6890 this, &info_ptr,
3e225074 6891 &dwo_comp_unit_die,
c0ab21c2 6892 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6893 {
6894 /* Dummy die. */
c0ab21c2 6895 dummy_p = true;
6a506a2d
DE
6896 return;
6897 }
6898 comp_unit_die = dwo_comp_unit_die;
6899 }
6900 else
6901 {
6902 /* Yikes, we couldn't find the rest of the DIE, we only have
6903 the stub. A complaint has already been logged. There's
6904 not much more we can do except pass on the stub DIE to
6905 die_reader_func. We don't want to throw an error on bad
6906 debug info. */
3019eac3
DE
6907 }
6908 }
c0ab21c2 6909}
3019eac3 6910
6751ebae
TT
6911void
6912cutu_reader::keep ()
c0ab21c2 6913{
b0c7bfa9 6914 /* Done, clean up. */
6751ebae
TT
6915 gdb_assert (!dummy_p);
6916 if (m_new_cu != NULL)
348e048f 6917 {
c0ab21c2
TT
6918 struct dwarf2_per_objfile *dwarf2_per_objfile
6919 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 6920 /* Link this CU into read_in_chain. */
c0ab21c2
TT
6921 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
6922 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 6923 /* The chain owns it now. */
c0ab21c2 6924 m_new_cu.release ();
348e048f 6925 }
dee91e82
DE
6926}
6927
18a8505e
AT
6928/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
6929 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
6930 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
6931
6932 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 6933 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
6934
6935 We fill in THIS_CU->length.
6936
dee91e82 6937 THIS_CU->cu is always freed when done.
3019eac3 6938 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
6939 to care whether it refers to the "main" CU or the DWO CU.
6940
6941 When parent_cu is passed, it is used to provide a default value for
6942 str_offsets_base and addr_base from the parent. */
dee91e82 6943
c0ab21c2
TT
6944cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6945 struct dwarf2_cu *parent_cu,
6946 struct dwo_file *dwo_file)
6947 : die_reader_specs {},
6948 m_this_cu (this_cu)
dee91e82 6949{
ed2dc618 6950 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6951 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6952 struct dwarf2_section_info *section = this_cu->section;
96b79293 6953 bfd *abfd = section->get_bfd_owner ();
33e80786 6954 struct dwarf2_section_info *abbrev_section;
d521ce57 6955 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 6956
b4f54984 6957 if (dwarf_die_debug)
9d8780f0 6958 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6959 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6960 sect_offset_str (this_cu->sect_off));
09406207 6961
dee91e82
DE
6962 gdb_assert (this_cu->cu == NULL);
6963
33e80786
DE
6964 abbrev_section = (dwo_file != NULL
6965 ? &dwo_file->sections.abbrev
6966 : get_abbrev_section_for_cu (this_cu));
6967
dee91e82 6968 /* This is cheap if the section is already read in. */
96b79293 6969 section->read (objfile);
dee91e82 6970
c0ab21c2 6971 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 6972
9c541725 6973 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 6974 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 6975 &m_new_cu->header, section,
4bdcc0c1 6976 abbrev_section, info_ptr,
43988095
JK
6977 (this_cu->is_debug_types
6978 ? rcuh_kind::TYPE
6979 : rcuh_kind::COMPILE));
dee91e82 6980
18a8505e
AT
6981 if (parent_cu != nullptr)
6982 {
c0ab21c2
TT
6983 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
6984 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 6985 }
4057dfde 6986 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
6987
6988 /* Skip dummy compilation units. */
6989 if (info_ptr >= begin_info_ptr + this_cu->length
6990 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6991 {
6992 dummy_p = true;
6993 return;
6994 }
72bf9492 6995
c0ab21c2 6996 m_abbrev_table_holder
86de1d91
TT
6997 = abbrev_table::read (objfile, abbrev_section,
6998 m_new_cu->header.abbrev_sect_off);
dee91e82 6999
c0ab21c2
TT
7000 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7001 m_abbrev_table_holder.get ());
3e225074 7002 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7003}
7004
0018ea6f
DE
7005\f
7006/* Type Unit Groups.
dee91e82 7007
0018ea6f
DE
7008 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7009 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7010 so that all types coming from the same compilation (.o file) are grouped
7011 together. A future step could be to put the types in the same symtab as
7012 the CU the types ultimately came from. */
ff013f42 7013
f4dc4d17
DE
7014static hashval_t
7015hash_type_unit_group (const void *item)
7016{
9a3c8263
SM
7017 const struct type_unit_group *tu_group
7018 = (const struct type_unit_group *) item;
f4dc4d17 7019
094b34ac 7020 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7021}
348e048f
DE
7022
7023static int
f4dc4d17 7024eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7025{
9a3c8263
SM
7026 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7027 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7028
094b34ac 7029 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7030}
348e048f 7031
f4dc4d17
DE
7032/* Allocate a hash table for type unit groups. */
7033
eaa5fa8b 7034static htab_up
298e9637 7035allocate_type_unit_groups_table ()
f4dc4d17 7036{
eaa5fa8b
TT
7037 return htab_up (htab_create_alloc (3,
7038 hash_type_unit_group,
7039 eq_type_unit_group,
7040 NULL, xcalloc, xfree));
f4dc4d17 7041}
dee91e82 7042
f4dc4d17
DE
7043/* Type units that don't have DW_AT_stmt_list are grouped into their own
7044 partial symtabs. We combine several TUs per psymtab to not let the size
7045 of any one psymtab grow too big. */
7046#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7047#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7048
094b34ac 7049/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7050 Create the type_unit_group object used to hold one or more TUs. */
7051
7052static struct type_unit_group *
094b34ac 7053create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7054{
518817b3
SM
7055 struct dwarf2_per_objfile *dwarf2_per_objfile
7056 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7057 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7058 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7059 struct type_unit_group *tu_group;
f4dc4d17
DE
7060
7061 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7062 struct type_unit_group);
094b34ac 7063 per_cu = &tu_group->per_cu;
518817b3 7064 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7065
094b34ac
DE
7066 if (dwarf2_per_objfile->using_index)
7067 {
7068 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7069 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7070 }
7071 else
7072 {
9c541725 7073 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7074 dwarf2_psymtab *pst;
528e1572 7075 std::string name;
094b34ac
DE
7076
7077 /* Give the symtab a useful name for debug purposes. */
7078 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7079 name = string_printf ("<type_units_%d>",
7080 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7081 else
528e1572 7082 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7083
528e1572 7084 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7085 pst->anonymous = true;
094b34ac 7086 }
f4dc4d17 7087
094b34ac 7088 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7089 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7090
7091 return tu_group;
7092}
7093
094b34ac
DE
7094/* Look up the type_unit_group for type unit CU, and create it if necessary.
7095 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7096
7097static struct type_unit_group *
ff39bb5e 7098get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7099{
518817b3
SM
7100 struct dwarf2_per_objfile *dwarf2_per_objfile
7101 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7102 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7103 struct type_unit_group *tu_group;
7104 void **slot;
7105 unsigned int line_offset;
7106 struct type_unit_group type_unit_group_for_lookup;
7107
7108 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7109 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7110
7111 /* Do we need to create a new group, or can we use an existing one? */
7112
7113 if (stmt_list)
7114 {
7115 line_offset = DW_UNSND (stmt_list);
7116 ++tu_stats->nr_symtab_sharers;
7117 }
7118 else
7119 {
7120 /* Ugh, no stmt_list. Rare, but we have to handle it.
7121 We can do various things here like create one group per TU or
7122 spread them over multiple groups to split up the expansion work.
7123 To avoid worst case scenarios (too many groups or too large groups)
7124 we, umm, group them in bunches. */
7125 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7126 | (tu_stats->nr_stmt_less_type_units
7127 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7128 ++tu_stats->nr_stmt_less_type_units;
7129 }
7130
094b34ac 7131 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7132 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7133 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7134 &type_unit_group_for_lookup, INSERT);
7135 if (*slot != NULL)
7136 {
9a3c8263 7137 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7138 gdb_assert (tu_group != NULL);
7139 }
7140 else
7141 {
9c541725 7142 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7143 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7144 *slot = tu_group;
7145 ++tu_stats->nr_symtabs;
7146 }
7147
7148 return tu_group;
7149}
0018ea6f
DE
7150\f
7151/* Partial symbol tables. */
7152
7153/* Create a psymtab named NAME and assign it to PER_CU.
7154
7155 The caller must fill in the following details:
7156 dirname, textlow, texthigh. */
7157
891813be 7158static dwarf2_psymtab *
0018ea6f
DE
7159create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7160{
e3b94546 7161 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7162 dwarf2_psymtab *pst;
0018ea6f 7163
891813be 7164 pst = new dwarf2_psymtab (name, objfile, 0);
0018ea6f 7165
6d94535f 7166 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7167
7168 /* This is the glue that links PST into GDB's symbol API. */
891813be 7169 pst->per_cu_data = per_cu;
0018ea6f
DE
7170 per_cu->v.psymtab = pst;
7171
7172 return pst;
7173}
7174
c0ab21c2 7175/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7176
7177static void
7178process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7179 const gdb_byte *info_ptr,
0018ea6f 7180 struct die_info *comp_unit_die,
c0ab21c2 7181 enum language pretend_language)
0018ea6f
DE
7182{
7183 struct dwarf2_cu *cu = reader->cu;
518817b3 7184 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 7185 struct gdbarch *gdbarch = get_objfile_arch (objfile);
0018ea6f 7186 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7187 CORE_ADDR baseaddr;
7188 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7189 dwarf2_psymtab *pst;
3a2b436a 7190 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7191 const char *filename;
0018ea6f 7192
0018ea6f
DE
7193 gdb_assert (! per_cu->is_debug_types);
7194
c0ab21c2 7195 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7196
0018ea6f 7197 /* Allocate a new partial symbol table structure. */
2e927613
TV
7198 gdb::unique_xmalloc_ptr<char> debug_filename;
7199 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7200 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7201 if (filename == NULL)
0018ea6f 7202 filename = "";
2e927613
TV
7203 else if (strcmp (filename, artificial) == 0)
7204 {
7205 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7206 sect_offset_str (per_cu->sect_off),
7207 (char *) NULL));
2e927613
TV
7208 filename = debug_filename.get ();
7209 }
0018ea6f
DE
7210
7211 pst = create_partial_symtab (per_cu, filename);
7212
7213 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7214 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7215
b3b3bada 7216 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7217
7218 dwarf2_find_base_address (comp_unit_die, cu);
7219
7220 /* Possibly set the default values of LOWPC and HIGHPC from
7221 `DW_AT_ranges'. */
3a2b436a
JK
7222 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7223 &best_highpc, cu, pst);
7224 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7225 {
7226 CORE_ADDR low
7227 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7228 - baseaddr);
7229 CORE_ADDR high
7230 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7231 - baseaddr - 1);
7232 /* Store the contiguous range if it is not empty; it can be
7233 empty for CUs with no code. */
d320c2b5
TT
7234 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7235 low, high, pst);
79748972 7236 }
0018ea6f
DE
7237
7238 /* Check if comp unit has_children.
7239 If so, read the rest of the partial symbols from this comp unit.
7240 If not, there's no more debug_info for this comp unit. */
3e225074 7241 if (comp_unit_die->has_children)
0018ea6f
DE
7242 {
7243 struct partial_die_info *first_die;
7244 CORE_ADDR lowpc, highpc;
7245
7246 lowpc = ((CORE_ADDR) -1);
7247 highpc = ((CORE_ADDR) 0);
7248
7249 first_die = load_partial_dies (reader, info_ptr, 1);
7250
7251 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7252 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7253
7254 /* If we didn't find a lowpc, set it to highpc to avoid
7255 complaints from `maint check'. */
7256 if (lowpc == ((CORE_ADDR) -1))
7257 lowpc = highpc;
7258
7259 /* If the compilation unit didn't have an explicit address range,
7260 then use the information extracted from its child dies. */
e385593e 7261 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7262 {
7263 best_lowpc = lowpc;
7264 best_highpc = highpc;
7265 }
7266 }
4ae976d1 7267 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7268 best_lowpc + baseaddr)
7269 - baseaddr);
4ae976d1 7270 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7271 best_highpc + baseaddr)
7272 - baseaddr);
0018ea6f 7273
8763cede 7274 end_psymtab_common (objfile, pst);
0018ea6f 7275
ae640021 7276 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7277 {
7278 int i;
ae640021 7279 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7280
7281 /* Fill in 'dependencies' here; we fill in 'users' in a
7282 post-pass. */
7283 pst->number_of_dependencies = len;
a9342b62
TT
7284 pst->dependencies
7285 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7286 for (i = 0; i < len; ++i)
7287 {
7288 pst->dependencies[i]
7289 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7290 }
0018ea6f 7291
ae640021 7292 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7293 }
7294
7295 /* Get the list of files included in the current compilation unit,
7296 and build a psymtab for each of them. */
7297 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7298
b4f54984 7299 if (dwarf_read_debug)
b926417a
TT
7300 fprintf_unfiltered (gdb_stdlog,
7301 "Psymtab for %s unit @%s: %s - %s"
7302 ", %d global, %d static syms\n",
7303 per_cu->is_debug_types ? "type" : "comp",
7304 sect_offset_str (per_cu->sect_off),
7305 paddress (gdbarch, pst->text_low (objfile)),
7306 paddress (gdbarch, pst->text_high (objfile)),
7307 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7308}
7309
7310/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7311 Process compilation unit THIS_CU for a psymtab. */
7312
7313static void
7314process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7315 bool want_partial_unit,
b93601f3 7316 enum language pretend_language)
0018ea6f
DE
7317{
7318 /* If this compilation unit was already read in, free the
7319 cached copy in order to read it in again. This is
7320 necessary because we skipped some symbols when we first
7321 read in the compilation unit (see load_partial_dies).
7322 This problem could be avoided, but the benefit is unclear. */
7323 if (this_cu->cu != NULL)
7324 free_one_cached_comp_unit (this_cu);
7325
6751ebae 7326 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7327
58990295
TV
7328 switch (reader.comp_unit_die->tag)
7329 {
7330 case DW_TAG_compile_unit:
7331 this_cu->unit_type = DW_UT_compile;
7332 break;
7333 case DW_TAG_partial_unit:
7334 this_cu->unit_type = DW_UT_partial;
7335 break;
7336 default:
7337 abort ();
7338 }
7339
c0ab21c2 7340 if (reader.dummy_p)
f1902523 7341 {
c0ab21c2 7342 /* Nothing. */
f1902523 7343 }
c0ab21c2 7344 else if (this_cu->is_debug_types)
3e225074
TT
7345 build_type_psymtabs_reader (&reader, reader.info_ptr,
7346 reader.comp_unit_die);
135f5437
TT
7347 else if (want_partial_unit
7348 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7349 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7350 reader.comp_unit_die,
c0ab21c2 7351 pretend_language);
0018ea6f 7352
58990295
TV
7353 this_cu->lang = this_cu->cu->language;
7354
0018ea6f 7355 /* Age out any secondary CUs. */
ed2dc618 7356 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7357}
f4dc4d17
DE
7358
7359/* Reader function for build_type_psymtabs. */
7360
7361static void
7362build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7363 const gdb_byte *info_ptr,
3e225074 7364 struct die_info *type_unit_die)
f4dc4d17 7365{
ed2dc618 7366 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7367 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7368 struct objfile *objfile = dwarf2_per_objfile->objfile;
7369 struct dwarf2_cu *cu = reader->cu;
7370 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7371 struct signatured_type *sig_type;
f4dc4d17
DE
7372 struct type_unit_group *tu_group;
7373 struct attribute *attr;
7374 struct partial_die_info *first_die;
7375 CORE_ADDR lowpc, highpc;
891813be 7376 dwarf2_psymtab *pst;
f4dc4d17 7377
0186c6a7
DE
7378 gdb_assert (per_cu->is_debug_types);
7379 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7380
3e225074 7381 if (! type_unit_die->has_children)
f4dc4d17
DE
7382 return;
7383
052c8bb8 7384 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7385 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7386
df07e2c7 7387 if (tu_group->tus == nullptr)
a8b3b8e9 7388 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7389 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7390
7391 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7392 pst = create_partial_symtab (per_cu, "");
6d94535f 7393 pst->anonymous = true;
f4dc4d17
DE
7394
7395 first_die = load_partial_dies (reader, info_ptr, 1);
7396
7397 lowpc = (CORE_ADDR) -1;
7398 highpc = (CORE_ADDR) 0;
7399 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7400
8763cede 7401 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7402}
7403
73051182
DE
7404/* Struct used to sort TUs by their abbreviation table offset. */
7405
7406struct tu_abbrev_offset
7407{
b2bdb8cf
SM
7408 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7409 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7410 {}
7411
7412 signatured_type *sig_type;
73051182
DE
7413 sect_offset abbrev_offset;
7414};
7415
484cf504 7416/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7417
484cf504
TT
7418static bool
7419sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7420 const struct tu_abbrev_offset &b)
73051182 7421{
484cf504 7422 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7423}
7424
7425/* Efficiently read all the type units.
7426 This does the bulk of the work for build_type_psymtabs.
7427
7428 The efficiency is because we sort TUs by the abbrev table they use and
7429 only read each abbrev table once. In one program there are 200K TUs
7430 sharing 8K abbrev tables.
7431
7432 The main purpose of this function is to support building the
7433 dwarf2_per_objfile->type_unit_groups table.
7434 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7435 can collapse the search space by grouping them by stmt_list.
7436 The savings can be significant, in the same program from above the 200K TUs
7437 share 8K stmt_list tables.
7438
7439 FUNC is expected to call get_type_unit_group, which will create the
7440 struct type_unit_group if necessary and add it to
7441 dwarf2_per_objfile->type_unit_groups. */
7442
7443static void
ed2dc618 7444build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7445{
73051182 7446 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7447 abbrev_table_up abbrev_table;
73051182 7448 sect_offset abbrev_offset;
73051182
DE
7449
7450 /* It's up to the caller to not call us multiple times. */
7451 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7452
b2bdb8cf 7453 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7454 return;
7455
7456 /* TUs typically share abbrev tables, and there can be way more TUs than
7457 abbrev tables. Sort by abbrev table to reduce the number of times we
7458 read each abbrev table in.
7459 Alternatives are to punt or to maintain a cache of abbrev tables.
7460 This is simpler and efficient enough for now.
7461
7462 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7463 symtab to use). Typically TUs with the same abbrev offset have the same
7464 stmt_list value too so in practice this should work well.
7465
7466 The basic algorithm here is:
7467
7468 sort TUs by abbrev table
7469 for each TU with same abbrev table:
7470 read abbrev table if first user
7471 read TU top level DIE
7472 [IWBN if DWO skeletons had DW_AT_stmt_list]
7473 call FUNC */
7474
b4f54984 7475 if (dwarf_read_debug)
73051182
DE
7476 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7477
7478 /* Sort in a separate table to maintain the order of all_type_units
7479 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7480 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7481 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7482
7483 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7484 sorted_by_abbrev.emplace_back
7485 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7486 sig_type->per_cu.section,
7487 sig_type->per_cu.sect_off));
73051182 7488
484cf504
TT
7489 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7490 sort_tu_by_abbrev_offset);
73051182 7491
9c541725 7492 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7493
b2bdb8cf 7494 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7495 {
73051182
DE
7496 /* Switch to the next abbrev table if necessary. */
7497 if (abbrev_table == NULL
b2bdb8cf 7498 || tu.abbrev_offset != abbrev_offset)
73051182 7499 {
b2bdb8cf 7500 abbrev_offset = tu.abbrev_offset;
73051182 7501 abbrev_table =
86de1d91
TT
7502 abbrev_table::read (dwarf2_per_objfile->objfile,
7503 &dwarf2_per_objfile->abbrev,
7504 abbrev_offset);
73051182
DE
7505 ++tu_stats->nr_uniq_abbrev_tables;
7506 }
7507
c0ab21c2 7508 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7509 0, false);
c0ab21c2
TT
7510 if (!reader.dummy_p)
7511 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7512 reader.comp_unit_die);
73051182 7513 }
6aa5f3a6 7514}
73051182 7515
6aa5f3a6
DE
7516/* Print collected type unit statistics. */
7517
7518static void
ed2dc618 7519print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7520{
7521 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7522
7523 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7524 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7525 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7526 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7527 tu_stats->nr_uniq_abbrev_tables);
7528 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7529 tu_stats->nr_symtabs);
7530 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7531 tu_stats->nr_symtab_sharers);
7532 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7533 tu_stats->nr_stmt_less_type_units);
7534 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7535 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7536}
7537
f4dc4d17
DE
7538/* Traversal function for build_type_psymtabs. */
7539
7540static int
7541build_type_psymtab_dependencies (void **slot, void *info)
7542{
ed2dc618
SM
7543 struct dwarf2_per_objfile *dwarf2_per_objfile
7544 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7545 struct objfile *objfile = dwarf2_per_objfile->objfile;
7546 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7547 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7548 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7549 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7550 int i;
7551
7552 gdb_assert (len > 0);
197400e8 7553 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7554
7555 pst->number_of_dependencies = len;
a9342b62 7556 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7557 for (i = 0; i < len; ++i)
f4dc4d17 7558 {
df07e2c7 7559 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7560 gdb_assert (iter->per_cu.is_debug_types);
7561 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7562 iter->type_unit_group = tu_group;
f4dc4d17
DE
7563 }
7564
df07e2c7
AB
7565 delete tu_group->tus;
7566 tu_group->tus = nullptr;
348e048f
DE
7567
7568 return 1;
7569}
7570
7571/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7572 Build partial symbol tables for the .debug_types comp-units. */
7573
7574static void
ed2dc618 7575build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7576{
ed2dc618 7577 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7578 return;
7579
ed2dc618 7580 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7581}
f4dc4d17 7582
6aa5f3a6
DE
7583/* Traversal function for process_skeletonless_type_unit.
7584 Read a TU in a DWO file and build partial symbols for it. */
7585
7586static int
7587process_skeletonless_type_unit (void **slot, void *info)
7588{
7589 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7590 struct dwarf2_per_objfile *dwarf2_per_objfile
7591 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7592 struct signatured_type find_entry, *entry;
7593
7594 /* If this TU doesn't exist in the global table, add it and read it in. */
7595
7596 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7597 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7598
7599 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7600 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7601 &find_entry, INSERT);
6aa5f3a6
DE
7602 /* If we've already seen this type there's nothing to do. What's happening
7603 is we're doing our own version of comdat-folding here. */
7604 if (*slot != NULL)
7605 return 1;
7606
7607 /* This does the job that create_all_type_units would have done for
7608 this TU. */
ed2dc618
SM
7609 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7610 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7611 *slot = entry;
7612
7613 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7614 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7615 if (!reader.dummy_p)
7616 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7617 reader.comp_unit_die);
6aa5f3a6
DE
7618
7619 return 1;
7620}
7621
7622/* Traversal function for process_skeletonless_type_units. */
7623
7624static int
7625process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7626{
7627 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7628
7629 if (dwo_file->tus != NULL)
b0b6a987
TT
7630 htab_traverse_noresize (dwo_file->tus.get (),
7631 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7632
7633 return 1;
7634}
7635
7636/* Scan all TUs of DWO files, verifying we've processed them.
7637 This is needed in case a TU was emitted without its skeleton.
7638 Note: This can't be done until we know what all the DWO files are. */
7639
7640static void
ed2dc618 7641process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7642{
7643 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7644 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7645 && dwarf2_per_objfile->dwo_files != NULL)
7646 {
51ac9db5 7647 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7648 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7649 dwarf2_per_objfile);
6aa5f3a6 7650 }
348e048f
DE
7651}
7652
ed2dc618 7653/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7654
7655static void
ed2dc618 7656set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7657{
b76e467d 7658 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7659 {
891813be 7660 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7661
36586728
TT
7662 if (pst == NULL)
7663 continue;
7664
b76e467d 7665 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7666 {
7667 /* Set the 'user' field only if it is not already set. */
7668 if (pst->dependencies[j]->user == NULL)
7669 pst->dependencies[j]->user = pst;
7670 }
7671 }
7672}
7673
93311388
DE
7674/* Build the partial symbol table by doing a quick pass through the
7675 .debug_info and .debug_abbrev sections. */
72bf9492 7676
93311388 7677static void
ed2dc618 7678dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7679{
ed2dc618 7680 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7681
b4f54984 7682 if (dwarf_read_debug)
45cfd468
DE
7683 {
7684 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7685 objfile_name (objfile));
45cfd468
DE
7686 }
7687
76935768
TT
7688 scoped_restore restore_reading_psyms
7689 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7690 true);
98bfdba5 7691
96b79293 7692 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7693
93311388
DE
7694 /* Any cached compilation units will be linked by the per-objfile
7695 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7696 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7697
ed2dc618 7698 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7699
ed2dc618 7700 create_all_comp_units (dwarf2_per_objfile);
c906108c 7701
60606b2c
TT
7702 /* Create a temporary address map on a temporary obstack. We later
7703 copy this to the final obstack. */
8268c778 7704 auto_obstack temp_obstack;
791afaa2
TT
7705
7706 scoped_restore save_psymtabs_addrmap
d320c2b5 7707 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7708 addrmap_create_mutable (&temp_obstack));
72bf9492 7709
b76e467d 7710 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
135f5437 7711 process_psymtab_comp_unit (per_cu, false, language_minimal);
ff013f42 7712
6aa5f3a6 7713 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7714 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7715
7716 /* Now that all TUs have been processed we can fill in the dependencies. */
7717 if (dwarf2_per_objfile->type_unit_groups != NULL)
7718 {
eaa5fa8b 7719 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7720 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7721 }
7722
b4f54984 7723 if (dwarf_read_debug)
ed2dc618 7724 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7725
ed2dc618 7726 set_partial_user (dwarf2_per_objfile);
95554aad 7727
d320c2b5
TT
7728 objfile->partial_symtabs->psymtabs_addrmap
7729 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7730 objfile->partial_symtabs->obstack ());
791afaa2
TT
7731 /* At this point we want to keep the address map. */
7732 save_psymtabs_addrmap.release ();
ff013f42 7733
b4f54984 7734 if (dwarf_read_debug)
45cfd468 7735 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7736 objfile_name (objfile));
ae038cb0
DJ
7737}
7738
dee91e82
DE
7739/* Load the partial DIEs for a secondary CU into memory.
7740 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7741
dee91e82
DE
7742static void
7743load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7744{
6751ebae 7745 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7746
7747 if (!reader.dummy_p)
7748 {
7749 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7750 language_minimal);
7751
7752 /* Check if comp unit has_children.
7753 If so, read the rest of the partial symbols from this comp unit.
7754 If not, there's no more debug_info for this comp unit. */
3e225074 7755 if (reader.comp_unit_die->has_children)
c0ab21c2 7756 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7757
7758 reader.keep ();
c0ab21c2 7759 }
ae038cb0
DJ
7760}
7761
ae038cb0 7762static void
ed2dc618 7763read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7764 struct dwarf2_section_info *section,
f1902523 7765 struct dwarf2_section_info *abbrev_section,
b76e467d 7766 unsigned int is_dwz)
ae038cb0 7767{
d521ce57 7768 const gdb_byte *info_ptr;
ed2dc618 7769 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7770
b4f54984 7771 if (dwarf_read_debug)
bf6af496 7772 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7773 section->get_name (),
7774 section->get_file_name ());
bf6af496 7775
96b79293 7776 section->read (objfile);
ae038cb0 7777
36586728 7778 info_ptr = section->buffer;
6e70227d 7779
36586728 7780 while (info_ptr < section->buffer + section->size)
ae038cb0 7781 {
ae038cb0 7782 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7783
9c541725 7784 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7785
f1902523 7786 comp_unit_head cu_header;
ed2dc618
SM
7787 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7788 abbrev_section, info_ptr,
7789 rcuh_kind::COMPILE);
ae038cb0
DJ
7790
7791 /* Save the compilation unit for later lookup. */
f1902523
JK
7792 if (cu_header.unit_type != DW_UT_type)
7793 {
7794 this_cu = XOBNEW (&objfile->objfile_obstack,
7795 struct dwarf2_per_cu_data);
7796 memset (this_cu, 0, sizeof (*this_cu));
7797 }
7798 else
7799 {
7800 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7801 struct signatured_type);
7802 memset (sig_type, 0, sizeof (*sig_type));
7803 sig_type->signature = cu_header.signature;
7804 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7805 this_cu = &sig_type->per_cu;
7806 }
7807 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7808 this_cu->sect_off = sect_off;
f1902523 7809 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7810 this_cu->is_dwz = is_dwz;
e3b94546 7811 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7812 this_cu->section = section;
ae038cb0 7813
b76e467d 7814 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7815
7816 info_ptr = info_ptr + this_cu->length;
7817 }
36586728
TT
7818}
7819
7820/* Create a list of all compilation units in OBJFILE.
7821 This is only done for -readnow and building partial symtabs. */
7822
7823static void
ed2dc618 7824create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7825{
b76e467d 7826 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7827 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7828 &dwarf2_per_objfile->abbrev, 0);
36586728 7829
b76e467d 7830 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7831 if (dwz != NULL)
ed2dc618 7832 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7833 1);
c906108c
SS
7834}
7835
5734ee8b 7836/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7837 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7838 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7839 DW_AT_ranges). See the comments of add_partial_subprogram on how
7840 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7841
72bf9492
DJ
7842static void
7843scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7844 CORE_ADDR *highpc, int set_addrmap,
7845 struct dwarf2_cu *cu)
c906108c 7846{
72bf9492 7847 struct partial_die_info *pdi;
c906108c 7848
91c24f0a
DC
7849 /* Now, march along the PDI's, descending into ones which have
7850 interesting children but skipping the children of the other ones,
7851 until we reach the end of the compilation unit. */
c906108c 7852
72bf9492 7853 pdi = first_die;
91c24f0a 7854
72bf9492
DJ
7855 while (pdi != NULL)
7856 {
52356b79 7857 pdi->fixup (cu);
c906108c 7858
f55ee35c 7859 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7860 children, so we need to look at them. Ditto for anonymous
7861 enums. */
933c6fe4 7862
72bf9492 7863 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7864 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7865 || pdi->tag == DW_TAG_imported_unit
7866 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7867 {
72bf9492 7868 switch (pdi->tag)
c906108c
SS
7869 {
7870 case DW_TAG_subprogram:
b1dc1806 7871 case DW_TAG_inlined_subroutine:
cdc07690 7872 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7873 break;
72929c62 7874 case DW_TAG_constant:
c906108c
SS
7875 case DW_TAG_variable:
7876 case DW_TAG_typedef:
91c24f0a 7877 case DW_TAG_union_type:
72bf9492 7878 if (!pdi->is_declaration)
63d06c5c 7879 {
72bf9492 7880 add_partial_symbol (pdi, cu);
63d06c5c
DC
7881 }
7882 break;
c906108c 7883 case DW_TAG_class_type:
680b30c7 7884 case DW_TAG_interface_type:
c906108c 7885 case DW_TAG_structure_type:
72bf9492 7886 if (!pdi->is_declaration)
c906108c 7887 {
72bf9492 7888 add_partial_symbol (pdi, cu);
c906108c 7889 }
b7fee5a3
KS
7890 if ((cu->language == language_rust
7891 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
7892 scan_partial_symbols (pdi->die_child, lowpc, highpc,
7893 set_addrmap, cu);
c906108c 7894 break;
91c24f0a 7895 case DW_TAG_enumeration_type:
72bf9492
DJ
7896 if (!pdi->is_declaration)
7897 add_partial_enumeration (pdi, cu);
c906108c
SS
7898 break;
7899 case DW_TAG_base_type:
a02abb62 7900 case DW_TAG_subrange_type:
c906108c 7901 /* File scope base type definitions are added to the partial
c5aa993b 7902 symbol table. */
72bf9492 7903 add_partial_symbol (pdi, cu);
c906108c 7904 break;
d9fa45fe 7905 case DW_TAG_namespace:
cdc07690 7906 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 7907 break;
5d7cb8df 7908 case DW_TAG_module:
59c35742
AB
7909 if (!pdi->is_declaration)
7910 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 7911 break;
95554aad
TT
7912 case DW_TAG_imported_unit:
7913 {
7914 struct dwarf2_per_cu_data *per_cu;
7915
f4dc4d17
DE
7916 /* For now we don't handle imported units in type units. */
7917 if (cu->per_cu->is_debug_types)
7918 {
7919 error (_("Dwarf Error: DW_TAG_imported_unit is not"
7920 " supported in type units [in module %s]"),
518817b3 7921 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
7922 }
7923
e3b94546
SM
7924 per_cu = dwarf2_find_containing_comp_unit
7925 (pdi->d.sect_off, pdi->is_dwz,
518817b3 7926 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
7927
7928 /* Go read the partial unit, if needed. */
7929 if (per_cu->v.psymtab == NULL)
135f5437 7930 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 7931
ae640021 7932 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
7933 }
7934 break;
74921315
KS
7935 case DW_TAG_imported_declaration:
7936 add_partial_symbol (pdi, cu);
7937 break;
c906108c
SS
7938 default:
7939 break;
7940 }
7941 }
7942
72bf9492
DJ
7943 /* If the die has a sibling, skip to the sibling. */
7944
7945 pdi = pdi->die_sibling;
7946 }
7947}
7948
7949/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 7950
72bf9492 7951 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 7952 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
7953 Enumerators are an exception; they use the scope of their parent
7954 enumeration type, i.e. the name of the enumeration type is not
7955 prepended to the enumerator.
91c24f0a 7956
72bf9492
DJ
7957 There are two complexities. One is DW_AT_specification; in this
7958 case "parent" means the parent of the target of the specification,
7959 instead of the direct parent of the DIE. The other is compilers
7960 which do not emit DW_TAG_namespace; in this case we try to guess
7961 the fully qualified name of structure types from their members'
7962 linkage names. This must be done using the DIE's children rather
7963 than the children of any DW_AT_specification target. We only need
7964 to do this for structures at the top level, i.e. if the target of
7965 any DW_AT_specification (if any; otherwise the DIE itself) does not
7966 have a parent. */
7967
7968/* Compute the scope prefix associated with PDI's parent, in
7969 compilation unit CU. The result will be allocated on CU's
7970 comp_unit_obstack, or a copy of the already allocated PDI->NAME
7971 field. NULL is returned if no prefix is necessary. */
15d034d0 7972static const char *
72bf9492
DJ
7973partial_die_parent_scope (struct partial_die_info *pdi,
7974 struct dwarf2_cu *cu)
7975{
15d034d0 7976 const char *grandparent_scope;
72bf9492 7977 struct partial_die_info *parent, *real_pdi;
91c24f0a 7978
72bf9492
DJ
7979 /* We need to look at our parent DIE; if we have a DW_AT_specification,
7980 then this means the parent of the specification DIE. */
7981
7982 real_pdi = pdi;
72bf9492 7983 while (real_pdi->has_specification)
fb816e8b 7984 {
122cf0f2
AB
7985 auto res = find_partial_die (real_pdi->spec_offset,
7986 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
7987 real_pdi = res.pdi;
7988 cu = res.cu;
7989 }
72bf9492
DJ
7990
7991 parent = real_pdi->die_parent;
7992 if (parent == NULL)
7993 return NULL;
7994
7995 if (parent->scope_set)
7996 return parent->scope;
7997
52356b79 7998 parent->fixup (cu);
72bf9492 7999
10b3939b 8000 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8001
acebe513
UW
8002 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8003 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8004 Work around this problem here. */
8005 if (cu->language == language_cplus
6e70227d 8006 && parent->tag == DW_TAG_namespace
acebe513
UW
8007 && strcmp (parent->name, "::") == 0
8008 && grandparent_scope == NULL)
8009 {
8010 parent->scope = NULL;
8011 parent->scope_set = 1;
8012 return NULL;
8013 }
8014
0a4b0913 8015 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8016 if (pdi->tag == DW_TAG_enumerator)
8017 /* Enumerators should not get the name of the enumeration as a prefix. */
8018 parent->scope = grandparent_scope;
8019 else if (parent->tag == DW_TAG_namespace
f55ee35c 8020 || parent->tag == DW_TAG_module
72bf9492
DJ
8021 || parent->tag == DW_TAG_structure_type
8022 || parent->tag == DW_TAG_class_type
680b30c7 8023 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8024 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8025 || parent->tag == DW_TAG_enumeration_type
8026 || (cu->language == language_fortran
8027 && parent->tag == DW_TAG_subprogram
8028 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8029 {
8030 if (grandparent_scope == NULL)
8031 parent->scope = parent->name;
8032 else
3e43a32a
MS
8033 parent->scope = typename_concat (&cu->comp_unit_obstack,
8034 grandparent_scope,
f55ee35c 8035 parent->name, 0, cu);
72bf9492 8036 }
72bf9492
DJ
8037 else
8038 {
8039 /* FIXME drow/2004-04-01: What should we be doing with
8040 function-local names? For partial symbols, we should probably be
8041 ignoring them. */
fa9c3fa0
TT
8042 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8043 dwarf_tag_name (parent->tag),
8044 sect_offset_str (pdi->sect_off));
72bf9492 8045 parent->scope = grandparent_scope;
c906108c
SS
8046 }
8047
72bf9492
DJ
8048 parent->scope_set = 1;
8049 return parent->scope;
8050}
8051
8052/* Return the fully scoped name associated with PDI, from compilation unit
8053 CU. The result will be allocated with malloc. */
4568ecf9 8054
43816ebc 8055static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8056partial_die_full_name (struct partial_die_info *pdi,
8057 struct dwarf2_cu *cu)
8058{
15d034d0 8059 const char *parent_scope;
72bf9492 8060
98bfdba5
PA
8061 /* If this is a template instantiation, we can not work out the
8062 template arguments from partial DIEs. So, unfortunately, we have
8063 to go through the full DIEs. At least any work we do building
8064 types here will be reused if full symbols are loaded later. */
8065 if (pdi->has_template_arguments)
8066 {
52356b79 8067 pdi->fixup (cu);
98bfdba5
PA
8068
8069 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8070 {
8071 struct die_info *die;
8072 struct attribute attr;
8073 struct dwarf2_cu *ref_cu = cu;
8074
b64f50a1 8075 /* DW_FORM_ref_addr is using section offset. */
b4069958 8076 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8077 attr.form = DW_FORM_ref_addr;
9c541725 8078 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8079 die = follow_die_ref (NULL, &attr, &ref_cu);
8080
43816ebc 8081 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8082 }
8083 }
8084
72bf9492
DJ
8085 parent_scope = partial_die_parent_scope (pdi, cu);
8086 if (parent_scope == NULL)
8087 return NULL;
8088 else
43816ebc
TT
8089 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8090 pdi->name, 0, cu));
c906108c
SS
8091}
8092
8093static void
72bf9492 8094add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8095{
518817b3
SM
8096 struct dwarf2_per_objfile *dwarf2_per_objfile
8097 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8098 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 8099 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 8100 CORE_ADDR addr = 0;
15d034d0 8101 const char *actual_name = NULL;
e142c38c
DJ
8102 CORE_ADDR baseaddr;
8103
b3b3bada 8104 baseaddr = objfile->text_section_offset ();
c906108c 8105
43816ebc
TT
8106 gdb::unique_xmalloc_ptr<char> built_actual_name
8107 = partial_die_full_name (pdi, cu);
15d034d0 8108 if (built_actual_name != NULL)
43816ebc 8109 actual_name = built_actual_name.get ();
63d06c5c 8110
72bf9492
DJ
8111 if (actual_name == NULL)
8112 actual_name = pdi->name;
8113
c906108c
SS
8114 switch (pdi->tag)
8115 {
b1dc1806 8116 case DW_TAG_inlined_subroutine:
c906108c 8117 case DW_TAG_subprogram:
79748972
TT
8118 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8119 - baseaddr);
0a4b0913
AB
8120 if (pdi->is_external
8121 || cu->language == language_ada
8122 || (cu->language == language_fortran
8123 && pdi->die_parent != NULL
8124 && pdi->die_parent->tag == DW_TAG_subprogram))
8125 {
8126 /* Normally, only "external" DIEs are part of the global scope.
8127 But in Ada and Fortran, we want to be able to access nested
8128 procedures globally. So all Ada and Fortran subprograms are
8129 stored in the global scope. */
31edb802 8130 add_psymbol_to_list (actual_name,
15d034d0 8131 built_actual_name != NULL,
f47fb265 8132 VAR_DOMAIN, LOC_BLOCK,
79748972 8133 SECT_OFF_TEXT (objfile),
75aedd27 8134 psymbol_placement::GLOBAL,
79748972
TT
8135 addr,
8136 cu->language, objfile);
c906108c
SS
8137 }
8138 else
8139 {
31edb802 8140 add_psymbol_to_list (actual_name,
15d034d0 8141 built_actual_name != NULL,
f47fb265 8142 VAR_DOMAIN, LOC_BLOCK,
79748972 8143 SECT_OFF_TEXT (objfile),
75aedd27 8144 psymbol_placement::STATIC,
1762568f 8145 addr, cu->language, objfile);
c906108c 8146 }
0c1b455e
TT
8147
8148 if (pdi->main_subprogram && actual_name != NULL)
8149 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8150 break;
72929c62 8151 case DW_TAG_constant:
31edb802 8152 add_psymbol_to_list (actual_name,
75aedd27
TT
8153 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8154 -1, (pdi->is_external
8155 ? psymbol_placement::GLOBAL
8156 : psymbol_placement::STATIC),
8157 0, cu->language, objfile);
72929c62 8158 break;
c906108c 8159 case DW_TAG_variable:
95554aad
TT
8160 if (pdi->d.locdesc)
8161 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8162
95554aad 8163 if (pdi->d.locdesc
caac4577
JG
8164 && addr == 0
8165 && !dwarf2_per_objfile->has_section_at_zero)
8166 {
8167 /* A global or static variable may also have been stripped
8168 out by the linker if unused, in which case its address
8169 will be nullified; do not add such variables into partial
8170 symbol table then. */
8171 }
8172 else if (pdi->is_external)
c906108c
SS
8173 {
8174 /* Global Variable.
8175 Don't enter into the minimal symbol tables as there is
8176 a minimal symbol table entry from the ELF symbols already.
8177 Enter into partial symbol table if it has a location
8178 descriptor or a type.
8179 If the location descriptor is missing, new_symbol will create
8180 a LOC_UNRESOLVED symbol, the address of the variable will then
8181 be determined from the minimal symbol table whenever the variable
8182 is referenced.
8183 The address for the partial symbol table entry is not
8184 used by GDB, but it comes in handy for debugging partial symbol
8185 table building. */
8186
95554aad 8187 if (pdi->d.locdesc || pdi->has_type)
31edb802 8188 add_psymbol_to_list (actual_name,
15d034d0 8189 built_actual_name != NULL,
f47fb265 8190 VAR_DOMAIN, LOC_STATIC,
79748972 8191 SECT_OFF_TEXT (objfile),
75aedd27 8192 psymbol_placement::GLOBAL,
79748972 8193 addr, cu->language, objfile);
c906108c
SS
8194 }
8195 else
8196 {
ff908ebf
AW
8197 int has_loc = pdi->d.locdesc != NULL;
8198
8199 /* Static Variable. Skip symbols whose value we cannot know (those
8200 without location descriptors or constant values). */
8201 if (!has_loc && !pdi->has_const_value)
43816ebc 8202 return;
ff908ebf 8203
31edb802 8204 add_psymbol_to_list (actual_name,
15d034d0 8205 built_actual_name != NULL,
f47fb265 8206 VAR_DOMAIN, LOC_STATIC,
79748972 8207 SECT_OFF_TEXT (objfile),
75aedd27 8208 psymbol_placement::STATIC,
79748972 8209 has_loc ? addr : 0,
f47fb265 8210 cu->language, objfile);
c906108c
SS
8211 }
8212 break;
8213 case DW_TAG_typedef:
8214 case DW_TAG_base_type:
a02abb62 8215 case DW_TAG_subrange_type:
31edb802 8216 add_psymbol_to_list (actual_name,
15d034d0 8217 built_actual_name != NULL,
79748972 8218 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8219 psymbol_placement::STATIC,
1762568f 8220 0, cu->language, objfile);
c906108c 8221 break;
74921315 8222 case DW_TAG_imported_declaration:
72bf9492 8223 case DW_TAG_namespace:
31edb802 8224 add_psymbol_to_list (actual_name,
15d034d0 8225 built_actual_name != NULL,
79748972 8226 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8227 psymbol_placement::GLOBAL,
1762568f 8228 0, cu->language, objfile);
72bf9492 8229 break;
530e8392 8230 case DW_TAG_module:
a5fd13a9
BH
8231 /* With Fortran 77 there might be a "BLOCK DATA" module
8232 available without any name. If so, we skip the module as it
8233 doesn't bring any value. */
8234 if (actual_name != nullptr)
31edb802 8235 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8236 built_actual_name != NULL,
8237 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8238 psymbol_placement::GLOBAL,
8239 0, cu->language, objfile);
530e8392 8240 break;
c906108c 8241 case DW_TAG_class_type:
680b30c7 8242 case DW_TAG_interface_type:
c906108c
SS
8243 case DW_TAG_structure_type:
8244 case DW_TAG_union_type:
8245 case DW_TAG_enumeration_type:
fa4028e9
JB
8246 /* Skip external references. The DWARF standard says in the section
8247 about "Structure, Union, and Class Type Entries": "An incomplete
8248 structure, union or class type is represented by a structure,
8249 union or class entry that does not have a byte size attribute
8250 and that has a DW_AT_declaration attribute." */
8251 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8252 return;
fa4028e9 8253
63d06c5c
DC
8254 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8255 static vs. global. */
31edb802 8256 add_psymbol_to_list (actual_name,
15d034d0 8257 built_actual_name != NULL,
79748972 8258 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8259 cu->language == language_cplus
75aedd27
TT
8260 ? psymbol_placement::GLOBAL
8261 : psymbol_placement::STATIC,
1762568f 8262 0, cu->language, objfile);
c906108c 8263
c906108c
SS
8264 break;
8265 case DW_TAG_enumerator:
31edb802 8266 add_psymbol_to_list (actual_name,
15d034d0 8267 built_actual_name != NULL,
79748972 8268 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8269 cu->language == language_cplus
75aedd27
TT
8270 ? psymbol_placement::GLOBAL
8271 : psymbol_placement::STATIC,
1762568f 8272 0, cu->language, objfile);
c906108c
SS
8273 break;
8274 default:
8275 break;
8276 }
8277}
8278
5c4e30ca
DC
8279/* Read a partial die corresponding to a namespace; also, add a symbol
8280 corresponding to that namespace to the symbol table. NAMESPACE is
8281 the name of the enclosing namespace. */
91c24f0a 8282
72bf9492
DJ
8283static void
8284add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8285 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8286 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8287{
72bf9492 8288 /* Add a symbol for the namespace. */
e7c27a73 8289
72bf9492 8290 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8291
8292 /* Now scan partial symbols in that namespace. */
8293
91c24f0a 8294 if (pdi->has_children)
cdc07690 8295 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8296}
8297
5d7cb8df
JK
8298/* Read a partial die corresponding to a Fortran module. */
8299
8300static void
8301add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8302 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8303{
530e8392
KB
8304 /* Add a symbol for the namespace. */
8305
8306 add_partial_symbol (pdi, cu);
8307
f55ee35c 8308 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8309
8310 if (pdi->has_children)
cdc07690 8311 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8312}
8313
b1dc1806
XR
8314/* Read a partial die corresponding to a subprogram or an inlined
8315 subprogram and create a partial symbol for that subprogram.
8316 When the CU language allows it, this routine also defines a partial
8317 symbol for each nested subprogram that this subprogram contains.
8318 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8319 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8320
cdc07690
YQ
8321 PDI may also be a lexical block, in which case we simply search
8322 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8323 Again, this is only performed when the CU language allows this
8324 type of definitions. */
8325
8326static void
8327add_partial_subprogram (struct partial_die_info *pdi,
8328 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8329 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8330{
b1dc1806 8331 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8332 {
8333 if (pdi->has_pc_info)
8334 {
8335 if (pdi->lowpc < *lowpc)
8336 *lowpc = pdi->lowpc;
8337 if (pdi->highpc > *highpc)
8338 *highpc = pdi->highpc;
cdc07690 8339 if (set_addrmap)
5734ee8b 8340 {
518817b3 8341 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a
MR
8342 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8343 CORE_ADDR baseaddr;
b926417a
TT
8344 CORE_ADDR this_highpc;
8345 CORE_ADDR this_lowpc;
5734ee8b 8346
b3b3bada 8347 baseaddr = objfile->text_section_offset ();
b926417a
TT
8348 this_lowpc
8349 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8350 pdi->lowpc + baseaddr)
8351 - baseaddr);
8352 this_highpc
8353 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8354 pdi->highpc + baseaddr)
8355 - baseaddr);
d320c2b5 8356 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8357 this_lowpc, this_highpc - 1,
9291a0cd 8358 cu->per_cu->v.psymtab);
5734ee8b 8359 }
481860b3
GB
8360 }
8361
8362 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8363 {
bc30ff58 8364 if (!pdi->is_declaration)
e8d05480
JB
8365 /* Ignore subprogram DIEs that do not have a name, they are
8366 illegal. Do not emit a complaint at this point, we will
8367 do so when we convert this psymtab into a symtab. */
8368 if (pdi->name)
8369 add_partial_symbol (pdi, cu);
bc30ff58
JB
8370 }
8371 }
6e70227d 8372
bc30ff58
JB
8373 if (! pdi->has_children)
8374 return;
8375
0a4b0913 8376 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8377 {
8378 pdi = pdi->die_child;
8379 while (pdi != NULL)
8380 {
52356b79 8381 pdi->fixup (cu);
bc30ff58 8382 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8383 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8384 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8385 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8386 pdi = pdi->die_sibling;
8387 }
8388 }
8389}
8390
91c24f0a
DC
8391/* Read a partial die corresponding to an enumeration type. */
8392
72bf9492
DJ
8393static void
8394add_partial_enumeration (struct partial_die_info *enum_pdi,
8395 struct dwarf2_cu *cu)
91c24f0a 8396{
72bf9492 8397 struct partial_die_info *pdi;
91c24f0a
DC
8398
8399 if (enum_pdi->name != NULL)
72bf9492
DJ
8400 add_partial_symbol (enum_pdi, cu);
8401
8402 pdi = enum_pdi->die_child;
8403 while (pdi)
91c24f0a 8404 {
72bf9492 8405 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8406 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8407 else
72bf9492
DJ
8408 add_partial_symbol (pdi, cu);
8409 pdi = pdi->die_sibling;
91c24f0a 8410 }
91c24f0a
DC
8411}
8412
6caca83c
CC
8413/* Return the initial uleb128 in the die at INFO_PTR. */
8414
8415static unsigned int
d521ce57 8416peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8417{
8418 unsigned int bytes_read;
8419
8420 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8421}
8422
685af9cd
TT
8423/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8424 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8425
4bb7a0a7
DJ
8426 Return the corresponding abbrev, or NULL if the number is zero (indicating
8427 an empty DIE). In either case *BYTES_READ will be set to the length of
8428 the initial number. */
8429
8430static struct abbrev_info *
685af9cd
TT
8431peek_die_abbrev (const die_reader_specs &reader,
8432 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8433{
685af9cd 8434 dwarf2_cu *cu = reader.cu;
518817b3 8435 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8436 unsigned int abbrev_number
8437 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8438
8439 if (abbrev_number == 0)
8440 return NULL;
8441
685af9cd 8442 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8443 if (!abbrev)
8444 {
422b9917 8445 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8446 " at offset %s [in module %s]"),
422b9917 8447 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8448 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8449 }
8450
8451 return abbrev;
8452}
8453
93311388
DE
8454/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8455 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8456 DIE. Any children of the skipped DIEs will also be skipped. */
8457
d521ce57
TT
8458static const gdb_byte *
8459skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8460{
4bb7a0a7
DJ
8461 while (1)
8462 {
685af9cd
TT
8463 unsigned int bytes_read;
8464 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8465
4bb7a0a7
DJ
8466 if (abbrev == NULL)
8467 return info_ptr + bytes_read;
8468 else
dee91e82 8469 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8470 }
8471}
8472
93311388
DE
8473/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8474 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8475 abbrev corresponding to that skipped uleb128 should be passed in
8476 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8477 children. */
8478
d521ce57
TT
8479static const gdb_byte *
8480skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8481 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8482{
8483 unsigned int bytes_read;
8484 struct attribute attr;
dee91e82
DE
8485 bfd *abfd = reader->abfd;
8486 struct dwarf2_cu *cu = reader->cu;
d521ce57 8487 const gdb_byte *buffer = reader->buffer;
f664829e 8488 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8489 unsigned int form, i;
8490
8491 for (i = 0; i < abbrev->num_attrs; i++)
8492 {
8493 /* The only abbrev we care about is DW_AT_sibling. */
8494 if (abbrev->attrs[i].name == DW_AT_sibling)
8495 {
18a8505e
AT
8496 bool ignored;
8497 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8498 &ignored);
4bb7a0a7 8499 if (attr.form == DW_FORM_ref_addr)
b98664d3 8500 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8501 else
b9502d3f 8502 {
0826b30a 8503 sect_offset off = attr.get_ref_die_offset ();
9c541725 8504 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8505
8506 if (sibling_ptr < info_ptr)
b98664d3 8507 complaint (_("DW_AT_sibling points backwards"));
22869d73 8508 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8509 reader->die_section->overflow_complaint ();
b9502d3f
WN
8510 else
8511 return sibling_ptr;
8512 }
4bb7a0a7
DJ
8513 }
8514
8515 /* If it isn't DW_AT_sibling, skip this attribute. */
8516 form = abbrev->attrs[i].form;
8517 skip_attribute:
8518 switch (form)
8519 {
4bb7a0a7 8520 case DW_FORM_ref_addr:
ae411497
TT
8521 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8522 and later it is offset sized. */
8523 if (cu->header.version == 2)
8524 info_ptr += cu->header.addr_size;
8525 else
8526 info_ptr += cu->header.offset_size;
8527 break;
36586728
TT
8528 case DW_FORM_GNU_ref_alt:
8529 info_ptr += cu->header.offset_size;
8530 break;
ae411497 8531 case DW_FORM_addr:
4bb7a0a7
DJ
8532 info_ptr += cu->header.addr_size;
8533 break;
8534 case DW_FORM_data1:
8535 case DW_FORM_ref1:
8536 case DW_FORM_flag:
8fe0f950 8537 case DW_FORM_strx1:
4bb7a0a7
DJ
8538 info_ptr += 1;
8539 break;
2dc7f7b3 8540 case DW_FORM_flag_present:
43988095 8541 case DW_FORM_implicit_const:
2dc7f7b3 8542 break;
4bb7a0a7
DJ
8543 case DW_FORM_data2:
8544 case DW_FORM_ref2:
8fe0f950 8545 case DW_FORM_strx2:
4bb7a0a7
DJ
8546 info_ptr += 2;
8547 break;
8fe0f950
AT
8548 case DW_FORM_strx3:
8549 info_ptr += 3;
8550 break;
4bb7a0a7
DJ
8551 case DW_FORM_data4:
8552 case DW_FORM_ref4:
8fe0f950 8553 case DW_FORM_strx4:
4bb7a0a7
DJ
8554 info_ptr += 4;
8555 break;
8556 case DW_FORM_data8:
8557 case DW_FORM_ref8:
55f1336d 8558 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8559 info_ptr += 8;
8560 break;
0224619f
JK
8561 case DW_FORM_data16:
8562 info_ptr += 16;
8563 break;
4bb7a0a7 8564 case DW_FORM_string:
9b1c24c8 8565 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8566 info_ptr += bytes_read;
8567 break;
2dc7f7b3 8568 case DW_FORM_sec_offset:
4bb7a0a7 8569 case DW_FORM_strp:
36586728 8570 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8571 info_ptr += cu->header.offset_size;
8572 break;
2dc7f7b3 8573 case DW_FORM_exprloc:
4bb7a0a7
DJ
8574 case DW_FORM_block:
8575 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8576 info_ptr += bytes_read;
8577 break;
8578 case DW_FORM_block1:
8579 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8580 break;
8581 case DW_FORM_block2:
8582 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8583 break;
8584 case DW_FORM_block4:
8585 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8586 break;
336d760d 8587 case DW_FORM_addrx:
cf532bd1 8588 case DW_FORM_strx:
4bb7a0a7
DJ
8589 case DW_FORM_sdata:
8590 case DW_FORM_udata:
8591 case DW_FORM_ref_udata:
3019eac3
DE
8592 case DW_FORM_GNU_addr_index:
8593 case DW_FORM_GNU_str_index:
18a8505e 8594 case DW_FORM_rnglistx:
d521ce57 8595 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8596 break;
8597 case DW_FORM_indirect:
8598 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8599 info_ptr += bytes_read;
8600 /* We need to continue parsing from here, so just go back to
8601 the top. */
8602 goto skip_attribute;
8603
8604 default:
3e43a32a
MS
8605 error (_("Dwarf Error: Cannot handle %s "
8606 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8607 dwarf_form_name (form),
8608 bfd_get_filename (abfd));
8609 }
8610 }
8611
8612 if (abbrev->has_children)
dee91e82 8613 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8614 else
8615 return info_ptr;
8616}
8617
93311388 8618/* Locate ORIG_PDI's sibling.
dee91e82 8619 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8620
d521ce57 8621static const gdb_byte *
dee91e82
DE
8622locate_pdi_sibling (const struct die_reader_specs *reader,
8623 struct partial_die_info *orig_pdi,
d521ce57 8624 const gdb_byte *info_ptr)
91c24f0a
DC
8625{
8626 /* Do we know the sibling already? */
72bf9492 8627
91c24f0a
DC
8628 if (orig_pdi->sibling)
8629 return orig_pdi->sibling;
8630
8631 /* Are there any children to deal with? */
8632
8633 if (!orig_pdi->has_children)
8634 return info_ptr;
8635
4bb7a0a7 8636 /* Skip the children the long way. */
91c24f0a 8637
dee91e82 8638 return skip_children (reader, info_ptr);
91c24f0a
DC
8639}
8640
257e7a09 8641/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8642 not NULL. */
c906108c 8643
891813be
TT
8644void
8645dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8646{
ed2dc618
SM
8647 struct dwarf2_per_objfile *dwarf2_per_objfile
8648 = get_dwarf2_per_objfile (objfile);
8649
077cbab2
TT
8650 gdb_assert (!readin);
8651 /* If this psymtab is constructed from a debug-only objfile, the
8652 has_section_at_zero flag will not necessarily be correct. We
8653 can get the correct value for this flag by looking at the data
8654 associated with the (presumably stripped) associated objfile. */
8655 if (objfile->separate_debug_objfile_backlink)
c906108c 8656 {
077cbab2
TT
8657 struct dwarf2_per_objfile *dpo_backlink
8658 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8659
077cbab2
TT
8660 dwarf2_per_objfile->has_section_at_zero
8661 = dpo_backlink->has_section_at_zero;
8662 }
98bfdba5 8663
8566b89b 8664 expand_psymtab (objfile);
95554aad 8665
ed2dc618 8666 process_cu_includes (dwarf2_per_objfile);
c906108c 8667}
9cdd5dbd
DE
8668\f
8669/* Reading in full CUs. */
c906108c 8670
10b3939b
DJ
8671/* Add PER_CU to the queue. */
8672
8673static void
95554aad
TT
8674queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8675 enum language pretend_language)
10b3939b 8676{
10b3939b 8677 per_cu->queued = 1;
39856def 8678 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8679}
8680
89e63ee4
DE
8681/* If PER_CU is not yet queued, add it to the queue.
8682 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8683 dependency.
0907af0c 8684 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8685 meaning either PER_CU is already queued or it is already loaded.
8686
8687 N.B. There is an invariant here that if a CU is queued then it is loaded.
8688 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8689
8690static int
89e63ee4 8691maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8692 struct dwarf2_per_cu_data *per_cu,
8693 enum language pretend_language)
8694{
8695 /* We may arrive here during partial symbol reading, if we need full
8696 DIEs to process an unusual case (e.g. template arguments). Do
8697 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8698 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8699 {
8700 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8701 return 1;
8702 return 0;
8703 }
8704
8705 /* Mark the dependence relation so that we don't flush PER_CU
8706 too early. */
89e63ee4
DE
8707 if (dependent_cu != NULL)
8708 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8709
8710 /* If it's already on the queue, we have nothing to do. */
8711 if (per_cu->queued)
8712 return 0;
8713
8714 /* If the compilation unit is already loaded, just mark it as
8715 used. */
8716 if (per_cu->cu != NULL)
8717 {
8718 per_cu->cu->last_used = 0;
8719 return 0;
8720 }
8721
8722 /* Add it to the queue. */
8723 queue_comp_unit (per_cu, pretend_language);
8724
8725 return 1;
8726}
8727
10b3939b
DJ
8728/* Process the queue. */
8729
8730static void
ed2dc618 8731process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8732{
b4f54984 8733 if (dwarf_read_debug)
45cfd468
DE
8734 {
8735 fprintf_unfiltered (gdb_stdlog,
8736 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8737 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8738 }
8739
03dd20cc
DJ
8740 /* The queue starts out with one item, but following a DIE reference
8741 may load a new CU, adding it to the end of the queue. */
39856def 8742 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8743 {
39856def
TT
8744 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8745
cc12ce38 8746 if ((dwarf2_per_objfile->using_index
39856def
TT
8747 ? !item.per_cu->v.quick->compunit_symtab
8748 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8749 /* Skip dummy CUs. */
39856def 8750 && item.per_cu->cu != NULL)
f4dc4d17 8751 {
39856def 8752 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8753 unsigned int debug_print_threshold;
247f5c4f 8754 char buf[100];
f4dc4d17 8755
247f5c4f 8756 if (per_cu->is_debug_types)
f4dc4d17 8757 {
247f5c4f
DE
8758 struct signatured_type *sig_type =
8759 (struct signatured_type *) per_cu;
8760
9d8780f0 8761 sprintf (buf, "TU %s at offset %s",
73be47f5 8762 hex_string (sig_type->signature),
9d8780f0 8763 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8764 /* There can be 100s of TUs.
8765 Only print them in verbose mode. */
8766 debug_print_threshold = 2;
f4dc4d17 8767 }
247f5c4f 8768 else
73be47f5 8769 {
9d8780f0
SM
8770 sprintf (buf, "CU at offset %s",
8771 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8772 debug_print_threshold = 1;
8773 }
247f5c4f 8774
b4f54984 8775 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8776 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8777
8778 if (per_cu->is_debug_types)
39856def 8779 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8780 else
39856def 8781 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8782
b4f54984 8783 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8784 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8785 }
10b3939b 8786
39856def
TT
8787 item.per_cu->queued = 0;
8788 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8789 }
8790
b4f54984 8791 if (dwarf_read_debug)
45cfd468
DE
8792 {
8793 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8794 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8795 }
10b3939b
DJ
8796}
8797
10b3939b
DJ
8798/* Read in full symbols for PST, and anything it depends on. */
8799
8566b89b
TT
8800void
8801dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8802{
8566b89b 8803 if (readin)
95554aad
TT
8804 return;
8805
48993951 8806 expand_dependencies (objfile);
aaa75496 8807
b83470bf
TT
8808 dw2_do_instantiate_symtab (per_cu_data, false);
8809 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8810}
8811
dee91e82
DE
8812/* Trivial hash function for die_info: the hash value of a DIE
8813 is its offset in .debug_info for this objfile. */
10b3939b 8814
dee91e82
DE
8815static hashval_t
8816die_hash (const void *item)
10b3939b 8817{
9a3c8263 8818 const struct die_info *die = (const struct die_info *) item;
6502dd73 8819
9c541725 8820 return to_underlying (die->sect_off);
dee91e82 8821}
63d06c5c 8822
dee91e82
DE
8823/* Trivial comparison function for die_info structures: two DIEs
8824 are equal if they have the same offset. */
98bfdba5 8825
dee91e82
DE
8826static int
8827die_eq (const void *item_lhs, const void *item_rhs)
8828{
9a3c8263
SM
8829 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8830 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8831
9c541725 8832 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8833}
c906108c 8834
c0ab21c2 8835/* Load the DIEs associated with PER_CU into memory. */
c906108c 8836
dee91e82 8837static void
c0ab21c2
TT
8838load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8839 bool skip_partial,
8840 enum language pretend_language)
dee91e82 8841{
c0ab21c2
TT
8842 gdb_assert (! this_cu->is_debug_types);
8843
6751ebae 8844 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8845 if (reader.dummy_p)
8846 return;
8847
8848 struct dwarf2_cu *cu = reader.cu;
8849 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8850
dee91e82
DE
8851 gdb_assert (cu->die_hash == NULL);
8852 cu->die_hash =
8853 htab_create_alloc_ex (cu->header.length / 12,
8854 die_hash,
8855 die_eq,
8856 NULL,
8857 &cu->comp_unit_obstack,
8858 hashtab_obstack_allocate,
8859 dummy_obstack_deallocate);
e142c38c 8860
3e225074 8861 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8862 reader.comp_unit_die->child
8863 = read_die_and_siblings (&reader, reader.info_ptr,
8864 &info_ptr, reader.comp_unit_die);
8865 cu->dies = reader.comp_unit_die;
dee91e82 8866 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8867
8868 /* We try not to read any attributes in this function, because not
9cdd5dbd 8869 all CUs needed for references have been loaded yet, and symbol
10b3939b 8870 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8871 or we won't be able to build types correctly.
8872 Similarly, if we do not read the producer, we can not apply
8873 producer-specific interpretation. */
c0ab21c2 8874 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8875
8876 reader.keep ();
10b3939b
DJ
8877}
8878
3da10d80
KS
8879/* Add a DIE to the delayed physname list. */
8880
8881static void
8882add_to_method_list (struct type *type, int fnfield_index, int index,
8883 const char *name, struct die_info *die,
8884 struct dwarf2_cu *cu)
8885{
8886 struct delayed_method_info mi;
8887 mi.type = type;
8888 mi.fnfield_index = fnfield_index;
8889 mi.index = index;
8890 mi.name = name;
8891 mi.die = die;
c89b44cd 8892 cu->method_list.push_back (mi);
3da10d80
KS
8893}
8894
3693fdb3
PA
8895/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
8896 "const" / "volatile". If so, decrements LEN by the length of the
8897 modifier and return true. Otherwise return false. */
8898
8899template<size_t N>
8900static bool
8901check_modifier (const char *physname, size_t &len, const char (&mod)[N])
8902{
8903 size_t mod_len = sizeof (mod) - 1;
8904 if (len > mod_len && startswith (physname + (len - mod_len), mod))
8905 {
8906 len -= mod_len;
8907 return true;
8908 }
8909 return false;
8910}
8911
3da10d80
KS
8912/* Compute the physnames of any methods on the CU's method list.
8913
8914 The computation of method physnames is delayed in order to avoid the
8915 (bad) condition that one of the method's formal parameters is of an as yet
8916 incomplete type. */
8917
8918static void
8919compute_delayed_physnames (struct dwarf2_cu *cu)
8920{
3693fdb3 8921 /* Only C++ delays computing physnames. */
c89b44cd 8922 if (cu->method_list.empty ())
3693fdb3
PA
8923 return;
8924 gdb_assert (cu->language == language_cplus);
8925
52941706 8926 for (const delayed_method_info &mi : cu->method_list)
3da10d80 8927 {
1d06ead6 8928 const char *physname;
3da10d80 8929 struct fn_fieldlist *fn_flp
c89b44cd
TT
8930 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
8931 physname = dwarf2_physname (mi.name, mi.die, cu);
8932 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 8933 = physname ? physname : "";
3693fdb3
PA
8934
8935 /* Since there's no tag to indicate whether a method is a
8936 const/volatile overload, extract that information out of the
8937 demangled name. */
8938 if (physname != NULL)
8939 {
8940 size_t len = strlen (physname);
8941
8942 while (1)
8943 {
8944 if (physname[len] == ')') /* shortcut */
8945 break;
8946 else if (check_modifier (physname, len, " const"))
c89b44cd 8947 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 8948 else if (check_modifier (physname, len, " volatile"))
c89b44cd 8949 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
8950 else
8951 break;
8952 }
8953 }
3da10d80 8954 }
c89b44cd
TT
8955
8956 /* The list is no longer needed. */
8957 cu->method_list.clear ();
3da10d80
KS
8958}
8959
a766d390
DE
8960/* Go objects should be embedded in a DW_TAG_module DIE,
8961 and it's not clear if/how imported objects will appear.
8962 To keep Go support simple until that's worked out,
8963 go back through what we've read and create something usable.
8964 We could do this while processing each DIE, and feels kinda cleaner,
8965 but that way is more invasive.
8966 This is to, for example, allow the user to type "p var" or "b main"
8967 without having to specify the package name, and allow lookups
8968 of module.object to work in contexts that use the expression
8969 parser. */
8970
8971static void
8972fixup_go_packaging (struct dwarf2_cu *cu)
8973{
421d1616 8974 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
8975 struct pending *list;
8976 int i;
8977
c24bdb02 8978 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
8979 list != NULL;
8980 list = list->next)
a766d390
DE
8981 {
8982 for (i = 0; i < list->nsyms; ++i)
8983 {
8984 struct symbol *sym = list->symbol[i];
8985
c1b5c1eb 8986 if (sym->language () == language_go
a766d390
DE
8987 && SYMBOL_CLASS (sym) == LOC_BLOCK)
8988 {
421d1616
TT
8989 gdb::unique_xmalloc_ptr<char> this_package_name
8990 (go_symbol_package_name (sym));
a766d390
DE
8991
8992 if (this_package_name == NULL)
8993 continue;
8994 if (package_name == NULL)
421d1616 8995 package_name = std::move (this_package_name);
a766d390
DE
8996 else
8997 {
518817b3
SM
8998 struct objfile *objfile
8999 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9000 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9001 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9002 (symbol_symtab (sym) != NULL
9003 ? symtab_to_filename_for_display
9004 (symbol_symtab (sym))
e3b94546 9005 : objfile_name (objfile)),
421d1616 9006 this_package_name.get (), package_name.get ());
a766d390
DE
9007 }
9008 }
9009 }
9010 }
9011
9012 if (package_name != NULL)
9013 {
518817b3 9014 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9015 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9016 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9017 saved_package_name);
a766d390
DE
9018 struct symbol *sym;
9019
e623cf5d 9020 sym = allocate_symbol (objfile);
d3ecddab 9021 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9022 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9023 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9024 e.g., "main" finds the "main" module and not C's main(). */
9025 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9026 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9027 SYMBOL_TYPE (sym) = type;
9028
c24bdb02 9029 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9030 }
9031}
9032
c9317f21
TT
9033/* Allocate a fully-qualified name consisting of the two parts on the
9034 obstack. */
9035
9036static const char *
9037rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9038{
9039 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9040}
9041
9042/* A helper that allocates a struct discriminant_info to attach to a
9043 union type. */
9044
9045static struct discriminant_info *
9046alloc_discriminant_info (struct type *type, int discriminant_index,
9047 int default_index)
9048{
9049 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
c7b15a66
TT
9050 gdb_assert (discriminant_index == -1
9051 || (discriminant_index >= 0
9052 && discriminant_index < TYPE_NFIELDS (type)));
c9317f21 9053 gdb_assert (default_index == -1
c7b15a66 9054 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21
TT
9055
9056 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
9057
9058 struct discriminant_info *disc
9059 = ((struct discriminant_info *)
9060 TYPE_ZALLOC (type,
9061 offsetof (struct discriminant_info, discriminants)
9062 + TYPE_NFIELDS (type) * sizeof (disc->discriminants[0])));
9063 disc->default_index = default_index;
9064 disc->discriminant_index = discriminant_index;
9065
9066 struct dynamic_prop prop;
9067 prop.kind = PROP_UNDEFINED;
9068 prop.data.baton = disc;
9069
9070 add_dyn_prop (DYN_PROP_DISCRIMINATED, prop, type);
9071
9072 return disc;
9073}
9074
9075/* Some versions of rustc emitted enums in an unusual way.
9076
9077 Ordinary enums were emitted as unions. The first element of each
9078 structure in the union was named "RUST$ENUM$DISR". This element
9079 held the discriminant.
9080
9081 These versions of Rust also implemented the "non-zero"
9082 optimization. When the enum had two values, and one is empty and
9083 the other holds a pointer that cannot be zero, the pointer is used
9084 as the discriminant, with a zero value meaning the empty variant.
9085 Here, the union's first member is of the form
9086 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9087 where the fieldnos are the indices of the fields that should be
9088 traversed in order to find the field (which may be several fields deep)
9089 and the variantname is the name of the variant of the case when the
9090 field is zero.
9091
9092 This function recognizes whether TYPE is of one of these forms,
9093 and, if so, smashes it to be a variant type. */
9094
9095static void
9096quirk_rust_enum (struct type *type, struct objfile *objfile)
9097{
9098 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9099
9100 /* We don't need to deal with empty enums. */
9101 if (TYPE_NFIELDS (type) == 0)
9102 return;
9103
9104#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9105 if (TYPE_NFIELDS (type) == 1
9106 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9107 {
9108 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9109
9110 /* Decode the field name to find the offset of the
9111 discriminant. */
9112 ULONGEST bit_offset = 0;
9113 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9114 while (name[0] >= '0' && name[0] <= '9')
9115 {
9116 char *tail;
9117 unsigned long index = strtoul (name, &tail, 10);
9118 name = tail;
9119 if (*name != '$'
9120 || index >= TYPE_NFIELDS (field_type)
9121 || (TYPE_FIELD_LOC_KIND (field_type, index)
9122 != FIELD_LOC_KIND_BITPOS))
9123 {
b98664d3 9124 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9125 "[in module %s]"),
9126 TYPE_FIELD_NAME (type, 0),
9127 objfile_name (objfile));
9128 return;
9129 }
9130 ++name;
9131
9132 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9133 field_type = TYPE_FIELD_TYPE (field_type, index);
9134 }
9135
9136 /* Make a union to hold the variants. */
9137 struct type *union_type = alloc_type (objfile);
9138 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9139 TYPE_NFIELDS (union_type) = 3;
9140 TYPE_FIELDS (union_type)
9141 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
9142 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9143 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9144
9145 /* Put the discriminant must at index 0. */
9146 TYPE_FIELD_TYPE (union_type, 0) = field_type;
9147 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9148 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9149 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 0), bit_offset);
9150
9151 /* The order of fields doesn't really matter, so put the real
9152 field at index 1 and the data-less field at index 2. */
9153 struct discriminant_info *disc
9154 = alloc_discriminant_info (union_type, 0, 1);
9155 TYPE_FIELD (union_type, 1) = TYPE_FIELD (type, 0);
9156 TYPE_FIELD_NAME (union_type, 1)
9157 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1)));
9158 TYPE_NAME (TYPE_FIELD_TYPE (union_type, 1))
9159 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9160 TYPE_FIELD_NAME (union_type, 1));
9161
9162 const char *dataless_name
9163 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9164 name);
9165 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9166 dataless_name);
9167 TYPE_FIELD_TYPE (union_type, 2) = dataless_type;
9168 /* NAME points into the original discriminant name, which
9169 already has the correct lifetime. */
9170 TYPE_FIELD_NAME (union_type, 2) = name;
9171 SET_FIELD_BITPOS (TYPE_FIELD (union_type, 2), 0);
9172 disc->discriminants[2] = 0;
9173
9174 /* Smash this type to be a structure type. We have to do this
9175 because the type has already been recorded. */
9176 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9177 TYPE_NFIELDS (type) = 1;
9178 TYPE_FIELDS (type)
9179 = (struct field *) TYPE_ZALLOC (type, sizeof (struct field));
9180
9181 /* Install the variant part. */
9182 TYPE_FIELD_TYPE (type, 0) = union_type;
9183 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9184 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9185 }
77c2dba3
TT
9186 /* A union with a single anonymous field is probably an old-style
9187 univariant enum. */
9188 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9189 {
c9317f21
TT
9190 /* Smash this type to be a structure type. We have to do this
9191 because the type has already been recorded. */
9192 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9193
9194 /* Make a union to hold the variants. */
9195 struct type *union_type = alloc_type (objfile);
9196 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9197 TYPE_NFIELDS (union_type) = TYPE_NFIELDS (type);
9198 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9199 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9200 TYPE_FIELDS (union_type) = TYPE_FIELDS (type);
9201
9202 struct type *field_type = TYPE_FIELD_TYPE (union_type, 0);
9203 const char *variant_name
9204 = rust_last_path_segment (TYPE_NAME (field_type));
9205 TYPE_FIELD_NAME (union_type, 0) = variant_name;
9206 TYPE_NAME (field_type)
9207 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9208 TYPE_NAME (type), variant_name);
c9317f21
TT
9209
9210 /* Install the union in the outer struct type. */
9211 TYPE_NFIELDS (type) = 1;
9212 TYPE_FIELDS (type)
9213 = (struct field *) TYPE_ZALLOC (union_type, sizeof (struct field));
9214 TYPE_FIELD_TYPE (type, 0) = union_type;
9215 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9216 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9217
9218 alloc_discriminant_info (union_type, -1, 0);
9219 }
9220 else
9221 {
9222 struct type *disr_type = nullptr;
9223 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9224 {
9225 disr_type = TYPE_FIELD_TYPE (type, i);
9226
a037790e
TT
9227 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9228 {
9229 /* All fields of a true enum will be structs. */
9230 return;
9231 }
9232 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9233 {
9234 /* Could be data-less variant, so keep going. */
a037790e 9235 disr_type = nullptr;
c9317f21
TT
9236 }
9237 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9238 "RUST$ENUM$DISR") != 0)
9239 {
9240 /* Not a Rust enum. */
9241 return;
9242 }
9243 else
9244 {
9245 /* Found one. */
9246 break;
9247 }
9248 }
9249
9250 /* If we got here without a discriminant, then it's probably
9251 just a union. */
9252 if (disr_type == nullptr)
9253 return;
9254
9255 /* Smash this type to be a structure type. We have to do this
9256 because the type has already been recorded. */
9257 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9258
9259 /* Make a union to hold the variants. */
9260 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9261 struct type *union_type = alloc_type (objfile);
9262 TYPE_CODE (union_type) = TYPE_CODE_UNION;
9263 TYPE_NFIELDS (union_type) = 1 + TYPE_NFIELDS (type);
9264 TYPE_LENGTH (union_type) = TYPE_LENGTH (type);
2b4424c3 9265 set_type_align (union_type, TYPE_RAW_ALIGN (type));
c9317f21
TT
9266 TYPE_FIELDS (union_type)
9267 = (struct field *) TYPE_ZALLOC (union_type,
9268 (TYPE_NFIELDS (union_type)
9269 * sizeof (struct field)));
9270
9271 memcpy (TYPE_FIELDS (union_type) + 1, TYPE_FIELDS (type),
9272 TYPE_NFIELDS (type) * sizeof (struct field));
9273
9274 /* Install the discriminant at index 0 in the union. */
9275 TYPE_FIELD (union_type, 0) = *disr_field;
9276 TYPE_FIELD_ARTIFICIAL (union_type, 0) = 1;
9277 TYPE_FIELD_NAME (union_type, 0) = "<<discriminant>>";
9278
9279 /* Install the union in the outer struct type. */
9280 TYPE_FIELD_TYPE (type, 0) = union_type;
9281 TYPE_FIELD_NAME (type, 0) = "<<variants>>";
9282 TYPE_NFIELDS (type) = 1;
9283
9284 /* Set the size and offset of the union type. */
9285 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), 0);
9286
9287 /* We need a way to find the correct discriminant given a
9288 variant name. For convenience we build a map here. */
9289 struct type *enum_type = FIELD_TYPE (*disr_field);
9290 std::unordered_map<std::string, ULONGEST> discriminant_map;
9291 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9292 {
9293 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9294 {
9295 const char *name
9296 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9297 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9298 }
9299 }
9300
9301 int n_fields = TYPE_NFIELDS (union_type);
9302 struct discriminant_info *disc
9303 = alloc_discriminant_info (union_type, 0, -1);
9304 /* Skip the discriminant here. */
9305 for (int i = 1; i < n_fields; ++i)
9306 {
9307 /* Find the final word in the name of this variant's type.
9308 That name can be used to look up the correct
9309 discriminant. */
9310 const char *variant_name
9311 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (union_type,
9312 i)));
9313
9314 auto iter = discriminant_map.find (variant_name);
9315 if (iter != discriminant_map.end ())
9316 disc->discriminants[i] = iter->second;
9317
bedda9ac 9318 /* Remove the discriminant field, if it exists. */
c9317f21 9319 struct type *sub_type = TYPE_FIELD_TYPE (union_type, i);
bedda9ac
TT
9320 if (TYPE_NFIELDS (sub_type) > 0)
9321 {
9322 --TYPE_NFIELDS (sub_type);
9323 ++TYPE_FIELDS (sub_type);
9324 }
c9317f21
TT
9325 TYPE_FIELD_NAME (union_type, i) = variant_name;
9326 TYPE_NAME (sub_type)
9327 = rust_fully_qualify (&objfile->objfile_obstack,
9328 TYPE_NAME (type), variant_name);
9329 }
9330 }
9331}
9332
9333/* Rewrite some Rust unions to be structures with variants parts. */
9334
9335static void
9336rust_union_quirks (struct dwarf2_cu *cu)
9337{
9338 gdb_assert (cu->language == language_rust);
52941706
SM
9339 for (type *type_ : cu->rust_unions)
9340 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9341 /* We don't need this any more. */
9342 cu->rust_unions.clear ();
c9317f21
TT
9343}
9344
95554aad
TT
9345/* Return the symtab for PER_CU. This works properly regardless of
9346 whether we're using the index or psymtabs. */
9347
43f3e411
DE
9348static struct compunit_symtab *
9349get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9350{
ed2dc618 9351 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9352 ? per_cu->v.quick->compunit_symtab
9353 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9354}
9355
9356/* A helper function for computing the list of all symbol tables
9357 included by PER_CU. */
9358
9359static void
4c39bc03 9360recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9361 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9362 struct dwarf2_per_cu_data *per_cu,
43f3e411 9363 struct compunit_symtab *immediate_parent)
95554aad
TT
9364{
9365 void **slot;
43f3e411 9366 struct compunit_symtab *cust;
95554aad
TT
9367
9368 slot = htab_find_slot (all_children, per_cu, INSERT);
9369 if (*slot != NULL)
9370 {
9371 /* This inclusion and its children have been processed. */
9372 return;
9373 }
9374
9375 *slot = per_cu;
9376 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9377 cust = get_compunit_symtab (per_cu);
9378 if (cust != NULL)
ec94af83
DE
9379 {
9380 /* If this is a type unit only add its symbol table if we haven't
9381 seen it yet (type unit per_cu's can share symtabs). */
9382 if (per_cu->is_debug_types)
9383 {
43f3e411 9384 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9385 if (*slot == NULL)
9386 {
43f3e411 9387 *slot = cust;
4c39bc03 9388 result->push_back (cust);
43f3e411
DE
9389 if (cust->user == NULL)
9390 cust->user = immediate_parent;
ec94af83
DE
9391 }
9392 }
9393 else
f9125b6c 9394 {
4c39bc03 9395 result->push_back (cust);
43f3e411
DE
9396 if (cust->user == NULL)
9397 cust->user = immediate_parent;
f9125b6c 9398 }
ec94af83 9399 }
95554aad 9400
ae640021
AB
9401 if (!per_cu->imported_symtabs_empty ())
9402 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9403 {
9404 recursively_compute_inclusions (result, all_children,
9405 all_type_symtabs, ptr, cust);
9406 }
95554aad
TT
9407}
9408
43f3e411 9409/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9410 PER_CU. */
9411
9412static void
43f3e411 9413compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9414{
f4dc4d17
DE
9415 gdb_assert (! per_cu->is_debug_types);
9416
ae640021 9417 if (!per_cu->imported_symtabs_empty ())
95554aad 9418 {
ae640021 9419 int len;
4c39bc03 9420 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9421 htab_t all_children, all_type_symtabs;
43f3e411 9422 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9423
9424 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9425 if (cust == NULL)
95554aad
TT
9426 return;
9427
9428 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9429 NULL, xcalloc, xfree);
ec94af83
DE
9430 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9431 NULL, xcalloc, xfree);
95554aad 9432
ae640021 9433 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9434 {
9435 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9436 all_type_symtabs, ptr, cust);
ec94af83 9437 }
95554aad 9438
ec94af83 9439 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9440 len = result_symtabs.size ();
43f3e411 9441 cust->includes
ed2dc618 9442 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9443 struct compunit_symtab *, len + 1);
4c39bc03
TT
9444 memcpy (cust->includes, result_symtabs.data (),
9445 len * sizeof (compunit_symtab *));
43f3e411 9446 cust->includes[len] = NULL;
95554aad 9447
95554aad 9448 htab_delete (all_children);
ec94af83 9449 htab_delete (all_type_symtabs);
95554aad
TT
9450 }
9451}
9452
9453/* Compute the 'includes' field for the symtabs of all the CUs we just
9454 read. */
9455
9456static void
ed2dc618 9457process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9458{
71b73764 9459 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9460 {
9461 if (! iter->is_debug_types)
43f3e411 9462 compute_compunit_symtab_includes (iter);
f4dc4d17 9463 }
95554aad 9464
c5d0225d 9465 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9466}
9467
9cdd5dbd 9468/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9469 already been loaded into memory. */
9470
9471static void
95554aad
TT
9472process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9473 enum language pretend_language)
10b3939b 9474{
10b3939b 9475 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9476 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9477 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 9478 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10b3939b 9479 CORE_ADDR lowpc, highpc;
43f3e411 9480 struct compunit_symtab *cust;
10b3939b 9481 CORE_ADDR baseaddr;
4359dff1 9482 struct block *static_block;
3e29f34a 9483 CORE_ADDR addr;
10b3939b 9484
b3b3bada 9485 baseaddr = objfile->text_section_offset ();
10b3939b 9486
c89b44cd
TT
9487 /* Clear the list here in case something was left over. */
9488 cu->method_list.clear ();
10b3939b 9489
95554aad
TT
9490 cu->language = pretend_language;
9491 cu->language_defn = language_def (cu->language);
9492
c906108c 9493 /* Do line number decoding in read_file_scope () */
10b3939b 9494 process_die (cu->dies, cu);
c906108c 9495
a766d390
DE
9496 /* For now fudge the Go package. */
9497 if (cu->language == language_go)
9498 fixup_go_packaging (cu);
9499
5f48f8f3 9500 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9501 should be complete, and it should now be safe to compute all of the
9502 physnames. */
9503 compute_delayed_physnames (cu);
3da10d80 9504
c9317f21
TT
9505 if (cu->language == language_rust)
9506 rust_union_quirks (cu);
9507
fae299cd
DC
9508 /* Some compilers don't define a DW_AT_high_pc attribute for the
9509 compilation unit. If the DW_AT_high_pc is missing, synthesize
9510 it, by scanning the DIE's below the compilation unit. */
10b3939b 9511 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9512
3e29f34a 9513 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9514 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9515
9516 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9517 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9518 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9519 addrmap to help ensure it has an accurate map of pc values belonging to
9520 this comp unit. */
9521 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9522
c24bdb02 9523 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9524 SECT_OFF_TEXT (objfile),
9525 0);
c906108c 9526
43f3e411 9527 if (cust != NULL)
c906108c 9528 {
df15bd07 9529 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9530
8be455d7
JK
9531 /* Set symtab language to language from DW_AT_language. If the
9532 compilation is from a C file generated by language preprocessors, do
9533 not set the language if it was already deduced by start_subfile. */
43f3e411 9534 if (!(cu->language == language_c
40e3ad0e 9535 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9536 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9537
9538 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9539 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9540 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9541 there were bugs in prologue debug info, fixed later in GCC-4.5
9542 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9543
9544 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9545 needed, it would be wrong due to missing DW_AT_producer there.
9546
9547 Still one can confuse GDB by using non-standard GCC compilation
9548 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9549 */
ab260dad 9550 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9551 cust->locations_valid = 1;
e0d00bc7
JK
9552
9553 if (gcc_4_minor >= 5)
43f3e411 9554 cust->epilogue_unwind_valid = 1;
96408a79 9555
43f3e411 9556 cust->call_site_htab = cu->call_site_htab;
c906108c 9557 }
9291a0cd
TT
9558
9559 if (dwarf2_per_objfile->using_index)
43f3e411 9560 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9561 else
9562 {
891813be 9563 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9564 pst->compunit_symtab = cust;
6d94535f 9565 pst->readin = true;
9291a0cd 9566 }
c906108c 9567
95554aad 9568 /* Push it for inclusion processing later. */
c5d0225d 9569 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9570
9571 /* Not needed any more. */
c24bdb02 9572 cu->reset_builder ();
f4dc4d17 9573}
45cfd468 9574
f4dc4d17
DE
9575/* Generate full symbol information for type unit PER_CU, whose DIEs have
9576 already been loaded into memory. */
9577
9578static void
9579process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9580 enum language pretend_language)
9581{
9582 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9583 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9584 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9585 struct compunit_symtab *cust;
0186c6a7
DE
9586 struct signatured_type *sig_type;
9587
9588 gdb_assert (per_cu->is_debug_types);
9589 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9590
c89b44cd
TT
9591 /* Clear the list here in case something was left over. */
9592 cu->method_list.clear ();
f4dc4d17 9593
f4dc4d17
DE
9594 cu->language = pretend_language;
9595 cu->language_defn = language_def (cu->language);
9596
9597 /* The symbol tables are set up in read_type_unit_scope. */
9598 process_die (cu->dies, cu);
9599
9600 /* For now fudge the Go package. */
9601 if (cu->language == language_go)
9602 fixup_go_packaging (cu);
9603
5f48f8f3 9604 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9605 should be complete, and it should now be safe to compute all of the
9606 physnames. */
9607 compute_delayed_physnames (cu);
f4dc4d17 9608
c9317f21
TT
9609 if (cu->language == language_rust)
9610 rust_union_quirks (cu);
9611
f4dc4d17
DE
9612 /* TUs share symbol tables.
9613 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9614 of it with end_expandable_symtab. Otherwise, complete the addition of
9615 this TU's symbols to the existing symtab. */
43f3e411 9616 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9617 {
c24bdb02
KS
9618 buildsym_compunit *builder = cu->get_builder ();
9619 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9620 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9621
43f3e411 9622 if (cust != NULL)
f4dc4d17
DE
9623 {
9624 /* Set symtab language to language from DW_AT_language. If the
9625 compilation is from a C file generated by language preprocessors,
9626 do not set the language if it was already deduced by
9627 start_subfile. */
43f3e411
DE
9628 if (!(cu->language == language_c
9629 && COMPUNIT_FILETABS (cust)->language != language_c))
9630 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9631 }
9632 }
9633 else
9634 {
c24bdb02 9635 cu->get_builder ()->augment_type_symtab ();
43f3e411 9636 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9637 }
9638
9639 if (dwarf2_per_objfile->using_index)
43f3e411 9640 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9641 else
9642 {
891813be 9643 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9644 pst->compunit_symtab = cust;
6d94535f 9645 pst->readin = true;
45cfd468 9646 }
804d2729
TT
9647
9648 /* Not needed any more. */
c24bdb02 9649 cu->reset_builder ();
c906108c
SS
9650}
9651
95554aad
TT
9652/* Process an imported unit DIE. */
9653
9654static void
9655process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9656{
9657 struct attribute *attr;
9658
f4dc4d17
DE
9659 /* For now we don't handle imported units in type units. */
9660 if (cu->per_cu->is_debug_types)
9661 {
9662 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9663 " supported in type units [in module %s]"),
518817b3 9664 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9665 }
9666
95554aad
TT
9667 attr = dwarf2_attr (die, DW_AT_import, cu);
9668 if (attr != NULL)
9669 {
0826b30a 9670 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9671 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9672 dwarf2_per_cu_data *per_cu
e3b94546 9673 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9674 cu->per_cu->dwarf2_per_objfile);
95554aad 9675
58990295
TV
9676 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9677 into another compilation unit, at root level. Regard this as a hint,
9678 and ignore it. */
9679 if (die->parent && die->parent->parent == NULL
9680 && per_cu->unit_type == DW_UT_compile
9681 && per_cu->lang == language_cplus)
9682 return;
9683
69d751e3 9684 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9685 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9686 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9687
ae640021 9688 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9689 }
9690}
9691
4c8aa72d
PA
9692/* RAII object that represents a process_die scope: i.e.,
9693 starts/finishes processing a DIE. */
9694class process_die_scope
adde2bff 9695{
4c8aa72d
PA
9696public:
9697 process_die_scope (die_info *die, dwarf2_cu *cu)
9698 : m_die (die), m_cu (cu)
9699 {
9700 /* We should only be processing DIEs not already in process. */
9701 gdb_assert (!m_die->in_process);
9702 m_die->in_process = true;
9703 }
8c3cb9fa 9704
4c8aa72d
PA
9705 ~process_die_scope ()
9706 {
9707 m_die->in_process = false;
9708
9709 /* If we're done processing the DIE for the CU that owns the line
9710 header, we don't need the line header anymore. */
9711 if (m_cu->line_header_die_owner == m_die)
9712 {
9713 delete m_cu->line_header;
9714 m_cu->line_header = NULL;
9715 m_cu->line_header_die_owner = NULL;
9716 }
9717 }
9718
9719private:
9720 die_info *m_die;
9721 dwarf2_cu *m_cu;
9722};
adde2bff 9723
c906108c
SS
9724/* Process a die and its children. */
9725
9726static void
e7c27a73 9727process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9728{
4c8aa72d 9729 process_die_scope scope (die, cu);
adde2bff 9730
c906108c
SS
9731 switch (die->tag)
9732 {
9733 case DW_TAG_padding:
9734 break;
9735 case DW_TAG_compile_unit:
95554aad 9736 case DW_TAG_partial_unit:
e7c27a73 9737 read_file_scope (die, cu);
c906108c 9738 break;
348e048f
DE
9739 case DW_TAG_type_unit:
9740 read_type_unit_scope (die, cu);
9741 break;
c906108c 9742 case DW_TAG_subprogram:
0a4b0913
AB
9743 /* Nested subprograms in Fortran get a prefix. */
9744 if (cu->language == language_fortran
9745 && die->parent != NULL
9746 && die->parent->tag == DW_TAG_subprogram)
9747 cu->processing_has_namespace_info = true;
9748 /* Fall through. */
c906108c 9749 case DW_TAG_inlined_subroutine:
edb3359d 9750 read_func_scope (die, cu);
c906108c
SS
9751 break;
9752 case DW_TAG_lexical_block:
14898363
L
9753 case DW_TAG_try_block:
9754 case DW_TAG_catch_block:
e7c27a73 9755 read_lexical_block_scope (die, cu);
c906108c 9756 break;
216f72a1 9757 case DW_TAG_call_site:
96408a79
SA
9758 case DW_TAG_GNU_call_site:
9759 read_call_site_scope (die, cu);
9760 break;
c906108c 9761 case DW_TAG_class_type:
680b30c7 9762 case DW_TAG_interface_type:
c906108c
SS
9763 case DW_TAG_structure_type:
9764 case DW_TAG_union_type:
134d01f1 9765 process_structure_scope (die, cu);
c906108c
SS
9766 break;
9767 case DW_TAG_enumeration_type:
134d01f1 9768 process_enumeration_scope (die, cu);
c906108c 9769 break;
134d01f1 9770
f792889a
DJ
9771 /* These dies have a type, but processing them does not create
9772 a symbol or recurse to process the children. Therefore we can
9773 read them on-demand through read_type_die. */
c906108c 9774 case DW_TAG_subroutine_type:
72019c9c 9775 case DW_TAG_set_type:
c906108c 9776 case DW_TAG_array_type:
c906108c 9777 case DW_TAG_pointer_type:
c906108c 9778 case DW_TAG_ptr_to_member_type:
c906108c 9779 case DW_TAG_reference_type:
4297a3f0 9780 case DW_TAG_rvalue_reference_type:
c906108c 9781 case DW_TAG_string_type:
c906108c 9782 break;
134d01f1 9783
c906108c 9784 case DW_TAG_base_type:
a02abb62 9785 case DW_TAG_subrange_type:
cb249c71 9786 case DW_TAG_typedef:
134d01f1
DJ
9787 /* Add a typedef symbol for the type definition, if it has a
9788 DW_AT_name. */
f792889a 9789 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9790 break;
c906108c 9791 case DW_TAG_common_block:
e7c27a73 9792 read_common_block (die, cu);
c906108c
SS
9793 break;
9794 case DW_TAG_common_inclusion:
9795 break;
d9fa45fe 9796 case DW_TAG_namespace:
9068261f 9797 cu->processing_has_namespace_info = true;
e7c27a73 9798 read_namespace (die, cu);
d9fa45fe 9799 break;
5d7cb8df 9800 case DW_TAG_module:
9068261f 9801 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9802 read_module (die, cu);
9803 break;
d9fa45fe 9804 case DW_TAG_imported_declaration:
9068261f 9805 cu->processing_has_namespace_info = true;
74921315
KS
9806 if (read_namespace_alias (die, cu))
9807 break;
86a73007
TT
9808 /* The declaration is not a global namespace alias. */
9809 /* Fall through. */
d9fa45fe 9810 case DW_TAG_imported_module:
9068261f 9811 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9812 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9813 || cu->language != language_fortran))
b98664d3 9814 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9815 dwarf_tag_name (die->tag));
9816 read_import_statement (die, cu);
d9fa45fe 9817 break;
95554aad
TT
9818
9819 case DW_TAG_imported_unit:
9820 process_imported_unit_die (die, cu);
9821 break;
9822
71a3c369
TT
9823 case DW_TAG_variable:
9824 read_variable (die, cu);
9825 break;
9826
c906108c 9827 default:
e7c27a73 9828 new_symbol (die, NULL, cu);
c906108c
SS
9829 break;
9830 }
9831}
ca69b9e6
DE
9832\f
9833/* DWARF name computation. */
c906108c 9834
94af9270
KS
9835/* A helper function for dwarf2_compute_name which determines whether DIE
9836 needs to have the name of the scope prepended to the name listed in the
9837 die. */
9838
9839static int
9840die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9841{
1c809c68
TT
9842 struct attribute *attr;
9843
94af9270
KS
9844 switch (die->tag)
9845 {
9846 case DW_TAG_namespace:
9847 case DW_TAG_typedef:
9848 case DW_TAG_class_type:
9849 case DW_TAG_interface_type:
9850 case DW_TAG_structure_type:
9851 case DW_TAG_union_type:
9852 case DW_TAG_enumeration_type:
9853 case DW_TAG_enumerator:
9854 case DW_TAG_subprogram:
08a76f8a 9855 case DW_TAG_inlined_subroutine:
94af9270 9856 case DW_TAG_member:
74921315 9857 case DW_TAG_imported_declaration:
94af9270
KS
9858 return 1;
9859
9860 case DW_TAG_variable:
c2b0a229 9861 case DW_TAG_constant:
94af9270
KS
9862 /* We only need to prefix "globally" visible variables. These include
9863 any variable marked with DW_AT_external or any variable that
9864 lives in a namespace. [Variables in anonymous namespaces
9865 require prefixing, but they are not DW_AT_external.] */
9866
9867 if (dwarf2_attr (die, DW_AT_specification, cu))
9868 {
9869 struct dwarf2_cu *spec_cu = cu;
9a619af0 9870
94af9270
KS
9871 return die_needs_namespace (die_specification (die, &spec_cu),
9872 spec_cu);
9873 }
9874
1c809c68 9875 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9876 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9877 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9878 return 0;
9879 /* A variable in a lexical block of some kind does not need a
9880 namespace, even though in C++ such variables may be external
9881 and have a mangled name. */
9882 if (die->parent->tag == DW_TAG_lexical_block
9883 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9884 || die->parent->tag == DW_TAG_catch_block
9885 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9886 return 0;
9887 return 1;
94af9270
KS
9888
9889 default:
9890 return 0;
9891 }
9892}
9893
73b9be8b
KS
9894/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
9895 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9896 defined for the given DIE. */
9897
9898static struct attribute *
9899dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
9900{
9901 struct attribute *attr;
9902
9903 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
9904 if (attr == NULL)
9905 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
9906
9907 return attr;
9908}
9909
9910/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
9911 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
9912 defined for the given DIE. */
9913
9914static const char *
9915dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
9916{
9917 const char *linkage_name;
9918
9919 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
9920 if (linkage_name == NULL)
9921 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
9922
9923 return linkage_name;
9924}
9925
94af9270 9926/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 9927 compute the physname for the object, which include a method's:
9c37b5ae 9928 - formal parameters (C++),
a766d390 9929 - receiver type (Go),
a766d390
DE
9930
9931 The term "physname" is a bit confusing.
9932 For C++, for example, it is the demangled name.
9933 For Go, for example, it's the mangled name.
94af9270 9934
af6b7be1
JB
9935 For Ada, return the DIE's linkage name rather than the fully qualified
9936 name. PHYSNAME is ignored..
9937
94af9270
KS
9938 The result is allocated on the objfile_obstack and canonicalized. */
9939
9940static const char *
15d034d0
TT
9941dwarf2_compute_name (const char *name,
9942 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
9943 int physname)
9944{
518817b3 9945 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 9946
94af9270
KS
9947 if (name == NULL)
9948 name = dwarf2_name (die, cu);
9949
2ee7123e
DE
9950 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
9951 but otherwise compute it by typename_concat inside GDB.
9952 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 9953 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 9954 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
9955 will set the demangled name to the result of dwarf2_full_name, and it is
9956 the demangled name that GDB uses if it exists. */
f55ee35c
JK
9957 if (cu->language == language_ada
9958 || (cu->language == language_fortran && physname))
9959 {
9960 /* For Ada unit, we prefer the linkage name over the name, as
9961 the former contains the exported name, which the user expects
9962 to be able to reference. Ideally, we want the user to be able
9963 to reference this entity using either natural or linkage name,
9964 but we haven't started looking at this enhancement yet. */
73b9be8b 9965 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 9966
2ee7123e
DE
9967 if (linkage_name != NULL)
9968 return linkage_name;
f55ee35c
JK
9969 }
9970
94af9270
KS
9971 /* These are the only languages we know how to qualify names in. */
9972 if (name != NULL
9c37b5ae 9973 && (cu->language == language_cplus
c44af4eb
TT
9974 || cu->language == language_fortran || cu->language == language_d
9975 || cu->language == language_rust))
94af9270
KS
9976 {
9977 if (die_needs_namespace (die, cu))
9978 {
0d5cff50 9979 const char *prefix;
34a68019 9980 const char *canonical_name = NULL;
94af9270 9981
d7e74731
PA
9982 string_file buf;
9983
94af9270 9984 prefix = determine_prefix (die, cu);
94af9270
KS
9985 if (*prefix != '\0')
9986 {
43816ebc
TT
9987 gdb::unique_xmalloc_ptr<char> prefixed_name
9988 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 9989
43816ebc 9990 buf.puts (prefixed_name.get ());
94af9270
KS
9991 }
9992 else
d7e74731 9993 buf.puts (name);
94af9270 9994
98bfdba5
PA
9995 /* Template parameters may be specified in the DIE's DW_AT_name, or
9996 as children with DW_TAG_template_type_param or
9997 DW_TAG_value_type_param. If the latter, add them to the name
9998 here. If the name already has template parameters, then
9999 skip this step; some versions of GCC emit both, and
10000 it is more efficient to use the pre-computed name.
10001
10002 Something to keep in mind about this process: it is very
10003 unlikely, or in some cases downright impossible, to produce
10004 something that will match the mangled name of a function.
10005 If the definition of the function has the same debug info,
10006 we should be able to match up with it anyway. But fallbacks
10007 using the minimal symbol, for instance to find a method
10008 implemented in a stripped copy of libstdc++, will not work.
10009 If we do not have debug info for the definition, we will have to
10010 match them up some other way.
10011
10012 When we do name matching there is a related problem with function
10013 templates; two instantiated function templates are allowed to
10014 differ only by their return types, which we do not add here. */
10015
10016 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10017 {
10018 struct attribute *attr;
10019 struct die_info *child;
10020 int first = 1;
10021
10022 die->building_fullname = 1;
10023
10024 for (child = die->child; child != NULL; child = child->sibling)
10025 {
10026 struct type *type;
12df843f 10027 LONGEST value;
d521ce57 10028 const gdb_byte *bytes;
98bfdba5
PA
10029 struct dwarf2_locexpr_baton *baton;
10030 struct value *v;
10031
10032 if (child->tag != DW_TAG_template_type_param
10033 && child->tag != DW_TAG_template_value_param)
10034 continue;
10035
10036 if (first)
10037 {
d7e74731 10038 buf.puts ("<");
98bfdba5
PA
10039 first = 0;
10040 }
10041 else
d7e74731 10042 buf.puts (", ");
98bfdba5
PA
10043
10044 attr = dwarf2_attr (child, DW_AT_type, cu);
10045 if (attr == NULL)
10046 {
b98664d3 10047 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10048 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10049 continue;
10050 }
10051 type = die_type (child, cu);
10052
10053 if (child->tag == DW_TAG_template_type_param)
10054 {
c1ec8cea
TT
10055 c_print_type (type, "", &buf, -1, 0, cu->language,
10056 &type_print_raw_options);
98bfdba5
PA
10057 continue;
10058 }
10059
10060 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10061 if (attr == NULL)
10062 {
b98664d3 10063 complaint (_("template parameter missing "
3e43a32a 10064 "DW_AT_const_value"));
d7e74731 10065 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10066 continue;
10067 }
10068
10069 dwarf2_const_value_attr (attr, type, name,
10070 &cu->comp_unit_obstack, cu,
10071 &value, &bytes, &baton);
10072
10073 if (TYPE_NOSIGN (type))
10074 /* GDB prints characters as NUMBER 'CHAR'. If that's
10075 changed, this can use value_print instead. */
d7e74731 10076 c_printchar (value, type, &buf);
98bfdba5
PA
10077 else
10078 {
10079 struct value_print_options opts;
10080
10081 if (baton != NULL)
10082 v = dwarf2_evaluate_loc_desc (type, NULL,
10083 baton->data,
10084 baton->size,
10085 baton->per_cu);
10086 else if (bytes != NULL)
10087 {
10088 v = allocate_value (type);
10089 memcpy (value_contents_writeable (v), bytes,
10090 TYPE_LENGTH (type));
10091 }
10092 else
10093 v = value_from_longest (type, value);
10094
3e43a32a
MS
10095 /* Specify decimal so that we do not depend on
10096 the radix. */
98bfdba5
PA
10097 get_formatted_print_options (&opts, 'd');
10098 opts.raw = 1;
d7e74731 10099 value_print (v, &buf, &opts);
98bfdba5 10100 release_value (v);
98bfdba5
PA
10101 }
10102 }
10103
10104 die->building_fullname = 0;
10105
10106 if (!first)
10107 {
10108 /* Close the argument list, with a space if necessary
10109 (nested templates). */
d7e74731
PA
10110 if (!buf.empty () && buf.string ().back () == '>')
10111 buf.puts (" >");
98bfdba5 10112 else
d7e74731 10113 buf.puts (">");
98bfdba5
PA
10114 }
10115 }
10116
9c37b5ae 10117 /* For C++ methods, append formal parameter type
94af9270 10118 information, if PHYSNAME. */
6e70227d 10119
94af9270 10120 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10121 && cu->language == language_cplus)
94af9270
KS
10122 {
10123 struct type *type = read_type_die (die, cu);
10124
d7e74731 10125 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10126 &type_print_raw_options);
94af9270 10127
9c37b5ae 10128 if (cu->language == language_cplus)
94af9270 10129 {
60430eff
DJ
10130 /* Assume that an artificial first parameter is
10131 "this", but do not crash if it is not. RealView
10132 marks unnamed (and thus unused) parameters as
10133 artificial; there is no way to differentiate
10134 the two cases. */
94af9270
KS
10135 if (TYPE_NFIELDS (type) > 0
10136 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10137 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10138 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10139 0))))
d7e74731 10140 buf.puts (" const");
94af9270
KS
10141 }
10142 }
10143
d7e74731 10144 const std::string &intermediate_name = buf.string ();
94af9270
KS
10145
10146 if (cu->language == language_cplus)
34a68019 10147 canonical_name
322a8516 10148 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10149 objfile);
34a68019
TT
10150
10151 /* If we only computed INTERMEDIATE_NAME, or if
10152 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10153 intern it. */
322a8516 10154 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10155 name = objfile->intern (intermediate_name);
34a68019
TT
10156 else
10157 name = canonical_name;
94af9270
KS
10158 }
10159 }
10160
10161 return name;
10162}
10163
0114d602
DJ
10164/* Return the fully qualified name of DIE, based on its DW_AT_name.
10165 If scope qualifiers are appropriate they will be added. The result
34a68019 10166 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10167 not have a name. NAME may either be from a previous call to
10168 dwarf2_name or NULL.
10169
9c37b5ae 10170 The output string will be canonicalized (if C++). */
0114d602
DJ
10171
10172static const char *
15d034d0 10173dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10174{
94af9270
KS
10175 return dwarf2_compute_name (name, die, cu, 0);
10176}
0114d602 10177
94af9270
KS
10178/* Construct a physname for the given DIE in CU. NAME may either be
10179 from a previous call to dwarf2_name or NULL. The result will be
10180 allocated on the objfile_objstack or NULL if the DIE does not have a
10181 name.
0114d602 10182
9c37b5ae 10183 The output string will be canonicalized (if C++). */
0114d602 10184
94af9270 10185static const char *
15d034d0 10186dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10187{
518817b3 10188 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10189 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10190 int need_copy = 1;
10191
10192 /* In this case dwarf2_compute_name is just a shortcut not building anything
10193 on its own. */
10194 if (!die_needs_namespace (die, cu))
10195 return dwarf2_compute_name (name, die, cu, 1);
10196
73b9be8b 10197 mangled = dw2_linkage_name (die, cu);
900e11f9 10198
e98c9e7c
TT
10199 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10200 See https://github.com/rust-lang/rust/issues/32925. */
10201 if (cu->language == language_rust && mangled != NULL
10202 && strchr (mangled, '{') != NULL)
10203 mangled = NULL;
10204
900e11f9
JK
10205 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10206 has computed. */
791afaa2 10207 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10208 if (mangled != NULL)
900e11f9 10209 {
900e11f9 10210
59cc4834
JB
10211 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10212 {
10213 /* Do nothing (do not demangle the symbol name). */
10214 }
10215 else if (cu->language == language_go)
a766d390 10216 {
5e2db402
TT
10217 /* This is a lie, but we already lie to the caller new_symbol.
10218 new_symbol assumes we return the mangled name.
a766d390 10219 This just undoes that lie until things are cleaned up. */
a766d390
DE
10220 }
10221 else
10222 {
0eb876f5
JB
10223 /* Use DMGL_RET_DROP for C++ template functions to suppress
10224 their return type. It is easier for GDB users to search
10225 for such functions as `name(params)' than `long name(params)'.
10226 In such case the minimal symbol names do not match the full
10227 symbol names but for template functions there is never a need
10228 to look up their definition from their declaration so
10229 the only disadvantage remains the minimal symbol variant
10230 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10231 demangled.reset (gdb_demangle (mangled,
10232 (DMGL_PARAMS | DMGL_ANSI
10233 | DMGL_RET_DROP)));
a766d390 10234 }
900e11f9 10235 if (demangled)
791afaa2 10236 canon = demangled.get ();
900e11f9
JK
10237 else
10238 {
10239 canon = mangled;
10240 need_copy = 0;
10241 }
10242 }
10243
10244 if (canon == NULL || check_physname)
10245 {
10246 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10247
10248 if (canon != NULL && strcmp (physname, canon) != 0)
10249 {
10250 /* It may not mean a bug in GDB. The compiler could also
10251 compute DW_AT_linkage_name incorrectly. But in such case
10252 GDB would need to be bug-to-bug compatible. */
10253
b98664d3 10254 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10255 "(from linkage <%s>) - DIE at %s [in module %s]"),
10256 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10257 objfile_name (objfile));
900e11f9
JK
10258
10259 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10260 is available here - over computed PHYSNAME. It is safer
10261 against both buggy GDB and buggy compilers. */
10262
10263 retval = canon;
10264 }
10265 else
10266 {
10267 retval = physname;
10268 need_copy = 0;
10269 }
10270 }
10271 else
10272 retval = canon;
10273
10274 if (need_copy)
be1e3d3e 10275 retval = objfile->intern (retval);
900e11f9 10276
900e11f9 10277 return retval;
0114d602
DJ
10278}
10279
74921315
KS
10280/* Inspect DIE in CU for a namespace alias. If one exists, record
10281 a new symbol for it.
10282
10283 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10284
10285static int
10286read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10287{
10288 struct attribute *attr;
10289
10290 /* If the die does not have a name, this is not a namespace
10291 alias. */
10292 attr = dwarf2_attr (die, DW_AT_name, cu);
10293 if (attr != NULL)
10294 {
10295 int num;
10296 struct die_info *d = die;
10297 struct dwarf2_cu *imported_cu = cu;
10298
10299 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10300 keep inspecting DIEs until we hit the underlying import. */
10301#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10302 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10303 {
10304 attr = dwarf2_attr (d, DW_AT_import, cu);
10305 if (attr == NULL)
10306 break;
10307
10308 d = follow_die_ref (d, attr, &imported_cu);
10309 if (d->tag != DW_TAG_imported_declaration)
10310 break;
10311 }
10312
10313 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10314 {
b98664d3 10315 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10316 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10317 return 0;
10318 }
10319
10320 if (attr != NULL)
10321 {
10322 struct type *type;
0826b30a 10323 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10324
9c541725 10325 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10326 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10327 {
10328 /* This declaration is a global namespace alias. Add
10329 a symbol for it whose type is the aliased namespace. */
10330 new_symbol (die, type, cu);
10331 return 1;
10332 }
10333 }
10334 }
10335
10336 return 0;
10337}
10338
22cee43f 10339/* Return the using directives repository (global or local?) to use in the
804d2729 10340 current context for CU.
22cee43f
PMR
10341
10342 For Ada, imported declarations can materialize renamings, which *may* be
10343 global. However it is impossible (for now?) in DWARF to distinguish
10344 "external" imported declarations and "static" ones. As all imported
10345 declarations seem to be static in all other languages, make them all CU-wide
10346 global only in Ada. */
10347
10348static struct using_direct **
804d2729 10349using_directives (struct dwarf2_cu *cu)
22cee43f 10350{
c24bdb02
KS
10351 if (cu->language == language_ada
10352 && cu->get_builder ()->outermost_context_p ())
10353 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10354 else
c24bdb02 10355 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10356}
10357
27aa8d6a
SW
10358/* Read the import statement specified by the given die and record it. */
10359
10360static void
10361read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10362{
518817b3 10363 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10364 struct attribute *import_attr;
32019081 10365 struct die_info *imported_die, *child_die;
de4affc9 10366 struct dwarf2_cu *imported_cu;
27aa8d6a 10367 const char *imported_name;
794684b6 10368 const char *imported_name_prefix;
13387711
SW
10369 const char *canonical_name;
10370 const char *import_alias;
10371 const char *imported_declaration = NULL;
794684b6 10372 const char *import_prefix;
eb1e02fd 10373 std::vector<const char *> excludes;
13387711 10374
27aa8d6a
SW
10375 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10376 if (import_attr == NULL)
10377 {
b98664d3 10378 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10379 dwarf_tag_name (die->tag));
10380 return;
10381 }
10382
de4affc9
CC
10383 imported_cu = cu;
10384 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10385 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10386 if (imported_name == NULL)
10387 {
10388 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10389
10390 The import in the following code:
10391 namespace A
10392 {
10393 typedef int B;
10394 }
10395
10396 int main ()
10397 {
10398 using A::B;
10399 B b;
10400 return b;
10401 }
10402
10403 ...
10404 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10405 <52> DW_AT_decl_file : 1
10406 <53> DW_AT_decl_line : 6
10407 <54> DW_AT_import : <0x75>
10408 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10409 <59> DW_AT_name : B
10410 <5b> DW_AT_decl_file : 1
10411 <5c> DW_AT_decl_line : 2
10412 <5d> DW_AT_type : <0x6e>
10413 ...
10414 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10415 <76> DW_AT_byte_size : 4
10416 <77> DW_AT_encoding : 5 (signed)
10417
10418 imports the wrong die ( 0x75 instead of 0x58 ).
10419 This case will be ignored until the gcc bug is fixed. */
10420 return;
10421 }
10422
82856980
SW
10423 /* Figure out the local name after import. */
10424 import_alias = dwarf2_name (die, cu);
27aa8d6a 10425
794684b6
SW
10426 /* Figure out where the statement is being imported to. */
10427 import_prefix = determine_prefix (die, cu);
10428
10429 /* Figure out what the scope of the imported die is and prepend it
10430 to the name of the imported die. */
de4affc9 10431 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10432
f55ee35c
JK
10433 if (imported_die->tag != DW_TAG_namespace
10434 && imported_die->tag != DW_TAG_module)
794684b6 10435 {
13387711
SW
10436 imported_declaration = imported_name;
10437 canonical_name = imported_name_prefix;
794684b6 10438 }
13387711 10439 else if (strlen (imported_name_prefix) > 0)
12aaed36 10440 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10441 imported_name_prefix,
10442 (cu->language == language_d ? "." : "::"),
10443 imported_name, (char *) NULL);
13387711
SW
10444 else
10445 canonical_name = imported_name;
794684b6 10446
32019081
JK
10447 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10448 for (child_die = die->child; child_die && child_die->tag;
436c571c 10449 child_die = child_die->sibling)
32019081
JK
10450 {
10451 /* DWARF-4: A Fortran use statement with a “rename list” may be
10452 represented by an imported module entry with an import attribute
10453 referring to the module and owned entries corresponding to those
10454 entities that are renamed as part of being imported. */
10455
10456 if (child_die->tag != DW_TAG_imported_declaration)
10457 {
b98664d3 10458 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10459 "- DIE at %s [in module %s]"),
10460 sect_offset_str (child_die->sect_off),
10461 objfile_name (objfile));
32019081
JK
10462 continue;
10463 }
10464
10465 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10466 if (import_attr == NULL)
10467 {
b98664d3 10468 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10469 dwarf_tag_name (child_die->tag));
10470 continue;
10471 }
10472
10473 imported_cu = cu;
10474 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10475 &imported_cu);
10476 imported_name = dwarf2_name (imported_die, imported_cu);
10477 if (imported_name == NULL)
10478 {
b98664d3 10479 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10480 "imported name - DIE at %s [in module %s]"),
10481 sect_offset_str (child_die->sect_off),
10482 objfile_name (objfile));
32019081
JK
10483 continue;
10484 }
10485
eb1e02fd 10486 excludes.push_back (imported_name);
32019081
JK
10487
10488 process_die (child_die, cu);
10489 }
10490
804d2729 10491 add_using_directive (using_directives (cu),
22cee43f
PMR
10492 import_prefix,
10493 canonical_name,
10494 import_alias,
10495 imported_declaration,
10496 excludes,
10497 0,
10498 &objfile->objfile_obstack);
27aa8d6a
SW
10499}
10500
5230b05a
WT
10501/* ICC<14 does not output the required DW_AT_declaration on incomplete
10502 types, but gives them a size of zero. Starting with version 14,
10503 ICC is compatible with GCC. */
10504
9068261f 10505static bool
5230b05a
WT
10506producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10507{
10508 if (!cu->checked_producer)
10509 check_producer (cu);
10510
10511 return cu->producer_is_icc_lt_14;
10512}
10513
eb77c9df
AB
10514/* ICC generates a DW_AT_type for C void functions. This was observed on
10515 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10516 which says that void functions should not have a DW_AT_type. */
10517
10518static bool
10519producer_is_icc (struct dwarf2_cu *cu)
10520{
10521 if (!cu->checked_producer)
10522 check_producer (cu);
10523
10524 return cu->producer_is_icc;
10525}
10526
1b80a9fa
JK
10527/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10528 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10529 this, it was first present in GCC release 4.3.0. */
10530
9068261f 10531static bool
1b80a9fa
JK
10532producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10533{
10534 if (!cu->checked_producer)
10535 check_producer (cu);
10536
10537 return cu->producer_is_gcc_lt_4_3;
10538}
10539
d721ba37
PA
10540static file_and_directory
10541find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10542{
d721ba37
PA
10543 file_and_directory res;
10544
9291a0cd
TT
10545 /* Find the filename. Do not use dwarf2_name here, since the filename
10546 is not a source language identifier. */
d721ba37
PA
10547 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10548 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10549
d721ba37
PA
10550 if (res.comp_dir == NULL
10551 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10552 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10553 {
d721ba37
PA
10554 res.comp_dir_storage = ldirname (res.name);
10555 if (!res.comp_dir_storage.empty ())
10556 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10557 }
d721ba37 10558 if (res.comp_dir != NULL)
9291a0cd
TT
10559 {
10560 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10561 directory, get rid of it. */
d721ba37 10562 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10563
d721ba37
PA
10564 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10565 res.comp_dir = cp + 1;
9291a0cd
TT
10566 }
10567
d721ba37
PA
10568 if (res.name == NULL)
10569 res.name = "<unknown>";
10570
10571 return res;
9291a0cd
TT
10572}
10573
f4dc4d17
DE
10574/* Handle DW_AT_stmt_list for a compilation unit.
10575 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10576 COMP_DIR is the compilation directory. LOWPC is passed to
10577 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10578
10579static void
10580handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10581 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10582{
518817b3
SM
10583 struct dwarf2_per_objfile *dwarf2_per_objfile
10584 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10585 struct attribute *attr;
527f3840
JK
10586 struct line_header line_header_local;
10587 hashval_t line_header_local_hash;
527f3840
JK
10588 void **slot;
10589 int decode_mapping;
2ab95328 10590
f4dc4d17
DE
10591 gdb_assert (! cu->per_cu->is_debug_types);
10592
2ab95328 10593 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10594 if (attr == NULL)
10595 return;
10596
9c541725 10597 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10598
10599 /* The line header hash table is only created if needed (it exists to
10600 prevent redundant reading of the line table for partial_units).
10601 If we're given a partial_unit, we'll need it. If we're given a
10602 compile_unit, then use the line header hash table if it's already
10603 created, but don't create one just yet. */
10604
10605 if (dwarf2_per_objfile->line_header_hash == NULL
10606 && die->tag == DW_TAG_partial_unit)
2ab95328 10607 {
527f3840 10608 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10609 .reset (htab_create_alloc (127, line_header_hash_voidp,
10610 line_header_eq_voidp,
10611 free_line_header_voidp,
10612 xcalloc, xfree));
527f3840 10613 }
2ab95328 10614
9c541725 10615 line_header_local.sect_off = line_offset;
527f3840
JK
10616 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10617 line_header_local_hash = line_header_hash (&line_header_local);
10618 if (dwarf2_per_objfile->line_header_hash != NULL)
10619 {
d15acc42 10620 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10621 &line_header_local,
10622 line_header_local_hash, NO_INSERT);
10623
10624 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10625 is not present in *SLOT (since if there is something in *SLOT then
10626 it will be for a partial_unit). */
10627 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10628 {
527f3840 10629 gdb_assert (*slot != NULL);
9a3c8263 10630 cu->line_header = (struct line_header *) *slot;
527f3840 10631 return;
dee91e82 10632 }
2ab95328 10633 }
527f3840
JK
10634
10635 /* dwarf_decode_line_header does not yet provide sufficient information.
10636 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10637 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10638 if (lh == NULL)
527f3840 10639 return;
4c8aa72d
PA
10640
10641 cu->line_header = lh.release ();
10642 cu->line_header_die_owner = die;
527f3840
JK
10643
10644 if (dwarf2_per_objfile->line_header_hash == NULL)
10645 slot = NULL;
10646 else
10647 {
d15acc42 10648 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10649 &line_header_local,
10650 line_header_local_hash, INSERT);
10651 gdb_assert (slot != NULL);
10652 }
10653 if (slot != NULL && *slot == NULL)
10654 {
10655 /* This newly decoded line number information unit will be owned
10656 by line_header_hash hash table. */
10657 *slot = cu->line_header;
4c8aa72d 10658 cu->line_header_die_owner = NULL;
527f3840
JK
10659 }
10660 else
10661 {
10662 /* We cannot free any current entry in (*slot) as that struct line_header
10663 may be already used by multiple CUs. Create only temporary decoded
10664 line_header for this CU - it may happen at most once for each line
10665 number information unit. And if we're not using line_header_hash
10666 then this is what we want as well. */
10667 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10668 }
10669 decode_mapping = (die->tag != DW_TAG_partial_unit);
10670 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10671 decode_mapping);
fff8551c 10672
2ab95328
TT
10673}
10674
95554aad 10675/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10676
c906108c 10677static void
e7c27a73 10678read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10679{
518817b3
SM
10680 struct dwarf2_per_objfile *dwarf2_per_objfile
10681 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10682 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 10683 struct gdbarch *gdbarch = get_objfile_arch (objfile);
2acceee2 10684 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10685 CORE_ADDR highpc = ((CORE_ADDR) 0);
10686 struct attribute *attr;
c906108c 10687 struct die_info *child_die;
e142c38c 10688 CORE_ADDR baseaddr;
6e70227d 10689
380618d6 10690 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10691 baseaddr = objfile->text_section_offset ();
c906108c 10692
fae299cd 10693 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10694
10695 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10696 from finish_block. */
2acceee2 10697 if (lowpc == ((CORE_ADDR) -1))
c906108c 10698 lowpc = highpc;
3e29f34a 10699 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10700
d721ba37 10701 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10702
f4b8a18d
KW
10703 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10704 standardised yet. As a workaround for the language detection we fall
10705 back to the DW_AT_producer string. */
10706 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10707 cu->language = language_opencl;
10708
3019eac3
DE
10709 /* Similar hack for Go. */
10710 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10711 set_cu_language (DW_LANG_Go, cu);
10712
c24bdb02 10713 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10714
10715 /* Decode line number information if present. We do this before
10716 processing child DIEs, so that the line header table is available
10717 for DW_AT_decl_file. */
d721ba37 10718 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10719
10720 /* Process all dies in compilation unit. */
10721 if (die->child != NULL)
10722 {
10723 child_die = die->child;
10724 while (child_die && child_die->tag)
10725 {
10726 process_die (child_die, cu);
436c571c 10727 child_die = child_die->sibling;
3019eac3
DE
10728 }
10729 }
10730
10731 /* Decode macro information, if present. Dwarf 2 macro information
10732 refers to information in the line number info statement program
10733 header, so we can only read it if we've read the header
10734 successfully. */
0af92d60
JK
10735 attr = dwarf2_attr (die, DW_AT_macros, cu);
10736 if (attr == NULL)
10737 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10738 if (attr && cu->line_header)
10739 {
10740 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10741 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10742
43f3e411 10743 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10744 }
10745 else
10746 {
10747 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10748 if (attr && cu->line_header)
10749 {
10750 unsigned int macro_offset = DW_UNSND (attr);
10751
43f3e411 10752 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10753 }
10754 }
3019eac3
DE
10755}
10756
c24bdb02
KS
10757void
10758dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10759{
f4dc4d17
DE
10760 struct type_unit_group *tu_group;
10761 int first_time;
3019eac3 10762 struct attribute *attr;
9c541725 10763 unsigned int i;
0186c6a7 10764 struct signatured_type *sig_type;
3019eac3 10765
f4dc4d17 10766 gdb_assert (per_cu->is_debug_types);
0186c6a7 10767 sig_type = (struct signatured_type *) per_cu;
3019eac3 10768
c24bdb02 10769 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10770
f4dc4d17 10771 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10772 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10773 if (sig_type->type_unit_group == NULL)
c24bdb02 10774 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10775 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10776
10777 /* If we've already processed this stmt_list there's no real need to
10778 do it again, we could fake it and just recreate the part we need
10779 (file name,index -> symtab mapping). If data shows this optimization
10780 is useful we can do it then. */
43f3e411 10781 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10782
10783 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10784 debug info. */
fff8551c 10785 line_header_up lh;
f4dc4d17 10786 if (attr != NULL)
3019eac3 10787 {
9c541725 10788 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10789 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10790 }
10791 if (lh == NULL)
10792 {
10793 if (first_time)
c24bdb02 10794 start_symtab ("", NULL, 0);
f4dc4d17
DE
10795 else
10796 {
10797 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10798 gdb_assert (m_builder == nullptr);
804d2729 10799 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10800 m_builder.reset (new struct buildsym_compunit
10801 (COMPUNIT_OBJFILE (cust), "",
10802 COMPUNIT_DIRNAME (cust),
10803 compunit_language (cust),
10804 0, cust));
f4dc4d17 10805 }
f4dc4d17 10806 return;
3019eac3
DE
10807 }
10808
c24bdb02
KS
10809 line_header = lh.release ();
10810 line_header_die_owner = die;
3019eac3 10811
f4dc4d17
DE
10812 if (first_time)
10813 {
c24bdb02 10814 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10815
1fd60fc0
DE
10816 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10817 still initializing it, and our caller (a few levels up)
10818 process_full_type_unit still needs to know if this is the first
10819 time. */
10820
4ac93832
TT
10821 tu_group->symtabs
10822 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10823 struct symtab *, line_header->file_names_size ());
3019eac3 10824
7ba99d21
AT
10825 auto &file_names = line_header->file_names ();
10826 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10827 {
7ba99d21 10828 file_entry &fe = file_names[i];
c24bdb02
KS
10829 dwarf2_start_subfile (this, fe.name,
10830 fe.include_dir (line_header));
10831 buildsym_compunit *b = get_builder ();
10832 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10833 {
4c8aa72d
PA
10834 /* NOTE: start_subfile will recognize when it's been
10835 passed a file it has already seen. So we can't
10836 assume there's a simple mapping from
10837 cu->line_header->file_names to subfiles, plus
10838 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10839 b->get_current_subfile ()->symtab
10840 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10841 }
10842
c24bdb02 10843 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10844 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10845 }
10846 }
10847 else
3019eac3 10848 {
c24bdb02 10849 gdb_assert (m_builder == nullptr);
804d2729 10850 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10851 m_builder.reset (new struct buildsym_compunit
10852 (COMPUNIT_OBJFILE (cust), "",
10853 COMPUNIT_DIRNAME (cust),
10854 compunit_language (cust),
10855 0, cust));
f4dc4d17 10856
7ba99d21
AT
10857 auto &file_names = line_header->file_names ();
10858 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10859 {
7ba99d21 10860 file_entry &fe = file_names[i];
4c8aa72d 10861 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10862 }
3019eac3
DE
10863 }
10864
f4dc4d17
DE
10865 /* The main symtab is allocated last. Type units don't have DW_AT_name
10866 so they don't have a "real" (so to speak) symtab anyway.
10867 There is later code that will assign the main symtab to all symbols
10868 that don't have one. We need to handle the case of a symbol with a
10869 missing symtab (DW_AT_decl_file) anyway. */
10870}
3019eac3 10871
f4dc4d17
DE
10872/* Process DW_TAG_type_unit.
10873 For TUs we want to skip the first top level sibling if it's not the
10874 actual type being defined by this TU. In this case the first top
10875 level sibling is there to provide context only. */
3019eac3 10876
f4dc4d17
DE
10877static void
10878read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10879{
10880 struct die_info *child_die;
3019eac3 10881
f4dc4d17
DE
10882 prepare_one_comp_unit (cu, die, language_minimal);
10883
10884 /* Initialize (or reinitialize) the machinery for building symtabs.
10885 We do this before processing child DIEs, so that the line header table
10886 is available for DW_AT_decl_file. */
c24bdb02 10887 cu->setup_type_unit_groups (die);
f4dc4d17
DE
10888
10889 if (die->child != NULL)
10890 {
10891 child_die = die->child;
10892 while (child_die && child_die->tag)
10893 {
10894 process_die (child_die, cu);
436c571c 10895 child_die = child_die->sibling;
f4dc4d17
DE
10896 }
10897 }
3019eac3
DE
10898}
10899\f
80626a55
DE
10900/* DWO/DWP files.
10901
10902 http://gcc.gnu.org/wiki/DebugFission
10903 http://gcc.gnu.org/wiki/DebugFissionDWP
10904
10905 To simplify handling of both DWO files ("object" files with the DWARF info)
10906 and DWP files (a file with the DWOs packaged up into one file), we treat
10907 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
10908
10909static hashval_t
10910hash_dwo_file (const void *item)
10911{
9a3c8263 10912 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 10913 hashval_t hash;
3019eac3 10914
a2ce51a0
DE
10915 hash = htab_hash_string (dwo_file->dwo_name);
10916 if (dwo_file->comp_dir != NULL)
10917 hash += htab_hash_string (dwo_file->comp_dir);
10918 return hash;
3019eac3
DE
10919}
10920
10921static int
10922eq_dwo_file (const void *item_lhs, const void *item_rhs)
10923{
9a3c8263
SM
10924 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
10925 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 10926
a2ce51a0
DE
10927 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
10928 return 0;
10929 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
10930 return lhs->comp_dir == rhs->comp_dir;
10931 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
10932}
10933
10934/* Allocate a hash table for DWO files. */
10935
51ac9db5 10936static htab_up
298e9637 10937allocate_dwo_file_hash_table ()
3019eac3 10938{
51ac9db5
SM
10939 auto delete_dwo_file = [] (void *item)
10940 {
10941 struct dwo_file *dwo_file = (struct dwo_file *) item;
10942
10943 delete dwo_file;
10944 };
10945
bc68fb19
TT
10946 return htab_up (htab_create_alloc (41,
10947 hash_dwo_file,
10948 eq_dwo_file,
10949 delete_dwo_file,
10950 xcalloc, xfree));
3019eac3
DE
10951}
10952
80626a55
DE
10953/* Lookup DWO file DWO_NAME. */
10954
10955static void **
ed2dc618
SM
10956lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
10957 const char *dwo_name,
10958 const char *comp_dir)
80626a55
DE
10959{
10960 struct dwo_file find_entry;
10961 void **slot;
10962
10963 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 10964 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 10965
0ac5b59e
DE
10966 find_entry.dwo_name = dwo_name;
10967 find_entry.comp_dir = comp_dir;
51ac9db5
SM
10968 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
10969 INSERT);
80626a55
DE
10970
10971 return slot;
10972}
10973
3019eac3
DE
10974static hashval_t
10975hash_dwo_unit (const void *item)
10976{
9a3c8263 10977 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
10978
10979 /* This drops the top 32 bits of the id, but is ok for a hash. */
10980 return dwo_unit->signature;
10981}
10982
10983static int
10984eq_dwo_unit (const void *item_lhs, const void *item_rhs)
10985{
9a3c8263
SM
10986 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
10987 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
10988
10989 /* The signature is assumed to be unique within the DWO file.
10990 So while object file CU dwo_id's always have the value zero,
10991 that's OK, assuming each object file DWO file has only one CU,
10992 and that's the rule for now. */
10993 return lhs->signature == rhs->signature;
10994}
10995
10996/* Allocate a hash table for DWO CUs,TUs.
10997 There is one of these tables for each of CUs,TUs for each DWO file. */
10998
b0b6a987 10999static htab_up
298e9637 11000allocate_dwo_unit_table ()
3019eac3
DE
11001{
11002 /* Start out with a pretty small number.
11003 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11004 return htab_up (htab_create_alloc (3,
11005 hash_dwo_unit,
11006 eq_dwo_unit,
11007 NULL, xcalloc, xfree));
3019eac3
DE
11008}
11009
19c3d4c9 11010/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11011
11012static void
19c3d4c9
DE
11013create_dwo_cu_reader (const struct die_reader_specs *reader,
11014 const gdb_byte *info_ptr,
11015 struct die_info *comp_unit_die,
c0ab21c2
TT
11016 struct dwo_file *dwo_file,
11017 struct dwo_unit *dwo_unit)
3019eac3
DE
11018{
11019 struct dwarf2_cu *cu = reader->cu;
9c541725 11020 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11021 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11022
a084a2a6
AT
11023 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11024 if (!signature.has_value ())
3019eac3 11025 {
b98664d3 11026 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11027 " its dwo_id [in module %s]"),
9d8780f0 11028 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11029 return;
11030 }
11031
3019eac3 11032 dwo_unit->dwo_file = dwo_file;
a084a2a6 11033 dwo_unit->signature = *signature;
8a0459fd 11034 dwo_unit->section = section;
9c541725 11035 dwo_unit->sect_off = sect_off;
3019eac3
DE
11036 dwo_unit->length = cu->per_cu->length;
11037
b4f54984 11038 if (dwarf_read_debug)
9d8780f0
SM
11039 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11040 sect_offset_str (sect_off),
9c541725 11041 hex_string (dwo_unit->signature));
3019eac3
DE
11042}
11043
33c5cd75 11044/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11045 Note: This function processes DWO files only, not DWP files. */
3019eac3 11046
33c5cd75 11047static void
ed2dc618 11048create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11049 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11050 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11051{
11052 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11053 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11054
96b79293 11055 section.read (objfile);
33c5cd75 11056 info_ptr = section.buffer;
3019eac3
DE
11057
11058 if (info_ptr == NULL)
33c5cd75 11059 return;
3019eac3 11060
b4f54984 11061 if (dwarf_read_debug)
19c3d4c9
DE
11062 {
11063 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11064 section.get_name (),
11065 section.get_file_name ());
19c3d4c9 11066 }
3019eac3 11067
33c5cd75 11068 end_ptr = info_ptr + section.size;
3019eac3
DE
11069 while (info_ptr < end_ptr)
11070 {
11071 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11072 struct dwo_unit read_unit {};
33c5cd75
DB
11073 struct dwo_unit *dwo_unit;
11074 void **slot;
11075 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11076
11077 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11078 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11079 per_cu.is_debug_types = 0;
33c5cd75
DB
11080 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11081 per_cu.section = &section;
11082
c0ab21c2
TT
11083 cutu_reader reader (&per_cu, cu, &dwo_file);
11084 if (!reader.dummy_p)
11085 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11086 &dwo_file, &read_unit);
33c5cd75
DB
11087 info_ptr += per_cu.length;
11088
11089 // If the unit could not be parsed, skip it.
c0ab21c2 11090 if (read_unit.dwo_file == NULL)
33c5cd75 11091 continue;
3019eac3 11092
33c5cd75 11093 if (cus_htab == NULL)
298e9637 11094 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11095
33c5cd75 11096 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11097 *dwo_unit = read_unit;
b0b6a987 11098 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11099 gdb_assert (slot != NULL);
11100 if (*slot != NULL)
19c3d4c9 11101 {
33c5cd75
DB
11102 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11103 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11104
b98664d3 11105 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11106 " the entry at offset %s, signature %s"),
11107 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11108 hex_string (dwo_unit->signature));
19c3d4c9 11109 }
33c5cd75 11110 *slot = (void *)dwo_unit;
3019eac3 11111 }
3019eac3
DE
11112}
11113
80626a55
DE
11114/* DWP file .debug_{cu,tu}_index section format:
11115 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11116
d2415c6c
DE
11117 DWP Version 1:
11118
80626a55
DE
11119 Both index sections have the same format, and serve to map a 64-bit
11120 signature to a set of section numbers. Each section begins with a header,
11121 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11122 indexes, and a pool of 32-bit section numbers. The index sections will be
11123 aligned at 8-byte boundaries in the file.
11124
d2415c6c
DE
11125 The index section header consists of:
11126
11127 V, 32 bit version number
11128 -, 32 bits unused
11129 N, 32 bit number of compilation units or type units in the index
11130 M, 32 bit number of slots in the hash table
80626a55 11131
d2415c6c 11132 Numbers are recorded using the byte order of the application binary.
80626a55 11133
d2415c6c
DE
11134 The hash table begins at offset 16 in the section, and consists of an array
11135 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11136 order of the application binary). Unused slots in the hash table are 0.
11137 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11138
d2415c6c
DE
11139 The parallel table begins immediately after the hash table
11140 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11141 array of 32-bit indexes (using the byte order of the application binary),
11142 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11143 table contains a 32-bit index into the pool of section numbers. For unused
11144 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11145
73869dc2
DE
11146 The pool of section numbers begins immediately following the hash table
11147 (at offset 16 + 12 * M from the beginning of the section). The pool of
11148 section numbers consists of an array of 32-bit words (using the byte order
11149 of the application binary). Each item in the array is indexed starting
11150 from 0. The hash table entry provides the index of the first section
11151 number in the set. Additional section numbers in the set follow, and the
11152 set is terminated by a 0 entry (section number 0 is not used in ELF).
11153
11154 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11155 section must be the first entry in the set, and the .debug_abbrev.dwo must
11156 be the second entry. Other members of the set may follow in any order.
11157
11158 ---
11159
11160 DWP Version 2:
11161
11162 DWP Version 2 combines all the .debug_info, etc. sections into one,
11163 and the entries in the index tables are now offsets into these sections.
11164 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11165 section.
11166
11167 Index Section Contents:
11168 Header
11169 Hash Table of Signatures dwp_hash_table.hash_table
11170 Parallel Table of Indices dwp_hash_table.unit_table
11171 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11172 Table of Section Sizes dwp_hash_table.v2.sizes
11173
11174 The index section header consists of:
11175
11176 V, 32 bit version number
11177 L, 32 bit number of columns in the table of section offsets
11178 N, 32 bit number of compilation units or type units in the index
11179 M, 32 bit number of slots in the hash table
11180
11181 Numbers are recorded using the byte order of the application binary.
11182
11183 The hash table has the same format as version 1.
11184 The parallel table of indices has the same format as version 1,
11185 except that the entries are origin-1 indices into the table of sections
11186 offsets and the table of section sizes.
11187
11188 The table of offsets begins immediately following the parallel table
11189 (at offset 16 + 12 * M from the beginning of the section). The table is
11190 a two-dimensional array of 32-bit words (using the byte order of the
11191 application binary), with L columns and N+1 rows, in row-major order.
11192 Each row in the array is indexed starting from 0. The first row provides
11193 a key to the remaining rows: each column in this row provides an identifier
11194 for a debug section, and the offsets in the same column of subsequent rows
11195 refer to that section. The section identifiers are:
11196
11197 DW_SECT_INFO 1 .debug_info.dwo
11198 DW_SECT_TYPES 2 .debug_types.dwo
11199 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11200 DW_SECT_LINE 4 .debug_line.dwo
11201 DW_SECT_LOC 5 .debug_loc.dwo
11202 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11203 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11204 DW_SECT_MACRO 8 .debug_macro.dwo
11205
11206 The offsets provided by the CU and TU index sections are the base offsets
11207 for the contributions made by each CU or TU to the corresponding section
11208 in the package file. Each CU and TU header contains an abbrev_offset
11209 field, used to find the abbreviations table for that CU or TU within the
11210 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11211 be interpreted as relative to the base offset given in the index section.
11212 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11213 should be interpreted as relative to the base offset for .debug_line.dwo,
11214 and offsets into other debug sections obtained from DWARF attributes should
11215 also be interpreted as relative to the corresponding base offset.
11216
11217 The table of sizes begins immediately following the table of offsets.
11218 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11219 with L columns and N rows, in row-major order. Each row in the array is
11220 indexed starting from 1 (row 0 is shared by the two tables).
11221
11222 ---
11223
11224 Hash table lookup is handled the same in version 1 and 2:
11225
11226 We assume that N and M will not exceed 2^32 - 1.
11227 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11228
d2415c6c
DE
11229 Given a 64-bit compilation unit signature or a type signature S, an entry
11230 in the hash table is located as follows:
80626a55 11231
d2415c6c
DE
11232 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11233 the low-order k bits all set to 1.
80626a55 11234
d2415c6c 11235 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11236
d2415c6c
DE
11237 3) If the hash table entry at index H matches the signature, use that
11238 entry. If the hash table entry at index H is unused (all zeroes),
11239 terminate the search: the signature is not present in the table.
80626a55 11240
d2415c6c 11241 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11242
d2415c6c 11243 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11244 to stop at an unused slot or find the match. */
80626a55
DE
11245
11246/* Create a hash table to map DWO IDs to their CU/TU entry in
11247 .debug_{info,types}.dwo in DWP_FILE.
11248 Returns NULL if there isn't one.
11249 Note: This function processes DWP files only, not DWO files. */
11250
11251static struct dwp_hash_table *
ed2dc618
SM
11252create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11253 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11254{
11255 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11256 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11257 const gdb_byte *index_ptr, *index_end;
80626a55 11258 struct dwarf2_section_info *index;
73869dc2 11259 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11260 struct dwp_hash_table *htab;
11261
11262 if (is_debug_types)
11263 index = &dwp_file->sections.tu_index;
11264 else
11265 index = &dwp_file->sections.cu_index;
11266
96b79293 11267 if (index->empty ())
80626a55 11268 return NULL;
96b79293 11269 index->read (objfile);
80626a55
DE
11270
11271 index_ptr = index->buffer;
11272 index_end = index_ptr + index->size;
11273
11274 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11275 index_ptr += 4;
11276 if (version == 2)
11277 nr_columns = read_4_bytes (dbfd, index_ptr);
11278 else
11279 nr_columns = 0;
11280 index_ptr += 4;
80626a55
DE
11281 nr_units = read_4_bytes (dbfd, index_ptr);
11282 index_ptr += 4;
11283 nr_slots = read_4_bytes (dbfd, index_ptr);
11284 index_ptr += 4;
11285
73869dc2 11286 if (version != 1 && version != 2)
80626a55 11287 {
21aa081e 11288 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11289 " [in module %s]"),
21aa081e 11290 pulongest (version), dwp_file->name);
80626a55
DE
11291 }
11292 if (nr_slots != (nr_slots & -nr_slots))
11293 {
21aa081e 11294 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11295 " is not power of 2 [in module %s]"),
21aa081e 11296 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11297 }
11298
11299 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11300 htab->version = version;
11301 htab->nr_columns = nr_columns;
80626a55
DE
11302 htab->nr_units = nr_units;
11303 htab->nr_slots = nr_slots;
11304 htab->hash_table = index_ptr;
11305 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11306
11307 /* Exit early if the table is empty. */
11308 if (nr_slots == 0 || nr_units == 0
11309 || (version == 2 && nr_columns == 0))
11310 {
11311 /* All must be zero. */
11312 if (nr_slots != 0 || nr_units != 0
11313 || (version == 2 && nr_columns != 0))
11314 {
b98664d3 11315 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11316 " all zero [in modules %s]"),
11317 dwp_file->name);
11318 }
11319 return htab;
11320 }
11321
11322 if (version == 1)
11323 {
11324 htab->section_pool.v1.indices =
11325 htab->unit_table + sizeof (uint32_t) * nr_slots;
11326 /* It's harder to decide whether the section is too small in v1.
11327 V1 is deprecated anyway so we punt. */
11328 }
11329 else
11330 {
11331 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11332 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11333 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11334 /* Reverse map for error checking. */
11335 int ids_seen[DW_SECT_MAX + 1];
11336 int i;
11337
11338 if (nr_columns < 2)
11339 {
11340 error (_("Dwarf Error: bad DWP hash table, too few columns"
11341 " in section table [in module %s]"),
11342 dwp_file->name);
11343 }
11344 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11345 {
11346 error (_("Dwarf Error: bad DWP hash table, too many columns"
11347 " in section table [in module %s]"),
11348 dwp_file->name);
11349 }
04fd5eed
GB
11350 memset (ids, 255, sizeof_ids);
11351 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11352 for (i = 0; i < nr_columns; ++i)
11353 {
11354 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11355
11356 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11357 {
11358 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11359 " in section table [in module %s]"),
11360 id, dwp_file->name);
11361 }
11362 if (ids_seen[id] != -1)
11363 {
11364 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11365 " id %d in section table [in module %s]"),
11366 id, dwp_file->name);
11367 }
11368 ids_seen[id] = i;
11369 ids[i] = id;
11370 }
11371 /* Must have exactly one info or types section. */
11372 if (((ids_seen[DW_SECT_INFO] != -1)
11373 + (ids_seen[DW_SECT_TYPES] != -1))
11374 != 1)
11375 {
11376 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11377 " DWO info/types section [in module %s]"),
11378 dwp_file->name);
11379 }
11380 /* Must have an abbrev section. */
11381 if (ids_seen[DW_SECT_ABBREV] == -1)
11382 {
11383 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11384 " section [in module %s]"),
11385 dwp_file->name);
11386 }
11387 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11388 htab->section_pool.v2.sizes =
11389 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11390 * nr_units * nr_columns);
11391 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11392 * nr_units * nr_columns))
11393 > index_end)
11394 {
11395 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11396 " [in module %s]"),
11397 dwp_file->name);
11398 }
11399 }
80626a55
DE
11400
11401 return htab;
11402}
11403
11404/* Update SECTIONS with the data from SECTP.
11405
11406 This function is like the other "locate" section routines that are
11407 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11408 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11409
11410 The result is non-zero for success, or zero if an error was found. */
11411
11412static int
73869dc2
DE
11413locate_v1_virtual_dwo_sections (asection *sectp,
11414 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11415{
11416 const struct dwop_section_names *names = &dwop_section_names;
11417
11418 if (section_is_p (sectp->name, &names->abbrev_dwo))
11419 {
11420 /* There can be only one. */
049412e3 11421 if (sections->abbrev.s.section != NULL)
80626a55 11422 return 0;
049412e3 11423 sections->abbrev.s.section = sectp;
fd361982 11424 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11425 }
11426 else if (section_is_p (sectp->name, &names->info_dwo)
11427 || section_is_p (sectp->name, &names->types_dwo))
11428 {
11429 /* There can be only one. */
049412e3 11430 if (sections->info_or_types.s.section != NULL)
80626a55 11431 return 0;
049412e3 11432 sections->info_or_types.s.section = sectp;
fd361982 11433 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11434 }
11435 else if (section_is_p (sectp->name, &names->line_dwo))
11436 {
11437 /* There can be only one. */
049412e3 11438 if (sections->line.s.section != NULL)
80626a55 11439 return 0;
049412e3 11440 sections->line.s.section = sectp;
fd361982 11441 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11442 }
11443 else if (section_is_p (sectp->name, &names->loc_dwo))
11444 {
11445 /* There can be only one. */
049412e3 11446 if (sections->loc.s.section != NULL)
80626a55 11447 return 0;
049412e3 11448 sections->loc.s.section = sectp;
fd361982 11449 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11450 }
11451 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11452 {
11453 /* There can be only one. */
049412e3 11454 if (sections->macinfo.s.section != NULL)
80626a55 11455 return 0;
049412e3 11456 sections->macinfo.s.section = sectp;
fd361982 11457 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11458 }
11459 else if (section_is_p (sectp->name, &names->macro_dwo))
11460 {
11461 /* There can be only one. */
049412e3 11462 if (sections->macro.s.section != NULL)
80626a55 11463 return 0;
049412e3 11464 sections->macro.s.section = sectp;
fd361982 11465 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11466 }
11467 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11468 {
11469 /* There can be only one. */
049412e3 11470 if (sections->str_offsets.s.section != NULL)
80626a55 11471 return 0;
049412e3 11472 sections->str_offsets.s.section = sectp;
fd361982 11473 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11474 }
11475 else
11476 {
11477 /* No other kind of section is valid. */
11478 return 0;
11479 }
11480
11481 return 1;
11482}
11483
73869dc2
DE
11484/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11485 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11486 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11487 This is for DWP version 1 files. */
80626a55
DE
11488
11489static struct dwo_unit *
ed2dc618
SM
11490create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11491 struct dwp_file *dwp_file,
73869dc2
DE
11492 uint32_t unit_index,
11493 const char *comp_dir,
11494 ULONGEST signature, int is_debug_types)
80626a55
DE
11495{
11496 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11497 const struct dwp_hash_table *dwp_htab =
11498 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11499 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11500 const char *kind = is_debug_types ? "TU" : "CU";
11501 struct dwo_file *dwo_file;
11502 struct dwo_unit *dwo_unit;
73869dc2 11503 struct virtual_v1_dwo_sections sections;
80626a55 11504 void **dwo_file_slot;
80626a55
DE
11505 int i;
11506
73869dc2
DE
11507 gdb_assert (dwp_file->version == 1);
11508
b4f54984 11509 if (dwarf_read_debug)
80626a55 11510 {
73869dc2 11511 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11512 kind,
73869dc2 11513 pulongest (unit_index), hex_string (signature),
80626a55
DE
11514 dwp_file->name);
11515 }
11516
19ac8c2e 11517 /* Fetch the sections of this DWO unit.
80626a55
DE
11518 Put a limit on the number of sections we look for so that bad data
11519 doesn't cause us to loop forever. */
11520
73869dc2 11521#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11522 (1 /* .debug_info or .debug_types */ \
11523 + 1 /* .debug_abbrev */ \
11524 + 1 /* .debug_line */ \
11525 + 1 /* .debug_loc */ \
11526 + 1 /* .debug_str_offsets */ \
19ac8c2e 11527 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11528 + 1 /* trailing zero */)
11529
11530 memset (&sections, 0, sizeof (sections));
80626a55 11531
73869dc2 11532 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11533 {
11534 asection *sectp;
11535 uint32_t section_nr =
11536 read_4_bytes (dbfd,
73869dc2
DE
11537 dwp_htab->section_pool.v1.indices
11538 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11539
11540 if (section_nr == 0)
11541 break;
11542 if (section_nr >= dwp_file->num_sections)
11543 {
11544 error (_("Dwarf Error: bad DWP hash table, section number too large"
11545 " [in module %s]"),
11546 dwp_file->name);
11547 }
11548
11549 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11550 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11551 {
11552 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11553 " [in module %s]"),
11554 dwp_file->name);
11555 }
11556 }
11557
11558 if (i < 2
96b79293
TT
11559 || sections.info_or_types.empty ()
11560 || sections.abbrev.empty ())
80626a55
DE
11561 {
11562 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11563 " [in module %s]"),
11564 dwp_file->name);
11565 }
73869dc2 11566 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11567 {
11568 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11569 " [in module %s]"),
11570 dwp_file->name);
11571 }
11572
11573 /* It's easier for the rest of the code if we fake a struct dwo_file and
11574 have dwo_unit "live" in that. At least for now.
11575
11576 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11577 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11578 file, we can combine them back into a virtual DWO file to save space
11579 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11580 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11581
791afaa2
TT
11582 std::string virtual_dwo_name =
11583 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11584 sections.abbrev.get_id (),
11585 sections.line.get_id (),
11586 sections.loc.get_id (),
11587 sections.str_offsets.get_id ());
80626a55 11588 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11589 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11590 virtual_dwo_name.c_str (),
11591 comp_dir);
80626a55
DE
11592 /* Create one if necessary. */
11593 if (*dwo_file_slot == NULL)
11594 {
b4f54984 11595 if (dwarf_read_debug)
80626a55
DE
11596 {
11597 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11598 virtual_dwo_name.c_str ());
80626a55 11599 }
51ac9db5 11600 dwo_file = new struct dwo_file;
be1e3d3e 11601 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11602 dwo_file->comp_dir = comp_dir;
80626a55
DE
11603 dwo_file->sections.abbrev = sections.abbrev;
11604 dwo_file->sections.line = sections.line;
11605 dwo_file->sections.loc = sections.loc;
11606 dwo_file->sections.macinfo = sections.macinfo;
11607 dwo_file->sections.macro = sections.macro;
11608 dwo_file->sections.str_offsets = sections.str_offsets;
11609 /* The "str" section is global to the entire DWP file. */
11610 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11611 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11612 there's no need to record it in dwo_file.
11613 Also, we can't simply record type sections in dwo_file because
11614 we record a pointer into the vector in dwo_unit. As we collect more
11615 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11616 for it, invalidating all copies of pointers into the previous
11617 contents. */
80626a55
DE
11618 *dwo_file_slot = dwo_file;
11619 }
11620 else
11621 {
b4f54984 11622 if (dwarf_read_debug)
80626a55
DE
11623 {
11624 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11625 virtual_dwo_name.c_str ());
80626a55 11626 }
9a3c8263 11627 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11628 }
80626a55
DE
11629
11630 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11631 dwo_unit->dwo_file = dwo_file;
11632 dwo_unit->signature = signature;
8d749320
SM
11633 dwo_unit->section =
11634 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11635 *dwo_unit->section = sections.info_or_types;
57d63ce2 11636 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11637
11638 return dwo_unit;
11639}
11640
73869dc2
DE
11641/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11642 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11643 piece within that section used by a TU/CU, return a virtual section
11644 of just that piece. */
11645
11646static struct dwarf2_section_info
ed2dc618
SM
11647create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11648 struct dwarf2_section_info *section,
73869dc2
DE
11649 bfd_size_type offset, bfd_size_type size)
11650{
11651 struct dwarf2_section_info result;
11652 asection *sectp;
11653
11654 gdb_assert (section != NULL);
11655 gdb_assert (!section->is_virtual);
11656
11657 memset (&result, 0, sizeof (result));
11658 result.s.containing_section = section;
dc4ccb6f 11659 result.is_virtual = true;
73869dc2
DE
11660
11661 if (size == 0)
11662 return result;
11663
96b79293 11664 sectp = section->get_bfd_section ();
73869dc2
DE
11665
11666 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11667 bounds of the real section. This is a pretty-rare event, so just
11668 flag an error (easier) instead of a warning and trying to cope. */
11669 if (sectp == NULL
fd361982 11670 || offset + size > bfd_section_size (sectp))
73869dc2 11671 {
73869dc2
DE
11672 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11673 " in section %s [in module %s]"),
fd361982 11674 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11675 objfile_name (dwarf2_per_objfile->objfile));
11676 }
11677
11678 result.virtual_offset = offset;
11679 result.size = size;
11680 return result;
11681}
11682
11683/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11684 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11685 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11686 This is for DWP version 2 files. */
11687
11688static struct dwo_unit *
ed2dc618
SM
11689create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11690 struct dwp_file *dwp_file,
73869dc2
DE
11691 uint32_t unit_index,
11692 const char *comp_dir,
11693 ULONGEST signature, int is_debug_types)
11694{
11695 struct objfile *objfile = dwarf2_per_objfile->objfile;
11696 const struct dwp_hash_table *dwp_htab =
11697 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11698 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11699 const char *kind = is_debug_types ? "TU" : "CU";
11700 struct dwo_file *dwo_file;
11701 struct dwo_unit *dwo_unit;
11702 struct virtual_v2_dwo_sections sections;
11703 void **dwo_file_slot;
73869dc2
DE
11704 int i;
11705
11706 gdb_assert (dwp_file->version == 2);
11707
b4f54984 11708 if (dwarf_read_debug)
73869dc2
DE
11709 {
11710 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11711 kind,
11712 pulongest (unit_index), hex_string (signature),
11713 dwp_file->name);
11714 }
11715
11716 /* Fetch the section offsets of this DWO unit. */
11717
11718 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11719
11720 for (i = 0; i < dwp_htab->nr_columns; ++i)
11721 {
11722 uint32_t offset = read_4_bytes (dbfd,
11723 dwp_htab->section_pool.v2.offsets
11724 + (((unit_index - 1) * dwp_htab->nr_columns
11725 + i)
11726 * sizeof (uint32_t)));
11727 uint32_t size = read_4_bytes (dbfd,
11728 dwp_htab->section_pool.v2.sizes
11729 + (((unit_index - 1) * dwp_htab->nr_columns
11730 + i)
11731 * sizeof (uint32_t)));
11732
11733 switch (dwp_htab->section_pool.v2.section_ids[i])
11734 {
11735 case DW_SECT_INFO:
11736 case DW_SECT_TYPES:
11737 sections.info_or_types_offset = offset;
11738 sections.info_or_types_size = size;
11739 break;
11740 case DW_SECT_ABBREV:
11741 sections.abbrev_offset = offset;
11742 sections.abbrev_size = size;
11743 break;
11744 case DW_SECT_LINE:
11745 sections.line_offset = offset;
11746 sections.line_size = size;
11747 break;
11748 case DW_SECT_LOC:
11749 sections.loc_offset = offset;
11750 sections.loc_size = size;
11751 break;
11752 case DW_SECT_STR_OFFSETS:
11753 sections.str_offsets_offset = offset;
11754 sections.str_offsets_size = size;
11755 break;
11756 case DW_SECT_MACINFO:
11757 sections.macinfo_offset = offset;
11758 sections.macinfo_size = size;
11759 break;
11760 case DW_SECT_MACRO:
11761 sections.macro_offset = offset;
11762 sections.macro_size = size;
11763 break;
11764 }
11765 }
11766
11767 /* It's easier for the rest of the code if we fake a struct dwo_file and
11768 have dwo_unit "live" in that. At least for now.
11769
11770 The DWP file can be made up of a random collection of CUs and TUs.
11771 However, for each CU + set of TUs that came from the same original DWO
11772 file, we can combine them back into a virtual DWO file to save space
11773 (fewer struct dwo_file objects to allocate). Remember that for really
11774 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11775
791afaa2
TT
11776 std::string virtual_dwo_name =
11777 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11778 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11779 (long) (sections.line_size ? sections.line_offset : 0),
11780 (long) (sections.loc_size ? sections.loc_offset : 0),
11781 (long) (sections.str_offsets_size
11782 ? sections.str_offsets_offset : 0));
73869dc2 11783 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11784 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11785 virtual_dwo_name.c_str (),
11786 comp_dir);
73869dc2
DE
11787 /* Create one if necessary. */
11788 if (*dwo_file_slot == NULL)
11789 {
b4f54984 11790 if (dwarf_read_debug)
73869dc2
DE
11791 {
11792 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11793 virtual_dwo_name.c_str ());
73869dc2 11794 }
51ac9db5 11795 dwo_file = new struct dwo_file;
be1e3d3e 11796 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11797 dwo_file->comp_dir = comp_dir;
11798 dwo_file->sections.abbrev =
ed2dc618 11799 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11800 sections.abbrev_offset, sections.abbrev_size);
11801 dwo_file->sections.line =
ed2dc618 11802 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11803 sections.line_offset, sections.line_size);
11804 dwo_file->sections.loc =
ed2dc618 11805 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11806 sections.loc_offset, sections.loc_size);
11807 dwo_file->sections.macinfo =
ed2dc618 11808 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11809 sections.macinfo_offset, sections.macinfo_size);
11810 dwo_file->sections.macro =
ed2dc618 11811 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11812 sections.macro_offset, sections.macro_size);
11813 dwo_file->sections.str_offsets =
ed2dc618
SM
11814 create_dwp_v2_section (dwarf2_per_objfile,
11815 &dwp_file->sections.str_offsets,
73869dc2
DE
11816 sections.str_offsets_offset,
11817 sections.str_offsets_size);
11818 /* The "str" section is global to the entire DWP file. */
11819 dwo_file->sections.str = dwp_file->sections.str;
11820 /* The info or types section is assigned below to dwo_unit,
11821 there's no need to record it in dwo_file.
11822 Also, we can't simply record type sections in dwo_file because
11823 we record a pointer into the vector in dwo_unit. As we collect more
11824 types we'll grow the vector and eventually have to reallocate space
11825 for it, invalidating all copies of pointers into the previous
11826 contents. */
11827 *dwo_file_slot = dwo_file;
11828 }
11829 else
11830 {
b4f54984 11831 if (dwarf_read_debug)
73869dc2
DE
11832 {
11833 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11834 virtual_dwo_name.c_str ());
73869dc2 11835 }
9a3c8263 11836 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11837 }
73869dc2
DE
11838
11839 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11840 dwo_unit->dwo_file = dwo_file;
11841 dwo_unit->signature = signature;
8d749320
SM
11842 dwo_unit->section =
11843 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11844 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11845 is_debug_types
73869dc2
DE
11846 ? &dwp_file->sections.types
11847 : &dwp_file->sections.info,
11848 sections.info_or_types_offset,
11849 sections.info_or_types_size);
11850 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11851
11852 return dwo_unit;
11853}
11854
57d63ce2
DE
11855/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11856 Returns NULL if the signature isn't found. */
80626a55
DE
11857
11858static struct dwo_unit *
ed2dc618
SM
11859lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11860 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11861 ULONGEST signature, int is_debug_types)
80626a55 11862{
57d63ce2
DE
11863 const struct dwp_hash_table *dwp_htab =
11864 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11865 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11866 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11867 uint32_t hash = signature & mask;
11868 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11869 unsigned int i;
11870 void **slot;
870f88f7 11871 struct dwo_unit find_dwo_cu;
80626a55
DE
11872
11873 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11874 find_dwo_cu.signature = signature;
19ac8c2e 11875 slot = htab_find_slot (is_debug_types
48b490f2
TT
11876 ? dwp_file->loaded_tus.get ()
11877 : dwp_file->loaded_cus.get (),
19ac8c2e 11878 &find_dwo_cu, INSERT);
80626a55
DE
11879
11880 if (*slot != NULL)
9a3c8263 11881 return (struct dwo_unit *) *slot;
80626a55
DE
11882
11883 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11884 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11885 {
11886 ULONGEST signature_in_table;
11887
11888 signature_in_table =
57d63ce2 11889 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
11890 if (signature_in_table == signature)
11891 {
57d63ce2
DE
11892 uint32_t unit_index =
11893 read_4_bytes (dbfd,
11894 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 11895
73869dc2
DE
11896 if (dwp_file->version == 1)
11897 {
ed2dc618
SM
11898 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
11899 dwp_file, unit_index,
73869dc2
DE
11900 comp_dir, signature,
11901 is_debug_types);
11902 }
11903 else
11904 {
ed2dc618
SM
11905 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
11906 dwp_file, unit_index,
73869dc2
DE
11907 comp_dir, signature,
11908 is_debug_types);
11909 }
9a3c8263 11910 return (struct dwo_unit *) *slot;
80626a55
DE
11911 }
11912 if (signature_in_table == 0)
11913 return NULL;
11914 hash = (hash + hash2) & mask;
11915 }
11916
11917 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
11918 " [in module %s]"),
11919 dwp_file->name);
11920}
11921
ab5088bf 11922/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
11923 Open the file specified by FILE_NAME and hand it off to BFD for
11924 preliminary analysis. Return a newly initialized bfd *, which
11925 includes a canonicalized copy of FILE_NAME.
80626a55 11926 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
11927 SEARCH_CWD is true if the current directory is to be searched.
11928 It will be searched before debug-file-directory.
13aaf454
DE
11929 If successful, the file is added to the bfd include table of the
11930 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 11931 If unable to find/open the file, return NULL.
3019eac3
DE
11932 NOTE: This function is derived from symfile_bfd_open. */
11933
192b62ce 11934static gdb_bfd_ref_ptr
ed2dc618
SM
11935try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11936 const char *file_name, int is_dwp, int search_cwd)
3019eac3 11937{
24b9144d 11938 int desc;
9c02c129
DE
11939 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
11940 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
11941 to debug_file_directory. */
e0cc99a6 11942 const char *search_path;
9c02c129
DE
11943 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
11944
e0cc99a6 11945 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
11946 if (search_cwd)
11947 {
11948 if (*debug_file_directory != '\0')
e0cc99a6
TT
11949 {
11950 search_path_holder.reset (concat (".", dirname_separator_string,
11951 debug_file_directory,
11952 (char *) NULL));
11953 search_path = search_path_holder.get ();
11954 }
6ac97d4c 11955 else
e0cc99a6 11956 search_path = ".";
6ac97d4c 11957 }
9c02c129 11958 else
e0cc99a6 11959 search_path = debug_file_directory;
3019eac3 11960
24b9144d 11961 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
11962 if (is_dwp)
11963 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
11964
11965 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 11966 desc = openp (search_path, flags, file_name,
3019eac3
DE
11967 O_RDONLY | O_BINARY, &absolute_name);
11968 if (desc < 0)
11969 return NULL;
11970
e0cc99a6
TT
11971 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
11972 gnutarget, desc));
9c02c129
DE
11973 if (sym_bfd == NULL)
11974 return NULL;
192b62ce 11975 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 11976
192b62ce
TT
11977 if (!bfd_check_format (sym_bfd.get (), bfd_object))
11978 return NULL;
3019eac3 11979
13aaf454
DE
11980 /* Success. Record the bfd as having been included by the objfile's bfd.
11981 This is important because things like demangled_names_hash lives in the
11982 objfile's per_bfd space and may have references to things like symbol
11983 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 11984 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 11985
3019eac3
DE
11986 return sym_bfd;
11987}
11988
ab5088bf 11989/* Try to open DWO file FILE_NAME.
3019eac3
DE
11990 COMP_DIR is the DW_AT_comp_dir attribute.
11991 The result is the bfd handle of the file.
11992 If there is a problem finding or opening the file, return NULL.
11993 Upon success, the canonicalized path of the file is stored in the bfd,
11994 same as symfile_bfd_open. */
11995
192b62ce 11996static gdb_bfd_ref_ptr
ed2dc618
SM
11997open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
11998 const char *file_name, const char *comp_dir)
3019eac3 11999{
80626a55 12000 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12001 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12002 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12003
12004 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12005
12006 if (comp_dir != NULL)
12007 {
43816ebc
TT
12008 gdb::unique_xmalloc_ptr<char> path_to_try
12009 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12010
12011 /* NOTE: If comp_dir is a relative path, this will also try the
12012 search path, which seems useful. */
ed2dc618 12013 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12014 path_to_try.get (),
ed2dc618 12015 0 /*is_dwp*/,
192b62ce 12016 1 /*search_cwd*/));
3019eac3
DE
12017 if (abfd != NULL)
12018 return abfd;
12019 }
12020
12021 /* That didn't work, try debug-file-directory, which, despite its name,
12022 is a list of paths. */
12023
12024 if (*debug_file_directory == '\0')
12025 return NULL;
12026
ed2dc618
SM
12027 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12028 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12029}
12030
80626a55
DE
12031/* This function is mapped across the sections and remembers the offset and
12032 size of each of the DWO debugging sections we are interested in. */
12033
12034static void
12035dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12036{
9a3c8263 12037 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12038 const struct dwop_section_names *names = &dwop_section_names;
12039
12040 if (section_is_p (sectp->name, &names->abbrev_dwo))
12041 {
049412e3 12042 dwo_sections->abbrev.s.section = sectp;
fd361982 12043 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12044 }
12045 else if (section_is_p (sectp->name, &names->info_dwo))
12046 {
049412e3 12047 dwo_sections->info.s.section = sectp;
fd361982 12048 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12049 }
12050 else if (section_is_p (sectp->name, &names->line_dwo))
12051 {
049412e3 12052 dwo_sections->line.s.section = sectp;
fd361982 12053 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12054 }
12055 else if (section_is_p (sectp->name, &names->loc_dwo))
12056 {
049412e3 12057 dwo_sections->loc.s.section = sectp;
fd361982 12058 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12059 }
12060 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12061 {
049412e3 12062 dwo_sections->macinfo.s.section = sectp;
fd361982 12063 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12064 }
12065 else if (section_is_p (sectp->name, &names->macro_dwo))
12066 {
049412e3 12067 dwo_sections->macro.s.section = sectp;
fd361982 12068 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12069 }
12070 else if (section_is_p (sectp->name, &names->str_dwo))
12071 {
049412e3 12072 dwo_sections->str.s.section = sectp;
fd361982 12073 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12074 }
12075 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12076 {
049412e3 12077 dwo_sections->str_offsets.s.section = sectp;
fd361982 12078 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12079 }
12080 else if (section_is_p (sectp->name, &names->types_dwo))
12081 {
12082 struct dwarf2_section_info type_section;
12083
12084 memset (&type_section, 0, sizeof (type_section));
049412e3 12085 type_section.s.section = sectp;
fd361982 12086 type_section.size = bfd_section_size (sectp);
fd5866f6 12087 dwo_sections->types.push_back (type_section);
80626a55
DE
12088 }
12089}
12090
ab5088bf 12091/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12092 by PER_CU. This is for the non-DWP case.
80626a55 12093 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12094
12095static struct dwo_file *
0ac5b59e
DE
12096open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12097 const char *dwo_name, const char *comp_dir)
3019eac3 12098{
ed2dc618 12099 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12100
fb1eb2f9 12101 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12102 if (dbfd == NULL)
12103 {
b4f54984 12104 if (dwarf_read_debug)
80626a55
DE
12105 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12106 return NULL;
12107 }
263db9a1 12108
51ac9db5 12109 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12110 dwo_file->dwo_name = dwo_name;
12111 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12112 dwo_file->dbfd = std::move (dbfd);
3019eac3 12113
fb1eb2f9 12114 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12115 &dwo_file->sections);
3019eac3 12116
18a8505e
AT
12117 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12118 dwo_file->sections.info, dwo_file->cus);
3019eac3 12119
263db9a1 12120 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12121 dwo_file->sections.types, dwo_file->tus);
3019eac3 12122
b4f54984 12123 if (dwarf_read_debug)
80626a55
DE
12124 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12125
263db9a1 12126 return dwo_file.release ();
3019eac3
DE
12127}
12128
80626a55 12129/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12130 size of each of the DWP debugging sections common to version 1 and 2 that
12131 we are interested in. */
3019eac3 12132
80626a55 12133static void
73869dc2
DE
12134dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12135 void *dwp_file_ptr)
3019eac3 12136{
9a3c8263 12137 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12138 const struct dwop_section_names *names = &dwop_section_names;
12139 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12140
80626a55 12141 /* Record the ELF section number for later lookup: this is what the
73869dc2 12142 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12143 gdb_assert (elf_section_nr < dwp_file->num_sections);
12144 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12145
80626a55
DE
12146 /* Look for specific sections that we need. */
12147 if (section_is_p (sectp->name, &names->str_dwo))
12148 {
049412e3 12149 dwp_file->sections.str.s.section = sectp;
fd361982 12150 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12151 }
12152 else if (section_is_p (sectp->name, &names->cu_index))
12153 {
049412e3 12154 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12155 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12156 }
12157 else if (section_is_p (sectp->name, &names->tu_index))
12158 {
049412e3 12159 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12160 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12161 }
12162}
3019eac3 12163
73869dc2
DE
12164/* This function is mapped across the sections and remembers the offset and
12165 size of each of the DWP version 2 debugging sections that we are interested
12166 in. This is split into a separate function because we don't know if we
12167 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12168
12169static void
12170dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12171{
9a3c8263 12172 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12173 const struct dwop_section_names *names = &dwop_section_names;
12174 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12175
12176 /* Record the ELF section number for later lookup: this is what the
12177 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12178 gdb_assert (elf_section_nr < dwp_file->num_sections);
12179 dwp_file->elf_sections[elf_section_nr] = sectp;
12180
12181 /* Look for specific sections that we need. */
12182 if (section_is_p (sectp->name, &names->abbrev_dwo))
12183 {
049412e3 12184 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12185 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12186 }
12187 else if (section_is_p (sectp->name, &names->info_dwo))
12188 {
049412e3 12189 dwp_file->sections.info.s.section = sectp;
fd361982 12190 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12191 }
12192 else if (section_is_p (sectp->name, &names->line_dwo))
12193 {
049412e3 12194 dwp_file->sections.line.s.section = sectp;
fd361982 12195 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12196 }
12197 else if (section_is_p (sectp->name, &names->loc_dwo))
12198 {
049412e3 12199 dwp_file->sections.loc.s.section = sectp;
fd361982 12200 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12201 }
12202 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12203 {
049412e3 12204 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12205 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12206 }
12207 else if (section_is_p (sectp->name, &names->macro_dwo))
12208 {
049412e3 12209 dwp_file->sections.macro.s.section = sectp;
fd361982 12210 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12211 }
12212 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12213 {
049412e3 12214 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12215 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12216 }
12217 else if (section_is_p (sectp->name, &names->types_dwo))
12218 {
049412e3 12219 dwp_file->sections.types.s.section = sectp;
fd361982 12220 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12221 }
12222}
12223
80626a55 12224/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12225
80626a55
DE
12226static hashval_t
12227hash_dwp_loaded_cutus (const void *item)
12228{
9a3c8263 12229 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12230
80626a55
DE
12231 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12232 return dwo_unit->signature;
3019eac3
DE
12233}
12234
80626a55 12235/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12236
80626a55
DE
12237static int
12238eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12239{
9a3c8263
SM
12240 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12241 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12242
80626a55
DE
12243 return dua->signature == dub->signature;
12244}
3019eac3 12245
80626a55 12246/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12247
48b490f2 12248static htab_up
298e9637 12249allocate_dwp_loaded_cutus_table ()
80626a55 12250{
48b490f2
TT
12251 return htab_up (htab_create_alloc (3,
12252 hash_dwp_loaded_cutus,
12253 eq_dwp_loaded_cutus,
12254 NULL, xcalloc, xfree));
80626a55 12255}
3019eac3 12256
ab5088bf
DE
12257/* Try to open DWP file FILE_NAME.
12258 The result is the bfd handle of the file.
12259 If there is a problem finding or opening the file, return NULL.
12260 Upon success, the canonicalized path of the file is stored in the bfd,
12261 same as symfile_bfd_open. */
12262
192b62ce 12263static gdb_bfd_ref_ptr
ed2dc618
SM
12264open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12265 const char *file_name)
ab5088bf 12266{
ed2dc618
SM
12267 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12268 1 /*is_dwp*/,
192b62ce 12269 1 /*search_cwd*/));
6ac97d4c
DE
12270 if (abfd != NULL)
12271 return abfd;
12272
12273 /* Work around upstream bug 15652.
12274 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12275 [Whether that's a "bug" is debatable, but it is getting in our way.]
12276 We have no real idea where the dwp file is, because gdb's realpath-ing
12277 of the executable's path may have discarded the needed info.
12278 [IWBN if the dwp file name was recorded in the executable, akin to
12279 .gnu_debuglink, but that doesn't exist yet.]
12280 Strip the directory from FILE_NAME and search again. */
12281 if (*debug_file_directory != '\0')
12282 {
12283 /* Don't implicitly search the current directory here.
12284 If the user wants to search "." to handle this case,
12285 it must be added to debug-file-directory. */
ed2dc618
SM
12286 return try_open_dwop_file (dwarf2_per_objfile,
12287 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12288 0 /*search_cwd*/);
12289 }
12290
12291 return NULL;
ab5088bf
DE
12292}
12293
80626a55
DE
12294/* Initialize the use of the DWP file for the current objfile.
12295 By convention the name of the DWP file is ${objfile}.dwp.
12296 The result is NULL if it can't be found. */
a766d390 12297
400174b1 12298static std::unique_ptr<struct dwp_file>
ed2dc618 12299open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12300{
12301 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12302
82bf32bc
JK
12303 /* Try to find first .dwp for the binary file before any symbolic links
12304 resolving. */
6c447423
DE
12305
12306 /* If the objfile is a debug file, find the name of the real binary
12307 file and get the name of dwp file from there. */
d721ba37 12308 std::string dwp_name;
6c447423
DE
12309 if (objfile->separate_debug_objfile_backlink != NULL)
12310 {
12311 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12312 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12313
d721ba37 12314 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12315 }
12316 else
d721ba37
PA
12317 dwp_name = objfile->original_name;
12318
12319 dwp_name += ".dwp";
80626a55 12320
ed2dc618 12321 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12322 if (dbfd == NULL
12323 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12324 {
12325 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12326 dwp_name = objfile_name (objfile);
12327 dwp_name += ".dwp";
ed2dc618 12328 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12329 }
12330
80626a55
DE
12331 if (dbfd == NULL)
12332 {
b4f54984 12333 if (dwarf_read_debug)
d721ba37 12334 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12335 return std::unique_ptr<dwp_file> ();
3019eac3 12336 }
400174b1
TT
12337
12338 const char *name = bfd_get_filename (dbfd.get ());
12339 std::unique_ptr<struct dwp_file> dwp_file
12340 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12341
0a0f4c01 12342 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12343 dwp_file->elf_sections =
12344 OBSTACK_CALLOC (&objfile->objfile_obstack,
12345 dwp_file->num_sections, asection *);
12346
400174b1
TT
12347 bfd_map_over_sections (dwp_file->dbfd.get (),
12348 dwarf2_locate_common_dwp_sections,
12349 dwp_file.get ());
80626a55 12350
400174b1
TT
12351 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12352 0);
80626a55 12353
400174b1
TT
12354 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12355 1);
80626a55 12356
73869dc2 12357 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12358 if (dwp_file->cus && dwp_file->tus
12359 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12360 {
12361 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12362 pretty bizarre. We use pulongest here because that's the established
4d65956b 12363 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12364 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12365 " TU version %s [in DWP file %s]"),
12366 pulongest (dwp_file->cus->version),
d721ba37 12367 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12368 }
08302ed2
DE
12369
12370 if (dwp_file->cus)
12371 dwp_file->version = dwp_file->cus->version;
12372 else if (dwp_file->tus)
12373 dwp_file->version = dwp_file->tus->version;
12374 else
12375 dwp_file->version = 2;
73869dc2
DE
12376
12377 if (dwp_file->version == 2)
400174b1
TT
12378 bfd_map_over_sections (dwp_file->dbfd.get (),
12379 dwarf2_locate_v2_dwp_sections,
12380 dwp_file.get ());
73869dc2 12381
298e9637
SM
12382 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12383 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12384
b4f54984 12385 if (dwarf_read_debug)
80626a55
DE
12386 {
12387 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12388 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12389 " %s CUs, %s TUs\n",
12390 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12391 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12392 }
12393
12394 return dwp_file;
3019eac3 12395}
c906108c 12396
ab5088bf
DE
12397/* Wrapper around open_and_init_dwp_file, only open it once. */
12398
12399static struct dwp_file *
ed2dc618 12400get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12401{
12402 if (! dwarf2_per_objfile->dwp_checked)
12403 {
ed2dc618
SM
12404 dwarf2_per_objfile->dwp_file
12405 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12406 dwarf2_per_objfile->dwp_checked = 1;
12407 }
400174b1 12408 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12409}
12410
80626a55
DE
12411/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12412 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12413 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12414 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12415 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12416
12417 This is called, for example, when wanting to read a variable with a
12418 complex location. Therefore we don't want to do file i/o for every call.
12419 Therefore we don't want to look for a DWO file on every call.
12420 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12421 then we check if we've already seen DWO_NAME, and only THEN do we check
12422 for a DWO file.
12423
1c658ad5 12424 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12425 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12426
3019eac3 12427static struct dwo_unit *
80626a55
DE
12428lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12429 const char *dwo_name, const char *comp_dir,
12430 ULONGEST signature, int is_debug_types)
3019eac3 12431{
ed2dc618 12432 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12433 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12434 const char *kind = is_debug_types ? "TU" : "CU";
12435 void **dwo_file_slot;
3019eac3 12436 struct dwo_file *dwo_file;
80626a55 12437 struct dwp_file *dwp_file;
cb1df416 12438
6a506a2d
DE
12439 /* First see if there's a DWP file.
12440 If we have a DWP file but didn't find the DWO inside it, don't
12441 look for the original DWO file. It makes gdb behave differently
12442 depending on whether one is debugging in the build tree. */
cf2c3c16 12443
ed2dc618 12444 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12445 if (dwp_file != NULL)
cf2c3c16 12446 {
80626a55
DE
12447 const struct dwp_hash_table *dwp_htab =
12448 is_debug_types ? dwp_file->tus : dwp_file->cus;
12449
12450 if (dwp_htab != NULL)
12451 {
12452 struct dwo_unit *dwo_cutu =
ed2dc618 12453 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12454 signature, is_debug_types);
80626a55
DE
12455
12456 if (dwo_cutu != NULL)
12457 {
b4f54984 12458 if (dwarf_read_debug)
80626a55
DE
12459 {
12460 fprintf_unfiltered (gdb_stdlog,
12461 "Virtual DWO %s %s found: @%s\n",
12462 kind, hex_string (signature),
12463 host_address_to_string (dwo_cutu));
12464 }
12465 return dwo_cutu;
12466 }
12467 }
12468 }
6a506a2d 12469 else
80626a55 12470 {
6a506a2d 12471 /* No DWP file, look for the DWO file. */
80626a55 12472
ed2dc618
SM
12473 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12474 dwo_name, comp_dir);
6a506a2d 12475 if (*dwo_file_slot == NULL)
80626a55 12476 {
6a506a2d
DE
12477 /* Read in the file and build a table of the CUs/TUs it contains. */
12478 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12479 }
6a506a2d 12480 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12481 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12482
6a506a2d 12483 if (dwo_file != NULL)
19c3d4c9 12484 {
6a506a2d
DE
12485 struct dwo_unit *dwo_cutu = NULL;
12486
12487 if (is_debug_types && dwo_file->tus)
12488 {
12489 struct dwo_unit find_dwo_cutu;
12490
12491 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12492 find_dwo_cutu.signature = signature;
9a3c8263 12493 dwo_cutu
b0b6a987
TT
12494 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12495 &find_dwo_cutu);
6a506a2d 12496 }
33c5cd75 12497 else if (!is_debug_types && dwo_file->cus)
80626a55 12498 {
33c5cd75
DB
12499 struct dwo_unit find_dwo_cutu;
12500
12501 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12502 find_dwo_cutu.signature = signature;
b0b6a987 12503 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12504 &find_dwo_cutu);
6a506a2d
DE
12505 }
12506
12507 if (dwo_cutu != NULL)
12508 {
b4f54984 12509 if (dwarf_read_debug)
6a506a2d
DE
12510 {
12511 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12512 kind, dwo_name, hex_string (signature),
12513 host_address_to_string (dwo_cutu));
12514 }
12515 return dwo_cutu;
80626a55
DE
12516 }
12517 }
2e276125 12518 }
9cdd5dbd 12519
80626a55
DE
12520 /* We didn't find it. This could mean a dwo_id mismatch, or
12521 someone deleted the DWO/DWP file, or the search path isn't set up
12522 correctly to find the file. */
12523
b4f54984 12524 if (dwarf_read_debug)
80626a55
DE
12525 {
12526 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12527 kind, dwo_name, hex_string (signature));
12528 }
3019eac3 12529
6656a72d
DE
12530 /* This is a warning and not a complaint because it can be caused by
12531 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12532 {
12533 /* Print the name of the DWP file if we looked there, helps the user
12534 better diagnose the problem. */
791afaa2 12535 std::string dwp_text;
43942612
DE
12536
12537 if (dwp_file != NULL)
791afaa2
TT
12538 dwp_text = string_printf (" [in DWP file %s]",
12539 lbasename (dwp_file->name));
43942612 12540
9d8780f0 12541 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12542 " [in module %s]"),
12543 kind, dwo_name, hex_string (signature),
791afaa2 12544 dwp_text.c_str (),
43942612 12545 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12546 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12547 }
3019eac3 12548 return NULL;
5fb290d7
DJ
12549}
12550
80626a55
DE
12551/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12552 See lookup_dwo_cutu_unit for details. */
12553
12554static struct dwo_unit *
12555lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12556 const char *dwo_name, const char *comp_dir,
12557 ULONGEST signature)
12558{
12559 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12560}
12561
12562/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12563 See lookup_dwo_cutu_unit for details. */
12564
12565static struct dwo_unit *
12566lookup_dwo_type_unit (struct signatured_type *this_tu,
12567 const char *dwo_name, const char *comp_dir)
12568{
12569 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12570}
12571
89e63ee4
DE
12572/* Traversal function for queue_and_load_all_dwo_tus. */
12573
12574static int
12575queue_and_load_dwo_tu (void **slot, void *info)
12576{
12577 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12578 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12579 ULONGEST signature = dwo_unit->signature;
12580 struct signatured_type *sig_type =
12581 lookup_dwo_signatured_type (per_cu->cu, signature);
12582
12583 if (sig_type != NULL)
12584 {
12585 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12586
12587 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12588 a real dependency of PER_CU on SIG_TYPE. That is detected later
12589 while processing PER_CU. */
12590 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12591 load_full_type_unit (sig_cu);
ae640021 12592 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12593 }
12594
12595 return 1;
12596}
12597
12598/* Queue all TUs contained in the DWO of PER_CU to be read in.
12599 The DWO may have the only definition of the type, though it may not be
12600 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12601 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12602
12603static void
12604queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12605{
12606 struct dwo_unit *dwo_unit;
12607 struct dwo_file *dwo_file;
12608
12609 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12610 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12611 gdb_assert (per_cu->cu != NULL);
12612
12613 dwo_unit = per_cu->cu->dwo_unit;
12614 gdb_assert (dwo_unit != NULL);
12615
12616 dwo_file = dwo_unit->dwo_file;
12617 if (dwo_file->tus != NULL)
b0b6a987
TT
12618 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12619 per_cu);
89e63ee4
DE
12620}
12621
3019eac3 12622/* Read in various DIEs. */
348e048f 12623
d389af10 12624/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12625 Inherit only the children of the DW_AT_abstract_origin DIE not being
12626 already referenced by DW_AT_abstract_origin from the children of the
12627 current DIE. */
d389af10
JK
12628
12629static void
12630inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12631{
12632 struct die_info *child_die;
791afaa2 12633 sect_offset *offsetp;
d389af10
JK
12634 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12635 struct die_info *origin_die;
12636 /* Iterator of the ORIGIN_DIE children. */
12637 struct die_info *origin_child_die;
d389af10 12638 struct attribute *attr;
cd02d79d
PA
12639 struct dwarf2_cu *origin_cu;
12640 struct pending **origin_previous_list_in_scope;
d389af10
JK
12641
12642 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12643 if (!attr)
12644 return;
12645
cd02d79d
PA
12646 /* Note that following die references may follow to a die in a
12647 different cu. */
12648
12649 origin_cu = cu;
12650 origin_die = follow_die_ref (die, attr, &origin_cu);
12651
12652 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12653 symbols in. */
12654 origin_previous_list_in_scope = origin_cu->list_in_scope;
12655 origin_cu->list_in_scope = cu->list_in_scope;
12656
edb3359d
DJ
12657 if (die->tag != origin_die->tag
12658 && !(die->tag == DW_TAG_inlined_subroutine
12659 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12660 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12661 sect_offset_str (die->sect_off),
12662 sect_offset_str (origin_die->sect_off));
d389af10 12663
791afaa2 12664 std::vector<sect_offset> offsets;
d389af10 12665
3ea89b92
PMR
12666 for (child_die = die->child;
12667 child_die && child_die->tag;
436c571c 12668 child_die = child_die->sibling)
3ea89b92
PMR
12669 {
12670 struct die_info *child_origin_die;
12671 struct dwarf2_cu *child_origin_cu;
12672
12673 /* We are trying to process concrete instance entries:
216f72a1 12674 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12675 it's not relevant to our analysis here. i.e. detecting DIEs that are
12676 present in the abstract instance but not referenced in the concrete
12677 one. */
216f72a1
JK
12678 if (child_die->tag == DW_TAG_call_site
12679 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12680 continue;
12681
c38f313d
DJ
12682 /* For each CHILD_DIE, find the corresponding child of
12683 ORIGIN_DIE. If there is more than one layer of
12684 DW_AT_abstract_origin, follow them all; there shouldn't be,
12685 but GCC versions at least through 4.4 generate this (GCC PR
12686 40573). */
3ea89b92
PMR
12687 child_origin_die = child_die;
12688 child_origin_cu = cu;
c38f313d
DJ
12689 while (1)
12690 {
cd02d79d
PA
12691 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12692 child_origin_cu);
c38f313d
DJ
12693 if (attr == NULL)
12694 break;
cd02d79d
PA
12695 child_origin_die = follow_die_ref (child_origin_die, attr,
12696 &child_origin_cu);
c38f313d
DJ
12697 }
12698
d389af10
JK
12699 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12700 counterpart may exist. */
c38f313d 12701 if (child_origin_die != child_die)
d389af10 12702 {
edb3359d
DJ
12703 if (child_die->tag != child_origin_die->tag
12704 && !(child_die->tag == DW_TAG_inlined_subroutine
12705 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12706 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12707 "different tags"),
9d8780f0
SM
12708 sect_offset_str (child_die->sect_off),
12709 sect_offset_str (child_origin_die->sect_off));
c38f313d 12710 if (child_origin_die->parent != origin_die)
b98664d3 12711 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12712 "different parents"),
9d8780f0
SM
12713 sect_offset_str (child_die->sect_off),
12714 sect_offset_str (child_origin_die->sect_off));
c38f313d 12715 else
791afaa2 12716 offsets.push_back (child_origin_die->sect_off);
d389af10 12717 }
d389af10 12718 }
791afaa2
TT
12719 std::sort (offsets.begin (), offsets.end ());
12720 sect_offset *offsets_end = offsets.data () + offsets.size ();
12721 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12722 if (offsetp[-1] == *offsetp)
b98664d3 12723 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12724 "to DIE %s as their abstract origin"),
12725 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12726
791afaa2 12727 offsetp = offsets.data ();
d389af10
JK
12728 origin_child_die = origin_die->child;
12729 while (origin_child_die && origin_child_die->tag)
12730 {
12731 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12732 while (offsetp < offsets_end
9c541725 12733 && *offsetp < origin_child_die->sect_off)
d389af10 12734 offsetp++;
b64f50a1 12735 if (offsetp >= offsets_end
9c541725 12736 || *offsetp > origin_child_die->sect_off)
d389af10 12737 {
adde2bff
DE
12738 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12739 Check whether we're already processing ORIGIN_CHILD_DIE.
12740 This can happen with mutually referenced abstract_origins.
12741 PR 16581. */
12742 if (!origin_child_die->in_process)
12743 process_die (origin_child_die, origin_cu);
d389af10 12744 }
436c571c 12745 origin_child_die = origin_child_die->sibling;
d389af10 12746 }
cd02d79d 12747 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12748
12749 if (cu != origin_cu)
12750 compute_delayed_physnames (origin_cu);
d389af10
JK
12751}
12752
c906108c 12753static void
e7c27a73 12754read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12755{
518817b3 12756 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
fe978cb0 12758 struct context_stack *newobj;
c906108c
SS
12759 CORE_ADDR lowpc;
12760 CORE_ADDR highpc;
12761 struct die_info *child_die;
edb3359d 12762 struct attribute *attr, *call_line, *call_file;
15d034d0 12763 const char *name;
e142c38c 12764 CORE_ADDR baseaddr;
801e3a5b 12765 struct block *block;
edb3359d 12766 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12767 std::vector<struct symbol *> template_args;
34eaf542 12768 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12769
12770 if (inlined_func)
12771 {
12772 /* If we do not have call site information, we can't show the
12773 caller of this inlined function. That's too confusing, so
12774 only use the scope for local variables. */
12775 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12776 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12777 if (call_line == NULL || call_file == NULL)
12778 {
12779 read_lexical_block_scope (die, cu);
12780 return;
12781 }
12782 }
c906108c 12783
b3b3bada 12784 baseaddr = objfile->text_section_offset ();
e142c38c 12785
94af9270 12786 name = dwarf2_name (die, cu);
c906108c 12787
e8d05480
JB
12788 /* Ignore functions with missing or empty names. These are actually
12789 illegal according to the DWARF standard. */
12790 if (name == NULL)
12791 {
b98664d3 12792 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12793 sect_offset_str (die->sect_off));
e8d05480
JB
12794 return;
12795 }
12796
12797 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12798 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12799 <= PC_BOUNDS_INVALID)
e8d05480 12800 {
ae4d0c03
PM
12801 attr = dwarf2_attr (die, DW_AT_external, cu);
12802 if (!attr || !DW_UNSND (attr))
b98664d3 12803 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12804 "for subprogram DIE at %s"),
12805 sect_offset_str (die->sect_off));
e8d05480
JB
12806 return;
12807 }
c906108c 12808
3e29f34a
MR
12809 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12810 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12811
34eaf542
TT
12812 /* If we have any template arguments, then we must allocate a
12813 different sort of symbol. */
436c571c 12814 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12815 {
12816 if (child_die->tag == DW_TAG_template_type_param
12817 || child_die->tag == DW_TAG_template_value_param)
12818 {
e623cf5d 12819 templ_func = allocate_template_symbol (objfile);
cf724bc9 12820 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12821 break;
12822 }
12823 }
12824
c24bdb02 12825 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12826 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12827 (struct symbol *) templ_func);
4c2df51b 12828
81873cc8 12829 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12830 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12831 cu->language);
12832
4cecd739
DJ
12833 /* If there is a location expression for DW_AT_frame_base, record
12834 it. */
e142c38c 12835 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12836 if (attr != nullptr)
fe978cb0 12837 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12838
63e43d3a
PMR
12839 /* If there is a location for the static link, record it. */
12840 newobj->static_link = NULL;
12841 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12842 if (attr != nullptr)
63e43d3a 12843 {
224c3ddb
SM
12844 newobj->static_link
12845 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12846 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12847 cu->per_cu->addr_type ());
63e43d3a
PMR
12848 }
12849
c24bdb02 12850 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12851
639d11d3 12852 if (die->child != NULL)
c906108c 12853 {
639d11d3 12854 child_die = die->child;
c906108c
SS
12855 while (child_die && child_die->tag)
12856 {
34eaf542
TT
12857 if (child_die->tag == DW_TAG_template_type_param
12858 || child_die->tag == DW_TAG_template_value_param)
12859 {
12860 struct symbol *arg = new_symbol (child_die, NULL, cu);
12861
f1078f66 12862 if (arg != NULL)
2f4732b0 12863 template_args.push_back (arg);
34eaf542
TT
12864 }
12865 else
12866 process_die (child_die, cu);
436c571c 12867 child_die = child_die->sibling;
c906108c
SS
12868 }
12869 }
12870
d389af10
JK
12871 inherit_abstract_dies (die, cu);
12872
4a811a97
UW
12873 /* If we have a DW_AT_specification, we might need to import using
12874 directives from the context of the specification DIE. See the
12875 comment in determine_prefix. */
12876 if (cu->language == language_cplus
12877 && dwarf2_attr (die, DW_AT_specification, cu))
12878 {
12879 struct dwarf2_cu *spec_cu = cu;
12880 struct die_info *spec_die = die_specification (die, &spec_cu);
12881
12882 while (spec_die)
12883 {
12884 child_die = spec_die->child;
12885 while (child_die && child_die->tag)
12886 {
12887 if (child_die->tag == DW_TAG_imported_module)
12888 process_die (child_die, spec_cu);
436c571c 12889 child_die = child_die->sibling;
4a811a97
UW
12890 }
12891
12892 /* In some cases, GCC generates specification DIEs that
12893 themselves contain DW_AT_specification attributes. */
12894 spec_die = die_specification (spec_die, &spec_cu);
12895 }
12896 }
12897
c24bdb02 12898 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12899 /* Make a block for the local symbols within. */
c24bdb02 12900 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 12901 cstk.static_link, lowpc, highpc);
801e3a5b 12902
df8a16a1 12903 /* For C++, set the block's scope. */
45280282
IB
12904 if ((cu->language == language_cplus
12905 || cu->language == language_fortran
c44af4eb
TT
12906 || cu->language == language_d
12907 || cu->language == language_rust)
4d4ec4e5 12908 && cu->processing_has_namespace_info)
195a3f6c
TT
12909 block_set_scope (block, determine_prefix (die, cu),
12910 &objfile->objfile_obstack);
df8a16a1 12911
801e3a5b
JB
12912 /* If we have address ranges, record them. */
12913 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 12914
a60f3166 12915 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 12916
34eaf542 12917 /* Attach template arguments to function. */
2f4732b0 12918 if (!template_args.empty ())
34eaf542
TT
12919 {
12920 gdb_assert (templ_func != NULL);
12921
2f4732b0 12922 templ_func->n_template_arguments = template_args.size ();
34eaf542 12923 templ_func->template_arguments
8d749320
SM
12924 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
12925 templ_func->n_template_arguments);
34eaf542 12926 memcpy (templ_func->template_arguments,
2f4732b0 12927 template_args.data (),
34eaf542 12928 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
12929
12930 /* Make sure that the symtab is set on the new symbols. Even
12931 though they don't appear in this symtab directly, other parts
12932 of gdb assume that symbols do, and this is reasonably
12933 true. */
8634679f 12934 for (symbol *sym : template_args)
3e1d3d8c 12935 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
12936 }
12937
208d8187
JB
12938 /* In C++, we can have functions nested inside functions (e.g., when
12939 a function declares a class that has methods). This means that
12940 when we finish processing a function scope, we may need to go
12941 back to building a containing block's symbol lists. */
c24bdb02
KS
12942 *cu->get_builder ()->get_local_symbols () = cstk.locals;
12943 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 12944
921e78cf
JB
12945 /* If we've finished processing a top-level function, subsequent
12946 symbols go in the file symbol list. */
c24bdb02
KS
12947 if (cu->get_builder ()->outermost_context_p ())
12948 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
12949}
12950
12951/* Process all the DIES contained within a lexical block scope. Start
12952 a new scope, process the dies, and then close the scope. */
12953
12954static void
e7c27a73 12955read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12956{
518817b3 12957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 12958 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
12959 CORE_ADDR lowpc, highpc;
12960 struct die_info *child_die;
e142c38c
DJ
12961 CORE_ADDR baseaddr;
12962
b3b3bada 12963 baseaddr = objfile->text_section_offset ();
c906108c
SS
12964
12965 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
12966 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
12967 as multiple lexical blocks? Handling children in a sane way would
6e70227d 12968 be nasty. Might be easier to properly extend generic blocks to
af34e669 12969 describe ranges. */
e385593e
JK
12970 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
12971 {
12972 case PC_BOUNDS_NOT_PRESENT:
12973 /* DW_TAG_lexical_block has no attributes, process its children as if
12974 there was no wrapping by that DW_TAG_lexical_block.
12975 GCC does no longer produces such DWARF since GCC r224161. */
12976 for (child_die = die->child;
12977 child_die != NULL && child_die->tag;
436c571c 12978 child_die = child_die->sibling)
e385593e
JK
12979 process_die (child_die, cu);
12980 return;
12981 case PC_BOUNDS_INVALID:
12982 return;
12983 }
3e29f34a
MR
12984 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12985 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12986
c24bdb02 12987 cu->get_builder ()->push_context (0, lowpc);
639d11d3 12988 if (die->child != NULL)
c906108c 12989 {
639d11d3 12990 child_die = die->child;
c906108c
SS
12991 while (child_die && child_die->tag)
12992 {
e7c27a73 12993 process_die (child_die, cu);
436c571c 12994 child_die = child_die->sibling;
c906108c
SS
12995 }
12996 }
3ea89b92 12997 inherit_abstract_dies (die, cu);
c24bdb02 12998 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 12999
c24bdb02
KS
13000 if (*cu->get_builder ()->get_local_symbols () != NULL
13001 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13002 {
801e3a5b 13003 struct block *block
c24bdb02 13004 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13005 cstk.start_addr, highpc);
801e3a5b
JB
13006
13007 /* Note that recording ranges after traversing children, as we
13008 do here, means that recording a parent's ranges entails
13009 walking across all its children's ranges as they appear in
13010 the address map, which is quadratic behavior.
13011
13012 It would be nicer to record the parent's ranges before
13013 traversing its children, simply overriding whatever you find
13014 there. But since we don't even decide whether to create a
13015 block until after we've traversed its children, that's hard
13016 to do. */
13017 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13018 }
c24bdb02
KS
13019 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13020 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13021}
13022
216f72a1 13023/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13024
13025static void
13026read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13027{
518817b3 13028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
96408a79
SA
13029 struct gdbarch *gdbarch = get_objfile_arch (objfile);
13030 CORE_ADDR pc, baseaddr;
13031 struct attribute *attr;
13032 struct call_site *call_site, call_site_local;
13033 void **slot;
13034 int nparams;
13035 struct die_info *child_die;
13036
b3b3bada 13037 baseaddr = objfile->text_section_offset ();
96408a79 13038
216f72a1
JK
13039 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13040 if (attr == NULL)
13041 {
13042 /* This was a pre-DWARF-5 GNU extension alias
13043 for DW_AT_call_return_pc. */
13044 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13045 }
96408a79
SA
13046 if (!attr)
13047 {
b98664d3 13048 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13049 "DIE %s [in module %s]"),
13050 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13051 return;
13052 }
cd6c91b4 13053 pc = attr->value_as_address () + baseaddr;
3e29f34a 13054 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13055
13056 if (cu->call_site_htab == NULL)
13057 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13058 NULL, &objfile->objfile_obstack,
13059 hashtab_obstack_allocate, NULL);
13060 call_site_local.pc = pc;
13061 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13062 if (*slot != NULL)
13063 {
b98664d3 13064 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13065 "DIE %s [in module %s]"),
13066 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13067 objfile_name (objfile));
96408a79
SA
13068 return;
13069 }
13070
13071 /* Count parameters at the caller. */
13072
13073 nparams = 0;
13074 for (child_die = die->child; child_die && child_die->tag;
436c571c 13075 child_die = child_die->sibling)
96408a79 13076 {
216f72a1
JK
13077 if (child_die->tag != DW_TAG_call_site_parameter
13078 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13079 {
b98664d3 13080 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13081 "DW_TAG_call_site child DIE %s [in module %s]"),
13082 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13083 objfile_name (objfile));
96408a79
SA
13084 continue;
13085 }
13086
13087 nparams++;
13088 }
13089
224c3ddb
SM
13090 call_site
13091 = ((struct call_site *)
13092 obstack_alloc (&objfile->objfile_obstack,
13093 sizeof (*call_site)
13094 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13095 *slot = call_site;
13096 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13097 call_site->pc = pc;
13098
216f72a1
JK
13099 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13100 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13101 {
13102 struct die_info *func_die;
13103
13104 /* Skip also over DW_TAG_inlined_subroutine. */
13105 for (func_die = die->parent;
13106 func_die && func_die->tag != DW_TAG_subprogram
13107 && func_die->tag != DW_TAG_subroutine_type;
13108 func_die = func_die->parent);
13109
216f72a1
JK
13110 /* DW_AT_call_all_calls is a superset
13111 of DW_AT_call_all_tail_calls. */
96408a79 13112 if (func_die
216f72a1 13113 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13114 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13115 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13116 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13117 {
13118 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13119 not complete. But keep CALL_SITE for look ups via call_site_htab,
13120 both the initial caller containing the real return address PC and
13121 the final callee containing the current PC of a chain of tail
13122 calls do not need to have the tail call list complete. But any
13123 function candidate for a virtual tail call frame searched via
13124 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13125 determined unambiguously. */
13126 }
13127 else
13128 {
13129 struct type *func_type = NULL;
13130
13131 if (func_die)
13132 func_type = get_die_type (func_die, cu);
13133 if (func_type != NULL)
13134 {
13135 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13136
13137 /* Enlist this call site to the function. */
13138 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13139 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13140 }
13141 else
b98664d3 13142 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13143 "DIE %s [in module %s]"),
13144 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13145 }
13146 }
13147
216f72a1
JK
13148 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13149 if (attr == NULL)
13150 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13151 if (attr == NULL)
13152 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13153 if (attr == NULL)
216f72a1
JK
13154 {
13155 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13156 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13157 }
96408a79 13158 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13159 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13160 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13161 else if (attr->form_is_block ())
96408a79
SA
13162 {
13163 struct dwarf2_locexpr_baton *dlbaton;
13164
8d749320 13165 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13166 dlbaton->data = DW_BLOCK (attr)->data;
13167 dlbaton->size = DW_BLOCK (attr)->size;
13168 dlbaton->per_cu = cu->per_cu;
13169
13170 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13171 }
cd6c91b4 13172 else if (attr->form_is_ref ())
96408a79 13173 {
96408a79
SA
13174 struct dwarf2_cu *target_cu = cu;
13175 struct die_info *target_die;
13176
ac9ec31b 13177 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13178 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13179 if (die_is_declaration (target_die, target_cu))
13180 {
7d45c7c3 13181 const char *target_physname;
9112db09
JK
13182
13183 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13184 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13185 if (target_physname == NULL)
9112db09 13186 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13187 if (target_physname == NULL)
b98664d3 13188 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13189 "physname, for referencing DIE %s [in module %s]"),
13190 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13191 else
7d455152 13192 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13193 }
13194 else
13195 {
13196 CORE_ADDR lowpc;
13197
13198 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13199 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13200 <= PC_BOUNDS_INVALID)
b98664d3 13201 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13202 "low pc, for referencing DIE %s [in module %s]"),
13203 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13204 else
3e29f34a
MR
13205 {
13206 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13207 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13208 }
96408a79
SA
13209 }
13210 }
13211 else
b98664d3 13212 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13213 "block nor reference, for DIE %s [in module %s]"),
13214 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13215
13216 call_site->per_cu = cu->per_cu;
13217
13218 for (child_die = die->child;
13219 child_die && child_die->tag;
436c571c 13220 child_die = child_die->sibling)
96408a79 13221 {
96408a79 13222 struct call_site_parameter *parameter;
1788b2d3 13223 struct attribute *loc, *origin;
96408a79 13224
216f72a1
JK
13225 if (child_die->tag != DW_TAG_call_site_parameter
13226 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13227 {
13228 /* Already printed the complaint above. */
13229 continue;
13230 }
13231
13232 gdb_assert (call_site->parameter_count < nparams);
13233 parameter = &call_site->parameter[call_site->parameter_count];
13234
1788b2d3
JK
13235 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13236 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13237 register is contained in DW_AT_call_value. */
96408a79 13238
24c5c679 13239 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13240 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13241 if (origin == NULL)
13242 {
13243 /* This was a pre-DWARF-5 GNU extension alias
13244 for DW_AT_call_parameter. */
13245 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13246 }
cd6c91b4 13247 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13248 {
1788b2d3 13249 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13250
0826b30a 13251 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13252 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13253 {
13254 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13255 binding can be done only inside one CU. Such referenced DIE
13256 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13257 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13258 "DW_TAG_call_site child DIE %s [in module %s]"),
13259 sect_offset_str (child_die->sect_off),
9c541725 13260 objfile_name (objfile));
d76b7dbc
JK
13261 continue;
13262 }
9c541725
PA
13263 parameter->u.param_cu_off
13264 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13265 }
4fc6c0d5 13266 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13267 {
b98664d3 13268 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13269 "DW_TAG_call_site child DIE %s [in module %s]"),
13270 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13271 continue;
13272 }
24c5c679 13273 else
96408a79 13274 {
24c5c679
JK
13275 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13276 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13277 if (parameter->u.dwarf_reg != -1)
13278 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13279 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13280 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13281 &parameter->u.fb_offset))
13282 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13283 else
13284 {
b98664d3 13285 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13286 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13287 "DW_TAG_call_site child DIE %s "
24c5c679 13288 "[in module %s]"),
9d8780f0 13289 sect_offset_str (child_die->sect_off),
9c541725 13290 objfile_name (objfile));
24c5c679
JK
13291 continue;
13292 }
96408a79
SA
13293 }
13294
216f72a1
JK
13295 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13296 if (attr == NULL)
13297 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13298 if (attr == NULL || !attr->form_is_block ())
96408a79 13299 {
b98664d3 13300 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13301 "DW_TAG_call_site child DIE %s [in module %s]"),
13302 sect_offset_str (child_die->sect_off),
9c541725 13303 objfile_name (objfile));
96408a79
SA
13304 continue;
13305 }
13306 parameter->value = DW_BLOCK (attr)->data;
13307 parameter->value_size = DW_BLOCK (attr)->size;
13308
13309 /* Parameters are not pre-cleared by memset above. */
13310 parameter->data_value = NULL;
13311 parameter->data_value_size = 0;
13312 call_site->parameter_count++;
13313
216f72a1
JK
13314 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13315 if (attr == NULL)
13316 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13317 if (attr != nullptr)
96408a79 13318 {
4fc6c0d5 13319 if (!attr->form_is_block ())
b98664d3 13320 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13321 "DW_TAG_call_site child DIE %s [in module %s]"),
13322 sect_offset_str (child_die->sect_off),
9c541725 13323 objfile_name (objfile));
96408a79
SA
13324 else
13325 {
13326 parameter->data_value = DW_BLOCK (attr)->data;
13327 parameter->data_value_size = DW_BLOCK (attr)->size;
13328 }
13329 }
13330 }
13331}
13332
71a3c369
TT
13333/* Helper function for read_variable. If DIE represents a virtual
13334 table, then return the type of the concrete object that is
13335 associated with the virtual table. Otherwise, return NULL. */
13336
13337static struct type *
13338rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13339{
13340 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13341 if (attr == NULL)
13342 return NULL;
13343
13344 /* Find the type DIE. */
13345 struct die_info *type_die = NULL;
13346 struct dwarf2_cu *type_cu = cu;
13347
cd6c91b4 13348 if (attr->form_is_ref ())
71a3c369
TT
13349 type_die = follow_die_ref (die, attr, &type_cu);
13350 if (type_die == NULL)
13351 return NULL;
13352
13353 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13354 return NULL;
13355 return die_containing_type (type_die, type_cu);
13356}
13357
13358/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13359
13360static void
13361read_variable (struct die_info *die, struct dwarf2_cu *cu)
13362{
13363 struct rust_vtable_symbol *storage = NULL;
13364
13365 if (cu->language == language_rust)
13366 {
13367 struct type *containing_type = rust_containing_type (die, cu);
13368
13369 if (containing_type != NULL)
13370 {
518817b3 13371 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13372
468c0cbb 13373 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13374 initialize_objfile_symbol (storage);
13375 storage->concrete_type = containing_type;
cf724bc9 13376 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13377 }
13378 }
13379
e4a62c65
TV
13380 struct symbol *res = new_symbol (die, NULL, cu, storage);
13381 struct attribute *abstract_origin
13382 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13383 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13384 if (res == NULL && loc && abstract_origin)
13385 {
13386 /* We have a variable without a name, but with a location and an abstract
13387 origin. This may be a concrete instance of an abstract variable
13388 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13389 later. */
13390 struct dwarf2_cu *origin_cu = cu;
13391 struct die_info *origin_die
13392 = follow_die_ref (die, abstract_origin, &origin_cu);
13393 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13394 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13395 }
71a3c369
TT
13396}
13397
43988095
JK
13398/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13399 reading .debug_rnglists.
13400 Callback's type should be:
13401 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13402 Return true if the attributes are present and valid, otherwise,
13403 return false. */
13404
13405template <typename Callback>
13406static bool
13407dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13408 Callback &&callback)
13409{
ed2dc618 13410 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13411 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13412 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13413 bfd *obfd = objfile->obfd;
43988095 13414 /* Base address selection entry. */
2b24b6e4 13415 gdb::optional<CORE_ADDR> base;
43988095 13416 const gdb_byte *buffer;
43988095
JK
13417 CORE_ADDR baseaddr;
13418 bool overflow = false;
13419
43988095
JK
13420 base = cu->base_address;
13421
96b79293 13422 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13423 if (offset >= dwarf2_per_objfile->rnglists.size)
13424 {
b98664d3 13425 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13426 offset);
13427 return false;
13428 }
13429 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13430
b3b3bada 13431 baseaddr = objfile->text_section_offset ();
43988095
JK
13432
13433 while (1)
13434 {
7814882a
JK
13435 /* Initialize it due to a false compiler warning. */
13436 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13437 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13438 + dwarf2_per_objfile->rnglists.size);
13439 unsigned int bytes_read;
13440
13441 if (buffer == buf_end)
13442 {
13443 overflow = true;
13444 break;
13445 }
13446 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13447 switch (rlet)
13448 {
13449 case DW_RLE_end_of_list:
13450 break;
13451 case DW_RLE_base_address:
13452 if (buffer + cu->header.addr_size > buf_end)
13453 {
13454 overflow = true;
13455 break;
13456 }
c8a7a66f 13457 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13458 buffer += bytes_read;
13459 break;
13460 case DW_RLE_start_length:
13461 if (buffer + cu->header.addr_size > buf_end)
13462 {
13463 overflow = true;
13464 break;
13465 }
c8a7a66f
TT
13466 range_beginning = cu->header.read_address (obfd, buffer,
13467 &bytes_read);
43988095
JK
13468 buffer += bytes_read;
13469 range_end = (range_beginning
13470 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13471 buffer += bytes_read;
13472 if (buffer > buf_end)
13473 {
13474 overflow = true;
13475 break;
13476 }
13477 break;
13478 case DW_RLE_offset_pair:
13479 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13480 buffer += bytes_read;
13481 if (buffer > buf_end)
13482 {
13483 overflow = true;
13484 break;
13485 }
13486 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13487 buffer += bytes_read;
13488 if (buffer > buf_end)
13489 {
13490 overflow = true;
13491 break;
13492 }
13493 break;
13494 case DW_RLE_start_end:
13495 if (buffer + 2 * cu->header.addr_size > buf_end)
13496 {
13497 overflow = true;
13498 break;
13499 }
c8a7a66f
TT
13500 range_beginning = cu->header.read_address (obfd, buffer,
13501 &bytes_read);
43988095 13502 buffer += bytes_read;
c8a7a66f 13503 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13504 buffer += bytes_read;
13505 break;
13506 default:
b98664d3 13507 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13508 return false;
13509 }
13510 if (rlet == DW_RLE_end_of_list || overflow)
13511 break;
13512 if (rlet == DW_RLE_base_address)
13513 continue;
13514
2b24b6e4 13515 if (!base.has_value ())
43988095
JK
13516 {
13517 /* We have no valid base address for the ranges
13518 data. */
b98664d3 13519 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13520 return false;
13521 }
13522
13523 if (range_beginning > range_end)
13524 {
13525 /* Inverted range entries are invalid. */
b98664d3 13526 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13527 return false;
13528 }
13529
13530 /* Empty range entries have no effect. */
13531 if (range_beginning == range_end)
13532 continue;
13533
2b24b6e4
TT
13534 range_beginning += *base;
13535 range_end += *base;
43988095
JK
13536
13537 /* A not-uncommon case of bad debug info.
13538 Don't pollute the addrmap with bad data. */
13539 if (range_beginning + baseaddr == 0
13540 && !dwarf2_per_objfile->has_section_at_zero)
13541 {
b98664d3 13542 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13543 " [in module %s]"), objfile_name (objfile));
13544 continue;
13545 }
13546
13547 callback (range_beginning, range_end);
13548 }
13549
13550 if (overflow)
13551 {
b98664d3 13552 complaint (_("Offset %d is not terminated "
43988095
JK
13553 "for DW_AT_ranges attribute"),
13554 offset);
13555 return false;
13556 }
13557
13558 return true;
13559}
13560
13561/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13562 Callback's type should be:
13563 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13564 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13565
43988095 13566template <typename Callback>
43039443 13567static int
5f46c5a5 13568dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13569 Callback &&callback)
43039443 13570{
ed2dc618 13571 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13572 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13573 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13574 struct comp_unit_head *cu_header = &cu->header;
13575 bfd *obfd = objfile->obfd;
13576 unsigned int addr_size = cu_header->addr_size;
13577 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13578 /* Base address selection entry. */
2b24b6e4 13579 gdb::optional<CORE_ADDR> base;
43039443 13580 unsigned int dummy;
d521ce57 13581 const gdb_byte *buffer;
ff013f42 13582 CORE_ADDR baseaddr;
43039443 13583
43988095
JK
13584 if (cu_header->version >= 5)
13585 return dwarf2_rnglists_process (offset, cu, callback);
13586
d00adf39 13587 base = cu->base_address;
43039443 13588
96b79293 13589 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13590 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13591 {
b98664d3 13592 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13593 offset);
13594 return 0;
13595 }
dce234bc 13596 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13597
b3b3bada 13598 baseaddr = objfile->text_section_offset ();
ff013f42 13599
43039443
JK
13600 while (1)
13601 {
13602 CORE_ADDR range_beginning, range_end;
13603
c8a7a66f 13604 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13605 buffer += addr_size;
c8a7a66f 13606 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13607 buffer += addr_size;
13608 offset += 2 * addr_size;
13609
13610 /* An end of list marker is a pair of zero addresses. */
13611 if (range_beginning == 0 && range_end == 0)
13612 /* Found the end of list entry. */
13613 break;
13614
13615 /* Each base address selection entry is a pair of 2 values.
13616 The first is the largest possible address, the second is
13617 the base address. Check for a base address here. */
13618 if ((range_beginning & mask) == mask)
13619 {
28d2bfb9
AB
13620 /* If we found the largest possible address, then we already
13621 have the base address in range_end. */
13622 base = range_end;
43039443
JK
13623 continue;
13624 }
13625
2b24b6e4 13626 if (!base.has_value ())
43039443
JK
13627 {
13628 /* We have no valid base address for the ranges
13629 data. */
b98664d3 13630 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13631 return 0;
13632 }
13633
9277c30c
UW
13634 if (range_beginning > range_end)
13635 {
13636 /* Inverted range entries are invalid. */
b98664d3 13637 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13638 return 0;
13639 }
13640
13641 /* Empty range entries have no effect. */
13642 if (range_beginning == range_end)
13643 continue;
13644
2b24b6e4
TT
13645 range_beginning += *base;
13646 range_end += *base;
43039443 13647
01093045
DE
13648 /* A not-uncommon case of bad debug info.
13649 Don't pollute the addrmap with bad data. */
13650 if (range_beginning + baseaddr == 0
13651 && !dwarf2_per_objfile->has_section_at_zero)
13652 {
b98664d3 13653 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13654 " [in module %s]"), objfile_name (objfile));
01093045
DE
13655 continue;
13656 }
13657
5f46c5a5
JK
13658 callback (range_beginning, range_end);
13659 }
13660
13661 return 1;
13662}
13663
13664/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13665 Return 1 if the attributes are present and valid, otherwise, return 0.
13666 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13667
13668static int
13669dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13670 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13671 dwarf2_psymtab *ranges_pst)
5f46c5a5 13672{
518817b3 13673 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5f46c5a5 13674 struct gdbarch *gdbarch = get_objfile_arch (objfile);
b3b3bada 13675 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13676 int low_set = 0;
13677 CORE_ADDR low = 0;
13678 CORE_ADDR high = 0;
13679 int retval;
13680
13681 retval = dwarf2_ranges_process (offset, cu,
13682 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13683 {
9277c30c 13684 if (ranges_pst != NULL)
3e29f34a
MR
13685 {
13686 CORE_ADDR lowpc;
13687 CORE_ADDR highpc;
13688
79748972
TT
13689 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13690 range_beginning + baseaddr)
13691 - baseaddr);
13692 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13693 range_end + baseaddr)
13694 - baseaddr);
d320c2b5
TT
13695 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13696 lowpc, highpc - 1, ranges_pst);
3e29f34a 13697 }
ff013f42 13698
43039443
JK
13699 /* FIXME: This is recording everything as a low-high
13700 segment of consecutive addresses. We should have a
13701 data structure for discontiguous block ranges
13702 instead. */
13703 if (! low_set)
13704 {
13705 low = range_beginning;
13706 high = range_end;
13707 low_set = 1;
13708 }
13709 else
13710 {
13711 if (range_beginning < low)
13712 low = range_beginning;
13713 if (range_end > high)
13714 high = range_end;
13715 }
5f46c5a5
JK
13716 });
13717 if (!retval)
13718 return 0;
43039443
JK
13719
13720 if (! low_set)
13721 /* If the first entry is an end-of-list marker, the range
13722 describes an empty scope, i.e. no instructions. */
13723 return 0;
13724
13725 if (low_return)
13726 *low_return = low;
13727 if (high_return)
13728 *high_return = high;
13729 return 1;
13730}
13731
3a2b436a
JK
13732/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13733 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13734 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13735
3a2b436a 13736static enum pc_bounds_kind
af34e669 13737dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13738 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13739 dwarf2_psymtab *pst)
c906108c 13740{
518817b3
SM
13741 struct dwarf2_per_objfile *dwarf2_per_objfile
13742 = cu->per_cu->dwarf2_per_objfile;
c906108c 13743 struct attribute *attr;
91da1414 13744 struct attribute *attr_high;
af34e669
DJ
13745 CORE_ADDR low = 0;
13746 CORE_ADDR high = 0;
e385593e 13747 enum pc_bounds_kind ret;
c906108c 13748
91da1414
MW
13749 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13750 if (attr_high)
af34e669 13751 {
e142c38c 13752 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13753 if (attr != nullptr)
91da1414 13754 {
cd6c91b4
TT
13755 low = attr->value_as_address ();
13756 high = attr_high->value_as_address ();
13757 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13758 high += low;
91da1414 13759 }
af34e669
DJ
13760 else
13761 /* Found high w/o low attribute. */
e385593e 13762 return PC_BOUNDS_INVALID;
af34e669
DJ
13763
13764 /* Found consecutive range of addresses. */
3a2b436a 13765 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13766 }
c906108c 13767 else
af34e669 13768 {
e142c38c 13769 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13770 if (attr != NULL)
13771 {
18a8505e 13772 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13773 We take advantage of the fact that DW_AT_ranges does not appear
13774 in DW_TAG_compile_unit of DWO files. */
13775 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13776 unsigned int ranges_offset = (DW_UNSND (attr)
13777 + (need_ranges_base
13778 ? cu->ranges_base
13779 : 0));
2e3cf129 13780
af34e669 13781 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13782 .debug_ranges section. */
2e3cf129 13783 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13784 return PC_BOUNDS_INVALID;
43039443 13785 /* Found discontinuous range of addresses. */
3a2b436a 13786 ret = PC_BOUNDS_RANGES;
af34e669 13787 }
e385593e
JK
13788 else
13789 return PC_BOUNDS_NOT_PRESENT;
af34e669 13790 }
c906108c 13791
48fbe735 13792 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13793 if (high <= low)
e385593e 13794 return PC_BOUNDS_INVALID;
c906108c
SS
13795
13796 /* When using the GNU linker, .gnu.linkonce. sections are used to
13797 eliminate duplicate copies of functions and vtables and such.
13798 The linker will arbitrarily choose one and discard the others.
13799 The AT_*_pc values for such functions refer to local labels in
13800 these sections. If the section from that file was discarded, the
13801 labels are not in the output, so the relocs get a value of 0.
13802 If this is a discarded function, mark the pc bounds as invalid,
13803 so that GDB will ignore it. */
72dca2f5 13804 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13805 return PC_BOUNDS_INVALID;
c906108c
SS
13806
13807 *lowpc = low;
96408a79
SA
13808 if (highpc)
13809 *highpc = high;
af34e669 13810 return ret;
c906108c
SS
13811}
13812
b084d499
JB
13813/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13814 its low and high PC addresses. Do nothing if these addresses could not
13815 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13816 and HIGHPC to the high address if greater than HIGHPC. */
13817
13818static void
13819dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13820 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13821 struct dwarf2_cu *cu)
13822{
13823 CORE_ADDR low, high;
13824 struct die_info *child = die->child;
13825
e385593e 13826 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13827 {
325fac50
PA
13828 *lowpc = std::min (*lowpc, low);
13829 *highpc = std::max (*highpc, high);
b084d499
JB
13830 }
13831
13832 /* If the language does not allow nested subprograms (either inside
13833 subprograms or lexical blocks), we're done. */
13834 if (cu->language != language_ada)
13835 return;
6e70227d 13836
b084d499
JB
13837 /* Check all the children of the given DIE. If it contains nested
13838 subprograms, then check their pc bounds. Likewise, we need to
13839 check lexical blocks as well, as they may also contain subprogram
13840 definitions. */
13841 while (child && child->tag)
13842 {
13843 if (child->tag == DW_TAG_subprogram
13844 || child->tag == DW_TAG_lexical_block)
13845 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 13846 child = child->sibling;
b084d499
JB
13847 }
13848}
13849
fae299cd
DC
13850/* Get the low and high pc's represented by the scope DIE, and store
13851 them in *LOWPC and *HIGHPC. If the correct values can't be
13852 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13853
13854static void
13855get_scope_pc_bounds (struct die_info *die,
13856 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13857 struct dwarf2_cu *cu)
13858{
13859 CORE_ADDR best_low = (CORE_ADDR) -1;
13860 CORE_ADDR best_high = (CORE_ADDR) 0;
13861 CORE_ADDR current_low, current_high;
13862
3a2b436a 13863 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13864 >= PC_BOUNDS_RANGES)
fae299cd
DC
13865 {
13866 best_low = current_low;
13867 best_high = current_high;
13868 }
13869 else
13870 {
13871 struct die_info *child = die->child;
13872
13873 while (child && child->tag)
13874 {
13875 switch (child->tag) {
13876 case DW_TAG_subprogram:
b084d499 13877 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13878 break;
13879 case DW_TAG_namespace:
f55ee35c 13880 case DW_TAG_module:
fae299cd
DC
13881 /* FIXME: carlton/2004-01-16: Should we do this for
13882 DW_TAG_class_type/DW_TAG_structure_type, too? I think
13883 that current GCC's always emit the DIEs corresponding
13884 to definitions of methods of classes as children of a
13885 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
13886 the DIEs giving the declarations, which could be
13887 anywhere). But I don't see any reason why the
13888 standards says that they have to be there. */
13889 get_scope_pc_bounds (child, &current_low, &current_high, cu);
13890
13891 if (current_low != ((CORE_ADDR) -1))
13892 {
325fac50
PA
13893 best_low = std::min (best_low, current_low);
13894 best_high = std::max (best_high, current_high);
fae299cd
DC
13895 }
13896 break;
13897 default:
0963b4bd 13898 /* Ignore. */
fae299cd
DC
13899 break;
13900 }
13901
436c571c 13902 child = child->sibling;
fae299cd
DC
13903 }
13904 }
13905
13906 *lowpc = best_low;
13907 *highpc = best_high;
13908}
13909
801e3a5b
JB
13910/* Record the address ranges for BLOCK, offset by BASEADDR, as given
13911 in DIE. */
380bca97 13912
801e3a5b
JB
13913static void
13914dwarf2_record_block_ranges (struct die_info *die, struct block *block,
13915 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
13916{
518817b3 13917 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3e29f34a 13918 struct gdbarch *gdbarch = get_objfile_arch (objfile);
801e3a5b 13919 struct attribute *attr;
91da1414 13920 struct attribute *attr_high;
801e3a5b 13921
91da1414
MW
13922 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13923 if (attr_high)
801e3a5b 13924 {
801e3a5b 13925 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13926 if (attr != nullptr)
801e3a5b 13927 {
cd6c91b4
TT
13928 CORE_ADDR low = attr->value_as_address ();
13929 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 13930
cd6c91b4 13931 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13932 high += low;
9a619af0 13933
3e29f34a
MR
13934 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
13935 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 13936 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
13937 }
13938 }
13939
13940 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 13941 if (attr != nullptr)
801e3a5b 13942 {
18a8505e 13943 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13944 We take advantage of the fact that DW_AT_ranges does not appear
13945 in DW_TAG_compile_unit of DWO files. */
13946 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
13947
13948 /* The value of the DW_AT_ranges attribute is the offset of the
13949 address range list in the .debug_ranges section. */
ab435259
DE
13950 unsigned long offset = (DW_UNSND (attr)
13951 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 13952
2d5f09ec 13953 std::vector<blockrange> blockvec;
5f46c5a5
JK
13954 dwarf2_ranges_process (offset, cu,
13955 [&] (CORE_ADDR start, CORE_ADDR end)
13956 {
58fdfd2c
JK
13957 start += baseaddr;
13958 end += baseaddr;
5f46c5a5
JK
13959 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
13960 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 13961 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 13962 blockvec.emplace_back (start, end);
5f46c5a5 13963 });
2d5f09ec
KB
13964
13965 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
13966 }
13967}
13968
685b1105
JK
13969/* Check whether the producer field indicates either of GCC < 4.6, or the
13970 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 13971
685b1105
JK
13972static void
13973check_producer (struct dwarf2_cu *cu)
60d5a603 13974{
38360086 13975 int major, minor;
60d5a603
JK
13976
13977 if (cu->producer == NULL)
13978 {
13979 /* For unknown compilers expect their behavior is DWARF version
13980 compliant.
13981
13982 GCC started to support .debug_types sections by -gdwarf-4 since
13983 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
13984 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
13985 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
13986 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 13987 }
b1ffba5a 13988 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 13989 {
38360086
MW
13990 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
13991 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 13992 }
5230b05a 13993 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
13994 {
13995 cu->producer_is_icc = true;
13996 cu->producer_is_icc_lt_14 = major < 14;
13997 }
c258c396
JD
13998 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
13999 cu->producer_is_codewarrior = true;
685b1105
JK
14000 else
14001 {
14002 /* For other non-GCC compilers, expect their behavior is DWARF version
14003 compliant. */
60d5a603
JK
14004 }
14005
9068261f 14006 cu->checked_producer = true;
685b1105 14007}
ba919b58 14008
685b1105
JK
14009/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14010 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14011 during 4.6.0 experimental. */
14012
9068261f 14013static bool
685b1105
JK
14014producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14015{
14016 if (!cu->checked_producer)
14017 check_producer (cu);
14018
14019 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14020}
14021
c258c396
JD
14022
14023/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14024 with incorrect is_stmt attributes. */
14025
14026static bool
14027producer_is_codewarrior (struct dwarf2_cu *cu)
14028{
14029 if (!cu->checked_producer)
14030 check_producer (cu);
14031
14032 return cu->producer_is_codewarrior;
14033}
14034
405feb71 14035/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14036 DW_AT_accessibility. */
14037
14038static enum dwarf_access_attribute
14039dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14040{
14041 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14042 {
14043 /* The default DWARF 2 accessibility for members is public, the default
14044 accessibility for inheritance is private. */
14045
14046 if (die->tag != DW_TAG_inheritance)
14047 return DW_ACCESS_public;
14048 else
14049 return DW_ACCESS_private;
14050 }
14051 else
14052 {
14053 /* DWARF 3+ defines the default accessibility a different way. The same
14054 rules apply now for DW_TAG_inheritance as for the members and it only
14055 depends on the container kind. */
14056
14057 if (die->parent->tag == DW_TAG_class_type)
14058 return DW_ACCESS_private;
14059 else
14060 return DW_ACCESS_public;
14061 }
14062}
14063
74ac6d43
TT
14064/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14065 offset. If the attribute was not found return 0, otherwise return
14066 1. If it was found but could not properly be handled, set *OFFSET
14067 to 0. */
14068
14069static int
14070handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14071 LONGEST *offset)
14072{
14073 struct attribute *attr;
14074
14075 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14076 if (attr != NULL)
14077 {
14078 *offset = 0;
14079
14080 /* Note that we do not check for a section offset first here.
14081 This is because DW_AT_data_member_location is new in DWARF 4,
14082 so if we see it, we can assume that a constant form is really
14083 a constant and not a section offset. */
cd6c91b4 14084 if (attr->form_is_constant ())
0826b30a 14085 *offset = attr->constant_value (0);
cd6c91b4 14086 else if (attr->form_is_section_offset ())
74ac6d43 14087 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14088 else if (attr->form_is_block ())
74ac6d43
TT
14089 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14090 else
14091 dwarf2_complex_location_expr_complaint ();
14092
14093 return 1;
14094 }
14095
14096 return 0;
14097}
14098
c906108c
SS
14099/* Add an aggregate field to the field list. */
14100
14101static void
107d2387 14102dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14103 struct dwarf2_cu *cu)
6e70227d 14104{
518817b3 14105 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
5e2b427d 14106 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
14107 struct nextfield *new_field;
14108 struct attribute *attr;
14109 struct field *fp;
15d034d0 14110 const char *fieldname = "";
c906108c 14111
7d0ccb61
DJ
14112 if (die->tag == DW_TAG_inheritance)
14113 {
be2daae6
TT
14114 fip->baseclasses.emplace_back ();
14115 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14116 }
14117 else
14118 {
be2daae6
TT
14119 fip->fields.emplace_back ();
14120 new_field = &fip->fields.back ();
7d0ccb61 14121 }
be2daae6 14122
e142c38c 14123 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14124 if (attr != nullptr)
c906108c 14125 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14126 else
14127 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14128 if (new_field->accessibility != DW_ACCESS_public)
14129 fip->non_public_fields = 1;
60d5a603 14130
e142c38c 14131 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14132 if (attr != nullptr)
c906108c 14133 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14134 else
14135 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14136
14137 fp = &new_field->field;
a9a9bd0f 14138
e142c38c 14139 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14140 {
74ac6d43
TT
14141 LONGEST offset;
14142
a9a9bd0f 14143 /* Data member other than a C++ static data member. */
6e70227d 14144
c906108c 14145 /* Get type of field. */
e7c27a73 14146 fp->type = die_type (die, cu);
c906108c 14147
d6a843b5 14148 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14149
c906108c 14150 /* Get bit size of field (zero if none). */
e142c38c 14151 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14152 if (attr != nullptr)
c906108c
SS
14153 {
14154 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14155 }
14156 else
14157 {
14158 FIELD_BITSIZE (*fp) = 0;
14159 }
14160
14161 /* Get bit offset of field. */
74ac6d43
TT
14162 if (handle_data_member_location (die, cu, &offset))
14163 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
e142c38c 14164 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14165 if (attr != nullptr)
c906108c 14166 {
d5a22e77 14167 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14168 {
14169 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14170 additional bit offset from the MSB of the containing
14171 anonymous object to the MSB of the field. We don't
14172 have to do anything special since we don't need to
14173 know the size of the anonymous object. */
f41f5e61 14174 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14175 }
14176 else
14177 {
14178 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14179 MSB of the anonymous object, subtract off the number of
14180 bits from the MSB of the field to the MSB of the
14181 object, and then subtract off the number of bits of
14182 the field itself. The result is the bit offset of
14183 the LSB of the field. */
c906108c
SS
14184 int anonymous_size;
14185 int bit_offset = DW_UNSND (attr);
14186
e142c38c 14187 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14188 if (attr != nullptr)
c906108c
SS
14189 {
14190 /* The size of the anonymous object containing
14191 the bit field is explicit, so use the
14192 indicated size (in bytes). */
14193 anonymous_size = DW_UNSND (attr);
14194 }
14195 else
14196 {
14197 /* The size of the anonymous object containing
14198 the bit field must be inferred from the type
14199 attribute of the data member containing the
14200 bit field. */
14201 anonymous_size = TYPE_LENGTH (fp->type);
14202 }
f41f5e61
PA
14203 SET_FIELD_BITPOS (*fp,
14204 (FIELD_BITPOS (*fp)
14205 + anonymous_size * bits_per_byte
14206 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14207 }
14208 }
da5b30da
AA
14209 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14210 if (attr != NULL)
14211 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14212 + attr->constant_value (0)));
c906108c
SS
14213
14214 /* Get name of field. */
39cbfefa
DJ
14215 fieldname = dwarf2_name (die, cu);
14216 if (fieldname == NULL)
14217 fieldname = "";
d8151005
DJ
14218
14219 /* The name is already allocated along with this objfile, so we don't
14220 need to duplicate it for the type. */
14221 fp->name = fieldname;
c906108c
SS
14222
14223 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14224 pointer or virtual base class pointer) to private. */
e142c38c 14225 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14226 {
d48cc9dd 14227 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14228 new_field->accessibility = DW_ACCESS_private;
14229 fip->non_public_fields = 1;
14230 }
14231 }
a9a9bd0f 14232 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14233 {
a9a9bd0f
DC
14234 /* C++ static member. */
14235
14236 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14237 is a declaration, but all versions of G++ as of this writing
14238 (so through at least 3.2.1) incorrectly generate
14239 DW_TAG_variable tags. */
6e70227d 14240
ff355380 14241 const char *physname;
c906108c 14242
a9a9bd0f 14243 /* Get name of field. */
39cbfefa
DJ
14244 fieldname = dwarf2_name (die, cu);
14245 if (fieldname == NULL)
c906108c
SS
14246 return;
14247
254e6b9e 14248 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14249 if (attr
14250 /* Only create a symbol if this is an external value.
14251 new_symbol checks this and puts the value in the global symbol
14252 table, which we want. If it is not external, new_symbol
14253 will try to put the value in cu->list_in_scope which is wrong. */
14254 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14255 {
14256 /* A static const member, not much different than an enum as far as
14257 we're concerned, except that we can support more types. */
14258 new_symbol (die, NULL, cu);
14259 }
14260
2df3850c 14261 /* Get physical name. */
ff355380 14262 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14263
d8151005
DJ
14264 /* The name is already allocated along with this objfile, so we don't
14265 need to duplicate it for the type. */
14266 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14267 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14268 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14269 }
14270 else if (die->tag == DW_TAG_inheritance)
14271 {
74ac6d43 14272 LONGEST offset;
d4b96c9a 14273
74ac6d43
TT
14274 /* C++ base class field. */
14275 if (handle_data_member_location (die, cu, &offset))
14276 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
c906108c 14277 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14278 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14279 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14280 }
2ddeaf8a
TT
14281 else if (die->tag == DW_TAG_variant_part)
14282 {
14283 /* process_structure_scope will treat this DIE as a union. */
14284 process_structure_scope (die, cu);
14285
14286 /* The variant part is relative to the start of the enclosing
14287 structure. */
14288 SET_FIELD_BITPOS (*fp, 0);
14289 fp->type = get_die_type (die, cu);
14290 fp->artificial = 1;
14291 fp->name = "<<variant>>";
c8c81635
TT
14292
14293 /* Normally a DW_TAG_variant_part won't have a size, but our
14294 representation requires one, so set it to the maximum of the
489dbda6
TT
14295 child sizes, being sure to account for the offset at which
14296 each child is seen. */
c8c81635
TT
14297 if (TYPE_LENGTH (fp->type) == 0)
14298 {
14299 unsigned max = 0;
14300 for (int i = 0; i < TYPE_NFIELDS (fp->type); ++i)
489dbda6
TT
14301 {
14302 unsigned len = ((TYPE_FIELD_BITPOS (fp->type, i) + 7) / 8
14303 + TYPE_LENGTH (TYPE_FIELD_TYPE (fp->type, i)));
14304 if (len > max)
14305 max = len;
14306 }
c8c81635
TT
14307 TYPE_LENGTH (fp->type) = max;
14308 }
2ddeaf8a
TT
14309 }
14310 else
14311 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14312}
14313
883fd55a
KS
14314/* Can the type given by DIE define another type? */
14315
14316static bool
14317type_can_define_types (const struct die_info *die)
14318{
14319 switch (die->tag)
14320 {
14321 case DW_TAG_typedef:
14322 case DW_TAG_class_type:
14323 case DW_TAG_structure_type:
14324 case DW_TAG_union_type:
14325 case DW_TAG_enumeration_type:
14326 return true;
14327
14328 default:
14329 return false;
14330 }
14331}
14332
14333/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14334
14335static void
883fd55a
KS
14336dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14337 struct dwarf2_cu *cu)
6e70227d 14338{
be2daae6
TT
14339 struct decl_field fp;
14340 memset (&fp, 0, sizeof (fp));
98751a41 14341
883fd55a 14342 gdb_assert (type_can_define_types (die));
98751a41 14343
883fd55a 14344 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14345 fp.name = dwarf2_name (die, cu);
14346 fp.type = read_type_die (die, cu);
98751a41 14347
c191a687
KS
14348 /* Save accessibility. */
14349 enum dwarf_access_attribute accessibility;
14350 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14351 if (attr != NULL)
14352 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14353 else
14354 accessibility = dwarf2_default_access_attribute (die, cu);
14355 switch (accessibility)
14356 {
14357 case DW_ACCESS_public:
14358 /* The assumed value if neither private nor protected. */
14359 break;
14360 case DW_ACCESS_private:
be2daae6 14361 fp.is_private = 1;
c191a687
KS
14362 break;
14363 case DW_ACCESS_protected:
be2daae6 14364 fp.is_protected = 1;
c191a687
KS
14365 break;
14366 default:
b98664d3 14367 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14368 }
14369
883fd55a 14370 if (die->tag == DW_TAG_typedef)
be2daae6 14371 fip->typedef_field_list.push_back (fp);
883fd55a 14372 else
be2daae6 14373 fip->nested_types_list.push_back (fp);
98751a41
JK
14374}
14375
c906108c
SS
14376/* Create the vector of fields, and attach it to the type. */
14377
14378static void
fba45db2 14379dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14380 struct dwarf2_cu *cu)
c906108c 14381{
317f7127 14382 int nfields = fip->nfields ();
c906108c
SS
14383
14384 /* Record the field count, allocate space for the array of fields,
14385 and create blank accessibility bitfields if necessary. */
14386 TYPE_NFIELDS (type) = nfields;
14387 TYPE_FIELDS (type) = (struct field *)
be2daae6 14388 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14389
b4ba55a1 14390 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14391 {
14392 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14393
14394 TYPE_FIELD_PRIVATE_BITS (type) =
14395 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14396 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14397
14398 TYPE_FIELD_PROTECTED_BITS (type) =
14399 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14400 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14401
774b6a14
TT
14402 TYPE_FIELD_IGNORE_BITS (type) =
14403 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14404 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14405 }
14406
14407 /* If the type has baseclasses, allocate and clear a bit vector for
14408 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14409 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14410 {
be2daae6 14411 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14412 unsigned char *pointer;
c906108c
SS
14413
14414 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14415 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14416 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14417 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14418 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14419 }
14420
2ddeaf8a
TT
14421 if (TYPE_FLAG_DISCRIMINATED_UNION (type))
14422 {
14423 struct discriminant_info *di = alloc_discriminant_info (type, -1, -1);
14424
be2daae6 14425 for (int index = 0; index < nfields; ++index)
2ddeaf8a 14426 {
be2daae6
TT
14427 struct nextfield &field = fip->fields[index];
14428
14429 if (field.variant.is_discriminant)
2ddeaf8a 14430 di->discriminant_index = index;
be2daae6 14431 else if (field.variant.default_branch)
2ddeaf8a
TT
14432 di->default_index = index;
14433 else
be2daae6 14434 di->discriminants[index] = field.variant.discriminant_value;
2ddeaf8a
TT
14435 }
14436 }
14437
be2daae6
TT
14438 /* Copy the saved-up fields into the field vector. */
14439 for (int i = 0; i < nfields; ++i)
c906108c 14440 {
be2daae6
TT
14441 struct nextfield &field
14442 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14443 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14444
be2daae6
TT
14445 TYPE_FIELD (type, i) = field.field;
14446 switch (field.accessibility)
c906108c 14447 {
c5aa993b 14448 case DW_ACCESS_private:
b4ba55a1 14449 if (cu->language != language_ada)
be2daae6 14450 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14451 break;
c906108c 14452
c5aa993b 14453 case DW_ACCESS_protected:
b4ba55a1 14454 if (cu->language != language_ada)
be2daae6 14455 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14456 break;
c906108c 14457
c5aa993b
JM
14458 case DW_ACCESS_public:
14459 break;
c906108c 14460
c5aa993b
JM
14461 default:
14462 /* Unknown accessibility. Complain and treat it as public. */
14463 {
b98664d3 14464 complaint (_("unsupported accessibility %d"),
be2daae6 14465 field.accessibility);
c5aa993b
JM
14466 }
14467 break;
c906108c 14468 }
be2daae6 14469 if (i < fip->baseclasses.size ())
c906108c 14470 {
be2daae6 14471 switch (field.virtuality)
c906108c 14472 {
c5aa993b
JM
14473 case DW_VIRTUALITY_virtual:
14474 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14475 if (cu->language == language_ada)
a73c6dcd 14476 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14477 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14478 break;
c906108c
SS
14479 }
14480 }
c906108c
SS
14481 }
14482}
14483
7d27a96d
TT
14484/* Return true if this member function is a constructor, false
14485 otherwise. */
14486
14487static int
14488dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14489{
14490 const char *fieldname;
fe978cb0 14491 const char *type_name;
7d27a96d
TT
14492 int len;
14493
14494 if (die->parent == NULL)
14495 return 0;
14496
14497 if (die->parent->tag != DW_TAG_structure_type
14498 && die->parent->tag != DW_TAG_union_type
14499 && die->parent->tag != DW_TAG_class_type)
14500 return 0;
14501
14502 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14503 type_name = dwarf2_name (die->parent, cu);
14504 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14505 return 0;
14506
14507 len = strlen (fieldname);
fe978cb0
PA
14508 return (strncmp (fieldname, type_name, len) == 0
14509 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14510}
14511
e35000a7
TBA
14512/* Check if the given VALUE is a recognized enum
14513 dwarf_defaulted_attribute constant according to DWARF5 spec,
14514 Table 7.24. */
14515
14516static bool
14517is_valid_DW_AT_defaulted (ULONGEST value)
14518{
14519 switch (value)
14520 {
14521 case DW_DEFAULTED_no:
14522 case DW_DEFAULTED_in_class:
14523 case DW_DEFAULTED_out_of_class:
14524 return true;
14525 }
14526
3142e908 14527 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14528 return false;
14529}
14530
c906108c
SS
14531/* Add a member function to the proper fieldlist. */
14532
14533static void
107d2387 14534dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14535 struct type *type, struct dwarf2_cu *cu)
c906108c 14536{
518817b3 14537 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14538 struct attribute *attr;
c906108c 14539 int i;
be2daae6 14540 struct fnfieldlist *flp = nullptr;
c906108c 14541 struct fn_field *fnp;
15d034d0 14542 const char *fieldname;
f792889a 14543 struct type *this_type;
60d5a603 14544 enum dwarf_access_attribute accessibility;
c906108c 14545
b4ba55a1 14546 if (cu->language == language_ada)
a73c6dcd 14547 error (_("unexpected member function in Ada type"));
b4ba55a1 14548
2df3850c 14549 /* Get name of member function. */
39cbfefa
DJ
14550 fieldname = dwarf2_name (die, cu);
14551 if (fieldname == NULL)
2df3850c 14552 return;
c906108c 14553
c906108c 14554 /* Look up member function name in fieldlist. */
be2daae6 14555 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14556 {
27bfe10e 14557 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14558 {
14559 flp = &fip->fnfieldlists[i];
14560 break;
14561 }
c906108c
SS
14562 }
14563
be2daae6
TT
14564 /* Create a new fnfieldlist if necessary. */
14565 if (flp == nullptr)
c906108c 14566 {
be2daae6
TT
14567 fip->fnfieldlists.emplace_back ();
14568 flp = &fip->fnfieldlists.back ();
c906108c 14569 flp->name = fieldname;
be2daae6 14570 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14571 }
14572
be2daae6
TT
14573 /* Create a new member function field and add it to the vector of
14574 fnfieldlists. */
14575 flp->fnfields.emplace_back ();
14576 fnp = &flp->fnfields.back ();
3da10d80
KS
14577
14578 /* Delay processing of the physname until later. */
9c37b5ae 14579 if (cu->language == language_cplus)
be2daae6
TT
14580 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14581 die, cu);
3da10d80
KS
14582 else
14583 {
1d06ead6 14584 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14585 fnp->physname = physname ? physname : "";
14586 }
14587
c906108c 14588 fnp->type = alloc_type (objfile);
f792889a
DJ
14589 this_type = read_type_die (die, cu);
14590 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14591 {
f792889a 14592 int nparams = TYPE_NFIELDS (this_type);
c906108c 14593
f792889a 14594 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14595 of the method itself (TYPE_CODE_METHOD). */
14596 smash_to_method_type (fnp->type, type,
f792889a
DJ
14597 TYPE_TARGET_TYPE (this_type),
14598 TYPE_FIELDS (this_type),
14599 TYPE_NFIELDS (this_type),
14600 TYPE_VARARGS (this_type));
c906108c
SS
14601
14602 /* Handle static member functions.
c5aa993b 14603 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14604 member functions. G++ helps GDB by marking the first
14605 parameter for non-static member functions (which is the this
14606 pointer) as artificial. We obtain this information from
14607 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14608 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14609 fnp->voffset = VOFFSET_STATIC;
14610 }
14611 else
b98664d3 14612 complaint (_("member function type missing for '%s'"),
3da10d80 14613 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14614
14615 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14616 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14617 fnp->fcontext = die_containing_type (die, cu);
c906108c 14618
3e43a32a
MS
14619 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14620 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14621
14622 /* Get accessibility. */
e142c38c 14623 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14624 if (attr != nullptr)
aead7601 14625 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14626 else
14627 accessibility = dwarf2_default_access_attribute (die, cu);
14628 switch (accessibility)
c906108c 14629 {
60d5a603
JK
14630 case DW_ACCESS_private:
14631 fnp->is_private = 1;
14632 break;
14633 case DW_ACCESS_protected:
14634 fnp->is_protected = 1;
14635 break;
c906108c
SS
14636 }
14637
b02dede2 14638 /* Check for artificial methods. */
e142c38c 14639 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14640 if (attr && DW_UNSND (attr) != 0)
14641 fnp->is_artificial = 1;
14642
e35000a7
TBA
14643 /* Check for defaulted methods. */
14644 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14645 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14646 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14647
14648 /* Check for deleted methods. */
14649 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14650 if (attr != nullptr && DW_UNSND (attr) != 0)
14651 fnp->is_deleted = 1;
14652
7d27a96d
TT
14653 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14654
0d564a31 14655 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14656 function. For older versions of GCC, this is an offset in the
14657 appropriate virtual table, as specified by DW_AT_containing_type.
14658 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14659 to the object address. */
14660
e142c38c 14661 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14662 if (attr != nullptr)
8e19ed76 14663 {
4fc6c0d5 14664 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14665 {
aec5aa8b
TT
14666 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14667 {
14668 /* Old-style GCC. */
14669 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14670 }
14671 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14672 || (DW_BLOCK (attr)->size > 1
14673 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14674 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14675 {
aec5aa8b
TT
14676 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14677 if ((fnp->voffset % cu->header.addr_size) != 0)
14678 dwarf2_complex_location_expr_complaint ();
14679 else
14680 fnp->voffset /= cu->header.addr_size;
14681 fnp->voffset += 2;
14682 }
14683 else
14684 dwarf2_complex_location_expr_complaint ();
14685
14686 if (!fnp->fcontext)
7e993ebf
KS
14687 {
14688 /* If there is no `this' field and no DW_AT_containing_type,
14689 we cannot actually find a base class context for the
14690 vtable! */
14691 if (TYPE_NFIELDS (this_type) == 0
14692 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
14693 {
b98664d3 14694 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
14695 "function \"%s\" (offset %s)"),
14696 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
14697 }
14698 else
14699 {
14700 fnp->fcontext
14701 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
14702 }
14703 }
aec5aa8b 14704 }
cd6c91b4 14705 else if (attr->form_is_section_offset ())
8e19ed76 14706 {
4d3c2250 14707 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
14708 }
14709 else
14710 {
4d3c2250
KB
14711 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
14712 fieldname);
8e19ed76 14713 }
0d564a31 14714 }
d48cc9dd
DJ
14715 else
14716 {
14717 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
14718 if (attr && DW_UNSND (attr))
14719 {
14720 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 14721 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 14722 "but the vtable offset is not specified"),
9d8780f0 14723 fieldname, sect_offset_str (die->sect_off));
9655fd1a 14724 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
14725 TYPE_CPLUS_DYNAMIC (type) = 1;
14726 }
14727 }
c906108c
SS
14728}
14729
14730/* Create the vector of member function fields, and attach it to the type. */
14731
14732static void
fba45db2 14733dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14734 struct dwarf2_cu *cu)
c906108c 14735{
b4ba55a1 14736 if (cu->language == language_ada)
a73c6dcd 14737 error (_("unexpected member functions in Ada type"));
b4ba55a1 14738
c906108c
SS
14739 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14740 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
14741 TYPE_ALLOC (type,
14742 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 14743
be2daae6 14744 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14745 {
be2daae6 14746 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 14747 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 14748
be2daae6
TT
14749 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
14750 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 14751 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
14752 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
14753
14754 for (int k = 0; k < nf.fnfields.size (); ++k)
14755 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
14756 }
14757
be2daae6 14758 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
14759}
14760
1168df01
JB
14761/* Returns non-zero if NAME is the name of a vtable member in CU's
14762 language, zero otherwise. */
14763static int
14764is_vtable_name (const char *name, struct dwarf2_cu *cu)
14765{
14766 static const char vptr[] = "_vptr";
14767
9c37b5ae
TT
14768 /* Look for the C++ form of the vtable. */
14769 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
14770 return 1;
14771
14772 return 0;
14773}
14774
c0dd20ea 14775/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
14776 functions, with the ABI-specified layout. If TYPE describes
14777 such a structure, smash it into a member function type.
61049d3b
DJ
14778
14779 GCC shouldn't do this; it should just output pointer to member DIEs.
14780 This is GCC PR debug/28767. */
c0dd20ea 14781
0b92b5bb
TT
14782static void
14783quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 14784{
09e2d7c7 14785 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
14786
14787 /* Check for a structure with no name and two children. */
0b92b5bb
TT
14788 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
14789 return;
c0dd20ea
DJ
14790
14791 /* Check for __pfn and __delta members. */
0b92b5bb
TT
14792 if (TYPE_FIELD_NAME (type, 0) == NULL
14793 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
14794 || TYPE_FIELD_NAME (type, 1) == NULL
14795 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
14796 return;
c0dd20ea
DJ
14797
14798 /* Find the type of the method. */
0b92b5bb 14799 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
14800 if (pfn_type == NULL
14801 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
14802 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 14803 return;
c0dd20ea
DJ
14804
14805 /* Look for the "this" argument. */
14806 pfn_type = TYPE_TARGET_TYPE (pfn_type);
14807 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 14808 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 14809 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 14810 return;
c0dd20ea 14811
09e2d7c7 14812 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 14813 new_type = alloc_type (objfile);
09e2d7c7 14814 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
14815 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
14816 TYPE_VARARGS (pfn_type));
0b92b5bb 14817 smash_to_methodptr_type (type, new_type);
c0dd20ea 14818}
1168df01 14819
2b4424c3
TT
14820/* If the DIE has a DW_AT_alignment attribute, return its value, doing
14821 appropriate error checking and issuing complaints if there is a
14822 problem. */
14823
14824static ULONGEST
14825get_alignment (struct dwarf2_cu *cu, struct die_info *die)
14826{
14827 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
14828
14829 if (attr == nullptr)
14830 return 0;
14831
cd6c91b4 14832 if (!attr->form_is_constant ())
2b4424c3 14833 {
b98664d3 14834 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
14835 " - DIE at %s [in module %s]"),
14836 sect_offset_str (die->sect_off),
14837 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14838 return 0;
14839 }
14840
14841 ULONGEST align;
14842 if (attr->form == DW_FORM_sdata)
14843 {
14844 LONGEST val = DW_SND (attr);
14845 if (val < 0)
14846 {
b98664d3 14847 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
14848 " - DIE at %s [in module %s]"),
14849 sect_offset_str (die->sect_off),
14850 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14851 return 0;
14852 }
14853 align = val;
14854 }
14855 else
14856 align = DW_UNSND (attr);
14857
14858 if (align == 0)
14859 {
b98664d3 14860 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
14861 " - DIE at %s [in module %s]"),
14862 sect_offset_str (die->sect_off),
14863 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14864 return 0;
14865 }
14866 if ((align & (align - 1)) != 0)
14867 {
b98664d3 14868 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
14869 " - DIE at %s [in module %s]"),
14870 sect_offset_str (die->sect_off),
14871 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14872 return 0;
14873 }
14874
14875 return align;
14876}
14877
14878/* If the DIE has a DW_AT_alignment attribute, use its value to set
14879 the alignment for TYPE. */
14880
14881static void
14882maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
14883 struct type *type)
14884{
14885 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 14886 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
14887 " - DIE at %s [in module %s]"),
14888 sect_offset_str (die->sect_off),
14889 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
14890}
685b1105 14891
e35000a7
TBA
14892/* Check if the given VALUE is a valid enum dwarf_calling_convention
14893 constant for a type, according to DWARF5 spec, Table 5.5. */
14894
14895static bool
14896is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
14897{
14898 switch (value)
14899 {
14900 case DW_CC_normal:
14901 case DW_CC_pass_by_reference:
14902 case DW_CC_pass_by_value:
14903 return true;
14904
14905 default:
14906 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14907 "(%s) for a type"), pulongest (value));
e35000a7
TBA
14908 return false;
14909 }
14910}
14911
d0922fcf
TBA
14912/* Check if the given VALUE is a valid enum dwarf_calling_convention
14913 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
14914 also according to GNU-specific values (see include/dwarf2.h). */
14915
14916static bool
14917is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
14918{
14919 switch (value)
14920 {
14921 case DW_CC_normal:
14922 case DW_CC_program:
14923 case DW_CC_nocall:
14924 return true;
14925
14926 case DW_CC_GNU_renesas_sh:
14927 case DW_CC_GNU_borland_fastcall_i386:
14928 case DW_CC_GDB_IBM_OpenCL:
14929 return true;
14930
14931 default:
14932 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 14933 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
14934 return false;
14935 }
14936}
14937
c906108c 14938/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
14939 (definition) to create a type for the structure or union. Fill in
14940 the type's name and general properties; the members will not be
83655187
DE
14941 processed until process_structure_scope. A symbol table entry for
14942 the type will also not be done until process_structure_scope (assuming
14943 the type has a name).
c906108c 14944
c767944b
DJ
14945 NOTE: we need to call these functions regardless of whether or not the
14946 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 14947 structure or union. This gets the type entered into our set of
83655187 14948 user defined types. */
c906108c 14949
f792889a 14950static struct type *
134d01f1 14951read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 14952{
518817b3 14953 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
14954 struct type *type;
14955 struct attribute *attr;
15d034d0 14956 const char *name;
c906108c 14957
348e048f
DE
14958 /* If the definition of this type lives in .debug_types, read that type.
14959 Don't follow DW_AT_specification though, that will take us back up
14960 the chain and we want to go down. */
052c8bb8 14961 attr = die->attr (DW_AT_signature);
435d3d88 14962 if (attr != nullptr)
348e048f 14963 {
ac9ec31b 14964 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 14965
ac9ec31b 14966 /* The type's CU may not be the same as CU.
02142a6c 14967 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
14968 return set_die_type (die, type, cu);
14969 }
14970
c0dd20ea 14971 type = alloc_type (objfile);
c906108c 14972 INIT_CPLUS_SPECIFIC (type);
93311388 14973
39cbfefa
DJ
14974 name = dwarf2_name (die, cu);
14975 if (name != NULL)
c906108c 14976 {
987504bb 14977 if (cu->language == language_cplus
c44af4eb
TT
14978 || cu->language == language_d
14979 || cu->language == language_rust)
63d06c5c 14980 {
15d034d0 14981 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
14982
14983 /* dwarf2_full_name might have already finished building the DIE's
14984 type. If so, there is no need to continue. */
14985 if (get_die_type (die, cu) != NULL)
14986 return get_die_type (die, cu);
14987
e86ca25f 14988 TYPE_NAME (type) = full_name;
63d06c5c
DC
14989 }
14990 else
14991 {
d8151005
DJ
14992 /* The name is already allocated along with this objfile, so
14993 we don't need to duplicate it for the type. */
e86ca25f 14994 TYPE_NAME (type) = name;
63d06c5c 14995 }
c906108c
SS
14996 }
14997
14998 if (die->tag == DW_TAG_structure_type)
14999 {
15000 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15001 }
15002 else if (die->tag == DW_TAG_union_type)
15003 {
15004 TYPE_CODE (type) = TYPE_CODE_UNION;
15005 }
2ddeaf8a
TT
15006 else if (die->tag == DW_TAG_variant_part)
15007 {
15008 TYPE_CODE (type) = TYPE_CODE_UNION;
15009 TYPE_FLAG_DISCRIMINATED_UNION (type) = 1;
15010 }
c906108c
SS
15011 else
15012 {
4753d33b 15013 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15014 }
15015
0cc2414c
TT
15016 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15017 TYPE_DECLARED_CLASS (type) = 1;
15018
e35000a7
TBA
15019 /* Store the calling convention in the type if it's available in
15020 the die. Otherwise the calling convention remains set to
15021 the default value DW_CC_normal. */
15022 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15023 if (attr != nullptr
15024 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15025 {
15026 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15027 TYPE_CPLUS_CALLING_CONVENTION (type)
15028 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15029 }
15030
e142c38c 15031 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15032 if (attr != nullptr)
c906108c 15033 {
cd6c91b4 15034 if (attr->form_is_constant ())
155bfbd3
JB
15035 TYPE_LENGTH (type) = DW_UNSND (attr);
15036 else
15037 {
15038 /* For the moment, dynamic type sizes are not supported
15039 by GDB's struct type. The actual size is determined
15040 on-demand when resolving the type of a given object,
15041 so set the type's length to zero for now. Otherwise,
15042 we record an expression as the length, and that expression
15043 could lead to a very large value, which could eventually
15044 lead to us trying to allocate that much memory when creating
15045 a value of that type. */
15046 TYPE_LENGTH (type) = 0;
15047 }
c906108c
SS
15048 }
15049 else
15050 {
15051 TYPE_LENGTH (type) = 0;
15052 }
15053
2b4424c3
TT
15054 maybe_set_alignment (cu, die, type);
15055
5230b05a 15056 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15057 {
5230b05a
WT
15058 /* ICC<14 does not output the required DW_AT_declaration on
15059 incomplete types, but gives them a size of zero. */
422b1cb0 15060 TYPE_STUB (type) = 1;
685b1105
JK
15061 }
15062 else
15063 TYPE_STUB_SUPPORTED (type) = 1;
15064
dc718098 15065 if (die_is_declaration (die, cu))
876cecd0 15066 TYPE_STUB (type) = 1;
a6c727b2
DJ
15067 else if (attr == NULL && die->child == NULL
15068 && producer_is_realview (cu->producer))
15069 /* RealView does not output the required DW_AT_declaration
15070 on incomplete types. */
15071 TYPE_STUB (type) = 1;
dc718098 15072
c906108c
SS
15073 /* We need to add the type field to the die immediately so we don't
15074 infinitely recurse when dealing with pointers to the structure
0963b4bd 15075 type within the structure itself. */
1c379e20 15076 set_die_type (die, type, cu);
c906108c 15077
7e314c57
JK
15078 /* set_die_type should be already done. */
15079 set_descriptive_type (type, die, cu);
15080
c767944b
DJ
15081 return type;
15082}
15083
2ddeaf8a
TT
15084/* A helper for process_structure_scope that handles a single member
15085 DIE. */
15086
15087static void
15088handle_struct_member_die (struct die_info *child_die, struct type *type,
15089 struct field_info *fi,
15090 std::vector<struct symbol *> *template_args,
15091 struct dwarf2_cu *cu)
15092{
15093 if (child_die->tag == DW_TAG_member
15094 || child_die->tag == DW_TAG_variable
15095 || child_die->tag == DW_TAG_variant_part)
15096 {
15097 /* NOTE: carlton/2002-11-05: A C++ static data member
15098 should be a DW_TAG_member that is a declaration, but
15099 all versions of G++ as of this writing (so through at
15100 least 3.2.1) incorrectly generate DW_TAG_variable
15101 tags for them instead. */
15102 dwarf2_add_field (fi, child_die, cu);
15103 }
15104 else if (child_die->tag == DW_TAG_subprogram)
15105 {
15106 /* Rust doesn't have member functions in the C++ sense.
15107 However, it does emit ordinary functions as children
15108 of a struct DIE. */
15109 if (cu->language == language_rust)
15110 read_func_scope (child_die, cu);
15111 else
15112 {
15113 /* C++ member function. */
15114 dwarf2_add_member_fn (fi, child_die, type, cu);
15115 }
15116 }
15117 else if (child_die->tag == DW_TAG_inheritance)
15118 {
15119 /* C++ base class field. */
15120 dwarf2_add_field (fi, child_die, cu);
15121 }
15122 else if (type_can_define_types (child_die))
15123 dwarf2_add_type_defn (fi, child_die, cu);
15124 else if (child_die->tag == DW_TAG_template_type_param
15125 || child_die->tag == DW_TAG_template_value_param)
15126 {
15127 struct symbol *arg = new_symbol (child_die, NULL, cu);
15128
15129 if (arg != NULL)
15130 template_args->push_back (arg);
15131 }
15132 else if (child_die->tag == DW_TAG_variant)
15133 {
15134 /* In a variant we want to get the discriminant and also add a
15135 field for our sole member child. */
15136 struct attribute *discr = dwarf2_attr (child_die, DW_AT_discr_value, cu);
15137
bde09ab7 15138 for (die_info *variant_child = child_die->child;
2ddeaf8a 15139 variant_child != NULL;
436c571c 15140 variant_child = variant_child->sibling)
2ddeaf8a
TT
15141 {
15142 if (variant_child->tag == DW_TAG_member)
15143 {
15144 handle_struct_member_die (variant_child, type, fi,
15145 template_args, cu);
15146 /* Only handle the one. */
15147 break;
15148 }
15149 }
15150
15151 /* We don't handle this but we might as well report it if we see
15152 it. */
15153 if (dwarf2_attr (child_die, DW_AT_discr_list, cu) != nullptr)
b98664d3 15154 complaint (_("DW_AT_discr_list is not supported yet"
2ddeaf8a
TT
15155 " - DIE at %s [in module %s]"),
15156 sect_offset_str (child_die->sect_off),
15157 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15158
15159 /* The first field was just added, so we can stash the
15160 discriminant there. */
be2daae6 15161 gdb_assert (!fi->fields.empty ());
2ddeaf8a 15162 if (discr == NULL)
be2daae6 15163 fi->fields.back ().variant.default_branch = true;
2ddeaf8a 15164 else
be2daae6 15165 fi->fields.back ().variant.discriminant_value = DW_UNSND (discr);
2ddeaf8a
TT
15166 }
15167}
15168
c767944b
DJ
15169/* Finish creating a structure or union type, including filling in
15170 its members and creating a symbol for it. */
15171
15172static void
15173process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15174{
518817b3 15175 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15176 struct die_info *child_die;
c767944b
DJ
15177 struct type *type;
15178
15179 type = get_die_type (die, cu);
15180 if (type == NULL)
15181 type = read_structure_type (die, cu);
15182
2ddeaf8a
TT
15183 /* When reading a DW_TAG_variant_part, we need to notice when we
15184 read the discriminant member, so we can record it later in the
15185 discriminant_info. */
15186 bool is_variant_part = TYPE_FLAG_DISCRIMINATED_UNION (type);
feee869b 15187 sect_offset discr_offset {};
3e1d3d8c 15188 bool has_template_parameters = false;
2ddeaf8a
TT
15189
15190 if (is_variant_part)
15191 {
15192 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15193 if (discr == NULL)
15194 {
15195 /* Maybe it's a univariant form, an extension we support.
15196 In this case arrange not to check the offset. */
15197 is_variant_part = false;
15198 }
cd6c91b4 15199 else if (discr->form_is_ref ())
2ddeaf8a
TT
15200 {
15201 struct dwarf2_cu *target_cu = cu;
15202 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15203
15204 discr_offset = target_die->sect_off;
15205 }
15206 else
15207 {
b98664d3 15208 complaint (_("DW_AT_discr does not have DIE reference form"
2ddeaf8a
TT
15209 " - DIE at %s [in module %s]"),
15210 sect_offset_str (die->sect_off),
15211 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15212 is_variant_part = false;
15213 }
15214 }
15215
e142c38c 15216 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15217 {
15218 struct field_info fi;
2f4732b0 15219 std::vector<struct symbol *> template_args;
c906108c 15220
639d11d3 15221 child_die = die->child;
c906108c
SS
15222
15223 while (child_die && child_die->tag)
15224 {
2ddeaf8a 15225 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
34eaf542 15226
2ddeaf8a 15227 if (is_variant_part && discr_offset == child_die->sect_off)
be2daae6 15228 fi.fields.back ().variant.is_discriminant = true;
34eaf542 15229
436c571c 15230 child_die = child_die->sibling;
c906108c
SS
15231 }
15232
34eaf542 15233 /* Attach template arguments to type. */
2f4732b0 15234 if (!template_args.empty ())
34eaf542 15235 {
3e1d3d8c 15236 has_template_parameters = true;
34eaf542 15237 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15238 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15239 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15240 = XOBNEWVEC (&objfile->objfile_obstack,
15241 struct symbol *,
15242 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15243 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15244 template_args.data (),
34eaf542
TT
15245 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15246 * sizeof (struct symbol *)));
34eaf542
TT
15247 }
15248
c906108c 15249 /* Attach fields and member functions to the type. */
317f7127 15250 if (fi.nfields () > 0)
e7c27a73 15251 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15252 if (!fi.fnfieldlists.empty ())
c906108c 15253 {
e7c27a73 15254 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15255
c5aa993b 15256 /* Get the type which refers to the base class (possibly this
c906108c 15257 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15258 class from the DW_AT_containing_type attribute. This use of
15259 DW_AT_containing_type is a GNU extension. */
c906108c 15260
e142c38c 15261 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15262 {
e7c27a73 15263 struct type *t = die_containing_type (die, cu);
c906108c 15264
ae6ae975 15265 set_type_vptr_basetype (type, t);
c906108c
SS
15266 if (type == t)
15267 {
c906108c
SS
15268 int i;
15269
15270 /* Our own class provides vtbl ptr. */
15271 for (i = TYPE_NFIELDS (t) - 1;
15272 i >= TYPE_N_BASECLASSES (t);
15273 --i)
15274 {
0d5cff50 15275 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15276
1168df01 15277 if (is_vtable_name (fieldname, cu))
c906108c 15278 {
ae6ae975 15279 set_type_vptr_fieldno (type, i);
c906108c
SS
15280 break;
15281 }
15282 }
15283
15284 /* Complain if virtual function table field not found. */
15285 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15286 complaint (_("virtual function table pointer "
3e43a32a 15287 "not found when defining class '%s'"),
e86ca25f 15288 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15289 }
15290 else
15291 {
ae6ae975 15292 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15293 }
15294 }
f6235d4c 15295 else if (cu->producer
61012eef 15296 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15297 {
15298 /* The IBM XLC compiler does not provide direct indication
15299 of the containing type, but the vtable pointer is
15300 always named __vfp. */
15301
15302 int i;
15303
15304 for (i = TYPE_NFIELDS (type) - 1;
15305 i >= TYPE_N_BASECLASSES (type);
15306 --i)
15307 {
15308 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15309 {
ae6ae975
DE
15310 set_type_vptr_fieldno (type, i);
15311 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15312 break;
15313 }
15314 }
15315 }
c906108c 15316 }
98751a41
JK
15317
15318 /* Copy fi.typedef_field_list linked list elements content into the
15319 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15320 if (!fi.typedef_field_list.empty ())
98751a41 15321 {
be2daae6 15322 int count = fi.typedef_field_list.size ();
98751a41 15323
a0d7a4ff 15324 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15325 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15326 = ((struct decl_field *)
be2daae6
TT
15327 TYPE_ALLOC (type,
15328 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15329 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15330
be2daae6
TT
15331 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15332 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15333 }
c767944b 15334
883fd55a
KS
15335 /* Copy fi.nested_types_list linked list elements content into the
15336 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15337 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15338 {
be2daae6 15339 int count = fi.nested_types_list.size ();
883fd55a
KS
15340
15341 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15342 TYPE_NESTED_TYPES_ARRAY (type)
15343 = ((struct decl_field *)
be2daae6
TT
15344 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15345 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15346
be2daae6
TT
15347 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15348 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15349 }
c906108c 15350 }
63d06c5c 15351
bb5ed363 15352 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15353 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15354 cu->rust_unions.push_back (type);
0b92b5bb 15355
90aeadfc
DC
15356 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15357 snapshots) has been known to create a die giving a declaration
15358 for a class that has, as a child, a die giving a definition for a
15359 nested class. So we have to process our children even if the
15360 current die is a declaration. Normally, of course, a declaration
15361 won't have any children at all. */
134d01f1 15362
ca040673
DE
15363 child_die = die->child;
15364
90aeadfc
DC
15365 while (child_die != NULL && child_die->tag)
15366 {
15367 if (child_die->tag == DW_TAG_member
15368 || child_die->tag == DW_TAG_variable
34eaf542
TT
15369 || child_die->tag == DW_TAG_inheritance
15370 || child_die->tag == DW_TAG_template_value_param
15371 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15372 {
90aeadfc 15373 /* Do nothing. */
134d01f1 15374 }
90aeadfc
DC
15375 else
15376 process_die (child_die, cu);
134d01f1 15377
436c571c 15378 child_die = child_die->sibling;
134d01f1
DJ
15379 }
15380
fa4028e9
JB
15381 /* Do not consider external references. According to the DWARF standard,
15382 these DIEs are identified by the fact that they have no byte_size
15383 attribute, and a declaration attribute. */
15384 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15385 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15386 {
15387 struct symbol *sym = new_symbol (die, type, cu);
15388
15389 if (has_template_parameters)
15390 {
a776957c
TT
15391 struct symtab *symtab;
15392 if (sym != nullptr)
15393 symtab = symbol_symtab (sym);
15394 else if (cu->line_header != nullptr)
15395 {
15396 /* Any related symtab will do. */
15397 symtab
7ba99d21 15398 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15399 }
15400 else
15401 {
15402 symtab = nullptr;
15403 complaint (_("could not find suitable "
15404 "symtab for template parameter"
15405 " - DIE at %s [in module %s]"),
15406 sect_offset_str (die->sect_off),
15407 objfile_name (objfile));
15408 }
15409
15410 if (symtab != nullptr)
15411 {
15412 /* Make sure that the symtab is set on the new symbols.
15413 Even though they don't appear in this symtab directly,
15414 other parts of gdb assume that symbols do, and this is
15415 reasonably true. */
15416 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15417 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15418 }
3e1d3d8c
TT
15419 }
15420 }
134d01f1
DJ
15421}
15422
55426c9d
JB
15423/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15424 update TYPE using some information only available in DIE's children. */
15425
15426static void
15427update_enumeration_type_from_children (struct die_info *die,
15428 struct type *type,
15429 struct dwarf2_cu *cu)
15430{
60f7655a 15431 struct die_info *child_die;
55426c9d
JB
15432 int unsigned_enum = 1;
15433 int flag_enum = 1;
55426c9d 15434
8268c778 15435 auto_obstack obstack;
55426c9d 15436
60f7655a
DE
15437 for (child_die = die->child;
15438 child_die != NULL && child_die->tag;
436c571c 15439 child_die = child_die->sibling)
55426c9d
JB
15440 {
15441 struct attribute *attr;
15442 LONGEST value;
15443 const gdb_byte *bytes;
15444 struct dwarf2_locexpr_baton *baton;
15445 const char *name;
60f7655a 15446
55426c9d
JB
15447 if (child_die->tag != DW_TAG_enumerator)
15448 continue;
15449
15450 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15451 if (attr == NULL)
15452 continue;
15453
15454 name = dwarf2_name (child_die, cu);
15455 if (name == NULL)
15456 name = "<anonymous enumerator>";
15457
15458 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15459 &value, &bytes, &baton);
15460 if (value < 0)
15461 {
15462 unsigned_enum = 0;
15463 flag_enum = 0;
15464 }
55426c9d 15465 else
edd45eb0
SM
15466 {
15467 if (count_one_bits_ll (value) >= 2)
15468 flag_enum = 0;
edd45eb0 15469 }
55426c9d
JB
15470
15471 /* If we already know that the enum type is neither unsigned, nor
15472 a flag type, no need to look at the rest of the enumerates. */
15473 if (!unsigned_enum && !flag_enum)
15474 break;
55426c9d
JB
15475 }
15476
15477 if (unsigned_enum)
15478 TYPE_UNSIGNED (type) = 1;
15479 if (flag_enum)
15480 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15481}
15482
134d01f1
DJ
15483/* Given a DW_AT_enumeration_type die, set its type. We do not
15484 complete the type's fields yet, or create any symbols. */
c906108c 15485
f792889a 15486static struct type *
134d01f1 15487read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15488{
518817b3 15489 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15490 struct type *type;
c906108c 15491 struct attribute *attr;
0114d602 15492 const char *name;
134d01f1 15493
348e048f
DE
15494 /* If the definition of this type lives in .debug_types, read that type.
15495 Don't follow DW_AT_specification though, that will take us back up
15496 the chain and we want to go down. */
052c8bb8 15497 attr = die->attr (DW_AT_signature);
435d3d88 15498 if (attr != nullptr)
348e048f 15499 {
ac9ec31b 15500 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15501
ac9ec31b 15502 /* The type's CU may not be the same as CU.
02142a6c 15503 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15504 return set_die_type (die, type, cu);
15505 }
15506
c906108c
SS
15507 type = alloc_type (objfile);
15508
15509 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15510 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15511 if (name != NULL)
e86ca25f 15512 TYPE_NAME (type) = name;
c906108c 15513
0626fc76
TT
15514 attr = dwarf2_attr (die, DW_AT_type, cu);
15515 if (attr != NULL)
15516 {
15517 struct type *underlying_type = die_type (die, cu);
15518
15519 TYPE_TARGET_TYPE (type) = underlying_type;
15520 }
15521
e142c38c 15522 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15523 if (attr != nullptr)
c906108c
SS
15524 {
15525 TYPE_LENGTH (type) = DW_UNSND (attr);
15526 }
15527 else
15528 {
15529 TYPE_LENGTH (type) = 0;
15530 }
15531
2b4424c3
TT
15532 maybe_set_alignment (cu, die, type);
15533
137033e9
JB
15534 /* The enumeration DIE can be incomplete. In Ada, any type can be
15535 declared as private in the package spec, and then defined only
15536 inside the package body. Such types are known as Taft Amendment
15537 Types. When another package uses such a type, an incomplete DIE
15538 may be generated by the compiler. */
02eb380e 15539 if (die_is_declaration (die, cu))
876cecd0 15540 TYPE_STUB (type) = 1;
02eb380e 15541
0626fc76
TT
15542 /* Finish the creation of this type by using the enum's children.
15543 We must call this even when the underlying type has been provided
15544 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15545 update_enumeration_type_from_children (die, type, cu);
15546
0626fc76
TT
15547 /* If this type has an underlying type that is not a stub, then we
15548 may use its attributes. We always use the "unsigned" attribute
15549 in this situation, because ordinarily we guess whether the type
15550 is unsigned -- but the guess can be wrong and the underlying type
15551 can tell us the reality. However, we defer to a local size
15552 attribute if one exists, because this lets the compiler override
15553 the underlying type if needed. */
15554 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15555 {
9e7c9a03
HD
15556 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15557 underlying_type = check_typedef (underlying_type);
15558 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 15559 if (TYPE_LENGTH (type) == 0)
9e7c9a03 15560 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 15561 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
15562 && TYPE_RAW_ALIGN (underlying_type) != 0)
15563 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
15564 }
15565
3d567982
TT
15566 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15567
f792889a 15568 return set_die_type (die, type, cu);
134d01f1
DJ
15569}
15570
15571/* Given a pointer to a die which begins an enumeration, process all
15572 the dies that define the members of the enumeration, and create the
15573 symbol for the enumeration type.
15574
15575 NOTE: We reverse the order of the element list. */
15576
15577static void
15578process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15579{
f792889a 15580 struct type *this_type;
134d01f1 15581
f792889a
DJ
15582 this_type = get_die_type (die, cu);
15583 if (this_type == NULL)
15584 this_type = read_enumeration_type (die, cu);
9dc481d3 15585
639d11d3 15586 if (die->child != NULL)
c906108c 15587 {
9dc481d3
DE
15588 struct die_info *child_die;
15589 struct symbol *sym;
43816ebc 15590 std::vector<struct field> fields;
15d034d0 15591 const char *name;
9dc481d3 15592
639d11d3 15593 child_die = die->child;
c906108c
SS
15594 while (child_die && child_die->tag)
15595 {
15596 if (child_die->tag != DW_TAG_enumerator)
15597 {
e7c27a73 15598 process_die (child_die, cu);
c906108c
SS
15599 }
15600 else
15601 {
39cbfefa
DJ
15602 name = dwarf2_name (child_die, cu);
15603 if (name)
c906108c 15604 {
f792889a 15605 sym = new_symbol (child_die, this_type, cu);
c906108c 15606
43816ebc
TT
15607 fields.emplace_back ();
15608 struct field &field = fields.back ();
c906108c 15609
43816ebc
TT
15610 FIELD_NAME (field) = sym->linkage_name ();
15611 FIELD_TYPE (field) = NULL;
15612 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15613 FIELD_BITSIZE (field) = 0;
c906108c
SS
15614 }
15615 }
15616
436c571c 15617 child_die = child_die->sibling;
c906108c
SS
15618 }
15619
43816ebc 15620 if (!fields.empty ())
c906108c 15621 {
43816ebc 15622 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15623 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15624 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15625 memcpy (TYPE_FIELDS (this_type), fields.data (),
15626 sizeof (struct field) * fields.size ());
c906108c 15627 }
c906108c 15628 }
134d01f1 15629
6c83ed52
TT
15630 /* If we are reading an enum from a .debug_types unit, and the enum
15631 is a declaration, and the enum is not the signatured type in the
15632 unit, then we do not want to add a symbol for it. Adding a
15633 symbol would in some cases obscure the true definition of the
15634 enum, giving users an incomplete type when the definition is
15635 actually available. Note that we do not want to do this for all
15636 enums which are just declarations, because C++0x allows forward
15637 enum declarations. */
3019eac3 15638 if (cu->per_cu->is_debug_types
6c83ed52
TT
15639 && die_is_declaration (die, cu))
15640 {
52dc124a 15641 struct signatured_type *sig_type;
6c83ed52 15642
c0f78cd4 15643 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
15644 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
15645 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
15646 return;
15647 }
15648
f792889a 15649 new_symbol (die, this_type, cu);
c906108c
SS
15650}
15651
15652/* Extract all information from a DW_TAG_array_type DIE and put it in
15653 the DIE's type field. For now, this only handles one dimensional
15654 arrays. */
15655
f792889a 15656static struct type *
e7c27a73 15657read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15658{
518817b3 15659 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15660 struct die_info *child_die;
7e314c57 15661 struct type *type;
c906108c 15662 struct type *element_type, *range_type, *index_type;
c906108c 15663 struct attribute *attr;
15d034d0 15664 const char *name;
a405673c 15665 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 15666 unsigned int bit_stride = 0;
c906108c 15667
e7c27a73 15668 element_type = die_type (die, cu);
c906108c 15669
7e314c57
JK
15670 /* The die_type call above may have already set the type for this DIE. */
15671 type = get_die_type (die, cu);
15672 if (type)
15673 return type;
15674
dc53a7ad
JB
15675 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
15676 if (attr != NULL)
a405673c
JB
15677 {
15678 int stride_ok;
09ba997f 15679 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
15680
15681 byte_stride_prop
15682 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
15683 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
15684 prop_type);
a405673c
JB
15685 if (!stride_ok)
15686 {
b98664d3 15687 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
15688 " - DIE at %s [in module %s]"),
15689 sect_offset_str (die->sect_off),
518817b3 15690 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
15691 /* Ignore this attribute. We will likely not be able to print
15692 arrays of this type correctly, but there is little we can do
15693 to help if we cannot read the attribute's value. */
15694 byte_stride_prop = NULL;
15695 }
15696 }
dc53a7ad
JB
15697
15698 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
15699 if (attr != NULL)
15700 bit_stride = DW_UNSND (attr);
15701
c906108c
SS
15702 /* Irix 6.2 native cc creates array types without children for
15703 arrays with unspecified length. */
639d11d3 15704 if (die->child == NULL)
c906108c 15705 {
46bf5051 15706 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 15707 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 15708 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 15709 byte_stride_prop, bit_stride);
f792889a 15710 return set_die_type (die, type, cu);
c906108c
SS
15711 }
15712
791afaa2 15713 std::vector<struct type *> range_types;
639d11d3 15714 child_die = die->child;
c906108c
SS
15715 while (child_die && child_die->tag)
15716 {
15717 if (child_die->tag == DW_TAG_subrange_type)
15718 {
f792889a 15719 struct type *child_type = read_type_die (child_die, cu);
9a619af0 15720
f792889a 15721 if (child_type != NULL)
a02abb62 15722 {
0963b4bd
MS
15723 /* The range type was succesfully read. Save it for the
15724 array type creation. */
791afaa2 15725 range_types.push_back (child_type);
a02abb62 15726 }
c906108c 15727 }
436c571c 15728 child_die = child_die->sibling;
c906108c
SS
15729 }
15730
15731 /* Dwarf2 dimensions are output from left to right, create the
15732 necessary array types in backwards order. */
7ca2d3a3 15733
c906108c 15734 type = element_type;
7ca2d3a3
DL
15735
15736 if (read_array_order (die, cu) == DW_ORD_col_major)
15737 {
15738 int i = 0;
9a619af0 15739
791afaa2 15740 while (i < range_types.size ())
dc53a7ad 15741 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 15742 byte_stride_prop, bit_stride);
7ca2d3a3
DL
15743 }
15744 else
15745 {
791afaa2 15746 size_t ndim = range_types.size ();
7ca2d3a3 15747 while (ndim-- > 0)
dc53a7ad 15748 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 15749 byte_stride_prop, bit_stride);
7ca2d3a3 15750 }
c906108c 15751
f5f8a009
EZ
15752 /* Understand Dwarf2 support for vector types (like they occur on
15753 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
15754 array type. This is not part of the Dwarf2/3 standard yet, but a
15755 custom vendor extension. The main difference between a regular
15756 array and the vector variant is that vectors are passed by value
15757 to functions. */
e142c38c 15758 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 15759 if (attr != nullptr)
ea37ba09 15760 make_vector_type (type);
f5f8a009 15761
dbc98a8b
KW
15762 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
15763 implementation may choose to implement triple vectors using this
15764 attribute. */
15765 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15766 if (attr != nullptr)
dbc98a8b
KW
15767 {
15768 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
15769 TYPE_LENGTH (type) = DW_UNSND (attr);
15770 else
b98664d3 15771 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 15772 "than the total size of elements"));
dbc98a8b
KW
15773 }
15774
39cbfefa
DJ
15775 name = dwarf2_name (die, cu);
15776 if (name)
15777 TYPE_NAME (type) = name;
6e70227d 15778
2b4424c3
TT
15779 maybe_set_alignment (cu, die, type);
15780
0963b4bd 15781 /* Install the type in the die. */
7e314c57
JK
15782 set_die_type (die, type, cu);
15783
15784 /* set_die_type should be already done. */
b4ba55a1
JB
15785 set_descriptive_type (type, die, cu);
15786
7e314c57 15787 return type;
c906108c
SS
15788}
15789
7ca2d3a3 15790static enum dwarf_array_dim_ordering
6e70227d 15791read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
15792{
15793 struct attribute *attr;
15794
15795 attr = dwarf2_attr (die, DW_AT_ordering, cu);
15796
435d3d88 15797 if (attr != nullptr)
aead7601 15798 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 15799
0963b4bd
MS
15800 /* GNU F77 is a special case, as at 08/2004 array type info is the
15801 opposite order to the dwarf2 specification, but data is still
15802 laid out as per normal fortran.
7ca2d3a3 15803
0963b4bd
MS
15804 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
15805 version checking. */
7ca2d3a3 15806
905e0470
PM
15807 if (cu->language == language_fortran
15808 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
15809 {
15810 return DW_ORD_row_major;
15811 }
15812
6e70227d 15813 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
15814 {
15815 case array_column_major:
15816 return DW_ORD_col_major;
15817 case array_row_major:
15818 default:
15819 return DW_ORD_row_major;
15820 };
15821}
15822
72019c9c 15823/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 15824 the DIE's type field. */
72019c9c 15825
f792889a 15826static struct type *
72019c9c
GM
15827read_set_type (struct die_info *die, struct dwarf2_cu *cu)
15828{
7e314c57
JK
15829 struct type *domain_type, *set_type;
15830 struct attribute *attr;
f792889a 15831
7e314c57
JK
15832 domain_type = die_type (die, cu);
15833
15834 /* The die_type call above may have already set the type for this DIE. */
15835 set_type = get_die_type (die, cu);
15836 if (set_type)
15837 return set_type;
15838
15839 set_type = create_set_type (NULL, domain_type);
15840
15841 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15842 if (attr != nullptr)
d09039dd 15843 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 15844
2b4424c3
TT
15845 maybe_set_alignment (cu, die, set_type);
15846
f792889a 15847 return set_die_type (die, set_type, cu);
72019c9c 15848}
7ca2d3a3 15849
0971de02
TT
15850/* A helper for read_common_block that creates a locexpr baton.
15851 SYM is the symbol which we are marking as computed.
15852 COMMON_DIE is the DIE for the common block.
15853 COMMON_LOC is the location expression attribute for the common
15854 block itself.
15855 MEMBER_LOC is the location expression attribute for the particular
15856 member of the common block that we are processing.
15857 CU is the CU from which the above come. */
15858
15859static void
15860mark_common_block_symbol_computed (struct symbol *sym,
15861 struct die_info *common_die,
15862 struct attribute *common_loc,
15863 struct attribute *member_loc,
15864 struct dwarf2_cu *cu)
15865{
518817b3
SM
15866 struct dwarf2_per_objfile *dwarf2_per_objfile
15867 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
15868 struct objfile *objfile = dwarf2_per_objfile->objfile;
15869 struct dwarf2_locexpr_baton *baton;
15870 gdb_byte *ptr;
15871 unsigned int cu_off;
15872 enum bfd_endian byte_order = gdbarch_byte_order (get_objfile_arch (objfile));
15873 LONGEST offset = 0;
15874
15875 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
15876 gdb_assert (common_loc->form_is_block ());
15877 gdb_assert (member_loc->form_is_block ()
cd6c91b4 15878 || member_loc->form_is_constant ());
0971de02 15879
8d749320 15880 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
15881 baton->per_cu = cu->per_cu;
15882 gdb_assert (baton->per_cu);
15883
15884 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
15885
cd6c91b4 15886 if (member_loc->form_is_constant ())
0971de02 15887 {
0826b30a 15888 offset = member_loc->constant_value (0);
0971de02
TT
15889 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
15890 }
15891 else
15892 baton->size += DW_BLOCK (member_loc)->size;
15893
224c3ddb 15894 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
15895 baton->data = ptr;
15896
15897 *ptr++ = DW_OP_call4;
9c541725 15898 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
15899 store_unsigned_integer (ptr, 4, byte_order, cu_off);
15900 ptr += 4;
15901
cd6c91b4 15902 if (member_loc->form_is_constant ())
0971de02
TT
15903 {
15904 *ptr++ = DW_OP_addr;
15905 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
15906 ptr += cu->header.addr_size;
15907 }
15908 else
15909 {
15910 /* We have to copy the data here, because DW_OP_call4 will only
15911 use a DW_AT_location attribute. */
15912 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
15913 ptr += DW_BLOCK (member_loc)->size;
15914 }
15915
15916 *ptr++ = DW_OP_plus;
15917 gdb_assert (ptr - baton->data == baton->size);
15918
0971de02 15919 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 15920 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
15921}
15922
4357ac6c
TT
15923/* Create appropriate locally-scoped variables for all the
15924 DW_TAG_common_block entries. Also create a struct common_block
15925 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 15926 is used to separate the common blocks name namespace from regular
4357ac6c 15927 variable names. */
c906108c
SS
15928
15929static void
e7c27a73 15930read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15931{
0971de02
TT
15932 struct attribute *attr;
15933
15934 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 15935 if (attr != nullptr)
0971de02
TT
15936 {
15937 /* Support the .debug_loc offsets. */
4fc6c0d5 15938 if (attr->form_is_block ())
0971de02
TT
15939 {
15940 /* Ok. */
15941 }
cd6c91b4 15942 else if (attr->form_is_section_offset ())
0971de02
TT
15943 {
15944 dwarf2_complex_location_expr_complaint ();
15945 attr = NULL;
15946 }
15947 else
15948 {
15949 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
15950 "common block member");
15951 attr = NULL;
15952 }
15953 }
15954
639d11d3 15955 if (die->child != NULL)
c906108c 15956 {
518817b3 15957 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
15958 struct die_info *child_die;
15959 size_t n_entries = 0, size;
15960 struct common_block *common_block;
15961 struct symbol *sym;
74ac6d43 15962
4357ac6c
TT
15963 for (child_die = die->child;
15964 child_die && child_die->tag;
436c571c 15965 child_die = child_die->sibling)
4357ac6c
TT
15966 ++n_entries;
15967
15968 size = (sizeof (struct common_block)
15969 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
15970 common_block
15971 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
15972 size);
4357ac6c
TT
15973 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
15974 common_block->n_entries = 0;
15975
15976 for (child_die = die->child;
15977 child_die && child_die->tag;
436c571c 15978 child_die = child_die->sibling)
4357ac6c
TT
15979 {
15980 /* Create the symbol in the DW_TAG_common_block block in the current
15981 symbol scope. */
e7c27a73 15982 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
15983 if (sym != NULL)
15984 {
15985 struct attribute *member_loc;
15986
15987 common_block->contents[common_block->n_entries++] = sym;
15988
15989 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
15990 cu);
15991 if (member_loc)
15992 {
15993 /* GDB has handled this for a long time, but it is
15994 not specified by DWARF. It seems to have been
15995 emitted by gfortran at least as recently as:
15996 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 15997 complaint (_("Variable in common block has "
0971de02 15998 "DW_AT_data_member_location "
9d8780f0
SM
15999 "- DIE at %s [in module %s]"),
16000 sect_offset_str (child_die->sect_off),
518817b3 16001 objfile_name (objfile));
0971de02 16002
cd6c91b4 16003 if (member_loc->form_is_section_offset ())
0971de02 16004 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16005 else if (member_loc->form_is_constant ()
4fc6c0d5 16006 || member_loc->form_is_block ())
0971de02 16007 {
435d3d88 16008 if (attr != nullptr)
0971de02
TT
16009 mark_common_block_symbol_computed (sym, die, attr,
16010 member_loc, cu);
16011 }
16012 else
16013 dwarf2_complex_location_expr_complaint ();
16014 }
16015 }
c906108c 16016 }
4357ac6c
TT
16017
16018 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16019 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16020 }
16021}
16022
0114d602 16023/* Create a type for a C++ namespace. */
d9fa45fe 16024
0114d602
DJ
16025static struct type *
16026read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16027{
518817b3 16028 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16029 const char *previous_prefix, *name;
9219021c 16030 int is_anonymous;
0114d602
DJ
16031 struct type *type;
16032
16033 /* For extensions, reuse the type of the original namespace. */
16034 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16035 {
16036 struct die_info *ext_die;
16037 struct dwarf2_cu *ext_cu = cu;
9a619af0 16038
0114d602
DJ
16039 ext_die = dwarf2_extension (die, &ext_cu);
16040 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16041
16042 /* EXT_CU may not be the same as CU.
02142a6c 16043 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16044 return set_die_type (die, type, cu);
16045 }
9219021c 16046
e142c38c 16047 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16048
16049 /* Now build the name of the current namespace. */
16050
0114d602
DJ
16051 previous_prefix = determine_prefix (die, cu);
16052 if (previous_prefix[0] != '\0')
16053 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16054 previous_prefix, name, 0, cu);
0114d602
DJ
16055
16056 /* Create the type. */
19f392bc 16057 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16058
60531b24 16059 return set_die_type (die, type, cu);
0114d602
DJ
16060}
16061
22cee43f 16062/* Read a namespace scope. */
0114d602
DJ
16063
16064static void
16065read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16066{
518817b3 16067 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16068 int is_anonymous;
9219021c 16069
5c4e30ca
DC
16070 /* Add a symbol associated to this if we haven't seen the namespace
16071 before. Also, add a using directive if it's an anonymous
16072 namespace. */
9219021c 16073
f2f0e013 16074 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16075 {
16076 struct type *type;
16077
0114d602 16078 type = read_type_die (die, cu);
e7c27a73 16079 new_symbol (die, type, cu);
5c4e30ca 16080
e8e80198 16081 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16082 if (is_anonymous)
0114d602
DJ
16083 {
16084 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16085
eb1e02fd 16086 std::vector<const char *> excludes;
804d2729 16087 add_using_directive (using_directives (cu),
22cee43f 16088 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16089 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16090 }
5c4e30ca 16091 }
9219021c 16092
639d11d3 16093 if (die->child != NULL)
d9fa45fe 16094 {
639d11d3 16095 struct die_info *child_die = die->child;
6e70227d 16096
d9fa45fe
DC
16097 while (child_die && child_die->tag)
16098 {
e7c27a73 16099 process_die (child_die, cu);
436c571c 16100 child_die = child_die->sibling;
d9fa45fe
DC
16101 }
16102 }
38d518c9
EZ
16103}
16104
f55ee35c
JK
16105/* Read a Fortran module as type. This DIE can be only a declaration used for
16106 imported module. Still we need that type as local Fortran "use ... only"
16107 declaration imports depend on the created type in determine_prefix. */
16108
16109static struct type *
16110read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16111{
518817b3 16112 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16113 const char *module_name;
f55ee35c
JK
16114 struct type *type;
16115
16116 module_name = dwarf2_name (die, cu);
19f392bc 16117 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16118
f55ee35c
JK
16119 return set_die_type (die, type, cu);
16120}
16121
5d7cb8df
JK
16122/* Read a Fortran module. */
16123
16124static void
16125read_module (struct die_info *die, struct dwarf2_cu *cu)
16126{
16127 struct die_info *child_die = die->child;
530e8392
KB
16128 struct type *type;
16129
16130 type = read_type_die (die, cu);
16131 new_symbol (die, type, cu);
5d7cb8df 16132
5d7cb8df
JK
16133 while (child_die && child_die->tag)
16134 {
16135 process_die (child_die, cu);
436c571c 16136 child_die = child_die->sibling;
5d7cb8df
JK
16137 }
16138}
16139
38d518c9
EZ
16140/* Return the name of the namespace represented by DIE. Set
16141 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16142 namespace. */
16143
16144static const char *
e142c38c 16145namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16146{
16147 struct die_info *current_die;
16148 const char *name = NULL;
16149
16150 /* Loop through the extensions until we find a name. */
16151
16152 for (current_die = die;
16153 current_die != NULL;
f2f0e013 16154 current_die = dwarf2_extension (die, &cu))
38d518c9 16155 {
96553a0c
DE
16156 /* We don't use dwarf2_name here so that we can detect the absence
16157 of a name -> anonymous namespace. */
7d45c7c3 16158 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16159
38d518c9
EZ
16160 if (name != NULL)
16161 break;
16162 }
16163
16164 /* Is it an anonymous namespace? */
16165
16166 *is_anonymous = (name == NULL);
16167 if (*is_anonymous)
2b1dbab0 16168 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16169
16170 return name;
d9fa45fe
DC
16171}
16172
c906108c
SS
16173/* Extract all information from a DW_TAG_pointer_type DIE and add to
16174 the user defined type vector. */
16175
f792889a 16176static struct type *
e7c27a73 16177read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16178{
518817b3
SM
16179 struct gdbarch *gdbarch
16180 = get_objfile_arch (cu->per_cu->dwarf2_per_objfile->objfile);
e7c27a73 16181 struct comp_unit_head *cu_header = &cu->header;
c906108c 16182 struct type *type;
8b2dbe47
KB
16183 struct attribute *attr_byte_size;
16184 struct attribute *attr_address_class;
16185 int byte_size, addr_class;
7e314c57
JK
16186 struct type *target_type;
16187
16188 target_type = die_type (die, cu);
c906108c 16189
7e314c57
JK
16190 /* The die_type call above may have already set the type for this DIE. */
16191 type = get_die_type (die, cu);
16192 if (type)
16193 return type;
16194
16195 type = lookup_pointer_type (target_type);
8b2dbe47 16196
e142c38c 16197 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16198 if (attr_byte_size)
16199 byte_size = DW_UNSND (attr_byte_size);
c906108c 16200 else
8b2dbe47
KB
16201 byte_size = cu_header->addr_size;
16202
e142c38c 16203 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16204 if (attr_address_class)
16205 addr_class = DW_UNSND (attr_address_class);
16206 else
16207 addr_class = DW_ADDR_none;
16208
2b4424c3
TT
16209 ULONGEST alignment = get_alignment (cu, die);
16210
16211 /* If the pointer size, alignment, or address class is different
16212 than the default, create a type variant marked as such and set
16213 the length accordingly. */
16214 if (TYPE_LENGTH (type) != byte_size
16215 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16216 && alignment != TYPE_RAW_ALIGN (type))
16217 || addr_class != DW_ADDR_none)
c906108c 16218 {
5e2b427d 16219 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16220 {
16221 int type_flags;
16222
849957d9 16223 type_flags = gdbarch_address_class_type_flags
5e2b427d 16224 (gdbarch, byte_size, addr_class);
876cecd0
TT
16225 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16226 == 0);
8b2dbe47
KB
16227 type = make_type_with_address_space (type, type_flags);
16228 }
16229 else if (TYPE_LENGTH (type) != byte_size)
16230 {
b98664d3 16231 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16232 }
2b4424c3
TT
16233 else if (TYPE_RAW_ALIGN (type) != alignment)
16234 {
b98664d3 16235 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16236 " - DIE at %s [in module %s]"),
16237 sect_offset_str (die->sect_off),
16238 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16239 }
6e70227d 16240 else
9a619af0
MS
16241 {
16242 /* Should we also complain about unhandled address classes? */
16243 }
c906108c 16244 }
8b2dbe47
KB
16245
16246 TYPE_LENGTH (type) = byte_size;
2b4424c3 16247 set_type_align (type, alignment);
f792889a 16248 return set_die_type (die, type, cu);
c906108c
SS
16249}
16250
16251/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16252 the user defined type vector. */
16253
f792889a 16254static struct type *
e7c27a73 16255read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16256{
16257 struct type *type;
16258 struct type *to_type;
16259 struct type *domain;
16260
e7c27a73
DJ
16261 to_type = die_type (die, cu);
16262 domain = die_containing_type (die, cu);
0d5de010 16263
7e314c57
JK
16264 /* The calls above may have already set the type for this DIE. */
16265 type = get_die_type (die, cu);
16266 if (type)
16267 return type;
16268
0d5de010
DJ
16269 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16270 type = lookup_methodptr_type (to_type);
7078baeb
TT
16271 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16272 {
518817b3
SM
16273 struct type *new_type
16274 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16275
16276 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16277 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16278 TYPE_VARARGS (to_type));
16279 type = lookup_methodptr_type (new_type);
16280 }
0d5de010
DJ
16281 else
16282 type = lookup_memberptr_type (to_type, domain);
c906108c 16283
f792889a 16284 return set_die_type (die, type, cu);
c906108c
SS
16285}
16286
4297a3f0 16287/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16288 the user defined type vector. */
16289
f792889a 16290static struct type *
4297a3f0
AV
16291read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16292 enum type_code refcode)
c906108c 16293{
e7c27a73 16294 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16295 struct type *type, *target_type;
c906108c
SS
16296 struct attribute *attr;
16297
4297a3f0
AV
16298 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16299
7e314c57
JK
16300 target_type = die_type (die, cu);
16301
16302 /* The die_type call above may have already set the type for this DIE. */
16303 type = get_die_type (die, cu);
16304 if (type)
16305 return type;
16306
4297a3f0 16307 type = lookup_reference_type (target_type, refcode);
e142c38c 16308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16309 if (attr != nullptr)
c906108c
SS
16310 {
16311 TYPE_LENGTH (type) = DW_UNSND (attr);
16312 }
16313 else
16314 {
107d2387 16315 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16316 }
2b4424c3 16317 maybe_set_alignment (cu, die, type);
f792889a 16318 return set_die_type (die, type, cu);
c906108c
SS
16319}
16320
cf363f18
MW
16321/* Add the given cv-qualifiers to the element type of the array. GCC
16322 outputs DWARF type qualifiers that apply to an array, not the
16323 element type. But GDB relies on the array element type to carry
16324 the cv-qualifiers. This mimics section 6.7.3 of the C99
16325 specification. */
16326
16327static struct type *
16328add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16329 struct type *base_type, int cnst, int voltl)
16330{
16331 struct type *el_type, *inner_array;
16332
16333 base_type = copy_type (base_type);
16334 inner_array = base_type;
16335
16336 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16337 {
16338 TYPE_TARGET_TYPE (inner_array) =
16339 copy_type (TYPE_TARGET_TYPE (inner_array));
16340 inner_array = TYPE_TARGET_TYPE (inner_array);
16341 }
16342
16343 el_type = TYPE_TARGET_TYPE (inner_array);
16344 cnst |= TYPE_CONST (el_type);
16345 voltl |= TYPE_VOLATILE (el_type);
16346 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16347
16348 return set_die_type (die, base_type, cu);
16349}
16350
f792889a 16351static struct type *
e7c27a73 16352read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16353{
f792889a 16354 struct type *base_type, *cv_type;
c906108c 16355
e7c27a73 16356 base_type = die_type (die, cu);
7e314c57
JK
16357
16358 /* The die_type call above may have already set the type for this DIE. */
16359 cv_type = get_die_type (die, cu);
16360 if (cv_type)
16361 return cv_type;
16362
2f608a3a
KW
16363 /* In case the const qualifier is applied to an array type, the element type
16364 is so qualified, not the array type (section 6.7.3 of C99). */
16365 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16366 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16367
f792889a
DJ
16368 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16369 return set_die_type (die, cv_type, cu);
c906108c
SS
16370}
16371
f792889a 16372static struct type *
e7c27a73 16373read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16374{
f792889a 16375 struct type *base_type, *cv_type;
c906108c 16376
e7c27a73 16377 base_type = die_type (die, cu);
7e314c57
JK
16378
16379 /* The die_type call above may have already set the type for this DIE. */
16380 cv_type = get_die_type (die, cu);
16381 if (cv_type)
16382 return cv_type;
16383
cf363f18
MW
16384 /* In case the volatile qualifier is applied to an array type, the
16385 element type is so qualified, not the array type (section 6.7.3
16386 of C99). */
16387 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16388 return add_array_cv_type (die, cu, base_type, 0, 1);
16389
f792889a
DJ
16390 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16391 return set_die_type (die, cv_type, cu);
c906108c
SS
16392}
16393
06d66ee9
TT
16394/* Handle DW_TAG_restrict_type. */
16395
16396static struct type *
16397read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16398{
16399 struct type *base_type, *cv_type;
16400
16401 base_type = die_type (die, cu);
16402
16403 /* The die_type call above may have already set the type for this DIE. */
16404 cv_type = get_die_type (die, cu);
16405 if (cv_type)
16406 return cv_type;
16407
16408 cv_type = make_restrict_type (base_type);
16409 return set_die_type (die, cv_type, cu);
16410}
16411
a2c2acaf
MW
16412/* Handle DW_TAG_atomic_type. */
16413
16414static struct type *
16415read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16416{
16417 struct type *base_type, *cv_type;
16418
16419 base_type = die_type (die, cu);
16420
16421 /* The die_type call above may have already set the type for this DIE. */
16422 cv_type = get_die_type (die, cu);
16423 if (cv_type)
16424 return cv_type;
16425
16426 cv_type = make_atomic_type (base_type);
16427 return set_die_type (die, cv_type, cu);
16428}
16429
c906108c
SS
16430/* Extract all information from a DW_TAG_string_type DIE and add to
16431 the user defined type vector. It isn't really a user defined type,
16432 but it behaves like one, with other DIE's using an AT_user_def_type
16433 attribute to reference it. */
16434
f792889a 16435static struct type *
e7c27a73 16436read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16437{
518817b3 16438 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
3b7538c0 16439 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c
SS
16440 struct type *type, *range_type, *index_type, *char_type;
16441 struct attribute *attr;
216a7e6b
AB
16442 struct dynamic_prop prop;
16443 bool length_is_constant = true;
16444 LONGEST length;
16445
16446 /* There are a couple of places where bit sizes might be made use of
16447 when parsing a DW_TAG_string_type, however, no producer that we know
16448 of make use of these. Handling bit sizes that are a multiple of the
16449 byte size is easy enough, but what about other bit sizes? Lets deal
16450 with that problem when we have to. Warn about these attributes being
16451 unsupported, then parse the type and ignore them like we always
16452 have. */
16453 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16454 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16455 {
16456 static bool warning_printed = false;
16457 if (!warning_printed)
16458 {
16459 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16460 "currently supported on DW_TAG_string_type."));
16461 warning_printed = true;
16462 }
16463 }
c906108c 16464
e142c38c 16465 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16466 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16467 {
16468 /* The string length describes the location at which the length of
16469 the string can be found. The size of the length field can be
16470 specified with one of the attributes below. */
16471 struct type *prop_type;
16472 struct attribute *len
16473 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16474 if (len == nullptr)
16475 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16476 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16477 {
16478 /* Pass 0 as the default as we know this attribute is constant
16479 and the default value will not be returned. */
0826b30a 16480 LONGEST sz = len->constant_value (0);
09ba997f 16481 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16482 }
16483 else
16484 {
16485 /* If the size is not specified then we assume it is the size of
16486 an address on this target. */
09ba997f 16487 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16488 }
16489
16490 /* Convert the attribute into a dynamic property. */
16491 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16492 length = 1;
16493 else
16494 length_is_constant = false;
16495 }
16496 else if (attr != nullptr)
16497 {
16498 /* This DW_AT_string_length just contains the length with no
16499 indirection. There's no need to create a dynamic property in this
16500 case. Pass 0 for the default value as we know it will not be
16501 returned in this case. */
0826b30a 16502 length = attr->constant_value (0);
216a7e6b
AB
16503 }
16504 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16505 {
216a7e6b 16506 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16507 length = attr->constant_value (1);
c906108c
SS
16508 }
16509 else
16510 {
216a7e6b
AB
16511 /* Use 1 as a fallback length if we have nothing else. */
16512 length = 1;
c906108c 16513 }
6ccb9162 16514
46bf5051 16515 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16516 if (length_is_constant)
16517 range_type = create_static_range_type (NULL, index_type, 1, length);
16518 else
16519 {
16520 struct dynamic_prop low_bound;
16521
16522 low_bound.kind = PROP_CONST;
16523 low_bound.data.const_val = 1;
16524 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16525 }
3b7538c0
UW
16526 char_type = language_string_char_type (cu->language_defn, gdbarch);
16527 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16528
f792889a 16529 return set_die_type (die, type, cu);
c906108c
SS
16530}
16531
4d804846
JB
16532/* Assuming that DIE corresponds to a function, returns nonzero
16533 if the function is prototyped. */
16534
16535static int
16536prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16537{
16538 struct attribute *attr;
16539
16540 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16541 if (attr && (DW_UNSND (attr) != 0))
16542 return 1;
16543
16544 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16545 is only meaningful for C, but the concept also extends to other
4d804846
JB
16546 languages that allow unprototyped functions (Eg: Objective C).
16547 For all other languages, assume that functions are always
16548 prototyped. */
16549 if (cu->language != language_c
16550 && cu->language != language_objc
16551 && cu->language != language_opencl)
16552 return 1;
16553
16554 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16555 prototyped and unprototyped functions; default to prototyped,
16556 since that is more common in modern code (and RealView warns
16557 about unprototyped functions). */
16558 if (producer_is_realview (cu->producer))
16559 return 1;
16560
16561 return 0;
16562}
16563
c906108c
SS
16564/* Handle DIES due to C code like:
16565
16566 struct foo
c5aa993b
JM
16567 {
16568 int (*funcp)(int a, long l);
16569 int b;
16570 };
c906108c 16571
0963b4bd 16572 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16573
f792889a 16574static struct type *
e7c27a73 16575read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16576{
518817b3 16577 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16578 struct type *type; /* Type that this function returns. */
16579 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16580 struct attribute *attr;
16581
e7c27a73 16582 type = die_type (die, cu);
7e314c57
JK
16583
16584 /* The die_type call above may have already set the type for this DIE. */
16585 ftype = get_die_type (die, cu);
16586 if (ftype)
16587 return ftype;
16588
0c8b41f1 16589 ftype = lookup_function_type (type);
c906108c 16590
4d804846 16591 if (prototyped_function_p (die, cu))
a6c727b2 16592 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16593
c055b101
CV
16594 /* Store the calling convention in the type if it's available in
16595 the subroutine die. Otherwise set the calling convention to
16596 the default value DW_CC_normal. */
16597 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16598 if (attr != nullptr
16599 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16600 TYPE_CALLING_CONVENTION (ftype)
16601 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16602 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16603 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16604 else
16605 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16606
743649fd
MW
16607 /* Record whether the function returns normally to its caller or not
16608 if the DWARF producer set that information. */
16609 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16610 if (attr && (DW_UNSND (attr) != 0))
16611 TYPE_NO_RETURN (ftype) = 1;
16612
76c10ea2
GM
16613 /* We need to add the subroutine type to the die immediately so
16614 we don't infinitely recurse when dealing with parameters
0963b4bd 16615 declared as the same subroutine type. */
76c10ea2 16616 set_die_type (die, ftype, cu);
6e70227d 16617
639d11d3 16618 if (die->child != NULL)
c906108c 16619 {
bb5ed363 16620 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16621 struct die_info *child_die;
8072405b 16622 int nparams, iparams;
c906108c
SS
16623
16624 /* Count the number of parameters.
16625 FIXME: GDB currently ignores vararg functions, but knows about
16626 vararg member functions. */
8072405b 16627 nparams = 0;
639d11d3 16628 child_die = die->child;
c906108c
SS
16629 while (child_die && child_die->tag)
16630 {
16631 if (child_die->tag == DW_TAG_formal_parameter)
16632 nparams++;
16633 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16634 TYPE_VARARGS (ftype) = 1;
436c571c 16635 child_die = child_die->sibling;
c906108c
SS
16636 }
16637
16638 /* Allocate storage for parameters and fill them in. */
16639 TYPE_NFIELDS (ftype) = nparams;
16640 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 16641 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 16642
8072405b
JK
16643 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
16644 even if we error out during the parameters reading below. */
16645 for (iparams = 0; iparams < nparams; iparams++)
16646 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
16647
16648 iparams = 0;
639d11d3 16649 child_die = die->child;
c906108c
SS
16650 while (child_die && child_die->tag)
16651 {
16652 if (child_die->tag == DW_TAG_formal_parameter)
16653 {
3ce3b1ba
PA
16654 struct type *arg_type;
16655
16656 /* DWARF version 2 has no clean way to discern C++
16657 static and non-static member functions. G++ helps
16658 GDB by marking the first parameter for non-static
16659 member functions (which is the this pointer) as
16660 artificial. We pass this information to
16661 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
16662
16663 DWARF version 3 added DW_AT_object_pointer, which GCC
16664 4.5 does not yet generate. */
e142c38c 16665 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 16666 if (attr != nullptr)
c906108c
SS
16667 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
16668 else
9c37b5ae 16669 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
16670 arg_type = die_type (child_die, cu);
16671
16672 /* RealView does not mark THIS as const, which the testsuite
16673 expects. GCC marks THIS as const in method definitions,
16674 but not in the class specifications (GCC PR 43053). */
16675 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
16676 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
16677 {
16678 int is_this = 0;
16679 struct dwarf2_cu *arg_cu = cu;
16680 const char *name = dwarf2_name (child_die, cu);
16681
16682 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 16683 if (attr != nullptr)
3ce3b1ba
PA
16684 {
16685 /* If the compiler emits this, use it. */
16686 if (follow_die_ref (die, attr, &arg_cu) == child_die)
16687 is_this = 1;
16688 }
16689 else if (name && strcmp (name, "this") == 0)
16690 /* Function definitions will have the argument names. */
16691 is_this = 1;
16692 else if (name == NULL && iparams == 0)
16693 /* Declarations may not have the names, so like
16694 elsewhere in GDB, assume an artificial first
16695 argument is "this". */
16696 is_this = 1;
16697
16698 if (is_this)
16699 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
16700 arg_type, 0);
16701 }
16702
16703 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
16704 iparams++;
16705 }
436c571c 16706 child_die = child_die->sibling;
c906108c
SS
16707 }
16708 }
16709
76c10ea2 16710 return ftype;
c906108c
SS
16711}
16712
f792889a 16713static struct type *
e7c27a73 16714read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16715{
518817b3 16716 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16717 const char *name = NULL;
3c8e0968 16718 struct type *this_type, *target_type;
c906108c 16719
94af9270 16720 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
16721 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
16722 TYPE_TARGET_STUB (this_type) = 1;
f792889a 16723 set_die_type (die, this_type, cu);
3c8e0968
DE
16724 target_type = die_type (die, cu);
16725 if (target_type != this_type)
16726 TYPE_TARGET_TYPE (this_type) = target_type;
16727 else
16728 {
16729 /* Self-referential typedefs are, it seems, not allowed by the DWARF
16730 spec and cause infinite loops in GDB. */
b98664d3 16731 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
16732 "- DIE at %s [in module %s]"),
16733 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
16734 TYPE_TARGET_TYPE (this_type) = NULL;
16735 }
e4003a34
TV
16736 if (name == NULL)
16737 {
16738 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
16739 anonymous typedefs, which is, strictly speaking, invalid DWARF.
16740 Handle these by just returning the target type, rather than
16741 constructing an anonymous typedef type and trying to handle this
16742 elsewhere. */
16743 set_die_type (die, target_type, cu);
16744 return target_type;
16745 }
f792889a 16746 return this_type;
c906108c
SS
16747}
16748
9b790ce7
UW
16749/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
16750 (which may be different from NAME) to the architecture back-end to allow
16751 it to guess the correct format if necessary. */
16752
16753static struct type *
16754dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 16755 const char *name_hint, enum bfd_endian byte_order)
9b790ce7
UW
16756{
16757 struct gdbarch *gdbarch = get_objfile_arch (objfile);
16758 const struct floatformat **format;
16759 struct type *type;
16760
16761 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
16762 if (format)
103a685e 16763 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 16764 else
77b7c781 16765 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
16766
16767 return type;
16768}
16769
eb77c9df
AB
16770/* Allocate an integer type of size BITS and name NAME. */
16771
16772static struct type *
16773dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
16774 int bits, int unsigned_p, const char *name)
16775{
16776 struct type *type;
16777
16778 /* Versions of Intel's C Compiler generate an integer type called "void"
16779 instead of using DW_TAG_unspecified_type. This has been seen on
16780 at least versions 14, 17, and 18. */
35ee2dc2
AB
16781 if (bits == 0 && producer_is_icc (cu) && name != nullptr
16782 && strcmp (name, "void") == 0)
eb77c9df
AB
16783 type = objfile_type (objfile)->builtin_void;
16784 else
16785 type = init_integer_type (objfile, bits, unsigned_p, name);
16786
16787 return type;
16788}
16789
8bdc1658
AB
16790/* Initialise and return a floating point type of size BITS suitable for
16791 use as a component of a complex number. The NAME_HINT is passed through
16792 when initialising the floating point type and is the name of the complex
16793 type.
16794
16795 As DWARF doesn't currently provide an explicit name for the components
16796 of a complex number, but it can be helpful to have these components
16797 named, we try to select a suitable name based on the size of the
16798 component. */
16799static struct type *
16800dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
16801 struct objfile *objfile,
103a685e
TT
16802 int bits, const char *name_hint,
16803 enum bfd_endian byte_order)
8bdc1658
AB
16804{
16805 gdbarch *gdbarch = get_objfile_arch (objfile);
16806 struct type *tt = nullptr;
16807
35add35e
AB
16808 /* Try to find a suitable floating point builtin type of size BITS.
16809 We're going to use the name of this type as the name for the complex
16810 target type that we are about to create. */
1db455a7 16811 switch (cu->language)
8bdc1658 16812 {
1db455a7
AB
16813 case language_fortran:
16814 switch (bits)
16815 {
16816 case 32:
16817 tt = builtin_f_type (gdbarch)->builtin_real;
16818 break;
16819 case 64:
16820 tt = builtin_f_type (gdbarch)->builtin_real_s8;
16821 break;
16822 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16823 case 128:
16824 tt = builtin_f_type (gdbarch)->builtin_real_s16;
16825 break;
16826 }
8bdc1658 16827 break;
1db455a7
AB
16828 default:
16829 switch (bits)
16830 {
16831 case 32:
16832 tt = builtin_type (gdbarch)->builtin_float;
16833 break;
16834 case 64:
16835 tt = builtin_type (gdbarch)->builtin_double;
16836 break;
16837 case 96: /* The x86-32 ABI specifies 96-bit long double. */
16838 case 128:
16839 tt = builtin_type (gdbarch)->builtin_long_double;
16840 break;
16841 }
8bdc1658
AB
16842 break;
16843 }
16844
35add35e
AB
16845 /* If the type we found doesn't match the size we were looking for, then
16846 pretend we didn't find a type at all, the complex target type we
16847 create will then be nameless. */
a12e5744 16848 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
16849 tt = nullptr;
16850
8bdc1658 16851 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 16852 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
16853}
16854
c906108c
SS
16855/* Find a representation of a given base type and install
16856 it in the TYPE field of the die. */
16857
f792889a 16858static struct type *
e7c27a73 16859read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16860{
518817b3 16861 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
16862 struct type *type;
16863 struct attribute *attr;
19f392bc 16864 int encoding = 0, bits = 0;
15d034d0 16865 const char *name;
34877895 16866 gdbarch *arch;
c906108c 16867
e142c38c 16868 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 16869 if (attr != nullptr)
34877895 16870 encoding = DW_UNSND (attr);
e142c38c 16871 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16872 if (attr != nullptr)
34877895 16873 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 16874 name = dwarf2_name (die, cu);
6ccb9162 16875 if (!name)
34877895 16876 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e
TT
16877
16878 arch = get_objfile_arch (objfile);
16879 enum bfd_endian byte_order = gdbarch_byte_order (arch);
16880
34877895
PJ
16881 attr = dwarf2_attr (die, DW_AT_endianity, cu);
16882 if (attr)
103a685e
TT
16883 {
16884 int endianity = DW_UNSND (attr);
16885
16886 switch (endianity)
16887 {
16888 case DW_END_big:
16889 byte_order = BFD_ENDIAN_BIG;
16890 break;
16891 case DW_END_little:
16892 byte_order = BFD_ENDIAN_LITTLE;
16893 break;
16894 default:
16895 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
16896 break;
16897 }
16898 }
6ccb9162
UW
16899
16900 switch (encoding)
c906108c 16901 {
6ccb9162
UW
16902 case DW_ATE_address:
16903 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 16904 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 16905 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
16906 break;
16907 case DW_ATE_boolean:
19f392bc 16908 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
16909 break;
16910 case DW_ATE_complex_float:
103a685e
TT
16911 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
16912 byte_order);
93689ce9
TT
16913 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
16914 {
16915 if (name == nullptr)
16916 {
16917 struct obstack *obstack
16918 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
16919 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
16920 nullptr);
16921 }
16922 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
16923 }
16924 else
16925 type = init_complex_type (name, type);
6ccb9162
UW
16926 break;
16927 case DW_ATE_decimal_float:
19f392bc 16928 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
16929 break;
16930 case DW_ATE_float:
103a685e 16931 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
16932 break;
16933 case DW_ATE_signed:
eb77c9df 16934 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16935 break;
16936 case DW_ATE_unsigned:
3b2b8fea
TT
16937 if (cu->language == language_fortran
16938 && name
61012eef 16939 && startswith (name, "character("))
19f392bc
UW
16940 type = init_character_type (objfile, bits, 1, name);
16941 else
eb77c9df 16942 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
16943 break;
16944 case DW_ATE_signed_char:
6e70227d 16945 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
16946 || cu->language == language_pascal
16947 || cu->language == language_fortran)
19f392bc
UW
16948 type = init_character_type (objfile, bits, 0, name);
16949 else
eb77c9df 16950 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
16951 break;
16952 case DW_ATE_unsigned_char:
868a0084 16953 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 16954 || cu->language == language_pascal
c44af4eb
TT
16955 || cu->language == language_fortran
16956 || cu->language == language_rust)
19f392bc
UW
16957 type = init_character_type (objfile, bits, 1, name);
16958 else
eb77c9df 16959 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 16960 break;
75079b2b 16961 case DW_ATE_UTF:
53e710ac 16962 {
53e710ac
PA
16963 if (bits == 16)
16964 type = builtin_type (arch)->builtin_char16;
16965 else if (bits == 32)
16966 type = builtin_type (arch)->builtin_char32;
16967 else
16968 {
b98664d3 16969 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 16970 bits);
eb77c9df 16971 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
16972 }
16973 return set_die_type (die, type, cu);
16974 }
75079b2b
TT
16975 break;
16976
6ccb9162 16977 default:
b98664d3 16978 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 16979 dwarf_type_encoding_name (encoding));
77b7c781 16980 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 16981 break;
c906108c 16982 }
6ccb9162 16983
0114d602 16984 if (name && strcmp (name, "char") == 0)
876cecd0 16985 TYPE_NOSIGN (type) = 1;
0114d602 16986
2b4424c3
TT
16987 maybe_set_alignment (cu, die, type);
16988
103a685e 16989 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 16990
f792889a 16991 return set_die_type (die, type, cu);
c906108c
SS
16992}
16993
80180f79
SA
16994/* Parse dwarf attribute if it's a block, reference or constant and put the
16995 resulting value of the attribute into struct bound_prop.
16996 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
16997
16998static int
16999attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17000 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17001 struct type *default_type)
80180f79
SA
17002{
17003 struct dwarf2_property_baton *baton;
518817b3
SM
17004 struct obstack *obstack
17005 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17006
9a49df9d
AB
17007 gdb_assert (default_type != NULL);
17008
80180f79
SA
17009 if (attr == NULL || prop == NULL)
17010 return 0;
17011
4fc6c0d5 17012 if (attr->form_is_block ())
80180f79 17013 {
8d749320 17014 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17015 baton->property_type = default_type;
80180f79
SA
17016 baton->locexpr.per_cu = cu->per_cu;
17017 baton->locexpr.size = DW_BLOCK (attr)->size;
17018 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17019 switch (attr->name)
17020 {
17021 case DW_AT_string_length:
17022 baton->locexpr.is_reference = true;
17023 break;
17024 default:
17025 baton->locexpr.is_reference = false;
17026 break;
17027 }
80180f79
SA
17028 prop->data.baton = baton;
17029 prop->kind = PROP_LOCEXPR;
17030 gdb_assert (prop->data.baton != NULL);
17031 }
cd6c91b4 17032 else if (attr->form_is_ref ())
80180f79
SA
17033 {
17034 struct dwarf2_cu *target_cu = cu;
17035 struct die_info *target_die;
17036 struct attribute *target_attr;
17037
17038 target_die = follow_die_ref (die, attr, &target_cu);
17039 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17040 if (target_attr == NULL)
17041 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17042 target_cu);
80180f79
SA
17043 if (target_attr == NULL)
17044 return 0;
17045
df25ebbd 17046 switch (target_attr->name)
80180f79 17047 {
df25ebbd 17048 case DW_AT_location:
cd6c91b4 17049 if (target_attr->form_is_section_offset ())
df25ebbd 17050 {
8d749320 17051 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17052 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17053 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17054 prop->data.baton = baton;
17055 prop->kind = PROP_LOCLIST;
17056 gdb_assert (prop->data.baton != NULL);
17057 }
4fc6c0d5 17058 else if (target_attr->form_is_block ())
df25ebbd 17059 {
8d749320 17060 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17061 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17062 baton->locexpr.per_cu = cu->per_cu;
17063 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17064 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17065 baton->locexpr.is_reference = true;
df25ebbd
JB
17066 prop->data.baton = baton;
17067 prop->kind = PROP_LOCEXPR;
17068 gdb_assert (prop->data.baton != NULL);
17069 }
17070 else
17071 {
17072 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17073 "dynamic property");
17074 return 0;
17075 }
17076 break;
17077 case DW_AT_data_member_location:
17078 {
17079 LONGEST offset;
17080
17081 if (!handle_data_member_location (target_die, target_cu,
17082 &offset))
17083 return 0;
17084
8d749320 17085 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17086 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17087 target_cu);
df25ebbd
JB
17088 baton->offset_info.offset = offset;
17089 baton->offset_info.type = die_type (target_die, target_cu);
17090 prop->data.baton = baton;
17091 prop->kind = PROP_ADDR_OFFSET;
17092 break;
17093 }
80180f79
SA
17094 }
17095 }
cd6c91b4 17096 else if (attr->form_is_constant ())
80180f79 17097 {
0826b30a 17098 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17099 prop->kind = PROP_CONST;
17100 }
17101 else
17102 {
17103 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17104 dwarf2_name (die, cu));
17105 return 0;
17106 }
17107
17108 return 1;
17109}
17110
09ba997f 17111/* See read.h. */
9a49df9d 17112
09ba997f
TT
17113struct type *
17114dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17115{
09ba997f 17116 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17117 struct type *int_type;
17118
17119 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17120#define TRY_TYPE(F) \
17121 int_type = (unsigned_p \
17122 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17123 : objfile_type (objfile)->builtin_ ## F); \
17124 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17125 return int_type
17126
17127 TRY_TYPE (char);
17128 TRY_TYPE (short);
17129 TRY_TYPE (int);
17130 TRY_TYPE (long);
17131 TRY_TYPE (long_long);
17132
17133#undef TRY_TYPE
17134
17135 gdb_assert_not_reached ("unable to find suitable integer type");
17136}
17137
09ba997f 17138/* See read.h. */
11a8b164 17139
09ba997f
TT
17140struct type *
17141dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17142{
09ba997f
TT
17143 int addr_size = this->addr_size ();
17144 return int_type (addr_size, unsigned_p);
11a8b164
AB
17145}
17146
b86352cf
AB
17147/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17148 present (which is valid) then compute the default type based on the
17149 compilation units address size. */
17150
17151static struct type *
17152read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17153{
17154 struct type *index_type = die_type (die, cu);
17155
17156 /* Dwarf-2 specifications explicitly allows to create subrange types
17157 without specifying a base type.
17158 In that case, the base type must be set to the type of
17159 the lower bound, upper bound or count, in that order, if any of these
17160 three attributes references an object that has a type.
17161 If no base type is found, the Dwarf-2 specifications say that
17162 a signed integer type of size equal to the size of an address should
17163 be used.
17164 For the following C code: `extern char gdb_int [];'
17165 GCC produces an empty range DIE.
17166 FIXME: muller/2010-05-28: Possible references to object for low bound,
17167 high bound or count are not yet handled by this code. */
17168 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17169 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17170
17171 return index_type;
17172}
17173
a02abb62
JB
17174/* Read the given DW_AT_subrange DIE. */
17175
f792889a 17176static struct type *
a02abb62
JB
17177read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17178{
4c9ad8c2 17179 struct type *base_type, *orig_base_type;
a02abb62
JB
17180 struct type *range_type;
17181 struct attribute *attr;
729efb13 17182 struct dynamic_prop low, high;
4fae6e18 17183 int low_default_is_valid;
c451ebe5 17184 int high_bound_is_count = 0;
15d034d0 17185 const char *name;
d359392f 17186 ULONGEST negative_mask;
e77813c8 17187
b86352cf
AB
17188 orig_base_type = read_subrange_index_type (die, cu);
17189
4c9ad8c2
TT
17190 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17191 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17192 creating the range type, but we use the result of check_typedef
17193 when examining properties of the type. */
17194 base_type = check_typedef (orig_base_type);
a02abb62 17195
7e314c57
JK
17196 /* The die_type call above may have already set the type for this DIE. */
17197 range_type = get_die_type (die, cu);
17198 if (range_type)
17199 return range_type;
17200
729efb13
SA
17201 low.kind = PROP_CONST;
17202 high.kind = PROP_CONST;
17203 high.data.const_val = 0;
17204
4fae6e18
JK
17205 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17206 omitting DW_AT_lower_bound. */
17207 switch (cu->language)
6e70227d 17208 {
4fae6e18
JK
17209 case language_c:
17210 case language_cplus:
729efb13 17211 low.data.const_val = 0;
4fae6e18
JK
17212 low_default_is_valid = 1;
17213 break;
17214 case language_fortran:
729efb13 17215 low.data.const_val = 1;
4fae6e18
JK
17216 low_default_is_valid = 1;
17217 break;
17218 case language_d:
4fae6e18 17219 case language_objc:
c44af4eb 17220 case language_rust:
729efb13 17221 low.data.const_val = 0;
4fae6e18
JK
17222 low_default_is_valid = (cu->header.version >= 4);
17223 break;
17224 case language_ada:
17225 case language_m2:
17226 case language_pascal:
729efb13 17227 low.data.const_val = 1;
4fae6e18
JK
17228 low_default_is_valid = (cu->header.version >= 4);
17229 break;
17230 default:
729efb13 17231 low.data.const_val = 0;
4fae6e18
JK
17232 low_default_is_valid = 0;
17233 break;
a02abb62
JB
17234 }
17235
e142c38c 17236 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17237 if (attr != nullptr)
9a49df9d 17238 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17239 else if (!low_default_is_valid)
b98664d3 17240 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17241 "- DIE at %s [in module %s]"),
17242 sect_offset_str (die->sect_off),
518817b3 17243 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17244
506f5c41
TV
17245 struct attribute *attr_ub, *attr_count;
17246 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17247 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17248 {
506f5c41 17249 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17250 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17251 {
c451ebe5
SA
17252 /* If bounds are constant do the final calculation here. */
17253 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17254 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17255 else
17256 high_bound_is_count = 1;
c2ff108b 17257 }
506f5c41
TV
17258 else
17259 {
17260 if (attr_ub != NULL)
17261 complaint (_("Unresolved DW_AT_upper_bound "
17262 "- DIE at %s [in module %s]"),
17263 sect_offset_str (die->sect_off),
17264 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17265 if (attr_count != NULL)
17266 complaint (_("Unresolved DW_AT_count "
17267 "- DIE at %s [in module %s]"),
17268 sect_offset_str (die->sect_off),
17269 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17270 }
e77813c8 17271 }
a02abb62 17272
4e962e74
TT
17273 LONGEST bias = 0;
17274 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17275 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17276 bias = bias_attr->constant_value (0);
4e962e74 17277
dbb9c2b1
JB
17278 /* Normally, the DWARF producers are expected to use a signed
17279 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17280 But this is unfortunately not always the case, as witnessed
17281 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17282 is used instead. To work around that ambiguity, we treat
17283 the bounds as signed, and thus sign-extend their values, when
17284 the base type is signed. */
6e70227d 17285 negative_mask =
d359392f 17286 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17287 if (low.kind == PROP_CONST
17288 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17289 low.data.const_val |= negative_mask;
17290 if (high.kind == PROP_CONST
17291 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17292 high.data.const_val |= negative_mask;
43bbcdc2 17293
5bbd8269
AB
17294 /* Check for bit and byte strides. */
17295 struct dynamic_prop byte_stride_prop;
17296 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17297 if (attr_byte_stride != nullptr)
17298 {
09ba997f 17299 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17300 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17301 prop_type);
17302 }
17303
17304 struct dynamic_prop bit_stride_prop;
17305 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17306 if (attr_bit_stride != nullptr)
17307 {
17308 /* It only makes sense to have either a bit or byte stride. */
17309 if (attr_byte_stride != nullptr)
17310 {
17311 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17312 "- DIE at %s [in module %s]"),
17313 sect_offset_str (die->sect_off),
17314 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17315 attr_bit_stride = nullptr;
17316 }
17317 else
17318 {
09ba997f 17319 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17320 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17321 prop_type);
17322 }
17323 }
17324
17325 if (attr_byte_stride != nullptr
17326 || attr_bit_stride != nullptr)
17327 {
17328 bool byte_stride_p = (attr_byte_stride != nullptr);
17329 struct dynamic_prop *stride
17330 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17331
17332 range_type
17333 = create_range_type_with_stride (NULL, orig_base_type, &low,
17334 &high, bias, stride, byte_stride_p);
17335 }
17336 else
17337 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17338
c451ebe5
SA
17339 if (high_bound_is_count)
17340 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17341
c2ff108b
JK
17342 /* Ada expects an empty array on no boundary attributes. */
17343 if (attr == NULL && cu->language != language_ada)
729efb13 17344 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17345
39cbfefa
DJ
17346 name = dwarf2_name (die, cu);
17347 if (name)
17348 TYPE_NAME (range_type) = name;
6e70227d 17349
e142c38c 17350 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17351 if (attr != nullptr)
a02abb62
JB
17352 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17353
2b4424c3
TT
17354 maybe_set_alignment (cu, die, range_type);
17355
7e314c57
JK
17356 set_die_type (die, range_type, cu);
17357
17358 /* set_die_type should be already done. */
b4ba55a1
JB
17359 set_descriptive_type (range_type, die, cu);
17360
7e314c57 17361 return range_type;
a02abb62 17362}
6e70227d 17363
f792889a 17364static struct type *
81a17f79
JB
17365read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17366{
17367 struct type *type;
81a17f79 17368
518817b3
SM
17369 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17370 NULL);
0114d602 17371 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17372
74a2f8ff 17373 /* In Ada, an unspecified type is typically used when the description
85102364 17374 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17375 such a type, we treat it as a stub, and try to resolve it later on,
17376 when needed. */
17377 if (cu->language == language_ada)
17378 TYPE_STUB (type) = 1;
17379
f792889a 17380 return set_die_type (die, type, cu);
81a17f79 17381}
a02abb62 17382
639d11d3
DC
17383/* Read a single die and all its descendents. Set the die's sibling
17384 field to NULL; set other fields in the die correctly, and set all
17385 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17386 location of the info_ptr after reading all of those dies. PARENT
17387 is the parent of the die in question. */
17388
17389static struct die_info *
dee91e82 17390read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17391 const gdb_byte *info_ptr,
17392 const gdb_byte **new_info_ptr,
dee91e82 17393 struct die_info *parent)
639d11d3
DC
17394{
17395 struct die_info *die;
d521ce57 17396 const gdb_byte *cur_ptr;
639d11d3 17397
3e225074 17398 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17399 if (die == NULL)
17400 {
17401 *new_info_ptr = cur_ptr;
17402 return NULL;
17403 }
93311388 17404 store_in_ref_table (die, reader->cu);
639d11d3 17405
3e225074 17406 if (die->has_children)
bf6af496 17407 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17408 else
17409 {
17410 die->child = NULL;
17411 *new_info_ptr = cur_ptr;
17412 }
17413
17414 die->sibling = NULL;
17415 die->parent = parent;
17416 return die;
17417}
17418
17419/* Read a die, all of its descendents, and all of its siblings; set
17420 all of the fields of all of the dies correctly. Arguments are as
17421 in read_die_and_children. */
17422
17423static struct die_info *
bf6af496 17424read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17425 const gdb_byte *info_ptr,
17426 const gdb_byte **new_info_ptr,
bf6af496 17427 struct die_info *parent)
639d11d3
DC
17428{
17429 struct die_info *first_die, *last_sibling;
d521ce57 17430 const gdb_byte *cur_ptr;
639d11d3 17431
c906108c 17432 cur_ptr = info_ptr;
639d11d3
DC
17433 first_die = last_sibling = NULL;
17434
17435 while (1)
c906108c 17436 {
639d11d3 17437 struct die_info *die
dee91e82 17438 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17439
1d325ec1 17440 if (die == NULL)
c906108c 17441 {
639d11d3
DC
17442 *new_info_ptr = cur_ptr;
17443 return first_die;
c906108c 17444 }
1d325ec1
DJ
17445
17446 if (!first_die)
17447 first_die = die;
c906108c 17448 else
1d325ec1
DJ
17449 last_sibling->sibling = die;
17450
17451 last_sibling = die;
c906108c 17452 }
c906108c
SS
17453}
17454
bf6af496
DE
17455/* Read a die, all of its descendents, and all of its siblings; set
17456 all of the fields of all of the dies correctly. Arguments are as
17457 in read_die_and_children.
17458 This the main entry point for reading a DIE and all its children. */
17459
17460static struct die_info *
17461read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17462 const gdb_byte *info_ptr,
17463 const gdb_byte **new_info_ptr,
bf6af496
DE
17464 struct die_info *parent)
17465{
17466 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17467 new_info_ptr, parent);
17468
b4f54984 17469 if (dwarf_die_debug)
bf6af496
DE
17470 {
17471 fprintf_unfiltered (gdb_stdlog,
17472 "Read die from %s@0x%x of %s:\n",
96b79293 17473 reader->die_section->get_name (),
bf6af496
DE
17474 (unsigned) (info_ptr - reader->die_section->buffer),
17475 bfd_get_filename (reader->abfd));
b4f54984 17476 dump_die (die, dwarf_die_debug);
bf6af496
DE
17477 }
17478
17479 return die;
17480}
17481
3019eac3
DE
17482/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17483 attributes.
17484 The caller is responsible for filling in the extra attributes
17485 and updating (*DIEP)->num_attrs.
17486 Set DIEP to point to a newly allocated die with its information,
3e225074 17487 except for its child, sibling, and parent fields. */
93311388 17488
d521ce57 17489static const gdb_byte *
3019eac3 17490read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17491 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17492 int num_extra_attrs)
93311388 17493{
b64f50a1 17494 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17495 struct abbrev_info *abbrev;
17496 struct die_info *die;
17497 struct dwarf2_cu *cu = reader->cu;
17498 bfd *abfd = reader->abfd;
17499
9c541725 17500 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17501 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17502 info_ptr += bytes_read;
17503 if (!abbrev_number)
17504 {
17505 *diep = NULL;
93311388
DE
17506 return info_ptr;
17507 }
17508
685af9cd 17509 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17510 if (!abbrev)
348e048f
DE
17511 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17512 abbrev_number,
17513 bfd_get_filename (abfd));
17514
3019eac3 17515 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17516 die->sect_off = sect_off;
93311388
DE
17517 die->tag = abbrev->tag;
17518 die->abbrev = abbrev_number;
3e225074 17519 die->has_children = abbrev->has_children;
93311388 17520
3019eac3
DE
17521 /* Make the result usable.
17522 The caller needs to update num_attrs after adding the extra
17523 attributes. */
93311388
DE
17524 die->num_attrs = abbrev->num_attrs;
17525
18a8505e 17526 std::vector<int> indexes_that_need_reprocess;
93311388 17527 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17528 {
17529 bool need_reprocess;
17530 info_ptr =
17531 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17532 info_ptr, &need_reprocess);
17533 if (need_reprocess)
17534 indexes_that_need_reprocess.push_back (i);
17535 }
17536
052c8bb8 17537 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17538 if (attr != nullptr)
17539 cu->str_offsets_base = DW_UNSND (attr);
93311388 17540
a39fdb41 17541 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17542 if (maybe_addr_base.has_value ())
17543 cu->addr_base = *maybe_addr_base;
17544 for (int index : indexes_that_need_reprocess)
17545 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17546 *diep = die;
93311388
DE
17547 return info_ptr;
17548}
17549
3019eac3
DE
17550/* Read a die and all its attributes.
17551 Set DIEP to point to a newly allocated die with its information,
3e225074 17552 except for its child, sibling, and parent fields. */
3019eac3 17553
d521ce57 17554static const gdb_byte *
3019eac3 17555read_full_die (const struct die_reader_specs *reader,
3e225074 17556 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17557{
d521ce57 17558 const gdb_byte *result;
bf6af496 17559
3e225074 17560 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17561
b4f54984 17562 if (dwarf_die_debug)
bf6af496
DE
17563 {
17564 fprintf_unfiltered (gdb_stdlog,
17565 "Read die from %s@0x%x of %s:\n",
96b79293 17566 reader->die_section->get_name (),
bf6af496
DE
17567 (unsigned) (info_ptr - reader->die_section->buffer),
17568 bfd_get_filename (reader->abfd));
b4f54984 17569 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17570 }
17571
17572 return result;
3019eac3 17573}
433df2d4 17574\f
c906108c 17575
72bf9492
DJ
17576/* Returns nonzero if TAG represents a type that we might generate a partial
17577 symbol for. */
17578
17579static int
17580is_type_tag_for_partial (int tag)
17581{
17582 switch (tag)
17583 {
17584#if 0
17585 /* Some types that would be reasonable to generate partial symbols for,
17586 that we don't at present. */
17587 case DW_TAG_array_type:
17588 case DW_TAG_file_type:
17589 case DW_TAG_ptr_to_member_type:
17590 case DW_TAG_set_type:
17591 case DW_TAG_string_type:
17592 case DW_TAG_subroutine_type:
17593#endif
17594 case DW_TAG_base_type:
17595 case DW_TAG_class_type:
680b30c7 17596 case DW_TAG_interface_type:
72bf9492
DJ
17597 case DW_TAG_enumeration_type:
17598 case DW_TAG_structure_type:
17599 case DW_TAG_subrange_type:
17600 case DW_TAG_typedef:
17601 case DW_TAG_union_type:
17602 return 1;
17603 default:
17604 return 0;
17605 }
17606}
17607
17608/* Load all DIEs that are interesting for partial symbols into memory. */
17609
17610static struct partial_die_info *
dee91e82 17611load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17612 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17613{
dee91e82 17614 struct dwarf2_cu *cu = reader->cu;
518817b3 17615 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17616 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17617 unsigned int bytes_read;
5afb4e99 17618 unsigned int load_all = 0;
72bf9492
DJ
17619 int nesting_level = 1;
17620
17621 parent_die = NULL;
17622 last_die = NULL;
17623
7adf1e79
DE
17624 gdb_assert (cu->per_cu != NULL);
17625 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17626 load_all = 1;
17627
72bf9492
DJ
17628 cu->partial_dies
17629 = htab_create_alloc_ex (cu->header.length / 12,
17630 partial_die_hash,
17631 partial_die_eq,
17632 NULL,
17633 &cu->comp_unit_obstack,
17634 hashtab_obstack_allocate,
17635 dummy_obstack_deallocate);
17636
72bf9492
DJ
17637 while (1)
17638 {
685af9cd 17639 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
17640
17641 /* A NULL abbrev means the end of a series of children. */
17642 if (abbrev == NULL)
17643 {
17644 if (--nesting_level == 0)
cd9983dd
YQ
17645 return first_die;
17646
72bf9492
DJ
17647 info_ptr += bytes_read;
17648 last_die = parent_die;
17649 parent_die = parent_die->die_parent;
17650 continue;
17651 }
17652
98bfdba5
PA
17653 /* Check for template arguments. We never save these; if
17654 they're seen, we just mark the parent, and go on our way. */
17655 if (parent_die != NULL
17656 && cu->language == language_cplus
17657 && (abbrev->tag == DW_TAG_template_type_param
17658 || abbrev->tag == DW_TAG_template_value_param))
17659 {
17660 parent_die->has_template_arguments = 1;
17661
17662 if (!load_all)
17663 {
17664 /* We don't need a partial DIE for the template argument. */
dee91e82 17665 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17666 continue;
17667 }
17668 }
17669
0d99eb77 17670 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
17671 Skip their other children. */
17672 if (!load_all
17673 && cu->language == language_cplus
17674 && parent_die != NULL
17675 && parent_die->tag == DW_TAG_subprogram)
17676 {
dee91e82 17677 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
17678 continue;
17679 }
17680
5afb4e99
DJ
17681 /* Check whether this DIE is interesting enough to save. Normally
17682 we would not be interested in members here, but there may be
17683 later variables referencing them via DW_AT_specification (for
17684 static members). */
17685 if (!load_all
17686 && !is_type_tag_for_partial (abbrev->tag)
72929c62 17687 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
17688 && abbrev->tag != DW_TAG_enumerator
17689 && abbrev->tag != DW_TAG_subprogram
b1dc1806 17690 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 17691 && abbrev->tag != DW_TAG_lexical_block
72bf9492 17692 && abbrev->tag != DW_TAG_variable
5afb4e99 17693 && abbrev->tag != DW_TAG_namespace
f55ee35c 17694 && abbrev->tag != DW_TAG_module
95554aad 17695 && abbrev->tag != DW_TAG_member
74921315
KS
17696 && abbrev->tag != DW_TAG_imported_unit
17697 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
17698 {
17699 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17700 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
17701 continue;
17702 }
17703
6f06d47b
YQ
17704 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
17705 abbrev);
cd9983dd 17706
48fbe735 17707 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
17708
17709 /* This two-pass algorithm for processing partial symbols has a
17710 high cost in cache pressure. Thus, handle some simple cases
17711 here which cover the majority of C partial symbols. DIEs
17712 which neither have specification tags in them, nor could have
17713 specification tags elsewhere pointing at them, can simply be
17714 processed and discarded.
17715
17716 This segment is also optional; scan_partial_symbols and
17717 add_partial_symbol will handle these DIEs if we chain
17718 them in normally. When compilers which do not emit large
17719 quantities of duplicate debug information are more common,
17720 this code can probably be removed. */
17721
17722 /* Any complete simple types at the top level (pretty much all
17723 of them, for a language without namespaces), can be processed
17724 directly. */
17725 if (parent_die == NULL
cd9983dd
YQ
17726 && pdi.has_specification == 0
17727 && pdi.is_declaration == 0
17728 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
17729 || pdi.tag == DW_TAG_base_type
17730 || pdi.tag == DW_TAG_subrange_type))
72bf9492 17731 {
cd9983dd 17732 if (building_psymtab && pdi.name != NULL)
31edb802 17733 add_psymbol_to_list (pdi.name, false,
79748972 17734 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 17735 psymbol_placement::STATIC,
1762568f 17736 0, cu->language, objfile);
cd9983dd 17737 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17738 continue;
17739 }
17740
d8228535
JK
17741 /* The exception for DW_TAG_typedef with has_children above is
17742 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 17743 type_name_or_error will error on such types later.
d8228535
JK
17744
17745 GDB skipped children of DW_TAG_typedef by the shortcut above and then
17746 it could not find the child DIEs referenced later, this is checked
17747 above. In correct DWARF DW_TAG_typedef should have no children. */
17748
cd9983dd 17749 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 17750 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 17751 "- DIE at %s [in module %s]"),
cd9983dd 17752 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 17753
72bf9492
DJ
17754 /* If we're at the second level, and we're an enumerator, and
17755 our parent has no specification (meaning possibly lives in a
17756 namespace elsewhere), then we can add the partial symbol now
17757 instead of queueing it. */
cd9983dd 17758 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
17759 && parent_die != NULL
17760 && parent_die->die_parent == NULL
17761 && parent_die->tag == DW_TAG_enumeration_type
17762 && parent_die->has_specification == 0)
17763 {
cd9983dd 17764 if (pdi.name == NULL)
b98664d3 17765 complaint (_("malformed enumerator DIE ignored"));
72bf9492 17766 else if (building_psymtab)
31edb802 17767 add_psymbol_to_list (pdi.name, false,
79748972 17768 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 17769 cu->language == language_cplus
75aedd27
TT
17770 ? psymbol_placement::GLOBAL
17771 : psymbol_placement::STATIC,
1762568f 17772 0, cu->language, objfile);
72bf9492 17773
cd9983dd 17774 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
17775 continue;
17776 }
17777
cd9983dd 17778 struct partial_die_info *part_die
6f06d47b 17779 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 17780
72bf9492
DJ
17781 /* We'll save this DIE so link it in. */
17782 part_die->die_parent = parent_die;
17783 part_die->die_sibling = NULL;
17784 part_die->die_child = NULL;
17785
17786 if (last_die && last_die == parent_die)
17787 last_die->die_child = part_die;
17788 else if (last_die)
17789 last_die->die_sibling = part_die;
17790
17791 last_die = part_die;
17792
17793 if (first_die == NULL)
17794 first_die = part_die;
17795
17796 /* Maybe add the DIE to the hash table. Not all DIEs that we
17797 find interesting need to be in the hash table, because we
17798 also have the parent/sibling/child chains; only those that we
17799 might refer to by offset later during partial symbol reading.
17800
17801 For now this means things that might have be the target of a
17802 DW_AT_specification, DW_AT_abstract_origin, or
17803 DW_AT_extension. DW_AT_extension will refer only to
17804 namespaces; DW_AT_abstract_origin refers to functions (and
17805 many things under the function DIE, but we do not recurse
17806 into function DIEs during partial symbol reading) and
17807 possibly variables as well; DW_AT_specification refers to
17808 declarations. Declarations ought to have the DW_AT_declaration
17809 flag. It happens that GCC forgets to put it in sometimes, but
17810 only for functions, not for types.
17811
17812 Adding more things than necessary to the hash table is harmless
17813 except for the performance cost. Adding too few will result in
5afb4e99
DJ
17814 wasted time in find_partial_die, when we reread the compilation
17815 unit with load_all_dies set. */
72bf9492 17816
5afb4e99 17817 if (load_all
72929c62 17818 || abbrev->tag == DW_TAG_constant
5afb4e99 17819 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
17820 || abbrev->tag == DW_TAG_variable
17821 || abbrev->tag == DW_TAG_namespace
17822 || part_die->is_declaration)
17823 {
17824 void **slot;
17825
17826 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
17827 to_underlying (part_die->sect_off),
17828 INSERT);
72bf9492
DJ
17829 *slot = part_die;
17830 }
17831
72bf9492 17832 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 17833 we have no reason to follow the children of structures; for other
98bfdba5
PA
17834 languages we have to, so that we can get at method physnames
17835 to infer fully qualified class names, for DW_AT_specification,
17836 and for C++ template arguments. For C++, we also look one level
17837 inside functions to find template arguments (if the name of the
17838 function does not already contain the template arguments).
bc30ff58 17839
0a4b0913
AB
17840 For Ada and Fortran, we need to scan the children of subprograms
17841 and lexical blocks as well because these languages allow the
17842 definition of nested entities that could be interesting for the
17843 debugger, such as nested subprograms for instance. */
72bf9492 17844 if (last_die->has_children
5afb4e99
DJ
17845 && (load_all
17846 || last_die->tag == DW_TAG_namespace
f55ee35c 17847 || last_die->tag == DW_TAG_module
72bf9492 17848 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
17849 || (cu->language == language_cplus
17850 && last_die->tag == DW_TAG_subprogram
17851 && (last_die->name == NULL
17852 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
17853 || (cu->language != language_c
17854 && (last_die->tag == DW_TAG_class_type
680b30c7 17855 || last_die->tag == DW_TAG_interface_type
72bf9492 17856 || last_die->tag == DW_TAG_structure_type
bc30ff58 17857 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
17858 || ((cu->language == language_ada
17859 || cu->language == language_fortran)
bc30ff58
JB
17860 && (last_die->tag == DW_TAG_subprogram
17861 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
17862 {
17863 nesting_level++;
17864 parent_die = last_die;
17865 continue;
17866 }
17867
17868 /* Otherwise we skip to the next sibling, if any. */
dee91e82 17869 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
17870
17871 /* Back to the top, do it again. */
17872 }
17873}
17874
6f06d47b
YQ
17875partial_die_info::partial_die_info (sect_offset sect_off_,
17876 struct abbrev_info *abbrev)
17877 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
17878{
17879}
17880
35cc7ed7
YQ
17881/* Read a minimal amount of information into the minimal die structure.
17882 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 17883
48fbe735
YQ
17884const gdb_byte *
17885partial_die_info::read (const struct die_reader_specs *reader,
17886 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 17887{
dee91e82 17888 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
17889 struct dwarf2_per_objfile *dwarf2_per_objfile
17890 = cu->per_cu->dwarf2_per_objfile;
fa238c03 17891 unsigned int i;
c5aa993b 17892 int has_low_pc_attr = 0;
c906108c 17893 int has_high_pc_attr = 0;
91da1414 17894 int high_pc_relative = 0;
c906108c 17895
fd0a254f 17896 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 17897 {
e7da7f8f 17898 attribute attr;
18a8505e 17899 bool need_reprocess;
e7da7f8f 17900 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
17901 info_ptr, &need_reprocess);
17902 /* String and address offsets that need to do the reprocessing have
17903 already been read at this point, so there is no need to wait until
17904 the loop terminates to do the reprocessing. */
17905 if (need_reprocess)
e7da7f8f 17906 read_attribute_reprocess (reader, &attr);
c906108c 17907 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 17908 partial symbol table. */
c906108c
SS
17909 switch (attr.name)
17910 {
17911 case DW_AT_name:
48fbe735 17912 switch (tag)
71c25dea
TT
17913 {
17914 case DW_TAG_compile_unit:
95554aad 17915 case DW_TAG_partial_unit:
348e048f 17916 case DW_TAG_type_unit:
71c25dea
TT
17917 /* Compilation units have a DW_AT_name that is a filename, not
17918 a source language identifier. */
17919 case DW_TAG_enumeration_type:
17920 case DW_TAG_enumerator:
17921 /* These tags always have simple identifiers already; no need
17922 to canonicalize them. */
48fbe735 17923 name = DW_STRING (&attr);
71c25dea
TT
17924 break;
17925 default:
48fbe735
YQ
17926 {
17927 struct objfile *objfile = dwarf2_per_objfile->objfile;
17928
17929 name
be1e3d3e 17930 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 17931 }
71c25dea
TT
17932 break;
17933 }
c906108c 17934 break;
31ef98ae 17935 case DW_AT_linkage_name:
c906108c 17936 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
17937 /* Note that both forms of linkage name might appear. We
17938 assume they will be the same, and we only store the last
17939 one we see. */
48fbe735 17940 linkage_name = DW_STRING (&attr);
c906108c
SS
17941 break;
17942 case DW_AT_low_pc:
17943 has_low_pc_attr = 1;
cd6c91b4 17944 lowpc = attr.value_as_address ();
c906108c
SS
17945 break;
17946 case DW_AT_high_pc:
17947 has_high_pc_attr = 1;
cd6c91b4
TT
17948 highpc = attr.value_as_address ();
17949 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 17950 high_pc_relative = 1;
c906108c
SS
17951 break;
17952 case DW_AT_location:
0963b4bd 17953 /* Support the .debug_loc offsets. */
4fc6c0d5 17954 if (attr.form_is_block ())
8e19ed76 17955 {
48fbe735 17956 d.locdesc = DW_BLOCK (&attr);
8e19ed76 17957 }
cd6c91b4 17958 else if (attr.form_is_section_offset ())
8e19ed76 17959 {
4d3c2250 17960 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
17961 }
17962 else
17963 {
4d3c2250
KB
17964 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17965 "partial symbol information");
8e19ed76 17966 }
c906108c 17967 break;
c906108c 17968 case DW_AT_external:
48fbe735 17969 is_external = DW_UNSND (&attr);
c906108c
SS
17970 break;
17971 case DW_AT_declaration:
48fbe735 17972 is_declaration = DW_UNSND (&attr);
c906108c
SS
17973 break;
17974 case DW_AT_type:
48fbe735 17975 has_type = 1;
c906108c
SS
17976 break;
17977 case DW_AT_abstract_origin:
17978 case DW_AT_specification:
72bf9492 17979 case DW_AT_extension:
48fbe735 17980 has_specification = 1;
0826b30a 17981 spec_offset = attr.get_ref_die_offset ();
48fbe735 17982 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 17983 || cu->per_cu->is_dwz);
c906108c
SS
17984 break;
17985 case DW_AT_sibling:
17986 /* Ignore absolute siblings, they might point outside of
17987 the current compile unit. */
17988 if (attr.form == DW_FORM_ref_addr)
b98664d3 17989 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 17990 else
b9502d3f 17991 {
48fbe735 17992 const gdb_byte *buffer = reader->buffer;
0826b30a 17993 sect_offset off = attr.get_ref_die_offset ();
9c541725 17994 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
17995
17996 if (sibling_ptr < info_ptr)
b98664d3 17997 complaint (_("DW_AT_sibling points backwards"));
22869d73 17998 else if (sibling_ptr > reader->buffer_end)
a0194fa8 17999 reader->die_section->overflow_complaint ();
b9502d3f 18000 else
48fbe735 18001 sibling = sibling_ptr;
b9502d3f 18002 }
c906108c 18003 break;
fa4028e9 18004 case DW_AT_byte_size:
48fbe735 18005 has_byte_size = 1;
fa4028e9 18006 break;
ff908ebf 18007 case DW_AT_const_value:
48fbe735 18008 has_const_value = 1;
ff908ebf 18009 break;
68511cec
CES
18010 case DW_AT_calling_convention:
18011 /* DWARF doesn't provide a way to identify a program's source-level
18012 entry point. DW_AT_calling_convention attributes are only meant
18013 to describe functions' calling conventions.
18014
18015 However, because it's a necessary piece of information in
0c1b455e
TT
18016 Fortran, and before DWARF 4 DW_CC_program was the only
18017 piece of debugging information whose definition refers to
18018 a 'main program' at all, several compilers marked Fortran
18019 main programs with DW_CC_program --- even when those
18020 functions use the standard calling conventions.
18021
18022 Although DWARF now specifies a way to provide this
18023 information, we support this practice for backward
18024 compatibility. */
68511cec 18025 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18026 && cu->language == language_fortran)
48fbe735 18027 main_subprogram = 1;
68511cec 18028 break;
481860b3
GB
18029 case DW_AT_inline:
18030 if (DW_UNSND (&attr) == DW_INL_inlined
18031 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18032 may_be_inlined = 1;
481860b3 18033 break;
95554aad
TT
18034
18035 case DW_AT_import:
48fbe735 18036 if (tag == DW_TAG_imported_unit)
36586728 18037 {
0826b30a 18038 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18039 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18040 || cu->per_cu->is_dwz);
18041 }
95554aad
TT
18042 break;
18043
0c1b455e 18044 case DW_AT_main_subprogram:
48fbe735 18045 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18046 break;
18047
05caa1d2
TT
18048 case DW_AT_ranges:
18049 {
18050 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18051 but that requires a full DIE, so instead we just
18052 reimplement it. */
18053 int need_ranges_base = tag != DW_TAG_compile_unit;
18054 unsigned int ranges_offset = (DW_UNSND (&attr)
18055 + (need_ranges_base
18056 ? cu->ranges_base
18057 : 0));
18058
18059 /* Value of the DW_AT_ranges attribute is the offset in the
18060 .debug_ranges section. */
18061 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18062 nullptr))
18063 has_pc_info = 1;
18064 }
18065 break;
18066
c906108c
SS
18067 default:
18068 break;
18069 }
18070 }
18071
10d06d82
TT
18072 /* For Ada, if both the name and the linkage name appear, we prefer
18073 the latter. This lets "catch exception" work better, regardless
18074 of the order in which the name and linkage name were emitted.
18075 Really, though, this is just a workaround for the fact that gdb
18076 doesn't store both the name and the linkage name. */
18077 if (cu->language == language_ada && linkage_name != nullptr)
18078 name = linkage_name;
18079
91da1414 18080 if (high_pc_relative)
48fbe735 18081 highpc += lowpc;
91da1414 18082
9373cf26
JK
18083 if (has_low_pc_attr && has_high_pc_attr)
18084 {
18085 /* When using the GNU linker, .gnu.linkonce. sections are used to
18086 eliminate duplicate copies of functions and vtables and such.
18087 The linker will arbitrarily choose one and discard the others.
18088 The AT_*_pc values for such functions refer to local labels in
18089 these sections. If the section from that file was discarded, the
18090 labels are not in the output, so the relocs get a value of 0.
18091 If this is a discarded function, mark the pc bounds as invalid,
18092 so that GDB will ignore it. */
48fbe735 18093 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18094 {
48fbe735 18095 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18096 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18097
b98664d3 18098 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18099 "for DIE at %s [in module %s]"),
48fbe735
YQ
18100 paddress (gdbarch, lowpc),
18101 sect_offset_str (sect_off),
9d8780f0 18102 objfile_name (objfile));
9373cf26
JK
18103 }
18104 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18105 else if (lowpc >= highpc)
9373cf26 18106 {
48fbe735 18107 struct objfile *objfile = dwarf2_per_objfile->objfile;
bb5ed363 18108 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9373cf26 18109
b98664d3 18110 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18111 "for DIE at %s [in module %s]"),
48fbe735
YQ
18112 paddress (gdbarch, lowpc),
18113 paddress (gdbarch, highpc),
18114 sect_offset_str (sect_off),
9c541725 18115 objfile_name (objfile));
9373cf26
JK
18116 }
18117 else
48fbe735 18118 has_pc_info = 1;
9373cf26 18119 }
85cbf3d3 18120
c906108c
SS
18121 return info_ptr;
18122}
18123
72bf9492
DJ
18124/* Find a cached partial DIE at OFFSET in CU. */
18125
d590ff25
YQ
18126struct partial_die_info *
18127dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18128{
18129 struct partial_die_info *lookup_die = NULL;
6f06d47b 18130 struct partial_die_info part_die (sect_off);
72bf9492 18131
9a3c8263 18132 lookup_die = ((struct partial_die_info *)
d590ff25 18133 htab_find_with_hash (partial_dies, &part_die,
9c541725 18134 to_underlying (sect_off)));
72bf9492 18135
72bf9492
DJ
18136 return lookup_die;
18137}
18138
348e048f
DE
18139/* Find a partial DIE at OFFSET, which may or may not be in CU,
18140 except in the case of .debug_types DIEs which do not reference
18141 outside their CU (they do however referencing other types via
55f1336d 18142 DW_FORM_ref_sig8). */
72bf9492 18143
122cf0f2 18144static const struct cu_partial_die_info
9c541725 18145find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18146{
518817b3
SM
18147 struct dwarf2_per_objfile *dwarf2_per_objfile
18148 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18149 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18150 struct dwarf2_per_cu_data *per_cu = NULL;
18151 struct partial_die_info *pd = NULL;
72bf9492 18152
36586728 18153 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18154 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18155 {
d590ff25 18156 pd = cu->find_partial_die (sect_off);
5afb4e99 18157 if (pd != NULL)
fb816e8b 18158 return { cu, pd };
0d99eb77
DE
18159 /* We missed recording what we needed.
18160 Load all dies and try again. */
18161 per_cu = cu->per_cu;
5afb4e99 18162 }
0d99eb77
DE
18163 else
18164 {
18165 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18166 if (cu->per_cu->is_debug_types)
0d99eb77 18167 {
9d8780f0
SM
18168 error (_("Dwarf Error: Type Unit at offset %s contains"
18169 " external reference to offset %s [in module %s].\n"),
18170 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18171 bfd_get_filename (objfile->obfd));
18172 }
9c541725 18173 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18174 dwarf2_per_objfile);
72bf9492 18175
0d99eb77
DE
18176 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18177 load_partial_comp_unit (per_cu);
ae038cb0 18178
0d99eb77 18179 per_cu->cu->last_used = 0;
d590ff25 18180 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18181 }
5afb4e99 18182
dee91e82
DE
18183 /* If we didn't find it, and not all dies have been loaded,
18184 load them all and try again. */
18185
5afb4e99
DJ
18186 if (pd == NULL && per_cu->load_all_dies == 0)
18187 {
5afb4e99 18188 per_cu->load_all_dies = 1;
fd820528
DE
18189
18190 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18191 THIS_CU->cu may already be in use. So we can't just free it and
18192 replace its DIEs with the ones we read in. Instead, we leave those
18193 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18194 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18195 set. */
dee91e82 18196 load_partial_comp_unit (per_cu);
5afb4e99 18197
d590ff25 18198 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18199 }
18200
18201 if (pd == NULL)
18202 internal_error (__FILE__, __LINE__,
9d8780f0 18203 _("could not find partial DIE %s "
3e43a32a 18204 "in cache [from module %s]\n"),
9d8780f0 18205 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18206 return { per_cu->cu, pd };
72bf9492
DJ
18207}
18208
abc72ce4
DE
18209/* See if we can figure out if the class lives in a namespace. We do
18210 this by looking for a member function; its demangled name will
18211 contain namespace info, if there is any. */
18212
18213static void
18214guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18215 struct dwarf2_cu *cu)
18216{
18217 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18218 what template types look like, because the demangler
18219 frequently doesn't give the same name as the debug info. We
18220 could fix this by only using the demangled name to get the
18221 prefix (but see comment in read_structure_type). */
18222
18223 struct partial_die_info *real_pdi;
18224 struct partial_die_info *child_pdi;
18225
18226 /* If this DIE (this DIE's specification, if any) has a parent, then
18227 we should not do this. We'll prepend the parent's fully qualified
18228 name when we create the partial symbol. */
18229
18230 real_pdi = struct_pdi;
18231 while (real_pdi->has_specification)
fb816e8b 18232 {
122cf0f2
AB
18233 auto res = find_partial_die (real_pdi->spec_offset,
18234 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18235 real_pdi = res.pdi;
18236 cu = res.cu;
18237 }
abc72ce4
DE
18238
18239 if (real_pdi->die_parent != NULL)
18240 return;
18241
18242 for (child_pdi = struct_pdi->die_child;
18243 child_pdi != NULL;
18244 child_pdi = child_pdi->die_sibling)
18245 {
18246 if (child_pdi->tag == DW_TAG_subprogram
18247 && child_pdi->linkage_name != NULL)
18248 {
43816ebc
TT
18249 gdb::unique_xmalloc_ptr<char> actual_class_name
18250 (language_class_name_from_physname (cu->language_defn,
18251 child_pdi->linkage_name));
abc72ce4
DE
18252 if (actual_class_name != NULL)
18253 {
518817b3 18254 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18255 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18256 }
18257 break;
18258 }
18259 }
18260}
18261
25c11aca
TV
18262/* Return true if a DIE with TAG may have the DW_AT_const_value
18263 attribute. */
18264
18265static bool
18266can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18267{
18268 switch (tag)
18269 {
18270 case DW_TAG_constant:
18271 case DW_TAG_enumerator:
18272 case DW_TAG_formal_parameter:
18273 case DW_TAG_template_value_param:
18274 case DW_TAG_variable:
18275 return true;
18276 }
18277
18278 return false;
18279}
18280
52356b79
YQ
18281void
18282partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18283{
abc72ce4
DE
18284 /* Once we've fixed up a die, there's no point in doing so again.
18285 This also avoids a memory leak if we were to call
18286 guess_partial_die_structure_name multiple times. */
52356b79 18287 if (fixup_called)
abc72ce4
DE
18288 return;
18289
72bf9492
DJ
18290 /* If we found a reference attribute and the DIE has no name, try
18291 to find a name in the referred to DIE. */
18292
52356b79 18293 if (name == NULL && has_specification)
72bf9492
DJ
18294 {
18295 struct partial_die_info *spec_die;
72bf9492 18296
122cf0f2 18297 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18298 spec_die = res.pdi;
18299 cu = res.cu;
72bf9492 18300
52356b79 18301 spec_die->fixup (cu);
72bf9492
DJ
18302
18303 if (spec_die->name)
18304 {
52356b79 18305 name = spec_die->name;
72bf9492
DJ
18306
18307 /* Copy DW_AT_external attribute if it is set. */
18308 if (spec_die->is_external)
52356b79 18309 is_external = spec_die->is_external;
72bf9492
DJ
18310 }
18311 }
18312
25c11aca
TV
18313 if (!has_const_value && has_specification
18314 && can_have_DW_AT_const_value_p (tag))
18315 {
18316 struct partial_die_info *spec_die;
18317
18318 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18319 spec_die = res.pdi;
18320 cu = res.cu;
18321
18322 spec_die->fixup (cu);
18323
18324 if (spec_die->has_const_value)
18325 {
18326 /* Copy DW_AT_const_value attribute if it is set. */
18327 has_const_value = spec_die->has_const_value;
18328 }
18329 }
18330
72bf9492 18331 /* Set default names for some unnamed DIEs. */
72bf9492 18332
52356b79
YQ
18333 if (name == NULL && tag == DW_TAG_namespace)
18334 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18335
abc72ce4
DE
18336 /* If there is no parent die to provide a namespace, and there are
18337 children, see if we can determine the namespace from their linkage
122d1940 18338 name. */
abc72ce4 18339 if (cu->language == language_cplus
fd5866f6 18340 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18341 && die_parent == NULL
18342 && has_children
18343 && (tag == DW_TAG_class_type
18344 || tag == DW_TAG_structure_type
18345 || tag == DW_TAG_union_type))
18346 guess_partial_die_structure_name (this, cu);
abc72ce4 18347
53832f31
TT
18348 /* GCC might emit a nameless struct or union that has a linkage
18349 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18350 if (name == NULL
18351 && (tag == DW_TAG_class_type
18352 || tag == DW_TAG_interface_type
18353 || tag == DW_TAG_structure_type
18354 || tag == DW_TAG_union_type)
18355 && linkage_name != NULL)
53832f31 18356 {
43816ebc
TT
18357 gdb::unique_xmalloc_ptr<char> demangled
18358 (gdb_demangle (linkage_name, DMGL_TYPES));
18359 if (demangled != nullptr)
53832f31 18360 {
96408a79
SA
18361 const char *base;
18362
18363 /* Strip any leading namespaces/classes, keep only the base name.
18364 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18365 base = strrchr (demangled.get (), ':');
18366 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18367 base++;
18368 else
43816ebc 18369 base = demangled.get ();
96408a79 18370
518817b3 18371 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18372 name = objfile->intern (base);
53832f31
TT
18373 }
18374 }
18375
52356b79 18376 fixup_called = 1;
72bf9492
DJ
18377}
18378
18a8505e
AT
18379/* Process the attributes that had to be skipped in the first round. These
18380 attributes are the ones that need str_offsets_base or addr_base attributes.
18381 They could not have been processed in the first round, because at the time
18382 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18383static void
18384read_attribute_reprocess (const struct die_reader_specs *reader,
18385 struct attribute *attr)
18a8505e
AT
18386{
18387 struct dwarf2_cu *cu = reader->cu;
18388 switch (attr->form)
18389 {
18390 case DW_FORM_addrx:
18391 case DW_FORM_GNU_addr_index:
18392 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18393 break;
18394 case DW_FORM_strx:
18395 case DW_FORM_strx1:
18396 case DW_FORM_strx2:
18397 case DW_FORM_strx3:
18398 case DW_FORM_strx4:
18399 case DW_FORM_GNU_str_index:
18400 {
18401 unsigned int str_index = DW_UNSND (attr);
18402 if (reader->dwo_file != NULL)
18403 {
18404 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18405 DW_STRING_IS_CANONICAL (attr) = 0;
18406 }
18407 else
18408 {
18409 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18410 DW_STRING_IS_CANONICAL (attr) = 0;
18411 }
18412 break;
18413 }
18414 default:
18415 gdb_assert_not_reached (_("Unexpected DWARF form."));
18416 }
18417}
18418
a8329558 18419/* Read an attribute value described by an attribute form. */
c906108c 18420
d521ce57 18421static const gdb_byte *
dee91e82
DE
18422read_attribute_value (const struct die_reader_specs *reader,
18423 struct attribute *attr, unsigned form,
18a8505e
AT
18424 LONGEST implicit_const, const gdb_byte *info_ptr,
18425 bool *need_reprocess)
c906108c 18426{
dee91e82 18427 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18428 struct dwarf2_per_objfile *dwarf2_per_objfile
18429 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18430 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 18431 struct gdbarch *gdbarch = get_objfile_arch (objfile);
dee91e82 18432 bfd *abfd = reader->abfd;
e7c27a73 18433 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18434 unsigned int bytes_read;
18435 struct dwarf_block *blk;
18a8505e 18436 *need_reprocess = false;
c906108c 18437
aead7601 18438 attr->form = (enum dwarf_form) form;
a8329558 18439 switch (form)
c906108c 18440 {
c906108c 18441 case DW_FORM_ref_addr:
ae411497 18442 if (cu->header.version == 2)
c8a7a66f
TT
18443 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18444 &bytes_read);
ae411497 18445 else
8266302d
TT
18446 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18447 &bytes_read);
ae411497
TT
18448 info_ptr += bytes_read;
18449 break;
36586728 18450 case DW_FORM_GNU_ref_alt:
8266302d 18451 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18452 info_ptr += bytes_read;
18453 break;
ae411497 18454 case DW_FORM_addr:
c8a7a66f 18455 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
3e29f34a 18456 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
107d2387 18457 info_ptr += bytes_read;
c906108c
SS
18458 break;
18459 case DW_FORM_block2:
7b5a2f43 18460 blk = dwarf_alloc_block (cu);
c906108c
SS
18461 blk->size = read_2_bytes (abfd, info_ptr);
18462 info_ptr += 2;
18463 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18464 info_ptr += blk->size;
18465 DW_BLOCK (attr) = blk;
18466 break;
18467 case DW_FORM_block4:
7b5a2f43 18468 blk = dwarf_alloc_block (cu);
c906108c
SS
18469 blk->size = read_4_bytes (abfd, info_ptr);
18470 info_ptr += 4;
18471 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18472 info_ptr += blk->size;
18473 DW_BLOCK (attr) = blk;
18474 break;
18475 case DW_FORM_data2:
18476 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18477 info_ptr += 2;
18478 break;
18479 case DW_FORM_data4:
18480 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18481 info_ptr += 4;
18482 break;
18483 case DW_FORM_data8:
18484 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18485 info_ptr += 8;
18486 break;
0224619f
JK
18487 case DW_FORM_data16:
18488 blk = dwarf_alloc_block (cu);
18489 blk->size = 16;
18490 blk->data = read_n_bytes (abfd, info_ptr, 16);
18491 info_ptr += 16;
18492 DW_BLOCK (attr) = blk;
18493 break;
2dc7f7b3 18494 case DW_FORM_sec_offset:
8266302d 18495 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18496 info_ptr += bytes_read;
18497 break;
c906108c 18498 case DW_FORM_string:
9b1c24c8 18499 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18500 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18501 info_ptr += bytes_read;
18502 break;
4bdf3d34 18503 case DW_FORM_strp:
36586728
TT
18504 if (!cu->per_cu->is_dwz)
18505 {
ed2dc618
SM
18506 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18507 abfd, info_ptr, cu_header,
36586728
TT
18508 &bytes_read);
18509 DW_STRING_IS_CANONICAL (attr) = 0;
18510 info_ptr += bytes_read;
18511 break;
18512 }
18513 /* FALLTHROUGH */
43988095
JK
18514 case DW_FORM_line_strp:
18515 if (!cu->per_cu->is_dwz)
18516 {
86c0bb4c
TT
18517 DW_STRING (attr)
18518 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18519 &bytes_read);
43988095
JK
18520 DW_STRING_IS_CANONICAL (attr) = 0;
18521 info_ptr += bytes_read;
18522 break;
18523 }
18524 /* FALLTHROUGH */
36586728
TT
18525 case DW_FORM_GNU_strp_alt:
18526 {
ed2dc618 18527 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18528 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18529 &bytes_read);
36586728 18530
0314b390 18531 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18532 DW_STRING_IS_CANONICAL (attr) = 0;
18533 info_ptr += bytes_read;
18534 }
4bdf3d34 18535 break;
2dc7f7b3 18536 case DW_FORM_exprloc:
c906108c 18537 case DW_FORM_block:
7b5a2f43 18538 blk = dwarf_alloc_block (cu);
c906108c
SS
18539 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18540 info_ptr += bytes_read;
18541 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18542 info_ptr += blk->size;
18543 DW_BLOCK (attr) = blk;
18544 break;
18545 case DW_FORM_block1:
7b5a2f43 18546 blk = dwarf_alloc_block (cu);
c906108c
SS
18547 blk->size = read_1_byte (abfd, info_ptr);
18548 info_ptr += 1;
18549 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18550 info_ptr += blk->size;
18551 DW_BLOCK (attr) = blk;
18552 break;
18553 case DW_FORM_data1:
18554 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18555 info_ptr += 1;
18556 break;
18557 case DW_FORM_flag:
18558 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
18559 info_ptr += 1;
18560 break;
2dc7f7b3
TT
18561 case DW_FORM_flag_present:
18562 DW_UNSND (attr) = 1;
18563 break;
c906108c
SS
18564 case DW_FORM_sdata:
18565 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18566 info_ptr += bytes_read;
18567 break;
18568 case DW_FORM_udata:
18a8505e 18569 case DW_FORM_rnglistx:
c906108c
SS
18570 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18571 info_ptr += bytes_read;
18572 break;
18573 case DW_FORM_ref1:
9c541725 18574 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18575 + read_1_byte (abfd, info_ptr));
c906108c
SS
18576 info_ptr += 1;
18577 break;
18578 case DW_FORM_ref2:
9c541725 18579 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18580 + read_2_bytes (abfd, info_ptr));
c906108c
SS
18581 info_ptr += 2;
18582 break;
18583 case DW_FORM_ref4:
9c541725 18584 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18585 + read_4_bytes (abfd, info_ptr));
c906108c
SS
18586 info_ptr += 4;
18587 break;
613e1657 18588 case DW_FORM_ref8:
9c541725 18589 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18590 + read_8_bytes (abfd, info_ptr));
613e1657
KB
18591 info_ptr += 8;
18592 break;
55f1336d 18593 case DW_FORM_ref_sig8:
ac9ec31b 18594 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
18595 info_ptr += 8;
18596 break;
c906108c 18597 case DW_FORM_ref_udata:
9c541725 18598 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 18599 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
18600 info_ptr += bytes_read;
18601 break;
c906108c 18602 case DW_FORM_indirect:
a8329558
KW
18603 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18604 info_ptr += bytes_read;
43988095
JK
18605 if (form == DW_FORM_implicit_const)
18606 {
18607 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
18608 info_ptr += bytes_read;
18609 }
18610 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 18611 info_ptr, need_reprocess);
43988095
JK
18612 break;
18613 case DW_FORM_implicit_const:
18614 DW_SND (attr) = implicit_const;
a8329558 18615 break;
336d760d 18616 case DW_FORM_addrx:
3019eac3 18617 case DW_FORM_GNU_addr_index:
18a8505e
AT
18618 *need_reprocess = true;
18619 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
18620 info_ptr += bytes_read;
18621 break;
cf532bd1 18622 case DW_FORM_strx:
15f18d14
AT
18623 case DW_FORM_strx1:
18624 case DW_FORM_strx2:
18625 case DW_FORM_strx3:
18626 case DW_FORM_strx4:
3019eac3 18627 case DW_FORM_GNU_str_index:
3019eac3 18628 {
15f18d14
AT
18629 ULONGEST str_index;
18630 if (form == DW_FORM_strx1)
18631 {
18632 str_index = read_1_byte (abfd, info_ptr);
18633 info_ptr += 1;
18634 }
18635 else if (form == DW_FORM_strx2)
18636 {
18637 str_index = read_2_bytes (abfd, info_ptr);
18638 info_ptr += 2;
18639 }
18640 else if (form == DW_FORM_strx3)
18641 {
18642 str_index = read_3_bytes (abfd, info_ptr);
18643 info_ptr += 3;
18644 }
18645 else if (form == DW_FORM_strx4)
18646 {
18647 str_index = read_4_bytes (abfd, info_ptr);
18648 info_ptr += 4;
18649 }
18650 else
18651 {
18652 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18653 info_ptr += bytes_read;
18654 }
18a8505e
AT
18655 *need_reprocess = true;
18656 DW_UNSND (attr) = str_index;
18657 }
3019eac3 18658 break;
c906108c 18659 default:
8a3fe4f8 18660 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
18661 dwarf_form_name (form),
18662 bfd_get_filename (abfd));
c906108c 18663 }
28e94949 18664
36586728 18665 /* Super hack. */
cd6c91b4 18666 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
18667 attr->form = DW_FORM_GNU_ref_alt;
18668
28e94949
JB
18669 /* We have seen instances where the compiler tried to emit a byte
18670 size attribute of -1 which ended up being encoded as an unsigned
18671 0xffffffff. Although 0xffffffff is technically a valid size value,
18672 an object of this size seems pretty unlikely so we can relatively
18673 safely treat these cases as if the size attribute was invalid and
18674 treat them as zero by default. */
18675 if (attr->name == DW_AT_byte_size
18676 && form == DW_FORM_data4
18677 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
18678 {
18679 complaint
b98664d3 18680 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 18681 hex_string (DW_UNSND (attr)));
01c66ae6
JB
18682 DW_UNSND (attr) = 0;
18683 }
28e94949 18684
c906108c
SS
18685 return info_ptr;
18686}
18687
a8329558
KW
18688/* Read an attribute described by an abbreviated attribute. */
18689
d521ce57 18690static const gdb_byte *
dee91e82
DE
18691read_attribute (const struct die_reader_specs *reader,
18692 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 18693 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
18694{
18695 attr->name = abbrev->name;
43988095 18696 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
18697 abbrev->implicit_const, info_ptr,
18698 need_reprocess);
a8329558
KW
18699}
18700
43988095
JK
18701/* Return pointer to string at .debug_str offset STR_OFFSET. */
18702
18703static const char *
ed2dc618 18704read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 18705 LONGEST str_offset)
43988095 18706{
4f44ae6c
TT
18707 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
18708 str_offset, "DW_FORM_strp");
c906108c
SS
18709}
18710
43988095
JK
18711/* Return pointer to string at .debug_str offset as read from BUF.
18712 BUF is assumed to be in a compilation unit described by CU_HEADER.
18713 Return *BYTES_READ_PTR count of bytes read from BUF. */
18714
d521ce57 18715static const char *
ed2dc618
SM
18716read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
18717 const gdb_byte *buf,
cf2c3c16
TT
18718 const struct comp_unit_head *cu_header,
18719 unsigned int *bytes_read_ptr)
18720{
8266302d 18721 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 18722
4f44ae6c 18723 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
18724}
18725
86c0bb4c 18726/* See read.h. */
43988095 18727
86c0bb4c
TT
18728const char *
18729dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
18730 const struct comp_unit_head *cu_header,
18731 unsigned int *bytes_read_ptr)
18732{
86c0bb4c 18733 bfd *abfd = objfile->obfd;
8266302d 18734 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 18735
86c0bb4c 18736 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
18737}
18738
3019eac3 18739/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 18740 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
18741 ADDR_SIZE is the size of addresses from the CU header. */
18742
18743static CORE_ADDR
ed2dc618 18744read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
18745 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
18746 int addr_size)
3019eac3
DE
18747{
18748 struct objfile *objfile = dwarf2_per_objfile->objfile;
18749 bfd *abfd = objfile->obfd;
18750 const gdb_byte *info_ptr;
18a8505e 18751 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 18752
96b79293 18753 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
18754 if (dwarf2_per_objfile->addr.buffer == NULL)
18755 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 18756 objfile_name (objfile));
18a8505e
AT
18757 if (addr_base_or_zero + addr_index * addr_size
18758 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
18759 error (_("DW_FORM_addr_index pointing outside of "
18760 ".debug_addr section [in module %s]"),
4262abfb 18761 objfile_name (objfile));
3019eac3 18762 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 18763 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
18764 if (addr_size == 4)
18765 return bfd_get_32 (abfd, info_ptr);
18766 else
18767 return bfd_get_64 (abfd, info_ptr);
18768}
18769
18770/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
18771
18772static CORE_ADDR
18773read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
18774{
518817b3
SM
18775 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
18776 cu->addr_base, cu->header.addr_size);
3019eac3
DE
18777}
18778
18779/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
18780
18781static CORE_ADDR
d521ce57 18782read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
18783 unsigned int *bytes_read)
18784{
518817b3 18785 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
18786 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
18787
18788 return read_addr_index (cu, addr_index);
18789}
18790
450a1bfc 18791/* See read.h. */
3019eac3
DE
18792
18793CORE_ADDR
450a1bfc 18794dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 18795{
ed2dc618 18796 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 18797 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 18798 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
18799 int addr_size;
18800
3019eac3
DE
18801 /* We need addr_base and addr_size.
18802 If we don't have PER_CU->cu, we have to get it.
18803 Nasty, but the alternative is storing the needed info in PER_CU,
18804 which at this point doesn't seem justified: it's not clear how frequently
18805 it would get used and it would increase the size of every PER_CU.
18806 Entry points like dwarf2_per_cu_addr_size do a similar thing
18807 so we're not in uncharted territory here.
18808 Alas we need to be a bit more complicated as addr_base is contained
18809 in the DIE.
18810
18811 We don't need to read the entire CU(/TU).
18812 We just need the header and top level die.
a1b64ce1 18813
3019eac3 18814 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 18815 For now we skip this optimization. */
3019eac3
DE
18816
18817 if (cu != NULL)
18818 {
18819 addr_base = cu->addr_base;
18820 addr_size = cu->header.addr_size;
18821 }
18822 else
18823 {
6751ebae 18824 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
18825 addr_base = reader.cu->addr_base;
18826 addr_size = reader.cu->header.addr_size;
3019eac3
DE
18827 }
18828
ed2dc618
SM
18829 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
18830 addr_size);
3019eac3
DE
18831}
18832
18a8505e
AT
18833/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
18834 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
18835 DWO file. */
3019eac3 18836
d521ce57 18837static const char *
18a8505e
AT
18838read_str_index (struct dwarf2_cu *cu,
18839 struct dwarf2_section_info *str_section,
18840 struct dwarf2_section_info *str_offsets_section,
18841 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 18842{
518817b3
SM
18843 struct dwarf2_per_objfile *dwarf2_per_objfile
18844 = cu->per_cu->dwarf2_per_objfile;
3019eac3 18845 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 18846 const char *objf_name = objfile_name (objfile);
3019eac3 18847 bfd *abfd = objfile->obfd;
d521ce57 18848 const gdb_byte *info_ptr;
3019eac3 18849 ULONGEST str_offset;
cf532bd1 18850 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 18851
96b79293
TT
18852 str_section->read (objfile);
18853 str_offsets_section->read (objfile);
73869dc2 18854 if (str_section->buffer == NULL)
18a8505e 18855 error (_("%s used without %s section"
9d8780f0 18856 " in CU at offset %s [in module %s]"),
96b79293 18857 form_name, str_section->get_name (),
18a8505e 18858 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18859 if (str_offsets_section->buffer == NULL)
18a8505e 18860 error (_("%s used without %s section"
9d8780f0 18861 " in CU at offset %s [in module %s]"),
96b79293 18862 form_name, str_section->get_name (),
18a8505e 18863 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18864 info_ptr = (str_offsets_section->buffer
18a8505e 18865 + str_offsets_base
3019eac3
DE
18866 + str_index * cu->header.offset_size);
18867 if (cu->header.offset_size == 4)
18868 str_offset = bfd_get_32 (abfd, info_ptr);
18869 else
18870 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 18871 if (str_offset >= str_section->size)
57d63ce2 18872 error (_("Offset from %s pointing outside of"
9d8780f0
SM
18873 " .debug_str.dwo section in CU at offset %s [in module %s]"),
18874 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 18875 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
18876}
18877
18a8505e
AT
18878/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
18879
18880static const char *
18881read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
18882{
18883 ULONGEST str_offsets_base = reader->cu->header.version >= 5
18884 ? reader->cu->header.addr_size : 0;
18885 return read_str_index (reader->cu,
18886 &reader->dwo_file->sections.str,
18887 &reader->dwo_file->sections.str_offsets,
18888 str_offsets_base, str_index);
18889}
18890
18891/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
18892
18893static const char *
18894read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
18895{
18896 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
18897 const char *objf_name = objfile_name (objfile);
18898 static const char form_name[] = "DW_FORM_GNU_str_index";
18899 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
18900
18901 if (!cu->str_offsets_base.has_value ())
18902 error (_("%s used in Fission stub without %s"
18903 " in CU at offset 0x%lx [in module %s]"),
18904 form_name, str_offsets_attr_name,
18905 (long) cu->header.offset_size, objf_name);
18906
18907 return read_str_index (cu,
18908 &cu->per_cu->dwarf2_per_objfile->str,
18909 &cu->per_cu->dwarf2_per_objfile->str_offsets,
18910 *cu->str_offsets_base, str_index);
18911}
18912
3019eac3
DE
18913/* Return the length of an LEB128 number in BUF. */
18914
18915static int
18916leb128_size (const gdb_byte *buf)
18917{
18918 const gdb_byte *begin = buf;
18919 gdb_byte byte;
18920
18921 while (1)
18922 {
18923 byte = *buf++;
18924 if ((byte & 128) == 0)
18925 return buf - begin;
18926 }
18927}
18928
c906108c 18929static void
e142c38c 18930set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
18931{
18932 switch (lang)
18933 {
18934 case DW_LANG_C89:
76bee0cc 18935 case DW_LANG_C99:
0cfd832f 18936 case DW_LANG_C11:
c906108c 18937 case DW_LANG_C:
d1be3247 18938 case DW_LANG_UPC:
e142c38c 18939 cu->language = language_c;
c906108c 18940 break;
9c37b5ae 18941 case DW_LANG_Java:
c906108c 18942 case DW_LANG_C_plus_plus:
0cfd832f
MW
18943 case DW_LANG_C_plus_plus_11:
18944 case DW_LANG_C_plus_plus_14:
e142c38c 18945 cu->language = language_cplus;
c906108c 18946 break;
6aecb9c2
JB
18947 case DW_LANG_D:
18948 cu->language = language_d;
18949 break;
c906108c
SS
18950 case DW_LANG_Fortran77:
18951 case DW_LANG_Fortran90:
b21b22e0 18952 case DW_LANG_Fortran95:
f7de9aab
MW
18953 case DW_LANG_Fortran03:
18954 case DW_LANG_Fortran08:
e142c38c 18955 cu->language = language_fortran;
c906108c 18956 break;
a766d390
DE
18957 case DW_LANG_Go:
18958 cu->language = language_go;
18959 break;
c906108c 18960 case DW_LANG_Mips_Assembler:
e142c38c 18961 cu->language = language_asm;
c906108c
SS
18962 break;
18963 case DW_LANG_Ada83:
8aaf0b47 18964 case DW_LANG_Ada95:
bc5f45f8
JB
18965 cu->language = language_ada;
18966 break;
72019c9c
GM
18967 case DW_LANG_Modula2:
18968 cu->language = language_m2;
18969 break;
fe8e67fd
PM
18970 case DW_LANG_Pascal83:
18971 cu->language = language_pascal;
18972 break;
22566fbd
DJ
18973 case DW_LANG_ObjC:
18974 cu->language = language_objc;
18975 break;
c44af4eb
TT
18976 case DW_LANG_Rust:
18977 case DW_LANG_Rust_old:
18978 cu->language = language_rust;
18979 break;
c906108c
SS
18980 case DW_LANG_Cobol74:
18981 case DW_LANG_Cobol85:
c906108c 18982 default:
e142c38c 18983 cu->language = language_minimal;
c906108c
SS
18984 break;
18985 }
e142c38c 18986 cu->language_defn = language_def (cu->language);
c906108c
SS
18987}
18988
18989/* Return the named attribute or NULL if not there. */
18990
18991static struct attribute *
e142c38c 18992dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 18993{
a48e046c 18994 for (;;)
c906108c 18995 {
a48e046c
TT
18996 unsigned int i;
18997 struct attribute *spec = NULL;
18998
18999 for (i = 0; i < die->num_attrs; ++i)
19000 {
19001 if (die->attrs[i].name == name)
19002 return &die->attrs[i];
19003 if (die->attrs[i].name == DW_AT_specification
19004 || die->attrs[i].name == DW_AT_abstract_origin)
19005 spec = &die->attrs[i];
19006 }
19007
19008 if (!spec)
19009 break;
c906108c 19010
f2f0e013 19011 die = follow_die_ref (die, spec, &cu);
f2f0e013 19012 }
c5aa993b 19013
c906108c
SS
19014 return NULL;
19015}
19016
7d45c7c3
KB
19017/* Return the string associated with a string-typed attribute, or NULL if it
19018 is either not found or is of an incorrect type. */
19019
19020static const char *
19021dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19022{
19023 struct attribute *attr;
19024 const char *str = NULL;
19025
19026 attr = dwarf2_attr (die, name, cu);
19027
19028 if (attr != NULL)
19029 {
43988095 19030 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19031 || attr->form == DW_FORM_string
cf532bd1 19032 || attr->form == DW_FORM_strx
8fe0f950
AT
19033 || attr->form == DW_FORM_strx1
19034 || attr->form == DW_FORM_strx2
19035 || attr->form == DW_FORM_strx3
19036 || attr->form == DW_FORM_strx4
b3340438 19037 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19038 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19039 str = DW_STRING (attr);
19040 else
b98664d3 19041 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19042 "DIE at %s in module %s"),
19043 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19044 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19045 }
19046
19047 return str;
19048}
19049
a084a2a6 19050/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19051 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19052static const char *
19053dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19054{
19055 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19056 if (dwo_name == nullptr)
19057 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19058 return dwo_name;
19059}
19060
05cf31d1
JB
19061/* Return non-zero iff the attribute NAME is defined for the given DIE,
19062 and holds a non-zero value. This function should only be used for
2dc7f7b3 19063 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19064
19065static int
19066dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19067{
19068 struct attribute *attr = dwarf2_attr (die, name, cu);
19069
19070 return (attr && DW_UNSND (attr));
19071}
19072
3ca72b44 19073static int
e142c38c 19074die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19075{
05cf31d1
JB
19076 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19077 which value is non-zero. However, we have to be careful with
19078 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19079 (via dwarf2_flag_true_p) follows this attribute. So we may
19080 end up accidently finding a declaration attribute that belongs
19081 to a different DIE referenced by the specification attribute,
19082 even though the given DIE does not have a declaration attribute. */
19083 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19084 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19085}
19086
63d06c5c 19087/* Return the die giving the specification for DIE, if there is
f2f0e013 19088 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19089 containing the return value on output. If there is no
19090 specification, but there is an abstract origin, that is
19091 returned. */
63d06c5c
DC
19092
19093static struct die_info *
f2f0e013 19094die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19095{
f2f0e013
DJ
19096 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19097 *spec_cu);
63d06c5c 19098
edb3359d
DJ
19099 if (spec_attr == NULL)
19100 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19101
63d06c5c
DC
19102 if (spec_attr == NULL)
19103 return NULL;
19104 else
f2f0e013 19105 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19106}
c906108c 19107
527f3840
JK
19108/* Stub for free_line_header to match void * callback types. */
19109
19110static void
19111free_line_header_voidp (void *arg)
19112{
9a3c8263 19113 struct line_header *lh = (struct line_header *) arg;
527f3840 19114
fff8551c 19115 delete lh;
527f3840
JK
19116}
19117
83769d0b 19118/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19119
19120static struct dwarf2_section_info *
19121get_debug_line_section (struct dwarf2_cu *cu)
19122{
19123 struct dwarf2_section_info *section;
518817b3
SM
19124 struct dwarf2_per_objfile *dwarf2_per_objfile
19125 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19126
19127 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19128 DWO file. */
19129 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19130 section = &cu->dwo_unit->dwo_file->sections.line;
19131 else if (cu->per_cu->is_dwz)
19132 {
ed2dc618 19133 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19134
19135 section = &dwz->line;
19136 }
19137 else
19138 section = &dwarf2_per_objfile->line;
19139
19140 return section;
19141}
19142
debd256d 19143/* Read the statement program header starting at OFFSET in
3019eac3 19144 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19145 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19146 Returns NULL if there is a problem reading the header, e.g., if it
19147 has a version we don't understand.
debd256d
JB
19148
19149 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19150 the returned object point into the dwarf line section buffer,
19151 and must not be freed. */
ae2de4f8 19152
fff8551c 19153static line_header_up
9c541725 19154dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19155{
3019eac3 19156 struct dwarf2_section_info *section;
518817b3
SM
19157 struct dwarf2_per_objfile *dwarf2_per_objfile
19158 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19159
36586728 19160 section = get_debug_line_section (cu);
96b79293 19161 section->read (dwarf2_per_objfile->objfile);
3019eac3 19162 if (section->buffer == NULL)
debd256d 19163 {
3019eac3 19164 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19165 complaint (_("missing .debug_line.dwo section"));
3019eac3 19166 else
b98664d3 19167 complaint (_("missing .debug_line section"));
debd256d
JB
19168 return 0;
19169 }
19170
0df7ad3a
TT
19171 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19172 dwarf2_per_objfile, section,
19173 &cu->header);
debd256d 19174}
c906108c 19175
c6da4cef 19176/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19177 Return the file name of the psymtab for the given file_entry.
c6da4cef 19178 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19179 If space for the result is malloc'd, *NAME_HOLDER will be set.
19180 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19181
d521ce57 19182static const char *
7ba99d21 19183psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19184 const dwarf2_psymtab *pst,
c89b44cd
TT
19185 const char *comp_dir,
19186 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19187{
d521ce57
TT
19188 const char *include_name = fe.name;
19189 const char *include_name_to_compare = include_name;
72b9f47f 19190 const char *pst_filename;
c6da4cef
DE
19191 int file_is_pst;
19192
8c43009f 19193 const char *dir_name = fe.include_dir (lh);
c6da4cef 19194
c89b44cd 19195 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19196 if (!IS_ABSOLUTE_PATH (include_name)
19197 && (dir_name != NULL || comp_dir != NULL))
19198 {
19199 /* Avoid creating a duplicate psymtab for PST.
19200 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19201 Before we do the comparison, however, we need to account
19202 for DIR_NAME and COMP_DIR.
19203 First prepend dir_name (if non-NULL). If we still don't
19204 have an absolute path prepend comp_dir (if non-NULL).
19205 However, the directory we record in the include-file's
19206 psymtab does not contain COMP_DIR (to match the
19207 corresponding symtab(s)).
19208
19209 Example:
19210
19211 bash$ cd /tmp
19212 bash$ gcc -g ./hello.c
19213 include_name = "hello.c"
19214 dir_name = "."
19215 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19216 DW_AT_name = "./hello.c"
19217
19218 */
c6da4cef
DE
19219
19220 if (dir_name != NULL)
19221 {
c89b44cd
TT
19222 name_holder->reset (concat (dir_name, SLASH_STRING,
19223 include_name, (char *) NULL));
19224 include_name = name_holder->get ();
c6da4cef 19225 include_name_to_compare = include_name;
c6da4cef
DE
19226 }
19227 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19228 {
c89b44cd
TT
19229 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19230 include_name, (char *) NULL));
19231 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19232 }
19233 }
19234
19235 pst_filename = pst->filename;
c89b44cd 19236 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19237 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19238 {
c89b44cd
TT
19239 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19240 pst_filename, (char *) NULL));
19241 pst_filename = copied_name.get ();
c6da4cef
DE
19242 }
19243
1e3fad37 19244 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19245
c6da4cef
DE
19246 if (file_is_pst)
19247 return NULL;
19248 return include_name;
19249}
19250
d9b3de22
DE
19251/* State machine to track the state of the line number program. */
19252
6f77053d 19253class lnp_state_machine
d9b3de22 19254{
6f77053d
PA
19255public:
19256 /* Initialize a machine state for the start of a line number
19257 program. */
804d2729
TT
19258 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19259 bool record_lines_p);
6f77053d 19260
8c43009f
PA
19261 file_entry *current_file ()
19262 {
19263 /* lh->file_names is 0-based, but the file name numbers in the
19264 statement program are 1-based. */
6f77053d
PA
19265 return m_line_header->file_name_at (m_file);
19266 }
19267
19268 /* Record the line in the state machine. END_SEQUENCE is true if
19269 we're processing the end of a sequence. */
19270 void record_line (bool end_sequence);
19271
7ab6656f
OJ
19272 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19273 nop-out rest of the lines in this sequence. */
6f77053d
PA
19274 void check_line_address (struct dwarf2_cu *cu,
19275 const gdb_byte *line_ptr,
7ab6656f 19276 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19277
19278 void handle_set_discriminator (unsigned int discriminator)
19279 {
19280 m_discriminator = discriminator;
19281 m_line_has_non_zero_discriminator |= discriminator != 0;
19282 }
19283
19284 /* Handle DW_LNE_set_address. */
19285 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19286 {
19287 m_op_index = 0;
19288 address += baseaddr;
19289 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19290 }
19291
19292 /* Handle DW_LNS_advance_pc. */
19293 void handle_advance_pc (CORE_ADDR adjust);
19294
19295 /* Handle a special opcode. */
19296 void handle_special_opcode (unsigned char op_code);
19297
19298 /* Handle DW_LNS_advance_line. */
19299 void handle_advance_line (int line_delta)
19300 {
19301 advance_line (line_delta);
19302 }
19303
19304 /* Handle DW_LNS_set_file. */
19305 void handle_set_file (file_name_index file);
19306
19307 /* Handle DW_LNS_negate_stmt. */
19308 void handle_negate_stmt ()
19309 {
19310 m_is_stmt = !m_is_stmt;
19311 }
19312
19313 /* Handle DW_LNS_const_add_pc. */
19314 void handle_const_add_pc ();
19315
19316 /* Handle DW_LNS_fixed_advance_pc. */
19317 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19318 {
19319 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19320 m_op_index = 0;
19321 }
19322
19323 /* Handle DW_LNS_copy. */
19324 void handle_copy ()
19325 {
19326 record_line (false);
19327 m_discriminator = 0;
19328 }
19329
19330 /* Handle DW_LNE_end_sequence. */
19331 void handle_end_sequence ()
19332 {
804d2729 19333 m_currently_recording_lines = true;
6f77053d
PA
19334 }
19335
19336private:
19337 /* Advance the line by LINE_DELTA. */
19338 void advance_line (int line_delta)
19339 {
19340 m_line += line_delta;
19341
19342 if (line_delta != 0)
19343 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19344 }
19345
804d2729
TT
19346 struct dwarf2_cu *m_cu;
19347
6f77053d
PA
19348 gdbarch *m_gdbarch;
19349
19350 /* True if we're recording lines.
19351 Otherwise we're building partial symtabs and are just interested in
19352 finding include files mentioned by the line number program. */
19353 bool m_record_lines_p;
19354
8c43009f 19355 /* The line number header. */
6f77053d 19356 line_header *m_line_header;
8c43009f 19357
6f77053d
PA
19358 /* These are part of the standard DWARF line number state machine,
19359 and initialized according to the DWARF spec. */
d9b3de22 19360
6f77053d 19361 unsigned char m_op_index = 0;
7ba99d21
AT
19362 /* The line table index of the current file. */
19363 file_name_index m_file = 1;
6f77053d
PA
19364 unsigned int m_line = 1;
19365
19366 /* These are initialized in the constructor. */
19367
19368 CORE_ADDR m_address;
19369 bool m_is_stmt;
19370 unsigned int m_discriminator;
d9b3de22
DE
19371
19372 /* Additional bits of state we need to track. */
19373
19374 /* The last file that we called dwarf2_start_subfile for.
19375 This is only used for TLLs. */
6f77053d 19376 unsigned int m_last_file = 0;
d9b3de22 19377 /* The last file a line number was recorded for. */
6f77053d 19378 struct subfile *m_last_subfile = NULL;
d9b3de22 19379
804d2729
TT
19380 /* When true, record the lines we decode. */
19381 bool m_currently_recording_lines = false;
d9b3de22
DE
19382
19383 /* The last line number that was recorded, used to coalesce
19384 consecutive entries for the same line. This can happen, for
19385 example, when discriminators are present. PR 17276. */
6f77053d
PA
19386 unsigned int m_last_line = 0;
19387 bool m_line_has_non_zero_discriminator = false;
8c43009f 19388};
d9b3de22 19389
6f77053d
PA
19390void
19391lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19392{
19393 CORE_ADDR addr_adj = (((m_op_index + adjust)
19394 / m_line_header->maximum_ops_per_instruction)
19395 * m_line_header->minimum_instruction_length);
19396 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19397 m_op_index = ((m_op_index + adjust)
19398 % m_line_header->maximum_ops_per_instruction);
19399}
d9b3de22 19400
6f77053d
PA
19401void
19402lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19403{
6f77053d 19404 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19405 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19406 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19407 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19408 / m_line_header->maximum_ops_per_instruction)
19409 * m_line_header->minimum_instruction_length);
19410 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19411 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19412 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19413
258bf0ee 19414 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19415 advance_line (line_delta);
19416 record_line (false);
19417 m_discriminator = 0;
19418}
d9b3de22 19419
6f77053d
PA
19420void
19421lnp_state_machine::handle_set_file (file_name_index file)
19422{
19423 m_file = file;
19424
19425 const file_entry *fe = current_file ();
19426 if (fe == NULL)
19427 dwarf2_debug_line_missing_file_complaint ();
19428 else if (m_record_lines_p)
19429 {
19430 const char *dir = fe->include_dir (m_line_header);
19431
c24bdb02 19432 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19433 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19434 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19435 }
19436}
19437
19438void
19439lnp_state_machine::handle_const_add_pc ()
19440{
19441 CORE_ADDR adjust
19442 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19443
19444 CORE_ADDR addr_adj
19445 = (((m_op_index + adjust)
19446 / m_line_header->maximum_ops_per_instruction)
19447 * m_line_header->minimum_instruction_length);
19448
19449 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19450 m_op_index = ((m_op_index + adjust)
19451 % m_line_header->maximum_ops_per_instruction);
19452}
d9b3de22 19453
a05a36a5
DE
19454/* Return non-zero if we should add LINE to the line number table.
19455 LINE is the line to add, LAST_LINE is the last line that was added,
19456 LAST_SUBFILE is the subfile for LAST_LINE.
19457 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19458 had a non-zero discriminator.
19459
19460 We have to be careful in the presence of discriminators.
19461 E.g., for this line:
19462
19463 for (i = 0; i < 100000; i++);
19464
19465 clang can emit four line number entries for that one line,
19466 each with a different discriminator.
19467 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19468
19469 However, we want gdb to coalesce all four entries into one.
19470 Otherwise the user could stepi into the middle of the line and
19471 gdb would get confused about whether the pc really was in the
19472 middle of the line.
19473
19474 Things are further complicated by the fact that two consecutive
19475 line number entries for the same line is a heuristic used by gcc
19476 to denote the end of the prologue. So we can't just discard duplicate
19477 entries, we have to be selective about it. The heuristic we use is
19478 that we only collapse consecutive entries for the same line if at least
19479 one of those entries has a non-zero discriminator. PR 17276.
19480
19481 Note: Addresses in the line number state machine can never go backwards
19482 within one sequence, thus this coalescing is ok. */
19483
19484static int
804d2729
TT
19485dwarf_record_line_p (struct dwarf2_cu *cu,
19486 unsigned int line, unsigned int last_line,
a05a36a5
DE
19487 int line_has_non_zero_discriminator,
19488 struct subfile *last_subfile)
19489{
c24bdb02 19490 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19491 return 1;
19492 if (line != last_line)
19493 return 1;
19494 /* Same line for the same file that we've seen already.
19495 As a last check, for pr 17276, only record the line if the line
19496 has never had a non-zero discriminator. */
19497 if (!line_has_non_zero_discriminator)
19498 return 1;
19499 return 0;
19500}
19501
804d2729
TT
19502/* Use the CU's builder to record line number LINE beginning at
19503 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19504
19505static void
d9b3de22 19506dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19507 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19508 struct dwarf2_cu *cu)
252a6764
DE
19509{
19510 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19511
27e0867f
DE
19512 if (dwarf_line_debug)
19513 {
19514 fprintf_unfiltered (gdb_stdlog,
19515 "Recording line %u, file %s, address %s\n",
19516 line, lbasename (subfile->name),
19517 paddress (gdbarch, address));
19518 }
19519
804d2729 19520 if (cu != nullptr)
8c95582d 19521 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19522}
19523
19524/* Subroutine of dwarf_decode_lines_1 to simplify it.
19525 Mark the end of a set of line number records.
d9b3de22 19526 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19527 If SUBFILE is NULL the request is ignored. */
19528
19529static void
19530dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19531 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19532{
27e0867f
DE
19533 if (subfile == NULL)
19534 return;
19535
19536 if (dwarf_line_debug)
19537 {
19538 fprintf_unfiltered (gdb_stdlog,
19539 "Finishing current line, file %s, address %s\n",
19540 lbasename (subfile->name),
19541 paddress (gdbarch, address));
19542 }
19543
8c95582d 19544 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
19545}
19546
6f77053d
PA
19547void
19548lnp_state_machine::record_line (bool end_sequence)
d9b3de22 19549{
d9b3de22
DE
19550 if (dwarf_line_debug)
19551 {
19552 fprintf_unfiltered (gdb_stdlog,
19553 "Processing actual line %u: file %u,"
94a72be7 19554 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 19555 m_line, m_file,
6f77053d 19556 paddress (m_gdbarch, m_address),
94a72be7
AB
19557 m_is_stmt, m_discriminator,
19558 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
19559 }
19560
6f77053d 19561 file_entry *fe = current_file ();
8c43009f
PA
19562
19563 if (fe == NULL)
d9b3de22
DE
19564 dwarf2_debug_line_missing_file_complaint ();
19565 /* For now we ignore lines not starting on an instruction boundary.
19566 But not when processing end_sequence for compatibility with the
19567 previous version of the code. */
6f77053d 19568 else if (m_op_index == 0 || end_sequence)
d9b3de22 19569 {
8c43009f 19570 fe->included_p = 1;
8c95582d 19571 if (m_record_lines_p)
d9b3de22 19572 {
c24bdb02 19573 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 19574 || end_sequence)
d9b3de22 19575 {
804d2729
TT
19576 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
19577 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
19578 }
19579
19580 if (!end_sequence)
19581 {
8c95582d
AB
19582 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
19583
804d2729 19584 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
19585 m_line_has_non_zero_discriminator,
19586 m_last_subfile))
d9b3de22 19587 {
c24bdb02 19588 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 19589 dwarf_record_line_1 (m_gdbarch,
c24bdb02 19590 builder->get_current_subfile (),
8c95582d 19591 m_line, m_address, is_stmt,
804d2729 19592 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 19593 }
c24bdb02 19594 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19595 m_last_line = m_line;
d9b3de22
DE
19596 }
19597 }
19598 }
19599}
19600
804d2729
TT
19601lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
19602 line_header *lh, bool record_lines_p)
d9b3de22 19603{
804d2729 19604 m_cu = cu;
6f77053d
PA
19605 m_gdbarch = arch;
19606 m_record_lines_p = record_lines_p;
19607 m_line_header = lh;
d9b3de22 19608
804d2729 19609 m_currently_recording_lines = true;
d9b3de22 19610
d9b3de22
DE
19611 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
19612 was a line entry for it so that the backend has a chance to adjust it
19613 and also record it in case it needs it. This is currently used by MIPS
19614 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
19615 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
19616 m_is_stmt = lh->default_is_stmt;
19617 m_discriminator = 0;
252a6764
DE
19618}
19619
6f77053d
PA
19620void
19621lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
19622 const gdb_byte *line_ptr,
7ab6656f 19623 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 19624{
7ab6656f
OJ
19625 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
19626 the pc range of the CU. However, we restrict the test to only ADDRESS
19627 values of zero to preserve GDB's previous behaviour which is to handle
19628 the specific case of a function being GC'd by the linker. */
924c2928 19629
7ab6656f 19630 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
19631 {
19632 /* This line table is for a function which has been
19633 GCd by the linker. Ignore it. PR gdb/12528 */
19634
518817b3 19635 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
19636 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
19637
b98664d3 19638 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 19639 line_offset, objfile_name (objfile));
804d2729
TT
19640 m_currently_recording_lines = false;
19641 /* Note: m_currently_recording_lines is left as false until we see
19642 DW_LNE_end_sequence. */
924c2928
DE
19643 }
19644}
19645
f3f5162e 19646/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
19647 Process the line number information in LH.
19648 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
19649 program in order to set included_p for every referenced header. */
debd256d 19650
c906108c 19651static void
43f3e411
DE
19652dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
19653 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 19654{
d521ce57
TT
19655 const gdb_byte *line_ptr, *extended_end;
19656 const gdb_byte *line_end;
a8c50c1f 19657 unsigned int bytes_read, extended_len;
699ca60a 19658 unsigned char op_code, extended_op;
e142c38c 19659 CORE_ADDR baseaddr;
518817b3 19660 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19661 bfd *abfd = objfile->obfd;
fbf65064 19662 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6f77053d
PA
19663 /* True if we're recording line info (as opposed to building partial
19664 symtabs and just interested in finding include files mentioned by
19665 the line number program). */
19666 bool record_lines_p = !decode_for_pst_p;
e142c38c 19667
b3b3bada 19668 baseaddr = objfile->text_section_offset ();
c906108c 19669
debd256d
JB
19670 line_ptr = lh->statement_program_start;
19671 line_end = lh->statement_program_end;
c906108c
SS
19672
19673 /* Read the statement sequences until there's nothing left. */
19674 while (line_ptr < line_end)
19675 {
6f77053d
PA
19676 /* The DWARF line number program state machine. Reset the state
19677 machine at the start of each sequence. */
804d2729 19678 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 19679 bool end_sequence = false;
d9b3de22 19680
8c43009f 19681 if (record_lines_p)
c906108c 19682 {
8c43009f
PA
19683 /* Start a subfile for the current file of the state
19684 machine. */
19685 const file_entry *fe = state_machine.current_file ();
19686
19687 if (fe != NULL)
804d2729 19688 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
19689 }
19690
a738430d 19691 /* Decode the table. */
d9b3de22 19692 while (line_ptr < line_end && !end_sequence)
c906108c
SS
19693 {
19694 op_code = read_1_byte (abfd, line_ptr);
19695 line_ptr += 1;
9aa1fe7e 19696
debd256d 19697 if (op_code >= lh->opcode_base)
6e70227d 19698 {
8e07a239 19699 /* Special opcode. */
6f77053d 19700 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
19701 }
19702 else switch (op_code)
c906108c
SS
19703 {
19704 case DW_LNS_extended_op:
3e43a32a
MS
19705 extended_len = read_unsigned_leb128 (abfd, line_ptr,
19706 &bytes_read);
473b7be6 19707 line_ptr += bytes_read;
a8c50c1f 19708 extended_end = line_ptr + extended_len;
c906108c
SS
19709 extended_op = read_1_byte (abfd, line_ptr);
19710 line_ptr += 1;
19711 switch (extended_op)
19712 {
19713 case DW_LNE_end_sequence:
6f77053d
PA
19714 state_machine.handle_end_sequence ();
19715 end_sequence = true;
c906108c
SS
19716 break;
19717 case DW_LNE_set_address:
d9b3de22
DE
19718 {
19719 CORE_ADDR address
c8a7a66f 19720 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 19721 line_ptr += bytes_read;
6f77053d
PA
19722
19723 state_machine.check_line_address (cu, line_ptr,
7ab6656f 19724 lowpc - baseaddr, address);
6f77053d 19725 state_machine.handle_set_address (baseaddr, address);
d9b3de22 19726 }
c906108c
SS
19727 break;
19728 case DW_LNE_define_file:
debd256d 19729 {
d521ce57 19730 const char *cur_file;
ecfb656c
PA
19731 unsigned int mod_time, length;
19732 dir_index dindex;
6e70227d 19733
3e43a32a
MS
19734 cur_file = read_direct_string (abfd, line_ptr,
19735 &bytes_read);
debd256d 19736 line_ptr += bytes_read;
ecfb656c 19737 dindex = (dir_index)
debd256d
JB
19738 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19739 line_ptr += bytes_read;
19740 mod_time =
19741 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19742 line_ptr += bytes_read;
19743 length =
19744 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19745 line_ptr += bytes_read;
ecfb656c 19746 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 19747 }
c906108c 19748 break;
d0c6ba3d 19749 case DW_LNE_set_discriminator:
6f77053d
PA
19750 {
19751 /* The discriminator is not interesting to the
19752 debugger; just ignore it. We still need to
19753 check its value though:
19754 if there are consecutive entries for the same
19755 (non-prologue) line we want to coalesce them.
19756 PR 17276. */
19757 unsigned int discr
19758 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19759 line_ptr += bytes_read;
19760
19761 state_machine.handle_set_discriminator (discr);
19762 }
d0c6ba3d 19763 break;
c906108c 19764 default:
b98664d3 19765 complaint (_("mangled .debug_line section"));
debd256d 19766 return;
c906108c 19767 }
a8c50c1f
DJ
19768 /* Make sure that we parsed the extended op correctly. If e.g.
19769 we expected a different address size than the producer used,
19770 we may have read the wrong number of bytes. */
19771 if (line_ptr != extended_end)
19772 {
b98664d3 19773 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
19774 return;
19775 }
c906108c
SS
19776 break;
19777 case DW_LNS_copy:
6f77053d 19778 state_machine.handle_copy ();
c906108c
SS
19779 break;
19780 case DW_LNS_advance_pc:
2dc7f7b3
TT
19781 {
19782 CORE_ADDR adjust
19783 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 19784 line_ptr += bytes_read;
6f77053d
PA
19785
19786 state_machine.handle_advance_pc (adjust);
2dc7f7b3 19787 }
c906108c
SS
19788 break;
19789 case DW_LNS_advance_line:
a05a36a5
DE
19790 {
19791 int line_delta
19792 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 19793 line_ptr += bytes_read;
6f77053d
PA
19794
19795 state_machine.handle_advance_line (line_delta);
a05a36a5 19796 }
c906108c
SS
19797 break;
19798 case DW_LNS_set_file:
d9b3de22 19799 {
6f77053d 19800 file_name_index file
ecfb656c
PA
19801 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
19802 &bytes_read);
d9b3de22 19803 line_ptr += bytes_read;
8c43009f 19804
6f77053d 19805 state_machine.handle_set_file (file);
d9b3de22 19806 }
c906108c
SS
19807 break;
19808 case DW_LNS_set_column:
0ad93d4f 19809 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
19810 line_ptr += bytes_read;
19811 break;
19812 case DW_LNS_negate_stmt:
6f77053d 19813 state_machine.handle_negate_stmt ();
c906108c
SS
19814 break;
19815 case DW_LNS_set_basic_block:
c906108c 19816 break;
c2c6d25f
JM
19817 /* Add to the address register of the state machine the
19818 address increment value corresponding to special opcode
a738430d
MK
19819 255. I.e., this value is scaled by the minimum
19820 instruction length since special opcode 255 would have
b021a221 19821 scaled the increment. */
c906108c 19822 case DW_LNS_const_add_pc:
6f77053d 19823 state_machine.handle_const_add_pc ();
c906108c
SS
19824 break;
19825 case DW_LNS_fixed_advance_pc:
3e29f34a 19826 {
6f77053d 19827 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 19828 line_ptr += 2;
6f77053d
PA
19829
19830 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 19831 }
c906108c 19832 break;
9aa1fe7e 19833 default:
a738430d
MK
19834 {
19835 /* Unknown standard opcode, ignore it. */
9aa1fe7e 19836 int i;
a738430d 19837
debd256d 19838 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
19839 {
19840 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
19841 line_ptr += bytes_read;
19842 }
19843 }
c906108c
SS
19844 }
19845 }
d9b3de22
DE
19846
19847 if (!end_sequence)
19848 dwarf2_debug_line_missing_end_sequence_complaint ();
19849
19850 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
19851 in which case we still finish recording the last line). */
6f77053d 19852 state_machine.record_line (true);
c906108c 19853 }
f3f5162e
DE
19854}
19855
19856/* Decode the Line Number Program (LNP) for the given line_header
19857 structure and CU. The actual information extracted and the type
19858 of structures created from the LNP depends on the value of PST.
19859
19860 1. If PST is NULL, then this procedure uses the data from the program
19861 to create all necessary symbol tables, and their linetables.
19862
19863 2. If PST is not NULL, this procedure reads the program to determine
19864 the list of files included by the unit represented by PST, and
19865 builds all the associated partial symbol tables.
19866
19867 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
19868 It is used for relative paths in the line table.
19869 NOTE: When processing partial symtabs (pst != NULL),
19870 comp_dir == pst->dirname.
19871
19872 NOTE: It is important that psymtabs have the same file name (via strcmp)
19873 as the corresponding symtab. Since COMP_DIR is not used in the name of the
19874 symtab we don't use it in the name of the psymtabs we create.
19875 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
19876 A good testcase for this is mb-inline.exp.
19877
527f3840
JK
19878 LOWPC is the lowest address in CU (or 0 if not known).
19879
19880 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
19881 for its PC<->lines mapping information. Otherwise only the filename
19882 table is read in. */
f3f5162e
DE
19883
19884static void
19885dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 19886 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 19887 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 19888{
518817b3 19889 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 19890 const int decode_for_pst_p = (pst != NULL);
f3f5162e 19891
527f3840
JK
19892 if (decode_mapping)
19893 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
19894
19895 if (decode_for_pst_p)
19896 {
aaa75496
JB
19897 /* Now that we're done scanning the Line Header Program, we can
19898 create the psymtab of each included file. */
7ba99d21
AT
19899 for (auto &file_entry : lh->file_names ())
19900 if (file_entry.included_p == 1)
aaa75496 19901 {
c89b44cd 19902 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 19903 const char *include_name =
7ba99d21
AT
19904 psymtab_include_file_name (lh, file_entry, pst,
19905 comp_dir, &name_holder);
c6da4cef 19906 if (include_name != NULL)
aaa75496
JB
19907 dwarf2_create_include_psymtab (include_name, pst, objfile);
19908 }
19909 }
cb1df416
DJ
19910 else
19911 {
19912 /* Make sure a symtab is created for every file, even files
19913 which contain only variables (i.e. no code with associated
19914 line numbers). */
c24bdb02
KS
19915 buildsym_compunit *builder = cu->get_builder ();
19916 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 19917
7ba99d21 19918 for (auto &fe : lh->file_names ())
cb1df416 19919 {
804d2729 19920 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 19921 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 19922 {
c24bdb02 19923 builder->get_current_subfile ()->symtab
804d2729 19924 = allocate_symtab (cust,
c24bdb02 19925 builder->get_current_subfile ()->name);
43f3e411 19926 }
c24bdb02 19927 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
19928 }
19929 }
c906108c
SS
19930}
19931
19932/* Start a subfile for DWARF. FILENAME is the name of the file and
19933 DIRNAME the name of the source directory which contains FILENAME
4d663531 19934 or NULL if not known.
c906108c
SS
19935 This routine tries to keep line numbers from identical absolute and
19936 relative file names in a common subfile.
19937
19938 Using the `list' example from the GDB testsuite, which resides in
19939 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
19940 of /srcdir/list0.c yields the following debugging information for list0.c:
19941
c5aa993b 19942 DW_AT_name: /srcdir/list0.c
4d663531 19943 DW_AT_comp_dir: /compdir
357e46e7 19944 files.files[0].name: list0.h
c5aa993b 19945 files.files[0].dir: /srcdir
357e46e7 19946 files.files[1].name: list0.c
c5aa993b 19947 files.files[1].dir: /srcdir
c906108c
SS
19948
19949 The line number information for list0.c has to end up in a single
4f1520fb
FR
19950 subfile, so that `break /srcdir/list0.c:1' works as expected.
19951 start_subfile will ensure that this happens provided that we pass the
19952 concatenation of files.files[1].dir and files.files[1].name as the
19953 subfile's name. */
c906108c
SS
19954
19955static void
804d2729
TT
19956dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
19957 const char *dirname)
c906108c 19958{
43816ebc 19959 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 19960
4d663531 19961 /* In order not to lose the line information directory,
4f1520fb
FR
19962 we concatenate it to the filename when it makes sense.
19963 Note that the Dwarf3 standard says (speaking of filenames in line
19964 information): ``The directory index is ignored for file names
19965 that represent full path names''. Thus ignoring dirname in the
19966 `else' branch below isn't an issue. */
c906108c 19967
d5166ae1 19968 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 19969 {
43816ebc
TT
19970 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
19971 filename = copy.get ();
d521ce57 19972 }
c906108c 19973
c24bdb02 19974 cu->get_builder ()->start_subfile (filename);
c906108c
SS
19975}
19976
804d2729
TT
19977/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
19978 buildsym_compunit constructor. */
f4dc4d17 19979
c24bdb02
KS
19980struct compunit_symtab *
19981dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
19982 CORE_ADDR low_pc)
f4dc4d17 19983{
c24bdb02 19984 gdb_assert (m_builder == nullptr);
43f3e411 19985
c24bdb02
KS
19986 m_builder.reset (new struct buildsym_compunit
19987 (per_cu->dwarf2_per_objfile->objfile,
19988 name, comp_dir, language, low_pc));
93b8bea4 19989
c24bdb02 19990 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 19991
c24bdb02
KS
19992 get_builder ()->record_debugformat ("DWARF 2");
19993 get_builder ()->record_producer (producer);
f4dc4d17 19994
c24bdb02 19995 processing_has_namespace_info = false;
43f3e411 19996
c24bdb02 19997 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
19998}
19999
4c2df51b
DJ
20000static void
20001var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20002 struct dwarf2_cu *cu)
4c2df51b 20003{
518817b3 20004 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20005 struct comp_unit_head *cu_header = &cu->header;
20006
4c2df51b
DJ
20007 /* NOTE drow/2003-01-30: There used to be a comment and some special
20008 code here to turn a symbol with DW_AT_external and a
20009 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20010 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20011 with some versions of binutils) where shared libraries could have
20012 relocations against symbols in their debug information - the
20013 minimal symbol would have the right address, but the debug info
20014 would not. It's no longer necessary, because we will explicitly
20015 apply relocations when we read in the debug information now. */
20016
20017 /* A DW_AT_location attribute with no contents indicates that a
20018 variable has been optimized away. */
4fc6c0d5 20019 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20020 {
f1e6e072 20021 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20022 return;
20023 }
20024
20025 /* Handle one degenerate form of location expression specially, to
20026 preserve GDB's previous behavior when section offsets are
336d760d
AT
20027 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20028 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20029
4fc6c0d5 20030 if (attr->form_is_block ()
3019eac3
DE
20031 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20032 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20033 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20034 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20035 && (DW_BLOCK (attr)->size
20036 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20037 {
891d2f0b 20038 unsigned int dummy;
4c2df51b 20039
3019eac3 20040 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20041 SET_SYMBOL_VALUE_ADDRESS
20042 (sym, cu->header.read_address (objfile->obfd,
20043 DW_BLOCK (attr)->data + 1,
20044 &dummy));
3019eac3 20045 else
38583298
TT
20046 SET_SYMBOL_VALUE_ADDRESS
20047 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20048 &dummy));
f1e6e072 20049 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20050 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20051 SET_SYMBOL_VALUE_ADDRESS
20052 (sym,
20053 SYMBOL_VALUE_ADDRESS (sym)
20054 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20055 return;
20056 }
20057
20058 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20059 expression evaluator, and use LOC_COMPUTED only when necessary
20060 (i.e. when the value of a register or memory location is
20061 referenced, or a thread-local block, etc.). Then again, it might
20062 not be worthwhile. I'm assuming that it isn't unless performance
20063 or memory numbers show me otherwise. */
20064
f1e6e072 20065 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20066
f1e6e072 20067 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20068 cu->has_loclist = true;
4c2df51b
DJ
20069}
20070
c906108c
SS
20071/* Given a pointer to a DWARF information entry, figure out if we need
20072 to make a symbol table entry for it, and if so, create a new entry
20073 and return a pointer to it.
20074 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20075 used the passed type.
20076 If SPACE is not NULL, use it to hold the new symbol. If it is
20077 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20078
20079static struct symbol *
5e2db402
TT
20080new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20081 struct symbol *space)
c906108c 20082{
518817b3
SM
20083 struct dwarf2_per_objfile *dwarf2_per_objfile
20084 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20085 struct objfile *objfile = dwarf2_per_objfile->objfile;
3e29f34a 20086 struct gdbarch *gdbarch = get_objfile_arch (objfile);
c906108c 20087 struct symbol *sym = NULL;
15d034d0 20088 const char *name;
c906108c
SS
20089 struct attribute *attr = NULL;
20090 struct attribute *attr2 = NULL;
e142c38c 20091 CORE_ADDR baseaddr;
e37fd15a
SW
20092 struct pending **list_to_add = NULL;
20093
edb3359d 20094 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20095
b3b3bada 20096 baseaddr = objfile->text_section_offset ();
c906108c 20097
94af9270 20098 name = dwarf2_name (die, cu);
c906108c
SS
20099 if (name)
20100 {
94af9270 20101 const char *linkagename;
34eaf542 20102 int suppress_add = 0;
94af9270 20103
34eaf542
TT
20104 if (space)
20105 sym = space;
20106 else
e623cf5d 20107 sym = allocate_symbol (objfile);
c906108c 20108 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20109
20110 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20111 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20112 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20113 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20114
f55ee35c
JK
20115 /* Fortran does not have mangling standard and the mangling does differ
20116 between gfortran, iFort etc. */
20117 if (cu->language == language_fortran
468c0cbb
CB
20118 && symbol_get_demangled_name (sym) == NULL)
20119 symbol_set_demangled_name (sym,
cfc594ee 20120 dwarf2_full_name (name, die, cu),
29df156d 20121 NULL);
f55ee35c 20122
c906108c 20123 /* Default assumptions.
c5aa993b 20124 Use the passed type or decode it from the die. */
176620f1 20125 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20126 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20127 if (type != NULL)
20128 SYMBOL_TYPE (sym) = type;
20129 else
e7c27a73 20130 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20131 attr = dwarf2_attr (die,
20132 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20133 cu);
435d3d88 20134 if (attr != nullptr)
c906108c
SS
20135 {
20136 SYMBOL_LINE (sym) = DW_UNSND (attr);
20137 }
cb1df416 20138
edb3359d
DJ
20139 attr = dwarf2_attr (die,
20140 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20141 cu);
435d3d88 20142 if (attr != nullptr)
cb1df416 20143 {
ecfb656c 20144 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20145 struct file_entry *fe;
9a619af0 20146
ecfb656c
PA
20147 if (cu->line_header != NULL)
20148 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20149 else
20150 fe = NULL;
20151
20152 if (fe == NULL)
b98664d3 20153 complaint (_("file index out of range"));
8c43009f
PA
20154 else
20155 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20156 }
20157
c906108c
SS
20158 switch (die->tag)
20159 {
20160 case DW_TAG_label:
e142c38c 20161 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20162 if (attr != nullptr)
3e29f34a
MR
20163 {
20164 CORE_ADDR addr;
20165
cd6c91b4 20166 addr = attr->value_as_address ();
3e29f34a 20167 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20168 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20169 }
0f5238ed
TT
20170 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20171 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20172 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20173 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20174 break;
20175 case DW_TAG_subprogram:
20176 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20177 finish_block. */
f1e6e072 20178 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20179 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20180 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20181 || cu->language == language_ada
20182 || cu->language == language_fortran)
c906108c 20183 {
2cfa0c8d 20184 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20185 Ada and Fortran subprograms, whether marked external or
20186 not, are always stored as a global symbol, because we want
20187 to be able to access them globally. For instance, we want
20188 to be able to break on a nested subprogram without having
20189 to specify the context. */
c24bdb02 20190 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20191 }
20192 else
20193 {
e37fd15a 20194 list_to_add = cu->list_in_scope;
c906108c
SS
20195 }
20196 break;
edb3359d
DJ
20197 case DW_TAG_inlined_subroutine:
20198 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20199 finish_block. */
f1e6e072 20200 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20201 SYMBOL_INLINED (sym) = 1;
481860b3 20202 list_to_add = cu->list_in_scope;
edb3359d 20203 break;
34eaf542
TT
20204 case DW_TAG_template_value_param:
20205 suppress_add = 1;
20206 /* Fall through. */
72929c62 20207 case DW_TAG_constant:
c906108c 20208 case DW_TAG_variable:
254e6b9e 20209 case DW_TAG_member:
0963b4bd
MS
20210 /* Compilation with minimal debug info may result in
20211 variables with missing type entries. Change the
20212 misleading `void' type to something sensible. */
c906108c 20213 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20214 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20215
e142c38c 20216 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20217 /* In the case of DW_TAG_member, we should only be called for
20218 static const members. */
20219 if (die->tag == DW_TAG_member)
20220 {
3863f96c
DE
20221 /* dwarf2_add_field uses die_is_declaration,
20222 so we do the same. */
254e6b9e
DE
20223 gdb_assert (die_is_declaration (die, cu));
20224 gdb_assert (attr);
20225 }
435d3d88 20226 if (attr != nullptr)
c906108c 20227 {
e7c27a73 20228 dwarf2_const_value (attr, sym, cu);
e142c38c 20229 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20230 if (!suppress_add)
34eaf542
TT
20231 {
20232 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20233 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20234 else
e37fd15a 20235 list_to_add = cu->list_in_scope;
34eaf542 20236 }
c906108c
SS
20237 break;
20238 }
e142c38c 20239 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20240 if (attr != nullptr)
c906108c 20241 {
e7c27a73 20242 var_decode_location (attr, sym, cu);
e142c38c 20243 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20244
20245 /* Fortran explicitly imports any global symbols to the local
20246 scope by DW_TAG_common_block. */
20247 if (cu->language == language_fortran && die->parent
20248 && die->parent->tag == DW_TAG_common_block)
20249 attr2 = NULL;
20250
caac4577
JG
20251 if (SYMBOL_CLASS (sym) == LOC_STATIC
20252 && SYMBOL_VALUE_ADDRESS (sym) == 0
20253 && !dwarf2_per_objfile->has_section_at_zero)
20254 {
20255 /* When a static variable is eliminated by the linker,
20256 the corresponding debug information is not stripped
20257 out, but the variable address is set to null;
20258 do not add such variables into symbol table. */
20259 }
20260 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20261 {
4b610737
TT
20262 if (SYMBOL_CLASS (sym) == LOC_STATIC
20263 && (objfile->flags & OBJF_MAINLINE) == 0
20264 && dwarf2_per_objfile->can_copy)
20265 {
20266 /* A global static variable might be subject to
20267 copy relocation. We first check for a local
20268 minsym, though, because maybe the symbol was
20269 marked hidden, in which case this would not
20270 apply. */
20271 bound_minimal_symbol found
20272 = (lookup_minimal_symbol_linkage
987012b8 20273 (sym->linkage_name (), objfile));
4b610737
TT
20274 if (found.minsym != nullptr)
20275 sym->maybe_copied = 1;
20276 }
f55ee35c 20277
1c809c68
TT
20278 /* A variable with DW_AT_external is never static,
20279 but it may be block-scoped. */
804d2729 20280 list_to_add
c24bdb02
KS
20281 = ((cu->list_in_scope
20282 == cu->get_builder ()->get_file_symbols ())
20283 ? cu->get_builder ()->get_global_symbols ()
804d2729 20284 : cu->list_in_scope);
1c809c68 20285 }
c906108c 20286 else
e37fd15a 20287 list_to_add = cu->list_in_scope;
c906108c
SS
20288 }
20289 else
20290 {
20291 /* We do not know the address of this symbol.
c5aa993b
JM
20292 If it is an external symbol and we have type information
20293 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20294 The address of the variable will then be determined from
20295 the minimal symbol table whenever the variable is
20296 referenced. */
e142c38c 20297 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20298
20299 /* Fortran explicitly imports any global symbols to the local
20300 scope by DW_TAG_common_block. */
20301 if (cu->language == language_fortran && die->parent
20302 && die->parent->tag == DW_TAG_common_block)
20303 {
20304 /* SYMBOL_CLASS doesn't matter here because
20305 read_common_block is going to reset it. */
20306 if (!suppress_add)
20307 list_to_add = cu->list_in_scope;
20308 }
20309 else if (attr2 && (DW_UNSND (attr2) != 0)
20310 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20311 {
0fe7935b
DJ
20312 /* A variable with DW_AT_external is never static, but it
20313 may be block-scoped. */
804d2729 20314 list_to_add
c24bdb02
KS
20315 = ((cu->list_in_scope
20316 == cu->get_builder ()->get_file_symbols ())
20317 ? cu->get_builder ()->get_global_symbols ()
804d2729 20318 : cu->list_in_scope);
0fe7935b 20319
f1e6e072 20320 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20321 }
442ddf59
JK
20322 else if (!die_is_declaration (die, cu))
20323 {
20324 /* Use the default LOC_OPTIMIZED_OUT class. */
20325 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20326 if (!suppress_add)
20327 list_to_add = cu->list_in_scope;
442ddf59 20328 }
c906108c
SS
20329 }
20330 break;
20331 case DW_TAG_formal_parameter:
a60f3166
TT
20332 {
20333 /* If we are inside a function, mark this as an argument. If
20334 not, we might be looking at an argument to an inlined function
20335 when we do not have enough information to show inlined frames;
20336 pretend it's a local variable in that case so that the user can
20337 still see it. */
804d2729 20338 struct context_stack *curr
c24bdb02 20339 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20340 if (curr != nullptr && curr->name != nullptr)
20341 SYMBOL_IS_ARGUMENT (sym) = 1;
20342 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20343 if (attr != nullptr)
a60f3166
TT
20344 {
20345 var_decode_location (attr, sym, cu);
20346 }
20347 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20348 if (attr != nullptr)
a60f3166
TT
20349 {
20350 dwarf2_const_value (attr, sym, cu);
20351 }
f346a30d 20352
a60f3166
TT
20353 list_to_add = cu->list_in_scope;
20354 }
c906108c
SS
20355 break;
20356 case DW_TAG_unspecified_parameters:
20357 /* From varargs functions; gdb doesn't seem to have any
20358 interest in this information, so just ignore it for now.
20359 (FIXME?) */
20360 break;
34eaf542
TT
20361 case DW_TAG_template_type_param:
20362 suppress_add = 1;
20363 /* Fall through. */
c906108c 20364 case DW_TAG_class_type:
680b30c7 20365 case DW_TAG_interface_type:
c906108c
SS
20366 case DW_TAG_structure_type:
20367 case DW_TAG_union_type:
72019c9c 20368 case DW_TAG_set_type:
c906108c 20369 case DW_TAG_enumeration_type:
f1e6e072 20370 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20371 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20372
63d06c5c 20373 {
9c37b5ae 20374 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20375 really ever be static objects: otherwise, if you try
20376 to, say, break of a class's method and you're in a file
20377 which doesn't mention that class, it won't work unless
20378 the check for all static symbols in lookup_symbol_aux
20379 saves you. See the OtherFileClass tests in
20380 gdb.c++/namespace.exp. */
20381
e37fd15a 20382 if (!suppress_add)
34eaf542 20383 {
c24bdb02 20384 buildsym_compunit *builder = cu->get_builder ();
804d2729 20385 list_to_add
c24bdb02 20386 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20387 && cu->language == language_cplus
c24bdb02 20388 ? builder->get_global_symbols ()
804d2729 20389 : cu->list_in_scope);
63d06c5c 20390
64382290 20391 /* The semantics of C++ state that "struct foo {
9c37b5ae 20392 ... }" also defines a typedef for "foo". */
64382290 20393 if (cu->language == language_cplus
45280282 20394 || cu->language == language_ada
c44af4eb
TT
20395 || cu->language == language_d
20396 || cu->language == language_rust)
64382290
TT
20397 {
20398 /* The symbol's name is already allocated along
20399 with this objfile, so we don't need to
20400 duplicate it for the type. */
20401 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20402 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20403 }
63d06c5c
DC
20404 }
20405 }
c906108c
SS
20406 break;
20407 case DW_TAG_typedef:
f1e6e072 20408 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20409 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20410 list_to_add = cu->list_in_scope;
63d06c5c 20411 break;
c906108c 20412 case DW_TAG_base_type:
a02abb62 20413 case DW_TAG_subrange_type:
f1e6e072 20414 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20415 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20416 list_to_add = cu->list_in_scope;
c906108c
SS
20417 break;
20418 case DW_TAG_enumerator:
e142c38c 20419 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20420 if (attr != nullptr)
c906108c 20421 {
e7c27a73 20422 dwarf2_const_value (attr, sym, cu);
c906108c 20423 }
63d06c5c
DC
20424 {
20425 /* NOTE: carlton/2003-11-10: See comment above in the
20426 DW_TAG_class_type, etc. block. */
20427
804d2729 20428 list_to_add
c24bdb02 20429 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20430 && cu->language == language_cplus
c24bdb02 20431 ? cu->get_builder ()->get_global_symbols ()
804d2729 20432 : cu->list_in_scope);
63d06c5c 20433 }
c906108c 20434 break;
74921315 20435 case DW_TAG_imported_declaration:
5c4e30ca 20436 case DW_TAG_namespace:
f1e6e072 20437 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20438 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20439 break;
530e8392
KB
20440 case DW_TAG_module:
20441 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20442 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20443 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20444 break;
4357ac6c 20445 case DW_TAG_common_block:
f1e6e072 20446 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20447 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20448 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20449 break;
c906108c
SS
20450 default:
20451 /* Not a tag we recognize. Hopefully we aren't processing
20452 trash data, but since we must specifically ignore things
20453 we don't recognize, there is nothing else we should do at
0963b4bd 20454 this point. */
b98664d3 20455 complaint (_("unsupported tag: '%s'"),
4d3c2250 20456 dwarf_tag_name (die->tag));
c906108c
SS
20457 break;
20458 }
df8a16a1 20459
e37fd15a
SW
20460 if (suppress_add)
20461 {
20462 sym->hash_next = objfile->template_symbols;
20463 objfile->template_symbols = sym;
20464 list_to_add = NULL;
20465 }
20466
20467 if (list_to_add != NULL)
d3cb6808 20468 add_symbol_to_list (sym, list_to_add);
e37fd15a 20469
df8a16a1
DJ
20470 /* For the benefit of old versions of GCC, check for anonymous
20471 namespaces based on the demangled name. */
4d4ec4e5 20472 if (!cu->processing_has_namespace_info
94af9270 20473 && cu->language == language_cplus)
c24bdb02 20474 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20475 }
20476 return (sym);
20477}
20478
98bfdba5
PA
20479/* Given an attr with a DW_FORM_dataN value in host byte order,
20480 zero-extend it as appropriate for the symbol's type. The DWARF
20481 standard (v4) is not entirely clear about the meaning of using
20482 DW_FORM_dataN for a constant with a signed type, where the type is
20483 wider than the data. The conclusion of a discussion on the DWARF
20484 list was that this is unspecified. We choose to always zero-extend
20485 because that is the interpretation long in use by GCC. */
c906108c 20486
98bfdba5 20487static gdb_byte *
ff39bb5e 20488dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20489 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20490{
518817b3 20491 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20492 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20493 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20494 LONGEST l = DW_UNSND (attr);
20495
20496 if (bits < sizeof (*value) * 8)
20497 {
20498 l &= ((LONGEST) 1 << bits) - 1;
20499 *value = l;
20500 }
20501 else if (bits == sizeof (*value) * 8)
20502 *value = l;
20503 else
20504 {
224c3ddb 20505 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20506 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20507 return bytes;
20508 }
20509
20510 return NULL;
20511}
20512
20513/* Read a constant value from an attribute. Either set *VALUE, or if
20514 the value does not fit in *VALUE, set *BYTES - either already
20515 allocated on the objfile obstack, or newly allocated on OBSTACK,
20516 or, set *BATON, if we translated the constant to a location
20517 expression. */
20518
20519static void
ff39bb5e 20520dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20521 const char *name, struct obstack *obstack,
20522 struct dwarf2_cu *cu,
d521ce57 20523 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20524 struct dwarf2_locexpr_baton **baton)
20525{
518817b3 20526 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20527 struct comp_unit_head *cu_header = &cu->header;
c906108c 20528 struct dwarf_block *blk;
98bfdba5
PA
20529 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20530 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20531
20532 *value = 0;
20533 *bytes = NULL;
20534 *baton = NULL;
c906108c
SS
20535
20536 switch (attr->form)
20537 {
20538 case DW_FORM_addr:
336d760d 20539 case DW_FORM_addrx:
3019eac3 20540 case DW_FORM_GNU_addr_index:
ac56253d 20541 {
ac56253d
TT
20542 gdb_byte *data;
20543
98bfdba5
PA
20544 if (TYPE_LENGTH (type) != cu_header->addr_size)
20545 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 20546 cu_header->addr_size,
98bfdba5 20547 TYPE_LENGTH (type));
ac56253d
TT
20548 /* Symbols of this form are reasonably rare, so we just
20549 piggyback on the existing location code rather than writing
20550 a new implementation of symbol_computed_ops. */
8d749320 20551 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
20552 (*baton)->per_cu = cu->per_cu;
20553 gdb_assert ((*baton)->per_cu);
ac56253d 20554
98bfdba5 20555 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 20556 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 20557 (*baton)->data = data;
ac56253d
TT
20558
20559 data[0] = DW_OP_addr;
20560 store_unsigned_integer (&data[1], cu_header->addr_size,
20561 byte_order, DW_ADDR (attr));
20562 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 20563 }
c906108c 20564 break;
4ac36638 20565 case DW_FORM_string:
93b5768b 20566 case DW_FORM_strp:
cf532bd1 20567 case DW_FORM_strx:
3019eac3 20568 case DW_FORM_GNU_str_index:
36586728 20569 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
20570 /* DW_STRING is already allocated on the objfile obstack, point
20571 directly to it. */
d521ce57 20572 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 20573 break;
c906108c
SS
20574 case DW_FORM_block1:
20575 case DW_FORM_block2:
20576 case DW_FORM_block4:
20577 case DW_FORM_block:
2dc7f7b3 20578 case DW_FORM_exprloc:
0224619f 20579 case DW_FORM_data16:
c906108c 20580 blk = DW_BLOCK (attr);
98bfdba5
PA
20581 if (TYPE_LENGTH (type) != blk->size)
20582 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
20583 TYPE_LENGTH (type));
20584 *bytes = blk->data;
c906108c 20585 break;
2df3850c
JM
20586
20587 /* The DW_AT_const_value attributes are supposed to carry the
20588 symbol's value "represented as it would be on the target
20589 architecture." By the time we get here, it's already been
20590 converted to host endianness, so we just need to sign- or
20591 zero-extend it as appropriate. */
20592 case DW_FORM_data1:
3aef2284 20593 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 20594 break;
c906108c 20595 case DW_FORM_data2:
3aef2284 20596 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 20597 break;
c906108c 20598 case DW_FORM_data4:
3aef2284 20599 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 20600 break;
c906108c 20601 case DW_FORM_data8:
3aef2284 20602 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
20603 break;
20604
c906108c 20605 case DW_FORM_sdata:
663c44ac 20606 case DW_FORM_implicit_const:
98bfdba5 20607 *value = DW_SND (attr);
2df3850c
JM
20608 break;
20609
c906108c 20610 case DW_FORM_udata:
98bfdba5 20611 *value = DW_UNSND (attr);
c906108c 20612 break;
2df3850c 20613
c906108c 20614 default:
b98664d3 20615 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 20616 dwarf_form_name (attr->form));
98bfdba5 20617 *value = 0;
c906108c
SS
20618 break;
20619 }
20620}
20621
2df3850c 20622
98bfdba5
PA
20623/* Copy constant value from an attribute to a symbol. */
20624
2df3850c 20625static void
ff39bb5e 20626dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 20627 struct dwarf2_cu *cu)
2df3850c 20628{
518817b3 20629 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 20630 LONGEST value;
d521ce57 20631 const gdb_byte *bytes;
98bfdba5 20632 struct dwarf2_locexpr_baton *baton;
2df3850c 20633
98bfdba5 20634 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 20635 sym->print_name (),
98bfdba5
PA
20636 &objfile->objfile_obstack, cu,
20637 &value, &bytes, &baton);
2df3850c 20638
98bfdba5
PA
20639 if (baton != NULL)
20640 {
98bfdba5 20641 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 20642 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
20643 }
20644 else if (bytes != NULL)
20645 {
20646 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 20647 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
20648 }
20649 else
20650 {
20651 SYMBOL_VALUE (sym) = value;
f1e6e072 20652 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 20653 }
2df3850c
JM
20654}
20655
c906108c
SS
20656/* Return the type of the die in question using its DW_AT_type attribute. */
20657
20658static struct type *
e7c27a73 20659die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20660{
c906108c 20661 struct attribute *type_attr;
c906108c 20662
e142c38c 20663 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
20664 if (!type_attr)
20665 {
518817b3 20666 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20667 /* A missing DW_AT_type represents a void type. */
518817b3 20668 return objfile_type (objfile)->builtin_void;
c906108c 20669 }
348e048f 20670
673bfd45 20671 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20672}
20673
b4ba55a1
JB
20674/* True iff CU's producer generates GNAT Ada auxiliary information
20675 that allows to find parallel types through that information instead
20676 of having to do expensive parallel lookups by type name. */
20677
20678static int
20679need_gnat_info (struct dwarf2_cu *cu)
20680{
de4cb04a
JB
20681 /* Assume that the Ada compiler was GNAT, which always produces
20682 the auxiliary information. */
20683 return (cu->language == language_ada);
b4ba55a1
JB
20684}
20685
b4ba55a1
JB
20686/* Return the auxiliary type of the die in question using its
20687 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
20688 attribute is not present. */
20689
20690static struct type *
20691die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
20692{
b4ba55a1 20693 struct attribute *type_attr;
b4ba55a1
JB
20694
20695 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
20696 if (!type_attr)
20697 return NULL;
20698
673bfd45 20699 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
20700}
20701
20702/* If DIE has a descriptive_type attribute, then set the TYPE's
20703 descriptive type accordingly. */
20704
20705static void
20706set_descriptive_type (struct type *type, struct die_info *die,
20707 struct dwarf2_cu *cu)
20708{
20709 struct type *descriptive_type = die_descriptive_type (die, cu);
20710
20711 if (descriptive_type)
20712 {
20713 ALLOCATE_GNAT_AUX_TYPE (type);
20714 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
20715 }
20716}
20717
c906108c
SS
20718/* Return the containing type of the die in question using its
20719 DW_AT_containing_type attribute. */
20720
20721static struct type *
e7c27a73 20722die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20723{
c906108c 20724 struct attribute *type_attr;
518817b3 20725 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 20726
e142c38c 20727 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
20728 if (!type_attr)
20729 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 20730 "[in module %s]"), objfile_name (objfile));
33ac96f0 20731
673bfd45 20732 return lookup_die_type (die, type_attr, cu);
c906108c
SS
20733}
20734
ac9ec31b
DE
20735/* Return an error marker type to use for the ill formed type in DIE/CU. */
20736
20737static struct type *
20738build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
20739{
518817b3
SM
20740 struct dwarf2_per_objfile *dwarf2_per_objfile
20741 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 20742 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 20743 char *saved;
ac9ec31b 20744
528e1572
SM
20745 std::string message
20746 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
20747 objfile_name (objfile),
20748 sect_offset_str (cu->header.sect_off),
20749 sect_offset_str (die->sect_off));
efba19b0 20750 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 20751
19f392bc 20752 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
20753}
20754
673bfd45 20755/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
20756 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
20757 DW_AT_containing_type.
673bfd45
DE
20758 If there is no type substitute an error marker. */
20759
c906108c 20760static struct type *
ff39bb5e 20761lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 20762 struct dwarf2_cu *cu)
c906108c 20763{
518817b3
SM
20764 struct dwarf2_per_objfile *dwarf2_per_objfile
20765 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20766 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
20767 struct type *this_type;
20768
ac9ec31b
DE
20769 gdb_assert (attr->name == DW_AT_type
20770 || attr->name == DW_AT_GNAT_descriptive_type
20771 || attr->name == DW_AT_containing_type);
20772
673bfd45
DE
20773 /* First see if we have it cached. */
20774
36586728
TT
20775 if (attr->form == DW_FORM_GNU_ref_alt)
20776 {
20777 struct dwarf2_per_cu_data *per_cu;
0826b30a 20778 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 20779
ed2dc618
SM
20780 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
20781 dwarf2_per_objfile);
9c541725 20782 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 20783 }
cd6c91b4 20784 else if (attr->form_is_ref ())
673bfd45 20785 {
0826b30a 20786 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 20787
9c541725 20788 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 20789 }
55f1336d 20790 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 20791 {
ac9ec31b 20792 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 20793
ac9ec31b 20794 return get_signatured_type (die, signature, cu);
673bfd45
DE
20795 }
20796 else
20797 {
b98664d3 20798 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
20799 " at %s [in module %s]"),
20800 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 20801 objfile_name (objfile));
ac9ec31b 20802 return build_error_marker_type (cu, die);
673bfd45
DE
20803 }
20804
20805 /* If not cached we need to read it in. */
20806
20807 if (this_type == NULL)
20808 {
ac9ec31b 20809 struct die_info *type_die = NULL;
673bfd45
DE
20810 struct dwarf2_cu *type_cu = cu;
20811
cd6c91b4 20812 if (attr->form_is_ref ())
ac9ec31b
DE
20813 type_die = follow_die_ref (die, attr, &type_cu);
20814 if (type_die == NULL)
20815 return build_error_marker_type (cu, die);
20816 /* If we find the type now, it's probably because the type came
3019eac3
DE
20817 from an inter-CU reference and the type's CU got expanded before
20818 ours. */
ac9ec31b 20819 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
20820 }
20821
20822 /* If we still don't have a type use an error marker. */
20823
20824 if (this_type == NULL)
ac9ec31b 20825 return build_error_marker_type (cu, die);
673bfd45 20826
f792889a 20827 return this_type;
c906108c
SS
20828}
20829
673bfd45
DE
20830/* Return the type in DIE, CU.
20831 Returns NULL for invalid types.
20832
02142a6c 20833 This first does a lookup in die_type_hash,
673bfd45
DE
20834 and only reads the die in if necessary.
20835
20836 NOTE: This can be called when reading in partial or full symbols. */
20837
f792889a 20838static struct type *
e7c27a73 20839read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 20840{
f792889a
DJ
20841 struct type *this_type;
20842
20843 this_type = get_die_type (die, cu);
20844 if (this_type)
20845 return this_type;
20846
673bfd45
DE
20847 return read_type_die_1 (die, cu);
20848}
20849
20850/* Read the type in DIE, CU.
20851 Returns NULL for invalid types. */
20852
20853static struct type *
20854read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
20855{
20856 struct type *this_type = NULL;
20857
c906108c
SS
20858 switch (die->tag)
20859 {
20860 case DW_TAG_class_type:
680b30c7 20861 case DW_TAG_interface_type:
c906108c
SS
20862 case DW_TAG_structure_type:
20863 case DW_TAG_union_type:
f792889a 20864 this_type = read_structure_type (die, cu);
c906108c
SS
20865 break;
20866 case DW_TAG_enumeration_type:
f792889a 20867 this_type = read_enumeration_type (die, cu);
c906108c
SS
20868 break;
20869 case DW_TAG_subprogram:
20870 case DW_TAG_subroutine_type:
edb3359d 20871 case DW_TAG_inlined_subroutine:
f792889a 20872 this_type = read_subroutine_type (die, cu);
c906108c
SS
20873 break;
20874 case DW_TAG_array_type:
f792889a 20875 this_type = read_array_type (die, cu);
c906108c 20876 break;
72019c9c 20877 case DW_TAG_set_type:
f792889a 20878 this_type = read_set_type (die, cu);
72019c9c 20879 break;
c906108c 20880 case DW_TAG_pointer_type:
f792889a 20881 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
20882 break;
20883 case DW_TAG_ptr_to_member_type:
f792889a 20884 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
20885 break;
20886 case DW_TAG_reference_type:
4297a3f0
AV
20887 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
20888 break;
20889 case DW_TAG_rvalue_reference_type:
20890 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
20891 break;
20892 case DW_TAG_const_type:
f792889a 20893 this_type = read_tag_const_type (die, cu);
c906108c
SS
20894 break;
20895 case DW_TAG_volatile_type:
f792889a 20896 this_type = read_tag_volatile_type (die, cu);
c906108c 20897 break;
06d66ee9
TT
20898 case DW_TAG_restrict_type:
20899 this_type = read_tag_restrict_type (die, cu);
20900 break;
c906108c 20901 case DW_TAG_string_type:
f792889a 20902 this_type = read_tag_string_type (die, cu);
c906108c
SS
20903 break;
20904 case DW_TAG_typedef:
f792889a 20905 this_type = read_typedef (die, cu);
c906108c 20906 break;
a02abb62 20907 case DW_TAG_subrange_type:
f792889a 20908 this_type = read_subrange_type (die, cu);
a02abb62 20909 break;
c906108c 20910 case DW_TAG_base_type:
f792889a 20911 this_type = read_base_type (die, cu);
c906108c 20912 break;
81a17f79 20913 case DW_TAG_unspecified_type:
f792889a 20914 this_type = read_unspecified_type (die, cu);
81a17f79 20915 break;
0114d602
DJ
20916 case DW_TAG_namespace:
20917 this_type = read_namespace_type (die, cu);
20918 break;
f55ee35c
JK
20919 case DW_TAG_module:
20920 this_type = read_module_type (die, cu);
20921 break;
a2c2acaf
MW
20922 case DW_TAG_atomic_type:
20923 this_type = read_tag_atomic_type (die, cu);
20924 break;
c906108c 20925 default:
b98664d3 20926 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 20927 dwarf_tag_name (die->tag));
c906108c
SS
20928 break;
20929 }
63d06c5c 20930
f792889a 20931 return this_type;
63d06c5c
DC
20932}
20933
abc72ce4
DE
20934/* See if we can figure out if the class lives in a namespace. We do
20935 this by looking for a member function; its demangled name will
20936 contain namespace info, if there is any.
20937 Return the computed name or NULL.
20938 Space for the result is allocated on the objfile's obstack.
20939 This is the full-die version of guess_partial_die_structure_name.
20940 In this case we know DIE has no useful parent. */
20941
43816ebc 20942static const char *
abc72ce4
DE
20943guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
20944{
20945 struct die_info *spec_die;
20946 struct dwarf2_cu *spec_cu;
20947 struct die_info *child;
518817b3 20948 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
20949
20950 spec_cu = cu;
20951 spec_die = die_specification (die, &spec_cu);
20952 if (spec_die != NULL)
20953 {
20954 die = spec_die;
20955 cu = spec_cu;
20956 }
20957
20958 for (child = die->child;
20959 child != NULL;
20960 child = child->sibling)
20961 {
20962 if (child->tag == DW_TAG_subprogram)
20963 {
73b9be8b 20964 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 20965
7d45c7c3 20966 if (linkage_name != NULL)
abc72ce4 20967 {
43816ebc
TT
20968 gdb::unique_xmalloc_ptr<char> actual_name
20969 (language_class_name_from_physname (cu->language_defn,
20970 linkage_name));
20971 const char *name = NULL;
abc72ce4
DE
20972
20973 if (actual_name != NULL)
20974 {
15d034d0 20975 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
20976
20977 if (die_name != NULL
43816ebc 20978 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
20979 {
20980 /* Strip off the class name from the full name.
20981 We want the prefix. */
20982 int die_name_len = strlen (die_name);
43816ebc
TT
20983 int actual_name_len = strlen (actual_name.get ());
20984 const char *ptr = actual_name.get ();
abc72ce4
DE
20985
20986 /* Test for '::' as a sanity check. */
20987 if (actual_name_len > die_name_len + 2
43816ebc 20988 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 20989 name = obstack_strndup (
e3b94546 20990 &objfile->per_bfd->storage_obstack,
43816ebc 20991 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
20992 }
20993 }
abc72ce4
DE
20994 return name;
20995 }
20996 }
20997 }
20998
20999 return NULL;
21000}
21001
96408a79
SA
21002/* GCC might emit a nameless typedef that has a linkage name. Determine the
21003 prefix part in such case. See
21004 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21005
a121b7c1 21006static const char *
96408a79
SA
21007anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21008{
21009 struct attribute *attr;
e6a959d6 21010 const char *base;
96408a79
SA
21011
21012 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21013 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21014 return NULL;
21015
7d45c7c3 21016 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21017 return NULL;
21018
73b9be8b 21019 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21020 if (attr == NULL || DW_STRING (attr) == NULL)
21021 return NULL;
21022
21023 /* dwarf2_name had to be already called. */
21024 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21025
21026 /* Strip the base name, keep any leading namespaces/classes. */
21027 base = strrchr (DW_STRING (attr), ':');
21028 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21029 return "";
21030
518817b3 21031 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21032 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21033 DW_STRING (attr),
21034 &base[-1] - DW_STRING (attr));
96408a79
SA
21035}
21036
fdde2d81 21037/* Return the name of the namespace/class that DIE is defined within,
0114d602 21038 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21039
0114d602
DJ
21040 For example, if we're within the method foo() in the following
21041 code:
21042
21043 namespace N {
21044 class C {
21045 void foo () {
21046 }
21047 };
21048 }
21049
21050 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21051
0d5cff50 21052static const char *
e142c38c 21053determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21054{
518817b3
SM
21055 struct dwarf2_per_objfile *dwarf2_per_objfile
21056 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21057 struct die_info *parent, *spec_die;
21058 struct dwarf2_cu *spec_cu;
21059 struct type *parent_type;
a121b7c1 21060 const char *retval;
63d06c5c 21061
9c37b5ae 21062 if (cu->language != language_cplus
c44af4eb
TT
21063 && cu->language != language_fortran && cu->language != language_d
21064 && cu->language != language_rust)
0114d602
DJ
21065 return "";
21066
96408a79
SA
21067 retval = anonymous_struct_prefix (die, cu);
21068 if (retval)
21069 return retval;
21070
0114d602
DJ
21071 /* We have to be careful in the presence of DW_AT_specification.
21072 For example, with GCC 3.4, given the code
21073
21074 namespace N {
21075 void foo() {
21076 // Definition of N::foo.
21077 }
21078 }
21079
21080 then we'll have a tree of DIEs like this:
21081
21082 1: DW_TAG_compile_unit
21083 2: DW_TAG_namespace // N
21084 3: DW_TAG_subprogram // declaration of N::foo
21085 4: DW_TAG_subprogram // definition of N::foo
21086 DW_AT_specification // refers to die #3
21087
21088 Thus, when processing die #4, we have to pretend that we're in
21089 the context of its DW_AT_specification, namely the contex of die
21090 #3. */
21091 spec_cu = cu;
21092 spec_die = die_specification (die, &spec_cu);
21093 if (spec_die == NULL)
21094 parent = die->parent;
21095 else
63d06c5c 21096 {
0114d602
DJ
21097 parent = spec_die->parent;
21098 cu = spec_cu;
63d06c5c 21099 }
0114d602
DJ
21100
21101 if (parent == NULL)
21102 return "";
98bfdba5
PA
21103 else if (parent->building_fullname)
21104 {
21105 const char *name;
21106 const char *parent_name;
21107
21108 /* It has been seen on RealView 2.2 built binaries,
21109 DW_TAG_template_type_param types actually _defined_ as
21110 children of the parent class:
21111
21112 enum E {};
21113 template class <class Enum> Class{};
21114 Class<enum E> class_e;
21115
21116 1: DW_TAG_class_type (Class)
21117 2: DW_TAG_enumeration_type (E)
21118 3: DW_TAG_enumerator (enum1:0)
21119 3: DW_TAG_enumerator (enum2:1)
21120 ...
21121 2: DW_TAG_template_type_param
21122 DW_AT_type DW_FORM_ref_udata (E)
21123
21124 Besides being broken debug info, it can put GDB into an
21125 infinite loop. Consider:
21126
21127 When we're building the full name for Class<E>, we'll start
21128 at Class, and go look over its template type parameters,
21129 finding E. We'll then try to build the full name of E, and
21130 reach here. We're now trying to build the full name of E,
21131 and look over the parent DIE for containing scope. In the
21132 broken case, if we followed the parent DIE of E, we'd again
21133 find Class, and once again go look at its template type
21134 arguments, etc., etc. Simply don't consider such parent die
21135 as source-level parent of this die (it can't be, the language
21136 doesn't allow it), and break the loop here. */
21137 name = dwarf2_name (die, cu);
21138 parent_name = dwarf2_name (parent, cu);
b98664d3 21139 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21140 name ? name : "<unknown>",
21141 parent_name ? parent_name : "<unknown>");
21142 return "";
21143 }
63d06c5c 21144 else
0114d602
DJ
21145 switch (parent->tag)
21146 {
63d06c5c 21147 case DW_TAG_namespace:
0114d602 21148 parent_type = read_type_die (parent, cu);
acebe513
UW
21149 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21150 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21151 Work around this problem here. */
21152 if (cu->language == language_cplus
e86ca25f 21153 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21154 return "";
0114d602 21155 /* We give a name to even anonymous namespaces. */
e86ca25f 21156 return TYPE_NAME (parent_type);
63d06c5c 21157 case DW_TAG_class_type:
680b30c7 21158 case DW_TAG_interface_type:
63d06c5c 21159 case DW_TAG_structure_type:
0114d602 21160 case DW_TAG_union_type:
f55ee35c 21161 case DW_TAG_module:
0114d602 21162 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21163 if (TYPE_NAME (parent_type) != NULL)
21164 return TYPE_NAME (parent_type);
0114d602
DJ
21165 else
21166 /* An anonymous structure is only allowed non-static data
21167 members; no typedefs, no member functions, et cetera.
21168 So it does not need a prefix. */
21169 return "";
abc72ce4 21170 case DW_TAG_compile_unit:
95554aad 21171 case DW_TAG_partial_unit:
abc72ce4
DE
21172 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21173 if (cu->language == language_cplus
fd5866f6 21174 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21175 && die->child != NULL
21176 && (die->tag == DW_TAG_class_type
21177 || die->tag == DW_TAG_structure_type
21178 || die->tag == DW_TAG_union_type))
21179 {
43816ebc 21180 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21181 if (name != NULL)
21182 return name;
21183 }
21184 return "";
0a4b0913
AB
21185 case DW_TAG_subprogram:
21186 /* Nested subroutines in Fortran get a prefix with the name
21187 of the parent's subroutine. */
21188 if (cu->language == language_fortran)
21189 {
21190 if ((die->tag == DW_TAG_subprogram)
21191 && (dwarf2_name (parent, cu) != NULL))
21192 return dwarf2_name (parent, cu);
21193 }
21194 return determine_prefix (parent, cu);
3d567982
TT
21195 case DW_TAG_enumeration_type:
21196 parent_type = read_type_die (parent, cu);
21197 if (TYPE_DECLARED_CLASS (parent_type))
21198 {
e86ca25f
TT
21199 if (TYPE_NAME (parent_type) != NULL)
21200 return TYPE_NAME (parent_type);
3d567982
TT
21201 return "";
21202 }
21203 /* Fall through. */
63d06c5c 21204 default:
8176b9b8 21205 return determine_prefix (parent, cu);
63d06c5c 21206 }
63d06c5c
DC
21207}
21208
3e43a32a
MS
21209/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21210 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21211 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21212 an obconcat, otherwise allocate storage for the result. The CU argument is
21213 used to determine the language and hence, the appropriate separator. */
987504bb 21214
f55ee35c 21215#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21216
21217static char *
f55ee35c
JK
21218typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21219 int physname, struct dwarf2_cu *cu)
63d06c5c 21220{
f55ee35c 21221 const char *lead = "";
5c315b68 21222 const char *sep;
63d06c5c 21223
3e43a32a
MS
21224 if (suffix == NULL || suffix[0] == '\0'
21225 || prefix == NULL || prefix[0] == '\0')
987504bb 21226 sep = "";
45280282
IB
21227 else if (cu->language == language_d)
21228 {
21229 /* For D, the 'main' function could be defined in any module, but it
21230 should never be prefixed. */
21231 if (strcmp (suffix, "D main") == 0)
21232 {
21233 prefix = "";
21234 sep = "";
21235 }
21236 else
21237 sep = ".";
21238 }
f55ee35c
JK
21239 else if (cu->language == language_fortran && physname)
21240 {
21241 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21242 DW_AT_MIPS_linkage_name is preferred and used instead. */
21243
21244 lead = "__";
21245 sep = "_MOD_";
21246 }
987504bb
JJ
21247 else
21248 sep = "::";
63d06c5c 21249
6dd47d34
DE
21250 if (prefix == NULL)
21251 prefix = "";
21252 if (suffix == NULL)
21253 suffix = "";
21254
987504bb
JJ
21255 if (obs == NULL)
21256 {
3e43a32a 21257 char *retval
224c3ddb
SM
21258 = ((char *)
21259 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21260
f55ee35c
JK
21261 strcpy (retval, lead);
21262 strcat (retval, prefix);
6dd47d34
DE
21263 strcat (retval, sep);
21264 strcat (retval, suffix);
63d06c5c
DC
21265 return retval;
21266 }
987504bb
JJ
21267 else
21268 {
21269 /* We have an obstack. */
f55ee35c 21270 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21271 }
63d06c5c
DC
21272}
21273
71c25dea
TT
21274/* Get name of a die, return NULL if not found. */
21275
15d034d0
TT
21276static const char *
21277dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21278 struct objfile *objfile)
71c25dea
TT
21279{
21280 if (name && cu->language == language_cplus)
21281 {
2f408ecb 21282 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21283
2f408ecb 21284 if (!canon_name.empty ())
71c25dea 21285 {
2f408ecb 21286 if (canon_name != name)
be1e3d3e 21287 name = objfile->intern (canon_name);
71c25dea
TT
21288 }
21289 }
21290
21291 return name;
c906108c
SS
21292}
21293
96553a0c
DE
21294/* Get name of a die, return NULL if not found.
21295 Anonymous namespaces are converted to their magic string. */
9219021c 21296
15d034d0 21297static const char *
e142c38c 21298dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21299{
21300 struct attribute *attr;
518817b3 21301 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21302
e142c38c 21303 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21304 if ((!attr || !DW_STRING (attr))
96553a0c 21305 && die->tag != DW_TAG_namespace
53832f31
TT
21306 && die->tag != DW_TAG_class_type
21307 && die->tag != DW_TAG_interface_type
21308 && die->tag != DW_TAG_structure_type
21309 && die->tag != DW_TAG_union_type)
71c25dea
TT
21310 return NULL;
21311
21312 switch (die->tag)
21313 {
21314 case DW_TAG_compile_unit:
95554aad 21315 case DW_TAG_partial_unit:
71c25dea
TT
21316 /* Compilation units have a DW_AT_name that is a filename, not
21317 a source language identifier. */
21318 case DW_TAG_enumeration_type:
21319 case DW_TAG_enumerator:
21320 /* These tags always have simple identifiers already; no need
21321 to canonicalize them. */
21322 return DW_STRING (attr);
907af001 21323
96553a0c
DE
21324 case DW_TAG_namespace:
21325 if (attr != NULL && DW_STRING (attr) != NULL)
21326 return DW_STRING (attr);
21327 return CP_ANONYMOUS_NAMESPACE_STR;
21328
907af001
UW
21329 case DW_TAG_class_type:
21330 case DW_TAG_interface_type:
21331 case DW_TAG_structure_type:
21332 case DW_TAG_union_type:
21333 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21334 structures or unions. These were of the form "._%d" in GCC 4.1,
21335 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21336 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21337 if (attr && DW_STRING (attr)
61012eef
GB
21338 && (startswith (DW_STRING (attr), "._")
21339 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21340 return NULL;
53832f31
TT
21341
21342 /* GCC might emit a nameless typedef that has a linkage name. See
21343 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21344 if (!attr || DW_STRING (attr) == NULL)
21345 {
73b9be8b 21346 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21347 if (attr == NULL || DW_STRING (attr) == NULL)
21348 return NULL;
21349
df5c6c50
JK
21350 /* Avoid demangling DW_STRING (attr) the second time on a second
21351 call for the same DIE. */
21352 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21353 {
43816ebc
TT
21354 gdb::unique_xmalloc_ptr<char> demangled
21355 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21356 if (demangled == nullptr)
21357 return nullptr;
43816ebc 21358
be1e3d3e 21359 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21360 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21361 }
67430cd0
TT
21362
21363 /* Strip any leading namespaces/classes, keep only the base name.
21364 DW_AT_name for named DIEs does not contain the prefixes. */
21365 const char *base = strrchr (DW_STRING (attr), ':');
21366 if (base && base > DW_STRING (attr) && base[-1] == ':')
21367 return &base[1];
21368 else
21369 return DW_STRING (attr);
53832f31 21370 }
907af001
UW
21371 break;
21372
71c25dea 21373 default:
907af001
UW
21374 break;
21375 }
21376
21377 if (!DW_STRING_IS_CANONICAL (attr))
21378 {
be1e3d3e
TT
21379 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21380 objfile);
907af001 21381 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21382 }
907af001 21383 return DW_STRING (attr);
9219021c
DC
21384}
21385
21386/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21387 is none. *EXT_CU is the CU containing DIE on input, and the CU
21388 containing the return value on output. */
9219021c
DC
21389
21390static struct die_info *
f2f0e013 21391dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21392{
21393 struct attribute *attr;
9219021c 21394
f2f0e013 21395 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21396 if (attr == NULL)
21397 return NULL;
21398
f2f0e013 21399 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21400}
21401
f9aca02d 21402static void
d97bc12b 21403dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21404{
21405 unsigned int i;
21406
d97bc12b 21407 print_spaces (indent, f);
9d8780f0 21408 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21409 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21410 sect_offset_str (die->sect_off));
d97bc12b
DE
21411
21412 if (die->parent != NULL)
21413 {
21414 print_spaces (indent, f);
9d8780f0
SM
21415 fprintf_unfiltered (f, " parent at offset: %s\n",
21416 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21417 }
21418
21419 print_spaces (indent, f);
21420 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21421 dwarf_bool_name (die->child != NULL));
c906108c 21422
d97bc12b
DE
21423 print_spaces (indent, f);
21424 fprintf_unfiltered (f, " attributes:\n");
21425
c906108c
SS
21426 for (i = 0; i < die->num_attrs; ++i)
21427 {
d97bc12b
DE
21428 print_spaces (indent, f);
21429 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21430 dwarf_attr_name (die->attrs[i].name),
21431 dwarf_form_name (die->attrs[i].form));
d97bc12b 21432
c906108c
SS
21433 switch (die->attrs[i].form)
21434 {
c906108c 21435 case DW_FORM_addr:
336d760d 21436 case DW_FORM_addrx:
3019eac3 21437 case DW_FORM_GNU_addr_index:
d97bc12b 21438 fprintf_unfiltered (f, "address: ");
5af949e3 21439 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21440 break;
21441 case DW_FORM_block2:
21442 case DW_FORM_block4:
21443 case DW_FORM_block:
21444 case DW_FORM_block1:
56eb65bd
SP
21445 fprintf_unfiltered (f, "block: size %s",
21446 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21447 break;
2dc7f7b3 21448 case DW_FORM_exprloc:
56eb65bd
SP
21449 fprintf_unfiltered (f, "expression: size %s",
21450 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21451 break;
0224619f
JK
21452 case DW_FORM_data16:
21453 fprintf_unfiltered (f, "constant of 16 bytes");
21454 break;
4568ecf9
DE
21455 case DW_FORM_ref_addr:
21456 fprintf_unfiltered (f, "ref address: ");
21457 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21458 break;
36586728
TT
21459 case DW_FORM_GNU_ref_alt:
21460 fprintf_unfiltered (f, "alt ref address: ");
21461 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21462 break;
10b3939b
DJ
21463 case DW_FORM_ref1:
21464 case DW_FORM_ref2:
21465 case DW_FORM_ref4:
4568ecf9
DE
21466 case DW_FORM_ref8:
21467 case DW_FORM_ref_udata:
d97bc12b 21468 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21469 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21470 break;
c906108c
SS
21471 case DW_FORM_data1:
21472 case DW_FORM_data2:
21473 case DW_FORM_data4:
ce5d95e1 21474 case DW_FORM_data8:
c906108c
SS
21475 case DW_FORM_udata:
21476 case DW_FORM_sdata:
43bbcdc2
PH
21477 fprintf_unfiltered (f, "constant: %s",
21478 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21479 break;
2dc7f7b3
TT
21480 case DW_FORM_sec_offset:
21481 fprintf_unfiltered (f, "section offset: %s",
21482 pulongest (DW_UNSND (&die->attrs[i])));
21483 break;
55f1336d 21484 case DW_FORM_ref_sig8:
ac9ec31b
DE
21485 fprintf_unfiltered (f, "signature: %s",
21486 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21487 break;
c906108c 21488 case DW_FORM_string:
4bdf3d34 21489 case DW_FORM_strp:
43988095 21490 case DW_FORM_line_strp:
cf532bd1 21491 case DW_FORM_strx:
3019eac3 21492 case DW_FORM_GNU_str_index:
36586728 21493 case DW_FORM_GNU_strp_alt:
8285870a 21494 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21495 DW_STRING (&die->attrs[i])
8285870a
JK
21496 ? DW_STRING (&die->attrs[i]) : "",
21497 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21498 break;
21499 case DW_FORM_flag:
21500 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21501 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21502 else
d97bc12b 21503 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21504 break;
2dc7f7b3
TT
21505 case DW_FORM_flag_present:
21506 fprintf_unfiltered (f, "flag: TRUE");
21507 break;
a8329558 21508 case DW_FORM_indirect:
0963b4bd
MS
21509 /* The reader will have reduced the indirect form to
21510 the "base form" so this form should not occur. */
5f48f8f3 21511 fprintf_unfiltered (f,
3e43a32a 21512 "unexpected attribute form: DW_FORM_indirect");
a8329558 21513 break;
663c44ac
JK
21514 case DW_FORM_implicit_const:
21515 fprintf_unfiltered (f, "constant: %s",
21516 plongest (DW_SND (&die->attrs[i])));
21517 break;
c906108c 21518 default:
d97bc12b 21519 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21520 die->attrs[i].form);
d97bc12b 21521 break;
c906108c 21522 }
d97bc12b 21523 fprintf_unfiltered (f, "\n");
c906108c
SS
21524 }
21525}
21526
f9aca02d 21527static void
d97bc12b 21528dump_die_for_error (struct die_info *die)
c906108c 21529{
d97bc12b
DE
21530 dump_die_shallow (gdb_stderr, 0, die);
21531}
21532
21533static void
21534dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21535{
21536 int indent = level * 4;
21537
21538 gdb_assert (die != NULL);
21539
21540 if (level >= max_level)
21541 return;
21542
21543 dump_die_shallow (f, indent, die);
21544
21545 if (die->child != NULL)
c906108c 21546 {
d97bc12b
DE
21547 print_spaces (indent, f);
21548 fprintf_unfiltered (f, " Children:");
21549 if (level + 1 < max_level)
21550 {
21551 fprintf_unfiltered (f, "\n");
21552 dump_die_1 (f, level + 1, max_level, die->child);
21553 }
21554 else
21555 {
3e43a32a
MS
21556 fprintf_unfiltered (f,
21557 " [not printed, max nesting level reached]\n");
d97bc12b
DE
21558 }
21559 }
21560
21561 if (die->sibling != NULL && level > 0)
21562 {
21563 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
21564 }
21565}
21566
d97bc12b
DE
21567/* This is called from the pdie macro in gdbinit.in.
21568 It's not static so gcc will keep a copy callable from gdb. */
21569
21570void
21571dump_die (struct die_info *die, int max_level)
21572{
21573 dump_die_1 (gdb_stdlog, 0, max_level, die);
21574}
21575
f9aca02d 21576static void
51545339 21577store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21578{
51545339 21579 void **slot;
c906108c 21580
9c541725
PA
21581 slot = htab_find_slot_with_hash (cu->die_hash, die,
21582 to_underlying (die->sect_off),
b64f50a1 21583 INSERT);
51545339
DJ
21584
21585 *slot = die;
c906108c
SS
21586}
21587
348e048f
DE
21588/* Follow reference or signature attribute ATTR of SRC_DIE.
21589 On entry *REF_CU is the CU of SRC_DIE.
21590 On exit *REF_CU is the CU of the result. */
21591
21592static struct die_info *
ff39bb5e 21593follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
21594 struct dwarf2_cu **ref_cu)
21595{
21596 struct die_info *die;
21597
cd6c91b4 21598 if (attr->form_is_ref ())
348e048f 21599 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 21600 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
21601 die = follow_die_sig (src_die, attr, ref_cu);
21602 else
21603 {
21604 dump_die_for_error (src_die);
21605 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 21606 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
21607 }
21608
21609 return die;
03dd20cc
DJ
21610}
21611
5c631832 21612/* Follow reference OFFSET.
673bfd45
DE
21613 On entry *REF_CU is the CU of the source die referencing OFFSET.
21614 On exit *REF_CU is the CU of the result.
21615 Returns NULL if OFFSET is invalid. */
f504f079 21616
f9aca02d 21617static struct die_info *
9c541725 21618follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 21619 struct dwarf2_cu **ref_cu)
c906108c 21620{
10b3939b 21621 struct die_info temp_die;
f2f0e013 21622 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
21623 struct dwarf2_per_objfile *dwarf2_per_objfile
21624 = cu->per_cu->dwarf2_per_objfile;
10b3939b 21625
348e048f
DE
21626 gdb_assert (cu->per_cu != NULL);
21627
98bfdba5
PA
21628 target_cu = cu;
21629
3019eac3 21630 if (cu->per_cu->is_debug_types)
348e048f
DE
21631 {
21632 /* .debug_types CUs cannot reference anything outside their CU.
21633 If they need to, they have to reference a signatured type via
55f1336d 21634 DW_FORM_ref_sig8. */
4057dfde 21635 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 21636 return NULL;
348e048f 21637 }
36586728 21638 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 21639 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
21640 {
21641 struct dwarf2_per_cu_data *per_cu;
9a619af0 21642
9c541725 21643 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 21644 dwarf2_per_objfile);
03dd20cc
DJ
21645
21646 /* If necessary, add it to the queue and load its DIEs. */
95554aad 21647 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 21648 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 21649
10b3939b
DJ
21650 target_cu = per_cu->cu;
21651 }
98bfdba5
PA
21652 else if (cu->dies == NULL)
21653 {
21654 /* We're loading full DIEs during partial symbol reading. */
21655 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 21656 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 21657 }
c906108c 21658
f2f0e013 21659 *ref_cu = target_cu;
9c541725 21660 temp_die.sect_off = sect_off;
c24bdb02
KS
21661
21662 if (target_cu != cu)
21663 target_cu->ancestor = cu;
21664
9a3c8263 21665 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
21666 &temp_die,
21667 to_underlying (sect_off));
5c631832 21668}
10b3939b 21669
5c631832
JK
21670/* Follow reference attribute ATTR of SRC_DIE.
21671 On entry *REF_CU is the CU of SRC_DIE.
21672 On exit *REF_CU is the CU of the result. */
21673
21674static struct die_info *
ff39bb5e 21675follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
21676 struct dwarf2_cu **ref_cu)
21677{
0826b30a 21678 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
21679 struct dwarf2_cu *cu = *ref_cu;
21680 struct die_info *die;
21681
9c541725 21682 die = follow_die_offset (sect_off,
36586728
TT
21683 (attr->form == DW_FORM_GNU_ref_alt
21684 || cu->per_cu->is_dwz),
21685 ref_cu);
5c631832 21686 if (!die)
9d8780f0
SM
21687 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
21688 "at %s [in module %s]"),
21689 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 21690 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 21691
5c631832
JK
21692 return die;
21693}
21694
d4c9a4f8 21695/* See read.h. */
5c631832
JK
21696
21697struct dwarf2_locexpr_baton
9c541725 21698dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 21699 dwarf2_per_cu_data *per_cu,
8b9737bf 21700 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 21701 void *baton, bool resolve_abstract_p)
5c631832 21702{
918dd910 21703 struct dwarf2_cu *cu;
5c631832
JK
21704 struct die_info *die;
21705 struct attribute *attr;
21706 struct dwarf2_locexpr_baton retval;
12359b5e
SM
21707 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
21708 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 21709
918dd910 21710 if (per_cu->cu == NULL)
58f0c718 21711 load_cu (per_cu, false);
918dd910 21712 cu = per_cu->cu;
cc12ce38
DE
21713 if (cu == NULL)
21714 {
21715 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21716 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21717 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21718 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21719 }
918dd910 21720
9c541725 21721 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 21722 if (!die)
9d8780f0
SM
21723 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21724 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21725
21726 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 21727 if (!attr && resolve_abstract_p
3360b6e7 21728 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
21729 != dwarf2_per_objfile->abstract_to_concrete.end ()))
21730 {
21731 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 21732 CORE_ADDR baseaddr = objfile->text_section_offset ();
eba4caf2 21733 struct gdbarch *gdbarch = get_objfile_arch (objfile);
e4a62c65 21734
3360b6e7
TV
21735 for (const auto &cand_off
21736 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 21737 {
3360b6e7
TV
21738 struct dwarf2_cu *cand_cu = cu;
21739 struct die_info *cand
21740 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
21741 if (!cand
21742 || !cand->parent
e4a62c65
TV
21743 || cand->parent->tag != DW_TAG_subprogram)
21744 continue;
21745
21746 CORE_ADDR pc_low, pc_high;
21747 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
21748 if (pc_low == ((CORE_ADDR) -1))
21749 continue;
21750 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
21751 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
21752 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
21753 continue;
21754
21755 die = cand;
21756 attr = dwarf2_attr (die, DW_AT_location, cu);
21757 break;
21758 }
21759 }
21760
5c631832
JK
21761 if (!attr)
21762 {
e103e986
JK
21763 /* DWARF: "If there is no such attribute, then there is no effect.".
21764 DATA is ignored if SIZE is 0. */
5c631832 21765
e103e986 21766 retval.data = NULL;
5c631832
JK
21767 retval.size = 0;
21768 }
cd6c91b4 21769 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
21770 {
21771 struct dwarf2_loclist_baton loclist_baton;
21772 CORE_ADDR pc = (*get_frame_pc) (baton);
21773 size_t size;
21774
21775 fill_in_loclist_baton (cu, &loclist_baton, attr);
21776
21777 retval.data = dwarf2_find_location_expression (&loclist_baton,
21778 &size, pc);
21779 retval.size = size;
21780 }
5c631832
JK
21781 else
21782 {
4fc6c0d5 21783 if (!attr->form_is_block ())
9d8780f0 21784 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 21785 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 21786 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
21787
21788 retval.data = DW_BLOCK (attr)->data;
21789 retval.size = DW_BLOCK (attr)->size;
21790 }
21791 retval.per_cu = cu->per_cu;
918dd910 21792
ed2dc618 21793 age_cached_comp_units (dwarf2_per_objfile);
918dd910 21794
5c631832 21795 return retval;
348e048f
DE
21796}
21797
d4c9a4f8 21798/* See read.h. */
8b9737bf
TT
21799
21800struct dwarf2_locexpr_baton
21801dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 21802 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
21803 CORE_ADDR (*get_frame_pc) (void *baton),
21804 void *baton)
21805{
9c541725 21806 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 21807
9c541725 21808 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
21809}
21810
b6807d98
TT
21811/* Write a constant of a given type as target-ordered bytes into
21812 OBSTACK. */
21813
21814static const gdb_byte *
21815write_constant_as_bytes (struct obstack *obstack,
21816 enum bfd_endian byte_order,
21817 struct type *type,
21818 ULONGEST value,
21819 LONGEST *len)
21820{
21821 gdb_byte *result;
21822
21823 *len = TYPE_LENGTH (type);
224c3ddb 21824 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21825 store_unsigned_integer (result, *len, byte_order, value);
21826
21827 return result;
21828}
21829
d4c9a4f8 21830/* See read.h. */
b6807d98
TT
21831
21832const gdb_byte *
9c541725 21833dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
21834 dwarf2_per_cu_data *per_cu,
21835 obstack *obstack,
b6807d98
TT
21836 LONGEST *len)
21837{
21838 struct dwarf2_cu *cu;
21839 struct die_info *die;
21840 struct attribute *attr;
21841 const gdb_byte *result = NULL;
21842 struct type *type;
21843 LONGEST value;
21844 enum bfd_endian byte_order;
e3b94546 21845 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 21846
b6807d98 21847 if (per_cu->cu == NULL)
58f0c718 21848 load_cu (per_cu, false);
b6807d98 21849 cu = per_cu->cu;
cc12ce38
DE
21850 if (cu == NULL)
21851 {
21852 /* We shouldn't get here for a dummy CU, but don't crash on the user.
21853 Instead just throw an error, not much else we can do. */
9d8780f0
SM
21854 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
21855 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 21856 }
b6807d98 21857
9c541725 21858 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 21859 if (!die)
9d8780f0
SM
21860 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
21861 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
21862
21863 attr = dwarf2_attr (die, DW_AT_const_value, cu);
21864 if (attr == NULL)
21865 return NULL;
21866
e3b94546 21867 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
21868 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21869
21870 switch (attr->form)
21871 {
21872 case DW_FORM_addr:
336d760d 21873 case DW_FORM_addrx:
b6807d98
TT
21874 case DW_FORM_GNU_addr_index:
21875 {
21876 gdb_byte *tem;
21877
21878 *len = cu->header.addr_size;
224c3ddb 21879 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
21880 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
21881 result = tem;
21882 }
21883 break;
21884 case DW_FORM_string:
21885 case DW_FORM_strp:
cf532bd1 21886 case DW_FORM_strx:
b6807d98
TT
21887 case DW_FORM_GNU_str_index:
21888 case DW_FORM_GNU_strp_alt:
21889 /* DW_STRING is already allocated on the objfile obstack, point
21890 directly to it. */
21891 result = (const gdb_byte *) DW_STRING (attr);
21892 *len = strlen (DW_STRING (attr));
21893 break;
21894 case DW_FORM_block1:
21895 case DW_FORM_block2:
21896 case DW_FORM_block4:
21897 case DW_FORM_block:
21898 case DW_FORM_exprloc:
0224619f 21899 case DW_FORM_data16:
b6807d98
TT
21900 result = DW_BLOCK (attr)->data;
21901 *len = DW_BLOCK (attr)->size;
21902 break;
21903
21904 /* The DW_AT_const_value attributes are supposed to carry the
21905 symbol's value "represented as it would be on the target
21906 architecture." By the time we get here, it's already been
21907 converted to host endianness, so we just need to sign- or
21908 zero-extend it as appropriate. */
21909 case DW_FORM_data1:
21910 type = die_type (die, cu);
21911 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
21912 if (result == NULL)
21913 result = write_constant_as_bytes (obstack, byte_order,
21914 type, value, len);
21915 break;
21916 case DW_FORM_data2:
21917 type = die_type (die, cu);
21918 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
21919 if (result == NULL)
21920 result = write_constant_as_bytes (obstack, byte_order,
21921 type, value, len);
21922 break;
21923 case DW_FORM_data4:
21924 type = die_type (die, cu);
21925 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
21926 if (result == NULL)
21927 result = write_constant_as_bytes (obstack, byte_order,
21928 type, value, len);
21929 break;
21930 case DW_FORM_data8:
21931 type = die_type (die, cu);
21932 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
21933 if (result == NULL)
21934 result = write_constant_as_bytes (obstack, byte_order,
21935 type, value, len);
21936 break;
21937
21938 case DW_FORM_sdata:
663c44ac 21939 case DW_FORM_implicit_const:
b6807d98
TT
21940 type = die_type (die, cu);
21941 result = write_constant_as_bytes (obstack, byte_order,
21942 type, DW_SND (attr), len);
21943 break;
21944
21945 case DW_FORM_udata:
21946 type = die_type (die, cu);
21947 result = write_constant_as_bytes (obstack, byte_order,
21948 type, DW_UNSND (attr), len);
21949 break;
21950
21951 default:
b98664d3 21952 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
21953 dwarf_form_name (attr->form));
21954 break;
21955 }
21956
21957 return result;
21958}
21959
d4c9a4f8 21960/* See read.h. */
7942e96e
AA
21961
21962struct type *
9c541725 21963dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 21964 dwarf2_per_cu_data *per_cu)
7942e96e
AA
21965{
21966 struct dwarf2_cu *cu;
21967 struct die_info *die;
21968
7942e96e 21969 if (per_cu->cu == NULL)
58f0c718 21970 load_cu (per_cu, false);
7942e96e
AA
21971 cu = per_cu->cu;
21972 if (!cu)
21973 return NULL;
21974
9c541725 21975 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
21976 if (!die)
21977 return NULL;
21978
21979 return die_type (die, cu);
21980}
21981
8cb5117c 21982/* See read.h. */
8a9b8146
TT
21983
21984struct type *
b64f50a1 21985dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
21986 struct dwarf2_per_cu_data *per_cu)
21987{
9c541725 21988 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 21989 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
21990}
21991
ac9ec31b 21992/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 21993 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
21994 On exit *REF_CU is the CU of the result.
21995 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
21996
21997static struct die_info *
ac9ec31b
DE
21998follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
21999 struct dwarf2_cu **ref_cu)
348e048f 22000{
348e048f 22001 struct die_info temp_die;
c24bdb02 22002 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22003 struct die_info *die;
22004
ac9ec31b
DE
22005 /* While it might be nice to assert sig_type->type == NULL here,
22006 we can get here for DW_AT_imported_declaration where we need
22007 the DIE not the type. */
348e048f
DE
22008
22009 /* If necessary, add it to the queue and load its DIEs. */
22010
95554aad 22011 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22012 read_signatured_type (sig_type);
348e048f 22013
348e048f 22014 sig_cu = sig_type->per_cu.cu;
69d751e3 22015 gdb_assert (sig_cu != NULL);
9c541725
PA
22016 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22017 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22018 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22019 to_underlying (temp_die.sect_off));
348e048f
DE
22020 if (die)
22021 {
ed2dc618 22022 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22023 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22024
796a7ff8
DE
22025 /* For .gdb_index version 7 keep track of included TUs.
22026 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22027 if (dwarf2_per_objfile->index_table != NULL
22028 && dwarf2_per_objfile->index_table->version <= 7)
22029 {
ae640021 22030 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22031 }
22032
348e048f 22033 *ref_cu = sig_cu;
c24bdb02
KS
22034 if (sig_cu != cu)
22035 sig_cu->ancestor = cu;
22036
348e048f
DE
22037 return die;
22038 }
22039
ac9ec31b
DE
22040 return NULL;
22041}
22042
22043/* Follow signatured type referenced by ATTR in SRC_DIE.
22044 On entry *REF_CU is the CU of SRC_DIE.
22045 On exit *REF_CU is the CU of the result.
22046 The result is the DIE of the type.
22047 If the referenced type cannot be found an error is thrown. */
22048
22049static struct die_info *
ff39bb5e 22050follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22051 struct dwarf2_cu **ref_cu)
22052{
22053 ULONGEST signature = DW_SIGNATURE (attr);
22054 struct signatured_type *sig_type;
22055 struct die_info *die;
22056
22057 gdb_assert (attr->form == DW_FORM_ref_sig8);
22058
a2ce51a0 22059 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22060 /* sig_type will be NULL if the signatured type is missing from
22061 the debug info. */
22062 if (sig_type == NULL)
22063 {
22064 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22065 " from DIE at %s [in module %s]"),
22066 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22067 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22068 }
22069
22070 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22071 if (die == NULL)
22072 {
22073 dump_die_for_error (src_die);
22074 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22075 " from DIE at %s [in module %s]"),
22076 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22077 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22078 }
22079
22080 return die;
22081}
22082
22083/* Get the type specified by SIGNATURE referenced in DIE/CU,
22084 reading in and processing the type unit if necessary. */
22085
22086static struct type *
22087get_signatured_type (struct die_info *die, ULONGEST signature,
22088 struct dwarf2_cu *cu)
22089{
518817b3
SM
22090 struct dwarf2_per_objfile *dwarf2_per_objfile
22091 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22092 struct signatured_type *sig_type;
22093 struct dwarf2_cu *type_cu;
22094 struct die_info *type_die;
22095 struct type *type;
22096
a2ce51a0 22097 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22098 /* sig_type will be NULL if the signatured type is missing from
22099 the debug info. */
22100 if (sig_type == NULL)
22101 {
b98664d3 22102 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22103 " from DIE at %s [in module %s]"),
22104 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22105 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22106 return build_error_marker_type (cu, die);
22107 }
22108
22109 /* If we already know the type we're done. */
22110 if (sig_type->type != NULL)
22111 return sig_type->type;
22112
22113 type_cu = cu;
22114 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22115 if (type_die != NULL)
22116 {
22117 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22118 is created. This is important, for example, because for c++ classes
22119 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22120 type = read_type_die (type_die, type_cu);
22121 if (type == NULL)
22122 {
b98664d3 22123 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22124 " referenced from DIE at %s [in module %s]"),
22125 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22126 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22127 type = build_error_marker_type (cu, die);
22128 }
22129 }
22130 else
22131 {
b98664d3 22132 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22133 " from DIE at %s [in module %s]"),
22134 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22135 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22136 type = build_error_marker_type (cu, die);
22137 }
22138 sig_type->type = type;
22139
22140 return type;
22141}
22142
22143/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22144 reading in and processing the type unit if necessary. */
22145
22146static struct type *
ff39bb5e 22147get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22148 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22149{
22150 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22151 if (attr->form_is_ref ())
ac9ec31b
DE
22152 {
22153 struct dwarf2_cu *type_cu = cu;
22154 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22155
22156 return read_type_die (type_die, type_cu);
22157 }
22158 else if (attr->form == DW_FORM_ref_sig8)
22159 {
22160 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22161 }
22162 else
22163 {
518817b3
SM
22164 struct dwarf2_per_objfile *dwarf2_per_objfile
22165 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22166
b98664d3 22167 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22168 " at %s [in module %s]"),
22169 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22170 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22171 return build_error_marker_type (cu, die);
22172 }
348e048f
DE
22173}
22174
e5fe5e75 22175/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22176
22177static void
e5fe5e75 22178load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22179{
52dc124a 22180 struct signatured_type *sig_type;
348e048f 22181
f4dc4d17 22182 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22183 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22184
6721b2ec
DE
22185 /* We have the per_cu, but we need the signatured_type.
22186 Fortunately this is an easy translation. */
22187 gdb_assert (per_cu->is_debug_types);
22188 sig_type = (struct signatured_type *) per_cu;
348e048f 22189
6721b2ec 22190 gdb_assert (per_cu->cu == NULL);
348e048f 22191
52dc124a 22192 read_signatured_type (sig_type);
348e048f 22193
6721b2ec 22194 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22195}
22196
3019eac3
DE
22197/* Read in a signatured type and build its CU and DIEs.
22198 If the type is a stub for the real type in a DWO file,
22199 read in the real type from the DWO file as well. */
dee91e82
DE
22200
22201static void
22202read_signatured_type (struct signatured_type *sig_type)
22203{
22204 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22205
3019eac3 22206 gdb_assert (per_cu->is_debug_types);
dee91e82 22207 gdb_assert (per_cu->cu == NULL);
348e048f 22208
6751ebae 22209 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22210
22211 if (!reader.dummy_p)
22212 {
22213 struct dwarf2_cu *cu = reader.cu;
22214 const gdb_byte *info_ptr = reader.info_ptr;
22215
22216 gdb_assert (cu->die_hash == NULL);
22217 cu->die_hash =
22218 htab_create_alloc_ex (cu->header.length / 12,
22219 die_hash,
22220 die_eq,
22221 NULL,
22222 &cu->comp_unit_obstack,
22223 hashtab_obstack_allocate,
22224 dummy_obstack_deallocate);
22225
3e225074 22226 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22227 reader.comp_unit_die->child
22228 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22229 reader.comp_unit_die);
22230 cu->dies = reader.comp_unit_die;
22231 /* comp_unit_die is not stored in die_hash, no need. */
22232
22233 /* We try not to read any attributes in this function, because
22234 not all CUs needed for references have been loaded yet, and
22235 symbol table processing isn't initialized. But we have to
22236 set the CU language, or we won't be able to build types
22237 correctly. Similarly, if we do not read the producer, we can
22238 not apply producer-specific interpretation. */
22239 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22240
22241 reader.keep ();
c0ab21c2
TT
22242 }
22243
7ee85ab1 22244 sig_type->per_cu.tu_read = 1;
c906108c
SS
22245}
22246
c906108c
SS
22247/* Decode simple location descriptions.
22248 Given a pointer to a dwarf block that defines a location, compute
22249 the location and return the value.
22250
4cecd739
DJ
22251 NOTE drow/2003-11-18: This function is called in two situations
22252 now: for the address of static or global variables (partial symbols
22253 only) and for offsets into structures which are expected to be
22254 (more or less) constant. The partial symbol case should go away,
22255 and only the constant case should remain. That will let this
22256 function complain more accurately. A few special modes are allowed
22257 without complaint for global variables (for instance, global
22258 register values and thread-local values).
c906108c
SS
22259
22260 A location description containing no operations indicates that the
4cecd739 22261 object is optimized out. The return value is 0 for that case.
6b992462
DJ
22262 FIXME drow/2003-11-16: No callers check for this case any more; soon all
22263 callers will only want a very basic result and this can become a
21ae7a4d
JK
22264 complaint.
22265
22266 Note that stack[0] is unused except as a default error return. */
c906108c
SS
22267
22268static CORE_ADDR
e7c27a73 22269decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
c906108c 22270{
518817b3 22271 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22272 size_t i;
22273 size_t size = blk->size;
d521ce57 22274 const gdb_byte *data = blk->data;
21ae7a4d
JK
22275 CORE_ADDR stack[64];
22276 int stacki;
22277 unsigned int bytes_read, unsnd;
22278 gdb_byte op;
c906108c 22279
21ae7a4d
JK
22280 i = 0;
22281 stacki = 0;
22282 stack[stacki] = 0;
22283 stack[++stacki] = 0;
22284
22285 while (i < size)
22286 {
22287 op = data[i++];
22288 switch (op)
22289 {
22290 case DW_OP_lit0:
22291 case DW_OP_lit1:
22292 case DW_OP_lit2:
22293 case DW_OP_lit3:
22294 case DW_OP_lit4:
22295 case DW_OP_lit5:
22296 case DW_OP_lit6:
22297 case DW_OP_lit7:
22298 case DW_OP_lit8:
22299 case DW_OP_lit9:
22300 case DW_OP_lit10:
22301 case DW_OP_lit11:
22302 case DW_OP_lit12:
22303 case DW_OP_lit13:
22304 case DW_OP_lit14:
22305 case DW_OP_lit15:
22306 case DW_OP_lit16:
22307 case DW_OP_lit17:
22308 case DW_OP_lit18:
22309 case DW_OP_lit19:
22310 case DW_OP_lit20:
22311 case DW_OP_lit21:
22312 case DW_OP_lit22:
22313 case DW_OP_lit23:
22314 case DW_OP_lit24:
22315 case DW_OP_lit25:
22316 case DW_OP_lit26:
22317 case DW_OP_lit27:
22318 case DW_OP_lit28:
22319 case DW_OP_lit29:
22320 case DW_OP_lit30:
22321 case DW_OP_lit31:
22322 stack[++stacki] = op - DW_OP_lit0;
22323 break;
f1bea926 22324
21ae7a4d
JK
22325 case DW_OP_reg0:
22326 case DW_OP_reg1:
22327 case DW_OP_reg2:
22328 case DW_OP_reg3:
22329 case DW_OP_reg4:
22330 case DW_OP_reg5:
22331 case DW_OP_reg6:
22332 case DW_OP_reg7:
22333 case DW_OP_reg8:
22334 case DW_OP_reg9:
22335 case DW_OP_reg10:
22336 case DW_OP_reg11:
22337 case DW_OP_reg12:
22338 case DW_OP_reg13:
22339 case DW_OP_reg14:
22340 case DW_OP_reg15:
22341 case DW_OP_reg16:
22342 case DW_OP_reg17:
22343 case DW_OP_reg18:
22344 case DW_OP_reg19:
22345 case DW_OP_reg20:
22346 case DW_OP_reg21:
22347 case DW_OP_reg22:
22348 case DW_OP_reg23:
22349 case DW_OP_reg24:
22350 case DW_OP_reg25:
22351 case DW_OP_reg26:
22352 case DW_OP_reg27:
22353 case DW_OP_reg28:
22354 case DW_OP_reg29:
22355 case DW_OP_reg30:
22356 case DW_OP_reg31:
22357 stack[++stacki] = op - DW_OP_reg0;
22358 if (i < size)
22359 dwarf2_complex_location_expr_complaint ();
22360 break;
c906108c 22361
21ae7a4d
JK
22362 case DW_OP_regx:
22363 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22364 i += bytes_read;
22365 stack[++stacki] = unsnd;
22366 if (i < size)
22367 dwarf2_complex_location_expr_complaint ();
22368 break;
c906108c 22369
21ae7a4d 22370 case DW_OP_addr:
c8a7a66f
TT
22371 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22372 &bytes_read);
21ae7a4d
JK
22373 i += bytes_read;
22374 break;
d53d4ac5 22375
21ae7a4d
JK
22376 case DW_OP_const1u:
22377 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22378 i += 1;
22379 break;
22380
22381 case DW_OP_const1s:
22382 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22383 i += 1;
22384 break;
22385
22386 case DW_OP_const2u:
22387 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22388 i += 2;
22389 break;
22390
22391 case DW_OP_const2s:
22392 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22393 i += 2;
22394 break;
d53d4ac5 22395
21ae7a4d
JK
22396 case DW_OP_const4u:
22397 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22398 i += 4;
22399 break;
22400
22401 case DW_OP_const4s:
22402 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22403 i += 4;
22404 break;
22405
585861ea
JK
22406 case DW_OP_const8u:
22407 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22408 i += 8;
22409 break;
22410
21ae7a4d
JK
22411 case DW_OP_constu:
22412 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22413 &bytes_read);
22414 i += bytes_read;
22415 break;
22416
22417 case DW_OP_consts:
22418 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22419 i += bytes_read;
22420 break;
22421
22422 case DW_OP_dup:
22423 stack[stacki + 1] = stack[stacki];
22424 stacki++;
22425 break;
22426
22427 case DW_OP_plus:
22428 stack[stacki - 1] += stack[stacki];
22429 stacki--;
22430 break;
22431
22432 case DW_OP_plus_uconst:
22433 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22434 &bytes_read);
22435 i += bytes_read;
22436 break;
22437
22438 case DW_OP_minus:
22439 stack[stacki - 1] -= stack[stacki];
22440 stacki--;
22441 break;
22442
22443 case DW_OP_deref:
22444 /* If we're not the last op, then we definitely can't encode
22445 this using GDB's address_class enum. This is valid for partial
22446 global symbols, although the variable's address will be bogus
22447 in the psymtab. */
22448 if (i < size)
22449 dwarf2_complex_location_expr_complaint ();
22450 break;
22451
22452 case DW_OP_GNU_push_tls_address:
4aa4e28b 22453 case DW_OP_form_tls_address:
21ae7a4d
JK
22454 /* The top of the stack has the offset from the beginning
22455 of the thread control block at which the variable is located. */
22456 /* Nothing should follow this operator, so the top of stack would
22457 be returned. */
22458 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22459 address will be bogus in the psymtab. Make it always at least
22460 non-zero to not look as a variable garbage collected by linker
22461 which have DW_OP_addr 0. */
21ae7a4d
JK
22462 if (i < size)
22463 dwarf2_complex_location_expr_complaint ();
585861ea 22464 stack[stacki]++;
21ae7a4d
JK
22465 break;
22466
22467 case DW_OP_GNU_uninit:
22468 break;
22469
336d760d 22470 case DW_OP_addrx:
3019eac3 22471 case DW_OP_GNU_addr_index:
49f6c839 22472 case DW_OP_GNU_const_index:
3019eac3
DE
22473 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22474 &bytes_read);
22475 i += bytes_read;
22476 break;
22477
21ae7a4d
JK
22478 default:
22479 {
f39c6ffd 22480 const char *name = get_DW_OP_name (op);
21ae7a4d
JK
22481
22482 if (name)
b98664d3 22483 complaint (_("unsupported stack op: '%s'"),
21ae7a4d
JK
22484 name);
22485 else
b98664d3 22486 complaint (_("unsupported stack op: '%02x'"),
21ae7a4d
JK
22487 op);
22488 }
22489
22490 return (stack[stacki]);
d53d4ac5 22491 }
3c6e0cb3 22492
21ae7a4d
JK
22493 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22494 outside of the allocated space. Also enforce minimum>0. */
22495 if (stacki >= ARRAY_SIZE (stack) - 1)
22496 {
b98664d3 22497 complaint (_("location description stack overflow"));
21ae7a4d
JK
22498 return 0;
22499 }
22500
22501 if (stacki <= 0)
22502 {
b98664d3 22503 complaint (_("location description stack underflow"));
21ae7a4d
JK
22504 return 0;
22505 }
22506 }
22507 return (stack[stacki]);
c906108c
SS
22508}
22509
22510/* memory allocation interface */
22511
c906108c 22512static struct dwarf_block *
7b5a2f43 22513dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22514{
8d749320 22515 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22516}
22517
c906108c 22518static struct die_info *
b60c80d6 22519dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22520{
22521 struct die_info *die;
b60c80d6
DJ
22522 size_t size = sizeof (struct die_info);
22523
22524 if (num_attrs > 1)
22525 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22526
b60c80d6 22527 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
22528 memset (die, 0, sizeof (struct die_info));
22529 return (die);
22530}
2e276125
JB
22531
22532\f
a036ba48 22533
c90ec28a 22534/* Macro support. */
cf2c3c16 22535
9eac9650
TT
22536/* An overload of dwarf_decode_macros that finds the correct section
22537 and ensures it is read in before calling the other overload. */
22538
22539static void
22540dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
22541 int section_is_gnu)
22542{
22543 struct dwarf2_per_objfile *dwarf2_per_objfile
22544 = cu->per_cu->dwarf2_per_objfile;
22545 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 22546 const struct line_header *lh = cu->line_header;
9eac9650
TT
22547 unsigned int offset_size = cu->header.offset_size;
22548 struct dwarf2_section_info *section;
22549 const char *section_name;
22550
22551 if (cu->dwo_unit != nullptr)
22552 {
22553 if (section_is_gnu)
22554 {
22555 section = &cu->dwo_unit->dwo_file->sections.macro;
22556 section_name = ".debug_macro.dwo";
22557 }
22558 else
22559 {
22560 section = &cu->dwo_unit->dwo_file->sections.macinfo;
22561 section_name = ".debug_macinfo.dwo";
22562 }
22563 }
22564 else
22565 {
22566 if (section_is_gnu)
22567 {
22568 section = &dwarf2_per_objfile->macro;
22569 section_name = ".debug_macro";
22570 }
22571 else
22572 {
22573 section = &dwarf2_per_objfile->macinfo;
22574 section_name = ".debug_macinfo";
22575 }
22576 }
22577
22578 section->read (objfile);
22579 if (section->buffer == nullptr)
22580 {
22581 complaint (_("missing %s section"), section_name);
22582 return;
22583 }
22584
22585 buildsym_compunit *builder = cu->get_builder ();
22586
22587 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
22588 offset_size, offset, section_is_gnu);
22589}
22590
3019eac3
DE
22591/* Return the .debug_loc section to use for CU.
22592 For DWO files use .debug_loc.dwo. */
22593
22594static struct dwarf2_section_info *
22595cu_debug_loc_section (struct dwarf2_cu *cu)
22596{
518817b3
SM
22597 struct dwarf2_per_objfile *dwarf2_per_objfile
22598 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22599
3019eac3 22600 if (cu->dwo_unit)
43988095
JK
22601 {
22602 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 22603
43988095
JK
22604 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
22605 }
22606 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
22607 : &dwarf2_per_objfile->loc);
3019eac3
DE
22608}
22609
8cf6f0b1
TT
22610/* A helper function that fills in a dwarf2_loclist_baton. */
22611
22612static void
22613fill_in_loclist_baton (struct dwarf2_cu *cu,
22614 struct dwarf2_loclist_baton *baton,
ff39bb5e 22615 const struct attribute *attr)
8cf6f0b1 22616{
518817b3
SM
22617 struct dwarf2_per_objfile *dwarf2_per_objfile
22618 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
22619 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
22620
96b79293 22621 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
22622
22623 baton->per_cu = cu->per_cu;
22624 gdb_assert (baton->per_cu);
22625 /* We don't know how long the location list is, but make sure we
22626 don't run off the edge of the section. */
3019eac3
DE
22627 baton->size = section->size - DW_UNSND (attr);
22628 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
22629 if (cu->base_address.has_value ())
22630 baton->base_address = *cu->base_address;
22631 else
22632 baton->base_address = 0;
f664829e 22633 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
22634}
22635
4c2df51b 22636static void
ff39bb5e 22637dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 22638 struct dwarf2_cu *cu, int is_block)
4c2df51b 22639{
518817b3
SM
22640 struct dwarf2_per_objfile *dwarf2_per_objfile
22641 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 22642 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 22643 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 22644
cd6c91b4 22645 if (attr->form_is_section_offset ()
3019eac3 22646 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
22647 the section. If so, fall through to the complaint in the
22648 other branch. */
2c7d5afc 22649 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 22650 {
0d53c4c4 22651 struct dwarf2_loclist_baton *baton;
4c2df51b 22652
8d749320 22653 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 22654
8cf6f0b1 22655 fill_in_loclist_baton (cu, baton, attr);
be391dca 22656
2b24b6e4 22657 if (!cu->base_address.has_value ())
b98664d3 22658 complaint (_("Location list used without "
3e43a32a 22659 "specifying the CU base address."));
4c2df51b 22660
f1e6e072
TT
22661 SYMBOL_ACLASS_INDEX (sym) = (is_block
22662 ? dwarf2_loclist_block_index
22663 : dwarf2_loclist_index);
0d53c4c4
DJ
22664 SYMBOL_LOCATION_BATON (sym) = baton;
22665 }
22666 else
22667 {
22668 struct dwarf2_locexpr_baton *baton;
22669
8d749320 22670 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
22671 baton->per_cu = cu->per_cu;
22672 gdb_assert (baton->per_cu);
0d53c4c4 22673
4fc6c0d5 22674 if (attr->form_is_block ())
0d53c4c4
DJ
22675 {
22676 /* Note that we're just copying the block's data pointer
22677 here, not the actual data. We're still pointing into the
6502dd73
DJ
22678 info_buffer for SYM's objfile; right now we never release
22679 that buffer, but when we do clean up properly this may
22680 need to change. */
0d53c4c4
DJ
22681 baton->size = DW_BLOCK (attr)->size;
22682 baton->data = DW_BLOCK (attr)->data;
22683 }
22684 else
22685 {
22686 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 22687 sym->natural_name ());
0d53c4c4 22688 baton->size = 0;
0d53c4c4 22689 }
6e70227d 22690
f1e6e072
TT
22691 SYMBOL_ACLASS_INDEX (sym) = (is_block
22692 ? dwarf2_locexpr_block_index
22693 : dwarf2_locexpr_index);
0d53c4c4
DJ
22694 SYMBOL_LOCATION_BATON (sym) = baton;
22695 }
4c2df51b 22696}
6502dd73 22697
09ba997f 22698/* See read.h. */
ae0d2f24
UW
22699
22700struct objfile *
09ba997f 22701dwarf2_per_cu_data::objfile () const
ae0d2f24 22702{
09ba997f 22703 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
22704
22705 /* Return the master objfile, so that we can report and look up the
22706 correct file containing this variable. */
22707 if (objfile->separate_debug_objfile_backlink)
22708 objfile = objfile->separate_debug_objfile_backlink;
22709
22710 return objfile;
22711}
22712
96408a79
SA
22713/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
22714 (CU_HEADERP is unused in such case) or prepare a temporary copy at
22715 CU_HEADERP first. */
22716
22717static const struct comp_unit_head *
22718per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 22719 const struct dwarf2_per_cu_data *per_cu)
96408a79 22720{
d521ce57 22721 const gdb_byte *info_ptr;
96408a79
SA
22722
22723 if (per_cu->cu)
22724 return &per_cu->cu->header;
22725
9c541725 22726 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
22727
22728 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
22729 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
22730 rcuh_kind::COMPILE);
96408a79
SA
22731
22732 return cu_headerp;
22733}
22734
09ba997f 22735/* See read.h. */
ae0d2f24 22736
98714339 22737int
09ba997f 22738dwarf2_per_cu_data::addr_size () const
ae0d2f24 22739{
96408a79
SA
22740 struct comp_unit_head cu_header_local;
22741 const struct comp_unit_head *cu_headerp;
c471e790 22742
09ba997f 22743 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22744
22745 return cu_headerp->addr_size;
ae0d2f24
UW
22746}
22747
09ba997f 22748/* See read.h. */
9eae7c52
TT
22749
22750int
09ba997f 22751dwarf2_per_cu_data::offset_size () const
9eae7c52 22752{
96408a79
SA
22753 struct comp_unit_head cu_header_local;
22754 const struct comp_unit_head *cu_headerp;
9c6c53f7 22755
09ba997f 22756 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22757
22758 return cu_headerp->offset_size;
22759}
22760
09ba997f 22761/* See read.h. */
96408a79
SA
22762
22763int
09ba997f 22764dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
22765{
22766 struct comp_unit_head cu_header_local;
22767 const struct comp_unit_head *cu_headerp;
22768
09ba997f 22769 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
22770
22771 if (cu_headerp->version == 2)
22772 return cu_headerp->addr_size;
22773 else
22774 return cu_headerp->offset_size;
181cebd4
JK
22775}
22776
09ba997f 22777/* See read.h. */
9aa1f1e3
TT
22778
22779CORE_ADDR
09ba997f 22780dwarf2_per_cu_data::text_offset () const
9aa1f1e3 22781{
09ba997f
TT
22782 struct objfile *objfile = dwarf2_per_objfile->objfile;
22783
22784 return objfile->text_section_offset ();
9aa1f1e3
TT
22785}
22786
09ba997f
TT
22787/* See read.h. */
22788
22789struct type *
22790dwarf2_per_cu_data::addr_type () const
9a49df9d 22791{
09ba997f 22792 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
22793 struct type *void_type = objfile_type (objfile)->builtin_void;
22794 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 22795 int addr_size = this->addr_size ();
9a49df9d
AB
22796
22797 if (TYPE_LENGTH (addr_type) == addr_size)
22798 return addr_type;
22799
09ba997f 22800 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
22801 return addr_type;
22802}
22803
22b6cd70
TT
22804/* A helper function for dwarf2_find_containing_comp_unit that returns
22805 the index of the result, and that searches a vector. It will
22806 return a result even if the offset in question does not actually
22807 occur in any CU. This is separate so that it can be unit
22808 tested. */
ae038cb0 22809
22b6cd70
TT
22810static int
22811dwarf2_find_containing_comp_unit
22812 (sect_offset sect_off,
22813 unsigned int offset_in_dwz,
22814 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 22815{
ae038cb0
DJ
22816 int low, high;
22817
ae038cb0 22818 low = 0;
22b6cd70 22819 high = all_comp_units.size () - 1;
ae038cb0
DJ
22820 while (high > low)
22821 {
36586728 22822 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 22823 int mid = low + (high - low) / 2;
9a619af0 22824
22b6cd70 22825 mid_cu = all_comp_units[mid];
36586728 22826 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 22827 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 22828 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
22829 high = mid;
22830 else
22831 low = mid + 1;
22832 }
22833 gdb_assert (low == high);
22b6cd70
TT
22834 return low;
22835}
22836
22837/* Locate the .debug_info compilation unit from CU's objfile which contains
22838 the DIE at OFFSET. Raises an error on failure. */
22839
22840static struct dwarf2_per_cu_data *
22841dwarf2_find_containing_comp_unit (sect_offset sect_off,
22842 unsigned int offset_in_dwz,
22843 struct dwarf2_per_objfile *dwarf2_per_objfile)
22844{
22845 int low
22846 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
22847 dwarf2_per_objfile->all_comp_units);
22848 struct dwarf2_per_cu_data *this_cu
22849 = dwarf2_per_objfile->all_comp_units[low];
22850
45b8ae0c 22851 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 22852 {
36586728 22853 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 22854 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
22855 "offset %s [in module %s]"),
22856 sect_offset_str (sect_off),
ed2dc618 22857 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 22858
9c541725
PA
22859 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
22860 <= sect_off);
ae038cb0
DJ
22861 return dwarf2_per_objfile->all_comp_units[low-1];
22862 }
22863 else
22864 {
b76e467d 22865 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 22866 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 22867 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 22868 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
22869 return this_cu;
22870 }
22871}
22872
22b6cd70
TT
22873#if GDB_SELF_TEST
22874
22875namespace selftests {
22876namespace find_containing_comp_unit {
22877
22878static void
22879run_test ()
22880{
22881 struct dwarf2_per_cu_data one {};
22882 struct dwarf2_per_cu_data two {};
22883 struct dwarf2_per_cu_data three {};
22884 struct dwarf2_per_cu_data four {};
22885
22886 one.length = 5;
22887 two.sect_off = sect_offset (one.length);
22888 two.length = 7;
22889
22890 three.length = 5;
22891 three.is_dwz = 1;
22892 four.sect_off = sect_offset (three.length);
22893 four.length = 7;
22894 four.is_dwz = 1;
22895
22896 std::vector<dwarf2_per_cu_data *> units;
22897 units.push_back (&one);
22898 units.push_back (&two);
22899 units.push_back (&three);
22900 units.push_back (&four);
22901
22902 int result;
22903
22904 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
22905 SELF_CHECK (units[result] == &one);
22906 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
22907 SELF_CHECK (units[result] == &one);
22908 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
22909 SELF_CHECK (units[result] == &two);
22910
22911 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
22912 SELF_CHECK (units[result] == &three);
22913 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
22914 SELF_CHECK (units[result] == &three);
22915 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
22916 SELF_CHECK (units[result] == &four);
22917}
22918
22919}
22920}
22921
22922#endif /* GDB_SELF_TEST */
22923
23745b47 22924/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 22925
fcd3b13d
SM
22926dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
22927 : per_cu (per_cu_),
9068261f
AB
22928 mark (false),
22929 has_loclist (false),
22930 checked_producer (false),
22931 producer_is_gxx_lt_4_6 (false),
22932 producer_is_gcc_lt_4_3 (false),
eb77c9df 22933 producer_is_icc (false),
9068261f 22934 producer_is_icc_lt_14 (false),
c258c396 22935 producer_is_codewarrior (false),
9068261f 22936 processing_has_namespace_info (false)
93311388 22937{
fcd3b13d
SM
22938 per_cu->cu = this;
22939}
22940
22941/* Destroy a dwarf2_cu. */
22942
22943dwarf2_cu::~dwarf2_cu ()
22944{
22945 per_cu->cu = NULL;
9816fde3
JK
22946}
22947
22948/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
22949
22950static void
95554aad
TT
22951prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
22952 enum language pretend_language)
9816fde3
JK
22953{
22954 struct attribute *attr;
22955
22956 /* Set the language we're debugging. */
22957 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 22958 if (attr != nullptr)
9816fde3
JK
22959 set_cu_language (DW_UNSND (attr), cu);
22960 else
9cded63f 22961 {
95554aad 22962 cu->language = pretend_language;
9cded63f
TT
22963 cu->language_defn = language_def (cu->language);
22964 }
dee91e82 22965
7d45c7c3 22966 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
22967}
22968
ae038cb0
DJ
22969/* Increase the age counter on each cached compilation unit, and free
22970 any that are too old. */
22971
22972static void
ed2dc618 22973age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
22974{
22975 struct dwarf2_per_cu_data *per_cu, **last_chain;
22976
22977 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
22978 per_cu = dwarf2_per_objfile->read_in_chain;
22979 while (per_cu != NULL)
22980 {
22981 per_cu->cu->last_used ++;
b4f54984 22982 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
22983 dwarf2_mark (per_cu->cu);
22984 per_cu = per_cu->cu->read_in_chain;
22985 }
22986
22987 per_cu = dwarf2_per_objfile->read_in_chain;
22988 last_chain = &dwarf2_per_objfile->read_in_chain;
22989 while (per_cu != NULL)
22990 {
22991 struct dwarf2_per_cu_data *next_cu;
22992
22993 next_cu = per_cu->cu->read_in_chain;
22994
22995 if (!per_cu->cu->mark)
22996 {
fcd3b13d 22997 delete per_cu->cu;
ae038cb0
DJ
22998 *last_chain = next_cu;
22999 }
23000 else
23001 last_chain = &per_cu->cu->read_in_chain;
23002
23003 per_cu = next_cu;
23004 }
23005}
23006
23007/* Remove a single compilation unit from the cache. */
23008
23009static void
dee91e82 23010free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23011{
23012 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23013 struct dwarf2_per_objfile *dwarf2_per_objfile
23014 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23015
23016 per_cu = dwarf2_per_objfile->read_in_chain;
23017 last_chain = &dwarf2_per_objfile->read_in_chain;
23018 while (per_cu != NULL)
23019 {
23020 struct dwarf2_per_cu_data *next_cu;
23021
23022 next_cu = per_cu->cu->read_in_chain;
23023
dee91e82 23024 if (per_cu == target_per_cu)
ae038cb0 23025 {
fcd3b13d 23026 delete per_cu->cu;
dee91e82 23027 per_cu->cu = NULL;
ae038cb0
DJ
23028 *last_chain = next_cu;
23029 break;
23030 }
23031 else
23032 last_chain = &per_cu->cu->read_in_chain;
23033
23034 per_cu = next_cu;
23035 }
23036}
23037
dee91e82
DE
23038/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23039 We store these in a hash table separate from the DIEs, and preserve them
23040 when the DIEs are flushed out of cache.
23041
23042 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23043 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23044 or the type may come from a DWO file. Furthermore, while it's more logical
23045 to use per_cu->section+offset, with Fission the section with the data is in
23046 the DWO file but we don't know that section at the point we need it.
23047 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23048 because we can enter the lookup routine, get_die_type_at_offset, from
23049 outside this file, and thus won't necessarily have PER_CU->cu.
23050 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23051
dee91e82 23052struct dwarf2_per_cu_offset_and_type
1c379e20 23053{
dee91e82 23054 const struct dwarf2_per_cu_data *per_cu;
9c541725 23055 sect_offset sect_off;
1c379e20
DJ
23056 struct type *type;
23057};
23058
dee91e82 23059/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23060
23061static hashval_t
dee91e82 23062per_cu_offset_and_type_hash (const void *item)
1c379e20 23063{
9a3c8263
SM
23064 const struct dwarf2_per_cu_offset_and_type *ofs
23065 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23066
9c541725 23067 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23068}
23069
dee91e82 23070/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23071
23072static int
dee91e82 23073per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23074{
9a3c8263
SM
23075 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23076 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23077 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23078 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23079
dee91e82 23080 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23081 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23082}
23083
23084/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23085 table if necessary. For convenience, return TYPE.
23086
23087 The DIEs reading must have careful ordering to:
85102364 23088 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23089 reading current DIE.
23090 * Not trying to dereference contents of still incompletely read in types
23091 while reading in other DIEs.
23092 * Enable referencing still incompletely read in types just by a pointer to
23093 the type without accessing its fields.
23094
23095 Therefore caller should follow these rules:
23096 * Try to fetch any prerequisite types we may need to build this DIE type
23097 before building the type and calling set_die_type.
e71ec853 23098 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23099 possible before fetching more types to complete the current type.
23100 * Make the type as complete as possible before fetching more types. */
1c379e20 23101
f792889a 23102static struct type *
1c379e20
DJ
23103set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23104{
518817b3
SM
23105 struct dwarf2_per_objfile *dwarf2_per_objfile
23106 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23107 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23108 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23109 struct attribute *attr;
23110 struct dynamic_prop prop;
1c379e20 23111
b4ba55a1
JB
23112 /* For Ada types, make sure that the gnat-specific data is always
23113 initialized (if not already set). There are a few types where
23114 we should not be doing so, because the type-specific area is
23115 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23116 where the type-specific area is used to store the floatformat).
23117 But this is not a problem, because the gnat-specific information
23118 is actually not needed for these types. */
23119 if (need_gnat_info (cu)
23120 && TYPE_CODE (type) != TYPE_CODE_FUNC
23121 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23122 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23123 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23124 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23125 && !HAVE_GNAT_AUX_INFO (type))
23126 INIT_GNAT_SPECIFIC (type);
23127
3f2f83dd
KB
23128 /* Read DW_AT_allocated and set in type. */
23129 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23130 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23131 {
09ba997f 23132 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23133 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23134 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
23135 }
23136 else if (attr != NULL)
23137 {
b98664d3 23138 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23139 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23140 sect_offset_str (die->sect_off));
3f2f83dd
KB
23141 }
23142
23143 /* Read DW_AT_associated and set in type. */
23144 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23145 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23146 {
09ba997f 23147 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23148 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23149 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
23150 }
23151 else if (attr != NULL)
23152 {
b98664d3 23153 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23154 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23155 sect_offset_str (die->sect_off));
3f2f83dd
KB
23156 }
23157
3cdcd0ce
JB
23158 /* Read DW_AT_data_location and set in type. */
23159 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23160 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23161 cu->per_cu->addr_type ()))
50a82047 23162 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 23163
dee91e82 23164 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23165 dwarf2_per_objfile->die_type_hash
23166 = htab_up (htab_create_alloc (127,
23167 per_cu_offset_and_type_hash,
23168 per_cu_offset_and_type_eq,
23169 NULL, xcalloc, xfree));
1c379e20 23170
dee91e82 23171 ofs.per_cu = cu->per_cu;
9c541725 23172 ofs.sect_off = die->sect_off;
1c379e20 23173 ofs.type = type;
dee91e82 23174 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23175 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23176 if (*slot)
b98664d3 23177 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23178 sect_offset_str (die->sect_off));
8d749320
SM
23179 *slot = XOBNEW (&objfile->objfile_obstack,
23180 struct dwarf2_per_cu_offset_and_type);
1c379e20 23181 **slot = ofs;
f792889a 23182 return type;
1c379e20
DJ
23183}
23184
9c541725 23185/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23186 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23187
23188static struct type *
9c541725 23189get_die_type_at_offset (sect_offset sect_off,
673bfd45 23190 struct dwarf2_per_cu_data *per_cu)
1c379e20 23191{
dee91e82 23192 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23193 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23194
dee91e82 23195 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23196 return NULL;
1c379e20 23197
dee91e82 23198 ofs.per_cu = per_cu;
9c541725 23199 ofs.sect_off = sect_off;
9a3c8263 23200 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23201 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23202 if (slot)
23203 return slot->type;
23204 else
23205 return NULL;
23206}
23207
02142a6c 23208/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23209 or return NULL if DIE does not have a saved type. */
23210
23211static struct type *
23212get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23213{
9c541725 23214 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23215}
23216
10b3939b
DJ
23217/* Add a dependence relationship from CU to REF_PER_CU. */
23218
23219static void
23220dwarf2_add_dependence (struct dwarf2_cu *cu,
23221 struct dwarf2_per_cu_data *ref_per_cu)
23222{
23223 void **slot;
23224
23225 if (cu->dependencies == NULL)
23226 cu->dependencies
23227 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23228 NULL, &cu->comp_unit_obstack,
23229 hashtab_obstack_allocate,
23230 dummy_obstack_deallocate);
23231
23232 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23233 if (*slot == NULL)
23234 *slot = ref_per_cu;
23235}
1c379e20 23236
f504f079
DE
23237/* Subroutine of dwarf2_mark to pass to htab_traverse.
23238 Set the mark field in every compilation unit in the
ae038cb0
DJ
23239 cache that we must keep because we are keeping CU. */
23240
10b3939b
DJ
23241static int
23242dwarf2_mark_helper (void **slot, void *data)
23243{
23244 struct dwarf2_per_cu_data *per_cu;
23245
23246 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23247
23248 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23249 reading of the chain. As such dependencies remain valid it is not much
23250 useful to track and undo them during QUIT cleanups. */
23251 if (per_cu->cu == NULL)
23252 return 1;
23253
10b3939b
DJ
23254 if (per_cu->cu->mark)
23255 return 1;
9068261f 23256 per_cu->cu->mark = true;
10b3939b
DJ
23257
23258 if (per_cu->cu->dependencies != NULL)
23259 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23260
23261 return 1;
23262}
23263
f504f079
DE
23264/* Set the mark field in CU and in every other compilation unit in the
23265 cache that we must keep because we are keeping CU. */
23266
ae038cb0
DJ
23267static void
23268dwarf2_mark (struct dwarf2_cu *cu)
23269{
23270 if (cu->mark)
23271 return;
9068261f 23272 cu->mark = true;
10b3939b
DJ
23273 if (cu->dependencies != NULL)
23274 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23275}
23276
23277static void
23278dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23279{
23280 while (per_cu)
23281 {
9068261f 23282 per_cu->cu->mark = false;
ae038cb0
DJ
23283 per_cu = per_cu->cu->read_in_chain;
23284 }
72bf9492
DJ
23285}
23286
72bf9492
DJ
23287/* Trivial hash function for partial_die_info: the hash value of a DIE
23288 is its offset in .debug_info for this objfile. */
23289
23290static hashval_t
23291partial_die_hash (const void *item)
23292{
9a3c8263
SM
23293 const struct partial_die_info *part_die
23294 = (const struct partial_die_info *) item;
9a619af0 23295
9c541725 23296 return to_underlying (part_die->sect_off);
72bf9492
DJ
23297}
23298
23299/* Trivial comparison function for partial_die_info structures: two DIEs
23300 are equal if they have the same offset. */
23301
23302static int
23303partial_die_eq (const void *item_lhs, const void *item_rhs)
23304{
9a3c8263
SM
23305 const struct partial_die_info *part_die_lhs
23306 = (const struct partial_die_info *) item_lhs;
23307 const struct partial_die_info *part_die_rhs
23308 = (const struct partial_die_info *) item_rhs;
9a619af0 23309
9c541725 23310 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23311}
23312
3c3bb058
AB
23313struct cmd_list_element *set_dwarf_cmdlist;
23314struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0
DJ
23315
23316static void
981a3fb3 23317set_dwarf_cmd (const char *args, int from_tty)
ae038cb0 23318{
b4f54984 23319 help_list (set_dwarf_cmdlist, "maintenance set dwarf ", all_commands,
635c7e8a 23320 gdb_stdout);
ae038cb0
DJ
23321}
23322
23323static void
981a3fb3 23324show_dwarf_cmd (const char *args, int from_tty)
6e70227d 23325{
b4f54984 23326 cmd_show_list (show_dwarf_cmdlist, from_tty, "");
ae038cb0
DJ
23327}
23328
9291a0cd 23329static void
cd4fb1b2
SM
23330show_check_physname (struct ui_file *file, int from_tty,
23331 struct cmd_list_element *c, const char *value)
9291a0cd 23332{
cd4fb1b2
SM
23333 fprintf_filtered (file,
23334 _("Whether to check \"physname\" is %s.\n"),
23335 value);
9291a0cd
TT
23336}
23337
6c265988 23338void _initialize_dwarf2_read ();
cd4fb1b2 23339void
6c265988 23340_initialize_dwarf2_read ()
9291a0cd 23341{
cd4fb1b2
SM
23342 add_prefix_cmd ("dwarf", class_maintenance, set_dwarf_cmd, _("\
23343Set DWARF specific variables.\n\
590042fc 23344Configure DWARF variables such as the cache size."),
cd4fb1b2
SM
23345 &set_dwarf_cmdlist, "maintenance set dwarf ",
23346 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23347
cd4fb1b2 23348 add_prefix_cmd ("dwarf", class_maintenance, show_dwarf_cmd, _("\
590042fc
PW
23349Show DWARF specific variables.\n\
23350Show DWARF variables such as the cache size."),
cd4fb1b2
SM
23351 &show_dwarf_cmdlist, "maintenance show dwarf ",
23352 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23353
cd4fb1b2
SM
23354 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23355 &dwarf_max_cache_age, _("\
23356Set the upper bound on the age of cached DWARF compilation units."), _("\
23357Show the upper bound on the age of cached DWARF compilation units."), _("\
23358A higher limit means that cached compilation units will be stored\n\
23359in memory longer, and more total memory will be used. Zero disables\n\
23360caching, which can slow down startup."),
23361 NULL,
23362 show_dwarf_max_cache_age,
23363 &set_dwarf_cmdlist,
23364 &show_dwarf_cmdlist);
156942c7 23365
cd4fb1b2
SM
23366 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23367Set debugging of the DWARF reader."), _("\
23368Show debugging of the DWARF reader."), _("\
23369When enabled (non-zero), debugging messages are printed during DWARF\n\
23370reading and symtab expansion. A value of 1 (one) provides basic\n\
23371information. A value greater than 1 provides more verbose information."),
23372 NULL,
23373 NULL,
23374 &setdebuglist, &showdebuglist);
9291a0cd 23375
cd4fb1b2
SM
23376 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23377Set debugging of the DWARF DIE reader."), _("\
23378Show debugging of the DWARF DIE reader."), _("\
23379When enabled (non-zero), DIEs are dumped after they are read in.\n\
23380The value is the maximum depth to print."),
23381 NULL,
23382 NULL,
23383 &setdebuglist, &showdebuglist);
9291a0cd 23384
cd4fb1b2
SM
23385 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23386Set debugging of the dwarf line reader."), _("\
23387Show debugging of the dwarf line reader."), _("\
23388When enabled (non-zero), line number entries are dumped as they are read in.\n\
23389A value of 1 (one) provides basic information.\n\
23390A value greater than 1 provides more verbose information."),
23391 NULL,
23392 NULL,
23393 &setdebuglist, &showdebuglist);
437afbb8 23394
cd4fb1b2
SM
23395 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23396Set cross-checking of \"physname\" code against demangler."), _("\
23397Show cross-checking of \"physname\" code against demangler."), _("\
23398When enabled, GDB's internal \"physname\" code is checked against\n\
23399the demangler."),
23400 NULL, show_check_physname,
23401 &setdebuglist, &showdebuglist);
900e11f9 23402
e615022a
DE
23403 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23404 no_class, &use_deprecated_index_sections, _("\
23405Set whether to use deprecated gdb_index sections."), _("\
23406Show whether to use deprecated gdb_index sections."), _("\
23407When enabled, deprecated .gdb_index sections are used anyway.\n\
23408Normally they are ignored either because of a missing feature or\n\
23409performance issue.\n\
23410Warning: This option must be enabled before gdb reads the file."),
23411 NULL,
23412 NULL,
23413 &setlist, &showlist);
23414
f1e6e072
TT
23415 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23416 &dwarf2_locexpr_funcs);
23417 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23418 &dwarf2_loclist_funcs);
23419
23420 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23421 &dwarf2_block_frame_base_locexpr_funcs);
23422 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23423 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23424
23425#if GDB_SELF_TEST
23426 selftests::register_test ("dw2_expand_symtabs_matching",
23427 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23428 selftests::register_test ("dwarf2_find_containing_comp_unit",
23429 selftests::find_containing_comp_unit::run_test);
c62446b1 23430#endif
6502dd73 23431}
This page took 5.256253 seconds and 4 git commands to generate.