Move the rust "{" hack
[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
41144253 117/* Size of .debug_loclists section header for 32-bit DWARF format. */
118#define LOCLIST_HEADER_SIZE32 12
119
120/* Size of .debug_loclists section header for 64-bit DWARF format. */
121#define LOCLIST_HEADER_SIZE64 20
122
3f563c84
PA
123/* An index into a (C++) symbol name component in a symbol name as
124 recorded in the mapped_index's symbol table. For each C++ symbol
125 in the symbol table, we record one entry for the start of each
126 component in the symbol in a table of name components, and then
127 sort the table, in order to be able to binary search symbol names,
128 ignoring leading namespaces, both completion and regular look up.
129 For example, for symbol "A::B::C", we'll have an entry that points
130 to "A::B::C", another that points to "B::C", and another for "C".
131 Note that function symbols in GDB index have no parameter
132 information, just the function/method names. You can convert a
133 name_component to a "const char *" using the
134 'mapped_index::symbol_name_at(offset_type)' method. */
135
136struct name_component
137{
138 /* Offset in the symbol name where the component starts. Stored as
139 a (32-bit) offset instead of a pointer to save memory and improve
140 locality on 64-bit architectures. */
141 offset_type name_offset;
142
143 /* The symbol's index in the symbol and constant pool tables of a
144 mapped_index. */
145 offset_type idx;
146};
147
44ed8f3e
PA
148/* Base class containing bits shared by both .gdb_index and
149 .debug_name indexes. */
150
151struct mapped_index_base
152{
22ca247e
TT
153 mapped_index_base () = default;
154 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
155
44ed8f3e
PA
156 /* The name_component table (a sorted vector). See name_component's
157 description above. */
158 std::vector<name_component> name_components;
159
160 /* How NAME_COMPONENTS is sorted. */
161 enum case_sensitivity name_components_casing;
162
163 /* Return the number of names in the symbol table. */
164 virtual size_t symbol_name_count () const = 0;
165
166 /* Get the name of the symbol at IDX in the symbol table. */
167 virtual const char *symbol_name_at (offset_type idx) const = 0;
168
169 /* Return whether the name at IDX in the symbol table should be
170 ignored. */
171 virtual bool symbol_name_slot_invalid (offset_type idx) const
172 {
173 return false;
174 }
175
176 /* Build the symbol name component sorted vector, if we haven't
177 yet. */
178 void build_name_components ();
179
180 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
181 possible matches for LN_NO_PARAMS in the name component
182 vector. */
183 std::pair<std::vector<name_component>::const_iterator,
184 std::vector<name_component>::const_iterator>
3b00ef10
TT
185 find_name_components_bounds (const lookup_name_info &ln_no_params,
186 enum language lang) const;
44ed8f3e
PA
187
188 /* Prevent deleting/destroying via a base class pointer. */
189protected:
190 ~mapped_index_base() = default;
191};
192
9291a0cd
TT
193/* A description of the mapped index. The file format is described in
194 a comment by the code that writes the index. */
fc898b42 195struct mapped_index final : public mapped_index_base
9291a0cd 196{
f00a2de2
PA
197 /* A slot/bucket in the symbol table hash. */
198 struct symbol_table_slot
199 {
200 const offset_type name;
201 const offset_type vec;
202 };
203
559a7a62 204 /* Index data format version. */
3063847f 205 int version = 0;
559a7a62 206
f00a2de2
PA
207 /* The address table data. */
208 gdb::array_view<const gdb_byte> address_table;
b11b1f88 209
3876f04e 210 /* The symbol table, implemented as a hash table. */
f00a2de2 211 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 212
9291a0cd 213 /* A pointer to the constant pool. */
3063847f 214 const char *constant_pool = nullptr;
3f563c84 215
44ed8f3e
PA
216 bool symbol_name_slot_invalid (offset_type idx) const override
217 {
218 const auto &bucket = this->symbol_table[idx];
9ab08412 219 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 220 }
5c58de74 221
3f563c84
PA
222 /* Convenience method to get at the name of the symbol at IDX in the
223 symbol table. */
44ed8f3e 224 const char *symbol_name_at (offset_type idx) const override
f00a2de2 225 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 226
44ed8f3e
PA
227 size_t symbol_name_count () const override
228 { return this->symbol_table.size (); }
9291a0cd
TT
229};
230
927aa2e7
JK
231/* A description of the mapped .debug_names.
232 Uninitialized map has CU_COUNT 0. */
fc898b42 233struct mapped_debug_names final : public mapped_index_base
927aa2e7 234{
ed2dc618
SM
235 mapped_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile_)
236 : dwarf2_per_objfile (dwarf2_per_objfile_)
237 {}
238
239 struct dwarf2_per_objfile *dwarf2_per_objfile;
927aa2e7
JK
240 bfd_endian dwarf5_byte_order;
241 bool dwarf5_is_dwarf64;
242 bool augmentation_is_gdb;
243 uint8_t offset_size;
244 uint32_t cu_count = 0;
245 uint32_t tu_count, bucket_count, name_count;
246 const gdb_byte *cu_table_reordered, *tu_table_reordered;
247 const uint32_t *bucket_table_reordered, *hash_table_reordered;
248 const gdb_byte *name_table_string_offs_reordered;
249 const gdb_byte *name_table_entry_offs_reordered;
250 const gdb_byte *entry_pool;
251
252 struct index_val
253 {
254 ULONGEST dwarf_tag;
255 struct attr
256 {
257 /* Attribute name DW_IDX_*. */
258 ULONGEST dw_idx;
259
260 /* Attribute form DW_FORM_*. */
261 ULONGEST form;
262
263 /* Value if FORM is DW_FORM_implicit_const. */
264 LONGEST implicit_const;
265 };
266 std::vector<attr> attr_vec;
267 };
268
269 std::unordered_map<ULONGEST, index_val> abbrev_map;
270
271 const char *namei_to_name (uint32_t namei) const;
44ed8f3e
PA
272
273 /* Implementation of the mapped_index_base virtual interface, for
274 the name_components cache. */
275
276 const char *symbol_name_at (offset_type idx) const override
277 { return namei_to_name (idx); }
278
279 size_t symbol_name_count () const override
280 { return this->name_count; }
927aa2e7
JK
281};
282
cd4fb1b2 283/* See dwarf2read.h. */
ed2dc618 284
cd4fb1b2 285dwarf2_per_objfile *
ed2dc618
SM
286get_dwarf2_per_objfile (struct objfile *objfile)
287{
5bfd760d 288 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 289}
c906108c 290
251d32d9 291/* Default names of the debugging sections. */
c906108c 292
233a11ab
CS
293/* Note that if the debugging section has been compressed, it might
294 have a name like .zdebug_info. */
295
9cdd5dbd
DE
296static const struct dwarf2_debug_sections dwarf2_elf_names =
297{
251d32d9
TG
298 { ".debug_info", ".zdebug_info" },
299 { ".debug_abbrev", ".zdebug_abbrev" },
300 { ".debug_line", ".zdebug_line" },
301 { ".debug_loc", ".zdebug_loc" },
43988095 302 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 303 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 304 { ".debug_macro", ".zdebug_macro" },
251d32d9 305 { ".debug_str", ".zdebug_str" },
18a8505e 306 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 307 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 308 { ".debug_ranges", ".zdebug_ranges" },
43988095 309 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 310 { ".debug_types", ".zdebug_types" },
3019eac3 311 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
312 { ".debug_frame", ".zdebug_frame" },
313 { ".eh_frame", NULL },
24d3216f 314 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
315 { ".debug_names", ".zdebug_names" },
316 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 317 23
251d32d9 318};
c906108c 319
80626a55 320/* List of DWO/DWP sections. */
3019eac3 321
80626a55 322static const struct dwop_section_names
3019eac3
DE
323{
324 struct dwarf2_section_names abbrev_dwo;
325 struct dwarf2_section_names info_dwo;
326 struct dwarf2_section_names line_dwo;
327 struct dwarf2_section_names loc_dwo;
43988095 328 struct dwarf2_section_names loclists_dwo;
09262596
DE
329 struct dwarf2_section_names macinfo_dwo;
330 struct dwarf2_section_names macro_dwo;
3019eac3
DE
331 struct dwarf2_section_names str_dwo;
332 struct dwarf2_section_names str_offsets_dwo;
333 struct dwarf2_section_names types_dwo;
80626a55
DE
334 struct dwarf2_section_names cu_index;
335 struct dwarf2_section_names tu_index;
3019eac3 336}
80626a55 337dwop_section_names =
3019eac3
DE
338{
339 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
340 { ".debug_info.dwo", ".zdebug_info.dwo" },
341 { ".debug_line.dwo", ".zdebug_line.dwo" },
342 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 343 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
344 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
345 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
3019eac3
DE
346 { ".debug_str.dwo", ".zdebug_str.dwo" },
347 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
348 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
349 { ".debug_cu_index", ".zdebug_cu_index" },
350 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
351};
352
c906108c
SS
353/* local data types */
354
41144253 355/* The location list section (.debug_loclists) begins with a header,
356 which contains the following information. */
357struct loclist_header
358{
359 /* A 4-byte or 12-byte length containing the length of the
360 set of entries for this compilation unit, not including the
361 length field itself. */
362 unsigned int length;
363
364 /* A 2-byte version identifier. */
365 short version;
366
367 /* A 1-byte unsigned integer containing the size in bytes of an address on
368 the target system. */
369 unsigned char addr_size;
370
371 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
372 on the target system. */
373 unsigned char segment_collector_size;
374
375 /* A 4-byte count of the number of offsets that follow the header. */
376 unsigned int offset_entry_count;
377};
378
3da10d80
KS
379/* Type used for delaying computation of method physnames.
380 See comments for compute_delayed_physnames. */
381struct delayed_method_info
382{
383 /* The type to which the method is attached, i.e., its parent class. */
384 struct type *type;
385
386 /* The index of the method in the type's function fieldlists. */
387 int fnfield_index;
388
389 /* The index of the method in the fieldlist. */
390 int index;
391
392 /* The name of the DIE. */
393 const char *name;
394
395 /* The DIE associated with this method. */
396 struct die_info *die;
397};
398
e7c27a73
DJ
399/* Internal state when decoding a particular compilation unit. */
400struct dwarf2_cu
401{
fcd3b13d
SM
402 explicit dwarf2_cu (struct dwarf2_per_cu_data *per_cu);
403 ~dwarf2_cu ();
404
405 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
406
c24bdb02
KS
407 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
408 Create the set of symtabs used by this TU, or if this TU is sharing
409 symtabs with another TU and the symtabs have already been created
410 then restore those symtabs in the line header.
411 We don't need the pc/line-number mapping for type units. */
412 void setup_type_unit_groups (struct die_info *die);
413
414 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
415 buildsym_compunit constructor. */
416 struct compunit_symtab *start_symtab (const char *name,
417 const char *comp_dir,
418 CORE_ADDR low_pc);
419
420 /* Reset the builder. */
421 void reset_builder () { m_builder.reset (); }
422
d00adf39 423 /* The header of the compilation unit. */
fcd3b13d 424 struct comp_unit_head header {};
e142c38c 425
d00adf39 426 /* Base address of this compilation unit. */
2b24b6e4 427 gdb::optional<CORE_ADDR> base_address;
d00adf39 428
e142c38c 429 /* The language we are debugging. */
fcd3b13d
SM
430 enum language language = language_unknown;
431 const struct language_defn *language_defn = nullptr;
e142c38c 432
fcd3b13d 433 const char *producer = nullptr;
b0f35d58 434
c24bdb02 435private:
804d2729
TT
436 /* The symtab builder for this CU. This is only non-NULL when full
437 symbols are being read. */
c24bdb02 438 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 439
c24bdb02 440public:
e142c38c
DJ
441 /* The generic symbol table building routines have separate lists for
442 file scope symbols and all all other scopes (local scopes). So
443 we need to select the right one to pass to add_symbol_to_list().
444 We do it by keeping a pointer to the correct list in list_in_scope.
445
446 FIXME: The original dwarf code just treated the file scope as the
447 first local scope, and all other local scopes as nested local
448 scopes, and worked fine. Check to see if we really need to
449 distinguish these in buildsym.c. */
fcd3b13d 450 struct pending **list_in_scope = nullptr;
e142c38c 451
b64f50a1
JK
452 /* Hash table holding all the loaded partial DIEs
453 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 454 htab_t partial_dies = nullptr;
72bf9492
DJ
455
456 /* Storage for things with the same lifetime as this read-in compilation
457 unit, including partial DIEs. */
fcd3b13d 458 auto_obstack comp_unit_obstack;
72bf9492 459
ae038cb0
DJ
460 /* When multiple dwarf2_cu structures are living in memory, this field
461 chains them all together, so that they can be released efficiently.
462 We will probably also want a generation counter so that most-recently-used
463 compilation units are cached... */
fcd3b13d 464 struct dwarf2_per_cu_data *read_in_chain = nullptr;
ae038cb0 465
69d751e3 466 /* Backlink to our per_cu entry. */
ae038cb0
DJ
467 struct dwarf2_per_cu_data *per_cu;
468
469 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 470 int last_used = 0;
ae038cb0 471
b64f50a1
JK
472 /* A hash table of DIE cu_offset for following references with
473 die_info->offset.sect_off as hash. */
fcd3b13d 474 htab_t die_hash = nullptr;
10b3939b
DJ
475
476 /* Full DIEs if read in. */
fcd3b13d 477 struct die_info *dies = nullptr;
10b3939b
DJ
478
479 /* A set of pointers to dwarf2_per_cu_data objects for compilation
480 units referenced by this one. Only set during full symbol processing;
481 partial symbol tables do not have dependencies. */
fcd3b13d 482 htab_t dependencies = nullptr;
10b3939b 483
cb1df416 484 /* Header data from the line table, during full symbol processing. */
fcd3b13d 485 struct line_header *line_header = nullptr;
4c8aa72d
PA
486 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
487 it's owned by dwarf2_per_objfile::line_header_hash. If non-NULL,
488 this is the DW_TAG_compile_unit die for this CU. We'll hold on
489 to the line header as long as this DIE is being processed. See
490 process_die_scope. */
fcd3b13d 491 die_info *line_header_die_owner = nullptr;
cb1df416 492
3da10d80
KS
493 /* A list of methods which need to have physnames computed
494 after all type information has been read. */
c89b44cd 495 std::vector<delayed_method_info> method_list;
3da10d80 496
96408a79 497 /* To be copied to symtab->call_site_htab. */
fcd3b13d 498 htab_t call_site_htab = nullptr;
96408a79 499
034e5797
DE
500 /* Non-NULL if this CU came from a DWO file.
501 There is an invariant here that is important to remember:
502 Except for attributes copied from the top level DIE in the "main"
503 (or "stub") file in preparation for reading the DWO file
18a8505e 504 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
505 Either there isn't a DWO file (in which case this is NULL and the point
506 is moot), or there is and either we're not going to read it (in which
507 case this is NULL) or there is and we are reading it (in which case this
508 is non-NULL). */
fcd3b13d 509 struct dwo_unit *dwo_unit = nullptr;
3019eac3 510
18a8505e 511 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 512 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 513 gdb::optional<ULONGEST> addr_base;
3019eac3 514
18a8505e 515 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 516 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 517 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
518 be used without needing to know whether DWO files are in use or not.
519 N.B. This does not apply to DW_AT_ranges appearing in
520 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
521 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 522 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 523 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 524 ULONGEST ranges_base = 0;
2e3cf129 525
41144253 526 /* The DW_AT_loclists_base attribute if present. */
527 ULONGEST loclist_base = 0;
528
c9317f21
TT
529 /* When reading debug info generated by older versions of rustc, we
530 have to rewrite some union types to be struct types with a
531 variant part. This rewriting must be done after the CU is fully
532 read in, because otherwise at the point of rewriting some struct
533 type might not have been fully processed. So, we keep a list of
534 all such types here and process them after expansion. */
535 std::vector<struct type *> rust_unions;
536
18a8505e
AT
537 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
538 files, the value is implicitly zero. For DWARF 5 version DWO files, the
539 value is often implicit and is the size of the header of
540 .debug_str_offsets section (8 or 4, depending on the address size). */
541 gdb::optional<ULONGEST> str_offsets_base;
542
ae038cb0 543 /* Mark used when releasing cached dies. */
9068261f 544 bool mark : 1;
ae038cb0 545
8be455d7
JK
546 /* This CU references .debug_loc. See the symtab->locations_valid field.
547 This test is imperfect as there may exist optimized debug code not using
548 any location list and still facing inlining issues if handled as
549 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 550 bool has_loclist : 1;
ba919b58 551
9068261f 552 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
553 if all the producer_is_* fields are valid. This information is cached
554 because profiling CU expansion showed excessive time spent in
555 producer_is_gxx_lt_4_6. */
9068261f
AB
556 bool checked_producer : 1;
557 bool producer_is_gxx_lt_4_6 : 1;
558 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 559 bool producer_is_icc : 1;
9068261f 560 bool producer_is_icc_lt_14 : 1;
c258c396 561 bool producer_is_codewarrior : 1;
4d4ec4e5 562
9068261f 563 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
564 debugging info for C++ namespaces. GCC 3.3.x did not produce
565 this information, but later versions do. */
566
9068261f 567 bool processing_has_namespace_info : 1;
d590ff25
YQ
568
569 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
570
571 /* If this CU was inherited by another CU (via specification,
572 abstract_origin, etc), this is the ancestor CU. */
573 dwarf2_cu *ancestor;
574
575 /* Get the buildsym_compunit for this CU. */
576 buildsym_compunit *get_builder ()
577 {
578 /* If this CU has a builder associated with it, use that. */
579 if (m_builder != nullptr)
580 return m_builder.get ();
581
582 /* Otherwise, search ancestors for a valid builder. */
583 if (ancestor != nullptr)
584 return ancestor->get_builder ();
585
586 return nullptr;
587 }
e7c27a73
DJ
588};
589
094b34ac
DE
590/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
591 This includes type_unit_group and quick_file_names. */
592
593struct stmt_list_hash
594{
595 /* The DWO unit this table is from or NULL if there is none. */
596 struct dwo_unit *dwo_unit;
597
598 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 599 sect_offset line_sect_off;
094b34ac
DE
600};
601
f4dc4d17
DE
602/* Each element of dwarf2_per_objfile->type_unit_groups is a pointer to
603 an object of this type. */
604
605struct type_unit_group
606{
0186c6a7 607 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
608 To simplify things we create an artificial CU that "includes" all the
609 type units using this stmt_list so that the rest of the code still has
197400e8 610 a "per_cu" handle on the symtab. */
094b34ac
DE
611 struct dwarf2_per_cu_data per_cu;
612
0186c6a7
DE
613 /* The TUs that share this DW_AT_stmt_list entry.
614 This is added to while parsing type units to build partial symtabs,
615 and is deleted afterwards and not used again. */
a8b3b8e9 616 std::vector<signatured_type *> *tus;
f4dc4d17 617
43f3e411 618 /* The compunit symtab.
094b34ac 619 Type units in a group needn't all be defined in the same source file,
43f3e411
DE
620 so we create an essentially anonymous symtab as the compunit symtab. */
621 struct compunit_symtab *compunit_symtab;
f4dc4d17 622
094b34ac
DE
623 /* The data used to construct the hash key. */
624 struct stmt_list_hash hash;
f4dc4d17 625
f4dc4d17
DE
626 /* The symbol tables for this TU (obtained from the files listed in
627 DW_AT_stmt_list).
628 WARNING: The order of entries here must match the order of entries
629 in the line header. After the first TU using this type_unit_group, the
630 line header for the subsequent TUs is recreated from this. This is done
631 because we need to use the same symtabs for each TU using the same
632 DW_AT_stmt_list value. Also note that symtabs may be repeated here,
633 there's no guarantee the line header doesn't have duplicate entries. */
634 struct symtab **symtabs;
635};
636
73869dc2 637/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
638
639struct dwo_sections
640{
641 struct dwarf2_section_info abbrev;
3019eac3
DE
642 struct dwarf2_section_info line;
643 struct dwarf2_section_info loc;
43988095 644 struct dwarf2_section_info loclists;
09262596
DE
645 struct dwarf2_section_info macinfo;
646 struct dwarf2_section_info macro;
3019eac3
DE
647 struct dwarf2_section_info str;
648 struct dwarf2_section_info str_offsets;
80626a55
DE
649 /* In the case of a virtual DWO file, these two are unused. */
650 struct dwarf2_section_info info;
fd5866f6 651 std::vector<dwarf2_section_info> types;
3019eac3
DE
652};
653
c88ee1f0 654/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
655
656struct dwo_unit
657{
658 /* Backlink to the containing struct dwo_file. */
659 struct dwo_file *dwo_file;
660
661 /* The "id" that distinguishes this CU/TU.
662 .debug_info calls this "dwo_id", .debug_types calls this "signature".
663 Since signatures came first, we stick with it for consistency. */
664 ULONGEST signature;
665
666 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 667 struct dwarf2_section_info *section;
3019eac3 668
9c541725
PA
669 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
670 sect_offset sect_off;
3019eac3
DE
671 unsigned int length;
672
673 /* For types, offset in the type's DIE of the type defined by this TU. */
674 cu_offset type_offset_in_tu;
675};
676
73869dc2
DE
677/* include/dwarf2.h defines the DWP section codes.
678 It defines a max value but it doesn't define a min value, which we
679 use for error checking, so provide one. */
680
681enum dwp_v2_section_ids
682{
683 DW_SECT_MIN = 1
684};
685
80626a55 686/* Data for one DWO file.
57d63ce2
DE
687
688 This includes virtual DWO files (a virtual DWO file is a DWO file as it
689 appears in a DWP file). DWP files don't really have DWO files per se -
690 comdat folding of types "loses" the DWO file they came from, and from
691 a high level view DWP files appear to contain a mass of random types.
692 However, to maintain consistency with the non-DWP case we pretend DWP
693 files contain virtual DWO files, and we assign each TU with one virtual
694 DWO file (generally based on the line and abbrev section offsets -
695 a heuristic that seems to work in practice). */
3019eac3
DE
696
697struct dwo_file
698{
51ac9db5
SM
699 dwo_file () = default;
700 DISABLE_COPY_AND_ASSIGN (dwo_file);
701
18a8505e 702 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
703 For virtual DWO files the name is constructed from the section offsets
704 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
705 from related CU+TUs. */
51ac9db5 706 const char *dwo_name = nullptr;
0ac5b59e
DE
707
708 /* The DW_AT_comp_dir attribute. */
51ac9db5 709 const char *comp_dir = nullptr;
3019eac3 710
80626a55
DE
711 /* The bfd, when the file is open. Otherwise this is NULL.
712 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 713 gdb_bfd_ref_ptr dbfd;
3019eac3 714
73869dc2
DE
715 /* The sections that make up this DWO file.
716 Remember that for virtual DWO files in DWP V2, these are virtual
717 sections (for lack of a better name). */
51ac9db5 718 struct dwo_sections sections {};
3019eac3 719
33c5cd75
DB
720 /* The CUs in the file.
721 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
722 an extension to handle LLVM's Link Time Optimization output (where
723 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 724 htab_up cus;
3019eac3
DE
725
726 /* Table of TUs in the file.
727 Each element is a struct dwo_unit. */
b0b6a987 728 htab_up tus;
3019eac3
DE
729};
730
80626a55
DE
731/* These sections are what may appear in a DWP file. */
732
733struct dwp_sections
734{
73869dc2 735 /* These are used by both DWP version 1 and 2. */
80626a55
DE
736 struct dwarf2_section_info str;
737 struct dwarf2_section_info cu_index;
738 struct dwarf2_section_info tu_index;
73869dc2
DE
739
740 /* These are only used by DWP version 2 files.
741 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
742 sections are referenced by section number, and are not recorded here.
743 In DWP version 2 there is at most one copy of all these sections, each
744 section being (effectively) comprised of the concatenation of all of the
745 individual sections that exist in the version 1 format.
746 To keep the code simple we treat each of these concatenated pieces as a
747 section itself (a virtual section?). */
748 struct dwarf2_section_info abbrev;
749 struct dwarf2_section_info info;
750 struct dwarf2_section_info line;
751 struct dwarf2_section_info loc;
752 struct dwarf2_section_info macinfo;
753 struct dwarf2_section_info macro;
754 struct dwarf2_section_info str_offsets;
755 struct dwarf2_section_info types;
80626a55
DE
756};
757
73869dc2
DE
758/* These sections are what may appear in a virtual DWO file in DWP version 1.
759 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 760
73869dc2 761struct virtual_v1_dwo_sections
80626a55
DE
762{
763 struct dwarf2_section_info abbrev;
764 struct dwarf2_section_info line;
765 struct dwarf2_section_info loc;
766 struct dwarf2_section_info macinfo;
767 struct dwarf2_section_info macro;
768 struct dwarf2_section_info str_offsets;
769 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 770 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
771 struct dwarf2_section_info info_or_types;
772};
773
73869dc2
DE
774/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
775 In version 2, the sections of the DWO files are concatenated together
776 and stored in one section of that name. Thus each ELF section contains
777 several "virtual" sections. */
778
779struct virtual_v2_dwo_sections
780{
781 bfd_size_type abbrev_offset;
782 bfd_size_type abbrev_size;
783
784 bfd_size_type line_offset;
785 bfd_size_type line_size;
786
787 bfd_size_type loc_offset;
788 bfd_size_type loc_size;
789
790 bfd_size_type macinfo_offset;
791 bfd_size_type macinfo_size;
792
793 bfd_size_type macro_offset;
794 bfd_size_type macro_size;
795
796 bfd_size_type str_offsets_offset;
797 bfd_size_type str_offsets_size;
798
799 /* Each DWP hash table entry records one CU or one TU.
800 That is recorded here, and copied to dwo_unit.section. */
801 bfd_size_type info_or_types_offset;
802 bfd_size_type info_or_types_size;
803};
804
80626a55
DE
805/* Contents of DWP hash tables. */
806
807struct dwp_hash_table
808{
73869dc2 809 uint32_t version, nr_columns;
80626a55 810 uint32_t nr_units, nr_slots;
73869dc2
DE
811 const gdb_byte *hash_table, *unit_table;
812 union
813 {
814 struct
815 {
816 const gdb_byte *indices;
817 } v1;
818 struct
819 {
820 /* This is indexed by column number and gives the id of the section
821 in that column. */
822#define MAX_NR_V2_DWO_SECTIONS \
823 (1 /* .debug_info or .debug_types */ \
824 + 1 /* .debug_abbrev */ \
825 + 1 /* .debug_line */ \
826 + 1 /* .debug_loc */ \
827 + 1 /* .debug_str_offsets */ \
828 + 1 /* .debug_macro or .debug_macinfo */)
829 int section_ids[MAX_NR_V2_DWO_SECTIONS];
830 const gdb_byte *offsets;
831 const gdb_byte *sizes;
832 } v2;
833 } section_pool;
80626a55
DE
834};
835
836/* Data for one DWP file. */
837
838struct dwp_file
839{
400174b1
TT
840 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
841 : name (name_),
842 dbfd (std::move (abfd))
843 {
844 }
845
80626a55
DE
846 /* Name of the file. */
847 const char *name;
848
73869dc2 849 /* File format version. */
400174b1 850 int version = 0;
73869dc2 851
93417882 852 /* The bfd. */
400174b1 853 gdb_bfd_ref_ptr dbfd;
80626a55
DE
854
855 /* Section info for this file. */
400174b1 856 struct dwp_sections sections {};
80626a55 857
57d63ce2 858 /* Table of CUs in the file. */
400174b1 859 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
860
861 /* Table of TUs in the file. */
400174b1 862 const struct dwp_hash_table *tus = nullptr;
80626a55 863
19ac8c2e 864 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
865 htab_up loaded_cus;
866 htab_up loaded_tus;
80626a55 867
73869dc2
DE
868 /* Table to map ELF section numbers to their sections.
869 This is only needed for the DWP V1 file format. */
400174b1
TT
870 unsigned int num_sections = 0;
871 asection **elf_sections = nullptr;
80626a55
DE
872};
873
0963b4bd
MS
874/* Struct used to pass misc. parameters to read_die_and_children, et
875 al. which are used for both .debug_info and .debug_types dies.
876 All parameters here are unchanging for the life of the call. This
dee91e82 877 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
878
879struct die_reader_specs
880{
a32a8923 881 /* The bfd of die_section. */
93311388
DE
882 bfd* abfd;
883
884 /* The CU of the DIE we are parsing. */
885 struct dwarf2_cu *cu;
886
80626a55 887 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
888 struct dwo_file *dwo_file;
889
dee91e82 890 /* The section the die comes from.
3019eac3 891 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
892 struct dwarf2_section_info *die_section;
893
894 /* die_section->buffer. */
d521ce57 895 const gdb_byte *buffer;
f664829e
DE
896
897 /* The end of the buffer. */
898 const gdb_byte *buffer_end;
a2ce51a0 899
685af9cd
TT
900 /* The abbreviation table to use when reading the DIEs. */
901 struct abbrev_table *abbrev_table;
93311388
DE
902};
903
c0ab21c2
TT
904/* A subclass of die_reader_specs that holds storage and has complex
905 constructor and destructor behavior. */
906
907class cutu_reader : public die_reader_specs
908{
909public:
910
911 cutu_reader (struct dwarf2_per_cu_data *this_cu,
912 struct abbrev_table *abbrev_table,
6751ebae 913 int use_existing_cu,
c0ab21c2
TT
914 bool skip_partial);
915
916 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
917 struct dwarf2_cu *parent_cu = nullptr,
918 struct dwo_file *dwo_file = nullptr);
919
c0ab21c2
TT
920 DISABLE_COPY_AND_ASSIGN (cutu_reader);
921
922 const gdb_byte *info_ptr = nullptr;
923 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
924 bool dummy_p = false;
925
6751ebae
TT
926 /* Release the new CU, putting it on the chain. This cannot be done
927 for dummy CUs. */
928 void keep ();
929
c0ab21c2
TT
930private:
931 void init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 932 int use_existing_cu);
c0ab21c2
TT
933
934 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
935 std::unique_ptr<dwarf2_cu> m_new_cu;
936
937 /* The ordinary abbreviation table. */
938 abbrev_table_up m_abbrev_table_holder;
939
940 /* The DWO abbreviation table. */
941 abbrev_table_up m_dwo_abbrev_table;
942};
dee91e82 943
c906108c 944/* When we construct a partial symbol table entry we only
0963b4bd 945 need this much information. */
6f06d47b 946struct partial_die_info : public allocate_on_obstack
c906108c 947 {
6f06d47b
YQ
948 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
949
950 /* Disable assign but still keep copy ctor, which is needed
951 load_partial_dies. */
952 partial_die_info& operator=(const partial_die_info& rhs) = delete;
953
52356b79
YQ
954 /* Adjust the partial die before generating a symbol for it. This
955 function may set the is_external flag or change the DIE's
956 name. */
957 void fixup (struct dwarf2_cu *cu);
958
48fbe735
YQ
959 /* Read a minimal amount of information into the minimal die
960 structure. */
961 const gdb_byte *read (const struct die_reader_specs *reader,
962 const struct abbrev_info &abbrev,
963 const gdb_byte *info_ptr);
964
72bf9492 965 /* Offset of this DIE. */
6f06d47b 966 const sect_offset sect_off;
72bf9492
DJ
967
968 /* DWARF-2 tag for this DIE. */
6f06d47b 969 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 970
72bf9492 971 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
972 const unsigned int has_children : 1;
973
72bf9492
DJ
974 unsigned int is_external : 1;
975 unsigned int is_declaration : 1;
976 unsigned int has_type : 1;
977 unsigned int has_specification : 1;
978 unsigned int has_pc_info : 1;
481860b3 979 unsigned int may_be_inlined : 1;
72bf9492 980
0c1b455e
TT
981 /* This DIE has been marked DW_AT_main_subprogram. */
982 unsigned int main_subprogram : 1;
983
72bf9492
DJ
984 /* Flag set if the SCOPE field of this structure has been
985 computed. */
986 unsigned int scope_set : 1;
987
fa4028e9
JB
988 /* Flag set if the DIE has a byte_size attribute. */
989 unsigned int has_byte_size : 1;
990
ff908ebf
AW
991 /* Flag set if the DIE has a DW_AT_const_value attribute. */
992 unsigned int has_const_value : 1;
993
98bfdba5
PA
994 /* Flag set if any of the DIE's children are template arguments. */
995 unsigned int has_template_arguments : 1;
996
52356b79 997 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
998 unsigned int fixup_called : 1;
999
36586728
TT
1000 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1001 unsigned int is_dwz : 1;
1002
1003 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1004 unsigned int spec_is_dwz : 1;
1005
72bf9492 1006 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1007 sometimes a default name for unnamed DIEs. */
6f06d47b 1008 const char *name = nullptr;
72bf9492 1009
abc72ce4 1010 /* The linkage name, if present. */
6f06d47b 1011 const char *linkage_name = nullptr;
abc72ce4 1012
72bf9492
DJ
1013 /* The scope to prepend to our children. This is generally
1014 allocated on the comp_unit_obstack, so will disappear
1015 when this compilation unit leaves the cache. */
6f06d47b 1016 const char *scope = nullptr;
72bf9492 1017
95554aad
TT
1018 /* Some data associated with the partial DIE. The tag determines
1019 which field is live. */
1020 union
1021 {
1022 /* The location description associated with this DIE, if any. */
1023 struct dwarf_block *locdesc;
1024 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1025 sect_offset sect_off;
6f06d47b 1026 } d {};
72bf9492
DJ
1027
1028 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1029 CORE_ADDR lowpc = 0;
1030 CORE_ADDR highpc = 0;
72bf9492 1031
93311388 1032 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1033 DW_AT_sibling, if any. */
48fbe735
YQ
1034 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1035 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1036 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1037
1038 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1039 DW_AT_specification (or DW_AT_abstract_origin or
1040 DW_AT_extension). */
6f06d47b 1041 sect_offset spec_offset {};
72bf9492
DJ
1042
1043 /* Pointers to this DIE's parent, first child, and next sibling,
1044 if any. */
6f06d47b
YQ
1045 struct partial_die_info *die_parent = nullptr;
1046 struct partial_die_info *die_child = nullptr;
1047 struct partial_die_info *die_sibling = nullptr;
1048
1049 friend struct partial_die_info *
1050 dwarf2_cu::find_partial_die (sect_offset sect_off);
1051
1052 private:
1053 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1054 partial_die_info (sect_offset sect_off)
1055 : partial_die_info (sect_off, DW_TAG_padding, 0)
1056 {
1057 }
1058
1059 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1060 int has_children_)
1061 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1062 {
1063 is_external = 0;
1064 is_declaration = 0;
1065 has_type = 0;
1066 has_specification = 0;
1067 has_pc_info = 0;
1068 may_be_inlined = 0;
1069 main_subprogram = 0;
1070 scope_set = 0;
1071 has_byte_size = 0;
1072 has_const_value = 0;
1073 has_template_arguments = 0;
1074 fixup_called = 0;
1075 is_dwz = 0;
1076 spec_is_dwz = 0;
1077 }
c906108c
SS
1078 };
1079
c906108c
SS
1080/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1081 but this would require a corresponding change in unpack_field_as_long
1082 and friends. */
1083static int bits_per_byte = 8;
1084
9c6a1327
TT
1085struct variant_part_builder;
1086
1087/* When reading a variant, we track a bit more information about the
1088 field, and store it in an object of this type. */
2ddeaf8a
TT
1089
1090struct variant_field
1091{
9c6a1327
TT
1092 int first_field = -1;
1093 int last_field = -1;
1094
1095 /* A variant can contain other variant parts. */
1096 std::vector<variant_part_builder> variant_parts;
1097
2ddeaf8a
TT
1098 /* If we see a DW_TAG_variant, then this will be set if this is the
1099 default branch. */
9c6a1327
TT
1100 bool default_branch = false;
1101 /* If we see a DW_AT_discr_value, then this will be the discriminant
1102 value. */
1103 ULONGEST discriminant_value = 0;
1104 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1105 data. */
1106 struct dwarf_block *discr_list_data = nullptr;
1107};
1108
1109/* This represents a DW_TAG_variant_part. */
1110
1111struct variant_part_builder
1112{
1113 /* The offset of the discriminant field. */
1114 sect_offset discriminant_offset {};
1115
1116 /* Variants that are direct children of this variant part. */
1117 std::vector<variant_field> variants;
1118
1119 /* True if we're currently reading a variant. */
1120 bool processing_variant = false;
2ddeaf8a
TT
1121};
1122
52059ffd
TT
1123struct nextfield
1124{
be2daae6
TT
1125 int accessibility = 0;
1126 int virtuality = 0;
9c6a1327
TT
1127 /* Variant parts need to find the discriminant, which is a DIE
1128 reference. We track the section offset of each field to make
1129 this link. */
1130 sect_offset offset;
be2daae6 1131 struct field field {};
52059ffd
TT
1132};
1133
1134struct fnfieldlist
1135{
be2daae6
TT
1136 const char *name = nullptr;
1137 std::vector<struct fn_field> fnfields;
52059ffd
TT
1138};
1139
c906108c
SS
1140/* The routines that read and process dies for a C struct or C++ class
1141 pass lists of data member fields and lists of member function fields
1142 in an instance of a field_info structure, as defined below. */
1143struct field_info
c5aa993b 1144 {
0963b4bd 1145 /* List of data member and baseclasses fields. */
be2daae6
TT
1146 std::vector<struct nextfield> fields;
1147 std::vector<struct nextfield> baseclasses;
c906108c 1148
85102364 1149 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1150 int non_public_fields = 0;
c906108c 1151
c5aa993b
JM
1152 /* Member function fieldlist array, contains name of possibly overloaded
1153 member function, number of overloaded member functions and a pointer
1154 to the head of the member function field chain. */
be2daae6 1155 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1156
1157 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1158 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1159 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1160
1161 /* Nested types defined by this class and the number of elements in this
1162 list. */
be2daae6 1163 std::vector<struct decl_field> nested_types_list;
317f7127 1164
9c6a1327
TT
1165 /* If non-null, this is the variant part we are currently
1166 reading. */
1167 variant_part_builder *current_variant_part = nullptr;
1168 /* This holds all the top-level variant parts attached to the type
1169 we're reading. */
1170 std::vector<variant_part_builder> variant_parts;
1171
317f7127
TT
1172 /* Return the total number of fields (including baseclasses). */
1173 int nfields () const
1174 {
1175 return fields.size () + baseclasses.size ();
1176 }
c5aa993b 1177 };
c906108c 1178
ae038cb0
DJ
1179/* Loaded secondary compilation units are kept in memory until they
1180 have not been referenced for the processing of this many
1181 compilation units. Set this to zero to disable caching. Cache
1182 sizes of up to at least twenty will improve startup time for
1183 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1184static int dwarf_max_cache_age = 5;
920d2a44 1185static void
b4f54984
DE
1186show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1187 struct cmd_list_element *c, const char *value)
920d2a44 1188{
3e43a32a 1189 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1190 "DWARF compilation units is %s.\n"),
920d2a44
AC
1191 value);
1192}
4390d890 1193\f
c906108c
SS
1194/* local function prototypes */
1195
918dd910
JK
1196static void dwarf2_find_base_address (struct die_info *die,
1197 struct dwarf2_cu *cu);
1198
891813be 1199static dwarf2_psymtab *create_partial_symtab
0018ea6f
DE
1200 (struct dwarf2_per_cu_data *per_cu, const char *name);
1201
f1902523
JK
1202static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1203 const gdb_byte *info_ptr,
3e225074 1204 struct die_info *type_unit_die);
f1902523 1205
ed2dc618
SM
1206static void dwarf2_build_psymtabs_hard
1207 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1208
72bf9492
DJ
1209static void scan_partial_symbols (struct partial_die_info *,
1210 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1211 int, struct dwarf2_cu *);
c906108c 1212
72bf9492
DJ
1213static void add_partial_symbol (struct partial_die_info *,
1214 struct dwarf2_cu *);
63d06c5c 1215
72bf9492
DJ
1216static void add_partial_namespace (struct partial_die_info *pdi,
1217 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1218 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1219
5d7cb8df 1220static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1221 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1222 struct dwarf2_cu *cu);
1223
72bf9492
DJ
1224static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1225 struct dwarf2_cu *cu);
91c24f0a 1226
bc30ff58
JB
1227static void add_partial_subprogram (struct partial_die_info *pdi,
1228 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1229 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1230
d521ce57 1231static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1232
dee91e82 1233static struct partial_die_info *load_partial_dies
d521ce57 1234 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1235
fb816e8b
TV
1236/* A pair of partial_die_info and compilation unit. */
1237struct cu_partial_die_info
1238{
1239 /* The compilation unit of the partial_die_info. */
1240 struct dwarf2_cu *cu;
1241 /* A partial_die_info. */
1242 struct partial_die_info *pdi;
122cf0f2
AB
1243
1244 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1245 : cu (cu),
1246 pdi (pdi)
405feb71 1247 { /* Nothing. */ }
122cf0f2
AB
1248
1249private:
1250 cu_partial_die_info () = delete;
fb816e8b
TV
1251};
1252
122cf0f2
AB
1253static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1254 struct dwarf2_cu *);
72bf9492 1255
d521ce57
TT
1256static const gdb_byte *read_attribute (const struct die_reader_specs *,
1257 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1258 const gdb_byte *, bool *need_reprocess);
1259
1260static void read_attribute_reprocess (const struct die_reader_specs *reader,
1261 struct attribute *attr);
1262
1263static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1264
ed2dc618
SM
1265static sect_offset read_abbrev_offset
1266 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1267 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1268
ed2dc618
SM
1269static const char *read_indirect_string
1270 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1271 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1272
ed2dc618 1273static const char *read_indirect_string_at_offset
4f44ae6c 1274 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1275
d521ce57
TT
1276static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1277 const gdb_byte *,
3019eac3
DE
1278 unsigned int *);
1279
18a8505e
AT
1280static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1281 ULONGEST str_index);
1282
1283static const char *read_stub_str_index (struct dwarf2_cu *cu,
1284 ULONGEST str_index);
3019eac3 1285
e142c38c 1286static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1287
e142c38c
DJ
1288static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1289 struct dwarf2_cu *);
c906108c 1290
7d45c7c3
KB
1291static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1292 struct dwarf2_cu *cu);
1293
a084a2a6
AT
1294static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1295
05cf31d1
JB
1296static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1297 struct dwarf2_cu *cu);
1298
e142c38c 1299static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1300
e142c38c 1301static struct die_info *die_specification (struct die_info *die,
f2f0e013 1302 struct dwarf2_cu **);
63d06c5c 1303
9c541725 1304static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1305 struct dwarf2_cu *cu);
debd256d 1306
f3f5162e 1307static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1308 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1309 CORE_ADDR, int decode_mapping);
c906108c 1310
804d2729
TT
1311static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1312 const char *);
c906108c 1313
a14ed312 1314static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1315 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1316
ff39bb5e 1317static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1318 struct dwarf2_cu *);
c906108c 1319
ff39bb5e 1320static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1321 struct type *type,
1322 const char *name,
1323 struct obstack *obstack,
12df843f 1324 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1325 const gdb_byte **bytes,
98bfdba5 1326 struct dwarf2_locexpr_baton **baton);
2df3850c 1327
e7c27a73 1328static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1329
b4ba55a1
JB
1330static int need_gnat_info (struct dwarf2_cu *);
1331
3e43a32a
MS
1332static struct type *die_descriptive_type (struct die_info *,
1333 struct dwarf2_cu *);
b4ba55a1
JB
1334
1335static void set_descriptive_type (struct type *, struct die_info *,
1336 struct dwarf2_cu *);
1337
e7c27a73
DJ
1338static struct type *die_containing_type (struct die_info *,
1339 struct dwarf2_cu *);
c906108c 1340
ff39bb5e 1341static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1342 struct dwarf2_cu *);
c906108c 1343
f792889a 1344static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1345
673bfd45
DE
1346static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1347
0d5cff50 1348static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1349
6e70227d 1350static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1351 const char *suffix, int physname,
1352 struct dwarf2_cu *cu);
63d06c5c 1353
e7c27a73 1354static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1355
348e048f
DE
1356static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1357
e7c27a73 1358static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1359
e7c27a73 1360static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1361
96408a79
SA
1362static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1363
71a3c369
TT
1364static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1365
ff013f42 1366static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1367 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1368
41144253 1369/* Return the .debug_loclists section to use for cu. */
1370static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1371
3a2b436a 1372/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1373 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1374enum pc_bounds_kind
1375{
e385593e 1376 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1377 PC_BOUNDS_NOT_PRESENT,
1378
e385593e
JK
1379 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1380 were present but they do not form a valid range of PC addresses. */
1381 PC_BOUNDS_INVALID,
1382
3a2b436a
JK
1383 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1384 PC_BOUNDS_RANGES,
1385
1386 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1387 PC_BOUNDS_HIGH_LOW,
1388};
1389
1390static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1391 CORE_ADDR *, CORE_ADDR *,
1392 struct dwarf2_cu *,
891813be 1393 dwarf2_psymtab *);
c906108c 1394
fae299cd
DC
1395static void get_scope_pc_bounds (struct die_info *,
1396 CORE_ADDR *, CORE_ADDR *,
1397 struct dwarf2_cu *);
1398
801e3a5b
JB
1399static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1400 CORE_ADDR, struct dwarf2_cu *);
1401
a14ed312 1402static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1403 struct dwarf2_cu *);
c906108c 1404
a14ed312 1405static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1406 struct type *, struct dwarf2_cu *);
c906108c 1407
a14ed312 1408static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1409 struct die_info *, struct type *,
e7c27a73 1410 struct dwarf2_cu *);
c906108c 1411
a14ed312 1412static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1413 struct type *,
1414 struct dwarf2_cu *);
c906108c 1415
134d01f1 1416static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1417
e7c27a73 1418static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1419
e7c27a73 1420static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1421
5d7cb8df
JK
1422static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1423
804d2729 1424static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1425
27aa8d6a
SW
1426static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1427
74921315
KS
1428static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1429
f55ee35c
JK
1430static struct type *read_module_type (struct die_info *die,
1431 struct dwarf2_cu *cu);
1432
38d518c9 1433static const char *namespace_name (struct die_info *die,
e142c38c 1434 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1435
134d01f1 1436static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1437
7d79de9a
TT
1438static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1439 bool * = nullptr);
c906108c 1440
6e70227d 1441static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1442 struct dwarf2_cu *);
1443
bf6af496 1444static struct die_info *read_die_and_siblings_1
d521ce57 1445 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1446 struct die_info *);
639d11d3 1447
dee91e82 1448static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1449 const gdb_byte *info_ptr,
1450 const gdb_byte **new_info_ptr,
639d11d3
DC
1451 struct die_info *parent);
1452
d521ce57
TT
1453static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1454 struct die_info **, const gdb_byte *,
3e225074 1455 int);
3019eac3 1456
d521ce57 1457static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1458 struct die_info **, const gdb_byte *);
93311388 1459
e7c27a73 1460static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1461
15d034d0 1462static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1463 struct objfile *);
71c25dea 1464
15d034d0 1465static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1466
15d034d0 1467static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1468 struct die_info *die,
1469 struct dwarf2_cu *cu);
1470
ca69b9e6
DE
1471static const char *dwarf2_physname (const char *name, struct die_info *die,
1472 struct dwarf2_cu *cu);
1473
e142c38c 1474static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1475 struct dwarf2_cu **);
9219021c 1476
d97bc12b
DE
1477static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1478
1479static void dump_die_for_error (struct die_info *);
1480
1481static void dump_die_1 (struct ui_file *, int level, int max_level,
1482 struct die_info *);
c906108c 1483
d97bc12b 1484/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1485
51545339 1486static void store_in_ref_table (struct die_info *,
10b3939b 1487 struct dwarf2_cu *);
c906108c 1488
348e048f 1489static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1490 const struct attribute *,
348e048f
DE
1491 struct dwarf2_cu **);
1492
10b3939b 1493static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1494 const struct attribute *,
f2f0e013 1495 struct dwarf2_cu **);
c906108c 1496
348e048f 1497static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1498 const struct attribute *,
348e048f
DE
1499 struct dwarf2_cu **);
1500
ac9ec31b
DE
1501static struct type *get_signatured_type (struct die_info *, ULONGEST,
1502 struct dwarf2_cu *);
1503
1504static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1505 const struct attribute *,
ac9ec31b
DE
1506 struct dwarf2_cu *);
1507
e5fe5e75 1508static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
348e048f 1509
52dc124a 1510static void read_signatured_type (struct signatured_type *);
348e048f 1511
63e43d3a
PMR
1512static int attr_to_dynamic_prop (const struct attribute *attr,
1513 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1514 struct dynamic_prop *prop, struct type *type);
63e43d3a 1515
c906108c
SS
1516/* memory allocation interface */
1517
7b5a2f43 1518static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1519
b60c80d6 1520static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1521
43f3e411 1522static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1523
8cf6f0b1
TT
1524static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1525 struct dwarf2_loclist_baton *baton,
ff39bb5e 1526 const struct attribute *attr);
8cf6f0b1 1527
ff39bb5e 1528static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1529 struct symbol *sym,
f1e6e072
TT
1530 struct dwarf2_cu *cu,
1531 int is_block);
4c2df51b 1532
d521ce57
TT
1533static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1534 const gdb_byte *info_ptr,
1535 struct abbrev_info *abbrev);
4bb7a0a7 1536
72bf9492
DJ
1537static hashval_t partial_die_hash (const void *item);
1538
1539static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1540
ae038cb0 1541static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618
SM
1542 (sect_offset sect_off, unsigned int offset_in_dwz,
1543 struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1544
9816fde3 1545static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1546 struct die_info *comp_unit_die,
1547 enum language pretend_language);
93311388 1548
ed2dc618 1549static void age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1550
dee91e82 1551static void free_one_cached_comp_unit (struct dwarf2_per_cu_data *);
ae038cb0 1552
f792889a
DJ
1553static struct type *set_die_type (struct die_info *, struct type *,
1554 struct dwarf2_cu *);
1c379e20 1555
ed2dc618 1556static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1557
ed2dc618 1558static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1559
58f0c718 1560static void load_full_comp_unit (struct dwarf2_per_cu_data *, bool,
95554aad 1561 enum language);
10b3939b 1562
95554aad
TT
1563static void process_full_comp_unit (struct dwarf2_per_cu_data *,
1564 enum language);
10b3939b 1565
f4dc4d17
DE
1566static void process_full_type_unit (struct dwarf2_per_cu_data *,
1567 enum language);
1568
10b3939b
DJ
1569static void dwarf2_add_dependence (struct dwarf2_cu *,
1570 struct dwarf2_per_cu_data *);
1571
ae038cb0
DJ
1572static void dwarf2_mark (struct dwarf2_cu *);
1573
1574static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1575
b64f50a1 1576static struct type *get_die_type_at_offset (sect_offset,
ac9ec31b 1577 struct dwarf2_per_cu_data *);
673bfd45 1578
f792889a 1579static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1580
95554aad
TT
1581static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1582 enum language pretend_language);
1583
ed2dc618 1584static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1585
b303c6f6
AB
1586/* Class, the destructor of which frees all allocated queue entries. This
1587 will only have work to do if an error was thrown while processing the
1588 dwarf. If no error was thrown then the queue entries should have all
1589 been processed, and freed, as we went along. */
1590
1591class dwarf2_queue_guard
1592{
1593public:
39856def
TT
1594 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1595 : m_per_objfile (per_objfile)
1596 {
1597 }
b303c6f6
AB
1598
1599 /* Free any entries remaining on the queue. There should only be
1600 entries left if we hit an error while processing the dwarf. */
1601 ~dwarf2_queue_guard ()
1602 {
39856def
TT
1603 /* Ensure that no memory is allocated by the queue. */
1604 std::queue<dwarf2_queue_item> empty;
1605 std::swap (m_per_objfile->queue, empty);
1606 }
b303c6f6 1607
39856def 1608 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1609
39856def
TT
1610private:
1611 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1612};
1613
39856def
TT
1614dwarf2_queue_item::~dwarf2_queue_item ()
1615{
1616 /* Anything still marked queued is likely to be in an
1617 inconsistent state, so discard it. */
1618 if (per_cu->queued)
1619 {
1620 if (per_cu->cu != NULL)
1621 free_one_cached_comp_unit (per_cu);
1622 per_cu->queued = 0;
1623 }
1624}
1625
d721ba37
PA
1626/* The return type of find_file_and_directory. Note, the enclosed
1627 string pointers are only valid while this object is valid. */
1628
1629struct file_and_directory
1630{
1631 /* The filename. This is never NULL. */
1632 const char *name;
1633
1634 /* The compilation directory. NULL if not known. If we needed to
1635 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1636 points directly to the DW_AT_comp_dir string attribute owned by
1637 the obstack that owns the DIE. */
1638 const char *comp_dir;
1639
1640 /* If we needed to build a new string for comp_dir, this is what
1641 owns the storage. */
1642 std::string comp_dir_storage;
1643};
1644
1645static file_and_directory find_file_and_directory (struct die_info *die,
1646 struct dwarf2_cu *cu);
9291a0cd 1647
298e9637 1648static htab_up allocate_signatured_type_table ();
1fd400ff 1649
298e9637 1650static htab_up allocate_dwo_unit_table ();
3019eac3 1651
57d63ce2 1652static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1653 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1654 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1655 ULONGEST signature, int is_debug_types);
a2ce51a0 1656
ed2dc618
SM
1657static struct dwp_file *get_dwp_file
1658 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1659
3019eac3 1660static struct dwo_unit *lookup_dwo_comp_unit
a1855c1d 1661 (struct dwarf2_per_cu_data *, const char *, const char *, ULONGEST);
3019eac3
DE
1662
1663static struct dwo_unit *lookup_dwo_type_unit
a1855c1d 1664 (struct signatured_type *, const char *, const char *);
3019eac3 1665
89e63ee4
DE
1666static void queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *);
1667
263db9a1
TT
1668/* A unique pointer to a dwo_file. */
1669
51ac9db5 1670typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1671
ed2dc618 1672static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1673
1b80a9fa 1674static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1675
1676static void free_line_header_voidp (void *arg);
4390d890
DE
1677\f
1678/* Various complaints about symbol reading that don't abort the process. */
1679
4390d890
DE
1680static void
1681dwarf2_debug_line_missing_file_complaint (void)
1682{
b98664d3 1683 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1684}
1685
1686static void
1687dwarf2_debug_line_missing_end_sequence_complaint (void)
1688{
b98664d3 1689 complaint (_(".debug_line section has line "
4390d890
DE
1690 "program sequence without an end"));
1691}
1692
1693static void
1694dwarf2_complex_location_expr_complaint (void)
1695{
b98664d3 1696 complaint (_("location expression too complex"));
4390d890
DE
1697}
1698
1699static void
1700dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1701 int arg3)
1702{
b98664d3 1703 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1704 arg1, arg2, arg3);
1705}
1706
4390d890
DE
1707static void
1708dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1709{
b98664d3 1710 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1711 arg1, arg2);
1712}
527f3840
JK
1713
1714/* Hash function for line_header_hash. */
1715
1716static hashval_t
1717line_header_hash (const struct line_header *ofs)
1718{
9c541725 1719 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1720}
1721
1722/* Hash function for htab_create_alloc_ex for line_header_hash. */
1723
1724static hashval_t
1725line_header_hash_voidp (const void *item)
1726{
9a3c8263 1727 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1728
1729 return line_header_hash (ofs);
1730}
1731
1732/* Equality function for line_header_hash. */
1733
1734static int
1735line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1736{
9a3c8263
SM
1737 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1738 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1739
9c541725 1740 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1741 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1742}
1743
4390d890 1744\f
9291a0cd 1745
330cdd98
PA
1746/* See declaration. */
1747
1748dwarf2_per_objfile::dwarf2_per_objfile (struct objfile *objfile_,
4b610737
TT
1749 const dwarf2_debug_sections *names,
1750 bool can_copy_)
1751 : objfile (objfile_),
1752 can_copy (can_copy_)
330cdd98
PA
1753{
1754 if (names == NULL)
1755 names = &dwarf2_elf_names;
1756
1757 bfd *obfd = objfile->obfd;
1758
1759 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1760 locate_sections (obfd, sec, *names);
1761}
1762
1763dwarf2_per_objfile::~dwarf2_per_objfile ()
1764{
1765 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
1766 free_cached_comp_units ();
1767
b76e467d 1768 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1769 per_cu->imported_symtabs_free ();
fc8e7e75 1770
b2bdb8cf 1771 for (signatured_type *sig_type : all_type_units)
ae640021 1772 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1773
330cdd98
PA
1774 /* Everything else should be on the objfile obstack. */
1775}
1776
1777/* See declaration. */
1778
1779void
1780dwarf2_per_objfile::free_cached_comp_units ()
1781{
1782 dwarf2_per_cu_data *per_cu = read_in_chain;
1783 dwarf2_per_cu_data **last_chain = &read_in_chain;
1784 while (per_cu != NULL)
1785 {
1786 dwarf2_per_cu_data *next_cu = per_cu->cu->read_in_chain;
1787
fcd3b13d 1788 delete per_cu->cu;
330cdd98
PA
1789 *last_chain = next_cu;
1790 per_cu = next_cu;
1791 }
1792}
1793
11ed8cad
TT
1794/* A helper class that calls free_cached_comp_units on
1795 destruction. */
1796
1797class free_cached_comp_units
1798{
1799public:
1800
1801 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1802 : m_per_objfile (per_objfile)
1803 {
1804 }
1805
1806 ~free_cached_comp_units ()
1807 {
1808 m_per_objfile->free_cached_comp_units ();
1809 }
1810
1811 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1812
1813private:
1814
1815 dwarf2_per_objfile *m_per_objfile;
1816};
1817
c906108c 1818/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1819 information and return true if we have enough to do something.
1820 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1821 ELF names are used. CAN_COPY is true for formats where symbol
1822 interposition is possible and so symbol values must follow copy
1823 relocation rules. */
c906108c
SS
1824
1825int
251d32d9 1826dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1827 const struct dwarf2_debug_sections *names,
1828 bool can_copy)
c906108c 1829{
97cbe998
SDJ
1830 if (objfile->flags & OBJF_READNEVER)
1831 return 0;
1832
ed2dc618
SM
1833 struct dwarf2_per_objfile *dwarf2_per_objfile
1834 = get_dwarf2_per_objfile (objfile);
1835
1836 if (dwarf2_per_objfile == NULL)
5bfd760d 1837 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile,
4b610737
TT
1838 names,
1839 can_copy);
5bfd760d 1840
73869dc2 1841 return (!dwarf2_per_objfile->info.is_virtual
049412e3 1842 && dwarf2_per_objfile->info.s.section != NULL
73869dc2 1843 && !dwarf2_per_objfile->abbrev.is_virtual
049412e3 1844 && dwarf2_per_objfile->abbrev.s.section != NULL);
73869dc2
DE
1845}
1846
251d32d9
TG
1847/* When loading sections, we look either for uncompressed section or for
1848 compressed section names. */
233a11ab
CS
1849
1850static int
251d32d9
TG
1851section_is_p (const char *section_name,
1852 const struct dwarf2_section_names *names)
233a11ab 1853{
251d32d9
TG
1854 if (names->normal != NULL
1855 && strcmp (section_name, names->normal) == 0)
1856 return 1;
1857 if (names->compressed != NULL
1858 && strcmp (section_name, names->compressed) == 0)
1859 return 1;
1860 return 0;
233a11ab
CS
1861}
1862
330cdd98 1863/* See declaration. */
c906108c 1864
330cdd98
PA
1865void
1866dwarf2_per_objfile::locate_sections (bfd *abfd, asection *sectp,
1867 const dwarf2_debug_sections &names)
c906108c 1868{
fd361982 1869 flagword aflag = bfd_section_flags (sectp);
251d32d9 1870
dc7650b8
JK
1871 if ((aflag & SEC_HAS_CONTENTS) == 0)
1872 {
1873 }
950b7495
KS
1874 else if (elf_section_data (sectp)->this_hdr.sh_size
1875 > bfd_get_file_size (abfd))
1876 {
1877 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1878 warning (_("Discarding section %s which has a section size (%s"
1879 ") larger than the file size [in module %s]"),
1880 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1881 bfd_get_filename (abfd));
1882 }
330cdd98 1883 else if (section_is_p (sectp->name, &names.info))
c906108c 1884 {
330cdd98 1885 this->info.s.section = sectp;
fd361982 1886 this->info.size = bfd_section_size (sectp);
c906108c 1887 }
330cdd98 1888 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1889 {
330cdd98 1890 this->abbrev.s.section = sectp;
fd361982 1891 this->abbrev.size = bfd_section_size (sectp);
c906108c 1892 }
330cdd98 1893 else if (section_is_p (sectp->name, &names.line))
c906108c 1894 {
330cdd98 1895 this->line.s.section = sectp;
fd361982 1896 this->line.size = bfd_section_size (sectp);
c906108c 1897 }
330cdd98 1898 else if (section_is_p (sectp->name, &names.loc))
c906108c 1899 {
330cdd98 1900 this->loc.s.section = sectp;
fd361982 1901 this->loc.size = bfd_section_size (sectp);
c906108c 1902 }
330cdd98 1903 else if (section_is_p (sectp->name, &names.loclists))
43988095 1904 {
330cdd98 1905 this->loclists.s.section = sectp;
fd361982 1906 this->loclists.size = bfd_section_size (sectp);
43988095 1907 }
330cdd98 1908 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1909 {
330cdd98 1910 this->macinfo.s.section = sectp;
fd361982 1911 this->macinfo.size = bfd_section_size (sectp);
c906108c 1912 }
330cdd98 1913 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1914 {
330cdd98 1915 this->macro.s.section = sectp;
fd361982 1916 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1917 }
330cdd98 1918 else if (section_is_p (sectp->name, &names.str))
c906108c 1919 {
330cdd98 1920 this->str.s.section = sectp;
fd361982 1921 this->str.size = bfd_section_size (sectp);
c906108c 1922 }
18a8505e
AT
1923 else if (section_is_p (sectp->name, &names.str_offsets))
1924 {
1925 this->str_offsets.s.section = sectp;
1926 this->str_offsets.size = bfd_section_size (sectp);
1927 }
330cdd98 1928 else if (section_is_p (sectp->name, &names.line_str))
43988095 1929 {
330cdd98 1930 this->line_str.s.section = sectp;
fd361982 1931 this->line_str.size = bfd_section_size (sectp);
43988095 1932 }
330cdd98 1933 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1934 {
330cdd98 1935 this->addr.s.section = sectp;
fd361982 1936 this->addr.size = bfd_section_size (sectp);
3019eac3 1937 }
330cdd98 1938 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1939 {
330cdd98 1940 this->frame.s.section = sectp;
fd361982 1941 this->frame.size = bfd_section_size (sectp);
b6af0555 1942 }
330cdd98 1943 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1944 {
330cdd98 1945 this->eh_frame.s.section = sectp;
fd361982 1946 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1947 }
330cdd98 1948 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1949 {
330cdd98 1950 this->ranges.s.section = sectp;
fd361982 1951 this->ranges.size = bfd_section_size (sectp);
af34e669 1952 }
330cdd98 1953 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1954 {
330cdd98 1955 this->rnglists.s.section = sectp;
fd361982 1956 this->rnglists.size = bfd_section_size (sectp);
43988095 1957 }
330cdd98 1958 else if (section_is_p (sectp->name, &names.types))
348e048f 1959 {
8b70b953
TT
1960 struct dwarf2_section_info type_section;
1961
1962 memset (&type_section, 0, sizeof (type_section));
049412e3 1963 type_section.s.section = sectp;
fd361982 1964 type_section.size = bfd_section_size (sectp);
8b70b953 1965
fd5866f6 1966 this->types.push_back (type_section);
348e048f 1967 }
330cdd98 1968 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1969 {
330cdd98 1970 this->gdb_index.s.section = sectp;
fd361982 1971 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1972 }
927aa2e7
JK
1973 else if (section_is_p (sectp->name, &names.debug_names))
1974 {
1975 this->debug_names.s.section = sectp;
fd361982 1976 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1977 }
1978 else if (section_is_p (sectp->name, &names.debug_aranges))
1979 {
1980 this->debug_aranges.s.section = sectp;
fd361982 1981 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 1982 }
dce234bc 1983
fd361982
AM
1984 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
1985 && bfd_section_vma (sectp) == 0)
330cdd98 1986 this->has_section_at_zero = true;
c906108c
SS
1987}
1988
dce234bc 1989/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 1990 SECTION_NAME. */
af34e669 1991
dce234bc 1992void
3017a003
TG
1993dwarf2_get_section_info (struct objfile *objfile,
1994 enum dwarf2_section_enum sect,
d521ce57 1995 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
1996 bfd_size_type *sizep)
1997{
5bfd760d 1998 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 1999 struct dwarf2_section_info *info;
a3b2a86b
TT
2000
2001 /* We may see an objfile without any DWARF, in which case we just
2002 return nothing. */
2003 if (data == NULL)
2004 {
2005 *sectp = NULL;
2006 *bufp = NULL;
2007 *sizep = 0;
2008 return;
2009 }
3017a003
TG
2010 switch (sect)
2011 {
2012 case DWARF2_DEBUG_FRAME:
2013 info = &data->frame;
2014 break;
2015 case DWARF2_EH_FRAME:
2016 info = &data->eh_frame;
2017 break;
2018 default:
2019 gdb_assert_not_reached ("unexpected section");
2020 }
dce234bc 2021
96b79293 2022 info->read (objfile);
dce234bc 2023
96b79293 2024 *sectp = info->get_bfd_section ();
dce234bc
PP
2025 *bufp = info->buffer;
2026 *sizep = info->size;
2027}
2028
36586728
TT
2029/* A helper function to find the sections for a .dwz file. */
2030
2031static void
2032locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2033{
9a3c8263 2034 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2035
2036 /* Note that we only support the standard ELF names, because .dwz
2037 is ELF-only (at the time of writing). */
2038 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2039 {
049412e3 2040 dwz_file->abbrev.s.section = sectp;
fd361982 2041 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2042 }
2043 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2044 {
049412e3 2045 dwz_file->info.s.section = sectp;
fd361982 2046 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2047 }
2048 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2049 {
049412e3 2050 dwz_file->str.s.section = sectp;
fd361982 2051 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2052 }
2053 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2054 {
049412e3 2055 dwz_file->line.s.section = sectp;
fd361982 2056 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2057 }
2058 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2059 {
049412e3 2060 dwz_file->macro.s.section = sectp;
fd361982 2061 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2062 }
2ec9a5e0
TT
2063 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2064 {
049412e3 2065 dwz_file->gdb_index.s.section = sectp;
fd361982 2066 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2067 }
927aa2e7
JK
2068 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2069 {
2070 dwz_file->debug_names.s.section = sectp;
fd361982 2071 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2072 }
36586728
TT
2073}
2074
c4973306 2075/* See dwarf2read.h. */
36586728 2076
c4973306 2077struct dwz_file *
ed2dc618 2078dwarf2_get_dwz_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 2079{
36586728 2080 const char *filename;
acd13123 2081 bfd_size_type buildid_len_arg;
dc294be5
TT
2082 size_t buildid_len;
2083 bfd_byte *buildid;
36586728
TT
2084
2085 if (dwarf2_per_objfile->dwz_file != NULL)
7ff8cb8c 2086 return dwarf2_per_objfile->dwz_file.get ();
36586728 2087
4db1a1dc 2088 bfd_set_error (bfd_error_no_error);
791afaa2
TT
2089 gdb::unique_xmalloc_ptr<char> data
2090 (bfd_get_alt_debug_link_info (dwarf2_per_objfile->objfile->obfd,
2091 &buildid_len_arg, &buildid));
4db1a1dc
TT
2092 if (data == NULL)
2093 {
2094 if (bfd_get_error () == bfd_error_no_error)
2095 return NULL;
2096 error (_("could not read '.gnu_debugaltlink' section: %s"),
2097 bfd_errmsg (bfd_get_error ()));
2098 }
791afaa2
TT
2099
2100 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2101
acd13123
TT
2102 buildid_len = (size_t) buildid_len_arg;
2103
791afaa2 2104 filename = data.get ();
d721ba37
PA
2105
2106 std::string abs_storage;
36586728
TT
2107 if (!IS_ABSOLUTE_PATH (filename))
2108 {
14278e1f
TT
2109 gdb::unique_xmalloc_ptr<char> abs
2110 = gdb_realpath (objfile_name (dwarf2_per_objfile->objfile));
36586728 2111
14278e1f 2112 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2113 filename = abs_storage.c_str ();
36586728
TT
2114 }
2115
dc294be5
TT
2116 /* First try the file name given in the section. If that doesn't
2117 work, try to use the build-id instead. */
192b62ce 2118 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget, -1));
dc294be5 2119 if (dwz_bfd != NULL)
36586728 2120 {
192b62ce 2121 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2122 dwz_bfd.reset (nullptr);
36586728
TT
2123 }
2124
dc294be5
TT
2125 if (dwz_bfd == NULL)
2126 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2127
0d79cdc4
AM
2128 if (dwz_bfd == nullptr)
2129 {
2130 gdb::unique_xmalloc_ptr<char> alt_filename;
2131 const char *origname = dwarf2_per_objfile->objfile->original_name;
2132
2133 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2134 buildid_len,
2135 origname,
2136 &alt_filename));
2137
2138 if (fd.get () >= 0)
2139 {
2140 /* File successfully retrieved from server. */
2141 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget, -1);
2142
2143 if (dwz_bfd == nullptr)
2144 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2145 alt_filename.get ());
2146 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2147 dwz_bfd.reset (nullptr);
2148 }
2149 }
2150
dc294be5
TT
2151 if (dwz_bfd == NULL)
2152 error (_("could not find '.gnu_debugaltlink' file for %s"),
2153 objfile_name (dwarf2_per_objfile->objfile));
2154
7ff8cb8c
TT
2155 std::unique_ptr<struct dwz_file> result
2156 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2157
7ff8cb8c
TT
2158 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2159 result.get ());
36586728 2160
7ff8cb8c
TT
2161 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd,
2162 result->dwz_bfd.get ());
2163 dwarf2_per_objfile->dwz_file = std::move (result);
2164 return dwarf2_per_objfile->dwz_file.get ();
36586728 2165}
9291a0cd 2166\f
7b9f3c50
DE
2167/* DWARF quick_symbols_functions support. */
2168
2169/* TUs can share .debug_line entries, and there can be a lot more TUs than
2170 unique line tables, so we maintain a separate table of all .debug_line
2171 derived entries to support the sharing.
2172 All the quick functions need is the list of file names. We discard the
2173 line_header when we're done and don't need to record it here. */
2174struct quick_file_names
2175{
094b34ac
DE
2176 /* The data used to construct the hash key. */
2177 struct stmt_list_hash hash;
7b9f3c50
DE
2178
2179 /* The number of entries in file_names, real_names. */
2180 unsigned int num_file_names;
2181
2182 /* The file names from the line table, after being run through
2183 file_full_name. */
2184 const char **file_names;
2185
2186 /* The file names from the line table after being run through
2187 gdb_realpath. These are computed lazily. */
2188 const char **real_names;
2189};
2190
2191/* When using the index (and thus not using psymtabs), each CU has an
2192 object of this type. This is used to hold information needed by
2193 the various "quick" methods. */
2194struct dwarf2_per_cu_quick_data
2195{
2196 /* The file table. This can be NULL if there was no file table
2197 or it's currently not read in.
2198 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
2199 struct quick_file_names *file_names;
2200
2201 /* The corresponding symbol table. This is NULL if symbols for this
2202 CU have not yet been read. */
43f3e411 2203 struct compunit_symtab *compunit_symtab;
7b9f3c50
DE
2204
2205 /* A temporary mark bit used when iterating over all CUs in
2206 expand_symtabs_matching. */
2207 unsigned int mark : 1;
2208
2209 /* True if we've tried to read the file table and found there isn't one.
2210 There will be no point in trying to read it again next time. */
2211 unsigned int no_file_data : 1;
2212};
2213
094b34ac
DE
2214/* Utility hash function for a stmt_list_hash. */
2215
2216static hashval_t
2217hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2218{
2219 hashval_t v = 0;
2220
2221 if (stmt_list_hash->dwo_unit != NULL)
2222 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2223 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2224 return v;
2225}
2226
2227/* Utility equality function for a stmt_list_hash. */
2228
2229static int
2230eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2231 const struct stmt_list_hash *rhs)
2232{
2233 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2234 return 0;
2235 if (lhs->dwo_unit != NULL
2236 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2237 return 0;
2238
9c541725 2239 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2240}
2241
7b9f3c50
DE
2242/* Hash function for a quick_file_names. */
2243
2244static hashval_t
2245hash_file_name_entry (const void *e)
2246{
9a3c8263
SM
2247 const struct quick_file_names *file_data
2248 = (const struct quick_file_names *) e;
7b9f3c50 2249
094b34ac 2250 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2251}
2252
2253/* Equality function for a quick_file_names. */
2254
2255static int
2256eq_file_name_entry (const void *a, const void *b)
2257{
9a3c8263
SM
2258 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2259 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2260
094b34ac 2261 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2262}
2263
2264/* Delete function for a quick_file_names. */
2265
2266static void
2267delete_file_name_entry (void *e)
2268{
9a3c8263 2269 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2270 int i;
2271
2272 for (i = 0; i < file_data->num_file_names; ++i)
2273 {
2274 xfree ((void*) file_data->file_names[i]);
2275 if (file_data->real_names)
2276 xfree ((void*) file_data->real_names[i]);
2277 }
2278
2279 /* The space for the struct itself lives on objfile_obstack,
2280 so we don't free it here. */
2281}
2282
2283/* Create a quick_file_names hash table. */
2284
5895093f 2285static htab_up
7b9f3c50
DE
2286create_quick_file_names_table (unsigned int nr_initial_entries)
2287{
5895093f
TT
2288 return htab_up (htab_create_alloc (nr_initial_entries,
2289 hash_file_name_entry, eq_file_name_entry,
2290 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2291}
9291a0cd 2292
918dd910
JK
2293/* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
2294 have to be created afterwards. You should call age_cached_comp_units after
2295 processing PER_CU->CU. dw2_setup must have been already called. */
2296
2297static void
58f0c718 2298load_cu (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
918dd910 2299{
3019eac3 2300 if (per_cu->is_debug_types)
e5fe5e75 2301 load_full_type_unit (per_cu);
918dd910 2302 else
58f0c718 2303 load_full_comp_unit (per_cu, skip_partial, language_minimal);
918dd910 2304
cc12ce38
DE
2305 if (per_cu->cu == NULL)
2306 return; /* Dummy CU. */
2dc860c0
DE
2307
2308 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
918dd910
JK
2309}
2310
a0f42c21 2311/* Read in the symbols for PER_CU. */
2fdf6df6 2312
9291a0cd 2313static void
58f0c718 2314dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2315{
ed2dc618 2316 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9291a0cd 2317
f4dc4d17
DE
2318 /* Skip type_unit_groups, reading the type units they contain
2319 is handled elsewhere. */
197400e8 2320 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2321 return;
2322
b303c6f6
AB
2323 /* The destructor of dwarf2_queue_guard frees any entries left on
2324 the queue. After this point we're guaranteed to leave this function
2325 with the dwarf queue empty. */
39856def 2326 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2327
95554aad 2328 if (dwarf2_per_objfile->using_index
43f3e411 2329 ? per_cu->v.quick->compunit_symtab == NULL
95554aad
TT
2330 : (per_cu->v.psymtab == NULL || !per_cu->v.psymtab->readin))
2331 {
2332 queue_comp_unit (per_cu, language_minimal);
58f0c718 2333 load_cu (per_cu, skip_partial);
89e63ee4
DE
2334
2335 /* If we just loaded a CU from a DWO, and we're working with an index
2336 that may badly handle TUs, load all the TUs in that DWO as well.
2337 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2338 if (!per_cu->is_debug_types
cc12ce38 2339 && per_cu->cu != NULL
89e63ee4
DE
2340 && per_cu->cu->dwo_unit != NULL
2341 && dwarf2_per_objfile->index_table != NULL
2342 && dwarf2_per_objfile->index_table->version <= 7
2343 /* DWP files aren't supported yet. */
ed2dc618 2344 && get_dwp_file (dwarf2_per_objfile) == NULL)
89e63ee4 2345 queue_and_load_all_dwo_tus (per_cu);
95554aad 2346 }
9291a0cd 2347
ed2dc618 2348 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2349
2350 /* Age the cache, releasing compilation units that have not
2351 been used recently. */
ed2dc618 2352 age_cached_comp_units (dwarf2_per_objfile);
9291a0cd
TT
2353}
2354
2355/* Ensure that the symbols for PER_CU have been read in. OBJFILE is
2356 the objfile from which this CU came. Returns the resulting symbol
2357 table. */
2fdf6df6 2358
43f3e411 2359static struct compunit_symtab *
58f0c718 2360dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu, bool skip_partial)
9291a0cd 2361{
ed2dc618
SM
2362 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
2363
95554aad 2364 gdb_assert (dwarf2_per_objfile->using_index);
43f3e411 2365 if (!per_cu->v.quick->compunit_symtab)
9291a0cd 2366 {
11ed8cad 2367 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2368 scoped_restore decrementer = increment_reading_symtab ();
58f0c718 2369 dw2_do_instantiate_symtab (per_cu, skip_partial);
ed2dc618 2370 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2371 }
f194fefb 2372
43f3e411 2373 return per_cu->v.quick->compunit_symtab;
9291a0cd
TT
2374}
2375
ff4c9fec 2376/* See declaration. */
f4dc4d17 2377
ff4c9fec
SM
2378dwarf2_per_cu_data *
2379dwarf2_per_objfile::get_cutu (int index)
2380{
b76e467d 2381 if (index >= this->all_comp_units.size ())
ff4c9fec 2382 {
b76e467d 2383 index -= this->all_comp_units.size ();
b2bdb8cf 2384 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2385 return &this->all_type_units[index]->per_cu;
2386 }
f4dc4d17 2387
ff4c9fec
SM
2388 return this->all_comp_units[index];
2389}
f4dc4d17 2390
ff4c9fec 2391/* See declaration. */
2fdf6df6 2392
ff4c9fec
SM
2393dwarf2_per_cu_data *
2394dwarf2_per_objfile::get_cu (int index)
1fd400ff 2395{
b76e467d 2396 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2397
ff4c9fec 2398 return this->all_comp_units[index];
f4dc4d17
DE
2399}
2400
ff4c9fec 2401/* See declaration. */
f4dc4d17 2402
ff4c9fec
SM
2403signatured_type *
2404dwarf2_per_objfile::get_tu (int index)
f4dc4d17 2405{
b2bdb8cf 2406 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2407
ff4c9fec 2408 return this->all_type_units[index];
1fd400ff
TT
2409}
2410
4b514bc8
JK
2411/* Return a new dwarf2_per_cu_data allocated on OBJFILE's
2412 objfile_obstack, and constructed with the specified field
2413 values. */
2414
2415static dwarf2_per_cu_data *
ed2dc618 2416create_cu_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
4b514bc8
JK
2417 struct dwarf2_section_info *section,
2418 int is_dwz,
2419 sect_offset sect_off, ULONGEST length)
2420{
ed2dc618 2421 struct objfile *objfile = dwarf2_per_objfile->objfile;
4b514bc8
JK
2422 dwarf2_per_cu_data *the_cu
2423 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2424 struct dwarf2_per_cu_data);
2425 the_cu->sect_off = sect_off;
2426 the_cu->length = length;
e3b94546 2427 the_cu->dwarf2_per_objfile = dwarf2_per_objfile;
4b514bc8
JK
2428 the_cu->section = section;
2429 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2430 struct dwarf2_per_cu_quick_data);
2431 the_cu->is_dwz = is_dwz;
2432 return the_cu;
2433}
2434
2ec9a5e0
TT
2435/* A helper for create_cus_from_index that handles a given list of
2436 CUs. */
2fdf6df6 2437
74a0d9f6 2438static void
12359b5e 2439create_cus_from_index_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2440 const gdb_byte *cu_list, offset_type n_elements,
2441 struct dwarf2_section_info *section,
b76e467d 2442 int is_dwz)
9291a0cd 2443{
12359b5e 2444 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2445 {
74a0d9f6 2446 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2447
2448 sect_offset sect_off
2449 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2450 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2451 cu_list += 2 * 8;
2452
b76e467d 2453 dwarf2_per_cu_data *per_cu
ed2dc618
SM
2454 = create_cu_from_index_list (dwarf2_per_objfile, section, is_dwz,
2455 sect_off, length);
b76e467d 2456 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
9291a0cd 2457 }
9291a0cd
TT
2458}
2459
2ec9a5e0 2460/* Read the CU list from the mapped index, and use it to create all
74a0d9f6 2461 the CU objects for this objfile. */
2ec9a5e0 2462
74a0d9f6 2463static void
12359b5e 2464create_cus_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2ec9a5e0
TT
2465 const gdb_byte *cu_list, offset_type cu_list_elements,
2466 const gdb_byte *dwz_list, offset_type dwz_elements)
2467{
b76e467d
SM
2468 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
2469 dwarf2_per_objfile->all_comp_units.reserve
2470 ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2471
12359b5e 2472 create_cus_from_index_list (dwarf2_per_objfile, cu_list, cu_list_elements,
b76e467d 2473 &dwarf2_per_objfile->info, 0);
2ec9a5e0
TT
2474
2475 if (dwz_elements == 0)
74a0d9f6 2476 return;
2ec9a5e0 2477
12359b5e
SM
2478 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
2479 create_cus_from_index_list (dwarf2_per_objfile, dwz_list, dwz_elements,
b76e467d 2480 &dwz->info, 1);
2ec9a5e0
TT
2481}
2482
1fd400ff 2483/* Create the signatured type hash table from the index. */
673bfd45 2484
74a0d9f6 2485static void
12359b5e
SM
2486create_signatured_type_table_from_index
2487 (struct dwarf2_per_objfile *dwarf2_per_objfile,
2488 struct dwarf2_section_info *section,
2489 const gdb_byte *bytes,
2490 offset_type elements)
1fd400ff 2491{
12359b5e 2492 struct objfile *objfile = dwarf2_per_objfile->objfile;
1fd400ff 2493
b2bdb8cf
SM
2494 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2495 dwarf2_per_objfile->all_type_units.reserve (elements / 3);
1fd400ff 2496
298e9637 2497 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2498
12359b5e 2499 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2500 {
52dc124a 2501 struct signatured_type *sig_type;
9c541725 2502 ULONGEST signature;
1fd400ff 2503 void **slot;
9c541725 2504 cu_offset type_offset_in_tu;
1fd400ff 2505
74a0d9f6 2506 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2507 sect_offset sect_off
2508 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2509 type_offset_in_tu
2510 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2511 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2512 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2513 bytes += 3 * 8;
2514
52dc124a 2515 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1fd400ff 2516 struct signatured_type);
52dc124a 2517 sig_type->signature = signature;
9c541725 2518 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2519 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2520 sig_type->per_cu.section = section;
9c541725 2521 sig_type->per_cu.sect_off = sect_off;
e3b94546 2522 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
52dc124a 2523 sig_type->per_cu.v.quick
1fd400ff
TT
2524 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2525 struct dwarf2_per_cu_quick_data);
2526
b0b6a987 2527 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2528 *slot = sig_type;
1fd400ff 2529
b2bdb8cf 2530 dwarf2_per_objfile->all_type_units.push_back (sig_type);
1fd400ff
TT
2531 }
2532
b0b6a987 2533 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2534}
2535
927aa2e7
JK
2536/* Create the signatured type hash table from .debug_names. */
2537
2538static void
2539create_signatured_type_table_from_debug_names
ed2dc618 2540 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2541 const mapped_debug_names &map,
2542 struct dwarf2_section_info *section,
2543 struct dwarf2_section_info *abbrev_section)
2544{
ed2dc618
SM
2545 struct objfile *objfile = dwarf2_per_objfile->objfile;
2546
96b79293
TT
2547 section->read (objfile);
2548 abbrev_section->read (objfile);
927aa2e7 2549
b2bdb8cf
SM
2550 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
2551 dwarf2_per_objfile->all_type_units.reserve (map.tu_count);
927aa2e7 2552
298e9637 2553 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2554
2555 for (uint32_t i = 0; i < map.tu_count; ++i)
2556 {
2557 struct signatured_type *sig_type;
927aa2e7 2558 void **slot;
927aa2e7
JK
2559
2560 sect_offset sect_off
2561 = (sect_offset) (extract_unsigned_integer
2562 (map.tu_table_reordered + i * map.offset_size,
2563 map.offset_size,
2564 map.dwarf5_byte_order));
2565
2566 comp_unit_head cu_header;
ed2dc618
SM
2567 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2568 abbrev_section,
927aa2e7
JK
2569 section->buffer + to_underlying (sect_off),
2570 rcuh_kind::TYPE);
2571
2572 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2573 struct signatured_type);
2574 sig_type->signature = cu_header.signature;
2575 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2576 sig_type->per_cu.is_debug_types = 1;
2577 sig_type->per_cu.section = section;
2578 sig_type->per_cu.sect_off = sect_off;
e3b94546 2579 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
927aa2e7
JK
2580 sig_type->per_cu.v.quick
2581 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2582 struct dwarf2_per_cu_quick_data);
2583
b0b6a987 2584 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2585 *slot = sig_type;
2586
b2bdb8cf 2587 dwarf2_per_objfile->all_type_units.push_back (sig_type);
927aa2e7
JK
2588 }
2589
b0b6a987 2590 dwarf2_per_objfile->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2591}
2592
9291a0cd
TT
2593/* Read the address map data from the mapped index, and use it to
2594 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2595
9291a0cd 2596static void
ed2dc618
SM
2597create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2598 struct mapped_index *index)
9291a0cd 2599{
ed2dc618 2600 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2601 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2602 const gdb_byte *iter, *end;
9291a0cd 2603 struct addrmap *mutable_map;
9291a0cd
TT
2604 CORE_ADDR baseaddr;
2605
8268c778
PA
2606 auto_obstack temp_obstack;
2607
9291a0cd
TT
2608 mutable_map = addrmap_create_mutable (&temp_obstack);
2609
f00a2de2
PA
2610 iter = index->address_table.data ();
2611 end = iter + index->address_table.size ();
9291a0cd 2612
b3b3bada 2613 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2614
2615 while (iter < end)
2616 {
2617 ULONGEST hi, lo, cu_index;
2618 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2619 iter += 8;
2620 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2621 iter += 8;
2622 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2623 iter += 4;
f652bce2 2624
24a55014 2625 if (lo > hi)
f652bce2 2626 {
b98664d3 2627 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2628 hex_string (lo), hex_string (hi));
24a55014 2629 continue;
f652bce2 2630 }
24a55014 2631
b76e467d 2632 if (cu_index >= dwarf2_per_objfile->all_comp_units.size ())
f652bce2 2633 {
b98664d3 2634 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2635 (unsigned) cu_index);
24a55014 2636 continue;
f652bce2 2637 }
24a55014 2638
79748972
TT
2639 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2640 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2641 addrmap_set_empty (mutable_map, lo, hi - 1,
ff4c9fec 2642 dwarf2_per_objfile->get_cu (cu_index));
9291a0cd
TT
2643 }
2644
d320c2b5 2645 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2646 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2647}
2648
927aa2e7
JK
2649/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2650 populate the objfile's psymtabs_addrmap. */
2651
2652static void
ed2dc618 2653create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2654 struct dwarf2_section_info *section)
2655{
ed2dc618 2656 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2657 bfd *abfd = objfile->obfd;
08feed99 2658 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2659 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2660
2661 auto_obstack temp_obstack;
2662 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2663
2664 std::unordered_map<sect_offset,
2665 dwarf2_per_cu_data *,
2666 gdb::hash_enum<sect_offset>>
2667 debug_info_offset_to_per_cu;
b76e467d 2668 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 2669 {
927aa2e7
JK
2670 const auto insertpair
2671 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2672 if (!insertpair.second)
2673 {
2674 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2675 "debug_info_offset %s, ignoring .debug_aranges."),
2676 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2677 return;
2678 }
2679 }
2680
96b79293 2681 section->read (objfile);
927aa2e7
JK
2682
2683 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2684
2685 const gdb_byte *addr = section->buffer;
2686
2687 while (addr < section->buffer + section->size)
2688 {
2689 const gdb_byte *const entry_addr = addr;
2690 unsigned int bytes_read;
2691
2692 const LONGEST entry_length = read_initial_length (abfd, addr,
2693 &bytes_read);
2694 addr += bytes_read;
2695
2696 const gdb_byte *const entry_end = addr + entry_length;
2697 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2698 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2699 if (addr + entry_length > section->buffer + section->size)
2700 {
47e3f474 2701 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2702 "length %s exceeds section length %s, "
2703 "ignoring .debug_aranges."),
47e3f474
TV
2704 objfile_name (objfile),
2705 plongest (entry_addr - section->buffer),
927aa2e7
JK
2706 plongest (bytes_read + entry_length),
2707 pulongest (section->size));
2708 return;
2709 }
2710
2711 /* The version number. */
2712 const uint16_t version = read_2_bytes (abfd, addr);
2713 addr += 2;
2714 if (version != 2)
2715 {
47e3f474 2716 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2717 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2718 objfile_name (objfile),
2719 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2720 return;
2721 }
2722
2723 const uint64_t debug_info_offset
2724 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2725 addr += offset_size;
2726 const auto per_cu_it
2727 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2728 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2729 {
47e3f474 2730 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2731 "debug_info_offset %s does not exists, "
2732 "ignoring .debug_aranges."),
47e3f474
TV
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
927aa2e7
JK
2735 pulongest (debug_info_offset));
2736 return;
2737 }
2738 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2739
2740 const uint8_t address_size = *addr++;
2741 if (address_size < 1 || address_size > 8)
2742 {
47e3f474 2743 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2744 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2745 objfile_name (objfile),
2746 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2747 return;
2748 }
2749
2750 const uint8_t segment_selector_size = *addr++;
2751 if (segment_selector_size != 0)
2752 {
47e3f474 2753 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2754 "segment_selector_size %u is not supported, "
2755 "ignoring .debug_aranges."),
47e3f474
TV
2756 objfile_name (objfile),
2757 plongest (entry_addr - section->buffer),
927aa2e7
JK
2758 segment_selector_size);
2759 return;
2760 }
2761
2762 /* Must pad to an alignment boundary that is twice the address
2763 size. It is undocumented by the DWARF standard but GCC does
2764 use it. */
2765 for (size_t padding = ((-(addr - section->buffer))
2766 & (2 * address_size - 1));
2767 padding > 0; padding--)
2768 if (*addr++ != 0)
2769 {
47e3f474 2770 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2771 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2772 objfile_name (objfile),
2773 plongest (entry_addr - section->buffer));
927aa2e7
JK
2774 return;
2775 }
2776
2777 for (;;)
2778 {
2779 if (addr + 2 * address_size > entry_end)
2780 {
47e3f474 2781 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2782 "address list is not properly terminated, "
2783 "ignoring .debug_aranges."),
47e3f474
TV
2784 objfile_name (objfile),
2785 plongest (entry_addr - section->buffer));
927aa2e7
JK
2786 return;
2787 }
2788 ULONGEST start = extract_unsigned_integer (addr, address_size,
2789 dwarf5_byte_order);
2790 addr += address_size;
2791 ULONGEST length = extract_unsigned_integer (addr, address_size,
2792 dwarf5_byte_order);
2793 addr += address_size;
2794 if (start == 0 && length == 0)
2795 break;
2796 if (start == 0 && !dwarf2_per_objfile->has_section_at_zero)
2797 {
2798 /* Symbol was eliminated due to a COMDAT group. */
2799 continue;
2800 }
2801 ULONGEST end = start + length;
79748972
TT
2802 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2803 - baseaddr);
2804 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2805 - baseaddr);
927aa2e7
JK
2806 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2807 }
2808 }
2809
d320c2b5 2810 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2811 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2812}
2813
9291a0cd
TT
2814/* Find a slot in the mapped index INDEX for the object named NAME.
2815 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2816 constant pool and return true. If NAME cannot be found, return
2817 false. */
2fdf6df6 2818
109483d9 2819static bool
9291a0cd
TT
2820find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2821 offset_type **vec_out)
2822{
0cf03b49 2823 offset_type hash;
9291a0cd 2824 offset_type slot, step;
559a7a62 2825 int (*cmp) (const char *, const char *);
9291a0cd 2826
791afaa2 2827 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2828 if (current_language->la_language == language_cplus
45280282
IB
2829 || current_language->la_language == language_fortran
2830 || current_language->la_language == language_d)
0cf03b49
JK
2831 {
2832 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2833 not contain any. */
a8719064 2834
72998fb3 2835 if (strchr (name, '(') != NULL)
0cf03b49 2836 {
109483d9 2837 without_params = cp_remove_params (name);
0cf03b49 2838
72998fb3 2839 if (without_params != NULL)
791afaa2 2840 name = without_params.get ();
0cf03b49
JK
2841 }
2842 }
2843
559a7a62 2844 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2845 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2846 simulate our NAME being searched is also lowercased. */
2847 hash = mapped_index_string_hash ((index->version == 4
2848 && case_sensitivity == case_sensitive_off
2849 ? 5 : index->version),
2850 name);
2851
f00a2de2
PA
2852 slot = hash & (index->symbol_table.size () - 1);
2853 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2854 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2855
2856 for (;;)
2857 {
9291a0cd 2858 const char *str;
f00a2de2
PA
2859
2860 const auto &bucket = index->symbol_table[slot];
2861 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2862 return false;
9291a0cd 2863
f00a2de2 2864 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2865 if (!cmp (name, str))
9291a0cd
TT
2866 {
2867 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2868 + MAYBE_SWAP (bucket.vec));
109483d9 2869 return true;
9291a0cd
TT
2870 }
2871
f00a2de2 2872 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2873 }
2874}
2875
4485a1c1
SM
2876/* A helper function that reads the .gdb_index from BUFFER and fills
2877 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2878 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2879 ok to use deprecated sections.
2880
2881 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2882 out parameters that are filled in with information about the CU and
2883 TU lists in the section.
2884
4485a1c1 2885 Returns true if all went well, false otherwise. */
2fdf6df6 2886
d33bc52e 2887static bool
3810f182 2888read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2889 bool deprecated_ok,
2890 gdb::array_view<const gdb_byte> buffer,
2891 struct mapped_index *map,
2892 const gdb_byte **cu_list,
2893 offset_type *cu_list_elements,
2894 const gdb_byte **types_list,
2895 offset_type *types_list_elements)
2896{
2897 const gdb_byte *addr = &buffer[0];
82430852 2898
9291a0cd 2899 /* Version check. */
4485a1c1 2900 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2901 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2902 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2903 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2904 indices. */
831adc1f 2905 if (version < 4)
481860b3
GB
2906 {
2907 static int warning_printed = 0;
2908 if (!warning_printed)
2909 {
2910 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2911 filename);
481860b3
GB
2912 warning_printed = 1;
2913 }
2914 return 0;
2915 }
2916 /* Index version 4 uses a different hash function than index version
2917 5 and later.
2918
2919 Versions earlier than 6 did not emit psymbols for inlined
2920 functions. Using these files will cause GDB not to be able to
2921 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2922 indices unless the user has done
2923 "set use-deprecated-index-sections on". */
2ec9a5e0 2924 if (version < 6 && !deprecated_ok)
481860b3
GB
2925 {
2926 static int warning_printed = 0;
2927 if (!warning_printed)
2928 {
e615022a
DE
2929 warning (_("\
2930Skipping deprecated .gdb_index section in %s.\n\
2931Do \"set use-deprecated-index-sections on\" before the file is read\n\
2932to use the section anyway."),
2ec9a5e0 2933 filename);
481860b3
GB
2934 warning_printed = 1;
2935 }
2936 return 0;
2937 }
796a7ff8 2938 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2939 of the TU (for symbols coming from TUs),
2940 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2941 Plus gold-generated indices can have duplicate entries for global symbols,
2942 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2943 These are just performance bugs, and we can't distinguish gdb-generated
2944 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2945
481860b3 2946 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2947 longer backward compatible. */
796a7ff8 2948 if (version > 8)
594e8718 2949 return 0;
9291a0cd 2950
559a7a62 2951 map->version = version;
9291a0cd 2952
4485a1c1 2953 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2954
4485a1c1 2955 int i = 0;
2ec9a5e0
TT
2956 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2957 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2958 / 8);
1fd400ff
TT
2959 ++i;
2960
2ec9a5e0
TT
2961 *types_list = addr + MAYBE_SWAP (metadata[i]);
2962 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2963 - MAYBE_SWAP (metadata[i]))
2964 / 8);
987d643c 2965 ++i;
1fd400ff 2966
f00a2de2
PA
2967 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2968 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2969 map->address_table
2970 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
2971 ++i;
2972
f00a2de2
PA
2973 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
2974 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2975 map->symbol_table
2976 = gdb::array_view<mapped_index::symbol_table_slot>
2977 ((mapped_index::symbol_table_slot *) symbol_table,
2978 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 2979
f00a2de2 2980 ++i;
f9d83a0b 2981 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 2982
2ec9a5e0
TT
2983 return 1;
2984}
2985
4485a1c1
SM
2986/* Callback types for dwarf2_read_gdb_index. */
2987
2988typedef gdb::function_view
2989 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_objfile *)>
2990 get_gdb_index_contents_ftype;
2991typedef gdb::function_view
2992 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
2993 get_gdb_index_contents_dwz_ftype;
2994
927aa2e7 2995/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
2996 elements of all the CUs and return 1. Otherwise, return 0. */
2997
2998static int
4485a1c1
SM
2999dwarf2_read_gdb_index
3000 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3001 get_gdb_index_contents_ftype get_gdb_index_contents,
3002 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3003{
2ec9a5e0
TT
3004 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3005 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3006 struct dwz_file *dwz;
12359b5e 3007 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3008
4485a1c1
SM
3009 gdb::array_view<const gdb_byte> main_index_contents
3010 = get_gdb_index_contents (objfile, dwarf2_per_objfile);
3011
3012 if (main_index_contents.empty ())
3013 return 0;
3014
3063847f 3015 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3016 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3017 use_deprecated_index_sections,
3018 main_index_contents, map.get (), &cu_list,
3019 &cu_list_elements, &types_list,
3020 &types_list_elements))
2ec9a5e0
TT
3021 return 0;
3022
0fefef59 3023 /* Don't use the index if it's empty. */
3063847f 3024 if (map->symbol_table.empty ())
0fefef59
DE
3025 return 0;
3026
2ec9a5e0
TT
3027 /* If there is a .dwz file, read it so we can get its CU list as
3028 well. */
ed2dc618 3029 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 3030 if (dwz != NULL)
2ec9a5e0 3031 {
2ec9a5e0
TT
3032 struct mapped_index dwz_map;
3033 const gdb_byte *dwz_types_ignore;
3034 offset_type dwz_types_elements_ignore;
3035
4485a1c1
SM
3036 gdb::array_view<const gdb_byte> dwz_index_content
3037 = get_gdb_index_contents_dwz (objfile, dwz);
3038
3039 if (dwz_index_content.empty ())
3040 return 0;
3041
3810f182 3042 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3043 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3044 &dwz_list, &dwz_list_elements,
3045 &dwz_types_ignore,
3046 &dwz_types_elements_ignore))
2ec9a5e0
TT
3047 {
3048 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3049 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3050 return 0;
3051 }
3052 }
3053
12359b5e
SM
3054 create_cus_from_index (dwarf2_per_objfile, cu_list, cu_list_elements,
3055 dwz_list, dwz_list_elements);
1fd400ff 3056
8b70b953
TT
3057 if (types_list_elements)
3058 {
8b70b953
TT
3059 /* We can only handle a single .debug_types when we have an
3060 index. */
fd5866f6 3061 if (dwarf2_per_objfile->types.size () != 1)
8b70b953
TT
3062 return 0;
3063
fd5866f6 3064 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
8b70b953 3065
12359b5e
SM
3066 create_signatured_type_table_from_index (dwarf2_per_objfile, section,
3067 types_list, types_list_elements);
8b70b953 3068 }
9291a0cd 3069
3063847f 3070 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3071
3063847f 3072 dwarf2_per_objfile->index_table = std::move (map);
9291a0cd 3073 dwarf2_per_objfile->using_index = 1;
7b9f3c50 3074 dwarf2_per_objfile->quick_file_names_table =
b76e467d 3075 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd
TT
3076
3077 return 1;
3078}
3079
dee91e82 3080/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3081
dee91e82
DE
3082static void
3083dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3084 const gdb_byte *info_ptr,
3e225074 3085 struct die_info *comp_unit_die)
9291a0cd 3086{
dee91e82 3087 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3088 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
518817b3
SM
3089 struct dwarf2_per_objfile *dwarf2_per_objfile
3090 = cu->per_cu->dwarf2_per_objfile;
dee91e82 3091 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 3092 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3093 struct attribute *attr;
7b9f3c50
DE
3094 void **slot;
3095 struct quick_file_names *qfn;
9291a0cd 3096
0186c6a7
DE
3097 gdb_assert (! this_cu->is_debug_types);
3098
07261596
TT
3099 /* Our callers never want to match partial units -- instead they
3100 will match the enclosing full CU. */
3101 if (comp_unit_die->tag == DW_TAG_partial_unit)
3102 {
3103 this_cu->v.quick->no_file_data = 1;
3104 return;
3105 }
3106
0186c6a7 3107 lh_cu = this_cu;
7b9f3c50 3108 slot = NULL;
dee91e82 3109
fff8551c 3110 line_header_up lh;
9c541725 3111 sect_offset line_offset {};
fff8551c 3112
dee91e82 3113 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3114 if (attr != nullptr)
9291a0cd 3115 {
7b9f3c50
DE
3116 struct quick_file_names find_entry;
3117
9c541725 3118 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3119
3120 /* We may have already read in this line header (TU line header sharing).
3121 If we have we're done. */
094b34ac 3122 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3123 find_entry.hash.line_sect_off = line_offset;
5895093f 3124 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50
DE
3125 &find_entry, INSERT);
3126 if (*slot != NULL)
3127 {
9a3c8263 3128 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3129 return;
7b9f3c50
DE
3130 }
3131
3019eac3 3132 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3133 }
3134 if (lh == NULL)
3135 {
094b34ac 3136 lh_cu->v.quick->no_file_data = 1;
dee91e82 3137 return;
9291a0cd
TT
3138 }
3139
8d749320 3140 qfn = XOBNEW (&objfile->objfile_obstack, struct quick_file_names);
094b34ac 3141 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3142 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3143 gdb_assert (slot != NULL);
3144 *slot = qfn;
9291a0cd 3145
d721ba37 3146 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3147
aa391654
TT
3148 int offset = 0;
3149 if (strcmp (fnd.name, "<unknown>") != 0)
3150 ++offset;
3151
7ba99d21 3152 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3153 qfn->file_names =
aa391654
TT
3154 XOBNEWVEC (&objfile->objfile_obstack, const char *, qfn->num_file_names);
3155 if (offset != 0)
3156 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3157 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3158 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3159 fnd.comp_dir).release ();
7b9f3c50 3160 qfn->real_names = NULL;
9291a0cd 3161
094b34ac 3162 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3163}
3164
3165/* A helper for the "quick" functions which attempts to read the line
3166 table for THIS_CU. */
3167
3168static struct quick_file_names *
e4a48d9d 3169dw2_get_file_names (struct dwarf2_per_cu_data *this_cu)
dee91e82 3170{
0186c6a7
DE
3171 /* This should never be called for TUs. */
3172 gdb_assert (! this_cu->is_debug_types);
3173 /* Nor type unit groups. */
197400e8 3174 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3175
dee91e82
DE
3176 if (this_cu->v.quick->file_names != NULL)
3177 return this_cu->v.quick->file_names;
3178 /* If we know there is no line data, no point in looking again. */
3179 if (this_cu->v.quick->no_file_data)
3180 return NULL;
3181
c0ab21c2
TT
3182 cutu_reader reader (this_cu);
3183 if (!reader.dummy_p)
3e225074 3184 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3185
3186 if (this_cu->v.quick->no_file_data)
3187 return NULL;
3188 return this_cu->v.quick->file_names;
9291a0cd
TT
3189}
3190
3191/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3192 real path for a given file name from the line table. */
2fdf6df6 3193
9291a0cd 3194static const char *
7b9f3c50
DE
3195dw2_get_real_path (struct objfile *objfile,
3196 struct quick_file_names *qfn, int index)
9291a0cd 3197{
7b9f3c50
DE
3198 if (qfn->real_names == NULL)
3199 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
26f2dc30 3200 qfn->num_file_names, const char *);
9291a0cd 3201
7b9f3c50 3202 if (qfn->real_names[index] == NULL)
14278e1f 3203 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3204
7b9f3c50 3205 return qfn->real_names[index];
9291a0cd
TT
3206}
3207
3208static struct symtab *
3209dw2_find_last_source_symtab (struct objfile *objfile)
3210{
ed2dc618
SM
3211 struct dwarf2_per_objfile *dwarf2_per_objfile
3212 = get_dwarf2_per_objfile (objfile);
b76e467d 3213 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->all_comp_units.back ();
58f0c718 3214 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, false);
ae2de4f8 3215
43f3e411
DE
3216 if (cust == NULL)
3217 return NULL;
ed2dc618 3218
43f3e411 3219 return compunit_primary_filetab (cust);
9291a0cd
TT
3220}
3221
7b9f3c50
DE
3222/* Traversal function for dw2_forget_cached_source_info. */
3223
3224static int
3225dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3226{
7b9f3c50 3227 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3228
7b9f3c50 3229 if (file_data->real_names)
9291a0cd 3230 {
7b9f3c50 3231 int i;
9291a0cd 3232
7b9f3c50 3233 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3234 {
7b9f3c50
DE
3235 xfree ((void*) file_data->real_names[i]);
3236 file_data->real_names[i] = NULL;
9291a0cd
TT
3237 }
3238 }
7b9f3c50
DE
3239
3240 return 1;
3241}
3242
3243static void
3244dw2_forget_cached_source_info (struct objfile *objfile)
3245{
ed2dc618
SM
3246 struct dwarf2_per_objfile *dwarf2_per_objfile
3247 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3248
5895093f 3249 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table.get (),
7b9f3c50 3250 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3251}
3252
f8eba3c6
TT
3253/* Helper function for dw2_map_symtabs_matching_filename that expands
3254 the symtabs and calls the iterator. */
3255
3256static int
3257dw2_map_expand_apply (struct objfile *objfile,
3258 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3259 const char *name, const char *real_path,
14bc53a8 3260 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3261{
43f3e411 3262 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3263
3264 /* Don't visit already-expanded CUs. */
43f3e411 3265 if (per_cu->v.quick->compunit_symtab)
f8eba3c6
TT
3266 return 0;
3267
3268 /* This may expand more than one symtab, and we want to iterate over
3269 all of them. */
58f0c718 3270 dw2_instantiate_symtab (per_cu, false);
f8eba3c6 3271
14bc53a8
PA
3272 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3273 last_made, callback);
f8eba3c6
TT
3274}
3275
3276/* Implementation of the map_symtabs_matching_filename method. */
3277
14bc53a8
PA
3278static bool
3279dw2_map_symtabs_matching_filename
3280 (struct objfile *objfile, const char *name, const char *real_path,
3281 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3282{
c011a4f4 3283 const char *name_basename = lbasename (name);
ed2dc618
SM
3284 struct dwarf2_per_objfile *dwarf2_per_objfile
3285 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3286
848e3e78
DE
3287 /* The rule is CUs specify all the files, including those used by
3288 any TU, so there's no need to scan TUs here. */
f4dc4d17 3289
b76e467d 3290 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3291 {
3d7bb9d9 3292 /* We only need to look at symtabs not already expanded. */
43f3e411 3293 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3294 continue;
3295
b76e467d 3296 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3297 if (file_data == NULL)
9291a0cd
TT
3298 continue;
3299
b76e467d 3300 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3301 {
7b9f3c50 3302 const char *this_name = file_data->file_names[j];
da235a7c 3303 const char *this_real_name;
9291a0cd 3304
af529f8f 3305 if (compare_filenames_for_search (this_name, name))
9291a0cd 3306 {
f5b95b50 3307 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3308 callback))
3309 return true;
288e77a7 3310 continue;
4aac40c8 3311 }
9291a0cd 3312
c011a4f4
DE
3313 /* Before we invoke realpath, which can get expensive when many
3314 files are involved, do a quick comparison of the basenames. */
3315 if (! basenames_may_differ
3316 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3317 continue;
3318
da235a7c
JK
3319 this_real_name = dw2_get_real_path (objfile, file_data, j);
3320 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3321 {
da235a7c 3322 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3323 callback))
3324 return true;
288e77a7 3325 continue;
da235a7c 3326 }
9291a0cd 3327
da235a7c
JK
3328 if (real_path != NULL)
3329 {
af529f8f
JK
3330 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3331 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3332 if (this_real_name != NULL
af529f8f 3333 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3334 {
f5b95b50 3335 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3336 callback))
3337 return true;
288e77a7 3338 continue;
9291a0cd
TT
3339 }
3340 }
3341 }
3342 }
3343
14bc53a8 3344 return false;
9291a0cd
TT
3345}
3346
da51c347
DE
3347/* Struct used to manage iterating over all CUs looking for a symbol. */
3348
3349struct dw2_symtab_iterator
9291a0cd 3350{
ed2dc618
SM
3351 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3352 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3353 /* If set, only look for symbols that match that block. Valid values are
3354 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3355 gdb::optional<block_enum> block_index;
da51c347
DE
3356 /* The kind of symbol we're looking for. */
3357 domain_enum domain;
3358 /* The list of CUs from the index entry of the symbol,
3359 or NULL if not found. */
3360 offset_type *vec;
3361 /* The next element in VEC to look at. */
3362 int next;
3363 /* The number of elements in VEC, or zero if there is no match. */
3364 int length;
8943b874
DE
3365 /* Have we seen a global version of the symbol?
3366 If so we can ignore all further global instances.
3367 This is to work around gold/15646, inefficient gold-generated
3368 indices. */
3369 int global_seen;
da51c347 3370};
9291a0cd 3371
2b79f376 3372/* Initialize the index symtab iterator ITER. */
2fdf6df6 3373
9291a0cd 3374static void
da51c347 3375dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3376 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3377 gdb::optional<block_enum> block_index,
da51c347
DE
3378 domain_enum domain,
3379 const char *name)
3380{
ed2dc618 3381 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3382 iter->block_index = block_index;
3383 iter->domain = domain;
3384 iter->next = 0;
8943b874 3385 iter->global_seen = 0;
da51c347 3386
3063847f 3387 mapped_index *index = dwarf2_per_objfile->index_table.get ();
ed2dc618
SM
3388
3389 /* index is NULL if OBJF_READNOW. */
3390 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3391 iter->length = MAYBE_SWAP (*iter->vec);
3392 else
3393 {
3394 iter->vec = NULL;
3395 iter->length = 0;
3396 }
3397}
3398
3399/* Return the next matching CU or NULL if there are no more. */
3400
3401static struct dwarf2_per_cu_data *
3402dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3403{
ed2dc618
SM
3404 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3405
da51c347
DE
3406 for ( ; iter->next < iter->length; ++iter->next)
3407 {
3408 offset_type cu_index_and_attrs =
3409 MAYBE_SWAP (iter->vec[iter->next + 1]);
3410 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3411 gdb_index_symbol_kind symbol_kind =
3412 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3413 /* Only check the symbol attributes if they're present.
3414 Indices prior to version 7 don't record them,
3415 and indices >= 7 may elide them for certain symbols
3416 (gold does this). */
3417 int attrs_valid =
ed2dc618 3418 (dwarf2_per_objfile->index_table->version >= 7
da51c347
DE
3419 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3420
3190f0c6 3421 /* Don't crash on bad data. */
b76e467d 3422 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3423 + dwarf2_per_objfile->all_type_units.size ()))
3190f0c6 3424 {
b98664d3 3425 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3426 " [in module %s]"),
3427 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3428 continue;
3429 }
3430
ff4c9fec 3431 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
3190f0c6 3432
da51c347 3433 /* Skip if already read in. */
43f3e411 3434 if (per_cu->v.quick->compunit_symtab)
da51c347
DE
3435 continue;
3436
8943b874
DE
3437 /* Check static vs global. */
3438 if (attrs_valid)
3439 {
2b79f376
SM
3440 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3441
3442 if (iter->block_index.has_value ())
3443 {
3444 bool want_static = *iter->block_index == STATIC_BLOCK;
3445
3446 if (is_static != want_static)
3447 continue;
3448 }
3449
8943b874
DE
3450 /* Work around gold/15646. */
3451 if (!is_static && iter->global_seen)
3452 continue;
3453 if (!is_static)
3454 iter->global_seen = 1;
3455 }
da51c347
DE
3456
3457 /* Only check the symbol's kind if it has one. */
3458 if (attrs_valid)
3459 {
3460 switch (iter->domain)
3461 {
3462 case VAR_DOMAIN:
3463 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3464 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3465 /* Some types are also in VAR_DOMAIN. */
3466 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3467 continue;
3468 break;
3469 case STRUCT_DOMAIN:
3470 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3471 continue;
3472 break;
3473 case LABEL_DOMAIN:
3474 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3475 continue;
3476 break;
59c35742
AB
3477 case MODULE_DOMAIN:
3478 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3479 continue;
3480 break;
da51c347
DE
3481 default:
3482 break;
3483 }
3484 }
3485
3486 ++iter->next;
3487 return per_cu;
3488 }
3489
3490 return NULL;
3491}
3492
43f3e411 3493static struct compunit_symtab *
c7f839cb 3494dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3495 const char *name, domain_enum domain)
9291a0cd 3496{
43f3e411 3497 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3498 struct dwarf2_per_objfile *dwarf2_per_objfile
3499 = get_dwarf2_per_objfile (objfile);
9291a0cd 3500
b5ec771e
PA
3501 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3502
ed2dc618
SM
3503 struct dw2_symtab_iterator iter;
3504 struct dwarf2_per_cu_data *per_cu;
da51c347 3505
2b79f376 3506 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3507
ed2dc618
SM
3508 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3509 {
3510 struct symbol *sym, *with_opaque = NULL;
58f0c718 3511 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
ed2dc618 3512 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3513 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3514
ed2dc618
SM
3515 sym = block_find_symbol (block, name, domain,
3516 block_find_non_opaque_type_preferred,
3517 &with_opaque);
b2e2f908 3518
ed2dc618
SM
3519 /* Some caution must be observed with overloaded functions
3520 and methods, since the index will not contain any overload
3521 information (but NAME might contain it). */
da51c347 3522
ed2dc618
SM
3523 if (sym != NULL
3524 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3525 return stab;
3526 if (with_opaque != NULL
3527 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3528 stab_best = stab;
da51c347 3529
ed2dc618 3530 /* Keep looking through other CUs. */
9291a0cd 3531 }
9291a0cd 3532
da51c347 3533 return stab_best;
9291a0cd
TT
3534}
3535
3536static void
3537dw2_print_stats (struct objfile *objfile)
3538{
ed2dc618
SM
3539 struct dwarf2_per_objfile *dwarf2_per_objfile
3540 = get_dwarf2_per_objfile (objfile);
b76e467d 3541 int total = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3542 + dwarf2_per_objfile->all_type_units.size ());
ed2dc618 3543 int count = 0;
9291a0cd 3544
ed2dc618 3545 for (int i = 0; i < total; ++i)
9291a0cd 3546 {
ff4c9fec 3547 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3548
43f3e411 3549 if (!per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3550 ++count;
3551 }
e4a48d9d 3552 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3553 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3554}
3555
779bd270
DE
3556/* This dumps minimal information about the index.
3557 It is called via "mt print objfiles".
3558 One use is to verify .gdb_index has been loaded by the
3559 gdb.dwarf2/gdb-index.exp testcase. */
3560
9291a0cd
TT
3561static void
3562dw2_dump (struct objfile *objfile)
3563{
ed2dc618
SM
3564 struct dwarf2_per_objfile *dwarf2_per_objfile
3565 = get_dwarf2_per_objfile (objfile);
3566
779bd270
DE
3567 gdb_assert (dwarf2_per_objfile->using_index);
3568 printf_filtered (".gdb_index:");
3569 if (dwarf2_per_objfile->index_table != NULL)
3570 {
3571 printf_filtered (" version %d\n",
3572 dwarf2_per_objfile->index_table->version);
3573 }
3574 else
3575 printf_filtered (" faked for \"readnow\"\n");
3576 printf_filtered ("\n");
9291a0cd
TT
3577}
3578
9291a0cd
TT
3579static void
3580dw2_expand_symtabs_for_function (struct objfile *objfile,
3581 const char *func_name)
3582{
ed2dc618
SM
3583 struct dwarf2_per_objfile *dwarf2_per_objfile
3584 = get_dwarf2_per_objfile (objfile);
da51c347 3585
ed2dc618
SM
3586 struct dw2_symtab_iterator iter;
3587 struct dwarf2_per_cu_data *per_cu;
da51c347 3588
2b79f376 3589 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3590
ed2dc618 3591 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
58f0c718 3592 dw2_instantiate_symtab (per_cu, false);
da51c347 3593
9291a0cd
TT
3594}
3595
3596static void
3597dw2_expand_all_symtabs (struct objfile *objfile)
3598{
ed2dc618
SM
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
b76e467d 3601 int total_units = (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 3602 + dwarf2_per_objfile->all_type_units.size ());
9291a0cd 3603
ed2dc618 3604 for (int i = 0; i < total_units; ++i)
9291a0cd 3605 {
ff4c9fec 3606 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 3607
58f0c718
TT
3608 /* We don't want to directly expand a partial CU, because if we
3609 read it with the wrong language, then assertion failures can
3610 be triggered later on. See PR symtab/23010. So, tell
3611 dw2_instantiate_symtab to skip partial CUs -- any important
3612 partial CU will be read via DW_TAG_imported_unit anyway. */
3613 dw2_instantiate_symtab (per_cu, true);
9291a0cd
TT
3614 }
3615}
3616
3617static void
652a8996
JK
3618dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3619 const char *fullname)
9291a0cd 3620{
ed2dc618
SM
3621 struct dwarf2_per_objfile *dwarf2_per_objfile
3622 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3623
3624 /* We don't need to consider type units here.
3625 This is only called for examining code, e.g. expand_line_sal.
3626 There can be an order of magnitude (or more) more type units
3627 than comp units, and we avoid them if we can. */
3628
b76e467d 3629 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
9291a0cd 3630 {
3d7bb9d9 3631 /* We only need to look at symtabs not already expanded. */
43f3e411 3632 if (per_cu->v.quick->compunit_symtab)
9291a0cd
TT
3633 continue;
3634
b76e467d 3635 quick_file_names *file_data = dw2_get_file_names (per_cu);
7b9f3c50 3636 if (file_data == NULL)
9291a0cd
TT
3637 continue;
3638
b76e467d 3639 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3640 {
652a8996
JK
3641 const char *this_fullname = file_data->file_names[j];
3642
3643 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3644 {
58f0c718 3645 dw2_instantiate_symtab (per_cu, false);
9291a0cd
TT
3646 break;
3647 }
3648 }
3649 }
3650}
3651
9291a0cd 3652static void
199b4314
TT
3653dw2_map_matching_symbols
3654 (struct objfile *objfile,
b054970d 3655 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3656 int global,
3657 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3658 symbol_compare_ftype *ordered_compare)
9291a0cd 3659{
1aa98955
TV
3660 /* Used for Ada. */
3661 struct dwarf2_per_objfile *dwarf2_per_objfile
3662 = get_dwarf2_per_objfile (objfile);
3663
3664 if (dwarf2_per_objfile->index_table != nullptr)
3665 {
3666 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3667 here though if the current language is Ada for a non-Ada objfile
3668 using GNU index. As Ada does not look for non-Ada symbols this
3669 function should just return. */
3670 return;
3671 }
3672
3673 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3674 inline psym_map_matching_symbols here, assuming all partial symtabs have
3675 been read in. */
3676 const int block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3677
3678 for (compunit_symtab *cust : objfile->compunits ())
3679 {
3680 const struct block *block;
3681
3682 if (cust == NULL)
3683 continue;
3684 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3685 if (!iterate_over_symbols_terminated (block, name,
3686 domain, callback))
3687 return;
3688 }
9291a0cd
TT
3689}
3690
e1ef7d7a
PA
3691/* Starting from a search name, return the string that finds the upper
3692 bound of all strings that start with SEARCH_NAME in a sorted name
3693 list. Returns the empty string to indicate that the upper bound is
3694 the end of the list. */
3695
3696static std::string
3697make_sort_after_prefix_name (const char *search_name)
3698{
3699 /* When looking to complete "func", we find the upper bound of all
3700 symbols that start with "func" by looking for where we'd insert
3701 the closest string that would follow "func" in lexicographical
3702 order. Usually, that's "func"-with-last-character-incremented,
3703 i.e. "fund". Mind non-ASCII characters, though. Usually those
3704 will be UTF-8 multi-byte sequences, but we can't be certain.
3705 Especially mind the 0xff character, which is a valid character in
3706 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3707 rule out compilers allowing it in identifiers. Note that
3708 conveniently, strcmp/strcasecmp are specified to compare
3709 characters interpreted as unsigned char. So what we do is treat
3710 the whole string as a base 256 number composed of a sequence of
3711 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3712 to 0, and carries 1 to the following more-significant position.
3713 If the very first character in SEARCH_NAME ends up incremented
3714 and carries/overflows, then the upper bound is the end of the
3715 list. The string after the empty string is also the empty
3716 string.
3717
3718 Some examples of this operation:
3719
3720 SEARCH_NAME => "+1" RESULT
3721
3722 "abc" => "abd"
3723 "ab\xff" => "ac"
3724 "\xff" "a" "\xff" => "\xff" "b"
3725 "\xff" => ""
3726 "\xff\xff" => ""
3727 "" => ""
3728
3729 Then, with these symbols for example:
3730
3731 func
3732 func1
3733 fund
3734
3735 completing "func" looks for symbols between "func" and
3736 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3737 which finds "func" and "func1", but not "fund".
3738
3739 And with:
3740
3741 funcÿ (Latin1 'ÿ' [0xff])
3742 funcÿ1
3743 fund
3744
3745 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3746 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3747
3748 And with:
3749
3750 ÿÿ (Latin1 'ÿ' [0xff])
3751 ÿÿ1
3752
3753 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3754 the end of the list.
3755 */
3756 std::string after = search_name;
3757 while (!after.empty () && (unsigned char) after.back () == 0xff)
3758 after.pop_back ();
3759 if (!after.empty ())
3760 after.back () = (unsigned char) after.back () + 1;
3761 return after;
3762}
3763
5c58de74 3764/* See declaration. */
61d96d7e 3765
5c58de74
PA
3766std::pair<std::vector<name_component>::const_iterator,
3767 std::vector<name_component>::const_iterator>
44ed8f3e 3768mapped_index_base::find_name_components_bounds
3b00ef10 3769 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3770{
5c58de74
PA
3771 auto *name_cmp
3772 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3773
3b00ef10 3774 const char *lang_name
e0802d59 3775 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3776
3f563c84
PA
3777 /* Comparison function object for lower_bound that matches against a
3778 given symbol name. */
3779 auto lookup_compare_lower = [&] (const name_component &elem,
3780 const char *name)
3781 {
5c58de74 3782 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3783 const char *elem_name = elem_qualified + elem.name_offset;
3784 return name_cmp (elem_name, name) < 0;
3785 };
3786
3787 /* Comparison function object for upper_bound that matches against a
3788 given symbol name. */
3789 auto lookup_compare_upper = [&] (const char *name,
3790 const name_component &elem)
3791 {
5c58de74 3792 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3793 const char *elem_name = elem_qualified + elem.name_offset;
3794 return name_cmp (name, elem_name) < 0;
3795 };
3796
5c58de74
PA
3797 auto begin = this->name_components.begin ();
3798 auto end = this->name_components.end ();
3f563c84
PA
3799
3800 /* Find the lower bound. */
3801 auto lower = [&] ()
3802 {
3b00ef10 3803 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3804 return begin;
3805 else
3b00ef10 3806 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3807 } ();
3808
3809 /* Find the upper bound. */
3810 auto upper = [&] ()
3811 {
5c58de74 3812 if (lookup_name_without_params.completion_mode ())
3f563c84 3813 {
e1ef7d7a
PA
3814 /* In completion mode, we want UPPER to point past all
3815 symbols names that have the same prefix. I.e., with
3816 these symbols, and completing "func":
3817
3818 function << lower bound
3819 function1
3820 other_function << upper bound
3821
3822 We find the upper bound by looking for the insertion
3823 point of "func"-with-last-character-incremented,
3824 i.e. "fund". */
3b00ef10 3825 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3826 if (after.empty ())
3f563c84 3827 return end;
e6b2f5ef
PA
3828 return std::lower_bound (lower, end, after.c_str (),
3829 lookup_compare_lower);
3f563c84
PA
3830 }
3831 else
3b00ef10 3832 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3833 } ();
3834
5c58de74
PA
3835 return {lower, upper};
3836}
3837
3838/* See declaration. */
3839
3840void
44ed8f3e 3841mapped_index_base::build_name_components ()
5c58de74
PA
3842{
3843 if (!this->name_components.empty ())
3844 return;
3845
3846 this->name_components_casing = case_sensitivity;
3847 auto *name_cmp
3848 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3849
3850 /* The code below only knows how to break apart components of C++
3851 symbol names (and other languages that use '::' as
3b00ef10 3852 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3853 auto count = this->symbol_name_count ();
3854 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3855 {
44ed8f3e 3856 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3857 continue;
3858
3859 const char *name = this->symbol_name_at (idx);
3860
3861 /* Add each name component to the name component table. */
3862 unsigned int previous_len = 0;
3b00ef10
TT
3863
3864 if (strstr (name, "::") != nullptr)
3865 {
3866 for (unsigned int current_len = cp_find_first_component (name);
3867 name[current_len] != '\0';
3868 current_len += cp_find_first_component (name + current_len))
3869 {
3870 gdb_assert (name[current_len] == ':');
3871 this->name_components.push_back ({previous_len, idx});
3872 /* Skip the '::'. */
3873 current_len += 2;
3874 previous_len = current_len;
3875 }
3876 }
3877 else
5c58de74 3878 {
3b00ef10
TT
3879 /* Handle the Ada encoded (aka mangled) form here. */
3880 for (const char *iter = strstr (name, "__");
3881 iter != nullptr;
3882 iter = strstr (iter, "__"))
3883 {
3884 this->name_components.push_back ({previous_len, idx});
3885 iter += 2;
3886 previous_len = iter - name;
3887 }
5c58de74 3888 }
3b00ef10 3889
5c58de74
PA
3890 this->name_components.push_back ({previous_len, idx});
3891 }
3892
3893 /* Sort name_components elements by name. */
3894 auto name_comp_compare = [&] (const name_component &left,
3895 const name_component &right)
3896 {
3897 const char *left_qualified = this->symbol_name_at (left.idx);
3898 const char *right_qualified = this->symbol_name_at (right.idx);
3899
3900 const char *left_name = left_qualified + left.name_offset;
3901 const char *right_name = right_qualified + right.name_offset;
3902
3903 return name_cmp (left_name, right_name) < 0;
3904 };
3905
3906 std::sort (this->name_components.begin (),
3907 this->name_components.end (),
3908 name_comp_compare);
3909}
3910
3911/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3912 mapped_index_base instead of the containing objfile. This is split
3913 to a separate function in order to be able to unit test the
3914 name_components matching using a mock mapped_index_base. For each
5c58de74 3915 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3916 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3917
3918static void
3919dw2_expand_symtabs_matching_symbol
44ed8f3e 3920 (mapped_index_base &index,
5c58de74
PA
3921 const lookup_name_info &lookup_name_in,
3922 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3923 enum search_domain kind,
3b00ef10 3924 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3925{
3926 lookup_name_info lookup_name_without_params
3927 = lookup_name_in.make_ignore_params ();
5c58de74
PA
3928
3929 /* Build the symbol name component sorted vector, if we haven't
3930 yet. */
3931 index.build_name_components ();
3932
3f563c84
PA
3933 /* The same symbol may appear more than once in the range though.
3934 E.g., if we're looking for symbols that complete "w", and we have
3935 a symbol named "w1::w2", we'll find the two name components for
3936 that same symbol in the range. To be sure we only call the
3937 callback once per symbol, we first collect the symbol name
3938 indexes that matched in a temporary vector and ignore
3939 duplicates. */
3940 std::vector<offset_type> matches;
3f563c84 3941
3b00ef10
TT
3942 struct name_and_matcher
3943 {
3944 symbol_name_matcher_ftype *matcher;
ecc6c606 3945 const char *name;
3b00ef10
TT
3946
3947 bool operator== (const name_and_matcher &other) const
3f563c84 3948 {
ecc6c606 3949 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
3950 }
3951 };
3952
3953 /* A vector holding all the different symbol name matchers, for all
3954 languages. */
3955 std::vector<name_and_matcher> matchers;
3956
3957 for (int i = 0; i < nr_languages; i++)
3958 {
3959 enum language lang_e = (enum language) i;
3960
3961 const language_defn *lang = language_def (lang_e);
3962 symbol_name_matcher_ftype *name_matcher
3963 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 3964
3b00ef10
TT
3965 name_and_matcher key {
3966 name_matcher,
3967 lookup_name_without_params.language_lookup_name (lang_e)
3968 };
3969
3970 /* Don't insert the same comparison routine more than once.
3971 Note that we do this linear walk. This is not a problem in
3972 practice because the number of supported languages is
3973 low. */
3974 if (std::find (matchers.begin (), matchers.end (), key)
3975 != matchers.end ())
9291a0cd 3976 continue;
3b00ef10
TT
3977 matchers.push_back (std::move (key));
3978
3979 auto bounds
3980 = index.find_name_components_bounds (lookup_name_without_params,
3981 lang_e);
3982
3983 /* Now for each symbol name in range, check to see if we have a name
3984 match, and if so, call the MATCH_CALLBACK callback. */
3985
3986 for (; bounds.first != bounds.second; ++bounds.first)
3987 {
3988 const char *qualified = index.symbol_name_at (bounds.first->idx);
3989
3990 if (!name_matcher (qualified, lookup_name_without_params, NULL)
3991 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
3992 continue;
9291a0cd 3993
3b00ef10
TT
3994 matches.push_back (bounds.first->idx);
3995 }
3f563c84
PA
3996 }
3997
3998 std::sort (matches.begin (), matches.end ());
3999
4000 /* Finally call the callback, once per match. */
4001 ULONGEST prev = -1;
4002 for (offset_type idx : matches)
4003 {
4004 if (prev != idx)
4005 {
3b00ef10
TT
4006 if (!match_callback (idx))
4007 break;
3f563c84
PA
4008 prev = idx;
4009 }
4010 }
4011
4012 /* Above we use a type wider than idx's for 'prev', since 0 and
4013 (offset_type)-1 are both possible values. */
4014 static_assert (sizeof (prev) > sizeof (offset_type), "");
4015}
4016
c62446b1
PA
4017#if GDB_SELF_TEST
4018
4019namespace selftests { namespace dw2_expand_symtabs_matching {
4020
a3c5fafd
PA
4021/* A mock .gdb_index/.debug_names-like name index table, enough to
4022 exercise dw2_expand_symtabs_matching_symbol, which works with the
4023 mapped_index_base interface. Builds an index from the symbol list
4024 passed as parameter to the constructor. */
4025class mock_mapped_index : public mapped_index_base
c62446b1
PA
4026{
4027public:
a3c5fafd
PA
4028 mock_mapped_index (gdb::array_view<const char *> symbols)
4029 : m_symbol_table (symbols)
c62446b1
PA
4030 {}
4031
a3c5fafd 4032 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4033
a3c5fafd 4034 /* Return the number of names in the symbol table. */
632e107b 4035 size_t symbol_name_count () const override
c62446b1 4036 {
a3c5fafd 4037 return m_symbol_table.size ();
c62446b1
PA
4038 }
4039
a3c5fafd 4040 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4041 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4042 {
4043 return m_symbol_table[idx];
4044 }
c62446b1 4045
a3c5fafd
PA
4046private:
4047 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4048};
4049
4050/* Convenience function that converts a NULL pointer to a "<null>"
4051 string, to pass to print routines. */
4052
4053static const char *
4054string_or_null (const char *str)
4055{
4056 return str != NULL ? str : "<null>";
4057}
4058
4059/* Check if a lookup_name_info built from
4060 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4061 index. EXPECTED_LIST is the list of expected matches, in expected
4062 matching order. If no match expected, then an empty list is
4063 specified. Returns true on success. On failure prints a warning
4064 indicating the file:line that failed, and returns false. */
4065
4066static bool
4067check_match (const char *file, int line,
4068 mock_mapped_index &mock_index,
4069 const char *name, symbol_name_match_type match_type,
4070 bool completion_mode,
4071 std::initializer_list<const char *> expected_list)
4072{
4073 lookup_name_info lookup_name (name, match_type, completion_mode);
4074
4075 bool matched = true;
4076
4077 auto mismatch = [&] (const char *expected_str,
4078 const char *got)
4079 {
4080 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4081 "expected=\"%s\", got=\"%s\"\n"),
4082 file, line,
4083 (match_type == symbol_name_match_type::FULL
4084 ? "FULL" : "WILD"),
4085 name, string_or_null (expected_str), string_or_null (got));
4086 matched = false;
4087 };
4088
4089 auto expected_it = expected_list.begin ();
4090 auto expected_end = expected_list.end ();
4091
a3c5fafd 4092 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4093 NULL, ALL_DOMAIN,
4094 [&] (offset_type idx)
4095 {
a3c5fafd 4096 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4097 const char *expected_str
4098 = expected_it == expected_end ? NULL : *expected_it++;
4099
4100 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4101 mismatch (expected_str, matched_name);
3b00ef10 4102 return true;
c62446b1
PA
4103 });
4104
4105 const char *expected_str
4106 = expected_it == expected_end ? NULL : *expected_it++;
4107 if (expected_str != NULL)
4108 mismatch (expected_str, NULL);
4109
4110 return matched;
4111}
4112
4113/* The symbols added to the mock mapped_index for testing (in
4114 canonical form). */
4115static const char *test_symbols[] = {
4116 "function",
4117 "std::bar",
4118 "std::zfunction",
4119 "std::zfunction2",
4120 "w1::w2",
4121 "ns::foo<char*>",
4122 "ns::foo<int>",
4123 "ns::foo<long>",
a20714ff
PA
4124 "ns2::tmpl<int>::foo2",
4125 "(anonymous namespace)::A::B::C",
c62446b1 4126
e1ef7d7a
PA
4127 /* These are used to check that the increment-last-char in the
4128 matching algorithm for completion doesn't match "t1_fund" when
4129 completing "t1_func". */
4130 "t1_func",
4131 "t1_func1",
4132 "t1_fund",
4133 "t1_fund1",
4134
4135 /* A UTF-8 name with multi-byte sequences to make sure that
4136 cp-name-parser understands this as a single identifier ("função"
4137 is "function" in PT). */
4138 u8"u8função",
4139
4140 /* \377 (0xff) is Latin1 'ÿ'. */
4141 "yfunc\377",
4142
4143 /* \377 (0xff) is Latin1 'ÿ'. */
4144 "\377",
4145 "\377\377123",
4146
c62446b1
PA
4147 /* A name with all sorts of complications. Starts with "z" to make
4148 it easier for the completion tests below. */
4149#define Z_SYM_NAME \
4150 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4151 "::tuple<(anonymous namespace)::ui*, " \
4152 "std::default_delete<(anonymous namespace)::ui>, void>"
4153
4154 Z_SYM_NAME
4155};
4156
a3c5fafd
PA
4157/* Returns true if the mapped_index_base::find_name_component_bounds
4158 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4159 in completion mode. */
5c58de74
PA
4160
4161static bool
a3c5fafd 4162check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4163 const char *search_name,
4164 gdb::array_view<const char *> expected_syms)
4165{
4166 lookup_name_info lookup_name (search_name,
4167 symbol_name_match_type::FULL, true);
4168
3b00ef10
TT
4169 auto bounds = index.find_name_components_bounds (lookup_name,
4170 language_cplus);
5c58de74
PA
4171
4172 size_t distance = std::distance (bounds.first, bounds.second);
4173 if (distance != expected_syms.size ())
4174 return false;
4175
4176 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4177 {
4178 auto nc_elem = bounds.first + exp_elem;
4179 const char *qualified = index.symbol_name_at (nc_elem->idx);
4180 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4181 return false;
4182 }
4183
4184 return true;
4185}
4186
4187/* Test the lower-level mapped_index::find_name_component_bounds
4188 method. */
4189
c62446b1 4190static void
5c58de74
PA
4191test_mapped_index_find_name_component_bounds ()
4192{
4193 mock_mapped_index mock_index (test_symbols);
4194
a3c5fafd 4195 mock_index.build_name_components ();
5c58de74
PA
4196
4197 /* Test the lower-level mapped_index::find_name_component_bounds
4198 method in completion mode. */
4199 {
4200 static const char *expected_syms[] = {
4201 "t1_func",
4202 "t1_func1",
5c58de74
PA
4203 };
4204
a3c5fafd 4205 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4206 "t1_func", expected_syms));
4207 }
4208
4209 /* Check that the increment-last-char in the name matching algorithm
4210 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4211 {
4212 static const char *expected_syms1[] = {
4213 "\377",
4214 "\377\377123",
4215 };
a3c5fafd 4216 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4217 "\377", expected_syms1));
4218
4219 static const char *expected_syms2[] = {
4220 "\377\377123",
4221 };
a3c5fafd 4222 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4223 "\377\377", expected_syms2));
4224 }
4225}
4226
4227/* Test dw2_expand_symtabs_matching_symbol. */
4228
4229static void
4230test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4231{
4232 mock_mapped_index mock_index (test_symbols);
4233
4234 /* We let all tests run until the end even if some fails, for debug
4235 convenience. */
4236 bool any_mismatch = false;
4237
4238 /* Create the expected symbols list (an initializer_list). Needed
4239 because lists have commas, and we need to pass them to CHECK,
4240 which is a macro. */
4241#define EXPECT(...) { __VA_ARGS__ }
4242
4243 /* Wrapper for check_match that passes down the current
4244 __FILE__/__LINE__. */
4245#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4246 any_mismatch |= !check_match (__FILE__, __LINE__, \
4247 mock_index, \
4248 NAME, MATCH_TYPE, COMPLETION_MODE, \
4249 EXPECTED_LIST)
4250
4251 /* Identity checks. */
4252 for (const char *sym : test_symbols)
4253 {
4254 /* Should be able to match all existing symbols. */
4255 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4256 EXPECT (sym));
4257
4258 /* Should be able to match all existing symbols with
4259 parameters. */
4260 std::string with_params = std::string (sym) + "(int)";
4261 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4262 EXPECT (sym));
4263
4264 /* Should be able to match all existing symbols with
4265 parameters and qualifiers. */
4266 with_params = std::string (sym) + " ( int ) const";
4267 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4268 EXPECT (sym));
4269
4270 /* This should really find sym, but cp-name-parser.y doesn't
4271 know about lvalue/rvalue qualifiers yet. */
4272 with_params = std::string (sym) + " ( int ) &&";
4273 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4274 {});
4275 }
4276
e1ef7d7a
PA
4277 /* Check that the name matching algorithm for completion doesn't get
4278 confused with Latin1 'ÿ' / 0xff. */
4279 {
4280 static const char str[] = "\377";
4281 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4282 EXPECT ("\377", "\377\377123"));
4283 }
4284
4285 /* Check that the increment-last-char in the matching algorithm for
4286 completion doesn't match "t1_fund" when completing "t1_func". */
4287 {
4288 static const char str[] = "t1_func";
4289 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4290 EXPECT ("t1_func", "t1_func1"));
4291 }
4292
c62446b1
PA
4293 /* Check that completion mode works at each prefix of the expected
4294 symbol name. */
4295 {
4296 static const char str[] = "function(int)";
4297 size_t len = strlen (str);
4298 std::string lookup;
4299
4300 for (size_t i = 1; i < len; i++)
4301 {
4302 lookup.assign (str, i);
4303 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4304 EXPECT ("function"));
4305 }
4306 }
4307
4308 /* While "w" is a prefix of both components, the match function
4309 should still only be called once. */
4310 {
4311 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4312 EXPECT ("w1::w2"));
a20714ff
PA
4313 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4314 EXPECT ("w1::w2"));
c62446b1
PA
4315 }
4316
4317 /* Same, with a "complicated" symbol. */
4318 {
4319 static const char str[] = Z_SYM_NAME;
4320 size_t len = strlen (str);
4321 std::string lookup;
4322
4323 for (size_t i = 1; i < len; i++)
4324 {
4325 lookup.assign (str, i);
4326 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4327 EXPECT (Z_SYM_NAME));
4328 }
4329 }
4330
4331 /* In FULL mode, an incomplete symbol doesn't match. */
4332 {
4333 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4334 {});
4335 }
4336
4337 /* A complete symbol with parameters matches any overload, since the
4338 index has no overload info. */
4339 {
4340 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4341 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4342 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4343 EXPECT ("std::zfunction", "std::zfunction2"));
4344 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4345 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4346 }
4347
4348 /* Check that whitespace is ignored appropriately. A symbol with a
4349 template argument list. */
4350 {
4351 static const char expected[] = "ns::foo<int>";
4352 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4353 EXPECT (expected));
a20714ff
PA
4354 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4355 EXPECT (expected));
c62446b1
PA
4356 }
4357
4358 /* Check that whitespace is ignored appropriately. A symbol with a
4359 template argument list that includes a pointer. */
4360 {
4361 static const char expected[] = "ns::foo<char*>";
4362 /* Try both completion and non-completion modes. */
4363 static const bool completion_mode[2] = {false, true};
4364 for (size_t i = 0; i < 2; i++)
4365 {
4366 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4367 completion_mode[i], EXPECT (expected));
a20714ff
PA
4368 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4369 completion_mode[i], EXPECT (expected));
c62446b1
PA
4370
4371 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4372 completion_mode[i], EXPECT (expected));
a20714ff
PA
4373 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4374 completion_mode[i], EXPECT (expected));
c62446b1
PA
4375 }
4376 }
4377
4378 {
4379 /* Check method qualifiers are ignored. */
4380 static const char expected[] = "ns::foo<char*>";
4381 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4382 symbol_name_match_type::FULL, true, EXPECT (expected));
4383 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4384 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4385 CHECK_MATCH ("foo < char * > ( int ) const",
4386 symbol_name_match_type::WILD, true, EXPECT (expected));
4387 CHECK_MATCH ("foo < char * > ( int ) &&",
4388 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4389 }
4390
4391 /* Test lookup names that don't match anything. */
4392 {
a20714ff
PA
4393 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4394 {});
4395
c62446b1
PA
4396 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4397 {});
4398 }
4399
a20714ff
PA
4400 /* Some wild matching tests, exercising "(anonymous namespace)",
4401 which should not be confused with a parameter list. */
4402 {
4403 static const char *syms[] = {
4404 "A::B::C",
4405 "B::C",
4406 "C",
4407 "A :: B :: C ( int )",
4408 "B :: C ( int )",
4409 "C ( int )",
4410 };
4411
4412 for (const char *s : syms)
4413 {
4414 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4415 EXPECT ("(anonymous namespace)::A::B::C"));
4416 }
4417 }
4418
4419 {
4420 static const char expected[] = "ns2::tmpl<int>::foo2";
4421 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4422 EXPECT (expected));
4423 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4424 EXPECT (expected));
4425 }
4426
c62446b1
PA
4427 SELF_CHECK (!any_mismatch);
4428
4429#undef EXPECT
4430#undef CHECK_MATCH
4431}
4432
5c58de74
PA
4433static void
4434run_test ()
4435{
4436 test_mapped_index_find_name_component_bounds ();
4437 test_dw2_expand_symtabs_matching_symbol ();
4438}
4439
c62446b1
PA
4440}} // namespace selftests::dw2_expand_symtabs_matching
4441
4442#endif /* GDB_SELF_TEST */
4443
4b514bc8
JK
4444/* If FILE_MATCHER is NULL or if PER_CU has
4445 dwarf2_per_cu_quick_data::MARK set (see
4446 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4447 EXPANSION_NOTIFY on it. */
4448
4449static void
4450dw2_expand_symtabs_matching_one
4451 (struct dwarf2_per_cu_data *per_cu,
4452 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4453 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4454{
4455 if (file_matcher == NULL || per_cu->v.quick->mark)
4456 {
4457 bool symtab_was_null
4458 = (per_cu->v.quick->compunit_symtab == NULL);
4459
58f0c718 4460 dw2_instantiate_symtab (per_cu, false);
4b514bc8
JK
4461
4462 if (expansion_notify != NULL
4463 && symtab_was_null
4464 && per_cu->v.quick->compunit_symtab != NULL)
4465 expansion_notify (per_cu->v.quick->compunit_symtab);
4466 }
4467}
4468
3f563c84
PA
4469/* Helper for dw2_expand_matching symtabs. Called on each symbol
4470 matched, to expand corresponding CUs that were marked. IDX is the
4471 index of the symbol name that matched. */
4472
4473static void
4474dw2_expand_marked_cus
ed2dc618 4475 (struct dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4476 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4477 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4478 search_domain kind)
4479{
3f563c84
PA
4480 offset_type *vec, vec_len, vec_idx;
4481 bool global_seen = false;
ed2dc618 4482 mapped_index &index = *dwarf2_per_objfile->index_table;
3f563c84 4483
61920122 4484 vec = (offset_type *) (index.constant_pool
f00a2de2 4485 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4486 vec_len = MAYBE_SWAP (vec[0]);
4487 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4488 {
61920122
PA
4489 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4490 /* This value is only valid for index versions >= 7. */
4491 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4492 gdb_index_symbol_kind symbol_kind =
4493 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4494 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4495 /* Only check the symbol attributes if they're present.
4496 Indices prior to version 7 don't record them,
4497 and indices >= 7 may elide them for certain symbols
4498 (gold does this). */
4499 int attrs_valid =
4500 (index.version >= 7
4501 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4502
4503 /* Work around gold/15646. */
4504 if (attrs_valid)
9291a0cd 4505 {
61920122
PA
4506 if (!is_static && global_seen)
4507 continue;
4508 if (!is_static)
4509 global_seen = true;
4510 }
3190f0c6 4511
61920122
PA
4512 /* Only check the symbol's kind if it has one. */
4513 if (attrs_valid)
4514 {
4515 switch (kind)
8943b874 4516 {
61920122
PA
4517 case VARIABLES_DOMAIN:
4518 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4519 continue;
4520 break;
4521 case FUNCTIONS_DOMAIN:
4522 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4523 continue;
61920122
PA
4524 break;
4525 case TYPES_DOMAIN:
4526 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4527 continue;
4528 break;
59c35742
AB
4529 case MODULES_DOMAIN:
4530 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4531 continue;
4532 break;
61920122
PA
4533 default:
4534 break;
8943b874 4535 }
61920122 4536 }
8943b874 4537
61920122 4538 /* Don't crash on bad data. */
b76e467d 4539 if (cu_index >= (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 4540 + dwarf2_per_objfile->all_type_units.size ()))
61920122 4541 {
b98664d3 4542 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4543 " [in module %s]"),
4544 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4545 continue;
4546 }
4547
ff4c9fec 4548 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (cu_index);
4b514bc8
JK
4549 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4550 expansion_notify);
61920122
PA
4551 }
4552}
4553
4b514bc8
JK
4554/* If FILE_MATCHER is non-NULL, set all the
4555 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4556 that match FILE_MATCHER. */
4557
61920122 4558static void
4b514bc8 4559dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4560 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4561 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4562{
4b514bc8 4563 if (file_matcher == NULL)
61920122
PA
4564 return;
4565
4b514bc8
JK
4566 objfile *const objfile = dwarf2_per_objfile->objfile;
4567
4568 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4569 htab_eq_pointer,
4570 NULL, xcalloc, xfree));
4571 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4572 htab_eq_pointer,
4573 NULL, xcalloc, xfree));
61920122 4574
4b514bc8
JK
4575 /* The rule is CUs specify all the files, including those used by
4576 any TU, so there's no need to scan TUs here. */
61920122 4577
b76e467d 4578 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4579 {
927aa2e7
JK
4580 QUIT;
4581
4582 per_cu->v.quick->mark = 0;
4583
4584 /* We only need to look at symtabs not already expanded. */
4585 if (per_cu->v.quick->compunit_symtab)
4586 continue;
4587
b76e467d 4588 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4589 if (file_data == NULL)
4590 continue;
4591
4592 if (htab_find (visited_not_found.get (), file_data) != NULL)
4593 continue;
4594 else if (htab_find (visited_found.get (), file_data) != NULL)
4595 {
4596 per_cu->v.quick->mark = 1;
4597 continue;
4598 }
4599
b76e467d 4600 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4601 {
4602 const char *this_real_name;
4603
4604 if (file_matcher (file_data->file_names[j], false))
4605 {
4606 per_cu->v.quick->mark = 1;
4607 break;
4608 }
4609
4610 /* Before we invoke realpath, which can get expensive when many
4611 files are involved, do a quick comparison of the basenames. */
4612 if (!basenames_may_differ
4613 && !file_matcher (lbasename (file_data->file_names[j]),
4614 true))
4615 continue;
4616
4617 this_real_name = dw2_get_real_path (objfile, file_data, j);
4618 if (file_matcher (this_real_name, false))
4619 {
4620 per_cu->v.quick->mark = 1;
4621 break;
4622 }
4623 }
4624
b76e467d
SM
4625 void **slot = htab_find_slot (per_cu->v.quick->mark
4626 ? visited_found.get ()
4627 : visited_not_found.get (),
4628 file_data, INSERT);
927aa2e7
JK
4629 *slot = file_data;
4630 }
4631}
4632
4633static void
4634dw2_expand_symtabs_matching
4635 (struct objfile *objfile,
4636 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4637 const lookup_name_info *lookup_name,
927aa2e7
JK
4638 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4639 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4640 enum search_domain kind)
4641{
ed2dc618
SM
4642 struct dwarf2_per_objfile *dwarf2_per_objfile
4643 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4644
4645 /* index_table is NULL if OBJF_READNOW. */
4646 if (!dwarf2_per_objfile->index_table)
4647 return;
4648
ed2dc618 4649 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4650
c1a66c06
TV
4651 if (symbol_matcher == NULL && lookup_name == NULL)
4652 {
4653 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
4654 {
4655 QUIT;
4656
4657 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
4658 expansion_notify);
4659 }
4660 return;
4661 }
4662
927aa2e7
JK
4663 mapped_index &index = *dwarf2_per_objfile->index_table;
4664
c1a66c06 4665 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4666 symbol_matcher,
4667 kind, [&] (offset_type idx)
4668 {
ed2dc618 4669 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4670 expansion_notify, kind);
3b00ef10 4671 return true;
927aa2e7
JK
4672 });
4673}
4674
4675/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4676 symtab. */
4677
4678static struct compunit_symtab *
4679recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4680 CORE_ADDR pc)
4681{
4682 int i;
4683
4684 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4685 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4686 return cust;
4687
4688 if (cust->includes == NULL)
4689 return NULL;
4690
4691 for (i = 0; cust->includes[i]; ++i)
4692 {
4693 struct compunit_symtab *s = cust->includes[i];
4694
4695 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4696 if (s != NULL)
4697 return s;
4698 }
4699
4700 return NULL;
4701}
4702
4703static struct compunit_symtab *
4704dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4705 struct bound_minimal_symbol msymbol,
4706 CORE_ADDR pc,
4707 struct obj_section *section,
4708 int warn_if_readin)
4709{
4710 struct dwarf2_per_cu_data *data;
4711 struct compunit_symtab *result;
4712
d320c2b5 4713 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4714 return NULL;
4715
b3b3bada 4716 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4717 data = (struct dwarf2_per_cu_data *) addrmap_find
4718 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4719 if (!data)
4720 return NULL;
4721
4722 if (warn_if_readin && data->v.quick->compunit_symtab)
4723 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4724 paddress (objfile->arch (), pc));
927aa2e7
JK
4725
4726 result
58f0c718
TT
4727 = recursively_find_pc_sect_compunit_symtab (dw2_instantiate_symtab (data,
4728 false),
927aa2e7
JK
4729 pc);
4730 gdb_assert (result != NULL);
4731 return result;
4732}
4733
4734static void
4735dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4736 void *data, int need_fullname)
4737{
ed2dc618
SM
4738 struct dwarf2_per_objfile *dwarf2_per_objfile
4739 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4740
4741 if (!dwarf2_per_objfile->filenames_cache)
4742 {
4743 dwarf2_per_objfile->filenames_cache.emplace ();
4744
4745 htab_up visited (htab_create_alloc (10,
4746 htab_hash_pointer, htab_eq_pointer,
4747 NULL, xcalloc, xfree));
4748
4749 /* The rule is CUs specify all the files, including those used
4750 by any TU, so there's no need to scan TUs here. We can
4751 ignore file names coming from already-expanded CUs. */
4752
b76e467d 4753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4754 {
927aa2e7
JK
4755 if (per_cu->v.quick->compunit_symtab)
4756 {
4757 void **slot = htab_find_slot (visited.get (),
4758 per_cu->v.quick->file_names,
4759 INSERT);
4760
4761 *slot = per_cu->v.quick->file_names;
4762 }
4763 }
4764
b76e467d 4765 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
927aa2e7 4766 {
927aa2e7
JK
4767 /* We only need to look at symtabs not already expanded. */
4768 if (per_cu->v.quick->compunit_symtab)
4769 continue;
4770
b76e467d 4771 quick_file_names *file_data = dw2_get_file_names (per_cu);
927aa2e7
JK
4772 if (file_data == NULL)
4773 continue;
4774
b76e467d 4775 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4776 if (*slot)
4777 {
4778 /* Already visited. */
4779 continue;
4780 }
4781 *slot = file_data;
4782
4783 for (int j = 0; j < file_data->num_file_names; ++j)
4784 {
4785 const char *filename = file_data->file_names[j];
4786 dwarf2_per_objfile->filenames_cache->seen (filename);
4787 }
4788 }
4789 }
4790
4791 dwarf2_per_objfile->filenames_cache->traverse ([&] (const char *filename)
4792 {
4793 gdb::unique_xmalloc_ptr<char> this_real_name;
4794
4795 if (need_fullname)
4796 this_real_name = gdb_realpath (filename);
4797 (*fun) (filename, this_real_name.get (), data);
4798 });
4799}
4800
4801static int
4802dw2_has_symbols (struct objfile *objfile)
4803{
4804 return 1;
4805}
4806
4807const struct quick_symbol_functions dwarf2_gdb_index_functions =
4808{
4809 dw2_has_symbols,
4810 dw2_find_last_source_symtab,
4811 dw2_forget_cached_source_info,
4812 dw2_map_symtabs_matching_filename,
4813 dw2_lookup_symbol,
d3214198 4814 NULL,
927aa2e7
JK
4815 dw2_print_stats,
4816 dw2_dump,
927aa2e7
JK
4817 dw2_expand_symtabs_for_function,
4818 dw2_expand_all_symtabs,
4819 dw2_expand_symtabs_with_fullname,
4820 dw2_map_matching_symbols,
4821 dw2_expand_symtabs_matching,
4822 dw2_find_pc_sect_compunit_symtab,
4823 NULL,
4824 dw2_map_symbol_filenames
4825};
4826
4827/* DWARF-5 debug_names reader. */
4828
4829/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4830static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4831
4832/* A helper function that reads the .debug_names section in SECTION
4833 and fills in MAP. FILENAME is the name of the file containing the
4834 section; it is used for error reporting.
4835
4836 Returns true if all went well, false otherwise. */
4837
4838static bool
4839read_debug_names_from_section (struct objfile *objfile,
4840 const char *filename,
4841 struct dwarf2_section_info *section,
4842 mapped_debug_names &map)
4843{
96b79293 4844 if (section->empty ())
927aa2e7
JK
4845 return false;
4846
4847 /* Older elfutils strip versions could keep the section in the main
4848 executable while splitting it for the separate debug info file. */
96b79293 4849 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4850 return false;
4851
96b79293 4852 section->read (objfile);
927aa2e7 4853
08feed99 4854 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4855
4856 const gdb_byte *addr = section->buffer;
4857
96b79293 4858 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4859
4860 unsigned int bytes_read;
4861 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4862 addr += bytes_read;
4863
4864 map.dwarf5_is_dwarf64 = bytes_read != 4;
4865 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4866 if (bytes_read + length != section->size)
4867 {
4868 /* There may be multiple per-CU indices. */
4869 warning (_("Section .debug_names in %s length %s does not match "
4870 "section length %s, ignoring .debug_names."),
4871 filename, plongest (bytes_read + length),
4872 pulongest (section->size));
4873 return false;
4874 }
4875
4876 /* The version number. */
4877 uint16_t version = read_2_bytes (abfd, addr);
4878 addr += 2;
4879 if (version != 5)
4880 {
4881 warning (_("Section .debug_names in %s has unsupported version %d, "
4882 "ignoring .debug_names."),
4883 filename, version);
4884 return false;
4885 }
4886
4887 /* Padding. */
4888 uint16_t padding = read_2_bytes (abfd, addr);
4889 addr += 2;
4890 if (padding != 0)
4891 {
4892 warning (_("Section .debug_names in %s has unsupported padding %d, "
4893 "ignoring .debug_names."),
4894 filename, padding);
4895 return false;
4896 }
4897
4898 /* comp_unit_count - The number of CUs in the CU list. */
4899 map.cu_count = read_4_bytes (abfd, addr);
4900 addr += 4;
4901
4902 /* local_type_unit_count - The number of TUs in the local TU
4903 list. */
4904 map.tu_count = read_4_bytes (abfd, addr);
4905 addr += 4;
4906
4907 /* foreign_type_unit_count - The number of TUs in the foreign TU
4908 list. */
4909 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4910 addr += 4;
4911 if (foreign_tu_count != 0)
4912 {
4913 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4914 "ignoring .debug_names."),
4915 filename, static_cast<unsigned long> (foreign_tu_count));
4916 return false;
4917 }
4918
4919 /* bucket_count - The number of hash buckets in the hash lookup
4920 table. */
4921 map.bucket_count = read_4_bytes (abfd, addr);
4922 addr += 4;
4923
4924 /* name_count - The number of unique names in the index. */
4925 map.name_count = read_4_bytes (abfd, addr);
4926 addr += 4;
4927
4928 /* abbrev_table_size - The size in bytes of the abbreviations
4929 table. */
4930 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
4931 addr += 4;
4932
4933 /* augmentation_string_size - The size in bytes of the augmentation
4934 string. This value is rounded up to a multiple of 4. */
4935 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
4936 addr += 4;
4937 map.augmentation_is_gdb = ((augmentation_string_size
4938 == sizeof (dwarf5_augmentation))
4939 && memcmp (addr, dwarf5_augmentation,
4940 sizeof (dwarf5_augmentation)) == 0);
4941 augmentation_string_size += (-augmentation_string_size) & 3;
4942 addr += augmentation_string_size;
4943
4944 /* List of CUs */
4945 map.cu_table_reordered = addr;
4946 addr += map.cu_count * map.offset_size;
4947
4948 /* List of Local TUs */
4949 map.tu_table_reordered = addr;
4950 addr += map.tu_count * map.offset_size;
4951
4952 /* Hash Lookup Table */
4953 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4954 addr += map.bucket_count * 4;
4955 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
4956 addr += map.name_count * 4;
4957
4958 /* Name Table */
4959 map.name_table_string_offs_reordered = addr;
4960 addr += map.name_count * map.offset_size;
4961 map.name_table_entry_offs_reordered = addr;
4962 addr += map.name_count * map.offset_size;
4963
4964 const gdb_byte *abbrev_table_start = addr;
4965 for (;;)
4966 {
927aa2e7
JK
4967 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
4968 addr += bytes_read;
4969 if (index_num == 0)
4970 break;
4971
4972 const auto insertpair
4973 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
4974 if (!insertpair.second)
4975 {
4976 warning (_("Section .debug_names in %s has duplicate index %s, "
4977 "ignoring .debug_names."),
4978 filename, pulongest (index_num));
4979 return false;
4980 }
4981 mapped_debug_names::index_val &indexval = insertpair.first->second;
4982 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
4983 addr += bytes_read;
4984
4985 for (;;)
4986 {
4987 mapped_debug_names::index_val::attr attr;
4988 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
4989 addr += bytes_read;
4990 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
4991 addr += bytes_read;
4992 if (attr.form == DW_FORM_implicit_const)
4993 {
4994 attr.implicit_const = read_signed_leb128 (abfd, addr,
4995 &bytes_read);
4996 addr += bytes_read;
4997 }
4998 if (attr.dw_idx == 0 && attr.form == 0)
4999 break;
5000 indexval.attr_vec.push_back (std::move (attr));
5001 }
5002 }
5003 if (addr != abbrev_table_start + abbrev_table_size)
5004 {
5005 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5006 "of size %s vs. written as %u, ignoring .debug_names."),
5007 filename, plongest (addr - abbrev_table_start),
5008 abbrev_table_size);
927aa2e7
JK
5009 return false;
5010 }
5011 map.entry_pool = addr;
5012
5013 return true;
5014}
5015
5016/* A helper for create_cus_from_debug_names that handles the MAP's CU
5017 list. */
5018
5019static void
ed2dc618 5020create_cus_from_debug_names_list (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5021 const mapped_debug_names &map,
5022 dwarf2_section_info &section,
b76e467d 5023 bool is_dwz)
927aa2e7
JK
5024{
5025 sect_offset sect_off_prev;
5026 for (uint32_t i = 0; i <= map.cu_count; ++i)
5027 {
5028 sect_offset sect_off_next;
5029 if (i < map.cu_count)
5030 {
5031 sect_off_next
5032 = (sect_offset) (extract_unsigned_integer
5033 (map.cu_table_reordered + i * map.offset_size,
5034 map.offset_size,
5035 map.dwarf5_byte_order));
5036 }
5037 else
5038 sect_off_next = (sect_offset) section.size;
5039 if (i >= 1)
5040 {
5041 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5042 dwarf2_per_cu_data *per_cu
ed2dc618 5043 = create_cu_from_index_list (dwarf2_per_objfile, &section, is_dwz,
927aa2e7 5044 sect_off_prev, length);
b76e467d 5045 dwarf2_per_objfile->all_comp_units.push_back (per_cu);
927aa2e7
JK
5046 }
5047 sect_off_prev = sect_off_next;
5048 }
5049}
5050
5051/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5052 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5053
5054static void
ed2dc618 5055create_cus_from_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
5056 const mapped_debug_names &map,
5057 const mapped_debug_names &dwz_map)
5058{
b76e467d
SM
5059 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
5060 dwarf2_per_objfile->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5061
ed2dc618
SM
5062 create_cus_from_debug_names_list (dwarf2_per_objfile, map,
5063 dwarf2_per_objfile->info,
b76e467d 5064 false /* is_dwz */);
927aa2e7
JK
5065
5066 if (dwz_map.cu_count == 0)
5067 return;
5068
ed2dc618
SM
5069 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
5070 create_cus_from_debug_names_list (dwarf2_per_objfile, dwz_map, dwz->info,
b76e467d 5071 true /* is_dwz */);
927aa2e7
JK
5072}
5073
5074/* Read .debug_names. If everything went ok, initialize the "quick"
5075 elements of all the CUs and return true. Otherwise, return false. */
5076
5077static bool
ed2dc618 5078dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5079{
22ca247e
TT
5080 std::unique_ptr<mapped_debug_names> map
5081 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5082 mapped_debug_names dwz_map (dwarf2_per_objfile);
5083 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5084
5085 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5086 &dwarf2_per_objfile->debug_names,
22ca247e 5087 *map))
927aa2e7
JK
5088 return false;
5089
5090 /* Don't use the index if it's empty. */
22ca247e 5091 if (map->name_count == 0)
927aa2e7
JK
5092 return false;
5093
5094 /* If there is a .dwz file, read it so we can get its CU list as
5095 well. */
ed2dc618 5096 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
927aa2e7
JK
5097 if (dwz != NULL)
5098 {
5099 if (!read_debug_names_from_section (objfile,
00f93c44 5100 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5101 &dwz->debug_names, dwz_map))
5102 {
5103 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5104 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5105 return false;
5106 }
5107 }
5108
22ca247e 5109 create_cus_from_debug_names (dwarf2_per_objfile, *map, dwz_map);
927aa2e7 5110
22ca247e 5111 if (map->tu_count != 0)
927aa2e7
JK
5112 {
5113 /* We can only handle a single .debug_types when we have an
5114 index. */
fd5866f6 5115 if (dwarf2_per_objfile->types.size () != 1)
927aa2e7
JK
5116 return false;
5117
fd5866f6 5118 dwarf2_section_info *section = &dwarf2_per_objfile->types[0];
927aa2e7
JK
5119
5120 create_signatured_type_table_from_debug_names
22ca247e 5121 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->abbrev);
927aa2e7
JK
5122 }
5123
ed2dc618
SM
5124 create_addrmap_from_aranges (dwarf2_per_objfile,
5125 &dwarf2_per_objfile->debug_aranges);
927aa2e7 5126
22ca247e 5127 dwarf2_per_objfile->debug_names_table = std::move (map);
927aa2e7
JK
5128 dwarf2_per_objfile->using_index = 1;
5129 dwarf2_per_objfile->quick_file_names_table =
b76e467d 5130 create_quick_file_names_table (dwarf2_per_objfile->all_comp_units.size ());
927aa2e7
JK
5131
5132 return true;
5133}
5134
927aa2e7
JK
5135/* Type used to manage iterating over all CUs looking for a symbol for
5136 .debug_names. */
5137
5138class dw2_debug_names_iterator
5139{
5140public:
927aa2e7 5141 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5142 gdb::optional<block_enum> block_index,
5143 domain_enum domain,
927aa2e7 5144 const char *name)
2b79f376 5145 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5146 m_addr (find_vec_in_debug_names (map, name))
5147 {}
5148
5149 dw2_debug_names_iterator (const mapped_debug_names &map,
5150 search_domain search, uint32_t namei)
5151 : m_map (map),
5152 m_search (search),
5153 m_addr (find_vec_in_debug_names (map, namei))
5154 {}
5155
3b00ef10
TT
5156 dw2_debug_names_iterator (const mapped_debug_names &map,
5157 block_enum block_index, domain_enum domain,
5158 uint32_t namei)
5159 : m_map (map), m_block_index (block_index), m_domain (domain),
5160 m_addr (find_vec_in_debug_names (map, namei))
5161 {}
5162
927aa2e7
JK
5163 /* Return the next matching CU or NULL if there are no more. */
5164 dwarf2_per_cu_data *next ();
5165
5166private:
5167 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5168 const char *name);
5169 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5170 uint32_t namei);
5171
5172 /* The internalized form of .debug_names. */
5173 const mapped_debug_names &m_map;
5174
2b79f376
SM
5175 /* If set, only look for symbols that match that block. Valid values are
5176 GLOBAL_BLOCK and STATIC_BLOCK. */
5177 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5178
5179 /* The kind of symbol we're looking for. */
5180 const domain_enum m_domain = UNDEF_DOMAIN;
5181 const search_domain m_search = ALL_DOMAIN;
5182
5183 /* The list of CUs from the index entry of the symbol, or NULL if
5184 not found. */
5185 const gdb_byte *m_addr;
5186};
5187
5188const char *
5189mapped_debug_names::namei_to_name (uint32_t namei) const
5190{
5191 const ULONGEST namei_string_offs
5192 = extract_unsigned_integer ((name_table_string_offs_reordered
5193 + namei * offset_size),
5194 offset_size,
5195 dwarf5_byte_order);
4f44ae6c
TT
5196 return read_indirect_string_at_offset (dwarf2_per_objfile,
5197 namei_string_offs);
927aa2e7
JK
5198}
5199
5200/* Find a slot in .debug_names for the object named NAME. If NAME is
5201 found, return pointer to its pool data. If NAME cannot be found,
5202 return NULL. */
5203
5204const gdb_byte *
5205dw2_debug_names_iterator::find_vec_in_debug_names
5206 (const mapped_debug_names &map, const char *name)
5207{
5208 int (*cmp) (const char *, const char *);
5209
54ee4252 5210 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5211 if (current_language->la_language == language_cplus
5212 || current_language->la_language == language_fortran
5213 || current_language->la_language == language_d)
5214 {
5215 /* NAME is already canonical. Drop any qualifiers as
5216 .debug_names does not contain any. */
5217
5218 if (strchr (name, '(') != NULL)
5219 {
54ee4252 5220 without_params = cp_remove_params (name);
927aa2e7 5221 if (without_params != NULL)
54ee4252 5222 name = without_params.get ();
927aa2e7
JK
5223 }
5224 }
5225
5226 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5227
5228 const uint32_t full_hash = dwarf5_djb_hash (name);
5229 uint32_t namei
5230 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5231 (map.bucket_table_reordered
5232 + (full_hash % map.bucket_count)), 4,
5233 map.dwarf5_byte_order);
5234 if (namei == 0)
5235 return NULL;
5236 --namei;
5237 if (namei >= map.name_count)
5238 {
b98664d3 5239 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5240 "[in module %s]"),
5241 namei, map.name_count,
ed2dc618 5242 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5243 return NULL;
5244 }
5245
5246 for (;;)
5247 {
5248 const uint32_t namei_full_hash
5249 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5250 (map.hash_table_reordered + namei), 4,
5251 map.dwarf5_byte_order);
5252 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5253 return NULL;
5254
5255 if (full_hash == namei_full_hash)
5256 {
5257 const char *const namei_string = map.namei_to_name (namei);
5258
5259#if 0 /* An expensive sanity check. */
5260 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5261 {
b98664d3 5262 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5263 "[in module %s]"),
5264 namei, objfile_name (dwarf2_per_objfile->objfile));
5265 return NULL;
5266 }
5267#endif
5268
5269 if (cmp (namei_string, name) == 0)
5270 {
5271 const ULONGEST namei_entry_offs
5272 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5273 + namei * map.offset_size),
5274 map.offset_size, map.dwarf5_byte_order);
5275 return map.entry_pool + namei_entry_offs;
5276 }
5277 }
5278
5279 ++namei;
5280 if (namei >= map.name_count)
5281 return NULL;
5282 }
5283}
5284
5285const gdb_byte *
5286dw2_debug_names_iterator::find_vec_in_debug_names
5287 (const mapped_debug_names &map, uint32_t namei)
5288{
5289 if (namei >= map.name_count)
5290 {
b98664d3 5291 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5292 "[in module %s]"),
5293 namei, map.name_count,
ed2dc618 5294 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5295 return NULL;
5296 }
5297
5298 const ULONGEST namei_entry_offs
5299 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5300 + namei * map.offset_size),
5301 map.offset_size, map.dwarf5_byte_order);
5302 return map.entry_pool + namei_entry_offs;
5303}
5304
5305/* See dw2_debug_names_iterator. */
5306
5307dwarf2_per_cu_data *
5308dw2_debug_names_iterator::next ()
5309{
5310 if (m_addr == NULL)
5311 return NULL;
5312
ed2dc618
SM
5313 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5314 struct objfile *objfile = dwarf2_per_objfile->objfile;
5315 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5316
5317 again:
5318
5319 unsigned int bytes_read;
5320 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5321 m_addr += bytes_read;
5322 if (abbrev == 0)
5323 return NULL;
5324
5325 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5326 if (indexval_it == m_map.abbrev_map.cend ())
5327 {
b98664d3 5328 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5329 "[in module %s]"),
ed2dc618 5330 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5331 return NULL;
5332 }
5333 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5334 enum class symbol_linkage {
5335 unknown,
5336 static_,
5337 extern_,
23c13d42 5338 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5339 dwarf2_per_cu_data *per_cu = NULL;
5340 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5341 {
5342 ULONGEST ull;
5343 switch (attr.form)
5344 {
5345 case DW_FORM_implicit_const:
5346 ull = attr.implicit_const;
5347 break;
5348 case DW_FORM_flag_present:
5349 ull = 1;
5350 break;
5351 case DW_FORM_udata:
5352 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5353 m_addr += bytes_read;
5354 break;
5355 default:
b98664d3 5356 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5357 dwarf_form_name (attr.form),
ed2dc618 5358 objfile_name (objfile));
927aa2e7
JK
5359 return NULL;
5360 }
5361 switch (attr.dw_idx)
5362 {
5363 case DW_IDX_compile_unit:
5364 /* Don't crash on bad data. */
b76e467d 5365 if (ull >= dwarf2_per_objfile->all_comp_units.size ())
927aa2e7 5366 {
b98664d3 5367 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5368 " [in module %s]"),
5369 pulongest (ull),
5370 objfile_name (dwarf2_per_objfile->objfile));
5371 continue;
5372 }
ff4c9fec 5373 per_cu = dwarf2_per_objfile->get_cutu (ull);
927aa2e7 5374 break;
8af5c486
JK
5375 case DW_IDX_type_unit:
5376 /* Don't crash on bad data. */
b2bdb8cf 5377 if (ull >= dwarf2_per_objfile->all_type_units.size ())
8af5c486 5378 {
b98664d3 5379 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5380 " [in module %s]"),
5381 pulongest (ull),
5382 objfile_name (dwarf2_per_objfile->objfile));
5383 continue;
5384 }
ff4c9fec 5385 per_cu = &dwarf2_per_objfile->get_tu (ull)->per_cu;
8af5c486 5386 break;
927aa2e7
JK
5387 case DW_IDX_GNU_internal:
5388 if (!m_map.augmentation_is_gdb)
5389 break;
23c13d42 5390 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5391 break;
5392 case DW_IDX_GNU_external:
5393 if (!m_map.augmentation_is_gdb)
5394 break;
23c13d42 5395 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5396 break;
5397 }
5398 }
5399
5400 /* Skip if already read in. */
5401 if (per_cu->v.quick->compunit_symtab)
5402 goto again;
5403
5404 /* Check static vs global. */
23c13d42 5405 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5406 {
2b79f376 5407 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5408 const bool symbol_is_static =
5409 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5410 if (want_static != symbol_is_static)
2b79f376 5411 goto again;
927aa2e7
JK
5412 }
5413
5414 /* Match dw2_symtab_iter_next, symbol_kind
5415 and debug_names::psymbol_tag. */
5416 switch (m_domain)
5417 {
5418 case VAR_DOMAIN:
5419 switch (indexval.dwarf_tag)
5420 {
5421 case DW_TAG_variable:
5422 case DW_TAG_subprogram:
5423 /* Some types are also in VAR_DOMAIN. */
5424 case DW_TAG_typedef:
5425 case DW_TAG_structure_type:
5426 break;
5427 default:
5428 goto again;
5429 }
5430 break;
5431 case STRUCT_DOMAIN:
5432 switch (indexval.dwarf_tag)
5433 {
5434 case DW_TAG_typedef:
5435 case DW_TAG_structure_type:
5436 break;
5437 default:
5438 goto again;
5439 }
5440 break;
5441 case LABEL_DOMAIN:
5442 switch (indexval.dwarf_tag)
5443 {
5444 case 0:
5445 case DW_TAG_variable:
5446 break;
5447 default:
5448 goto again;
5449 }
5450 break;
59c35742
AB
5451 case MODULE_DOMAIN:
5452 switch (indexval.dwarf_tag)
5453 {
5454 case DW_TAG_module:
5455 break;
5456 default:
5457 goto again;
5458 }
5459 break;
927aa2e7
JK
5460 default:
5461 break;
5462 }
5463
5464 /* Match dw2_expand_symtabs_matching, symbol_kind and
5465 debug_names::psymbol_tag. */
5466 switch (m_search)
4b514bc8 5467 {
927aa2e7
JK
5468 case VARIABLES_DOMAIN:
5469 switch (indexval.dwarf_tag)
4b514bc8 5470 {
927aa2e7
JK
5471 case DW_TAG_variable:
5472 break;
5473 default:
5474 goto again;
4b514bc8 5475 }
927aa2e7
JK
5476 break;
5477 case FUNCTIONS_DOMAIN:
5478 switch (indexval.dwarf_tag)
4b514bc8 5479 {
927aa2e7
JK
5480 case DW_TAG_subprogram:
5481 break;
5482 default:
5483 goto again;
4b514bc8 5484 }
927aa2e7
JK
5485 break;
5486 case TYPES_DOMAIN:
5487 switch (indexval.dwarf_tag)
5488 {
5489 case DW_TAG_typedef:
5490 case DW_TAG_structure_type:
5491 break;
5492 default:
5493 goto again;
5494 }
5495 break;
59c35742
AB
5496 case MODULES_DOMAIN:
5497 switch (indexval.dwarf_tag)
5498 {
5499 case DW_TAG_module:
5500 break;
5501 default:
5502 goto again;
5503 }
927aa2e7
JK
5504 default:
5505 break;
4b514bc8 5506 }
927aa2e7
JK
5507
5508 return per_cu;
4b514bc8 5509}
61920122 5510
927aa2e7 5511static struct compunit_symtab *
c7f839cb 5512dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5513 const char *name, domain_enum domain)
4b514bc8 5514{
ed2dc618
SM
5515 struct dwarf2_per_objfile *dwarf2_per_objfile
5516 = get_dwarf2_per_objfile (objfile);
61920122 5517
927aa2e7
JK
5518 const auto &mapp = dwarf2_per_objfile->debug_names_table;
5519 if (!mapp)
61920122 5520 {
927aa2e7
JK
5521 /* index is NULL if OBJF_READNOW. */
5522 return NULL;
5523 }
5524 const auto &map = *mapp;
9291a0cd 5525
2b79f376 5526 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5527
927aa2e7
JK
5528 struct compunit_symtab *stab_best = NULL;
5529 struct dwarf2_per_cu_data *per_cu;
5530 while ((per_cu = iter.next ()) != NULL)
5531 {
5532 struct symbol *sym, *with_opaque = NULL;
58f0c718 5533 struct compunit_symtab *stab = dw2_instantiate_symtab (per_cu, false);
927aa2e7 5534 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5535 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5536
927aa2e7
JK
5537 sym = block_find_symbol (block, name, domain,
5538 block_find_non_opaque_type_preferred,
5539 &with_opaque);
9703b513 5540
927aa2e7
JK
5541 /* Some caution must be observed with overloaded functions and
5542 methods, since the index will not contain any overload
5543 information (but NAME might contain it). */
a3ec0bb1 5544
927aa2e7 5545 if (sym != NULL
987012b8 5546 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5547 return stab;
5548 if (with_opaque != NULL
987012b8 5549 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5550 stab_best = stab;
9703b513 5551
927aa2e7 5552 /* Keep looking through other CUs. */
9703b513
TT
5553 }
5554
927aa2e7 5555 return stab_best;
9703b513
TT
5556}
5557
927aa2e7
JK
5558/* This dumps minimal information about .debug_names. It is called
5559 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5560 uses this to verify that .debug_names has been loaded. */
9291a0cd 5561
927aa2e7
JK
5562static void
5563dw2_debug_names_dump (struct objfile *objfile)
5564{
ed2dc618
SM
5565 struct dwarf2_per_objfile *dwarf2_per_objfile
5566 = get_dwarf2_per_objfile (objfile);
5567
927aa2e7
JK
5568 gdb_assert (dwarf2_per_objfile->using_index);
5569 printf_filtered (".debug_names:");
5570 if (dwarf2_per_objfile->debug_names_table)
5571 printf_filtered (" exists\n");
5572 else
5573 printf_filtered (" faked for \"readnow\"\n");
5574 printf_filtered ("\n");
9291a0cd
TT
5575}
5576
9291a0cd 5577static void
927aa2e7
JK
5578dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5579 const char *func_name)
9291a0cd 5580{
ed2dc618
SM
5581 struct dwarf2_per_objfile *dwarf2_per_objfile
5582 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5583
927aa2e7
JK
5584 /* dwarf2_per_objfile->debug_names_table is NULL if OBJF_READNOW. */
5585 if (dwarf2_per_objfile->debug_names_table)
24c79950 5586 {
927aa2e7 5587 const mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
24c79950 5588
2b79f376 5589 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5590
927aa2e7
JK
5591 struct dwarf2_per_cu_data *per_cu;
5592 while ((per_cu = iter.next ()) != NULL)
58f0c718 5593 dw2_instantiate_symtab (per_cu, false);
927aa2e7
JK
5594 }
5595}
24c79950 5596
3b00ef10
TT
5597static void
5598dw2_debug_names_map_matching_symbols
5599 (struct objfile *objfile,
5600 const lookup_name_info &name, domain_enum domain,
5601 int global,
5602 gdb::function_view<symbol_found_callback_ftype> callback,
5603 symbol_compare_ftype *ordered_compare)
5604{
5605 struct dwarf2_per_objfile *dwarf2_per_objfile
5606 = get_dwarf2_per_objfile (objfile);
5607
5608 /* debug_names_table is NULL if OBJF_READNOW. */
5609 if (!dwarf2_per_objfile->debug_names_table)
5610 return;
5611
5612 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
5613 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5614
5615 const char *match_name = name.ada ().lookup_name ().c_str ();
5616 auto matcher = [&] (const char *symname)
5617 {
5618 if (ordered_compare == nullptr)
5619 return true;
5620 return ordered_compare (symname, match_name) == 0;
5621 };
5622
5623 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5624 [&] (offset_type namei)
5625 {
5626 /* The name was matched, now expand corresponding CUs that were
5627 marked. */
5628 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5629
5630 struct dwarf2_per_cu_data *per_cu;
5631 while ((per_cu = iter.next ()) != NULL)
5632 dw2_expand_symtabs_matching_one (per_cu, nullptr, nullptr);
5633 return true;
5634 });
5635
5636 /* It's a shame we couldn't do this inside the
5637 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5638 that have already been expanded. Instead, this loop matches what
5639 the psymtab code does. */
5640 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5641 {
5642 struct compunit_symtab *cust = per_cu->v.quick->compunit_symtab;
5643 if (cust != nullptr)
5644 {
5645 const struct block *block
5646 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
5647 if (!iterate_over_symbols_terminated (block, name,
5648 domain, callback))
5649 break;
5650 }
5651 }
5652}
5653
927aa2e7
JK
5654static void
5655dw2_debug_names_expand_symtabs_matching
5656 (struct objfile *objfile,
5657 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5658 const lookup_name_info *lookup_name,
927aa2e7
JK
5659 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5660 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5661 enum search_domain kind)
5662{
ed2dc618
SM
5663 struct dwarf2_per_objfile *dwarf2_per_objfile
5664 = get_dwarf2_per_objfile (objfile);
9291a0cd 5665
927aa2e7
JK
5666 /* debug_names_table is NULL if OBJF_READNOW. */
5667 if (!dwarf2_per_objfile->debug_names_table)
5668 return;
9291a0cd 5669
ed2dc618 5670 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5671
c1a66c06
TV
5672 if (symbol_matcher == NULL && lookup_name == NULL)
5673 {
5674 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
5675 {
5676 QUIT;
5677
5678 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5679 expansion_notify);
5680 }
5681 return;
5682 }
5683
44ed8f3e 5684 mapped_debug_names &map = *dwarf2_per_objfile->debug_names_table;
bbf2f4df 5685
c1a66c06 5686 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5687 symbol_matcher,
5688 kind, [&] (offset_type namei)
927aa2e7 5689 {
927aa2e7
JK
5690 /* The name was matched, now expand corresponding CUs that were
5691 marked. */
5692 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5693
927aa2e7
JK
5694 struct dwarf2_per_cu_data *per_cu;
5695 while ((per_cu = iter.next ()) != NULL)
5696 dw2_expand_symtabs_matching_one (per_cu, file_matcher,
5697 expansion_notify);
3b00ef10 5698 return true;
44ed8f3e 5699 });
9291a0cd
TT
5700}
5701
927aa2e7 5702const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5703{
5704 dw2_has_symbols,
5705 dw2_find_last_source_symtab,
5706 dw2_forget_cached_source_info,
f8eba3c6 5707 dw2_map_symtabs_matching_filename,
927aa2e7 5708 dw2_debug_names_lookup_symbol,
d3214198 5709 NULL,
9291a0cd 5710 dw2_print_stats,
927aa2e7 5711 dw2_debug_names_dump,
927aa2e7 5712 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5713 dw2_expand_all_symtabs,
652a8996 5714 dw2_expand_symtabs_with_fullname,
3b00ef10 5715 dw2_debug_names_map_matching_symbols,
927aa2e7 5716 dw2_debug_names_expand_symtabs_matching,
43f3e411 5717 dw2_find_pc_sect_compunit_symtab,
71a3c369 5718 NULL,
9291a0cd
TT
5719 dw2_map_symbol_filenames
5720};
5721
4485a1c1
SM
5722/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5723 to either a dwarf2_per_objfile or dwz_file object. */
5724
5725template <typename T>
5726static gdb::array_view<const gdb_byte>
5727get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5728{
5729 dwarf2_section_info *section = &section_owner->gdb_index;
5730
96b79293 5731 if (section->empty ())
4485a1c1
SM
5732 return {};
5733
5734 /* Older elfutils strip versions could keep the section in the main
5735 executable while splitting it for the separate debug info file. */
96b79293 5736 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5737 return {};
5738
96b79293 5739 section->read (obj);
4485a1c1 5740
8bebfcda
PA
5741 /* dwarf2_section_info::size is a bfd_size_type, while
5742 gdb::array_view works with size_t. On 32-bit hosts, with
5743 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5744 is 32-bit. So we need an explicit narrowing conversion here.
5745 This is fine, because it's impossible to allocate or mmap an
5746 array/buffer larger than what size_t can represent. */
5747 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5748}
5749
87d6a7aa
SM
5750/* Lookup the index cache for the contents of the index associated to
5751 DWARF2_OBJ. */
5752
5753static gdb::array_view<const gdb_byte>
5754get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_objfile *dwarf2_obj)
5755{
5756 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5757 if (build_id == nullptr)
5758 return {};
5759
5760 return global_index_cache.lookup_gdb_index (build_id,
5761 &dwarf2_obj->index_cache_res);
5762}
5763
5764/* Same as the above, but for DWZ. */
5765
5766static gdb::array_view<const gdb_byte>
5767get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5768{
5769 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5770 if (build_id == nullptr)
5771 return {};
5772
5773 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5774}
5775
3c0aa29a 5776/* See symfile.h. */
9291a0cd 5777
3c0aa29a
PA
5778bool
5779dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5780{
ed2dc618
SM
5781 struct dwarf2_per_objfile *dwarf2_per_objfile
5782 = get_dwarf2_per_objfile (objfile);
5783
9291a0cd
TT
5784 /* If we're about to read full symbols, don't bother with the
5785 indices. In this case we also don't care if some other debug
5786 format is making psymtabs, because they are all about to be
5787 expanded anyway. */
5788 if ((objfile->flags & OBJF_READNOW))
5789 {
9291a0cd 5790 dwarf2_per_objfile->using_index = 1;
ed2dc618
SM
5791 create_all_comp_units (dwarf2_per_objfile);
5792 create_all_type_units (dwarf2_per_objfile);
b76e467d
SM
5793 dwarf2_per_objfile->quick_file_names_table
5794 = create_quick_file_names_table
5795 (dwarf2_per_objfile->all_comp_units.size ());
9291a0cd 5796
b76e467d 5797 for (int i = 0; i < (dwarf2_per_objfile->all_comp_units.size ()
b2bdb8cf 5798 + dwarf2_per_objfile->all_type_units.size ()); ++i)
9291a0cd 5799 {
ff4c9fec 5800 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->get_cutu (i);
9291a0cd 5801
e254ef6a
DE
5802 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5803 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5804 }
5805
5806 /* Return 1 so that gdb sees the "quick" functions. However,
5807 these functions will be no-ops because we will have expanded
5808 all symtabs. */
3c0aa29a
PA
5809 *index_kind = dw_index_kind::GDB_INDEX;
5810 return true;
9291a0cd
TT
5811 }
5812
ed2dc618 5813 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5814 {
5815 *index_kind = dw_index_kind::DEBUG_NAMES;
5816 return true;
5817 }
927aa2e7 5818
4485a1c1
SM
5819 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5820 get_gdb_index_contents_from_section<struct dwarf2_per_objfile>,
5821 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5822 {
5823 *index_kind = dw_index_kind::GDB_INDEX;
5824 return true;
5825 }
9291a0cd 5826
87d6a7aa
SM
5827 /* ... otherwise, try to find the index in the index cache. */
5828 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5829 get_gdb_index_contents_from_cache,
5830 get_gdb_index_contents_from_cache_dwz))
5831 {
5832 global_index_cache.hit ();
5833 *index_kind = dw_index_kind::GDB_INDEX;
5834 return true;
5835 }
5836
5837 global_index_cache.miss ();
3c0aa29a 5838 return false;
9291a0cd
TT
5839}
5840
5841\f
5842
dce234bc
PP
5843/* Build a partial symbol table. */
5844
5845void
f29dff0a 5846dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5847{
ed2dc618
SM
5848 struct dwarf2_per_objfile *dwarf2_per_objfile
5849 = get_dwarf2_per_objfile (objfile);
c9bf0622 5850
6eee24ce 5851 init_psymbol_list (objfile, 1024);
c906108c 5852
a70b8144 5853 try
c9bf0622
TT
5854 {
5855 /* This isn't really ideal: all the data we allocate on the
5856 objfile's obstack is still uselessly kept around. However,
5857 freeing it seems unsafe. */
906768f9 5858 psymtab_discarder psymtabs (objfile);
ed2dc618 5859 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5860 psymtabs.keep ();
87d6a7aa
SM
5861
5862 /* (maybe) store an index in the cache. */
5863 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5864 }
230d2906 5865 catch (const gdb_exception_error &except)
492d29ea
PA
5866 {
5867 exception_print (gdb_stderr, except);
5868 }
c906108c 5869}
c906108c 5870
3b80fe9b
DE
5871/* Find the base address of the compilation unit for range lists and
5872 location lists. It will normally be specified by DW_AT_low_pc.
5873 In DWARF-3 draft 4, the base address could be overridden by
5874 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5875 compilation units with discontinuous ranges. */
5876
5877static void
5878dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5879{
5880 struct attribute *attr;
5881
2b24b6e4 5882 cu->base_address.reset ();
3b80fe9b
DE
5883
5884 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 5885 if (attr != nullptr)
2b24b6e4 5886 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5887 else
5888 {
5889 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 5890 if (attr != nullptr)
2b24b6e4 5891 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
5892 }
5893}
5894
36586728
TT
5895/* Helper function that returns the proper abbrev section for
5896 THIS_CU. */
5897
5898static struct dwarf2_section_info *
5899get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
5900{
5901 struct dwarf2_section_info *abbrev;
ed2dc618 5902 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
36586728
TT
5903
5904 if (this_cu->is_dwz)
ed2dc618 5905 abbrev = &dwarf2_get_dwz_file (dwarf2_per_objfile)->abbrev;
36586728
TT
5906 else
5907 abbrev = &dwarf2_per_objfile->abbrev;
5908
5909 return abbrev;
5910}
5911
f4dc4d17
DE
5912/* Fetch the abbreviation table offset from a comp or type unit header. */
5913
5914static sect_offset
ed2dc618
SM
5915read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
5916 struct dwarf2_section_info *section,
9c541725 5917 sect_offset sect_off)
f4dc4d17 5918{
96b79293 5919 bfd *abfd = section->get_bfd_owner ();
d521ce57 5920 const gdb_byte *info_ptr;
ac298888 5921 unsigned int initial_length_size, offset_size;
43988095 5922 uint16_t version;
f4dc4d17 5923
96b79293 5924 section->read (dwarf2_per_objfile->objfile);
9c541725 5925 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 5926 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 5927 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
5928 info_ptr += initial_length_size;
5929
5930 version = read_2_bytes (abfd, info_ptr);
5931 info_ptr += 2;
5932 if (version >= 5)
5933 {
5934 /* Skip unit type and address size. */
5935 info_ptr += 2;
5936 }
5937
24aa364d 5938 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
5939}
5940
b83470bf
TT
5941/* A partial symtab that is used only for include files. */
5942struct dwarf2_include_psymtab : public partial_symtab
5943{
5944 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
5945 : partial_symtab (filename, objfile)
5946 {
5947 }
5948
5949 void read_symtab (struct objfile *objfile) override
5950 {
194d088f
TV
5951 /* It's an include file, no symbols to read for it.
5952 Everything is in the includer symtab. */
5953
5954 /* The expansion of a dwarf2_include_psymtab is just a trigger for
5955 expansion of the includer psymtab. We use the dependencies[0] field to
5956 model the includer. But if we go the regular route of calling
5957 expand_psymtab here, and having expand_psymtab call expand_dependencies
5958 to expand the includer, we'll only use expand_psymtab on the includer
5959 (making it a non-toplevel psymtab), while if we expand the includer via
5960 another path, we'll use read_symtab (making it a toplevel psymtab).
5961 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
5962 psymtab, and trigger read_symtab on the includer here directly. */
5963 includer ()->read_symtab (objfile);
b83470bf
TT
5964 }
5965
5966 void expand_psymtab (struct objfile *objfile) override
5967 {
194d088f
TV
5968 /* This is not called by read_symtab, and should not be called by any
5969 expand_dependencies. */
5970 gdb_assert (false);
b83470bf
TT
5971 }
5972
5973 bool readin_p () const override
5974 {
194d088f 5975 return includer ()->readin_p ();
b83470bf
TT
5976 }
5977
5978 struct compunit_symtab *get_compunit_symtab () const override
5979 {
5980 return nullptr;
5981 }
5982
5983private:
194d088f
TV
5984 partial_symtab *includer () const
5985 {
5986 /* An include psymtab has exactly one dependency: the psymtab that
5987 includes it. */
5988 gdb_assert (this->number_of_dependencies == 1);
5989 return this->dependencies[0];
5990 }
b83470bf
TT
5991};
5992
aaa75496
JB
5993/* Allocate a new partial symtab for file named NAME and mark this new
5994 partial symtab as being an include of PST. */
5995
5996static void
891813be 5997dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
5998 struct objfile *objfile)
5999{
b83470bf 6000 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6001
fbd9ab74
JK
6002 if (!IS_ABSOLUTE_PATH (subpst->filename))
6003 {
6004 /* It shares objfile->objfile_obstack. */
6005 subpst->dirname = pst->dirname;
6006 }
6007
a9342b62 6008 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6009 subpst->dependencies[0] = pst;
6010 subpst->number_of_dependencies = 1;
aaa75496
JB
6011}
6012
6013/* Read the Line Number Program data and extract the list of files
6014 included by the source file represented by PST. Build an include
d85a05f0 6015 partial symtab for each of these included files. */
aaa75496
JB
6016
6017static void
6018dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6019 struct die_info *die,
891813be 6020 dwarf2_psymtab *pst)
aaa75496 6021{
fff8551c 6022 line_header_up lh;
d85a05f0 6023 struct attribute *attr;
aaa75496 6024
d85a05f0 6025 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6026 if (attr != nullptr)
9c541725 6027 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6028 if (lh == NULL)
6029 return; /* No linetable, so no includes. */
6030
79748972
TT
6031 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6032 that we pass in the raw text_low here; that is ok because we're
6033 only decoding the line table to make include partial symtabs, and
6034 so the addresses aren't really used. */
4ae976d1 6035 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6036 pst->raw_text_low (), 1);
aaa75496
JB
6037}
6038
348e048f 6039static hashval_t
52dc124a 6040hash_signatured_type (const void *item)
348e048f 6041{
9a3c8263
SM
6042 const struct signatured_type *sig_type
6043 = (const struct signatured_type *) item;
9a619af0 6044
348e048f 6045 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6046 return sig_type->signature;
348e048f
DE
6047}
6048
6049static int
52dc124a 6050eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6051{
9a3c8263
SM
6052 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6053 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6054
348e048f
DE
6055 return lhs->signature == rhs->signature;
6056}
6057
1fd400ff
TT
6058/* Allocate a hash table for signatured types. */
6059
b0b6a987 6060static htab_up
298e9637 6061allocate_signatured_type_table ()
1fd400ff 6062{
b0b6a987
TT
6063 return htab_up (htab_create_alloc (41,
6064 hash_signatured_type,
6065 eq_signatured_type,
6066 NULL, xcalloc, xfree));
1fd400ff
TT
6067}
6068
d467dd73 6069/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6070
6071static int
d467dd73 6072add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6073{
9a3c8263 6074 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6075 std::vector<signatured_type *> *all_type_units
6076 = (std::vector<signatured_type *> *) datum;
1fd400ff 6077
b2bdb8cf 6078 all_type_units->push_back (sigt);
1fd400ff
TT
6079
6080 return 1;
6081}
6082
78d4d2c5 6083/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6084 and fill them into TYPES_HTAB. It will process only type units,
6085 therefore DW_UT_type. */
c88ee1f0 6086
78d4d2c5 6087static void
ed2dc618
SM
6088create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6089 struct dwo_file *dwo_file,
b0b6a987 6090 dwarf2_section_info *section, htab_up &types_htab,
43988095 6091 rcuh_kind section_kind)
348e048f 6092{
3019eac3 6093 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6094 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6095 bfd *abfd;
6096 const gdb_byte *info_ptr, *end_ptr;
348e048f 6097
4bdcc0c1
DE
6098 abbrev_section = (dwo_file != NULL
6099 ? &dwo_file->sections.abbrev
6100 : &dwarf2_per_objfile->abbrev);
6101
b4f54984 6102 if (dwarf_read_debug)
43988095 6103 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6104 section->get_name (),
6105 abbrev_section->get_file_name ());
09406207 6106
96b79293 6107 section->read (objfile);
78d4d2c5 6108 info_ptr = section->buffer;
348e048f 6109
78d4d2c5
JK
6110 if (info_ptr == NULL)
6111 return;
348e048f 6112
78d4d2c5
JK
6113 /* We can't set abfd until now because the section may be empty or
6114 not present, in which case the bfd is unknown. */
96b79293 6115 abfd = section->get_bfd_owner ();
348e048f 6116
c0ab21c2
TT
6117 /* We don't use cutu_reader here because we don't need to read
6118 any dies: the signature is in the header. */
3019eac3 6119
78d4d2c5
JK
6120 end_ptr = info_ptr + section->size;
6121 while (info_ptr < end_ptr)
6122 {
78d4d2c5
JK
6123 struct signatured_type *sig_type;
6124 struct dwo_unit *dwo_tu;
6125 void **slot;
6126 const gdb_byte *ptr = info_ptr;
6127 struct comp_unit_head header;
6128 unsigned int length;
8b70b953 6129
9c541725 6130 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6131
a49dd8dd
JK
6132 /* Initialize it due to a false compiler warning. */
6133 header.signature = -1;
9c541725 6134 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6135
78d4d2c5
JK
6136 /* We need to read the type's signature in order to build the hash
6137 table, but we don't need anything else just yet. */
348e048f 6138
ed2dc618 6139 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6140 abbrev_section, ptr, section_kind);
348e048f 6141
4057dfde 6142 length = header.get_length ();
6caca83c 6143
78d4d2c5
JK
6144 /* Skip dummy type units. */
6145 if (ptr >= info_ptr + length
43988095
JK
6146 || peek_abbrev_code (abfd, ptr) == 0
6147 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6148 {
6149 info_ptr += length;
6150 continue;
6151 }
dee91e82 6152
78d4d2c5
JK
6153 if (types_htab == NULL)
6154 {
6155 if (dwo_file)
298e9637 6156 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6157 else
298e9637 6158 types_htab = allocate_signatured_type_table ();
78d4d2c5 6159 }
8b70b953 6160
78d4d2c5
JK
6161 if (dwo_file)
6162 {
6163 sig_type = NULL;
6164 dwo_tu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6165 struct dwo_unit);
6166 dwo_tu->dwo_file = dwo_file;
43988095 6167 dwo_tu->signature = header.signature;
9c541725 6168 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6169 dwo_tu->section = section;
9c541725 6170 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6171 dwo_tu->length = length;
6172 }
6173 else
6174 {
6175 /* N.B.: type_offset is not usable if this type uses a DWO file.
6176 The real type_offset is in the DWO file. */
6177 dwo_tu = NULL;
6178 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6179 struct signatured_type);
43988095 6180 sig_type->signature = header.signature;
9c541725 6181 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
e3b94546 6182 sig_type->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
78d4d2c5
JK
6183 sig_type->per_cu.is_debug_types = 1;
6184 sig_type->per_cu.section = section;
9c541725 6185 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6186 sig_type->per_cu.length = length;
6187 }
6188
b0b6a987 6189 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6190 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6191 INSERT);
6192 gdb_assert (slot != NULL);
6193 if (*slot != NULL)
6194 {
9c541725 6195 sect_offset dup_sect_off;
0349ea22 6196
3019eac3
DE
6197 if (dwo_file)
6198 {
78d4d2c5
JK
6199 const struct dwo_unit *dup_tu
6200 = (const struct dwo_unit *) *slot;
6201
9c541725 6202 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6203 }
6204 else
6205 {
78d4d2c5
JK
6206 const struct signatured_type *dup_tu
6207 = (const struct signatured_type *) *slot;
6208
9c541725 6209 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6210 }
8b70b953 6211
b98664d3 6212 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6213 " the entry at offset %s, signature %s"),
6214 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6215 hex_string (header.signature));
78d4d2c5
JK
6216 }
6217 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6218
78d4d2c5 6219 if (dwarf_read_debug > 1)
9d8780f0
SM
6220 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6221 sect_offset_str (sect_off),
43988095 6222 hex_string (header.signature));
3019eac3 6223
78d4d2c5
JK
6224 info_ptr += length;
6225 }
6226}
3019eac3 6227
78d4d2c5
JK
6228/* Create the hash table of all entries in the .debug_types
6229 (or .debug_types.dwo) section(s).
6230 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6231 otherwise it is NULL.
b3c8eb43 6232
78d4d2c5 6233 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6234
78d4d2c5 6235 Note: This function processes DWO files only, not DWP files. */
348e048f 6236
78d4d2c5 6237static void
ed2dc618
SM
6238create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6239 struct dwo_file *dwo_file,
fd5866f6 6240 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6241 htab_up &types_htab)
78d4d2c5 6242{
fd5866f6
SM
6243 for (dwarf2_section_info &section : type_sections)
6244 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6245 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6246}
6247
6248/* Create the hash table of all entries in the .debug_types section,
6249 and initialize all_type_units.
6250 The result is zero if there is an error (e.g. missing .debug_types section),
6251 otherwise non-zero. */
6252
6253static int
ed2dc618 6254create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6255{
b0b6a987 6256 htab_up types_htab;
3019eac3 6257
ed2dc618
SM
6258 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
6259 &dwarf2_per_objfile->info, types_htab,
43988095 6260 rcuh_kind::COMPILE);
ed2dc618
SM
6261 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
6262 dwarf2_per_objfile->types, types_htab);
3019eac3
DE
6263 if (types_htab == NULL)
6264 {
6265 dwarf2_per_objfile->signatured_types = NULL;
6266 return 0;
6267 }
6268
b0b6a987 6269 dwarf2_per_objfile->signatured_types = std::move (types_htab);
348e048f 6270
b2bdb8cf 6271 gdb_assert (dwarf2_per_objfile->all_type_units.empty ());
b0b6a987
TT
6272 dwarf2_per_objfile->all_type_units.reserve
6273 (htab_elements (dwarf2_per_objfile->signatured_types.get ()));
b2bdb8cf 6274
b0b6a987
TT
6275 htab_traverse_noresize (dwarf2_per_objfile->signatured_types.get (),
6276 add_signatured_type_cu_to_table,
b2bdb8cf 6277 &dwarf2_per_objfile->all_type_units);
1fd400ff 6278
348e048f
DE
6279 return 1;
6280}
6281
6aa5f3a6
DE
6282/* Add an entry for signature SIG to dwarf2_per_objfile->signatured_types.
6283 If SLOT is non-NULL, it is the entry to use in the hash table.
6284 Otherwise we find one. */
6285
6286static struct signatured_type *
ed2dc618
SM
6287add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6288 void **slot)
6aa5f3a6
DE
6289{
6290 struct objfile *objfile = dwarf2_per_objfile->objfile;
6aa5f3a6 6291
b2bdb8cf
SM
6292 if (dwarf2_per_objfile->all_type_units.size ()
6293 == dwarf2_per_objfile->all_type_units.capacity ())
6294 ++dwarf2_per_objfile->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6295
b2bdb8cf
SM
6296 signatured_type *sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6297 struct signatured_type);
6298
6299 dwarf2_per_objfile->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6300 sig_type->signature = sig;
6301 sig_type->per_cu.is_debug_types = 1;
6302 if (dwarf2_per_objfile->using_index)
6303 {
6304 sig_type->per_cu.v.quick =
6305 OBSTACK_ZALLOC (&objfile->objfile_obstack,
6306 struct dwarf2_per_cu_quick_data);
6307 }
6308
6309 if (slot == NULL)
6310 {
b0b6a987 6311 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6
DE
6312 sig_type, INSERT);
6313 }
6314 gdb_assert (*slot == NULL);
6315 *slot = sig_type;
6316 /* The rest of sig_type must be filled in by the caller. */
6317 return sig_type;
6318}
6319
a2ce51a0
DE
6320/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6321 Fill in SIG_ENTRY with DWO_ENTRY. */
6322
6323static void
ed2dc618 6324fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6325 struct signatured_type *sig_entry,
6326 struct dwo_unit *dwo_entry)
6327{
7ee85ab1 6328 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0
DE
6329 gdb_assert (! sig_entry->per_cu.queued);
6330 gdb_assert (sig_entry->per_cu.cu == NULL);
6aa5f3a6
DE
6331 if (dwarf2_per_objfile->using_index)
6332 {
6333 gdb_assert (sig_entry->per_cu.v.quick != NULL);
43f3e411 6334 gdb_assert (sig_entry->per_cu.v.quick->compunit_symtab == NULL);
6aa5f3a6
DE
6335 }
6336 else
6337 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6338 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6339 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6340 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6341 gdb_assert (sig_entry->dwo_unit == NULL);
6342
6343 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6344 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6345 sig_entry->per_cu.length = dwo_entry->length;
6346 sig_entry->per_cu.reading_dwo_directly = 1;
e3b94546 6347 sig_entry->per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
a2ce51a0
DE
6348 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6349 sig_entry->dwo_unit = dwo_entry;
6350}
6351
6352/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6353 If we haven't read the TU yet, create the signatured_type data structure
6354 for a TU to be read in directly from a DWO file, bypassing the stub.
6355 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6356 using .gdb_index, then when reading a CU we want to stay in the DWO file
6357 containing that CU. Otherwise we could end up reading several other DWO
6358 files (due to comdat folding) to process the transitive closure of all the
6359 mentioned TUs, and that can be slow. The current DWO file will have every
6360 type signature that it needs.
a2ce51a0
DE
6361 We only do this for .gdb_index because in the psymtab case we already have
6362 to read all the DWOs to build the type unit groups. */
6363
6364static struct signatured_type *
6365lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6366{
518817b3
SM
6367 struct dwarf2_per_objfile *dwarf2_per_objfile
6368 = cu->per_cu->dwarf2_per_objfile;
a2ce51a0
DE
6369 struct dwo_file *dwo_file;
6370 struct dwo_unit find_dwo_entry, *dwo_entry;
6371 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6372 void **slot;
a2ce51a0
DE
6373
6374 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6375
6aa5f3a6
DE
6376 /* If TU skeletons have been removed then we may not have read in any
6377 TUs yet. */
6378 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6379 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6380
6381 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6382 Use the global signatured_types array to do our own comdat-folding
6383 of types. If this is the first time we're reading this TU, and
6384 the TU has an entry in .gdb_index, replace the recorded data from
6385 .gdb_index with this TU. */
a2ce51a0 6386
a2ce51a0 6387 find_sig_entry.signature = sig;
b0b6a987 6388 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6389 &find_sig_entry, INSERT);
9a3c8263 6390 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6391
6392 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6393 read. Don't reassign the global entry to point to this DWO if that's
6394 the case. Also note that if the TU is already being read, it may not
6395 have come from a DWO, the program may be a mix of Fission-compiled
6396 code and non-Fission-compiled code. */
6397
6398 /* Have we already tried to read this TU?
6399 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6400 needn't exist in the global table yet). */
6401 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6402 return sig_entry;
6403
6aa5f3a6
DE
6404 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6405 dwo_unit of the TU itself. */
6406 dwo_file = cu->dwo_unit->dwo_file;
6407
a2ce51a0
DE
6408 /* Ok, this is the first time we're reading this TU. */
6409 if (dwo_file->tus == NULL)
6410 return NULL;
6411 find_dwo_entry.signature = sig;
b0b6a987
TT
6412 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6413 &find_dwo_entry);
a2ce51a0
DE
6414 if (dwo_entry == NULL)
6415 return NULL;
6416
6aa5f3a6
DE
6417 /* If the global table doesn't have an entry for this TU, add one. */
6418 if (sig_entry == NULL)
ed2dc618 6419 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6420
ed2dc618 6421 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6422 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6423 return sig_entry;
6424}
6425
a2ce51a0
DE
6426/* Subroutine of lookup_signatured_type.
6427 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6428 then try the DWP file. If the TU stub (skeleton) has been removed then
6429 it won't be in .gdb_index. */
a2ce51a0
DE
6430
6431static struct signatured_type *
6432lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6433{
518817b3
SM
6434 struct dwarf2_per_objfile *dwarf2_per_objfile
6435 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6436 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6437 struct dwo_unit *dwo_entry;
6438 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6439 void **slot;
a2ce51a0
DE
6440
6441 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->using_index);
6442 gdb_assert (dwp_file != NULL);
6443
6aa5f3a6
DE
6444 /* If TU skeletons have been removed then we may not have read in any
6445 TUs yet. */
6446 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 6447 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6448
6aa5f3a6 6449 find_sig_entry.signature = sig;
b0b6a987 6450 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
6aa5f3a6 6451 &find_sig_entry, INSERT);
9a3c8263 6452 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6453
6454 /* Have we already tried to read this TU?
6455 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6456 needn't exist in the global table yet). */
6457 if (sig_entry != NULL)
6458 return sig_entry;
6459
a2ce51a0
DE
6460 if (dwp_file->tus == NULL)
6461 return NULL;
ed2dc618 6462 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6463 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6464 if (dwo_entry == NULL)
6465 return NULL;
6466
ed2dc618
SM
6467 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6468 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6469
a2ce51a0
DE
6470 return sig_entry;
6471}
6472
380bca97 6473/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6474 Returns NULL if signature SIG is not present in the table.
6475 It is up to the caller to complain about this. */
348e048f
DE
6476
6477static struct signatured_type *
a2ce51a0 6478lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6479{
518817b3
SM
6480 struct dwarf2_per_objfile *dwarf2_per_objfile
6481 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 6482
a2ce51a0
DE
6483 if (cu->dwo_unit
6484 && dwarf2_per_objfile->using_index)
6485 {
6486 /* We're in a DWO/DWP file, and we're using .gdb_index.
6487 These cases require special processing. */
ed2dc618 6488 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6489 return lookup_dwo_signatured_type (cu, sig);
6490 else
6491 return lookup_dwp_signatured_type (cu, sig);
6492 }
6493 else
6494 {
6495 struct signatured_type find_entry, *entry;
348e048f 6496
a2ce51a0
DE
6497 if (dwarf2_per_objfile->signatured_types == NULL)
6498 return NULL;
6499 find_entry.signature = sig;
9a3c8263 6500 entry = ((struct signatured_type *)
b0b6a987
TT
6501 htab_find (dwarf2_per_objfile->signatured_types.get (),
6502 &find_entry));
a2ce51a0
DE
6503 return entry;
6504 }
348e048f 6505}
18a8505e 6506
42e7ad6c 6507/* Low level DIE reading support. */
348e048f 6508
d85a05f0
DJ
6509/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6510
6511static void
6512init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6513 struct dwarf2_cu *cu,
3019eac3 6514 struct dwarf2_section_info *section,
685af9cd
TT
6515 struct dwo_file *dwo_file,
6516 struct abbrev_table *abbrev_table)
d85a05f0 6517{
fceca515 6518 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6519 reader->abfd = section->get_bfd_owner ();
d85a05f0 6520 reader->cu = cu;
3019eac3 6521 reader->dwo_file = dwo_file;
dee91e82
DE
6522 reader->die_section = section;
6523 reader->buffer = section->buffer;
f664829e 6524 reader->buffer_end = section->buffer + section->size;
685af9cd 6525 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6526}
6527
c0ab21c2 6528/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6529 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6530 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6531 already.
6532
6533 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6534 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6535 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6536 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6537 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6538 STUB_COMP_DIR may be non-NULL.
3e225074 6539 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6540 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6541 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6542 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6543 kept around for at least as long as *RESULT_READER.
6544
b0c7bfa9
DE
6545 The result is non-zero if a valid (non-dummy) DIE was found. */
6546
6547static int
6548read_cutu_die_from_dwo (struct dwarf2_per_cu_data *this_cu,
6549 struct dwo_unit *dwo_unit,
b0c7bfa9 6550 struct die_info *stub_comp_unit_die,
a2ce51a0 6551 const char *stub_comp_dir,
b0c7bfa9 6552 struct die_reader_specs *result_reader,
d521ce57 6553 const gdb_byte **result_info_ptr,
b0c7bfa9 6554 struct die_info **result_comp_unit_die,
685af9cd 6555 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6556{
ed2dc618 6557 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
b0c7bfa9
DE
6558 struct objfile *objfile = dwarf2_per_objfile->objfile;
6559 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6560 bfd *abfd;
d521ce57 6561 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6562 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6563 int i,num_extra_attrs;
6564 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6565 struct die_info *comp_unit_die;
6566
b0aeadb3
DE
6567 /* At most one of these may be provided. */
6568 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6569
b0c7bfa9
DE
6570 /* These attributes aren't processed until later:
6571 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6572 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6573 referenced later. However, these attributes are found in the stub
6574 which we won't have later. In order to not impose this complication
6575 on the rest of the code, we read them here and copy them to the
6576 DWO CU/TU die. */
b0c7bfa9
DE
6577
6578 stmt_list = NULL;
6579 low_pc = NULL;
6580 high_pc = NULL;
6581 ranges = NULL;
6582 comp_dir = NULL;
6583
6584 if (stub_comp_unit_die != NULL)
6585 {
6586 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6587 DWO file. */
6588 if (! this_cu->is_debug_types)
6589 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6590 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6591 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6592 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6593 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6594
a39fdb41 6595 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6596
18a8505e
AT
6597 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6598 here (if needed). We need the value before we can process
6599 DW_AT_ranges. */
a39fdb41 6600 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6601 }
a2ce51a0
DE
6602 else if (stub_comp_dir != NULL)
6603 {
6604 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6605 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6606 comp_dir->name = DW_AT_comp_dir;
6607 comp_dir->form = DW_FORM_string;
6608 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6609 DW_STRING (comp_dir) = stub_comp_dir;
6610 }
b0c7bfa9
DE
6611
6612 /* Set up for reading the DWO CU/TU. */
6613 cu->dwo_unit = dwo_unit;
685af9cd 6614 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6615 section->read (objfile);
6616 abfd = section->get_bfd_owner ();
9c541725
PA
6617 begin_info_ptr = info_ptr = (section->buffer
6618 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6619 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9
DE
6620
6621 if (this_cu->is_debug_types)
6622 {
b0c7bfa9
DE
6623 struct signatured_type *sig_type = (struct signatured_type *) this_cu;
6624
ed2dc618
SM
6625 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6626 &cu->header, section,
b0c7bfa9 6627 dwo_abbrev_section,
43988095 6628 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6629 /* This is not an assert because it can be caused by bad debug info. */
43988095 6630 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6631 {
6632 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6633 " TU at offset %s [in module %s]"),
a2ce51a0 6634 hex_string (sig_type->signature),
43988095 6635 hex_string (cu->header.signature),
9d8780f0 6636 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6637 bfd_get_filename (abfd));
6638 }
9c541725 6639 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6640 /* For DWOs coming from DWP files, we don't know the CU length
6641 nor the type's offset in the TU until now. */
4057dfde 6642 dwo_unit->length = cu->header.get_length ();
9c541725 6643 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6644
6645 /* Establish the type offset that can be used to lookup the type.
6646 For DWO files, we don't know it until now. */
9c541725
PA
6647 sig_type->type_offset_in_section
6648 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6649 }
6650 else
6651 {
ed2dc618
SM
6652 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6653 &cu->header, section,
b0c7bfa9 6654 dwo_abbrev_section,
43988095 6655 info_ptr, rcuh_kind::COMPILE);
9c541725 6656 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6657 /* For DWOs coming from DWP files, we don't know the CU length
6658 until now. */
4057dfde 6659 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6660 }
6661
685af9cd 6662 *result_dwo_abbrev_table
86de1d91
TT
6663 = abbrev_table::read (objfile, dwo_abbrev_section,
6664 cu->header.abbrev_sect_off);
685af9cd
TT
6665 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6666 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6667
6668 /* Read in the die, but leave space to copy over the attributes
6669 from the stub. This has the benefit of simplifying the rest of
6670 the code - all the work to maintain the illusion of a single
6671 DW_TAG_{compile,type}_unit DIE is done here. */
6672 num_extra_attrs = ((stmt_list != NULL)
6673 + (low_pc != NULL)
6674 + (high_pc != NULL)
6675 + (ranges != NULL)
6676 + (comp_dir != NULL));
6677 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6678 num_extra_attrs);
b0c7bfa9
DE
6679
6680 /* Copy over the attributes from the stub to the DIE we just read in. */
6681 comp_unit_die = *result_comp_unit_die;
6682 i = comp_unit_die->num_attrs;
6683 if (stmt_list != NULL)
6684 comp_unit_die->attrs[i++] = *stmt_list;
6685 if (low_pc != NULL)
6686 comp_unit_die->attrs[i++] = *low_pc;
6687 if (high_pc != NULL)
6688 comp_unit_die->attrs[i++] = *high_pc;
6689 if (ranges != NULL)
6690 comp_unit_die->attrs[i++] = *ranges;
6691 if (comp_dir != NULL)
6692 comp_unit_die->attrs[i++] = *comp_dir;
6693 comp_unit_die->num_attrs += num_extra_attrs;
6694
b4f54984 6695 if (dwarf_die_debug)
bf6af496
DE
6696 {
6697 fprintf_unfiltered (gdb_stdlog,
6698 "Read die from %s@0x%x of %s:\n",
96b79293 6699 section->get_name (),
bf6af496
DE
6700 (unsigned) (begin_info_ptr - section->buffer),
6701 bfd_get_filename (abfd));
b4f54984 6702 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6703 }
6704
b0c7bfa9
DE
6705 /* Skip dummy compilation units. */
6706 if (info_ptr >= begin_info_ptr + dwo_unit->length
6707 || peek_abbrev_code (abfd, info_ptr) == 0)
6708 return 0;
6709
6710 *result_info_ptr = info_ptr;
6711 return 1;
6712}
6713
a084a2a6
AT
6714/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6715 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6716 signature is part of the header. */
6717static gdb::optional<ULONGEST>
6718lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6719{
6720 if (cu->header.version >= 5)
6721 return cu->header.signature;
6722 struct attribute *attr;
6723 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6724 if (attr == nullptr)
6725 return gdb::optional<ULONGEST> ();
6726 return DW_UNSND (attr);
6727}
6728
c0ab21c2 6729/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6730 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6731 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6732
6733static struct dwo_unit *
6734lookup_dwo_unit (struct dwarf2_per_cu_data *this_cu,
c0ab21c2
TT
6735 struct die_info *comp_unit_die,
6736 const char *dwo_name)
b0c7bfa9
DE
6737{
6738 struct dwarf2_cu *cu = this_cu->cu;
b0c7bfa9 6739 struct dwo_unit *dwo_unit;
c0ab21c2 6740 const char *comp_dir;
b0c7bfa9 6741
a2ce51a0
DE
6742 gdb_assert (cu != NULL);
6743
b0c7bfa9 6744 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6745 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6746 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9
DE
6747
6748 if (this_cu->is_debug_types)
6749 {
6750 struct signatured_type *sig_type;
6751
6752 /* Since this_cu is the first member of struct signatured_type,
6753 we can go from a pointer to one to a pointer to the other. */
6754 sig_type = (struct signatured_type *) this_cu;
b0c7bfa9
DE
6755 dwo_unit = lookup_dwo_type_unit (sig_type, dwo_name, comp_dir);
6756 }
6757 else
6758 {
a084a2a6
AT
6759 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
6760 if (!signature.has_value ())
b0c7bfa9
DE
6761 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6762 " [in module %s]"),
e3b94546 6763 dwo_name, objfile_name (this_cu->dwarf2_per_objfile->objfile));
b0c7bfa9 6764 dwo_unit = lookup_dwo_comp_unit (this_cu, dwo_name, comp_dir,
a084a2a6 6765 *signature);
b0c7bfa9
DE
6766 }
6767
b0c7bfa9
DE
6768 return dwo_unit;
6769}
6770
c0ab21c2 6771/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6772 See it for a description of the parameters.
fcd3b13d 6773 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6774
c0ab21c2
TT
6775void
6776cutu_reader::init_tu_and_read_dwo_dies (struct dwarf2_per_cu_data *this_cu,
6751ebae 6777 int use_existing_cu)
a2ce51a0 6778{
a2ce51a0 6779 struct signatured_type *sig_type;
a2ce51a0
DE
6780
6781 /* Verify we can do the following downcast, and that we have the
6782 data we need. */
6783 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6784 sig_type = (struct signatured_type *) this_cu;
6785 gdb_assert (sig_type->dwo_unit != NULL);
6786
6aa5f3a6
DE
6787 if (use_existing_cu && this_cu->cu != NULL)
6788 {
6789 gdb_assert (this_cu->cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6790 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6791 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6792 }
6793 else
6794 {
6795 /* If !use_existing_cu, this_cu->cu must be NULL. */
6796 gdb_assert (this_cu->cu == NULL);
c0ab21c2 6797 m_new_cu.reset (new dwarf2_cu (this_cu));
6aa5f3a6
DE
6798 }
6799
6800 /* A future optimization, if needed, would be to use an existing
6801 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6802 could share abbrev tables. */
a2ce51a0
DE
6803
6804 if (read_cutu_die_from_dwo (this_cu, sig_type->dwo_unit,
a2ce51a0
DE
6805 NULL /* stub_comp_unit_die */,
6806 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6807 this, &info_ptr,
3e225074 6808 &comp_unit_die,
c0ab21c2 6809 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6810 {
6811 /* Dummy die. */
c0ab21c2 6812 dummy_p = true;
a2ce51a0 6813 }
a2ce51a0
DE
6814}
6815
fd820528 6816/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6817 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6818
f4dc4d17
DE
6819 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6820 Otherwise the table specified in the comp unit header is read in and used.
6821 This is an optimization for when we already have the abbrev table.
6822
dee91e82 6823 If USE_EXISTING_CU is non-zero, and THIS_CU->cu is non-NULL, then use it.
6751ebae 6824 Otherwise, a new CU is allocated with xmalloc. */
aaa75496 6825
c0ab21c2
TT
6826cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
6827 struct abbrev_table *abbrev_table,
6751ebae 6828 int use_existing_cu,
c0ab21c2
TT
6829 bool skip_partial)
6830 : die_reader_specs {},
6751ebae 6831 m_this_cu (this_cu)
c906108c 6832{
ed2dc618 6833 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 6834 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6835 struct dwarf2_section_info *section = this_cu->section;
96b79293 6836 bfd *abfd = section->get_bfd_owner ();
dee91e82 6837 struct dwarf2_cu *cu;
c0ab21c2 6838 const gdb_byte *begin_info_ptr;
dee91e82 6839 struct signatured_type *sig_type = NULL;
4bdcc0c1 6840 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6841 /* Non-zero if CU currently points to a DWO file and we need to
6842 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6843 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6844 int rereading_dwo_cu = 0;
c906108c 6845
b4f54984 6846 if (dwarf_die_debug)
9d8780f0 6847 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6848 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6849 sect_offset_str (this_cu->sect_off));
09406207 6850
a2ce51a0
DE
6851 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6852 file (instead of going through the stub), short-circuit all of this. */
6853 if (this_cu->reading_dwo_directly)
6854 {
6855 /* Narrow down the scope of possibilities to have to understand. */
6856 gdb_assert (this_cu->is_debug_types);
6857 gdb_assert (abbrev_table == NULL);
6751ebae 6858 init_tu_and_read_dwo_dies (this_cu, use_existing_cu);
a2ce51a0
DE
6859 return;
6860 }
6861
dee91e82 6862 /* This is cheap if the section is already read in. */
96b79293 6863 section->read (objfile);
dee91e82 6864
9c541725 6865 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6866
6867 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82
DE
6868
6869 if (use_existing_cu && this_cu->cu != NULL)
6870 {
6871 cu = this_cu->cu;
42e7ad6c
DE
6872 /* If this CU is from a DWO file we need to start over, we need to
6873 refetch the attributes from the skeleton CU.
6874 This could be optimized by retrieving those attributes from when we
6875 were here the first time: the previous comp_unit_die was stored in
6876 comp_unit_obstack. But there's no data yet that we need this
6877 optimization. */
6878 if (cu->dwo_unit != NULL)
6879 rereading_dwo_cu = 1;
dee91e82
DE
6880 }
6881 else
6882 {
6883 /* If !use_existing_cu, this_cu->cu must be NULL. */
6884 gdb_assert (this_cu->cu == NULL);
c0ab21c2
TT
6885 m_new_cu.reset (new dwarf2_cu (this_cu));
6886 cu = m_new_cu.get ();
42e7ad6c 6887 }
dee91e82 6888
b0c7bfa9 6889 /* Get the header. */
9c541725 6890 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
6891 {
6892 /* We already have the header, there's no need to read it in again. */
9c541725 6893 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
6894 }
6895 else
6896 {
3019eac3 6897 if (this_cu->is_debug_types)
dee91e82 6898 {
ed2dc618
SM
6899 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6900 &cu->header, section,
4bdcc0c1 6901 abbrev_section, info_ptr,
43988095 6902 rcuh_kind::TYPE);
dee91e82 6903
42e7ad6c
DE
6904 /* Since per_cu is the first member of struct signatured_type,
6905 we can go from a pointer to one to a pointer to the other. */
6906 sig_type = (struct signatured_type *) this_cu;
43988095 6907 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
6908 gdb_assert (sig_type->type_offset_in_tu
6909 == cu->header.type_cu_offset_in_tu);
6910 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 6911
42e7ad6c
DE
6912 /* LENGTH has not been set yet for type units if we're
6913 using .gdb_index. */
4057dfde 6914 this_cu->length = cu->header.get_length ();
3019eac3
DE
6915
6916 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
6917 sig_type->type_offset_in_section =
6918 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
6919
6920 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6921 }
6922 else
6923 {
ed2dc618
SM
6924 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6925 &cu->header, section,
4bdcc0c1 6926 abbrev_section,
43988095
JK
6927 info_ptr,
6928 rcuh_kind::COMPILE);
dee91e82 6929
9c541725 6930 gdb_assert (this_cu->sect_off == cu->header.sect_off);
4057dfde 6931 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 6932 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
6933 }
6934 }
10b3939b 6935
6caca83c 6936 /* Skip dummy compilation units. */
dee91e82 6937 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 6938 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
6939 {
6940 dummy_p = true;
6941 return;
6942 }
6caca83c 6943
433df2d4
DE
6944 /* If we don't have them yet, read the abbrevs for this compilation unit.
6945 And if we need to read them now, make sure they're freed when we're
c0ab21c2 6946 done. */
f4dc4d17 6947 if (abbrev_table != NULL)
685af9cd
TT
6948 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
6949 else
f4dc4d17 6950 {
c0ab21c2 6951 m_abbrev_table_holder
86de1d91
TT
6952 = abbrev_table::read (objfile, abbrev_section,
6953 cu->header.abbrev_sect_off);
c0ab21c2 6954 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 6955 }
af703f96 6956
dee91e82 6957 /* Read the top level CU/TU die. */
c0ab21c2 6958 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 6959 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 6960
58f0c718 6961 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
6962 {
6963 dummy_p = true;
6964 return;
6965 }
58f0c718 6966
b0c7bfa9 6967 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
6968 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
6969 table from the DWO file and pass the ownership over to us. It will be
6970 referenced from READER, so we must make sure to free it after we're done
6971 with READER.
6972
b0c7bfa9
DE
6973 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
6974 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 6975 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 6976 if (dwo_name != nullptr)
3019eac3 6977 {
3019eac3 6978 struct dwo_unit *dwo_unit;
b0c7bfa9 6979 struct die_info *dwo_comp_unit_die;
3019eac3 6980
3e225074 6981 if (comp_unit_die->has_children)
6a506a2d 6982 {
b98664d3 6983 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
6984 " has children (offset %s) [in module %s]"),
6985 sect_offset_str (this_cu->sect_off),
6986 bfd_get_filename (abfd));
6a506a2d 6987 }
c0ab21c2 6988 dwo_unit = lookup_dwo_unit (this_cu, comp_unit_die, dwo_name);
6a506a2d 6989 if (dwo_unit != NULL)
3019eac3 6990 {
6a506a2d 6991 if (read_cutu_die_from_dwo (this_cu, dwo_unit,
a2ce51a0 6992 comp_unit_die, NULL,
c0ab21c2 6993 this, &info_ptr,
3e225074 6994 &dwo_comp_unit_die,
c0ab21c2 6995 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
6996 {
6997 /* Dummy die. */
c0ab21c2 6998 dummy_p = true;
6a506a2d
DE
6999 return;
7000 }
7001 comp_unit_die = dwo_comp_unit_die;
7002 }
7003 else
7004 {
7005 /* Yikes, we couldn't find the rest of the DIE, we only have
7006 the stub. A complaint has already been logged. There's
7007 not much more we can do except pass on the stub DIE to
7008 die_reader_func. We don't want to throw an error on bad
7009 debug info. */
3019eac3
DE
7010 }
7011 }
c0ab21c2 7012}
3019eac3 7013
6751ebae
TT
7014void
7015cutu_reader::keep ()
c0ab21c2 7016{
b0c7bfa9 7017 /* Done, clean up. */
6751ebae
TT
7018 gdb_assert (!dummy_p);
7019 if (m_new_cu != NULL)
348e048f 7020 {
c0ab21c2
TT
7021 struct dwarf2_per_objfile *dwarf2_per_objfile
7022 = m_this_cu->dwarf2_per_objfile;
fcd3b13d 7023 /* Link this CU into read_in_chain. */
c0ab21c2
TT
7024 m_this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
7025 dwarf2_per_objfile->read_in_chain = m_this_cu;
fcd3b13d 7026 /* The chain owns it now. */
c0ab21c2 7027 m_new_cu.release ();
348e048f 7028 }
dee91e82
DE
7029}
7030
18a8505e
AT
7031/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7032 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7033 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7034
7035 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7036 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7037
7038 We fill in THIS_CU->length.
7039
dee91e82 7040 THIS_CU->cu is always freed when done.
3019eac3 7041 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7042 to care whether it refers to the "main" CU or the DWO CU.
7043
7044 When parent_cu is passed, it is used to provide a default value for
7045 str_offsets_base and addr_base from the parent. */
dee91e82 7046
c0ab21c2
TT
7047cutu_reader::cutu_reader (struct dwarf2_per_cu_data *this_cu,
7048 struct dwarf2_cu *parent_cu,
7049 struct dwo_file *dwo_file)
7050 : die_reader_specs {},
7051 m_this_cu (this_cu)
dee91e82 7052{
ed2dc618 7053 struct dwarf2_per_objfile *dwarf2_per_objfile = this_cu->dwarf2_per_objfile;
dee91e82 7054 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7055 struct dwarf2_section_info *section = this_cu->section;
96b79293 7056 bfd *abfd = section->get_bfd_owner ();
33e80786 7057 struct dwarf2_section_info *abbrev_section;
d521ce57 7058 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7059
b4f54984 7060 if (dwarf_die_debug)
9d8780f0 7061 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7062 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7063 sect_offset_str (this_cu->sect_off));
09406207 7064
dee91e82
DE
7065 gdb_assert (this_cu->cu == NULL);
7066
33e80786
DE
7067 abbrev_section = (dwo_file != NULL
7068 ? &dwo_file->sections.abbrev
7069 : get_abbrev_section_for_cu (this_cu));
7070
dee91e82 7071 /* This is cheap if the section is already read in. */
96b79293 7072 section->read (objfile);
dee91e82 7073
c0ab21c2 7074 m_new_cu.reset (new dwarf2_cu (this_cu));
dee91e82 7075
9c541725 7076 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7077 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7078 &m_new_cu->header, section,
4bdcc0c1 7079 abbrev_section, info_ptr,
43988095
JK
7080 (this_cu->is_debug_types
7081 ? rcuh_kind::TYPE
7082 : rcuh_kind::COMPILE));
dee91e82 7083
18a8505e
AT
7084 if (parent_cu != nullptr)
7085 {
c0ab21c2
TT
7086 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7087 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7088 }
4057dfde 7089 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7090
7091 /* Skip dummy compilation units. */
7092 if (info_ptr >= begin_info_ptr + this_cu->length
7093 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7094 {
7095 dummy_p = true;
7096 return;
7097 }
72bf9492 7098
c0ab21c2 7099 m_abbrev_table_holder
86de1d91
TT
7100 = abbrev_table::read (objfile, abbrev_section,
7101 m_new_cu->header.abbrev_sect_off);
dee91e82 7102
c0ab21c2
TT
7103 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7104 m_abbrev_table_holder.get ());
3e225074 7105 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7106}
7107
0018ea6f
DE
7108\f
7109/* Type Unit Groups.
dee91e82 7110
0018ea6f
DE
7111 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7112 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7113 so that all types coming from the same compilation (.o file) are grouped
7114 together. A future step could be to put the types in the same symtab as
7115 the CU the types ultimately came from. */
ff013f42 7116
f4dc4d17
DE
7117static hashval_t
7118hash_type_unit_group (const void *item)
7119{
9a3c8263
SM
7120 const struct type_unit_group *tu_group
7121 = (const struct type_unit_group *) item;
f4dc4d17 7122
094b34ac 7123 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7124}
348e048f
DE
7125
7126static int
f4dc4d17 7127eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7128{
9a3c8263
SM
7129 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7130 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7131
094b34ac 7132 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7133}
348e048f 7134
f4dc4d17
DE
7135/* Allocate a hash table for type unit groups. */
7136
eaa5fa8b 7137static htab_up
298e9637 7138allocate_type_unit_groups_table ()
f4dc4d17 7139{
eaa5fa8b
TT
7140 return htab_up (htab_create_alloc (3,
7141 hash_type_unit_group,
7142 eq_type_unit_group,
7143 NULL, xcalloc, xfree));
f4dc4d17 7144}
dee91e82 7145
f4dc4d17
DE
7146/* Type units that don't have DW_AT_stmt_list are grouped into their own
7147 partial symtabs. We combine several TUs per psymtab to not let the size
7148 of any one psymtab grow too big. */
7149#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7150#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7151
094b34ac 7152/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7153 Create the type_unit_group object used to hold one or more TUs. */
7154
7155static struct type_unit_group *
094b34ac 7156create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7157{
518817b3
SM
7158 struct dwarf2_per_objfile *dwarf2_per_objfile
7159 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17 7160 struct objfile *objfile = dwarf2_per_objfile->objfile;
094b34ac 7161 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7162 struct type_unit_group *tu_group;
f4dc4d17
DE
7163
7164 tu_group = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7165 struct type_unit_group);
094b34ac 7166 per_cu = &tu_group->per_cu;
518817b3 7167 per_cu->dwarf2_per_objfile = dwarf2_per_objfile;
f4dc4d17 7168
094b34ac
DE
7169 if (dwarf2_per_objfile->using_index)
7170 {
7171 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
7172 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7173 }
7174 else
7175 {
9c541725 7176 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7177 dwarf2_psymtab *pst;
528e1572 7178 std::string name;
094b34ac
DE
7179
7180 /* Give the symtab a useful name for debug purposes. */
7181 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7182 name = string_printf ("<type_units_%d>",
7183 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7184 else
528e1572 7185 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7186
528e1572 7187 pst = create_partial_symtab (per_cu, name.c_str ());
6d94535f 7188 pst->anonymous = true;
094b34ac 7189 }
f4dc4d17 7190
094b34ac 7191 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7192 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7193
7194 return tu_group;
7195}
7196
094b34ac
DE
7197/* Look up the type_unit_group for type unit CU, and create it if necessary.
7198 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7199
7200static struct type_unit_group *
ff39bb5e 7201get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7202{
518817b3
SM
7203 struct dwarf2_per_objfile *dwarf2_per_objfile
7204 = cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7205 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7206 struct type_unit_group *tu_group;
7207 void **slot;
7208 unsigned int line_offset;
7209 struct type_unit_group type_unit_group_for_lookup;
7210
7211 if (dwarf2_per_objfile->type_unit_groups == NULL)
298e9637 7212 dwarf2_per_objfile->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7213
7214 /* Do we need to create a new group, or can we use an existing one? */
7215
7216 if (stmt_list)
7217 {
7218 line_offset = DW_UNSND (stmt_list);
7219 ++tu_stats->nr_symtab_sharers;
7220 }
7221 else
7222 {
7223 /* Ugh, no stmt_list. Rare, but we have to handle it.
7224 We can do various things here like create one group per TU or
7225 spread them over multiple groups to split up the expansion work.
7226 To avoid worst case scenarios (too many groups or too large groups)
7227 we, umm, group them in bunches. */
7228 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7229 | (tu_stats->nr_stmt_less_type_units
7230 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7231 ++tu_stats->nr_stmt_less_type_units;
7232 }
7233
094b34ac 7234 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7235 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
eaa5fa8b 7236 slot = htab_find_slot (dwarf2_per_objfile->type_unit_groups.get (),
f4dc4d17
DE
7237 &type_unit_group_for_lookup, INSERT);
7238 if (*slot != NULL)
7239 {
9a3c8263 7240 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7241 gdb_assert (tu_group != NULL);
7242 }
7243 else
7244 {
9c541725 7245 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7246 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7247 *slot = tu_group;
7248 ++tu_stats->nr_symtabs;
7249 }
7250
7251 return tu_group;
7252}
0018ea6f
DE
7253\f
7254/* Partial symbol tables. */
7255
7256/* Create a psymtab named NAME and assign it to PER_CU.
7257
7258 The caller must fill in the following details:
7259 dirname, textlow, texthigh. */
7260
891813be 7261static dwarf2_psymtab *
0018ea6f
DE
7262create_partial_symtab (struct dwarf2_per_cu_data *per_cu, const char *name)
7263{
e3b94546 7264 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
891813be 7265 dwarf2_psymtab *pst;
0018ea6f 7266
9f4e76a4 7267 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7268
6d94535f 7269 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7270
7271 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7272 per_cu->v.psymtab = pst;
7273
7274 return pst;
7275}
7276
c0ab21c2 7277/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7278
7279static void
7280process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7281 const gdb_byte *info_ptr,
0018ea6f 7282 struct die_info *comp_unit_die,
c0ab21c2 7283 enum language pretend_language)
0018ea6f
DE
7284{
7285 struct dwarf2_cu *cu = reader->cu;
518817b3 7286 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 7287 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7288 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7289 CORE_ADDR baseaddr;
7290 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7291 dwarf2_psymtab *pst;
3a2b436a 7292 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7293 const char *filename;
0018ea6f 7294
0018ea6f
DE
7295 gdb_assert (! per_cu->is_debug_types);
7296
c0ab21c2 7297 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7298
0018ea6f 7299 /* Allocate a new partial symbol table structure. */
2e927613
TV
7300 gdb::unique_xmalloc_ptr<char> debug_filename;
7301 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7302 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7303 if (filename == NULL)
0018ea6f 7304 filename = "";
2e927613
TV
7305 else if (strcmp (filename, artificial) == 0)
7306 {
7307 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7308 sect_offset_str (per_cu->sect_off),
7309 (char *) NULL));
2e927613
TV
7310 filename = debug_filename.get ();
7311 }
0018ea6f
DE
7312
7313 pst = create_partial_symtab (per_cu, filename);
7314
7315 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7316 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7317
b3b3bada 7318 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7319
7320 dwarf2_find_base_address (comp_unit_die, cu);
7321
7322 /* Possibly set the default values of LOWPC and HIGHPC from
7323 `DW_AT_ranges'. */
3a2b436a
JK
7324 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7325 &best_highpc, cu, pst);
7326 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7327 {
7328 CORE_ADDR low
7329 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7330 - baseaddr);
7331 CORE_ADDR high
7332 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7333 - baseaddr - 1);
7334 /* Store the contiguous range if it is not empty; it can be
7335 empty for CUs with no code. */
d320c2b5
TT
7336 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7337 low, high, pst);
79748972 7338 }
0018ea6f
DE
7339
7340 /* Check if comp unit has_children.
7341 If so, read the rest of the partial symbols from this comp unit.
7342 If not, there's no more debug_info for this comp unit. */
3e225074 7343 if (comp_unit_die->has_children)
0018ea6f
DE
7344 {
7345 struct partial_die_info *first_die;
7346 CORE_ADDR lowpc, highpc;
7347
7348 lowpc = ((CORE_ADDR) -1);
7349 highpc = ((CORE_ADDR) 0);
7350
7351 first_die = load_partial_dies (reader, info_ptr, 1);
7352
7353 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7354 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7355
7356 /* If we didn't find a lowpc, set it to highpc to avoid
7357 complaints from `maint check'. */
7358 if (lowpc == ((CORE_ADDR) -1))
7359 lowpc = highpc;
7360
7361 /* If the compilation unit didn't have an explicit address range,
7362 then use the information extracted from its child dies. */
e385593e 7363 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7364 {
7365 best_lowpc = lowpc;
7366 best_highpc = highpc;
7367 }
7368 }
4ae976d1 7369 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7370 best_lowpc + baseaddr)
7371 - baseaddr);
4ae976d1 7372 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7373 best_highpc + baseaddr)
7374 - baseaddr);
0018ea6f 7375
8763cede 7376 end_psymtab_common (objfile, pst);
0018ea6f 7377
ae640021 7378 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7379 {
7380 int i;
ae640021 7381 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7382
7383 /* Fill in 'dependencies' here; we fill in 'users' in a
7384 post-pass. */
7385 pst->number_of_dependencies = len;
a9342b62
TT
7386 pst->dependencies
7387 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7388 for (i = 0; i < len; ++i)
7389 {
7390 pst->dependencies[i]
7391 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7392 }
0018ea6f 7393
ae640021 7394 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7395 }
7396
7397 /* Get the list of files included in the current compilation unit,
7398 and build a psymtab for each of them. */
7399 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7400
b4f54984 7401 if (dwarf_read_debug)
b926417a
TT
7402 fprintf_unfiltered (gdb_stdlog,
7403 "Psymtab for %s unit @%s: %s - %s"
7404 ", %d global, %d static syms\n",
7405 per_cu->is_debug_types ? "type" : "comp",
7406 sect_offset_str (per_cu->sect_off),
7407 paddress (gdbarch, pst->text_low (objfile)),
7408 paddress (gdbarch, pst->text_high (objfile)),
7409 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7410}
7411
7412/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7413 Process compilation unit THIS_CU for a psymtab. */
7414
7415static void
7416process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
135f5437 7417 bool want_partial_unit,
b93601f3 7418 enum language pretend_language)
0018ea6f
DE
7419{
7420 /* If this compilation unit was already read in, free the
7421 cached copy in order to read it in again. This is
7422 necessary because we skipped some symbols when we first
7423 read in the compilation unit (see load_partial_dies).
7424 This problem could be avoided, but the benefit is unclear. */
7425 if (this_cu->cu != NULL)
7426 free_one_cached_comp_unit (this_cu);
7427
6751ebae 7428 cutu_reader reader (this_cu, NULL, 0, false);
c0ab21c2 7429
58990295
TV
7430 switch (reader.comp_unit_die->tag)
7431 {
7432 case DW_TAG_compile_unit:
7433 this_cu->unit_type = DW_UT_compile;
7434 break;
7435 case DW_TAG_partial_unit:
7436 this_cu->unit_type = DW_UT_partial;
7437 break;
7438 default:
7439 abort ();
7440 }
7441
c0ab21c2 7442 if (reader.dummy_p)
f1902523 7443 {
c0ab21c2 7444 /* Nothing. */
f1902523 7445 }
c0ab21c2 7446 else if (this_cu->is_debug_types)
3e225074
TT
7447 build_type_psymtabs_reader (&reader, reader.info_ptr,
7448 reader.comp_unit_die);
135f5437
TT
7449 else if (want_partial_unit
7450 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7451 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7452 reader.comp_unit_die,
c0ab21c2 7453 pretend_language);
0018ea6f 7454
58990295
TV
7455 this_cu->lang = this_cu->cu->language;
7456
0018ea6f 7457 /* Age out any secondary CUs. */
ed2dc618 7458 age_cached_comp_units (this_cu->dwarf2_per_objfile);
0018ea6f 7459}
f4dc4d17
DE
7460
7461/* Reader function for build_type_psymtabs. */
7462
7463static void
7464build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7465 const gdb_byte *info_ptr,
3e225074 7466 struct die_info *type_unit_die)
f4dc4d17 7467{
ed2dc618 7468 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 7469 = reader->cu->per_cu->dwarf2_per_objfile;
f4dc4d17
DE
7470 struct objfile *objfile = dwarf2_per_objfile->objfile;
7471 struct dwarf2_cu *cu = reader->cu;
7472 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7473 struct signatured_type *sig_type;
f4dc4d17
DE
7474 struct type_unit_group *tu_group;
7475 struct attribute *attr;
7476 struct partial_die_info *first_die;
7477 CORE_ADDR lowpc, highpc;
891813be 7478 dwarf2_psymtab *pst;
f4dc4d17 7479
0186c6a7
DE
7480 gdb_assert (per_cu->is_debug_types);
7481 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7482
3e225074 7483 if (! type_unit_die->has_children)
f4dc4d17
DE
7484 return;
7485
052c8bb8 7486 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7487 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7488
df07e2c7 7489 if (tu_group->tus == nullptr)
a8b3b8e9 7490 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7491 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7492
7493 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
f4dc4d17 7494 pst = create_partial_symtab (per_cu, "");
6d94535f 7495 pst->anonymous = true;
f4dc4d17
DE
7496
7497 first_die = load_partial_dies (reader, info_ptr, 1);
7498
7499 lowpc = (CORE_ADDR) -1;
7500 highpc = (CORE_ADDR) 0;
7501 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7502
8763cede 7503 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7504}
7505
73051182
DE
7506/* Struct used to sort TUs by their abbreviation table offset. */
7507
7508struct tu_abbrev_offset
7509{
b2bdb8cf
SM
7510 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7511 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7512 {}
7513
7514 signatured_type *sig_type;
73051182
DE
7515 sect_offset abbrev_offset;
7516};
7517
484cf504 7518/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7519
484cf504
TT
7520static bool
7521sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7522 const struct tu_abbrev_offset &b)
73051182 7523{
484cf504 7524 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7525}
7526
7527/* Efficiently read all the type units.
7528 This does the bulk of the work for build_type_psymtabs.
7529
7530 The efficiency is because we sort TUs by the abbrev table they use and
7531 only read each abbrev table once. In one program there are 200K TUs
7532 sharing 8K abbrev tables.
7533
7534 The main purpose of this function is to support building the
7535 dwarf2_per_objfile->type_unit_groups table.
7536 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7537 can collapse the search space by grouping them by stmt_list.
7538 The savings can be significant, in the same program from above the 200K TUs
7539 share 8K stmt_list tables.
7540
7541 FUNC is expected to call get_type_unit_group, which will create the
7542 struct type_unit_group if necessary and add it to
7543 dwarf2_per_objfile->type_unit_groups. */
7544
7545static void
ed2dc618 7546build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7547{
73051182 7548 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
685af9cd 7549 abbrev_table_up abbrev_table;
73051182 7550 sect_offset abbrev_offset;
73051182
DE
7551
7552 /* It's up to the caller to not call us multiple times. */
7553 gdb_assert (dwarf2_per_objfile->type_unit_groups == NULL);
7554
b2bdb8cf 7555 if (dwarf2_per_objfile->all_type_units.empty ())
73051182
DE
7556 return;
7557
7558 /* TUs typically share abbrev tables, and there can be way more TUs than
7559 abbrev tables. Sort by abbrev table to reduce the number of times we
7560 read each abbrev table in.
7561 Alternatives are to punt or to maintain a cache of abbrev tables.
7562 This is simpler and efficient enough for now.
7563
7564 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7565 symtab to use). Typically TUs with the same abbrev offset have the same
7566 stmt_list value too so in practice this should work well.
7567
7568 The basic algorithm here is:
7569
7570 sort TUs by abbrev table
7571 for each TU with same abbrev table:
7572 read abbrev table if first user
7573 read TU top level DIE
7574 [IWBN if DWO skeletons had DW_AT_stmt_list]
7575 call FUNC */
7576
b4f54984 7577 if (dwarf_read_debug)
73051182
DE
7578 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7579
7580 /* Sort in a separate table to maintain the order of all_type_units
7581 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf
SM
7582 std::vector<tu_abbrev_offset> sorted_by_abbrev;
7583 sorted_by_abbrev.reserve (dwarf2_per_objfile->all_type_units.size ());
7584
7585 for (signatured_type *sig_type : dwarf2_per_objfile->all_type_units)
7586 sorted_by_abbrev.emplace_back
7587 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7588 sig_type->per_cu.section,
7589 sig_type->per_cu.sect_off));
73051182 7590
484cf504
TT
7591 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7592 sort_tu_by_abbrev_offset);
73051182 7593
9c541725 7594 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7595
b2bdb8cf 7596 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7597 {
73051182
DE
7598 /* Switch to the next abbrev table if necessary. */
7599 if (abbrev_table == NULL
b2bdb8cf 7600 || tu.abbrev_offset != abbrev_offset)
73051182 7601 {
b2bdb8cf 7602 abbrev_offset = tu.abbrev_offset;
73051182 7603 abbrev_table =
86de1d91
TT
7604 abbrev_table::read (dwarf2_per_objfile->objfile,
7605 &dwarf2_per_objfile->abbrev,
7606 abbrev_offset);
73051182
DE
7607 ++tu_stats->nr_uniq_abbrev_tables;
7608 }
7609
c0ab21c2 7610 cutu_reader reader (&tu.sig_type->per_cu, abbrev_table.get (),
6751ebae 7611 0, false);
c0ab21c2
TT
7612 if (!reader.dummy_p)
7613 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7614 reader.comp_unit_die);
73051182 7615 }
6aa5f3a6 7616}
73051182 7617
6aa5f3a6
DE
7618/* Print collected type unit statistics. */
7619
7620static void
ed2dc618 7621print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7622{
7623 struct tu_stats *tu_stats = &dwarf2_per_objfile->tu_stats;
7624
7625 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf
SM
7626 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
7627 dwarf2_per_objfile->all_type_units.size ());
6aa5f3a6
DE
7628 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7629 tu_stats->nr_uniq_abbrev_tables);
7630 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7631 tu_stats->nr_symtabs);
7632 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7633 tu_stats->nr_symtab_sharers);
7634 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7635 tu_stats->nr_stmt_less_type_units);
7636 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7637 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7638}
7639
f4dc4d17
DE
7640/* Traversal function for build_type_psymtabs. */
7641
7642static int
7643build_type_psymtab_dependencies (void **slot, void *info)
7644{
ed2dc618
SM
7645 struct dwarf2_per_objfile *dwarf2_per_objfile
7646 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7647 struct objfile *objfile = dwarf2_per_objfile->objfile;
7648 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7649 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7650 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7651 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7652 int i;
7653
7654 gdb_assert (len > 0);
197400e8 7655 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7656
7657 pst->number_of_dependencies = len;
a9342b62 7658 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7659 for (i = 0; i < len; ++i)
f4dc4d17 7660 {
df07e2c7 7661 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7662 gdb_assert (iter->per_cu.is_debug_types);
7663 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7664 iter->type_unit_group = tu_group;
f4dc4d17
DE
7665 }
7666
df07e2c7
AB
7667 delete tu_group->tus;
7668 tu_group->tus = nullptr;
348e048f
DE
7669
7670 return 1;
7671}
7672
7673/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7674 Build partial symbol tables for the .debug_types comp-units. */
7675
7676static void
ed2dc618 7677build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7678{
ed2dc618 7679 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7680 return;
7681
ed2dc618 7682 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7683}
f4dc4d17 7684
6aa5f3a6
DE
7685/* Traversal function for process_skeletonless_type_unit.
7686 Read a TU in a DWO file and build partial symbols for it. */
7687
7688static int
7689process_skeletonless_type_unit (void **slot, void *info)
7690{
7691 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7692 struct dwarf2_per_objfile *dwarf2_per_objfile
7693 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7694 struct signatured_type find_entry, *entry;
7695
7696 /* If this TU doesn't exist in the global table, add it and read it in. */
7697
7698 if (dwarf2_per_objfile->signatured_types == NULL)
298e9637 7699 dwarf2_per_objfile->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7700
7701 find_entry.signature = dwo_unit->signature;
b0b6a987
TT
7702 slot = htab_find_slot (dwarf2_per_objfile->signatured_types.get (),
7703 &find_entry, INSERT);
6aa5f3a6
DE
7704 /* If we've already seen this type there's nothing to do. What's happening
7705 is we're doing our own version of comdat-folding here. */
7706 if (*slot != NULL)
7707 return 1;
7708
7709 /* This does the job that create_all_type_units would have done for
7710 this TU. */
ed2dc618
SM
7711 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7712 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7713 *slot = entry;
7714
7715 /* This does the job that build_type_psymtabs_1 would have done. */
6751ebae 7716 cutu_reader reader (&entry->per_cu, NULL, 0, false);
c0ab21c2
TT
7717 if (!reader.dummy_p)
7718 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7719 reader.comp_unit_die);
6aa5f3a6
DE
7720
7721 return 1;
7722}
7723
7724/* Traversal function for process_skeletonless_type_units. */
7725
7726static int
7727process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7728{
7729 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7730
7731 if (dwo_file->tus != NULL)
b0b6a987
TT
7732 htab_traverse_noresize (dwo_file->tus.get (),
7733 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7734
7735 return 1;
7736}
7737
7738/* Scan all TUs of DWO files, verifying we've processed them.
7739 This is needed in case a TU was emitted without its skeleton.
7740 Note: This can't be done until we know what all the DWO files are. */
7741
7742static void
ed2dc618 7743process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7744{
7745 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7746 if (get_dwp_file (dwarf2_per_objfile) == NULL
6aa5f3a6
DE
7747 && dwarf2_per_objfile->dwo_files != NULL)
7748 {
51ac9db5 7749 htab_traverse_noresize (dwarf2_per_objfile->dwo_files.get (),
6aa5f3a6 7750 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7751 dwarf2_per_objfile);
6aa5f3a6 7752 }
348e048f
DE
7753}
7754
ed2dc618 7755/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7756
7757static void
ed2dc618 7758set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7759{
b76e467d 7760 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
95554aad 7761 {
891813be 7762 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7763
36586728
TT
7764 if (pst == NULL)
7765 continue;
7766
b76e467d 7767 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7768 {
7769 /* Set the 'user' field only if it is not already set. */
7770 if (pst->dependencies[j]->user == NULL)
7771 pst->dependencies[j]->user = pst;
7772 }
7773 }
7774}
7775
93311388
DE
7776/* Build the partial symbol table by doing a quick pass through the
7777 .debug_info and .debug_abbrev sections. */
72bf9492 7778
93311388 7779static void
ed2dc618 7780dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7781{
ed2dc618 7782 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7783
b4f54984 7784 if (dwarf_read_debug)
45cfd468
DE
7785 {
7786 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7787 objfile_name (objfile));
45cfd468
DE
7788 }
7789
76935768
TT
7790 scoped_restore restore_reading_psyms
7791 = make_scoped_restore (&dwarf2_per_objfile->reading_partial_symbols,
7792 true);
98bfdba5 7793
96b79293 7794 dwarf2_per_objfile->info.read (objfile);
91c24f0a 7795
93311388
DE
7796 /* Any cached compilation units will be linked by the per-objfile
7797 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7798 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7799
ed2dc618 7800 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7801
ed2dc618 7802 create_all_comp_units (dwarf2_per_objfile);
c906108c 7803
60606b2c
TT
7804 /* Create a temporary address map on a temporary obstack. We later
7805 copy this to the final obstack. */
8268c778 7806 auto_obstack temp_obstack;
791afaa2
TT
7807
7808 scoped_restore save_psymtabs_addrmap
d320c2b5 7809 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7810 addrmap_create_mutable (&temp_obstack));
72bf9492 7811
b76e467d 7812 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->all_comp_units)
3d5afab3
TV
7813 {
7814 if (per_cu->v.psymtab != NULL)
7815 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7816 continue;
7817 process_psymtab_comp_unit (per_cu, false, language_minimal);
7818 }
ff013f42 7819
6aa5f3a6 7820 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7821 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7822
7823 /* Now that all TUs have been processed we can fill in the dependencies. */
7824 if (dwarf2_per_objfile->type_unit_groups != NULL)
7825 {
eaa5fa8b 7826 htab_traverse_noresize (dwarf2_per_objfile->type_unit_groups.get (),
ed2dc618 7827 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7828 }
7829
b4f54984 7830 if (dwarf_read_debug)
ed2dc618 7831 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7832
ed2dc618 7833 set_partial_user (dwarf2_per_objfile);
95554aad 7834
d320c2b5
TT
7835 objfile->partial_symtabs->psymtabs_addrmap
7836 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7837 objfile->partial_symtabs->obstack ());
791afaa2
TT
7838 /* At this point we want to keep the address map. */
7839 save_psymtabs_addrmap.release ();
ff013f42 7840
b4f54984 7841 if (dwarf_read_debug)
45cfd468 7842 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7843 objfile_name (objfile));
ae038cb0
DJ
7844}
7845
dee91e82
DE
7846/* Load the partial DIEs for a secondary CU into memory.
7847 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7848
dee91e82
DE
7849static void
7850load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
7851{
6751ebae 7852 cutu_reader reader (this_cu, NULL, 1, false);
c0ab21c2
TT
7853
7854 if (!reader.dummy_p)
7855 {
7856 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7857 language_minimal);
7858
7859 /* Check if comp unit has_children.
7860 If so, read the rest of the partial symbols from this comp unit.
7861 If not, there's no more debug_info for this comp unit. */
3e225074 7862 if (reader.comp_unit_die->has_children)
c0ab21c2 7863 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7864
7865 reader.keep ();
c0ab21c2 7866 }
ae038cb0
DJ
7867}
7868
ae038cb0 7869static void
ed2dc618 7870read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7871 struct dwarf2_section_info *section,
f1902523 7872 struct dwarf2_section_info *abbrev_section,
b76e467d 7873 unsigned int is_dwz)
ae038cb0 7874{
d521ce57 7875 const gdb_byte *info_ptr;
ed2dc618 7876 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7877
b4f54984 7878 if (dwarf_read_debug)
bf6af496 7879 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7880 section->get_name (),
7881 section->get_file_name ());
bf6af496 7882
96b79293 7883 section->read (objfile);
ae038cb0 7884
36586728 7885 info_ptr = section->buffer;
6e70227d 7886
36586728 7887 while (info_ptr < section->buffer + section->size)
ae038cb0 7888 {
ae038cb0 7889 struct dwarf2_per_cu_data *this_cu;
ae038cb0 7890
9c541725 7891 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 7892
f1902523 7893 comp_unit_head cu_header;
ed2dc618
SM
7894 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
7895 abbrev_section, info_ptr,
7896 rcuh_kind::COMPILE);
ae038cb0
DJ
7897
7898 /* Save the compilation unit for later lookup. */
f1902523
JK
7899 if (cu_header.unit_type != DW_UT_type)
7900 {
7901 this_cu = XOBNEW (&objfile->objfile_obstack,
7902 struct dwarf2_per_cu_data);
7903 memset (this_cu, 0, sizeof (*this_cu));
7904 }
7905 else
7906 {
7907 auto sig_type = XOBNEW (&objfile->objfile_obstack,
7908 struct signatured_type);
7909 memset (sig_type, 0, sizeof (*sig_type));
7910 sig_type->signature = cu_header.signature;
7911 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
7912 this_cu = &sig_type->per_cu;
7913 }
7914 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 7915 this_cu->sect_off = sect_off;
f1902523 7916 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 7917 this_cu->is_dwz = is_dwz;
e3b94546 7918 this_cu->dwarf2_per_objfile = dwarf2_per_objfile;
8a0459fd 7919 this_cu->section = section;
ae038cb0 7920
b76e467d 7921 dwarf2_per_objfile->all_comp_units.push_back (this_cu);
ae038cb0
DJ
7922
7923 info_ptr = info_ptr + this_cu->length;
7924 }
36586728
TT
7925}
7926
7927/* Create a list of all compilation units in OBJFILE.
7928 This is only done for -readnow and building partial symtabs. */
7929
7930static void
ed2dc618 7931create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 7932{
b76e467d 7933 gdb_assert (dwarf2_per_objfile->all_comp_units.empty ());
ed2dc618 7934 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->info,
b76e467d 7935 &dwarf2_per_objfile->abbrev, 0);
36586728 7936
b76e467d 7937 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
4db1a1dc 7938 if (dwz != NULL)
ed2dc618 7939 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 7940 1);
c906108c
SS
7941}
7942
5734ee8b 7943/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 7944 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 7945 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
7946 DW_AT_ranges). See the comments of add_partial_subprogram on how
7947 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 7948
72bf9492
DJ
7949static void
7950scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
7951 CORE_ADDR *highpc, int set_addrmap,
7952 struct dwarf2_cu *cu)
c906108c 7953{
72bf9492 7954 struct partial_die_info *pdi;
c906108c 7955
91c24f0a
DC
7956 /* Now, march along the PDI's, descending into ones which have
7957 interesting children but skipping the children of the other ones,
7958 until we reach the end of the compilation unit. */
c906108c 7959
72bf9492 7960 pdi = first_die;
91c24f0a 7961
72bf9492
DJ
7962 while (pdi != NULL)
7963 {
52356b79 7964 pdi->fixup (cu);
c906108c 7965
f55ee35c 7966 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
7967 children, so we need to look at them. Ditto for anonymous
7968 enums. */
933c6fe4 7969
72bf9492 7970 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 7971 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
7972 || pdi->tag == DW_TAG_imported_unit
7973 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 7974 {
72bf9492 7975 switch (pdi->tag)
c906108c
SS
7976 {
7977 case DW_TAG_subprogram:
b1dc1806 7978 case DW_TAG_inlined_subroutine:
cdc07690 7979 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 7980 break;
72929c62 7981 case DW_TAG_constant:
c906108c
SS
7982 case DW_TAG_variable:
7983 case DW_TAG_typedef:
91c24f0a 7984 case DW_TAG_union_type:
317d2668
TV
7985 if (!pdi->is_declaration
7986 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 7987 {
72bf9492 7988 add_partial_symbol (pdi, cu);
63d06c5c
DC
7989 }
7990 break;
c906108c 7991 case DW_TAG_class_type:
680b30c7 7992 case DW_TAG_interface_type:
c906108c 7993 case DW_TAG_structure_type:
72bf9492 7994 if (!pdi->is_declaration)
c906108c 7995 {
72bf9492 7996 add_partial_symbol (pdi, cu);
c906108c 7997 }
b7fee5a3
KS
7998 if ((cu->language == language_rust
7999 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8000 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8001 set_addrmap, cu);
c906108c 8002 break;
91c24f0a 8003 case DW_TAG_enumeration_type:
72bf9492
DJ
8004 if (!pdi->is_declaration)
8005 add_partial_enumeration (pdi, cu);
c906108c
SS
8006 break;
8007 case DW_TAG_base_type:
a02abb62 8008 case DW_TAG_subrange_type:
c906108c 8009 /* File scope base type definitions are added to the partial
c5aa993b 8010 symbol table. */
72bf9492 8011 add_partial_symbol (pdi, cu);
c906108c 8012 break;
d9fa45fe 8013 case DW_TAG_namespace:
cdc07690 8014 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8015 break;
5d7cb8df 8016 case DW_TAG_module:
59c35742
AB
8017 if (!pdi->is_declaration)
8018 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8019 break;
95554aad
TT
8020 case DW_TAG_imported_unit:
8021 {
8022 struct dwarf2_per_cu_data *per_cu;
8023
f4dc4d17
DE
8024 /* For now we don't handle imported units in type units. */
8025 if (cu->per_cu->is_debug_types)
8026 {
8027 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8028 " supported in type units [in module %s]"),
518817b3 8029 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
8030 }
8031
e3b94546
SM
8032 per_cu = dwarf2_find_containing_comp_unit
8033 (pdi->d.sect_off, pdi->is_dwz,
518817b3 8034 cu->per_cu->dwarf2_per_objfile);
95554aad
TT
8035
8036 /* Go read the partial unit, if needed. */
8037 if (per_cu->v.psymtab == NULL)
135f5437 8038 process_psymtab_comp_unit (per_cu, true, cu->language);
95554aad 8039
ae640021 8040 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8041 }
8042 break;
74921315
KS
8043 case DW_TAG_imported_declaration:
8044 add_partial_symbol (pdi, cu);
8045 break;
c906108c
SS
8046 default:
8047 break;
8048 }
8049 }
8050
72bf9492
DJ
8051 /* If the die has a sibling, skip to the sibling. */
8052
8053 pdi = pdi->die_sibling;
8054 }
8055}
8056
8057/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8058
72bf9492 8059 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8060 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8061 Enumerators are an exception; they use the scope of their parent
8062 enumeration type, i.e. the name of the enumeration type is not
8063 prepended to the enumerator.
91c24f0a 8064
72bf9492
DJ
8065 There are two complexities. One is DW_AT_specification; in this
8066 case "parent" means the parent of the target of the specification,
8067 instead of the direct parent of the DIE. The other is compilers
8068 which do not emit DW_TAG_namespace; in this case we try to guess
8069 the fully qualified name of structure types from their members'
8070 linkage names. This must be done using the DIE's children rather
8071 than the children of any DW_AT_specification target. We only need
8072 to do this for structures at the top level, i.e. if the target of
8073 any DW_AT_specification (if any; otherwise the DIE itself) does not
8074 have a parent. */
8075
8076/* Compute the scope prefix associated with PDI's parent, in
8077 compilation unit CU. The result will be allocated on CU's
8078 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8079 field. NULL is returned if no prefix is necessary. */
15d034d0 8080static const char *
72bf9492
DJ
8081partial_die_parent_scope (struct partial_die_info *pdi,
8082 struct dwarf2_cu *cu)
8083{
15d034d0 8084 const char *grandparent_scope;
72bf9492 8085 struct partial_die_info *parent, *real_pdi;
91c24f0a 8086
72bf9492
DJ
8087 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8088 then this means the parent of the specification DIE. */
8089
8090 real_pdi = pdi;
72bf9492 8091 while (real_pdi->has_specification)
fb816e8b 8092 {
122cf0f2
AB
8093 auto res = find_partial_die (real_pdi->spec_offset,
8094 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8095 real_pdi = res.pdi;
8096 cu = res.cu;
8097 }
72bf9492
DJ
8098
8099 parent = real_pdi->die_parent;
8100 if (parent == NULL)
8101 return NULL;
8102
8103 if (parent->scope_set)
8104 return parent->scope;
8105
52356b79 8106 parent->fixup (cu);
72bf9492 8107
10b3939b 8108 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8109
acebe513
UW
8110 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8111 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8112 Work around this problem here. */
8113 if (cu->language == language_cplus
6e70227d 8114 && parent->tag == DW_TAG_namespace
acebe513
UW
8115 && strcmp (parent->name, "::") == 0
8116 && grandparent_scope == NULL)
8117 {
8118 parent->scope = NULL;
8119 parent->scope_set = 1;
8120 return NULL;
8121 }
8122
0a4b0913 8123 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8124 if (pdi->tag == DW_TAG_enumerator)
8125 /* Enumerators should not get the name of the enumeration as a prefix. */
8126 parent->scope = grandparent_scope;
8127 else if (parent->tag == DW_TAG_namespace
f55ee35c 8128 || parent->tag == DW_TAG_module
72bf9492
DJ
8129 || parent->tag == DW_TAG_structure_type
8130 || parent->tag == DW_TAG_class_type
680b30c7 8131 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8132 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8133 || parent->tag == DW_TAG_enumeration_type
8134 || (cu->language == language_fortran
8135 && parent->tag == DW_TAG_subprogram
8136 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8137 {
8138 if (grandparent_scope == NULL)
8139 parent->scope = parent->name;
8140 else
3e43a32a
MS
8141 parent->scope = typename_concat (&cu->comp_unit_obstack,
8142 grandparent_scope,
f55ee35c 8143 parent->name, 0, cu);
72bf9492 8144 }
72bf9492
DJ
8145 else
8146 {
8147 /* FIXME drow/2004-04-01: What should we be doing with
8148 function-local names? For partial symbols, we should probably be
8149 ignoring them. */
fa9c3fa0
TT
8150 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8151 dwarf_tag_name (parent->tag),
8152 sect_offset_str (pdi->sect_off));
72bf9492 8153 parent->scope = grandparent_scope;
c906108c
SS
8154 }
8155
72bf9492
DJ
8156 parent->scope_set = 1;
8157 return parent->scope;
8158}
8159
8160/* Return the fully scoped name associated with PDI, from compilation unit
8161 CU. The result will be allocated with malloc. */
4568ecf9 8162
43816ebc 8163static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8164partial_die_full_name (struct partial_die_info *pdi,
8165 struct dwarf2_cu *cu)
8166{
15d034d0 8167 const char *parent_scope;
72bf9492 8168
98bfdba5
PA
8169 /* If this is a template instantiation, we can not work out the
8170 template arguments from partial DIEs. So, unfortunately, we have
8171 to go through the full DIEs. At least any work we do building
8172 types here will be reused if full symbols are loaded later. */
8173 if (pdi->has_template_arguments)
8174 {
52356b79 8175 pdi->fixup (cu);
98bfdba5
PA
8176
8177 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8178 {
8179 struct die_info *die;
8180 struct attribute attr;
8181 struct dwarf2_cu *ref_cu = cu;
8182
b64f50a1 8183 /* DW_FORM_ref_addr is using section offset. */
b4069958 8184 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8185 attr.form = DW_FORM_ref_addr;
9c541725 8186 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8187 die = follow_die_ref (NULL, &attr, &ref_cu);
8188
43816ebc 8189 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8190 }
8191 }
8192
72bf9492
DJ
8193 parent_scope = partial_die_parent_scope (pdi, cu);
8194 if (parent_scope == NULL)
8195 return NULL;
8196 else
43816ebc
TT
8197 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8198 pdi->name, 0, cu));
c906108c
SS
8199}
8200
8201static void
72bf9492 8202add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8203{
518817b3
SM
8204 struct dwarf2_per_objfile *dwarf2_per_objfile
8205 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 8206 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8207 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8208 CORE_ADDR addr = 0;
15d034d0 8209 const char *actual_name = NULL;
e142c38c
DJ
8210 CORE_ADDR baseaddr;
8211
b3b3bada 8212 baseaddr = objfile->text_section_offset ();
c906108c 8213
43816ebc
TT
8214 gdb::unique_xmalloc_ptr<char> built_actual_name
8215 = partial_die_full_name (pdi, cu);
15d034d0 8216 if (built_actual_name != NULL)
43816ebc 8217 actual_name = built_actual_name.get ();
63d06c5c 8218
72bf9492
DJ
8219 if (actual_name == NULL)
8220 actual_name = pdi->name;
8221
c906108c
SS
8222 switch (pdi->tag)
8223 {
b1dc1806 8224 case DW_TAG_inlined_subroutine:
c906108c 8225 case DW_TAG_subprogram:
79748972
TT
8226 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8227 - baseaddr);
0a4b0913
AB
8228 if (pdi->is_external
8229 || cu->language == language_ada
8230 || (cu->language == language_fortran
8231 && pdi->die_parent != NULL
8232 && pdi->die_parent->tag == DW_TAG_subprogram))
8233 {
8234 /* Normally, only "external" DIEs are part of the global scope.
8235 But in Ada and Fortran, we want to be able to access nested
8236 procedures globally. So all Ada and Fortran subprograms are
8237 stored in the global scope. */
31edb802 8238 add_psymbol_to_list (actual_name,
15d034d0 8239 built_actual_name != NULL,
f47fb265 8240 VAR_DOMAIN, LOC_BLOCK,
79748972 8241 SECT_OFF_TEXT (objfile),
75aedd27 8242 psymbol_placement::GLOBAL,
79748972
TT
8243 addr,
8244 cu->language, objfile);
c906108c
SS
8245 }
8246 else
8247 {
31edb802 8248 add_psymbol_to_list (actual_name,
15d034d0 8249 built_actual_name != NULL,
f47fb265 8250 VAR_DOMAIN, LOC_BLOCK,
79748972 8251 SECT_OFF_TEXT (objfile),
75aedd27 8252 psymbol_placement::STATIC,
1762568f 8253 addr, cu->language, objfile);
c906108c 8254 }
0c1b455e
TT
8255
8256 if (pdi->main_subprogram && actual_name != NULL)
8257 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8258 break;
72929c62 8259 case DW_TAG_constant:
31edb802 8260 add_psymbol_to_list (actual_name,
75aedd27
TT
8261 built_actual_name != NULL, VAR_DOMAIN, LOC_STATIC,
8262 -1, (pdi->is_external
8263 ? psymbol_placement::GLOBAL
8264 : psymbol_placement::STATIC),
8265 0, cu->language, objfile);
72929c62 8266 break;
c906108c 8267 case DW_TAG_variable:
95554aad
TT
8268 if (pdi->d.locdesc)
8269 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8270
95554aad 8271 if (pdi->d.locdesc
caac4577
JG
8272 && addr == 0
8273 && !dwarf2_per_objfile->has_section_at_zero)
8274 {
8275 /* A global or static variable may also have been stripped
8276 out by the linker if unused, in which case its address
8277 will be nullified; do not add such variables into partial
8278 symbol table then. */
8279 }
8280 else if (pdi->is_external)
c906108c
SS
8281 {
8282 /* Global Variable.
8283 Don't enter into the minimal symbol tables as there is
8284 a minimal symbol table entry from the ELF symbols already.
8285 Enter into partial symbol table if it has a location
8286 descriptor or a type.
8287 If the location descriptor is missing, new_symbol will create
8288 a LOC_UNRESOLVED symbol, the address of the variable will then
8289 be determined from the minimal symbol table whenever the variable
8290 is referenced.
8291 The address for the partial symbol table entry is not
8292 used by GDB, but it comes in handy for debugging partial symbol
8293 table building. */
8294
95554aad 8295 if (pdi->d.locdesc || pdi->has_type)
31edb802 8296 add_psymbol_to_list (actual_name,
15d034d0 8297 built_actual_name != NULL,
f47fb265 8298 VAR_DOMAIN, LOC_STATIC,
79748972 8299 SECT_OFF_TEXT (objfile),
75aedd27 8300 psymbol_placement::GLOBAL,
79748972 8301 addr, cu->language, objfile);
c906108c
SS
8302 }
8303 else
8304 {
ff908ebf
AW
8305 int has_loc = pdi->d.locdesc != NULL;
8306
8307 /* Static Variable. Skip symbols whose value we cannot know (those
8308 without location descriptors or constant values). */
8309 if (!has_loc && !pdi->has_const_value)
43816ebc 8310 return;
ff908ebf 8311
31edb802 8312 add_psymbol_to_list (actual_name,
15d034d0 8313 built_actual_name != NULL,
f47fb265 8314 VAR_DOMAIN, LOC_STATIC,
79748972 8315 SECT_OFF_TEXT (objfile),
75aedd27 8316 psymbol_placement::STATIC,
79748972 8317 has_loc ? addr : 0,
f47fb265 8318 cu->language, objfile);
c906108c
SS
8319 }
8320 break;
8321 case DW_TAG_typedef:
8322 case DW_TAG_base_type:
a02abb62 8323 case DW_TAG_subrange_type:
31edb802 8324 add_psymbol_to_list (actual_name,
15d034d0 8325 built_actual_name != NULL,
79748972 8326 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8327 psymbol_placement::STATIC,
1762568f 8328 0, cu->language, objfile);
c906108c 8329 break;
74921315 8330 case DW_TAG_imported_declaration:
72bf9492 8331 case DW_TAG_namespace:
31edb802 8332 add_psymbol_to_list (actual_name,
15d034d0 8333 built_actual_name != NULL,
79748972 8334 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 8335 psymbol_placement::GLOBAL,
1762568f 8336 0, cu->language, objfile);
72bf9492 8337 break;
530e8392 8338 case DW_TAG_module:
a5fd13a9
BH
8339 /* With Fortran 77 there might be a "BLOCK DATA" module
8340 available without any name. If so, we skip the module as it
8341 doesn't bring any value. */
8342 if (actual_name != nullptr)
31edb802 8343 add_psymbol_to_list (actual_name,
a5fd13a9
BH
8344 built_actual_name != NULL,
8345 MODULE_DOMAIN, LOC_TYPEDEF, -1,
8346 psymbol_placement::GLOBAL,
8347 0, cu->language, objfile);
530e8392 8348 break;
c906108c 8349 case DW_TAG_class_type:
680b30c7 8350 case DW_TAG_interface_type:
c906108c
SS
8351 case DW_TAG_structure_type:
8352 case DW_TAG_union_type:
8353 case DW_TAG_enumeration_type:
fa4028e9
JB
8354 /* Skip external references. The DWARF standard says in the section
8355 about "Structure, Union, and Class Type Entries": "An incomplete
8356 structure, union or class type is represented by a structure,
8357 union or class entry that does not have a byte size attribute
8358 and that has a DW_AT_declaration attribute." */
8359 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8360 return;
fa4028e9 8361
63d06c5c
DC
8362 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8363 static vs. global. */
31edb802 8364 add_psymbol_to_list (actual_name,
15d034d0 8365 built_actual_name != NULL,
79748972 8366 STRUCT_DOMAIN, LOC_TYPEDEF, -1,
9c37b5ae 8367 cu->language == language_cplus
75aedd27
TT
8368 ? psymbol_placement::GLOBAL
8369 : psymbol_placement::STATIC,
1762568f 8370 0, cu->language, objfile);
c906108c 8371
c906108c
SS
8372 break;
8373 case DW_TAG_enumerator:
31edb802 8374 add_psymbol_to_list (actual_name,
15d034d0 8375 built_actual_name != NULL,
79748972 8376 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 8377 cu->language == language_cplus
75aedd27
TT
8378 ? psymbol_placement::GLOBAL
8379 : psymbol_placement::STATIC,
1762568f 8380 0, cu->language, objfile);
c906108c
SS
8381 break;
8382 default:
8383 break;
8384 }
8385}
8386
5c4e30ca
DC
8387/* Read a partial die corresponding to a namespace; also, add a symbol
8388 corresponding to that namespace to the symbol table. NAMESPACE is
8389 the name of the enclosing namespace. */
91c24f0a 8390
72bf9492
DJ
8391static void
8392add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8393 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8394 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8395{
72bf9492 8396 /* Add a symbol for the namespace. */
e7c27a73 8397
72bf9492 8398 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8399
8400 /* Now scan partial symbols in that namespace. */
8401
91c24f0a 8402 if (pdi->has_children)
cdc07690 8403 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8404}
8405
5d7cb8df
JK
8406/* Read a partial die corresponding to a Fortran module. */
8407
8408static void
8409add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8410 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8411{
530e8392
KB
8412 /* Add a symbol for the namespace. */
8413
8414 add_partial_symbol (pdi, cu);
8415
f55ee35c 8416 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8417
8418 if (pdi->has_children)
cdc07690 8419 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8420}
8421
b1dc1806
XR
8422/* Read a partial die corresponding to a subprogram or an inlined
8423 subprogram and create a partial symbol for that subprogram.
8424 When the CU language allows it, this routine also defines a partial
8425 symbol for each nested subprogram that this subprogram contains.
8426 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8427 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8428
cdc07690
YQ
8429 PDI may also be a lexical block, in which case we simply search
8430 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8431 Again, this is only performed when the CU language allows this
8432 type of definitions. */
8433
8434static void
8435add_partial_subprogram (struct partial_die_info *pdi,
8436 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8437 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8438{
b1dc1806 8439 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8440 {
8441 if (pdi->has_pc_info)
8442 {
8443 if (pdi->lowpc < *lowpc)
8444 *lowpc = pdi->lowpc;
8445 if (pdi->highpc > *highpc)
8446 *highpc = pdi->highpc;
cdc07690 8447 if (set_addrmap)
5734ee8b 8448 {
518817b3 8449 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 8450 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8451 CORE_ADDR baseaddr;
b926417a
TT
8452 CORE_ADDR this_highpc;
8453 CORE_ADDR this_lowpc;
5734ee8b 8454
b3b3bada 8455 baseaddr = objfile->text_section_offset ();
b926417a
TT
8456 this_lowpc
8457 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8458 pdi->lowpc + baseaddr)
8459 - baseaddr);
8460 this_highpc
8461 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8462 pdi->highpc + baseaddr)
8463 - baseaddr);
d320c2b5 8464 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8465 this_lowpc, this_highpc - 1,
9291a0cd 8466 cu->per_cu->v.psymtab);
5734ee8b 8467 }
481860b3
GB
8468 }
8469
8470 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8471 {
bc30ff58 8472 if (!pdi->is_declaration)
e8d05480
JB
8473 /* Ignore subprogram DIEs that do not have a name, they are
8474 illegal. Do not emit a complaint at this point, we will
8475 do so when we convert this psymtab into a symtab. */
8476 if (pdi->name)
8477 add_partial_symbol (pdi, cu);
bc30ff58
JB
8478 }
8479 }
6e70227d 8480
bc30ff58
JB
8481 if (! pdi->has_children)
8482 return;
8483
0a4b0913 8484 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8485 {
8486 pdi = pdi->die_child;
8487 while (pdi != NULL)
8488 {
52356b79 8489 pdi->fixup (cu);
bc30ff58 8490 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8491 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8492 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8493 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8494 pdi = pdi->die_sibling;
8495 }
8496 }
8497}
8498
91c24f0a
DC
8499/* Read a partial die corresponding to an enumeration type. */
8500
72bf9492
DJ
8501static void
8502add_partial_enumeration (struct partial_die_info *enum_pdi,
8503 struct dwarf2_cu *cu)
91c24f0a 8504{
72bf9492 8505 struct partial_die_info *pdi;
91c24f0a
DC
8506
8507 if (enum_pdi->name != NULL)
72bf9492
DJ
8508 add_partial_symbol (enum_pdi, cu);
8509
8510 pdi = enum_pdi->die_child;
8511 while (pdi)
91c24f0a 8512 {
72bf9492 8513 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8514 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8515 else
72bf9492
DJ
8516 add_partial_symbol (pdi, cu);
8517 pdi = pdi->die_sibling;
91c24f0a 8518 }
91c24f0a
DC
8519}
8520
6caca83c
CC
8521/* Return the initial uleb128 in the die at INFO_PTR. */
8522
8523static unsigned int
d521ce57 8524peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8525{
8526 unsigned int bytes_read;
8527
8528 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8529}
8530
685af9cd
TT
8531/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8532 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8533
4bb7a0a7
DJ
8534 Return the corresponding abbrev, or NULL if the number is zero (indicating
8535 an empty DIE). In either case *BYTES_READ will be set to the length of
8536 the initial number. */
8537
8538static struct abbrev_info *
685af9cd
TT
8539peek_die_abbrev (const die_reader_specs &reader,
8540 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8541{
685af9cd 8542 dwarf2_cu *cu = reader.cu;
518817b3 8543 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
685af9cd
TT
8544 unsigned int abbrev_number
8545 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8546
8547 if (abbrev_number == 0)
8548 return NULL;
8549
685af9cd 8550 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8551 if (!abbrev)
8552 {
422b9917 8553 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8554 " at offset %s [in module %s]"),
422b9917 8555 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8556 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8557 }
8558
8559 return abbrev;
8560}
8561
93311388
DE
8562/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8563 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8564 DIE. Any children of the skipped DIEs will also be skipped. */
8565
d521ce57
TT
8566static const gdb_byte *
8567skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8568{
4bb7a0a7
DJ
8569 while (1)
8570 {
685af9cd
TT
8571 unsigned int bytes_read;
8572 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8573
4bb7a0a7
DJ
8574 if (abbrev == NULL)
8575 return info_ptr + bytes_read;
8576 else
dee91e82 8577 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8578 }
8579}
8580
93311388
DE
8581/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8582 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8583 abbrev corresponding to that skipped uleb128 should be passed in
8584 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8585 children. */
8586
d521ce57
TT
8587static const gdb_byte *
8588skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8589 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8590{
8591 unsigned int bytes_read;
8592 struct attribute attr;
dee91e82
DE
8593 bfd *abfd = reader->abfd;
8594 struct dwarf2_cu *cu = reader->cu;
d521ce57 8595 const gdb_byte *buffer = reader->buffer;
f664829e 8596 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8597 unsigned int form, i;
8598
8599 for (i = 0; i < abbrev->num_attrs; i++)
8600 {
8601 /* The only abbrev we care about is DW_AT_sibling. */
8602 if (abbrev->attrs[i].name == DW_AT_sibling)
8603 {
18a8505e
AT
8604 bool ignored;
8605 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8606 &ignored);
4bb7a0a7 8607 if (attr.form == DW_FORM_ref_addr)
b98664d3 8608 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8609 else
b9502d3f 8610 {
0826b30a 8611 sect_offset off = attr.get_ref_die_offset ();
9c541725 8612 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8613
8614 if (sibling_ptr < info_ptr)
b98664d3 8615 complaint (_("DW_AT_sibling points backwards"));
22869d73 8616 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8617 reader->die_section->overflow_complaint ();
b9502d3f
WN
8618 else
8619 return sibling_ptr;
8620 }
4bb7a0a7
DJ
8621 }
8622
8623 /* If it isn't DW_AT_sibling, skip this attribute. */
8624 form = abbrev->attrs[i].form;
8625 skip_attribute:
8626 switch (form)
8627 {
4bb7a0a7 8628 case DW_FORM_ref_addr:
ae411497
TT
8629 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8630 and later it is offset sized. */
8631 if (cu->header.version == 2)
8632 info_ptr += cu->header.addr_size;
8633 else
8634 info_ptr += cu->header.offset_size;
8635 break;
36586728
TT
8636 case DW_FORM_GNU_ref_alt:
8637 info_ptr += cu->header.offset_size;
8638 break;
ae411497 8639 case DW_FORM_addr:
4bb7a0a7
DJ
8640 info_ptr += cu->header.addr_size;
8641 break;
8642 case DW_FORM_data1:
8643 case DW_FORM_ref1:
8644 case DW_FORM_flag:
8fe0f950 8645 case DW_FORM_strx1:
4bb7a0a7
DJ
8646 info_ptr += 1;
8647 break;
2dc7f7b3 8648 case DW_FORM_flag_present:
43988095 8649 case DW_FORM_implicit_const:
2dc7f7b3 8650 break;
4bb7a0a7
DJ
8651 case DW_FORM_data2:
8652 case DW_FORM_ref2:
8fe0f950 8653 case DW_FORM_strx2:
4bb7a0a7
DJ
8654 info_ptr += 2;
8655 break;
8fe0f950
AT
8656 case DW_FORM_strx3:
8657 info_ptr += 3;
8658 break;
4bb7a0a7
DJ
8659 case DW_FORM_data4:
8660 case DW_FORM_ref4:
8fe0f950 8661 case DW_FORM_strx4:
4bb7a0a7
DJ
8662 info_ptr += 4;
8663 break;
8664 case DW_FORM_data8:
8665 case DW_FORM_ref8:
55f1336d 8666 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8667 info_ptr += 8;
8668 break;
0224619f
JK
8669 case DW_FORM_data16:
8670 info_ptr += 16;
8671 break;
4bb7a0a7 8672 case DW_FORM_string:
9b1c24c8 8673 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8674 info_ptr += bytes_read;
8675 break;
2dc7f7b3 8676 case DW_FORM_sec_offset:
4bb7a0a7 8677 case DW_FORM_strp:
36586728 8678 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8679 info_ptr += cu->header.offset_size;
8680 break;
2dc7f7b3 8681 case DW_FORM_exprloc:
4bb7a0a7
DJ
8682 case DW_FORM_block:
8683 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8684 info_ptr += bytes_read;
8685 break;
8686 case DW_FORM_block1:
8687 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8688 break;
8689 case DW_FORM_block2:
8690 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8691 break;
8692 case DW_FORM_block4:
8693 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8694 break;
336d760d 8695 case DW_FORM_addrx:
cf532bd1 8696 case DW_FORM_strx:
4bb7a0a7
DJ
8697 case DW_FORM_sdata:
8698 case DW_FORM_udata:
8699 case DW_FORM_ref_udata:
3019eac3
DE
8700 case DW_FORM_GNU_addr_index:
8701 case DW_FORM_GNU_str_index:
18a8505e 8702 case DW_FORM_rnglistx:
41144253 8703 case DW_FORM_loclistx:
d521ce57 8704 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8705 break;
8706 case DW_FORM_indirect:
8707 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8708 info_ptr += bytes_read;
8709 /* We need to continue parsing from here, so just go back to
8710 the top. */
8711 goto skip_attribute;
8712
8713 default:
3e43a32a
MS
8714 error (_("Dwarf Error: Cannot handle %s "
8715 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8716 dwarf_form_name (form),
8717 bfd_get_filename (abfd));
8718 }
8719 }
8720
8721 if (abbrev->has_children)
dee91e82 8722 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8723 else
8724 return info_ptr;
8725}
8726
93311388 8727/* Locate ORIG_PDI's sibling.
dee91e82 8728 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8729
d521ce57 8730static const gdb_byte *
dee91e82
DE
8731locate_pdi_sibling (const struct die_reader_specs *reader,
8732 struct partial_die_info *orig_pdi,
d521ce57 8733 const gdb_byte *info_ptr)
91c24f0a
DC
8734{
8735 /* Do we know the sibling already? */
72bf9492 8736
91c24f0a
DC
8737 if (orig_pdi->sibling)
8738 return orig_pdi->sibling;
8739
8740 /* Are there any children to deal with? */
8741
8742 if (!orig_pdi->has_children)
8743 return info_ptr;
8744
4bb7a0a7 8745 /* Skip the children the long way. */
91c24f0a 8746
dee91e82 8747 return skip_children (reader, info_ptr);
91c24f0a
DC
8748}
8749
257e7a09 8750/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8751 not NULL. */
c906108c 8752
891813be
TT
8753void
8754dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8755{
ed2dc618
SM
8756 struct dwarf2_per_objfile *dwarf2_per_objfile
8757 = get_dwarf2_per_objfile (objfile);
8758
077cbab2
TT
8759 gdb_assert (!readin);
8760 /* If this psymtab is constructed from a debug-only objfile, the
8761 has_section_at_zero flag will not necessarily be correct. We
8762 can get the correct value for this flag by looking at the data
8763 associated with the (presumably stripped) associated objfile. */
8764 if (objfile->separate_debug_objfile_backlink)
c906108c 8765 {
077cbab2
TT
8766 struct dwarf2_per_objfile *dpo_backlink
8767 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8768
077cbab2
TT
8769 dwarf2_per_objfile->has_section_at_zero
8770 = dpo_backlink->has_section_at_zero;
8771 }
98bfdba5 8772
8566b89b 8773 expand_psymtab (objfile);
95554aad 8774
ed2dc618 8775 process_cu_includes (dwarf2_per_objfile);
c906108c 8776}
9cdd5dbd
DE
8777\f
8778/* Reading in full CUs. */
c906108c 8779
10b3939b
DJ
8780/* Add PER_CU to the queue. */
8781
8782static void
95554aad
TT
8783queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
8784 enum language pretend_language)
10b3939b 8785{
10b3939b 8786 per_cu->queued = 1;
39856def 8787 per_cu->dwarf2_per_objfile->queue.emplace (per_cu, pretend_language);
10b3939b
DJ
8788}
8789
89e63ee4
DE
8790/* If PER_CU is not yet queued, add it to the queue.
8791 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8792 dependency.
0907af0c 8793 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8794 meaning either PER_CU is already queued or it is already loaded.
8795
8796 N.B. There is an invariant here that if a CU is queued then it is loaded.
8797 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8798
8799static int
89e63ee4 8800maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
0907af0c
DE
8801 struct dwarf2_per_cu_data *per_cu,
8802 enum language pretend_language)
8803{
8804 /* We may arrive here during partial symbol reading, if we need full
8805 DIEs to process an unusual case (e.g. template arguments). Do
8806 not queue PER_CU, just tell our caller to load its DIEs. */
ed2dc618 8807 if (per_cu->dwarf2_per_objfile->reading_partial_symbols)
0907af0c
DE
8808 {
8809 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
8810 return 1;
8811 return 0;
8812 }
8813
8814 /* Mark the dependence relation so that we don't flush PER_CU
8815 too early. */
89e63ee4
DE
8816 if (dependent_cu != NULL)
8817 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8818
8819 /* If it's already on the queue, we have nothing to do. */
8820 if (per_cu->queued)
8821 return 0;
8822
8823 /* If the compilation unit is already loaded, just mark it as
8824 used. */
8825 if (per_cu->cu != NULL)
8826 {
8827 per_cu->cu->last_used = 0;
8828 return 0;
8829 }
8830
8831 /* Add it to the queue. */
8832 queue_comp_unit (per_cu, pretend_language);
8833
8834 return 1;
8835}
8836
10b3939b
DJ
8837/* Process the queue. */
8838
8839static void
ed2dc618 8840process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8841{
b4f54984 8842 if (dwarf_read_debug)
45cfd468
DE
8843 {
8844 fprintf_unfiltered (gdb_stdlog,
8845 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8846 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8847 }
8848
03dd20cc
DJ
8849 /* The queue starts out with one item, but following a DIE reference
8850 may load a new CU, adding it to the end of the queue. */
39856def 8851 while (!dwarf2_per_objfile->queue.empty ())
10b3939b 8852 {
39856def
TT
8853 dwarf2_queue_item &item = dwarf2_per_objfile->queue.front ();
8854
cc12ce38 8855 if ((dwarf2_per_objfile->using_index
39856def
TT
8856 ? !item.per_cu->v.quick->compunit_symtab
8857 : (item.per_cu->v.psymtab && !item.per_cu->v.psymtab->readin))
cc12ce38 8858 /* Skip dummy CUs. */
39856def 8859 && item.per_cu->cu != NULL)
f4dc4d17 8860 {
39856def 8861 struct dwarf2_per_cu_data *per_cu = item.per_cu;
73be47f5 8862 unsigned int debug_print_threshold;
247f5c4f 8863 char buf[100];
f4dc4d17 8864
247f5c4f 8865 if (per_cu->is_debug_types)
f4dc4d17 8866 {
247f5c4f
DE
8867 struct signatured_type *sig_type =
8868 (struct signatured_type *) per_cu;
8869
9d8780f0 8870 sprintf (buf, "TU %s at offset %s",
73be47f5 8871 hex_string (sig_type->signature),
9d8780f0 8872 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8873 /* There can be 100s of TUs.
8874 Only print them in verbose mode. */
8875 debug_print_threshold = 2;
f4dc4d17 8876 }
247f5c4f 8877 else
73be47f5 8878 {
9d8780f0
SM
8879 sprintf (buf, "CU at offset %s",
8880 sect_offset_str (per_cu->sect_off));
73be47f5
DE
8881 debug_print_threshold = 1;
8882 }
247f5c4f 8883
b4f54984 8884 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8885 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17
DE
8886
8887 if (per_cu->is_debug_types)
39856def 8888 process_full_type_unit (per_cu, item.pretend_language);
f4dc4d17 8889 else
39856def 8890 process_full_comp_unit (per_cu, item.pretend_language);
f4dc4d17 8891
b4f54984 8892 if (dwarf_read_debug >= debug_print_threshold)
247f5c4f 8893 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
f4dc4d17 8894 }
10b3939b 8895
39856def
TT
8896 item.per_cu->queued = 0;
8897 dwarf2_per_objfile->queue.pop ();
10b3939b
DJ
8898 }
8899
b4f54984 8900 if (dwarf_read_debug)
45cfd468
DE
8901 {
8902 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 8903 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 8904 }
10b3939b
DJ
8905}
8906
10b3939b
DJ
8907/* Read in full symbols for PST, and anything it depends on. */
8908
8566b89b
TT
8909void
8910dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 8911{
194d088f 8912 gdb_assert (!readin);
95554aad 8913
48993951 8914 expand_dependencies (objfile);
aaa75496 8915
b83470bf
TT
8916 dw2_do_instantiate_symtab (per_cu_data, false);
8917 gdb_assert (get_compunit_symtab () != nullptr);
10b3939b
DJ
8918}
8919
dee91e82
DE
8920/* Trivial hash function for die_info: the hash value of a DIE
8921 is its offset in .debug_info for this objfile. */
10b3939b 8922
dee91e82
DE
8923static hashval_t
8924die_hash (const void *item)
10b3939b 8925{
9a3c8263 8926 const struct die_info *die = (const struct die_info *) item;
6502dd73 8927
9c541725 8928 return to_underlying (die->sect_off);
dee91e82 8929}
63d06c5c 8930
dee91e82
DE
8931/* Trivial comparison function for die_info structures: two DIEs
8932 are equal if they have the same offset. */
98bfdba5 8933
dee91e82
DE
8934static int
8935die_eq (const void *item_lhs, const void *item_rhs)
8936{
9a3c8263
SM
8937 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
8938 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 8939
9c541725 8940 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 8941}
c906108c 8942
c0ab21c2 8943/* Load the DIEs associated with PER_CU into memory. */
c906108c 8944
dee91e82 8945static void
c0ab21c2
TT
8946load_full_comp_unit (struct dwarf2_per_cu_data *this_cu,
8947 bool skip_partial,
8948 enum language pretend_language)
dee91e82 8949{
c0ab21c2
TT
8950 gdb_assert (! this_cu->is_debug_types);
8951
6751ebae 8952 cutu_reader reader (this_cu, NULL, 1, skip_partial);
c0ab21c2
TT
8953 if (reader.dummy_p)
8954 return;
8955
8956 struct dwarf2_cu *cu = reader.cu;
8957 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 8958
dee91e82
DE
8959 gdb_assert (cu->die_hash == NULL);
8960 cu->die_hash =
8961 htab_create_alloc_ex (cu->header.length / 12,
8962 die_hash,
8963 die_eq,
8964 NULL,
8965 &cu->comp_unit_obstack,
8966 hashtab_obstack_allocate,
8967 dummy_obstack_deallocate);
e142c38c 8968
3e225074 8969 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
8970 reader.comp_unit_die->child
8971 = read_die_and_siblings (&reader, reader.info_ptr,
8972 &info_ptr, reader.comp_unit_die);
8973 cu->dies = reader.comp_unit_die;
dee91e82 8974 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
8975
8976 /* We try not to read any attributes in this function, because not
9cdd5dbd 8977 all CUs needed for references have been loaded yet, and symbol
10b3939b 8978 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
8979 or we won't be able to build types correctly.
8980 Similarly, if we do not read the producer, we can not apply
8981 producer-specific interpretation. */
c0ab21c2 8982 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
8983
8984 reader.keep ();
10b3939b
DJ
8985}
8986
3da10d80
KS
8987/* Add a DIE to the delayed physname list. */
8988
8989static void
8990add_to_method_list (struct type *type, int fnfield_index, int index,
8991 const char *name, struct die_info *die,
8992 struct dwarf2_cu *cu)
8993{
8994 struct delayed_method_info mi;
8995 mi.type = type;
8996 mi.fnfield_index = fnfield_index;
8997 mi.index = index;
8998 mi.name = name;
8999 mi.die = die;
c89b44cd 9000 cu->method_list.push_back (mi);
3da10d80
KS
9001}
9002
3693fdb3
PA
9003/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9004 "const" / "volatile". If so, decrements LEN by the length of the
9005 modifier and return true. Otherwise return false. */
9006
9007template<size_t N>
9008static bool
9009check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9010{
9011 size_t mod_len = sizeof (mod) - 1;
9012 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9013 {
9014 len -= mod_len;
9015 return true;
9016 }
9017 return false;
9018}
9019
3da10d80
KS
9020/* Compute the physnames of any methods on the CU's method list.
9021
9022 The computation of method physnames is delayed in order to avoid the
9023 (bad) condition that one of the method's formal parameters is of an as yet
9024 incomplete type. */
9025
9026static void
9027compute_delayed_physnames (struct dwarf2_cu *cu)
9028{
3693fdb3 9029 /* Only C++ delays computing physnames. */
c89b44cd 9030 if (cu->method_list.empty ())
3693fdb3
PA
9031 return;
9032 gdb_assert (cu->language == language_cplus);
9033
52941706 9034 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9035 {
1d06ead6 9036 const char *physname;
3da10d80 9037 struct fn_fieldlist *fn_flp
c89b44cd
TT
9038 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9039 physname = dwarf2_physname (mi.name, mi.die, cu);
9040 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9041 = physname ? physname : "";
3693fdb3
PA
9042
9043 /* Since there's no tag to indicate whether a method is a
9044 const/volatile overload, extract that information out of the
9045 demangled name. */
9046 if (physname != NULL)
9047 {
9048 size_t len = strlen (physname);
9049
9050 while (1)
9051 {
9052 if (physname[len] == ')') /* shortcut */
9053 break;
9054 else if (check_modifier (physname, len, " const"))
c89b44cd 9055 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9056 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9057 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9058 else
9059 break;
9060 }
9061 }
3da10d80 9062 }
c89b44cd
TT
9063
9064 /* The list is no longer needed. */
9065 cu->method_list.clear ();
3da10d80
KS
9066}
9067
a766d390
DE
9068/* Go objects should be embedded in a DW_TAG_module DIE,
9069 and it's not clear if/how imported objects will appear.
9070 To keep Go support simple until that's worked out,
9071 go back through what we've read and create something usable.
9072 We could do this while processing each DIE, and feels kinda cleaner,
9073 but that way is more invasive.
9074 This is to, for example, allow the user to type "p var" or "b main"
9075 without having to specify the package name, and allow lookups
9076 of module.object to work in contexts that use the expression
9077 parser. */
9078
9079static void
9080fixup_go_packaging (struct dwarf2_cu *cu)
9081{
421d1616 9082 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9083 struct pending *list;
9084 int i;
9085
c24bdb02 9086 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9087 list != NULL;
9088 list = list->next)
a766d390
DE
9089 {
9090 for (i = 0; i < list->nsyms; ++i)
9091 {
9092 struct symbol *sym = list->symbol[i];
9093
c1b5c1eb 9094 if (sym->language () == language_go
a766d390
DE
9095 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9096 {
421d1616
TT
9097 gdb::unique_xmalloc_ptr<char> this_package_name
9098 (go_symbol_package_name (sym));
a766d390
DE
9099
9100 if (this_package_name == NULL)
9101 continue;
9102 if (package_name == NULL)
421d1616 9103 package_name = std::move (this_package_name);
a766d390
DE
9104 else
9105 {
518817b3
SM
9106 struct objfile *objfile
9107 = cu->per_cu->dwarf2_per_objfile->objfile;
421d1616 9108 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9109 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9110 (symbol_symtab (sym) != NULL
9111 ? symtab_to_filename_for_display
9112 (symbol_symtab (sym))
e3b94546 9113 : objfile_name (objfile)),
421d1616 9114 this_package_name.get (), package_name.get ());
a766d390
DE
9115 }
9116 }
9117 }
9118 }
9119
9120 if (package_name != NULL)
9121 {
518817b3 9122 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 9123 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9124 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9125 saved_package_name);
a766d390
DE
9126 struct symbol *sym;
9127
e623cf5d 9128 sym = allocate_symbol (objfile);
d3ecddab 9129 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9130 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9131 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9132 e.g., "main" finds the "main" module and not C's main(). */
9133 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9134 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9135 SYMBOL_TYPE (sym) = type;
9136
c24bdb02 9137 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9138 }
9139}
9140
c9317f21
TT
9141/* Allocate a fully-qualified name consisting of the two parts on the
9142 obstack. */
9143
9144static const char *
9145rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9146{
9147 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9148}
9149
9c6a1327
TT
9150/* A helper that allocates a variant part to attach to a Rust enum
9151 type. OBSTACK is where the results should be allocated. TYPE is
9152 the type we're processing. DISCRIMINANT_INDEX is the index of the
9153 discriminant. It must be the index of one of the fields of TYPE.
9154 DEFAULT_INDEX is the index of the default field; or -1 if there is
9155 no default. RANGES is indexed by "effective" field number (the
9156 field index, but omitting the discriminant and default fields) and
9157 must hold the discriminant values used by the variants. Note that
9158 RANGES must have a lifetime at least as long as OBSTACK -- either
9159 already allocated on it, or static. */
c9317f21 9160
9c6a1327
TT
9161static void
9162alloc_rust_variant (struct obstack *obstack, struct type *type,
9163 int discriminant_index, int default_index,
9164 gdb::array_view<discriminant_range> ranges)
9165{
9166 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9167 must be handled by the caller. */
9168 gdb_assert (discriminant_index >= 0
9169 && discriminant_index < TYPE_NFIELDS (type));
c9317f21 9170 gdb_assert (default_index == -1
c7b15a66 9171 || (default_index >= 0 && default_index < TYPE_NFIELDS (type)));
c9317f21 9172
9c6a1327
TT
9173 /* We have one variant for each non-discriminant field. */
9174 int n_variants = TYPE_NFIELDS (type) - 1;
c9317f21 9175
9c6a1327
TT
9176 variant *variants = new (obstack) variant[n_variants];
9177 int var_idx = 0;
9178 int range_idx = 0;
9179 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9180 {
9181 if (i == discriminant_index)
9182 continue;
c9317f21 9183
9c6a1327
TT
9184 variants[var_idx].first_field = i;
9185 variants[var_idx].last_field = i + 1;
9186
9187 /* The default field does not need a range, but other fields do.
9188 We skipped the discriminant above. */
9189 if (i != default_index)
9190 {
9191 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9192 ++range_idx;
9193 }
c9317f21 9194
9c6a1327
TT
9195 ++var_idx;
9196 }
9197
9198 gdb_assert (range_idx == ranges.size ());
9199 gdb_assert (var_idx == n_variants);
9200
9201 variant_part *part = new (obstack) variant_part;
9202 part->discriminant_index = discriminant_index;
9203 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9204 discriminant_index));
9205 part->variants = gdb::array_view<variant> (variants, n_variants);
9206
9207 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9208 gdb::array_view<variant_part> *prop_value
9209 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9210
9c6a1327
TT
9211 struct dynamic_prop prop;
9212 prop.kind = PROP_VARIANT_PARTS;
9213 prop.data.variant_parts = prop_value;
9214
9215 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
c9317f21
TT
9216}
9217
9218/* Some versions of rustc emitted enums in an unusual way.
9219
9220 Ordinary enums were emitted as unions. The first element of each
9221 structure in the union was named "RUST$ENUM$DISR". This element
9222 held the discriminant.
9223
9224 These versions of Rust also implemented the "non-zero"
9225 optimization. When the enum had two values, and one is empty and
9226 the other holds a pointer that cannot be zero, the pointer is used
9227 as the discriminant, with a zero value meaning the empty variant.
9228 Here, the union's first member is of the form
9229 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9230 where the fieldnos are the indices of the fields that should be
9231 traversed in order to find the field (which may be several fields deep)
9232 and the variantname is the name of the variant of the case when the
9233 field is zero.
9234
9235 This function recognizes whether TYPE is of one of these forms,
9236 and, if so, smashes it to be a variant type. */
9237
9238static void
9239quirk_rust_enum (struct type *type, struct objfile *objfile)
9240{
9241 gdb_assert (TYPE_CODE (type) == TYPE_CODE_UNION);
9242
9243 /* We don't need to deal with empty enums. */
9244 if (TYPE_NFIELDS (type) == 0)
9245 return;
9246
9247#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
9248 if (TYPE_NFIELDS (type) == 1
9249 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9250 {
9251 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9252
9253 /* Decode the field name to find the offset of the
9254 discriminant. */
9255 ULONGEST bit_offset = 0;
9256 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9257 while (name[0] >= '0' && name[0] <= '9')
9258 {
9259 char *tail;
9260 unsigned long index = strtoul (name, &tail, 10);
9261 name = tail;
9262 if (*name != '$'
9263 || index >= TYPE_NFIELDS (field_type)
9264 || (TYPE_FIELD_LOC_KIND (field_type, index)
9265 != FIELD_LOC_KIND_BITPOS))
9266 {
b98664d3 9267 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9268 "[in module %s]"),
9269 TYPE_FIELD_NAME (type, 0),
9270 objfile_name (objfile));
9271 return;
9272 }
9273 ++name;
9274
9275 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9276 field_type = TYPE_FIELD_TYPE (field_type, index);
9277 }
9278
9c6a1327
TT
9279 /* Smash this type to be a structure type. We have to do this
9280 because the type has already been recorded. */
9281 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9282 TYPE_NFIELDS (type) = 3;
9283 /* Save the field we care about. */
9284 struct field saved_field = TYPE_FIELD (type, 0);
9285 TYPE_FIELDS (type)
c9317f21 9286 = (struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field));
c9317f21 9287
9c6a1327
TT
9288 /* Put the discriminant at index 0. */
9289 TYPE_FIELD_TYPE (type, 0) = field_type;
9290 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9291 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
9292 SET_FIELD_BITPOS (TYPE_FIELD (type, 0), bit_offset);
c9317f21
TT
9293
9294 /* The order of fields doesn't really matter, so put the real
9295 field at index 1 and the data-less field at index 2. */
9c6a1327
TT
9296 TYPE_FIELD (type, 1) = saved_field;
9297 TYPE_FIELD_NAME (type, 1)
9298 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, 1)));
9299 TYPE_NAME (TYPE_FIELD_TYPE (type, 1))
c9317f21 9300 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9c6a1327 9301 TYPE_FIELD_NAME (type, 1));
c9317f21
TT
9302
9303 const char *dataless_name
9304 = rust_fully_qualify (&objfile->objfile_obstack, TYPE_NAME (type),
9305 name);
9306 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9307 dataless_name);
9c6a1327 9308 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9309 /* NAME points into the original discriminant name, which
9310 already has the correct lifetime. */
9c6a1327
TT
9311 TYPE_FIELD_NAME (type, 2) = name;
9312 SET_FIELD_BITPOS (TYPE_FIELD (type, 2), 0);
c9317f21 9313
9c6a1327
TT
9314 /* Indicate that this is a variant type. */
9315 static discriminant_range ranges[1] = { { 0, 0 } };
9316 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9317 }
77c2dba3
TT
9318 /* A union with a single anonymous field is probably an old-style
9319 univariant enum. */
9320 else if (TYPE_NFIELDS (type) == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9321 {
c9317f21
TT
9322 /* Smash this type to be a structure type. We have to do this
9323 because the type has already been recorded. */
9324 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9325
9c6a1327 9326 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21
TT
9327 const char *variant_name
9328 = rust_last_path_segment (TYPE_NAME (field_type));
9c6a1327 9329 TYPE_FIELD_NAME (type, 0) = variant_name;
c9317f21
TT
9330 TYPE_NAME (field_type)
9331 = rust_fully_qualify (&objfile->objfile_obstack,
c7b15a66 9332 TYPE_NAME (type), variant_name);
c9317f21
TT
9333 }
9334 else
9335 {
9336 struct type *disr_type = nullptr;
9337 for (int i = 0; i < TYPE_NFIELDS (type); ++i)
9338 {
9339 disr_type = TYPE_FIELD_TYPE (type, i);
9340
a037790e
TT
9341 if (TYPE_CODE (disr_type) != TYPE_CODE_STRUCT)
9342 {
9343 /* All fields of a true enum will be structs. */
9344 return;
9345 }
9346 else if (TYPE_NFIELDS (disr_type) == 0)
c9317f21
TT
9347 {
9348 /* Could be data-less variant, so keep going. */
a037790e 9349 disr_type = nullptr;
c9317f21
TT
9350 }
9351 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9352 "RUST$ENUM$DISR") != 0)
9353 {
9354 /* Not a Rust enum. */
9355 return;
9356 }
9357 else
9358 {
9359 /* Found one. */
9360 break;
9361 }
9362 }
9363
9364 /* If we got here without a discriminant, then it's probably
9365 just a union. */
9366 if (disr_type == nullptr)
9367 return;
9368
9369 /* Smash this type to be a structure type. We have to do this
9370 because the type has already been recorded. */
9371 TYPE_CODE (type) = TYPE_CODE_STRUCT;
9372
9c6a1327 9373 /* Make space for the discriminant field. */
c9317f21 9374 struct field *disr_field = &TYPE_FIELD (disr_type, 0);
9c6a1327
TT
9375 field *new_fields
9376 = (struct field *) TYPE_ZALLOC (type, (TYPE_NFIELDS (type)
9377 * sizeof (struct field)));
9378 memcpy (new_fields + 1, TYPE_FIELDS (type),
c9317f21 9379 TYPE_NFIELDS (type) * sizeof (struct field));
9c6a1327
TT
9380 TYPE_FIELDS (type) = new_fields;
9381 TYPE_NFIELDS (type) = TYPE_NFIELDS (type) + 1;
c9317f21
TT
9382
9383 /* Install the discriminant at index 0 in the union. */
9c6a1327
TT
9384 TYPE_FIELD (type, 0) = *disr_field;
9385 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9386 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9387
9388 /* We need a way to find the correct discriminant given a
9389 variant name. For convenience we build a map here. */
9390 struct type *enum_type = FIELD_TYPE (*disr_field);
9391 std::unordered_map<std::string, ULONGEST> discriminant_map;
9392 for (int i = 0; i < TYPE_NFIELDS (enum_type); ++i)
9393 {
9394 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9395 {
9396 const char *name
9397 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9398 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9399 }
9400 }
9401
9c6a1327
TT
9402 int n_fields = TYPE_NFIELDS (type);
9403 /* We don't need a range entry for the discriminant, but we do
9404 need one for every other field, as there is no default
9405 variant. */
9406 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9407 discriminant_range,
9408 n_fields - 1);
c9317f21
TT
9409 /* Skip the discriminant here. */
9410 for (int i = 1; i < n_fields; ++i)
9411 {
9412 /* Find the final word in the name of this variant's type.
9413 That name can be used to look up the correct
9414 discriminant. */
9415 const char *variant_name
9c6a1327 9416 = rust_last_path_segment (TYPE_NAME (TYPE_FIELD_TYPE (type, i)));
c9317f21
TT
9417
9418 auto iter = discriminant_map.find (variant_name);
9419 if (iter != discriminant_map.end ())
9c6a1327
TT
9420 {
9421 ranges[i].low = iter->second;
9422 ranges[i].high = iter->second;
9423 }
c9317f21 9424
bedda9ac 9425 /* Remove the discriminant field, if it exists. */
9c6a1327 9426 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
bedda9ac
TT
9427 if (TYPE_NFIELDS (sub_type) > 0)
9428 {
9429 --TYPE_NFIELDS (sub_type);
9430 ++TYPE_FIELDS (sub_type);
9431 }
9c6a1327 9432 TYPE_FIELD_NAME (type, i) = variant_name;
c9317f21
TT
9433 TYPE_NAME (sub_type)
9434 = rust_fully_qualify (&objfile->objfile_obstack,
9435 TYPE_NAME (type), variant_name);
9436 }
9c6a1327
TT
9437
9438 /* Indicate that this is a variant type. */
9439 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9440 gdb::array_view<discriminant_range> (ranges,
9441 n_fields - 1));
c9317f21
TT
9442 }
9443}
9444
9445/* Rewrite some Rust unions to be structures with variants parts. */
9446
9447static void
9448rust_union_quirks (struct dwarf2_cu *cu)
9449{
9450 gdb_assert (cu->language == language_rust);
52941706
SM
9451 for (type *type_ : cu->rust_unions)
9452 quirk_rust_enum (type_, cu->per_cu->dwarf2_per_objfile->objfile);
2d79090e
TT
9453 /* We don't need this any more. */
9454 cu->rust_unions.clear ();
c9317f21
TT
9455}
9456
95554aad
TT
9457/* Return the symtab for PER_CU. This works properly regardless of
9458 whether we're using the index or psymtabs. */
9459
43f3e411
DE
9460static struct compunit_symtab *
9461get_compunit_symtab (struct dwarf2_per_cu_data *per_cu)
95554aad 9462{
ed2dc618 9463 return (per_cu->dwarf2_per_objfile->using_index
43f3e411
DE
9464 ? per_cu->v.quick->compunit_symtab
9465 : per_cu->v.psymtab->compunit_symtab);
95554aad
TT
9466}
9467
9468/* A helper function for computing the list of all symbol tables
9469 included by PER_CU. */
9470
9471static void
4c39bc03 9472recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9473 htab_t all_children, htab_t all_type_symtabs,
f9125b6c 9474 struct dwarf2_per_cu_data *per_cu,
43f3e411 9475 struct compunit_symtab *immediate_parent)
95554aad
TT
9476{
9477 void **slot;
43f3e411 9478 struct compunit_symtab *cust;
95554aad
TT
9479
9480 slot = htab_find_slot (all_children, per_cu, INSERT);
9481 if (*slot != NULL)
9482 {
9483 /* This inclusion and its children have been processed. */
9484 return;
9485 }
9486
9487 *slot = per_cu;
9488 /* Only add a CU if it has a symbol table. */
43f3e411
DE
9489 cust = get_compunit_symtab (per_cu);
9490 if (cust != NULL)
ec94af83
DE
9491 {
9492 /* If this is a type unit only add its symbol table if we haven't
9493 seen it yet (type unit per_cu's can share symtabs). */
9494 if (per_cu->is_debug_types)
9495 {
43f3e411 9496 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9497 if (*slot == NULL)
9498 {
43f3e411 9499 *slot = cust;
4c39bc03 9500 result->push_back (cust);
43f3e411
DE
9501 if (cust->user == NULL)
9502 cust->user = immediate_parent;
ec94af83
DE
9503 }
9504 }
9505 else
f9125b6c 9506 {
4c39bc03 9507 result->push_back (cust);
43f3e411
DE
9508 if (cust->user == NULL)
9509 cust->user = immediate_parent;
f9125b6c 9510 }
ec94af83 9511 }
95554aad 9512
ae640021
AB
9513 if (!per_cu->imported_symtabs_empty ())
9514 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9515 {
9516 recursively_compute_inclusions (result, all_children,
9517 all_type_symtabs, ptr, cust);
9518 }
95554aad
TT
9519}
9520
43f3e411 9521/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9522 PER_CU. */
9523
9524static void
43f3e411 9525compute_compunit_symtab_includes (struct dwarf2_per_cu_data *per_cu)
95554aad 9526{
f4dc4d17
DE
9527 gdb_assert (! per_cu->is_debug_types);
9528
ae640021 9529 if (!per_cu->imported_symtabs_empty ())
95554aad 9530 {
ae640021 9531 int len;
4c39bc03 9532 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9533 htab_t all_children, all_type_symtabs;
43f3e411 9534 struct compunit_symtab *cust = get_compunit_symtab (per_cu);
95554aad
TT
9535
9536 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9537 if (cust == NULL)
95554aad
TT
9538 return;
9539
9540 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9541 NULL, xcalloc, xfree);
ec94af83
DE
9542 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9543 NULL, xcalloc, xfree);
95554aad 9544
ae640021 9545 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9546 {
9547 recursively_compute_inclusions (&result_symtabs, all_children,
ae640021 9548 all_type_symtabs, ptr, cust);
ec94af83 9549 }
95554aad 9550
ec94af83 9551 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9552 len = result_symtabs.size ();
43f3e411 9553 cust->includes
ed2dc618 9554 = XOBNEWVEC (&per_cu->dwarf2_per_objfile->objfile->objfile_obstack,
8d749320 9555 struct compunit_symtab *, len + 1);
4c39bc03
TT
9556 memcpy (cust->includes, result_symtabs.data (),
9557 len * sizeof (compunit_symtab *));
43f3e411 9558 cust->includes[len] = NULL;
95554aad 9559
95554aad 9560 htab_delete (all_children);
ec94af83 9561 htab_delete (all_type_symtabs);
95554aad
TT
9562 }
9563}
9564
9565/* Compute the 'includes' field for the symtabs of all the CUs we just
9566 read. */
9567
9568static void
ed2dc618 9569process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9570{
71b73764 9571 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->just_read_cus)
f4dc4d17
DE
9572 {
9573 if (! iter->is_debug_types)
43f3e411 9574 compute_compunit_symtab_includes (iter);
f4dc4d17 9575 }
95554aad 9576
c5d0225d 9577 dwarf2_per_objfile->just_read_cus.clear ();
95554aad
TT
9578}
9579
9cdd5dbd 9580/* Generate full symbol information for PER_CU, whose DIEs have
10b3939b
DJ
9581 already been loaded into memory. */
9582
9583static void
95554aad
TT
9584process_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
9585 enum language pretend_language)
10b3939b 9586{
10b3939b 9587 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9588 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9589 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9590 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9591 CORE_ADDR lowpc, highpc;
43f3e411 9592 struct compunit_symtab *cust;
10b3939b 9593 CORE_ADDR baseaddr;
4359dff1 9594 struct block *static_block;
3e29f34a 9595 CORE_ADDR addr;
10b3939b 9596
b3b3bada 9597 baseaddr = objfile->text_section_offset ();
10b3939b 9598
c89b44cd
TT
9599 /* Clear the list here in case something was left over. */
9600 cu->method_list.clear ();
10b3939b 9601
95554aad
TT
9602 cu->language = pretend_language;
9603 cu->language_defn = language_def (cu->language);
9604
c906108c 9605 /* Do line number decoding in read_file_scope () */
10b3939b 9606 process_die (cu->dies, cu);
c906108c 9607
a766d390
DE
9608 /* For now fudge the Go package. */
9609 if (cu->language == language_go)
9610 fixup_go_packaging (cu);
9611
5f48f8f3 9612 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9613 should be complete, and it should now be safe to compute all of the
9614 physnames. */
9615 compute_delayed_physnames (cu);
3da10d80 9616
c9317f21
TT
9617 if (cu->language == language_rust)
9618 rust_union_quirks (cu);
9619
fae299cd
DC
9620 /* Some compilers don't define a DW_AT_high_pc attribute for the
9621 compilation unit. If the DW_AT_high_pc is missing, synthesize
9622 it, by scanning the DIE's below the compilation unit. */
10b3939b 9623 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9624
3e29f34a 9625 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9626 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9627
9628 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9629 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9630 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9631 addrmap to help ensure it has an accurate map of pc values belonging to
9632 this comp unit. */
9633 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9634
c24bdb02 9635 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9636 SECT_OFF_TEXT (objfile),
9637 0);
c906108c 9638
43f3e411 9639 if (cust != NULL)
c906108c 9640 {
df15bd07 9641 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9642
8be455d7
JK
9643 /* Set symtab language to language from DW_AT_language. If the
9644 compilation is from a C file generated by language preprocessors, do
9645 not set the language if it was already deduced by start_subfile. */
43f3e411 9646 if (!(cu->language == language_c
40e3ad0e 9647 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9648 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9649
9650 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9651 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9652 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9653 there were bugs in prologue debug info, fixed later in GCC-4.5
9654 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9655
9656 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9657 needed, it would be wrong due to missing DW_AT_producer there.
9658
9659 Still one can confuse GDB by using non-standard GCC compilation
9660 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9661 */
ab260dad 9662 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9663 cust->locations_valid = 1;
e0d00bc7
JK
9664
9665 if (gcc_4_minor >= 5)
43f3e411 9666 cust->epilogue_unwind_valid = 1;
96408a79 9667
43f3e411 9668 cust->call_site_htab = cu->call_site_htab;
c906108c 9669 }
9291a0cd
TT
9670
9671 if (dwarf2_per_objfile->using_index)
43f3e411 9672 per_cu->v.quick->compunit_symtab = cust;
9291a0cd
TT
9673 else
9674 {
891813be 9675 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9676 pst->compunit_symtab = cust;
6d94535f 9677 pst->readin = true;
9291a0cd 9678 }
c906108c 9679
95554aad 9680 /* Push it for inclusion processing later. */
c5d0225d 9681 dwarf2_per_objfile->just_read_cus.push_back (per_cu);
804d2729
TT
9682
9683 /* Not needed any more. */
c24bdb02 9684 cu->reset_builder ();
f4dc4d17 9685}
45cfd468 9686
f4dc4d17
DE
9687/* Generate full symbol information for type unit PER_CU, whose DIEs have
9688 already been loaded into memory. */
9689
9690static void
9691process_full_type_unit (struct dwarf2_per_cu_data *per_cu,
9692 enum language pretend_language)
9693{
9694 struct dwarf2_cu *cu = per_cu->cu;
ed2dc618
SM
9695 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
9696 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9697 struct compunit_symtab *cust;
0186c6a7
DE
9698 struct signatured_type *sig_type;
9699
9700 gdb_assert (per_cu->is_debug_types);
9701 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 9702
c89b44cd
TT
9703 /* Clear the list here in case something was left over. */
9704 cu->method_list.clear ();
f4dc4d17 9705
f4dc4d17
DE
9706 cu->language = pretend_language;
9707 cu->language_defn = language_def (cu->language);
9708
9709 /* The symbol tables are set up in read_type_unit_scope. */
9710 process_die (cu->dies, cu);
9711
9712 /* For now fudge the Go package. */
9713 if (cu->language == language_go)
9714 fixup_go_packaging (cu);
9715
5f48f8f3 9716 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9717 should be complete, and it should now be safe to compute all of the
9718 physnames. */
9719 compute_delayed_physnames (cu);
f4dc4d17 9720
c9317f21
TT
9721 if (cu->language == language_rust)
9722 rust_union_quirks (cu);
9723
f4dc4d17
DE
9724 /* TUs share symbol tables.
9725 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9726 of it with end_expandable_symtab. Otherwise, complete the addition of
9727 this TU's symbols to the existing symtab. */
43f3e411 9728 if (sig_type->type_unit_group->compunit_symtab == NULL)
45cfd468 9729 {
c24bdb02
KS
9730 buildsym_compunit *builder = cu->get_builder ();
9731 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
43f3e411 9732 sig_type->type_unit_group->compunit_symtab = cust;
f4dc4d17 9733
43f3e411 9734 if (cust != NULL)
f4dc4d17
DE
9735 {
9736 /* Set symtab language to language from DW_AT_language. If the
9737 compilation is from a C file generated by language preprocessors,
9738 do not set the language if it was already deduced by
9739 start_subfile. */
43f3e411
DE
9740 if (!(cu->language == language_c
9741 && COMPUNIT_FILETABS (cust)->language != language_c))
9742 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9743 }
9744 }
9745 else
9746 {
c24bdb02 9747 cu->get_builder ()->augment_type_symtab ();
43f3e411 9748 cust = sig_type->type_unit_group->compunit_symtab;
f4dc4d17
DE
9749 }
9750
9751 if (dwarf2_per_objfile->using_index)
43f3e411 9752 per_cu->v.quick->compunit_symtab = cust;
f4dc4d17
DE
9753 else
9754 {
891813be 9755 dwarf2_psymtab *pst = per_cu->v.psymtab;
43f3e411 9756 pst->compunit_symtab = cust;
6d94535f 9757 pst->readin = true;
45cfd468 9758 }
804d2729
TT
9759
9760 /* Not needed any more. */
c24bdb02 9761 cu->reset_builder ();
c906108c
SS
9762}
9763
95554aad
TT
9764/* Process an imported unit DIE. */
9765
9766static void
9767process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9768{
9769 struct attribute *attr;
9770
f4dc4d17
DE
9771 /* For now we don't handle imported units in type units. */
9772 if (cu->per_cu->is_debug_types)
9773 {
9774 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9775 " supported in type units [in module %s]"),
518817b3 9776 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
f4dc4d17
DE
9777 }
9778
95554aad
TT
9779 attr = dwarf2_attr (die, DW_AT_import, cu);
9780 if (attr != NULL)
9781 {
0826b30a 9782 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725
PA
9783 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
9784 dwarf2_per_cu_data *per_cu
e3b94546 9785 = dwarf2_find_containing_comp_unit (sect_off, is_dwz,
518817b3 9786 cu->per_cu->dwarf2_per_objfile);
95554aad 9787
58990295
TV
9788 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9789 into another compilation unit, at root level. Regard this as a hint,
9790 and ignore it. */
9791 if (die->parent && die->parent->parent == NULL
9792 && per_cu->unit_type == DW_UT_compile
9793 && per_cu->lang == language_cplus)
9794 return;
9795
69d751e3 9796 /* If necessary, add it to the queue and load its DIEs. */
95554aad 9797 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 9798 load_full_comp_unit (per_cu, false, cu->language);
95554aad 9799
ae640021 9800 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9801 }
9802}
9803
4c8aa72d
PA
9804/* RAII object that represents a process_die scope: i.e.,
9805 starts/finishes processing a DIE. */
9806class process_die_scope
adde2bff 9807{
4c8aa72d
PA
9808public:
9809 process_die_scope (die_info *die, dwarf2_cu *cu)
9810 : m_die (die), m_cu (cu)
9811 {
9812 /* We should only be processing DIEs not already in process. */
9813 gdb_assert (!m_die->in_process);
9814 m_die->in_process = true;
9815 }
8c3cb9fa 9816
4c8aa72d
PA
9817 ~process_die_scope ()
9818 {
9819 m_die->in_process = false;
9820
9821 /* If we're done processing the DIE for the CU that owns the line
9822 header, we don't need the line header anymore. */
9823 if (m_cu->line_header_die_owner == m_die)
9824 {
9825 delete m_cu->line_header;
9826 m_cu->line_header = NULL;
9827 m_cu->line_header_die_owner = NULL;
9828 }
9829 }
9830
9831private:
9832 die_info *m_die;
9833 dwarf2_cu *m_cu;
9834};
adde2bff 9835
c906108c
SS
9836/* Process a die and its children. */
9837
9838static void
e7c27a73 9839process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9840{
4c8aa72d 9841 process_die_scope scope (die, cu);
adde2bff 9842
c906108c
SS
9843 switch (die->tag)
9844 {
9845 case DW_TAG_padding:
9846 break;
9847 case DW_TAG_compile_unit:
95554aad 9848 case DW_TAG_partial_unit:
e7c27a73 9849 read_file_scope (die, cu);
c906108c 9850 break;
348e048f
DE
9851 case DW_TAG_type_unit:
9852 read_type_unit_scope (die, cu);
9853 break;
c906108c 9854 case DW_TAG_subprogram:
0a4b0913
AB
9855 /* Nested subprograms in Fortran get a prefix. */
9856 if (cu->language == language_fortran
9857 && die->parent != NULL
9858 && die->parent->tag == DW_TAG_subprogram)
9859 cu->processing_has_namespace_info = true;
9860 /* Fall through. */
c906108c 9861 case DW_TAG_inlined_subroutine:
edb3359d 9862 read_func_scope (die, cu);
c906108c
SS
9863 break;
9864 case DW_TAG_lexical_block:
14898363
L
9865 case DW_TAG_try_block:
9866 case DW_TAG_catch_block:
e7c27a73 9867 read_lexical_block_scope (die, cu);
c906108c 9868 break;
216f72a1 9869 case DW_TAG_call_site:
96408a79
SA
9870 case DW_TAG_GNU_call_site:
9871 read_call_site_scope (die, cu);
9872 break;
c906108c 9873 case DW_TAG_class_type:
680b30c7 9874 case DW_TAG_interface_type:
c906108c
SS
9875 case DW_TAG_structure_type:
9876 case DW_TAG_union_type:
134d01f1 9877 process_structure_scope (die, cu);
c906108c
SS
9878 break;
9879 case DW_TAG_enumeration_type:
134d01f1 9880 process_enumeration_scope (die, cu);
c906108c 9881 break;
134d01f1 9882
f792889a
DJ
9883 /* These dies have a type, but processing them does not create
9884 a symbol or recurse to process the children. Therefore we can
9885 read them on-demand through read_type_die. */
c906108c 9886 case DW_TAG_subroutine_type:
72019c9c 9887 case DW_TAG_set_type:
c906108c 9888 case DW_TAG_array_type:
c906108c 9889 case DW_TAG_pointer_type:
c906108c 9890 case DW_TAG_ptr_to_member_type:
c906108c 9891 case DW_TAG_reference_type:
4297a3f0 9892 case DW_TAG_rvalue_reference_type:
c906108c 9893 case DW_TAG_string_type:
c906108c 9894 break;
134d01f1 9895
c906108c 9896 case DW_TAG_base_type:
a02abb62 9897 case DW_TAG_subrange_type:
cb249c71 9898 case DW_TAG_typedef:
134d01f1
DJ
9899 /* Add a typedef symbol for the type definition, if it has a
9900 DW_AT_name. */
f792889a 9901 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 9902 break;
c906108c 9903 case DW_TAG_common_block:
e7c27a73 9904 read_common_block (die, cu);
c906108c
SS
9905 break;
9906 case DW_TAG_common_inclusion:
9907 break;
d9fa45fe 9908 case DW_TAG_namespace:
9068261f 9909 cu->processing_has_namespace_info = true;
e7c27a73 9910 read_namespace (die, cu);
d9fa45fe 9911 break;
5d7cb8df 9912 case DW_TAG_module:
9068261f 9913 cu->processing_has_namespace_info = true;
5d7cb8df
JK
9914 read_module (die, cu);
9915 break;
d9fa45fe 9916 case DW_TAG_imported_declaration:
9068261f 9917 cu->processing_has_namespace_info = true;
74921315
KS
9918 if (read_namespace_alias (die, cu))
9919 break;
86a73007
TT
9920 /* The declaration is not a global namespace alias. */
9921 /* Fall through. */
d9fa45fe 9922 case DW_TAG_imported_module:
9068261f 9923 cu->processing_has_namespace_info = true;
27aa8d6a
SW
9924 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
9925 || cu->language != language_fortran))
b98664d3 9926 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
9927 dwarf_tag_name (die->tag));
9928 read_import_statement (die, cu);
d9fa45fe 9929 break;
95554aad
TT
9930
9931 case DW_TAG_imported_unit:
9932 process_imported_unit_die (die, cu);
9933 break;
9934
71a3c369
TT
9935 case DW_TAG_variable:
9936 read_variable (die, cu);
9937 break;
9938
c906108c 9939 default:
e7c27a73 9940 new_symbol (die, NULL, cu);
c906108c
SS
9941 break;
9942 }
9943}
ca69b9e6
DE
9944\f
9945/* DWARF name computation. */
c906108c 9946
94af9270
KS
9947/* A helper function for dwarf2_compute_name which determines whether DIE
9948 needs to have the name of the scope prepended to the name listed in the
9949 die. */
9950
9951static int
9952die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
9953{
1c809c68
TT
9954 struct attribute *attr;
9955
94af9270
KS
9956 switch (die->tag)
9957 {
9958 case DW_TAG_namespace:
9959 case DW_TAG_typedef:
9960 case DW_TAG_class_type:
9961 case DW_TAG_interface_type:
9962 case DW_TAG_structure_type:
9963 case DW_TAG_union_type:
9964 case DW_TAG_enumeration_type:
9965 case DW_TAG_enumerator:
9966 case DW_TAG_subprogram:
08a76f8a 9967 case DW_TAG_inlined_subroutine:
94af9270 9968 case DW_TAG_member:
74921315 9969 case DW_TAG_imported_declaration:
94af9270
KS
9970 return 1;
9971
9972 case DW_TAG_variable:
c2b0a229 9973 case DW_TAG_constant:
94af9270
KS
9974 /* We only need to prefix "globally" visible variables. These include
9975 any variable marked with DW_AT_external or any variable that
9976 lives in a namespace. [Variables in anonymous namespaces
9977 require prefixing, but they are not DW_AT_external.] */
9978
9979 if (dwarf2_attr (die, DW_AT_specification, cu))
9980 {
9981 struct dwarf2_cu *spec_cu = cu;
9a619af0 9982
94af9270
KS
9983 return die_needs_namespace (die_specification (die, &spec_cu),
9984 spec_cu);
9985 }
9986
1c809c68 9987 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
9988 if (attr == NULL && die->parent->tag != DW_TAG_namespace
9989 && die->parent->tag != DW_TAG_module)
1c809c68
TT
9990 return 0;
9991 /* A variable in a lexical block of some kind does not need a
9992 namespace, even though in C++ such variables may be external
9993 and have a mangled name. */
9994 if (die->parent->tag == DW_TAG_lexical_block
9995 || die->parent->tag == DW_TAG_try_block
1054b214
TT
9996 || die->parent->tag == DW_TAG_catch_block
9997 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
9998 return 0;
9999 return 1;
94af9270
KS
10000
10001 default:
10002 return 0;
10003 }
10004}
10005
73b9be8b
KS
10006/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10007 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10008 defined for the given DIE. */
10009
10010static struct attribute *
10011dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10012{
10013 struct attribute *attr;
10014
10015 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10016 if (attr == NULL)
10017 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10018
10019 return attr;
10020}
10021
10022/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10023 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10024 defined for the given DIE. */
10025
10026static const char *
10027dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10028{
10029 const char *linkage_name;
10030
10031 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10032 if (linkage_name == NULL)
10033 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10034
787de330
TT
10035 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10036 See https://github.com/rust-lang/rust/issues/32925. */
10037 if (cu->language == language_rust && linkage_name != NULL
10038 && strchr (linkage_name, '{') != NULL)
10039 linkage_name = NULL;
10040
73b9be8b
KS
10041 return linkage_name;
10042}
10043
94af9270 10044/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10045 compute the physname for the object, which include a method's:
9c37b5ae 10046 - formal parameters (C++),
a766d390 10047 - receiver type (Go),
a766d390
DE
10048
10049 The term "physname" is a bit confusing.
10050 For C++, for example, it is the demangled name.
10051 For Go, for example, it's the mangled name.
94af9270 10052
af6b7be1
JB
10053 For Ada, return the DIE's linkage name rather than the fully qualified
10054 name. PHYSNAME is ignored..
10055
94af9270
KS
10056 The result is allocated on the objfile_obstack and canonicalized. */
10057
10058static const char *
15d034d0
TT
10059dwarf2_compute_name (const char *name,
10060 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10061 int physname)
10062{
518817b3 10063 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
bb5ed363 10064
94af9270
KS
10065 if (name == NULL)
10066 name = dwarf2_name (die, cu);
10067
2ee7123e
DE
10068 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10069 but otherwise compute it by typename_concat inside GDB.
10070 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10071 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10072 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10073 will set the demangled name to the result of dwarf2_full_name, and it is
10074 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10075 if (cu->language == language_ada
10076 || (cu->language == language_fortran && physname))
10077 {
10078 /* For Ada unit, we prefer the linkage name over the name, as
10079 the former contains the exported name, which the user expects
10080 to be able to reference. Ideally, we want the user to be able
10081 to reference this entity using either natural or linkage name,
10082 but we haven't started looking at this enhancement yet. */
73b9be8b 10083 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10084
2ee7123e
DE
10085 if (linkage_name != NULL)
10086 return linkage_name;
f55ee35c
JK
10087 }
10088
94af9270
KS
10089 /* These are the only languages we know how to qualify names in. */
10090 if (name != NULL
9c37b5ae 10091 && (cu->language == language_cplus
c44af4eb
TT
10092 || cu->language == language_fortran || cu->language == language_d
10093 || cu->language == language_rust))
94af9270
KS
10094 {
10095 if (die_needs_namespace (die, cu))
10096 {
0d5cff50 10097 const char *prefix;
34a68019 10098 const char *canonical_name = NULL;
94af9270 10099
d7e74731
PA
10100 string_file buf;
10101
94af9270 10102 prefix = determine_prefix (die, cu);
94af9270
KS
10103 if (*prefix != '\0')
10104 {
43816ebc
TT
10105 gdb::unique_xmalloc_ptr<char> prefixed_name
10106 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10107
43816ebc 10108 buf.puts (prefixed_name.get ());
94af9270
KS
10109 }
10110 else
d7e74731 10111 buf.puts (name);
94af9270 10112
98bfdba5
PA
10113 /* Template parameters may be specified in the DIE's DW_AT_name, or
10114 as children with DW_TAG_template_type_param or
10115 DW_TAG_value_type_param. If the latter, add them to the name
10116 here. If the name already has template parameters, then
10117 skip this step; some versions of GCC emit both, and
10118 it is more efficient to use the pre-computed name.
10119
10120 Something to keep in mind about this process: it is very
10121 unlikely, or in some cases downright impossible, to produce
10122 something that will match the mangled name of a function.
10123 If the definition of the function has the same debug info,
10124 we should be able to match up with it anyway. But fallbacks
10125 using the minimal symbol, for instance to find a method
10126 implemented in a stripped copy of libstdc++, will not work.
10127 If we do not have debug info for the definition, we will have to
10128 match them up some other way.
10129
10130 When we do name matching there is a related problem with function
10131 templates; two instantiated function templates are allowed to
10132 differ only by their return types, which we do not add here. */
10133
10134 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10135 {
10136 struct attribute *attr;
10137 struct die_info *child;
10138 int first = 1;
10139
10140 die->building_fullname = 1;
10141
10142 for (child = die->child; child != NULL; child = child->sibling)
10143 {
10144 struct type *type;
12df843f 10145 LONGEST value;
d521ce57 10146 const gdb_byte *bytes;
98bfdba5
PA
10147 struct dwarf2_locexpr_baton *baton;
10148 struct value *v;
10149
10150 if (child->tag != DW_TAG_template_type_param
10151 && child->tag != DW_TAG_template_value_param)
10152 continue;
10153
10154 if (first)
10155 {
d7e74731 10156 buf.puts ("<");
98bfdba5
PA
10157 first = 0;
10158 }
10159 else
d7e74731 10160 buf.puts (", ");
98bfdba5
PA
10161
10162 attr = dwarf2_attr (child, DW_AT_type, cu);
10163 if (attr == NULL)
10164 {
b98664d3 10165 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10166 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10167 continue;
10168 }
10169 type = die_type (child, cu);
10170
10171 if (child->tag == DW_TAG_template_type_param)
10172 {
c1ec8cea
TT
10173 c_print_type (type, "", &buf, -1, 0, cu->language,
10174 &type_print_raw_options);
98bfdba5
PA
10175 continue;
10176 }
10177
10178 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10179 if (attr == NULL)
10180 {
b98664d3 10181 complaint (_("template parameter missing "
3e43a32a 10182 "DW_AT_const_value"));
d7e74731 10183 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10184 continue;
10185 }
10186
10187 dwarf2_const_value_attr (attr, type, name,
10188 &cu->comp_unit_obstack, cu,
10189 &value, &bytes, &baton);
10190
10191 if (TYPE_NOSIGN (type))
10192 /* GDB prints characters as NUMBER 'CHAR'. If that's
10193 changed, this can use value_print instead. */
d7e74731 10194 c_printchar (value, type, &buf);
98bfdba5
PA
10195 else
10196 {
10197 struct value_print_options opts;
10198
10199 if (baton != NULL)
10200 v = dwarf2_evaluate_loc_desc (type, NULL,
10201 baton->data,
10202 baton->size,
10203 baton->per_cu);
10204 else if (bytes != NULL)
10205 {
10206 v = allocate_value (type);
10207 memcpy (value_contents_writeable (v), bytes,
10208 TYPE_LENGTH (type));
10209 }
10210 else
10211 v = value_from_longest (type, value);
10212
3e43a32a
MS
10213 /* Specify decimal so that we do not depend on
10214 the radix. */
98bfdba5
PA
10215 get_formatted_print_options (&opts, 'd');
10216 opts.raw = 1;
d7e74731 10217 value_print (v, &buf, &opts);
98bfdba5 10218 release_value (v);
98bfdba5
PA
10219 }
10220 }
10221
10222 die->building_fullname = 0;
10223
10224 if (!first)
10225 {
10226 /* Close the argument list, with a space if necessary
10227 (nested templates). */
d7e74731
PA
10228 if (!buf.empty () && buf.string ().back () == '>')
10229 buf.puts (" >");
98bfdba5 10230 else
d7e74731 10231 buf.puts (">");
98bfdba5
PA
10232 }
10233 }
10234
9c37b5ae 10235 /* For C++ methods, append formal parameter type
94af9270 10236 information, if PHYSNAME. */
6e70227d 10237
94af9270 10238 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10239 && cu->language == language_cplus)
94af9270
KS
10240 {
10241 struct type *type = read_type_die (die, cu);
10242
d7e74731 10243 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10244 &type_print_raw_options);
94af9270 10245
9c37b5ae 10246 if (cu->language == language_cplus)
94af9270 10247 {
60430eff
DJ
10248 /* Assume that an artificial first parameter is
10249 "this", but do not crash if it is not. RealView
10250 marks unnamed (and thus unused) parameters as
10251 artificial; there is no way to differentiate
10252 the two cases. */
94af9270
KS
10253 if (TYPE_NFIELDS (type) > 0
10254 && TYPE_FIELD_ARTIFICIAL (type, 0)
60430eff 10255 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
3e43a32a
MS
10256 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10257 0))))
d7e74731 10258 buf.puts (" const");
94af9270
KS
10259 }
10260 }
10261
d7e74731 10262 const std::string &intermediate_name = buf.string ();
94af9270
KS
10263
10264 if (cu->language == language_cplus)
34a68019 10265 canonical_name
322a8516 10266 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10267 objfile);
34a68019
TT
10268
10269 /* If we only computed INTERMEDIATE_NAME, or if
10270 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10271 intern it. */
322a8516 10272 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10273 name = objfile->intern (intermediate_name);
34a68019
TT
10274 else
10275 name = canonical_name;
94af9270
KS
10276 }
10277 }
10278
10279 return name;
10280}
10281
0114d602
DJ
10282/* Return the fully qualified name of DIE, based on its DW_AT_name.
10283 If scope qualifiers are appropriate they will be added. The result
34a68019 10284 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10285 not have a name. NAME may either be from a previous call to
10286 dwarf2_name or NULL.
10287
9c37b5ae 10288 The output string will be canonicalized (if C++). */
0114d602
DJ
10289
10290static const char *
15d034d0 10291dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10292{
94af9270
KS
10293 return dwarf2_compute_name (name, die, cu, 0);
10294}
0114d602 10295
94af9270
KS
10296/* Construct a physname for the given DIE in CU. NAME may either be
10297 from a previous call to dwarf2_name or NULL. The result will be
10298 allocated on the objfile_objstack or NULL if the DIE does not have a
10299 name.
0114d602 10300
9c37b5ae 10301 The output string will be canonicalized (if C++). */
0114d602 10302
94af9270 10303static const char *
15d034d0 10304dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10305{
518817b3 10306 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
900e11f9 10307 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10308 int need_copy = 1;
10309
10310 /* In this case dwarf2_compute_name is just a shortcut not building anything
10311 on its own. */
10312 if (!die_needs_namespace (die, cu))
10313 return dwarf2_compute_name (name, die, cu, 1);
10314
73b9be8b 10315 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10316
10317 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10318 has computed. */
791afaa2 10319 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10320 if (mangled != NULL)
900e11f9 10321 {
900e11f9 10322
59cc4834
JB
10323 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10324 {
10325 /* Do nothing (do not demangle the symbol name). */
10326 }
10327 else if (cu->language == language_go)
a766d390 10328 {
5e2db402
TT
10329 /* This is a lie, but we already lie to the caller new_symbol.
10330 new_symbol assumes we return the mangled name.
a766d390 10331 This just undoes that lie until things are cleaned up. */
a766d390
DE
10332 }
10333 else
10334 {
0eb876f5
JB
10335 /* Use DMGL_RET_DROP for C++ template functions to suppress
10336 their return type. It is easier for GDB users to search
10337 for such functions as `name(params)' than `long name(params)'.
10338 In such case the minimal symbol names do not match the full
10339 symbol names but for template functions there is never a need
10340 to look up their definition from their declaration so
10341 the only disadvantage remains the minimal symbol variant
10342 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10343 demangled.reset (gdb_demangle (mangled,
10344 (DMGL_PARAMS | DMGL_ANSI
10345 | DMGL_RET_DROP)));
a766d390 10346 }
900e11f9 10347 if (demangled)
791afaa2 10348 canon = demangled.get ();
900e11f9
JK
10349 else
10350 {
10351 canon = mangled;
10352 need_copy = 0;
10353 }
10354 }
10355
10356 if (canon == NULL || check_physname)
10357 {
10358 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10359
10360 if (canon != NULL && strcmp (physname, canon) != 0)
10361 {
10362 /* It may not mean a bug in GDB. The compiler could also
10363 compute DW_AT_linkage_name incorrectly. But in such case
10364 GDB would need to be bug-to-bug compatible. */
10365
b98664d3 10366 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10367 "(from linkage <%s>) - DIE at %s [in module %s]"),
10368 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10369 objfile_name (objfile));
900e11f9
JK
10370
10371 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10372 is available here - over computed PHYSNAME. It is safer
10373 against both buggy GDB and buggy compilers. */
10374
10375 retval = canon;
10376 }
10377 else
10378 {
10379 retval = physname;
10380 need_copy = 0;
10381 }
10382 }
10383 else
10384 retval = canon;
10385
10386 if (need_copy)
be1e3d3e 10387 retval = objfile->intern (retval);
900e11f9 10388
900e11f9 10389 return retval;
0114d602
DJ
10390}
10391
74921315
KS
10392/* Inspect DIE in CU for a namespace alias. If one exists, record
10393 a new symbol for it.
10394
10395 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10396
10397static int
10398read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10399{
10400 struct attribute *attr;
10401
10402 /* If the die does not have a name, this is not a namespace
10403 alias. */
10404 attr = dwarf2_attr (die, DW_AT_name, cu);
10405 if (attr != NULL)
10406 {
10407 int num;
10408 struct die_info *d = die;
10409 struct dwarf2_cu *imported_cu = cu;
10410
10411 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10412 keep inspecting DIEs until we hit the underlying import. */
10413#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10414 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10415 {
10416 attr = dwarf2_attr (d, DW_AT_import, cu);
10417 if (attr == NULL)
10418 break;
10419
10420 d = follow_die_ref (d, attr, &imported_cu);
10421 if (d->tag != DW_TAG_imported_declaration)
10422 break;
10423 }
10424
10425 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10426 {
b98664d3 10427 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10428 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10429 return 0;
10430 }
10431
10432 if (attr != NULL)
10433 {
10434 struct type *type;
0826b30a 10435 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10436
9c541725 10437 type = get_die_type_at_offset (sect_off, cu->per_cu);
74921315
KS
10438 if (type != NULL && TYPE_CODE (type) == TYPE_CODE_NAMESPACE)
10439 {
10440 /* This declaration is a global namespace alias. Add
10441 a symbol for it whose type is the aliased namespace. */
10442 new_symbol (die, type, cu);
10443 return 1;
10444 }
10445 }
10446 }
10447
10448 return 0;
10449}
10450
22cee43f 10451/* Return the using directives repository (global or local?) to use in the
804d2729 10452 current context for CU.
22cee43f
PMR
10453
10454 For Ada, imported declarations can materialize renamings, which *may* be
10455 global. However it is impossible (for now?) in DWARF to distinguish
10456 "external" imported declarations and "static" ones. As all imported
10457 declarations seem to be static in all other languages, make them all CU-wide
10458 global only in Ada. */
10459
10460static struct using_direct **
804d2729 10461using_directives (struct dwarf2_cu *cu)
22cee43f 10462{
c24bdb02
KS
10463 if (cu->language == language_ada
10464 && cu->get_builder ()->outermost_context_p ())
10465 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10466 else
c24bdb02 10467 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10468}
10469
27aa8d6a
SW
10470/* Read the import statement specified by the given die and record it. */
10471
10472static void
10473read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10474{
518817b3 10475 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
27aa8d6a 10476 struct attribute *import_attr;
32019081 10477 struct die_info *imported_die, *child_die;
de4affc9 10478 struct dwarf2_cu *imported_cu;
27aa8d6a 10479 const char *imported_name;
794684b6 10480 const char *imported_name_prefix;
13387711
SW
10481 const char *canonical_name;
10482 const char *import_alias;
10483 const char *imported_declaration = NULL;
794684b6 10484 const char *import_prefix;
eb1e02fd 10485 std::vector<const char *> excludes;
13387711 10486
27aa8d6a
SW
10487 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10488 if (import_attr == NULL)
10489 {
b98664d3 10490 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10491 dwarf_tag_name (die->tag));
10492 return;
10493 }
10494
de4affc9
CC
10495 imported_cu = cu;
10496 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10497 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10498 if (imported_name == NULL)
10499 {
10500 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10501
10502 The import in the following code:
10503 namespace A
10504 {
10505 typedef int B;
10506 }
10507
10508 int main ()
10509 {
10510 using A::B;
10511 B b;
10512 return b;
10513 }
10514
10515 ...
10516 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10517 <52> DW_AT_decl_file : 1
10518 <53> DW_AT_decl_line : 6
10519 <54> DW_AT_import : <0x75>
10520 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10521 <59> DW_AT_name : B
10522 <5b> DW_AT_decl_file : 1
10523 <5c> DW_AT_decl_line : 2
10524 <5d> DW_AT_type : <0x6e>
10525 ...
10526 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10527 <76> DW_AT_byte_size : 4
10528 <77> DW_AT_encoding : 5 (signed)
10529
10530 imports the wrong die ( 0x75 instead of 0x58 ).
10531 This case will be ignored until the gcc bug is fixed. */
10532 return;
10533 }
10534
82856980
SW
10535 /* Figure out the local name after import. */
10536 import_alias = dwarf2_name (die, cu);
27aa8d6a 10537
794684b6
SW
10538 /* Figure out where the statement is being imported to. */
10539 import_prefix = determine_prefix (die, cu);
10540
10541 /* Figure out what the scope of the imported die is and prepend it
10542 to the name of the imported die. */
de4affc9 10543 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10544
f55ee35c
JK
10545 if (imported_die->tag != DW_TAG_namespace
10546 && imported_die->tag != DW_TAG_module)
794684b6 10547 {
13387711
SW
10548 imported_declaration = imported_name;
10549 canonical_name = imported_name_prefix;
794684b6 10550 }
13387711 10551 else if (strlen (imported_name_prefix) > 0)
12aaed36 10552 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10553 imported_name_prefix,
10554 (cu->language == language_d ? "." : "::"),
10555 imported_name, (char *) NULL);
13387711
SW
10556 else
10557 canonical_name = imported_name;
794684b6 10558
32019081
JK
10559 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10560 for (child_die = die->child; child_die && child_die->tag;
436c571c 10561 child_die = child_die->sibling)
32019081
JK
10562 {
10563 /* DWARF-4: A Fortran use statement with a “rename list” may be
10564 represented by an imported module entry with an import attribute
10565 referring to the module and owned entries corresponding to those
10566 entities that are renamed as part of being imported. */
10567
10568 if (child_die->tag != DW_TAG_imported_declaration)
10569 {
b98664d3 10570 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10571 "- DIE at %s [in module %s]"),
10572 sect_offset_str (child_die->sect_off),
10573 objfile_name (objfile));
32019081
JK
10574 continue;
10575 }
10576
10577 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10578 if (import_attr == NULL)
10579 {
b98664d3 10580 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10581 dwarf_tag_name (child_die->tag));
10582 continue;
10583 }
10584
10585 imported_cu = cu;
10586 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10587 &imported_cu);
10588 imported_name = dwarf2_name (imported_die, imported_cu);
10589 if (imported_name == NULL)
10590 {
b98664d3 10591 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10592 "imported name - DIE at %s [in module %s]"),
10593 sect_offset_str (child_die->sect_off),
10594 objfile_name (objfile));
32019081
JK
10595 continue;
10596 }
10597
eb1e02fd 10598 excludes.push_back (imported_name);
32019081
JK
10599
10600 process_die (child_die, cu);
10601 }
10602
804d2729 10603 add_using_directive (using_directives (cu),
22cee43f
PMR
10604 import_prefix,
10605 canonical_name,
10606 import_alias,
10607 imported_declaration,
10608 excludes,
10609 0,
10610 &objfile->objfile_obstack);
27aa8d6a
SW
10611}
10612
5230b05a
WT
10613/* ICC<14 does not output the required DW_AT_declaration on incomplete
10614 types, but gives them a size of zero. Starting with version 14,
10615 ICC is compatible with GCC. */
10616
9068261f 10617static bool
5230b05a
WT
10618producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10619{
10620 if (!cu->checked_producer)
10621 check_producer (cu);
10622
10623 return cu->producer_is_icc_lt_14;
10624}
10625
eb77c9df
AB
10626/* ICC generates a DW_AT_type for C void functions. This was observed on
10627 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10628 which says that void functions should not have a DW_AT_type. */
10629
10630static bool
10631producer_is_icc (struct dwarf2_cu *cu)
10632{
10633 if (!cu->checked_producer)
10634 check_producer (cu);
10635
10636 return cu->producer_is_icc;
10637}
10638
1b80a9fa
JK
10639/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10640 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10641 this, it was first present in GCC release 4.3.0. */
10642
9068261f 10643static bool
1b80a9fa
JK
10644producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10645{
10646 if (!cu->checked_producer)
10647 check_producer (cu);
10648
10649 return cu->producer_is_gcc_lt_4_3;
10650}
10651
d721ba37
PA
10652static file_and_directory
10653find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10654{
d721ba37
PA
10655 file_and_directory res;
10656
9291a0cd
TT
10657 /* Find the filename. Do not use dwarf2_name here, since the filename
10658 is not a source language identifier. */
d721ba37
PA
10659 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10660 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10661
d721ba37
PA
10662 if (res.comp_dir == NULL
10663 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10664 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10665 {
d721ba37
PA
10666 res.comp_dir_storage = ldirname (res.name);
10667 if (!res.comp_dir_storage.empty ())
10668 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10669 }
d721ba37 10670 if (res.comp_dir != NULL)
9291a0cd
TT
10671 {
10672 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10673 directory, get rid of it. */
d721ba37 10674 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10675
d721ba37
PA
10676 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10677 res.comp_dir = cp + 1;
9291a0cd
TT
10678 }
10679
d721ba37
PA
10680 if (res.name == NULL)
10681 res.name = "<unknown>";
10682
10683 return res;
9291a0cd
TT
10684}
10685
f4dc4d17
DE
10686/* Handle DW_AT_stmt_list for a compilation unit.
10687 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10688 COMP_DIR is the compilation directory. LOWPC is passed to
10689 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10690
10691static void
10692handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10693 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10694{
518817b3
SM
10695 struct dwarf2_per_objfile *dwarf2_per_objfile
10696 = cu->per_cu->dwarf2_per_objfile;
2ab95328 10697 struct attribute *attr;
527f3840
JK
10698 struct line_header line_header_local;
10699 hashval_t line_header_local_hash;
527f3840
JK
10700 void **slot;
10701 int decode_mapping;
2ab95328 10702
f4dc4d17
DE
10703 gdb_assert (! cu->per_cu->is_debug_types);
10704
2ab95328 10705 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10706 if (attr == NULL)
10707 return;
10708
9c541725 10709 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10710
10711 /* The line header hash table is only created if needed (it exists to
10712 prevent redundant reading of the line table for partial_units).
10713 If we're given a partial_unit, we'll need it. If we're given a
10714 compile_unit, then use the line header hash table if it's already
10715 created, but don't create one just yet. */
10716
10717 if (dwarf2_per_objfile->line_header_hash == NULL
10718 && die->tag == DW_TAG_partial_unit)
2ab95328 10719 {
527f3840 10720 dwarf2_per_objfile->line_header_hash
d15acc42
TT
10721 .reset (htab_create_alloc (127, line_header_hash_voidp,
10722 line_header_eq_voidp,
10723 free_line_header_voidp,
10724 xcalloc, xfree));
527f3840 10725 }
2ab95328 10726
9c541725 10727 line_header_local.sect_off = line_offset;
527f3840
JK
10728 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10729 line_header_local_hash = line_header_hash (&line_header_local);
10730 if (dwarf2_per_objfile->line_header_hash != NULL)
10731 {
d15acc42 10732 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10733 &line_header_local,
10734 line_header_local_hash, NO_INSERT);
10735
10736 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10737 is not present in *SLOT (since if there is something in *SLOT then
10738 it will be for a partial_unit). */
10739 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10740 {
527f3840 10741 gdb_assert (*slot != NULL);
9a3c8263 10742 cu->line_header = (struct line_header *) *slot;
527f3840 10743 return;
dee91e82 10744 }
2ab95328 10745 }
527f3840
JK
10746
10747 /* dwarf_decode_line_header does not yet provide sufficient information.
10748 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10749 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10750 if (lh == NULL)
527f3840 10751 return;
4c8aa72d
PA
10752
10753 cu->line_header = lh.release ();
10754 cu->line_header_die_owner = die;
527f3840
JK
10755
10756 if (dwarf2_per_objfile->line_header_hash == NULL)
10757 slot = NULL;
10758 else
10759 {
d15acc42 10760 slot = htab_find_slot_with_hash (dwarf2_per_objfile->line_header_hash.get (),
527f3840
JK
10761 &line_header_local,
10762 line_header_local_hash, INSERT);
10763 gdb_assert (slot != NULL);
10764 }
10765 if (slot != NULL && *slot == NULL)
10766 {
10767 /* This newly decoded line number information unit will be owned
10768 by line_header_hash hash table. */
10769 *slot = cu->line_header;
4c8aa72d 10770 cu->line_header_die_owner = NULL;
527f3840
JK
10771 }
10772 else
10773 {
10774 /* We cannot free any current entry in (*slot) as that struct line_header
10775 may be already used by multiple CUs. Create only temporary decoded
10776 line_header for this CU - it may happen at most once for each line
10777 number information unit. And if we're not using line_header_hash
10778 then this is what we want as well. */
10779 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10780 }
10781 decode_mapping = (die->tag != DW_TAG_partial_unit);
10782 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10783 decode_mapping);
fff8551c 10784
2ab95328
TT
10785}
10786
95554aad 10787/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10788
c906108c 10789static void
e7c27a73 10790read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10791{
518817b3
SM
10792 struct dwarf2_per_objfile *dwarf2_per_objfile
10793 = cu->per_cu->dwarf2_per_objfile;
dee91e82 10794 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10795 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10796 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10797 CORE_ADDR highpc = ((CORE_ADDR) 0);
10798 struct attribute *attr;
c906108c 10799 struct die_info *child_die;
e142c38c 10800 CORE_ADDR baseaddr;
6e70227d 10801
380618d6 10802 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10803 baseaddr = objfile->text_section_offset ();
c906108c 10804
fae299cd 10805 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10806
10807 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10808 from finish_block. */
2acceee2 10809 if (lowpc == ((CORE_ADDR) -1))
c906108c 10810 lowpc = highpc;
3e29f34a 10811 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10812
d721ba37 10813 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10814
f4b8a18d
KW
10815 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10816 standardised yet. As a workaround for the language detection we fall
10817 back to the DW_AT_producer string. */
10818 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10819 cu->language = language_opencl;
10820
3019eac3
DE
10821 /* Similar hack for Go. */
10822 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10823 set_cu_language (DW_LANG_Go, cu);
10824
c24bdb02 10825 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10826
10827 /* Decode line number information if present. We do this before
10828 processing child DIEs, so that the line header table is available
10829 for DW_AT_decl_file. */
d721ba37 10830 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10831
10832 /* Process all dies in compilation unit. */
10833 if (die->child != NULL)
10834 {
10835 child_die = die->child;
10836 while (child_die && child_die->tag)
10837 {
10838 process_die (child_die, cu);
436c571c 10839 child_die = child_die->sibling;
3019eac3
DE
10840 }
10841 }
10842
10843 /* Decode macro information, if present. Dwarf 2 macro information
10844 refers to information in the line number info statement program
10845 header, so we can only read it if we've read the header
10846 successfully. */
0af92d60
JK
10847 attr = dwarf2_attr (die, DW_AT_macros, cu);
10848 if (attr == NULL)
10849 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
10850 if (attr && cu->line_header)
10851 {
10852 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 10853 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 10854
43f3e411 10855 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
10856 }
10857 else
10858 {
10859 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
10860 if (attr && cu->line_header)
10861 {
10862 unsigned int macro_offset = DW_UNSND (attr);
10863
43f3e411 10864 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
10865 }
10866 }
3019eac3
DE
10867}
10868
c24bdb02
KS
10869void
10870dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 10871{
f4dc4d17
DE
10872 struct type_unit_group *tu_group;
10873 int first_time;
3019eac3 10874 struct attribute *attr;
9c541725 10875 unsigned int i;
0186c6a7 10876 struct signatured_type *sig_type;
3019eac3 10877
f4dc4d17 10878 gdb_assert (per_cu->is_debug_types);
0186c6a7 10879 sig_type = (struct signatured_type *) per_cu;
3019eac3 10880
c24bdb02 10881 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 10882
f4dc4d17 10883 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 10884 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 10885 if (sig_type->type_unit_group == NULL)
c24bdb02 10886 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 10887 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
10888
10889 /* If we've already processed this stmt_list there's no real need to
10890 do it again, we could fake it and just recreate the part we need
10891 (file name,index -> symtab mapping). If data shows this optimization
10892 is useful we can do it then. */
43f3e411 10893 first_time = tu_group->compunit_symtab == NULL;
f4dc4d17
DE
10894
10895 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
10896 debug info. */
fff8551c 10897 line_header_up lh;
f4dc4d17 10898 if (attr != NULL)
3019eac3 10899 {
9c541725 10900 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 10901 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
10902 }
10903 if (lh == NULL)
10904 {
10905 if (first_time)
c24bdb02 10906 start_symtab ("", NULL, 0);
f4dc4d17
DE
10907 else
10908 {
10909 gdb_assert (tu_group->symtabs == NULL);
c24bdb02 10910 gdb_assert (m_builder == nullptr);
804d2729 10911 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10912 m_builder.reset (new struct buildsym_compunit
10913 (COMPUNIT_OBJFILE (cust), "",
10914 COMPUNIT_DIRNAME (cust),
10915 compunit_language (cust),
10916 0, cust));
f4dc4d17 10917 }
f4dc4d17 10918 return;
3019eac3
DE
10919 }
10920
c24bdb02
KS
10921 line_header = lh.release ();
10922 line_header_die_owner = die;
3019eac3 10923
f4dc4d17
DE
10924 if (first_time)
10925 {
c24bdb02 10926 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 10927
1fd60fc0
DE
10928 /* Note: We don't assign tu_group->compunit_symtab yet because we're
10929 still initializing it, and our caller (a few levels up)
10930 process_full_type_unit still needs to know if this is the first
10931 time. */
10932
4ac93832
TT
10933 tu_group->symtabs
10934 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
10935 struct symtab *, line_header->file_names_size ());
3019eac3 10936
7ba99d21
AT
10937 auto &file_names = line_header->file_names ();
10938 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10939 {
7ba99d21 10940 file_entry &fe = file_names[i];
c24bdb02
KS
10941 dwarf2_start_subfile (this, fe.name,
10942 fe.include_dir (line_header));
10943 buildsym_compunit *b = get_builder ();
10944 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 10945 {
4c8aa72d
PA
10946 /* NOTE: start_subfile will recognize when it's been
10947 passed a file it has already seen. So we can't
10948 assume there's a simple mapping from
10949 cu->line_header->file_names to subfiles, plus
10950 cu->line_header->file_names may contain dups. */
c24bdb02
KS
10951 b->get_current_subfile ()->symtab
10952 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
10953 }
10954
c24bdb02 10955 fe.symtab = b->get_current_subfile ()->symtab;
8c43009f 10956 tu_group->symtabs[i] = fe.symtab;
f4dc4d17
DE
10957 }
10958 }
10959 else
3019eac3 10960 {
c24bdb02 10961 gdb_assert (m_builder == nullptr);
804d2729 10962 struct compunit_symtab *cust = tu_group->compunit_symtab;
c24bdb02
KS
10963 m_builder.reset (new struct buildsym_compunit
10964 (COMPUNIT_OBJFILE (cust), "",
10965 COMPUNIT_DIRNAME (cust),
10966 compunit_language (cust),
10967 0, cust));
f4dc4d17 10968
7ba99d21
AT
10969 auto &file_names = line_header->file_names ();
10970 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 10971 {
7ba99d21 10972 file_entry &fe = file_names[i];
4c8aa72d 10973 fe.symtab = tu_group->symtabs[i];
f4dc4d17 10974 }
3019eac3
DE
10975 }
10976
f4dc4d17
DE
10977 /* The main symtab is allocated last. Type units don't have DW_AT_name
10978 so they don't have a "real" (so to speak) symtab anyway.
10979 There is later code that will assign the main symtab to all symbols
10980 that don't have one. We need to handle the case of a symbol with a
10981 missing symtab (DW_AT_decl_file) anyway. */
10982}
3019eac3 10983
f4dc4d17
DE
10984/* Process DW_TAG_type_unit.
10985 For TUs we want to skip the first top level sibling if it's not the
10986 actual type being defined by this TU. In this case the first top
10987 level sibling is there to provide context only. */
3019eac3 10988
f4dc4d17
DE
10989static void
10990read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
10991{
10992 struct die_info *child_die;
3019eac3 10993
f4dc4d17
DE
10994 prepare_one_comp_unit (cu, die, language_minimal);
10995
10996 /* Initialize (or reinitialize) the machinery for building symtabs.
10997 We do this before processing child DIEs, so that the line header table
10998 is available for DW_AT_decl_file. */
c24bdb02 10999 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11000
11001 if (die->child != NULL)
11002 {
11003 child_die = die->child;
11004 while (child_die && child_die->tag)
11005 {
11006 process_die (child_die, cu);
436c571c 11007 child_die = child_die->sibling;
f4dc4d17
DE
11008 }
11009 }
3019eac3
DE
11010}
11011\f
80626a55
DE
11012/* DWO/DWP files.
11013
11014 http://gcc.gnu.org/wiki/DebugFission
11015 http://gcc.gnu.org/wiki/DebugFissionDWP
11016
11017 To simplify handling of both DWO files ("object" files with the DWARF info)
11018 and DWP files (a file with the DWOs packaged up into one file), we treat
11019 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11020
11021static hashval_t
11022hash_dwo_file (const void *item)
11023{
9a3c8263 11024 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11025 hashval_t hash;
3019eac3 11026
a2ce51a0
DE
11027 hash = htab_hash_string (dwo_file->dwo_name);
11028 if (dwo_file->comp_dir != NULL)
11029 hash += htab_hash_string (dwo_file->comp_dir);
11030 return hash;
3019eac3
DE
11031}
11032
11033static int
11034eq_dwo_file (const void *item_lhs, const void *item_rhs)
11035{
9a3c8263
SM
11036 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11037 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11038
a2ce51a0
DE
11039 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11040 return 0;
11041 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11042 return lhs->comp_dir == rhs->comp_dir;
11043 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11044}
11045
11046/* Allocate a hash table for DWO files. */
11047
51ac9db5 11048static htab_up
298e9637 11049allocate_dwo_file_hash_table ()
3019eac3 11050{
51ac9db5
SM
11051 auto delete_dwo_file = [] (void *item)
11052 {
11053 struct dwo_file *dwo_file = (struct dwo_file *) item;
11054
11055 delete dwo_file;
11056 };
11057
bc68fb19
TT
11058 return htab_up (htab_create_alloc (41,
11059 hash_dwo_file,
11060 eq_dwo_file,
11061 delete_dwo_file,
11062 xcalloc, xfree));
3019eac3
DE
11063}
11064
80626a55
DE
11065/* Lookup DWO file DWO_NAME. */
11066
11067static void **
ed2dc618
SM
11068lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11069 const char *dwo_name,
11070 const char *comp_dir)
80626a55
DE
11071{
11072 struct dwo_file find_entry;
11073 void **slot;
11074
11075 if (dwarf2_per_objfile->dwo_files == NULL)
298e9637 11076 dwarf2_per_objfile->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11077
0ac5b59e
DE
11078 find_entry.dwo_name = dwo_name;
11079 find_entry.comp_dir = comp_dir;
51ac9db5
SM
11080 slot = htab_find_slot (dwarf2_per_objfile->dwo_files.get (), &find_entry,
11081 INSERT);
80626a55
DE
11082
11083 return slot;
11084}
11085
3019eac3
DE
11086static hashval_t
11087hash_dwo_unit (const void *item)
11088{
9a3c8263 11089 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11090
11091 /* This drops the top 32 bits of the id, but is ok for a hash. */
11092 return dwo_unit->signature;
11093}
11094
11095static int
11096eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11097{
9a3c8263
SM
11098 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11099 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11100
11101 /* The signature is assumed to be unique within the DWO file.
11102 So while object file CU dwo_id's always have the value zero,
11103 that's OK, assuming each object file DWO file has only one CU,
11104 and that's the rule for now. */
11105 return lhs->signature == rhs->signature;
11106}
11107
11108/* Allocate a hash table for DWO CUs,TUs.
11109 There is one of these tables for each of CUs,TUs for each DWO file. */
11110
b0b6a987 11111static htab_up
298e9637 11112allocate_dwo_unit_table ()
3019eac3
DE
11113{
11114 /* Start out with a pretty small number.
11115 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11116 return htab_up (htab_create_alloc (3,
11117 hash_dwo_unit,
11118 eq_dwo_unit,
11119 NULL, xcalloc, xfree));
3019eac3
DE
11120}
11121
19c3d4c9 11122/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11123
11124static void
19c3d4c9
DE
11125create_dwo_cu_reader (const struct die_reader_specs *reader,
11126 const gdb_byte *info_ptr,
11127 struct die_info *comp_unit_die,
c0ab21c2
TT
11128 struct dwo_file *dwo_file,
11129 struct dwo_unit *dwo_unit)
3019eac3
DE
11130{
11131 struct dwarf2_cu *cu = reader->cu;
9c541725 11132 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11133 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11134
a084a2a6
AT
11135 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11136 if (!signature.has_value ())
3019eac3 11137 {
b98664d3 11138 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11139 " its dwo_id [in module %s]"),
9d8780f0 11140 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11141 return;
11142 }
11143
3019eac3 11144 dwo_unit->dwo_file = dwo_file;
a084a2a6 11145 dwo_unit->signature = *signature;
8a0459fd 11146 dwo_unit->section = section;
9c541725 11147 dwo_unit->sect_off = sect_off;
3019eac3
DE
11148 dwo_unit->length = cu->per_cu->length;
11149
b4f54984 11150 if (dwarf_read_debug)
9d8780f0
SM
11151 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11152 sect_offset_str (sect_off),
9c541725 11153 hex_string (dwo_unit->signature));
3019eac3
DE
11154}
11155
33c5cd75 11156/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11157 Note: This function processes DWO files only, not DWP files. */
3019eac3 11158
33c5cd75 11159static void
ed2dc618 11160create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11161 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11162 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11163{
11164 struct objfile *objfile = dwarf2_per_objfile->objfile;
d521ce57 11165 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11166
96b79293 11167 section.read (objfile);
33c5cd75 11168 info_ptr = section.buffer;
3019eac3
DE
11169
11170 if (info_ptr == NULL)
33c5cd75 11171 return;
3019eac3 11172
b4f54984 11173 if (dwarf_read_debug)
19c3d4c9
DE
11174 {
11175 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11176 section.get_name (),
11177 section.get_file_name ());
19c3d4c9 11178 }
3019eac3 11179
33c5cd75 11180 end_ptr = info_ptr + section.size;
3019eac3
DE
11181 while (info_ptr < end_ptr)
11182 {
11183 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11184 struct dwo_unit read_unit {};
33c5cd75
DB
11185 struct dwo_unit *dwo_unit;
11186 void **slot;
11187 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11188
11189 memset (&per_cu, 0, sizeof (per_cu));
e3b94546 11190 per_cu.dwarf2_per_objfile = dwarf2_per_objfile;
3019eac3 11191 per_cu.is_debug_types = 0;
33c5cd75
DB
11192 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11193 per_cu.section = &section;
11194
c0ab21c2
TT
11195 cutu_reader reader (&per_cu, cu, &dwo_file);
11196 if (!reader.dummy_p)
11197 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11198 &dwo_file, &read_unit);
33c5cd75
DB
11199 info_ptr += per_cu.length;
11200
11201 // If the unit could not be parsed, skip it.
c0ab21c2 11202 if (read_unit.dwo_file == NULL)
33c5cd75 11203 continue;
3019eac3 11204
33c5cd75 11205 if (cus_htab == NULL)
298e9637 11206 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11207
33c5cd75 11208 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
c0ab21c2 11209 *dwo_unit = read_unit;
b0b6a987 11210 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11211 gdb_assert (slot != NULL);
11212 if (*slot != NULL)
19c3d4c9 11213 {
33c5cd75
DB
11214 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11215 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11216
b98664d3 11217 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11218 " the entry at offset %s, signature %s"),
11219 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11220 hex_string (dwo_unit->signature));
19c3d4c9 11221 }
33c5cd75 11222 *slot = (void *)dwo_unit;
3019eac3 11223 }
3019eac3
DE
11224}
11225
80626a55
DE
11226/* DWP file .debug_{cu,tu}_index section format:
11227 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11228
d2415c6c
DE
11229 DWP Version 1:
11230
80626a55
DE
11231 Both index sections have the same format, and serve to map a 64-bit
11232 signature to a set of section numbers. Each section begins with a header,
11233 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11234 indexes, and a pool of 32-bit section numbers. The index sections will be
11235 aligned at 8-byte boundaries in the file.
11236
d2415c6c
DE
11237 The index section header consists of:
11238
11239 V, 32 bit version number
11240 -, 32 bits unused
11241 N, 32 bit number of compilation units or type units in the index
11242 M, 32 bit number of slots in the hash table
80626a55 11243
d2415c6c 11244 Numbers are recorded using the byte order of the application binary.
80626a55 11245
d2415c6c
DE
11246 The hash table begins at offset 16 in the section, and consists of an array
11247 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11248 order of the application binary). Unused slots in the hash table are 0.
11249 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11250
d2415c6c
DE
11251 The parallel table begins immediately after the hash table
11252 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11253 array of 32-bit indexes (using the byte order of the application binary),
11254 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11255 table contains a 32-bit index into the pool of section numbers. For unused
11256 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11257
73869dc2
DE
11258 The pool of section numbers begins immediately following the hash table
11259 (at offset 16 + 12 * M from the beginning of the section). The pool of
11260 section numbers consists of an array of 32-bit words (using the byte order
11261 of the application binary). Each item in the array is indexed starting
11262 from 0. The hash table entry provides the index of the first section
11263 number in the set. Additional section numbers in the set follow, and the
11264 set is terminated by a 0 entry (section number 0 is not used in ELF).
11265
11266 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11267 section must be the first entry in the set, and the .debug_abbrev.dwo must
11268 be the second entry. Other members of the set may follow in any order.
11269
11270 ---
11271
11272 DWP Version 2:
11273
11274 DWP Version 2 combines all the .debug_info, etc. sections into one,
11275 and the entries in the index tables are now offsets into these sections.
11276 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11277 section.
11278
11279 Index Section Contents:
11280 Header
11281 Hash Table of Signatures dwp_hash_table.hash_table
11282 Parallel Table of Indices dwp_hash_table.unit_table
11283 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11284 Table of Section Sizes dwp_hash_table.v2.sizes
11285
11286 The index section header consists of:
11287
11288 V, 32 bit version number
11289 L, 32 bit number of columns in the table of section offsets
11290 N, 32 bit number of compilation units or type units in the index
11291 M, 32 bit number of slots in the hash table
11292
11293 Numbers are recorded using the byte order of the application binary.
11294
11295 The hash table has the same format as version 1.
11296 The parallel table of indices has the same format as version 1,
11297 except that the entries are origin-1 indices into the table of sections
11298 offsets and the table of section sizes.
11299
11300 The table of offsets begins immediately following the parallel table
11301 (at offset 16 + 12 * M from the beginning of the section). The table is
11302 a two-dimensional array of 32-bit words (using the byte order of the
11303 application binary), with L columns and N+1 rows, in row-major order.
11304 Each row in the array is indexed starting from 0. The first row provides
11305 a key to the remaining rows: each column in this row provides an identifier
11306 for a debug section, and the offsets in the same column of subsequent rows
11307 refer to that section. The section identifiers are:
11308
11309 DW_SECT_INFO 1 .debug_info.dwo
11310 DW_SECT_TYPES 2 .debug_types.dwo
11311 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11312 DW_SECT_LINE 4 .debug_line.dwo
11313 DW_SECT_LOC 5 .debug_loc.dwo
11314 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11315 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11316 DW_SECT_MACRO 8 .debug_macro.dwo
11317
11318 The offsets provided by the CU and TU index sections are the base offsets
11319 for the contributions made by each CU or TU to the corresponding section
11320 in the package file. Each CU and TU header contains an abbrev_offset
11321 field, used to find the abbreviations table for that CU or TU within the
11322 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11323 be interpreted as relative to the base offset given in the index section.
11324 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11325 should be interpreted as relative to the base offset for .debug_line.dwo,
11326 and offsets into other debug sections obtained from DWARF attributes should
11327 also be interpreted as relative to the corresponding base offset.
11328
11329 The table of sizes begins immediately following the table of offsets.
11330 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11331 with L columns and N rows, in row-major order. Each row in the array is
11332 indexed starting from 1 (row 0 is shared by the two tables).
11333
11334 ---
11335
11336 Hash table lookup is handled the same in version 1 and 2:
11337
11338 We assume that N and M will not exceed 2^32 - 1.
11339 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11340
d2415c6c
DE
11341 Given a 64-bit compilation unit signature or a type signature S, an entry
11342 in the hash table is located as follows:
80626a55 11343
d2415c6c
DE
11344 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11345 the low-order k bits all set to 1.
80626a55 11346
d2415c6c 11347 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11348
d2415c6c
DE
11349 3) If the hash table entry at index H matches the signature, use that
11350 entry. If the hash table entry at index H is unused (all zeroes),
11351 terminate the search: the signature is not present in the table.
80626a55 11352
d2415c6c 11353 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11354
d2415c6c 11355 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11356 to stop at an unused slot or find the match. */
80626a55
DE
11357
11358/* Create a hash table to map DWO IDs to their CU/TU entry in
11359 .debug_{info,types}.dwo in DWP_FILE.
11360 Returns NULL if there isn't one.
11361 Note: This function processes DWP files only, not DWO files. */
11362
11363static struct dwp_hash_table *
ed2dc618
SM
11364create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11365 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11366{
11367 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11368 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11369 const gdb_byte *index_ptr, *index_end;
80626a55 11370 struct dwarf2_section_info *index;
73869dc2 11371 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11372 struct dwp_hash_table *htab;
11373
11374 if (is_debug_types)
11375 index = &dwp_file->sections.tu_index;
11376 else
11377 index = &dwp_file->sections.cu_index;
11378
96b79293 11379 if (index->empty ())
80626a55 11380 return NULL;
96b79293 11381 index->read (objfile);
80626a55
DE
11382
11383 index_ptr = index->buffer;
11384 index_end = index_ptr + index->size;
11385
11386 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11387 index_ptr += 4;
11388 if (version == 2)
11389 nr_columns = read_4_bytes (dbfd, index_ptr);
11390 else
11391 nr_columns = 0;
11392 index_ptr += 4;
80626a55
DE
11393 nr_units = read_4_bytes (dbfd, index_ptr);
11394 index_ptr += 4;
11395 nr_slots = read_4_bytes (dbfd, index_ptr);
11396 index_ptr += 4;
11397
73869dc2 11398 if (version != 1 && version != 2)
80626a55 11399 {
21aa081e 11400 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11401 " [in module %s]"),
21aa081e 11402 pulongest (version), dwp_file->name);
80626a55
DE
11403 }
11404 if (nr_slots != (nr_slots & -nr_slots))
11405 {
21aa081e 11406 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11407 " is not power of 2 [in module %s]"),
21aa081e 11408 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11409 }
11410
11411 htab = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwp_hash_table);
73869dc2
DE
11412 htab->version = version;
11413 htab->nr_columns = nr_columns;
80626a55
DE
11414 htab->nr_units = nr_units;
11415 htab->nr_slots = nr_slots;
11416 htab->hash_table = index_ptr;
11417 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11418
11419 /* Exit early if the table is empty. */
11420 if (nr_slots == 0 || nr_units == 0
11421 || (version == 2 && nr_columns == 0))
11422 {
11423 /* All must be zero. */
11424 if (nr_slots != 0 || nr_units != 0
11425 || (version == 2 && nr_columns != 0))
11426 {
b98664d3 11427 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11428 " all zero [in modules %s]"),
11429 dwp_file->name);
11430 }
11431 return htab;
11432 }
11433
11434 if (version == 1)
11435 {
11436 htab->section_pool.v1.indices =
11437 htab->unit_table + sizeof (uint32_t) * nr_slots;
11438 /* It's harder to decide whether the section is too small in v1.
11439 V1 is deprecated anyway so we punt. */
11440 }
11441 else
11442 {
11443 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11444 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11445 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11446 /* Reverse map for error checking. */
11447 int ids_seen[DW_SECT_MAX + 1];
11448 int i;
11449
11450 if (nr_columns < 2)
11451 {
11452 error (_("Dwarf Error: bad DWP hash table, too few columns"
11453 " in section table [in module %s]"),
11454 dwp_file->name);
11455 }
11456 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11457 {
11458 error (_("Dwarf Error: bad DWP hash table, too many columns"
11459 " in section table [in module %s]"),
11460 dwp_file->name);
11461 }
04fd5eed
GB
11462 memset (ids, 255, sizeof_ids);
11463 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11464 for (i = 0; i < nr_columns; ++i)
11465 {
11466 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11467
11468 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11469 {
11470 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11471 " in section table [in module %s]"),
11472 id, dwp_file->name);
11473 }
11474 if (ids_seen[id] != -1)
11475 {
11476 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11477 " id %d in section table [in module %s]"),
11478 id, dwp_file->name);
11479 }
11480 ids_seen[id] = i;
11481 ids[i] = id;
11482 }
11483 /* Must have exactly one info or types section. */
11484 if (((ids_seen[DW_SECT_INFO] != -1)
11485 + (ids_seen[DW_SECT_TYPES] != -1))
11486 != 1)
11487 {
11488 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11489 " DWO info/types section [in module %s]"),
11490 dwp_file->name);
11491 }
11492 /* Must have an abbrev section. */
11493 if (ids_seen[DW_SECT_ABBREV] == -1)
11494 {
11495 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11496 " section [in module %s]"),
11497 dwp_file->name);
11498 }
11499 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11500 htab->section_pool.v2.sizes =
11501 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11502 * nr_units * nr_columns);
11503 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11504 * nr_units * nr_columns))
11505 > index_end)
11506 {
11507 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11508 " [in module %s]"),
11509 dwp_file->name);
11510 }
11511 }
80626a55
DE
11512
11513 return htab;
11514}
11515
11516/* Update SECTIONS with the data from SECTP.
11517
11518 This function is like the other "locate" section routines that are
11519 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11520 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11521
11522 The result is non-zero for success, or zero if an error was found. */
11523
11524static int
73869dc2
DE
11525locate_v1_virtual_dwo_sections (asection *sectp,
11526 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11527{
11528 const struct dwop_section_names *names = &dwop_section_names;
11529
11530 if (section_is_p (sectp->name, &names->abbrev_dwo))
11531 {
11532 /* There can be only one. */
049412e3 11533 if (sections->abbrev.s.section != NULL)
80626a55 11534 return 0;
049412e3 11535 sections->abbrev.s.section = sectp;
fd361982 11536 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11537 }
11538 else if (section_is_p (sectp->name, &names->info_dwo)
11539 || section_is_p (sectp->name, &names->types_dwo))
11540 {
11541 /* There can be only one. */
049412e3 11542 if (sections->info_or_types.s.section != NULL)
80626a55 11543 return 0;
049412e3 11544 sections->info_or_types.s.section = sectp;
fd361982 11545 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11546 }
11547 else if (section_is_p (sectp->name, &names->line_dwo))
11548 {
11549 /* There can be only one. */
049412e3 11550 if (sections->line.s.section != NULL)
80626a55 11551 return 0;
049412e3 11552 sections->line.s.section = sectp;
fd361982 11553 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11554 }
11555 else if (section_is_p (sectp->name, &names->loc_dwo))
11556 {
11557 /* There can be only one. */
049412e3 11558 if (sections->loc.s.section != NULL)
80626a55 11559 return 0;
049412e3 11560 sections->loc.s.section = sectp;
fd361982 11561 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11562 }
11563 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11564 {
11565 /* There can be only one. */
049412e3 11566 if (sections->macinfo.s.section != NULL)
80626a55 11567 return 0;
049412e3 11568 sections->macinfo.s.section = sectp;
fd361982 11569 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11570 }
11571 else if (section_is_p (sectp->name, &names->macro_dwo))
11572 {
11573 /* There can be only one. */
049412e3 11574 if (sections->macro.s.section != NULL)
80626a55 11575 return 0;
049412e3 11576 sections->macro.s.section = sectp;
fd361982 11577 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11578 }
11579 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11580 {
11581 /* There can be only one. */
049412e3 11582 if (sections->str_offsets.s.section != NULL)
80626a55 11583 return 0;
049412e3 11584 sections->str_offsets.s.section = sectp;
fd361982 11585 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11586 }
11587 else
11588 {
11589 /* No other kind of section is valid. */
11590 return 0;
11591 }
11592
11593 return 1;
11594}
11595
73869dc2
DE
11596/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11597 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11598 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11599 This is for DWP version 1 files. */
80626a55
DE
11600
11601static struct dwo_unit *
ed2dc618
SM
11602create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11603 struct dwp_file *dwp_file,
73869dc2
DE
11604 uint32_t unit_index,
11605 const char *comp_dir,
11606 ULONGEST signature, int is_debug_types)
80626a55
DE
11607{
11608 struct objfile *objfile = dwarf2_per_objfile->objfile;
73869dc2
DE
11609 const struct dwp_hash_table *dwp_htab =
11610 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11611 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11612 const char *kind = is_debug_types ? "TU" : "CU";
11613 struct dwo_file *dwo_file;
11614 struct dwo_unit *dwo_unit;
73869dc2 11615 struct virtual_v1_dwo_sections sections;
80626a55 11616 void **dwo_file_slot;
80626a55
DE
11617 int i;
11618
73869dc2
DE
11619 gdb_assert (dwp_file->version == 1);
11620
b4f54984 11621 if (dwarf_read_debug)
80626a55 11622 {
73869dc2 11623 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11624 kind,
73869dc2 11625 pulongest (unit_index), hex_string (signature),
80626a55
DE
11626 dwp_file->name);
11627 }
11628
19ac8c2e 11629 /* Fetch the sections of this DWO unit.
80626a55
DE
11630 Put a limit on the number of sections we look for so that bad data
11631 doesn't cause us to loop forever. */
11632
73869dc2 11633#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11634 (1 /* .debug_info or .debug_types */ \
11635 + 1 /* .debug_abbrev */ \
11636 + 1 /* .debug_line */ \
11637 + 1 /* .debug_loc */ \
11638 + 1 /* .debug_str_offsets */ \
19ac8c2e 11639 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11640 + 1 /* trailing zero */)
11641
11642 memset (&sections, 0, sizeof (sections));
80626a55 11643
73869dc2 11644 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11645 {
11646 asection *sectp;
11647 uint32_t section_nr =
11648 read_4_bytes (dbfd,
73869dc2
DE
11649 dwp_htab->section_pool.v1.indices
11650 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11651
11652 if (section_nr == 0)
11653 break;
11654 if (section_nr >= dwp_file->num_sections)
11655 {
11656 error (_("Dwarf Error: bad DWP hash table, section number too large"
11657 " [in module %s]"),
11658 dwp_file->name);
11659 }
11660
11661 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11662 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11663 {
11664 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11665 " [in module %s]"),
11666 dwp_file->name);
11667 }
11668 }
11669
11670 if (i < 2
96b79293
TT
11671 || sections.info_or_types.empty ()
11672 || sections.abbrev.empty ())
80626a55
DE
11673 {
11674 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11675 " [in module %s]"),
11676 dwp_file->name);
11677 }
73869dc2 11678 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11679 {
11680 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11681 " [in module %s]"),
11682 dwp_file->name);
11683 }
11684
11685 /* It's easier for the rest of the code if we fake a struct dwo_file and
11686 have dwo_unit "live" in that. At least for now.
11687
11688 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11689 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11690 file, we can combine them back into a virtual DWO file to save space
11691 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11692 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11693
791afaa2
TT
11694 std::string virtual_dwo_name =
11695 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11696 sections.abbrev.get_id (),
11697 sections.line.get_id (),
11698 sections.loc.get_id (),
11699 sections.str_offsets.get_id ());
80626a55 11700 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11701 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11702 virtual_dwo_name.c_str (),
11703 comp_dir);
80626a55
DE
11704 /* Create one if necessary. */
11705 if (*dwo_file_slot == NULL)
11706 {
b4f54984 11707 if (dwarf_read_debug)
80626a55
DE
11708 {
11709 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11710 virtual_dwo_name.c_str ());
80626a55 11711 }
51ac9db5 11712 dwo_file = new struct dwo_file;
be1e3d3e 11713 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
0ac5b59e 11714 dwo_file->comp_dir = comp_dir;
80626a55
DE
11715 dwo_file->sections.abbrev = sections.abbrev;
11716 dwo_file->sections.line = sections.line;
11717 dwo_file->sections.loc = sections.loc;
11718 dwo_file->sections.macinfo = sections.macinfo;
11719 dwo_file->sections.macro = sections.macro;
11720 dwo_file->sections.str_offsets = sections.str_offsets;
11721 /* The "str" section is global to the entire DWP file. */
11722 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11723 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11724 there's no need to record it in dwo_file.
11725 Also, we can't simply record type sections in dwo_file because
11726 we record a pointer into the vector in dwo_unit. As we collect more
11727 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11728 for it, invalidating all copies of pointers into the previous
11729 contents. */
80626a55
DE
11730 *dwo_file_slot = dwo_file;
11731 }
11732 else
11733 {
b4f54984 11734 if (dwarf_read_debug)
80626a55
DE
11735 {
11736 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11737 virtual_dwo_name.c_str ());
80626a55 11738 }
9a3c8263 11739 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11740 }
80626a55
DE
11741
11742 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11743 dwo_unit->dwo_file = dwo_file;
11744 dwo_unit->signature = signature;
8d749320
SM
11745 dwo_unit->section =
11746 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
8a0459fd 11747 *dwo_unit->section = sections.info_or_types;
57d63ce2 11748 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11749
11750 return dwo_unit;
11751}
11752
73869dc2
DE
11753/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11754 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11755 piece within that section used by a TU/CU, return a virtual section
11756 of just that piece. */
11757
11758static struct dwarf2_section_info
ed2dc618
SM
11759create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11760 struct dwarf2_section_info *section,
73869dc2
DE
11761 bfd_size_type offset, bfd_size_type size)
11762{
11763 struct dwarf2_section_info result;
11764 asection *sectp;
11765
11766 gdb_assert (section != NULL);
11767 gdb_assert (!section->is_virtual);
11768
11769 memset (&result, 0, sizeof (result));
11770 result.s.containing_section = section;
dc4ccb6f 11771 result.is_virtual = true;
73869dc2
DE
11772
11773 if (size == 0)
11774 return result;
11775
96b79293 11776 sectp = section->get_bfd_section ();
73869dc2
DE
11777
11778 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11779 bounds of the real section. This is a pretty-rare event, so just
11780 flag an error (easier) instead of a warning and trying to cope. */
11781 if (sectp == NULL
fd361982 11782 || offset + size > bfd_section_size (sectp))
73869dc2 11783 {
73869dc2
DE
11784 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11785 " in section %s [in module %s]"),
fd361982 11786 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11787 objfile_name (dwarf2_per_objfile->objfile));
11788 }
11789
11790 result.virtual_offset = offset;
11791 result.size = size;
11792 return result;
11793}
11794
11795/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11796 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11797 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11798 This is for DWP version 2 files. */
11799
11800static struct dwo_unit *
ed2dc618
SM
11801create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11802 struct dwp_file *dwp_file,
73869dc2
DE
11803 uint32_t unit_index,
11804 const char *comp_dir,
11805 ULONGEST signature, int is_debug_types)
11806{
11807 struct objfile *objfile = dwarf2_per_objfile->objfile;
11808 const struct dwp_hash_table *dwp_htab =
11809 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11810 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11811 const char *kind = is_debug_types ? "TU" : "CU";
11812 struct dwo_file *dwo_file;
11813 struct dwo_unit *dwo_unit;
11814 struct virtual_v2_dwo_sections sections;
11815 void **dwo_file_slot;
73869dc2
DE
11816 int i;
11817
11818 gdb_assert (dwp_file->version == 2);
11819
b4f54984 11820 if (dwarf_read_debug)
73869dc2
DE
11821 {
11822 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11823 kind,
11824 pulongest (unit_index), hex_string (signature),
11825 dwp_file->name);
11826 }
11827
11828 /* Fetch the section offsets of this DWO unit. */
11829
11830 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11831
11832 for (i = 0; i < dwp_htab->nr_columns; ++i)
11833 {
11834 uint32_t offset = read_4_bytes (dbfd,
11835 dwp_htab->section_pool.v2.offsets
11836 + (((unit_index - 1) * dwp_htab->nr_columns
11837 + i)
11838 * sizeof (uint32_t)));
11839 uint32_t size = read_4_bytes (dbfd,
11840 dwp_htab->section_pool.v2.sizes
11841 + (((unit_index - 1) * dwp_htab->nr_columns
11842 + i)
11843 * sizeof (uint32_t)));
11844
11845 switch (dwp_htab->section_pool.v2.section_ids[i])
11846 {
11847 case DW_SECT_INFO:
11848 case DW_SECT_TYPES:
11849 sections.info_or_types_offset = offset;
11850 sections.info_or_types_size = size;
11851 break;
11852 case DW_SECT_ABBREV:
11853 sections.abbrev_offset = offset;
11854 sections.abbrev_size = size;
11855 break;
11856 case DW_SECT_LINE:
11857 sections.line_offset = offset;
11858 sections.line_size = size;
11859 break;
11860 case DW_SECT_LOC:
11861 sections.loc_offset = offset;
11862 sections.loc_size = size;
11863 break;
11864 case DW_SECT_STR_OFFSETS:
11865 sections.str_offsets_offset = offset;
11866 sections.str_offsets_size = size;
11867 break;
11868 case DW_SECT_MACINFO:
11869 sections.macinfo_offset = offset;
11870 sections.macinfo_size = size;
11871 break;
11872 case DW_SECT_MACRO:
11873 sections.macro_offset = offset;
11874 sections.macro_size = size;
11875 break;
11876 }
11877 }
11878
11879 /* It's easier for the rest of the code if we fake a struct dwo_file and
11880 have dwo_unit "live" in that. At least for now.
11881
11882 The DWP file can be made up of a random collection of CUs and TUs.
11883 However, for each CU + set of TUs that came from the same original DWO
11884 file, we can combine them back into a virtual DWO file to save space
11885 (fewer struct dwo_file objects to allocate). Remember that for really
11886 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11887
791afaa2
TT
11888 std::string virtual_dwo_name =
11889 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
11890 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
11891 (long) (sections.line_size ? sections.line_offset : 0),
11892 (long) (sections.loc_size ? sections.loc_offset : 0),
11893 (long) (sections.str_offsets_size
11894 ? sections.str_offsets_offset : 0));
73869dc2 11895 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11896 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11897 virtual_dwo_name.c_str (),
11898 comp_dir);
73869dc2
DE
11899 /* Create one if necessary. */
11900 if (*dwo_file_slot == NULL)
11901 {
b4f54984 11902 if (dwarf_read_debug)
73869dc2
DE
11903 {
11904 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11905 virtual_dwo_name.c_str ());
73869dc2 11906 }
51ac9db5 11907 dwo_file = new struct dwo_file;
be1e3d3e 11908 dwo_file->dwo_name = objfile->intern (virtual_dwo_name);
73869dc2
DE
11909 dwo_file->comp_dir = comp_dir;
11910 dwo_file->sections.abbrev =
ed2dc618 11911 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
11912 sections.abbrev_offset, sections.abbrev_size);
11913 dwo_file->sections.line =
ed2dc618 11914 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
11915 sections.line_offset, sections.line_size);
11916 dwo_file->sections.loc =
ed2dc618 11917 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
11918 sections.loc_offset, sections.loc_size);
11919 dwo_file->sections.macinfo =
ed2dc618 11920 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
11921 sections.macinfo_offset, sections.macinfo_size);
11922 dwo_file->sections.macro =
ed2dc618 11923 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
11924 sections.macro_offset, sections.macro_size);
11925 dwo_file->sections.str_offsets =
ed2dc618
SM
11926 create_dwp_v2_section (dwarf2_per_objfile,
11927 &dwp_file->sections.str_offsets,
73869dc2
DE
11928 sections.str_offsets_offset,
11929 sections.str_offsets_size);
11930 /* The "str" section is global to the entire DWP file. */
11931 dwo_file->sections.str = dwp_file->sections.str;
11932 /* The info or types section is assigned below to dwo_unit,
11933 there's no need to record it in dwo_file.
11934 Also, we can't simply record type sections in dwo_file because
11935 we record a pointer into the vector in dwo_unit. As we collect more
11936 types we'll grow the vector and eventually have to reallocate space
11937 for it, invalidating all copies of pointers into the previous
11938 contents. */
11939 *dwo_file_slot = dwo_file;
11940 }
11941 else
11942 {
b4f54984 11943 if (dwarf_read_debug)
73869dc2
DE
11944 {
11945 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11946 virtual_dwo_name.c_str ());
73869dc2 11947 }
9a3c8263 11948 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 11949 }
73869dc2
DE
11950
11951 dwo_unit = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct dwo_unit);
11952 dwo_unit->dwo_file = dwo_file;
11953 dwo_unit->signature = signature;
8d749320
SM
11954 dwo_unit->section =
11955 XOBNEW (&objfile->objfile_obstack, struct dwarf2_section_info);
ed2dc618
SM
11956 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
11957 is_debug_types
73869dc2
DE
11958 ? &dwp_file->sections.types
11959 : &dwp_file->sections.info,
11960 sections.info_or_types_offset,
11961 sections.info_or_types_size);
11962 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
11963
11964 return dwo_unit;
11965}
11966
57d63ce2
DE
11967/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
11968 Returns NULL if the signature isn't found. */
80626a55
DE
11969
11970static struct dwo_unit *
ed2dc618
SM
11971lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
11972 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 11973 ULONGEST signature, int is_debug_types)
80626a55 11974{
57d63ce2
DE
11975 const struct dwp_hash_table *dwp_htab =
11976 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11977 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 11978 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
11979 uint32_t hash = signature & mask;
11980 uint32_t hash2 = ((signature >> 32) & mask) | 1;
11981 unsigned int i;
11982 void **slot;
870f88f7 11983 struct dwo_unit find_dwo_cu;
80626a55
DE
11984
11985 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
11986 find_dwo_cu.signature = signature;
19ac8c2e 11987 slot = htab_find_slot (is_debug_types
48b490f2
TT
11988 ? dwp_file->loaded_tus.get ()
11989 : dwp_file->loaded_cus.get (),
19ac8c2e 11990 &find_dwo_cu, INSERT);
80626a55
DE
11991
11992 if (*slot != NULL)
9a3c8263 11993 return (struct dwo_unit *) *slot;
80626a55
DE
11994
11995 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 11996 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
11997 {
11998 ULONGEST signature_in_table;
11999
12000 signature_in_table =
57d63ce2 12001 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12002 if (signature_in_table == signature)
12003 {
57d63ce2
DE
12004 uint32_t unit_index =
12005 read_4_bytes (dbfd,
12006 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12007
73869dc2
DE
12008 if (dwp_file->version == 1)
12009 {
ed2dc618
SM
12010 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12011 dwp_file, unit_index,
73869dc2
DE
12012 comp_dir, signature,
12013 is_debug_types);
12014 }
12015 else
12016 {
ed2dc618
SM
12017 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12018 dwp_file, unit_index,
73869dc2
DE
12019 comp_dir, signature,
12020 is_debug_types);
12021 }
9a3c8263 12022 return (struct dwo_unit *) *slot;
80626a55
DE
12023 }
12024 if (signature_in_table == 0)
12025 return NULL;
12026 hash = (hash + hash2) & mask;
12027 }
12028
12029 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12030 " [in module %s]"),
12031 dwp_file->name);
12032}
12033
ab5088bf 12034/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12035 Open the file specified by FILE_NAME and hand it off to BFD for
12036 preliminary analysis. Return a newly initialized bfd *, which
12037 includes a canonicalized copy of FILE_NAME.
80626a55 12038 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12039 SEARCH_CWD is true if the current directory is to be searched.
12040 It will be searched before debug-file-directory.
13aaf454
DE
12041 If successful, the file is added to the bfd include table of the
12042 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12043 If unable to find/open the file, return NULL.
3019eac3
DE
12044 NOTE: This function is derived from symfile_bfd_open. */
12045
192b62ce 12046static gdb_bfd_ref_ptr
ed2dc618
SM
12047try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12048 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12049{
24b9144d 12050 int desc;
9c02c129
DE
12051 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12052 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12053 to debug_file_directory. */
e0cc99a6 12054 const char *search_path;
9c02c129
DE
12055 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12056
e0cc99a6 12057 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12058 if (search_cwd)
12059 {
12060 if (*debug_file_directory != '\0')
e0cc99a6
TT
12061 {
12062 search_path_holder.reset (concat (".", dirname_separator_string,
12063 debug_file_directory,
12064 (char *) NULL));
12065 search_path = search_path_holder.get ();
12066 }
6ac97d4c 12067 else
e0cc99a6 12068 search_path = ".";
6ac97d4c 12069 }
9c02c129 12070 else
e0cc99a6 12071 search_path = debug_file_directory;
3019eac3 12072
24b9144d 12073 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12074 if (is_dwp)
12075 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12076
12077 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12078 desc = openp (search_path, flags, file_name,
3019eac3
DE
12079 O_RDONLY | O_BINARY, &absolute_name);
12080 if (desc < 0)
12081 return NULL;
12082
e0cc99a6
TT
12083 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12084 gnutarget, desc));
9c02c129
DE
12085 if (sym_bfd == NULL)
12086 return NULL;
192b62ce 12087 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12088
192b62ce
TT
12089 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12090 return NULL;
3019eac3 12091
13aaf454
DE
12092 /* Success. Record the bfd as having been included by the objfile's bfd.
12093 This is important because things like demangled_names_hash lives in the
12094 objfile's per_bfd space and may have references to things like symbol
12095 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12096 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12097
3019eac3
DE
12098 return sym_bfd;
12099}
12100
ab5088bf 12101/* Try to open DWO file FILE_NAME.
3019eac3
DE
12102 COMP_DIR is the DW_AT_comp_dir attribute.
12103 The result is the bfd handle of the file.
12104 If there is a problem finding or opening the file, return NULL.
12105 Upon success, the canonicalized path of the file is stored in the bfd,
12106 same as symfile_bfd_open. */
12107
192b62ce 12108static gdb_bfd_ref_ptr
ed2dc618
SM
12109open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12110 const char *file_name, const char *comp_dir)
3019eac3 12111{
80626a55 12112 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12113 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12114 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12115
12116 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12117
12118 if (comp_dir != NULL)
12119 {
43816ebc
TT
12120 gdb::unique_xmalloc_ptr<char> path_to_try
12121 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12122
12123 /* NOTE: If comp_dir is a relative path, this will also try the
12124 search path, which seems useful. */
ed2dc618 12125 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12126 path_to_try.get (),
ed2dc618 12127 0 /*is_dwp*/,
192b62ce 12128 1 /*search_cwd*/));
3019eac3
DE
12129 if (abfd != NULL)
12130 return abfd;
12131 }
12132
12133 /* That didn't work, try debug-file-directory, which, despite its name,
12134 is a list of paths. */
12135
12136 if (*debug_file_directory == '\0')
12137 return NULL;
12138
ed2dc618
SM
12139 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12140 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12141}
12142
80626a55
DE
12143/* This function is mapped across the sections and remembers the offset and
12144 size of each of the DWO debugging sections we are interested in. */
12145
12146static void
12147dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12148{
9a3c8263 12149 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12150 const struct dwop_section_names *names = &dwop_section_names;
12151
12152 if (section_is_p (sectp->name, &names->abbrev_dwo))
12153 {
049412e3 12154 dwo_sections->abbrev.s.section = sectp;
fd361982 12155 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12156 }
12157 else if (section_is_p (sectp->name, &names->info_dwo))
12158 {
049412e3 12159 dwo_sections->info.s.section = sectp;
fd361982 12160 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12161 }
12162 else if (section_is_p (sectp->name, &names->line_dwo))
12163 {
049412e3 12164 dwo_sections->line.s.section = sectp;
fd361982 12165 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12166 }
12167 else if (section_is_p (sectp->name, &names->loc_dwo))
12168 {
049412e3 12169 dwo_sections->loc.s.section = sectp;
fd361982 12170 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12171 }
41144253 12172 else if (section_is_p (sectp->name, &names->loclists_dwo))
12173 {
12174 dwo_sections->loclists.s.section = sectp;
12175 dwo_sections->loclists.size = bfd_section_size (sectp);
12176 }
80626a55
DE
12177 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12178 {
049412e3 12179 dwo_sections->macinfo.s.section = sectp;
fd361982 12180 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12181 }
12182 else if (section_is_p (sectp->name, &names->macro_dwo))
12183 {
049412e3 12184 dwo_sections->macro.s.section = sectp;
fd361982 12185 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12186 }
12187 else if (section_is_p (sectp->name, &names->str_dwo))
12188 {
049412e3 12189 dwo_sections->str.s.section = sectp;
fd361982 12190 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12191 }
12192 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12193 {
049412e3 12194 dwo_sections->str_offsets.s.section = sectp;
fd361982 12195 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12196 }
12197 else if (section_is_p (sectp->name, &names->types_dwo))
12198 {
12199 struct dwarf2_section_info type_section;
12200
12201 memset (&type_section, 0, sizeof (type_section));
049412e3 12202 type_section.s.section = sectp;
fd361982 12203 type_section.size = bfd_section_size (sectp);
fd5866f6 12204 dwo_sections->types.push_back (type_section);
80626a55
DE
12205 }
12206}
12207
ab5088bf 12208/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12209 by PER_CU. This is for the non-DWP case.
80626a55 12210 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12211
12212static struct dwo_file *
0ac5b59e
DE
12213open_and_init_dwo_file (struct dwarf2_per_cu_data *per_cu,
12214 const char *dwo_name, const char *comp_dir)
3019eac3 12215{
ed2dc618 12216 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 12217
fb1eb2f9 12218 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12219 if (dbfd == NULL)
12220 {
b4f54984 12221 if (dwarf_read_debug)
80626a55
DE
12222 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12223 return NULL;
12224 }
263db9a1 12225
51ac9db5 12226 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12227 dwo_file->dwo_name = dwo_name;
12228 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12229 dwo_file->dbfd = std::move (dbfd);
3019eac3 12230
fb1eb2f9 12231 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12232 &dwo_file->sections);
3019eac3 12233
18a8505e
AT
12234 create_cus_hash_table (dwarf2_per_objfile, per_cu->cu, *dwo_file,
12235 dwo_file->sections.info, dwo_file->cus);
3019eac3 12236
263db9a1 12237 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12238 dwo_file->sections.types, dwo_file->tus);
3019eac3 12239
b4f54984 12240 if (dwarf_read_debug)
80626a55
DE
12241 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12242
263db9a1 12243 return dwo_file.release ();
3019eac3
DE
12244}
12245
80626a55 12246/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12247 size of each of the DWP debugging sections common to version 1 and 2 that
12248 we are interested in. */
3019eac3 12249
80626a55 12250static void
73869dc2
DE
12251dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12252 void *dwp_file_ptr)
3019eac3 12253{
9a3c8263 12254 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12255 const struct dwop_section_names *names = &dwop_section_names;
12256 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12257
80626a55 12258 /* Record the ELF section number for later lookup: this is what the
73869dc2 12259 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12260 gdb_assert (elf_section_nr < dwp_file->num_sections);
12261 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12262
80626a55
DE
12263 /* Look for specific sections that we need. */
12264 if (section_is_p (sectp->name, &names->str_dwo))
12265 {
049412e3 12266 dwp_file->sections.str.s.section = sectp;
fd361982 12267 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12268 }
12269 else if (section_is_p (sectp->name, &names->cu_index))
12270 {
049412e3 12271 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12272 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12273 }
12274 else if (section_is_p (sectp->name, &names->tu_index))
12275 {
049412e3 12276 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12277 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12278 }
12279}
3019eac3 12280
73869dc2
DE
12281/* This function is mapped across the sections and remembers the offset and
12282 size of each of the DWP version 2 debugging sections that we are interested
12283 in. This is split into a separate function because we don't know if we
12284 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12285
12286static void
12287dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12288{
9a3c8263 12289 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12290 const struct dwop_section_names *names = &dwop_section_names;
12291 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12292
12293 /* Record the ELF section number for later lookup: this is what the
12294 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12295 gdb_assert (elf_section_nr < dwp_file->num_sections);
12296 dwp_file->elf_sections[elf_section_nr] = sectp;
12297
12298 /* Look for specific sections that we need. */
12299 if (section_is_p (sectp->name, &names->abbrev_dwo))
12300 {
049412e3 12301 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12302 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12303 }
12304 else if (section_is_p (sectp->name, &names->info_dwo))
12305 {
049412e3 12306 dwp_file->sections.info.s.section = sectp;
fd361982 12307 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12308 }
12309 else if (section_is_p (sectp->name, &names->line_dwo))
12310 {
049412e3 12311 dwp_file->sections.line.s.section = sectp;
fd361982 12312 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12313 }
12314 else if (section_is_p (sectp->name, &names->loc_dwo))
12315 {
049412e3 12316 dwp_file->sections.loc.s.section = sectp;
fd361982 12317 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12318 }
12319 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12320 {
049412e3 12321 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12322 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12323 }
12324 else if (section_is_p (sectp->name, &names->macro_dwo))
12325 {
049412e3 12326 dwp_file->sections.macro.s.section = sectp;
fd361982 12327 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12328 }
12329 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12330 {
049412e3 12331 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12332 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12333 }
12334 else if (section_is_p (sectp->name, &names->types_dwo))
12335 {
049412e3 12336 dwp_file->sections.types.s.section = sectp;
fd361982 12337 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12338 }
12339}
12340
80626a55 12341/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12342
80626a55
DE
12343static hashval_t
12344hash_dwp_loaded_cutus (const void *item)
12345{
9a3c8263 12346 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12347
80626a55
DE
12348 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12349 return dwo_unit->signature;
3019eac3
DE
12350}
12351
80626a55 12352/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12353
80626a55
DE
12354static int
12355eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12356{
9a3c8263
SM
12357 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12358 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12359
80626a55
DE
12360 return dua->signature == dub->signature;
12361}
3019eac3 12362
80626a55 12363/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12364
48b490f2 12365static htab_up
298e9637 12366allocate_dwp_loaded_cutus_table ()
80626a55 12367{
48b490f2
TT
12368 return htab_up (htab_create_alloc (3,
12369 hash_dwp_loaded_cutus,
12370 eq_dwp_loaded_cutus,
12371 NULL, xcalloc, xfree));
80626a55 12372}
3019eac3 12373
ab5088bf
DE
12374/* Try to open DWP file FILE_NAME.
12375 The result is the bfd handle of the file.
12376 If there is a problem finding or opening the file, return NULL.
12377 Upon success, the canonicalized path of the file is stored in the bfd,
12378 same as symfile_bfd_open. */
12379
192b62ce 12380static gdb_bfd_ref_ptr
ed2dc618
SM
12381open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12382 const char *file_name)
ab5088bf 12383{
ed2dc618
SM
12384 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12385 1 /*is_dwp*/,
192b62ce 12386 1 /*search_cwd*/));
6ac97d4c
DE
12387 if (abfd != NULL)
12388 return abfd;
12389
12390 /* Work around upstream bug 15652.
12391 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12392 [Whether that's a "bug" is debatable, but it is getting in our way.]
12393 We have no real idea where the dwp file is, because gdb's realpath-ing
12394 of the executable's path may have discarded the needed info.
12395 [IWBN if the dwp file name was recorded in the executable, akin to
12396 .gnu_debuglink, but that doesn't exist yet.]
12397 Strip the directory from FILE_NAME and search again. */
12398 if (*debug_file_directory != '\0')
12399 {
12400 /* Don't implicitly search the current directory here.
12401 If the user wants to search "." to handle this case,
12402 it must be added to debug-file-directory. */
ed2dc618
SM
12403 return try_open_dwop_file (dwarf2_per_objfile,
12404 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12405 0 /*search_cwd*/);
12406 }
12407
12408 return NULL;
ab5088bf
DE
12409}
12410
80626a55
DE
12411/* Initialize the use of the DWP file for the current objfile.
12412 By convention the name of the DWP file is ${objfile}.dwp.
12413 The result is NULL if it can't be found. */
a766d390 12414
400174b1 12415static std::unique_ptr<struct dwp_file>
ed2dc618 12416open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12417{
12418 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12419
82bf32bc
JK
12420 /* Try to find first .dwp for the binary file before any symbolic links
12421 resolving. */
6c447423
DE
12422
12423 /* If the objfile is a debug file, find the name of the real binary
12424 file and get the name of dwp file from there. */
d721ba37 12425 std::string dwp_name;
6c447423
DE
12426 if (objfile->separate_debug_objfile_backlink != NULL)
12427 {
12428 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12429 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12430
d721ba37 12431 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12432 }
12433 else
d721ba37
PA
12434 dwp_name = objfile->original_name;
12435
12436 dwp_name += ".dwp";
80626a55 12437
ed2dc618 12438 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12439 if (dbfd == NULL
12440 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12441 {
12442 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12443 dwp_name = objfile_name (objfile);
12444 dwp_name += ".dwp";
ed2dc618 12445 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12446 }
12447
80626a55
DE
12448 if (dbfd == NULL)
12449 {
b4f54984 12450 if (dwarf_read_debug)
d721ba37 12451 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12452 return std::unique_ptr<dwp_file> ();
3019eac3 12453 }
400174b1
TT
12454
12455 const char *name = bfd_get_filename (dbfd.get ());
12456 std::unique_ptr<struct dwp_file> dwp_file
12457 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12458
0a0f4c01 12459 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55
DE
12460 dwp_file->elf_sections =
12461 OBSTACK_CALLOC (&objfile->objfile_obstack,
12462 dwp_file->num_sections, asection *);
12463
400174b1
TT
12464 bfd_map_over_sections (dwp_file->dbfd.get (),
12465 dwarf2_locate_common_dwp_sections,
12466 dwp_file.get ());
80626a55 12467
400174b1
TT
12468 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12469 0);
80626a55 12470
400174b1
TT
12471 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12472 1);
80626a55 12473
73869dc2 12474 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12475 if (dwp_file->cus && dwp_file->tus
12476 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12477 {
12478 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12479 pretty bizarre. We use pulongest here because that's the established
4d65956b 12480 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12481 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12482 " TU version %s [in DWP file %s]"),
12483 pulongest (dwp_file->cus->version),
d721ba37 12484 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12485 }
08302ed2
DE
12486
12487 if (dwp_file->cus)
12488 dwp_file->version = dwp_file->cus->version;
12489 else if (dwp_file->tus)
12490 dwp_file->version = dwp_file->tus->version;
12491 else
12492 dwp_file->version = 2;
73869dc2
DE
12493
12494 if (dwp_file->version == 2)
400174b1
TT
12495 bfd_map_over_sections (dwp_file->dbfd.get (),
12496 dwarf2_locate_v2_dwp_sections,
12497 dwp_file.get ());
73869dc2 12498
298e9637
SM
12499 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12500 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12501
b4f54984 12502 if (dwarf_read_debug)
80626a55
DE
12503 {
12504 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12505 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12506 " %s CUs, %s TUs\n",
12507 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12508 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12509 }
12510
12511 return dwp_file;
3019eac3 12512}
c906108c 12513
ab5088bf
DE
12514/* Wrapper around open_and_init_dwp_file, only open it once. */
12515
12516static struct dwp_file *
ed2dc618 12517get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf
DE
12518{
12519 if (! dwarf2_per_objfile->dwp_checked)
12520 {
ed2dc618
SM
12521 dwarf2_per_objfile->dwp_file
12522 = open_and_init_dwp_file (dwarf2_per_objfile);
ab5088bf
DE
12523 dwarf2_per_objfile->dwp_checked = 1;
12524 }
400174b1 12525 return dwarf2_per_objfile->dwp_file.get ();
ab5088bf
DE
12526}
12527
80626a55
DE
12528/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12529 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12530 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12531 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12532 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12533
12534 This is called, for example, when wanting to read a variable with a
12535 complex location. Therefore we don't want to do file i/o for every call.
12536 Therefore we don't want to look for a DWO file on every call.
12537 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12538 then we check if we've already seen DWO_NAME, and only THEN do we check
12539 for a DWO file.
12540
1c658ad5 12541 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12542 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12543
3019eac3 12544static struct dwo_unit *
80626a55
DE
12545lookup_dwo_cutu (struct dwarf2_per_cu_data *this_unit,
12546 const char *dwo_name, const char *comp_dir,
12547 ULONGEST signature, int is_debug_types)
3019eac3 12548{
ed2dc618 12549 struct dwarf2_per_objfile *dwarf2_per_objfile = this_unit->dwarf2_per_objfile;
3019eac3 12550 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12551 const char *kind = is_debug_types ? "TU" : "CU";
12552 void **dwo_file_slot;
3019eac3 12553 struct dwo_file *dwo_file;
80626a55 12554 struct dwp_file *dwp_file;
cb1df416 12555
6a506a2d
DE
12556 /* First see if there's a DWP file.
12557 If we have a DWP file but didn't find the DWO inside it, don't
12558 look for the original DWO file. It makes gdb behave differently
12559 depending on whether one is debugging in the build tree. */
cf2c3c16 12560
ed2dc618 12561 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12562 if (dwp_file != NULL)
cf2c3c16 12563 {
80626a55
DE
12564 const struct dwp_hash_table *dwp_htab =
12565 is_debug_types ? dwp_file->tus : dwp_file->cus;
12566
12567 if (dwp_htab != NULL)
12568 {
12569 struct dwo_unit *dwo_cutu =
ed2dc618 12570 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12571 signature, is_debug_types);
80626a55
DE
12572
12573 if (dwo_cutu != NULL)
12574 {
b4f54984 12575 if (dwarf_read_debug)
80626a55
DE
12576 {
12577 fprintf_unfiltered (gdb_stdlog,
12578 "Virtual DWO %s %s found: @%s\n",
12579 kind, hex_string (signature),
12580 host_address_to_string (dwo_cutu));
12581 }
12582 return dwo_cutu;
12583 }
12584 }
12585 }
6a506a2d 12586 else
80626a55 12587 {
6a506a2d 12588 /* No DWP file, look for the DWO file. */
80626a55 12589
ed2dc618
SM
12590 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12591 dwo_name, comp_dir);
6a506a2d 12592 if (*dwo_file_slot == NULL)
80626a55 12593 {
6a506a2d
DE
12594 /* Read in the file and build a table of the CUs/TUs it contains. */
12595 *dwo_file_slot = open_and_init_dwo_file (this_unit, dwo_name, comp_dir);
19c3d4c9 12596 }
6a506a2d 12597 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12598 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12599
6a506a2d 12600 if (dwo_file != NULL)
19c3d4c9 12601 {
6a506a2d
DE
12602 struct dwo_unit *dwo_cutu = NULL;
12603
12604 if (is_debug_types && dwo_file->tus)
12605 {
12606 struct dwo_unit find_dwo_cutu;
12607
12608 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12609 find_dwo_cutu.signature = signature;
9a3c8263 12610 dwo_cutu
b0b6a987
TT
12611 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12612 &find_dwo_cutu);
6a506a2d 12613 }
33c5cd75 12614 else if (!is_debug_types && dwo_file->cus)
80626a55 12615 {
33c5cd75
DB
12616 struct dwo_unit find_dwo_cutu;
12617
12618 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12619 find_dwo_cutu.signature = signature;
b0b6a987 12620 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12621 &find_dwo_cutu);
6a506a2d
DE
12622 }
12623
12624 if (dwo_cutu != NULL)
12625 {
b4f54984 12626 if (dwarf_read_debug)
6a506a2d
DE
12627 {
12628 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12629 kind, dwo_name, hex_string (signature),
12630 host_address_to_string (dwo_cutu));
12631 }
12632 return dwo_cutu;
80626a55
DE
12633 }
12634 }
2e276125 12635 }
9cdd5dbd 12636
80626a55
DE
12637 /* We didn't find it. This could mean a dwo_id mismatch, or
12638 someone deleted the DWO/DWP file, or the search path isn't set up
12639 correctly to find the file. */
12640
b4f54984 12641 if (dwarf_read_debug)
80626a55
DE
12642 {
12643 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12644 kind, dwo_name, hex_string (signature));
12645 }
3019eac3 12646
6656a72d
DE
12647 /* This is a warning and not a complaint because it can be caused by
12648 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12649 {
12650 /* Print the name of the DWP file if we looked there, helps the user
12651 better diagnose the problem. */
791afaa2 12652 std::string dwp_text;
43942612
DE
12653
12654 if (dwp_file != NULL)
791afaa2
TT
12655 dwp_text = string_printf (" [in DWP file %s]",
12656 lbasename (dwp_file->name));
43942612 12657
9d8780f0 12658 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612
DE
12659 " [in module %s]"),
12660 kind, dwo_name, hex_string (signature),
791afaa2 12661 dwp_text.c_str (),
43942612 12662 this_unit->is_debug_types ? "TU" : "CU",
9d8780f0 12663 sect_offset_str (this_unit->sect_off), objfile_name (objfile));
43942612 12664 }
3019eac3 12665 return NULL;
5fb290d7
DJ
12666}
12667
80626a55
DE
12668/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12669 See lookup_dwo_cutu_unit for details. */
12670
12671static struct dwo_unit *
12672lookup_dwo_comp_unit (struct dwarf2_per_cu_data *this_cu,
12673 const char *dwo_name, const char *comp_dir,
12674 ULONGEST signature)
12675{
12676 return lookup_dwo_cutu (this_cu, dwo_name, comp_dir, signature, 0);
12677}
12678
12679/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12680 See lookup_dwo_cutu_unit for details. */
12681
12682static struct dwo_unit *
12683lookup_dwo_type_unit (struct signatured_type *this_tu,
12684 const char *dwo_name, const char *comp_dir)
12685{
12686 return lookup_dwo_cutu (&this_tu->per_cu, dwo_name, comp_dir, this_tu->signature, 1);
12687}
12688
89e63ee4
DE
12689/* Traversal function for queue_and_load_all_dwo_tus. */
12690
12691static int
12692queue_and_load_dwo_tu (void **slot, void *info)
12693{
12694 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
12695 struct dwarf2_per_cu_data *per_cu = (struct dwarf2_per_cu_data *) info;
12696 ULONGEST signature = dwo_unit->signature;
12697 struct signatured_type *sig_type =
12698 lookup_dwo_signatured_type (per_cu->cu, signature);
12699
12700 if (sig_type != NULL)
12701 {
12702 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12703
12704 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12705 a real dependency of PER_CU on SIG_TYPE. That is detected later
12706 while processing PER_CU. */
12707 if (maybe_queue_comp_unit (NULL, sig_cu, per_cu->cu->language))
12708 load_full_type_unit (sig_cu);
ae640021 12709 per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12710 }
12711
12712 return 1;
12713}
12714
12715/* Queue all TUs contained in the DWO of PER_CU to be read in.
12716 The DWO may have the only definition of the type, though it may not be
12717 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12718 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12719
12720static void
12721queue_and_load_all_dwo_tus (struct dwarf2_per_cu_data *per_cu)
12722{
12723 struct dwo_unit *dwo_unit;
12724 struct dwo_file *dwo_file;
12725
12726 gdb_assert (!per_cu->is_debug_types);
ed2dc618 12727 gdb_assert (get_dwp_file (per_cu->dwarf2_per_objfile) == NULL);
89e63ee4
DE
12728 gdb_assert (per_cu->cu != NULL);
12729
12730 dwo_unit = per_cu->cu->dwo_unit;
12731 gdb_assert (dwo_unit != NULL);
12732
12733 dwo_file = dwo_unit->dwo_file;
12734 if (dwo_file->tus != NULL)
b0b6a987
TT
12735 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu,
12736 per_cu);
89e63ee4
DE
12737}
12738
3019eac3 12739/* Read in various DIEs. */
348e048f 12740
d389af10 12741/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12742 Inherit only the children of the DW_AT_abstract_origin DIE not being
12743 already referenced by DW_AT_abstract_origin from the children of the
12744 current DIE. */
d389af10
JK
12745
12746static void
12747inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12748{
12749 struct die_info *child_die;
791afaa2 12750 sect_offset *offsetp;
d389af10
JK
12751 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12752 struct die_info *origin_die;
12753 /* Iterator of the ORIGIN_DIE children. */
12754 struct die_info *origin_child_die;
d389af10 12755 struct attribute *attr;
cd02d79d
PA
12756 struct dwarf2_cu *origin_cu;
12757 struct pending **origin_previous_list_in_scope;
d389af10
JK
12758
12759 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12760 if (!attr)
12761 return;
12762
cd02d79d
PA
12763 /* Note that following die references may follow to a die in a
12764 different cu. */
12765
12766 origin_cu = cu;
12767 origin_die = follow_die_ref (die, attr, &origin_cu);
12768
12769 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12770 symbols in. */
12771 origin_previous_list_in_scope = origin_cu->list_in_scope;
12772 origin_cu->list_in_scope = cu->list_in_scope;
12773
edb3359d
DJ
12774 if (die->tag != origin_die->tag
12775 && !(die->tag == DW_TAG_inlined_subroutine
12776 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12777 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12778 sect_offset_str (die->sect_off),
12779 sect_offset_str (origin_die->sect_off));
d389af10 12780
791afaa2 12781 std::vector<sect_offset> offsets;
d389af10 12782
3ea89b92
PMR
12783 for (child_die = die->child;
12784 child_die && child_die->tag;
436c571c 12785 child_die = child_die->sibling)
3ea89b92
PMR
12786 {
12787 struct die_info *child_origin_die;
12788 struct dwarf2_cu *child_origin_cu;
12789
12790 /* We are trying to process concrete instance entries:
216f72a1 12791 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12792 it's not relevant to our analysis here. i.e. detecting DIEs that are
12793 present in the abstract instance but not referenced in the concrete
12794 one. */
216f72a1
JK
12795 if (child_die->tag == DW_TAG_call_site
12796 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12797 continue;
12798
c38f313d
DJ
12799 /* For each CHILD_DIE, find the corresponding child of
12800 ORIGIN_DIE. If there is more than one layer of
12801 DW_AT_abstract_origin, follow them all; there shouldn't be,
12802 but GCC versions at least through 4.4 generate this (GCC PR
12803 40573). */
3ea89b92
PMR
12804 child_origin_die = child_die;
12805 child_origin_cu = cu;
c38f313d
DJ
12806 while (1)
12807 {
cd02d79d
PA
12808 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12809 child_origin_cu);
c38f313d
DJ
12810 if (attr == NULL)
12811 break;
cd02d79d
PA
12812 child_origin_die = follow_die_ref (child_origin_die, attr,
12813 &child_origin_cu);
c38f313d
DJ
12814 }
12815
d389af10
JK
12816 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12817 counterpart may exist. */
c38f313d 12818 if (child_origin_die != child_die)
d389af10 12819 {
edb3359d
DJ
12820 if (child_die->tag != child_origin_die->tag
12821 && !(child_die->tag == DW_TAG_inlined_subroutine
12822 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12823 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12824 "different tags"),
9d8780f0
SM
12825 sect_offset_str (child_die->sect_off),
12826 sect_offset_str (child_origin_die->sect_off));
c38f313d 12827 if (child_origin_die->parent != origin_die)
b98664d3 12828 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12829 "different parents"),
9d8780f0
SM
12830 sect_offset_str (child_die->sect_off),
12831 sect_offset_str (child_origin_die->sect_off));
c38f313d 12832 else
791afaa2 12833 offsets.push_back (child_origin_die->sect_off);
d389af10 12834 }
d389af10 12835 }
791afaa2
TT
12836 std::sort (offsets.begin (), offsets.end ());
12837 sect_offset *offsets_end = offsets.data () + offsets.size ();
12838 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12839 if (offsetp[-1] == *offsetp)
b98664d3 12840 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12841 "to DIE %s as their abstract origin"),
12842 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12843
791afaa2 12844 offsetp = offsets.data ();
d389af10
JK
12845 origin_child_die = origin_die->child;
12846 while (origin_child_die && origin_child_die->tag)
12847 {
12848 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 12849 while (offsetp < offsets_end
9c541725 12850 && *offsetp < origin_child_die->sect_off)
d389af10 12851 offsetp++;
b64f50a1 12852 if (offsetp >= offsets_end
9c541725 12853 || *offsetp > origin_child_die->sect_off)
d389af10 12854 {
adde2bff
DE
12855 /* Found that ORIGIN_CHILD_DIE is really not referenced.
12856 Check whether we're already processing ORIGIN_CHILD_DIE.
12857 This can happen with mutually referenced abstract_origins.
12858 PR 16581. */
12859 if (!origin_child_die->in_process)
12860 process_die (origin_child_die, origin_cu);
d389af10 12861 }
436c571c 12862 origin_child_die = origin_child_die->sibling;
d389af10 12863 }
cd02d79d 12864 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
12865
12866 if (cu != origin_cu)
12867 compute_delayed_physnames (origin_cu);
d389af10
JK
12868}
12869
c906108c 12870static void
e7c27a73 12871read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 12872{
518817b3 12873 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 12874 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 12875 struct context_stack *newobj;
c906108c
SS
12876 CORE_ADDR lowpc;
12877 CORE_ADDR highpc;
12878 struct die_info *child_die;
edb3359d 12879 struct attribute *attr, *call_line, *call_file;
15d034d0 12880 const char *name;
e142c38c 12881 CORE_ADDR baseaddr;
801e3a5b 12882 struct block *block;
edb3359d 12883 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 12884 std::vector<struct symbol *> template_args;
34eaf542 12885 struct template_symbol *templ_func = NULL;
edb3359d
DJ
12886
12887 if (inlined_func)
12888 {
12889 /* If we do not have call site information, we can't show the
12890 caller of this inlined function. That's too confusing, so
12891 only use the scope for local variables. */
12892 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
12893 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
12894 if (call_line == NULL || call_file == NULL)
12895 {
12896 read_lexical_block_scope (die, cu);
12897 return;
12898 }
12899 }
c906108c 12900
b3b3bada 12901 baseaddr = objfile->text_section_offset ();
e142c38c 12902
94af9270 12903 name = dwarf2_name (die, cu);
c906108c 12904
e8d05480
JB
12905 /* Ignore functions with missing or empty names. These are actually
12906 illegal according to the DWARF standard. */
12907 if (name == NULL)
12908 {
b98664d3 12909 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 12910 sect_offset_str (die->sect_off));
e8d05480
JB
12911 return;
12912 }
12913
12914 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 12915 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 12916 <= PC_BOUNDS_INVALID)
e8d05480 12917 {
ae4d0c03
PM
12918 attr = dwarf2_attr (die, DW_AT_external, cu);
12919 if (!attr || !DW_UNSND (attr))
b98664d3 12920 complaint (_("cannot get low and high bounds "
9d8780f0
SM
12921 "for subprogram DIE at %s"),
12922 sect_offset_str (die->sect_off));
e8d05480
JB
12923 return;
12924 }
c906108c 12925
3e29f34a
MR
12926 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
12927 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 12928
34eaf542
TT
12929 /* If we have any template arguments, then we must allocate a
12930 different sort of symbol. */
436c571c 12931 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
12932 {
12933 if (child_die->tag == DW_TAG_template_type_param
12934 || child_die->tag == DW_TAG_template_value_param)
12935 {
e623cf5d 12936 templ_func = allocate_template_symbol (objfile);
cf724bc9 12937 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
12938 break;
12939 }
12940 }
12941
c24bdb02 12942 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
12943 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
12944 (struct symbol *) templ_func);
4c2df51b 12945
81873cc8 12946 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 12947 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
12948 cu->language);
12949
4cecd739
DJ
12950 /* If there is a location expression for DW_AT_frame_base, record
12951 it. */
e142c38c 12952 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 12953 if (attr != nullptr)
fe978cb0 12954 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 12955
63e43d3a
PMR
12956 /* If there is a location for the static link, record it. */
12957 newobj->static_link = NULL;
12958 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 12959 if (attr != nullptr)
63e43d3a 12960 {
224c3ddb
SM
12961 newobj->static_link
12962 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 12963 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
09ba997f 12964 cu->per_cu->addr_type ());
63e43d3a
PMR
12965 }
12966
c24bdb02 12967 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 12968
639d11d3 12969 if (die->child != NULL)
c906108c 12970 {
639d11d3 12971 child_die = die->child;
c906108c
SS
12972 while (child_die && child_die->tag)
12973 {
34eaf542
TT
12974 if (child_die->tag == DW_TAG_template_type_param
12975 || child_die->tag == DW_TAG_template_value_param)
12976 {
12977 struct symbol *arg = new_symbol (child_die, NULL, cu);
12978
f1078f66 12979 if (arg != NULL)
2f4732b0 12980 template_args.push_back (arg);
34eaf542
TT
12981 }
12982 else
12983 process_die (child_die, cu);
436c571c 12984 child_die = child_die->sibling;
c906108c
SS
12985 }
12986 }
12987
d389af10
JK
12988 inherit_abstract_dies (die, cu);
12989
4a811a97
UW
12990 /* If we have a DW_AT_specification, we might need to import using
12991 directives from the context of the specification DIE. See the
12992 comment in determine_prefix. */
12993 if (cu->language == language_cplus
12994 && dwarf2_attr (die, DW_AT_specification, cu))
12995 {
12996 struct dwarf2_cu *spec_cu = cu;
12997 struct die_info *spec_die = die_specification (die, &spec_cu);
12998
12999 while (spec_die)
13000 {
13001 child_die = spec_die->child;
13002 while (child_die && child_die->tag)
13003 {
13004 if (child_die->tag == DW_TAG_imported_module)
13005 process_die (child_die, spec_cu);
436c571c 13006 child_die = child_die->sibling;
4a811a97
UW
13007 }
13008
13009 /* In some cases, GCC generates specification DIEs that
13010 themselves contain DW_AT_specification attributes. */
13011 spec_die = die_specification (spec_die, &spec_cu);
13012 }
13013 }
13014
c24bdb02 13015 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13016 /* Make a block for the local symbols within. */
c24bdb02 13017 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13018 cstk.static_link, lowpc, highpc);
801e3a5b 13019
df8a16a1 13020 /* For C++, set the block's scope. */
45280282
IB
13021 if ((cu->language == language_cplus
13022 || cu->language == language_fortran
c44af4eb
TT
13023 || cu->language == language_d
13024 || cu->language == language_rust)
4d4ec4e5 13025 && cu->processing_has_namespace_info)
195a3f6c
TT
13026 block_set_scope (block, determine_prefix (die, cu),
13027 &objfile->objfile_obstack);
df8a16a1 13028
801e3a5b
JB
13029 /* If we have address ranges, record them. */
13030 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13031
a60f3166 13032 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13033
34eaf542 13034 /* Attach template arguments to function. */
2f4732b0 13035 if (!template_args.empty ())
34eaf542
TT
13036 {
13037 gdb_assert (templ_func != NULL);
13038
2f4732b0 13039 templ_func->n_template_arguments = template_args.size ();
34eaf542 13040 templ_func->template_arguments
8d749320
SM
13041 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13042 templ_func->n_template_arguments);
34eaf542 13043 memcpy (templ_func->template_arguments,
2f4732b0 13044 template_args.data (),
34eaf542 13045 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13046
13047 /* Make sure that the symtab is set on the new symbols. Even
13048 though they don't appear in this symtab directly, other parts
13049 of gdb assume that symbols do, and this is reasonably
13050 true. */
8634679f 13051 for (symbol *sym : template_args)
3e1d3d8c 13052 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13053 }
13054
208d8187
JB
13055 /* In C++, we can have functions nested inside functions (e.g., when
13056 a function declares a class that has methods). This means that
13057 when we finish processing a function scope, we may need to go
13058 back to building a containing block's symbol lists. */
c24bdb02
KS
13059 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13060 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13061
921e78cf
JB
13062 /* If we've finished processing a top-level function, subsequent
13063 symbols go in the file symbol list. */
c24bdb02
KS
13064 if (cu->get_builder ()->outermost_context_p ())
13065 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13066}
13067
13068/* Process all the DIES contained within a lexical block scope. Start
13069 a new scope, process the dies, and then close the scope. */
13070
13071static void
e7c27a73 13072read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13073{
518817b3 13074 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13075 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13076 CORE_ADDR lowpc, highpc;
13077 struct die_info *child_die;
e142c38c
DJ
13078 CORE_ADDR baseaddr;
13079
b3b3bada 13080 baseaddr = objfile->text_section_offset ();
c906108c
SS
13081
13082 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13083 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13084 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13085 be nasty. Might be easier to properly extend generic blocks to
af34e669 13086 describe ranges. */
e385593e
JK
13087 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13088 {
13089 case PC_BOUNDS_NOT_PRESENT:
13090 /* DW_TAG_lexical_block has no attributes, process its children as if
13091 there was no wrapping by that DW_TAG_lexical_block.
13092 GCC does no longer produces such DWARF since GCC r224161. */
13093 for (child_die = die->child;
13094 child_die != NULL && child_die->tag;
436c571c 13095 child_die = child_die->sibling)
e385593e
JK
13096 process_die (child_die, cu);
13097 return;
13098 case PC_BOUNDS_INVALID:
13099 return;
13100 }
3e29f34a
MR
13101 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13102 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13103
c24bdb02 13104 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13105 if (die->child != NULL)
c906108c 13106 {
639d11d3 13107 child_die = die->child;
c906108c
SS
13108 while (child_die && child_die->tag)
13109 {
e7c27a73 13110 process_die (child_die, cu);
436c571c 13111 child_die = child_die->sibling;
c906108c
SS
13112 }
13113 }
3ea89b92 13114 inherit_abstract_dies (die, cu);
c24bdb02 13115 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13116
c24bdb02
KS
13117 if (*cu->get_builder ()->get_local_symbols () != NULL
13118 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13119 {
801e3a5b 13120 struct block *block
c24bdb02 13121 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13122 cstk.start_addr, highpc);
801e3a5b
JB
13123
13124 /* Note that recording ranges after traversing children, as we
13125 do here, means that recording a parent's ranges entails
13126 walking across all its children's ranges as they appear in
13127 the address map, which is quadratic behavior.
13128
13129 It would be nicer to record the parent's ranges before
13130 traversing its children, simply overriding whatever you find
13131 there. But since we don't even decide whether to create a
13132 block until after we've traversed its children, that's hard
13133 to do. */
13134 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13135 }
c24bdb02
KS
13136 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13137 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13138}
13139
216f72a1 13140/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13141
13142static void
13143read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13144{
518817b3 13145 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13146 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13147 CORE_ADDR pc, baseaddr;
13148 struct attribute *attr;
13149 struct call_site *call_site, call_site_local;
13150 void **slot;
13151 int nparams;
13152 struct die_info *child_die;
13153
b3b3bada 13154 baseaddr = objfile->text_section_offset ();
96408a79 13155
216f72a1
JK
13156 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13157 if (attr == NULL)
13158 {
13159 /* This was a pre-DWARF-5 GNU extension alias
13160 for DW_AT_call_return_pc. */
13161 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13162 }
96408a79
SA
13163 if (!attr)
13164 {
b98664d3 13165 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13166 "DIE %s [in module %s]"),
13167 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13168 return;
13169 }
cd6c91b4 13170 pc = attr->value_as_address () + baseaddr;
3e29f34a 13171 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13172
13173 if (cu->call_site_htab == NULL)
13174 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13175 NULL, &objfile->objfile_obstack,
13176 hashtab_obstack_allocate, NULL);
13177 call_site_local.pc = pc;
13178 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13179 if (*slot != NULL)
13180 {
b98664d3 13181 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13182 "DIE %s [in module %s]"),
13183 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13184 objfile_name (objfile));
96408a79
SA
13185 return;
13186 }
13187
13188 /* Count parameters at the caller. */
13189
13190 nparams = 0;
13191 for (child_die = die->child; child_die && child_die->tag;
436c571c 13192 child_die = child_die->sibling)
96408a79 13193 {
216f72a1
JK
13194 if (child_die->tag != DW_TAG_call_site_parameter
13195 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13196 {
b98664d3 13197 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13198 "DW_TAG_call_site child DIE %s [in module %s]"),
13199 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13200 objfile_name (objfile));
96408a79
SA
13201 continue;
13202 }
13203
13204 nparams++;
13205 }
13206
224c3ddb
SM
13207 call_site
13208 = ((struct call_site *)
13209 obstack_alloc (&objfile->objfile_obstack,
13210 sizeof (*call_site)
13211 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13212 *slot = call_site;
13213 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13214 call_site->pc = pc;
13215
216f72a1
JK
13216 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13217 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13218 {
13219 struct die_info *func_die;
13220
13221 /* Skip also over DW_TAG_inlined_subroutine. */
13222 for (func_die = die->parent;
13223 func_die && func_die->tag != DW_TAG_subprogram
13224 && func_die->tag != DW_TAG_subroutine_type;
13225 func_die = func_die->parent);
13226
216f72a1
JK
13227 /* DW_AT_call_all_calls is a superset
13228 of DW_AT_call_all_tail_calls. */
96408a79 13229 if (func_die
216f72a1 13230 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13231 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13232 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13233 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13234 {
13235 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13236 not complete. But keep CALL_SITE for look ups via call_site_htab,
13237 both the initial caller containing the real return address PC and
13238 the final callee containing the current PC of a chain of tail
13239 calls do not need to have the tail call list complete. But any
13240 function candidate for a virtual tail call frame searched via
13241 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13242 determined unambiguously. */
13243 }
13244 else
13245 {
13246 struct type *func_type = NULL;
13247
13248 if (func_die)
13249 func_type = get_die_type (func_die, cu);
13250 if (func_type != NULL)
13251 {
13252 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
13253
13254 /* Enlist this call site to the function. */
13255 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13256 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13257 }
13258 else
b98664d3 13259 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13260 "DIE %s [in module %s]"),
13261 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13262 }
13263 }
13264
216f72a1
JK
13265 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13266 if (attr == NULL)
13267 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13268 if (attr == NULL)
13269 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13270 if (attr == NULL)
216f72a1
JK
13271 {
13272 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13273 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13274 }
96408a79 13275 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13276 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13277 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13278 else if (attr->form_is_block ())
96408a79
SA
13279 {
13280 struct dwarf2_locexpr_baton *dlbaton;
13281
8d749320 13282 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13283 dlbaton->data = DW_BLOCK (attr)->data;
13284 dlbaton->size = DW_BLOCK (attr)->size;
13285 dlbaton->per_cu = cu->per_cu;
13286
13287 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13288 }
cd6c91b4 13289 else if (attr->form_is_ref ())
96408a79 13290 {
96408a79
SA
13291 struct dwarf2_cu *target_cu = cu;
13292 struct die_info *target_die;
13293
ac9ec31b 13294 target_die = follow_die_ref (die, attr, &target_cu);
518817b3 13295 gdb_assert (target_cu->per_cu->dwarf2_per_objfile->objfile == objfile);
96408a79
SA
13296 if (die_is_declaration (target_die, target_cu))
13297 {
7d45c7c3 13298 const char *target_physname;
9112db09
JK
13299
13300 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13301 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13302 if (target_physname == NULL)
9112db09 13303 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13304 if (target_physname == NULL)
b98664d3 13305 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13306 "physname, for referencing DIE %s [in module %s]"),
13307 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13308 else
7d455152 13309 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13310 }
13311 else
13312 {
13313 CORE_ADDR lowpc;
13314
13315 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13316 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13317 <= PC_BOUNDS_INVALID)
b98664d3 13318 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13319 "low pc, for referencing DIE %s [in module %s]"),
13320 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13321 else
3e29f34a
MR
13322 {
13323 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13324 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13325 }
96408a79
SA
13326 }
13327 }
13328 else
b98664d3 13329 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13330 "block nor reference, for DIE %s [in module %s]"),
13331 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13332
13333 call_site->per_cu = cu->per_cu;
13334
13335 for (child_die = die->child;
13336 child_die && child_die->tag;
436c571c 13337 child_die = child_die->sibling)
96408a79 13338 {
96408a79 13339 struct call_site_parameter *parameter;
1788b2d3 13340 struct attribute *loc, *origin;
96408a79 13341
216f72a1
JK
13342 if (child_die->tag != DW_TAG_call_site_parameter
13343 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13344 {
13345 /* Already printed the complaint above. */
13346 continue;
13347 }
13348
13349 gdb_assert (call_site->parameter_count < nparams);
13350 parameter = &call_site->parameter[call_site->parameter_count];
13351
1788b2d3
JK
13352 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13353 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13354 register is contained in DW_AT_call_value. */
96408a79 13355
24c5c679 13356 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13357 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13358 if (origin == NULL)
13359 {
13360 /* This was a pre-DWARF-5 GNU extension alias
13361 for DW_AT_call_parameter. */
13362 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13363 }
cd6c91b4 13364 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13365 {
1788b2d3 13366 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13367
0826b30a 13368 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13369 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13370 {
13371 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13372 binding can be done only inside one CU. Such referenced DIE
13373 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13374 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13375 "DW_TAG_call_site child DIE %s [in module %s]"),
13376 sect_offset_str (child_die->sect_off),
9c541725 13377 objfile_name (objfile));
d76b7dbc
JK
13378 continue;
13379 }
9c541725
PA
13380 parameter->u.param_cu_off
13381 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13382 }
4fc6c0d5 13383 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13384 {
b98664d3 13385 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13386 "DW_TAG_call_site child DIE %s [in module %s]"),
13387 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13388 continue;
13389 }
24c5c679 13390 else
96408a79 13391 {
24c5c679
JK
13392 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13393 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13394 if (parameter->u.dwarf_reg != -1)
13395 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13396 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13397 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13398 &parameter->u.fb_offset))
13399 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13400 else
13401 {
b98664d3 13402 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13403 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13404 "DW_TAG_call_site child DIE %s "
24c5c679 13405 "[in module %s]"),
9d8780f0 13406 sect_offset_str (child_die->sect_off),
9c541725 13407 objfile_name (objfile));
24c5c679
JK
13408 continue;
13409 }
96408a79
SA
13410 }
13411
216f72a1
JK
13412 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13413 if (attr == NULL)
13414 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13415 if (attr == NULL || !attr->form_is_block ())
96408a79 13416 {
b98664d3 13417 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13418 "DW_TAG_call_site child DIE %s [in module %s]"),
13419 sect_offset_str (child_die->sect_off),
9c541725 13420 objfile_name (objfile));
96408a79
SA
13421 continue;
13422 }
13423 parameter->value = DW_BLOCK (attr)->data;
13424 parameter->value_size = DW_BLOCK (attr)->size;
13425
13426 /* Parameters are not pre-cleared by memset above. */
13427 parameter->data_value = NULL;
13428 parameter->data_value_size = 0;
13429 call_site->parameter_count++;
13430
216f72a1
JK
13431 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13432 if (attr == NULL)
13433 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13434 if (attr != nullptr)
96408a79 13435 {
4fc6c0d5 13436 if (!attr->form_is_block ())
b98664d3 13437 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13438 "DW_TAG_call_site child DIE %s [in module %s]"),
13439 sect_offset_str (child_die->sect_off),
9c541725 13440 objfile_name (objfile));
96408a79
SA
13441 else
13442 {
13443 parameter->data_value = DW_BLOCK (attr)->data;
13444 parameter->data_value_size = DW_BLOCK (attr)->size;
13445 }
13446 }
13447 }
13448}
13449
71a3c369
TT
13450/* Helper function for read_variable. If DIE represents a virtual
13451 table, then return the type of the concrete object that is
13452 associated with the virtual table. Otherwise, return NULL. */
13453
13454static struct type *
13455rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13456{
13457 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13458 if (attr == NULL)
13459 return NULL;
13460
13461 /* Find the type DIE. */
13462 struct die_info *type_die = NULL;
13463 struct dwarf2_cu *type_cu = cu;
13464
cd6c91b4 13465 if (attr->form_is_ref ())
71a3c369
TT
13466 type_die = follow_die_ref (die, attr, &type_cu);
13467 if (type_die == NULL)
13468 return NULL;
13469
13470 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13471 return NULL;
13472 return die_containing_type (type_die, type_cu);
13473}
13474
13475/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13476
13477static void
13478read_variable (struct die_info *die, struct dwarf2_cu *cu)
13479{
13480 struct rust_vtable_symbol *storage = NULL;
13481
13482 if (cu->language == language_rust)
13483 {
13484 struct type *containing_type = rust_containing_type (die, cu);
13485
13486 if (containing_type != NULL)
13487 {
518817b3 13488 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
71a3c369 13489
468c0cbb 13490 storage = new (&objfile->objfile_obstack) rust_vtable_symbol ();
71a3c369
TT
13491 initialize_objfile_symbol (storage);
13492 storage->concrete_type = containing_type;
cf724bc9 13493 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13494 }
13495 }
13496
e4a62c65
TV
13497 struct symbol *res = new_symbol (die, NULL, cu, storage);
13498 struct attribute *abstract_origin
13499 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13500 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13501 if (res == NULL && loc && abstract_origin)
13502 {
13503 /* We have a variable without a name, but with a location and an abstract
13504 origin. This may be a concrete instance of an abstract variable
13505 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13506 later. */
13507 struct dwarf2_cu *origin_cu = cu;
13508 struct die_info *origin_die
13509 = follow_die_ref (die, abstract_origin, &origin_cu);
13510 dwarf2_per_objfile *dpo = cu->per_cu->dwarf2_per_objfile;
3360b6e7 13511 dpo->abstract_to_concrete[origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13512 }
71a3c369
TT
13513}
13514
43988095
JK
13515/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13516 reading .debug_rnglists.
13517 Callback's type should be:
13518 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13519 Return true if the attributes are present and valid, otherwise,
13520 return false. */
13521
13522template <typename Callback>
13523static bool
13524dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13525 Callback &&callback)
13526{
ed2dc618 13527 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13528 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13529 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13530 bfd *obfd = objfile->obfd;
43988095 13531 /* Base address selection entry. */
2b24b6e4 13532 gdb::optional<CORE_ADDR> base;
43988095 13533 const gdb_byte *buffer;
43988095
JK
13534 CORE_ADDR baseaddr;
13535 bool overflow = false;
13536
43988095
JK
13537 base = cu->base_address;
13538
96b79293 13539 dwarf2_per_objfile->rnglists.read (objfile);
43988095
JK
13540 if (offset >= dwarf2_per_objfile->rnglists.size)
13541 {
b98664d3 13542 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13543 offset);
13544 return false;
13545 }
13546 buffer = dwarf2_per_objfile->rnglists.buffer + offset;
13547
b3b3bada 13548 baseaddr = objfile->text_section_offset ();
43988095
JK
13549
13550 while (1)
13551 {
7814882a
JK
13552 /* Initialize it due to a false compiler warning. */
13553 CORE_ADDR range_beginning = 0, range_end = 0;
43988095
JK
13554 const gdb_byte *buf_end = (dwarf2_per_objfile->rnglists.buffer
13555 + dwarf2_per_objfile->rnglists.size);
13556 unsigned int bytes_read;
13557
13558 if (buffer == buf_end)
13559 {
13560 overflow = true;
13561 break;
13562 }
13563 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13564 switch (rlet)
13565 {
13566 case DW_RLE_end_of_list:
13567 break;
13568 case DW_RLE_base_address:
13569 if (buffer + cu->header.addr_size > buf_end)
13570 {
13571 overflow = true;
13572 break;
13573 }
c8a7a66f 13574 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13575 buffer += bytes_read;
13576 break;
13577 case DW_RLE_start_length:
13578 if (buffer + cu->header.addr_size > buf_end)
13579 {
13580 overflow = true;
13581 break;
13582 }
c8a7a66f
TT
13583 range_beginning = cu->header.read_address (obfd, buffer,
13584 &bytes_read);
43988095
JK
13585 buffer += bytes_read;
13586 range_end = (range_beginning
13587 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13588 buffer += bytes_read;
13589 if (buffer > buf_end)
13590 {
13591 overflow = true;
13592 break;
13593 }
13594 break;
13595 case DW_RLE_offset_pair:
13596 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13597 buffer += bytes_read;
13598 if (buffer > buf_end)
13599 {
13600 overflow = true;
13601 break;
13602 }
13603 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13604 buffer += bytes_read;
13605 if (buffer > buf_end)
13606 {
13607 overflow = true;
13608 break;
13609 }
13610 break;
13611 case DW_RLE_start_end:
13612 if (buffer + 2 * cu->header.addr_size > buf_end)
13613 {
13614 overflow = true;
13615 break;
13616 }
c8a7a66f
TT
13617 range_beginning = cu->header.read_address (obfd, buffer,
13618 &bytes_read);
43988095 13619 buffer += bytes_read;
c8a7a66f 13620 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13621 buffer += bytes_read;
13622 break;
13623 default:
b98664d3 13624 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13625 return false;
13626 }
13627 if (rlet == DW_RLE_end_of_list || overflow)
13628 break;
13629 if (rlet == DW_RLE_base_address)
13630 continue;
13631
2b24b6e4 13632 if (!base.has_value ())
43988095
JK
13633 {
13634 /* We have no valid base address for the ranges
13635 data. */
b98664d3 13636 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13637 return false;
13638 }
13639
13640 if (range_beginning > range_end)
13641 {
13642 /* Inverted range entries are invalid. */
b98664d3 13643 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13644 return false;
13645 }
13646
13647 /* Empty range entries have no effect. */
13648 if (range_beginning == range_end)
13649 continue;
13650
2b24b6e4
TT
13651 range_beginning += *base;
13652 range_end += *base;
43988095
JK
13653
13654 /* A not-uncommon case of bad debug info.
13655 Don't pollute the addrmap with bad data. */
13656 if (range_beginning + baseaddr == 0
13657 && !dwarf2_per_objfile->has_section_at_zero)
13658 {
b98664d3 13659 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13660 " [in module %s]"), objfile_name (objfile));
13661 continue;
13662 }
13663
13664 callback (range_beginning, range_end);
13665 }
13666
13667 if (overflow)
13668 {
b98664d3 13669 complaint (_("Offset %d is not terminated "
43988095
JK
13670 "for DW_AT_ranges attribute"),
13671 offset);
13672 return false;
13673 }
13674
13675 return true;
13676}
13677
13678/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13679 Callback's type should be:
13680 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13681 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13682
43988095 13683template <typename Callback>
43039443 13684static int
5f46c5a5 13685dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13686 Callback &&callback)
43039443 13687{
ed2dc618 13688 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 13689 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 13690 struct objfile *objfile = dwarf2_per_objfile->objfile;
43039443
JK
13691 struct comp_unit_head *cu_header = &cu->header;
13692 bfd *obfd = objfile->obfd;
13693 unsigned int addr_size = cu_header->addr_size;
13694 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13695 /* Base address selection entry. */
2b24b6e4 13696 gdb::optional<CORE_ADDR> base;
43039443 13697 unsigned int dummy;
d521ce57 13698 const gdb_byte *buffer;
ff013f42 13699 CORE_ADDR baseaddr;
43039443 13700
43988095
JK
13701 if (cu_header->version >= 5)
13702 return dwarf2_rnglists_process (offset, cu, callback);
13703
d00adf39 13704 base = cu->base_address;
43039443 13705
96b79293 13706 dwarf2_per_objfile->ranges.read (objfile);
dce234bc 13707 if (offset >= dwarf2_per_objfile->ranges.size)
43039443 13708 {
b98664d3 13709 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13710 offset);
13711 return 0;
13712 }
dce234bc 13713 buffer = dwarf2_per_objfile->ranges.buffer + offset;
43039443 13714
b3b3bada 13715 baseaddr = objfile->text_section_offset ();
ff013f42 13716
43039443
JK
13717 while (1)
13718 {
13719 CORE_ADDR range_beginning, range_end;
13720
c8a7a66f 13721 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13722 buffer += addr_size;
c8a7a66f 13723 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13724 buffer += addr_size;
13725 offset += 2 * addr_size;
13726
13727 /* An end of list marker is a pair of zero addresses. */
13728 if (range_beginning == 0 && range_end == 0)
13729 /* Found the end of list entry. */
13730 break;
13731
13732 /* Each base address selection entry is a pair of 2 values.
13733 The first is the largest possible address, the second is
13734 the base address. Check for a base address here. */
13735 if ((range_beginning & mask) == mask)
13736 {
28d2bfb9
AB
13737 /* If we found the largest possible address, then we already
13738 have the base address in range_end. */
13739 base = range_end;
43039443
JK
13740 continue;
13741 }
13742
2b24b6e4 13743 if (!base.has_value ())
43039443
JK
13744 {
13745 /* We have no valid base address for the ranges
13746 data. */
b98664d3 13747 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13748 return 0;
13749 }
13750
9277c30c
UW
13751 if (range_beginning > range_end)
13752 {
13753 /* Inverted range entries are invalid. */
b98664d3 13754 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13755 return 0;
13756 }
13757
13758 /* Empty range entries have no effect. */
13759 if (range_beginning == range_end)
13760 continue;
13761
2b24b6e4
TT
13762 range_beginning += *base;
13763 range_end += *base;
43039443 13764
01093045
DE
13765 /* A not-uncommon case of bad debug info.
13766 Don't pollute the addrmap with bad data. */
13767 if (range_beginning + baseaddr == 0
13768 && !dwarf2_per_objfile->has_section_at_zero)
13769 {
b98664d3 13770 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13771 " [in module %s]"), objfile_name (objfile));
01093045
DE
13772 continue;
13773 }
13774
5f46c5a5
JK
13775 callback (range_beginning, range_end);
13776 }
13777
13778 return 1;
13779}
13780
13781/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13782 Return 1 if the attributes are present and valid, otherwise, return 0.
13783 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13784
13785static int
13786dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13787 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13788 dwarf2_psymtab *ranges_pst)
5f46c5a5 13789{
518817b3 13790 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 13791 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13792 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13793 int low_set = 0;
13794 CORE_ADDR low = 0;
13795 CORE_ADDR high = 0;
13796 int retval;
13797
13798 retval = dwarf2_ranges_process (offset, cu,
13799 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13800 {
9277c30c 13801 if (ranges_pst != NULL)
3e29f34a
MR
13802 {
13803 CORE_ADDR lowpc;
13804 CORE_ADDR highpc;
13805
79748972
TT
13806 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13807 range_beginning + baseaddr)
13808 - baseaddr);
13809 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13810 range_end + baseaddr)
13811 - baseaddr);
d320c2b5
TT
13812 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13813 lowpc, highpc - 1, ranges_pst);
3e29f34a 13814 }
ff013f42 13815
43039443
JK
13816 /* FIXME: This is recording everything as a low-high
13817 segment of consecutive addresses. We should have a
13818 data structure for discontiguous block ranges
13819 instead. */
13820 if (! low_set)
13821 {
13822 low = range_beginning;
13823 high = range_end;
13824 low_set = 1;
13825 }
13826 else
13827 {
13828 if (range_beginning < low)
13829 low = range_beginning;
13830 if (range_end > high)
13831 high = range_end;
13832 }
5f46c5a5
JK
13833 });
13834 if (!retval)
13835 return 0;
43039443
JK
13836
13837 if (! low_set)
13838 /* If the first entry is an end-of-list marker, the range
13839 describes an empty scope, i.e. no instructions. */
13840 return 0;
13841
13842 if (low_return)
13843 *low_return = low;
13844 if (high_return)
13845 *high_return = high;
13846 return 1;
13847}
13848
3a2b436a
JK
13849/* Get low and high pc attributes from a die. See enum pc_bounds_kind
13850 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 13851 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 13852
3a2b436a 13853static enum pc_bounds_kind
af34e669 13854dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 13855 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 13856 dwarf2_psymtab *pst)
c906108c 13857{
518817b3
SM
13858 struct dwarf2_per_objfile *dwarf2_per_objfile
13859 = cu->per_cu->dwarf2_per_objfile;
c906108c 13860 struct attribute *attr;
91da1414 13861 struct attribute *attr_high;
af34e669
DJ
13862 CORE_ADDR low = 0;
13863 CORE_ADDR high = 0;
e385593e 13864 enum pc_bounds_kind ret;
c906108c 13865
91da1414
MW
13866 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
13867 if (attr_high)
af34e669 13868 {
e142c38c 13869 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 13870 if (attr != nullptr)
91da1414 13871 {
cd6c91b4
TT
13872 low = attr->value_as_address ();
13873 high = attr_high->value_as_address ();
13874 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 13875 high += low;
91da1414 13876 }
af34e669
DJ
13877 else
13878 /* Found high w/o low attribute. */
e385593e 13879 return PC_BOUNDS_INVALID;
af34e669
DJ
13880
13881 /* Found consecutive range of addresses. */
3a2b436a 13882 ret = PC_BOUNDS_HIGH_LOW;
af34e669 13883 }
c906108c 13884 else
af34e669 13885 {
e142c38c 13886 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
13887 if (attr != NULL)
13888 {
18a8505e 13889 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
13890 We take advantage of the fact that DW_AT_ranges does not appear
13891 in DW_TAG_compile_unit of DWO files. */
13892 int need_ranges_base = die->tag != DW_TAG_compile_unit;
13893 unsigned int ranges_offset = (DW_UNSND (attr)
13894 + (need_ranges_base
13895 ? cu->ranges_base
13896 : 0));
2e3cf129 13897
af34e669 13898 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 13899 .debug_ranges section. */
2e3cf129 13900 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 13901 return PC_BOUNDS_INVALID;
43039443 13902 /* Found discontinuous range of addresses. */
3a2b436a 13903 ret = PC_BOUNDS_RANGES;
af34e669 13904 }
e385593e
JK
13905 else
13906 return PC_BOUNDS_NOT_PRESENT;
af34e669 13907 }
c906108c 13908
48fbe735 13909 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 13910 if (high <= low)
e385593e 13911 return PC_BOUNDS_INVALID;
c906108c
SS
13912
13913 /* When using the GNU linker, .gnu.linkonce. sections are used to
13914 eliminate duplicate copies of functions and vtables and such.
13915 The linker will arbitrarily choose one and discard the others.
13916 The AT_*_pc values for such functions refer to local labels in
13917 these sections. If the section from that file was discarded, the
13918 labels are not in the output, so the relocs get a value of 0.
13919 If this is a discarded function, mark the pc bounds as invalid,
13920 so that GDB will ignore it. */
72dca2f5 13921 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
e385593e 13922 return PC_BOUNDS_INVALID;
c906108c
SS
13923
13924 *lowpc = low;
96408a79
SA
13925 if (highpc)
13926 *highpc = high;
af34e669 13927 return ret;
c906108c
SS
13928}
13929
b084d499
JB
13930/* Assuming that DIE represents a subprogram DIE or a lexical block, get
13931 its low and high PC addresses. Do nothing if these addresses could not
13932 be determined. Otherwise, set LOWPC to the low address if it is smaller,
13933 and HIGHPC to the high address if greater than HIGHPC. */
13934
13935static void
13936dwarf2_get_subprogram_pc_bounds (struct die_info *die,
13937 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13938 struct dwarf2_cu *cu)
13939{
13940 CORE_ADDR low, high;
13941 struct die_info *child = die->child;
13942
e385593e 13943 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 13944 {
325fac50
PA
13945 *lowpc = std::min (*lowpc, low);
13946 *highpc = std::max (*highpc, high);
b084d499
JB
13947 }
13948
13949 /* If the language does not allow nested subprograms (either inside
13950 subprograms or lexical blocks), we're done. */
13951 if (cu->language != language_ada)
13952 return;
6e70227d 13953
b084d499
JB
13954 /* Check all the children of the given DIE. If it contains nested
13955 subprograms, then check their pc bounds. Likewise, we need to
13956 check lexical blocks as well, as they may also contain subprogram
13957 definitions. */
13958 while (child && child->tag)
13959 {
13960 if (child->tag == DW_TAG_subprogram
13961 || child->tag == DW_TAG_lexical_block)
13962 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 13963 child = child->sibling;
b084d499
JB
13964 }
13965}
13966
fae299cd
DC
13967/* Get the low and high pc's represented by the scope DIE, and store
13968 them in *LOWPC and *HIGHPC. If the correct values can't be
13969 determined, set *LOWPC to -1 and *HIGHPC to 0. */
13970
13971static void
13972get_scope_pc_bounds (struct die_info *die,
13973 CORE_ADDR *lowpc, CORE_ADDR *highpc,
13974 struct dwarf2_cu *cu)
13975{
13976 CORE_ADDR best_low = (CORE_ADDR) -1;
13977 CORE_ADDR best_high = (CORE_ADDR) 0;
13978 CORE_ADDR current_low, current_high;
13979
3a2b436a 13980 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 13981 >= PC_BOUNDS_RANGES)
fae299cd
DC
13982 {
13983 best_low = current_low;
13984 best_high = current_high;
13985 }
13986 else
13987 {
13988 struct die_info *child = die->child;
13989
13990 while (child && child->tag)
13991 {
13992 switch (child->tag) {
13993 case DW_TAG_subprogram:
b084d499 13994 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
13995 break;
13996 case DW_TAG_namespace:
f55ee35c 13997 case DW_TAG_module:
fae299cd
DC
13998 /* FIXME: carlton/2004-01-16: Should we do this for
13999 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14000 that current GCC's always emit the DIEs corresponding
14001 to definitions of methods of classes as children of a
14002 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14003 the DIEs giving the declarations, which could be
14004 anywhere). But I don't see any reason why the
14005 standards says that they have to be there. */
14006 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14007
14008 if (current_low != ((CORE_ADDR) -1))
14009 {
325fac50
PA
14010 best_low = std::min (best_low, current_low);
14011 best_high = std::max (best_high, current_high);
fae299cd
DC
14012 }
14013 break;
14014 default:
0963b4bd 14015 /* Ignore. */
fae299cd
DC
14016 break;
14017 }
14018
436c571c 14019 child = child->sibling;
fae299cd
DC
14020 }
14021 }
14022
14023 *lowpc = best_low;
14024 *highpc = best_high;
14025}
14026
801e3a5b
JB
14027/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14028 in DIE. */
380bca97 14029
801e3a5b
JB
14030static void
14031dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14032 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14033{
518817b3 14034 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14035 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14036 struct attribute *attr;
91da1414 14037 struct attribute *attr_high;
801e3a5b 14038
91da1414
MW
14039 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14040 if (attr_high)
801e3a5b 14041 {
801e3a5b 14042 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14043 if (attr != nullptr)
801e3a5b 14044 {
cd6c91b4
TT
14045 CORE_ADDR low = attr->value_as_address ();
14046 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14047
cd6c91b4 14048 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14049 high += low;
9a619af0 14050
3e29f34a
MR
14051 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14052 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14053 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14054 }
14055 }
14056
14057 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14058 if (attr != nullptr)
801e3a5b 14059 {
18a8505e 14060 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14061 We take advantage of the fact that DW_AT_ranges does not appear
14062 in DW_TAG_compile_unit of DWO files. */
14063 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14064
14065 /* The value of the DW_AT_ranges attribute is the offset of the
14066 address range list in the .debug_ranges section. */
ab435259
DE
14067 unsigned long offset = (DW_UNSND (attr)
14068 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14069
2d5f09ec 14070 std::vector<blockrange> blockvec;
5f46c5a5
JK
14071 dwarf2_ranges_process (offset, cu,
14072 [&] (CORE_ADDR start, CORE_ADDR end)
14073 {
58fdfd2c
JK
14074 start += baseaddr;
14075 end += baseaddr;
5f46c5a5
JK
14076 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14077 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14078 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14079 blockvec.emplace_back (start, end);
5f46c5a5 14080 });
2d5f09ec
KB
14081
14082 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14083 }
14084}
14085
685b1105
JK
14086/* Check whether the producer field indicates either of GCC < 4.6, or the
14087 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14088
685b1105
JK
14089static void
14090check_producer (struct dwarf2_cu *cu)
60d5a603 14091{
38360086 14092 int major, minor;
60d5a603
JK
14093
14094 if (cu->producer == NULL)
14095 {
14096 /* For unknown compilers expect their behavior is DWARF version
14097 compliant.
14098
14099 GCC started to support .debug_types sections by -gdwarf-4 since
14100 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14101 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14102 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14103 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14104 }
b1ffba5a 14105 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14106 {
38360086
MW
14107 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14108 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14109 }
5230b05a 14110 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14111 {
14112 cu->producer_is_icc = true;
14113 cu->producer_is_icc_lt_14 = major < 14;
14114 }
c258c396
JD
14115 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14116 cu->producer_is_codewarrior = true;
685b1105
JK
14117 else
14118 {
14119 /* For other non-GCC compilers, expect their behavior is DWARF version
14120 compliant. */
60d5a603
JK
14121 }
14122
9068261f 14123 cu->checked_producer = true;
685b1105 14124}
ba919b58 14125
685b1105
JK
14126/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14127 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14128 during 4.6.0 experimental. */
14129
9068261f 14130static bool
685b1105
JK
14131producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14132{
14133 if (!cu->checked_producer)
14134 check_producer (cu);
14135
14136 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14137}
14138
c258c396
JD
14139
14140/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14141 with incorrect is_stmt attributes. */
14142
14143static bool
14144producer_is_codewarrior (struct dwarf2_cu *cu)
14145{
14146 if (!cu->checked_producer)
14147 check_producer (cu);
14148
14149 return cu->producer_is_codewarrior;
14150}
14151
405feb71 14152/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14153 DW_AT_accessibility. */
14154
14155static enum dwarf_access_attribute
14156dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14157{
14158 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14159 {
14160 /* The default DWARF 2 accessibility for members is public, the default
14161 accessibility for inheritance is private. */
14162
14163 if (die->tag != DW_TAG_inheritance)
14164 return DW_ACCESS_public;
14165 else
14166 return DW_ACCESS_private;
14167 }
14168 else
14169 {
14170 /* DWARF 3+ defines the default accessibility a different way. The same
14171 rules apply now for DW_TAG_inheritance as for the members and it only
14172 depends on the container kind. */
14173
14174 if (die->parent->tag == DW_TAG_class_type)
14175 return DW_ACCESS_private;
14176 else
14177 return DW_ACCESS_public;
14178 }
14179}
14180
74ac6d43
TT
14181/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14182 offset. If the attribute was not found return 0, otherwise return
14183 1. If it was found but could not properly be handled, set *OFFSET
14184 to 0. */
14185
14186static int
14187handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14188 LONGEST *offset)
14189{
14190 struct attribute *attr;
14191
14192 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14193 if (attr != NULL)
14194 {
14195 *offset = 0;
14196
14197 /* Note that we do not check for a section offset first here.
14198 This is because DW_AT_data_member_location is new in DWARF 4,
14199 so if we see it, we can assume that a constant form is really
14200 a constant and not a section offset. */
cd6c91b4 14201 if (attr->form_is_constant ())
0826b30a 14202 *offset = attr->constant_value (0);
cd6c91b4 14203 else if (attr->form_is_section_offset ())
74ac6d43 14204 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14205 else if (attr->form_is_block ())
74ac6d43
TT
14206 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14207 else
14208 dwarf2_complex_location_expr_complaint ();
14209
14210 return 1;
14211 }
14212
14213 return 0;
14214}
14215
7d79de9a
TT
14216/* Look for DW_AT_data_member_location and store the results in FIELD. */
14217
14218static void
14219handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14220 struct field *field)
14221{
14222 struct attribute *attr;
14223
14224 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14225 if (attr != NULL)
14226 {
14227 if (attr->form_is_constant ())
14228 {
14229 LONGEST offset = attr->constant_value (0);
14230 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14231 }
14232 else if (attr->form_is_section_offset ())
14233 dwarf2_complex_location_expr_complaint ();
14234 else if (attr->form_is_block ())
14235 {
14236 bool handled;
14237 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14238 if (handled)
14239 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14240 else
14241 {
14242 struct objfile *objfile
14243 = cu->per_cu->dwarf2_per_objfile->objfile;
14244 struct dwarf2_locexpr_baton *dlbaton
14245 = XOBNEW (&objfile->objfile_obstack,
14246 struct dwarf2_locexpr_baton);
14247 dlbaton->data = DW_BLOCK (attr)->data;
14248 dlbaton->size = DW_BLOCK (attr)->size;
14249 /* When using this baton, we want to compute the address
14250 of the field, not the value. This is why
14251 is_reference is set to false here. */
14252 dlbaton->is_reference = false;
14253 dlbaton->per_cu = cu->per_cu;
14254
14255 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14256 }
14257 }
14258 else
14259 dwarf2_complex_location_expr_complaint ();
14260 }
14261}
14262
c906108c
SS
14263/* Add an aggregate field to the field list. */
14264
14265static void
107d2387 14266dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14267 struct dwarf2_cu *cu)
6e70227d 14268{
518817b3 14269 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 14270 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14271 struct nextfield *new_field;
14272 struct attribute *attr;
14273 struct field *fp;
15d034d0 14274 const char *fieldname = "";
c906108c 14275
7d0ccb61
DJ
14276 if (die->tag == DW_TAG_inheritance)
14277 {
be2daae6
TT
14278 fip->baseclasses.emplace_back ();
14279 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14280 }
14281 else
14282 {
be2daae6
TT
14283 fip->fields.emplace_back ();
14284 new_field = &fip->fields.back ();
7d0ccb61 14285 }
be2daae6 14286
9c6a1327
TT
14287 new_field->offset = die->sect_off;
14288
e142c38c 14289 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14290 if (attr != nullptr)
c906108c 14291 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14292 else
14293 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14294 if (new_field->accessibility != DW_ACCESS_public)
14295 fip->non_public_fields = 1;
60d5a603 14296
e142c38c 14297 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14298 if (attr != nullptr)
c906108c 14299 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14300 else
14301 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14302
14303 fp = &new_field->field;
a9a9bd0f 14304
e142c38c 14305 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14306 {
a9a9bd0f 14307 /* Data member other than a C++ static data member. */
6e70227d 14308
c906108c 14309 /* Get type of field. */
e7c27a73 14310 fp->type = die_type (die, cu);
c906108c 14311
d6a843b5 14312 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14313
c906108c 14314 /* Get bit size of field (zero if none). */
e142c38c 14315 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14316 if (attr != nullptr)
c906108c
SS
14317 {
14318 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14319 }
14320 else
14321 {
14322 FIELD_BITSIZE (*fp) = 0;
14323 }
14324
14325 /* Get bit offset of field. */
7d79de9a 14326 handle_data_member_location (die, cu, fp);
e142c38c 14327 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14328 if (attr != nullptr)
c906108c 14329 {
d5a22e77 14330 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14331 {
14332 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14333 additional bit offset from the MSB of the containing
14334 anonymous object to the MSB of the field. We don't
14335 have to do anything special since we don't need to
14336 know the size of the anonymous object. */
f41f5e61 14337 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14338 }
14339 else
14340 {
14341 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14342 MSB of the anonymous object, subtract off the number of
14343 bits from the MSB of the field to the MSB of the
14344 object, and then subtract off the number of bits of
14345 the field itself. The result is the bit offset of
14346 the LSB of the field. */
c906108c
SS
14347 int anonymous_size;
14348 int bit_offset = DW_UNSND (attr);
14349
e142c38c 14350 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14351 if (attr != nullptr)
c906108c
SS
14352 {
14353 /* The size of the anonymous object containing
14354 the bit field is explicit, so use the
14355 indicated size (in bytes). */
14356 anonymous_size = DW_UNSND (attr);
14357 }
14358 else
14359 {
14360 /* The size of the anonymous object containing
14361 the bit field must be inferred from the type
14362 attribute of the data member containing the
14363 bit field. */
14364 anonymous_size = TYPE_LENGTH (fp->type);
14365 }
f41f5e61
PA
14366 SET_FIELD_BITPOS (*fp,
14367 (FIELD_BITPOS (*fp)
14368 + anonymous_size * bits_per_byte
14369 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14370 }
14371 }
da5b30da
AA
14372 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14373 if (attr != NULL)
14374 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14375 + attr->constant_value (0)));
c906108c
SS
14376
14377 /* Get name of field. */
39cbfefa
DJ
14378 fieldname = dwarf2_name (die, cu);
14379 if (fieldname == NULL)
14380 fieldname = "";
d8151005
DJ
14381
14382 /* The name is already allocated along with this objfile, so we don't
14383 need to duplicate it for the type. */
14384 fp->name = fieldname;
c906108c
SS
14385
14386 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14387 pointer or virtual base class pointer) to private. */
e142c38c 14388 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14389 {
d48cc9dd 14390 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14391 new_field->accessibility = DW_ACCESS_private;
14392 fip->non_public_fields = 1;
14393 }
14394 }
a9a9bd0f 14395 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14396 {
a9a9bd0f
DC
14397 /* C++ static member. */
14398
14399 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14400 is a declaration, but all versions of G++ as of this writing
14401 (so through at least 3.2.1) incorrectly generate
14402 DW_TAG_variable tags. */
6e70227d 14403
ff355380 14404 const char *physname;
c906108c 14405
a9a9bd0f 14406 /* Get name of field. */
39cbfefa
DJ
14407 fieldname = dwarf2_name (die, cu);
14408 if (fieldname == NULL)
c906108c
SS
14409 return;
14410
254e6b9e 14411 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14412 if (attr
14413 /* Only create a symbol if this is an external value.
14414 new_symbol checks this and puts the value in the global symbol
14415 table, which we want. If it is not external, new_symbol
14416 will try to put the value in cu->list_in_scope which is wrong. */
14417 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14418 {
14419 /* A static const member, not much different than an enum as far as
14420 we're concerned, except that we can support more types. */
14421 new_symbol (die, NULL, cu);
14422 }
14423
2df3850c 14424 /* Get physical name. */
ff355380 14425 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14426
d8151005
DJ
14427 /* The name is already allocated along with this objfile, so we don't
14428 need to duplicate it for the type. */
14429 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14430 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14431 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14432 }
14433 else if (die->tag == DW_TAG_inheritance)
14434 {
74ac6d43 14435 /* C++ base class field. */
7d79de9a 14436 handle_data_member_location (die, cu, fp);
c906108c 14437 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14438 FIELD_TYPE (*fp) = die_type (die, cu);
a737d952 14439 FIELD_NAME (*fp) = TYPE_NAME (fp->type);
c906108c 14440 }
2ddeaf8a
TT
14441 else
14442 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14443}
14444
883fd55a
KS
14445/* Can the type given by DIE define another type? */
14446
14447static bool
14448type_can_define_types (const struct die_info *die)
14449{
14450 switch (die->tag)
14451 {
14452 case DW_TAG_typedef:
14453 case DW_TAG_class_type:
14454 case DW_TAG_structure_type:
14455 case DW_TAG_union_type:
14456 case DW_TAG_enumeration_type:
14457 return true;
14458
14459 default:
14460 return false;
14461 }
14462}
14463
14464/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14465
14466static void
883fd55a
KS
14467dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14468 struct dwarf2_cu *cu)
6e70227d 14469{
be2daae6
TT
14470 struct decl_field fp;
14471 memset (&fp, 0, sizeof (fp));
98751a41 14472
883fd55a 14473 gdb_assert (type_can_define_types (die));
98751a41 14474
883fd55a 14475 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14476 fp.name = dwarf2_name (die, cu);
14477 fp.type = read_type_die (die, cu);
98751a41 14478
c191a687
KS
14479 /* Save accessibility. */
14480 enum dwarf_access_attribute accessibility;
14481 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14482 if (attr != NULL)
14483 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14484 else
14485 accessibility = dwarf2_default_access_attribute (die, cu);
14486 switch (accessibility)
14487 {
14488 case DW_ACCESS_public:
14489 /* The assumed value if neither private nor protected. */
14490 break;
14491 case DW_ACCESS_private:
be2daae6 14492 fp.is_private = 1;
c191a687
KS
14493 break;
14494 case DW_ACCESS_protected:
be2daae6 14495 fp.is_protected = 1;
c191a687
KS
14496 break;
14497 default:
b98664d3 14498 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14499 }
14500
883fd55a 14501 if (die->tag == DW_TAG_typedef)
be2daae6 14502 fip->typedef_field_list.push_back (fp);
883fd55a 14503 else
be2daae6 14504 fip->nested_types_list.push_back (fp);
98751a41
JK
14505}
14506
9c6a1327
TT
14507/* A convenience typedef that's used when finding the discriminant
14508 field for a variant part. */
14509typedef std::unordered_map<sect_offset, int> offset_map_type;
14510
14511/* Compute the discriminant range for a given variant. OBSTACK is
14512 where the results will be stored. VARIANT is the variant to
14513 process. IS_UNSIGNED indicates whether the discriminant is signed
14514 or unsigned. */
14515
14516static const gdb::array_view<discriminant_range>
14517convert_variant_range (struct obstack *obstack, const variant_field &variant,
14518 bool is_unsigned)
14519{
14520 std::vector<discriminant_range> ranges;
14521
14522 if (variant.default_branch)
14523 return {};
14524
14525 if (variant.discr_list_data == nullptr)
14526 {
14527 discriminant_range r
14528 = {variant.discriminant_value, variant.discriminant_value};
14529 ranges.push_back (r);
14530 }
14531 else
14532 {
14533 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14534 variant.discr_list_data->size);
14535 while (!data.empty ())
14536 {
14537 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14538 {
14539 complaint (_("invalid discriminant marker: %d"), data[0]);
14540 break;
14541 }
14542 bool is_range = data[0] == DW_DSC_range;
14543 data = data.slice (1);
14544
14545 ULONGEST low, high;
14546 unsigned int bytes_read;
14547
14548 if (data.empty ())
14549 {
14550 complaint (_("DW_AT_discr_list missing low value"));
14551 break;
14552 }
14553 if (is_unsigned)
14554 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14555 else
14556 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14557 &bytes_read);
14558 data = data.slice (bytes_read);
14559
14560 if (is_range)
14561 {
14562 if (data.empty ())
14563 {
14564 complaint (_("DW_AT_discr_list missing high value"));
14565 break;
14566 }
14567 if (is_unsigned)
14568 high = read_unsigned_leb128 (nullptr, data.data (),
14569 &bytes_read);
14570 else
14571 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14572 &bytes_read);
14573 data = data.slice (bytes_read);
14574 }
14575 else
14576 high = low;
14577
14578 ranges.push_back ({ low, high });
14579 }
14580 }
14581
14582 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14583 ranges.size ());
14584 std::copy (ranges.begin (), ranges.end (), result);
14585 return gdb::array_view<discriminant_range> (result, ranges.size ());
14586}
14587
14588static const gdb::array_view<variant_part> create_variant_parts
14589 (struct obstack *obstack,
14590 const offset_map_type &offset_map,
14591 struct field_info *fi,
14592 const std::vector<variant_part_builder> &variant_parts);
14593
14594/* Fill in a "struct variant" for a given variant field. RESULT is
14595 the variant to fill in. OBSTACK is where any needed allocations
14596 will be done. OFFSET_MAP holds the mapping from section offsets to
14597 fields for the type. FI describes the fields of the type we're
14598 processing. FIELD is the variant field we're converting. */
14599
14600static void
14601create_one_variant (variant &result, struct obstack *obstack,
14602 const offset_map_type &offset_map,
14603 struct field_info *fi, const variant_field &field)
14604{
14605 result.discriminants = convert_variant_range (obstack, field, false);
14606 result.first_field = field.first_field + fi->baseclasses.size ();
14607 result.last_field = field.last_field + fi->baseclasses.size ();
14608 result.parts = create_variant_parts (obstack, offset_map, fi,
14609 field.variant_parts);
14610}
14611
14612/* Fill in a "struct variant_part" for a given variant part. RESULT
14613 is the variant part to fill in. OBSTACK is where any needed
14614 allocations will be done. OFFSET_MAP holds the mapping from
14615 section offsets to fields for the type. FI describes the fields of
14616 the type we're processing. BUILDER is the variant part to be
14617 converted. */
14618
14619static void
14620create_one_variant_part (variant_part &result,
14621 struct obstack *obstack,
14622 const offset_map_type &offset_map,
14623 struct field_info *fi,
14624 const variant_part_builder &builder)
14625{
14626 auto iter = offset_map.find (builder.discriminant_offset);
14627 if (iter == offset_map.end ())
14628 {
14629 result.discriminant_index = -1;
14630 /* Doesn't matter. */
14631 result.is_unsigned = false;
14632 }
14633 else
14634 {
14635 result.discriminant_index = iter->second;
14636 result.is_unsigned
14637 = TYPE_UNSIGNED (FIELD_TYPE
14638 (fi->fields[result.discriminant_index].field));
14639 }
14640
14641 size_t n = builder.variants.size ();
14642 variant *output = new (obstack) variant[n];
14643 for (size_t i = 0; i < n; ++i)
14644 create_one_variant (output[i], obstack, offset_map, fi,
14645 builder.variants[i]);
14646
14647 result.variants = gdb::array_view<variant> (output, n);
14648}
14649
14650/* Create a vector of variant parts that can be attached to a type.
14651 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14652 holds the mapping from section offsets to fields for the type. FI
14653 describes the fields of the type we're processing. VARIANT_PARTS
14654 is the vector to convert. */
14655
14656static const gdb::array_view<variant_part>
14657create_variant_parts (struct obstack *obstack,
14658 const offset_map_type &offset_map,
14659 struct field_info *fi,
14660 const std::vector<variant_part_builder> &variant_parts)
14661{
14662 if (variant_parts.empty ())
14663 return {};
14664
14665 size_t n = variant_parts.size ();
14666 variant_part *result = new (obstack) variant_part[n];
14667 for (size_t i = 0; i < n; ++i)
14668 create_one_variant_part (result[i], obstack, offset_map, fi,
14669 variant_parts[i]);
14670
14671 return gdb::array_view<variant_part> (result, n);
14672}
14673
14674/* Compute the variant part vector for FIP, attaching it to TYPE when
14675 done. */
14676
14677static void
14678add_variant_property (struct field_info *fip, struct type *type,
14679 struct dwarf2_cu *cu)
14680{
14681 /* Map section offsets of fields to their field index. Note the
14682 field index here does not take the number of baseclasses into
14683 account. */
14684 offset_map_type offset_map;
14685 for (int i = 0; i < fip->fields.size (); ++i)
14686 offset_map[fip->fields[i].offset] = i;
14687
14688 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
14689 gdb::array_view<variant_part> parts
14690 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14691 fip->variant_parts);
14692
14693 struct dynamic_prop prop;
14694 prop.kind = PROP_VARIANT_PARTS;
14695 prop.data.variant_parts
14696 = ((gdb::array_view<variant_part> *)
14697 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14698
14699 add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop, type);
14700}
14701
c906108c
SS
14702/* Create the vector of fields, and attach it to the type. */
14703
14704static void
fba45db2 14705dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14706 struct dwarf2_cu *cu)
c906108c 14707{
317f7127 14708 int nfields = fip->nfields ();
c906108c
SS
14709
14710 /* Record the field count, allocate space for the array of fields,
14711 and create blank accessibility bitfields if necessary. */
14712 TYPE_NFIELDS (type) = nfields;
14713 TYPE_FIELDS (type) = (struct field *)
be2daae6 14714 TYPE_ZALLOC (type, sizeof (struct field) * nfields);
c906108c 14715
b4ba55a1 14716 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14717 {
14718 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14719
14720 TYPE_FIELD_PRIVATE_BITS (type) =
14721 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14722 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14723
14724 TYPE_FIELD_PROTECTED_BITS (type) =
14725 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14726 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14727
774b6a14
TT
14728 TYPE_FIELD_IGNORE_BITS (type) =
14729 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14730 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14731 }
14732
14733 /* If the type has baseclasses, allocate and clear a bit vector for
14734 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14735 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14736 {
be2daae6 14737 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14738 unsigned char *pointer;
c906108c
SS
14739
14740 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14741 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14742 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14743 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14744 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14745 }
14746
9c6a1327
TT
14747 if (!fip->variant_parts.empty ())
14748 add_variant_property (fip, type, cu);
2ddeaf8a 14749
be2daae6
TT
14750 /* Copy the saved-up fields into the field vector. */
14751 for (int i = 0; i < nfields; ++i)
c906108c 14752 {
be2daae6
TT
14753 struct nextfield &field
14754 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14755 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14756
be2daae6
TT
14757 TYPE_FIELD (type, i) = field.field;
14758 switch (field.accessibility)
c906108c 14759 {
c5aa993b 14760 case DW_ACCESS_private:
b4ba55a1 14761 if (cu->language != language_ada)
be2daae6 14762 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14763 break;
c906108c 14764
c5aa993b 14765 case DW_ACCESS_protected:
b4ba55a1 14766 if (cu->language != language_ada)
be2daae6 14767 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14768 break;
c906108c 14769
c5aa993b
JM
14770 case DW_ACCESS_public:
14771 break;
c906108c 14772
c5aa993b
JM
14773 default:
14774 /* Unknown accessibility. Complain and treat it as public. */
14775 {
b98664d3 14776 complaint (_("unsupported accessibility %d"),
be2daae6 14777 field.accessibility);
c5aa993b
JM
14778 }
14779 break;
c906108c 14780 }
be2daae6 14781 if (i < fip->baseclasses.size ())
c906108c 14782 {
be2daae6 14783 switch (field.virtuality)
c906108c 14784 {
c5aa993b
JM
14785 case DW_VIRTUALITY_virtual:
14786 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14787 if (cu->language == language_ada)
a73c6dcd 14788 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14789 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14790 break;
c906108c
SS
14791 }
14792 }
c906108c
SS
14793 }
14794}
14795
7d27a96d
TT
14796/* Return true if this member function is a constructor, false
14797 otherwise. */
14798
14799static int
14800dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14801{
14802 const char *fieldname;
fe978cb0 14803 const char *type_name;
7d27a96d
TT
14804 int len;
14805
14806 if (die->parent == NULL)
14807 return 0;
14808
14809 if (die->parent->tag != DW_TAG_structure_type
14810 && die->parent->tag != DW_TAG_union_type
14811 && die->parent->tag != DW_TAG_class_type)
14812 return 0;
14813
14814 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14815 type_name = dwarf2_name (die->parent, cu);
14816 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14817 return 0;
14818
14819 len = strlen (fieldname);
fe978cb0
PA
14820 return (strncmp (fieldname, type_name, len) == 0
14821 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14822}
14823
e35000a7
TBA
14824/* Check if the given VALUE is a recognized enum
14825 dwarf_defaulted_attribute constant according to DWARF5 spec,
14826 Table 7.24. */
14827
14828static bool
14829is_valid_DW_AT_defaulted (ULONGEST value)
14830{
14831 switch (value)
14832 {
14833 case DW_DEFAULTED_no:
14834 case DW_DEFAULTED_in_class:
14835 case DW_DEFAULTED_out_of_class:
14836 return true;
14837 }
14838
3142e908 14839 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
14840 return false;
14841}
14842
c906108c
SS
14843/* Add a member function to the proper fieldlist. */
14844
14845static void
107d2387 14846dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 14847 struct type *type, struct dwarf2_cu *cu)
c906108c 14848{
518817b3 14849 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 14850 struct attribute *attr;
c906108c 14851 int i;
be2daae6 14852 struct fnfieldlist *flp = nullptr;
c906108c 14853 struct fn_field *fnp;
15d034d0 14854 const char *fieldname;
f792889a 14855 struct type *this_type;
60d5a603 14856 enum dwarf_access_attribute accessibility;
c906108c 14857
b4ba55a1 14858 if (cu->language == language_ada)
a73c6dcd 14859 error (_("unexpected member function in Ada type"));
b4ba55a1 14860
2df3850c 14861 /* Get name of member function. */
39cbfefa
DJ
14862 fieldname = dwarf2_name (die, cu);
14863 if (fieldname == NULL)
2df3850c 14864 return;
c906108c 14865
c906108c 14866 /* Look up member function name in fieldlist. */
be2daae6 14867 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 14868 {
27bfe10e 14869 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
14870 {
14871 flp = &fip->fnfieldlists[i];
14872 break;
14873 }
c906108c
SS
14874 }
14875
be2daae6
TT
14876 /* Create a new fnfieldlist if necessary. */
14877 if (flp == nullptr)
c906108c 14878 {
be2daae6
TT
14879 fip->fnfieldlists.emplace_back ();
14880 flp = &fip->fnfieldlists.back ();
c906108c 14881 flp->name = fieldname;
be2daae6 14882 i = fip->fnfieldlists.size () - 1;
c906108c
SS
14883 }
14884
be2daae6
TT
14885 /* Create a new member function field and add it to the vector of
14886 fnfieldlists. */
14887 flp->fnfields.emplace_back ();
14888 fnp = &flp->fnfields.back ();
3da10d80
KS
14889
14890 /* Delay processing of the physname until later. */
9c37b5ae 14891 if (cu->language == language_cplus)
be2daae6
TT
14892 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
14893 die, cu);
3da10d80
KS
14894 else
14895 {
1d06ead6 14896 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
14897 fnp->physname = physname ? physname : "";
14898 }
14899
c906108c 14900 fnp->type = alloc_type (objfile);
f792889a
DJ
14901 this_type = read_type_die (die, cu);
14902 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
c906108c 14903 {
f792889a 14904 int nparams = TYPE_NFIELDS (this_type);
c906108c 14905
f792889a 14906 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
14907 of the method itself (TYPE_CODE_METHOD). */
14908 smash_to_method_type (fnp->type, type,
f792889a
DJ
14909 TYPE_TARGET_TYPE (this_type),
14910 TYPE_FIELDS (this_type),
14911 TYPE_NFIELDS (this_type),
14912 TYPE_VARARGS (this_type));
c906108c
SS
14913
14914 /* Handle static member functions.
c5aa993b 14915 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
14916 member functions. G++ helps GDB by marking the first
14917 parameter for non-static member functions (which is the this
14918 pointer) as artificial. We obtain this information from
14919 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 14920 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
14921 fnp->voffset = VOFFSET_STATIC;
14922 }
14923 else
b98664d3 14924 complaint (_("member function type missing for '%s'"),
3da10d80 14925 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
14926
14927 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 14928 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 14929 fnp->fcontext = die_containing_type (die, cu);
c906108c 14930
3e43a32a
MS
14931 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
14932 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
14933
14934 /* Get accessibility. */
e142c38c 14935 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14936 if (attr != nullptr)
aead7601 14937 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
14938 else
14939 accessibility = dwarf2_default_access_attribute (die, cu);
14940 switch (accessibility)
c906108c 14941 {
60d5a603
JK
14942 case DW_ACCESS_private:
14943 fnp->is_private = 1;
14944 break;
14945 case DW_ACCESS_protected:
14946 fnp->is_protected = 1;
14947 break;
c906108c
SS
14948 }
14949
b02dede2 14950 /* Check for artificial methods. */
e142c38c 14951 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
14952 if (attr && DW_UNSND (attr) != 0)
14953 fnp->is_artificial = 1;
14954
e35000a7
TBA
14955 /* Check for defaulted methods. */
14956 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
14957 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
14958 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
14959
14960 /* Check for deleted methods. */
14961 attr = dwarf2_attr (die, DW_AT_deleted, cu);
14962 if (attr != nullptr && DW_UNSND (attr) != 0)
14963 fnp->is_deleted = 1;
14964
7d27a96d
TT
14965 fnp->is_constructor = dwarf2_is_constructor (die, cu);
14966
0d564a31 14967 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
14968 function. For older versions of GCC, this is an offset in the
14969 appropriate virtual table, as specified by DW_AT_containing_type.
14970 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
14971 to the object address. */
14972
e142c38c 14973 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 14974 if (attr != nullptr)
8e19ed76 14975 {
4fc6c0d5 14976 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 14977 {
aec5aa8b
TT
14978 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
14979 {
14980 /* Old-style GCC. */
14981 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
14982 }
14983 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
14984 || (DW_BLOCK (attr)->size > 1
14985 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
14986 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
14987 {
aec5aa8b
TT
14988 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
14989 if ((fnp->voffset % cu->header.addr_size) != 0)
14990 dwarf2_complex_location_expr_complaint ();
14991 else
14992 fnp->voffset /= cu->header.addr_size;
14993 fnp->voffset += 2;
14994 }
14995 else
14996 dwarf2_complex_location_expr_complaint ();
14997
14998 if (!fnp->fcontext)
7e993ebf
KS
14999 {
15000 /* If there is no `this' field and no DW_AT_containing_type,
15001 we cannot actually find a base class context for the
15002 vtable! */
15003 if (TYPE_NFIELDS (this_type) == 0
15004 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15005 {
b98664d3 15006 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15007 "function \"%s\" (offset %s)"),
15008 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15009 }
15010 else
15011 {
15012 fnp->fcontext
15013 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15014 }
15015 }
aec5aa8b 15016 }
cd6c91b4 15017 else if (attr->form_is_section_offset ())
8e19ed76 15018 {
4d3c2250 15019 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15020 }
15021 else
15022 {
4d3c2250
KB
15023 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15024 fieldname);
8e19ed76 15025 }
0d564a31 15026 }
d48cc9dd
DJ
15027 else
15028 {
15029 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15030 if (attr && DW_UNSND (attr))
15031 {
15032 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15033 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15034 "but the vtable offset is not specified"),
9d8780f0 15035 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15036 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15037 TYPE_CPLUS_DYNAMIC (type) = 1;
15038 }
15039 }
c906108c
SS
15040}
15041
15042/* Create the vector of member function fields, and attach it to the type. */
15043
15044static void
fba45db2 15045dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15046 struct dwarf2_cu *cu)
c906108c 15047{
b4ba55a1 15048 if (cu->language == language_ada)
a73c6dcd 15049 error (_("unexpected member functions in Ada type"));
b4ba55a1 15050
c906108c
SS
15051 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15052 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15053 TYPE_ALLOC (type,
15054 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15055
be2daae6 15056 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15057 {
be2daae6 15058 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15059 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15060
be2daae6
TT
15061 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15062 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15063 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15064 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15065
15066 for (int k = 0; k < nf.fnfields.size (); ++k)
15067 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15068 }
15069
be2daae6 15070 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15071}
15072
1168df01
JB
15073/* Returns non-zero if NAME is the name of a vtable member in CU's
15074 language, zero otherwise. */
15075static int
15076is_vtable_name (const char *name, struct dwarf2_cu *cu)
15077{
15078 static const char vptr[] = "_vptr";
15079
9c37b5ae
TT
15080 /* Look for the C++ form of the vtable. */
15081 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15082 return 1;
15083
15084 return 0;
15085}
15086
c0dd20ea 15087/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15088 functions, with the ABI-specified layout. If TYPE describes
15089 such a structure, smash it into a member function type.
61049d3b
DJ
15090
15091 GCC shouldn't do this; it should just output pointer to member DIEs.
15092 This is GCC PR debug/28767. */
c0dd20ea 15093
0b92b5bb
TT
15094static void
15095quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15096{
09e2d7c7 15097 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15098
15099 /* Check for a structure with no name and two children. */
0b92b5bb
TT
15100 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
15101 return;
c0dd20ea
DJ
15102
15103 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15104 if (TYPE_FIELD_NAME (type, 0) == NULL
15105 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15106 || TYPE_FIELD_NAME (type, 1) == NULL
15107 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15108 return;
c0dd20ea
DJ
15109
15110 /* Find the type of the method. */
0b92b5bb 15111 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea
DJ
15112 if (pfn_type == NULL
15113 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
15114 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
0b92b5bb 15115 return;
c0dd20ea
DJ
15116
15117 /* Look for the "this" argument. */
15118 pfn_type = TYPE_TARGET_TYPE (pfn_type);
15119 if (TYPE_NFIELDS (pfn_type) == 0
0b92b5bb 15120 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
c0dd20ea 15121 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
0b92b5bb 15122 return;
c0dd20ea 15123
09e2d7c7 15124 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15125 new_type = alloc_type (objfile);
09e2d7c7 15126 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
c0dd20ea
DJ
15127 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
15128 TYPE_VARARGS (pfn_type));
0b92b5bb 15129 smash_to_methodptr_type (type, new_type);
c0dd20ea 15130}
1168df01 15131
2b4424c3
TT
15132/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15133 appropriate error checking and issuing complaints if there is a
15134 problem. */
15135
15136static ULONGEST
15137get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15138{
15139 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15140
15141 if (attr == nullptr)
15142 return 0;
15143
cd6c91b4 15144 if (!attr->form_is_constant ())
2b4424c3 15145 {
b98664d3 15146 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15147 " - DIE at %s [in module %s]"),
15148 sect_offset_str (die->sect_off),
15149 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15150 return 0;
15151 }
15152
15153 ULONGEST align;
15154 if (attr->form == DW_FORM_sdata)
15155 {
15156 LONGEST val = DW_SND (attr);
15157 if (val < 0)
15158 {
b98664d3 15159 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15160 " - DIE at %s [in module %s]"),
15161 sect_offset_str (die->sect_off),
15162 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15163 return 0;
15164 }
15165 align = val;
15166 }
15167 else
15168 align = DW_UNSND (attr);
15169
15170 if (align == 0)
15171 {
b98664d3 15172 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15173 " - DIE at %s [in module %s]"),
15174 sect_offset_str (die->sect_off),
15175 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15176 return 0;
15177 }
15178 if ((align & (align - 1)) != 0)
15179 {
b98664d3 15180 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15181 " - DIE at %s [in module %s]"),
15182 sect_offset_str (die->sect_off),
15183 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15184 return 0;
15185 }
15186
15187 return align;
15188}
15189
15190/* If the DIE has a DW_AT_alignment attribute, use its value to set
15191 the alignment for TYPE. */
15192
15193static void
15194maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15195 struct type *type)
15196{
15197 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15198 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15199 " - DIE at %s [in module %s]"),
15200 sect_offset_str (die->sect_off),
15201 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15202}
685b1105 15203
e35000a7
TBA
15204/* Check if the given VALUE is a valid enum dwarf_calling_convention
15205 constant for a type, according to DWARF5 spec, Table 5.5. */
15206
15207static bool
15208is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15209{
15210 switch (value)
15211 {
15212 case DW_CC_normal:
15213 case DW_CC_pass_by_reference:
15214 case DW_CC_pass_by_value:
15215 return true;
15216
15217 default:
15218 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15219 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15220 return false;
15221 }
15222}
15223
d0922fcf
TBA
15224/* Check if the given VALUE is a valid enum dwarf_calling_convention
15225 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15226 also according to GNU-specific values (see include/dwarf2.h). */
15227
15228static bool
15229is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15230{
15231 switch (value)
15232 {
15233 case DW_CC_normal:
15234 case DW_CC_program:
15235 case DW_CC_nocall:
15236 return true;
15237
15238 case DW_CC_GNU_renesas_sh:
15239 case DW_CC_GNU_borland_fastcall_i386:
15240 case DW_CC_GDB_IBM_OpenCL:
15241 return true;
15242
15243 default:
15244 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15245 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15246 return false;
15247 }
15248}
15249
c906108c 15250/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15251 (definition) to create a type for the structure or union. Fill in
15252 the type's name and general properties; the members will not be
83655187
DE
15253 processed until process_structure_scope. A symbol table entry for
15254 the type will also not be done until process_structure_scope (assuming
15255 the type has a name).
c906108c 15256
c767944b
DJ
15257 NOTE: we need to call these functions regardless of whether or not the
15258 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15259 structure or union. This gets the type entered into our set of
83655187 15260 user defined types. */
c906108c 15261
f792889a 15262static struct type *
134d01f1 15263read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15264{
518817b3 15265 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
15266 struct type *type;
15267 struct attribute *attr;
15d034d0 15268 const char *name;
c906108c 15269
348e048f
DE
15270 /* If the definition of this type lives in .debug_types, read that type.
15271 Don't follow DW_AT_specification though, that will take us back up
15272 the chain and we want to go down. */
052c8bb8 15273 attr = die->attr (DW_AT_signature);
435d3d88 15274 if (attr != nullptr)
348e048f 15275 {
ac9ec31b 15276 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15277
ac9ec31b 15278 /* The type's CU may not be the same as CU.
02142a6c 15279 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15280 return set_die_type (die, type, cu);
15281 }
15282
c0dd20ea 15283 type = alloc_type (objfile);
c906108c 15284 INIT_CPLUS_SPECIFIC (type);
93311388 15285
39cbfefa
DJ
15286 name = dwarf2_name (die, cu);
15287 if (name != NULL)
c906108c 15288 {
987504bb 15289 if (cu->language == language_cplus
c44af4eb
TT
15290 || cu->language == language_d
15291 || cu->language == language_rust)
63d06c5c 15292 {
15d034d0 15293 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15294
15295 /* dwarf2_full_name might have already finished building the DIE's
15296 type. If so, there is no need to continue. */
15297 if (get_die_type (die, cu) != NULL)
15298 return get_die_type (die, cu);
15299
e86ca25f 15300 TYPE_NAME (type) = full_name;
63d06c5c
DC
15301 }
15302 else
15303 {
d8151005
DJ
15304 /* The name is already allocated along with this objfile, so
15305 we don't need to duplicate it for the type. */
e86ca25f 15306 TYPE_NAME (type) = name;
63d06c5c 15307 }
c906108c
SS
15308 }
15309
15310 if (die->tag == DW_TAG_structure_type)
15311 {
15312 TYPE_CODE (type) = TYPE_CODE_STRUCT;
15313 }
15314 else if (die->tag == DW_TAG_union_type)
15315 {
15316 TYPE_CODE (type) = TYPE_CODE_UNION;
15317 }
15318 else
15319 {
4753d33b 15320 TYPE_CODE (type) = TYPE_CODE_STRUCT;
c906108c
SS
15321 }
15322
0cc2414c
TT
15323 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15324 TYPE_DECLARED_CLASS (type) = 1;
15325
e35000a7
TBA
15326 /* Store the calling convention in the type if it's available in
15327 the die. Otherwise the calling convention remains set to
15328 the default value DW_CC_normal. */
15329 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15330 if (attr != nullptr
15331 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15332 {
15333 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15334 TYPE_CPLUS_CALLING_CONVENTION (type)
15335 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15336 }
15337
e142c38c 15338 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15339 if (attr != nullptr)
c906108c 15340 {
cd6c91b4 15341 if (attr->form_is_constant ())
155bfbd3
JB
15342 TYPE_LENGTH (type) = DW_UNSND (attr);
15343 else
15344 {
f8e89861
TT
15345 struct dynamic_prop prop;
15346 if (attr_to_dynamic_prop (attr, die, cu, &prop,
15347 cu->per_cu->addr_type ()))
15348 add_dyn_prop (DYN_PROP_BYTE_SIZE, prop, type);
155bfbd3
JB
15349 TYPE_LENGTH (type) = 0;
15350 }
c906108c
SS
15351 }
15352 else
15353 {
15354 TYPE_LENGTH (type) = 0;
15355 }
15356
2b4424c3
TT
15357 maybe_set_alignment (cu, die, type);
15358
5230b05a 15359 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15360 {
5230b05a
WT
15361 /* ICC<14 does not output the required DW_AT_declaration on
15362 incomplete types, but gives them a size of zero. */
422b1cb0 15363 TYPE_STUB (type) = 1;
685b1105
JK
15364 }
15365 else
15366 TYPE_STUB_SUPPORTED (type) = 1;
15367
dc718098 15368 if (die_is_declaration (die, cu))
876cecd0 15369 TYPE_STUB (type) = 1;
a6c727b2
DJ
15370 else if (attr == NULL && die->child == NULL
15371 && producer_is_realview (cu->producer))
15372 /* RealView does not output the required DW_AT_declaration
15373 on incomplete types. */
15374 TYPE_STUB (type) = 1;
dc718098 15375
c906108c
SS
15376 /* We need to add the type field to the die immediately so we don't
15377 infinitely recurse when dealing with pointers to the structure
0963b4bd 15378 type within the structure itself. */
1c379e20 15379 set_die_type (die, type, cu);
c906108c 15380
7e314c57
JK
15381 /* set_die_type should be already done. */
15382 set_descriptive_type (type, die, cu);
15383
c767944b
DJ
15384 return type;
15385}
15386
9c6a1327
TT
15387static void handle_struct_member_die
15388 (struct die_info *child_die,
15389 struct type *type,
15390 struct field_info *fi,
15391 std::vector<struct symbol *> *template_args,
15392 struct dwarf2_cu *cu);
15393
15394/* A helper for handle_struct_member_die that handles
15395 DW_TAG_variant_part. */
15396
15397static void
15398handle_variant_part (struct die_info *die, struct type *type,
15399 struct field_info *fi,
15400 std::vector<struct symbol *> *template_args,
15401 struct dwarf2_cu *cu)
15402{
15403 variant_part_builder *new_part;
15404 if (fi->current_variant_part == nullptr)
15405 {
15406 fi->variant_parts.emplace_back ();
15407 new_part = &fi->variant_parts.back ();
15408 }
15409 else if (!fi->current_variant_part->processing_variant)
15410 {
15411 complaint (_("nested DW_TAG_variant_part seen "
15412 "- DIE at %s [in module %s]"),
15413 sect_offset_str (die->sect_off),
15414 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15415 return;
15416 }
15417 else
15418 {
15419 variant_field &current = fi->current_variant_part->variants.back ();
15420 current.variant_parts.emplace_back ();
15421 new_part = &current.variant_parts.back ();
15422 }
15423
15424 /* When we recurse, we want callees to add to this new variant
15425 part. */
15426 scoped_restore save_current_variant_part
15427 = make_scoped_restore (&fi->current_variant_part, new_part);
15428
15429 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15430 if (discr == NULL)
15431 {
15432 /* It's a univariant form, an extension we support. */
15433 }
15434 else if (discr->form_is_ref ())
15435 {
15436 struct dwarf2_cu *target_cu = cu;
15437 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15438
15439 new_part->discriminant_offset = target_die->sect_off;
15440 }
15441 else
15442 {
15443 complaint (_("DW_AT_discr does not have DIE reference form"
15444 " - DIE at %s [in module %s]"),
15445 sect_offset_str (die->sect_off),
15446 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15447 }
15448
15449 for (die_info *child_die = die->child;
15450 child_die != NULL;
15451 child_die = child_die->sibling)
15452 handle_struct_member_die (child_die, type, fi, template_args, cu);
15453}
15454
15455/* A helper for handle_struct_member_die that handles
15456 DW_TAG_variant. */
15457
15458static void
15459handle_variant (struct die_info *die, struct type *type,
15460 struct field_info *fi,
15461 std::vector<struct symbol *> *template_args,
15462 struct dwarf2_cu *cu)
15463{
15464 if (fi->current_variant_part == nullptr)
15465 {
15466 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15467 "- DIE at %s [in module %s]"),
15468 sect_offset_str (die->sect_off),
15469 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15470 return;
15471 }
15472 if (fi->current_variant_part->processing_variant)
15473 {
15474 complaint (_("nested DW_TAG_variant seen "
15475 "- DIE at %s [in module %s]"),
15476 sect_offset_str (die->sect_off),
15477 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
15478 return;
15479 }
15480
15481 scoped_restore save_processing_variant
15482 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15483 true);
15484
15485 fi->current_variant_part->variants.emplace_back ();
15486 variant_field &variant = fi->current_variant_part->variants.back ();
15487 variant.first_field = fi->fields.size ();
15488
15489 /* In a variant we want to get the discriminant and also add a
15490 field for our sole member child. */
15491 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15492 if (discr == nullptr)
15493 {
15494 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15495 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15496 variant.default_branch = true;
15497 else
15498 variant.discr_list_data = DW_BLOCK (discr);
15499 }
15500 else
15501 variant.discriminant_value = DW_UNSND (discr);
15502
15503 for (die_info *variant_child = die->child;
15504 variant_child != NULL;
15505 variant_child = variant_child->sibling)
15506 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15507
15508 variant.last_field = fi->fields.size ();
15509}
15510
2ddeaf8a
TT
15511/* A helper for process_structure_scope that handles a single member
15512 DIE. */
15513
15514static void
15515handle_struct_member_die (struct die_info *child_die, struct type *type,
15516 struct field_info *fi,
15517 std::vector<struct symbol *> *template_args,
15518 struct dwarf2_cu *cu)
15519{
15520 if (child_die->tag == DW_TAG_member
9c6a1327 15521 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15522 {
15523 /* NOTE: carlton/2002-11-05: A C++ static data member
15524 should be a DW_TAG_member that is a declaration, but
15525 all versions of G++ as of this writing (so through at
15526 least 3.2.1) incorrectly generate DW_TAG_variable
15527 tags for them instead. */
15528 dwarf2_add_field (fi, child_die, cu);
15529 }
15530 else if (child_die->tag == DW_TAG_subprogram)
15531 {
15532 /* Rust doesn't have member functions in the C++ sense.
15533 However, it does emit ordinary functions as children
15534 of a struct DIE. */
15535 if (cu->language == language_rust)
15536 read_func_scope (child_die, cu);
15537 else
15538 {
15539 /* C++ member function. */
15540 dwarf2_add_member_fn (fi, child_die, type, cu);
15541 }
15542 }
15543 else if (child_die->tag == DW_TAG_inheritance)
15544 {
15545 /* C++ base class field. */
15546 dwarf2_add_field (fi, child_die, cu);
15547 }
15548 else if (type_can_define_types (child_die))
15549 dwarf2_add_type_defn (fi, child_die, cu);
15550 else if (child_die->tag == DW_TAG_template_type_param
15551 || child_die->tag == DW_TAG_template_value_param)
15552 {
15553 struct symbol *arg = new_symbol (child_die, NULL, cu);
15554
15555 if (arg != NULL)
15556 template_args->push_back (arg);
15557 }
9c6a1327
TT
15558 else if (child_die->tag == DW_TAG_variant_part)
15559 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15560 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15561 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15562}
15563
c767944b
DJ
15564/* Finish creating a structure or union type, including filling in
15565 its members and creating a symbol for it. */
15566
15567static void
15568process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15569{
518817b3 15570 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
ca040673 15571 struct die_info *child_die;
c767944b
DJ
15572 struct type *type;
15573
15574 type = get_die_type (die, cu);
15575 if (type == NULL)
15576 type = read_structure_type (die, cu);
15577
3e1d3d8c 15578 bool has_template_parameters = false;
e142c38c 15579 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15580 {
15581 struct field_info fi;
2f4732b0 15582 std::vector<struct symbol *> template_args;
c906108c 15583
639d11d3 15584 child_die = die->child;
c906108c
SS
15585
15586 while (child_die && child_die->tag)
15587 {
2ddeaf8a 15588 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15589 child_die = child_die->sibling;
c906108c
SS
15590 }
15591
34eaf542 15592 /* Attach template arguments to type. */
2f4732b0 15593 if (!template_args.empty ())
34eaf542 15594 {
3e1d3d8c 15595 has_template_parameters = true;
34eaf542 15596 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15597 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15598 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15599 = XOBNEWVEC (&objfile->objfile_obstack,
15600 struct symbol *,
15601 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15602 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15603 template_args.data (),
34eaf542
TT
15604 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15605 * sizeof (struct symbol *)));
34eaf542
TT
15606 }
15607
c906108c 15608 /* Attach fields and member functions to the type. */
317f7127 15609 if (fi.nfields () > 0)
e7c27a73 15610 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15611 if (!fi.fnfieldlists.empty ())
c906108c 15612 {
e7c27a73 15613 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15614
c5aa993b 15615 /* Get the type which refers to the base class (possibly this
c906108c 15616 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15617 class from the DW_AT_containing_type attribute. This use of
15618 DW_AT_containing_type is a GNU extension. */
c906108c 15619
e142c38c 15620 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15621 {
e7c27a73 15622 struct type *t = die_containing_type (die, cu);
c906108c 15623
ae6ae975 15624 set_type_vptr_basetype (type, t);
c906108c
SS
15625 if (type == t)
15626 {
c906108c
SS
15627 int i;
15628
15629 /* Our own class provides vtbl ptr. */
15630 for (i = TYPE_NFIELDS (t) - 1;
15631 i >= TYPE_N_BASECLASSES (t);
15632 --i)
15633 {
0d5cff50 15634 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15635
1168df01 15636 if (is_vtable_name (fieldname, cu))
c906108c 15637 {
ae6ae975 15638 set_type_vptr_fieldno (type, i);
c906108c
SS
15639 break;
15640 }
15641 }
15642
15643 /* Complain if virtual function table field not found. */
15644 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15645 complaint (_("virtual function table pointer "
3e43a32a 15646 "not found when defining class '%s'"),
e86ca25f 15647 TYPE_NAME (type) ? TYPE_NAME (type) : "");
c906108c
SS
15648 }
15649 else
15650 {
ae6ae975 15651 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15652 }
15653 }
f6235d4c 15654 else if (cu->producer
61012eef 15655 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15656 {
15657 /* The IBM XLC compiler does not provide direct indication
15658 of the containing type, but the vtable pointer is
15659 always named __vfp. */
15660
15661 int i;
15662
15663 for (i = TYPE_NFIELDS (type) - 1;
15664 i >= TYPE_N_BASECLASSES (type);
15665 --i)
15666 {
15667 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15668 {
ae6ae975
DE
15669 set_type_vptr_fieldno (type, i);
15670 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15671 break;
15672 }
15673 }
15674 }
c906108c 15675 }
98751a41
JK
15676
15677 /* Copy fi.typedef_field_list linked list elements content into the
15678 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15679 if (!fi.typedef_field_list.empty ())
98751a41 15680 {
be2daae6 15681 int count = fi.typedef_field_list.size ();
98751a41 15682
a0d7a4ff 15683 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15684 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15685 = ((struct decl_field *)
be2daae6
TT
15686 TYPE_ALLOC (type,
15687 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15688 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15689
be2daae6
TT
15690 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15691 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15692 }
c767944b 15693
883fd55a
KS
15694 /* Copy fi.nested_types_list linked list elements content into the
15695 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15696 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15697 {
be2daae6 15698 int count = fi.nested_types_list.size ();
883fd55a
KS
15699
15700 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15701 TYPE_NESTED_TYPES_ARRAY (type)
15702 = ((struct decl_field *)
be2daae6
TT
15703 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15704 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15705
be2daae6
TT
15706 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15707 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15708 }
c906108c 15709 }
63d06c5c 15710
bb5ed363 15711 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15712 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15713 cu->rust_unions.push_back (type);
0b92b5bb 15714
90aeadfc
DC
15715 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15716 snapshots) has been known to create a die giving a declaration
15717 for a class that has, as a child, a die giving a definition for a
15718 nested class. So we have to process our children even if the
15719 current die is a declaration. Normally, of course, a declaration
15720 won't have any children at all. */
134d01f1 15721
ca040673
DE
15722 child_die = die->child;
15723
90aeadfc
DC
15724 while (child_die != NULL && child_die->tag)
15725 {
15726 if (child_die->tag == DW_TAG_member
15727 || child_die->tag == DW_TAG_variable
34eaf542
TT
15728 || child_die->tag == DW_TAG_inheritance
15729 || child_die->tag == DW_TAG_template_value_param
15730 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15731 {
90aeadfc 15732 /* Do nothing. */
134d01f1 15733 }
90aeadfc
DC
15734 else
15735 process_die (child_die, cu);
134d01f1 15736
436c571c 15737 child_die = child_die->sibling;
134d01f1
DJ
15738 }
15739
fa4028e9
JB
15740 /* Do not consider external references. According to the DWARF standard,
15741 these DIEs are identified by the fact that they have no byte_size
15742 attribute, and a declaration attribute. */
15743 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15744 || !die_is_declaration (die, cu))
3e1d3d8c
TT
15745 {
15746 struct symbol *sym = new_symbol (die, type, cu);
15747
15748 if (has_template_parameters)
15749 {
a776957c
TT
15750 struct symtab *symtab;
15751 if (sym != nullptr)
15752 symtab = symbol_symtab (sym);
15753 else if (cu->line_header != nullptr)
15754 {
15755 /* Any related symtab will do. */
15756 symtab
7ba99d21 15757 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15758 }
15759 else
15760 {
15761 symtab = nullptr;
15762 complaint (_("could not find suitable "
15763 "symtab for template parameter"
15764 " - DIE at %s [in module %s]"),
15765 sect_offset_str (die->sect_off),
15766 objfile_name (objfile));
15767 }
15768
15769 if (symtab != nullptr)
15770 {
15771 /* Make sure that the symtab is set on the new symbols.
15772 Even though they don't appear in this symtab directly,
15773 other parts of gdb assume that symbols do, and this is
15774 reasonably true. */
15775 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15776 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15777 }
3e1d3d8c
TT
15778 }
15779 }
134d01f1
DJ
15780}
15781
55426c9d
JB
15782/* Assuming DIE is an enumeration type, and TYPE is its associated type,
15783 update TYPE using some information only available in DIE's children. */
15784
15785static void
15786update_enumeration_type_from_children (struct die_info *die,
15787 struct type *type,
15788 struct dwarf2_cu *cu)
15789{
60f7655a 15790 struct die_info *child_die;
55426c9d
JB
15791 int unsigned_enum = 1;
15792 int flag_enum = 1;
55426c9d 15793
8268c778 15794 auto_obstack obstack;
55426c9d 15795
60f7655a
DE
15796 for (child_die = die->child;
15797 child_die != NULL && child_die->tag;
436c571c 15798 child_die = child_die->sibling)
55426c9d
JB
15799 {
15800 struct attribute *attr;
15801 LONGEST value;
15802 const gdb_byte *bytes;
15803 struct dwarf2_locexpr_baton *baton;
15804 const char *name;
60f7655a 15805
55426c9d
JB
15806 if (child_die->tag != DW_TAG_enumerator)
15807 continue;
15808
15809 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15810 if (attr == NULL)
15811 continue;
15812
15813 name = dwarf2_name (child_die, cu);
15814 if (name == NULL)
15815 name = "<anonymous enumerator>";
15816
15817 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15818 &value, &bytes, &baton);
15819 if (value < 0)
15820 {
15821 unsigned_enum = 0;
15822 flag_enum = 0;
15823 }
55426c9d 15824 else
edd45eb0
SM
15825 {
15826 if (count_one_bits_ll (value) >= 2)
15827 flag_enum = 0;
edd45eb0 15828 }
55426c9d
JB
15829
15830 /* If we already know that the enum type is neither unsigned, nor
15831 a flag type, no need to look at the rest of the enumerates. */
15832 if (!unsigned_enum && !flag_enum)
15833 break;
55426c9d
JB
15834 }
15835
15836 if (unsigned_enum)
15837 TYPE_UNSIGNED (type) = 1;
15838 if (flag_enum)
15839 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
15840}
15841
134d01f1
DJ
15842/* Given a DW_AT_enumeration_type die, set its type. We do not
15843 complete the type's fields yet, or create any symbols. */
c906108c 15844
f792889a 15845static struct type *
134d01f1 15846read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15847{
518817b3 15848 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 15849 struct type *type;
c906108c 15850 struct attribute *attr;
0114d602 15851 const char *name;
134d01f1 15852
348e048f
DE
15853 /* If the definition of this type lives in .debug_types, read that type.
15854 Don't follow DW_AT_specification though, that will take us back up
15855 the chain and we want to go down. */
052c8bb8 15856 attr = die->attr (DW_AT_signature);
435d3d88 15857 if (attr != nullptr)
348e048f 15858 {
ac9ec31b 15859 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15860
ac9ec31b 15861 /* The type's CU may not be the same as CU.
02142a6c 15862 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15863 return set_die_type (die, type, cu);
15864 }
15865
c906108c
SS
15866 type = alloc_type (objfile);
15867
15868 TYPE_CODE (type) = TYPE_CODE_ENUM;
94af9270 15869 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 15870 if (name != NULL)
e86ca25f 15871 TYPE_NAME (type) = name;
c906108c 15872
0626fc76
TT
15873 attr = dwarf2_attr (die, DW_AT_type, cu);
15874 if (attr != NULL)
15875 {
15876 struct type *underlying_type = die_type (die, cu);
15877
15878 TYPE_TARGET_TYPE (type) = underlying_type;
15879 }
15880
e142c38c 15881 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15882 if (attr != nullptr)
c906108c
SS
15883 {
15884 TYPE_LENGTH (type) = DW_UNSND (attr);
15885 }
15886 else
15887 {
15888 TYPE_LENGTH (type) = 0;
15889 }
15890
2b4424c3
TT
15891 maybe_set_alignment (cu, die, type);
15892
137033e9
JB
15893 /* The enumeration DIE can be incomplete. In Ada, any type can be
15894 declared as private in the package spec, and then defined only
15895 inside the package body. Such types are known as Taft Amendment
15896 Types. When another package uses such a type, an incomplete DIE
15897 may be generated by the compiler. */
02eb380e 15898 if (die_is_declaration (die, cu))
876cecd0 15899 TYPE_STUB (type) = 1;
02eb380e 15900
0626fc76
TT
15901 /* Finish the creation of this type by using the enum's children.
15902 We must call this even when the underlying type has been provided
15903 so that we can determine if we're looking at a "flag" enum. */
55426c9d
JB
15904 update_enumeration_type_from_children (die, type, cu);
15905
0626fc76
TT
15906 /* If this type has an underlying type that is not a stub, then we
15907 may use its attributes. We always use the "unsigned" attribute
15908 in this situation, because ordinarily we guess whether the type
15909 is unsigned -- but the guess can be wrong and the underlying type
15910 can tell us the reality. However, we defer to a local size
15911 attribute if one exists, because this lets the compiler override
15912 the underlying type if needed. */
15913 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
15914 {
9e7c9a03
HD
15915 struct type *underlying_type = TYPE_TARGET_TYPE (type);
15916 underlying_type = check_typedef (underlying_type);
15917 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 15918 if (TYPE_LENGTH (type) == 0)
9e7c9a03 15919 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 15920 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
15921 && TYPE_RAW_ALIGN (underlying_type) != 0)
15922 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
15923 }
15924
3d567982
TT
15925 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
15926
f792889a 15927 return set_die_type (die, type, cu);
134d01f1
DJ
15928}
15929
15930/* Given a pointer to a die which begins an enumeration, process all
15931 the dies that define the members of the enumeration, and create the
15932 symbol for the enumeration type.
15933
15934 NOTE: We reverse the order of the element list. */
15935
15936static void
15937process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
15938{
f792889a 15939 struct type *this_type;
134d01f1 15940
f792889a
DJ
15941 this_type = get_die_type (die, cu);
15942 if (this_type == NULL)
15943 this_type = read_enumeration_type (die, cu);
9dc481d3 15944
639d11d3 15945 if (die->child != NULL)
c906108c 15946 {
9dc481d3
DE
15947 struct die_info *child_die;
15948 struct symbol *sym;
43816ebc 15949 std::vector<struct field> fields;
15d034d0 15950 const char *name;
9dc481d3 15951
639d11d3 15952 child_die = die->child;
c906108c
SS
15953 while (child_die && child_die->tag)
15954 {
15955 if (child_die->tag != DW_TAG_enumerator)
15956 {
e7c27a73 15957 process_die (child_die, cu);
c906108c
SS
15958 }
15959 else
15960 {
39cbfefa
DJ
15961 name = dwarf2_name (child_die, cu);
15962 if (name)
c906108c 15963 {
f792889a 15964 sym = new_symbol (child_die, this_type, cu);
c906108c 15965
43816ebc
TT
15966 fields.emplace_back ();
15967 struct field &field = fields.back ();
c906108c 15968
43816ebc
TT
15969 FIELD_NAME (field) = sym->linkage_name ();
15970 FIELD_TYPE (field) = NULL;
15971 SET_FIELD_ENUMVAL (field, SYMBOL_VALUE (sym));
15972 FIELD_BITSIZE (field) = 0;
c906108c
SS
15973 }
15974 }
15975
436c571c 15976 child_die = child_die->sibling;
c906108c
SS
15977 }
15978
43816ebc 15979 if (!fields.empty ())
c906108c 15980 {
43816ebc 15981 TYPE_NFIELDS (this_type) = fields.size ();
f792889a 15982 TYPE_FIELDS (this_type) = (struct field *)
43816ebc
TT
15983 TYPE_ALLOC (this_type, sizeof (struct field) * fields.size ());
15984 memcpy (TYPE_FIELDS (this_type), fields.data (),
15985 sizeof (struct field) * fields.size ());
c906108c 15986 }
c906108c 15987 }
134d01f1 15988
6c83ed52
TT
15989 /* If we are reading an enum from a .debug_types unit, and the enum
15990 is a declaration, and the enum is not the signatured type in the
15991 unit, then we do not want to add a symbol for it. Adding a
15992 symbol would in some cases obscure the true definition of the
15993 enum, giving users an incomplete type when the definition is
15994 actually available. Note that we do not want to do this for all
15995 enums which are just declarations, because C++0x allows forward
15996 enum declarations. */
3019eac3 15997 if (cu->per_cu->is_debug_types
6c83ed52
TT
15998 && die_is_declaration (die, cu))
15999 {
52dc124a 16000 struct signatured_type *sig_type;
6c83ed52 16001
c0f78cd4 16002 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16003 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16004 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16005 return;
16006 }
16007
f792889a 16008 new_symbol (die, this_type, cu);
c906108c
SS
16009}
16010
16011/* Extract all information from a DW_TAG_array_type DIE and put it in
16012 the DIE's type field. For now, this only handles one dimensional
16013 arrays. */
16014
f792889a 16015static struct type *
e7c27a73 16016read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16017{
518817b3 16018 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 16019 struct die_info *child_die;
7e314c57 16020 struct type *type;
c906108c 16021 struct type *element_type, *range_type, *index_type;
c906108c 16022 struct attribute *attr;
15d034d0 16023 const char *name;
a405673c 16024 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16025 unsigned int bit_stride = 0;
c906108c 16026
e7c27a73 16027 element_type = die_type (die, cu);
c906108c 16028
7e314c57
JK
16029 /* The die_type call above may have already set the type for this DIE. */
16030 type = get_die_type (die, cu);
16031 if (type)
16032 return type;
16033
dc53a7ad
JB
16034 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16035 if (attr != NULL)
a405673c
JB
16036 {
16037 int stride_ok;
09ba997f 16038 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
a405673c
JB
16039
16040 byte_stride_prop
16041 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16042 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16043 prop_type);
a405673c
JB
16044 if (!stride_ok)
16045 {
b98664d3 16046 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16047 " - DIE at %s [in module %s]"),
16048 sect_offset_str (die->sect_off),
518817b3 16049 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a405673c
JB
16050 /* Ignore this attribute. We will likely not be able to print
16051 arrays of this type correctly, but there is little we can do
16052 to help if we cannot read the attribute's value. */
16053 byte_stride_prop = NULL;
16054 }
16055 }
dc53a7ad
JB
16056
16057 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16058 if (attr != NULL)
16059 bit_stride = DW_UNSND (attr);
16060
c906108c
SS
16061 /* Irix 6.2 native cc creates array types without children for
16062 arrays with unspecified length. */
639d11d3 16063 if (die->child == NULL)
c906108c 16064 {
46bf5051 16065 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16066 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16067 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16068 byte_stride_prop, bit_stride);
f792889a 16069 return set_die_type (die, type, cu);
c906108c
SS
16070 }
16071
791afaa2 16072 std::vector<struct type *> range_types;
639d11d3 16073 child_die = die->child;
c906108c
SS
16074 while (child_die && child_die->tag)
16075 {
16076 if (child_die->tag == DW_TAG_subrange_type)
16077 {
f792889a 16078 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16079
f792889a 16080 if (child_type != NULL)
a02abb62 16081 {
0963b4bd
MS
16082 /* The range type was succesfully read. Save it for the
16083 array type creation. */
791afaa2 16084 range_types.push_back (child_type);
a02abb62 16085 }
c906108c 16086 }
436c571c 16087 child_die = child_die->sibling;
c906108c
SS
16088 }
16089
16090 /* Dwarf2 dimensions are output from left to right, create the
16091 necessary array types in backwards order. */
7ca2d3a3 16092
c906108c 16093 type = element_type;
7ca2d3a3
DL
16094
16095 if (read_array_order (die, cu) == DW_ORD_col_major)
16096 {
16097 int i = 0;
9a619af0 16098
791afaa2 16099 while (i < range_types.size ())
dc53a7ad 16100 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16101 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16102 }
16103 else
16104 {
791afaa2 16105 size_t ndim = range_types.size ();
7ca2d3a3 16106 while (ndim-- > 0)
dc53a7ad 16107 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16108 byte_stride_prop, bit_stride);
7ca2d3a3 16109 }
c906108c 16110
f5f8a009
EZ
16111 /* Understand Dwarf2 support for vector types (like they occur on
16112 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16113 array type. This is not part of the Dwarf2/3 standard yet, but a
16114 custom vendor extension. The main difference between a regular
16115 array and the vector variant is that vectors are passed by value
16116 to functions. */
e142c38c 16117 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16118 if (attr != nullptr)
ea37ba09 16119 make_vector_type (type);
f5f8a009 16120
dbc98a8b
KW
16121 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16122 implementation may choose to implement triple vectors using this
16123 attribute. */
16124 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16125 if (attr != nullptr)
dbc98a8b
KW
16126 {
16127 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16128 TYPE_LENGTH (type) = DW_UNSND (attr);
16129 else
b98664d3 16130 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16131 "than the total size of elements"));
dbc98a8b
KW
16132 }
16133
39cbfefa
DJ
16134 name = dwarf2_name (die, cu);
16135 if (name)
16136 TYPE_NAME (type) = name;
6e70227d 16137
2b4424c3
TT
16138 maybe_set_alignment (cu, die, type);
16139
0963b4bd 16140 /* Install the type in the die. */
7e314c57
JK
16141 set_die_type (die, type, cu);
16142
16143 /* set_die_type should be already done. */
b4ba55a1
JB
16144 set_descriptive_type (type, die, cu);
16145
7e314c57 16146 return type;
c906108c
SS
16147}
16148
7ca2d3a3 16149static enum dwarf_array_dim_ordering
6e70227d 16150read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16151{
16152 struct attribute *attr;
16153
16154 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16155
435d3d88 16156 if (attr != nullptr)
aead7601 16157 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16158
0963b4bd
MS
16159 /* GNU F77 is a special case, as at 08/2004 array type info is the
16160 opposite order to the dwarf2 specification, but data is still
16161 laid out as per normal fortran.
7ca2d3a3 16162
0963b4bd
MS
16163 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16164 version checking. */
7ca2d3a3 16165
905e0470
PM
16166 if (cu->language == language_fortran
16167 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16168 {
16169 return DW_ORD_row_major;
16170 }
16171
6e70227d 16172 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16173 {
16174 case array_column_major:
16175 return DW_ORD_col_major;
16176 case array_row_major:
16177 default:
16178 return DW_ORD_row_major;
16179 };
16180}
16181
72019c9c 16182/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16183 the DIE's type field. */
72019c9c 16184
f792889a 16185static struct type *
72019c9c
GM
16186read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16187{
7e314c57
JK
16188 struct type *domain_type, *set_type;
16189 struct attribute *attr;
f792889a 16190
7e314c57
JK
16191 domain_type = die_type (die, cu);
16192
16193 /* The die_type call above may have already set the type for this DIE. */
16194 set_type = get_die_type (die, cu);
16195 if (set_type)
16196 return set_type;
16197
16198 set_type = create_set_type (NULL, domain_type);
16199
16200 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16201 if (attr != nullptr)
d09039dd 16202 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16203
2b4424c3
TT
16204 maybe_set_alignment (cu, die, set_type);
16205
f792889a 16206 return set_die_type (die, set_type, cu);
72019c9c 16207}
7ca2d3a3 16208
0971de02
TT
16209/* A helper for read_common_block that creates a locexpr baton.
16210 SYM is the symbol which we are marking as computed.
16211 COMMON_DIE is the DIE for the common block.
16212 COMMON_LOC is the location expression attribute for the common
16213 block itself.
16214 MEMBER_LOC is the location expression attribute for the particular
16215 member of the common block that we are processing.
16216 CU is the CU from which the above come. */
16217
16218static void
16219mark_common_block_symbol_computed (struct symbol *sym,
16220 struct die_info *common_die,
16221 struct attribute *common_loc,
16222 struct attribute *member_loc,
16223 struct dwarf2_cu *cu)
16224{
518817b3
SM
16225 struct dwarf2_per_objfile *dwarf2_per_objfile
16226 = cu->per_cu->dwarf2_per_objfile;
0971de02
TT
16227 struct objfile *objfile = dwarf2_per_objfile->objfile;
16228 struct dwarf2_locexpr_baton *baton;
16229 gdb_byte *ptr;
16230 unsigned int cu_off;
08feed99 16231 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16232 LONGEST offset = 0;
16233
16234 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16235 gdb_assert (common_loc->form_is_block ());
16236 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16237 || member_loc->form_is_constant ());
0971de02 16238
8d749320 16239 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
0971de02
TT
16240 baton->per_cu = cu->per_cu;
16241 gdb_assert (baton->per_cu);
16242
16243 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16244
cd6c91b4 16245 if (member_loc->form_is_constant ())
0971de02 16246 {
0826b30a 16247 offset = member_loc->constant_value (0);
0971de02
TT
16248 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16249 }
16250 else
16251 baton->size += DW_BLOCK (member_loc)->size;
16252
224c3ddb 16253 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16254 baton->data = ptr;
16255
16256 *ptr++ = DW_OP_call4;
9c541725 16257 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16258 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16259 ptr += 4;
16260
cd6c91b4 16261 if (member_loc->form_is_constant ())
0971de02
TT
16262 {
16263 *ptr++ = DW_OP_addr;
16264 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16265 ptr += cu->header.addr_size;
16266 }
16267 else
16268 {
16269 /* We have to copy the data here, because DW_OP_call4 will only
16270 use a DW_AT_location attribute. */
16271 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16272 ptr += DW_BLOCK (member_loc)->size;
16273 }
16274
16275 *ptr++ = DW_OP_plus;
16276 gdb_assert (ptr - baton->data == baton->size);
16277
0971de02 16278 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16279 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16280}
16281
4357ac6c
TT
16282/* Create appropriate locally-scoped variables for all the
16283 DW_TAG_common_block entries. Also create a struct common_block
16284 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16285 is used to separate the common blocks name namespace from regular
4357ac6c 16286 variable names. */
c906108c
SS
16287
16288static void
e7c27a73 16289read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16290{
0971de02
TT
16291 struct attribute *attr;
16292
16293 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16294 if (attr != nullptr)
0971de02
TT
16295 {
16296 /* Support the .debug_loc offsets. */
4fc6c0d5 16297 if (attr->form_is_block ())
0971de02
TT
16298 {
16299 /* Ok. */
16300 }
cd6c91b4 16301 else if (attr->form_is_section_offset ())
0971de02
TT
16302 {
16303 dwarf2_complex_location_expr_complaint ();
16304 attr = NULL;
16305 }
16306 else
16307 {
16308 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16309 "common block member");
16310 attr = NULL;
16311 }
16312 }
16313
639d11d3 16314 if (die->child != NULL)
c906108c 16315 {
518817b3 16316 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
4357ac6c
TT
16317 struct die_info *child_die;
16318 size_t n_entries = 0, size;
16319 struct common_block *common_block;
16320 struct symbol *sym;
74ac6d43 16321
4357ac6c
TT
16322 for (child_die = die->child;
16323 child_die && child_die->tag;
436c571c 16324 child_die = child_die->sibling)
4357ac6c
TT
16325 ++n_entries;
16326
16327 size = (sizeof (struct common_block)
16328 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16329 common_block
16330 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16331 size);
4357ac6c
TT
16332 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16333 common_block->n_entries = 0;
16334
16335 for (child_die = die->child;
16336 child_die && child_die->tag;
436c571c 16337 child_die = child_die->sibling)
4357ac6c
TT
16338 {
16339 /* Create the symbol in the DW_TAG_common_block block in the current
16340 symbol scope. */
e7c27a73 16341 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16342 if (sym != NULL)
16343 {
16344 struct attribute *member_loc;
16345
16346 common_block->contents[common_block->n_entries++] = sym;
16347
16348 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16349 cu);
16350 if (member_loc)
16351 {
16352 /* GDB has handled this for a long time, but it is
16353 not specified by DWARF. It seems to have been
16354 emitted by gfortran at least as recently as:
16355 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16356 complaint (_("Variable in common block has "
0971de02 16357 "DW_AT_data_member_location "
9d8780f0
SM
16358 "- DIE at %s [in module %s]"),
16359 sect_offset_str (child_die->sect_off),
518817b3 16360 objfile_name (objfile));
0971de02 16361
cd6c91b4 16362 if (member_loc->form_is_section_offset ())
0971de02 16363 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16364 else if (member_loc->form_is_constant ()
4fc6c0d5 16365 || member_loc->form_is_block ())
0971de02 16366 {
435d3d88 16367 if (attr != nullptr)
0971de02
TT
16368 mark_common_block_symbol_computed (sym, die, attr,
16369 member_loc, cu);
16370 }
16371 else
16372 dwarf2_complex_location_expr_complaint ();
16373 }
16374 }
c906108c 16375 }
4357ac6c
TT
16376
16377 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16378 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16379 }
16380}
16381
0114d602 16382/* Create a type for a C++ namespace. */
d9fa45fe 16383
0114d602
DJ
16384static struct type *
16385read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16386{
518817b3 16387 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16388 const char *previous_prefix, *name;
9219021c 16389 int is_anonymous;
0114d602
DJ
16390 struct type *type;
16391
16392 /* For extensions, reuse the type of the original namespace. */
16393 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16394 {
16395 struct die_info *ext_die;
16396 struct dwarf2_cu *ext_cu = cu;
9a619af0 16397
0114d602
DJ
16398 ext_die = dwarf2_extension (die, &ext_cu);
16399 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16400
16401 /* EXT_CU may not be the same as CU.
02142a6c 16402 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16403 return set_die_type (die, type, cu);
16404 }
9219021c 16405
e142c38c 16406 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16407
16408 /* Now build the name of the current namespace. */
16409
0114d602
DJ
16410 previous_prefix = determine_prefix (die, cu);
16411 if (previous_prefix[0] != '\0')
16412 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16413 previous_prefix, name, 0, cu);
0114d602
DJ
16414
16415 /* Create the type. */
19f392bc 16416 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16417
60531b24 16418 return set_die_type (die, type, cu);
0114d602
DJ
16419}
16420
22cee43f 16421/* Read a namespace scope. */
0114d602
DJ
16422
16423static void
16424read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16425{
518817b3 16426 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 16427 int is_anonymous;
9219021c 16428
5c4e30ca
DC
16429 /* Add a symbol associated to this if we haven't seen the namespace
16430 before. Also, add a using directive if it's an anonymous
16431 namespace. */
9219021c 16432
f2f0e013 16433 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16434 {
16435 struct type *type;
16436
0114d602 16437 type = read_type_die (die, cu);
e7c27a73 16438 new_symbol (die, type, cu);
5c4e30ca 16439
e8e80198 16440 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16441 if (is_anonymous)
0114d602
DJ
16442 {
16443 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16444
eb1e02fd 16445 std::vector<const char *> excludes;
804d2729 16446 add_using_directive (using_directives (cu),
22cee43f 16447 previous_prefix, TYPE_NAME (type), NULL,
eb1e02fd 16448 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16449 }
5c4e30ca 16450 }
9219021c 16451
639d11d3 16452 if (die->child != NULL)
d9fa45fe 16453 {
639d11d3 16454 struct die_info *child_die = die->child;
6e70227d 16455
d9fa45fe
DC
16456 while (child_die && child_die->tag)
16457 {
e7c27a73 16458 process_die (child_die, cu);
436c571c 16459 child_die = child_die->sibling;
d9fa45fe
DC
16460 }
16461 }
38d518c9
EZ
16462}
16463
f55ee35c
JK
16464/* Read a Fortran module as type. This DIE can be only a declaration used for
16465 imported module. Still we need that type as local Fortran "use ... only"
16466 declaration imports depend on the created type in determine_prefix. */
16467
16468static struct type *
16469read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16470{
518817b3 16471 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
15d034d0 16472 const char *module_name;
f55ee35c
JK
16473 struct type *type;
16474
16475 module_name = dwarf2_name (die, cu);
19f392bc 16476 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16477
f55ee35c
JK
16478 return set_die_type (die, type, cu);
16479}
16480
5d7cb8df
JK
16481/* Read a Fortran module. */
16482
16483static void
16484read_module (struct die_info *die, struct dwarf2_cu *cu)
16485{
16486 struct die_info *child_die = die->child;
530e8392
KB
16487 struct type *type;
16488
16489 type = read_type_die (die, cu);
16490 new_symbol (die, type, cu);
5d7cb8df 16491
5d7cb8df
JK
16492 while (child_die && child_die->tag)
16493 {
16494 process_die (child_die, cu);
436c571c 16495 child_die = child_die->sibling;
5d7cb8df
JK
16496 }
16497}
16498
38d518c9
EZ
16499/* Return the name of the namespace represented by DIE. Set
16500 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16501 namespace. */
16502
16503static const char *
e142c38c 16504namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16505{
16506 struct die_info *current_die;
16507 const char *name = NULL;
16508
16509 /* Loop through the extensions until we find a name. */
16510
16511 for (current_die = die;
16512 current_die != NULL;
f2f0e013 16513 current_die = dwarf2_extension (die, &cu))
38d518c9 16514 {
96553a0c
DE
16515 /* We don't use dwarf2_name here so that we can detect the absence
16516 of a name -> anonymous namespace. */
7d45c7c3 16517 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16518
38d518c9
EZ
16519 if (name != NULL)
16520 break;
16521 }
16522
16523 /* Is it an anonymous namespace? */
16524
16525 *is_anonymous = (name == NULL);
16526 if (*is_anonymous)
2b1dbab0 16527 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16528
16529 return name;
d9fa45fe
DC
16530}
16531
c906108c
SS
16532/* Extract all information from a DW_TAG_pointer_type DIE and add to
16533 the user defined type vector. */
16534
f792889a 16535static struct type *
e7c27a73 16536read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16537{
518817b3 16538 struct gdbarch *gdbarch
08feed99 16539 = cu->per_cu->dwarf2_per_objfile->objfile->arch ();
e7c27a73 16540 struct comp_unit_head *cu_header = &cu->header;
c906108c 16541 struct type *type;
8b2dbe47
KB
16542 struct attribute *attr_byte_size;
16543 struct attribute *attr_address_class;
16544 int byte_size, addr_class;
7e314c57
JK
16545 struct type *target_type;
16546
16547 target_type = die_type (die, cu);
c906108c 16548
7e314c57
JK
16549 /* The die_type call above may have already set the type for this DIE. */
16550 type = get_die_type (die, cu);
16551 if (type)
16552 return type;
16553
16554 type = lookup_pointer_type (target_type);
8b2dbe47 16555
e142c38c 16556 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16557 if (attr_byte_size)
16558 byte_size = DW_UNSND (attr_byte_size);
c906108c 16559 else
8b2dbe47
KB
16560 byte_size = cu_header->addr_size;
16561
e142c38c 16562 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16563 if (attr_address_class)
16564 addr_class = DW_UNSND (attr_address_class);
16565 else
16566 addr_class = DW_ADDR_none;
16567
2b4424c3
TT
16568 ULONGEST alignment = get_alignment (cu, die);
16569
16570 /* If the pointer size, alignment, or address class is different
16571 than the default, create a type variant marked as such and set
16572 the length accordingly. */
16573 if (TYPE_LENGTH (type) != byte_size
16574 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16575 && alignment != TYPE_RAW_ALIGN (type))
16576 || addr_class != DW_ADDR_none)
c906108c 16577 {
5e2b427d 16578 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16579 {
16580 int type_flags;
16581
849957d9 16582 type_flags = gdbarch_address_class_type_flags
5e2b427d 16583 (gdbarch, byte_size, addr_class);
876cecd0
TT
16584 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16585 == 0);
8b2dbe47
KB
16586 type = make_type_with_address_space (type, type_flags);
16587 }
16588 else if (TYPE_LENGTH (type) != byte_size)
16589 {
b98664d3 16590 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16591 }
2b4424c3
TT
16592 else if (TYPE_RAW_ALIGN (type) != alignment)
16593 {
b98664d3 16594 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16595 " - DIE at %s [in module %s]"),
16596 sect_offset_str (die->sect_off),
16597 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
16598 }
6e70227d 16599 else
9a619af0
MS
16600 {
16601 /* Should we also complain about unhandled address classes? */
16602 }
c906108c 16603 }
8b2dbe47
KB
16604
16605 TYPE_LENGTH (type) = byte_size;
2b4424c3 16606 set_type_align (type, alignment);
f792889a 16607 return set_die_type (die, type, cu);
c906108c
SS
16608}
16609
16610/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16611 the user defined type vector. */
16612
f792889a 16613static struct type *
e7c27a73 16614read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16615{
16616 struct type *type;
16617 struct type *to_type;
16618 struct type *domain;
16619
e7c27a73
DJ
16620 to_type = die_type (die, cu);
16621 domain = die_containing_type (die, cu);
0d5de010 16622
7e314c57
JK
16623 /* The calls above may have already set the type for this DIE. */
16624 type = get_die_type (die, cu);
16625 if (type)
16626 return type;
16627
0d5de010
DJ
16628 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
16629 type = lookup_methodptr_type (to_type);
7078baeb
TT
16630 else if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_FUNC)
16631 {
518817b3
SM
16632 struct type *new_type
16633 = alloc_type (cu->per_cu->dwarf2_per_objfile->objfile);
7078baeb
TT
16634
16635 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
16636 TYPE_FIELDS (to_type), TYPE_NFIELDS (to_type),
16637 TYPE_VARARGS (to_type));
16638 type = lookup_methodptr_type (new_type);
16639 }
0d5de010
DJ
16640 else
16641 type = lookup_memberptr_type (to_type, domain);
c906108c 16642
f792889a 16643 return set_die_type (die, type, cu);
c906108c
SS
16644}
16645
4297a3f0 16646/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16647 the user defined type vector. */
16648
f792889a 16649static struct type *
4297a3f0
AV
16650read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16651 enum type_code refcode)
c906108c 16652{
e7c27a73 16653 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16654 struct type *type, *target_type;
c906108c
SS
16655 struct attribute *attr;
16656
4297a3f0
AV
16657 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16658
7e314c57
JK
16659 target_type = die_type (die, cu);
16660
16661 /* The die_type call above may have already set the type for this DIE. */
16662 type = get_die_type (die, cu);
16663 if (type)
16664 return type;
16665
4297a3f0 16666 type = lookup_reference_type (target_type, refcode);
e142c38c 16667 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16668 if (attr != nullptr)
c906108c
SS
16669 {
16670 TYPE_LENGTH (type) = DW_UNSND (attr);
16671 }
16672 else
16673 {
107d2387 16674 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16675 }
2b4424c3 16676 maybe_set_alignment (cu, die, type);
f792889a 16677 return set_die_type (die, type, cu);
c906108c
SS
16678}
16679
cf363f18
MW
16680/* Add the given cv-qualifiers to the element type of the array. GCC
16681 outputs DWARF type qualifiers that apply to an array, not the
16682 element type. But GDB relies on the array element type to carry
16683 the cv-qualifiers. This mimics section 6.7.3 of the C99
16684 specification. */
16685
16686static struct type *
16687add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16688 struct type *base_type, int cnst, int voltl)
16689{
16690 struct type *el_type, *inner_array;
16691
16692 base_type = copy_type (base_type);
16693 inner_array = base_type;
16694
16695 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
16696 {
16697 TYPE_TARGET_TYPE (inner_array) =
16698 copy_type (TYPE_TARGET_TYPE (inner_array));
16699 inner_array = TYPE_TARGET_TYPE (inner_array);
16700 }
16701
16702 el_type = TYPE_TARGET_TYPE (inner_array);
16703 cnst |= TYPE_CONST (el_type);
16704 voltl |= TYPE_VOLATILE (el_type);
16705 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16706
16707 return set_die_type (die, base_type, cu);
16708}
16709
f792889a 16710static struct type *
e7c27a73 16711read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16712{
f792889a 16713 struct type *base_type, *cv_type;
c906108c 16714
e7c27a73 16715 base_type = die_type (die, cu);
7e314c57
JK
16716
16717 /* The die_type call above may have already set the type for this DIE. */
16718 cv_type = get_die_type (die, cu);
16719 if (cv_type)
16720 return cv_type;
16721
2f608a3a
KW
16722 /* In case the const qualifier is applied to an array type, the element type
16723 is so qualified, not the array type (section 6.7.3 of C99). */
16724 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
cf363f18 16725 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16726
f792889a
DJ
16727 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16728 return set_die_type (die, cv_type, cu);
c906108c
SS
16729}
16730
f792889a 16731static struct type *
e7c27a73 16732read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16733{
f792889a 16734 struct type *base_type, *cv_type;
c906108c 16735
e7c27a73 16736 base_type = die_type (die, cu);
7e314c57
JK
16737
16738 /* The die_type call above may have already set the type for this DIE. */
16739 cv_type = get_die_type (die, cu);
16740 if (cv_type)
16741 return cv_type;
16742
cf363f18
MW
16743 /* In case the volatile qualifier is applied to an array type, the
16744 element type is so qualified, not the array type (section 6.7.3
16745 of C99). */
16746 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
16747 return add_array_cv_type (die, cu, base_type, 0, 1);
16748
f792889a
DJ
16749 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16750 return set_die_type (die, cv_type, cu);
c906108c
SS
16751}
16752
06d66ee9
TT
16753/* Handle DW_TAG_restrict_type. */
16754
16755static struct type *
16756read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16757{
16758 struct type *base_type, *cv_type;
16759
16760 base_type = die_type (die, cu);
16761
16762 /* The die_type call above may have already set the type for this DIE. */
16763 cv_type = get_die_type (die, cu);
16764 if (cv_type)
16765 return cv_type;
16766
16767 cv_type = make_restrict_type (base_type);
16768 return set_die_type (die, cv_type, cu);
16769}
16770
a2c2acaf
MW
16771/* Handle DW_TAG_atomic_type. */
16772
16773static struct type *
16774read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16775{
16776 struct type *base_type, *cv_type;
16777
16778 base_type = die_type (die, cu);
16779
16780 /* The die_type call above may have already set the type for this DIE. */
16781 cv_type = get_die_type (die, cu);
16782 if (cv_type)
16783 return cv_type;
16784
16785 cv_type = make_atomic_type (base_type);
16786 return set_die_type (die, cv_type, cu);
16787}
16788
c906108c
SS
16789/* Extract all information from a DW_TAG_string_type DIE and add to
16790 the user defined type vector. It isn't really a user defined type,
16791 but it behaves like one, with other DIE's using an AT_user_def_type
16792 attribute to reference it. */
16793
f792889a 16794static struct type *
e7c27a73 16795read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16796{
518817b3 16797 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
08feed99 16798 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16799 struct type *type, *range_type, *index_type, *char_type;
16800 struct attribute *attr;
216a7e6b
AB
16801 struct dynamic_prop prop;
16802 bool length_is_constant = true;
16803 LONGEST length;
16804
16805 /* There are a couple of places where bit sizes might be made use of
16806 when parsing a DW_TAG_string_type, however, no producer that we know
16807 of make use of these. Handling bit sizes that are a multiple of the
16808 byte size is easy enough, but what about other bit sizes? Lets deal
16809 with that problem when we have to. Warn about these attributes being
16810 unsupported, then parse the type and ignore them like we always
16811 have. */
16812 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16813 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16814 {
16815 static bool warning_printed = false;
16816 if (!warning_printed)
16817 {
16818 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16819 "currently supported on DW_TAG_string_type."));
16820 warning_printed = true;
16821 }
16822 }
c906108c 16823
e142c38c 16824 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16825 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16826 {
16827 /* The string length describes the location at which the length of
16828 the string can be found. The size of the length field can be
16829 specified with one of the attributes below. */
16830 struct type *prop_type;
16831 struct attribute *len
16832 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16833 if (len == nullptr)
16834 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16835 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16836 {
16837 /* Pass 0 as the default as we know this attribute is constant
16838 and the default value will not be returned. */
0826b30a 16839 LONGEST sz = len->constant_value (0);
09ba997f 16840 prop_type = cu->per_cu->int_type (sz, true);
216a7e6b
AB
16841 }
16842 else
16843 {
16844 /* If the size is not specified then we assume it is the size of
16845 an address on this target. */
09ba997f 16846 prop_type = cu->per_cu->addr_sized_int_type (true);
216a7e6b
AB
16847 }
16848
16849 /* Convert the attribute into a dynamic property. */
16850 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
16851 length = 1;
16852 else
16853 length_is_constant = false;
16854 }
16855 else if (attr != nullptr)
16856 {
16857 /* This DW_AT_string_length just contains the length with no
16858 indirection. There's no need to create a dynamic property in this
16859 case. Pass 0 for the default value as we know it will not be
16860 returned in this case. */
0826b30a 16861 length = attr->constant_value (0);
216a7e6b
AB
16862 }
16863 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 16864 {
216a7e6b 16865 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 16866 length = attr->constant_value (1);
c906108c
SS
16867 }
16868 else
16869 {
216a7e6b
AB
16870 /* Use 1 as a fallback length if we have nothing else. */
16871 length = 1;
c906108c 16872 }
6ccb9162 16873
46bf5051 16874 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
16875 if (length_is_constant)
16876 range_type = create_static_range_type (NULL, index_type, 1, length);
16877 else
16878 {
16879 struct dynamic_prop low_bound;
16880
16881 low_bound.kind = PROP_CONST;
16882 low_bound.data.const_val = 1;
16883 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
16884 }
3b7538c0
UW
16885 char_type = language_string_char_type (cu->language_defn, gdbarch);
16886 type = create_string_type (NULL, char_type, range_type);
6ccb9162 16887
f792889a 16888 return set_die_type (die, type, cu);
c906108c
SS
16889}
16890
4d804846
JB
16891/* Assuming that DIE corresponds to a function, returns nonzero
16892 if the function is prototyped. */
16893
16894static int
16895prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
16896{
16897 struct attribute *attr;
16898
16899 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
16900 if (attr && (DW_UNSND (attr) != 0))
16901 return 1;
16902
16903 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 16904 is only meaningful for C, but the concept also extends to other
4d804846
JB
16905 languages that allow unprototyped functions (Eg: Objective C).
16906 For all other languages, assume that functions are always
16907 prototyped. */
16908 if (cu->language != language_c
16909 && cu->language != language_objc
16910 && cu->language != language_opencl)
16911 return 1;
16912
16913 /* RealView does not emit DW_AT_prototyped. We can not distinguish
16914 prototyped and unprototyped functions; default to prototyped,
16915 since that is more common in modern code (and RealView warns
16916 about unprototyped functions). */
16917 if (producer_is_realview (cu->producer))
16918 return 1;
16919
16920 return 0;
16921}
16922
c906108c
SS
16923/* Handle DIES due to C code like:
16924
16925 struct foo
c5aa993b
JM
16926 {
16927 int (*funcp)(int a, long l);
16928 int b;
16929 };
c906108c 16930
0963b4bd 16931 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 16932
f792889a 16933static struct type *
e7c27a73 16934read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16935{
518817b3 16936 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0963b4bd
MS
16937 struct type *type; /* Type that this function returns. */
16938 struct type *ftype; /* Function that returns above type. */
c906108c
SS
16939 struct attribute *attr;
16940
e7c27a73 16941 type = die_type (die, cu);
7e314c57
JK
16942
16943 /* The die_type call above may have already set the type for this DIE. */
16944 ftype = get_die_type (die, cu);
16945 if (ftype)
16946 return ftype;
16947
0c8b41f1 16948 ftype = lookup_function_type (type);
c906108c 16949
4d804846 16950 if (prototyped_function_p (die, cu))
a6c727b2 16951 TYPE_PROTOTYPED (ftype) = 1;
c906108c 16952
c055b101
CV
16953 /* Store the calling convention in the type if it's available in
16954 the subroutine die. Otherwise set the calling convention to
16955 the default value DW_CC_normal. */
16956 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
16957 if (attr != nullptr
16958 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
16959 TYPE_CALLING_CONVENTION (ftype)
16960 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
16961 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
16962 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
16963 else
16964 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 16965
743649fd
MW
16966 /* Record whether the function returns normally to its caller or not
16967 if the DWARF producer set that information. */
16968 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
16969 if (attr && (DW_UNSND (attr) != 0))
16970 TYPE_NO_RETURN (ftype) = 1;
16971
76c10ea2
GM
16972 /* We need to add the subroutine type to the die immediately so
16973 we don't infinitely recurse when dealing with parameters
0963b4bd 16974 declared as the same subroutine type. */
76c10ea2 16975 set_die_type (die, ftype, cu);
6e70227d 16976
639d11d3 16977 if (die->child != NULL)
c906108c 16978 {
bb5ed363 16979 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 16980 struct die_info *child_die;
8072405b 16981 int nparams, iparams;
c906108c
SS
16982
16983 /* Count the number of parameters.
16984 FIXME: GDB currently ignores vararg functions, but knows about
16985 vararg member functions. */
8072405b 16986 nparams = 0;
639d11d3 16987 child_die = die->child;
c906108c
SS
16988 while (child_die && child_die->tag)
16989 {
16990 if (child_die->tag == DW_TAG_formal_parameter)
16991 nparams++;
16992 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 16993 TYPE_VARARGS (ftype) = 1;
436c571c 16994 child_die = child_die->sibling;
c906108c
SS
16995 }
16996
16997 /* Allocate storage for parameters and fill them in. */
16998 TYPE_NFIELDS (ftype) = nparams;
16999 TYPE_FIELDS (ftype) = (struct field *)
ae5a43e0 17000 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
c906108c 17001
8072405b
JK
17002 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17003 even if we error out during the parameters reading below. */
17004 for (iparams = 0; iparams < nparams; iparams++)
17005 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17006
17007 iparams = 0;
639d11d3 17008 child_die = die->child;
c906108c
SS
17009 while (child_die && child_die->tag)
17010 {
17011 if (child_die->tag == DW_TAG_formal_parameter)
17012 {
3ce3b1ba
PA
17013 struct type *arg_type;
17014
17015 /* DWARF version 2 has no clean way to discern C++
17016 static and non-static member functions. G++ helps
17017 GDB by marking the first parameter for non-static
17018 member functions (which is the this pointer) as
17019 artificial. We pass this information to
17020 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17021
17022 DWARF version 3 added DW_AT_object_pointer, which GCC
17023 4.5 does not yet generate. */
e142c38c 17024 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17025 if (attr != nullptr)
c906108c
SS
17026 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17027 else
9c37b5ae 17028 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17029 arg_type = die_type (child_die, cu);
17030
17031 /* RealView does not mark THIS as const, which the testsuite
17032 expects. GCC marks THIS as const in method definitions,
17033 but not in the class specifications (GCC PR 43053). */
17034 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17035 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17036 {
17037 int is_this = 0;
17038 struct dwarf2_cu *arg_cu = cu;
17039 const char *name = dwarf2_name (child_die, cu);
17040
17041 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17042 if (attr != nullptr)
3ce3b1ba
PA
17043 {
17044 /* If the compiler emits this, use it. */
17045 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17046 is_this = 1;
17047 }
17048 else if (name && strcmp (name, "this") == 0)
17049 /* Function definitions will have the argument names. */
17050 is_this = 1;
17051 else if (name == NULL && iparams == 0)
17052 /* Declarations may not have the names, so like
17053 elsewhere in GDB, assume an artificial first
17054 argument is "this". */
17055 is_this = 1;
17056
17057 if (is_this)
17058 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17059 arg_type, 0);
17060 }
17061
17062 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17063 iparams++;
17064 }
436c571c 17065 child_die = child_die->sibling;
c906108c
SS
17066 }
17067 }
17068
76c10ea2 17069 return ftype;
c906108c
SS
17070}
17071
f792889a 17072static struct type *
e7c27a73 17073read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17074{
518817b3 17075 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0114d602 17076 const char *name = NULL;
3c8e0968 17077 struct type *this_type, *target_type;
c906108c 17078
94af9270 17079 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17080 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17081 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17082 set_die_type (die, this_type, cu);
3c8e0968
DE
17083 target_type = die_type (die, cu);
17084 if (target_type != this_type)
17085 TYPE_TARGET_TYPE (this_type) = target_type;
17086 else
17087 {
17088 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17089 spec and cause infinite loops in GDB. */
b98664d3 17090 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17091 "- DIE at %s [in module %s]"),
17092 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17093 TYPE_TARGET_TYPE (this_type) = NULL;
17094 }
e4003a34
TV
17095 if (name == NULL)
17096 {
17097 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17098 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17099 Handle these by just returning the target type, rather than
17100 constructing an anonymous typedef type and trying to handle this
17101 elsewhere. */
17102 set_die_type (die, target_type, cu);
17103 return target_type;
17104 }
f792889a 17105 return this_type;
c906108c
SS
17106}
17107
9b790ce7
UW
17108/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17109 (which may be different from NAME) to the architecture back-end to allow
17110 it to guess the correct format if necessary. */
17111
17112static struct type *
17113dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17114 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17115{
08feed99 17116 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17117 const struct floatformat **format;
17118 struct type *type;
17119
17120 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17121 if (format)
103a685e 17122 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17123 else
77b7c781 17124 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17125
17126 return type;
17127}
17128
eb77c9df
AB
17129/* Allocate an integer type of size BITS and name NAME. */
17130
17131static struct type *
17132dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17133 int bits, int unsigned_p, const char *name)
17134{
17135 struct type *type;
17136
17137 /* Versions of Intel's C Compiler generate an integer type called "void"
17138 instead of using DW_TAG_unspecified_type. This has been seen on
17139 at least versions 14, 17, and 18. */
35ee2dc2
AB
17140 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17141 && strcmp (name, "void") == 0)
eb77c9df
AB
17142 type = objfile_type (objfile)->builtin_void;
17143 else
17144 type = init_integer_type (objfile, bits, unsigned_p, name);
17145
17146 return type;
17147}
17148
8bdc1658
AB
17149/* Initialise and return a floating point type of size BITS suitable for
17150 use as a component of a complex number. The NAME_HINT is passed through
17151 when initialising the floating point type and is the name of the complex
17152 type.
17153
17154 As DWARF doesn't currently provide an explicit name for the components
17155 of a complex number, but it can be helpful to have these components
17156 named, we try to select a suitable name based on the size of the
17157 component. */
17158static struct type *
17159dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17160 struct objfile *objfile,
103a685e
TT
17161 int bits, const char *name_hint,
17162 enum bfd_endian byte_order)
8bdc1658 17163{
08feed99 17164 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17165 struct type *tt = nullptr;
17166
35add35e
AB
17167 /* Try to find a suitable floating point builtin type of size BITS.
17168 We're going to use the name of this type as the name for the complex
17169 target type that we are about to create. */
1db455a7 17170 switch (cu->language)
8bdc1658 17171 {
1db455a7
AB
17172 case language_fortran:
17173 switch (bits)
17174 {
17175 case 32:
17176 tt = builtin_f_type (gdbarch)->builtin_real;
17177 break;
17178 case 64:
17179 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17180 break;
17181 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17182 case 128:
17183 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17184 break;
17185 }
8bdc1658 17186 break;
1db455a7
AB
17187 default:
17188 switch (bits)
17189 {
17190 case 32:
17191 tt = builtin_type (gdbarch)->builtin_float;
17192 break;
17193 case 64:
17194 tt = builtin_type (gdbarch)->builtin_double;
17195 break;
17196 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17197 case 128:
17198 tt = builtin_type (gdbarch)->builtin_long_double;
17199 break;
17200 }
8bdc1658
AB
17201 break;
17202 }
17203
35add35e
AB
17204 /* If the type we found doesn't match the size we were looking for, then
17205 pretend we didn't find a type at all, the complex target type we
17206 create will then be nameless. */
a12e5744 17207 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17208 tt = nullptr;
17209
8bdc1658 17210 const char *name = (tt == nullptr) ? nullptr : TYPE_NAME (tt);
103a685e 17211 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17212}
17213
c906108c
SS
17214/* Find a representation of a given base type and install
17215 it in the TYPE field of the die. */
17216
f792889a 17217static struct type *
e7c27a73 17218read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17219{
518817b3 17220 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c
SS
17221 struct type *type;
17222 struct attribute *attr;
19f392bc 17223 int encoding = 0, bits = 0;
15d034d0 17224 const char *name;
34877895 17225 gdbarch *arch;
c906108c 17226
e142c38c 17227 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17228 if (attr != nullptr)
34877895 17229 encoding = DW_UNSND (attr);
e142c38c 17230 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17231 if (attr != nullptr)
34877895 17232 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17233 name = dwarf2_name (die, cu);
6ccb9162 17234 if (!name)
34877895 17235 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17236
08feed99 17237 arch = objfile->arch ();
103a685e
TT
17238 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17239
34877895
PJ
17240 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17241 if (attr)
103a685e
TT
17242 {
17243 int endianity = DW_UNSND (attr);
17244
17245 switch (endianity)
17246 {
17247 case DW_END_big:
17248 byte_order = BFD_ENDIAN_BIG;
17249 break;
17250 case DW_END_little:
17251 byte_order = BFD_ENDIAN_LITTLE;
17252 break;
17253 default:
17254 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17255 break;
17256 }
17257 }
6ccb9162
UW
17258
17259 switch (encoding)
c906108c 17260 {
6ccb9162
UW
17261 case DW_ATE_address:
17262 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17263 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17264 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17265 break;
17266 case DW_ATE_boolean:
19f392bc 17267 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17268 break;
17269 case DW_ATE_complex_float:
103a685e
TT
17270 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17271 byte_order);
93689ce9
TT
17272 if (TYPE_CODE (type) == TYPE_CODE_ERROR)
17273 {
17274 if (name == nullptr)
17275 {
17276 struct obstack *obstack
17277 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
17278 name = obconcat (obstack, "_Complex ", TYPE_NAME (type),
17279 nullptr);
17280 }
17281 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17282 }
17283 else
17284 type = init_complex_type (name, type);
6ccb9162
UW
17285 break;
17286 case DW_ATE_decimal_float:
19f392bc 17287 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17288 break;
17289 case DW_ATE_float:
103a685e 17290 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17291 break;
17292 case DW_ATE_signed:
eb77c9df 17293 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17294 break;
17295 case DW_ATE_unsigned:
3b2b8fea
TT
17296 if (cu->language == language_fortran
17297 && name
61012eef 17298 && startswith (name, "character("))
19f392bc
UW
17299 type = init_character_type (objfile, bits, 1, name);
17300 else
eb77c9df 17301 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17302 break;
17303 case DW_ATE_signed_char:
6e70227d 17304 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17305 || cu->language == language_pascal
17306 || cu->language == language_fortran)
19f392bc
UW
17307 type = init_character_type (objfile, bits, 0, name);
17308 else
eb77c9df 17309 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17310 break;
17311 case DW_ATE_unsigned_char:
868a0084 17312 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17313 || cu->language == language_pascal
c44af4eb
TT
17314 || cu->language == language_fortran
17315 || cu->language == language_rust)
19f392bc
UW
17316 type = init_character_type (objfile, bits, 1, name);
17317 else
eb77c9df 17318 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17319 break;
75079b2b 17320 case DW_ATE_UTF:
53e710ac 17321 {
53e710ac
PA
17322 if (bits == 16)
17323 type = builtin_type (arch)->builtin_char16;
17324 else if (bits == 32)
17325 type = builtin_type (arch)->builtin_char32;
17326 else
17327 {
b98664d3 17328 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17329 bits);
eb77c9df 17330 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17331 }
17332 return set_die_type (die, type, cu);
17333 }
75079b2b
TT
17334 break;
17335
6ccb9162 17336 default:
b98664d3 17337 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17338 dwarf_type_encoding_name (encoding));
77b7c781 17339 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17340 break;
c906108c 17341 }
6ccb9162 17342
0114d602 17343 if (name && strcmp (name, "char") == 0)
876cecd0 17344 TYPE_NOSIGN (type) = 1;
0114d602 17345
2b4424c3
TT
17346 maybe_set_alignment (cu, die, type);
17347
103a685e 17348 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17349
f792889a 17350 return set_die_type (die, type, cu);
c906108c
SS
17351}
17352
80180f79
SA
17353/* Parse dwarf attribute if it's a block, reference or constant and put the
17354 resulting value of the attribute into struct bound_prop.
17355 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17356
17357static int
17358attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17359 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17360 struct type *default_type)
80180f79
SA
17361{
17362 struct dwarf2_property_baton *baton;
518817b3
SM
17363 struct obstack *obstack
17364 = &cu->per_cu->dwarf2_per_objfile->objfile->objfile_obstack;
80180f79 17365
9a49df9d
AB
17366 gdb_assert (default_type != NULL);
17367
80180f79
SA
17368 if (attr == NULL || prop == NULL)
17369 return 0;
17370
4fc6c0d5 17371 if (attr->form_is_block ())
80180f79 17372 {
8d749320 17373 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17374 baton->property_type = default_type;
80180f79
SA
17375 baton->locexpr.per_cu = cu->per_cu;
17376 baton->locexpr.size = DW_BLOCK (attr)->size;
17377 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17378 switch (attr->name)
17379 {
17380 case DW_AT_string_length:
17381 baton->locexpr.is_reference = true;
17382 break;
17383 default:
17384 baton->locexpr.is_reference = false;
17385 break;
17386 }
80180f79
SA
17387 prop->data.baton = baton;
17388 prop->kind = PROP_LOCEXPR;
17389 gdb_assert (prop->data.baton != NULL);
17390 }
cd6c91b4 17391 else if (attr->form_is_ref ())
80180f79
SA
17392 {
17393 struct dwarf2_cu *target_cu = cu;
17394 struct die_info *target_die;
17395 struct attribute *target_attr;
17396
17397 target_die = follow_die_ref (die, attr, &target_cu);
17398 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17399 if (target_attr == NULL)
17400 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17401 target_cu);
80180f79
SA
17402 if (target_attr == NULL)
17403 return 0;
17404
df25ebbd 17405 switch (target_attr->name)
80180f79 17406 {
df25ebbd 17407 case DW_AT_location:
cd6c91b4 17408 if (target_attr->form_is_section_offset ())
df25ebbd 17409 {
8d749320 17410 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17411 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17412 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17413 prop->data.baton = baton;
17414 prop->kind = PROP_LOCLIST;
17415 gdb_assert (prop->data.baton != NULL);
17416 }
4fc6c0d5 17417 else if (target_attr->form_is_block ())
df25ebbd 17418 {
8d749320 17419 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17420 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17421 baton->locexpr.per_cu = cu->per_cu;
17422 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17423 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17424 baton->locexpr.is_reference = true;
df25ebbd
JB
17425 prop->data.baton = baton;
17426 prop->kind = PROP_LOCEXPR;
17427 gdb_assert (prop->data.baton != NULL);
17428 }
17429 else
17430 {
17431 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17432 "dynamic property");
17433 return 0;
17434 }
17435 break;
17436 case DW_AT_data_member_location:
17437 {
17438 LONGEST offset;
17439
17440 if (!handle_data_member_location (target_die, target_cu,
17441 &offset))
17442 return 0;
17443
8d749320 17444 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17445 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17446 target_cu);
df25ebbd
JB
17447 baton->offset_info.offset = offset;
17448 baton->offset_info.type = die_type (target_die, target_cu);
17449 prop->data.baton = baton;
17450 prop->kind = PROP_ADDR_OFFSET;
17451 break;
17452 }
80180f79
SA
17453 }
17454 }
cd6c91b4 17455 else if (attr->form_is_constant ())
80180f79 17456 {
0826b30a 17457 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17458 prop->kind = PROP_CONST;
17459 }
17460 else
17461 {
17462 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17463 dwarf2_name (die, cu));
17464 return 0;
17465 }
17466
17467 return 1;
17468}
17469
09ba997f 17470/* See read.h. */
9a49df9d 17471
09ba997f
TT
17472struct type *
17473dwarf2_per_cu_data::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17474{
09ba997f 17475 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
17476 struct type *int_type;
17477
17478 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17479#define TRY_TYPE(F) \
17480 int_type = (unsigned_p \
17481 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17482 : objfile_type (objfile)->builtin_ ## F); \
17483 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17484 return int_type
17485
17486 TRY_TYPE (char);
17487 TRY_TYPE (short);
17488 TRY_TYPE (int);
17489 TRY_TYPE (long);
17490 TRY_TYPE (long_long);
17491
17492#undef TRY_TYPE
17493
17494 gdb_assert_not_reached ("unable to find suitable integer type");
17495}
17496
09ba997f 17497/* See read.h. */
11a8b164 17498
09ba997f
TT
17499struct type *
17500dwarf2_per_cu_data::addr_sized_int_type (bool unsigned_p) const
11a8b164 17501{
09ba997f
TT
17502 int addr_size = this->addr_size ();
17503 return int_type (addr_size, unsigned_p);
11a8b164
AB
17504}
17505
b86352cf
AB
17506/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17507 present (which is valid) then compute the default type based on the
17508 compilation units address size. */
17509
17510static struct type *
17511read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17512{
17513 struct type *index_type = die_type (die, cu);
17514
17515 /* Dwarf-2 specifications explicitly allows to create subrange types
17516 without specifying a base type.
17517 In that case, the base type must be set to the type of
17518 the lower bound, upper bound or count, in that order, if any of these
17519 three attributes references an object that has a type.
17520 If no base type is found, the Dwarf-2 specifications say that
17521 a signed integer type of size equal to the size of an address should
17522 be used.
17523 For the following C code: `extern char gdb_int [];'
17524 GCC produces an empty range DIE.
17525 FIXME: muller/2010-05-28: Possible references to object for low bound,
17526 high bound or count are not yet handled by this code. */
17527 if (TYPE_CODE (index_type) == TYPE_CODE_VOID)
09ba997f 17528 index_type = cu->per_cu->addr_sized_int_type (false);
b86352cf
AB
17529
17530 return index_type;
17531}
17532
a02abb62
JB
17533/* Read the given DW_AT_subrange DIE. */
17534
f792889a 17535static struct type *
a02abb62
JB
17536read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17537{
4c9ad8c2 17538 struct type *base_type, *orig_base_type;
a02abb62
JB
17539 struct type *range_type;
17540 struct attribute *attr;
729efb13 17541 struct dynamic_prop low, high;
4fae6e18 17542 int low_default_is_valid;
c451ebe5 17543 int high_bound_is_count = 0;
15d034d0 17544 const char *name;
d359392f 17545 ULONGEST negative_mask;
e77813c8 17546
b86352cf
AB
17547 orig_base_type = read_subrange_index_type (die, cu);
17548
4c9ad8c2
TT
17549 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17550 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17551 creating the range type, but we use the result of check_typedef
17552 when examining properties of the type. */
17553 base_type = check_typedef (orig_base_type);
a02abb62 17554
7e314c57
JK
17555 /* The die_type call above may have already set the type for this DIE. */
17556 range_type = get_die_type (die, cu);
17557 if (range_type)
17558 return range_type;
17559
729efb13
SA
17560 low.kind = PROP_CONST;
17561 high.kind = PROP_CONST;
17562 high.data.const_val = 0;
17563
4fae6e18
JK
17564 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17565 omitting DW_AT_lower_bound. */
17566 switch (cu->language)
6e70227d 17567 {
4fae6e18
JK
17568 case language_c:
17569 case language_cplus:
729efb13 17570 low.data.const_val = 0;
4fae6e18
JK
17571 low_default_is_valid = 1;
17572 break;
17573 case language_fortran:
729efb13 17574 low.data.const_val = 1;
4fae6e18
JK
17575 low_default_is_valid = 1;
17576 break;
17577 case language_d:
4fae6e18 17578 case language_objc:
c44af4eb 17579 case language_rust:
729efb13 17580 low.data.const_val = 0;
4fae6e18
JK
17581 low_default_is_valid = (cu->header.version >= 4);
17582 break;
17583 case language_ada:
17584 case language_m2:
17585 case language_pascal:
729efb13 17586 low.data.const_val = 1;
4fae6e18
JK
17587 low_default_is_valid = (cu->header.version >= 4);
17588 break;
17589 default:
729efb13 17590 low.data.const_val = 0;
4fae6e18
JK
17591 low_default_is_valid = 0;
17592 break;
a02abb62
JB
17593 }
17594
e142c38c 17595 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17596 if (attr != nullptr)
9a49df9d 17597 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17598 else if (!low_default_is_valid)
b98664d3 17599 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17600 "- DIE at %s [in module %s]"),
17601 sect_offset_str (die->sect_off),
518817b3 17602 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
a02abb62 17603
506f5c41
TV
17604 struct attribute *attr_ub, *attr_count;
17605 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17606 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17607 {
506f5c41 17608 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17609 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17610 {
c451ebe5
SA
17611 /* If bounds are constant do the final calculation here. */
17612 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17613 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17614 else
17615 high_bound_is_count = 1;
c2ff108b 17616 }
506f5c41
TV
17617 else
17618 {
17619 if (attr_ub != NULL)
17620 complaint (_("Unresolved DW_AT_upper_bound "
17621 "- DIE at %s [in module %s]"),
17622 sect_offset_str (die->sect_off),
17623 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17624 if (attr_count != NULL)
17625 complaint (_("Unresolved DW_AT_count "
17626 "- DIE at %s [in module %s]"),
17627 sect_offset_str (die->sect_off),
17628 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17629 }
e77813c8 17630 }
a02abb62 17631
4e962e74
TT
17632 LONGEST bias = 0;
17633 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17634 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17635 bias = bias_attr->constant_value (0);
4e962e74 17636
dbb9c2b1
JB
17637 /* Normally, the DWARF producers are expected to use a signed
17638 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17639 But this is unfortunately not always the case, as witnessed
17640 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17641 is used instead. To work around that ambiguity, we treat
17642 the bounds as signed, and thus sign-extend their values, when
17643 the base type is signed. */
6e70227d 17644 negative_mask =
d359392f 17645 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17646 if (low.kind == PROP_CONST
17647 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17648 low.data.const_val |= negative_mask;
17649 if (high.kind == PROP_CONST
17650 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17651 high.data.const_val |= negative_mask;
43bbcdc2 17652
5bbd8269
AB
17653 /* Check for bit and byte strides. */
17654 struct dynamic_prop byte_stride_prop;
17655 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17656 if (attr_byte_stride != nullptr)
17657 {
09ba997f 17658 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17659 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17660 prop_type);
17661 }
17662
17663 struct dynamic_prop bit_stride_prop;
17664 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17665 if (attr_bit_stride != nullptr)
17666 {
17667 /* It only makes sense to have either a bit or byte stride. */
17668 if (attr_byte_stride != nullptr)
17669 {
17670 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17671 "- DIE at %s [in module %s]"),
17672 sect_offset_str (die->sect_off),
17673 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
17674 attr_bit_stride = nullptr;
17675 }
17676 else
17677 {
09ba997f 17678 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
5bbd8269
AB
17679 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17680 prop_type);
17681 }
17682 }
17683
17684 if (attr_byte_stride != nullptr
17685 || attr_bit_stride != nullptr)
17686 {
17687 bool byte_stride_p = (attr_byte_stride != nullptr);
17688 struct dynamic_prop *stride
17689 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17690
17691 range_type
17692 = create_range_type_with_stride (NULL, orig_base_type, &low,
17693 &high, bias, stride, byte_stride_p);
17694 }
17695 else
17696 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17697
c451ebe5
SA
17698 if (high_bound_is_count)
17699 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17700
c2ff108b
JK
17701 /* Ada expects an empty array on no boundary attributes. */
17702 if (attr == NULL && cu->language != language_ada)
729efb13 17703 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17704
39cbfefa
DJ
17705 name = dwarf2_name (die, cu);
17706 if (name)
17707 TYPE_NAME (range_type) = name;
6e70227d 17708
e142c38c 17709 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17710 if (attr != nullptr)
a02abb62
JB
17711 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17712
2b4424c3
TT
17713 maybe_set_alignment (cu, die, range_type);
17714
7e314c57
JK
17715 set_die_type (die, range_type, cu);
17716
17717 /* set_die_type should be already done. */
b4ba55a1
JB
17718 set_descriptive_type (range_type, die, cu);
17719
7e314c57 17720 return range_type;
a02abb62 17721}
6e70227d 17722
f792889a 17723static struct type *
81a17f79
JB
17724read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17725{
17726 struct type *type;
81a17f79 17727
518817b3
SM
17728 type = init_type (cu->per_cu->dwarf2_per_objfile->objfile, TYPE_CODE_VOID,0,
17729 NULL);
0114d602 17730 TYPE_NAME (type) = dwarf2_name (die, cu);
81a17f79 17731
74a2f8ff 17732 /* In Ada, an unspecified type is typically used when the description
85102364 17733 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17734 such a type, we treat it as a stub, and try to resolve it later on,
17735 when needed. */
17736 if (cu->language == language_ada)
17737 TYPE_STUB (type) = 1;
17738
f792889a 17739 return set_die_type (die, type, cu);
81a17f79 17740}
a02abb62 17741
639d11d3
DC
17742/* Read a single die and all its descendents. Set the die's sibling
17743 field to NULL; set other fields in the die correctly, and set all
17744 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17745 location of the info_ptr after reading all of those dies. PARENT
17746 is the parent of the die in question. */
17747
17748static struct die_info *
dee91e82 17749read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17750 const gdb_byte *info_ptr,
17751 const gdb_byte **new_info_ptr,
dee91e82 17752 struct die_info *parent)
639d11d3
DC
17753{
17754 struct die_info *die;
d521ce57 17755 const gdb_byte *cur_ptr;
639d11d3 17756
3e225074 17757 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17758 if (die == NULL)
17759 {
17760 *new_info_ptr = cur_ptr;
17761 return NULL;
17762 }
93311388 17763 store_in_ref_table (die, reader->cu);
639d11d3 17764
3e225074 17765 if (die->has_children)
bf6af496 17766 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17767 else
17768 {
17769 die->child = NULL;
17770 *new_info_ptr = cur_ptr;
17771 }
17772
17773 die->sibling = NULL;
17774 die->parent = parent;
17775 return die;
17776}
17777
17778/* Read a die, all of its descendents, and all of its siblings; set
17779 all of the fields of all of the dies correctly. Arguments are as
17780 in read_die_and_children. */
17781
17782static struct die_info *
bf6af496 17783read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17784 const gdb_byte *info_ptr,
17785 const gdb_byte **new_info_ptr,
bf6af496 17786 struct die_info *parent)
639d11d3
DC
17787{
17788 struct die_info *first_die, *last_sibling;
d521ce57 17789 const gdb_byte *cur_ptr;
639d11d3 17790
c906108c 17791 cur_ptr = info_ptr;
639d11d3
DC
17792 first_die = last_sibling = NULL;
17793
17794 while (1)
c906108c 17795 {
639d11d3 17796 struct die_info *die
dee91e82 17797 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17798
1d325ec1 17799 if (die == NULL)
c906108c 17800 {
639d11d3
DC
17801 *new_info_ptr = cur_ptr;
17802 return first_die;
c906108c 17803 }
1d325ec1
DJ
17804
17805 if (!first_die)
17806 first_die = die;
c906108c 17807 else
1d325ec1
DJ
17808 last_sibling->sibling = die;
17809
17810 last_sibling = die;
c906108c 17811 }
c906108c
SS
17812}
17813
bf6af496
DE
17814/* Read a die, all of its descendents, and all of its siblings; set
17815 all of the fields of all of the dies correctly. Arguments are as
17816 in read_die_and_children.
17817 This the main entry point for reading a DIE and all its children. */
17818
17819static struct die_info *
17820read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17821 const gdb_byte *info_ptr,
17822 const gdb_byte **new_info_ptr,
bf6af496
DE
17823 struct die_info *parent)
17824{
17825 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17826 new_info_ptr, parent);
17827
b4f54984 17828 if (dwarf_die_debug)
bf6af496
DE
17829 {
17830 fprintf_unfiltered (gdb_stdlog,
17831 "Read die from %s@0x%x of %s:\n",
96b79293 17832 reader->die_section->get_name (),
bf6af496
DE
17833 (unsigned) (info_ptr - reader->die_section->buffer),
17834 bfd_get_filename (reader->abfd));
b4f54984 17835 dump_die (die, dwarf_die_debug);
bf6af496
DE
17836 }
17837
17838 return die;
17839}
17840
3019eac3
DE
17841/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
17842 attributes.
17843 The caller is responsible for filling in the extra attributes
17844 and updating (*DIEP)->num_attrs.
17845 Set DIEP to point to a newly allocated die with its information,
3e225074 17846 except for its child, sibling, and parent fields. */
93311388 17847
d521ce57 17848static const gdb_byte *
3019eac3 17849read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 17850 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 17851 int num_extra_attrs)
93311388 17852{
b64f50a1 17853 unsigned int abbrev_number, bytes_read, i;
93311388
DE
17854 struct abbrev_info *abbrev;
17855 struct die_info *die;
17856 struct dwarf2_cu *cu = reader->cu;
17857 bfd *abfd = reader->abfd;
17858
9c541725 17859 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
17860 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
17861 info_ptr += bytes_read;
17862 if (!abbrev_number)
17863 {
17864 *diep = NULL;
93311388
DE
17865 return info_ptr;
17866 }
17867
685af9cd 17868 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 17869 if (!abbrev)
348e048f
DE
17870 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
17871 abbrev_number,
17872 bfd_get_filename (abfd));
17873
3019eac3 17874 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 17875 die->sect_off = sect_off;
93311388
DE
17876 die->tag = abbrev->tag;
17877 die->abbrev = abbrev_number;
3e225074 17878 die->has_children = abbrev->has_children;
93311388 17879
3019eac3
DE
17880 /* Make the result usable.
17881 The caller needs to update num_attrs after adding the extra
17882 attributes. */
93311388
DE
17883 die->num_attrs = abbrev->num_attrs;
17884
18a8505e 17885 std::vector<int> indexes_that_need_reprocess;
93311388 17886 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
17887 {
17888 bool need_reprocess;
17889 info_ptr =
17890 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
17891 info_ptr, &need_reprocess);
17892 if (need_reprocess)
17893 indexes_that_need_reprocess.push_back (i);
17894 }
17895
052c8bb8 17896 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
17897 if (attr != nullptr)
17898 cu->str_offsets_base = DW_UNSND (attr);
93311388 17899
41144253 17900 attr = die->attr (DW_AT_loclists_base);
17901 if (attr != nullptr)
17902 cu->loclist_base = DW_UNSND (attr);
17903
a39fdb41 17904 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
17905 if (maybe_addr_base.has_value ())
17906 cu->addr_base = *maybe_addr_base;
17907 for (int index : indexes_that_need_reprocess)
17908 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 17909 *diep = die;
93311388
DE
17910 return info_ptr;
17911}
17912
3019eac3
DE
17913/* Read a die and all its attributes.
17914 Set DIEP to point to a newly allocated die with its information,
3e225074 17915 except for its child, sibling, and parent fields. */
3019eac3 17916
d521ce57 17917static const gdb_byte *
3019eac3 17918read_full_die (const struct die_reader_specs *reader,
3e225074 17919 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 17920{
d521ce57 17921 const gdb_byte *result;
bf6af496 17922
3e225074 17923 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 17924
b4f54984 17925 if (dwarf_die_debug)
bf6af496
DE
17926 {
17927 fprintf_unfiltered (gdb_stdlog,
17928 "Read die from %s@0x%x of %s:\n",
96b79293 17929 reader->die_section->get_name (),
bf6af496
DE
17930 (unsigned) (info_ptr - reader->die_section->buffer),
17931 bfd_get_filename (reader->abfd));
b4f54984 17932 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
17933 }
17934
17935 return result;
3019eac3 17936}
433df2d4 17937\f
c906108c 17938
72bf9492
DJ
17939/* Returns nonzero if TAG represents a type that we might generate a partial
17940 symbol for. */
17941
17942static int
17943is_type_tag_for_partial (int tag)
17944{
17945 switch (tag)
17946 {
17947#if 0
17948 /* Some types that would be reasonable to generate partial symbols for,
17949 that we don't at present. */
17950 case DW_TAG_array_type:
17951 case DW_TAG_file_type:
17952 case DW_TAG_ptr_to_member_type:
17953 case DW_TAG_set_type:
17954 case DW_TAG_string_type:
17955 case DW_TAG_subroutine_type:
17956#endif
17957 case DW_TAG_base_type:
17958 case DW_TAG_class_type:
680b30c7 17959 case DW_TAG_interface_type:
72bf9492
DJ
17960 case DW_TAG_enumeration_type:
17961 case DW_TAG_structure_type:
17962 case DW_TAG_subrange_type:
17963 case DW_TAG_typedef:
17964 case DW_TAG_union_type:
17965 return 1;
17966 default:
17967 return 0;
17968 }
17969}
17970
17971/* Load all DIEs that are interesting for partial symbols into memory. */
17972
17973static struct partial_die_info *
dee91e82 17974load_partial_dies (const struct die_reader_specs *reader,
d521ce57 17975 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 17976{
dee91e82 17977 struct dwarf2_cu *cu = reader->cu;
518817b3 17978 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
72bf9492 17979 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 17980 unsigned int bytes_read;
5afb4e99 17981 unsigned int load_all = 0;
72bf9492
DJ
17982 int nesting_level = 1;
17983
17984 parent_die = NULL;
17985 last_die = NULL;
17986
7adf1e79
DE
17987 gdb_assert (cu->per_cu != NULL);
17988 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
17989 load_all = 1;
17990
72bf9492
DJ
17991 cu->partial_dies
17992 = htab_create_alloc_ex (cu->header.length / 12,
17993 partial_die_hash,
17994 partial_die_eq,
17995 NULL,
17996 &cu->comp_unit_obstack,
17997 hashtab_obstack_allocate,
17998 dummy_obstack_deallocate);
17999
72bf9492
DJ
18000 while (1)
18001 {
685af9cd 18002 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18003
18004 /* A NULL abbrev means the end of a series of children. */
18005 if (abbrev == NULL)
18006 {
18007 if (--nesting_level == 0)
cd9983dd
YQ
18008 return first_die;
18009
72bf9492
DJ
18010 info_ptr += bytes_read;
18011 last_die = parent_die;
18012 parent_die = parent_die->die_parent;
18013 continue;
18014 }
18015
98bfdba5
PA
18016 /* Check for template arguments. We never save these; if
18017 they're seen, we just mark the parent, and go on our way. */
18018 if (parent_die != NULL
18019 && cu->language == language_cplus
18020 && (abbrev->tag == DW_TAG_template_type_param
18021 || abbrev->tag == DW_TAG_template_value_param))
18022 {
18023 parent_die->has_template_arguments = 1;
18024
18025 if (!load_all)
18026 {
18027 /* We don't need a partial DIE for the template argument. */
dee91e82 18028 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18029 continue;
18030 }
18031 }
18032
0d99eb77 18033 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18034 Skip their other children. */
18035 if (!load_all
18036 && cu->language == language_cplus
18037 && parent_die != NULL
18038 && parent_die->tag == DW_TAG_subprogram)
18039 {
dee91e82 18040 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18041 continue;
18042 }
18043
5afb4e99
DJ
18044 /* Check whether this DIE is interesting enough to save. Normally
18045 we would not be interested in members here, but there may be
18046 later variables referencing them via DW_AT_specification (for
18047 static members). */
18048 if (!load_all
18049 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18050 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18051 && abbrev->tag != DW_TAG_enumerator
18052 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18053 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18054 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18055 && abbrev->tag != DW_TAG_variable
5afb4e99 18056 && abbrev->tag != DW_TAG_namespace
f55ee35c 18057 && abbrev->tag != DW_TAG_module
95554aad 18058 && abbrev->tag != DW_TAG_member
74921315
KS
18059 && abbrev->tag != DW_TAG_imported_unit
18060 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18061 {
18062 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18063 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18064 continue;
18065 }
18066
6f06d47b
YQ
18067 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18068 abbrev);
cd9983dd 18069
48fbe735 18070 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18071
18072 /* This two-pass algorithm for processing partial symbols has a
18073 high cost in cache pressure. Thus, handle some simple cases
18074 here which cover the majority of C partial symbols. DIEs
18075 which neither have specification tags in them, nor could have
18076 specification tags elsewhere pointing at them, can simply be
18077 processed and discarded.
18078
18079 This segment is also optional; scan_partial_symbols and
18080 add_partial_symbol will handle these DIEs if we chain
18081 them in normally. When compilers which do not emit large
18082 quantities of duplicate debug information are more common,
18083 this code can probably be removed. */
18084
18085 /* Any complete simple types at the top level (pretty much all
18086 of them, for a language without namespaces), can be processed
18087 directly. */
18088 if (parent_die == NULL
cd9983dd
YQ
18089 && pdi.has_specification == 0
18090 && pdi.is_declaration == 0
18091 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18092 || pdi.tag == DW_TAG_base_type
18093 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18094 {
cd9983dd 18095 if (building_psymtab && pdi.name != NULL)
31edb802 18096 add_psymbol_to_list (pdi.name, false,
79748972 18097 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18098 psymbol_placement::STATIC,
1762568f 18099 0, cu->language, objfile);
cd9983dd 18100 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18101 continue;
18102 }
18103
d8228535
JK
18104 /* The exception for DW_TAG_typedef with has_children above is
18105 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18106 type_name_or_error will error on such types later.
d8228535
JK
18107
18108 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18109 it could not find the child DIEs referenced later, this is checked
18110 above. In correct DWARF DW_TAG_typedef should have no children. */
18111
cd9983dd 18112 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18113 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18114 "- DIE at %s [in module %s]"),
cd9983dd 18115 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18116
72bf9492
DJ
18117 /* If we're at the second level, and we're an enumerator, and
18118 our parent has no specification (meaning possibly lives in a
18119 namespace elsewhere), then we can add the partial symbol now
18120 instead of queueing it. */
cd9983dd 18121 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18122 && parent_die != NULL
18123 && parent_die->die_parent == NULL
18124 && parent_die->tag == DW_TAG_enumeration_type
18125 && parent_die->has_specification == 0)
18126 {
cd9983dd 18127 if (pdi.name == NULL)
b98664d3 18128 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18129 else if (building_psymtab)
31edb802 18130 add_psymbol_to_list (pdi.name, false,
79748972 18131 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18132 cu->language == language_cplus
75aedd27
TT
18133 ? psymbol_placement::GLOBAL
18134 : psymbol_placement::STATIC,
1762568f 18135 0, cu->language, objfile);
72bf9492 18136
cd9983dd 18137 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18138 continue;
18139 }
18140
cd9983dd 18141 struct partial_die_info *part_die
6f06d47b 18142 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18143
72bf9492
DJ
18144 /* We'll save this DIE so link it in. */
18145 part_die->die_parent = parent_die;
18146 part_die->die_sibling = NULL;
18147 part_die->die_child = NULL;
18148
18149 if (last_die && last_die == parent_die)
18150 last_die->die_child = part_die;
18151 else if (last_die)
18152 last_die->die_sibling = part_die;
18153
18154 last_die = part_die;
18155
18156 if (first_die == NULL)
18157 first_die = part_die;
18158
18159 /* Maybe add the DIE to the hash table. Not all DIEs that we
18160 find interesting need to be in the hash table, because we
18161 also have the parent/sibling/child chains; only those that we
18162 might refer to by offset later during partial symbol reading.
18163
18164 For now this means things that might have be the target of a
18165 DW_AT_specification, DW_AT_abstract_origin, or
18166 DW_AT_extension. DW_AT_extension will refer only to
18167 namespaces; DW_AT_abstract_origin refers to functions (and
18168 many things under the function DIE, but we do not recurse
18169 into function DIEs during partial symbol reading) and
18170 possibly variables as well; DW_AT_specification refers to
18171 declarations. Declarations ought to have the DW_AT_declaration
18172 flag. It happens that GCC forgets to put it in sometimes, but
18173 only for functions, not for types.
18174
18175 Adding more things than necessary to the hash table is harmless
18176 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18177 wasted time in find_partial_die, when we reread the compilation
18178 unit with load_all_dies set. */
72bf9492 18179
5afb4e99 18180 if (load_all
72929c62 18181 || abbrev->tag == DW_TAG_constant
5afb4e99 18182 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18183 || abbrev->tag == DW_TAG_variable
18184 || abbrev->tag == DW_TAG_namespace
18185 || part_die->is_declaration)
18186 {
18187 void **slot;
18188
18189 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18190 to_underlying (part_die->sect_off),
18191 INSERT);
72bf9492
DJ
18192 *slot = part_die;
18193 }
18194
72bf9492 18195 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18196 we have no reason to follow the children of structures; for other
98bfdba5
PA
18197 languages we have to, so that we can get at method physnames
18198 to infer fully qualified class names, for DW_AT_specification,
18199 and for C++ template arguments. For C++, we also look one level
18200 inside functions to find template arguments (if the name of the
18201 function does not already contain the template arguments).
bc30ff58 18202
0a4b0913
AB
18203 For Ada and Fortran, we need to scan the children of subprograms
18204 and lexical blocks as well because these languages allow the
18205 definition of nested entities that could be interesting for the
18206 debugger, such as nested subprograms for instance. */
72bf9492 18207 if (last_die->has_children
5afb4e99
DJ
18208 && (load_all
18209 || last_die->tag == DW_TAG_namespace
f55ee35c 18210 || last_die->tag == DW_TAG_module
72bf9492 18211 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18212 || (cu->language == language_cplus
18213 && last_die->tag == DW_TAG_subprogram
18214 && (last_die->name == NULL
18215 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18216 || (cu->language != language_c
18217 && (last_die->tag == DW_TAG_class_type
680b30c7 18218 || last_die->tag == DW_TAG_interface_type
72bf9492 18219 || last_die->tag == DW_TAG_structure_type
bc30ff58 18220 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18221 || ((cu->language == language_ada
18222 || cu->language == language_fortran)
bc30ff58
JB
18223 && (last_die->tag == DW_TAG_subprogram
18224 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18225 {
18226 nesting_level++;
18227 parent_die = last_die;
18228 continue;
18229 }
18230
18231 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18232 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18233
18234 /* Back to the top, do it again. */
18235 }
18236}
18237
6f06d47b
YQ
18238partial_die_info::partial_die_info (sect_offset sect_off_,
18239 struct abbrev_info *abbrev)
18240 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18241{
18242}
18243
35cc7ed7
YQ
18244/* Read a minimal amount of information into the minimal die structure.
18245 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18246
48fbe735
YQ
18247const gdb_byte *
18248partial_die_info::read (const struct die_reader_specs *reader,
18249 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18250{
dee91e82 18251 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18252 struct dwarf2_per_objfile *dwarf2_per_objfile
18253 = cu->per_cu->dwarf2_per_objfile;
fa238c03 18254 unsigned int i;
c5aa993b 18255 int has_low_pc_attr = 0;
c906108c 18256 int has_high_pc_attr = 0;
91da1414 18257 int high_pc_relative = 0;
c906108c 18258
fd0a254f 18259 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18260 {
e7da7f8f 18261 attribute attr;
18a8505e 18262 bool need_reprocess;
e7da7f8f 18263 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18264 info_ptr, &need_reprocess);
18265 /* String and address offsets that need to do the reprocessing have
18266 already been read at this point, so there is no need to wait until
18267 the loop terminates to do the reprocessing. */
18268 if (need_reprocess)
e7da7f8f 18269 read_attribute_reprocess (reader, &attr);
c906108c 18270 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18271 partial symbol table. */
c906108c
SS
18272 switch (attr.name)
18273 {
18274 case DW_AT_name:
48fbe735 18275 switch (tag)
71c25dea
TT
18276 {
18277 case DW_TAG_compile_unit:
95554aad 18278 case DW_TAG_partial_unit:
348e048f 18279 case DW_TAG_type_unit:
71c25dea
TT
18280 /* Compilation units have a DW_AT_name that is a filename, not
18281 a source language identifier. */
18282 case DW_TAG_enumeration_type:
18283 case DW_TAG_enumerator:
18284 /* These tags always have simple identifiers already; no need
18285 to canonicalize them. */
48fbe735 18286 name = DW_STRING (&attr);
71c25dea
TT
18287 break;
18288 default:
48fbe735
YQ
18289 {
18290 struct objfile *objfile = dwarf2_per_objfile->objfile;
18291
18292 name
be1e3d3e 18293 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18294 }
71c25dea
TT
18295 break;
18296 }
c906108c 18297 break;
31ef98ae 18298 case DW_AT_linkage_name:
c906108c 18299 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18300 /* Note that both forms of linkage name might appear. We
18301 assume they will be the same, and we only store the last
18302 one we see. */
48fbe735 18303 linkage_name = DW_STRING (&attr);
787de330
TT
18304 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18305 See https://github.com/rust-lang/rust/issues/32925. */
18306 if (cu->language == language_rust && linkage_name != NULL
18307 && strchr (linkage_name, '{') != NULL)
18308 linkage_name = NULL;
c906108c
SS
18309 break;
18310 case DW_AT_low_pc:
18311 has_low_pc_attr = 1;
cd6c91b4 18312 lowpc = attr.value_as_address ();
c906108c
SS
18313 break;
18314 case DW_AT_high_pc:
18315 has_high_pc_attr = 1;
cd6c91b4
TT
18316 highpc = attr.value_as_address ();
18317 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18318 high_pc_relative = 1;
c906108c
SS
18319 break;
18320 case DW_AT_location:
0963b4bd 18321 /* Support the .debug_loc offsets. */
4fc6c0d5 18322 if (attr.form_is_block ())
8e19ed76 18323 {
48fbe735 18324 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18325 }
cd6c91b4 18326 else if (attr.form_is_section_offset ())
8e19ed76 18327 {
4d3c2250 18328 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18329 }
18330 else
18331 {
4d3c2250
KB
18332 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18333 "partial symbol information");
8e19ed76 18334 }
c906108c 18335 break;
c906108c 18336 case DW_AT_external:
48fbe735 18337 is_external = DW_UNSND (&attr);
c906108c
SS
18338 break;
18339 case DW_AT_declaration:
48fbe735 18340 is_declaration = DW_UNSND (&attr);
c906108c
SS
18341 break;
18342 case DW_AT_type:
48fbe735 18343 has_type = 1;
c906108c
SS
18344 break;
18345 case DW_AT_abstract_origin:
18346 case DW_AT_specification:
72bf9492 18347 case DW_AT_extension:
48fbe735 18348 has_specification = 1;
0826b30a 18349 spec_offset = attr.get_ref_die_offset ();
48fbe735 18350 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18351 || cu->per_cu->is_dwz);
c906108c
SS
18352 break;
18353 case DW_AT_sibling:
18354 /* Ignore absolute siblings, they might point outside of
18355 the current compile unit. */
18356 if (attr.form == DW_FORM_ref_addr)
b98664d3 18357 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18358 else
b9502d3f 18359 {
48fbe735 18360 const gdb_byte *buffer = reader->buffer;
0826b30a 18361 sect_offset off = attr.get_ref_die_offset ();
9c541725 18362 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18363
18364 if (sibling_ptr < info_ptr)
b98664d3 18365 complaint (_("DW_AT_sibling points backwards"));
22869d73 18366 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18367 reader->die_section->overflow_complaint ();
b9502d3f 18368 else
48fbe735 18369 sibling = sibling_ptr;
b9502d3f 18370 }
c906108c 18371 break;
fa4028e9 18372 case DW_AT_byte_size:
48fbe735 18373 has_byte_size = 1;
fa4028e9 18374 break;
ff908ebf 18375 case DW_AT_const_value:
48fbe735 18376 has_const_value = 1;
ff908ebf 18377 break;
68511cec
CES
18378 case DW_AT_calling_convention:
18379 /* DWARF doesn't provide a way to identify a program's source-level
18380 entry point. DW_AT_calling_convention attributes are only meant
18381 to describe functions' calling conventions.
18382
18383 However, because it's a necessary piece of information in
0c1b455e
TT
18384 Fortran, and before DWARF 4 DW_CC_program was the only
18385 piece of debugging information whose definition refers to
18386 a 'main program' at all, several compilers marked Fortran
18387 main programs with DW_CC_program --- even when those
18388 functions use the standard calling conventions.
18389
18390 Although DWARF now specifies a way to provide this
18391 information, we support this practice for backward
18392 compatibility. */
68511cec 18393 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18394 && cu->language == language_fortran)
48fbe735 18395 main_subprogram = 1;
68511cec 18396 break;
481860b3
GB
18397 case DW_AT_inline:
18398 if (DW_UNSND (&attr) == DW_INL_inlined
18399 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18400 may_be_inlined = 1;
481860b3 18401 break;
95554aad
TT
18402
18403 case DW_AT_import:
48fbe735 18404 if (tag == DW_TAG_imported_unit)
36586728 18405 {
0826b30a 18406 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18407 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18408 || cu->per_cu->is_dwz);
18409 }
95554aad
TT
18410 break;
18411
0c1b455e 18412 case DW_AT_main_subprogram:
48fbe735 18413 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18414 break;
18415
05caa1d2
TT
18416 case DW_AT_ranges:
18417 {
18418 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18419 but that requires a full DIE, so instead we just
18420 reimplement it. */
18421 int need_ranges_base = tag != DW_TAG_compile_unit;
18422 unsigned int ranges_offset = (DW_UNSND (&attr)
18423 + (need_ranges_base
18424 ? cu->ranges_base
18425 : 0));
18426
18427 /* Value of the DW_AT_ranges attribute is the offset in the
18428 .debug_ranges section. */
18429 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18430 nullptr))
18431 has_pc_info = 1;
18432 }
18433 break;
18434
c906108c
SS
18435 default:
18436 break;
18437 }
18438 }
18439
10d06d82
TT
18440 /* For Ada, if both the name and the linkage name appear, we prefer
18441 the latter. This lets "catch exception" work better, regardless
18442 of the order in which the name and linkage name were emitted.
18443 Really, though, this is just a workaround for the fact that gdb
18444 doesn't store both the name and the linkage name. */
18445 if (cu->language == language_ada && linkage_name != nullptr)
18446 name = linkage_name;
18447
91da1414 18448 if (high_pc_relative)
48fbe735 18449 highpc += lowpc;
91da1414 18450
9373cf26
JK
18451 if (has_low_pc_attr && has_high_pc_attr)
18452 {
18453 /* When using the GNU linker, .gnu.linkonce. sections are used to
18454 eliminate duplicate copies of functions and vtables and such.
18455 The linker will arbitrarily choose one and discard the others.
18456 The AT_*_pc values for such functions refer to local labels in
18457 these sections. If the section from that file was discarded, the
18458 labels are not in the output, so the relocs get a value of 0.
18459 If this is a discarded function, mark the pc bounds as invalid,
18460 so that GDB will ignore it. */
48fbe735 18461 if (lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9373cf26 18462 {
48fbe735 18463 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18464 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18465
b98664d3 18466 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18467 "for DIE at %s [in module %s]"),
48fbe735
YQ
18468 paddress (gdbarch, lowpc),
18469 sect_offset_str (sect_off),
9d8780f0 18470 objfile_name (objfile));
9373cf26
JK
18471 }
18472 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18473 else if (lowpc >= highpc)
9373cf26 18474 {
48fbe735 18475 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18476 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18477
b98664d3 18478 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18479 "for DIE at %s [in module %s]"),
48fbe735
YQ
18480 paddress (gdbarch, lowpc),
18481 paddress (gdbarch, highpc),
18482 sect_offset_str (sect_off),
9c541725 18483 objfile_name (objfile));
9373cf26
JK
18484 }
18485 else
48fbe735 18486 has_pc_info = 1;
9373cf26 18487 }
85cbf3d3 18488
c906108c
SS
18489 return info_ptr;
18490}
18491
72bf9492
DJ
18492/* Find a cached partial DIE at OFFSET in CU. */
18493
d590ff25
YQ
18494struct partial_die_info *
18495dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18496{
18497 struct partial_die_info *lookup_die = NULL;
6f06d47b 18498 struct partial_die_info part_die (sect_off);
72bf9492 18499
9a3c8263 18500 lookup_die = ((struct partial_die_info *)
d590ff25 18501 htab_find_with_hash (partial_dies, &part_die,
9c541725 18502 to_underlying (sect_off)));
72bf9492 18503
72bf9492
DJ
18504 return lookup_die;
18505}
18506
348e048f
DE
18507/* Find a partial DIE at OFFSET, which may or may not be in CU,
18508 except in the case of .debug_types DIEs which do not reference
18509 outside their CU (they do however referencing other types via
55f1336d 18510 DW_FORM_ref_sig8). */
72bf9492 18511
122cf0f2 18512static const struct cu_partial_die_info
9c541725 18513find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18514{
518817b3
SM
18515 struct dwarf2_per_objfile *dwarf2_per_objfile
18516 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18517 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99
DJ
18518 struct dwarf2_per_cu_data *per_cu = NULL;
18519 struct partial_die_info *pd = NULL;
72bf9492 18520
36586728 18521 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18522 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18523 {
d590ff25 18524 pd = cu->find_partial_die (sect_off);
5afb4e99 18525 if (pd != NULL)
fb816e8b 18526 return { cu, pd };
0d99eb77
DE
18527 /* We missed recording what we needed.
18528 Load all dies and try again. */
18529 per_cu = cu->per_cu;
5afb4e99 18530 }
0d99eb77
DE
18531 else
18532 {
18533 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18534 if (cu->per_cu->is_debug_types)
0d99eb77 18535 {
9d8780f0
SM
18536 error (_("Dwarf Error: Type Unit at offset %s contains"
18537 " external reference to offset %s [in module %s].\n"),
18538 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18539 bfd_get_filename (objfile->obfd));
18540 }
9c541725 18541 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 18542 dwarf2_per_objfile);
72bf9492 18543
0d99eb77
DE
18544 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
18545 load_partial_comp_unit (per_cu);
ae038cb0 18546
0d99eb77 18547 per_cu->cu->last_used = 0;
d590ff25 18548 pd = per_cu->cu->find_partial_die (sect_off);
0d99eb77 18549 }
5afb4e99 18550
dee91e82
DE
18551 /* If we didn't find it, and not all dies have been loaded,
18552 load them all and try again. */
18553
5afb4e99
DJ
18554 if (pd == NULL && per_cu->load_all_dies == 0)
18555 {
5afb4e99 18556 per_cu->load_all_dies = 1;
fd820528
DE
18557
18558 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18559 THIS_CU->cu may already be in use. So we can't just free it and
18560 replace its DIEs with the ones we read in. Instead, we leave those
18561 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18562 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18563 set. */
dee91e82 18564 load_partial_comp_unit (per_cu);
5afb4e99 18565
d590ff25 18566 pd = per_cu->cu->find_partial_die (sect_off);
5afb4e99
DJ
18567 }
18568
18569 if (pd == NULL)
18570 internal_error (__FILE__, __LINE__,
9d8780f0 18571 _("could not find partial DIE %s "
3e43a32a 18572 "in cache [from module %s]\n"),
9d8780f0 18573 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
fb816e8b 18574 return { per_cu->cu, pd };
72bf9492
DJ
18575}
18576
abc72ce4
DE
18577/* See if we can figure out if the class lives in a namespace. We do
18578 this by looking for a member function; its demangled name will
18579 contain namespace info, if there is any. */
18580
18581static void
18582guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18583 struct dwarf2_cu *cu)
18584{
18585 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18586 what template types look like, because the demangler
18587 frequently doesn't give the same name as the debug info. We
18588 could fix this by only using the demangled name to get the
18589 prefix (but see comment in read_structure_type). */
18590
18591 struct partial_die_info *real_pdi;
18592 struct partial_die_info *child_pdi;
18593
18594 /* If this DIE (this DIE's specification, if any) has a parent, then
18595 we should not do this. We'll prepend the parent's fully qualified
18596 name when we create the partial symbol. */
18597
18598 real_pdi = struct_pdi;
18599 while (real_pdi->has_specification)
fb816e8b 18600 {
122cf0f2
AB
18601 auto res = find_partial_die (real_pdi->spec_offset,
18602 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18603 real_pdi = res.pdi;
18604 cu = res.cu;
18605 }
abc72ce4
DE
18606
18607 if (real_pdi->die_parent != NULL)
18608 return;
18609
18610 for (child_pdi = struct_pdi->die_child;
18611 child_pdi != NULL;
18612 child_pdi = child_pdi->die_sibling)
18613 {
18614 if (child_pdi->tag == DW_TAG_subprogram
18615 && child_pdi->linkage_name != NULL)
18616 {
43816ebc
TT
18617 gdb::unique_xmalloc_ptr<char> actual_class_name
18618 (language_class_name_from_physname (cu->language_defn,
18619 child_pdi->linkage_name));
abc72ce4
DE
18620 if (actual_class_name != NULL)
18621 {
518817b3 18622 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18623 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18624 }
18625 break;
18626 }
18627 }
18628}
18629
25c11aca
TV
18630/* Return true if a DIE with TAG may have the DW_AT_const_value
18631 attribute. */
18632
18633static bool
18634can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18635{
18636 switch (tag)
18637 {
18638 case DW_TAG_constant:
18639 case DW_TAG_enumerator:
18640 case DW_TAG_formal_parameter:
18641 case DW_TAG_template_value_param:
18642 case DW_TAG_variable:
18643 return true;
18644 }
18645
18646 return false;
18647}
18648
52356b79
YQ
18649void
18650partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18651{
abc72ce4
DE
18652 /* Once we've fixed up a die, there's no point in doing so again.
18653 This also avoids a memory leak if we were to call
18654 guess_partial_die_structure_name multiple times. */
52356b79 18655 if (fixup_called)
abc72ce4
DE
18656 return;
18657
72bf9492
DJ
18658 /* If we found a reference attribute and the DIE has no name, try
18659 to find a name in the referred to DIE. */
18660
52356b79 18661 if (name == NULL && has_specification)
72bf9492
DJ
18662 {
18663 struct partial_die_info *spec_die;
72bf9492 18664
122cf0f2 18665 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18666 spec_die = res.pdi;
18667 cu = res.cu;
72bf9492 18668
52356b79 18669 spec_die->fixup (cu);
72bf9492
DJ
18670
18671 if (spec_die->name)
18672 {
52356b79 18673 name = spec_die->name;
72bf9492
DJ
18674
18675 /* Copy DW_AT_external attribute if it is set. */
18676 if (spec_die->is_external)
52356b79 18677 is_external = spec_die->is_external;
72bf9492
DJ
18678 }
18679 }
18680
25c11aca
TV
18681 if (!has_const_value && has_specification
18682 && can_have_DW_AT_const_value_p (tag))
18683 {
18684 struct partial_die_info *spec_die;
18685
18686 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18687 spec_die = res.pdi;
18688 cu = res.cu;
18689
18690 spec_die->fixup (cu);
18691
18692 if (spec_die->has_const_value)
18693 {
18694 /* Copy DW_AT_const_value attribute if it is set. */
18695 has_const_value = spec_die->has_const_value;
18696 }
18697 }
18698
72bf9492 18699 /* Set default names for some unnamed DIEs. */
72bf9492 18700
52356b79
YQ
18701 if (name == NULL && tag == DW_TAG_namespace)
18702 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18703
abc72ce4
DE
18704 /* If there is no parent die to provide a namespace, and there are
18705 children, see if we can determine the namespace from their linkage
122d1940 18706 name. */
abc72ce4 18707 if (cu->language == language_cplus
fd5866f6 18708 && !cu->per_cu->dwarf2_per_objfile->types.empty ()
52356b79
YQ
18709 && die_parent == NULL
18710 && has_children
18711 && (tag == DW_TAG_class_type
18712 || tag == DW_TAG_structure_type
18713 || tag == DW_TAG_union_type))
18714 guess_partial_die_structure_name (this, cu);
abc72ce4 18715
53832f31
TT
18716 /* GCC might emit a nameless struct or union that has a linkage
18717 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18718 if (name == NULL
18719 && (tag == DW_TAG_class_type
18720 || tag == DW_TAG_interface_type
18721 || tag == DW_TAG_structure_type
18722 || tag == DW_TAG_union_type)
18723 && linkage_name != NULL)
53832f31 18724 {
43816ebc
TT
18725 gdb::unique_xmalloc_ptr<char> demangled
18726 (gdb_demangle (linkage_name, DMGL_TYPES));
18727 if (demangled != nullptr)
53832f31 18728 {
96408a79
SA
18729 const char *base;
18730
18731 /* Strip any leading namespaces/classes, keep only the base name.
18732 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18733 base = strrchr (demangled.get (), ':');
18734 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18735 base++;
18736 else
43816ebc 18737 base = demangled.get ();
96408a79 18738
518817b3 18739 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
be1e3d3e 18740 name = objfile->intern (base);
53832f31
TT
18741 }
18742 }
18743
52356b79 18744 fixup_called = 1;
72bf9492
DJ
18745}
18746
41144253 18747/* Read the .debug_loclists header contents from the given SECTION in the
18748 HEADER. */
18749static void
18750read_loclist_header (struct loclist_header *header,
18751 struct dwarf2_section_info *section)
18752{
18753 unsigned int bytes_read;
18754 bfd *abfd = section->get_bfd_owner ();
18755 const gdb_byte *info_ptr = section->buffer;
18756 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18757 info_ptr += bytes_read;
18758 header->version = read_2_bytes (abfd, info_ptr);
18759 info_ptr += 2;
18760 header->addr_size = read_1_byte (abfd, info_ptr);
18761 info_ptr += 1;
18762 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18763 info_ptr += 1;
18764 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18765}
18766
18767/* Return the DW_AT_loclists_base value for the CU. */
18768static ULONGEST
18769lookup_loclist_base (struct dwarf2_cu *cu)
18770{
18771 /* For the .dwo unit, the loclist_base points to the first offset following
18772 the header. The header consists of the following entities-
18773 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18774 bit format)
18775 2. version (2 bytes)
18776 3. address size (1 byte)
18777 4. segment selector size (1 byte)
18778 5. offset entry count (4 bytes)
18779 These sizes are derived as per the DWARFv5 standard. */
18780 if (cu->dwo_unit != nullptr)
18781 {
18782 if (cu->header.initial_length_size == 4)
18783 return LOCLIST_HEADER_SIZE32;
18784 return LOCLIST_HEADER_SIZE64;
18785 }
18786 return cu->loclist_base;
18787}
18788
18789/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18790 array of offsets in the .debug_loclists section. */
18791static CORE_ADDR
18792read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18793{
18794 struct dwarf2_per_objfile *dwarf2_per_objfile
18795 = cu->per_cu->dwarf2_per_objfile;
18796 struct objfile *objfile = dwarf2_per_objfile->objfile;
18797 bfd *abfd = objfile->obfd;
18798 ULONGEST loclist_base = lookup_loclist_base (cu);
18799 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18800
18801 section->read (objfile);
18802 if (section->buffer == NULL)
18803 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18804 "section [in module %s]"), objfile_name (objfile));
18805 struct loclist_header header;
18806 read_loclist_header (&header, section);
18807 if (loclist_index >= header.offset_entry_count)
18808 complaint (_("DW_FORM_loclistx pointing outside of "
18809 ".debug_loclists offset array [in module %s]"),
18810 objfile_name (objfile));
18811 if (loclist_base + loclist_index * cu->header.offset_size
18812 >= section->size)
18813 complaint (_("DW_FORM_loclistx pointing outside of "
18814 ".debug_loclists section [in module %s]"),
18815 objfile_name (objfile));
18816 const gdb_byte *info_ptr
18817 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18818
18819 if (cu->header.offset_size == 4)
18820 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18821 else
18822 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18823}
18824
18a8505e
AT
18825/* Process the attributes that had to be skipped in the first round. These
18826 attributes are the ones that need str_offsets_base or addr_base attributes.
18827 They could not have been processed in the first round, because at the time
18828 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18829static void
18830read_attribute_reprocess (const struct die_reader_specs *reader,
18831 struct attribute *attr)
18a8505e
AT
18832{
18833 struct dwarf2_cu *cu = reader->cu;
18834 switch (attr->form)
18835 {
18836 case DW_FORM_addrx:
18837 case DW_FORM_GNU_addr_index:
18838 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18839 break;
41144253 18840 case DW_FORM_loclistx:
18841 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
18842 break;
18a8505e
AT
18843 case DW_FORM_strx:
18844 case DW_FORM_strx1:
18845 case DW_FORM_strx2:
18846 case DW_FORM_strx3:
18847 case DW_FORM_strx4:
18848 case DW_FORM_GNU_str_index:
18849 {
18850 unsigned int str_index = DW_UNSND (attr);
18851 if (reader->dwo_file != NULL)
18852 {
18853 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
18854 DW_STRING_IS_CANONICAL (attr) = 0;
18855 }
18856 else
18857 {
18858 DW_STRING (attr) = read_stub_str_index (cu, str_index);
18859 DW_STRING_IS_CANONICAL (attr) = 0;
18860 }
18861 break;
18862 }
18863 default:
18864 gdb_assert_not_reached (_("Unexpected DWARF form."));
18865 }
18866}
18867
a8329558 18868/* Read an attribute value described by an attribute form. */
c906108c 18869
d521ce57 18870static const gdb_byte *
dee91e82
DE
18871read_attribute_value (const struct die_reader_specs *reader,
18872 struct attribute *attr, unsigned form,
18a8505e
AT
18873 LONGEST implicit_const, const gdb_byte *info_ptr,
18874 bool *need_reprocess)
c906108c 18875{
dee91e82 18876 struct dwarf2_cu *cu = reader->cu;
518817b3
SM
18877 struct dwarf2_per_objfile *dwarf2_per_objfile
18878 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 18879 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 18880 bfd *abfd = reader->abfd;
e7c27a73 18881 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
18882 unsigned int bytes_read;
18883 struct dwarf_block *blk;
18a8505e 18884 *need_reprocess = false;
c906108c 18885
aead7601 18886 attr->form = (enum dwarf_form) form;
a8329558 18887 switch (form)
c906108c 18888 {
c906108c 18889 case DW_FORM_ref_addr:
ae411497 18890 if (cu->header.version == 2)
c8a7a66f
TT
18891 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
18892 &bytes_read);
ae411497 18893 else
8266302d
TT
18894 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
18895 &bytes_read);
ae411497
TT
18896 info_ptr += bytes_read;
18897 break;
36586728 18898 case DW_FORM_GNU_ref_alt:
8266302d 18899 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
18900 info_ptr += bytes_read;
18901 break;
ae411497 18902 case DW_FORM_addr:
08feed99
TT
18903 {
18904 struct gdbarch *gdbarch = objfile->arch ();
18905 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
18906 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
18907 info_ptr += bytes_read;
18908 }
c906108c
SS
18909 break;
18910 case DW_FORM_block2:
7b5a2f43 18911 blk = dwarf_alloc_block (cu);
c906108c
SS
18912 blk->size = read_2_bytes (abfd, info_ptr);
18913 info_ptr += 2;
18914 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18915 info_ptr += blk->size;
18916 DW_BLOCK (attr) = blk;
18917 break;
18918 case DW_FORM_block4:
7b5a2f43 18919 blk = dwarf_alloc_block (cu);
c906108c
SS
18920 blk->size = read_4_bytes (abfd, info_ptr);
18921 info_ptr += 4;
18922 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
18923 info_ptr += blk->size;
18924 DW_BLOCK (attr) = blk;
18925 break;
18926 case DW_FORM_data2:
18927 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
18928 info_ptr += 2;
18929 break;
18930 case DW_FORM_data4:
18931 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
18932 info_ptr += 4;
18933 break;
18934 case DW_FORM_data8:
18935 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
18936 info_ptr += 8;
18937 break;
0224619f
JK
18938 case DW_FORM_data16:
18939 blk = dwarf_alloc_block (cu);
18940 blk->size = 16;
18941 blk->data = read_n_bytes (abfd, info_ptr, 16);
18942 info_ptr += 16;
18943 DW_BLOCK (attr) = blk;
18944 break;
2dc7f7b3 18945 case DW_FORM_sec_offset:
8266302d 18946 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
18947 info_ptr += bytes_read;
18948 break;
41144253 18949 case DW_FORM_loclistx:
18950 {
18951 *need_reprocess = true;
18952 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18953 info_ptr += bytes_read;
18954 }
18955 break;
c906108c 18956 case DW_FORM_string:
9b1c24c8 18957 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 18958 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
18959 info_ptr += bytes_read;
18960 break;
4bdf3d34 18961 case DW_FORM_strp:
36586728
TT
18962 if (!cu->per_cu->is_dwz)
18963 {
ed2dc618
SM
18964 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
18965 abfd, info_ptr, cu_header,
36586728
TT
18966 &bytes_read);
18967 DW_STRING_IS_CANONICAL (attr) = 0;
18968 info_ptr += bytes_read;
18969 break;
18970 }
18971 /* FALLTHROUGH */
43988095
JK
18972 case DW_FORM_line_strp:
18973 if (!cu->per_cu->is_dwz)
18974 {
86c0bb4c
TT
18975 DW_STRING (attr)
18976 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
18977 &bytes_read);
43988095
JK
18978 DW_STRING_IS_CANONICAL (attr) = 0;
18979 info_ptr += bytes_read;
18980 break;
18981 }
18982 /* FALLTHROUGH */
36586728
TT
18983 case DW_FORM_GNU_strp_alt:
18984 {
ed2dc618 18985 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
8266302d
TT
18986 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
18987 &bytes_read);
36586728 18988
0314b390 18989 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
18990 DW_STRING_IS_CANONICAL (attr) = 0;
18991 info_ptr += bytes_read;
18992 }
4bdf3d34 18993 break;
2dc7f7b3 18994 case DW_FORM_exprloc:
c906108c 18995 case DW_FORM_block:
7b5a2f43 18996 blk = dwarf_alloc_block (cu);
c906108c
SS
18997 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18998 info_ptr += bytes_read;
18999 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19000 info_ptr += blk->size;
19001 DW_BLOCK (attr) = blk;
19002 break;
19003 case DW_FORM_block1:
7b5a2f43 19004 blk = dwarf_alloc_block (cu);
c906108c
SS
19005 blk->size = read_1_byte (abfd, info_ptr);
19006 info_ptr += 1;
19007 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19008 info_ptr += blk->size;
19009 DW_BLOCK (attr) = blk;
19010 break;
19011 case DW_FORM_data1:
19012 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19013 info_ptr += 1;
19014 break;
19015 case DW_FORM_flag:
19016 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19017 info_ptr += 1;
19018 break;
2dc7f7b3
TT
19019 case DW_FORM_flag_present:
19020 DW_UNSND (attr) = 1;
19021 break;
c906108c
SS
19022 case DW_FORM_sdata:
19023 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19024 info_ptr += bytes_read;
19025 break;
19026 case DW_FORM_udata:
18a8505e 19027 case DW_FORM_rnglistx:
c906108c
SS
19028 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19029 info_ptr += bytes_read;
19030 break;
19031 case DW_FORM_ref1:
9c541725 19032 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19033 + read_1_byte (abfd, info_ptr));
c906108c
SS
19034 info_ptr += 1;
19035 break;
19036 case DW_FORM_ref2:
9c541725 19037 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19038 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19039 info_ptr += 2;
19040 break;
19041 case DW_FORM_ref4:
9c541725 19042 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19043 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19044 info_ptr += 4;
19045 break;
613e1657 19046 case DW_FORM_ref8:
9c541725 19047 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19048 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19049 info_ptr += 8;
19050 break;
55f1336d 19051 case DW_FORM_ref_sig8:
ac9ec31b 19052 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19053 info_ptr += 8;
19054 break;
c906108c 19055 case DW_FORM_ref_udata:
9c541725 19056 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19057 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19058 info_ptr += bytes_read;
19059 break;
c906108c 19060 case DW_FORM_indirect:
a8329558
KW
19061 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19062 info_ptr += bytes_read;
43988095
JK
19063 if (form == DW_FORM_implicit_const)
19064 {
19065 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19066 info_ptr += bytes_read;
19067 }
19068 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19069 info_ptr, need_reprocess);
43988095
JK
19070 break;
19071 case DW_FORM_implicit_const:
19072 DW_SND (attr) = implicit_const;
a8329558 19073 break;
336d760d 19074 case DW_FORM_addrx:
3019eac3 19075 case DW_FORM_GNU_addr_index:
18a8505e
AT
19076 *need_reprocess = true;
19077 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19078 info_ptr += bytes_read;
19079 break;
cf532bd1 19080 case DW_FORM_strx:
15f18d14
AT
19081 case DW_FORM_strx1:
19082 case DW_FORM_strx2:
19083 case DW_FORM_strx3:
19084 case DW_FORM_strx4:
3019eac3 19085 case DW_FORM_GNU_str_index:
3019eac3 19086 {
15f18d14
AT
19087 ULONGEST str_index;
19088 if (form == DW_FORM_strx1)
19089 {
19090 str_index = read_1_byte (abfd, info_ptr);
19091 info_ptr += 1;
19092 }
19093 else if (form == DW_FORM_strx2)
19094 {
19095 str_index = read_2_bytes (abfd, info_ptr);
19096 info_ptr += 2;
19097 }
19098 else if (form == DW_FORM_strx3)
19099 {
19100 str_index = read_3_bytes (abfd, info_ptr);
19101 info_ptr += 3;
19102 }
19103 else if (form == DW_FORM_strx4)
19104 {
19105 str_index = read_4_bytes (abfd, info_ptr);
19106 info_ptr += 4;
19107 }
19108 else
19109 {
19110 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19111 info_ptr += bytes_read;
19112 }
18a8505e
AT
19113 *need_reprocess = true;
19114 DW_UNSND (attr) = str_index;
19115 }
3019eac3 19116 break;
c906108c 19117 default:
8a3fe4f8 19118 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19119 dwarf_form_name (form),
19120 bfd_get_filename (abfd));
c906108c 19121 }
28e94949 19122
36586728 19123 /* Super hack. */
cd6c91b4 19124 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19125 attr->form = DW_FORM_GNU_ref_alt;
19126
28e94949
JB
19127 /* We have seen instances where the compiler tried to emit a byte
19128 size attribute of -1 which ended up being encoded as an unsigned
19129 0xffffffff. Although 0xffffffff is technically a valid size value,
19130 an object of this size seems pretty unlikely so we can relatively
19131 safely treat these cases as if the size attribute was invalid and
19132 treat them as zero by default. */
19133 if (attr->name == DW_AT_byte_size
19134 && form == DW_FORM_data4
19135 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19136 {
19137 complaint
b98664d3 19138 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19139 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19140 DW_UNSND (attr) = 0;
19141 }
28e94949 19142
c906108c
SS
19143 return info_ptr;
19144}
19145
a8329558
KW
19146/* Read an attribute described by an abbreviated attribute. */
19147
d521ce57 19148static const gdb_byte *
dee91e82
DE
19149read_attribute (const struct die_reader_specs *reader,
19150 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19151 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19152{
19153 attr->name = abbrev->name;
43988095 19154 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19155 abbrev->implicit_const, info_ptr,
19156 need_reprocess);
a8329558
KW
19157}
19158
43988095
JK
19159/* Return pointer to string at .debug_str offset STR_OFFSET. */
19160
19161static const char *
ed2dc618 19162read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19163 LONGEST str_offset)
43988095 19164{
4f44ae6c
TT
19165 return dwarf2_per_objfile->str.read_string (dwarf2_per_objfile->objfile,
19166 str_offset, "DW_FORM_strp");
c906108c
SS
19167}
19168
43988095
JK
19169/* Return pointer to string at .debug_str offset as read from BUF.
19170 BUF is assumed to be in a compilation unit described by CU_HEADER.
19171 Return *BYTES_READ_PTR count of bytes read from BUF. */
19172
d521ce57 19173static const char *
ed2dc618
SM
19174read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19175 const gdb_byte *buf,
cf2c3c16
TT
19176 const struct comp_unit_head *cu_header,
19177 unsigned int *bytes_read_ptr)
19178{
8266302d 19179 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19180
4f44ae6c 19181 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19182}
19183
86c0bb4c 19184/* See read.h. */
43988095 19185
86c0bb4c
TT
19186const char *
19187dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
43988095
JK
19188 const struct comp_unit_head *cu_header,
19189 unsigned int *bytes_read_ptr)
19190{
86c0bb4c 19191 bfd *abfd = objfile->obfd;
8266302d 19192 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19193
86c0bb4c 19194 return line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19195}
19196
3019eac3 19197/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19198 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19199 ADDR_SIZE is the size of addresses from the CU header. */
19200
19201static CORE_ADDR
ed2dc618 19202read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19203 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19204 int addr_size)
3019eac3
DE
19205{
19206 struct objfile *objfile = dwarf2_per_objfile->objfile;
19207 bfd *abfd = objfile->obfd;
19208 const gdb_byte *info_ptr;
18a8505e 19209 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19210
96b79293 19211 dwarf2_per_objfile->addr.read (objfile);
3019eac3
DE
19212 if (dwarf2_per_objfile->addr.buffer == NULL)
19213 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19214 objfile_name (objfile));
18a8505e
AT
19215 if (addr_base_or_zero + addr_index * addr_size
19216 >= dwarf2_per_objfile->addr.size)
3019eac3
DE
19217 error (_("DW_FORM_addr_index pointing outside of "
19218 ".debug_addr section [in module %s]"),
4262abfb 19219 objfile_name (objfile));
3019eac3 19220 info_ptr = (dwarf2_per_objfile->addr.buffer
18a8505e 19221 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19222 if (addr_size == 4)
19223 return bfd_get_32 (abfd, info_ptr);
19224 else
19225 return bfd_get_64 (abfd, info_ptr);
19226}
19227
19228/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19229
19230static CORE_ADDR
19231read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19232{
518817b3
SM
19233 return read_addr_index_1 (cu->per_cu->dwarf2_per_objfile, addr_index,
19234 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19235}
19236
19237/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19238
19239static CORE_ADDR
d521ce57 19240read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19241 unsigned int *bytes_read)
19242{
518817b3 19243 bfd *abfd = cu->per_cu->dwarf2_per_objfile->objfile->obfd;
3019eac3
DE
19244 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19245
19246 return read_addr_index (cu, addr_index);
19247}
19248
450a1bfc 19249/* See read.h. */
3019eac3
DE
19250
19251CORE_ADDR
450a1bfc 19252dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu, unsigned int addr_index)
3019eac3 19253{
ed2dc618 19254 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
3019eac3 19255 struct dwarf2_cu *cu = per_cu->cu;
18a8505e 19256 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19257 int addr_size;
19258
3019eac3
DE
19259 /* We need addr_base and addr_size.
19260 If we don't have PER_CU->cu, we have to get it.
19261 Nasty, but the alternative is storing the needed info in PER_CU,
19262 which at this point doesn't seem justified: it's not clear how frequently
19263 it would get used and it would increase the size of every PER_CU.
19264 Entry points like dwarf2_per_cu_addr_size do a similar thing
19265 so we're not in uncharted territory here.
19266 Alas we need to be a bit more complicated as addr_base is contained
19267 in the DIE.
19268
19269 We don't need to read the entire CU(/TU).
19270 We just need the header and top level die.
a1b64ce1 19271
3019eac3 19272 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19273 For now we skip this optimization. */
3019eac3
DE
19274
19275 if (cu != NULL)
19276 {
19277 addr_base = cu->addr_base;
19278 addr_size = cu->header.addr_size;
19279 }
19280 else
19281 {
6751ebae 19282 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
19283 addr_base = reader.cu->addr_base;
19284 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19285 }
19286
ed2dc618
SM
19287 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19288 addr_size);
3019eac3
DE
19289}
19290
18a8505e
AT
19291/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19292 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19293 DWO file. */
3019eac3 19294
d521ce57 19295static const char *
18a8505e
AT
19296read_str_index (struct dwarf2_cu *cu,
19297 struct dwarf2_section_info *str_section,
19298 struct dwarf2_section_info *str_offsets_section,
19299 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19300{
518817b3
SM
19301 struct dwarf2_per_objfile *dwarf2_per_objfile
19302 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19303 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19304 const char *objf_name = objfile_name (objfile);
3019eac3 19305 bfd *abfd = objfile->obfd;
d521ce57 19306 const gdb_byte *info_ptr;
3019eac3 19307 ULONGEST str_offset;
cf532bd1 19308 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19309
96b79293
TT
19310 str_section->read (objfile);
19311 str_offsets_section->read (objfile);
73869dc2 19312 if (str_section->buffer == NULL)
18a8505e 19313 error (_("%s used without %s section"
9d8780f0 19314 " in CU at offset %s [in module %s]"),
96b79293 19315 form_name, str_section->get_name (),
18a8505e 19316 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19317 if (str_offsets_section->buffer == NULL)
18a8505e 19318 error (_("%s used without %s section"
9d8780f0 19319 " in CU at offset %s [in module %s]"),
96b79293 19320 form_name, str_section->get_name (),
18a8505e 19321 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19322 info_ptr = (str_offsets_section->buffer
18a8505e 19323 + str_offsets_base
3019eac3
DE
19324 + str_index * cu->header.offset_size);
19325 if (cu->header.offset_size == 4)
19326 str_offset = bfd_get_32 (abfd, info_ptr);
19327 else
19328 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19329 if (str_offset >= str_section->size)
57d63ce2 19330 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19331 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19332 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19333 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19334}
19335
18a8505e
AT
19336/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19337
19338static const char *
19339read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19340{
19341 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19342 ? reader->cu->header.addr_size : 0;
19343 return read_str_index (reader->cu,
19344 &reader->dwo_file->sections.str,
19345 &reader->dwo_file->sections.str_offsets,
19346 str_offsets_base, str_index);
19347}
19348
19349/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19350
19351static const char *
19352read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19353{
19354 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
19355 const char *objf_name = objfile_name (objfile);
19356 static const char form_name[] = "DW_FORM_GNU_str_index";
19357 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19358
19359 if (!cu->str_offsets_base.has_value ())
19360 error (_("%s used in Fission stub without %s"
19361 " in CU at offset 0x%lx [in module %s]"),
19362 form_name, str_offsets_attr_name,
19363 (long) cu->header.offset_size, objf_name);
19364
19365 return read_str_index (cu,
19366 &cu->per_cu->dwarf2_per_objfile->str,
19367 &cu->per_cu->dwarf2_per_objfile->str_offsets,
19368 *cu->str_offsets_base, str_index);
19369}
19370
3019eac3
DE
19371/* Return the length of an LEB128 number in BUF. */
19372
19373static int
19374leb128_size (const gdb_byte *buf)
19375{
19376 const gdb_byte *begin = buf;
19377 gdb_byte byte;
19378
19379 while (1)
19380 {
19381 byte = *buf++;
19382 if ((byte & 128) == 0)
19383 return buf - begin;
19384 }
19385}
19386
c906108c 19387static void
e142c38c 19388set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19389{
19390 switch (lang)
19391 {
19392 case DW_LANG_C89:
76bee0cc 19393 case DW_LANG_C99:
0cfd832f 19394 case DW_LANG_C11:
c906108c 19395 case DW_LANG_C:
d1be3247 19396 case DW_LANG_UPC:
e142c38c 19397 cu->language = language_c;
c906108c 19398 break;
9c37b5ae 19399 case DW_LANG_Java:
c906108c 19400 case DW_LANG_C_plus_plus:
0cfd832f
MW
19401 case DW_LANG_C_plus_plus_11:
19402 case DW_LANG_C_plus_plus_14:
e142c38c 19403 cu->language = language_cplus;
c906108c 19404 break;
6aecb9c2
JB
19405 case DW_LANG_D:
19406 cu->language = language_d;
19407 break;
c906108c
SS
19408 case DW_LANG_Fortran77:
19409 case DW_LANG_Fortran90:
b21b22e0 19410 case DW_LANG_Fortran95:
f7de9aab
MW
19411 case DW_LANG_Fortran03:
19412 case DW_LANG_Fortran08:
e142c38c 19413 cu->language = language_fortran;
c906108c 19414 break;
a766d390
DE
19415 case DW_LANG_Go:
19416 cu->language = language_go;
19417 break;
c906108c 19418 case DW_LANG_Mips_Assembler:
e142c38c 19419 cu->language = language_asm;
c906108c
SS
19420 break;
19421 case DW_LANG_Ada83:
8aaf0b47 19422 case DW_LANG_Ada95:
bc5f45f8
JB
19423 cu->language = language_ada;
19424 break;
72019c9c
GM
19425 case DW_LANG_Modula2:
19426 cu->language = language_m2;
19427 break;
fe8e67fd
PM
19428 case DW_LANG_Pascal83:
19429 cu->language = language_pascal;
19430 break;
22566fbd
DJ
19431 case DW_LANG_ObjC:
19432 cu->language = language_objc;
19433 break;
c44af4eb
TT
19434 case DW_LANG_Rust:
19435 case DW_LANG_Rust_old:
19436 cu->language = language_rust;
19437 break;
c906108c
SS
19438 case DW_LANG_Cobol74:
19439 case DW_LANG_Cobol85:
c906108c 19440 default:
e142c38c 19441 cu->language = language_minimal;
c906108c
SS
19442 break;
19443 }
e142c38c 19444 cu->language_defn = language_def (cu->language);
c906108c
SS
19445}
19446
19447/* Return the named attribute or NULL if not there. */
19448
19449static struct attribute *
e142c38c 19450dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19451{
a48e046c 19452 for (;;)
c906108c 19453 {
a48e046c
TT
19454 unsigned int i;
19455 struct attribute *spec = NULL;
19456
19457 for (i = 0; i < die->num_attrs; ++i)
19458 {
19459 if (die->attrs[i].name == name)
19460 return &die->attrs[i];
19461 if (die->attrs[i].name == DW_AT_specification
19462 || die->attrs[i].name == DW_AT_abstract_origin)
19463 spec = &die->attrs[i];
19464 }
19465
19466 if (!spec)
19467 break;
c906108c 19468
f2f0e013 19469 die = follow_die_ref (die, spec, &cu);
f2f0e013 19470 }
c5aa993b 19471
c906108c
SS
19472 return NULL;
19473}
19474
7d45c7c3
KB
19475/* Return the string associated with a string-typed attribute, or NULL if it
19476 is either not found or is of an incorrect type. */
19477
19478static const char *
19479dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19480{
19481 struct attribute *attr;
19482 const char *str = NULL;
19483
19484 attr = dwarf2_attr (die, name, cu);
19485
19486 if (attr != NULL)
19487 {
43988095 19488 if (attr->form == DW_FORM_strp || attr->form == DW_FORM_line_strp
b3340438 19489 || attr->form == DW_FORM_string
cf532bd1 19490 || attr->form == DW_FORM_strx
8fe0f950
AT
19491 || attr->form == DW_FORM_strx1
19492 || attr->form == DW_FORM_strx2
19493 || attr->form == DW_FORM_strx3
19494 || attr->form == DW_FORM_strx4
b3340438 19495 || attr->form == DW_FORM_GNU_str_index
16eb6b2d 19496 || attr->form == DW_FORM_GNU_strp_alt)
7d45c7c3
KB
19497 str = DW_STRING (attr);
19498 else
b98664d3 19499 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19500 "DIE at %s in module %s"),
19501 dwarf_attr_name (name), sect_offset_str (die->sect_off),
518817b3 19502 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
7d45c7c3
KB
19503 }
19504
19505 return str;
19506}
19507
a084a2a6 19508/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19509 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19510static const char *
19511dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19512{
19513 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19514 if (dwo_name == nullptr)
19515 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19516 return dwo_name;
19517}
19518
05cf31d1
JB
19519/* Return non-zero iff the attribute NAME is defined for the given DIE,
19520 and holds a non-zero value. This function should only be used for
2dc7f7b3 19521 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19522
19523static int
19524dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19525{
19526 struct attribute *attr = dwarf2_attr (die, name, cu);
19527
19528 return (attr && DW_UNSND (attr));
19529}
19530
3ca72b44 19531static int
e142c38c 19532die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19533{
05cf31d1
JB
19534 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19535 which value is non-zero. However, we have to be careful with
19536 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19537 (via dwarf2_flag_true_p) follows this attribute. So we may
19538 end up accidently finding a declaration attribute that belongs
19539 to a different DIE referenced by the specification attribute,
19540 even though the given DIE does not have a declaration attribute. */
19541 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19542 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19543}
19544
63d06c5c 19545/* Return the die giving the specification for DIE, if there is
f2f0e013 19546 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19547 containing the return value on output. If there is no
19548 specification, but there is an abstract origin, that is
19549 returned. */
63d06c5c
DC
19550
19551static struct die_info *
f2f0e013 19552die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19553{
f2f0e013
DJ
19554 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19555 *spec_cu);
63d06c5c 19556
edb3359d
DJ
19557 if (spec_attr == NULL)
19558 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19559
63d06c5c
DC
19560 if (spec_attr == NULL)
19561 return NULL;
19562 else
f2f0e013 19563 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19564}
c906108c 19565
527f3840
JK
19566/* Stub for free_line_header to match void * callback types. */
19567
19568static void
19569free_line_header_voidp (void *arg)
19570{
9a3c8263 19571 struct line_header *lh = (struct line_header *) arg;
527f3840 19572
fff8551c 19573 delete lh;
527f3840
JK
19574}
19575
83769d0b 19576/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19577
19578static struct dwarf2_section_info *
19579get_debug_line_section (struct dwarf2_cu *cu)
19580{
19581 struct dwarf2_section_info *section;
518817b3
SM
19582 struct dwarf2_per_objfile *dwarf2_per_objfile
19583 = cu->per_cu->dwarf2_per_objfile;
36586728
TT
19584
19585 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19586 DWO file. */
19587 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19588 section = &cu->dwo_unit->dwo_file->sections.line;
19589 else if (cu->per_cu->is_dwz)
19590 {
ed2dc618 19591 struct dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile);
36586728
TT
19592
19593 section = &dwz->line;
19594 }
19595 else
19596 section = &dwarf2_per_objfile->line;
19597
19598 return section;
19599}
19600
debd256d 19601/* Read the statement program header starting at OFFSET in
3019eac3 19602 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19603 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19604 Returns NULL if there is a problem reading the header, e.g., if it
19605 has a version we don't understand.
debd256d
JB
19606
19607 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19608 the returned object point into the dwarf line section buffer,
19609 and must not be freed. */
ae2de4f8 19610
fff8551c 19611static line_header_up
9c541725 19612dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19613{
3019eac3 19614 struct dwarf2_section_info *section;
518817b3
SM
19615 struct dwarf2_per_objfile *dwarf2_per_objfile
19616 = cu->per_cu->dwarf2_per_objfile;
3019eac3 19617
36586728 19618 section = get_debug_line_section (cu);
96b79293 19619 section->read (dwarf2_per_objfile->objfile);
3019eac3 19620 if (section->buffer == NULL)
debd256d 19621 {
3019eac3 19622 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19623 complaint (_("missing .debug_line.dwo section"));
3019eac3 19624 else
b98664d3 19625 complaint (_("missing .debug_line section"));
debd256d
JB
19626 return 0;
19627 }
19628
0df7ad3a
TT
19629 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19630 dwarf2_per_objfile, section,
19631 &cu->header);
debd256d 19632}
c906108c 19633
c6da4cef 19634/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19635 Return the file name of the psymtab for the given file_entry.
c6da4cef 19636 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19637 If space for the result is malloc'd, *NAME_HOLDER will be set.
19638 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19639
d521ce57 19640static const char *
7ba99d21 19641psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19642 const dwarf2_psymtab *pst,
c89b44cd
TT
19643 const char *comp_dir,
19644 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19645{
d521ce57
TT
19646 const char *include_name = fe.name;
19647 const char *include_name_to_compare = include_name;
72b9f47f 19648 const char *pst_filename;
c6da4cef
DE
19649 int file_is_pst;
19650
8c43009f 19651 const char *dir_name = fe.include_dir (lh);
c6da4cef 19652
c89b44cd 19653 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19654 if (!IS_ABSOLUTE_PATH (include_name)
19655 && (dir_name != NULL || comp_dir != NULL))
19656 {
19657 /* Avoid creating a duplicate psymtab for PST.
19658 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19659 Before we do the comparison, however, we need to account
19660 for DIR_NAME and COMP_DIR.
19661 First prepend dir_name (if non-NULL). If we still don't
19662 have an absolute path prepend comp_dir (if non-NULL).
19663 However, the directory we record in the include-file's
19664 psymtab does not contain COMP_DIR (to match the
19665 corresponding symtab(s)).
19666
19667 Example:
19668
19669 bash$ cd /tmp
19670 bash$ gcc -g ./hello.c
19671 include_name = "hello.c"
19672 dir_name = "."
19673 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19674 DW_AT_name = "./hello.c"
19675
19676 */
c6da4cef
DE
19677
19678 if (dir_name != NULL)
19679 {
c89b44cd
TT
19680 name_holder->reset (concat (dir_name, SLASH_STRING,
19681 include_name, (char *) NULL));
19682 include_name = name_holder->get ();
c6da4cef 19683 include_name_to_compare = include_name;
c6da4cef
DE
19684 }
19685 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19686 {
c89b44cd
TT
19687 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19688 include_name, (char *) NULL));
19689 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19690 }
19691 }
19692
19693 pst_filename = pst->filename;
c89b44cd 19694 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19695 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19696 {
c89b44cd
TT
19697 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19698 pst_filename, (char *) NULL));
19699 pst_filename = copied_name.get ();
c6da4cef
DE
19700 }
19701
1e3fad37 19702 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19703
c6da4cef
DE
19704 if (file_is_pst)
19705 return NULL;
19706 return include_name;
19707}
19708
d9b3de22
DE
19709/* State machine to track the state of the line number program. */
19710
6f77053d 19711class lnp_state_machine
d9b3de22 19712{
6f77053d
PA
19713public:
19714 /* Initialize a machine state for the start of a line number
19715 program. */
804d2729
TT
19716 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19717 bool record_lines_p);
6f77053d 19718
8c43009f
PA
19719 file_entry *current_file ()
19720 {
19721 /* lh->file_names is 0-based, but the file name numbers in the
19722 statement program are 1-based. */
6f77053d
PA
19723 return m_line_header->file_name_at (m_file);
19724 }
19725
19726 /* Record the line in the state machine. END_SEQUENCE is true if
19727 we're processing the end of a sequence. */
19728 void record_line (bool end_sequence);
19729
7ab6656f
OJ
19730 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19731 nop-out rest of the lines in this sequence. */
6f77053d
PA
19732 void check_line_address (struct dwarf2_cu *cu,
19733 const gdb_byte *line_ptr,
7ab6656f 19734 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19735
19736 void handle_set_discriminator (unsigned int discriminator)
19737 {
19738 m_discriminator = discriminator;
19739 m_line_has_non_zero_discriminator |= discriminator != 0;
19740 }
19741
19742 /* Handle DW_LNE_set_address. */
19743 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19744 {
19745 m_op_index = 0;
19746 address += baseaddr;
19747 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19748 }
19749
19750 /* Handle DW_LNS_advance_pc. */
19751 void handle_advance_pc (CORE_ADDR adjust);
19752
19753 /* Handle a special opcode. */
19754 void handle_special_opcode (unsigned char op_code);
19755
19756 /* Handle DW_LNS_advance_line. */
19757 void handle_advance_line (int line_delta)
19758 {
19759 advance_line (line_delta);
19760 }
19761
19762 /* Handle DW_LNS_set_file. */
19763 void handle_set_file (file_name_index file);
19764
19765 /* Handle DW_LNS_negate_stmt. */
19766 void handle_negate_stmt ()
19767 {
19768 m_is_stmt = !m_is_stmt;
19769 }
19770
19771 /* Handle DW_LNS_const_add_pc. */
19772 void handle_const_add_pc ();
19773
19774 /* Handle DW_LNS_fixed_advance_pc. */
19775 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19776 {
19777 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19778 m_op_index = 0;
19779 }
19780
19781 /* Handle DW_LNS_copy. */
19782 void handle_copy ()
19783 {
19784 record_line (false);
19785 m_discriminator = 0;
19786 }
19787
19788 /* Handle DW_LNE_end_sequence. */
19789 void handle_end_sequence ()
19790 {
804d2729 19791 m_currently_recording_lines = true;
6f77053d
PA
19792 }
19793
19794private:
19795 /* Advance the line by LINE_DELTA. */
19796 void advance_line (int line_delta)
19797 {
19798 m_line += line_delta;
19799
19800 if (line_delta != 0)
19801 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19802 }
19803
804d2729
TT
19804 struct dwarf2_cu *m_cu;
19805
6f77053d
PA
19806 gdbarch *m_gdbarch;
19807
19808 /* True if we're recording lines.
19809 Otherwise we're building partial symtabs and are just interested in
19810 finding include files mentioned by the line number program. */
19811 bool m_record_lines_p;
19812
8c43009f 19813 /* The line number header. */
6f77053d 19814 line_header *m_line_header;
8c43009f 19815
6f77053d
PA
19816 /* These are part of the standard DWARF line number state machine,
19817 and initialized according to the DWARF spec. */
d9b3de22 19818
6f77053d 19819 unsigned char m_op_index = 0;
7ba99d21
AT
19820 /* The line table index of the current file. */
19821 file_name_index m_file = 1;
6f77053d
PA
19822 unsigned int m_line = 1;
19823
19824 /* These are initialized in the constructor. */
19825
19826 CORE_ADDR m_address;
19827 bool m_is_stmt;
19828 unsigned int m_discriminator;
d9b3de22
DE
19829
19830 /* Additional bits of state we need to track. */
19831
19832 /* The last file that we called dwarf2_start_subfile for.
19833 This is only used for TLLs. */
6f77053d 19834 unsigned int m_last_file = 0;
d9b3de22 19835 /* The last file a line number was recorded for. */
6f77053d 19836 struct subfile *m_last_subfile = NULL;
d9b3de22 19837
804d2729
TT
19838 /* When true, record the lines we decode. */
19839 bool m_currently_recording_lines = false;
d9b3de22
DE
19840
19841 /* The last line number that was recorded, used to coalesce
19842 consecutive entries for the same line. This can happen, for
19843 example, when discriminators are present. PR 17276. */
6f77053d
PA
19844 unsigned int m_last_line = 0;
19845 bool m_line_has_non_zero_discriminator = false;
8c43009f 19846};
d9b3de22 19847
6f77053d
PA
19848void
19849lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19850{
19851 CORE_ADDR addr_adj = (((m_op_index + adjust)
19852 / m_line_header->maximum_ops_per_instruction)
19853 * m_line_header->minimum_instruction_length);
19854 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19855 m_op_index = ((m_op_index + adjust)
19856 % m_line_header->maximum_ops_per_instruction);
19857}
d9b3de22 19858
6f77053d
PA
19859void
19860lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 19861{
6f77053d 19862 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
19863 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
19864 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
19865 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
19866 / m_line_header->maximum_ops_per_instruction)
19867 * m_line_header->minimum_instruction_length);
19868 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 19869 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 19870 % m_line_header->maximum_ops_per_instruction);
d9b3de22 19871
258bf0ee 19872 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
19873 advance_line (line_delta);
19874 record_line (false);
19875 m_discriminator = 0;
19876}
d9b3de22 19877
6f77053d
PA
19878void
19879lnp_state_machine::handle_set_file (file_name_index file)
19880{
19881 m_file = file;
19882
19883 const file_entry *fe = current_file ();
19884 if (fe == NULL)
19885 dwarf2_debug_line_missing_file_complaint ();
19886 else if (m_record_lines_p)
19887 {
19888 const char *dir = fe->include_dir (m_line_header);
19889
c24bdb02 19890 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 19891 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 19892 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
19893 }
19894}
19895
19896void
19897lnp_state_machine::handle_const_add_pc ()
19898{
19899 CORE_ADDR adjust
19900 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
19901
19902 CORE_ADDR addr_adj
19903 = (((m_op_index + adjust)
19904 / m_line_header->maximum_ops_per_instruction)
19905 * m_line_header->minimum_instruction_length);
19906
19907 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19908 m_op_index = ((m_op_index + adjust)
19909 % m_line_header->maximum_ops_per_instruction);
19910}
d9b3de22 19911
a05a36a5
DE
19912/* Return non-zero if we should add LINE to the line number table.
19913 LINE is the line to add, LAST_LINE is the last line that was added,
19914 LAST_SUBFILE is the subfile for LAST_LINE.
19915 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
19916 had a non-zero discriminator.
19917
19918 We have to be careful in the presence of discriminators.
19919 E.g., for this line:
19920
19921 for (i = 0; i < 100000; i++);
19922
19923 clang can emit four line number entries for that one line,
19924 each with a different discriminator.
19925 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
19926
19927 However, we want gdb to coalesce all four entries into one.
19928 Otherwise the user could stepi into the middle of the line and
19929 gdb would get confused about whether the pc really was in the
19930 middle of the line.
19931
19932 Things are further complicated by the fact that two consecutive
19933 line number entries for the same line is a heuristic used by gcc
19934 to denote the end of the prologue. So we can't just discard duplicate
19935 entries, we have to be selective about it. The heuristic we use is
19936 that we only collapse consecutive entries for the same line if at least
19937 one of those entries has a non-zero discriminator. PR 17276.
19938
19939 Note: Addresses in the line number state machine can never go backwards
19940 within one sequence, thus this coalescing is ok. */
19941
19942static int
804d2729
TT
19943dwarf_record_line_p (struct dwarf2_cu *cu,
19944 unsigned int line, unsigned int last_line,
a05a36a5
DE
19945 int line_has_non_zero_discriminator,
19946 struct subfile *last_subfile)
19947{
c24bdb02 19948 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
19949 return 1;
19950 if (line != last_line)
19951 return 1;
19952 /* Same line for the same file that we've seen already.
19953 As a last check, for pr 17276, only record the line if the line
19954 has never had a non-zero discriminator. */
19955 if (!line_has_non_zero_discriminator)
19956 return 1;
19957 return 0;
19958}
19959
804d2729
TT
19960/* Use the CU's builder to record line number LINE beginning at
19961 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
19962
19963static void
d9b3de22 19964dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 19965 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 19966 struct dwarf2_cu *cu)
252a6764
DE
19967{
19968 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
19969
27e0867f
DE
19970 if (dwarf_line_debug)
19971 {
19972 fprintf_unfiltered (gdb_stdlog,
19973 "Recording line %u, file %s, address %s\n",
19974 line, lbasename (subfile->name),
19975 paddress (gdbarch, address));
19976 }
19977
804d2729 19978 if (cu != nullptr)
8c95582d 19979 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
19980}
19981
19982/* Subroutine of dwarf_decode_lines_1 to simplify it.
19983 Mark the end of a set of line number records.
d9b3de22 19984 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
19985 If SUBFILE is NULL the request is ignored. */
19986
19987static void
19988dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 19989 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 19990{
27e0867f
DE
19991 if (subfile == NULL)
19992 return;
19993
19994 if (dwarf_line_debug)
19995 {
19996 fprintf_unfiltered (gdb_stdlog,
19997 "Finishing current line, file %s, address %s\n",
19998 lbasename (subfile->name),
19999 paddress (gdbarch, address));
20000 }
20001
8c95582d 20002 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20003}
20004
6f77053d
PA
20005void
20006lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20007{
d9b3de22
DE
20008 if (dwarf_line_debug)
20009 {
20010 fprintf_unfiltered (gdb_stdlog,
20011 "Processing actual line %u: file %u,"
94a72be7 20012 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20013 m_line, m_file,
6f77053d 20014 paddress (m_gdbarch, m_address),
94a72be7
AB
20015 m_is_stmt, m_discriminator,
20016 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20017 }
20018
6f77053d 20019 file_entry *fe = current_file ();
8c43009f
PA
20020
20021 if (fe == NULL)
d9b3de22
DE
20022 dwarf2_debug_line_missing_file_complaint ();
20023 /* For now we ignore lines not starting on an instruction boundary.
20024 But not when processing end_sequence for compatibility with the
20025 previous version of the code. */
6f77053d 20026 else if (m_op_index == 0 || end_sequence)
d9b3de22 20027 {
8c43009f 20028 fe->included_p = 1;
8c95582d 20029 if (m_record_lines_p)
d9b3de22 20030 {
c24bdb02 20031 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20032 || end_sequence)
d9b3de22 20033 {
804d2729
TT
20034 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20035 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20036 }
20037
20038 if (!end_sequence)
20039 {
8c95582d
AB
20040 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20041
804d2729 20042 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20043 m_line_has_non_zero_discriminator,
20044 m_last_subfile))
d9b3de22 20045 {
c24bdb02 20046 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20047 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20048 builder->get_current_subfile (),
8c95582d 20049 m_line, m_address, is_stmt,
804d2729 20050 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20051 }
c24bdb02 20052 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20053 m_last_line = m_line;
d9b3de22
DE
20054 }
20055 }
20056 }
20057}
20058
804d2729
TT
20059lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20060 line_header *lh, bool record_lines_p)
d9b3de22 20061{
804d2729 20062 m_cu = cu;
6f77053d
PA
20063 m_gdbarch = arch;
20064 m_record_lines_p = record_lines_p;
20065 m_line_header = lh;
d9b3de22 20066
804d2729 20067 m_currently_recording_lines = true;
d9b3de22 20068
d9b3de22
DE
20069 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20070 was a line entry for it so that the backend has a chance to adjust it
20071 and also record it in case it needs it. This is currently used by MIPS
20072 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20073 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20074 m_is_stmt = lh->default_is_stmt;
20075 m_discriminator = 0;
252a6764
DE
20076}
20077
6f77053d
PA
20078void
20079lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20080 const gdb_byte *line_ptr,
7ab6656f 20081 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20082{
7ab6656f
OJ
20083 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20084 the pc range of the CU. However, we restrict the test to only ADDRESS
20085 values of zero to preserve GDB's previous behaviour which is to handle
20086 the specific case of a function being GC'd by the linker. */
924c2928 20087
7ab6656f 20088 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20089 {
20090 /* This line table is for a function which has been
20091 GCd by the linker. Ignore it. PR gdb/12528 */
20092
518817b3 20093 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
924c2928
DE
20094 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20095
b98664d3 20096 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20097 line_offset, objfile_name (objfile));
804d2729
TT
20098 m_currently_recording_lines = false;
20099 /* Note: m_currently_recording_lines is left as false until we see
20100 DW_LNE_end_sequence. */
924c2928
DE
20101 }
20102}
20103
f3f5162e 20104/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20105 Process the line number information in LH.
20106 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20107 program in order to set included_p for every referenced header. */
debd256d 20108
c906108c 20109static void
43f3e411
DE
20110dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20111 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20112{
d521ce57
TT
20113 const gdb_byte *line_ptr, *extended_end;
20114 const gdb_byte *line_end;
a8c50c1f 20115 unsigned int bytes_read, extended_len;
699ca60a 20116 unsigned char op_code, extended_op;
e142c38c 20117 CORE_ADDR baseaddr;
518817b3 20118 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20119 bfd *abfd = objfile->obfd;
08feed99 20120 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20121 /* True if we're recording line info (as opposed to building partial
20122 symtabs and just interested in finding include files mentioned by
20123 the line number program). */
20124 bool record_lines_p = !decode_for_pst_p;
e142c38c 20125
b3b3bada 20126 baseaddr = objfile->text_section_offset ();
c906108c 20127
debd256d
JB
20128 line_ptr = lh->statement_program_start;
20129 line_end = lh->statement_program_end;
c906108c
SS
20130
20131 /* Read the statement sequences until there's nothing left. */
20132 while (line_ptr < line_end)
20133 {
6f77053d
PA
20134 /* The DWARF line number program state machine. Reset the state
20135 machine at the start of each sequence. */
804d2729 20136 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20137 bool end_sequence = false;
d9b3de22 20138
8c43009f 20139 if (record_lines_p)
c906108c 20140 {
8c43009f
PA
20141 /* Start a subfile for the current file of the state
20142 machine. */
20143 const file_entry *fe = state_machine.current_file ();
20144
20145 if (fe != NULL)
804d2729 20146 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20147 }
20148
a738430d 20149 /* Decode the table. */
d9b3de22 20150 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20151 {
20152 op_code = read_1_byte (abfd, line_ptr);
20153 line_ptr += 1;
9aa1fe7e 20154
debd256d 20155 if (op_code >= lh->opcode_base)
6e70227d 20156 {
8e07a239 20157 /* Special opcode. */
6f77053d 20158 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20159 }
20160 else switch (op_code)
c906108c
SS
20161 {
20162 case DW_LNS_extended_op:
3e43a32a
MS
20163 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20164 &bytes_read);
473b7be6 20165 line_ptr += bytes_read;
a8c50c1f 20166 extended_end = line_ptr + extended_len;
c906108c
SS
20167 extended_op = read_1_byte (abfd, line_ptr);
20168 line_ptr += 1;
20169 switch (extended_op)
20170 {
20171 case DW_LNE_end_sequence:
6f77053d
PA
20172 state_machine.handle_end_sequence ();
20173 end_sequence = true;
c906108c
SS
20174 break;
20175 case DW_LNE_set_address:
d9b3de22
DE
20176 {
20177 CORE_ADDR address
c8a7a66f 20178 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20179 line_ptr += bytes_read;
6f77053d
PA
20180
20181 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20182 lowpc - baseaddr, address);
6f77053d 20183 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20184 }
c906108c
SS
20185 break;
20186 case DW_LNE_define_file:
debd256d 20187 {
d521ce57 20188 const char *cur_file;
ecfb656c
PA
20189 unsigned int mod_time, length;
20190 dir_index dindex;
6e70227d 20191
3e43a32a
MS
20192 cur_file = read_direct_string (abfd, line_ptr,
20193 &bytes_read);
debd256d 20194 line_ptr += bytes_read;
ecfb656c 20195 dindex = (dir_index)
debd256d
JB
20196 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20197 line_ptr += bytes_read;
20198 mod_time =
20199 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20200 line_ptr += bytes_read;
20201 length =
20202 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20203 line_ptr += bytes_read;
ecfb656c 20204 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20205 }
c906108c 20206 break;
d0c6ba3d 20207 case DW_LNE_set_discriminator:
6f77053d
PA
20208 {
20209 /* The discriminator is not interesting to the
20210 debugger; just ignore it. We still need to
20211 check its value though:
20212 if there are consecutive entries for the same
20213 (non-prologue) line we want to coalesce them.
20214 PR 17276. */
20215 unsigned int discr
20216 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20217 line_ptr += bytes_read;
20218
20219 state_machine.handle_set_discriminator (discr);
20220 }
d0c6ba3d 20221 break;
c906108c 20222 default:
b98664d3 20223 complaint (_("mangled .debug_line section"));
debd256d 20224 return;
c906108c 20225 }
a8c50c1f
DJ
20226 /* Make sure that we parsed the extended op correctly. If e.g.
20227 we expected a different address size than the producer used,
20228 we may have read the wrong number of bytes. */
20229 if (line_ptr != extended_end)
20230 {
b98664d3 20231 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20232 return;
20233 }
c906108c
SS
20234 break;
20235 case DW_LNS_copy:
6f77053d 20236 state_machine.handle_copy ();
c906108c
SS
20237 break;
20238 case DW_LNS_advance_pc:
2dc7f7b3
TT
20239 {
20240 CORE_ADDR adjust
20241 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20242 line_ptr += bytes_read;
6f77053d
PA
20243
20244 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20245 }
c906108c
SS
20246 break;
20247 case DW_LNS_advance_line:
a05a36a5
DE
20248 {
20249 int line_delta
20250 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20251 line_ptr += bytes_read;
6f77053d
PA
20252
20253 state_machine.handle_advance_line (line_delta);
a05a36a5 20254 }
c906108c
SS
20255 break;
20256 case DW_LNS_set_file:
d9b3de22 20257 {
6f77053d 20258 file_name_index file
ecfb656c
PA
20259 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20260 &bytes_read);
d9b3de22 20261 line_ptr += bytes_read;
8c43009f 20262
6f77053d 20263 state_machine.handle_set_file (file);
d9b3de22 20264 }
c906108c
SS
20265 break;
20266 case DW_LNS_set_column:
0ad93d4f 20267 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20268 line_ptr += bytes_read;
20269 break;
20270 case DW_LNS_negate_stmt:
6f77053d 20271 state_machine.handle_negate_stmt ();
c906108c
SS
20272 break;
20273 case DW_LNS_set_basic_block:
c906108c 20274 break;
c2c6d25f
JM
20275 /* Add to the address register of the state machine the
20276 address increment value corresponding to special opcode
a738430d
MK
20277 255. I.e., this value is scaled by the minimum
20278 instruction length since special opcode 255 would have
b021a221 20279 scaled the increment. */
c906108c 20280 case DW_LNS_const_add_pc:
6f77053d 20281 state_machine.handle_const_add_pc ();
c906108c
SS
20282 break;
20283 case DW_LNS_fixed_advance_pc:
3e29f34a 20284 {
6f77053d 20285 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20286 line_ptr += 2;
6f77053d
PA
20287
20288 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20289 }
c906108c 20290 break;
9aa1fe7e 20291 default:
a738430d
MK
20292 {
20293 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20294 int i;
a738430d 20295
debd256d 20296 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20297 {
20298 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20299 line_ptr += bytes_read;
20300 }
20301 }
c906108c
SS
20302 }
20303 }
d9b3de22
DE
20304
20305 if (!end_sequence)
20306 dwarf2_debug_line_missing_end_sequence_complaint ();
20307
20308 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20309 in which case we still finish recording the last line). */
6f77053d 20310 state_machine.record_line (true);
c906108c 20311 }
f3f5162e
DE
20312}
20313
20314/* Decode the Line Number Program (LNP) for the given line_header
20315 structure and CU. The actual information extracted and the type
20316 of structures created from the LNP depends on the value of PST.
20317
20318 1. If PST is NULL, then this procedure uses the data from the program
20319 to create all necessary symbol tables, and their linetables.
20320
20321 2. If PST is not NULL, this procedure reads the program to determine
20322 the list of files included by the unit represented by PST, and
20323 builds all the associated partial symbol tables.
20324
20325 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20326 It is used for relative paths in the line table.
20327 NOTE: When processing partial symtabs (pst != NULL),
20328 comp_dir == pst->dirname.
20329
20330 NOTE: It is important that psymtabs have the same file name (via strcmp)
20331 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20332 symtab we don't use it in the name of the psymtabs we create.
20333 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20334 A good testcase for this is mb-inline.exp.
20335
527f3840
JK
20336 LOWPC is the lowest address in CU (or 0 if not known).
20337
20338 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20339 for its PC<->lines mapping information. Otherwise only the filename
20340 table is read in. */
f3f5162e
DE
20341
20342static void
20343dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20344 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20345 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20346{
518817b3 20347 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
f3f5162e 20348 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20349
527f3840
JK
20350 if (decode_mapping)
20351 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20352
20353 if (decode_for_pst_p)
20354 {
aaa75496
JB
20355 /* Now that we're done scanning the Line Header Program, we can
20356 create the psymtab of each included file. */
7ba99d21
AT
20357 for (auto &file_entry : lh->file_names ())
20358 if (file_entry.included_p == 1)
aaa75496 20359 {
c89b44cd 20360 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20361 const char *include_name =
7ba99d21
AT
20362 psymtab_include_file_name (lh, file_entry, pst,
20363 comp_dir, &name_holder);
c6da4cef 20364 if (include_name != NULL)
aaa75496
JB
20365 dwarf2_create_include_psymtab (include_name, pst, objfile);
20366 }
20367 }
cb1df416
DJ
20368 else
20369 {
20370 /* Make sure a symtab is created for every file, even files
20371 which contain only variables (i.e. no code with associated
20372 line numbers). */
c24bdb02
KS
20373 buildsym_compunit *builder = cu->get_builder ();
20374 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20375
7ba99d21 20376 for (auto &fe : lh->file_names ())
cb1df416 20377 {
804d2729 20378 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20379 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20380 {
c24bdb02 20381 builder->get_current_subfile ()->symtab
804d2729 20382 = allocate_symtab (cust,
c24bdb02 20383 builder->get_current_subfile ()->name);
43f3e411 20384 }
c24bdb02 20385 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20386 }
20387 }
c906108c
SS
20388}
20389
20390/* Start a subfile for DWARF. FILENAME is the name of the file and
20391 DIRNAME the name of the source directory which contains FILENAME
4d663531 20392 or NULL if not known.
c906108c
SS
20393 This routine tries to keep line numbers from identical absolute and
20394 relative file names in a common subfile.
20395
20396 Using the `list' example from the GDB testsuite, which resides in
20397 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20398 of /srcdir/list0.c yields the following debugging information for list0.c:
20399
c5aa993b 20400 DW_AT_name: /srcdir/list0.c
4d663531 20401 DW_AT_comp_dir: /compdir
357e46e7 20402 files.files[0].name: list0.h
c5aa993b 20403 files.files[0].dir: /srcdir
357e46e7 20404 files.files[1].name: list0.c
c5aa993b 20405 files.files[1].dir: /srcdir
c906108c
SS
20406
20407 The line number information for list0.c has to end up in a single
4f1520fb
FR
20408 subfile, so that `break /srcdir/list0.c:1' works as expected.
20409 start_subfile will ensure that this happens provided that we pass the
20410 concatenation of files.files[1].dir and files.files[1].name as the
20411 subfile's name. */
c906108c
SS
20412
20413static void
804d2729
TT
20414dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20415 const char *dirname)
c906108c 20416{
43816ebc 20417 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20418
4d663531 20419 /* In order not to lose the line information directory,
4f1520fb
FR
20420 we concatenate it to the filename when it makes sense.
20421 Note that the Dwarf3 standard says (speaking of filenames in line
20422 information): ``The directory index is ignored for file names
20423 that represent full path names''. Thus ignoring dirname in the
20424 `else' branch below isn't an issue. */
c906108c 20425
d5166ae1 20426 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20427 {
43816ebc
TT
20428 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20429 filename = copy.get ();
d521ce57 20430 }
c906108c 20431
c24bdb02 20432 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20433}
20434
804d2729
TT
20435/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20436 buildsym_compunit constructor. */
f4dc4d17 20437
c24bdb02
KS
20438struct compunit_symtab *
20439dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20440 CORE_ADDR low_pc)
f4dc4d17 20441{
c24bdb02 20442 gdb_assert (m_builder == nullptr);
43f3e411 20443
c24bdb02
KS
20444 m_builder.reset (new struct buildsym_compunit
20445 (per_cu->dwarf2_per_objfile->objfile,
20446 name, comp_dir, language, low_pc));
93b8bea4 20447
c24bdb02 20448 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20449
c24bdb02
KS
20450 get_builder ()->record_debugformat ("DWARF 2");
20451 get_builder ()->record_producer (producer);
f4dc4d17 20452
c24bdb02 20453 processing_has_namespace_info = false;
43f3e411 20454
c24bdb02 20455 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20456}
20457
4c2df51b
DJ
20458static void
20459var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20460 struct dwarf2_cu *cu)
4c2df51b 20461{
518817b3 20462 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e7c27a73
DJ
20463 struct comp_unit_head *cu_header = &cu->header;
20464
4c2df51b
DJ
20465 /* NOTE drow/2003-01-30: There used to be a comment and some special
20466 code here to turn a symbol with DW_AT_external and a
20467 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20468 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20469 with some versions of binutils) where shared libraries could have
20470 relocations against symbols in their debug information - the
20471 minimal symbol would have the right address, but the debug info
20472 would not. It's no longer necessary, because we will explicitly
20473 apply relocations when we read in the debug information now. */
20474
20475 /* A DW_AT_location attribute with no contents indicates that a
20476 variable has been optimized away. */
4fc6c0d5 20477 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20478 {
f1e6e072 20479 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20480 return;
20481 }
20482
20483 /* Handle one degenerate form of location expression specially, to
20484 preserve GDB's previous behavior when section offsets are
336d760d
AT
20485 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20486 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20487
4fc6c0d5 20488 if (attr->form_is_block ()
3019eac3
DE
20489 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20490 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20491 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20492 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20493 && (DW_BLOCK (attr)->size
20494 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20495 {
891d2f0b 20496 unsigned int dummy;
4c2df51b 20497
3019eac3 20498 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20499 SET_SYMBOL_VALUE_ADDRESS
20500 (sym, cu->header.read_address (objfile->obfd,
20501 DW_BLOCK (attr)->data + 1,
20502 &dummy));
3019eac3 20503 else
38583298
TT
20504 SET_SYMBOL_VALUE_ADDRESS
20505 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20506 &dummy));
f1e6e072 20507 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20508 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20509 SET_SYMBOL_VALUE_ADDRESS
20510 (sym,
20511 SYMBOL_VALUE_ADDRESS (sym)
20512 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20513 return;
20514 }
20515
20516 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20517 expression evaluator, and use LOC_COMPUTED only when necessary
20518 (i.e. when the value of a register or memory location is
20519 referenced, or a thread-local block, etc.). Then again, it might
20520 not be worthwhile. I'm assuming that it isn't unless performance
20521 or memory numbers show me otherwise. */
20522
f1e6e072 20523 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20524
f1e6e072 20525 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20526 cu->has_loclist = true;
4c2df51b
DJ
20527}
20528
c906108c
SS
20529/* Given a pointer to a DWARF information entry, figure out if we need
20530 to make a symbol table entry for it, and if so, create a new entry
20531 and return a pointer to it.
20532 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20533 used the passed type.
20534 If SPACE is not NULL, use it to hold the new symbol. If it is
20535 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20536
20537static struct symbol *
5e2db402
TT
20538new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20539 struct symbol *space)
c906108c 20540{
518817b3
SM
20541 struct dwarf2_per_objfile *dwarf2_per_objfile
20542 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 20543 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20544 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20545 struct symbol *sym = NULL;
15d034d0 20546 const char *name;
c906108c
SS
20547 struct attribute *attr = NULL;
20548 struct attribute *attr2 = NULL;
e142c38c 20549 CORE_ADDR baseaddr;
e37fd15a
SW
20550 struct pending **list_to_add = NULL;
20551
edb3359d 20552 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20553
b3b3bada 20554 baseaddr = objfile->text_section_offset ();
c906108c 20555
94af9270 20556 name = dwarf2_name (die, cu);
c906108c
SS
20557 if (name)
20558 {
94af9270 20559 const char *linkagename;
34eaf542 20560 int suppress_add = 0;
94af9270 20561
34eaf542
TT
20562 if (space)
20563 sym = space;
20564 else
e623cf5d 20565 sym = allocate_symbol (objfile);
c906108c 20566 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20567
20568 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20569 sym->set_language (cu->language, &objfile->objfile_obstack);
94af9270 20570 linkagename = dwarf2_physname (name, die, cu);
4d4eaa30 20571 sym->compute_and_set_names (linkagename, false, objfile->per_bfd);
c906108c 20572
f55ee35c
JK
20573 /* Fortran does not have mangling standard and the mangling does differ
20574 between gfortran, iFort etc. */
20575 if (cu->language == language_fortran
468c0cbb 20576 && symbol_get_demangled_name (sym) == NULL)
ff985671 20577 sym->set_demangled_name (dwarf2_full_name (name, die, cu), NULL);
f55ee35c 20578
c906108c 20579 /* Default assumptions.
c5aa993b 20580 Use the passed type or decode it from the die. */
176620f1 20581 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20582 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20583 if (type != NULL)
20584 SYMBOL_TYPE (sym) = type;
20585 else
e7c27a73 20586 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20587 attr = dwarf2_attr (die,
20588 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20589 cu);
435d3d88 20590 if (attr != nullptr)
c906108c
SS
20591 {
20592 SYMBOL_LINE (sym) = DW_UNSND (attr);
20593 }
cb1df416 20594
edb3359d
DJ
20595 attr = dwarf2_attr (die,
20596 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20597 cu);
435d3d88 20598 if (attr != nullptr)
cb1df416 20599 {
ecfb656c 20600 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20601 struct file_entry *fe;
9a619af0 20602
ecfb656c
PA
20603 if (cu->line_header != NULL)
20604 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20605 else
20606 fe = NULL;
20607
20608 if (fe == NULL)
b98664d3 20609 complaint (_("file index out of range"));
8c43009f
PA
20610 else
20611 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20612 }
20613
c906108c
SS
20614 switch (die->tag)
20615 {
20616 case DW_TAG_label:
e142c38c 20617 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20618 if (attr != nullptr)
3e29f34a
MR
20619 {
20620 CORE_ADDR addr;
20621
cd6c91b4 20622 addr = attr->value_as_address ();
3e29f34a 20623 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20624 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20625 }
0f5238ed
TT
20626 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20627 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20628 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20629 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20630 break;
20631 case DW_TAG_subprogram:
20632 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20633 finish_block. */
f1e6e072 20634 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20635 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20636 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20637 || cu->language == language_ada
20638 || cu->language == language_fortran)
c906108c 20639 {
2cfa0c8d 20640 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20641 Ada and Fortran subprograms, whether marked external or
20642 not, are always stored as a global symbol, because we want
20643 to be able to access them globally. For instance, we want
20644 to be able to break on a nested subprogram without having
20645 to specify the context. */
c24bdb02 20646 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20647 }
20648 else
20649 {
e37fd15a 20650 list_to_add = cu->list_in_scope;
c906108c
SS
20651 }
20652 break;
edb3359d
DJ
20653 case DW_TAG_inlined_subroutine:
20654 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20655 finish_block. */
f1e6e072 20656 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20657 SYMBOL_INLINED (sym) = 1;
481860b3 20658 list_to_add = cu->list_in_scope;
edb3359d 20659 break;
34eaf542
TT
20660 case DW_TAG_template_value_param:
20661 suppress_add = 1;
20662 /* Fall through. */
72929c62 20663 case DW_TAG_constant:
c906108c 20664 case DW_TAG_variable:
254e6b9e 20665 case DW_TAG_member:
0963b4bd
MS
20666 /* Compilation with minimal debug info may result in
20667 variables with missing type entries. Change the
20668 misleading `void' type to something sensible. */
c906108c 20669 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
46a4882b 20670 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20671
e142c38c 20672 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20673 /* In the case of DW_TAG_member, we should only be called for
20674 static const members. */
20675 if (die->tag == DW_TAG_member)
20676 {
3863f96c
DE
20677 /* dwarf2_add_field uses die_is_declaration,
20678 so we do the same. */
254e6b9e
DE
20679 gdb_assert (die_is_declaration (die, cu));
20680 gdb_assert (attr);
20681 }
435d3d88 20682 if (attr != nullptr)
c906108c 20683 {
e7c27a73 20684 dwarf2_const_value (attr, sym, cu);
e142c38c 20685 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20686 if (!suppress_add)
34eaf542
TT
20687 {
20688 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20689 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20690 else
e37fd15a 20691 list_to_add = cu->list_in_scope;
34eaf542 20692 }
c906108c
SS
20693 break;
20694 }
e142c38c 20695 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20696 if (attr != nullptr)
c906108c 20697 {
e7c27a73 20698 var_decode_location (attr, sym, cu);
e142c38c 20699 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20700
20701 /* Fortran explicitly imports any global symbols to the local
20702 scope by DW_TAG_common_block. */
20703 if (cu->language == language_fortran && die->parent
20704 && die->parent->tag == DW_TAG_common_block)
20705 attr2 = NULL;
20706
caac4577
JG
20707 if (SYMBOL_CLASS (sym) == LOC_STATIC
20708 && SYMBOL_VALUE_ADDRESS (sym) == 0
20709 && !dwarf2_per_objfile->has_section_at_zero)
20710 {
20711 /* When a static variable is eliminated by the linker,
20712 the corresponding debug information is not stripped
20713 out, but the variable address is set to null;
20714 do not add such variables into symbol table. */
20715 }
20716 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20717 {
4b610737
TT
20718 if (SYMBOL_CLASS (sym) == LOC_STATIC
20719 && (objfile->flags & OBJF_MAINLINE) == 0
20720 && dwarf2_per_objfile->can_copy)
20721 {
20722 /* A global static variable might be subject to
20723 copy relocation. We first check for a local
20724 minsym, though, because maybe the symbol was
20725 marked hidden, in which case this would not
20726 apply. */
20727 bound_minimal_symbol found
20728 = (lookup_minimal_symbol_linkage
987012b8 20729 (sym->linkage_name (), objfile));
4b610737
TT
20730 if (found.minsym != nullptr)
20731 sym->maybe_copied = 1;
20732 }
f55ee35c 20733
1c809c68
TT
20734 /* A variable with DW_AT_external is never static,
20735 but it may be block-scoped. */
804d2729 20736 list_to_add
c24bdb02
KS
20737 = ((cu->list_in_scope
20738 == cu->get_builder ()->get_file_symbols ())
20739 ? cu->get_builder ()->get_global_symbols ()
804d2729 20740 : cu->list_in_scope);
1c809c68 20741 }
c906108c 20742 else
e37fd15a 20743 list_to_add = cu->list_in_scope;
c906108c
SS
20744 }
20745 else
20746 {
20747 /* We do not know the address of this symbol.
c5aa993b
JM
20748 If it is an external symbol and we have type information
20749 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20750 The address of the variable will then be determined from
20751 the minimal symbol table whenever the variable is
20752 referenced. */
e142c38c 20753 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20754
20755 /* Fortran explicitly imports any global symbols to the local
20756 scope by DW_TAG_common_block. */
20757 if (cu->language == language_fortran && die->parent
20758 && die->parent->tag == DW_TAG_common_block)
20759 {
20760 /* SYMBOL_CLASS doesn't matter here because
20761 read_common_block is going to reset it. */
20762 if (!suppress_add)
20763 list_to_add = cu->list_in_scope;
20764 }
20765 else if (attr2 && (DW_UNSND (attr2) != 0)
20766 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20767 {
0fe7935b
DJ
20768 /* A variable with DW_AT_external is never static, but it
20769 may be block-scoped. */
804d2729 20770 list_to_add
c24bdb02
KS
20771 = ((cu->list_in_scope
20772 == cu->get_builder ()->get_file_symbols ())
20773 ? cu->get_builder ()->get_global_symbols ()
804d2729 20774 : cu->list_in_scope);
0fe7935b 20775
f1e6e072 20776 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20777 }
442ddf59
JK
20778 else if (!die_is_declaration (die, cu))
20779 {
20780 /* Use the default LOC_OPTIMIZED_OUT class. */
20781 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20782 if (!suppress_add)
20783 list_to_add = cu->list_in_scope;
442ddf59 20784 }
c906108c
SS
20785 }
20786 break;
20787 case DW_TAG_formal_parameter:
a60f3166
TT
20788 {
20789 /* If we are inside a function, mark this as an argument. If
20790 not, we might be looking at an argument to an inlined function
20791 when we do not have enough information to show inlined frames;
20792 pretend it's a local variable in that case so that the user can
20793 still see it. */
804d2729 20794 struct context_stack *curr
c24bdb02 20795 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20796 if (curr != nullptr && curr->name != nullptr)
20797 SYMBOL_IS_ARGUMENT (sym) = 1;
20798 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20799 if (attr != nullptr)
a60f3166
TT
20800 {
20801 var_decode_location (attr, sym, cu);
20802 }
20803 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20804 if (attr != nullptr)
a60f3166
TT
20805 {
20806 dwarf2_const_value (attr, sym, cu);
20807 }
f346a30d 20808
a60f3166
TT
20809 list_to_add = cu->list_in_scope;
20810 }
c906108c
SS
20811 break;
20812 case DW_TAG_unspecified_parameters:
20813 /* From varargs functions; gdb doesn't seem to have any
20814 interest in this information, so just ignore it for now.
20815 (FIXME?) */
20816 break;
34eaf542
TT
20817 case DW_TAG_template_type_param:
20818 suppress_add = 1;
20819 /* Fall through. */
c906108c 20820 case DW_TAG_class_type:
680b30c7 20821 case DW_TAG_interface_type:
c906108c
SS
20822 case DW_TAG_structure_type:
20823 case DW_TAG_union_type:
72019c9c 20824 case DW_TAG_set_type:
c906108c 20825 case DW_TAG_enumeration_type:
f1e6e072 20826 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20827 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20828
63d06c5c 20829 {
9c37b5ae 20830 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20831 really ever be static objects: otherwise, if you try
20832 to, say, break of a class's method and you're in a file
20833 which doesn't mention that class, it won't work unless
20834 the check for all static symbols in lookup_symbol_aux
20835 saves you. See the OtherFileClass tests in
20836 gdb.c++/namespace.exp. */
20837
e37fd15a 20838 if (!suppress_add)
34eaf542 20839 {
c24bdb02 20840 buildsym_compunit *builder = cu->get_builder ();
804d2729 20841 list_to_add
c24bdb02 20842 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20843 && cu->language == language_cplus
c24bdb02 20844 ? builder->get_global_symbols ()
804d2729 20845 : cu->list_in_scope);
63d06c5c 20846
64382290 20847 /* The semantics of C++ state that "struct foo {
9c37b5ae 20848 ... }" also defines a typedef for "foo". */
64382290 20849 if (cu->language == language_cplus
45280282 20850 || cu->language == language_ada
c44af4eb
TT
20851 || cu->language == language_d
20852 || cu->language == language_rust)
64382290
TT
20853 {
20854 /* The symbol's name is already allocated along
20855 with this objfile, so we don't need to
20856 duplicate it for the type. */
20857 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
987012b8 20858 TYPE_NAME (SYMBOL_TYPE (sym)) = sym->search_name ();
64382290 20859 }
63d06c5c
DC
20860 }
20861 }
c906108c
SS
20862 break;
20863 case DW_TAG_typedef:
f1e6e072 20864 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 20865 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20866 list_to_add = cu->list_in_scope;
63d06c5c 20867 break;
c906108c 20868 case DW_TAG_base_type:
a02abb62 20869 case DW_TAG_subrange_type:
f1e6e072 20870 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20871 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 20872 list_to_add = cu->list_in_scope;
c906108c
SS
20873 break;
20874 case DW_TAG_enumerator:
e142c38c 20875 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20876 if (attr != nullptr)
c906108c 20877 {
e7c27a73 20878 dwarf2_const_value (attr, sym, cu);
c906108c 20879 }
63d06c5c
DC
20880 {
20881 /* NOTE: carlton/2003-11-10: See comment above in the
20882 DW_TAG_class_type, etc. block. */
20883
804d2729 20884 list_to_add
c24bdb02 20885 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 20886 && cu->language == language_cplus
c24bdb02 20887 ? cu->get_builder ()->get_global_symbols ()
804d2729 20888 : cu->list_in_scope);
63d06c5c 20889 }
c906108c 20890 break;
74921315 20891 case DW_TAG_imported_declaration:
5c4e30ca 20892 case DW_TAG_namespace:
f1e6e072 20893 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 20894 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 20895 break;
530e8392
KB
20896 case DW_TAG_module:
20897 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
20898 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 20899 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 20900 break;
4357ac6c 20901 case DW_TAG_common_block:
f1e6e072 20902 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 20903 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 20904 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 20905 break;
c906108c
SS
20906 default:
20907 /* Not a tag we recognize. Hopefully we aren't processing
20908 trash data, but since we must specifically ignore things
20909 we don't recognize, there is nothing else we should do at
0963b4bd 20910 this point. */
b98664d3 20911 complaint (_("unsupported tag: '%s'"),
4d3c2250 20912 dwarf_tag_name (die->tag));
c906108c
SS
20913 break;
20914 }
df8a16a1 20915
e37fd15a
SW
20916 if (suppress_add)
20917 {
20918 sym->hash_next = objfile->template_symbols;
20919 objfile->template_symbols = sym;
20920 list_to_add = NULL;
20921 }
20922
20923 if (list_to_add != NULL)
d3cb6808 20924 add_symbol_to_list (sym, list_to_add);
e37fd15a 20925
df8a16a1
DJ
20926 /* For the benefit of old versions of GCC, check for anonymous
20927 namespaces based on the demangled name. */
4d4ec4e5 20928 if (!cu->processing_has_namespace_info
94af9270 20929 && cu->language == language_cplus)
c24bdb02 20930 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
20931 }
20932 return (sym);
20933}
20934
98bfdba5
PA
20935/* Given an attr with a DW_FORM_dataN value in host byte order,
20936 zero-extend it as appropriate for the symbol's type. The DWARF
20937 standard (v4) is not entirely clear about the meaning of using
20938 DW_FORM_dataN for a constant with a signed type, where the type is
20939 wider than the data. The conclusion of a discussion on the DWARF
20940 list was that this is unspecified. We choose to always zero-extend
20941 because that is the interpretation long in use by GCC. */
c906108c 20942
98bfdba5 20943static gdb_byte *
ff39bb5e 20944dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 20945 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 20946{
518817b3 20947 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
e17a4113
UW
20948 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
20949 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
20950 LONGEST l = DW_UNSND (attr);
20951
20952 if (bits < sizeof (*value) * 8)
20953 {
20954 l &= ((LONGEST) 1 << bits) - 1;
20955 *value = l;
20956 }
20957 else if (bits == sizeof (*value) * 8)
20958 *value = l;
20959 else
20960 {
224c3ddb 20961 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
20962 store_unsigned_integer (bytes, bits / 8, byte_order, l);
20963 return bytes;
20964 }
20965
20966 return NULL;
20967}
20968
20969/* Read a constant value from an attribute. Either set *VALUE, or if
20970 the value does not fit in *VALUE, set *BYTES - either already
20971 allocated on the objfile obstack, or newly allocated on OBSTACK,
20972 or, set *BATON, if we translated the constant to a location
20973 expression. */
20974
20975static void
ff39bb5e 20976dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
20977 const char *name, struct obstack *obstack,
20978 struct dwarf2_cu *cu,
d521ce57 20979 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
20980 struct dwarf2_locexpr_baton **baton)
20981{
518817b3 20982 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
98bfdba5 20983 struct comp_unit_head *cu_header = &cu->header;
c906108c 20984 struct dwarf_block *blk;
98bfdba5
PA
20985 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
20986 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
20987
20988 *value = 0;
20989 *bytes = NULL;
20990 *baton = NULL;
c906108c
SS
20991
20992 switch (attr->form)
20993 {
20994 case DW_FORM_addr:
336d760d 20995 case DW_FORM_addrx:
3019eac3 20996 case DW_FORM_GNU_addr_index:
ac56253d 20997 {
ac56253d
TT
20998 gdb_byte *data;
20999
98bfdba5
PA
21000 if (TYPE_LENGTH (type) != cu_header->addr_size)
21001 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21002 cu_header->addr_size,
98bfdba5 21003 TYPE_LENGTH (type));
ac56253d
TT
21004 /* Symbols of this form are reasonably rare, so we just
21005 piggyback on the existing location code rather than writing
21006 a new implementation of symbol_computed_ops. */
8d749320 21007 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
98bfdba5
PA
21008 (*baton)->per_cu = cu->per_cu;
21009 gdb_assert ((*baton)->per_cu);
ac56253d 21010
98bfdba5 21011 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21012 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21013 (*baton)->data = data;
ac56253d
TT
21014
21015 data[0] = DW_OP_addr;
21016 store_unsigned_integer (&data[1], cu_header->addr_size,
21017 byte_order, DW_ADDR (attr));
21018 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21019 }
c906108c 21020 break;
4ac36638 21021 case DW_FORM_string:
93b5768b 21022 case DW_FORM_strp:
cf532bd1 21023 case DW_FORM_strx:
3019eac3 21024 case DW_FORM_GNU_str_index:
36586728 21025 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21026 /* DW_STRING is already allocated on the objfile obstack, point
21027 directly to it. */
d521ce57 21028 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21029 break;
c906108c
SS
21030 case DW_FORM_block1:
21031 case DW_FORM_block2:
21032 case DW_FORM_block4:
21033 case DW_FORM_block:
2dc7f7b3 21034 case DW_FORM_exprloc:
0224619f 21035 case DW_FORM_data16:
c906108c 21036 blk = DW_BLOCK (attr);
98bfdba5
PA
21037 if (TYPE_LENGTH (type) != blk->size)
21038 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21039 TYPE_LENGTH (type));
21040 *bytes = blk->data;
c906108c 21041 break;
2df3850c
JM
21042
21043 /* The DW_AT_const_value attributes are supposed to carry the
21044 symbol's value "represented as it would be on the target
21045 architecture." By the time we get here, it's already been
21046 converted to host endianness, so we just need to sign- or
21047 zero-extend it as appropriate. */
21048 case DW_FORM_data1:
3aef2284 21049 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21050 break;
c906108c 21051 case DW_FORM_data2:
3aef2284 21052 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21053 break;
c906108c 21054 case DW_FORM_data4:
3aef2284 21055 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21056 break;
c906108c 21057 case DW_FORM_data8:
3aef2284 21058 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21059 break;
21060
c906108c 21061 case DW_FORM_sdata:
663c44ac 21062 case DW_FORM_implicit_const:
98bfdba5 21063 *value = DW_SND (attr);
2df3850c
JM
21064 break;
21065
c906108c 21066 case DW_FORM_udata:
98bfdba5 21067 *value = DW_UNSND (attr);
c906108c 21068 break;
2df3850c 21069
c906108c 21070 default:
b98664d3 21071 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21072 dwarf_form_name (attr->form));
98bfdba5 21073 *value = 0;
c906108c
SS
21074 break;
21075 }
21076}
21077
2df3850c 21078
98bfdba5
PA
21079/* Copy constant value from an attribute to a symbol. */
21080
2df3850c 21081static void
ff39bb5e 21082dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21083 struct dwarf2_cu *cu)
2df3850c 21084{
518817b3 21085 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
12df843f 21086 LONGEST value;
d521ce57 21087 const gdb_byte *bytes;
98bfdba5 21088 struct dwarf2_locexpr_baton *baton;
2df3850c 21089
98bfdba5 21090 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21091 sym->print_name (),
98bfdba5
PA
21092 &objfile->objfile_obstack, cu,
21093 &value, &bytes, &baton);
2df3850c 21094
98bfdba5
PA
21095 if (baton != NULL)
21096 {
98bfdba5 21097 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21098 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21099 }
21100 else if (bytes != NULL)
21101 {
21102 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21103 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21104 }
21105 else
21106 {
21107 SYMBOL_VALUE (sym) = value;
f1e6e072 21108 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21109 }
2df3850c
JM
21110}
21111
c906108c
SS
21112/* Return the type of the die in question using its DW_AT_type attribute. */
21113
21114static struct type *
e7c27a73 21115die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21116{
c906108c 21117 struct attribute *type_attr;
c906108c 21118
e142c38c 21119 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21120 if (!type_attr)
21121 {
518817b3 21122 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21123 /* A missing DW_AT_type represents a void type. */
518817b3 21124 return objfile_type (objfile)->builtin_void;
c906108c 21125 }
348e048f 21126
673bfd45 21127 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21128}
21129
b4ba55a1
JB
21130/* True iff CU's producer generates GNAT Ada auxiliary information
21131 that allows to find parallel types through that information instead
21132 of having to do expensive parallel lookups by type name. */
21133
21134static int
21135need_gnat_info (struct dwarf2_cu *cu)
21136{
de4cb04a
JB
21137 /* Assume that the Ada compiler was GNAT, which always produces
21138 the auxiliary information. */
21139 return (cu->language == language_ada);
b4ba55a1
JB
21140}
21141
b4ba55a1
JB
21142/* Return the auxiliary type of the die in question using its
21143 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21144 attribute is not present. */
21145
21146static struct type *
21147die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21148{
b4ba55a1 21149 struct attribute *type_attr;
b4ba55a1
JB
21150
21151 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21152 if (!type_attr)
21153 return NULL;
21154
673bfd45 21155 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21156}
21157
21158/* If DIE has a descriptive_type attribute, then set the TYPE's
21159 descriptive type accordingly. */
21160
21161static void
21162set_descriptive_type (struct type *type, struct die_info *die,
21163 struct dwarf2_cu *cu)
21164{
21165 struct type *descriptive_type = die_descriptive_type (die, cu);
21166
21167 if (descriptive_type)
21168 {
21169 ALLOCATE_GNAT_AUX_TYPE (type);
21170 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21171 }
21172}
21173
c906108c
SS
21174/* Return the containing type of the die in question using its
21175 DW_AT_containing_type attribute. */
21176
21177static struct type *
e7c27a73 21178die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21179{
c906108c 21180 struct attribute *type_attr;
518817b3 21181 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
c906108c 21182
e142c38c 21183 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21184 if (!type_attr)
21185 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21186 "[in module %s]"), objfile_name (objfile));
33ac96f0 21187
673bfd45 21188 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21189}
21190
ac9ec31b
DE
21191/* Return an error marker type to use for the ill formed type in DIE/CU. */
21192
21193static struct type *
21194build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21195{
518817b3
SM
21196 struct dwarf2_per_objfile *dwarf2_per_objfile
21197 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b 21198 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21199 char *saved;
ac9ec31b 21200
528e1572
SM
21201 std::string message
21202 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21203 objfile_name (objfile),
21204 sect_offset_str (cu->header.sect_off),
21205 sect_offset_str (die->sect_off));
efba19b0 21206 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21207
19f392bc 21208 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21209}
21210
673bfd45 21211/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21212 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21213 DW_AT_containing_type.
673bfd45
DE
21214 If there is no type substitute an error marker. */
21215
c906108c 21216static struct type *
ff39bb5e 21217lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21218 struct dwarf2_cu *cu)
c906108c 21219{
518817b3
SM
21220 struct dwarf2_per_objfile *dwarf2_per_objfile
21221 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 21222 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21223 struct type *this_type;
21224
ac9ec31b
DE
21225 gdb_assert (attr->name == DW_AT_type
21226 || attr->name == DW_AT_GNAT_descriptive_type
21227 || attr->name == DW_AT_containing_type);
21228
673bfd45
DE
21229 /* First see if we have it cached. */
21230
36586728
TT
21231 if (attr->form == DW_FORM_GNU_ref_alt)
21232 {
21233 struct dwarf2_per_cu_data *per_cu;
0826b30a 21234 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21235
ed2dc618
SM
21236 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21237 dwarf2_per_objfile);
9c541725 21238 this_type = get_die_type_at_offset (sect_off, per_cu);
36586728 21239 }
cd6c91b4 21240 else if (attr->form_is_ref ())
673bfd45 21241 {
0826b30a 21242 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21243
9c541725 21244 this_type = get_die_type_at_offset (sect_off, cu->per_cu);
673bfd45 21245 }
55f1336d 21246 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21247 {
ac9ec31b 21248 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21249
ac9ec31b 21250 return get_signatured_type (die, signature, cu);
673bfd45
DE
21251 }
21252 else
21253 {
b98664d3 21254 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21255 " at %s [in module %s]"),
21256 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21257 objfile_name (objfile));
ac9ec31b 21258 return build_error_marker_type (cu, die);
673bfd45
DE
21259 }
21260
21261 /* If not cached we need to read it in. */
21262
21263 if (this_type == NULL)
21264 {
ac9ec31b 21265 struct die_info *type_die = NULL;
673bfd45
DE
21266 struct dwarf2_cu *type_cu = cu;
21267
cd6c91b4 21268 if (attr->form_is_ref ())
ac9ec31b
DE
21269 type_die = follow_die_ref (die, attr, &type_cu);
21270 if (type_die == NULL)
21271 return build_error_marker_type (cu, die);
21272 /* If we find the type now, it's probably because the type came
3019eac3
DE
21273 from an inter-CU reference and the type's CU got expanded before
21274 ours. */
ac9ec31b 21275 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21276 }
21277
21278 /* If we still don't have a type use an error marker. */
21279
21280 if (this_type == NULL)
ac9ec31b 21281 return build_error_marker_type (cu, die);
673bfd45 21282
f792889a 21283 return this_type;
c906108c
SS
21284}
21285
673bfd45
DE
21286/* Return the type in DIE, CU.
21287 Returns NULL for invalid types.
21288
02142a6c 21289 This first does a lookup in die_type_hash,
673bfd45
DE
21290 and only reads the die in if necessary.
21291
21292 NOTE: This can be called when reading in partial or full symbols. */
21293
f792889a 21294static struct type *
e7c27a73 21295read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21296{
f792889a
DJ
21297 struct type *this_type;
21298
21299 this_type = get_die_type (die, cu);
21300 if (this_type)
21301 return this_type;
21302
673bfd45
DE
21303 return read_type_die_1 (die, cu);
21304}
21305
21306/* Read the type in DIE, CU.
21307 Returns NULL for invalid types. */
21308
21309static struct type *
21310read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21311{
21312 struct type *this_type = NULL;
21313
c906108c
SS
21314 switch (die->tag)
21315 {
21316 case DW_TAG_class_type:
680b30c7 21317 case DW_TAG_interface_type:
c906108c
SS
21318 case DW_TAG_structure_type:
21319 case DW_TAG_union_type:
f792889a 21320 this_type = read_structure_type (die, cu);
c906108c
SS
21321 break;
21322 case DW_TAG_enumeration_type:
f792889a 21323 this_type = read_enumeration_type (die, cu);
c906108c
SS
21324 break;
21325 case DW_TAG_subprogram:
21326 case DW_TAG_subroutine_type:
edb3359d 21327 case DW_TAG_inlined_subroutine:
f792889a 21328 this_type = read_subroutine_type (die, cu);
c906108c
SS
21329 break;
21330 case DW_TAG_array_type:
f792889a 21331 this_type = read_array_type (die, cu);
c906108c 21332 break;
72019c9c 21333 case DW_TAG_set_type:
f792889a 21334 this_type = read_set_type (die, cu);
72019c9c 21335 break;
c906108c 21336 case DW_TAG_pointer_type:
f792889a 21337 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21338 break;
21339 case DW_TAG_ptr_to_member_type:
f792889a 21340 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21341 break;
21342 case DW_TAG_reference_type:
4297a3f0
AV
21343 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21344 break;
21345 case DW_TAG_rvalue_reference_type:
21346 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21347 break;
21348 case DW_TAG_const_type:
f792889a 21349 this_type = read_tag_const_type (die, cu);
c906108c
SS
21350 break;
21351 case DW_TAG_volatile_type:
f792889a 21352 this_type = read_tag_volatile_type (die, cu);
c906108c 21353 break;
06d66ee9
TT
21354 case DW_TAG_restrict_type:
21355 this_type = read_tag_restrict_type (die, cu);
21356 break;
c906108c 21357 case DW_TAG_string_type:
f792889a 21358 this_type = read_tag_string_type (die, cu);
c906108c
SS
21359 break;
21360 case DW_TAG_typedef:
f792889a 21361 this_type = read_typedef (die, cu);
c906108c 21362 break;
a02abb62 21363 case DW_TAG_subrange_type:
f792889a 21364 this_type = read_subrange_type (die, cu);
a02abb62 21365 break;
c906108c 21366 case DW_TAG_base_type:
f792889a 21367 this_type = read_base_type (die, cu);
c906108c 21368 break;
81a17f79 21369 case DW_TAG_unspecified_type:
f792889a 21370 this_type = read_unspecified_type (die, cu);
81a17f79 21371 break;
0114d602
DJ
21372 case DW_TAG_namespace:
21373 this_type = read_namespace_type (die, cu);
21374 break;
f55ee35c
JK
21375 case DW_TAG_module:
21376 this_type = read_module_type (die, cu);
21377 break;
a2c2acaf
MW
21378 case DW_TAG_atomic_type:
21379 this_type = read_tag_atomic_type (die, cu);
21380 break;
c906108c 21381 default:
b98664d3 21382 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21383 dwarf_tag_name (die->tag));
c906108c
SS
21384 break;
21385 }
63d06c5c 21386
f792889a 21387 return this_type;
63d06c5c
DC
21388}
21389
abc72ce4
DE
21390/* See if we can figure out if the class lives in a namespace. We do
21391 this by looking for a member function; its demangled name will
21392 contain namespace info, if there is any.
21393 Return the computed name or NULL.
21394 Space for the result is allocated on the objfile's obstack.
21395 This is the full-die version of guess_partial_die_structure_name.
21396 In this case we know DIE has no useful parent. */
21397
43816ebc 21398static const char *
abc72ce4
DE
21399guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21400{
21401 struct die_info *spec_die;
21402 struct dwarf2_cu *spec_cu;
21403 struct die_info *child;
518817b3 21404 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
abc72ce4
DE
21405
21406 spec_cu = cu;
21407 spec_die = die_specification (die, &spec_cu);
21408 if (spec_die != NULL)
21409 {
21410 die = spec_die;
21411 cu = spec_cu;
21412 }
21413
21414 for (child = die->child;
21415 child != NULL;
21416 child = child->sibling)
21417 {
21418 if (child->tag == DW_TAG_subprogram)
21419 {
73b9be8b 21420 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21421
7d45c7c3 21422 if (linkage_name != NULL)
abc72ce4 21423 {
43816ebc
TT
21424 gdb::unique_xmalloc_ptr<char> actual_name
21425 (language_class_name_from_physname (cu->language_defn,
21426 linkage_name));
21427 const char *name = NULL;
abc72ce4
DE
21428
21429 if (actual_name != NULL)
21430 {
15d034d0 21431 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21432
21433 if (die_name != NULL
43816ebc 21434 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21435 {
21436 /* Strip off the class name from the full name.
21437 We want the prefix. */
21438 int die_name_len = strlen (die_name);
43816ebc
TT
21439 int actual_name_len = strlen (actual_name.get ());
21440 const char *ptr = actual_name.get ();
abc72ce4
DE
21441
21442 /* Test for '::' as a sanity check. */
21443 if (actual_name_len > die_name_len + 2
43816ebc 21444 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21445 name = obstack_strndup (
e3b94546 21446 &objfile->per_bfd->storage_obstack,
43816ebc 21447 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21448 }
21449 }
abc72ce4
DE
21450 return name;
21451 }
21452 }
21453 }
21454
21455 return NULL;
21456}
21457
96408a79
SA
21458/* GCC might emit a nameless typedef that has a linkage name. Determine the
21459 prefix part in such case. See
21460 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21461
a121b7c1 21462static const char *
96408a79
SA
21463anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21464{
21465 struct attribute *attr;
e6a959d6 21466 const char *base;
96408a79
SA
21467
21468 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21469 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21470 return NULL;
21471
7d45c7c3 21472 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21473 return NULL;
21474
73b9be8b 21475 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21476 if (attr == NULL || DW_STRING (attr) == NULL)
21477 return NULL;
21478
21479 /* dwarf2_name had to be already called. */
21480 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21481
21482 /* Strip the base name, keep any leading namespaces/classes. */
21483 base = strrchr (DW_STRING (attr), ':');
21484 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21485 return "";
21486
518817b3 21487 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
0cf9feb9
TT
21488 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21489 DW_STRING (attr),
21490 &base[-1] - DW_STRING (attr));
96408a79
SA
21491}
21492
fdde2d81 21493/* Return the name of the namespace/class that DIE is defined within,
0114d602 21494 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21495
0114d602
DJ
21496 For example, if we're within the method foo() in the following
21497 code:
21498
21499 namespace N {
21500 class C {
21501 void foo () {
21502 }
21503 };
21504 }
21505
21506 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21507
0d5cff50 21508static const char *
e142c38c 21509determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21510{
518817b3
SM
21511 struct dwarf2_per_objfile *dwarf2_per_objfile
21512 = cu->per_cu->dwarf2_per_objfile;
0114d602
DJ
21513 struct die_info *parent, *spec_die;
21514 struct dwarf2_cu *spec_cu;
21515 struct type *parent_type;
a121b7c1 21516 const char *retval;
63d06c5c 21517
9c37b5ae 21518 if (cu->language != language_cplus
c44af4eb
TT
21519 && cu->language != language_fortran && cu->language != language_d
21520 && cu->language != language_rust)
0114d602
DJ
21521 return "";
21522
96408a79
SA
21523 retval = anonymous_struct_prefix (die, cu);
21524 if (retval)
21525 return retval;
21526
0114d602
DJ
21527 /* We have to be careful in the presence of DW_AT_specification.
21528 For example, with GCC 3.4, given the code
21529
21530 namespace N {
21531 void foo() {
21532 // Definition of N::foo.
21533 }
21534 }
21535
21536 then we'll have a tree of DIEs like this:
21537
21538 1: DW_TAG_compile_unit
21539 2: DW_TAG_namespace // N
21540 3: DW_TAG_subprogram // declaration of N::foo
21541 4: DW_TAG_subprogram // definition of N::foo
21542 DW_AT_specification // refers to die #3
21543
21544 Thus, when processing die #4, we have to pretend that we're in
21545 the context of its DW_AT_specification, namely the contex of die
21546 #3. */
21547 spec_cu = cu;
21548 spec_die = die_specification (die, &spec_cu);
21549 if (spec_die == NULL)
21550 parent = die->parent;
21551 else
63d06c5c 21552 {
0114d602
DJ
21553 parent = spec_die->parent;
21554 cu = spec_cu;
63d06c5c 21555 }
0114d602
DJ
21556
21557 if (parent == NULL)
21558 return "";
98bfdba5
PA
21559 else if (parent->building_fullname)
21560 {
21561 const char *name;
21562 const char *parent_name;
21563
21564 /* It has been seen on RealView 2.2 built binaries,
21565 DW_TAG_template_type_param types actually _defined_ as
21566 children of the parent class:
21567
21568 enum E {};
21569 template class <class Enum> Class{};
21570 Class<enum E> class_e;
21571
21572 1: DW_TAG_class_type (Class)
21573 2: DW_TAG_enumeration_type (E)
21574 3: DW_TAG_enumerator (enum1:0)
21575 3: DW_TAG_enumerator (enum2:1)
21576 ...
21577 2: DW_TAG_template_type_param
21578 DW_AT_type DW_FORM_ref_udata (E)
21579
21580 Besides being broken debug info, it can put GDB into an
21581 infinite loop. Consider:
21582
21583 When we're building the full name for Class<E>, we'll start
21584 at Class, and go look over its template type parameters,
21585 finding E. We'll then try to build the full name of E, and
21586 reach here. We're now trying to build the full name of E,
21587 and look over the parent DIE for containing scope. In the
21588 broken case, if we followed the parent DIE of E, we'd again
21589 find Class, and once again go look at its template type
21590 arguments, etc., etc. Simply don't consider such parent die
21591 as source-level parent of this die (it can't be, the language
21592 doesn't allow it), and break the loop here. */
21593 name = dwarf2_name (die, cu);
21594 parent_name = dwarf2_name (parent, cu);
b98664d3 21595 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21596 name ? name : "<unknown>",
21597 parent_name ? parent_name : "<unknown>");
21598 return "";
21599 }
63d06c5c 21600 else
0114d602
DJ
21601 switch (parent->tag)
21602 {
63d06c5c 21603 case DW_TAG_namespace:
0114d602 21604 parent_type = read_type_die (parent, cu);
acebe513
UW
21605 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21606 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21607 Work around this problem here. */
21608 if (cu->language == language_cplus
e86ca25f 21609 && strcmp (TYPE_NAME (parent_type), "::") == 0)
acebe513 21610 return "";
0114d602 21611 /* We give a name to even anonymous namespaces. */
e86ca25f 21612 return TYPE_NAME (parent_type);
63d06c5c 21613 case DW_TAG_class_type:
680b30c7 21614 case DW_TAG_interface_type:
63d06c5c 21615 case DW_TAG_structure_type:
0114d602 21616 case DW_TAG_union_type:
f55ee35c 21617 case DW_TAG_module:
0114d602 21618 parent_type = read_type_die (parent, cu);
e86ca25f
TT
21619 if (TYPE_NAME (parent_type) != NULL)
21620 return TYPE_NAME (parent_type);
0114d602
DJ
21621 else
21622 /* An anonymous structure is only allowed non-static data
21623 members; no typedefs, no member functions, et cetera.
21624 So it does not need a prefix. */
21625 return "";
abc72ce4 21626 case DW_TAG_compile_unit:
95554aad 21627 case DW_TAG_partial_unit:
abc72ce4
DE
21628 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21629 if (cu->language == language_cplus
fd5866f6 21630 && !dwarf2_per_objfile->types.empty ()
abc72ce4
DE
21631 && die->child != NULL
21632 && (die->tag == DW_TAG_class_type
21633 || die->tag == DW_TAG_structure_type
21634 || die->tag == DW_TAG_union_type))
21635 {
43816ebc 21636 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21637 if (name != NULL)
21638 return name;
21639 }
21640 return "";
0a4b0913
AB
21641 case DW_TAG_subprogram:
21642 /* Nested subroutines in Fortran get a prefix with the name
21643 of the parent's subroutine. */
21644 if (cu->language == language_fortran)
21645 {
21646 if ((die->tag == DW_TAG_subprogram)
21647 && (dwarf2_name (parent, cu) != NULL))
21648 return dwarf2_name (parent, cu);
21649 }
21650 return determine_prefix (parent, cu);
3d567982
TT
21651 case DW_TAG_enumeration_type:
21652 parent_type = read_type_die (parent, cu);
21653 if (TYPE_DECLARED_CLASS (parent_type))
21654 {
e86ca25f
TT
21655 if (TYPE_NAME (parent_type) != NULL)
21656 return TYPE_NAME (parent_type);
3d567982
TT
21657 return "";
21658 }
21659 /* Fall through. */
63d06c5c 21660 default:
8176b9b8 21661 return determine_prefix (parent, cu);
63d06c5c 21662 }
63d06c5c
DC
21663}
21664
3e43a32a
MS
21665/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21666 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21667 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21668 an obconcat, otherwise allocate storage for the result. The CU argument is
21669 used to determine the language and hence, the appropriate separator. */
987504bb 21670
f55ee35c 21671#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21672
21673static char *
f55ee35c
JK
21674typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21675 int physname, struct dwarf2_cu *cu)
63d06c5c 21676{
f55ee35c 21677 const char *lead = "";
5c315b68 21678 const char *sep;
63d06c5c 21679
3e43a32a
MS
21680 if (suffix == NULL || suffix[0] == '\0'
21681 || prefix == NULL || prefix[0] == '\0')
987504bb 21682 sep = "";
45280282
IB
21683 else if (cu->language == language_d)
21684 {
21685 /* For D, the 'main' function could be defined in any module, but it
21686 should never be prefixed. */
21687 if (strcmp (suffix, "D main") == 0)
21688 {
21689 prefix = "";
21690 sep = "";
21691 }
21692 else
21693 sep = ".";
21694 }
f55ee35c
JK
21695 else if (cu->language == language_fortran && physname)
21696 {
21697 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21698 DW_AT_MIPS_linkage_name is preferred and used instead. */
21699
21700 lead = "__";
21701 sep = "_MOD_";
21702 }
987504bb
JJ
21703 else
21704 sep = "::";
63d06c5c 21705
6dd47d34
DE
21706 if (prefix == NULL)
21707 prefix = "";
21708 if (suffix == NULL)
21709 suffix = "";
21710
987504bb
JJ
21711 if (obs == NULL)
21712 {
3e43a32a 21713 char *retval
224c3ddb
SM
21714 = ((char *)
21715 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21716
f55ee35c
JK
21717 strcpy (retval, lead);
21718 strcat (retval, prefix);
6dd47d34
DE
21719 strcat (retval, sep);
21720 strcat (retval, suffix);
63d06c5c
DC
21721 return retval;
21722 }
987504bb
JJ
21723 else
21724 {
21725 /* We have an obstack. */
f55ee35c 21726 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21727 }
63d06c5c
DC
21728}
21729
71c25dea
TT
21730/* Get name of a die, return NULL if not found. */
21731
15d034d0
TT
21732static const char *
21733dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21734 struct objfile *objfile)
71c25dea
TT
21735{
21736 if (name && cu->language == language_cplus)
21737 {
2f408ecb 21738 std::string canon_name = cp_canonicalize_string (name);
71c25dea 21739
2f408ecb 21740 if (!canon_name.empty ())
71c25dea 21741 {
2f408ecb 21742 if (canon_name != name)
be1e3d3e 21743 name = objfile->intern (canon_name);
71c25dea
TT
21744 }
21745 }
21746
21747 return name;
c906108c
SS
21748}
21749
96553a0c
DE
21750/* Get name of a die, return NULL if not found.
21751 Anonymous namespaces are converted to their magic string. */
9219021c 21752
15d034d0 21753static const char *
e142c38c 21754dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21755{
21756 struct attribute *attr;
518817b3 21757 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
9219021c 21758
e142c38c 21759 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21760 if ((!attr || !DW_STRING (attr))
96553a0c 21761 && die->tag != DW_TAG_namespace
53832f31
TT
21762 && die->tag != DW_TAG_class_type
21763 && die->tag != DW_TAG_interface_type
21764 && die->tag != DW_TAG_structure_type
21765 && die->tag != DW_TAG_union_type)
71c25dea
TT
21766 return NULL;
21767
21768 switch (die->tag)
21769 {
21770 case DW_TAG_compile_unit:
95554aad 21771 case DW_TAG_partial_unit:
71c25dea
TT
21772 /* Compilation units have a DW_AT_name that is a filename, not
21773 a source language identifier. */
21774 case DW_TAG_enumeration_type:
21775 case DW_TAG_enumerator:
21776 /* These tags always have simple identifiers already; no need
21777 to canonicalize them. */
21778 return DW_STRING (attr);
907af001 21779
96553a0c
DE
21780 case DW_TAG_namespace:
21781 if (attr != NULL && DW_STRING (attr) != NULL)
21782 return DW_STRING (attr);
21783 return CP_ANONYMOUS_NAMESPACE_STR;
21784
907af001
UW
21785 case DW_TAG_class_type:
21786 case DW_TAG_interface_type:
21787 case DW_TAG_structure_type:
21788 case DW_TAG_union_type:
21789 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21790 structures or unions. These were of the form "._%d" in GCC 4.1,
21791 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21792 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21793 if (attr && DW_STRING (attr)
61012eef
GB
21794 && (startswith (DW_STRING (attr), "._")
21795 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21796 return NULL;
53832f31
TT
21797
21798 /* GCC might emit a nameless typedef that has a linkage name. See
21799 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21800 if (!attr || DW_STRING (attr) == NULL)
21801 {
73b9be8b 21802 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21803 if (attr == NULL || DW_STRING (attr) == NULL)
21804 return NULL;
21805
df5c6c50
JK
21806 /* Avoid demangling DW_STRING (attr) the second time on a second
21807 call for the same DIE. */
21808 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21809 {
43816ebc
TT
21810 gdb::unique_xmalloc_ptr<char> demangled
21811 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21812 if (demangled == nullptr)
21813 return nullptr;
43816ebc 21814
be1e3d3e 21815 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21816 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21817 }
67430cd0
TT
21818
21819 /* Strip any leading namespaces/classes, keep only the base name.
21820 DW_AT_name for named DIEs does not contain the prefixes. */
21821 const char *base = strrchr (DW_STRING (attr), ':');
21822 if (base && base > DW_STRING (attr) && base[-1] == ':')
21823 return &base[1];
21824 else
21825 return DW_STRING (attr);
53832f31 21826 }
907af001
UW
21827 break;
21828
71c25dea 21829 default:
907af001
UW
21830 break;
21831 }
21832
21833 if (!DW_STRING_IS_CANONICAL (attr))
21834 {
be1e3d3e
TT
21835 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21836 objfile);
907af001 21837 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21838 }
907af001 21839 return DW_STRING (attr);
9219021c
DC
21840}
21841
21842/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21843 is none. *EXT_CU is the CU containing DIE on input, and the CU
21844 containing the return value on output. */
9219021c
DC
21845
21846static struct die_info *
f2f0e013 21847dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21848{
21849 struct attribute *attr;
9219021c 21850
f2f0e013 21851 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
21852 if (attr == NULL)
21853 return NULL;
21854
f2f0e013 21855 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
21856}
21857
f9aca02d 21858static void
d97bc12b 21859dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
21860{
21861 unsigned int i;
21862
d97bc12b 21863 print_spaces (indent, f);
9d8780f0 21864 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 21865 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 21866 sect_offset_str (die->sect_off));
d97bc12b
DE
21867
21868 if (die->parent != NULL)
21869 {
21870 print_spaces (indent, f);
9d8780f0
SM
21871 fprintf_unfiltered (f, " parent at offset: %s\n",
21872 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
21873 }
21874
21875 print_spaces (indent, f);
21876 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 21877 dwarf_bool_name (die->child != NULL));
c906108c 21878
d97bc12b
DE
21879 print_spaces (indent, f);
21880 fprintf_unfiltered (f, " attributes:\n");
21881
c906108c
SS
21882 for (i = 0; i < die->num_attrs; ++i)
21883 {
d97bc12b
DE
21884 print_spaces (indent, f);
21885 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
21886 dwarf_attr_name (die->attrs[i].name),
21887 dwarf_form_name (die->attrs[i].form));
d97bc12b 21888
c906108c
SS
21889 switch (die->attrs[i].form)
21890 {
c906108c 21891 case DW_FORM_addr:
336d760d 21892 case DW_FORM_addrx:
3019eac3 21893 case DW_FORM_GNU_addr_index:
d97bc12b 21894 fprintf_unfiltered (f, "address: ");
5af949e3 21895 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
21896 break;
21897 case DW_FORM_block2:
21898 case DW_FORM_block4:
21899 case DW_FORM_block:
21900 case DW_FORM_block1:
56eb65bd
SP
21901 fprintf_unfiltered (f, "block: size %s",
21902 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 21903 break;
2dc7f7b3 21904 case DW_FORM_exprloc:
56eb65bd
SP
21905 fprintf_unfiltered (f, "expression: size %s",
21906 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 21907 break;
0224619f
JK
21908 case DW_FORM_data16:
21909 fprintf_unfiltered (f, "constant of 16 bytes");
21910 break;
4568ecf9
DE
21911 case DW_FORM_ref_addr:
21912 fprintf_unfiltered (f, "ref address: ");
21913 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21914 break;
36586728
TT
21915 case DW_FORM_GNU_ref_alt:
21916 fprintf_unfiltered (f, "alt ref address: ");
21917 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
21918 break;
10b3939b
DJ
21919 case DW_FORM_ref1:
21920 case DW_FORM_ref2:
21921 case DW_FORM_ref4:
4568ecf9
DE
21922 case DW_FORM_ref8:
21923 case DW_FORM_ref_udata:
d97bc12b 21924 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 21925 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 21926 break;
c906108c
SS
21927 case DW_FORM_data1:
21928 case DW_FORM_data2:
21929 case DW_FORM_data4:
ce5d95e1 21930 case DW_FORM_data8:
c906108c
SS
21931 case DW_FORM_udata:
21932 case DW_FORM_sdata:
43bbcdc2
PH
21933 fprintf_unfiltered (f, "constant: %s",
21934 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 21935 break;
2dc7f7b3
TT
21936 case DW_FORM_sec_offset:
21937 fprintf_unfiltered (f, "section offset: %s",
21938 pulongest (DW_UNSND (&die->attrs[i])));
21939 break;
55f1336d 21940 case DW_FORM_ref_sig8:
ac9ec31b
DE
21941 fprintf_unfiltered (f, "signature: %s",
21942 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 21943 break;
c906108c 21944 case DW_FORM_string:
4bdf3d34 21945 case DW_FORM_strp:
43988095 21946 case DW_FORM_line_strp:
cf532bd1 21947 case DW_FORM_strx:
3019eac3 21948 case DW_FORM_GNU_str_index:
36586728 21949 case DW_FORM_GNU_strp_alt:
8285870a 21950 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 21951 DW_STRING (&die->attrs[i])
8285870a
JK
21952 ? DW_STRING (&die->attrs[i]) : "",
21953 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
21954 break;
21955 case DW_FORM_flag:
21956 if (DW_UNSND (&die->attrs[i]))
d97bc12b 21957 fprintf_unfiltered (f, "flag: TRUE");
c906108c 21958 else
d97bc12b 21959 fprintf_unfiltered (f, "flag: FALSE");
c906108c 21960 break;
2dc7f7b3
TT
21961 case DW_FORM_flag_present:
21962 fprintf_unfiltered (f, "flag: TRUE");
21963 break;
a8329558 21964 case DW_FORM_indirect:
0963b4bd
MS
21965 /* The reader will have reduced the indirect form to
21966 the "base form" so this form should not occur. */
5f48f8f3 21967 fprintf_unfiltered (f,
3e43a32a 21968 "unexpected attribute form: DW_FORM_indirect");
a8329558 21969 break;
663c44ac
JK
21970 case DW_FORM_implicit_const:
21971 fprintf_unfiltered (f, "constant: %s",
21972 plongest (DW_SND (&die->attrs[i])));
21973 break;
c906108c 21974 default:
d97bc12b 21975 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 21976 die->attrs[i].form);
d97bc12b 21977 break;
c906108c 21978 }
d97bc12b 21979 fprintf_unfiltered (f, "\n");
c906108c
SS
21980 }
21981}
21982
f9aca02d 21983static void
d97bc12b 21984dump_die_for_error (struct die_info *die)
c906108c 21985{
d97bc12b
DE
21986 dump_die_shallow (gdb_stderr, 0, die);
21987}
21988
21989static void
21990dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
21991{
21992 int indent = level * 4;
21993
21994 gdb_assert (die != NULL);
21995
21996 if (level >= max_level)
21997 return;
21998
21999 dump_die_shallow (f, indent, die);
22000
22001 if (die->child != NULL)
c906108c 22002 {
d97bc12b
DE
22003 print_spaces (indent, f);
22004 fprintf_unfiltered (f, " Children:");
22005 if (level + 1 < max_level)
22006 {
22007 fprintf_unfiltered (f, "\n");
22008 dump_die_1 (f, level + 1, max_level, die->child);
22009 }
22010 else
22011 {
3e43a32a
MS
22012 fprintf_unfiltered (f,
22013 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22014 }
22015 }
22016
22017 if (die->sibling != NULL && level > 0)
22018 {
22019 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22020 }
22021}
22022
d97bc12b
DE
22023/* This is called from the pdie macro in gdbinit.in.
22024 It's not static so gcc will keep a copy callable from gdb. */
22025
22026void
22027dump_die (struct die_info *die, int max_level)
22028{
22029 dump_die_1 (gdb_stdlog, 0, max_level, die);
22030}
22031
f9aca02d 22032static void
51545339 22033store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22034{
51545339 22035 void **slot;
c906108c 22036
9c541725
PA
22037 slot = htab_find_slot_with_hash (cu->die_hash, die,
22038 to_underlying (die->sect_off),
b64f50a1 22039 INSERT);
51545339
DJ
22040
22041 *slot = die;
c906108c
SS
22042}
22043
348e048f
DE
22044/* Follow reference or signature attribute ATTR of SRC_DIE.
22045 On entry *REF_CU is the CU of SRC_DIE.
22046 On exit *REF_CU is the CU of the result. */
22047
22048static struct die_info *
ff39bb5e 22049follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22050 struct dwarf2_cu **ref_cu)
22051{
22052 struct die_info *die;
22053
cd6c91b4 22054 if (attr->form_is_ref ())
348e048f 22055 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22056 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22057 die = follow_die_sig (src_die, attr, ref_cu);
22058 else
22059 {
22060 dump_die_for_error (src_die);
22061 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
518817b3 22062 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
348e048f
DE
22063 }
22064
22065 return die;
03dd20cc
DJ
22066}
22067
5c631832 22068/* Follow reference OFFSET.
673bfd45
DE
22069 On entry *REF_CU is the CU of the source die referencing OFFSET.
22070 On exit *REF_CU is the CU of the result.
22071 Returns NULL if OFFSET is invalid. */
f504f079 22072
f9aca02d 22073static struct die_info *
9c541725 22074follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22075 struct dwarf2_cu **ref_cu)
c906108c 22076{
10b3939b 22077 struct die_info temp_die;
f2f0e013 22078 struct dwarf2_cu *target_cu, *cu = *ref_cu;
518817b3
SM
22079 struct dwarf2_per_objfile *dwarf2_per_objfile
22080 = cu->per_cu->dwarf2_per_objfile;
10b3939b 22081
348e048f
DE
22082 gdb_assert (cu->per_cu != NULL);
22083
98bfdba5
PA
22084 target_cu = cu;
22085
3019eac3 22086 if (cu->per_cu->is_debug_types)
348e048f
DE
22087 {
22088 /* .debug_types CUs cannot reference anything outside their CU.
22089 If they need to, they have to reference a signatured type via
55f1336d 22090 DW_FORM_ref_sig8. */
4057dfde 22091 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22092 return NULL;
348e048f 22093 }
36586728 22094 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22095 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22096 {
22097 struct dwarf2_per_cu_data *per_cu;
9a619af0 22098
9c541725 22099 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22100 dwarf2_per_objfile);
03dd20cc
DJ
22101
22102 /* If necessary, add it to the queue and load its DIEs. */
95554aad 22103 if (maybe_queue_comp_unit (cu, per_cu, cu->language))
58f0c718 22104 load_full_comp_unit (per_cu, false, cu->language);
03dd20cc 22105
10b3939b
DJ
22106 target_cu = per_cu->cu;
22107 }
98bfdba5
PA
22108 else if (cu->dies == NULL)
22109 {
22110 /* We're loading full DIEs during partial symbol reading. */
22111 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
58f0c718 22112 load_full_comp_unit (cu->per_cu, false, language_minimal);
98bfdba5 22113 }
c906108c 22114
f2f0e013 22115 *ref_cu = target_cu;
9c541725 22116 temp_die.sect_off = sect_off;
c24bdb02
KS
22117
22118 if (target_cu != cu)
22119 target_cu->ancestor = cu;
22120
9a3c8263 22121 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22122 &temp_die,
22123 to_underlying (sect_off));
5c631832 22124}
10b3939b 22125
5c631832
JK
22126/* Follow reference attribute ATTR of SRC_DIE.
22127 On entry *REF_CU is the CU of SRC_DIE.
22128 On exit *REF_CU is the CU of the result. */
22129
22130static struct die_info *
ff39bb5e 22131follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22132 struct dwarf2_cu **ref_cu)
22133{
0826b30a 22134 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22135 struct dwarf2_cu *cu = *ref_cu;
22136 struct die_info *die;
22137
9c541725 22138 die = follow_die_offset (sect_off,
36586728
TT
22139 (attr->form == DW_FORM_GNU_ref_alt
22140 || cu->per_cu->is_dwz),
22141 ref_cu);
5c631832 22142 if (!die)
9d8780f0
SM
22143 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22144 "at %s [in module %s]"),
22145 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
518817b3 22146 objfile_name (cu->per_cu->dwarf2_per_objfile->objfile));
348e048f 22147
5c631832
JK
22148 return die;
22149}
22150
d4c9a4f8 22151/* See read.h. */
5c631832
JK
22152
22153struct dwarf2_locexpr_baton
9c541725 22154dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22155 dwarf2_per_cu_data *per_cu,
8b9737bf 22156 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22157 void *baton, bool resolve_abstract_p)
5c631832 22158{
918dd910 22159 struct dwarf2_cu *cu;
5c631832
JK
22160 struct die_info *die;
22161 struct attribute *attr;
22162 struct dwarf2_locexpr_baton retval;
12359b5e
SM
22163 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
22164 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22165
918dd910 22166 if (per_cu->cu == NULL)
58f0c718 22167 load_cu (per_cu, false);
918dd910 22168 cu = per_cu->cu;
cc12ce38
DE
22169 if (cu == NULL)
22170 {
22171 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22172 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22173 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22174 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22175 }
918dd910 22176
9c541725 22177 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22178 if (!die)
9d8780f0
SM
22179 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22180 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22181
22182 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22183 if (!attr && resolve_abstract_p
3360b6e7 22184 && (dwarf2_per_objfile->abstract_to_concrete.find (die->sect_off)
e4a62c65
TV
22185 != dwarf2_per_objfile->abstract_to_concrete.end ()))
22186 {
22187 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22188 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22189 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22190
3360b6e7
TV
22191 for (const auto &cand_off
22192 : dwarf2_per_objfile->abstract_to_concrete[die->sect_off])
e4a62c65 22193 {
3360b6e7
TV
22194 struct dwarf2_cu *cand_cu = cu;
22195 struct die_info *cand
22196 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22197 if (!cand
22198 || !cand->parent
e4a62c65
TV
22199 || cand->parent->tag != DW_TAG_subprogram)
22200 continue;
22201
22202 CORE_ADDR pc_low, pc_high;
22203 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22204 if (pc_low == ((CORE_ADDR) -1))
22205 continue;
22206 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22207 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22208 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22209 continue;
22210
22211 die = cand;
22212 attr = dwarf2_attr (die, DW_AT_location, cu);
22213 break;
22214 }
22215 }
22216
5c631832
JK
22217 if (!attr)
22218 {
e103e986
JK
22219 /* DWARF: "If there is no such attribute, then there is no effect.".
22220 DATA is ignored if SIZE is 0. */
5c631832 22221
e103e986 22222 retval.data = NULL;
5c631832
JK
22223 retval.size = 0;
22224 }
cd6c91b4 22225 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22226 {
22227 struct dwarf2_loclist_baton loclist_baton;
22228 CORE_ADDR pc = (*get_frame_pc) (baton);
22229 size_t size;
22230
22231 fill_in_loclist_baton (cu, &loclist_baton, attr);
22232
22233 retval.data = dwarf2_find_location_expression (&loclist_baton,
22234 &size, pc);
22235 retval.size = size;
22236 }
5c631832
JK
22237 else
22238 {
4fc6c0d5 22239 if (!attr->form_is_block ())
9d8780f0 22240 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22241 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22242 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22243
22244 retval.data = DW_BLOCK (attr)->data;
22245 retval.size = DW_BLOCK (attr)->size;
22246 }
22247 retval.per_cu = cu->per_cu;
918dd910 22248
ed2dc618 22249 age_cached_comp_units (dwarf2_per_objfile);
918dd910 22250
5c631832 22251 return retval;
348e048f
DE
22252}
22253
d4c9a4f8 22254/* See read.h. */
8b9737bf
TT
22255
22256struct dwarf2_locexpr_baton
22257dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22258 dwarf2_per_cu_data *per_cu,
8b9737bf
TT
22259 CORE_ADDR (*get_frame_pc) (void *baton),
22260 void *baton)
22261{
9c541725 22262 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22263
9c541725 22264 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, get_frame_pc, baton);
8b9737bf
TT
22265}
22266
b6807d98
TT
22267/* Write a constant of a given type as target-ordered bytes into
22268 OBSTACK. */
22269
22270static const gdb_byte *
22271write_constant_as_bytes (struct obstack *obstack,
22272 enum bfd_endian byte_order,
22273 struct type *type,
22274 ULONGEST value,
22275 LONGEST *len)
22276{
22277 gdb_byte *result;
22278
22279 *len = TYPE_LENGTH (type);
224c3ddb 22280 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22281 store_unsigned_integer (result, *len, byte_order, value);
22282
22283 return result;
22284}
22285
d4c9a4f8 22286/* See read.h. */
b6807d98
TT
22287
22288const gdb_byte *
9c541725 22289dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8
SM
22290 dwarf2_per_cu_data *per_cu,
22291 obstack *obstack,
b6807d98
TT
22292 LONGEST *len)
22293{
22294 struct dwarf2_cu *cu;
22295 struct die_info *die;
22296 struct attribute *attr;
22297 const gdb_byte *result = NULL;
22298 struct type *type;
22299 LONGEST value;
22300 enum bfd_endian byte_order;
e3b94546 22301 struct objfile *objfile = per_cu->dwarf2_per_objfile->objfile;
b6807d98 22302
b6807d98 22303 if (per_cu->cu == NULL)
58f0c718 22304 load_cu (per_cu, false);
b6807d98 22305 cu = per_cu->cu;
cc12ce38
DE
22306 if (cu == NULL)
22307 {
22308 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22309 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22310 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22311 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22312 }
b6807d98 22313
9c541725 22314 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22315 if (!die)
9d8780f0
SM
22316 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22317 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22318
22319 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22320 if (attr == NULL)
22321 return NULL;
22322
e3b94546 22323 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22324 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22325
22326 switch (attr->form)
22327 {
22328 case DW_FORM_addr:
336d760d 22329 case DW_FORM_addrx:
b6807d98
TT
22330 case DW_FORM_GNU_addr_index:
22331 {
22332 gdb_byte *tem;
22333
22334 *len = cu->header.addr_size;
224c3ddb 22335 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22336 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22337 result = tem;
22338 }
22339 break;
22340 case DW_FORM_string:
22341 case DW_FORM_strp:
cf532bd1 22342 case DW_FORM_strx:
b6807d98
TT
22343 case DW_FORM_GNU_str_index:
22344 case DW_FORM_GNU_strp_alt:
22345 /* DW_STRING is already allocated on the objfile obstack, point
22346 directly to it. */
22347 result = (const gdb_byte *) DW_STRING (attr);
22348 *len = strlen (DW_STRING (attr));
22349 break;
22350 case DW_FORM_block1:
22351 case DW_FORM_block2:
22352 case DW_FORM_block4:
22353 case DW_FORM_block:
22354 case DW_FORM_exprloc:
0224619f 22355 case DW_FORM_data16:
b6807d98
TT
22356 result = DW_BLOCK (attr)->data;
22357 *len = DW_BLOCK (attr)->size;
22358 break;
22359
22360 /* The DW_AT_const_value attributes are supposed to carry the
22361 symbol's value "represented as it would be on the target
22362 architecture." By the time we get here, it's already been
22363 converted to host endianness, so we just need to sign- or
22364 zero-extend it as appropriate. */
22365 case DW_FORM_data1:
22366 type = die_type (die, cu);
22367 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22368 if (result == NULL)
22369 result = write_constant_as_bytes (obstack, byte_order,
22370 type, value, len);
22371 break;
22372 case DW_FORM_data2:
22373 type = die_type (die, cu);
22374 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22375 if (result == NULL)
22376 result = write_constant_as_bytes (obstack, byte_order,
22377 type, value, len);
22378 break;
22379 case DW_FORM_data4:
22380 type = die_type (die, cu);
22381 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22382 if (result == NULL)
22383 result = write_constant_as_bytes (obstack, byte_order,
22384 type, value, len);
22385 break;
22386 case DW_FORM_data8:
22387 type = die_type (die, cu);
22388 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22389 if (result == NULL)
22390 result = write_constant_as_bytes (obstack, byte_order,
22391 type, value, len);
22392 break;
22393
22394 case DW_FORM_sdata:
663c44ac 22395 case DW_FORM_implicit_const:
b6807d98
TT
22396 type = die_type (die, cu);
22397 result = write_constant_as_bytes (obstack, byte_order,
22398 type, DW_SND (attr), len);
22399 break;
22400
22401 case DW_FORM_udata:
22402 type = die_type (die, cu);
22403 result = write_constant_as_bytes (obstack, byte_order,
22404 type, DW_UNSND (attr), len);
22405 break;
22406
22407 default:
b98664d3 22408 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22409 dwarf_form_name (attr->form));
22410 break;
22411 }
22412
22413 return result;
22414}
22415
d4c9a4f8 22416/* See read.h. */
7942e96e
AA
22417
22418struct type *
9c541725 22419dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
d4c9a4f8 22420 dwarf2_per_cu_data *per_cu)
7942e96e
AA
22421{
22422 struct dwarf2_cu *cu;
22423 struct die_info *die;
22424
7942e96e 22425 if (per_cu->cu == NULL)
58f0c718 22426 load_cu (per_cu, false);
7942e96e
AA
22427 cu = per_cu->cu;
22428 if (!cu)
22429 return NULL;
22430
9c541725 22431 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22432 if (!die)
22433 return NULL;
22434
22435 return die_type (die, cu);
22436}
22437
8cb5117c 22438/* See read.h. */
8a9b8146
TT
22439
22440struct type *
b64f50a1 22441dwarf2_get_die_type (cu_offset die_offset,
8a9b8146
TT
22442 struct dwarf2_per_cu_data *per_cu)
22443{
9c541725 22444 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
b64f50a1 22445 return get_die_type_at_offset (die_offset_sect, per_cu);
8a9b8146
TT
22446}
22447
ac9ec31b 22448/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22449 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22450 On exit *REF_CU is the CU of the result.
22451 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22452
22453static struct die_info *
ac9ec31b
DE
22454follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22455 struct dwarf2_cu **ref_cu)
348e048f 22456{
348e048f 22457 struct die_info temp_die;
c24bdb02 22458 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f
DE
22459 struct die_info *die;
22460
ac9ec31b
DE
22461 /* While it might be nice to assert sig_type->type == NULL here,
22462 we can get here for DW_AT_imported_declaration where we need
22463 the DIE not the type. */
348e048f
DE
22464
22465 /* If necessary, add it to the queue and load its DIEs. */
22466
95554aad 22467 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, language_minimal))
a0f42c21 22468 read_signatured_type (sig_type);
348e048f 22469
348e048f 22470 sig_cu = sig_type->per_cu.cu;
69d751e3 22471 gdb_assert (sig_cu != NULL);
9c541725
PA
22472 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22473 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22474 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22475 to_underlying (temp_die.sect_off));
348e048f
DE
22476 if (die)
22477 {
ed2dc618 22478 struct dwarf2_per_objfile *dwarf2_per_objfile
518817b3 22479 = (*ref_cu)->per_cu->dwarf2_per_objfile;
ed2dc618 22480
796a7ff8
DE
22481 /* For .gdb_index version 7 keep track of included TUs.
22482 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
22483 if (dwarf2_per_objfile->index_table != NULL
22484 && dwarf2_per_objfile->index_table->version <= 7)
22485 {
ae640021 22486 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22487 }
22488
348e048f 22489 *ref_cu = sig_cu;
c24bdb02
KS
22490 if (sig_cu != cu)
22491 sig_cu->ancestor = cu;
22492
348e048f
DE
22493 return die;
22494 }
22495
ac9ec31b
DE
22496 return NULL;
22497}
22498
22499/* Follow signatured type referenced by ATTR in SRC_DIE.
22500 On entry *REF_CU is the CU of SRC_DIE.
22501 On exit *REF_CU is the CU of the result.
22502 The result is the DIE of the type.
22503 If the referenced type cannot be found an error is thrown. */
22504
22505static struct die_info *
ff39bb5e 22506follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22507 struct dwarf2_cu **ref_cu)
22508{
22509 ULONGEST signature = DW_SIGNATURE (attr);
22510 struct signatured_type *sig_type;
22511 struct die_info *die;
22512
22513 gdb_assert (attr->form == DW_FORM_ref_sig8);
22514
a2ce51a0 22515 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22516 /* sig_type will be NULL if the signatured type is missing from
22517 the debug info. */
22518 if (sig_type == NULL)
22519 {
22520 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22521 " from DIE at %s [in module %s]"),
22522 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22523 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22524 }
22525
22526 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22527 if (die == NULL)
22528 {
22529 dump_die_for_error (src_die);
22530 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22531 " from DIE at %s [in module %s]"),
22532 hex_string (signature), sect_offset_str (src_die->sect_off),
518817b3 22533 objfile_name ((*ref_cu)->per_cu->dwarf2_per_objfile->objfile));
ac9ec31b
DE
22534 }
22535
22536 return die;
22537}
22538
22539/* Get the type specified by SIGNATURE referenced in DIE/CU,
22540 reading in and processing the type unit if necessary. */
22541
22542static struct type *
22543get_signatured_type (struct die_info *die, ULONGEST signature,
22544 struct dwarf2_cu *cu)
22545{
518817b3
SM
22546 struct dwarf2_per_objfile *dwarf2_per_objfile
22547 = cu->per_cu->dwarf2_per_objfile;
ac9ec31b
DE
22548 struct signatured_type *sig_type;
22549 struct dwarf2_cu *type_cu;
22550 struct die_info *type_die;
22551 struct type *type;
22552
a2ce51a0 22553 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22554 /* sig_type will be NULL if the signatured type is missing from
22555 the debug info. */
22556 if (sig_type == NULL)
22557 {
b98664d3 22558 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22559 " from DIE at %s [in module %s]"),
22560 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22561 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22562 return build_error_marker_type (cu, die);
22563 }
22564
22565 /* If we already know the type we're done. */
22566 if (sig_type->type != NULL)
22567 return sig_type->type;
22568
22569 type_cu = cu;
22570 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22571 if (type_die != NULL)
22572 {
22573 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22574 is created. This is important, for example, because for c++ classes
22575 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22576 type = read_type_die (type_die, type_cu);
22577 if (type == NULL)
22578 {
b98664d3 22579 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22580 " referenced from DIE at %s [in module %s]"),
22581 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22582 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22583 type = build_error_marker_type (cu, die);
22584 }
22585 }
22586 else
22587 {
b98664d3 22588 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22589 " from DIE at %s [in module %s]"),
22590 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22591 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22592 type = build_error_marker_type (cu, die);
22593 }
22594 sig_type->type = type;
22595
22596 return type;
22597}
22598
22599/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22600 reading in and processing the type unit if necessary. */
22601
22602static struct type *
ff39bb5e 22603get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22604 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22605{
22606 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22607 if (attr->form_is_ref ())
ac9ec31b
DE
22608 {
22609 struct dwarf2_cu *type_cu = cu;
22610 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22611
22612 return read_type_die (type_die, type_cu);
22613 }
22614 else if (attr->form == DW_FORM_ref_sig8)
22615 {
22616 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22617 }
22618 else
22619 {
518817b3
SM
22620 struct dwarf2_per_objfile *dwarf2_per_objfile
22621 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 22622
b98664d3 22623 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22624 " at %s [in module %s]"),
22625 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22626 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22627 return build_error_marker_type (cu, die);
22628 }
348e048f
DE
22629}
22630
e5fe5e75 22631/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22632
22633static void
e5fe5e75 22634load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
348e048f 22635{
52dc124a 22636 struct signatured_type *sig_type;
348e048f 22637
f4dc4d17 22638 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22639 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22640
6721b2ec
DE
22641 /* We have the per_cu, but we need the signatured_type.
22642 Fortunately this is an easy translation. */
22643 gdb_assert (per_cu->is_debug_types);
22644 sig_type = (struct signatured_type *) per_cu;
348e048f 22645
6721b2ec 22646 gdb_assert (per_cu->cu == NULL);
348e048f 22647
52dc124a 22648 read_signatured_type (sig_type);
348e048f 22649
6721b2ec 22650 gdb_assert (per_cu->cu != NULL);
348e048f
DE
22651}
22652
3019eac3
DE
22653/* Read in a signatured type and build its CU and DIEs.
22654 If the type is a stub for the real type in a DWO file,
22655 read in the real type from the DWO file as well. */
dee91e82
DE
22656
22657static void
22658read_signatured_type (struct signatured_type *sig_type)
22659{
22660 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22661
3019eac3 22662 gdb_assert (per_cu->is_debug_types);
dee91e82 22663 gdb_assert (per_cu->cu == NULL);
348e048f 22664
6751ebae 22665 cutu_reader reader (per_cu, NULL, 0, false);
c0ab21c2
TT
22666
22667 if (!reader.dummy_p)
22668 {
22669 struct dwarf2_cu *cu = reader.cu;
22670 const gdb_byte *info_ptr = reader.info_ptr;
22671
22672 gdb_assert (cu->die_hash == NULL);
22673 cu->die_hash =
22674 htab_create_alloc_ex (cu->header.length / 12,
22675 die_hash,
22676 die_eq,
22677 NULL,
22678 &cu->comp_unit_obstack,
22679 hashtab_obstack_allocate,
22680 dummy_obstack_deallocate);
22681
3e225074 22682 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22683 reader.comp_unit_die->child
22684 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22685 reader.comp_unit_die);
22686 cu->dies = reader.comp_unit_die;
22687 /* comp_unit_die is not stored in die_hash, no need. */
22688
22689 /* We try not to read any attributes in this function, because
22690 not all CUs needed for references have been loaded yet, and
22691 symbol table processing isn't initialized. But we have to
22692 set the CU language, or we won't be able to build types
22693 correctly. Similarly, if we do not read the producer, we can
22694 not apply producer-specific interpretation. */
22695 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22696
22697 reader.keep ();
c0ab21c2
TT
22698 }
22699
7ee85ab1 22700 sig_type->per_cu.tu_read = 1;
c906108c
SS
22701}
22702
c906108c
SS
22703/* Decode simple location descriptions.
22704 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22705 the location and return the value. If COMPUTED is non-null, it is
22706 set to true to indicate that decoding was successful, and false
22707 otherwise. If COMPUTED is null, then this function may emit a
22708 complaint. */
c906108c
SS
22709
22710static CORE_ADDR
7d79de9a 22711decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22712{
518817b3 22713 struct objfile *objfile = cu->per_cu->dwarf2_per_objfile->objfile;
56eb65bd
SP
22714 size_t i;
22715 size_t size = blk->size;
d521ce57 22716 const gdb_byte *data = blk->data;
21ae7a4d
JK
22717 CORE_ADDR stack[64];
22718 int stacki;
22719 unsigned int bytes_read, unsnd;
22720 gdb_byte op;
c906108c 22721
7d79de9a
TT
22722 if (computed != nullptr)
22723 *computed = false;
22724
21ae7a4d
JK
22725 i = 0;
22726 stacki = 0;
22727 stack[stacki] = 0;
22728 stack[++stacki] = 0;
22729
22730 while (i < size)
22731 {
22732 op = data[i++];
22733 switch (op)
22734 {
22735 case DW_OP_lit0:
22736 case DW_OP_lit1:
22737 case DW_OP_lit2:
22738 case DW_OP_lit3:
22739 case DW_OP_lit4:
22740 case DW_OP_lit5:
22741 case DW_OP_lit6:
22742 case DW_OP_lit7:
22743 case DW_OP_lit8:
22744 case DW_OP_lit9:
22745 case DW_OP_lit10:
22746 case DW_OP_lit11:
22747 case DW_OP_lit12:
22748 case DW_OP_lit13:
22749 case DW_OP_lit14:
22750 case DW_OP_lit15:
22751 case DW_OP_lit16:
22752 case DW_OP_lit17:
22753 case DW_OP_lit18:
22754 case DW_OP_lit19:
22755 case DW_OP_lit20:
22756 case DW_OP_lit21:
22757 case DW_OP_lit22:
22758 case DW_OP_lit23:
22759 case DW_OP_lit24:
22760 case DW_OP_lit25:
22761 case DW_OP_lit26:
22762 case DW_OP_lit27:
22763 case DW_OP_lit28:
22764 case DW_OP_lit29:
22765 case DW_OP_lit30:
22766 case DW_OP_lit31:
22767 stack[++stacki] = op - DW_OP_lit0;
22768 break;
f1bea926 22769
21ae7a4d
JK
22770 case DW_OP_reg0:
22771 case DW_OP_reg1:
22772 case DW_OP_reg2:
22773 case DW_OP_reg3:
22774 case DW_OP_reg4:
22775 case DW_OP_reg5:
22776 case DW_OP_reg6:
22777 case DW_OP_reg7:
22778 case DW_OP_reg8:
22779 case DW_OP_reg9:
22780 case DW_OP_reg10:
22781 case DW_OP_reg11:
22782 case DW_OP_reg12:
22783 case DW_OP_reg13:
22784 case DW_OP_reg14:
22785 case DW_OP_reg15:
22786 case DW_OP_reg16:
22787 case DW_OP_reg17:
22788 case DW_OP_reg18:
22789 case DW_OP_reg19:
22790 case DW_OP_reg20:
22791 case DW_OP_reg21:
22792 case DW_OP_reg22:
22793 case DW_OP_reg23:
22794 case DW_OP_reg24:
22795 case DW_OP_reg25:
22796 case DW_OP_reg26:
22797 case DW_OP_reg27:
22798 case DW_OP_reg28:
22799 case DW_OP_reg29:
22800 case DW_OP_reg30:
22801 case DW_OP_reg31:
22802 stack[++stacki] = op - DW_OP_reg0;
22803 if (i < size)
7d79de9a
TT
22804 {
22805 if (computed == nullptr)
22806 dwarf2_complex_location_expr_complaint ();
22807 else
22808 return 0;
22809 }
21ae7a4d 22810 break;
c906108c 22811
21ae7a4d
JK
22812 case DW_OP_regx:
22813 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22814 i += bytes_read;
22815 stack[++stacki] = unsnd;
22816 if (i < size)
7d79de9a
TT
22817 {
22818 if (computed == nullptr)
22819 dwarf2_complex_location_expr_complaint ();
22820 else
22821 return 0;
22822 }
21ae7a4d 22823 break;
c906108c 22824
21ae7a4d 22825 case DW_OP_addr:
c8a7a66f
TT
22826 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22827 &bytes_read);
21ae7a4d
JK
22828 i += bytes_read;
22829 break;
d53d4ac5 22830
21ae7a4d
JK
22831 case DW_OP_const1u:
22832 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22833 i += 1;
22834 break;
22835
22836 case DW_OP_const1s:
22837 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22838 i += 1;
22839 break;
22840
22841 case DW_OP_const2u:
22842 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
22843 i += 2;
22844 break;
22845
22846 case DW_OP_const2s:
22847 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
22848 i += 2;
22849 break;
d53d4ac5 22850
21ae7a4d
JK
22851 case DW_OP_const4u:
22852 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
22853 i += 4;
22854 break;
22855
22856 case DW_OP_const4s:
22857 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
22858 i += 4;
22859 break;
22860
585861ea
JK
22861 case DW_OP_const8u:
22862 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
22863 i += 8;
22864 break;
22865
21ae7a4d
JK
22866 case DW_OP_constu:
22867 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
22868 &bytes_read);
22869 i += bytes_read;
22870 break;
22871
22872 case DW_OP_consts:
22873 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
22874 i += bytes_read;
22875 break;
22876
22877 case DW_OP_dup:
22878 stack[stacki + 1] = stack[stacki];
22879 stacki++;
22880 break;
22881
22882 case DW_OP_plus:
22883 stack[stacki - 1] += stack[stacki];
22884 stacki--;
22885 break;
22886
22887 case DW_OP_plus_uconst:
22888 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
22889 &bytes_read);
22890 i += bytes_read;
22891 break;
22892
22893 case DW_OP_minus:
22894 stack[stacki - 1] -= stack[stacki];
22895 stacki--;
22896 break;
22897
22898 case DW_OP_deref:
22899 /* If we're not the last op, then we definitely can't encode
22900 this using GDB's address_class enum. This is valid for partial
22901 global symbols, although the variable's address will be bogus
22902 in the psymtab. */
22903 if (i < size)
7d79de9a
TT
22904 {
22905 if (computed == nullptr)
22906 dwarf2_complex_location_expr_complaint ();
22907 else
22908 return 0;
22909 }
21ae7a4d
JK
22910 break;
22911
22912 case DW_OP_GNU_push_tls_address:
4aa4e28b 22913 case DW_OP_form_tls_address:
21ae7a4d
JK
22914 /* The top of the stack has the offset from the beginning
22915 of the thread control block at which the variable is located. */
22916 /* Nothing should follow this operator, so the top of stack would
22917 be returned. */
22918 /* This is valid for partial global symbols, but the variable's
585861ea
JK
22919 address will be bogus in the psymtab. Make it always at least
22920 non-zero to not look as a variable garbage collected by linker
22921 which have DW_OP_addr 0. */
21ae7a4d 22922 if (i < size)
7d79de9a
TT
22923 {
22924 if (computed == nullptr)
22925 dwarf2_complex_location_expr_complaint ();
22926 else
22927 return 0;
22928 }
585861ea 22929 stack[stacki]++;
21ae7a4d
JK
22930 break;
22931
22932 case DW_OP_GNU_uninit:
7d79de9a
TT
22933 if (computed != nullptr)
22934 return 0;
21ae7a4d
JK
22935 break;
22936
336d760d 22937 case DW_OP_addrx:
3019eac3 22938 case DW_OP_GNU_addr_index:
49f6c839 22939 case DW_OP_GNU_const_index:
3019eac3
DE
22940 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
22941 &bytes_read);
22942 i += bytes_read;
22943 break;
22944
21ae7a4d 22945 default:
7d79de9a
TT
22946 if (computed == nullptr)
22947 {
22948 const char *name = get_DW_OP_name (op);
21ae7a4d 22949
7d79de9a
TT
22950 if (name)
22951 complaint (_("unsupported stack op: '%s'"),
22952 name);
22953 else
22954 complaint (_("unsupported stack op: '%02x'"),
22955 op);
22956 }
21ae7a4d
JK
22957
22958 return (stack[stacki]);
d53d4ac5 22959 }
3c6e0cb3 22960
21ae7a4d
JK
22961 /* Enforce maximum stack depth of SIZE-1 to avoid writing
22962 outside of the allocated space. Also enforce minimum>0. */
22963 if (stacki >= ARRAY_SIZE (stack) - 1)
22964 {
7d79de9a
TT
22965 if (computed == nullptr)
22966 complaint (_("location description stack overflow"));
21ae7a4d
JK
22967 return 0;
22968 }
22969
22970 if (stacki <= 0)
22971 {
7d79de9a
TT
22972 if (computed == nullptr)
22973 complaint (_("location description stack underflow"));
21ae7a4d
JK
22974 return 0;
22975 }
22976 }
7d79de9a
TT
22977
22978 if (computed != nullptr)
22979 *computed = true;
21ae7a4d 22980 return (stack[stacki]);
c906108c
SS
22981}
22982
22983/* memory allocation interface */
22984
c906108c 22985static struct dwarf_block *
7b5a2f43 22986dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 22987{
8d749320 22988 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
22989}
22990
c906108c 22991static struct die_info *
b60c80d6 22992dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
22993{
22994 struct die_info *die;
b60c80d6
DJ
22995 size_t size = sizeof (struct die_info);
22996
22997 if (num_attrs > 1)
22998 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 22999
b60c80d6 23000 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23001 memset (die, 0, sizeof (struct die_info));
23002 return (die);
23003}
2e276125
JB
23004
23005\f
a036ba48 23006
c90ec28a 23007/* Macro support. */
cf2c3c16 23008
9eac9650
TT
23009/* An overload of dwarf_decode_macros that finds the correct section
23010 and ensures it is read in before calling the other overload. */
23011
23012static void
23013dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23014 int section_is_gnu)
23015{
23016 struct dwarf2_per_objfile *dwarf2_per_objfile
23017 = cu->per_cu->dwarf2_per_objfile;
23018 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23019 const struct line_header *lh = cu->line_header;
9eac9650
TT
23020 unsigned int offset_size = cu->header.offset_size;
23021 struct dwarf2_section_info *section;
23022 const char *section_name;
23023
23024 if (cu->dwo_unit != nullptr)
23025 {
23026 if (section_is_gnu)
23027 {
23028 section = &cu->dwo_unit->dwo_file->sections.macro;
23029 section_name = ".debug_macro.dwo";
23030 }
23031 else
23032 {
23033 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23034 section_name = ".debug_macinfo.dwo";
23035 }
23036 }
23037 else
23038 {
23039 if (section_is_gnu)
23040 {
23041 section = &dwarf2_per_objfile->macro;
23042 section_name = ".debug_macro";
23043 }
23044 else
23045 {
23046 section = &dwarf2_per_objfile->macinfo;
23047 section_name = ".debug_macinfo";
23048 }
23049 }
23050
23051 section->read (objfile);
23052 if (section->buffer == nullptr)
23053 {
23054 complaint (_("missing %s section"), section_name);
23055 return;
23056 }
23057
23058 buildsym_compunit *builder = cu->get_builder ();
23059
23060 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23061 offset_size, offset, section_is_gnu);
23062}
23063
3019eac3
DE
23064/* Return the .debug_loc section to use for CU.
23065 For DWO files use .debug_loc.dwo. */
23066
23067static struct dwarf2_section_info *
23068cu_debug_loc_section (struct dwarf2_cu *cu)
23069{
518817b3
SM
23070 struct dwarf2_per_objfile *dwarf2_per_objfile
23071 = cu->per_cu->dwarf2_per_objfile;
ed2dc618 23072
3019eac3 23073 if (cu->dwo_unit)
43988095
JK
23074 {
23075 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23076
43988095
JK
23077 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23078 }
23079 return (cu->header.version >= 5 ? &dwarf2_per_objfile->loclists
23080 : &dwarf2_per_objfile->loc);
3019eac3
DE
23081}
23082
8cf6f0b1
TT
23083/* A helper function that fills in a dwarf2_loclist_baton. */
23084
23085static void
23086fill_in_loclist_baton (struct dwarf2_cu *cu,
23087 struct dwarf2_loclist_baton *baton,
ff39bb5e 23088 const struct attribute *attr)
8cf6f0b1 23089{
518817b3
SM
23090 struct dwarf2_per_objfile *dwarf2_per_objfile
23091 = cu->per_cu->dwarf2_per_objfile;
3019eac3
DE
23092 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23093
96b79293 23094 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1
TT
23095
23096 baton->per_cu = cu->per_cu;
23097 gdb_assert (baton->per_cu);
23098 /* We don't know how long the location list is, but make sure we
23099 don't run off the edge of the section. */
3019eac3
DE
23100 baton->size = section->size - DW_UNSND (attr);
23101 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23102 if (cu->base_address.has_value ())
23103 baton->base_address = *cu->base_address;
23104 else
23105 baton->base_address = 0;
f664829e 23106 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23107}
23108
4c2df51b 23109static void
ff39bb5e 23110dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23111 struct dwarf2_cu *cu, int is_block)
4c2df51b 23112{
518817b3
SM
23113 struct dwarf2_per_objfile *dwarf2_per_objfile
23114 = cu->per_cu->dwarf2_per_objfile;
bb5ed363 23115 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23116 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23117
cd6c91b4 23118 if (attr->form_is_section_offset ()
3019eac3 23119 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23120 the section. If so, fall through to the complaint in the
23121 other branch. */
2c7d5afc 23122 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23123 {
0d53c4c4 23124 struct dwarf2_loclist_baton *baton;
4c2df51b 23125
8d749320 23126 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23127
8cf6f0b1 23128 fill_in_loclist_baton (cu, baton, attr);
be391dca 23129
2b24b6e4 23130 if (!cu->base_address.has_value ())
b98664d3 23131 complaint (_("Location list used without "
3e43a32a 23132 "specifying the CU base address."));
4c2df51b 23133
f1e6e072
TT
23134 SYMBOL_ACLASS_INDEX (sym) = (is_block
23135 ? dwarf2_loclist_block_index
23136 : dwarf2_loclist_index);
0d53c4c4
DJ
23137 SYMBOL_LOCATION_BATON (sym) = baton;
23138 }
23139 else
23140 {
23141 struct dwarf2_locexpr_baton *baton;
23142
8d749320 23143 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
ae0d2f24
UW
23144 baton->per_cu = cu->per_cu;
23145 gdb_assert (baton->per_cu);
0d53c4c4 23146
4fc6c0d5 23147 if (attr->form_is_block ())
0d53c4c4
DJ
23148 {
23149 /* Note that we're just copying the block's data pointer
23150 here, not the actual data. We're still pointing into the
6502dd73
DJ
23151 info_buffer for SYM's objfile; right now we never release
23152 that buffer, but when we do clean up properly this may
23153 need to change. */
0d53c4c4
DJ
23154 baton->size = DW_BLOCK (attr)->size;
23155 baton->data = DW_BLOCK (attr)->data;
23156 }
23157 else
23158 {
23159 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23160 sym->natural_name ());
0d53c4c4 23161 baton->size = 0;
0d53c4c4 23162 }
6e70227d 23163
f1e6e072
TT
23164 SYMBOL_ACLASS_INDEX (sym) = (is_block
23165 ? dwarf2_locexpr_block_index
23166 : dwarf2_locexpr_index);
0d53c4c4
DJ
23167 SYMBOL_LOCATION_BATON (sym) = baton;
23168 }
4c2df51b 23169}
6502dd73 23170
09ba997f 23171/* See read.h. */
ae0d2f24
UW
23172
23173struct objfile *
09ba997f 23174dwarf2_per_cu_data::objfile () const
ae0d2f24 23175{
09ba997f 23176 struct objfile *objfile = dwarf2_per_objfile->objfile;
ae0d2f24
UW
23177
23178 /* Return the master objfile, so that we can report and look up the
23179 correct file containing this variable. */
23180 if (objfile->separate_debug_objfile_backlink)
23181 objfile = objfile->separate_debug_objfile_backlink;
23182
23183 return objfile;
23184}
23185
96408a79
SA
23186/* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
23187 (CU_HEADERP is unused in such case) or prepare a temporary copy at
23188 CU_HEADERP first. */
23189
23190static const struct comp_unit_head *
23191per_cu_header_read_in (struct comp_unit_head *cu_headerp,
09ba997f 23192 const struct dwarf2_per_cu_data *per_cu)
96408a79 23193{
d521ce57 23194 const gdb_byte *info_ptr;
96408a79
SA
23195
23196 if (per_cu->cu)
23197 return &per_cu->cu->header;
23198
9c541725 23199 info_ptr = per_cu->section->buffer + to_underlying (per_cu->sect_off);
96408a79
SA
23200
23201 memset (cu_headerp, 0, sizeof (*cu_headerp));
43988095
JK
23202 read_comp_unit_head (cu_headerp, info_ptr, per_cu->section,
23203 rcuh_kind::COMPILE);
96408a79
SA
23204
23205 return cu_headerp;
23206}
23207
09ba997f 23208/* See read.h. */
ae0d2f24 23209
98714339 23210int
09ba997f 23211dwarf2_per_cu_data::addr_size () const
ae0d2f24 23212{
96408a79
SA
23213 struct comp_unit_head cu_header_local;
23214 const struct comp_unit_head *cu_headerp;
c471e790 23215
09ba997f 23216 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23217
23218 return cu_headerp->addr_size;
ae0d2f24
UW
23219}
23220
09ba997f 23221/* See read.h. */
9eae7c52
TT
23222
23223int
09ba997f 23224dwarf2_per_cu_data::offset_size () const
9eae7c52 23225{
96408a79
SA
23226 struct comp_unit_head cu_header_local;
23227 const struct comp_unit_head *cu_headerp;
9c6c53f7 23228
09ba997f 23229 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23230
23231 return cu_headerp->offset_size;
23232}
23233
09ba997f 23234/* See read.h. */
96408a79
SA
23235
23236int
09ba997f 23237dwarf2_per_cu_data::ref_addr_size () const
96408a79
SA
23238{
23239 struct comp_unit_head cu_header_local;
23240 const struct comp_unit_head *cu_headerp;
23241
09ba997f 23242 cu_headerp = per_cu_header_read_in (&cu_header_local, this);
96408a79
SA
23243
23244 if (cu_headerp->version == 2)
23245 return cu_headerp->addr_size;
23246 else
23247 return cu_headerp->offset_size;
181cebd4
JK
23248}
23249
09ba997f 23250/* See read.h. */
9aa1f1e3
TT
23251
23252CORE_ADDR
09ba997f 23253dwarf2_per_cu_data::text_offset () const
9aa1f1e3 23254{
09ba997f
TT
23255 struct objfile *objfile = dwarf2_per_objfile->objfile;
23256
23257 return objfile->text_section_offset ();
9aa1f1e3
TT
23258}
23259
09ba997f
TT
23260/* See read.h. */
23261
23262struct type *
23263dwarf2_per_cu_data::addr_type () const
9a49df9d 23264{
09ba997f 23265 struct objfile *objfile = dwarf2_per_objfile->objfile;
9a49df9d
AB
23266 struct type *void_type = objfile_type (objfile)->builtin_void;
23267 struct type *addr_type = lookup_pointer_type (void_type);
09ba997f 23268 int addr_size = this->addr_size ();
9a49df9d
AB
23269
23270 if (TYPE_LENGTH (addr_type) == addr_size)
23271 return addr_type;
23272
09ba997f 23273 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23274 return addr_type;
23275}
23276
22b6cd70
TT
23277/* A helper function for dwarf2_find_containing_comp_unit that returns
23278 the index of the result, and that searches a vector. It will
23279 return a result even if the offset in question does not actually
23280 occur in any CU. This is separate so that it can be unit
23281 tested. */
ae038cb0 23282
22b6cd70
TT
23283static int
23284dwarf2_find_containing_comp_unit
23285 (sect_offset sect_off,
23286 unsigned int offset_in_dwz,
23287 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23288{
ae038cb0
DJ
23289 int low, high;
23290
ae038cb0 23291 low = 0;
22b6cd70 23292 high = all_comp_units.size () - 1;
ae038cb0
DJ
23293 while (high > low)
23294 {
36586728 23295 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23296 int mid = low + (high - low) / 2;
9a619af0 23297
22b6cd70 23298 mid_cu = all_comp_units[mid];
36586728 23299 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23300 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23301 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23302 high = mid;
23303 else
23304 low = mid + 1;
23305 }
23306 gdb_assert (low == high);
22b6cd70
TT
23307 return low;
23308}
23309
23310/* Locate the .debug_info compilation unit from CU's objfile which contains
23311 the DIE at OFFSET. Raises an error on failure. */
23312
23313static struct dwarf2_per_cu_data *
23314dwarf2_find_containing_comp_unit (sect_offset sect_off,
23315 unsigned int offset_in_dwz,
23316 struct dwarf2_per_objfile *dwarf2_per_objfile)
23317{
23318 int low
23319 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
23320 dwarf2_per_objfile->all_comp_units);
23321 struct dwarf2_per_cu_data *this_cu
23322 = dwarf2_per_objfile->all_comp_units[low];
23323
45b8ae0c 23324 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23325 {
36586728 23326 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23327 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23328 "offset %s [in module %s]"),
23329 sect_offset_str (sect_off),
ed2dc618 23330 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23331
9c541725
PA
23332 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->sect_off
23333 <= sect_off);
ae038cb0
DJ
23334 return dwarf2_per_objfile->all_comp_units[low-1];
23335 }
23336 else
23337 {
b76e467d 23338 if (low == dwarf2_per_objfile->all_comp_units.size () - 1
9c541725 23339 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23340 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23341 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23342 return this_cu;
23343 }
23344}
23345
22b6cd70
TT
23346#if GDB_SELF_TEST
23347
23348namespace selftests {
23349namespace find_containing_comp_unit {
23350
23351static void
23352run_test ()
23353{
23354 struct dwarf2_per_cu_data one {};
23355 struct dwarf2_per_cu_data two {};
23356 struct dwarf2_per_cu_data three {};
23357 struct dwarf2_per_cu_data four {};
23358
23359 one.length = 5;
23360 two.sect_off = sect_offset (one.length);
23361 two.length = 7;
23362
23363 three.length = 5;
23364 three.is_dwz = 1;
23365 four.sect_off = sect_offset (three.length);
23366 four.length = 7;
23367 four.is_dwz = 1;
23368
23369 std::vector<dwarf2_per_cu_data *> units;
23370 units.push_back (&one);
23371 units.push_back (&two);
23372 units.push_back (&three);
23373 units.push_back (&four);
23374
23375 int result;
23376
23377 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23378 SELF_CHECK (units[result] == &one);
23379 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23380 SELF_CHECK (units[result] == &one);
23381 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23382 SELF_CHECK (units[result] == &two);
23383
23384 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23385 SELF_CHECK (units[result] == &three);
23386 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23387 SELF_CHECK (units[result] == &three);
23388 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23389 SELF_CHECK (units[result] == &four);
23390}
23391
23392}
23393}
23394
23395#endif /* GDB_SELF_TEST */
23396
23745b47 23397/* Initialize dwarf2_cu CU, owned by PER_CU. */
93311388 23398
fcd3b13d
SM
23399dwarf2_cu::dwarf2_cu (struct dwarf2_per_cu_data *per_cu_)
23400 : per_cu (per_cu_),
9068261f
AB
23401 mark (false),
23402 has_loclist (false),
23403 checked_producer (false),
23404 producer_is_gxx_lt_4_6 (false),
23405 producer_is_gcc_lt_4_3 (false),
eb77c9df 23406 producer_is_icc (false),
9068261f 23407 producer_is_icc_lt_14 (false),
c258c396 23408 producer_is_codewarrior (false),
9068261f 23409 processing_has_namespace_info (false)
93311388 23410{
fcd3b13d
SM
23411 per_cu->cu = this;
23412}
23413
23414/* Destroy a dwarf2_cu. */
23415
23416dwarf2_cu::~dwarf2_cu ()
23417{
23418 per_cu->cu = NULL;
9816fde3
JK
23419}
23420
23421/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23422
23423static void
95554aad
TT
23424prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23425 enum language pretend_language)
9816fde3
JK
23426{
23427 struct attribute *attr;
23428
23429 /* Set the language we're debugging. */
23430 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23431 if (attr != nullptr)
9816fde3
JK
23432 set_cu_language (DW_UNSND (attr), cu);
23433 else
9cded63f 23434 {
95554aad 23435 cu->language = pretend_language;
9cded63f
TT
23436 cu->language_defn = language_def (cu->language);
23437 }
dee91e82 23438
7d45c7c3 23439 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23440}
23441
ae038cb0
DJ
23442/* Increase the age counter on each cached compilation unit, and free
23443 any that are too old. */
23444
23445static void
ed2dc618 23446age_cached_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
ae038cb0
DJ
23447{
23448 struct dwarf2_per_cu_data *per_cu, **last_chain;
23449
23450 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
23451 per_cu = dwarf2_per_objfile->read_in_chain;
23452 while (per_cu != NULL)
23453 {
23454 per_cu->cu->last_used ++;
b4f54984 23455 if (per_cu->cu->last_used <= dwarf_max_cache_age)
ae038cb0
DJ
23456 dwarf2_mark (per_cu->cu);
23457 per_cu = per_cu->cu->read_in_chain;
23458 }
23459
23460 per_cu = dwarf2_per_objfile->read_in_chain;
23461 last_chain = &dwarf2_per_objfile->read_in_chain;
23462 while (per_cu != NULL)
23463 {
23464 struct dwarf2_per_cu_data *next_cu;
23465
23466 next_cu = per_cu->cu->read_in_chain;
23467
23468 if (!per_cu->cu->mark)
23469 {
fcd3b13d 23470 delete per_cu->cu;
ae038cb0
DJ
23471 *last_chain = next_cu;
23472 }
23473 else
23474 last_chain = &per_cu->cu->read_in_chain;
23475
23476 per_cu = next_cu;
23477 }
23478}
23479
23480/* Remove a single compilation unit from the cache. */
23481
23482static void
dee91e82 23483free_one_cached_comp_unit (struct dwarf2_per_cu_data *target_per_cu)
ae038cb0
DJ
23484{
23485 struct dwarf2_per_cu_data *per_cu, **last_chain;
ed2dc618
SM
23486 struct dwarf2_per_objfile *dwarf2_per_objfile
23487 = target_per_cu->dwarf2_per_objfile;
ae038cb0
DJ
23488
23489 per_cu = dwarf2_per_objfile->read_in_chain;
23490 last_chain = &dwarf2_per_objfile->read_in_chain;
23491 while (per_cu != NULL)
23492 {
23493 struct dwarf2_per_cu_data *next_cu;
23494
23495 next_cu = per_cu->cu->read_in_chain;
23496
dee91e82 23497 if (per_cu == target_per_cu)
ae038cb0 23498 {
fcd3b13d 23499 delete per_cu->cu;
dee91e82 23500 per_cu->cu = NULL;
ae038cb0
DJ
23501 *last_chain = next_cu;
23502 break;
23503 }
23504 else
23505 last_chain = &per_cu->cu->read_in_chain;
23506
23507 per_cu = next_cu;
23508 }
23509}
23510
dee91e82
DE
23511/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23512 We store these in a hash table separate from the DIEs, and preserve them
23513 when the DIEs are flushed out of cache.
23514
23515 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23516 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23517 or the type may come from a DWO file. Furthermore, while it's more logical
23518 to use per_cu->section+offset, with Fission the section with the data is in
23519 the DWO file but we don't know that section at the point we need it.
23520 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23521 because we can enter the lookup routine, get_die_type_at_offset, from
23522 outside this file, and thus won't necessarily have PER_CU->cu.
23523 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23524
dee91e82 23525struct dwarf2_per_cu_offset_and_type
1c379e20 23526{
dee91e82 23527 const struct dwarf2_per_cu_data *per_cu;
9c541725 23528 sect_offset sect_off;
1c379e20
DJ
23529 struct type *type;
23530};
23531
dee91e82 23532/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23533
23534static hashval_t
dee91e82 23535per_cu_offset_and_type_hash (const void *item)
1c379e20 23536{
9a3c8263
SM
23537 const struct dwarf2_per_cu_offset_and_type *ofs
23538 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23539
9c541725 23540 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23541}
23542
dee91e82 23543/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23544
23545static int
dee91e82 23546per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23547{
9a3c8263
SM
23548 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23549 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23550 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23551 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23552
dee91e82 23553 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23554 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23555}
23556
23557/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23558 table if necessary. For convenience, return TYPE.
23559
23560 The DIEs reading must have careful ordering to:
85102364 23561 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23562 reading current DIE.
23563 * Not trying to dereference contents of still incompletely read in types
23564 while reading in other DIEs.
23565 * Enable referencing still incompletely read in types just by a pointer to
23566 the type without accessing its fields.
23567
23568 Therefore caller should follow these rules:
23569 * Try to fetch any prerequisite types we may need to build this DIE type
23570 before building the type and calling set_die_type.
e71ec853 23571 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23572 possible before fetching more types to complete the current type.
23573 * Make the type as complete as possible before fetching more types. */
1c379e20 23574
f792889a 23575static struct type *
1c379e20
DJ
23576set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23577{
518817b3
SM
23578 struct dwarf2_per_objfile *dwarf2_per_objfile
23579 = cu->per_cu->dwarf2_per_objfile;
dee91e82 23580 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23581 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23582 struct attribute *attr;
23583 struct dynamic_prop prop;
1c379e20 23584
b4ba55a1
JB
23585 /* For Ada types, make sure that the gnat-specific data is always
23586 initialized (if not already set). There are a few types where
23587 we should not be doing so, because the type-specific area is
23588 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23589 where the type-specific area is used to store the floatformat).
23590 But this is not a problem, because the gnat-specific information
23591 is actually not needed for these types. */
23592 if (need_gnat_info (cu)
23593 && TYPE_CODE (type) != TYPE_CODE_FUNC
23594 && TYPE_CODE (type) != TYPE_CODE_FLT
09e2d7c7
DE
23595 && TYPE_CODE (type) != TYPE_CODE_METHODPTR
23596 && TYPE_CODE (type) != TYPE_CODE_MEMBERPTR
23597 && TYPE_CODE (type) != TYPE_CODE_METHOD
b4ba55a1
JB
23598 && !HAVE_GNAT_AUX_INFO (type))
23599 INIT_GNAT_SPECIFIC (type);
23600
3f2f83dd
KB
23601 /* Read DW_AT_allocated and set in type. */
23602 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23603 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23604 {
09ba997f 23605 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23606 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23607 add_dyn_prop (DYN_PROP_ALLOCATED, prop, type);
3f2f83dd
KB
23608 }
23609 else if (attr != NULL)
23610 {
b98664d3 23611 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23612 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23613 sect_offset_str (die->sect_off));
3f2f83dd
KB
23614 }
23615
23616 /* Read DW_AT_associated and set in type. */
23617 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23618 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23619 {
09ba997f 23620 struct type *prop_type = cu->per_cu->addr_sized_int_type (false);
9a49df9d 23621 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
50a82047 23622 add_dyn_prop (DYN_PROP_ASSOCIATED, prop, type);
3f2f83dd
KB
23623 }
23624 else if (attr != NULL)
23625 {
b98664d3 23626 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23627 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23628 sect_offset_str (die->sect_off));
3f2f83dd
KB
23629 }
23630
3cdcd0ce
JB
23631 /* Read DW_AT_data_location and set in type. */
23632 attr = dwarf2_attr (die, DW_AT_data_location, cu);
9a49df9d 23633 if (attr_to_dynamic_prop (attr, die, cu, &prop,
09ba997f 23634 cu->per_cu->addr_type ()))
50a82047 23635 add_dyn_prop (DYN_PROP_DATA_LOCATION, prop, type);
3cdcd0ce 23636
dee91e82 23637 if (dwarf2_per_objfile->die_type_hash == NULL)
0335378b
TT
23638 dwarf2_per_objfile->die_type_hash
23639 = htab_up (htab_create_alloc (127,
23640 per_cu_offset_and_type_hash,
23641 per_cu_offset_and_type_eq,
23642 NULL, xcalloc, xfree));
1c379e20 23643
dee91e82 23644 ofs.per_cu = cu->per_cu;
9c541725 23645 ofs.sect_off = die->sect_off;
1c379e20 23646 ofs.type = type;
dee91e82 23647 slot = (struct dwarf2_per_cu_offset_and_type **)
0335378b 23648 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23649 if (*slot)
b98664d3 23650 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23651 sect_offset_str (die->sect_off));
8d749320
SM
23652 *slot = XOBNEW (&objfile->objfile_obstack,
23653 struct dwarf2_per_cu_offset_and_type);
1c379e20 23654 **slot = ofs;
f792889a 23655 return type;
1c379e20
DJ
23656}
23657
9c541725 23658/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23659 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23660
23661static struct type *
9c541725 23662get_die_type_at_offset (sect_offset sect_off,
673bfd45 23663 struct dwarf2_per_cu_data *per_cu)
1c379e20 23664{
dee91e82 23665 struct dwarf2_per_cu_offset_and_type *slot, ofs;
ed2dc618 23666 struct dwarf2_per_objfile *dwarf2_per_objfile = per_cu->dwarf2_per_objfile;
f792889a 23667
dee91e82 23668 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23669 return NULL;
1c379e20 23670
dee91e82 23671 ofs.per_cu = per_cu;
9c541725 23672 ofs.sect_off = sect_off;
9a3c8263 23673 slot = ((struct dwarf2_per_cu_offset_and_type *)
0335378b 23674 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23675 if (slot)
23676 return slot->type;
23677 else
23678 return NULL;
23679}
23680
02142a6c 23681/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23682 or return NULL if DIE does not have a saved type. */
23683
23684static struct type *
23685get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23686{
9c541725 23687 return get_die_type_at_offset (die->sect_off, cu->per_cu);
673bfd45
DE
23688}
23689
10b3939b
DJ
23690/* Add a dependence relationship from CU to REF_PER_CU. */
23691
23692static void
23693dwarf2_add_dependence (struct dwarf2_cu *cu,
23694 struct dwarf2_per_cu_data *ref_per_cu)
23695{
23696 void **slot;
23697
23698 if (cu->dependencies == NULL)
23699 cu->dependencies
23700 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23701 NULL, &cu->comp_unit_obstack,
23702 hashtab_obstack_allocate,
23703 dummy_obstack_deallocate);
23704
23705 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23706 if (*slot == NULL)
23707 *slot = ref_per_cu;
23708}
1c379e20 23709
f504f079
DE
23710/* Subroutine of dwarf2_mark to pass to htab_traverse.
23711 Set the mark field in every compilation unit in the
ae038cb0
DJ
23712 cache that we must keep because we are keeping CU. */
23713
10b3939b
DJ
23714static int
23715dwarf2_mark_helper (void **slot, void *data)
23716{
23717 struct dwarf2_per_cu_data *per_cu;
23718
23719 per_cu = (struct dwarf2_per_cu_data *) *slot;
d07ed419
JK
23720
23721 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23722 reading of the chain. As such dependencies remain valid it is not much
23723 useful to track and undo them during QUIT cleanups. */
23724 if (per_cu->cu == NULL)
23725 return 1;
23726
10b3939b
DJ
23727 if (per_cu->cu->mark)
23728 return 1;
9068261f 23729 per_cu->cu->mark = true;
10b3939b
DJ
23730
23731 if (per_cu->cu->dependencies != NULL)
23732 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
23733
23734 return 1;
23735}
23736
f504f079
DE
23737/* Set the mark field in CU and in every other compilation unit in the
23738 cache that we must keep because we are keeping CU. */
23739
ae038cb0
DJ
23740static void
23741dwarf2_mark (struct dwarf2_cu *cu)
23742{
23743 if (cu->mark)
23744 return;
9068261f 23745 cu->mark = true;
10b3939b
DJ
23746 if (cu->dependencies != NULL)
23747 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
ae038cb0
DJ
23748}
23749
23750static void
23751dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
23752{
23753 while (per_cu)
23754 {
9068261f 23755 per_cu->cu->mark = false;
ae038cb0
DJ
23756 per_cu = per_cu->cu->read_in_chain;
23757 }
72bf9492
DJ
23758}
23759
72bf9492
DJ
23760/* Trivial hash function for partial_die_info: the hash value of a DIE
23761 is its offset in .debug_info for this objfile. */
23762
23763static hashval_t
23764partial_die_hash (const void *item)
23765{
9a3c8263
SM
23766 const struct partial_die_info *part_die
23767 = (const struct partial_die_info *) item;
9a619af0 23768
9c541725 23769 return to_underlying (part_die->sect_off);
72bf9492
DJ
23770}
23771
23772/* Trivial comparison function for partial_die_info structures: two DIEs
23773 are equal if they have the same offset. */
23774
23775static int
23776partial_die_eq (const void *item_lhs, const void *item_rhs)
23777{
9a3c8263
SM
23778 const struct partial_die_info *part_die_lhs
23779 = (const struct partial_die_info *) item_lhs;
23780 const struct partial_die_info *part_die_rhs
23781 = (const struct partial_die_info *) item_rhs;
9a619af0 23782
9c541725 23783 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23784}
23785
3c3bb058
AB
23786struct cmd_list_element *set_dwarf_cmdlist;
23787struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23788
9291a0cd 23789static void
cd4fb1b2
SM
23790show_check_physname (struct ui_file *file, int from_tty,
23791 struct cmd_list_element *c, const char *value)
9291a0cd 23792{
cd4fb1b2
SM
23793 fprintf_filtered (file,
23794 _("Whether to check \"physname\" is %s.\n"),
23795 value);
9291a0cd
TT
23796}
23797
6c265988 23798void _initialize_dwarf2_read ();
cd4fb1b2 23799void
6c265988 23800_initialize_dwarf2_read ()
9291a0cd 23801{
0743fc83 23802 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23803Set DWARF specific variables.\n\
590042fc 23804Configure DWARF variables such as the cache size."),
0743fc83
TT
23805 &set_dwarf_cmdlist, "maintenance set dwarf ",
23806 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23807
0743fc83 23808 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23809Show DWARF specific variables.\n\
23810Show DWARF variables such as the cache size."),
0743fc83
TT
23811 &show_dwarf_cmdlist, "maintenance show dwarf ",
23812 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23813
cd4fb1b2
SM
23814 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23815 &dwarf_max_cache_age, _("\
23816Set the upper bound on the age of cached DWARF compilation units."), _("\
23817Show the upper bound on the age of cached DWARF compilation units."), _("\
23818A higher limit means that cached compilation units will be stored\n\
23819in memory longer, and more total memory will be used. Zero disables\n\
23820caching, which can slow down startup."),
23821 NULL,
23822 show_dwarf_max_cache_age,
23823 &set_dwarf_cmdlist,
23824 &show_dwarf_cmdlist);
156942c7 23825
cd4fb1b2
SM
23826 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23827Set debugging of the DWARF reader."), _("\
23828Show debugging of the DWARF reader."), _("\
23829When enabled (non-zero), debugging messages are printed during DWARF\n\
23830reading and symtab expansion. A value of 1 (one) provides basic\n\
23831information. A value greater than 1 provides more verbose information."),
23832 NULL,
23833 NULL,
23834 &setdebuglist, &showdebuglist);
9291a0cd 23835
cd4fb1b2
SM
23836 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23837Set debugging of the DWARF DIE reader."), _("\
23838Show debugging of the DWARF DIE reader."), _("\
23839When enabled (non-zero), DIEs are dumped after they are read in.\n\
23840The value is the maximum depth to print."),
23841 NULL,
23842 NULL,
23843 &setdebuglist, &showdebuglist);
9291a0cd 23844
cd4fb1b2
SM
23845 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23846Set debugging of the dwarf line reader."), _("\
23847Show debugging of the dwarf line reader."), _("\
23848When enabled (non-zero), line number entries are dumped as they are read in.\n\
23849A value of 1 (one) provides basic information.\n\
23850A value greater than 1 provides more verbose information."),
23851 NULL,
23852 NULL,
23853 &setdebuglist, &showdebuglist);
437afbb8 23854
cd4fb1b2
SM
23855 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23856Set cross-checking of \"physname\" code against demangler."), _("\
23857Show cross-checking of \"physname\" code against demangler."), _("\
23858When enabled, GDB's internal \"physname\" code is checked against\n\
23859the demangler."),
23860 NULL, show_check_physname,
23861 &setdebuglist, &showdebuglist);
900e11f9 23862
e615022a
DE
23863 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23864 no_class, &use_deprecated_index_sections, _("\
23865Set whether to use deprecated gdb_index sections."), _("\
23866Show whether to use deprecated gdb_index sections."), _("\
23867When enabled, deprecated .gdb_index sections are used anyway.\n\
23868Normally they are ignored either because of a missing feature or\n\
23869performance issue.\n\
23870Warning: This option must be enabled before gdb reads the file."),
23871 NULL,
23872 NULL,
23873 &setlist, &showlist);
23874
f1e6e072
TT
23875 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23876 &dwarf2_locexpr_funcs);
23877 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23878 &dwarf2_loclist_funcs);
23879
23880 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23881 &dwarf2_block_frame_base_locexpr_funcs);
23882 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23883 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23884
23885#if GDB_SELF_TEST
23886 selftests::register_test ("dw2_expand_symtabs_matching",
23887 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23888 selftests::register_test ("dwarf2_find_containing_comp_unit",
23889 selftests::find_containing_comp_unit::run_test);
c62446b1 23890#endif
6502dd73 23891}
This page took 5.316667 seconds and 4 git commands to generate.