Replace dwarf2_per_cu_data::cu backlink with per-objfile map
[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{
9e021579
SM
402 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
403 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
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
293e7e51
SM
423 /* Return a type that is a generic pointer type, the size of which
424 matches the address size given in the compilation unit header for
425 this CU. */
426 struct type *addr_type () const;
427
428 /* Find an integer type the same size as the address size given in
429 the compilation unit header for this CU. UNSIGNED_P controls if
430 the integer is unsigned or not. */
431 struct type *addr_sized_int_type (bool unsigned_p) const;
432
d00adf39 433 /* The header of the compilation unit. */
fcd3b13d 434 struct comp_unit_head header {};
e142c38c 435
d00adf39 436 /* Base address of this compilation unit. */
2b24b6e4 437 gdb::optional<CORE_ADDR> base_address;
d00adf39 438
e142c38c 439 /* The language we are debugging. */
fcd3b13d
SM
440 enum language language = language_unknown;
441 const struct language_defn *language_defn = nullptr;
e142c38c 442
fcd3b13d 443 const char *producer = nullptr;
b0f35d58 444
c24bdb02 445private:
804d2729
TT
446 /* The symtab builder for this CU. This is only non-NULL when full
447 symbols are being read. */
c24bdb02 448 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 449
c24bdb02 450public:
e142c38c
DJ
451 /* The generic symbol table building routines have separate lists for
452 file scope symbols and all all other scopes (local scopes). So
453 we need to select the right one to pass to add_symbol_to_list().
454 We do it by keeping a pointer to the correct list in list_in_scope.
455
456 FIXME: The original dwarf code just treated the file scope as the
457 first local scope, and all other local scopes as nested local
458 scopes, and worked fine. Check to see if we really need to
459 distinguish these in buildsym.c. */
fcd3b13d 460 struct pending **list_in_scope = nullptr;
e142c38c 461
b64f50a1
JK
462 /* Hash table holding all the loaded partial DIEs
463 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 464 htab_t partial_dies = nullptr;
72bf9492
DJ
465
466 /* Storage for things with the same lifetime as this read-in compilation
467 unit, including partial DIEs. */
fcd3b13d 468 auto_obstack comp_unit_obstack;
72bf9492 469
69d751e3 470 /* Backlink to our per_cu entry. */
ae038cb0
DJ
471 struct dwarf2_per_cu_data *per_cu;
472
9e021579
SM
473 /* The dwarf2_per_objfile that owns this. */
474 struct dwarf2_per_objfile *per_objfile;
475
ae038cb0 476 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 477 int last_used = 0;
ae038cb0 478
b64f50a1
JK
479 /* A hash table of DIE cu_offset for following references with
480 die_info->offset.sect_off as hash. */
fcd3b13d 481 htab_t die_hash = nullptr;
10b3939b
DJ
482
483 /* Full DIEs if read in. */
fcd3b13d 484 struct die_info *dies = nullptr;
10b3939b
DJ
485
486 /* A set of pointers to dwarf2_per_cu_data objects for compilation
487 units referenced by this one. Only set during full symbol processing;
488 partial symbol tables do not have dependencies. */
fcd3b13d 489 htab_t dependencies = nullptr;
10b3939b 490
cb1df416 491 /* Header data from the line table, during full symbol processing. */
fcd3b13d 492 struct line_header *line_header = nullptr;
4c8aa72d 493 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 494 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
495 this is the DW_TAG_compile_unit die for this CU. We'll hold on
496 to the line header as long as this DIE is being processed. See
497 process_die_scope. */
fcd3b13d 498 die_info *line_header_die_owner = nullptr;
cb1df416 499
3da10d80
KS
500 /* A list of methods which need to have physnames computed
501 after all type information has been read. */
c89b44cd 502 std::vector<delayed_method_info> method_list;
3da10d80 503
96408a79 504 /* To be copied to symtab->call_site_htab. */
fcd3b13d 505 htab_t call_site_htab = nullptr;
96408a79 506
034e5797
DE
507 /* Non-NULL if this CU came from a DWO file.
508 There is an invariant here that is important to remember:
509 Except for attributes copied from the top level DIE in the "main"
510 (or "stub") file in preparation for reading the DWO file
18a8505e 511 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
512 Either there isn't a DWO file (in which case this is NULL and the point
513 is moot), or there is and either we're not going to read it (in which
514 case this is NULL) or there is and we are reading it (in which case this
515 is non-NULL). */
fcd3b13d 516 struct dwo_unit *dwo_unit = nullptr;
3019eac3 517
18a8505e 518 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 519 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 520 gdb::optional<ULONGEST> addr_base;
3019eac3 521
18a8505e 522 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 523 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 524 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
525 be used without needing to know whether DWO files are in use or not.
526 N.B. This does not apply to DW_AT_ranges appearing in
527 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
528 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 529 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 530 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 531 ULONGEST ranges_base = 0;
2e3cf129 532
41144253 533 /* The DW_AT_loclists_base attribute if present. */
534 ULONGEST loclist_base = 0;
535
c9317f21
TT
536 /* When reading debug info generated by older versions of rustc, we
537 have to rewrite some union types to be struct types with a
538 variant part. This rewriting must be done after the CU is fully
539 read in, because otherwise at the point of rewriting some struct
540 type might not have been fully processed. So, we keep a list of
541 all such types here and process them after expansion. */
542 std::vector<struct type *> rust_unions;
543
18a8505e
AT
544 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
545 files, the value is implicitly zero. For DWARF 5 version DWO files, the
546 value is often implicit and is the size of the header of
547 .debug_str_offsets section (8 or 4, depending on the address size). */
548 gdb::optional<ULONGEST> str_offsets_base;
549
ae038cb0 550 /* Mark used when releasing cached dies. */
9068261f 551 bool mark : 1;
ae038cb0 552
8be455d7
JK
553 /* This CU references .debug_loc. See the symtab->locations_valid field.
554 This test is imperfect as there may exist optimized debug code not using
555 any location list and still facing inlining issues if handled as
556 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 557 bool has_loclist : 1;
ba919b58 558
9068261f 559 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
560 if all the producer_is_* fields are valid. This information is cached
561 because profiling CU expansion showed excessive time spent in
562 producer_is_gxx_lt_4_6. */
9068261f
AB
563 bool checked_producer : 1;
564 bool producer_is_gxx_lt_4_6 : 1;
565 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 566 bool producer_is_icc : 1;
9068261f 567 bool producer_is_icc_lt_14 : 1;
c258c396 568 bool producer_is_codewarrior : 1;
4d4ec4e5 569
9068261f 570 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
571 debugging info for C++ namespaces. GCC 3.3.x did not produce
572 this information, but later versions do. */
573
9068261f 574 bool processing_has_namespace_info : 1;
d590ff25
YQ
575
576 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
577
578 /* If this CU was inherited by another CU (via specification,
579 abstract_origin, etc), this is the ancestor CU. */
580 dwarf2_cu *ancestor;
581
582 /* Get the buildsym_compunit for this CU. */
583 buildsym_compunit *get_builder ()
584 {
585 /* If this CU has a builder associated with it, use that. */
586 if (m_builder != nullptr)
587 return m_builder.get ();
588
589 /* Otherwise, search ancestors for a valid builder. */
590 if (ancestor != nullptr)
591 return ancestor->get_builder ();
592
593 return nullptr;
594 }
e7c27a73
DJ
595};
596
094b34ac
DE
597/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
598 This includes type_unit_group and quick_file_names. */
599
600struct stmt_list_hash
601{
602 /* The DWO unit this table is from or NULL if there is none. */
603 struct dwo_unit *dwo_unit;
604
605 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 606 sect_offset line_sect_off;
094b34ac
DE
607};
608
5989a64e 609/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
610 an object of this type. This contains elements of type unit groups
611 that can be shared across objfiles. The non-shareable parts are in
612 type_unit_group_unshareable. */
f4dc4d17
DE
613
614struct type_unit_group
615{
0186c6a7 616 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
617 To simplify things we create an artificial CU that "includes" all the
618 type units using this stmt_list so that the rest of the code still has
197400e8 619 a "per_cu" handle on the symtab. */
094b34ac
DE
620 struct dwarf2_per_cu_data per_cu;
621
0186c6a7
DE
622 /* The TUs that share this DW_AT_stmt_list entry.
623 This is added to while parsing type units to build partial symtabs,
624 and is deleted afterwards and not used again. */
a8b3b8e9 625 std::vector<signatured_type *> *tus;
f4dc4d17 626
094b34ac
DE
627 /* The data used to construct the hash key. */
628 struct stmt_list_hash hash;
f4dc4d17
DE
629};
630
73869dc2 631/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
632
633struct dwo_sections
634{
635 struct dwarf2_section_info abbrev;
3019eac3
DE
636 struct dwarf2_section_info line;
637 struct dwarf2_section_info loc;
43988095 638 struct dwarf2_section_info loclists;
09262596
DE
639 struct dwarf2_section_info macinfo;
640 struct dwarf2_section_info macro;
3019eac3
DE
641 struct dwarf2_section_info str;
642 struct dwarf2_section_info str_offsets;
80626a55
DE
643 /* In the case of a virtual DWO file, these two are unused. */
644 struct dwarf2_section_info info;
fd5866f6 645 std::vector<dwarf2_section_info> types;
3019eac3
DE
646};
647
c88ee1f0 648/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
649
650struct dwo_unit
651{
652 /* Backlink to the containing struct dwo_file. */
653 struct dwo_file *dwo_file;
654
655 /* The "id" that distinguishes this CU/TU.
656 .debug_info calls this "dwo_id", .debug_types calls this "signature".
657 Since signatures came first, we stick with it for consistency. */
658 ULONGEST signature;
659
660 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 661 struct dwarf2_section_info *section;
3019eac3 662
9c541725
PA
663 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
664 sect_offset sect_off;
3019eac3
DE
665 unsigned int length;
666
667 /* For types, offset in the type's DIE of the type defined by this TU. */
668 cu_offset type_offset_in_tu;
669};
670
73869dc2
DE
671/* include/dwarf2.h defines the DWP section codes.
672 It defines a max value but it doesn't define a min value, which we
673 use for error checking, so provide one. */
674
675enum dwp_v2_section_ids
676{
677 DW_SECT_MIN = 1
678};
679
80626a55 680/* Data for one DWO file.
57d63ce2
DE
681
682 This includes virtual DWO files (a virtual DWO file is a DWO file as it
683 appears in a DWP file). DWP files don't really have DWO files per se -
684 comdat folding of types "loses" the DWO file they came from, and from
685 a high level view DWP files appear to contain a mass of random types.
686 However, to maintain consistency with the non-DWP case we pretend DWP
687 files contain virtual DWO files, and we assign each TU with one virtual
688 DWO file (generally based on the line and abbrev section offsets -
689 a heuristic that seems to work in practice). */
3019eac3
DE
690
691struct dwo_file
692{
51ac9db5
SM
693 dwo_file () = default;
694 DISABLE_COPY_AND_ASSIGN (dwo_file);
695
18a8505e 696 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
697 For virtual DWO files the name is constructed from the section offsets
698 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
699 from related CU+TUs. */
51ac9db5 700 const char *dwo_name = nullptr;
0ac5b59e
DE
701
702 /* The DW_AT_comp_dir attribute. */
51ac9db5 703 const char *comp_dir = nullptr;
3019eac3 704
80626a55
DE
705 /* The bfd, when the file is open. Otherwise this is NULL.
706 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 707 gdb_bfd_ref_ptr dbfd;
3019eac3 708
73869dc2
DE
709 /* The sections that make up this DWO file.
710 Remember that for virtual DWO files in DWP V2, these are virtual
711 sections (for lack of a better name). */
51ac9db5 712 struct dwo_sections sections {};
3019eac3 713
33c5cd75
DB
714 /* The CUs in the file.
715 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
716 an extension to handle LLVM's Link Time Optimization output (where
717 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 718 htab_up cus;
3019eac3
DE
719
720 /* Table of TUs in the file.
721 Each element is a struct dwo_unit. */
b0b6a987 722 htab_up tus;
3019eac3
DE
723};
724
80626a55
DE
725/* These sections are what may appear in a DWP file. */
726
727struct dwp_sections
728{
73869dc2 729 /* These are used by both DWP version 1 and 2. */
80626a55
DE
730 struct dwarf2_section_info str;
731 struct dwarf2_section_info cu_index;
732 struct dwarf2_section_info tu_index;
73869dc2
DE
733
734 /* These are only used by DWP version 2 files.
735 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
736 sections are referenced by section number, and are not recorded here.
737 In DWP version 2 there is at most one copy of all these sections, each
738 section being (effectively) comprised of the concatenation of all of the
739 individual sections that exist in the version 1 format.
740 To keep the code simple we treat each of these concatenated pieces as a
741 section itself (a virtual section?). */
742 struct dwarf2_section_info abbrev;
743 struct dwarf2_section_info info;
744 struct dwarf2_section_info line;
745 struct dwarf2_section_info loc;
746 struct dwarf2_section_info macinfo;
747 struct dwarf2_section_info macro;
748 struct dwarf2_section_info str_offsets;
749 struct dwarf2_section_info types;
80626a55
DE
750};
751
73869dc2
DE
752/* These sections are what may appear in a virtual DWO file in DWP version 1.
753 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 754
73869dc2 755struct virtual_v1_dwo_sections
80626a55
DE
756{
757 struct dwarf2_section_info abbrev;
758 struct dwarf2_section_info line;
759 struct dwarf2_section_info loc;
760 struct dwarf2_section_info macinfo;
761 struct dwarf2_section_info macro;
762 struct dwarf2_section_info str_offsets;
763 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 764 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
765 struct dwarf2_section_info info_or_types;
766};
767
73869dc2
DE
768/* Similar to virtual_v1_dwo_sections, but for DWP version 2.
769 In version 2, the sections of the DWO files are concatenated together
770 and stored in one section of that name. Thus each ELF section contains
771 several "virtual" sections. */
772
773struct virtual_v2_dwo_sections
774{
775 bfd_size_type abbrev_offset;
776 bfd_size_type abbrev_size;
777
778 bfd_size_type line_offset;
779 bfd_size_type line_size;
780
781 bfd_size_type loc_offset;
782 bfd_size_type loc_size;
783
784 bfd_size_type macinfo_offset;
785 bfd_size_type macinfo_size;
786
787 bfd_size_type macro_offset;
788 bfd_size_type macro_size;
789
790 bfd_size_type str_offsets_offset;
791 bfd_size_type str_offsets_size;
792
793 /* Each DWP hash table entry records one CU or one TU.
794 That is recorded here, and copied to dwo_unit.section. */
795 bfd_size_type info_or_types_offset;
796 bfd_size_type info_or_types_size;
797};
798
80626a55
DE
799/* Contents of DWP hash tables. */
800
801struct dwp_hash_table
802{
73869dc2 803 uint32_t version, nr_columns;
80626a55 804 uint32_t nr_units, nr_slots;
73869dc2
DE
805 const gdb_byte *hash_table, *unit_table;
806 union
807 {
808 struct
809 {
810 const gdb_byte *indices;
811 } v1;
812 struct
813 {
814 /* This is indexed by column number and gives the id of the section
815 in that column. */
816#define MAX_NR_V2_DWO_SECTIONS \
817 (1 /* .debug_info or .debug_types */ \
818 + 1 /* .debug_abbrev */ \
819 + 1 /* .debug_line */ \
820 + 1 /* .debug_loc */ \
821 + 1 /* .debug_str_offsets */ \
822 + 1 /* .debug_macro or .debug_macinfo */)
823 int section_ids[MAX_NR_V2_DWO_SECTIONS];
824 const gdb_byte *offsets;
825 const gdb_byte *sizes;
826 } v2;
827 } section_pool;
80626a55
DE
828};
829
830/* Data for one DWP file. */
831
832struct dwp_file
833{
400174b1
TT
834 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
835 : name (name_),
836 dbfd (std::move (abfd))
837 {
838 }
839
80626a55
DE
840 /* Name of the file. */
841 const char *name;
842
73869dc2 843 /* File format version. */
400174b1 844 int version = 0;
73869dc2 845
93417882 846 /* The bfd. */
400174b1 847 gdb_bfd_ref_ptr dbfd;
80626a55
DE
848
849 /* Section info for this file. */
400174b1 850 struct dwp_sections sections {};
80626a55 851
57d63ce2 852 /* Table of CUs in the file. */
400174b1 853 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
854
855 /* Table of TUs in the file. */
400174b1 856 const struct dwp_hash_table *tus = nullptr;
80626a55 857
19ac8c2e 858 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
859 htab_up loaded_cus;
860 htab_up loaded_tus;
80626a55 861
73869dc2
DE
862 /* Table to map ELF section numbers to their sections.
863 This is only needed for the DWP V1 file format. */
400174b1
TT
864 unsigned int num_sections = 0;
865 asection **elf_sections = nullptr;
80626a55
DE
866};
867
0963b4bd
MS
868/* Struct used to pass misc. parameters to read_die_and_children, et
869 al. which are used for both .debug_info and .debug_types dies.
870 All parameters here are unchanging for the life of the call. This
dee91e82 871 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
872
873struct die_reader_specs
874{
a32a8923 875 /* The bfd of die_section. */
93311388
DE
876 bfd* abfd;
877
878 /* The CU of the DIE we are parsing. */
879 struct dwarf2_cu *cu;
880
80626a55 881 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
882 struct dwo_file *dwo_file;
883
dee91e82 884 /* The section the die comes from.
3019eac3 885 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
886 struct dwarf2_section_info *die_section;
887
888 /* die_section->buffer. */
d521ce57 889 const gdb_byte *buffer;
f664829e
DE
890
891 /* The end of the buffer. */
892 const gdb_byte *buffer_end;
a2ce51a0 893
685af9cd
TT
894 /* The abbreviation table to use when reading the DIEs. */
895 struct abbrev_table *abbrev_table;
93311388
DE
896};
897
c0ab21c2
TT
898/* A subclass of die_reader_specs that holds storage and has complex
899 constructor and destructor behavior. */
900
901class cutu_reader : public die_reader_specs
902{
903public:
904
ab432490
SM
905 cutu_reader (dwarf2_per_cu_data *this_cu,
906 dwarf2_per_objfile *per_objfile,
c0ab21c2 907 struct abbrev_table *abbrev_table,
2e671100 908 dwarf2_cu *existing_cu,
c0ab21c2
TT
909 bool skip_partial);
910
911 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 912 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
913 struct dwarf2_cu *parent_cu = nullptr,
914 struct dwo_file *dwo_file = nullptr);
915
c0ab21c2
TT
916 DISABLE_COPY_AND_ASSIGN (cutu_reader);
917
918 const gdb_byte *info_ptr = nullptr;
919 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
920 bool dummy_p = false;
921
6751ebae
TT
922 /* Release the new CU, putting it on the chain. This cannot be done
923 for dummy CUs. */
924 void keep ();
925
c0ab21c2 926private:
9e021579
SM
927 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
928 dwarf2_per_objfile *per_objfile,
2e671100 929 dwarf2_cu *existing_cu);
c0ab21c2
TT
930
931 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
932 std::unique_ptr<dwarf2_cu> m_new_cu;
933
934 /* The ordinary abbreviation table. */
935 abbrev_table_up m_abbrev_table_holder;
936
937 /* The DWO abbreviation table. */
938 abbrev_table_up m_dwo_abbrev_table;
939};
dee91e82 940
c906108c 941/* When we construct a partial symbol table entry we only
0963b4bd 942 need this much information. */
6f06d47b 943struct partial_die_info : public allocate_on_obstack
c906108c 944 {
6f06d47b
YQ
945 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
946
947 /* Disable assign but still keep copy ctor, which is needed
948 load_partial_dies. */
949 partial_die_info& operator=(const partial_die_info& rhs) = delete;
950
52356b79
YQ
951 /* Adjust the partial die before generating a symbol for it. This
952 function may set the is_external flag or change the DIE's
953 name. */
954 void fixup (struct dwarf2_cu *cu);
955
48fbe735
YQ
956 /* Read a minimal amount of information into the minimal die
957 structure. */
958 const gdb_byte *read (const struct die_reader_specs *reader,
959 const struct abbrev_info &abbrev,
960 const gdb_byte *info_ptr);
961
72bf9492 962 /* Offset of this DIE. */
6f06d47b 963 const sect_offset sect_off;
72bf9492
DJ
964
965 /* DWARF-2 tag for this DIE. */
6f06d47b 966 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 967
72bf9492 968 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
969 const unsigned int has_children : 1;
970
72bf9492
DJ
971 unsigned int is_external : 1;
972 unsigned int is_declaration : 1;
973 unsigned int has_type : 1;
974 unsigned int has_specification : 1;
975 unsigned int has_pc_info : 1;
481860b3 976 unsigned int may_be_inlined : 1;
72bf9492 977
0c1b455e
TT
978 /* This DIE has been marked DW_AT_main_subprogram. */
979 unsigned int main_subprogram : 1;
980
72bf9492
DJ
981 /* Flag set if the SCOPE field of this structure has been
982 computed. */
983 unsigned int scope_set : 1;
984
fa4028e9
JB
985 /* Flag set if the DIE has a byte_size attribute. */
986 unsigned int has_byte_size : 1;
987
ff908ebf
AW
988 /* Flag set if the DIE has a DW_AT_const_value attribute. */
989 unsigned int has_const_value : 1;
990
98bfdba5
PA
991 /* Flag set if any of the DIE's children are template arguments. */
992 unsigned int has_template_arguments : 1;
993
52356b79 994 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
995 unsigned int fixup_called : 1;
996
36586728
TT
997 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
998 unsigned int is_dwz : 1;
999
1000 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1001 unsigned int spec_is_dwz : 1;
1002
72bf9492 1003 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1004 sometimes a default name for unnamed DIEs. */
6f06d47b 1005 const char *name = nullptr;
72bf9492 1006
abc72ce4 1007 /* The linkage name, if present. */
6f06d47b 1008 const char *linkage_name = nullptr;
abc72ce4 1009
72bf9492
DJ
1010 /* The scope to prepend to our children. This is generally
1011 allocated on the comp_unit_obstack, so will disappear
1012 when this compilation unit leaves the cache. */
6f06d47b 1013 const char *scope = nullptr;
72bf9492 1014
95554aad
TT
1015 /* Some data associated with the partial DIE. The tag determines
1016 which field is live. */
1017 union
1018 {
1019 /* The location description associated with this DIE, if any. */
1020 struct dwarf_block *locdesc;
1021 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1022 sect_offset sect_off;
6f06d47b 1023 } d {};
72bf9492
DJ
1024
1025 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1026 CORE_ADDR lowpc = 0;
1027 CORE_ADDR highpc = 0;
72bf9492 1028
93311388 1029 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1030 DW_AT_sibling, if any. */
48fbe735
YQ
1031 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1032 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1033 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1034
1035 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1036 DW_AT_specification (or DW_AT_abstract_origin or
1037 DW_AT_extension). */
6f06d47b 1038 sect_offset spec_offset {};
72bf9492
DJ
1039
1040 /* Pointers to this DIE's parent, first child, and next sibling,
1041 if any. */
6f06d47b
YQ
1042 struct partial_die_info *die_parent = nullptr;
1043 struct partial_die_info *die_child = nullptr;
1044 struct partial_die_info *die_sibling = nullptr;
1045
1046 friend struct partial_die_info *
1047 dwarf2_cu::find_partial_die (sect_offset sect_off);
1048
1049 private:
1050 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1051 partial_die_info (sect_offset sect_off)
1052 : partial_die_info (sect_off, DW_TAG_padding, 0)
1053 {
1054 }
1055
1056 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1057 int has_children_)
1058 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1059 {
1060 is_external = 0;
1061 is_declaration = 0;
1062 has_type = 0;
1063 has_specification = 0;
1064 has_pc_info = 0;
1065 may_be_inlined = 0;
1066 main_subprogram = 0;
1067 scope_set = 0;
1068 has_byte_size = 0;
1069 has_const_value = 0;
1070 has_template_arguments = 0;
1071 fixup_called = 0;
1072 is_dwz = 0;
1073 spec_is_dwz = 0;
1074 }
c906108c
SS
1075 };
1076
c906108c
SS
1077/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1078 but this would require a corresponding change in unpack_field_as_long
1079 and friends. */
1080static int bits_per_byte = 8;
1081
9c6a1327
TT
1082struct variant_part_builder;
1083
1084/* When reading a variant, we track a bit more information about the
1085 field, and store it in an object of this type. */
2ddeaf8a
TT
1086
1087struct variant_field
1088{
9c6a1327
TT
1089 int first_field = -1;
1090 int last_field = -1;
1091
1092 /* A variant can contain other variant parts. */
1093 std::vector<variant_part_builder> variant_parts;
1094
2ddeaf8a
TT
1095 /* If we see a DW_TAG_variant, then this will be set if this is the
1096 default branch. */
9c6a1327
TT
1097 bool default_branch = false;
1098 /* If we see a DW_AT_discr_value, then this will be the discriminant
1099 value. */
1100 ULONGEST discriminant_value = 0;
1101 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1102 data. */
1103 struct dwarf_block *discr_list_data = nullptr;
1104};
1105
1106/* This represents a DW_TAG_variant_part. */
1107
1108struct variant_part_builder
1109{
1110 /* The offset of the discriminant field. */
1111 sect_offset discriminant_offset {};
1112
1113 /* Variants that are direct children of this variant part. */
1114 std::vector<variant_field> variants;
1115
1116 /* True if we're currently reading a variant. */
1117 bool processing_variant = false;
2ddeaf8a
TT
1118};
1119
52059ffd
TT
1120struct nextfield
1121{
be2daae6
TT
1122 int accessibility = 0;
1123 int virtuality = 0;
9c6a1327
TT
1124 /* Variant parts need to find the discriminant, which is a DIE
1125 reference. We track the section offset of each field to make
1126 this link. */
1127 sect_offset offset;
be2daae6 1128 struct field field {};
52059ffd
TT
1129};
1130
1131struct fnfieldlist
1132{
be2daae6
TT
1133 const char *name = nullptr;
1134 std::vector<struct fn_field> fnfields;
52059ffd
TT
1135};
1136
c906108c
SS
1137/* The routines that read and process dies for a C struct or C++ class
1138 pass lists of data member fields and lists of member function fields
1139 in an instance of a field_info structure, as defined below. */
1140struct field_info
c5aa993b 1141 {
0963b4bd 1142 /* List of data member and baseclasses fields. */
be2daae6
TT
1143 std::vector<struct nextfield> fields;
1144 std::vector<struct nextfield> baseclasses;
c906108c 1145
85102364 1146 /* Set if the accessibility of one of the fields is not public. */
be2daae6 1147 int non_public_fields = 0;
c906108c 1148
c5aa993b
JM
1149 /* Member function fieldlist array, contains name of possibly overloaded
1150 member function, number of overloaded member functions and a pointer
1151 to the head of the member function field chain. */
be2daae6 1152 std::vector<struct fnfieldlist> fnfieldlists;
98751a41
JK
1153
1154 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1155 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
be2daae6 1156 std::vector<struct decl_field> typedef_field_list;
883fd55a
KS
1157
1158 /* Nested types defined by this class and the number of elements in this
1159 list. */
be2daae6 1160 std::vector<struct decl_field> nested_types_list;
317f7127 1161
9c6a1327
TT
1162 /* If non-null, this is the variant part we are currently
1163 reading. */
1164 variant_part_builder *current_variant_part = nullptr;
1165 /* This holds all the top-level variant parts attached to the type
1166 we're reading. */
1167 std::vector<variant_part_builder> variant_parts;
1168
317f7127
TT
1169 /* Return the total number of fields (including baseclasses). */
1170 int nfields () const
1171 {
1172 return fields.size () + baseclasses.size ();
1173 }
c5aa993b 1174 };
c906108c 1175
ae038cb0
DJ
1176/* Loaded secondary compilation units are kept in memory until they
1177 have not been referenced for the processing of this many
1178 compilation units. Set this to zero to disable caching. Cache
1179 sizes of up to at least twenty will improve startup time for
1180 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1181static int dwarf_max_cache_age = 5;
920d2a44 1182static void
b4f54984
DE
1183show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1184 struct cmd_list_element *c, const char *value)
920d2a44 1185{
3e43a32a 1186 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1187 "DWARF compilation units is %s.\n"),
920d2a44
AC
1188 value);
1189}
4390d890 1190\f
c906108c
SS
1191/* local function prototypes */
1192
918dd910
JK
1193static void dwarf2_find_base_address (struct die_info *die,
1194 struct dwarf2_cu *cu);
1195
891813be 1196static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1197 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1198 const char *name);
0018ea6f 1199
f1902523
JK
1200static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1201 const gdb_byte *info_ptr,
3e225074 1202 struct die_info *type_unit_die);
f1902523 1203
ed2dc618
SM
1204static void dwarf2_build_psymtabs_hard
1205 (struct dwarf2_per_objfile *dwarf2_per_objfile);
c906108c 1206
72bf9492
DJ
1207static void scan_partial_symbols (struct partial_die_info *,
1208 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1209 int, struct dwarf2_cu *);
c906108c 1210
72bf9492
DJ
1211static void add_partial_symbol (struct partial_die_info *,
1212 struct dwarf2_cu *);
63d06c5c 1213
72bf9492
DJ
1214static void add_partial_namespace (struct partial_die_info *pdi,
1215 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1216 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1217
5d7cb8df 1218static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1219 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1220 struct dwarf2_cu *cu);
1221
72bf9492
DJ
1222static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1223 struct dwarf2_cu *cu);
91c24f0a 1224
bc30ff58
JB
1225static void add_partial_subprogram (struct partial_die_info *pdi,
1226 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1227 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1228
d521ce57 1229static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1230
dee91e82 1231static struct partial_die_info *load_partial_dies
d521ce57 1232 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1233
fb816e8b
TV
1234/* A pair of partial_die_info and compilation unit. */
1235struct cu_partial_die_info
1236{
1237 /* The compilation unit of the partial_die_info. */
1238 struct dwarf2_cu *cu;
1239 /* A partial_die_info. */
1240 struct partial_die_info *pdi;
122cf0f2
AB
1241
1242 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1243 : cu (cu),
1244 pdi (pdi)
405feb71 1245 { /* Nothing. */ }
122cf0f2
AB
1246
1247private:
1248 cu_partial_die_info () = delete;
fb816e8b
TV
1249};
1250
122cf0f2
AB
1251static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1252 struct dwarf2_cu *);
72bf9492 1253
d521ce57
TT
1254static const gdb_byte *read_attribute (const struct die_reader_specs *,
1255 struct attribute *, struct attr_abbrev *,
18a8505e
AT
1256 const gdb_byte *, bool *need_reprocess);
1257
1258static void read_attribute_reprocess (const struct die_reader_specs *reader,
1259 struct attribute *attr);
1260
1261static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1262
ed2dc618
SM
1263static sect_offset read_abbrev_offset
1264 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1265 struct dwarf2_section_info *, sect_offset);
f4dc4d17 1266
ed2dc618
SM
1267static const char *read_indirect_string
1268 (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *, const gdb_byte *,
1269 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1270
ed2dc618 1271static const char *read_indirect_string_at_offset
4f44ae6c 1272 (struct dwarf2_per_objfile *dwarf2_per_objfile, LONGEST str_offset);
927aa2e7 1273
d521ce57
TT
1274static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1275 const gdb_byte *,
3019eac3
DE
1276 unsigned int *);
1277
18a8505e
AT
1278static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1279 ULONGEST str_index);
1280
1281static const char *read_stub_str_index (struct dwarf2_cu *cu,
1282 ULONGEST str_index);
3019eac3 1283
e142c38c 1284static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1285
e142c38c
DJ
1286static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1287 struct dwarf2_cu *);
c906108c 1288
7d45c7c3
KB
1289static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
1290 struct dwarf2_cu *cu);
1291
a084a2a6
AT
1292static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1293
05cf31d1
JB
1294static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1295 struct dwarf2_cu *cu);
1296
e142c38c 1297static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1298
e142c38c 1299static struct die_info *die_specification (struct die_info *die,
f2f0e013 1300 struct dwarf2_cu **);
63d06c5c 1301
9c541725 1302static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1303 struct dwarf2_cu *cu);
debd256d 1304
f3f5162e 1305static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1306 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1307 CORE_ADDR, int decode_mapping);
c906108c 1308
804d2729
TT
1309static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1310 const char *);
c906108c 1311
a14ed312 1312static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1313 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1314
ff39bb5e 1315static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1316 struct dwarf2_cu *);
c906108c 1317
ff39bb5e 1318static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1319 struct type *type,
1320 const char *name,
1321 struct obstack *obstack,
12df843f 1322 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1323 const gdb_byte **bytes,
98bfdba5 1324 struct dwarf2_locexpr_baton **baton);
2df3850c 1325
e7c27a73 1326static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1327
b4ba55a1
JB
1328static int need_gnat_info (struct dwarf2_cu *);
1329
3e43a32a
MS
1330static struct type *die_descriptive_type (struct die_info *,
1331 struct dwarf2_cu *);
b4ba55a1
JB
1332
1333static void set_descriptive_type (struct type *, struct die_info *,
1334 struct dwarf2_cu *);
1335
e7c27a73
DJ
1336static struct type *die_containing_type (struct die_info *,
1337 struct dwarf2_cu *);
c906108c 1338
ff39bb5e 1339static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1340 struct dwarf2_cu *);
c906108c 1341
f792889a 1342static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1343
673bfd45
DE
1344static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1345
0d5cff50 1346static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1347
6e70227d 1348static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1349 const char *suffix, int physname,
1350 struct dwarf2_cu *cu);
63d06c5c 1351
e7c27a73 1352static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1353
348e048f
DE
1354static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1355
e7c27a73 1356static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1357
e7c27a73 1358static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1359
96408a79
SA
1360static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1361
71a3c369
TT
1362static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1363
ff013f42 1364static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
891813be 1365 struct dwarf2_cu *, dwarf2_psymtab *);
ff013f42 1366
41144253 1367/* Return the .debug_loclists section to use for cu. */
1368static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1369
3a2b436a 1370/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1371 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1372enum pc_bounds_kind
1373{
e385593e 1374 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1375 PC_BOUNDS_NOT_PRESENT,
1376
e385593e
JK
1377 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1378 were present but they do not form a valid range of PC addresses. */
1379 PC_BOUNDS_INVALID,
1380
3a2b436a
JK
1381 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1382 PC_BOUNDS_RANGES,
1383
1384 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1385 PC_BOUNDS_HIGH_LOW,
1386};
1387
1388static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1389 CORE_ADDR *, CORE_ADDR *,
1390 struct dwarf2_cu *,
891813be 1391 dwarf2_psymtab *);
c906108c 1392
fae299cd
DC
1393static void get_scope_pc_bounds (struct die_info *,
1394 CORE_ADDR *, CORE_ADDR *,
1395 struct dwarf2_cu *);
1396
801e3a5b
JB
1397static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1398 CORE_ADDR, struct dwarf2_cu *);
1399
a14ed312 1400static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1401 struct dwarf2_cu *);
c906108c 1402
a14ed312 1403static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1404 struct type *, struct dwarf2_cu *);
c906108c 1405
a14ed312 1406static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1407 struct die_info *, struct type *,
e7c27a73 1408 struct dwarf2_cu *);
c906108c 1409
a14ed312 1410static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1411 struct type *,
1412 struct dwarf2_cu *);
c906108c 1413
134d01f1 1414static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1415
e7c27a73 1416static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1417
e7c27a73 1418static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1419
5d7cb8df
JK
1420static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1421
804d2729 1422static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1423
27aa8d6a
SW
1424static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1425
74921315
KS
1426static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1427
f55ee35c
JK
1428static struct type *read_module_type (struct die_info *die,
1429 struct dwarf2_cu *cu);
1430
38d518c9 1431static const char *namespace_name (struct die_info *die,
e142c38c 1432 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1433
134d01f1 1434static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1435
7d79de9a
TT
1436static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1437 bool * = nullptr);
c906108c 1438
6e70227d 1439static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1440 struct dwarf2_cu *);
1441
bf6af496 1442static struct die_info *read_die_and_siblings_1
d521ce57 1443 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1444 struct die_info *);
639d11d3 1445
dee91e82 1446static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1447 const gdb_byte *info_ptr,
1448 const gdb_byte **new_info_ptr,
639d11d3
DC
1449 struct die_info *parent);
1450
d521ce57
TT
1451static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1452 struct die_info **, const gdb_byte *,
3e225074 1453 int);
3019eac3 1454
d521ce57 1455static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1456 struct die_info **, const gdb_byte *);
93311388 1457
e7c27a73 1458static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1459
15d034d0 1460static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1461 struct objfile *);
71c25dea 1462
15d034d0 1463static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1464
15d034d0 1465static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1466 struct die_info *die,
1467 struct dwarf2_cu *cu);
1468
ca69b9e6
DE
1469static const char *dwarf2_physname (const char *name, struct die_info *die,
1470 struct dwarf2_cu *cu);
1471
e142c38c 1472static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1473 struct dwarf2_cu **);
9219021c 1474
d97bc12b
DE
1475static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1476
1477static void dump_die_for_error (struct die_info *);
1478
1479static void dump_die_1 (struct ui_file *, int level, int max_level,
1480 struct die_info *);
c906108c 1481
d97bc12b 1482/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1483
51545339 1484static void store_in_ref_table (struct die_info *,
10b3939b 1485 struct dwarf2_cu *);
c906108c 1486
348e048f 1487static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1488 const struct attribute *,
348e048f
DE
1489 struct dwarf2_cu **);
1490
10b3939b 1491static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1492 const struct attribute *,
f2f0e013 1493 struct dwarf2_cu **);
c906108c 1494
348e048f 1495static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1496 const struct attribute *,
348e048f
DE
1497 struct dwarf2_cu **);
1498
ac9ec31b
DE
1499static struct type *get_signatured_type (struct die_info *, ULONGEST,
1500 struct dwarf2_cu *);
1501
1502static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1503 const struct attribute *,
ac9ec31b
DE
1504 struct dwarf2_cu *);
1505
ab432490
SM
1506static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1507 dwarf2_per_objfile *per_objfile);
348e048f 1508
ab432490
SM
1509static void read_signatured_type (signatured_type *sig_type,
1510 dwarf2_per_objfile *per_objfile);
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
f792889a
DJ
1549static struct type *set_die_type (struct die_info *, struct type *,
1550 struct dwarf2_cu *);
1c379e20 1551
ed2dc618 1552static void create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
ae038cb0 1553
ed2dc618 1554static int create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile);
1fd400ff 1555
ab432490
SM
1556static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1557 dwarf2_per_objfile *per_objfile,
1558 bool skip_partial,
1559 enum language pretend_language);
10b3939b 1560
8fc0b21d 1561static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1562 enum language pretend_language);
10b3939b 1563
8fc0b21d 1564static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1565 enum language pretend_language);
f4dc4d17 1566
10b3939b
DJ
1567static void dwarf2_add_dependence (struct dwarf2_cu *,
1568 struct dwarf2_per_cu_data *);
1569
ae038cb0
DJ
1570static void dwarf2_mark (struct dwarf2_cu *);
1571
b64f50a1 1572static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1573 dwarf2_per_cu_data *per_cu,
1574 dwarf2_per_objfile *per_objfile);
673bfd45 1575
f792889a 1576static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1577
120ce1b5
SM
1578static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1579 dwarf2_per_objfile *per_objfile,
95554aad
TT
1580 enum language pretend_language);
1581
ed2dc618 1582static void process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile);
9291a0cd 1583
b303c6f6
AB
1584/* Class, the destructor of which frees all allocated queue entries. This
1585 will only have work to do if an error was thrown while processing the
1586 dwarf. If no error was thrown then the queue entries should have all
1587 been processed, and freed, as we went along. */
1588
1589class dwarf2_queue_guard
1590{
1591public:
39856def
TT
1592 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1593 : m_per_objfile (per_objfile)
1594 {
1595 }
b303c6f6
AB
1596
1597 /* Free any entries remaining on the queue. There should only be
1598 entries left if we hit an error while processing the dwarf. */
1599 ~dwarf2_queue_guard ()
1600 {
39856def
TT
1601 /* Ensure that no memory is allocated by the queue. */
1602 std::queue<dwarf2_queue_item> empty;
5989a64e 1603 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1604 }
b303c6f6 1605
39856def 1606 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1607
39856def
TT
1608private:
1609 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1610};
1611
39856def
TT
1612dwarf2_queue_item::~dwarf2_queue_item ()
1613{
1614 /* Anything still marked queued is likely to be in an
1615 inconsistent state, so discard it. */
1616 if (per_cu->queued)
1617 {
7188ed02 1618 per_objfile->remove_cu (per_cu);
39856def
TT
1619 per_cu->queued = 0;
1620 }
1621}
1622
d721ba37
PA
1623/* The return type of find_file_and_directory. Note, the enclosed
1624 string pointers are only valid while this object is valid. */
1625
1626struct file_and_directory
1627{
1628 /* The filename. This is never NULL. */
1629 const char *name;
1630
1631 /* The compilation directory. NULL if not known. If we needed to
1632 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1633 points directly to the DW_AT_comp_dir string attribute owned by
1634 the obstack that owns the DIE. */
1635 const char *comp_dir;
1636
1637 /* If we needed to build a new string for comp_dir, this is what
1638 owns the storage. */
1639 std::string comp_dir_storage;
1640};
1641
1642static file_and_directory find_file_and_directory (struct die_info *die,
1643 struct dwarf2_cu *cu);
9291a0cd 1644
298e9637 1645static htab_up allocate_signatured_type_table ();
1fd400ff 1646
298e9637 1647static htab_up allocate_dwo_unit_table ();
3019eac3 1648
57d63ce2 1649static struct dwo_unit *lookup_dwo_unit_in_dwp
ed2dc618
SM
1650 (struct dwarf2_per_objfile *dwarf2_per_objfile,
1651 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 1652 ULONGEST signature, int is_debug_types);
a2ce51a0 1653
ed2dc618
SM
1654static struct dwp_file *get_dwp_file
1655 (struct dwarf2_per_objfile *dwarf2_per_objfile);
a2ce51a0 1656
3019eac3 1657static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1658 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1659 ULONGEST signature);
3019eac3
DE
1660
1661static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1662 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1663
1b555f17 1664static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1665
263db9a1
TT
1666/* A unique pointer to a dwo_file. */
1667
51ac9db5 1668typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1669
ed2dc618 1670static void process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile);
95554aad 1671
1b80a9fa 1672static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1673
1674static void free_line_header_voidp (void *arg);
4390d890
DE
1675\f
1676/* Various complaints about symbol reading that don't abort the process. */
1677
4390d890
DE
1678static void
1679dwarf2_debug_line_missing_file_complaint (void)
1680{
b98664d3 1681 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1682}
1683
1684static void
1685dwarf2_debug_line_missing_end_sequence_complaint (void)
1686{
b98664d3 1687 complaint (_(".debug_line section has line "
4390d890
DE
1688 "program sequence without an end"));
1689}
1690
1691static void
1692dwarf2_complex_location_expr_complaint (void)
1693{
b98664d3 1694 complaint (_("location expression too complex"));
4390d890
DE
1695}
1696
1697static void
1698dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1699 int arg3)
1700{
b98664d3 1701 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1702 arg1, arg2, arg3);
1703}
1704
4390d890
DE
1705static void
1706dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1707{
b98664d3 1708 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1709 arg1, arg2);
1710}
527f3840
JK
1711
1712/* Hash function for line_header_hash. */
1713
1714static hashval_t
1715line_header_hash (const struct line_header *ofs)
1716{
9c541725 1717 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1718}
1719
1720/* Hash function for htab_create_alloc_ex for line_header_hash. */
1721
1722static hashval_t
1723line_header_hash_voidp (const void *item)
1724{
9a3c8263 1725 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1726
1727 return line_header_hash (ofs);
1728}
1729
1730/* Equality function for line_header_hash. */
1731
1732static int
1733line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1734{
9a3c8263
SM
1735 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1736 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1737
9c541725 1738 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1739 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1740}
1741
4390d890 1742\f
9291a0cd 1743
330cdd98
PA
1744/* See declaration. */
1745
5989a64e
SM
1746dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1747 bool can_copy_)
c3699833
SM
1748 : obfd (obfd),
1749 can_copy (can_copy_)
330cdd98
PA
1750{
1751 if (names == NULL)
1752 names = &dwarf2_elf_names;
1753
330cdd98
PA
1754 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1755 locate_sections (obfd, sec, *names);
1756}
1757
5989a64e 1758dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1759{
b76e467d 1760 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1761 per_cu->imported_symtabs_free ();
fc8e7e75 1762
b2bdb8cf 1763 for (signatured_type *sig_type : all_type_units)
ae640021 1764 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1765
5989a64e 1766 /* Everything else should be on this->obstack. */
330cdd98
PA
1767}
1768
7188ed02 1769/* See read.h. */
330cdd98
PA
1770
1771void
7188ed02 1772dwarf2_per_objfile::remove_all_cus ()
330cdd98 1773{
7188ed02
SM
1774 for (auto pair : m_dwarf2_cus)
1775 delete pair.second;
330cdd98 1776
7188ed02 1777 m_dwarf2_cus.clear ();
330cdd98
PA
1778}
1779
11ed8cad
TT
1780/* A helper class that calls free_cached_comp_units on
1781 destruction. */
1782
1783class free_cached_comp_units
1784{
1785public:
1786
1787 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1788 : m_per_objfile (per_objfile)
1789 {
1790 }
1791
1792 ~free_cached_comp_units ()
1793 {
7188ed02 1794 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1795 }
1796
1797 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1798
1799private:
1800
1801 dwarf2_per_objfile *m_per_objfile;
1802};
1803
af758d11
SM
1804/* See read.h. */
1805
1806bool
1807dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1808{
1809 gdb_assert (per_cu->index < this->m_symtabs.size ());
1810
1811 return this->m_symtabs[per_cu->index] != nullptr;
1812}
1813
1814/* See read.h. */
1815
1816compunit_symtab *
1817dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1818{
1819 gdb_assert (per_cu->index < this->m_symtabs.size ());
1820
1821 return this->m_symtabs[per_cu->index];
1822}
1823
1824/* See read.h. */
1825
1826void
1827dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1828 compunit_symtab *symtab)
1829{
1830 gdb_assert (per_cu->index < this->m_symtabs.size ());
1831 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1832
1833 this->m_symtabs[per_cu->index] = symtab;
1834}
1835
c906108c 1836/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1837 information and return true if we have enough to do something.
1838 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1839 ELF names are used. CAN_COPY is true for formats where symbol
1840 interposition is possible and so symbol values must follow copy
1841 relocation rules. */
c906108c
SS
1842
1843int
251d32d9 1844dwarf2_has_info (struct objfile *objfile,
4b610737
TT
1845 const struct dwarf2_debug_sections *names,
1846 bool can_copy)
c906108c 1847{
97cbe998
SDJ
1848 if (objfile->flags & OBJF_READNEVER)
1849 return 0;
1850
ed2dc618
SM
1851 struct dwarf2_per_objfile *dwarf2_per_objfile
1852 = get_dwarf2_per_objfile (objfile);
1853
1854 if (dwarf2_per_objfile == NULL)
5989a64e
SM
1855 {
1856 /* For now, each dwarf2_per_objfile owns its own dwarf2_per_bfd (no
1857 sharing yet). */
1858 dwarf2_per_bfd *per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1859
1860 dwarf2_per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
1861 }
5bfd760d 1862
5989a64e
SM
1863 return (!dwarf2_per_objfile->per_bfd->info.is_virtual
1864 && dwarf2_per_objfile->per_bfd->info.s.section != NULL
1865 && !dwarf2_per_objfile->per_bfd->abbrev.is_virtual
1866 && dwarf2_per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1867}
1868
251d32d9
TG
1869/* When loading sections, we look either for uncompressed section or for
1870 compressed section names. */
233a11ab
CS
1871
1872static int
251d32d9
TG
1873section_is_p (const char *section_name,
1874 const struct dwarf2_section_names *names)
233a11ab 1875{
251d32d9
TG
1876 if (names->normal != NULL
1877 && strcmp (section_name, names->normal) == 0)
1878 return 1;
1879 if (names->compressed != NULL
1880 && strcmp (section_name, names->compressed) == 0)
1881 return 1;
1882 return 0;
233a11ab
CS
1883}
1884
330cdd98 1885/* See declaration. */
c906108c 1886
330cdd98 1887void
5989a64e
SM
1888dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1889 const dwarf2_debug_sections &names)
c906108c 1890{
fd361982 1891 flagword aflag = bfd_section_flags (sectp);
251d32d9 1892
dc7650b8
JK
1893 if ((aflag & SEC_HAS_CONTENTS) == 0)
1894 {
1895 }
950b7495
KS
1896 else if (elf_section_data (sectp)->this_hdr.sh_size
1897 > bfd_get_file_size (abfd))
1898 {
1899 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1900 warning (_("Discarding section %s which has a section size (%s"
1901 ") larger than the file size [in module %s]"),
1902 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1903 bfd_get_filename (abfd));
1904 }
330cdd98 1905 else if (section_is_p (sectp->name, &names.info))
c906108c 1906 {
330cdd98 1907 this->info.s.section = sectp;
fd361982 1908 this->info.size = bfd_section_size (sectp);
c906108c 1909 }
330cdd98 1910 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1911 {
330cdd98 1912 this->abbrev.s.section = sectp;
fd361982 1913 this->abbrev.size = bfd_section_size (sectp);
c906108c 1914 }
330cdd98 1915 else if (section_is_p (sectp->name, &names.line))
c906108c 1916 {
330cdd98 1917 this->line.s.section = sectp;
fd361982 1918 this->line.size = bfd_section_size (sectp);
c906108c 1919 }
330cdd98 1920 else if (section_is_p (sectp->name, &names.loc))
c906108c 1921 {
330cdd98 1922 this->loc.s.section = sectp;
fd361982 1923 this->loc.size = bfd_section_size (sectp);
c906108c 1924 }
330cdd98 1925 else if (section_is_p (sectp->name, &names.loclists))
43988095 1926 {
330cdd98 1927 this->loclists.s.section = sectp;
fd361982 1928 this->loclists.size = bfd_section_size (sectp);
43988095 1929 }
330cdd98 1930 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 1931 {
330cdd98 1932 this->macinfo.s.section = sectp;
fd361982 1933 this->macinfo.size = bfd_section_size (sectp);
c906108c 1934 }
330cdd98 1935 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 1936 {
330cdd98 1937 this->macro.s.section = sectp;
fd361982 1938 this->macro.size = bfd_section_size (sectp);
cf2c3c16 1939 }
330cdd98 1940 else if (section_is_p (sectp->name, &names.str))
c906108c 1941 {
330cdd98 1942 this->str.s.section = sectp;
fd361982 1943 this->str.size = bfd_section_size (sectp);
c906108c 1944 }
18a8505e
AT
1945 else if (section_is_p (sectp->name, &names.str_offsets))
1946 {
1947 this->str_offsets.s.section = sectp;
1948 this->str_offsets.size = bfd_section_size (sectp);
1949 }
330cdd98 1950 else if (section_is_p (sectp->name, &names.line_str))
43988095 1951 {
330cdd98 1952 this->line_str.s.section = sectp;
fd361982 1953 this->line_str.size = bfd_section_size (sectp);
43988095 1954 }
330cdd98 1955 else if (section_is_p (sectp->name, &names.addr))
3019eac3 1956 {
330cdd98 1957 this->addr.s.section = sectp;
fd361982 1958 this->addr.size = bfd_section_size (sectp);
3019eac3 1959 }
330cdd98 1960 else if (section_is_p (sectp->name, &names.frame))
b6af0555 1961 {
330cdd98 1962 this->frame.s.section = sectp;
fd361982 1963 this->frame.size = bfd_section_size (sectp);
b6af0555 1964 }
330cdd98 1965 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 1966 {
330cdd98 1967 this->eh_frame.s.section = sectp;
fd361982 1968 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 1969 }
330cdd98 1970 else if (section_is_p (sectp->name, &names.ranges))
af34e669 1971 {
330cdd98 1972 this->ranges.s.section = sectp;
fd361982 1973 this->ranges.size = bfd_section_size (sectp);
af34e669 1974 }
330cdd98 1975 else if (section_is_p (sectp->name, &names.rnglists))
43988095 1976 {
330cdd98 1977 this->rnglists.s.section = sectp;
fd361982 1978 this->rnglists.size = bfd_section_size (sectp);
43988095 1979 }
330cdd98 1980 else if (section_is_p (sectp->name, &names.types))
348e048f 1981 {
8b70b953
TT
1982 struct dwarf2_section_info type_section;
1983
1984 memset (&type_section, 0, sizeof (type_section));
049412e3 1985 type_section.s.section = sectp;
fd361982 1986 type_section.size = bfd_section_size (sectp);
8b70b953 1987
fd5866f6 1988 this->types.push_back (type_section);
348e048f 1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 1991 {
330cdd98 1992 this->gdb_index.s.section = sectp;
fd361982 1993 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 1994 }
927aa2e7
JK
1995 else if (section_is_p (sectp->name, &names.debug_names))
1996 {
1997 this->debug_names.s.section = sectp;
fd361982 1998 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
1999 }
2000 else if (section_is_p (sectp->name, &names.debug_aranges))
2001 {
2002 this->debug_aranges.s.section = sectp;
fd361982 2003 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2004 }
dce234bc 2005
fd361982
AM
2006 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2007 && bfd_section_vma (sectp) == 0)
330cdd98 2008 this->has_section_at_zero = true;
c906108c
SS
2009}
2010
dce234bc 2011/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2012 SECTION_NAME. */
af34e669 2013
dce234bc 2014void
3017a003
TG
2015dwarf2_get_section_info (struct objfile *objfile,
2016 enum dwarf2_section_enum sect,
d521ce57 2017 asection **sectp, const gdb_byte **bufp,
dce234bc
PP
2018 bfd_size_type *sizep)
2019{
5bfd760d 2020 struct dwarf2_per_objfile *data = dwarf2_objfile_data_key.get (objfile);
dce234bc 2021 struct dwarf2_section_info *info;
a3b2a86b
TT
2022
2023 /* We may see an objfile without any DWARF, in which case we just
2024 return nothing. */
2025 if (data == NULL)
2026 {
2027 *sectp = NULL;
2028 *bufp = NULL;
2029 *sizep = 0;
2030 return;
2031 }
3017a003
TG
2032 switch (sect)
2033 {
2034 case DWARF2_DEBUG_FRAME:
5989a64e 2035 info = &data->per_bfd->frame;
3017a003
TG
2036 break;
2037 case DWARF2_EH_FRAME:
5989a64e 2038 info = &data->per_bfd->eh_frame;
3017a003
TG
2039 break;
2040 default:
2041 gdb_assert_not_reached ("unexpected section");
2042 }
dce234bc 2043
96b79293 2044 info->read (objfile);
dce234bc 2045
96b79293 2046 *sectp = info->get_bfd_section ();
dce234bc
PP
2047 *bufp = info->buffer;
2048 *sizep = info->size;
2049}
2050
36586728
TT
2051/* A helper function to find the sections for a .dwz file. */
2052
2053static void
2054locate_dwz_sections (bfd *abfd, asection *sectp, void *arg)
2055{
9a3c8263 2056 struct dwz_file *dwz_file = (struct dwz_file *) arg;
36586728
TT
2057
2058 /* Note that we only support the standard ELF names, because .dwz
2059 is ELF-only (at the time of writing). */
2060 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2061 {
049412e3 2062 dwz_file->abbrev.s.section = sectp;
fd361982 2063 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2064 }
2065 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2066 {
049412e3 2067 dwz_file->info.s.section = sectp;
fd361982 2068 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2069 }
2070 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2071 {
049412e3 2072 dwz_file->str.s.section = sectp;
fd361982 2073 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2074 }
2075 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2076 {
049412e3 2077 dwz_file->line.s.section = sectp;
fd361982 2078 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2079 }
2080 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2081 {
049412e3 2082 dwz_file->macro.s.section = sectp;
fd361982 2083 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2084 }
2ec9a5e0
TT
2085 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2086 {
049412e3 2087 dwz_file->gdb_index.s.section = sectp;
fd361982 2088 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2089 }
927aa2e7
JK
2090 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2091 {
2092 dwz_file->debug_names.s.section = sectp;
fd361982 2093 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2094 }
36586728
TT
2095}
2096
c4973306 2097/* See dwarf2read.h. */
36586728 2098
c4973306 2099struct dwz_file *
c3699833 2100dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2101{
36586728 2102 const char *filename;
acd13123 2103 bfd_size_type buildid_len_arg;
dc294be5
TT
2104 size_t buildid_len;
2105 bfd_byte *buildid;
36586728 2106
c3699833
SM
2107 if (per_bfd->dwz_file != NULL)
2108 return per_bfd->dwz_file.get ();
36586728 2109
4db1a1dc 2110 bfd_set_error (bfd_error_no_error);
791afaa2 2111 gdb::unique_xmalloc_ptr<char> data
c3699833 2112 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2113 &buildid_len_arg, &buildid));
4db1a1dc
TT
2114 if (data == NULL)
2115 {
2116 if (bfd_get_error () == bfd_error_no_error)
2117 return NULL;
2118 error (_("could not read '.gnu_debugaltlink' section: %s"),
2119 bfd_errmsg (bfd_get_error ()));
2120 }
791afaa2
TT
2121
2122 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2123
acd13123
TT
2124 buildid_len = (size_t) buildid_len_arg;
2125
791afaa2 2126 filename = data.get ();
d721ba37
PA
2127
2128 std::string abs_storage;
36586728
TT
2129 if (!IS_ABSOLUTE_PATH (filename))
2130 {
14278e1f 2131 gdb::unique_xmalloc_ptr<char> abs
c3699833 2132 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2133
14278e1f 2134 abs_storage = ldirname (abs.get ()) + SLASH_STRING + filename;
d721ba37 2135 filename = abs_storage.c_str ();
36586728
TT
2136 }
2137
dc294be5
TT
2138 /* First try the file name given in the section. If that doesn't
2139 work, try to use the build-id instead. */
ad80db5b 2140 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename, gnutarget));
dc294be5 2141 if (dwz_bfd != NULL)
36586728 2142 {
192b62ce 2143 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2144 dwz_bfd.reset (nullptr);
36586728
TT
2145 }
2146
dc294be5
TT
2147 if (dwz_bfd == NULL)
2148 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2149
0d79cdc4
AM
2150 if (dwz_bfd == nullptr)
2151 {
2152 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2153 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2154
2155 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2156 buildid_len,
2157 origname,
2158 &alt_filename));
2159
2160 if (fd.get () >= 0)
2161 {
2162 /* File successfully retrieved from server. */
ad80db5b 2163 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2164
2165 if (dwz_bfd == nullptr)
2166 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2167 alt_filename.get ());
2168 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2169 dwz_bfd.reset (nullptr);
2170 }
2171 }
2172
dc294be5
TT
2173 if (dwz_bfd == NULL)
2174 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2175 bfd_get_filename (per_bfd->obfd));
dc294be5 2176
7ff8cb8c
TT
2177 std::unique_ptr<struct dwz_file> result
2178 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2179
7ff8cb8c
TT
2180 bfd_map_over_sections (result->dwz_bfd.get (), locate_dwz_sections,
2181 result.get ());
36586728 2182
c3699833
SM
2183 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2184 per_bfd->dwz_file = std::move (result);
2185 return per_bfd->dwz_file.get ();
36586728 2186}
9291a0cd 2187\f
7b9f3c50
DE
2188/* DWARF quick_symbols_functions support. */
2189
2190/* TUs can share .debug_line entries, and there can be a lot more TUs than
2191 unique line tables, so we maintain a separate table of all .debug_line
2192 derived entries to support the sharing.
2193 All the quick functions need is the list of file names. We discard the
2194 line_header when we're done and don't need to record it here. */
2195struct quick_file_names
2196{
094b34ac
DE
2197 /* The data used to construct the hash key. */
2198 struct stmt_list_hash hash;
7b9f3c50
DE
2199
2200 /* The number of entries in file_names, real_names. */
2201 unsigned int num_file_names;
2202
2203 /* The file names from the line table, after being run through
2204 file_full_name. */
2205 const char **file_names;
2206
2207 /* The file names from the line table after being run through
2208 gdb_realpath. These are computed lazily. */
2209 const char **real_names;
2210};
2211
2212/* When using the index (and thus not using psymtabs), each CU has an
2213 object of this type. This is used to hold information needed by
2214 the various "quick" methods. */
2215struct dwarf2_per_cu_quick_data
2216{
2217 /* The file table. This can be NULL if there was no file table
2218 or it's currently not read in.
5989a64e 2219 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2220 struct quick_file_names *file_names;
2221
7b9f3c50
DE
2222 /* A temporary mark bit used when iterating over all CUs in
2223 expand_symtabs_matching. */
2224 unsigned int mark : 1;
2225
2226 /* True if we've tried to read the file table and found there isn't one.
2227 There will be no point in trying to read it again next time. */
2228 unsigned int no_file_data : 1;
2229};
2230
094b34ac
DE
2231/* Utility hash function for a stmt_list_hash. */
2232
2233static hashval_t
2234hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2235{
2236 hashval_t v = 0;
2237
2238 if (stmt_list_hash->dwo_unit != NULL)
2239 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2240 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2241 return v;
2242}
2243
2244/* Utility equality function for a stmt_list_hash. */
2245
2246static int
2247eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2248 const struct stmt_list_hash *rhs)
2249{
2250 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2251 return 0;
2252 if (lhs->dwo_unit != NULL
2253 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2254 return 0;
2255
9c541725 2256 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2257}
2258
7b9f3c50
DE
2259/* Hash function for a quick_file_names. */
2260
2261static hashval_t
2262hash_file_name_entry (const void *e)
2263{
9a3c8263
SM
2264 const struct quick_file_names *file_data
2265 = (const struct quick_file_names *) e;
7b9f3c50 2266
094b34ac 2267 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2268}
2269
2270/* Equality function for a quick_file_names. */
2271
2272static int
2273eq_file_name_entry (const void *a, const void *b)
2274{
9a3c8263
SM
2275 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2276 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2277
094b34ac 2278 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2279}
2280
2281/* Delete function for a quick_file_names. */
2282
2283static void
2284delete_file_name_entry (void *e)
2285{
9a3c8263 2286 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2287 int i;
2288
2289 for (i = 0; i < file_data->num_file_names; ++i)
2290 {
2291 xfree ((void*) file_data->file_names[i]);
2292 if (file_data->real_names)
2293 xfree ((void*) file_data->real_names[i]);
2294 }
2295
45940949
TT
2296 /* The space for the struct itself lives on the obstack, so we don't
2297 free it here. */
7b9f3c50
DE
2298}
2299
2300/* Create a quick_file_names hash table. */
2301
5895093f 2302static htab_up
7b9f3c50
DE
2303create_quick_file_names_table (unsigned int nr_initial_entries)
2304{
5895093f
TT
2305 return htab_up (htab_create_alloc (nr_initial_entries,
2306 hash_file_name_entry, eq_file_name_entry,
2307 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2308}
9291a0cd 2309
ab432490
SM
2310/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2311 function is unrelated to symtabs, symtab would have to be created afterwards.
2312 You should call age_cached_comp_units after processing the CU. */
918dd910 2313
1b555f17 2314static dwarf2_cu *
ab432490
SM
2315load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2316 bool skip_partial)
918dd910 2317{
3019eac3 2318 if (per_cu->is_debug_types)
ab432490 2319 load_full_type_unit (per_cu, per_objfile);
918dd910 2320 else
ab432490 2321 load_full_comp_unit (per_cu, per_objfile, skip_partial, language_minimal);
918dd910 2322
7188ed02
SM
2323 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2324 if (cu == nullptr)
1b555f17 2325 return nullptr; /* Dummy CU. */
2dc860c0 2326
7188ed02 2327 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2328
7188ed02 2329 return cu;
918dd910
JK
2330}
2331
97a1449a 2332/* Read in the symbols for PER_CU in the context of DWARF"_PER_OBJFILE. */
2fdf6df6 2333
9291a0cd 2334static void
97a1449a
SM
2335dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2336 dwarf2_per_objfile *dwarf2_per_objfile,
2337 bool skip_partial)
9291a0cd 2338{
f4dc4d17
DE
2339 /* Skip type_unit_groups, reading the type units they contain
2340 is handled elsewhere. */
197400e8 2341 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2342 return;
2343
b303c6f6
AB
2344 /* The destructor of dwarf2_queue_guard frees any entries left on
2345 the queue. After this point we're guaranteed to leave this function
2346 with the dwarf queue empty. */
39856def 2347 dwarf2_queue_guard q_guard (dwarf2_per_objfile);
9291a0cd 2348
af758d11 2349 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
95554aad 2350 {
120ce1b5 2351 queue_comp_unit (per_cu, dwarf2_per_objfile, language_minimal);
1b555f17 2352 dwarf2_cu *cu = load_cu (per_cu, dwarf2_per_objfile, skip_partial);
89e63ee4
DE
2353
2354 /* If we just loaded a CU from a DWO, and we're working with an index
2355 that may badly handle TUs, load all the TUs in that DWO as well.
2356 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2357 if (!per_cu->is_debug_types
1b555f17
SM
2358 && cu != NULL
2359 && cu->dwo_unit != NULL
5989a64e
SM
2360 && dwarf2_per_objfile->per_bfd->index_table != NULL
2361 && dwarf2_per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2362 /* DWP files aren't supported yet. */
ed2dc618 2363 && get_dwp_file (dwarf2_per_objfile) == NULL)
1b555f17 2364 queue_and_load_all_dwo_tus (cu);
95554aad 2365 }
9291a0cd 2366
ed2dc618 2367 process_queue (dwarf2_per_objfile);
9291a0cd
TT
2368
2369 /* Age the cache, releasing compilation units that have not
2370 been used recently. */
7188ed02 2371 dwarf2_per_objfile->age_comp_units ();
9291a0cd
TT
2372}
2373
97a1449a
SM
2374/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2375 the per-objfile for which this symtab is instantiated.
2376
2377 Returns the resulting symbol table. */
2fdf6df6 2378
43f3e411 2379static struct compunit_symtab *
97a1449a
SM
2380dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
2381 dwarf2_per_objfile *dwarf2_per_objfile,
2382 bool skip_partial)
9291a0cd 2383{
5989a64e 2384 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
af758d11
SM
2385
2386 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd 2387 {
11ed8cad 2388 free_cached_comp_units freer (dwarf2_per_objfile);
c83dd867 2389 scoped_restore decrementer = increment_reading_symtab ();
97a1449a 2390 dw2_do_instantiate_symtab (per_cu, dwarf2_per_objfile, skip_partial);
ed2dc618 2391 process_cu_includes (dwarf2_per_objfile);
9291a0cd 2392 }
f194fefb 2393
af758d11 2394 return dwarf2_per_objfile->get_symtab (per_cu);
9291a0cd
TT
2395}
2396
ff4c9fec 2397/* See declaration. */
f4dc4d17 2398
ff4c9fec 2399dwarf2_per_cu_data *
5989a64e 2400dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2401{
b76e467d 2402 if (index >= this->all_comp_units.size ())
ff4c9fec 2403 {
b76e467d 2404 index -= this->all_comp_units.size ();
b2bdb8cf 2405 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2406 return &this->all_type_units[index]->per_cu;
2407 }
f4dc4d17 2408
ff4c9fec
SM
2409 return this->all_comp_units[index];
2410}
f4dc4d17 2411
ff4c9fec 2412/* See declaration. */
2fdf6df6 2413
ff4c9fec 2414dwarf2_per_cu_data *
5989a64e 2415dwarf2_per_bfd::get_cu (int index)
1fd400ff 2416{
b76e467d 2417 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2418
ff4c9fec 2419 return this->all_comp_units[index];
f4dc4d17
DE
2420}
2421
ff4c9fec 2422/* See declaration. */
f4dc4d17 2423
ff4c9fec 2424signatured_type *
5989a64e 2425dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2426{
b2bdb8cf 2427 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2428
ff4c9fec 2429 return this->all_type_units[index];
1fd400ff
TT
2430}
2431
d3473f0c
TT
2432/* See read.h. */
2433
2434dwarf2_per_cu_data *
5989a64e 2435dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2436{
2437 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2438 result->per_bfd = this;
d3473f0c
TT
2439 result->index = m_num_psymtabs++;
2440 return result;
2441}
2442
2443/* See read.h. */
2444
2445signatured_type *
5989a64e 2446dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2447{
2448 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2449 result->per_cu.per_bfd = this;
d3473f0c
TT
2450 result->per_cu.index = m_num_psymtabs++;
2451 return result;
2452}
2453
168c9250 2454/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2455 obstack, and constructed with the specified field values. */
4b514bc8
JK
2456
2457static dwarf2_per_cu_data *
168c9250
SM
2458create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2459 struct dwarf2_section_info *section,
2460 int is_dwz,
2461 sect_offset sect_off, ULONGEST length)
4b514bc8 2462{
168c9250 2463 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2464 the_cu->sect_off = sect_off;
2465 the_cu->length = length;
4b514bc8 2466 the_cu->section = section;
168c9250 2467 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2468 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2469 the_cu->is_dwz = is_dwz;
2470 return the_cu;
2471}
2472
2ec9a5e0
TT
2473/* A helper for create_cus_from_index that handles a given list of
2474 CUs. */
2fdf6df6 2475
74a0d9f6 2476static void
168c9250 2477create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2478 const gdb_byte *cu_list, offset_type n_elements,
2479 struct dwarf2_section_info *section,
b76e467d 2480 int is_dwz)
9291a0cd 2481{
12359b5e 2482 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2483 {
74a0d9f6 2484 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2485
2486 sect_offset sect_off
2487 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2488 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2489 cu_list += 2 * 8;
2490
b76e467d 2491 dwarf2_per_cu_data *per_cu
168c9250
SM
2492 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2493 length);
2494 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2495 }
9291a0cd
TT
2496}
2497
2ec9a5e0 2498/* Read the CU list from the mapped index, and use it to create all
168c9250 2499 the CU objects for PER_BFD. */
2ec9a5e0 2500
74a0d9f6 2501static void
168c9250 2502create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2503 const gdb_byte *cu_list, offset_type cu_list_elements,
2504 const gdb_byte *dwz_list, offset_type dwz_elements)
2505{
168c9250
SM
2506 gdb_assert (per_bfd->all_comp_units.empty ());
2507 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2508
168c9250
SM
2509 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2510 &per_bfd->info, 0);
2ec9a5e0
TT
2511
2512 if (dwz_elements == 0)
74a0d9f6 2513 return;
2ec9a5e0 2514
168c9250
SM
2515 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2516 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2517 &dwz->info, 1);
2ec9a5e0
TT
2518}
2519
1fd400ff 2520/* Create the signatured type hash table from the index. */
673bfd45 2521
74a0d9f6 2522static void
12359b5e 2523create_signatured_type_table_from_index
168c9250
SM
2524 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2525 const gdb_byte *bytes, offset_type elements)
1fd400ff 2526{
168c9250
SM
2527 gdb_assert (per_bfd->all_type_units.empty ());
2528 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2529
298e9637 2530 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2531
12359b5e 2532 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2533 {
52dc124a 2534 struct signatured_type *sig_type;
9c541725 2535 ULONGEST signature;
1fd400ff 2536 void **slot;
9c541725 2537 cu_offset type_offset_in_tu;
1fd400ff 2538
74a0d9f6 2539 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2540 sect_offset sect_off
2541 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2542 type_offset_in_tu
2543 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2544 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2545 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2546 bytes += 3 * 8;
2547
168c9250 2548 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2549 sig_type->signature = signature;
9c541725 2550 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2551 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2552 sig_type->per_cu.section = section;
9c541725 2553 sig_type->per_cu.sect_off = sect_off;
52dc124a 2554 sig_type->per_cu.v.quick
168c9250 2555 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2556 struct dwarf2_per_cu_quick_data);
2557
b0b6a987 2558 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2559 *slot = sig_type;
1fd400ff 2560
168c9250 2561 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2562 }
2563
168c9250 2564 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2565}
2566
927aa2e7
JK
2567/* Create the signatured type hash table from .debug_names. */
2568
2569static void
2570create_signatured_type_table_from_debug_names
ed2dc618 2571 (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2572 const mapped_debug_names &map,
2573 struct dwarf2_section_info *section,
2574 struct dwarf2_section_info *abbrev_section)
2575{
ed2dc618
SM
2576 struct objfile *objfile = dwarf2_per_objfile->objfile;
2577
96b79293
TT
2578 section->read (objfile);
2579 abbrev_section->read (objfile);
927aa2e7 2580
5989a64e
SM
2581 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
2582 dwarf2_per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2583
298e9637 2584 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2585
2586 for (uint32_t i = 0; i < map.tu_count; ++i)
2587 {
2588 struct signatured_type *sig_type;
927aa2e7 2589 void **slot;
927aa2e7
JK
2590
2591 sect_offset sect_off
2592 = (sect_offset) (extract_unsigned_integer
2593 (map.tu_table_reordered + i * map.offset_size,
2594 map.offset_size,
2595 map.dwarf5_byte_order));
2596
2597 comp_unit_head cu_header;
ed2dc618
SM
2598 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
2599 abbrev_section,
927aa2e7
JK
2600 section->buffer + to_underlying (sect_off),
2601 rcuh_kind::TYPE);
2602
5989a64e 2603 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2604 sig_type->signature = cu_header.signature;
2605 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2606 sig_type->per_cu.is_debug_types = 1;
2607 sig_type->per_cu.section = section;
2608 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2609 sig_type->per_cu.v.quick
5989a64e 2610 = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
927aa2e7
JK
2611 struct dwarf2_per_cu_quick_data);
2612
b0b6a987 2613 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2614 *slot = sig_type;
2615
5989a64e 2616 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2617 }
2618
5989a64e 2619 dwarf2_per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2620}
2621
9291a0cd
TT
2622/* Read the address map data from the mapped index, and use it to
2623 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2624
9291a0cd 2625static void
ed2dc618
SM
2626create_addrmap_from_index (struct dwarf2_per_objfile *dwarf2_per_objfile,
2627 struct mapped_index *index)
9291a0cd 2628{
ed2dc618 2629 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 2630 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2631 const gdb_byte *iter, *end;
9291a0cd 2632 struct addrmap *mutable_map;
9291a0cd
TT
2633 CORE_ADDR baseaddr;
2634
8268c778
PA
2635 auto_obstack temp_obstack;
2636
9291a0cd
TT
2637 mutable_map = addrmap_create_mutable (&temp_obstack);
2638
f00a2de2
PA
2639 iter = index->address_table.data ();
2640 end = iter + index->address_table.size ();
9291a0cd 2641
b3b3bada 2642 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2643
2644 while (iter < end)
2645 {
2646 ULONGEST hi, lo, cu_index;
2647 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2648 iter += 8;
2649 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2650 iter += 8;
2651 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2652 iter += 4;
f652bce2 2653
24a55014 2654 if (lo > hi)
f652bce2 2655 {
b98664d3 2656 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2657 hex_string (lo), hex_string (hi));
24a55014 2658 continue;
f652bce2 2659 }
24a55014 2660
5989a64e 2661 if (cu_index >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2662 {
b98664d3 2663 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2664 (unsigned) cu_index);
24a55014 2665 continue;
f652bce2 2666 }
24a55014 2667
79748972
TT
2668 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2669 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2670 addrmap_set_empty (mutable_map, lo, hi - 1,
5989a64e 2671 dwarf2_per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2672 }
2673
d320c2b5 2674 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2675 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2676}
2677
927aa2e7
JK
2678/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2679 populate the objfile's psymtabs_addrmap. */
2680
2681static void
ed2dc618 2682create_addrmap_from_aranges (struct dwarf2_per_objfile *dwarf2_per_objfile,
927aa2e7
JK
2683 struct dwarf2_section_info *section)
2684{
ed2dc618 2685 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7 2686 bfd *abfd = objfile->obfd;
08feed99 2687 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2688 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2689
2690 auto_obstack temp_obstack;
2691 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2692
2693 std::unordered_map<sect_offset,
2694 dwarf2_per_cu_data *,
2695 gdb::hash_enum<sect_offset>>
2696 debug_info_offset_to_per_cu;
5989a64e 2697 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 2698 {
927aa2e7
JK
2699 const auto insertpair
2700 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2701 if (!insertpair.second)
2702 {
2703 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2704 "debug_info_offset %s, ignoring .debug_aranges."),
2705 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2706 return;
2707 }
2708 }
2709
96b79293 2710 section->read (objfile);
927aa2e7
JK
2711
2712 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2713
2714 const gdb_byte *addr = section->buffer;
2715
2716 while (addr < section->buffer + section->size)
2717 {
2718 const gdb_byte *const entry_addr = addr;
2719 unsigned int bytes_read;
2720
2721 const LONGEST entry_length = read_initial_length (abfd, addr,
2722 &bytes_read);
2723 addr += bytes_read;
2724
2725 const gdb_byte *const entry_end = addr + entry_length;
2726 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2727 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2728 if (addr + entry_length > section->buffer + section->size)
2729 {
47e3f474 2730 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2731 "length %s exceeds section length %s, "
2732 "ignoring .debug_aranges."),
47e3f474
TV
2733 objfile_name (objfile),
2734 plongest (entry_addr - section->buffer),
927aa2e7
JK
2735 plongest (bytes_read + entry_length),
2736 pulongest (section->size));
2737 return;
2738 }
2739
2740 /* The version number. */
2741 const uint16_t version = read_2_bytes (abfd, addr);
2742 addr += 2;
2743 if (version != 2)
2744 {
47e3f474 2745 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2746 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2747 objfile_name (objfile),
2748 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2749 return;
2750 }
2751
2752 const uint64_t debug_info_offset
2753 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2754 addr += offset_size;
2755 const auto per_cu_it
2756 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2757 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2758 {
47e3f474 2759 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2760 "debug_info_offset %s does not exists, "
2761 "ignoring .debug_aranges."),
47e3f474
TV
2762 objfile_name (objfile),
2763 plongest (entry_addr - section->buffer),
927aa2e7
JK
2764 pulongest (debug_info_offset));
2765 return;
2766 }
2767 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2768
2769 const uint8_t address_size = *addr++;
2770 if (address_size < 1 || address_size > 8)
2771 {
47e3f474 2772 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2773 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2774 objfile_name (objfile),
2775 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2776 return;
2777 }
2778
2779 const uint8_t segment_selector_size = *addr++;
2780 if (segment_selector_size != 0)
2781 {
47e3f474 2782 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2783 "segment_selector_size %u is not supported, "
2784 "ignoring .debug_aranges."),
47e3f474
TV
2785 objfile_name (objfile),
2786 plongest (entry_addr - section->buffer),
927aa2e7
JK
2787 segment_selector_size);
2788 return;
2789 }
2790
2791 /* Must pad to an alignment boundary that is twice the address
2792 size. It is undocumented by the DWARF standard but GCC does
2793 use it. */
2794 for (size_t padding = ((-(addr - section->buffer))
2795 & (2 * address_size - 1));
2796 padding > 0; padding--)
2797 if (*addr++ != 0)
2798 {
47e3f474 2799 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2800 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2801 objfile_name (objfile),
2802 plongest (entry_addr - section->buffer));
927aa2e7
JK
2803 return;
2804 }
2805
2806 for (;;)
2807 {
2808 if (addr + 2 * address_size > entry_end)
2809 {
47e3f474 2810 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2811 "address list is not properly terminated, "
2812 "ignoring .debug_aranges."),
47e3f474
TV
2813 objfile_name (objfile),
2814 plongest (entry_addr - section->buffer));
927aa2e7
JK
2815 return;
2816 }
2817 ULONGEST start = extract_unsigned_integer (addr, address_size,
2818 dwarf5_byte_order);
2819 addr += address_size;
2820 ULONGEST length = extract_unsigned_integer (addr, address_size,
2821 dwarf5_byte_order);
2822 addr += address_size;
2823 if (start == 0 && length == 0)
2824 break;
5989a64e 2825 if (start == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
2826 {
2827 /* Symbol was eliminated due to a COMDAT group. */
2828 continue;
2829 }
2830 ULONGEST end = start + length;
79748972
TT
2831 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
2832 - baseaddr);
2833 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
2834 - baseaddr);
927aa2e7
JK
2835 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
2836 }
2837 }
2838
d320c2b5 2839 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2840 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
2841}
2842
9291a0cd
TT
2843/* Find a slot in the mapped index INDEX for the object named NAME.
2844 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
2845 constant pool and return true. If NAME cannot be found, return
2846 false. */
2fdf6df6 2847
109483d9 2848static bool
9291a0cd
TT
2849find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2850 offset_type **vec_out)
2851{
0cf03b49 2852 offset_type hash;
9291a0cd 2853 offset_type slot, step;
559a7a62 2854 int (*cmp) (const char *, const char *);
9291a0cd 2855
791afaa2 2856 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 2857 if (current_language->la_language == language_cplus
45280282
IB
2858 || current_language->la_language == language_fortran
2859 || current_language->la_language == language_d)
0cf03b49
JK
2860 {
2861 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2862 not contain any. */
a8719064 2863
72998fb3 2864 if (strchr (name, '(') != NULL)
0cf03b49 2865 {
109483d9 2866 without_params = cp_remove_params (name);
0cf03b49 2867
72998fb3 2868 if (without_params != NULL)
791afaa2 2869 name = without_params.get ();
0cf03b49
JK
2870 }
2871 }
2872
559a7a62 2873 /* Index version 4 did not support case insensitive searches. But the
feea76c2 2874 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
2875 simulate our NAME being searched is also lowercased. */
2876 hash = mapped_index_string_hash ((index->version == 4
2877 && case_sensitivity == case_sensitive_off
2878 ? 5 : index->version),
2879 name);
2880
f00a2de2
PA
2881 slot = hash & (index->symbol_table.size () - 1);
2882 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 2883 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
2884
2885 for (;;)
2886 {
9291a0cd 2887 const char *str;
f00a2de2
PA
2888
2889 const auto &bucket = index->symbol_table[slot];
2890 if (bucket.name == 0 && bucket.vec == 0)
109483d9 2891 return false;
9291a0cd 2892
f00a2de2 2893 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 2894 if (!cmp (name, str))
9291a0cd
TT
2895 {
2896 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 2897 + MAYBE_SWAP (bucket.vec));
109483d9 2898 return true;
9291a0cd
TT
2899 }
2900
f00a2de2 2901 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
2902 }
2903}
2904
4485a1c1
SM
2905/* A helper function that reads the .gdb_index from BUFFER and fills
2906 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 2907 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
2908 ok to use deprecated sections.
2909
2910 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
2911 out parameters that are filled in with information about the CU and
2912 TU lists in the section.
2913
4485a1c1 2914 Returns true if all went well, false otherwise. */
2fdf6df6 2915
d33bc52e 2916static bool
3810f182 2917read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
2918 bool deprecated_ok,
2919 gdb::array_view<const gdb_byte> buffer,
2920 struct mapped_index *map,
2921 const gdb_byte **cu_list,
2922 offset_type *cu_list_elements,
2923 const gdb_byte **types_list,
2924 offset_type *types_list_elements)
2925{
2926 const gdb_byte *addr = &buffer[0];
82430852 2927
9291a0cd 2928 /* Version check. */
4485a1c1 2929 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 2930 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 2931 causes the index to behave very poorly for certain requests. Version 3
831adc1f 2932 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 2933 indices. */
831adc1f 2934 if (version < 4)
481860b3
GB
2935 {
2936 static int warning_printed = 0;
2937 if (!warning_printed)
2938 {
2939 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 2940 filename);
481860b3
GB
2941 warning_printed = 1;
2942 }
2943 return 0;
2944 }
2945 /* Index version 4 uses a different hash function than index version
2946 5 and later.
2947
2948 Versions earlier than 6 did not emit psymbols for inlined
2949 functions. Using these files will cause GDB not to be able to
2950 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
2951 indices unless the user has done
2952 "set use-deprecated-index-sections on". */
2ec9a5e0 2953 if (version < 6 && !deprecated_ok)
481860b3
GB
2954 {
2955 static int warning_printed = 0;
2956 if (!warning_printed)
2957 {
e615022a
DE
2958 warning (_("\
2959Skipping deprecated .gdb_index section in %s.\n\
2960Do \"set use-deprecated-index-sections on\" before the file is read\n\
2961to use the section anyway."),
2ec9a5e0 2962 filename);
481860b3
GB
2963 warning_printed = 1;
2964 }
2965 return 0;
2966 }
796a7ff8 2967 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
2968 of the TU (for symbols coming from TUs),
2969 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
2970 Plus gold-generated indices can have duplicate entries for global symbols,
2971 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
2972 These are just performance bugs, and we can't distinguish gdb-generated
2973 indices from gold-generated ones, so issue no warning here. */
796a7ff8 2974
481860b3 2975 /* Indexes with higher version than the one supported by GDB may be no
594e8718 2976 longer backward compatible. */
796a7ff8 2977 if (version > 8)
594e8718 2978 return 0;
9291a0cd 2979
559a7a62 2980 map->version = version;
9291a0cd 2981
4485a1c1 2982 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 2983
4485a1c1 2984 int i = 0;
2ec9a5e0
TT
2985 *cu_list = addr + MAYBE_SWAP (metadata[i]);
2986 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2987 / 8);
1fd400ff
TT
2988 ++i;
2989
2ec9a5e0
TT
2990 *types_list = addr + MAYBE_SWAP (metadata[i]);
2991 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2992 - MAYBE_SWAP (metadata[i]))
2993 / 8);
987d643c 2994 ++i;
1fd400ff 2995
f00a2de2
PA
2996 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
2997 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
2998 map->address_table
2999 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3000 ++i;
3001
f00a2de2
PA
3002 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3003 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3004 map->symbol_table
3005 = gdb::array_view<mapped_index::symbol_table_slot>
3006 ((mapped_index::symbol_table_slot *) symbol_table,
3007 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3008
f00a2de2 3009 ++i;
f9d83a0b 3010 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3011
2ec9a5e0
TT
3012 return 1;
3013}
3014
4485a1c1
SM
3015/* Callback types for dwarf2_read_gdb_index. */
3016
3017typedef gdb::function_view
5989a64e 3018 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3019 get_gdb_index_contents_ftype;
3020typedef gdb::function_view
3021 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3022 get_gdb_index_contents_dwz_ftype;
3023
927aa2e7 3024/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3025 elements of all the CUs and return 1. Otherwise, return 0. */
3026
3027static int
4485a1c1
SM
3028dwarf2_read_gdb_index
3029 (struct dwarf2_per_objfile *dwarf2_per_objfile,
3030 get_gdb_index_contents_ftype get_gdb_index_contents,
3031 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3032{
2ec9a5e0
TT
3033 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3034 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3035 struct dwz_file *dwz;
12359b5e 3036 struct objfile *objfile = dwarf2_per_objfile->objfile;
2ec9a5e0 3037
4485a1c1 3038 gdb::array_view<const gdb_byte> main_index_contents
5989a64e 3039 = get_gdb_index_contents (objfile, dwarf2_per_objfile->per_bfd);
4485a1c1
SM
3040
3041 if (main_index_contents.empty ())
3042 return 0;
3043
3063847f 3044 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3045 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3046 use_deprecated_index_sections,
3047 main_index_contents, map.get (), &cu_list,
3048 &cu_list_elements, &types_list,
3049 &types_list_elements))
2ec9a5e0
TT
3050 return 0;
3051
0fefef59 3052 /* Don't use the index if it's empty. */
3063847f 3053 if (map->symbol_table.empty ())
0fefef59
DE
3054 return 0;
3055
2ec9a5e0
TT
3056 /* If there is a .dwz file, read it so we can get its CU list as
3057 well. */
c3699833 3058 dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 3059 if (dwz != NULL)
2ec9a5e0 3060 {
2ec9a5e0
TT
3061 struct mapped_index dwz_map;
3062 const gdb_byte *dwz_types_ignore;
3063 offset_type dwz_types_elements_ignore;
3064
4485a1c1
SM
3065 gdb::array_view<const gdb_byte> dwz_index_content
3066 = get_gdb_index_contents_dwz (objfile, dwz);
3067
3068 if (dwz_index_content.empty ())
3069 return 0;
3070
3810f182 3071 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3072 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3073 &dwz_list, &dwz_list_elements,
3074 &dwz_types_ignore,
3075 &dwz_types_elements_ignore))
2ec9a5e0
TT
3076 {
3077 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3078 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3079 return 0;
3080 }
3081 }
3082
168c9250 3083 create_cus_from_index (dwarf2_per_objfile->per_bfd, cu_list, cu_list_elements,
12359b5e 3084 dwz_list, dwz_list_elements);
1fd400ff 3085
8b70b953
TT
3086 if (types_list_elements)
3087 {
8b70b953
TT
3088 /* We can only handle a single .debug_types when we have an
3089 index. */
5989a64e 3090 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
8b70b953
TT
3091 return 0;
3092
5989a64e 3093 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
8b70b953 3094
168c9250
SM
3095 create_signatured_type_table_from_index (dwarf2_per_objfile->per_bfd,
3096 section, types_list,
3097 types_list_elements);
8b70b953 3098 }
9291a0cd 3099
3063847f 3100 create_addrmap_from_index (dwarf2_per_objfile, map.get ());
9291a0cd 3101
5989a64e
SM
3102 dwarf2_per_objfile->per_bfd->index_table = std::move (map);
3103 dwarf2_per_objfile->per_bfd->using_index = 1;
3104 dwarf2_per_objfile->per_bfd->quick_file_names_table =
3105 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
9291a0cd
TT
3106
3107 return 1;
3108}
3109
dee91e82 3110/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3111
dee91e82
DE
3112static void
3113dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3114 const gdb_byte *info_ptr,
3e225074 3115 struct die_info *comp_unit_die)
9291a0cd 3116{
dee91e82 3117 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3118 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
5e22e966 3119 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
094b34ac 3120 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3121 struct attribute *attr;
7b9f3c50
DE
3122 void **slot;
3123 struct quick_file_names *qfn;
9291a0cd 3124
0186c6a7
DE
3125 gdb_assert (! this_cu->is_debug_types);
3126
07261596
TT
3127 /* Our callers never want to match partial units -- instead they
3128 will match the enclosing full CU. */
3129 if (comp_unit_die->tag == DW_TAG_partial_unit)
3130 {
3131 this_cu->v.quick->no_file_data = 1;
3132 return;
3133 }
3134
0186c6a7 3135 lh_cu = this_cu;
7b9f3c50 3136 slot = NULL;
dee91e82 3137
fff8551c 3138 line_header_up lh;
9c541725 3139 sect_offset line_offset {};
fff8551c 3140
dee91e82 3141 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
435d3d88 3142 if (attr != nullptr)
9291a0cd 3143 {
7b9f3c50
DE
3144 struct quick_file_names find_entry;
3145
9c541725 3146 line_offset = (sect_offset) DW_UNSND (attr);
7b9f3c50
DE
3147
3148 /* We may have already read in this line header (TU line header sharing).
3149 If we have we're done. */
094b34ac 3150 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3151 find_entry.hash.line_sect_off = line_offset;
5989a64e 3152 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3153 &find_entry, INSERT);
3154 if (*slot != NULL)
3155 {
9a3c8263 3156 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3157 return;
7b9f3c50
DE
3158 }
3159
3019eac3 3160 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3161 }
3162 if (lh == NULL)
3163 {
094b34ac 3164 lh_cu->v.quick->no_file_data = 1;
dee91e82 3165 return;
9291a0cd
TT
3166 }
3167
5989a64e 3168 qfn = XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3169 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3170 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3171 gdb_assert (slot != NULL);
3172 *slot = qfn;
9291a0cd 3173
d721ba37 3174 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3175
aa391654
TT
3176 int offset = 0;
3177 if (strcmp (fnd.name, "<unknown>") != 0)
3178 ++offset;
3179
7ba99d21 3180 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3181 qfn->file_names =
5989a64e 3182 XOBNEWVEC (&dwarf2_per_objfile->per_bfd->obstack, const char *,
45940949 3183 qfn->num_file_names);
aa391654
TT
3184 if (offset != 0)
3185 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3186 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3187 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3188 fnd.comp_dir).release ();
7b9f3c50 3189 qfn->real_names = NULL;
9291a0cd 3190
094b34ac 3191 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3192}
3193
3194/* A helper for the "quick" functions which attempts to read the line
3195 table for THIS_CU. */
3196
3197static struct quick_file_names *
ab432490
SM
3198dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3199 dwarf2_per_objfile *per_objfile)
dee91e82 3200{
0186c6a7
DE
3201 /* This should never be called for TUs. */
3202 gdb_assert (! this_cu->is_debug_types);
3203 /* Nor type unit groups. */
197400e8 3204 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3205
dee91e82
DE
3206 if (this_cu->v.quick->file_names != NULL)
3207 return this_cu->v.quick->file_names;
3208 /* If we know there is no line data, no point in looking again. */
3209 if (this_cu->v.quick->no_file_data)
3210 return NULL;
3211
ab432490 3212 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3213 if (!reader.dummy_p)
3e225074 3214 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3215
3216 if (this_cu->v.quick->no_file_data)
3217 return NULL;
3218 return this_cu->v.quick->file_names;
9291a0cd
TT
3219}
3220
3221/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3222 real path for a given file name from the line table. */
2fdf6df6 3223
9291a0cd 3224static const char *
45940949 3225dw2_get_real_path (struct dwarf2_per_objfile *dwarf2_per_objfile,
7b9f3c50 3226 struct quick_file_names *qfn, int index)
9291a0cd 3227{
7b9f3c50 3228 if (qfn->real_names == NULL)
5989a64e 3229 qfn->real_names = OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
26f2dc30 3230 qfn->num_file_names, const char *);
9291a0cd 3231
7b9f3c50 3232 if (qfn->real_names[index] == NULL)
14278e1f 3233 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3234
7b9f3c50 3235 return qfn->real_names[index];
9291a0cd
TT
3236}
3237
3238static struct symtab *
3239dw2_find_last_source_symtab (struct objfile *objfile)
3240{
ed2dc618
SM
3241 struct dwarf2_per_objfile *dwarf2_per_objfile
3242 = get_dwarf2_per_objfile (objfile);
5989a64e 3243 dwarf2_per_cu_data *dwarf_cu = dwarf2_per_objfile->per_bfd->all_comp_units.back ();
97a1449a
SM
3244 compunit_symtab *cust
3245 = dw2_instantiate_symtab (dwarf_cu, dwarf2_per_objfile, false);
ae2de4f8 3246
43f3e411
DE
3247 if (cust == NULL)
3248 return NULL;
ed2dc618 3249
43f3e411 3250 return compunit_primary_filetab (cust);
9291a0cd
TT
3251}
3252
7b9f3c50
DE
3253/* Traversal function for dw2_forget_cached_source_info. */
3254
3255static int
3256dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3257{
7b9f3c50 3258 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3259
7b9f3c50 3260 if (file_data->real_names)
9291a0cd 3261 {
7b9f3c50 3262 int i;
9291a0cd 3263
7b9f3c50 3264 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3265 {
7b9f3c50
DE
3266 xfree ((void*) file_data->real_names[i]);
3267 file_data->real_names[i] = NULL;
9291a0cd
TT
3268 }
3269 }
7b9f3c50
DE
3270
3271 return 1;
3272}
3273
3274static void
3275dw2_forget_cached_source_info (struct objfile *objfile)
3276{
ed2dc618
SM
3277 struct dwarf2_per_objfile *dwarf2_per_objfile
3278 = get_dwarf2_per_objfile (objfile);
7b9f3c50 3279
5989a64e 3280 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3281 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3282}
3283
f8eba3c6
TT
3284/* Helper function for dw2_map_symtabs_matching_filename that expands
3285 the symtabs and calls the iterator. */
3286
3287static int
3288dw2_map_expand_apply (struct objfile *objfile,
3289 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3290 const char *name, const char *real_path,
14bc53a8 3291 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3292{
43f3e411 3293 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3294
3295 /* Don't visit already-expanded CUs. */
af758d11
SM
3296 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3297 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3298 return 0;
3299
3300 /* This may expand more than one symtab, and we want to iterate over
3301 all of them. */
97a1449a 3302 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3303
14bc53a8
PA
3304 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3305 last_made, callback);
f8eba3c6
TT
3306}
3307
3308/* Implementation of the map_symtabs_matching_filename method. */
3309
14bc53a8
PA
3310static bool
3311dw2_map_symtabs_matching_filename
3312 (struct objfile *objfile, const char *name, const char *real_path,
3313 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3314{
c011a4f4 3315 const char *name_basename = lbasename (name);
ed2dc618
SM
3316 struct dwarf2_per_objfile *dwarf2_per_objfile
3317 = get_dwarf2_per_objfile (objfile);
ae2de4f8 3318
848e3e78
DE
3319 /* The rule is CUs specify all the files, including those used by
3320 any TU, so there's no need to scan TUs here. */
f4dc4d17 3321
5989a64e 3322 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3323 {
3d7bb9d9 3324 /* We only need to look at symtabs not already expanded. */
af758d11 3325 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3326 continue;
3327
ab432490
SM
3328 quick_file_names *file_data
3329 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3330 if (file_data == NULL)
9291a0cd
TT
3331 continue;
3332
b76e467d 3333 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3334 {
7b9f3c50 3335 const char *this_name = file_data->file_names[j];
da235a7c 3336 const char *this_real_name;
9291a0cd 3337
af529f8f 3338 if (compare_filenames_for_search (this_name, name))
9291a0cd 3339 {
f5b95b50 3340 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3341 callback))
3342 return true;
288e77a7 3343 continue;
4aac40c8 3344 }
9291a0cd 3345
c011a4f4
DE
3346 /* Before we invoke realpath, which can get expensive when many
3347 files are involved, do a quick comparison of the basenames. */
3348 if (! basenames_may_differ
3349 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3350 continue;
3351
45940949
TT
3352 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
3353 file_data, j);
da235a7c 3354 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3355 {
da235a7c 3356 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3357 callback))
3358 return true;
288e77a7 3359 continue;
da235a7c 3360 }
9291a0cd 3361
da235a7c
JK
3362 if (real_path != NULL)
3363 {
af529f8f
JK
3364 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3365 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3366 if (this_real_name != NULL
af529f8f 3367 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3368 {
f5b95b50 3369 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3370 callback))
3371 return true;
288e77a7 3372 continue;
9291a0cd
TT
3373 }
3374 }
3375 }
3376 }
3377
14bc53a8 3378 return false;
9291a0cd
TT
3379}
3380
da51c347
DE
3381/* Struct used to manage iterating over all CUs looking for a symbol. */
3382
3383struct dw2_symtab_iterator
9291a0cd 3384{
ed2dc618
SM
3385 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
3386 struct dwarf2_per_objfile *dwarf2_per_objfile;
2b79f376
SM
3387 /* If set, only look for symbols that match that block. Valid values are
3388 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3389 gdb::optional<block_enum> block_index;
da51c347
DE
3390 /* The kind of symbol we're looking for. */
3391 domain_enum domain;
3392 /* The list of CUs from the index entry of the symbol,
3393 or NULL if not found. */
3394 offset_type *vec;
3395 /* The next element in VEC to look at. */
3396 int next;
3397 /* The number of elements in VEC, or zero if there is no match. */
3398 int length;
8943b874
DE
3399 /* Have we seen a global version of the symbol?
3400 If so we can ignore all further global instances.
3401 This is to work around gold/15646, inefficient gold-generated
3402 indices. */
3403 int global_seen;
da51c347 3404};
9291a0cd 3405
2b79f376 3406/* Initialize the index symtab iterator ITER. */
2fdf6df6 3407
9291a0cd 3408static void
da51c347 3409dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
ed2dc618 3410 struct dwarf2_per_objfile *dwarf2_per_objfile,
c7f839cb 3411 gdb::optional<block_enum> block_index,
da51c347
DE
3412 domain_enum domain,
3413 const char *name)
3414{
ed2dc618 3415 iter->dwarf2_per_objfile = dwarf2_per_objfile;
da51c347
DE
3416 iter->block_index = block_index;
3417 iter->domain = domain;
3418 iter->next = 0;
8943b874 3419 iter->global_seen = 0;
da51c347 3420
5989a64e 3421 mapped_index *index = dwarf2_per_objfile->per_bfd->index_table.get ();
ed2dc618
SM
3422
3423 /* index is NULL if OBJF_READNOW. */
3424 if (index != NULL && find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347
DE
3425 iter->length = MAYBE_SWAP (*iter->vec);
3426 else
3427 {
3428 iter->vec = NULL;
3429 iter->length = 0;
3430 }
3431}
3432
3433/* Return the next matching CU or NULL if there are no more. */
3434
3435static struct dwarf2_per_cu_data *
3436dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3437{
ed2dc618
SM
3438 struct dwarf2_per_objfile *dwarf2_per_objfile = iter->dwarf2_per_objfile;
3439
da51c347
DE
3440 for ( ; iter->next < iter->length; ++iter->next)
3441 {
3442 offset_type cu_index_and_attrs =
3443 MAYBE_SWAP (iter->vec[iter->next + 1]);
3444 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3445 gdb_index_symbol_kind symbol_kind =
3446 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3447 /* Only check the symbol attributes if they're present.
3448 Indices prior to version 7 don't record them,
3449 and indices >= 7 may elide them for certain symbols
3450 (gold does this). */
3451 int attrs_valid =
5989a64e 3452 (dwarf2_per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3453 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3454
3190f0c6 3455 /* Don't crash on bad data. */
5989a64e
SM
3456 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3457 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3458 {
b98664d3 3459 complaint (_(".gdb_index entry has bad CU index"
4262abfb
JK
3460 " [in module %s]"),
3461 objfile_name (dwarf2_per_objfile->objfile));
3190f0c6
DE
3462 continue;
3463 }
3464
5989a64e 3465 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3466
da51c347 3467 /* Skip if already read in. */
af758d11 3468 if (dwarf2_per_objfile->symtab_set_p (per_cu))
da51c347
DE
3469 continue;
3470
8943b874
DE
3471 /* Check static vs global. */
3472 if (attrs_valid)
3473 {
2b79f376
SM
3474 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3475
3476 if (iter->block_index.has_value ())
3477 {
3478 bool want_static = *iter->block_index == STATIC_BLOCK;
3479
3480 if (is_static != want_static)
3481 continue;
3482 }
3483
8943b874
DE
3484 /* Work around gold/15646. */
3485 if (!is_static && iter->global_seen)
3486 continue;
3487 if (!is_static)
3488 iter->global_seen = 1;
3489 }
da51c347
DE
3490
3491 /* Only check the symbol's kind if it has one. */
3492 if (attrs_valid)
3493 {
3494 switch (iter->domain)
3495 {
3496 case VAR_DOMAIN:
3497 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3498 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3499 /* Some types are also in VAR_DOMAIN. */
3500 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3501 continue;
3502 break;
3503 case STRUCT_DOMAIN:
3504 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3505 continue;
3506 break;
3507 case LABEL_DOMAIN:
3508 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3509 continue;
3510 break;
59c35742
AB
3511 case MODULE_DOMAIN:
3512 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3513 continue;
3514 break;
da51c347
DE
3515 default:
3516 break;
3517 }
3518 }
3519
3520 ++iter->next;
3521 return per_cu;
3522 }
3523
3524 return NULL;
3525}
3526
43f3e411 3527static struct compunit_symtab *
c7f839cb 3528dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3529 const char *name, domain_enum domain)
9291a0cd 3530{
43f3e411 3531 struct compunit_symtab *stab_best = NULL;
ed2dc618
SM
3532 struct dwarf2_per_objfile *dwarf2_per_objfile
3533 = get_dwarf2_per_objfile (objfile);
9291a0cd 3534
b5ec771e
PA
3535 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3536
ed2dc618
SM
3537 struct dw2_symtab_iterator iter;
3538 struct dwarf2_per_cu_data *per_cu;
da51c347 3539
2b79f376 3540 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_index, domain, name);
9291a0cd 3541
ed2dc618
SM
3542 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3543 {
3544 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
3545 struct compunit_symtab *stab
3546 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
ed2dc618 3547 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3548 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3549
ed2dc618
SM
3550 sym = block_find_symbol (block, name, domain,
3551 block_find_non_opaque_type_preferred,
3552 &with_opaque);
b2e2f908 3553
ed2dc618
SM
3554 /* Some caution must be observed with overloaded functions
3555 and methods, since the index will not contain any overload
3556 information (but NAME might contain it). */
da51c347 3557
ed2dc618
SM
3558 if (sym != NULL
3559 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3560 return stab;
3561 if (with_opaque != NULL
3562 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3563 stab_best = stab;
da51c347 3564
ed2dc618 3565 /* Keep looking through other CUs. */
9291a0cd 3566 }
9291a0cd 3567
da51c347 3568 return stab_best;
9291a0cd
TT
3569}
3570
3571static void
3572dw2_print_stats (struct objfile *objfile)
3573{
ed2dc618
SM
3574 struct dwarf2_per_objfile *dwarf2_per_objfile
3575 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3576 int total = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3577 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3578 int count = 0;
9291a0cd 3579
ed2dc618 3580 for (int i = 0; i < total; ++i)
9291a0cd 3581 {
5989a64e 3582 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3583
af758d11 3584 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3585 ++count;
3586 }
e4a48d9d 3587 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3588 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3589}
3590
779bd270
DE
3591/* This dumps minimal information about the index.
3592 It is called via "mt print objfiles".
3593 One use is to verify .gdb_index has been loaded by the
3594 gdb.dwarf2/gdb-index.exp testcase. */
3595
9291a0cd
TT
3596static void
3597dw2_dump (struct objfile *objfile)
3598{
ed2dc618
SM
3599 struct dwarf2_per_objfile *dwarf2_per_objfile
3600 = get_dwarf2_per_objfile (objfile);
3601
5989a64e 3602 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
779bd270 3603 printf_filtered (".gdb_index:");
5989a64e 3604 if (dwarf2_per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3605 {
3606 printf_filtered (" version %d\n",
5989a64e 3607 dwarf2_per_objfile->per_bfd->index_table->version);
779bd270
DE
3608 }
3609 else
3610 printf_filtered (" faked for \"readnow\"\n");
3611 printf_filtered ("\n");
9291a0cd
TT
3612}
3613
9291a0cd
TT
3614static void
3615dw2_expand_symtabs_for_function (struct objfile *objfile,
3616 const char *func_name)
3617{
ed2dc618
SM
3618 struct dwarf2_per_objfile *dwarf2_per_objfile
3619 = get_dwarf2_per_objfile (objfile);
da51c347 3620
ed2dc618
SM
3621 struct dw2_symtab_iterator iter;
3622 struct dwarf2_per_cu_data *per_cu;
da51c347 3623
2b79f376 3624 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3625
ed2dc618 3626 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a 3627 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
da51c347 3628
9291a0cd
TT
3629}
3630
3631static void
3632dw2_expand_all_symtabs (struct objfile *objfile)
3633{
ed2dc618
SM
3634 struct dwarf2_per_objfile *dwarf2_per_objfile
3635 = get_dwarf2_per_objfile (objfile);
5989a64e
SM
3636 int total_units = (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
3637 + dwarf2_per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3638
ed2dc618 3639 for (int i = 0; i < total_units; ++i)
9291a0cd 3640 {
5989a64e 3641 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 3642
58f0c718
TT
3643 /* We don't want to directly expand a partial CU, because if we
3644 read it with the wrong language, then assertion failures can
3645 be triggered later on. See PR symtab/23010. So, tell
3646 dw2_instantiate_symtab to skip partial CUs -- any important
3647 partial CU will be read via DW_TAG_imported_unit anyway. */
97a1449a 3648 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, true);
9291a0cd
TT
3649 }
3650}
3651
3652static void
652a8996
JK
3653dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3654 const char *fullname)
9291a0cd 3655{
ed2dc618
SM
3656 struct dwarf2_per_objfile *dwarf2_per_objfile
3657 = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3658
3659 /* We don't need to consider type units here.
3660 This is only called for examining code, e.g. expand_line_sal.
3661 There can be an order of magnitude (or more) more type units
3662 than comp units, and we avoid them if we can. */
3663
5989a64e 3664 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
9291a0cd 3665 {
3d7bb9d9 3666 /* We only need to look at symtabs not already expanded. */
af758d11 3667 if (dwarf2_per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3668 continue;
3669
ab432490
SM
3670 quick_file_names *file_data
3671 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
7b9f3c50 3672 if (file_data == NULL)
9291a0cd
TT
3673 continue;
3674
b76e467d 3675 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3676 {
652a8996
JK
3677 const char *this_fullname = file_data->file_names[j];
3678
3679 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3680 {
97a1449a 3681 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
9291a0cd
TT
3682 break;
3683 }
3684 }
3685 }
3686}
3687
9a0bacfb
TV
3688static void
3689dw2_expand_symtabs_matching_symbol
3690 (mapped_index_base &index,
3691 const lookup_name_info &lookup_name_in,
3692 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3693 enum search_domain kind,
3694 gdb::function_view<bool (offset_type)> match_callback);
3695
3696static void
3697dw2_expand_symtabs_matching_one
97a1449a
SM
3698 (dwarf2_per_cu_data *per_cu,
3699 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3700 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3701 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3702
9291a0cd 3703static void
199b4314
TT
3704dw2_map_matching_symbols
3705 (struct objfile *objfile,
b054970d 3706 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3707 int global,
3708 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3709 symbol_compare_ftype *ordered_compare)
9291a0cd 3710{
1aa98955
TV
3711 /* Used for Ada. */
3712 struct dwarf2_per_objfile *dwarf2_per_objfile
3713 = get_dwarf2_per_objfile (objfile);
3714
9a0bacfb
TV
3715 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3716
5989a64e 3717 if (dwarf2_per_objfile->per_bfd->index_table != nullptr)
1aa98955
TV
3718 {
3719 /* Ada currently doesn't support .gdb_index (see PR24713). We can get
3720 here though if the current language is Ada for a non-Ada objfile
9a0bacfb 3721 using GNU index. */
5989a64e 3722 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
1aa98955 3723
9a0bacfb
TV
3724 const char *match_name = name.ada ().lookup_name ().c_str ();
3725 auto matcher = [&] (const char *symname)
3726 {
3727 if (ordered_compare == nullptr)
3728 return true;
3729 return ordered_compare (symname, match_name) == 0;
3730 };
3731
3732 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3733 [&] (offset_type namei)
3734 {
3735 struct dw2_symtab_iterator iter;
3736 struct dwarf2_per_cu_data *per_cu;
3737
3738 dw2_symtab_iter_init (&iter, dwarf2_per_objfile, block_kind, domain,
3739 match_name);
3740 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
97a1449a
SM
3741 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
3742 nullptr);
9a0bacfb
TV
3743 return true;
3744 });
3745 }
3746 else
3747 {
3748 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3749 proceed assuming all symtabs have been read in. */
3750 }
1aa98955
TV
3751
3752 for (compunit_symtab *cust : objfile->compunits ())
3753 {
3754 const struct block *block;
3755
3756 if (cust == NULL)
3757 continue;
3758 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3759 if (!iterate_over_symbols_terminated (block, name,
3760 domain, callback))
3761 return;
3762 }
9291a0cd
TT
3763}
3764
e1ef7d7a
PA
3765/* Starting from a search name, return the string that finds the upper
3766 bound of all strings that start with SEARCH_NAME in a sorted name
3767 list. Returns the empty string to indicate that the upper bound is
3768 the end of the list. */
3769
3770static std::string
3771make_sort_after_prefix_name (const char *search_name)
3772{
3773 /* When looking to complete "func", we find the upper bound of all
3774 symbols that start with "func" by looking for where we'd insert
3775 the closest string that would follow "func" in lexicographical
3776 order. Usually, that's "func"-with-last-character-incremented,
3777 i.e. "fund". Mind non-ASCII characters, though. Usually those
3778 will be UTF-8 multi-byte sequences, but we can't be certain.
3779 Especially mind the 0xff character, which is a valid character in
3780 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3781 rule out compilers allowing it in identifiers. Note that
3782 conveniently, strcmp/strcasecmp are specified to compare
3783 characters interpreted as unsigned char. So what we do is treat
3784 the whole string as a base 256 number composed of a sequence of
3785 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3786 to 0, and carries 1 to the following more-significant position.
3787 If the very first character in SEARCH_NAME ends up incremented
3788 and carries/overflows, then the upper bound is the end of the
3789 list. The string after the empty string is also the empty
3790 string.
3791
3792 Some examples of this operation:
3793
3794 SEARCH_NAME => "+1" RESULT
3795
3796 "abc" => "abd"
3797 "ab\xff" => "ac"
3798 "\xff" "a" "\xff" => "\xff" "b"
3799 "\xff" => ""
3800 "\xff\xff" => ""
3801 "" => ""
3802
3803 Then, with these symbols for example:
3804
3805 func
3806 func1
3807 fund
3808
3809 completing "func" looks for symbols between "func" and
3810 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
3811 which finds "func" and "func1", but not "fund".
3812
3813 And with:
3814
3815 funcÿ (Latin1 'ÿ' [0xff])
3816 funcÿ1
3817 fund
3818
3819 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
3820 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
3821
3822 And with:
3823
3824 ÿÿ (Latin1 'ÿ' [0xff])
3825 ÿÿ1
3826
3827 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
3828 the end of the list.
3829 */
3830 std::string after = search_name;
3831 while (!after.empty () && (unsigned char) after.back () == 0xff)
3832 after.pop_back ();
3833 if (!after.empty ())
3834 after.back () = (unsigned char) after.back () + 1;
3835 return after;
3836}
3837
5c58de74 3838/* See declaration. */
61d96d7e 3839
5c58de74
PA
3840std::pair<std::vector<name_component>::const_iterator,
3841 std::vector<name_component>::const_iterator>
44ed8f3e 3842mapped_index_base::find_name_components_bounds
3b00ef10 3843 (const lookup_name_info &lookup_name_without_params, language lang) const
3f563c84 3844{
5c58de74
PA
3845 auto *name_cmp
3846 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 3847
3b00ef10 3848 const char *lang_name
e0802d59 3849 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 3850
3f563c84
PA
3851 /* Comparison function object for lower_bound that matches against a
3852 given symbol name. */
3853 auto lookup_compare_lower = [&] (const name_component &elem,
3854 const char *name)
3855 {
5c58de74 3856 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3857 const char *elem_name = elem_qualified + elem.name_offset;
3858 return name_cmp (elem_name, name) < 0;
3859 };
3860
3861 /* Comparison function object for upper_bound that matches against a
3862 given symbol name. */
3863 auto lookup_compare_upper = [&] (const char *name,
3864 const name_component &elem)
3865 {
5c58de74 3866 const char *elem_qualified = this->symbol_name_at (elem.idx);
3f563c84
PA
3867 const char *elem_name = elem_qualified + elem.name_offset;
3868 return name_cmp (name, elem_name) < 0;
3869 };
3870
5c58de74
PA
3871 auto begin = this->name_components.begin ();
3872 auto end = this->name_components.end ();
3f563c84
PA
3873
3874 /* Find the lower bound. */
3875 auto lower = [&] ()
3876 {
3b00ef10 3877 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
3878 return begin;
3879 else
3b00ef10 3880 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
3881 } ();
3882
3883 /* Find the upper bound. */
3884 auto upper = [&] ()
3885 {
5c58de74 3886 if (lookup_name_without_params.completion_mode ())
3f563c84 3887 {
e1ef7d7a
PA
3888 /* In completion mode, we want UPPER to point past all
3889 symbols names that have the same prefix. I.e., with
3890 these symbols, and completing "func":
3891
3892 function << lower bound
3893 function1
3894 other_function << upper bound
3895
3896 We find the upper bound by looking for the insertion
3897 point of "func"-with-last-character-incremented,
3898 i.e. "fund". */
3b00ef10 3899 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 3900 if (after.empty ())
3f563c84 3901 return end;
e6b2f5ef
PA
3902 return std::lower_bound (lower, end, after.c_str (),
3903 lookup_compare_lower);
3f563c84
PA
3904 }
3905 else
3b00ef10 3906 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
3907 } ();
3908
5c58de74
PA
3909 return {lower, upper};
3910}
3911
3912/* See declaration. */
3913
3914void
44ed8f3e 3915mapped_index_base::build_name_components ()
5c58de74
PA
3916{
3917 if (!this->name_components.empty ())
3918 return;
3919
3920 this->name_components_casing = case_sensitivity;
3921 auto *name_cmp
3922 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3923
3924 /* The code below only knows how to break apart components of C++
3925 symbol names (and other languages that use '::' as
3b00ef10 3926 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
3927 auto count = this->symbol_name_count ();
3928 for (offset_type idx = 0; idx < count; idx++)
5c58de74 3929 {
44ed8f3e 3930 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
3931 continue;
3932
3933 const char *name = this->symbol_name_at (idx);
3934
3935 /* Add each name component to the name component table. */
3936 unsigned int previous_len = 0;
3b00ef10
TT
3937
3938 if (strstr (name, "::") != nullptr)
3939 {
3940 for (unsigned int current_len = cp_find_first_component (name);
3941 name[current_len] != '\0';
3942 current_len += cp_find_first_component (name + current_len))
3943 {
3944 gdb_assert (name[current_len] == ':');
3945 this->name_components.push_back ({previous_len, idx});
3946 /* Skip the '::'. */
3947 current_len += 2;
3948 previous_len = current_len;
3949 }
3950 }
3951 else
5c58de74 3952 {
3b00ef10
TT
3953 /* Handle the Ada encoded (aka mangled) form here. */
3954 for (const char *iter = strstr (name, "__");
3955 iter != nullptr;
3956 iter = strstr (iter, "__"))
3957 {
3958 this->name_components.push_back ({previous_len, idx});
3959 iter += 2;
3960 previous_len = iter - name;
3961 }
5c58de74 3962 }
3b00ef10 3963
5c58de74
PA
3964 this->name_components.push_back ({previous_len, idx});
3965 }
3966
3967 /* Sort name_components elements by name. */
3968 auto name_comp_compare = [&] (const name_component &left,
3969 const name_component &right)
3970 {
3971 const char *left_qualified = this->symbol_name_at (left.idx);
3972 const char *right_qualified = this->symbol_name_at (right.idx);
3973
3974 const char *left_name = left_qualified + left.name_offset;
3975 const char *right_name = right_qualified + right.name_offset;
3976
3977 return name_cmp (left_name, right_name) < 0;
3978 };
3979
3980 std::sort (this->name_components.begin (),
3981 this->name_components.end (),
3982 name_comp_compare);
3983}
3984
3985/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
3986 mapped_index_base instead of the containing objfile. This is split
3987 to a separate function in order to be able to unit test the
3988 name_components matching using a mock mapped_index_base. For each
5c58de74 3989 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 3990 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
3991
3992static void
3993dw2_expand_symtabs_matching_symbol
44ed8f3e 3994 (mapped_index_base &index,
5c58de74
PA
3995 const lookup_name_info &lookup_name_in,
3996 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3997 enum search_domain kind,
3b00ef10 3998 gdb::function_view<bool (offset_type)> match_callback)
5c58de74
PA
3999{
4000 lookup_name_info lookup_name_without_params
4001 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4002
4003 /* Build the symbol name component sorted vector, if we haven't
4004 yet. */
4005 index.build_name_components ();
4006
3f563c84
PA
4007 /* The same symbol may appear more than once in the range though.
4008 E.g., if we're looking for symbols that complete "w", and we have
4009 a symbol named "w1::w2", we'll find the two name components for
4010 that same symbol in the range. To be sure we only call the
4011 callback once per symbol, we first collect the symbol name
4012 indexes that matched in a temporary vector and ignore
4013 duplicates. */
4014 std::vector<offset_type> matches;
3f563c84 4015
3b00ef10
TT
4016 struct name_and_matcher
4017 {
4018 symbol_name_matcher_ftype *matcher;
ecc6c606 4019 const char *name;
3b00ef10
TT
4020
4021 bool operator== (const name_and_matcher &other) const
3f563c84 4022 {
ecc6c606 4023 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4024 }
4025 };
4026
4027 /* A vector holding all the different symbol name matchers, for all
4028 languages. */
4029 std::vector<name_and_matcher> matchers;
4030
4031 for (int i = 0; i < nr_languages; i++)
4032 {
4033 enum language lang_e = (enum language) i;
4034
4035 const language_defn *lang = language_def (lang_e);
4036 symbol_name_matcher_ftype *name_matcher
4037 = get_symbol_name_matcher (lang, lookup_name_without_params);
3f563c84 4038
3b00ef10
TT
4039 name_and_matcher key {
4040 name_matcher,
4041 lookup_name_without_params.language_lookup_name (lang_e)
4042 };
4043
4044 /* Don't insert the same comparison routine more than once.
4045 Note that we do this linear walk. This is not a problem in
4046 practice because the number of supported languages is
4047 low. */
4048 if (std::find (matchers.begin (), matchers.end (), key)
4049 != matchers.end ())
9291a0cd 4050 continue;
3b00ef10
TT
4051 matchers.push_back (std::move (key));
4052
4053 auto bounds
4054 = index.find_name_components_bounds (lookup_name_without_params,
4055 lang_e);
4056
4057 /* Now for each symbol name in range, check to see if we have a name
4058 match, and if so, call the MATCH_CALLBACK callback. */
4059
4060 for (; bounds.first != bounds.second; ++bounds.first)
4061 {
4062 const char *qualified = index.symbol_name_at (bounds.first->idx);
4063
4064 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4065 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4066 continue;
9291a0cd 4067
3b00ef10
TT
4068 matches.push_back (bounds.first->idx);
4069 }
3f563c84
PA
4070 }
4071
4072 std::sort (matches.begin (), matches.end ());
4073
4074 /* Finally call the callback, once per match. */
4075 ULONGEST prev = -1;
4076 for (offset_type idx : matches)
4077 {
4078 if (prev != idx)
4079 {
3b00ef10
TT
4080 if (!match_callback (idx))
4081 break;
3f563c84
PA
4082 prev = idx;
4083 }
4084 }
4085
4086 /* Above we use a type wider than idx's for 'prev', since 0 and
4087 (offset_type)-1 are both possible values. */
4088 static_assert (sizeof (prev) > sizeof (offset_type), "");
4089}
4090
c62446b1
PA
4091#if GDB_SELF_TEST
4092
4093namespace selftests { namespace dw2_expand_symtabs_matching {
4094
a3c5fafd
PA
4095/* A mock .gdb_index/.debug_names-like name index table, enough to
4096 exercise dw2_expand_symtabs_matching_symbol, which works with the
4097 mapped_index_base interface. Builds an index from the symbol list
4098 passed as parameter to the constructor. */
4099class mock_mapped_index : public mapped_index_base
c62446b1
PA
4100{
4101public:
a3c5fafd
PA
4102 mock_mapped_index (gdb::array_view<const char *> symbols)
4103 : m_symbol_table (symbols)
c62446b1
PA
4104 {}
4105
a3c5fafd 4106 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4107
a3c5fafd 4108 /* Return the number of names in the symbol table. */
632e107b 4109 size_t symbol_name_count () const override
c62446b1 4110 {
a3c5fafd 4111 return m_symbol_table.size ();
c62446b1
PA
4112 }
4113
a3c5fafd 4114 /* Get the name of the symbol at IDX in the symbol table. */
632e107b 4115 const char *symbol_name_at (offset_type idx) const override
a3c5fafd
PA
4116 {
4117 return m_symbol_table[idx];
4118 }
c62446b1 4119
a3c5fafd
PA
4120private:
4121 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4122};
4123
4124/* Convenience function that converts a NULL pointer to a "<null>"
4125 string, to pass to print routines. */
4126
4127static const char *
4128string_or_null (const char *str)
4129{
4130 return str != NULL ? str : "<null>";
4131}
4132
4133/* Check if a lookup_name_info built from
4134 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4135 index. EXPECTED_LIST is the list of expected matches, in expected
4136 matching order. If no match expected, then an empty list is
4137 specified. Returns true on success. On failure prints a warning
4138 indicating the file:line that failed, and returns false. */
4139
4140static bool
4141check_match (const char *file, int line,
4142 mock_mapped_index &mock_index,
4143 const char *name, symbol_name_match_type match_type,
4144 bool completion_mode,
4145 std::initializer_list<const char *> expected_list)
4146{
4147 lookup_name_info lookup_name (name, match_type, completion_mode);
4148
4149 bool matched = true;
4150
4151 auto mismatch = [&] (const char *expected_str,
4152 const char *got)
4153 {
4154 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4155 "expected=\"%s\", got=\"%s\"\n"),
4156 file, line,
4157 (match_type == symbol_name_match_type::FULL
4158 ? "FULL" : "WILD"),
4159 name, string_or_null (expected_str), string_or_null (got));
4160 matched = false;
4161 };
4162
4163 auto expected_it = expected_list.begin ();
4164 auto expected_end = expected_list.end ();
4165
a3c5fafd 4166 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4167 NULL, ALL_DOMAIN,
4168 [&] (offset_type idx)
4169 {
a3c5fafd 4170 const char *matched_name = mock_index.symbol_name_at (idx);
c62446b1
PA
4171 const char *expected_str
4172 = expected_it == expected_end ? NULL : *expected_it++;
4173
4174 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4175 mismatch (expected_str, matched_name);
3b00ef10 4176 return true;
c62446b1
PA
4177 });
4178
4179 const char *expected_str
4180 = expected_it == expected_end ? NULL : *expected_it++;
4181 if (expected_str != NULL)
4182 mismatch (expected_str, NULL);
4183
4184 return matched;
4185}
4186
4187/* The symbols added to the mock mapped_index for testing (in
4188 canonical form). */
4189static const char *test_symbols[] = {
4190 "function",
4191 "std::bar",
4192 "std::zfunction",
4193 "std::zfunction2",
4194 "w1::w2",
4195 "ns::foo<char*>",
4196 "ns::foo<int>",
4197 "ns::foo<long>",
a20714ff
PA
4198 "ns2::tmpl<int>::foo2",
4199 "(anonymous namespace)::A::B::C",
c62446b1 4200
e1ef7d7a
PA
4201 /* These are used to check that the increment-last-char in the
4202 matching algorithm for completion doesn't match "t1_fund" when
4203 completing "t1_func". */
4204 "t1_func",
4205 "t1_func1",
4206 "t1_fund",
4207 "t1_fund1",
4208
4209 /* A UTF-8 name with multi-byte sequences to make sure that
4210 cp-name-parser understands this as a single identifier ("função"
4211 is "function" in PT). */
4212 u8"u8função",
4213
4214 /* \377 (0xff) is Latin1 'ÿ'. */
4215 "yfunc\377",
4216
4217 /* \377 (0xff) is Latin1 'ÿ'. */
4218 "\377",
4219 "\377\377123",
4220
c62446b1
PA
4221 /* A name with all sorts of complications. Starts with "z" to make
4222 it easier for the completion tests below. */
4223#define Z_SYM_NAME \
4224 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4225 "::tuple<(anonymous namespace)::ui*, " \
4226 "std::default_delete<(anonymous namespace)::ui>, void>"
4227
4228 Z_SYM_NAME
4229};
4230
a3c5fafd
PA
4231/* Returns true if the mapped_index_base::find_name_component_bounds
4232 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4233 in completion mode. */
5c58de74
PA
4234
4235static bool
a3c5fafd 4236check_find_bounds_finds (mapped_index_base &index,
5c58de74
PA
4237 const char *search_name,
4238 gdb::array_view<const char *> expected_syms)
4239{
4240 lookup_name_info lookup_name (search_name,
4241 symbol_name_match_type::FULL, true);
4242
3b00ef10
TT
4243 auto bounds = index.find_name_components_bounds (lookup_name,
4244 language_cplus);
5c58de74
PA
4245
4246 size_t distance = std::distance (bounds.first, bounds.second);
4247 if (distance != expected_syms.size ())
4248 return false;
4249
4250 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4251 {
4252 auto nc_elem = bounds.first + exp_elem;
4253 const char *qualified = index.symbol_name_at (nc_elem->idx);
4254 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4255 return false;
4256 }
4257
4258 return true;
4259}
4260
4261/* Test the lower-level mapped_index::find_name_component_bounds
4262 method. */
4263
c62446b1 4264static void
5c58de74
PA
4265test_mapped_index_find_name_component_bounds ()
4266{
4267 mock_mapped_index mock_index (test_symbols);
4268
a3c5fafd 4269 mock_index.build_name_components ();
5c58de74
PA
4270
4271 /* Test the lower-level mapped_index::find_name_component_bounds
4272 method in completion mode. */
4273 {
4274 static const char *expected_syms[] = {
4275 "t1_func",
4276 "t1_func1",
5c58de74
PA
4277 };
4278
a3c5fafd 4279 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4280 "t1_func", expected_syms));
4281 }
4282
4283 /* Check that the increment-last-char in the name matching algorithm
4284 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4285 {
4286 static const char *expected_syms1[] = {
4287 "\377",
4288 "\377\377123",
4289 };
a3c5fafd 4290 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4291 "\377", expected_syms1));
4292
4293 static const char *expected_syms2[] = {
4294 "\377\377123",
4295 };
a3c5fafd 4296 SELF_CHECK (check_find_bounds_finds (mock_index,
5c58de74
PA
4297 "\377\377", expected_syms2));
4298 }
4299}
4300
4301/* Test dw2_expand_symtabs_matching_symbol. */
4302
4303static void
4304test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4305{
4306 mock_mapped_index mock_index (test_symbols);
4307
4308 /* We let all tests run until the end even if some fails, for debug
4309 convenience. */
4310 bool any_mismatch = false;
4311
4312 /* Create the expected symbols list (an initializer_list). Needed
4313 because lists have commas, and we need to pass them to CHECK,
4314 which is a macro. */
4315#define EXPECT(...) { __VA_ARGS__ }
4316
4317 /* Wrapper for check_match that passes down the current
4318 __FILE__/__LINE__. */
4319#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4320 any_mismatch |= !check_match (__FILE__, __LINE__, \
4321 mock_index, \
4322 NAME, MATCH_TYPE, COMPLETION_MODE, \
4323 EXPECTED_LIST)
4324
4325 /* Identity checks. */
4326 for (const char *sym : test_symbols)
4327 {
4328 /* Should be able to match all existing symbols. */
4329 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4330 EXPECT (sym));
4331
4332 /* Should be able to match all existing symbols with
4333 parameters. */
4334 std::string with_params = std::string (sym) + "(int)";
4335 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4336 EXPECT (sym));
4337
4338 /* Should be able to match all existing symbols with
4339 parameters and qualifiers. */
4340 with_params = std::string (sym) + " ( int ) const";
4341 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4342 EXPECT (sym));
4343
4344 /* This should really find sym, but cp-name-parser.y doesn't
4345 know about lvalue/rvalue qualifiers yet. */
4346 with_params = std::string (sym) + " ( int ) &&";
4347 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4348 {});
4349 }
4350
e1ef7d7a
PA
4351 /* Check that the name matching algorithm for completion doesn't get
4352 confused with Latin1 'ÿ' / 0xff. */
4353 {
4354 static const char str[] = "\377";
4355 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4356 EXPECT ("\377", "\377\377123"));
4357 }
4358
4359 /* Check that the increment-last-char in the matching algorithm for
4360 completion doesn't match "t1_fund" when completing "t1_func". */
4361 {
4362 static const char str[] = "t1_func";
4363 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4364 EXPECT ("t1_func", "t1_func1"));
4365 }
4366
c62446b1
PA
4367 /* Check that completion mode works at each prefix of the expected
4368 symbol name. */
4369 {
4370 static const char str[] = "function(int)";
4371 size_t len = strlen (str);
4372 std::string lookup;
4373
4374 for (size_t i = 1; i < len; i++)
4375 {
4376 lookup.assign (str, i);
4377 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4378 EXPECT ("function"));
4379 }
4380 }
4381
4382 /* While "w" is a prefix of both components, the match function
4383 should still only be called once. */
4384 {
4385 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4386 EXPECT ("w1::w2"));
a20714ff
PA
4387 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4388 EXPECT ("w1::w2"));
c62446b1
PA
4389 }
4390
4391 /* Same, with a "complicated" symbol. */
4392 {
4393 static const char str[] = Z_SYM_NAME;
4394 size_t len = strlen (str);
4395 std::string lookup;
4396
4397 for (size_t i = 1; i < len; i++)
4398 {
4399 lookup.assign (str, i);
4400 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4401 EXPECT (Z_SYM_NAME));
4402 }
4403 }
4404
4405 /* In FULL mode, an incomplete symbol doesn't match. */
4406 {
4407 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4408 {});
4409 }
4410
4411 /* A complete symbol with parameters matches any overload, since the
4412 index has no overload info. */
4413 {
4414 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4415 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4416 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4417 EXPECT ("std::zfunction", "std::zfunction2"));
4418 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4419 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4420 }
4421
4422 /* Check that whitespace is ignored appropriately. A symbol with a
4423 template argument list. */
4424 {
4425 static const char expected[] = "ns::foo<int>";
4426 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4427 EXPECT (expected));
a20714ff
PA
4428 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4429 EXPECT (expected));
c62446b1
PA
4430 }
4431
4432 /* Check that whitespace is ignored appropriately. A symbol with a
4433 template argument list that includes a pointer. */
4434 {
4435 static const char expected[] = "ns::foo<char*>";
4436 /* Try both completion and non-completion modes. */
4437 static const bool completion_mode[2] = {false, true};
4438 for (size_t i = 0; i < 2; i++)
4439 {
4440 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4441 completion_mode[i], EXPECT (expected));
a20714ff
PA
4442 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4443 completion_mode[i], EXPECT (expected));
c62446b1
PA
4444
4445 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4446 completion_mode[i], EXPECT (expected));
a20714ff
PA
4447 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4448 completion_mode[i], EXPECT (expected));
c62446b1
PA
4449 }
4450 }
4451
4452 {
4453 /* Check method qualifiers are ignored. */
4454 static const char expected[] = "ns::foo<char*>";
4455 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4456 symbol_name_match_type::FULL, true, EXPECT (expected));
4457 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4458 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4459 CHECK_MATCH ("foo < char * > ( int ) const",
4460 symbol_name_match_type::WILD, true, EXPECT (expected));
4461 CHECK_MATCH ("foo < char * > ( int ) &&",
4462 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4463 }
4464
4465 /* Test lookup names that don't match anything. */
4466 {
a20714ff
PA
4467 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4468 {});
4469
c62446b1
PA
4470 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4471 {});
4472 }
4473
a20714ff
PA
4474 /* Some wild matching tests, exercising "(anonymous namespace)",
4475 which should not be confused with a parameter list. */
4476 {
4477 static const char *syms[] = {
4478 "A::B::C",
4479 "B::C",
4480 "C",
4481 "A :: B :: C ( int )",
4482 "B :: C ( int )",
4483 "C ( int )",
4484 };
4485
4486 for (const char *s : syms)
4487 {
4488 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4489 EXPECT ("(anonymous namespace)::A::B::C"));
4490 }
4491 }
4492
4493 {
4494 static const char expected[] = "ns2::tmpl<int>::foo2";
4495 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4496 EXPECT (expected));
4497 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4498 EXPECT (expected));
4499 }
4500
c62446b1
PA
4501 SELF_CHECK (!any_mismatch);
4502
4503#undef EXPECT
4504#undef CHECK_MATCH
4505}
4506
5c58de74
PA
4507static void
4508run_test ()
4509{
4510 test_mapped_index_find_name_component_bounds ();
4511 test_dw2_expand_symtabs_matching_symbol ();
4512}
4513
c62446b1
PA
4514}} // namespace selftests::dw2_expand_symtabs_matching
4515
4516#endif /* GDB_SELF_TEST */
4517
4b514bc8
JK
4518/* If FILE_MATCHER is NULL or if PER_CU has
4519 dwarf2_per_cu_quick_data::MARK set (see
4520 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4521 EXPANSION_NOTIFY on it. */
4522
4523static void
4524dw2_expand_symtabs_matching_one
97a1449a
SM
4525 (dwarf2_per_cu_data *per_cu,
4526 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4527 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4528 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4529{
4530 if (file_matcher == NULL || per_cu->v.quick->mark)
4531 {
af758d11 4532 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4533
97a1449a
SM
4534 compunit_symtab *symtab
4535 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4536 gdb_assert (symtab != nullptr);
4b514bc8 4537
af758d11
SM
4538 if (expansion_notify != NULL && symtab_was_null)
4539 expansion_notify (symtab);
4b514bc8
JK
4540 }
4541}
4542
3f563c84
PA
4543/* Helper for dw2_expand_matching symtabs. Called on each symbol
4544 matched, to expand corresponding CUs that were marked. IDX is the
4545 index of the symbol name that matched. */
4546
4547static void
4548dw2_expand_marked_cus
97a1449a 4549 (dwarf2_per_objfile *dwarf2_per_objfile, offset_type idx,
3f563c84
PA
4550 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4551 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4552 search_domain kind)
4553{
3f563c84
PA
4554 offset_type *vec, vec_len, vec_idx;
4555 bool global_seen = false;
5989a64e 4556 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
3f563c84 4557
61920122 4558 vec = (offset_type *) (index.constant_pool
f00a2de2 4559 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4560 vec_len = MAYBE_SWAP (vec[0]);
4561 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4562 {
61920122
PA
4563 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4564 /* This value is only valid for index versions >= 7. */
4565 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4566 gdb_index_symbol_kind symbol_kind =
4567 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4568 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4569 /* Only check the symbol attributes if they're present.
4570 Indices prior to version 7 don't record them,
4571 and indices >= 7 may elide them for certain symbols
4572 (gold does this). */
4573 int attrs_valid =
4574 (index.version >= 7
4575 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4576
4577 /* Work around gold/15646. */
4578 if (attrs_valid)
9291a0cd 4579 {
61920122
PA
4580 if (!is_static && global_seen)
4581 continue;
4582 if (!is_static)
4583 global_seen = true;
4584 }
3190f0c6 4585
61920122
PA
4586 /* Only check the symbol's kind if it has one. */
4587 if (attrs_valid)
4588 {
4589 switch (kind)
8943b874 4590 {
61920122
PA
4591 case VARIABLES_DOMAIN:
4592 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4593 continue;
4594 break;
4595 case FUNCTIONS_DOMAIN:
4596 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4597 continue;
61920122
PA
4598 break;
4599 case TYPES_DOMAIN:
4600 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4601 continue;
4602 break;
59c35742
AB
4603 case MODULES_DOMAIN:
4604 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4605 continue;
4606 break;
61920122
PA
4607 default:
4608 break;
8943b874 4609 }
61920122 4610 }
8943b874 4611
61920122 4612 /* Don't crash on bad data. */
5989a64e
SM
4613 if (cu_index >= (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
4614 + dwarf2_per_objfile->per_bfd->all_type_units.size ()))
61920122 4615 {
b98664d3 4616 complaint (_(".gdb_index entry has bad CU index"
ed2dc618
SM
4617 " [in module %s]"),
4618 objfile_name (dwarf2_per_objfile->objfile));
61920122
PA
4619 continue;
4620 }
4621
5989a64e 4622 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (cu_index);
97a1449a 4623 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, file_matcher,
4b514bc8 4624 expansion_notify);
61920122
PA
4625 }
4626}
4627
4b514bc8
JK
4628/* If FILE_MATCHER is non-NULL, set all the
4629 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4630 that match FILE_MATCHER. */
4631
61920122 4632static void
4b514bc8 4633dw_expand_symtabs_matching_file_matcher
ed2dc618
SM
4634 (struct dwarf2_per_objfile *dwarf2_per_objfile,
4635 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4636{
4b514bc8 4637 if (file_matcher == NULL)
61920122
PA
4638 return;
4639
4b514bc8
JK
4640 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4641 htab_eq_pointer,
4642 NULL, xcalloc, xfree));
4643 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4644 htab_eq_pointer,
4645 NULL, xcalloc, xfree));
61920122 4646
4b514bc8
JK
4647 /* The rule is CUs specify all the files, including those used by
4648 any TU, so there's no need to scan TUs here. */
61920122 4649
5989a64e 4650 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4651 {
927aa2e7
JK
4652 QUIT;
4653
4654 per_cu->v.quick->mark = 0;
4655
4656 /* We only need to look at symtabs not already expanded. */
af758d11 4657 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4658 continue;
4659
ab432490
SM
4660 quick_file_names *file_data
4661 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4662 if (file_data == NULL)
4663 continue;
4664
4665 if (htab_find (visited_not_found.get (), file_data) != NULL)
4666 continue;
4667 else if (htab_find (visited_found.get (), file_data) != NULL)
4668 {
4669 per_cu->v.quick->mark = 1;
4670 continue;
4671 }
4672
b76e467d 4673 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4674 {
4675 const char *this_real_name;
4676
4677 if (file_matcher (file_data->file_names[j], false))
4678 {
4679 per_cu->v.quick->mark = 1;
4680 break;
4681 }
4682
4683 /* Before we invoke realpath, which can get expensive when many
4684 files are involved, do a quick comparison of the basenames. */
4685 if (!basenames_may_differ
4686 && !file_matcher (lbasename (file_data->file_names[j]),
4687 true))
4688 continue;
4689
45940949
TT
4690 this_real_name = dw2_get_real_path (dwarf2_per_objfile,
4691 file_data, j);
927aa2e7
JK
4692 if (file_matcher (this_real_name, false))
4693 {
4694 per_cu->v.quick->mark = 1;
4695 break;
4696 }
4697 }
4698
b76e467d
SM
4699 void **slot = htab_find_slot (per_cu->v.quick->mark
4700 ? visited_found.get ()
4701 : visited_not_found.get (),
4702 file_data, INSERT);
927aa2e7
JK
4703 *slot = file_data;
4704 }
4705}
4706
4707static void
4708dw2_expand_symtabs_matching
4709 (struct objfile *objfile,
4710 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4711 const lookup_name_info *lookup_name,
927aa2e7
JK
4712 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4713 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4714 enum search_domain kind)
4715{
ed2dc618
SM
4716 struct dwarf2_per_objfile *dwarf2_per_objfile
4717 = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4718
4719 /* index_table is NULL if OBJF_READNOW. */
5989a64e 4720 if (!dwarf2_per_objfile->per_bfd->index_table)
927aa2e7
JK
4721 return;
4722
ed2dc618 4723 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
927aa2e7 4724
c1a66c06
TV
4725 if (symbol_matcher == NULL && lookup_name == NULL)
4726 {
5989a64e 4727 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4728 {
4729 QUIT;
4730
97a1449a
SM
4731 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
4732 file_matcher, expansion_notify);
c1a66c06
TV
4733 }
4734 return;
4735 }
4736
5989a64e 4737 mapped_index &index = *dwarf2_per_objfile->per_bfd->index_table;
927aa2e7 4738
c1a66c06 4739 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4740 symbol_matcher,
4741 kind, [&] (offset_type idx)
4742 {
ed2dc618 4743 dw2_expand_marked_cus (dwarf2_per_objfile, idx, file_matcher,
927aa2e7 4744 expansion_notify, kind);
3b00ef10 4745 return true;
927aa2e7
JK
4746 });
4747}
4748
4749/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4750 symtab. */
4751
4752static struct compunit_symtab *
4753recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4754 CORE_ADDR pc)
4755{
4756 int i;
4757
4758 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4759 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4760 return cust;
4761
4762 if (cust->includes == NULL)
4763 return NULL;
4764
4765 for (i = 0; cust->includes[i]; ++i)
4766 {
4767 struct compunit_symtab *s = cust->includes[i];
4768
4769 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4770 if (s != NULL)
4771 return s;
4772 }
4773
4774 return NULL;
4775}
4776
4777static struct compunit_symtab *
4778dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4779 struct bound_minimal_symbol msymbol,
4780 CORE_ADDR pc,
4781 struct obj_section *section,
4782 int warn_if_readin)
4783{
4784 struct dwarf2_per_cu_data *data;
4785 struct compunit_symtab *result;
4786
d320c2b5 4787 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4788 return NULL;
4789
b3b3bada 4790 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
4791 data = (struct dwarf2_per_cu_data *) addrmap_find
4792 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
4793 if (!data)
4794 return NULL;
4795
af758d11
SM
4796 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
4797 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 4798 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 4799 paddress (objfile->arch (), pc));
927aa2e7 4800
97a1449a
SM
4801 result = recursively_find_pc_sect_compunit_symtab
4802 (dw2_instantiate_symtab (data, per_objfile, false), pc);
4803
927aa2e7
JK
4804 gdb_assert (result != NULL);
4805 return result;
4806}
4807
4808static void
4809dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
4810 void *data, int need_fullname)
4811{
ed2dc618
SM
4812 struct dwarf2_per_objfile *dwarf2_per_objfile
4813 = get_dwarf2_per_objfile (objfile);
927aa2e7 4814
5989a64e 4815 if (!dwarf2_per_objfile->per_bfd->filenames_cache)
927aa2e7 4816 {
5989a64e 4817 dwarf2_per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
4818
4819 htab_up visited (htab_create_alloc (10,
4820 htab_hash_pointer, htab_eq_pointer,
4821 NULL, xcalloc, xfree));
4822
4823 /* The rule is CUs specify all the files, including those used
4824 by any TU, so there's no need to scan TUs here. We can
4825 ignore file names coming from already-expanded CUs. */
4826
5989a64e 4827 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4828 {
af758d11 4829 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4830 {
4831 void **slot = htab_find_slot (visited.get (),
4832 per_cu->v.quick->file_names,
4833 INSERT);
4834
4835 *slot = per_cu->v.quick->file_names;
4836 }
4837 }
4838
5989a64e 4839 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
927aa2e7 4840 {
927aa2e7 4841 /* We only need to look at symtabs not already expanded. */
af758d11 4842 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4843 continue;
4844
ab432490
SM
4845 quick_file_names *file_data
4846 = dw2_get_file_names (per_cu, dwarf2_per_objfile);
927aa2e7
JK
4847 if (file_data == NULL)
4848 continue;
4849
b76e467d 4850 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
4851 if (*slot)
4852 {
4853 /* Already visited. */
4854 continue;
4855 }
4856 *slot = file_data;
4857
4858 for (int j = 0; j < file_data->num_file_names; ++j)
4859 {
4860 const char *filename = file_data->file_names[j];
5989a64e 4861 dwarf2_per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
4862 }
4863 }
4864 }
4865
5989a64e 4866 dwarf2_per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
4867 {
4868 gdb::unique_xmalloc_ptr<char> this_real_name;
4869
4870 if (need_fullname)
4871 this_real_name = gdb_realpath (filename);
4872 (*fun) (filename, this_real_name.get (), data);
4873 });
4874}
4875
4876static int
4877dw2_has_symbols (struct objfile *objfile)
4878{
4879 return 1;
4880}
4881
4882const struct quick_symbol_functions dwarf2_gdb_index_functions =
4883{
4884 dw2_has_symbols,
4885 dw2_find_last_source_symtab,
4886 dw2_forget_cached_source_info,
4887 dw2_map_symtabs_matching_filename,
4888 dw2_lookup_symbol,
d3214198 4889 NULL,
927aa2e7
JK
4890 dw2_print_stats,
4891 dw2_dump,
927aa2e7
JK
4892 dw2_expand_symtabs_for_function,
4893 dw2_expand_all_symtabs,
4894 dw2_expand_symtabs_with_fullname,
4895 dw2_map_matching_symbols,
4896 dw2_expand_symtabs_matching,
4897 dw2_find_pc_sect_compunit_symtab,
4898 NULL,
4899 dw2_map_symbol_filenames
4900};
4901
4902/* DWARF-5 debug_names reader. */
4903
4904/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
4905static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
4906
4907/* A helper function that reads the .debug_names section in SECTION
4908 and fills in MAP. FILENAME is the name of the file containing the
4909 section; it is used for error reporting.
4910
4911 Returns true if all went well, false otherwise. */
4912
4913static bool
4914read_debug_names_from_section (struct objfile *objfile,
4915 const char *filename,
4916 struct dwarf2_section_info *section,
4917 mapped_debug_names &map)
4918{
96b79293 4919 if (section->empty ())
927aa2e7
JK
4920 return false;
4921
4922 /* Older elfutils strip versions could keep the section in the main
4923 executable while splitting it for the separate debug info file. */
96b79293 4924 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
4925 return false;
4926
96b79293 4927 section->read (objfile);
927aa2e7 4928
08feed99 4929 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
4930
4931 const gdb_byte *addr = section->buffer;
4932
96b79293 4933 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
4934
4935 unsigned int bytes_read;
4936 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
4937 addr += bytes_read;
4938
4939 map.dwarf5_is_dwarf64 = bytes_read != 4;
4940 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
4941 if (bytes_read + length != section->size)
4942 {
4943 /* There may be multiple per-CU indices. */
4944 warning (_("Section .debug_names in %s length %s does not match "
4945 "section length %s, ignoring .debug_names."),
4946 filename, plongest (bytes_read + length),
4947 pulongest (section->size));
4948 return false;
4949 }
4950
4951 /* The version number. */
4952 uint16_t version = read_2_bytes (abfd, addr);
4953 addr += 2;
4954 if (version != 5)
4955 {
4956 warning (_("Section .debug_names in %s has unsupported version %d, "
4957 "ignoring .debug_names."),
4958 filename, version);
4959 return false;
4960 }
4961
4962 /* Padding. */
4963 uint16_t padding = read_2_bytes (abfd, addr);
4964 addr += 2;
4965 if (padding != 0)
4966 {
4967 warning (_("Section .debug_names in %s has unsupported padding %d, "
4968 "ignoring .debug_names."),
4969 filename, padding);
4970 return false;
4971 }
4972
4973 /* comp_unit_count - The number of CUs in the CU list. */
4974 map.cu_count = read_4_bytes (abfd, addr);
4975 addr += 4;
4976
4977 /* local_type_unit_count - The number of TUs in the local TU
4978 list. */
4979 map.tu_count = read_4_bytes (abfd, addr);
4980 addr += 4;
4981
4982 /* foreign_type_unit_count - The number of TUs in the foreign TU
4983 list. */
4984 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
4985 addr += 4;
4986 if (foreign_tu_count != 0)
4987 {
4988 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
4989 "ignoring .debug_names."),
4990 filename, static_cast<unsigned long> (foreign_tu_count));
4991 return false;
4992 }
4993
4994 /* bucket_count - The number of hash buckets in the hash lookup
4995 table. */
4996 map.bucket_count = read_4_bytes (abfd, addr);
4997 addr += 4;
4998
4999 /* name_count - The number of unique names in the index. */
5000 map.name_count = read_4_bytes (abfd, addr);
5001 addr += 4;
5002
5003 /* abbrev_table_size - The size in bytes of the abbreviations
5004 table. */
5005 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5006 addr += 4;
5007
5008 /* augmentation_string_size - The size in bytes of the augmentation
5009 string. This value is rounded up to a multiple of 4. */
5010 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5011 addr += 4;
5012 map.augmentation_is_gdb = ((augmentation_string_size
5013 == sizeof (dwarf5_augmentation))
5014 && memcmp (addr, dwarf5_augmentation,
5015 sizeof (dwarf5_augmentation)) == 0);
5016 augmentation_string_size += (-augmentation_string_size) & 3;
5017 addr += augmentation_string_size;
5018
5019 /* List of CUs */
5020 map.cu_table_reordered = addr;
5021 addr += map.cu_count * map.offset_size;
5022
5023 /* List of Local TUs */
5024 map.tu_table_reordered = addr;
5025 addr += map.tu_count * map.offset_size;
5026
5027 /* Hash Lookup Table */
5028 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5029 addr += map.bucket_count * 4;
5030 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5031 addr += map.name_count * 4;
5032
5033 /* Name Table */
5034 map.name_table_string_offs_reordered = addr;
5035 addr += map.name_count * map.offset_size;
5036 map.name_table_entry_offs_reordered = addr;
5037 addr += map.name_count * map.offset_size;
5038
5039 const gdb_byte *abbrev_table_start = addr;
5040 for (;;)
5041 {
927aa2e7
JK
5042 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5043 addr += bytes_read;
5044 if (index_num == 0)
5045 break;
5046
5047 const auto insertpair
5048 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5049 if (!insertpair.second)
5050 {
5051 warning (_("Section .debug_names in %s has duplicate index %s, "
5052 "ignoring .debug_names."),
5053 filename, pulongest (index_num));
5054 return false;
5055 }
5056 mapped_debug_names::index_val &indexval = insertpair.first->second;
5057 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5058 addr += bytes_read;
5059
5060 for (;;)
5061 {
5062 mapped_debug_names::index_val::attr attr;
5063 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5064 addr += bytes_read;
5065 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5066 addr += bytes_read;
5067 if (attr.form == DW_FORM_implicit_const)
5068 {
5069 attr.implicit_const = read_signed_leb128 (abfd, addr,
5070 &bytes_read);
5071 addr += bytes_read;
5072 }
5073 if (attr.dw_idx == 0 && attr.form == 0)
5074 break;
5075 indexval.attr_vec.push_back (std::move (attr));
5076 }
5077 }
5078 if (addr != abbrev_table_start + abbrev_table_size)
5079 {
5080 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5081 "of size %s vs. written as %u, ignoring .debug_names."),
5082 filename, plongest (addr - abbrev_table_start),
5083 abbrev_table_size);
927aa2e7
JK
5084 return false;
5085 }
5086 map.entry_pool = addr;
5087
5088 return true;
5089}
5090
5091/* A helper for create_cus_from_debug_names that handles the MAP's CU
5092 list. */
5093
5094static void
168c9250 5095create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5096 const mapped_debug_names &map,
5097 dwarf2_section_info &section,
b76e467d 5098 bool is_dwz)
927aa2e7 5099{
3ee6bb11
TV
5100 if (!map.augmentation_is_gdb)
5101 {
5102 for (uint32_t i = 0; i < map.cu_count; ++i)
5103 {
5104 sect_offset sect_off
5105 = (sect_offset) (extract_unsigned_integer
5106 (map.cu_table_reordered + i * map.offset_size,
5107 map.offset_size,
5108 map.dwarf5_byte_order));
5109 /* We don't know the length of the CU, because the CU list in a
5110 .debug_names index can be incomplete, so we can't use the start of
5111 the next CU as end of this CU. We create the CUs here with length 0,
5112 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5113 dwarf2_per_cu_data *per_cu
168c9250
SM
5114 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5115 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5116 }
5117 }
5118
927aa2e7
JK
5119 sect_offset sect_off_prev;
5120 for (uint32_t i = 0; i <= map.cu_count; ++i)
5121 {
5122 sect_offset sect_off_next;
5123 if (i < map.cu_count)
5124 {
5125 sect_off_next
5126 = (sect_offset) (extract_unsigned_integer
5127 (map.cu_table_reordered + i * map.offset_size,
5128 map.offset_size,
5129 map.dwarf5_byte_order));
5130 }
5131 else
5132 sect_off_next = (sect_offset) section.size;
5133 if (i >= 1)
5134 {
5135 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5136 dwarf2_per_cu_data *per_cu
168c9250 5137 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5138 sect_off_prev, length);
168c9250 5139 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5140 }
5141 sect_off_prev = sect_off_next;
5142 }
5143}
5144
5145/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5146 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5147
5148static void
168c9250 5149create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5150 const mapped_debug_names &map,
5151 const mapped_debug_names &dwz_map)
5152{
168c9250
SM
5153 gdb_assert (per_bfd->all_comp_units.empty ());
5154 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5155
168c9250 5156 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5157 false /* is_dwz */);
927aa2e7
JK
5158
5159 if (dwz_map.cu_count == 0)
5160 return;
5161
168c9250
SM
5162 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5163 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5164 true /* is_dwz */);
927aa2e7
JK
5165}
5166
5167/* Read .debug_names. If everything went ok, initialize the "quick"
5168 elements of all the CUs and return true. Otherwise, return false. */
5169
5170static bool
ed2dc618 5171dwarf2_read_debug_names (struct dwarf2_per_objfile *dwarf2_per_objfile)
927aa2e7 5172{
22ca247e
TT
5173 std::unique_ptr<mapped_debug_names> map
5174 (new mapped_debug_names (dwarf2_per_objfile));
ed2dc618
SM
5175 mapped_debug_names dwz_map (dwarf2_per_objfile);
5176 struct objfile *objfile = dwarf2_per_objfile->objfile;
927aa2e7
JK
5177
5178 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
5989a64e 5179 &dwarf2_per_objfile->per_bfd->debug_names,
22ca247e 5180 *map))
927aa2e7
JK
5181 return false;
5182
5183 /* Don't use the index if it's empty. */
22ca247e 5184 if (map->name_count == 0)
927aa2e7
JK
5185 return false;
5186
5187 /* If there is a .dwz file, read it so we can get its CU list as
5188 well. */
c3699833 5189 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
927aa2e7
JK
5190 if (dwz != NULL)
5191 {
5192 if (!read_debug_names_from_section (objfile,
00f93c44 5193 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5194 &dwz->debug_names, dwz_map))
5195 {
5196 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5197 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5198 return false;
5199 }
5200 }
5201
168c9250 5202 create_cus_from_debug_names (dwarf2_per_objfile->per_bfd, *map, dwz_map);
927aa2e7 5203
22ca247e 5204 if (map->tu_count != 0)
927aa2e7
JK
5205 {
5206 /* We can only handle a single .debug_types when we have an
5207 index. */
5989a64e 5208 if (dwarf2_per_objfile->per_bfd->types.size () != 1)
927aa2e7
JK
5209 return false;
5210
5989a64e 5211 dwarf2_section_info *section = &dwarf2_per_objfile->per_bfd->types[0];
927aa2e7
JK
5212
5213 create_signatured_type_table_from_debug_names
5989a64e 5214 (dwarf2_per_objfile, *map, section, &dwarf2_per_objfile->per_bfd->abbrev);
927aa2e7
JK
5215 }
5216
ed2dc618 5217 create_addrmap_from_aranges (dwarf2_per_objfile,
5989a64e 5218 &dwarf2_per_objfile->per_bfd->debug_aranges);
927aa2e7 5219
5989a64e
SM
5220 dwarf2_per_objfile->per_bfd->debug_names_table = std::move (map);
5221 dwarf2_per_objfile->per_bfd->using_index = 1;
5222 dwarf2_per_objfile->per_bfd->quick_file_names_table =
5223 create_quick_file_names_table (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
927aa2e7
JK
5224
5225 return true;
5226}
5227
927aa2e7
JK
5228/* Type used to manage iterating over all CUs looking for a symbol for
5229 .debug_names. */
5230
5231class dw2_debug_names_iterator
5232{
5233public:
927aa2e7 5234 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5235 gdb::optional<block_enum> block_index,
5236 domain_enum domain,
927aa2e7 5237 const char *name)
2b79f376 5238 : m_map (map), m_block_index (block_index), m_domain (domain),
927aa2e7
JK
5239 m_addr (find_vec_in_debug_names (map, name))
5240 {}
5241
5242 dw2_debug_names_iterator (const mapped_debug_names &map,
5243 search_domain search, uint32_t namei)
5244 : m_map (map),
5245 m_search (search),
5246 m_addr (find_vec_in_debug_names (map, namei))
5247 {}
5248
3b00ef10
TT
5249 dw2_debug_names_iterator (const mapped_debug_names &map,
5250 block_enum block_index, domain_enum domain,
5251 uint32_t namei)
5252 : m_map (map), m_block_index (block_index), m_domain (domain),
5253 m_addr (find_vec_in_debug_names (map, namei))
5254 {}
5255
927aa2e7
JK
5256 /* Return the next matching CU or NULL if there are no more. */
5257 dwarf2_per_cu_data *next ();
5258
5259private:
5260 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5261 const char *name);
5262 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
5263 uint32_t namei);
5264
5265 /* The internalized form of .debug_names. */
5266 const mapped_debug_names &m_map;
5267
2b79f376
SM
5268 /* If set, only look for symbols that match that block. Valid values are
5269 GLOBAL_BLOCK and STATIC_BLOCK. */
5270 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5271
5272 /* The kind of symbol we're looking for. */
5273 const domain_enum m_domain = UNDEF_DOMAIN;
5274 const search_domain m_search = ALL_DOMAIN;
5275
5276 /* The list of CUs from the index entry of the symbol, or NULL if
5277 not found. */
5278 const gdb_byte *m_addr;
5279};
5280
5281const char *
5282mapped_debug_names::namei_to_name (uint32_t namei) const
5283{
5284 const ULONGEST namei_string_offs
5285 = extract_unsigned_integer ((name_table_string_offs_reordered
5286 + namei * offset_size),
5287 offset_size,
5288 dwarf5_byte_order);
4f44ae6c
TT
5289 return read_indirect_string_at_offset (dwarf2_per_objfile,
5290 namei_string_offs);
927aa2e7
JK
5291}
5292
5293/* Find a slot in .debug_names for the object named NAME. If NAME is
5294 found, return pointer to its pool data. If NAME cannot be found,
5295 return NULL. */
5296
5297const gdb_byte *
5298dw2_debug_names_iterator::find_vec_in_debug_names
5299 (const mapped_debug_names &map, const char *name)
5300{
5301 int (*cmp) (const char *, const char *);
5302
54ee4252 5303 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5304 if (current_language->la_language == language_cplus
5305 || current_language->la_language == language_fortran
5306 || current_language->la_language == language_d)
5307 {
5308 /* NAME is already canonical. Drop any qualifiers as
5309 .debug_names does not contain any. */
5310
5311 if (strchr (name, '(') != NULL)
5312 {
54ee4252 5313 without_params = cp_remove_params (name);
927aa2e7 5314 if (without_params != NULL)
54ee4252 5315 name = without_params.get ();
927aa2e7
JK
5316 }
5317 }
5318
5319 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5320
5321 const uint32_t full_hash = dwarf5_djb_hash (name);
5322 uint32_t namei
5323 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5324 (map.bucket_table_reordered
5325 + (full_hash % map.bucket_count)), 4,
5326 map.dwarf5_byte_order);
5327 if (namei == 0)
5328 return NULL;
5329 --namei;
5330 if (namei >= map.name_count)
5331 {
b98664d3 5332 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5333 "[in module %s]"),
5334 namei, map.name_count,
ed2dc618 5335 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5336 return NULL;
5337 }
5338
5339 for (;;)
5340 {
5341 const uint32_t namei_full_hash
5342 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5343 (map.hash_table_reordered + namei), 4,
5344 map.dwarf5_byte_order);
5345 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5346 return NULL;
5347
5348 if (full_hash == namei_full_hash)
5349 {
5350 const char *const namei_string = map.namei_to_name (namei);
5351
5352#if 0 /* An expensive sanity check. */
5353 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5354 {
b98664d3 5355 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5356 "[in module %s]"),
5357 namei, objfile_name (dwarf2_per_objfile->objfile));
5358 return NULL;
5359 }
5360#endif
5361
5362 if (cmp (namei_string, name) == 0)
5363 {
5364 const ULONGEST namei_entry_offs
5365 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5366 + namei * map.offset_size),
5367 map.offset_size, map.dwarf5_byte_order);
5368 return map.entry_pool + namei_entry_offs;
5369 }
5370 }
5371
5372 ++namei;
5373 if (namei >= map.name_count)
5374 return NULL;
5375 }
5376}
5377
5378const gdb_byte *
5379dw2_debug_names_iterator::find_vec_in_debug_names
5380 (const mapped_debug_names &map, uint32_t namei)
5381{
5382 if (namei >= map.name_count)
5383 {
b98664d3 5384 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5385 "[in module %s]"),
5386 namei, map.name_count,
ed2dc618 5387 objfile_name (map.dwarf2_per_objfile->objfile));
927aa2e7
JK
5388 return NULL;
5389 }
5390
5391 const ULONGEST namei_entry_offs
5392 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5393 + namei * map.offset_size),
5394 map.offset_size, map.dwarf5_byte_order);
5395 return map.entry_pool + namei_entry_offs;
5396}
5397
5398/* See dw2_debug_names_iterator. */
5399
5400dwarf2_per_cu_data *
5401dw2_debug_names_iterator::next ()
5402{
5403 if (m_addr == NULL)
5404 return NULL;
5405
ed2dc618
SM
5406 struct dwarf2_per_objfile *dwarf2_per_objfile = m_map.dwarf2_per_objfile;
5407 struct objfile *objfile = dwarf2_per_objfile->objfile;
5408 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5409
5410 again:
5411
5412 unsigned int bytes_read;
5413 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5414 m_addr += bytes_read;
5415 if (abbrev == 0)
5416 return NULL;
5417
5418 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5419 if (indexval_it == m_map.abbrev_map.cend ())
5420 {
b98664d3 5421 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5422 "[in module %s]"),
ed2dc618 5423 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5424 return NULL;
5425 }
5426 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5427 enum class symbol_linkage {
5428 unknown,
5429 static_,
5430 extern_,
23c13d42 5431 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5432 dwarf2_per_cu_data *per_cu = NULL;
5433 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5434 {
5435 ULONGEST ull;
5436 switch (attr.form)
5437 {
5438 case DW_FORM_implicit_const:
5439 ull = attr.implicit_const;
5440 break;
5441 case DW_FORM_flag_present:
5442 ull = 1;
5443 break;
5444 case DW_FORM_udata:
5445 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5446 m_addr += bytes_read;
5447 break;
6dc55ce9 5448 case DW_FORM_ref4:
5449 ull = read_4_bytes (abfd, m_addr);
5450 m_addr += 4;
5451 break;
5452 case DW_FORM_ref8:
5453 ull = read_8_bytes (abfd, m_addr);
5454 m_addr += 8;
5455 break;
5456 case DW_FORM_ref_sig8:
5457 ull = read_8_bytes (abfd, m_addr);
5458 m_addr += 8;
5459 break;
927aa2e7 5460 default:
b98664d3 5461 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5462 dwarf_form_name (attr.form),
ed2dc618 5463 objfile_name (objfile));
927aa2e7
JK
5464 return NULL;
5465 }
5466 switch (attr.dw_idx)
5467 {
5468 case DW_IDX_compile_unit:
5469 /* Don't crash on bad data. */
5989a64e 5470 if (ull >= dwarf2_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5471 {
b98664d3 5472 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5473 " [in module %s]"),
5474 pulongest (ull),
5475 objfile_name (dwarf2_per_objfile->objfile));
5476 continue;
5477 }
5989a64e 5478 per_cu = dwarf2_per_objfile->per_bfd->get_cutu (ull);
927aa2e7 5479 break;
8af5c486
JK
5480 case DW_IDX_type_unit:
5481 /* Don't crash on bad data. */
5989a64e 5482 if (ull >= dwarf2_per_objfile->per_bfd->all_type_units.size ())
8af5c486 5483 {
b98664d3 5484 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5485 " [in module %s]"),
5486 pulongest (ull),
5487 objfile_name (dwarf2_per_objfile->objfile));
5488 continue;
5489 }
5989a64e 5490 per_cu = &dwarf2_per_objfile->per_bfd->get_tu (ull)->per_cu;
8af5c486 5491 break;
6dc55ce9 5492 case DW_IDX_die_offset:
5493 /* In a per-CU index (as opposed to a per-module index), index
5494 entries without CU attribute implicitly refer to the single CU. */
5495 if (per_cu == NULL)
5989a64e 5496 per_cu = dwarf2_per_objfile->per_bfd->get_cu (0);
6dc55ce9 5497 break;
927aa2e7
JK
5498 case DW_IDX_GNU_internal:
5499 if (!m_map.augmentation_is_gdb)
5500 break;
23c13d42 5501 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5502 break;
5503 case DW_IDX_GNU_external:
5504 if (!m_map.augmentation_is_gdb)
5505 break;
23c13d42 5506 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5507 break;
5508 }
5509 }
5510
5511 /* Skip if already read in. */
af758d11 5512 if (dwarf2_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5513 goto again;
5514
5515 /* Check static vs global. */
23c13d42 5516 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5517 {
2b79f376 5518 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5519 const bool symbol_is_static =
5520 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5521 if (want_static != symbol_is_static)
2b79f376 5522 goto again;
927aa2e7
JK
5523 }
5524
5525 /* Match dw2_symtab_iter_next, symbol_kind
5526 and debug_names::psymbol_tag. */
5527 switch (m_domain)
5528 {
5529 case VAR_DOMAIN:
5530 switch (indexval.dwarf_tag)
5531 {
5532 case DW_TAG_variable:
5533 case DW_TAG_subprogram:
5534 /* Some types are also in VAR_DOMAIN. */
5535 case DW_TAG_typedef:
5536 case DW_TAG_structure_type:
5537 break;
5538 default:
5539 goto again;
5540 }
5541 break;
5542 case STRUCT_DOMAIN:
5543 switch (indexval.dwarf_tag)
5544 {
5545 case DW_TAG_typedef:
5546 case DW_TAG_structure_type:
5547 break;
5548 default:
5549 goto again;
5550 }
5551 break;
5552 case LABEL_DOMAIN:
5553 switch (indexval.dwarf_tag)
5554 {
5555 case 0:
5556 case DW_TAG_variable:
5557 break;
5558 default:
5559 goto again;
5560 }
5561 break;
59c35742
AB
5562 case MODULE_DOMAIN:
5563 switch (indexval.dwarf_tag)
5564 {
5565 case DW_TAG_module:
5566 break;
5567 default:
5568 goto again;
5569 }
5570 break;
927aa2e7
JK
5571 default:
5572 break;
5573 }
5574
5575 /* Match dw2_expand_symtabs_matching, symbol_kind and
5576 debug_names::psymbol_tag. */
5577 switch (m_search)
4b514bc8 5578 {
927aa2e7
JK
5579 case VARIABLES_DOMAIN:
5580 switch (indexval.dwarf_tag)
4b514bc8 5581 {
927aa2e7
JK
5582 case DW_TAG_variable:
5583 break;
5584 default:
5585 goto again;
4b514bc8 5586 }
927aa2e7
JK
5587 break;
5588 case FUNCTIONS_DOMAIN:
5589 switch (indexval.dwarf_tag)
4b514bc8 5590 {
927aa2e7
JK
5591 case DW_TAG_subprogram:
5592 break;
5593 default:
5594 goto again;
4b514bc8 5595 }
927aa2e7
JK
5596 break;
5597 case TYPES_DOMAIN:
5598 switch (indexval.dwarf_tag)
5599 {
5600 case DW_TAG_typedef:
5601 case DW_TAG_structure_type:
5602 break;
5603 default:
5604 goto again;
5605 }
5606 break;
59c35742
AB
5607 case MODULES_DOMAIN:
5608 switch (indexval.dwarf_tag)
5609 {
5610 case DW_TAG_module:
5611 break;
5612 default:
5613 goto again;
5614 }
927aa2e7
JK
5615 default:
5616 break;
4b514bc8 5617 }
927aa2e7
JK
5618
5619 return per_cu;
4b514bc8 5620}
61920122 5621
927aa2e7 5622static struct compunit_symtab *
c7f839cb 5623dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5624 const char *name, domain_enum domain)
4b514bc8 5625{
ed2dc618
SM
5626 struct dwarf2_per_objfile *dwarf2_per_objfile
5627 = get_dwarf2_per_objfile (objfile);
61920122 5628
5989a64e 5629 const auto &mapp = dwarf2_per_objfile->per_bfd->debug_names_table;
927aa2e7 5630 if (!mapp)
61920122 5631 {
927aa2e7
JK
5632 /* index is NULL if OBJF_READNOW. */
5633 return NULL;
5634 }
5635 const auto &map = *mapp;
9291a0cd 5636
2b79f376 5637 dw2_debug_names_iterator iter (map, block_index, domain, name);
9703b513 5638
927aa2e7
JK
5639 struct compunit_symtab *stab_best = NULL;
5640 struct dwarf2_per_cu_data *per_cu;
5641 while ((per_cu = iter.next ()) != NULL)
5642 {
5643 struct symbol *sym, *with_opaque = NULL;
97a1449a
SM
5644 compunit_symtab *stab
5645 = dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7 5646 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5647 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5648
927aa2e7
JK
5649 sym = block_find_symbol (block, name, domain,
5650 block_find_non_opaque_type_preferred,
5651 &with_opaque);
9703b513 5652
927aa2e7
JK
5653 /* Some caution must be observed with overloaded functions and
5654 methods, since the index will not contain any overload
5655 information (but NAME might contain it). */
a3ec0bb1 5656
927aa2e7 5657 if (sym != NULL
987012b8 5658 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5659 return stab;
5660 if (with_opaque != NULL
987012b8 5661 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5662 stab_best = stab;
9703b513 5663
927aa2e7 5664 /* Keep looking through other CUs. */
9703b513
TT
5665 }
5666
927aa2e7 5667 return stab_best;
9703b513
TT
5668}
5669
927aa2e7
JK
5670/* This dumps minimal information about .debug_names. It is called
5671 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5672 uses this to verify that .debug_names has been loaded. */
9291a0cd 5673
927aa2e7
JK
5674static void
5675dw2_debug_names_dump (struct objfile *objfile)
5676{
ed2dc618
SM
5677 struct dwarf2_per_objfile *dwarf2_per_objfile
5678 = get_dwarf2_per_objfile (objfile);
5679
5989a64e 5680 gdb_assert (dwarf2_per_objfile->per_bfd->using_index);
927aa2e7 5681 printf_filtered (".debug_names:");
5989a64e 5682 if (dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5683 printf_filtered (" exists\n");
5684 else
5685 printf_filtered (" faked for \"readnow\"\n");
5686 printf_filtered ("\n");
9291a0cd
TT
5687}
5688
9291a0cd 5689static void
927aa2e7
JK
5690dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5691 const char *func_name)
9291a0cd 5692{
ed2dc618
SM
5693 struct dwarf2_per_objfile *dwarf2_per_objfile
5694 = get_dwarf2_per_objfile (objfile);
ae2de4f8 5695
5989a64e
SM
5696 /* dwarf2_per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5697 if (dwarf2_per_objfile->per_bfd->debug_names_table)
24c79950 5698 {
5989a64e 5699 const mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
24c79950 5700
2b79f376 5701 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name);
24c79950 5702
927aa2e7
JK
5703 struct dwarf2_per_cu_data *per_cu;
5704 while ((per_cu = iter.next ()) != NULL)
97a1449a 5705 dw2_instantiate_symtab (per_cu, dwarf2_per_objfile, false);
927aa2e7
JK
5706 }
5707}
24c79950 5708
3b00ef10
TT
5709static void
5710dw2_debug_names_map_matching_symbols
5711 (struct objfile *objfile,
5712 const lookup_name_info &name, domain_enum domain,
5713 int global,
5714 gdb::function_view<symbol_found_callback_ftype> callback,
5715 symbol_compare_ftype *ordered_compare)
5716{
5717 struct dwarf2_per_objfile *dwarf2_per_objfile
5718 = get_dwarf2_per_objfile (objfile);
5719
5720 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5721 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5722 return;
5723
5989a64e 5724 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5725 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5726
5727 const char *match_name = name.ada ().lookup_name ().c_str ();
5728 auto matcher = [&] (const char *symname)
5729 {
5730 if (ordered_compare == nullptr)
5731 return true;
5732 return ordered_compare (symname, match_name) == 0;
5733 };
5734
5735 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5736 [&] (offset_type namei)
5737 {
5738 /* The name was matched, now expand corresponding CUs that were
5739 marked. */
5740 dw2_debug_names_iterator iter (map, block_kind, domain, namei);
5741
5742 struct dwarf2_per_cu_data *per_cu;
5743 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5744 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile, nullptr,
5745 nullptr);
3b00ef10
TT
5746 return true;
5747 });
5748
5749 /* It's a shame we couldn't do this inside the
5750 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5751 that have already been expanded. Instead, this loop matches what
5752 the psymtab code does. */
5989a64e 5753 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3b00ef10 5754 {
af758d11
SM
5755 compunit_symtab *symtab = dwarf2_per_objfile->get_symtab (per_cu);
5756 if (symtab != nullptr)
3b00ef10
TT
5757 {
5758 const struct block *block
af758d11 5759 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5760 if (!iterate_over_symbols_terminated (block, name,
5761 domain, callback))
5762 break;
5763 }
5764 }
5765}
5766
927aa2e7
JK
5767static void
5768dw2_debug_names_expand_symtabs_matching
5769 (struct objfile *objfile,
5770 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5771 const lookup_name_info *lookup_name,
927aa2e7
JK
5772 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5773 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5774 enum search_domain kind)
5775{
ed2dc618
SM
5776 struct dwarf2_per_objfile *dwarf2_per_objfile
5777 = get_dwarf2_per_objfile (objfile);
9291a0cd 5778
927aa2e7 5779 /* debug_names_table is NULL if OBJF_READNOW. */
5989a64e 5780 if (!dwarf2_per_objfile->per_bfd->debug_names_table)
927aa2e7 5781 return;
9291a0cd 5782
ed2dc618 5783 dw_expand_symtabs_matching_file_matcher (dwarf2_per_objfile, file_matcher);
24c79950 5784
c1a66c06
TV
5785 if (symbol_matcher == NULL && lookup_name == NULL)
5786 {
5989a64e 5787 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
5788 {
5789 QUIT;
5790
97a1449a
SM
5791 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5792 file_matcher, expansion_notify);
c1a66c06
TV
5793 }
5794 return;
5795 }
5796
5989a64e 5797 mapped_debug_names &map = *dwarf2_per_objfile->per_bfd->debug_names_table;
bbf2f4df 5798
c1a66c06 5799 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
5800 symbol_matcher,
5801 kind, [&] (offset_type namei)
927aa2e7 5802 {
927aa2e7
JK
5803 /* The name was matched, now expand corresponding CUs that were
5804 marked. */
5805 dw2_debug_names_iterator iter (map, kind, namei);
bbf2f4df 5806
927aa2e7
JK
5807 struct dwarf2_per_cu_data *per_cu;
5808 while ((per_cu = iter.next ()) != NULL)
97a1449a
SM
5809 dw2_expand_symtabs_matching_one (per_cu, dwarf2_per_objfile,
5810 file_matcher, expansion_notify);
3b00ef10 5811 return true;
44ed8f3e 5812 });
9291a0cd
TT
5813}
5814
927aa2e7 5815const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
5816{
5817 dw2_has_symbols,
5818 dw2_find_last_source_symtab,
5819 dw2_forget_cached_source_info,
f8eba3c6 5820 dw2_map_symtabs_matching_filename,
927aa2e7 5821 dw2_debug_names_lookup_symbol,
d3214198 5822 NULL,
9291a0cd 5823 dw2_print_stats,
927aa2e7 5824 dw2_debug_names_dump,
927aa2e7 5825 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 5826 dw2_expand_all_symtabs,
652a8996 5827 dw2_expand_symtabs_with_fullname,
3b00ef10 5828 dw2_debug_names_map_matching_symbols,
927aa2e7 5829 dw2_debug_names_expand_symtabs_matching,
43f3e411 5830 dw2_find_pc_sect_compunit_symtab,
71a3c369 5831 NULL,
9291a0cd
TT
5832 dw2_map_symbol_filenames
5833};
5834
4485a1c1 5835/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 5836 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
5837
5838template <typename T>
5839static gdb::array_view<const gdb_byte>
5840get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
5841{
5842 dwarf2_section_info *section = &section_owner->gdb_index;
5843
96b79293 5844 if (section->empty ())
4485a1c1
SM
5845 return {};
5846
5847 /* Older elfutils strip versions could keep the section in the main
5848 executable while splitting it for the separate debug info file. */
96b79293 5849 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
5850 return {};
5851
96b79293 5852 section->read (obj);
4485a1c1 5853
8bebfcda
PA
5854 /* dwarf2_section_info::size is a bfd_size_type, while
5855 gdb::array_view works with size_t. On 32-bit hosts, with
5856 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
5857 is 32-bit. So we need an explicit narrowing conversion here.
5858 This is fine, because it's impossible to allocate or mmap an
5859 array/buffer larger than what size_t can represent. */
5860 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
5861}
5862
87d6a7aa
SM
5863/* Lookup the index cache for the contents of the index associated to
5864 DWARF2_OBJ. */
5865
5866static gdb::array_view<const gdb_byte>
5989a64e 5867get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
5868{
5869 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
5870 if (build_id == nullptr)
5871 return {};
5872
5873 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 5874 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
5875}
5876
5877/* Same as the above, but for DWZ. */
5878
5879static gdb::array_view<const gdb_byte>
5880get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
5881{
5882 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
5883 if (build_id == nullptr)
5884 return {};
5885
5886 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
5887}
5888
3c0aa29a 5889/* See symfile.h. */
9291a0cd 5890
3c0aa29a
PA
5891bool
5892dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 5893{
ed2dc618
SM
5894 struct dwarf2_per_objfile *dwarf2_per_objfile
5895 = get_dwarf2_per_objfile (objfile);
5896
9291a0cd
TT
5897 /* If we're about to read full symbols, don't bother with the
5898 indices. In this case we also don't care if some other debug
5899 format is making psymtabs, because they are all about to be
5900 expanded anyway. */
5901 if ((objfile->flags & OBJF_READNOW))
5902 {
5989a64e 5903 dwarf2_per_objfile->per_bfd->using_index = 1;
ed2dc618
SM
5904 create_all_comp_units (dwarf2_per_objfile);
5905 create_all_type_units (dwarf2_per_objfile);
5989a64e 5906 dwarf2_per_objfile->per_bfd->quick_file_names_table
b76e467d 5907 = create_quick_file_names_table
5989a64e 5908 (dwarf2_per_objfile->per_bfd->all_comp_units.size ());
af758d11 5909 dwarf2_per_objfile->resize_symtabs ();
9291a0cd 5910
5989a64e
SM
5911 for (int i = 0; i < (dwarf2_per_objfile->per_bfd->all_comp_units.size ()
5912 + dwarf2_per_objfile->per_bfd->all_type_units.size ()); ++i)
9291a0cd 5913 {
5989a64e 5914 dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->per_bfd->get_cutu (i);
9291a0cd 5915
5989a64e 5916 per_cu->v.quick = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
e254ef6a 5917 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
5918 }
5919
5920 /* Return 1 so that gdb sees the "quick" functions. However,
5921 these functions will be no-ops because we will have expanded
5922 all symtabs. */
3c0aa29a
PA
5923 *index_kind = dw_index_kind::GDB_INDEX;
5924 return true;
9291a0cd
TT
5925 }
5926
ed2dc618 5927 if (dwarf2_read_debug_names (dwarf2_per_objfile))
3c0aa29a
PA
5928 {
5929 *index_kind = dw_index_kind::DEBUG_NAMES;
af758d11 5930 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5931 return true;
5932 }
927aa2e7 5933
4485a1c1 5934 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5989a64e 5935 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 5936 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
5937 {
5938 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5939 dwarf2_per_objfile->resize_symtabs ();
3c0aa29a
PA
5940 return true;
5941 }
9291a0cd 5942
87d6a7aa
SM
5943 /* ... otherwise, try to find the index in the index cache. */
5944 if (dwarf2_read_gdb_index (dwarf2_per_objfile,
5945 get_gdb_index_contents_from_cache,
5946 get_gdb_index_contents_from_cache_dwz))
5947 {
5948 global_index_cache.hit ();
5949 *index_kind = dw_index_kind::GDB_INDEX;
af758d11 5950 dwarf2_per_objfile->resize_symtabs ();
87d6a7aa
SM
5951 return true;
5952 }
5953
5954 global_index_cache.miss ();
3c0aa29a 5955 return false;
9291a0cd
TT
5956}
5957
5958\f
5959
dce234bc
PP
5960/* Build a partial symbol table. */
5961
5962void
f29dff0a 5963dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 5964{
ed2dc618
SM
5965 struct dwarf2_per_objfile *dwarf2_per_objfile
5966 = get_dwarf2_per_objfile (objfile);
c9bf0622 5967
6eee24ce 5968 init_psymbol_list (objfile, 1024);
c906108c 5969
a70b8144 5970 try
c9bf0622
TT
5971 {
5972 /* This isn't really ideal: all the data we allocate on the
5973 objfile's obstack is still uselessly kept around. However,
5974 freeing it seems unsafe. */
906768f9 5975 psymtab_discarder psymtabs (objfile);
ed2dc618 5976 dwarf2_build_psymtabs_hard (dwarf2_per_objfile);
906768f9 5977 psymtabs.keep ();
87d6a7aa 5978
af758d11
SM
5979 dwarf2_per_objfile->resize_symtabs ();
5980
87d6a7aa
SM
5981 /* (maybe) store an index in the cache. */
5982 global_index_cache.store (dwarf2_per_objfile);
c9bf0622 5983 }
230d2906 5984 catch (const gdb_exception_error &except)
492d29ea
PA
5985 {
5986 exception_print (gdb_stderr, except);
5987 }
c906108c 5988}
c906108c 5989
3b80fe9b
DE
5990/* Find the base address of the compilation unit for range lists and
5991 location lists. It will normally be specified by DW_AT_low_pc.
5992 In DWARF-3 draft 4, the base address could be overridden by
5993 DW_AT_entry_pc. It's been removed, but GCC still uses this for
5994 compilation units with discontinuous ranges. */
5995
5996static void
5997dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
5998{
5999 struct attribute *attr;
6000
2b24b6e4 6001 cu->base_address.reset ();
3b80fe9b
DE
6002
6003 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6004 if (attr != nullptr)
2b24b6e4 6005 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6006 else
6007 {
6008 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6009 if (attr != nullptr)
2b24b6e4 6010 cu->base_address = attr->value_as_address ();
3b80fe9b
DE
6011 }
6012}
6013
36586728
TT
6014/* Helper function that returns the proper abbrev section for
6015 THIS_CU. */
6016
6017static struct dwarf2_section_info *
6018get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6019{
6020 struct dwarf2_section_info *abbrev;
c3699833 6021 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6022
6023 if (this_cu->is_dwz)
c3699833 6024 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6025 else
c3699833 6026 abbrev = &per_bfd->abbrev;
36586728
TT
6027
6028 return abbrev;
6029}
6030
f4dc4d17
DE
6031/* Fetch the abbreviation table offset from a comp or type unit header. */
6032
6033static sect_offset
ed2dc618
SM
6034read_abbrev_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
6035 struct dwarf2_section_info *section,
9c541725 6036 sect_offset sect_off)
f4dc4d17 6037{
96b79293 6038 bfd *abfd = section->get_bfd_owner ();
d521ce57 6039 const gdb_byte *info_ptr;
ac298888 6040 unsigned int initial_length_size, offset_size;
43988095 6041 uint16_t version;
f4dc4d17 6042
96b79293 6043 section->read (dwarf2_per_objfile->objfile);
9c541725 6044 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6045 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6046 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6047 info_ptr += initial_length_size;
6048
6049 version = read_2_bytes (abfd, info_ptr);
6050 info_ptr += 2;
6051 if (version >= 5)
6052 {
6053 /* Skip unit type and address size. */
6054 info_ptr += 2;
6055 }
6056
24aa364d 6057 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6058}
6059
b83470bf
TT
6060/* A partial symtab that is used only for include files. */
6061struct dwarf2_include_psymtab : public partial_symtab
6062{
6063 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6064 : partial_symtab (filename, objfile)
6065 {
6066 }
6067
6068 void read_symtab (struct objfile *objfile) override
6069 {
194d088f
TV
6070 /* It's an include file, no symbols to read for it.
6071 Everything is in the includer symtab. */
6072
6073 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6074 expansion of the includer psymtab. We use the dependencies[0] field to
6075 model the includer. But if we go the regular route of calling
6076 expand_psymtab here, and having expand_psymtab call expand_dependencies
6077 to expand the includer, we'll only use expand_psymtab on the includer
6078 (making it a non-toplevel psymtab), while if we expand the includer via
6079 another path, we'll use read_symtab (making it a toplevel psymtab).
6080 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6081 psymtab, and trigger read_symtab on the includer here directly. */
6082 includer ()->read_symtab (objfile);
b83470bf
TT
6083 }
6084
6085 void expand_psymtab (struct objfile *objfile) override
6086 {
194d088f
TV
6087 /* This is not called by read_symtab, and should not be called by any
6088 expand_dependencies. */
6089 gdb_assert (false);
b83470bf
TT
6090 }
6091
5717c425 6092 bool readin_p (struct objfile *objfile) const override
b83470bf 6093 {
5717c425 6094 return includer ()->readin_p (objfile);
b83470bf
TT
6095 }
6096
5717c425 6097 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6098 {
6099 return nullptr;
6100 }
6101
6102private:
194d088f
TV
6103 partial_symtab *includer () const
6104 {
6105 /* An include psymtab has exactly one dependency: the psymtab that
6106 includes it. */
6107 gdb_assert (this->number_of_dependencies == 1);
6108 return this->dependencies[0];
6109 }
b83470bf
TT
6110};
6111
aaa75496
JB
6112/* Allocate a new partial symtab for file named NAME and mark this new
6113 partial symtab as being an include of PST. */
6114
6115static void
891813be 6116dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
aaa75496
JB
6117 struct objfile *objfile)
6118{
b83470bf 6119 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6120
fbd9ab74 6121 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6122 subpst->dirname = pst->dirname;
fbd9ab74 6123
a9342b62 6124 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6125 subpst->dependencies[0] = pst;
6126 subpst->number_of_dependencies = 1;
aaa75496
JB
6127}
6128
6129/* Read the Line Number Program data and extract the list of files
6130 included by the source file represented by PST. Build an include
d85a05f0 6131 partial symtab for each of these included files. */
aaa75496
JB
6132
6133static void
6134dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6135 struct die_info *die,
891813be 6136 dwarf2_psymtab *pst)
aaa75496 6137{
fff8551c 6138 line_header_up lh;
d85a05f0 6139 struct attribute *attr;
aaa75496 6140
d85a05f0 6141 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
435d3d88 6142 if (attr != nullptr)
9c541725 6143 lh = dwarf_decode_line_header ((sect_offset) DW_UNSND (attr), cu);
aaa75496
JB
6144 if (lh == NULL)
6145 return; /* No linetable, so no includes. */
6146
79748972
TT
6147 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6148 that we pass in the raw text_low here; that is ok because we're
6149 only decoding the line table to make include partial symtabs, and
6150 so the addresses aren't really used. */
4ae976d1 6151 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6152 pst->raw_text_low (), 1);
aaa75496
JB
6153}
6154
348e048f 6155static hashval_t
52dc124a 6156hash_signatured_type (const void *item)
348e048f 6157{
9a3c8263
SM
6158 const struct signatured_type *sig_type
6159 = (const struct signatured_type *) item;
9a619af0 6160
348e048f 6161 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6162 return sig_type->signature;
348e048f
DE
6163}
6164
6165static int
52dc124a 6166eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6167{
9a3c8263
SM
6168 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6169 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6170
348e048f
DE
6171 return lhs->signature == rhs->signature;
6172}
6173
1fd400ff
TT
6174/* Allocate a hash table for signatured types. */
6175
b0b6a987 6176static htab_up
298e9637 6177allocate_signatured_type_table ()
1fd400ff 6178{
b0b6a987
TT
6179 return htab_up (htab_create_alloc (41,
6180 hash_signatured_type,
6181 eq_signatured_type,
6182 NULL, xcalloc, xfree));
1fd400ff
TT
6183}
6184
d467dd73 6185/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6186
6187static int
d467dd73 6188add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6189{
9a3c8263 6190 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6191 std::vector<signatured_type *> *all_type_units
6192 = (std::vector<signatured_type *> *) datum;
1fd400ff 6193
b2bdb8cf 6194 all_type_units->push_back (sigt);
1fd400ff
TT
6195
6196 return 1;
6197}
6198
78d4d2c5 6199/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6200 and fill them into TYPES_HTAB. It will process only type units,
6201 therefore DW_UT_type. */
c88ee1f0 6202
78d4d2c5 6203static void
ed2dc618
SM
6204create_debug_type_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6205 struct dwo_file *dwo_file,
b0b6a987 6206 dwarf2_section_info *section, htab_up &types_htab,
43988095 6207 rcuh_kind section_kind)
348e048f 6208{
3019eac3 6209 struct objfile *objfile = dwarf2_per_objfile->objfile;
4bdcc0c1 6210 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6211 bfd *abfd;
6212 const gdb_byte *info_ptr, *end_ptr;
348e048f 6213
4bdcc0c1
DE
6214 abbrev_section = (dwo_file != NULL
6215 ? &dwo_file->sections.abbrev
5989a64e 6216 : &dwarf2_per_objfile->per_bfd->abbrev);
4bdcc0c1 6217
b4f54984 6218 if (dwarf_read_debug)
43988095 6219 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
6220 section->get_name (),
6221 abbrev_section->get_file_name ());
09406207 6222
96b79293 6223 section->read (objfile);
78d4d2c5 6224 info_ptr = section->buffer;
348e048f 6225
78d4d2c5
JK
6226 if (info_ptr == NULL)
6227 return;
348e048f 6228
78d4d2c5
JK
6229 /* We can't set abfd until now because the section may be empty or
6230 not present, in which case the bfd is unknown. */
96b79293 6231 abfd = section->get_bfd_owner ();
348e048f 6232
c0ab21c2
TT
6233 /* We don't use cutu_reader here because we don't need to read
6234 any dies: the signature is in the header. */
3019eac3 6235
78d4d2c5
JK
6236 end_ptr = info_ptr + section->size;
6237 while (info_ptr < end_ptr)
6238 {
78d4d2c5
JK
6239 struct signatured_type *sig_type;
6240 struct dwo_unit *dwo_tu;
6241 void **slot;
6242 const gdb_byte *ptr = info_ptr;
6243 struct comp_unit_head header;
6244 unsigned int length;
8b70b953 6245
9c541725 6246 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6247
a49dd8dd
JK
6248 /* Initialize it due to a false compiler warning. */
6249 header.signature = -1;
9c541725 6250 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6251
78d4d2c5
JK
6252 /* We need to read the type's signature in order to build the hash
6253 table, but we don't need anything else just yet. */
348e048f 6254
ed2dc618 6255 ptr = read_and_check_comp_unit_head (dwarf2_per_objfile, &header, section,
43988095 6256 abbrev_section, ptr, section_kind);
348e048f 6257
4057dfde 6258 length = header.get_length ();
6caca83c 6259
78d4d2c5
JK
6260 /* Skip dummy type units. */
6261 if (ptr >= info_ptr + length
43988095
JK
6262 || peek_abbrev_code (abfd, ptr) == 0
6263 || header.unit_type != DW_UT_type)
78d4d2c5
JK
6264 {
6265 info_ptr += length;
6266 continue;
6267 }
dee91e82 6268
78d4d2c5
JK
6269 if (types_htab == NULL)
6270 {
6271 if (dwo_file)
298e9637 6272 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6273 else
298e9637 6274 types_htab = allocate_signatured_type_table ();
78d4d2c5 6275 }
8b70b953 6276
78d4d2c5
JK
6277 if (dwo_file)
6278 {
6279 sig_type = NULL;
5989a64e 6280 dwo_tu = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
78d4d2c5
JK
6281 struct dwo_unit);
6282 dwo_tu->dwo_file = dwo_file;
43988095 6283 dwo_tu->signature = header.signature;
9c541725 6284 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6285 dwo_tu->section = section;
9c541725 6286 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6287 dwo_tu->length = length;
6288 }
6289 else
6290 {
6291 /* N.B.: type_offset is not usable if this type uses a DWO file.
6292 The real type_offset is in the DWO file. */
6293 dwo_tu = NULL;
5989a64e 6294 sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
43988095 6295 sig_type->signature = header.signature;
9c541725 6296 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6297 sig_type->per_cu.is_debug_types = 1;
6298 sig_type->per_cu.section = section;
9c541725 6299 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6300 sig_type->per_cu.length = length;
6301 }
6302
b0b6a987 6303 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6304 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6305 INSERT);
6306 gdb_assert (slot != NULL);
6307 if (*slot != NULL)
6308 {
9c541725 6309 sect_offset dup_sect_off;
0349ea22 6310
3019eac3
DE
6311 if (dwo_file)
6312 {
78d4d2c5
JK
6313 const struct dwo_unit *dup_tu
6314 = (const struct dwo_unit *) *slot;
6315
9c541725 6316 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6317 }
6318 else
6319 {
78d4d2c5
JK
6320 const struct signatured_type *dup_tu
6321 = (const struct signatured_type *) *slot;
6322
9c541725 6323 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6324 }
8b70b953 6325
b98664d3 6326 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6327 " the entry at offset %s, signature %s"),
6328 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6329 hex_string (header.signature));
78d4d2c5
JK
6330 }
6331 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6332
78d4d2c5 6333 if (dwarf_read_debug > 1)
9d8780f0
SM
6334 fprintf_unfiltered (gdb_stdlog, " offset %s, signature %s\n",
6335 sect_offset_str (sect_off),
43988095 6336 hex_string (header.signature));
3019eac3 6337
78d4d2c5
JK
6338 info_ptr += length;
6339 }
6340}
3019eac3 6341
78d4d2c5
JK
6342/* Create the hash table of all entries in the .debug_types
6343 (or .debug_types.dwo) section(s).
6344 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6345 otherwise it is NULL.
b3c8eb43 6346
78d4d2c5 6347 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6348
78d4d2c5 6349 Note: This function processes DWO files only, not DWP files. */
348e048f 6350
78d4d2c5 6351static void
ed2dc618
SM
6352create_debug_types_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
6353 struct dwo_file *dwo_file,
fd5866f6 6354 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6355 htab_up &types_htab)
78d4d2c5 6356{
fd5866f6
SM
6357 for (dwarf2_section_info &section : type_sections)
6358 create_debug_type_hash_table (dwarf2_per_objfile, dwo_file, &section,
ed2dc618 6359 types_htab, rcuh_kind::TYPE);
3019eac3
DE
6360}
6361
6362/* Create the hash table of all entries in the .debug_types section,
6363 and initialize all_type_units.
6364 The result is zero if there is an error (e.g. missing .debug_types section),
6365 otherwise non-zero. */
6366
6367static int
ed2dc618 6368create_all_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
3019eac3 6369{
b0b6a987 6370 htab_up types_htab;
3019eac3 6371
ed2dc618 6372 create_debug_type_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6373 &dwarf2_per_objfile->per_bfd->info, types_htab,
43988095 6374 rcuh_kind::COMPILE);
ed2dc618 6375 create_debug_types_hash_table (dwarf2_per_objfile, NULL,
5989a64e 6376 dwarf2_per_objfile->per_bfd->types, types_htab);
3019eac3
DE
6377 if (types_htab == NULL)
6378 {
5989a64e 6379 dwarf2_per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6380 return 0;
6381 }
6382
5989a64e 6383 dwarf2_per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6384
5989a64e
SM
6385 gdb_assert (dwarf2_per_objfile->per_bfd->all_type_units.empty ());
6386 dwarf2_per_objfile->per_bfd->all_type_units.reserve
6387 (htab_elements (dwarf2_per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6388
5989a64e 6389 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6390 add_signatured_type_cu_to_table,
5989a64e 6391 &dwarf2_per_objfile->per_bfd->all_type_units);
1fd400ff 6392
348e048f
DE
6393 return 1;
6394}
6395
5989a64e 6396/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6397 If SLOT is non-NULL, it is the entry to use in the hash table.
6398 Otherwise we find one. */
6399
6400static struct signatured_type *
ed2dc618
SM
6401add_type_unit (struct dwarf2_per_objfile *dwarf2_per_objfile, ULONGEST sig,
6402 void **slot)
6aa5f3a6 6403{
5989a64e
SM
6404 if (dwarf2_per_objfile->per_bfd->all_type_units.size ()
6405 == dwarf2_per_objfile->per_bfd->all_type_units.capacity ())
6406 ++dwarf2_per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6407
5989a64e 6408 signatured_type *sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6409
af758d11
SM
6410 dwarf2_per_objfile->resize_symtabs ();
6411
5989a64e 6412 dwarf2_per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6413 sig_type->signature = sig;
6414 sig_type->per_cu.is_debug_types = 1;
5989a64e 6415 if (dwarf2_per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6416 {
6417 sig_type->per_cu.v.quick =
5989a64e 6418 OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6419 struct dwarf2_per_cu_quick_data);
6420 }
6421
6422 if (slot == NULL)
6423 {
5989a64e 6424 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6425 sig_type, INSERT);
6426 }
6427 gdb_assert (*slot == NULL);
6428 *slot = sig_type;
6429 /* The rest of sig_type must be filled in by the caller. */
6430 return sig_type;
6431}
6432
a2ce51a0
DE
6433/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6434 Fill in SIG_ENTRY with DWO_ENTRY. */
6435
6436static void
ed2dc618 6437fill_in_sig_entry_from_dwo_entry (struct dwarf2_per_objfile *dwarf2_per_objfile,
a2ce51a0
DE
6438 struct signatured_type *sig_entry,
6439 struct dwo_unit *dwo_entry)
6440{
1859c670
SM
6441 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
6442
7ee85ab1 6443 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6444 gdb_assert (! sig_entry->per_cu.queued);
7188ed02 6445 gdb_assert (dwarf2_per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6446 if (per_bfd->using_index)
6aa5f3a6
DE
6447 {
6448 gdb_assert (sig_entry->per_cu.v.quick != NULL);
af758d11 6449 gdb_assert (!dwarf2_per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6450 }
6451 else
6452 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6453 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6454 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6455 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6456 gdb_assert (sig_entry->dwo_unit == NULL);
6457
6458 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6459 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6460 sig_entry->per_cu.length = dwo_entry->length;
6461 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6462 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6463 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6464 sig_entry->dwo_unit = dwo_entry;
6465}
6466
6467/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6468 If we haven't read the TU yet, create the signatured_type data structure
6469 for a TU to be read in directly from a DWO file, bypassing the stub.
6470 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6471 using .gdb_index, then when reading a CU we want to stay in the DWO file
6472 containing that CU. Otherwise we could end up reading several other DWO
6473 files (due to comdat folding) to process the transitive closure of all the
6474 mentioned TUs, and that can be slow. The current DWO file will have every
6475 type signature that it needs.
a2ce51a0
DE
6476 We only do this for .gdb_index because in the psymtab case we already have
6477 to read all the DWOs to build the type unit groups. */
6478
6479static struct signatured_type *
6480lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6481{
5e22e966 6482 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
a2ce51a0
DE
6483 struct dwo_file *dwo_file;
6484 struct dwo_unit find_dwo_entry, *dwo_entry;
6485 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6486 void **slot;
a2ce51a0 6487
5989a64e 6488 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0 6489
6aa5f3a6
DE
6490 /* If TU skeletons have been removed then we may not have read in any
6491 TUs yet. */
5989a64e
SM
6492 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6493 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6494
6495 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6496 Use the global signatured_types array to do our own comdat-folding
6497 of types. If this is the first time we're reading this TU, and
6498 the TU has an entry in .gdb_index, replace the recorded data from
6499 .gdb_index with this TU. */
a2ce51a0 6500
a2ce51a0 6501 find_sig_entry.signature = sig;
5989a64e 6502 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6503 &find_sig_entry, INSERT);
9a3c8263 6504 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6505
6506 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6507 read. Don't reassign the global entry to point to this DWO if that's
6508 the case. Also note that if the TU is already being read, it may not
6509 have come from a DWO, the program may be a mix of Fission-compiled
6510 code and non-Fission-compiled code. */
6511
6512 /* Have we already tried to read this TU?
6513 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6514 needn't exist in the global table yet). */
6515 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6516 return sig_entry;
6517
6aa5f3a6
DE
6518 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6519 dwo_unit of the TU itself. */
6520 dwo_file = cu->dwo_unit->dwo_file;
6521
a2ce51a0
DE
6522 /* Ok, this is the first time we're reading this TU. */
6523 if (dwo_file->tus == NULL)
6524 return NULL;
6525 find_dwo_entry.signature = sig;
b0b6a987
TT
6526 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6527 &find_dwo_entry);
a2ce51a0
DE
6528 if (dwo_entry == NULL)
6529 return NULL;
6530
6aa5f3a6
DE
6531 /* If the global table doesn't have an entry for this TU, add one. */
6532 if (sig_entry == NULL)
ed2dc618 6533 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6aa5f3a6 6534
ed2dc618 6535 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
89e63ee4 6536 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6537 return sig_entry;
6538}
6539
a2ce51a0
DE
6540/* Subroutine of lookup_signatured_type.
6541 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6542 then try the DWP file. If the TU stub (skeleton) has been removed then
6543 it won't be in .gdb_index. */
a2ce51a0
DE
6544
6545static struct signatured_type *
6546lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6547{
5e22e966 6548 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6549 struct dwp_file *dwp_file = get_dwp_file (dwarf2_per_objfile);
a2ce51a0
DE
6550 struct dwo_unit *dwo_entry;
6551 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6552 void **slot;
a2ce51a0 6553
5989a64e 6554 gdb_assert (cu->dwo_unit && dwarf2_per_objfile->per_bfd->using_index);
a2ce51a0
DE
6555 gdb_assert (dwp_file != NULL);
6556
6aa5f3a6
DE
6557 /* If TU skeletons have been removed then we may not have read in any
6558 TUs yet. */
5989a64e
SM
6559 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
6560 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6561
6aa5f3a6 6562 find_sig_entry.signature = sig;
5989a64e 6563 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6564 &find_sig_entry, INSERT);
9a3c8263 6565 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6566
6567 /* Have we already tried to read this TU?
6568 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6569 needn't exist in the global table yet). */
6570 if (sig_entry != NULL)
6571 return sig_entry;
6572
a2ce51a0
DE
6573 if (dwp_file->tus == NULL)
6574 return NULL;
ed2dc618 6575 dwo_entry = lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, NULL,
57d63ce2 6576 sig, 1 /* is_debug_types */);
a2ce51a0
DE
6577 if (dwo_entry == NULL)
6578 return NULL;
6579
ed2dc618
SM
6580 sig_entry = add_type_unit (dwarf2_per_objfile, sig, slot);
6581 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, sig_entry, dwo_entry);
a2ce51a0 6582
a2ce51a0
DE
6583 return sig_entry;
6584}
6585
380bca97 6586/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6587 Returns NULL if signature SIG is not present in the table.
6588 It is up to the caller to complain about this. */
348e048f
DE
6589
6590static struct signatured_type *
a2ce51a0 6591lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6592{
5e22e966 6593 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 6594
a2ce51a0 6595 if (cu->dwo_unit
5989a64e 6596 && dwarf2_per_objfile->per_bfd->using_index)
a2ce51a0
DE
6597 {
6598 /* We're in a DWO/DWP file, and we're using .gdb_index.
6599 These cases require special processing. */
ed2dc618 6600 if (get_dwp_file (dwarf2_per_objfile) == NULL)
a2ce51a0
DE
6601 return lookup_dwo_signatured_type (cu, sig);
6602 else
6603 return lookup_dwp_signatured_type (cu, sig);
6604 }
6605 else
6606 {
6607 struct signatured_type find_entry, *entry;
348e048f 6608
5989a64e 6609 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6610 return NULL;
6611 find_entry.signature = sig;
9a3c8263 6612 entry = ((struct signatured_type *)
5989a64e 6613 htab_find (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6614 &find_entry));
a2ce51a0
DE
6615 return entry;
6616 }
348e048f 6617}
18a8505e 6618
42e7ad6c 6619/* Low level DIE reading support. */
348e048f 6620
d85a05f0
DJ
6621/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6622
6623static void
6624init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6625 struct dwarf2_cu *cu,
3019eac3 6626 struct dwarf2_section_info *section,
685af9cd
TT
6627 struct dwo_file *dwo_file,
6628 struct abbrev_table *abbrev_table)
d85a05f0 6629{
fceca515 6630 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6631 reader->abfd = section->get_bfd_owner ();
d85a05f0 6632 reader->cu = cu;
3019eac3 6633 reader->dwo_file = dwo_file;
dee91e82
DE
6634 reader->die_section = section;
6635 reader->buffer = section->buffer;
f664829e 6636 reader->buffer_end = section->buffer + section->size;
685af9cd 6637 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6638}
6639
c0ab21c2 6640/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6641 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6642 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6643 already.
6644
6645 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6646 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6647 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6648 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6649 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6650 STUB_COMP_DIR may be non-NULL.
3e225074 6651 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6652 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6653 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6654 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6655 kept around for at least as long as *RESULT_READER.
6656
b0c7bfa9
DE
6657 The result is non-zero if a valid (non-dummy) DIE was found. */
6658
6659static int
4ab09049 6660read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6661 struct dwo_unit *dwo_unit,
b0c7bfa9 6662 struct die_info *stub_comp_unit_die,
a2ce51a0 6663 const char *stub_comp_dir,
b0c7bfa9 6664 struct die_reader_specs *result_reader,
d521ce57 6665 const gdb_byte **result_info_ptr,
b0c7bfa9 6666 struct die_info **result_comp_unit_die,
685af9cd 6667 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6668{
4ab09049
SM
6669 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
6670 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6671 struct objfile *objfile = dwarf2_per_objfile->objfile;
b0c7bfa9 6672 bfd *abfd;
d521ce57 6673 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6674 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6675 int i,num_extra_attrs;
6676 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6677 struct die_info *comp_unit_die;
6678
b0aeadb3
DE
6679 /* At most one of these may be provided. */
6680 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6681
b0c7bfa9
DE
6682 /* These attributes aren't processed until later:
6683 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6684 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6685 referenced later. However, these attributes are found in the stub
6686 which we won't have later. In order to not impose this complication
6687 on the rest of the code, we read them here and copy them to the
6688 DWO CU/TU die. */
b0c7bfa9
DE
6689
6690 stmt_list = NULL;
6691 low_pc = NULL;
6692 high_pc = NULL;
6693 ranges = NULL;
6694 comp_dir = NULL;
6695
6696 if (stub_comp_unit_die != NULL)
6697 {
6698 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6699 DWO file. */
4ab09049 6700 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6701 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6702 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6703 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6704 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6705 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6706
a39fdb41 6707 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6708
18a8505e
AT
6709 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6710 here (if needed). We need the value before we can process
6711 DW_AT_ranges. */
a39fdb41 6712 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6713 }
a2ce51a0
DE
6714 else if (stub_comp_dir != NULL)
6715 {
6716 /* Reconstruct the comp_dir attribute to simplify the code below. */
8d749320 6717 comp_dir = XOBNEW (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6718 comp_dir->name = DW_AT_comp_dir;
6719 comp_dir->form = DW_FORM_string;
6720 DW_STRING_IS_CANONICAL (comp_dir) = 0;
6721 DW_STRING (comp_dir) = stub_comp_dir;
6722 }
b0c7bfa9
DE
6723
6724 /* Set up for reading the DWO CU/TU. */
6725 cu->dwo_unit = dwo_unit;
685af9cd 6726 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6727 section->read (objfile);
6728 abfd = section->get_bfd_owner ();
9c541725
PA
6729 begin_info_ptr = info_ptr = (section->buffer
6730 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6731 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6732
4ab09049 6733 if (per_cu->is_debug_types)
b0c7bfa9 6734 {
4ab09049 6735 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6736
ed2dc618
SM
6737 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6738 &cu->header, section,
b0c7bfa9 6739 dwo_abbrev_section,
43988095 6740 info_ptr, rcuh_kind::TYPE);
a2ce51a0 6741 /* This is not an assert because it can be caused by bad debug info. */
43988095 6742 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
6743 {
6744 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 6745 " TU at offset %s [in module %s]"),
a2ce51a0 6746 hex_string (sig_type->signature),
43988095 6747 hex_string (cu->header.signature),
9d8780f0 6748 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
6749 bfd_get_filename (abfd));
6750 }
9c541725 6751 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6752 /* For DWOs coming from DWP files, we don't know the CU length
6753 nor the type's offset in the TU until now. */
4057dfde 6754 dwo_unit->length = cu->header.get_length ();
9c541725 6755 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
6756
6757 /* Establish the type offset that can be used to lookup the type.
6758 For DWO files, we don't know it until now. */
9c541725
PA
6759 sig_type->type_offset_in_section
6760 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
6761 }
6762 else
6763 {
ed2dc618
SM
6764 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
6765 &cu->header, section,
b0c7bfa9 6766 dwo_abbrev_section,
43988095 6767 info_ptr, rcuh_kind::COMPILE);
9c541725 6768 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
6769 /* For DWOs coming from DWP files, we don't know the CU length
6770 until now. */
4057dfde 6771 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
6772 }
6773
685af9cd 6774 *result_dwo_abbrev_table
86de1d91
TT
6775 = abbrev_table::read (objfile, dwo_abbrev_section,
6776 cu->header.abbrev_sect_off);
685af9cd
TT
6777 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
6778 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
6779
6780 /* Read in the die, but leave space to copy over the attributes
6781 from the stub. This has the benefit of simplifying the rest of
6782 the code - all the work to maintain the illusion of a single
6783 DW_TAG_{compile,type}_unit DIE is done here. */
6784 num_extra_attrs = ((stmt_list != NULL)
6785 + (low_pc != NULL)
6786 + (high_pc != NULL)
6787 + (ranges != NULL)
6788 + (comp_dir != NULL));
6789 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 6790 num_extra_attrs);
b0c7bfa9
DE
6791
6792 /* Copy over the attributes from the stub to the DIE we just read in. */
6793 comp_unit_die = *result_comp_unit_die;
6794 i = comp_unit_die->num_attrs;
6795 if (stmt_list != NULL)
6796 comp_unit_die->attrs[i++] = *stmt_list;
6797 if (low_pc != NULL)
6798 comp_unit_die->attrs[i++] = *low_pc;
6799 if (high_pc != NULL)
6800 comp_unit_die->attrs[i++] = *high_pc;
6801 if (ranges != NULL)
6802 comp_unit_die->attrs[i++] = *ranges;
6803 if (comp_dir != NULL)
6804 comp_unit_die->attrs[i++] = *comp_dir;
6805 comp_unit_die->num_attrs += num_extra_attrs;
6806
b4f54984 6807 if (dwarf_die_debug)
bf6af496
DE
6808 {
6809 fprintf_unfiltered (gdb_stdlog,
6810 "Read die from %s@0x%x of %s:\n",
96b79293 6811 section->get_name (),
bf6af496
DE
6812 (unsigned) (begin_info_ptr - section->buffer),
6813 bfd_get_filename (abfd));
b4f54984 6814 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
6815 }
6816
b0c7bfa9
DE
6817 /* Skip dummy compilation units. */
6818 if (info_ptr >= begin_info_ptr + dwo_unit->length
6819 || peek_abbrev_code (abfd, info_ptr) == 0)
6820 return 0;
6821
6822 *result_info_ptr = info_ptr;
6823 return 1;
6824}
6825
a084a2a6
AT
6826/* Return the signature of the compile unit, if found. In DWARF 4 and before,
6827 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
6828 signature is part of the header. */
6829static gdb::optional<ULONGEST>
6830lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
6831{
6832 if (cu->header.version >= 5)
6833 return cu->header.signature;
6834 struct attribute *attr;
6835 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
6836 if (attr == nullptr)
6837 return gdb::optional<ULONGEST> ();
6838 return DW_UNSND (attr);
6839}
6840
c0ab21c2 6841/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6842 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 6843 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
6844
6845static struct dwo_unit *
4ab09049 6846lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 6847{
4ab09049 6848 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 6849 struct dwo_unit *dwo_unit;
c0ab21c2 6850 const char *comp_dir;
b0c7bfa9 6851
a2ce51a0
DE
6852 gdb_assert (cu != NULL);
6853
b0c7bfa9 6854 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 6855 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 6856 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 6857
4ab09049
SM
6858 if (per_cu->is_debug_types)
6859 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
6860 else
6861 {
a084a2a6 6862 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 6863
a084a2a6 6864 if (!signature.has_value ())
b0c7bfa9
DE
6865 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
6866 " [in module %s]"),
4ab09049
SM
6867 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
6868
6869 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
6870 }
6871
b0c7bfa9
DE
6872 return dwo_unit;
6873}
6874
c0ab21c2 6875/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 6876 See it for a description of the parameters.
fcd3b13d 6877 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 6878
c0ab21c2 6879void
9e021579
SM
6880cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
6881 dwarf2_per_objfile *per_objfile,
2e671100 6882 dwarf2_cu *existing_cu)
a2ce51a0 6883{
a2ce51a0 6884 struct signatured_type *sig_type;
a2ce51a0
DE
6885
6886 /* Verify we can do the following downcast, and that we have the
6887 data we need. */
6888 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
6889 sig_type = (struct signatured_type *) this_cu;
6890 gdb_assert (sig_type->dwo_unit != NULL);
6891
2e671100
SM
6892 dwarf2_cu *cu;
6893
6894 if (existing_cu != nullptr)
6aa5f3a6 6895 {
2e671100
SM
6896 cu = existing_cu;
6897 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 6898 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 6899 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
6900 }
6901 else
6902 {
7188ed02
SM
6903 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6904 in per_objfile yet. */
6905 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 6906 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 6907 cu = m_new_cu.get ();
6aa5f3a6
DE
6908 }
6909
6910 /* A future optimization, if needed, would be to use an existing
6911 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
6912 could share abbrev tables. */
a2ce51a0 6913
2e671100 6914 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
6915 NULL /* stub_comp_unit_die */,
6916 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 6917 this, &info_ptr,
3e225074 6918 &comp_unit_die,
c0ab21c2 6919 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
6920 {
6921 /* Dummy die. */
c0ab21c2 6922 dummy_p = true;
a2ce51a0 6923 }
a2ce51a0
DE
6924}
6925
fd820528 6926/* Initialize a CU (or TU) and read its DIEs.
3019eac3 6927 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 6928
f4dc4d17
DE
6929 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
6930 Otherwise the table specified in the comp unit header is read in and used.
6931 This is an optimization for when we already have the abbrev table.
6932
2e671100
SM
6933 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
6934 allocated. */
aaa75496 6935
ab432490
SM
6936cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
6937 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2 6938 struct abbrev_table *abbrev_table,
2e671100 6939 dwarf2_cu *existing_cu,
c0ab21c2
TT
6940 bool skip_partial)
6941 : die_reader_specs {},
6751ebae 6942 m_this_cu (this_cu)
c906108c 6943{
dee91e82 6944 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 6945 struct dwarf2_section_info *section = this_cu->section;
96b79293 6946 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 6947 const gdb_byte *begin_info_ptr;
dee91e82 6948 struct signatured_type *sig_type = NULL;
4bdcc0c1 6949 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
6950 /* Non-zero if CU currently points to a DWO file and we need to
6951 reread it. When this happens we need to reread the skeleton die
a2ce51a0 6952 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 6953 int rereading_dwo_cu = 0;
c906108c 6954
b4f54984 6955 if (dwarf_die_debug)
9d8780f0 6956 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 6957 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 6958 sect_offset_str (this_cu->sect_off));
09406207 6959
a2ce51a0
DE
6960 /* If we're reading a TU directly from a DWO file, including a virtual DWO
6961 file (instead of going through the stub), short-circuit all of this. */
6962 if (this_cu->reading_dwo_directly)
6963 {
6964 /* Narrow down the scope of possibilities to have to understand. */
6965 gdb_assert (this_cu->is_debug_types);
6966 gdb_assert (abbrev_table == NULL);
2e671100 6967 init_tu_and_read_dwo_dies (this_cu, dwarf2_per_objfile, existing_cu);
a2ce51a0
DE
6968 return;
6969 }
6970
dee91e82 6971 /* This is cheap if the section is already read in. */
96b79293 6972 section->read (objfile);
dee91e82 6973
9c541725 6974 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
6975
6976 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 6977
2e671100
SM
6978 dwarf2_cu *cu;
6979
6980 if (existing_cu != nullptr)
dee91e82 6981 {
2e671100 6982 cu = existing_cu;
42e7ad6c
DE
6983 /* If this CU is from a DWO file we need to start over, we need to
6984 refetch the attributes from the skeleton CU.
6985 This could be optimized by retrieving those attributes from when we
6986 were here the first time: the previous comp_unit_die was stored in
6987 comp_unit_obstack. But there's no data yet that we need this
6988 optimization. */
6989 if (cu->dwo_unit != NULL)
6990 rereading_dwo_cu = 1;
dee91e82
DE
6991 }
6992 else
6993 {
7188ed02
SM
6994 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
6995 in per_objfile yet. */
6996 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
9e021579 6997 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
c0ab21c2 6998 cu = m_new_cu.get ();
42e7ad6c 6999 }
dee91e82 7000
b0c7bfa9 7001 /* Get the header. */
9c541725 7002 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7003 {
7004 /* We already have the header, there's no need to read it in again. */
9c541725 7005 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7006 }
7007 else
7008 {
3019eac3 7009 if (this_cu->is_debug_types)
dee91e82 7010 {
ed2dc618
SM
7011 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7012 &cu->header, section,
4bdcc0c1 7013 abbrev_section, info_ptr,
43988095 7014 rcuh_kind::TYPE);
dee91e82 7015
42e7ad6c
DE
7016 /* Since per_cu is the first member of struct signatured_type,
7017 we can go from a pointer to one to a pointer to the other. */
7018 sig_type = (struct signatured_type *) this_cu;
43988095 7019 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7020 gdb_assert (sig_type->type_offset_in_tu
7021 == cu->header.type_cu_offset_in_tu);
7022 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7023
42e7ad6c
DE
7024 /* LENGTH has not been set yet for type units if we're
7025 using .gdb_index. */
4057dfde 7026 this_cu->length = cu->header.get_length ();
3019eac3
DE
7027
7028 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7029 sig_type->type_offset_in_section =
7030 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7031
7032 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7033 }
7034 else
7035 {
ed2dc618
SM
7036 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
7037 &cu->header, section,
4bdcc0c1 7038 abbrev_section,
43988095
JK
7039 info_ptr,
7040 rcuh_kind::COMPILE);
dee91e82 7041
9c541725 7042 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7043 if (this_cu->length == 0)
7044 this_cu->length = cu->header.get_length ();
7045 else
7046 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7047 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7048 }
7049 }
10b3939b 7050
6caca83c 7051 /* Skip dummy compilation units. */
dee91e82 7052 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7053 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7054 {
7055 dummy_p = true;
7056 return;
7057 }
6caca83c 7058
433df2d4
DE
7059 /* If we don't have them yet, read the abbrevs for this compilation unit.
7060 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7061 done. */
f4dc4d17 7062 if (abbrev_table != NULL)
685af9cd
TT
7063 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7064 else
f4dc4d17 7065 {
c0ab21c2 7066 m_abbrev_table_holder
86de1d91
TT
7067 = abbrev_table::read (objfile, abbrev_section,
7068 cu->header.abbrev_sect_off);
c0ab21c2 7069 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7070 }
af703f96 7071
dee91e82 7072 /* Read the top level CU/TU die. */
c0ab21c2 7073 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7074 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7075
58f0c718 7076 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7077 {
7078 dummy_p = true;
7079 return;
7080 }
58f0c718 7081
b0c7bfa9 7082 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7083 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7084 table from the DWO file and pass the ownership over to us. It will be
7085 referenced from READER, so we must make sure to free it after we're done
7086 with READER.
7087
b0c7bfa9
DE
7088 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7089 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7090 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7091 if (dwo_name != nullptr)
3019eac3 7092 {
3019eac3 7093 struct dwo_unit *dwo_unit;
b0c7bfa9 7094 struct die_info *dwo_comp_unit_die;
3019eac3 7095
3e225074 7096 if (comp_unit_die->has_children)
6a506a2d 7097 {
b98664d3 7098 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7099 " has children (offset %s) [in module %s]"),
7100 sect_offset_str (this_cu->sect_off),
7101 bfd_get_filename (abfd));
6a506a2d 7102 }
4ab09049 7103 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7104 if (dwo_unit != NULL)
3019eac3 7105 {
4ab09049 7106 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7107 comp_unit_die, NULL,
c0ab21c2 7108 this, &info_ptr,
3e225074 7109 &dwo_comp_unit_die,
c0ab21c2 7110 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7111 {
7112 /* Dummy die. */
c0ab21c2 7113 dummy_p = true;
6a506a2d
DE
7114 return;
7115 }
7116 comp_unit_die = dwo_comp_unit_die;
7117 }
7118 else
7119 {
7120 /* Yikes, we couldn't find the rest of the DIE, we only have
7121 the stub. A complaint has already been logged. There's
7122 not much more we can do except pass on the stub DIE to
7123 die_reader_func. We don't want to throw an error on bad
7124 debug info. */
3019eac3
DE
7125 }
7126 }
c0ab21c2 7127}
3019eac3 7128
6751ebae
TT
7129void
7130cutu_reader::keep ()
c0ab21c2 7131{
b0c7bfa9 7132 /* Done, clean up. */
6751ebae
TT
7133 gdb_assert (!dummy_p);
7134 if (m_new_cu != NULL)
348e048f 7135 {
7188ed02
SM
7136 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
7137 now. */
7138 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7139 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7140 }
dee91e82
DE
7141}
7142
18a8505e
AT
7143/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7144 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7145 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7146
7147 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7148 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7149
7150 We fill in THIS_CU->length.
7151
dee91e82 7152 THIS_CU->cu is always freed when done.
3019eac3 7153 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7154 to care whether it refers to the "main" CU or the DWO CU.
7155
7156 When parent_cu is passed, it is used to provide a default value for
7157 str_offsets_base and addr_base from the parent. */
dee91e82 7158
ab432490
SM
7159cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
7160 dwarf2_per_objfile *dwarf2_per_objfile,
c0ab21c2
TT
7161 struct dwarf2_cu *parent_cu,
7162 struct dwo_file *dwo_file)
7163 : die_reader_specs {},
7164 m_this_cu (this_cu)
dee91e82
DE
7165{
7166 struct objfile *objfile = dwarf2_per_objfile->objfile;
8a0459fd 7167 struct dwarf2_section_info *section = this_cu->section;
96b79293 7168 bfd *abfd = section->get_bfd_owner ();
33e80786 7169 struct dwarf2_section_info *abbrev_section;
d521ce57 7170 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7171
b4f54984 7172 if (dwarf_die_debug)
9d8780f0 7173 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7174 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7175 sect_offset_str (this_cu->sect_off));
09406207 7176
7188ed02 7177 gdb_assert (dwarf2_per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7178
33e80786
DE
7179 abbrev_section = (dwo_file != NULL
7180 ? &dwo_file->sections.abbrev
7181 : get_abbrev_section_for_cu (this_cu));
7182
dee91e82 7183 /* This is cheap if the section is already read in. */
96b79293 7184 section->read (objfile);
dee91e82 7185
9e021579 7186 m_new_cu.reset (new dwarf2_cu (this_cu, dwarf2_per_objfile));
dee91e82 7187
9c541725 7188 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
ed2dc618 7189 info_ptr = read_and_check_comp_unit_head (dwarf2_per_objfile,
c0ab21c2 7190 &m_new_cu->header, section,
4bdcc0c1 7191 abbrev_section, info_ptr,
43988095
JK
7192 (this_cu->is_debug_types
7193 ? rcuh_kind::TYPE
7194 : rcuh_kind::COMPILE));
dee91e82 7195
18a8505e
AT
7196 if (parent_cu != nullptr)
7197 {
c0ab21c2
TT
7198 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7199 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7200 }
4057dfde 7201 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7202
7203 /* Skip dummy compilation units. */
7204 if (info_ptr >= begin_info_ptr + this_cu->length
7205 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7206 {
7207 dummy_p = true;
7208 return;
7209 }
72bf9492 7210
c0ab21c2 7211 m_abbrev_table_holder
86de1d91
TT
7212 = abbrev_table::read (objfile, abbrev_section,
7213 m_new_cu->header.abbrev_sect_off);
dee91e82 7214
c0ab21c2
TT
7215 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7216 m_abbrev_table_holder.get ());
3e225074 7217 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7218}
7219
0018ea6f
DE
7220\f
7221/* Type Unit Groups.
dee91e82 7222
0018ea6f
DE
7223 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7224 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7225 so that all types coming from the same compilation (.o file) are grouped
7226 together. A future step could be to put the types in the same symtab as
7227 the CU the types ultimately came from. */
ff013f42 7228
f4dc4d17
DE
7229static hashval_t
7230hash_type_unit_group (const void *item)
7231{
9a3c8263
SM
7232 const struct type_unit_group *tu_group
7233 = (const struct type_unit_group *) item;
f4dc4d17 7234
094b34ac 7235 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7236}
348e048f
DE
7237
7238static int
f4dc4d17 7239eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7240{
9a3c8263
SM
7241 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7242 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7243
094b34ac 7244 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7245}
348e048f 7246
f4dc4d17
DE
7247/* Allocate a hash table for type unit groups. */
7248
eaa5fa8b 7249static htab_up
298e9637 7250allocate_type_unit_groups_table ()
f4dc4d17 7251{
eaa5fa8b
TT
7252 return htab_up (htab_create_alloc (3,
7253 hash_type_unit_group,
7254 eq_type_unit_group,
7255 NULL, xcalloc, xfree));
f4dc4d17 7256}
dee91e82 7257
f4dc4d17
DE
7258/* Type units that don't have DW_AT_stmt_list are grouped into their own
7259 partial symtabs. We combine several TUs per psymtab to not let the size
7260 of any one psymtab grow too big. */
7261#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7262#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7263
094b34ac 7264/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7265 Create the type_unit_group object used to hold one or more TUs. */
7266
7267static struct type_unit_group *
094b34ac 7268create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7269{
5e22e966 7270 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
1859c670 7271 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
094b34ac 7272 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7273 struct type_unit_group *tu_group;
f4dc4d17 7274
5989a64e 7275 tu_group = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack,
f4dc4d17 7276 struct type_unit_group);
094b34ac 7277 per_cu = &tu_group->per_cu;
1859c670 7278 per_cu->per_bfd = per_bfd;
f4dc4d17 7279
1859c670 7280 if (per_bfd->using_index)
094b34ac 7281 {
1859c670 7282 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7283 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7284 }
7285 else
7286 {
9c541725 7287 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7288 dwarf2_psymtab *pst;
528e1572 7289 std::string name;
094b34ac
DE
7290
7291 /* Give the symtab a useful name for debug purposes. */
7292 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7293 name = string_printf ("<type_units_%d>",
7294 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7295 else
528e1572 7296 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7297
7aa104c4 7298 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, name.c_str ());
6d94535f 7299 pst->anonymous = true;
094b34ac 7300 }
f4dc4d17 7301
094b34ac 7302 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7303 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7304
7305 return tu_group;
7306}
7307
094b34ac
DE
7308/* Look up the type_unit_group for type unit CU, and create it if necessary.
7309 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7310
7311static struct type_unit_group *
ff39bb5e 7312get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7313{
5e22e966 7314 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
5989a64e 7315 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7316 struct type_unit_group *tu_group;
7317 void **slot;
7318 unsigned int line_offset;
7319 struct type_unit_group type_unit_group_for_lookup;
7320
5989a64e
SM
7321 if (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL)
7322 dwarf2_per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7323
7324 /* Do we need to create a new group, or can we use an existing one? */
7325
7326 if (stmt_list)
7327 {
7328 line_offset = DW_UNSND (stmt_list);
7329 ++tu_stats->nr_symtab_sharers;
7330 }
7331 else
7332 {
7333 /* Ugh, no stmt_list. Rare, but we have to handle it.
7334 We can do various things here like create one group per TU or
7335 spread them over multiple groups to split up the expansion work.
7336 To avoid worst case scenarios (too many groups or too large groups)
7337 we, umm, group them in bunches. */
7338 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7339 | (tu_stats->nr_stmt_less_type_units
7340 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7341 ++tu_stats->nr_stmt_less_type_units;
7342 }
7343
094b34ac 7344 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7345 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
5989a64e 7346 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7347 &type_unit_group_for_lookup, INSERT);
7348 if (*slot != NULL)
7349 {
9a3c8263 7350 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7351 gdb_assert (tu_group != NULL);
7352 }
7353 else
7354 {
9c541725 7355 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7356 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7357 *slot = tu_group;
7358 ++tu_stats->nr_symtabs;
7359 }
7360
7361 return tu_group;
7362}
0018ea6f
DE
7363\f
7364/* Partial symbol tables. */
7365
7366/* Create a psymtab named NAME and assign it to PER_CU.
7367
7368 The caller must fill in the following details:
7369 dirname, textlow, texthigh. */
7370
891813be 7371static dwarf2_psymtab *
7aa104c4
SM
7372create_partial_symtab (dwarf2_per_cu_data *per_cu,
7373 dwarf2_per_objfile *per_objfile,
7374 const char *name)
0018ea6f 7375{
7aa104c4 7376 struct objfile *objfile = per_objfile->objfile;
891813be 7377 dwarf2_psymtab *pst;
0018ea6f 7378
9f4e76a4 7379 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7380
6d94535f 7381 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7382
7383 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7384 per_cu->v.psymtab = pst;
7385
7386 return pst;
7387}
7388
c0ab21c2 7389/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7390
7391static void
7392process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7393 const gdb_byte *info_ptr,
0018ea6f 7394 struct die_info *comp_unit_die,
c0ab21c2 7395 enum language pretend_language)
0018ea6f
DE
7396{
7397 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7398 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7399 struct objfile *objfile = per_objfile->objfile;
08feed99 7400 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7401 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7402 CORE_ADDR baseaddr;
7403 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7404 dwarf2_psymtab *pst;
3a2b436a 7405 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7406 const char *filename;
0018ea6f 7407
0018ea6f
DE
7408 gdb_assert (! per_cu->is_debug_types);
7409
c0ab21c2 7410 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7411
0018ea6f 7412 /* Allocate a new partial symbol table structure. */
2e927613
TV
7413 gdb::unique_xmalloc_ptr<char> debug_filename;
7414 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7415 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7416 if (filename == NULL)
0018ea6f 7417 filename = "";
2e927613
TV
7418 else if (strcmp (filename, artificial) == 0)
7419 {
7420 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7421 sect_offset_str (per_cu->sect_off),
7422 (char *) NULL));
2e927613
TV
7423 filename = debug_filename.get ();
7424 }
0018ea6f 7425
7aa104c4 7426 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7427
7428 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7429 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7430
b3b3bada 7431 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7432
7433 dwarf2_find_base_address (comp_unit_die, cu);
7434
7435 /* Possibly set the default values of LOWPC and HIGHPC from
7436 `DW_AT_ranges'. */
3a2b436a
JK
7437 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7438 &best_highpc, cu, pst);
7439 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7440 {
7441 CORE_ADDR low
7442 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7443 - baseaddr);
7444 CORE_ADDR high
7445 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7446 - baseaddr - 1);
7447 /* Store the contiguous range if it is not empty; it can be
7448 empty for CUs with no code. */
d320c2b5
TT
7449 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7450 low, high, pst);
79748972 7451 }
0018ea6f
DE
7452
7453 /* Check if comp unit has_children.
7454 If so, read the rest of the partial symbols from this comp unit.
7455 If not, there's no more debug_info for this comp unit. */
3e225074 7456 if (comp_unit_die->has_children)
0018ea6f
DE
7457 {
7458 struct partial_die_info *first_die;
7459 CORE_ADDR lowpc, highpc;
7460
7461 lowpc = ((CORE_ADDR) -1);
7462 highpc = ((CORE_ADDR) 0);
7463
7464 first_die = load_partial_dies (reader, info_ptr, 1);
7465
7466 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7467 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7468
7469 /* If we didn't find a lowpc, set it to highpc to avoid
7470 complaints from `maint check'. */
7471 if (lowpc == ((CORE_ADDR) -1))
7472 lowpc = highpc;
7473
7474 /* If the compilation unit didn't have an explicit address range,
7475 then use the information extracted from its child dies. */
e385593e 7476 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7477 {
7478 best_lowpc = lowpc;
7479 best_highpc = highpc;
7480 }
7481 }
4ae976d1 7482 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7483 best_lowpc + baseaddr)
7484 - baseaddr);
4ae976d1 7485 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7486 best_highpc + baseaddr)
7487 - baseaddr);
0018ea6f 7488
8763cede 7489 end_psymtab_common (objfile, pst);
0018ea6f 7490
ae640021 7491 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7492 {
7493 int i;
ae640021 7494 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7495
7496 /* Fill in 'dependencies' here; we fill in 'users' in a
7497 post-pass. */
7498 pst->number_of_dependencies = len;
a9342b62
TT
7499 pst->dependencies
7500 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7501 for (i = 0; i < len; ++i)
7502 {
7503 pst->dependencies[i]
7504 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7505 }
0018ea6f 7506
ae640021 7507 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7508 }
7509
7510 /* Get the list of files included in the current compilation unit,
7511 and build a psymtab for each of them. */
7512 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7513
b4f54984 7514 if (dwarf_read_debug)
b926417a
TT
7515 fprintf_unfiltered (gdb_stdlog,
7516 "Psymtab for %s unit @%s: %s - %s"
7517 ", %d global, %d static syms\n",
7518 per_cu->is_debug_types ? "type" : "comp",
7519 sect_offset_str (per_cu->sect_off),
7520 paddress (gdbarch, pst->text_low (objfile)),
7521 paddress (gdbarch, pst->text_high (objfile)),
7522 pst->n_global_syms, pst->n_static_syms);
0018ea6f
DE
7523}
7524
7525/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7526 Process compilation unit THIS_CU for a psymtab. */
7527
7528static void
ab432490
SM
7529process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7530 dwarf2_per_objfile *per_objfile,
135f5437 7531 bool want_partial_unit,
b93601f3 7532 enum language pretend_language)
0018ea6f
DE
7533{
7534 /* If this compilation unit was already read in, free the
7535 cached copy in order to read it in again. This is
7536 necessary because we skipped some symbols when we first
7537 read in the compilation unit (see load_partial_dies).
7538 This problem could be avoided, but the benefit is unclear. */
7188ed02 7539 per_objfile->remove_cu (this_cu);
0018ea6f 7540
2e671100 7541 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7542
58990295
TV
7543 switch (reader.comp_unit_die->tag)
7544 {
7545 case DW_TAG_compile_unit:
7546 this_cu->unit_type = DW_UT_compile;
7547 break;
7548 case DW_TAG_partial_unit:
7549 this_cu->unit_type = DW_UT_partial;
7550 break;
7551 default:
7552 abort ();
7553 }
7554
c0ab21c2 7555 if (reader.dummy_p)
f1902523 7556 {
c0ab21c2 7557 /* Nothing. */
f1902523 7558 }
c0ab21c2 7559 else if (this_cu->is_debug_types)
3e225074
TT
7560 build_type_psymtabs_reader (&reader, reader.info_ptr,
7561 reader.comp_unit_die);
135f5437
TT
7562 else if (want_partial_unit
7563 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7564 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7565 reader.comp_unit_die,
c0ab21c2 7566 pretend_language);
0018ea6f 7567
7188ed02 7568 this_cu->lang = reader.cu->language;
58990295 7569
0018ea6f 7570 /* Age out any secondary CUs. */
7188ed02 7571 per_objfile->age_comp_units ();
0018ea6f 7572}
f4dc4d17
DE
7573
7574/* Reader function for build_type_psymtabs. */
7575
7576static void
7577build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7578 const gdb_byte *info_ptr,
3e225074 7579 struct die_info *type_unit_die)
f4dc4d17 7580{
5e22e966 7581 struct dwarf2_per_objfile *dwarf2_per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7582 struct objfile *objfile = dwarf2_per_objfile->objfile;
7583 struct dwarf2_cu *cu = reader->cu;
7584 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7585 struct signatured_type *sig_type;
f4dc4d17
DE
7586 struct type_unit_group *tu_group;
7587 struct attribute *attr;
7588 struct partial_die_info *first_die;
7589 CORE_ADDR lowpc, highpc;
891813be 7590 dwarf2_psymtab *pst;
f4dc4d17 7591
0186c6a7
DE
7592 gdb_assert (per_cu->is_debug_types);
7593 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7594
3e225074 7595 if (! type_unit_die->has_children)
f4dc4d17
DE
7596 return;
7597
052c8bb8 7598 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7599 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7600
df07e2c7 7601 if (tu_group->tus == nullptr)
a8b3b8e9 7602 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7603 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7604
7605 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
7aa104c4 7606 pst = create_partial_symtab (per_cu, dwarf2_per_objfile, "");
6d94535f 7607 pst->anonymous = true;
f4dc4d17
DE
7608
7609 first_die = load_partial_dies (reader, info_ptr, 1);
7610
7611 lowpc = (CORE_ADDR) -1;
7612 highpc = (CORE_ADDR) 0;
7613 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7614
8763cede 7615 end_psymtab_common (objfile, pst);
f4dc4d17
DE
7616}
7617
73051182
DE
7618/* Struct used to sort TUs by their abbreviation table offset. */
7619
7620struct tu_abbrev_offset
7621{
b2bdb8cf
SM
7622 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7623 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7624 {}
7625
7626 signatured_type *sig_type;
73051182
DE
7627 sect_offset abbrev_offset;
7628};
7629
484cf504 7630/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7631
484cf504
TT
7632static bool
7633sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7634 const struct tu_abbrev_offset &b)
73051182 7635{
484cf504 7636 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7637}
7638
7639/* Efficiently read all the type units.
7640 This does the bulk of the work for build_type_psymtabs.
7641
7642 The efficiency is because we sort TUs by the abbrev table they use and
7643 only read each abbrev table once. In one program there are 200K TUs
7644 sharing 8K abbrev tables.
7645
7646 The main purpose of this function is to support building the
5989a64e 7647 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7648 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7649 can collapse the search space by grouping them by stmt_list.
7650 The savings can be significant, in the same program from above the 200K TUs
7651 share 8K stmt_list tables.
7652
7653 FUNC is expected to call get_type_unit_group, which will create the
7654 struct type_unit_group if necessary and add it to
5989a64e 7655 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7656
7657static void
ed2dc618 7658build_type_psymtabs_1 (struct dwarf2_per_objfile *dwarf2_per_objfile)
73051182 7659{
5989a64e 7660 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
685af9cd 7661 abbrev_table_up abbrev_table;
73051182 7662 sect_offset abbrev_offset;
73051182
DE
7663
7664 /* It's up to the caller to not call us multiple times. */
5989a64e 7665 gdb_assert (dwarf2_per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7666
5989a64e 7667 if (dwarf2_per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7668 return;
7669
7670 /* TUs typically share abbrev tables, and there can be way more TUs than
7671 abbrev tables. Sort by abbrev table to reduce the number of times we
7672 read each abbrev table in.
7673 Alternatives are to punt or to maintain a cache of abbrev tables.
7674 This is simpler and efficient enough for now.
7675
7676 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7677 symtab to use). Typically TUs with the same abbrev offset have the same
7678 stmt_list value too so in practice this should work well.
7679
7680 The basic algorithm here is:
7681
7682 sort TUs by abbrev table
7683 for each TU with same abbrev table:
7684 read abbrev table if first user
7685 read TU top level DIE
7686 [IWBN if DWO skeletons had DW_AT_stmt_list]
7687 call FUNC */
7688
b4f54984 7689 if (dwarf_read_debug)
73051182
DE
7690 fprintf_unfiltered (gdb_stdlog, "Building type unit groups ...\n");
7691
7692 /* Sort in a separate table to maintain the order of all_type_units
7693 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7694 std::vector<tu_abbrev_offset> sorted_by_abbrev;
5989a64e 7695 sorted_by_abbrev.reserve (dwarf2_per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7696
5989a64e 7697 for (signatured_type *sig_type : dwarf2_per_objfile->per_bfd->all_type_units)
b2bdb8cf
SM
7698 sorted_by_abbrev.emplace_back
7699 (sig_type, read_abbrev_offset (dwarf2_per_objfile,
7700 sig_type->per_cu.section,
7701 sig_type->per_cu.sect_off));
73051182 7702
484cf504
TT
7703 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7704 sort_tu_by_abbrev_offset);
73051182 7705
9c541725 7706 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7707
b2bdb8cf 7708 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7709 {
73051182
DE
7710 /* Switch to the next abbrev table if necessary. */
7711 if (abbrev_table == NULL
b2bdb8cf 7712 || tu.abbrev_offset != abbrev_offset)
73051182 7713 {
b2bdb8cf 7714 abbrev_offset = tu.abbrev_offset;
73051182 7715 abbrev_table =
86de1d91 7716 abbrev_table::read (dwarf2_per_objfile->objfile,
5989a64e 7717 &dwarf2_per_objfile->per_bfd->abbrev,
86de1d91 7718 abbrev_offset);
73051182
DE
7719 ++tu_stats->nr_uniq_abbrev_tables;
7720 }
7721
ab432490 7722 cutu_reader reader (&tu.sig_type->per_cu, dwarf2_per_objfile,
2e671100 7723 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7724 if (!reader.dummy_p)
7725 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7726 reader.comp_unit_die);
73051182 7727 }
6aa5f3a6 7728}
73051182 7729
6aa5f3a6
DE
7730/* Print collected type unit statistics. */
7731
7732static void
ed2dc618 7733print_tu_stats (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6 7734{
5989a64e 7735 struct tu_stats *tu_stats = &dwarf2_per_objfile->per_bfd->tu_stats;
6aa5f3a6
DE
7736
7737 fprintf_unfiltered (gdb_stdlog, "Type unit statistics:\n");
b2bdb8cf 7738 fprintf_unfiltered (gdb_stdlog, " %zu TUs\n",
5989a64e 7739 dwarf2_per_objfile->per_bfd->all_type_units.size ());
6aa5f3a6
DE
7740 fprintf_unfiltered (gdb_stdlog, " %d uniq abbrev tables\n",
7741 tu_stats->nr_uniq_abbrev_tables);
7742 fprintf_unfiltered (gdb_stdlog, " %d symtabs from stmt_list entries\n",
7743 tu_stats->nr_symtabs);
7744 fprintf_unfiltered (gdb_stdlog, " %d symtab sharers\n",
7745 tu_stats->nr_symtab_sharers);
7746 fprintf_unfiltered (gdb_stdlog, " %d type units without a stmt_list\n",
7747 tu_stats->nr_stmt_less_type_units);
7748 fprintf_unfiltered (gdb_stdlog, " %d all_type_units reallocs\n",
7749 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7750}
7751
f4dc4d17
DE
7752/* Traversal function for build_type_psymtabs. */
7753
7754static int
7755build_type_psymtab_dependencies (void **slot, void *info)
7756{
ed2dc618
SM
7757 struct dwarf2_per_objfile *dwarf2_per_objfile
7758 = (struct dwarf2_per_objfile *) info;
f4dc4d17
DE
7759 struct objfile *objfile = dwarf2_per_objfile->objfile;
7760 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 7761 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 7762 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 7763 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
7764 int i;
7765
7766 gdb_assert (len > 0);
197400e8 7767 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
7768
7769 pst->number_of_dependencies = len;
a9342b62 7770 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 7771 for (i = 0; i < len; ++i)
f4dc4d17 7772 {
df07e2c7 7773 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
7774 gdb_assert (iter->per_cu.is_debug_types);
7775 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 7776 iter->type_unit_group = tu_group;
f4dc4d17
DE
7777 }
7778
df07e2c7
AB
7779 delete tu_group->tus;
7780 tu_group->tus = nullptr;
348e048f
DE
7781
7782 return 1;
7783}
7784
7785/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7786 Build partial symbol tables for the .debug_types comp-units. */
7787
7788static void
ed2dc618 7789build_type_psymtabs (struct dwarf2_per_objfile *dwarf2_per_objfile)
348e048f 7790{
ed2dc618 7791 if (! create_all_type_units (dwarf2_per_objfile))
348e048f
DE
7792 return;
7793
ed2dc618 7794 build_type_psymtabs_1 (dwarf2_per_objfile);
6aa5f3a6 7795}
f4dc4d17 7796
6aa5f3a6
DE
7797/* Traversal function for process_skeletonless_type_unit.
7798 Read a TU in a DWO file and build partial symbols for it. */
7799
7800static int
7801process_skeletonless_type_unit (void **slot, void *info)
7802{
7803 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
ed2dc618
SM
7804 struct dwarf2_per_objfile *dwarf2_per_objfile
7805 = (struct dwarf2_per_objfile *) info;
6aa5f3a6
DE
7806 struct signatured_type find_entry, *entry;
7807
7808 /* If this TU doesn't exist in the global table, add it and read it in. */
7809
5989a64e
SM
7810 if (dwarf2_per_objfile->per_bfd->signatured_types == NULL)
7811 dwarf2_per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
7812
7813 find_entry.signature = dwo_unit->signature;
5989a64e 7814 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->signatured_types.get (),
b0b6a987 7815 &find_entry, INSERT);
6aa5f3a6
DE
7816 /* If we've already seen this type there's nothing to do. What's happening
7817 is we're doing our own version of comdat-folding here. */
7818 if (*slot != NULL)
7819 return 1;
7820
7821 /* This does the job that create_all_type_units would have done for
7822 this TU. */
ed2dc618
SM
7823 entry = add_type_unit (dwarf2_per_objfile, dwo_unit->signature, slot);
7824 fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile, entry, dwo_unit);
6aa5f3a6
DE
7825 *slot = entry;
7826
7827 /* This does the job that build_type_psymtabs_1 would have done. */
2e671100
SM
7828 cutu_reader reader (&entry->per_cu, dwarf2_per_objfile, nullptr, nullptr,
7829 false);
c0ab21c2
TT
7830 if (!reader.dummy_p)
7831 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7832 reader.comp_unit_die);
6aa5f3a6
DE
7833
7834 return 1;
7835}
7836
7837/* Traversal function for process_skeletonless_type_units. */
7838
7839static int
7840process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
7841{
7842 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
7843
7844 if (dwo_file->tus != NULL)
b0b6a987
TT
7845 htab_traverse_noresize (dwo_file->tus.get (),
7846 process_skeletonless_type_unit, info);
6aa5f3a6
DE
7847
7848 return 1;
7849}
7850
7851/* Scan all TUs of DWO files, verifying we've processed them.
7852 This is needed in case a TU was emitted without its skeleton.
7853 Note: This can't be done until we know what all the DWO files are. */
7854
7855static void
ed2dc618 7856process_skeletonless_type_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
6aa5f3a6
DE
7857{
7858 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
ed2dc618 7859 if (get_dwp_file (dwarf2_per_objfile) == NULL
5989a64e 7860 && dwarf2_per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 7861 {
5989a64e 7862 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 7863 process_dwo_file_for_skeletonless_type_units,
ed2dc618 7864 dwarf2_per_objfile);
6aa5f3a6 7865 }
348e048f
DE
7866}
7867
ed2dc618 7868/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
7869
7870static void
ed2dc618 7871set_partial_user (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 7872{
5989a64e 7873 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
95554aad 7874 {
891813be 7875 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 7876
36586728
TT
7877 if (pst == NULL)
7878 continue;
7879
b76e467d 7880 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
7881 {
7882 /* Set the 'user' field only if it is not already set. */
7883 if (pst->dependencies[j]->user == NULL)
7884 pst->dependencies[j]->user = pst;
7885 }
7886 }
7887}
7888
93311388
DE
7889/* Build the partial symbol table by doing a quick pass through the
7890 .debug_info and .debug_abbrev sections. */
72bf9492 7891
93311388 7892static void
ed2dc618 7893dwarf2_build_psymtabs_hard (struct dwarf2_per_objfile *dwarf2_per_objfile)
93311388 7894{
ed2dc618 7895 struct objfile *objfile = dwarf2_per_objfile->objfile;
93311388 7896
b4f54984 7897 if (dwarf_read_debug)
45cfd468
DE
7898 {
7899 fprintf_unfiltered (gdb_stdlog, "Building psymtabs of objfile %s ...\n",
4262abfb 7900 objfile_name (objfile));
45cfd468
DE
7901 }
7902
76935768 7903 scoped_restore restore_reading_psyms
5989a64e 7904 = make_scoped_restore (&dwarf2_per_objfile->per_bfd->reading_partial_symbols,
76935768 7905 true);
98bfdba5 7906
5989a64e 7907 dwarf2_per_objfile->per_bfd->info.read (objfile);
91c24f0a 7908
93311388
DE
7909 /* Any cached compilation units will be linked by the per-objfile
7910 read_in_chain. Make sure to free them when we're done. */
11ed8cad 7911 free_cached_comp_units freer (dwarf2_per_objfile);
72bf9492 7912
ed2dc618 7913 build_type_psymtabs (dwarf2_per_objfile);
348e048f 7914
ed2dc618 7915 create_all_comp_units (dwarf2_per_objfile);
c906108c 7916
60606b2c
TT
7917 /* Create a temporary address map on a temporary obstack. We later
7918 copy this to the final obstack. */
8268c778 7919 auto_obstack temp_obstack;
791afaa2
TT
7920
7921 scoped_restore save_psymtabs_addrmap
d320c2b5 7922 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 7923 addrmap_create_mutable (&temp_obstack));
72bf9492 7924
5989a64e 7925 for (dwarf2_per_cu_data *per_cu : dwarf2_per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
7926 {
7927 if (per_cu->v.psymtab != NULL)
7928 /* In case a forward DW_TAG_imported_unit has read the CU already. */
7929 continue;
ab432490
SM
7930 process_psymtab_comp_unit (per_cu, dwarf2_per_objfile, false,
7931 language_minimal);
3d5afab3 7932 }
ff013f42 7933
6aa5f3a6 7934 /* This has to wait until we read the CUs, we need the list of DWOs. */
ed2dc618 7935 process_skeletonless_type_units (dwarf2_per_objfile);
6aa5f3a6
DE
7936
7937 /* Now that all TUs have been processed we can fill in the dependencies. */
5989a64e 7938 if (dwarf2_per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 7939 {
5989a64e 7940 htab_traverse_noresize (dwarf2_per_objfile->per_bfd->type_unit_groups.get (),
ed2dc618 7941 build_type_psymtab_dependencies, dwarf2_per_objfile);
6aa5f3a6
DE
7942 }
7943
b4f54984 7944 if (dwarf_read_debug)
ed2dc618 7945 print_tu_stats (dwarf2_per_objfile);
6aa5f3a6 7946
ed2dc618 7947 set_partial_user (dwarf2_per_objfile);
95554aad 7948
d320c2b5
TT
7949 objfile->partial_symtabs->psymtabs_addrmap
7950 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 7951 objfile->partial_symtabs->obstack ());
791afaa2
TT
7952 /* At this point we want to keep the address map. */
7953 save_psymtabs_addrmap.release ();
ff013f42 7954
b4f54984 7955 if (dwarf_read_debug)
45cfd468 7956 fprintf_unfiltered (gdb_stdlog, "Done building psymtabs of %s\n",
4262abfb 7957 objfile_name (objfile));
ae038cb0
DJ
7958}
7959
dee91e82
DE
7960/* Load the partial DIEs for a secondary CU into memory.
7961 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 7962
dee91e82 7963static void
ab432490 7964load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
7965 dwarf2_per_objfile *per_objfile,
7966 dwarf2_cu *existing_cu)
dee91e82 7967{
2e671100 7968 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
7969
7970 if (!reader.dummy_p)
7971 {
7972 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
7973 language_minimal);
7974
7975 /* Check if comp unit has_children.
7976 If so, read the rest of the partial symbols from this comp unit.
7977 If not, there's no more debug_info for this comp unit. */
3e225074 7978 if (reader.comp_unit_die->has_children)
c0ab21c2 7979 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
7980
7981 reader.keep ();
c0ab21c2 7982 }
ae038cb0
DJ
7983}
7984
ae038cb0 7985static void
ed2dc618 7986read_comp_units_from_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
36586728 7987 struct dwarf2_section_info *section,
f1902523 7988 struct dwarf2_section_info *abbrev_section,
b76e467d 7989 unsigned int is_dwz)
ae038cb0 7990{
d521ce57 7991 const gdb_byte *info_ptr;
ed2dc618 7992 struct objfile *objfile = dwarf2_per_objfile->objfile;
be391dca 7993
b4f54984 7994 if (dwarf_read_debug)
bf6af496 7995 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s\n",
96b79293
TT
7996 section->get_name (),
7997 section->get_file_name ());
bf6af496 7998
96b79293 7999 section->read (objfile);
ae038cb0 8000
36586728 8001 info_ptr = section->buffer;
6e70227d 8002
36586728 8003 while (info_ptr < section->buffer + section->size)
ae038cb0 8004 {
ae038cb0 8005 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8006
9c541725 8007 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8008
f1902523 8009 comp_unit_head cu_header;
ed2dc618
SM
8010 read_and_check_comp_unit_head (dwarf2_per_objfile, &cu_header, section,
8011 abbrev_section, info_ptr,
8012 rcuh_kind::COMPILE);
ae038cb0
DJ
8013
8014 /* Save the compilation unit for later lookup. */
f1902523 8015 if (cu_header.unit_type != DW_UT_type)
5989a64e 8016 this_cu = dwarf2_per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8017 else
8018 {
5989a64e 8019 auto sig_type = dwarf2_per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8020 sig_type->signature = cu_header.signature;
8021 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8022 this_cu = &sig_type->per_cu;
8023 }
8024 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8025 this_cu->sect_off = sect_off;
f1902523 8026 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8027 this_cu->is_dwz = is_dwz;
8a0459fd 8028 this_cu->section = section;
ae038cb0 8029
5989a64e 8030 dwarf2_per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8031
8032 info_ptr = info_ptr + this_cu->length;
8033 }
36586728
TT
8034}
8035
8036/* Create a list of all compilation units in OBJFILE.
8037 This is only done for -readnow and building partial symtabs. */
8038
8039static void
ed2dc618 8040create_all_comp_units (struct dwarf2_per_objfile *dwarf2_per_objfile)
36586728 8041{
5989a64e
SM
8042 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units.empty ());
8043 read_comp_units_from_section (dwarf2_per_objfile, &dwarf2_per_objfile->per_bfd->info,
8044 &dwarf2_per_objfile->per_bfd->abbrev, 0);
36586728 8045
c3699833 8046 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
4db1a1dc 8047 if (dwz != NULL)
ed2dc618 8048 read_comp_units_from_section (dwarf2_per_objfile, &dwz->info, &dwz->abbrev,
b76e467d 8049 1);
c906108c
SS
8050}
8051
5734ee8b 8052/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8053 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8054 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8055 DW_AT_ranges). See the comments of add_partial_subprogram on how
8056 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8057
72bf9492
DJ
8058static void
8059scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8060 CORE_ADDR *highpc, int set_addrmap,
8061 struct dwarf2_cu *cu)
c906108c 8062{
72bf9492 8063 struct partial_die_info *pdi;
c906108c 8064
91c24f0a
DC
8065 /* Now, march along the PDI's, descending into ones which have
8066 interesting children but skipping the children of the other ones,
8067 until we reach the end of the compilation unit. */
c906108c 8068
72bf9492 8069 pdi = first_die;
91c24f0a 8070
72bf9492
DJ
8071 while (pdi != NULL)
8072 {
52356b79 8073 pdi->fixup (cu);
c906108c 8074
f55ee35c 8075 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8076 children, so we need to look at them. Ditto for anonymous
8077 enums. */
933c6fe4 8078
72bf9492 8079 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8080 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8081 || pdi->tag == DW_TAG_imported_unit
8082 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8083 {
72bf9492 8084 switch (pdi->tag)
c906108c
SS
8085 {
8086 case DW_TAG_subprogram:
b1dc1806 8087 case DW_TAG_inlined_subroutine:
cdc07690 8088 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
c906108c 8089 break;
72929c62 8090 case DW_TAG_constant:
c906108c
SS
8091 case DW_TAG_variable:
8092 case DW_TAG_typedef:
91c24f0a 8093 case DW_TAG_union_type:
317d2668
TV
8094 if (!pdi->is_declaration
8095 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8096 {
72bf9492 8097 add_partial_symbol (pdi, cu);
63d06c5c
DC
8098 }
8099 break;
c906108c 8100 case DW_TAG_class_type:
680b30c7 8101 case DW_TAG_interface_type:
c906108c 8102 case DW_TAG_structure_type:
72bf9492 8103 if (!pdi->is_declaration)
c906108c 8104 {
72bf9492 8105 add_partial_symbol (pdi, cu);
c906108c 8106 }
b7fee5a3
KS
8107 if ((cu->language == language_rust
8108 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8109 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8110 set_addrmap, cu);
c906108c 8111 break;
91c24f0a 8112 case DW_TAG_enumeration_type:
72bf9492
DJ
8113 if (!pdi->is_declaration)
8114 add_partial_enumeration (pdi, cu);
c906108c
SS
8115 break;
8116 case DW_TAG_base_type:
a02abb62 8117 case DW_TAG_subrange_type:
c906108c 8118 /* File scope base type definitions are added to the partial
c5aa993b 8119 symbol table. */
72bf9492 8120 add_partial_symbol (pdi, cu);
c906108c 8121 break;
d9fa45fe 8122 case DW_TAG_namespace:
cdc07690 8123 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8124 break;
5d7cb8df 8125 case DW_TAG_module:
59c35742
AB
8126 if (!pdi->is_declaration)
8127 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8128 break;
95554aad
TT
8129 case DW_TAG_imported_unit:
8130 {
8131 struct dwarf2_per_cu_data *per_cu;
8132
f4dc4d17
DE
8133 /* For now we don't handle imported units in type units. */
8134 if (cu->per_cu->is_debug_types)
8135 {
8136 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8137 " supported in type units [in module %s]"),
5e22e966 8138 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8139 }
8140
e3b94546 8141 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8142 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8143
8144 /* Go read the partial unit, if needed. */
8145 if (per_cu->v.psymtab == NULL)
ab432490
SM
8146 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8147 cu->language);
95554aad 8148
ae640021 8149 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8150 }
8151 break;
74921315
KS
8152 case DW_TAG_imported_declaration:
8153 add_partial_symbol (pdi, cu);
8154 break;
c906108c
SS
8155 default:
8156 break;
8157 }
8158 }
8159
72bf9492
DJ
8160 /* If the die has a sibling, skip to the sibling. */
8161
8162 pdi = pdi->die_sibling;
8163 }
8164}
8165
8166/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8167
72bf9492 8168 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8169 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8170 Enumerators are an exception; they use the scope of their parent
8171 enumeration type, i.e. the name of the enumeration type is not
8172 prepended to the enumerator.
91c24f0a 8173
72bf9492
DJ
8174 There are two complexities. One is DW_AT_specification; in this
8175 case "parent" means the parent of the target of the specification,
8176 instead of the direct parent of the DIE. The other is compilers
8177 which do not emit DW_TAG_namespace; in this case we try to guess
8178 the fully qualified name of structure types from their members'
8179 linkage names. This must be done using the DIE's children rather
8180 than the children of any DW_AT_specification target. We only need
8181 to do this for structures at the top level, i.e. if the target of
8182 any DW_AT_specification (if any; otherwise the DIE itself) does not
8183 have a parent. */
8184
8185/* Compute the scope prefix associated with PDI's parent, in
8186 compilation unit CU. The result will be allocated on CU's
8187 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8188 field. NULL is returned if no prefix is necessary. */
15d034d0 8189static const char *
72bf9492
DJ
8190partial_die_parent_scope (struct partial_die_info *pdi,
8191 struct dwarf2_cu *cu)
8192{
15d034d0 8193 const char *grandparent_scope;
72bf9492 8194 struct partial_die_info *parent, *real_pdi;
91c24f0a 8195
72bf9492
DJ
8196 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8197 then this means the parent of the specification DIE. */
8198
8199 real_pdi = pdi;
72bf9492 8200 while (real_pdi->has_specification)
fb816e8b 8201 {
122cf0f2
AB
8202 auto res = find_partial_die (real_pdi->spec_offset,
8203 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8204 real_pdi = res.pdi;
8205 cu = res.cu;
8206 }
72bf9492
DJ
8207
8208 parent = real_pdi->die_parent;
8209 if (parent == NULL)
8210 return NULL;
8211
8212 if (parent->scope_set)
8213 return parent->scope;
8214
52356b79 8215 parent->fixup (cu);
72bf9492 8216
10b3939b 8217 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8218
acebe513
UW
8219 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8220 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8221 Work around this problem here. */
8222 if (cu->language == language_cplus
6e70227d 8223 && parent->tag == DW_TAG_namespace
acebe513
UW
8224 && strcmp (parent->name, "::") == 0
8225 && grandparent_scope == NULL)
8226 {
8227 parent->scope = NULL;
8228 parent->scope_set = 1;
8229 return NULL;
8230 }
8231
0a4b0913 8232 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8233 if (pdi->tag == DW_TAG_enumerator)
8234 /* Enumerators should not get the name of the enumeration as a prefix. */
8235 parent->scope = grandparent_scope;
8236 else if (parent->tag == DW_TAG_namespace
f55ee35c 8237 || parent->tag == DW_TAG_module
72bf9492
DJ
8238 || parent->tag == DW_TAG_structure_type
8239 || parent->tag == DW_TAG_class_type
680b30c7 8240 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8241 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8242 || parent->tag == DW_TAG_enumeration_type
8243 || (cu->language == language_fortran
8244 && parent->tag == DW_TAG_subprogram
8245 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8246 {
8247 if (grandparent_scope == NULL)
8248 parent->scope = parent->name;
8249 else
3e43a32a
MS
8250 parent->scope = typename_concat (&cu->comp_unit_obstack,
8251 grandparent_scope,
f55ee35c 8252 parent->name, 0, cu);
72bf9492 8253 }
72bf9492
DJ
8254 else
8255 {
8256 /* FIXME drow/2004-04-01: What should we be doing with
8257 function-local names? For partial symbols, we should probably be
8258 ignoring them. */
fa9c3fa0
TT
8259 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8260 dwarf_tag_name (parent->tag),
8261 sect_offset_str (pdi->sect_off));
72bf9492 8262 parent->scope = grandparent_scope;
c906108c
SS
8263 }
8264
72bf9492
DJ
8265 parent->scope_set = 1;
8266 return parent->scope;
8267}
8268
8269/* Return the fully scoped name associated with PDI, from compilation unit
8270 CU. The result will be allocated with malloc. */
4568ecf9 8271
43816ebc 8272static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8273partial_die_full_name (struct partial_die_info *pdi,
8274 struct dwarf2_cu *cu)
8275{
15d034d0 8276 const char *parent_scope;
72bf9492 8277
98bfdba5
PA
8278 /* If this is a template instantiation, we can not work out the
8279 template arguments from partial DIEs. So, unfortunately, we have
8280 to go through the full DIEs. At least any work we do building
8281 types here will be reused if full symbols are loaded later. */
8282 if (pdi->has_template_arguments)
8283 {
52356b79 8284 pdi->fixup (cu);
98bfdba5
PA
8285
8286 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
8287 {
8288 struct die_info *die;
8289 struct attribute attr;
8290 struct dwarf2_cu *ref_cu = cu;
8291
b64f50a1 8292 /* DW_FORM_ref_addr is using section offset. */
b4069958 8293 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8294 attr.form = DW_FORM_ref_addr;
9c541725 8295 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8296 die = follow_die_ref (NULL, &attr, &ref_cu);
8297
43816ebc 8298 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8299 }
8300 }
8301
72bf9492
DJ
8302 parent_scope = partial_die_parent_scope (pdi, cu);
8303 if (parent_scope == NULL)
8304 return NULL;
8305 else
43816ebc
TT
8306 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
8307 pdi->name, 0, cu));
c906108c
SS
8308}
8309
8310static void
72bf9492 8311add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8312{
5e22e966 8313 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 8314 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 8315 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8316 CORE_ADDR addr = 0;
15d034d0 8317 const char *actual_name = NULL;
e142c38c
DJ
8318 CORE_ADDR baseaddr;
8319
b3b3bada 8320 baseaddr = objfile->text_section_offset ();
c906108c 8321
43816ebc
TT
8322 gdb::unique_xmalloc_ptr<char> built_actual_name
8323 = partial_die_full_name (pdi, cu);
15d034d0 8324 if (built_actual_name != NULL)
43816ebc 8325 actual_name = built_actual_name.get ();
63d06c5c 8326
72bf9492
DJ
8327 if (actual_name == NULL)
8328 actual_name = pdi->name;
8329
76e288d1
TT
8330 partial_symbol psymbol;
8331 memset (&psymbol, 0, sizeof (psymbol));
8332 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8333 psymbol.ginfo.section = -1;
8334
8335 /* The code below indicates that the psymbol should be installed by
8336 setting this. */
8337 gdb::optional<psymbol_placement> where;
8338
c906108c
SS
8339 switch (pdi->tag)
8340 {
b1dc1806 8341 case DW_TAG_inlined_subroutine:
c906108c 8342 case DW_TAG_subprogram:
79748972
TT
8343 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8344 - baseaddr);
0a4b0913
AB
8345 if (pdi->is_external
8346 || cu->language == language_ada
8347 || (cu->language == language_fortran
8348 && pdi->die_parent != NULL
8349 && pdi->die_parent->tag == DW_TAG_subprogram))
8350 {
8351 /* Normally, only "external" DIEs are part of the global scope.
8352 But in Ada and Fortran, we want to be able to access nested
8353 procedures globally. So all Ada and Fortran subprograms are
8354 stored in the global scope. */
76e288d1 8355 where = psymbol_placement::GLOBAL;
c906108c
SS
8356 }
8357 else
76e288d1
TT
8358 where = psymbol_placement::STATIC;
8359
8360 psymbol.domain = VAR_DOMAIN;
8361 psymbol.aclass = LOC_BLOCK;
8362 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8363 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8364
8365 if (pdi->main_subprogram && actual_name != NULL)
8366 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8367 break;
72929c62 8368 case DW_TAG_constant:
76e288d1
TT
8369 psymbol.domain = VAR_DOMAIN;
8370 psymbol.aclass = LOC_STATIC;
8371 where = (pdi->is_external
8372 ? psymbol_placement::GLOBAL
8373 : psymbol_placement::STATIC);
72929c62 8374 break;
c906108c 8375 case DW_TAG_variable:
95554aad
TT
8376 if (pdi->d.locdesc)
8377 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8378
95554aad 8379 if (pdi->d.locdesc
caac4577 8380 && addr == 0
5989a64e 8381 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8382 {
8383 /* A global or static variable may also have been stripped
8384 out by the linker if unused, in which case its address
8385 will be nullified; do not add such variables into partial
8386 symbol table then. */
8387 }
8388 else if (pdi->is_external)
c906108c
SS
8389 {
8390 /* Global Variable.
8391 Don't enter into the minimal symbol tables as there is
8392 a minimal symbol table entry from the ELF symbols already.
8393 Enter into partial symbol table if it has a location
8394 descriptor or a type.
8395 If the location descriptor is missing, new_symbol will create
8396 a LOC_UNRESOLVED symbol, the address of the variable will then
8397 be determined from the minimal symbol table whenever the variable
8398 is referenced.
8399 The address for the partial symbol table entry is not
8400 used by GDB, but it comes in handy for debugging partial symbol
8401 table building. */
8402
95554aad 8403 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8404 {
8405 psymbol.domain = VAR_DOMAIN;
8406 psymbol.aclass = LOC_STATIC;
8407 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8408 psymbol.ginfo.value.address = addr;
8409 where = psymbol_placement::GLOBAL;
8410 }
c906108c
SS
8411 }
8412 else
8413 {
ff908ebf
AW
8414 int has_loc = pdi->d.locdesc != NULL;
8415
8416 /* Static Variable. Skip symbols whose value we cannot know (those
8417 without location descriptors or constant values). */
8418 if (!has_loc && !pdi->has_const_value)
43816ebc 8419 return;
ff908ebf 8420
76e288d1
TT
8421 psymbol.domain = VAR_DOMAIN;
8422 psymbol.aclass = LOC_STATIC;
8423 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8424 if (has_loc)
8425 psymbol.ginfo.value.address = addr;
8426 where = psymbol_placement::STATIC;
c906108c
SS
8427 }
8428 break;
8429 case DW_TAG_typedef:
8430 case DW_TAG_base_type:
a02abb62 8431 case DW_TAG_subrange_type:
76e288d1
TT
8432 psymbol.domain = VAR_DOMAIN;
8433 psymbol.aclass = LOC_TYPEDEF;
8434 where = psymbol_placement::STATIC;
c906108c 8435 break;
74921315 8436 case DW_TAG_imported_declaration:
72bf9492 8437 case DW_TAG_namespace:
76e288d1
TT
8438 psymbol.domain = VAR_DOMAIN;
8439 psymbol.aclass = LOC_TYPEDEF;
8440 where = psymbol_placement::GLOBAL;
72bf9492 8441 break;
530e8392 8442 case DW_TAG_module:
a5fd13a9
BH
8443 /* With Fortran 77 there might be a "BLOCK DATA" module
8444 available without any name. If so, we skip the module as it
8445 doesn't bring any value. */
8446 if (actual_name != nullptr)
76e288d1
TT
8447 {
8448 psymbol.domain = MODULE_DOMAIN;
8449 psymbol.aclass = LOC_TYPEDEF;
8450 where = psymbol_placement::GLOBAL;
8451 }
530e8392 8452 break;
c906108c 8453 case DW_TAG_class_type:
680b30c7 8454 case DW_TAG_interface_type:
c906108c
SS
8455 case DW_TAG_structure_type:
8456 case DW_TAG_union_type:
8457 case DW_TAG_enumeration_type:
fa4028e9
JB
8458 /* Skip external references. The DWARF standard says in the section
8459 about "Structure, Union, and Class Type Entries": "An incomplete
8460 structure, union or class type is represented by a structure,
8461 union or class entry that does not have a byte size attribute
8462 and that has a DW_AT_declaration attribute." */
8463 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8464 return;
fa4028e9 8465
63d06c5c
DC
8466 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8467 static vs. global. */
76e288d1
TT
8468 psymbol.domain = STRUCT_DOMAIN;
8469 psymbol.aclass = LOC_TYPEDEF;
8470 where = (cu->language == language_cplus
8471 ? psymbol_placement::GLOBAL
8472 : psymbol_placement::STATIC);
c906108c
SS
8473 break;
8474 case DW_TAG_enumerator:
76e288d1
TT
8475 psymbol.domain = VAR_DOMAIN;
8476 psymbol.aclass = LOC_CONST;
8477 where = (cu->language == language_cplus
8478 ? psymbol_placement::GLOBAL
8479 : psymbol_placement::STATIC);
c906108c
SS
8480 break;
8481 default:
8482 break;
8483 }
76e288d1
TT
8484
8485 if (where.has_value ())
8486 {
f049a313
TT
8487 if (built_actual_name != nullptr)
8488 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8489 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8490 psymbol.ginfo.set_linkage_name (actual_name);
8491 else
8492 {
8493 psymbol.ginfo.set_demangled_name (actual_name,
8494 &objfile->objfile_obstack);
8495 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8496 }
76e288d1
TT
8497 add_psymbol_to_list (psymbol, *where, objfile);
8498 }
c906108c
SS
8499}
8500
5c4e30ca
DC
8501/* Read a partial die corresponding to a namespace; also, add a symbol
8502 corresponding to that namespace to the symbol table. NAMESPACE is
8503 the name of the enclosing namespace. */
91c24f0a 8504
72bf9492
DJ
8505static void
8506add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8507 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8508 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8509{
72bf9492 8510 /* Add a symbol for the namespace. */
e7c27a73 8511
72bf9492 8512 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8513
8514 /* Now scan partial symbols in that namespace. */
8515
91c24f0a 8516 if (pdi->has_children)
cdc07690 8517 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8518}
8519
5d7cb8df
JK
8520/* Read a partial die corresponding to a Fortran module. */
8521
8522static void
8523add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8524 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8525{
530e8392
KB
8526 /* Add a symbol for the namespace. */
8527
8528 add_partial_symbol (pdi, cu);
8529
f55ee35c 8530 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8531
8532 if (pdi->has_children)
cdc07690 8533 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8534}
8535
b1dc1806
XR
8536/* Read a partial die corresponding to a subprogram or an inlined
8537 subprogram and create a partial symbol for that subprogram.
8538 When the CU language allows it, this routine also defines a partial
8539 symbol for each nested subprogram that this subprogram contains.
8540 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8541 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8542
cdc07690
YQ
8543 PDI may also be a lexical block, in which case we simply search
8544 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8545 Again, this is only performed when the CU language allows this
8546 type of definitions. */
8547
8548static void
8549add_partial_subprogram (struct partial_die_info *pdi,
8550 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8551 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8552{
b1dc1806 8553 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8554 {
8555 if (pdi->has_pc_info)
8556 {
8557 if (pdi->lowpc < *lowpc)
8558 *lowpc = pdi->lowpc;
8559 if (pdi->highpc > *highpc)
8560 *highpc = pdi->highpc;
cdc07690 8561 if (set_addrmap)
5734ee8b 8562 {
5e22e966 8563 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8564 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8565 CORE_ADDR baseaddr;
b926417a
TT
8566 CORE_ADDR this_highpc;
8567 CORE_ADDR this_lowpc;
5734ee8b 8568
b3b3bada 8569 baseaddr = objfile->text_section_offset ();
b926417a
TT
8570 this_lowpc
8571 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8572 pdi->lowpc + baseaddr)
8573 - baseaddr);
8574 this_highpc
8575 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8576 pdi->highpc + baseaddr)
8577 - baseaddr);
d320c2b5 8578 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8579 this_lowpc, this_highpc - 1,
9291a0cd 8580 cu->per_cu->v.psymtab);
5734ee8b 8581 }
481860b3
GB
8582 }
8583
8584 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8585 {
bc30ff58 8586 if (!pdi->is_declaration)
e8d05480
JB
8587 /* Ignore subprogram DIEs that do not have a name, they are
8588 illegal. Do not emit a complaint at this point, we will
8589 do so when we convert this psymtab into a symtab. */
8590 if (pdi->name)
8591 add_partial_symbol (pdi, cu);
bc30ff58
JB
8592 }
8593 }
6e70227d 8594
bc30ff58
JB
8595 if (! pdi->has_children)
8596 return;
8597
0a4b0913 8598 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8599 {
8600 pdi = pdi->die_child;
8601 while (pdi != NULL)
8602 {
52356b79 8603 pdi->fixup (cu);
bc30ff58 8604 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8605 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8606 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8607 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8608 pdi = pdi->die_sibling;
8609 }
8610 }
8611}
8612
91c24f0a
DC
8613/* Read a partial die corresponding to an enumeration type. */
8614
72bf9492
DJ
8615static void
8616add_partial_enumeration (struct partial_die_info *enum_pdi,
8617 struct dwarf2_cu *cu)
91c24f0a 8618{
72bf9492 8619 struct partial_die_info *pdi;
91c24f0a
DC
8620
8621 if (enum_pdi->name != NULL)
72bf9492
DJ
8622 add_partial_symbol (enum_pdi, cu);
8623
8624 pdi = enum_pdi->die_child;
8625 while (pdi)
91c24f0a 8626 {
72bf9492 8627 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
b98664d3 8628 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8629 else
72bf9492
DJ
8630 add_partial_symbol (pdi, cu);
8631 pdi = pdi->die_sibling;
91c24f0a 8632 }
91c24f0a
DC
8633}
8634
6caca83c
CC
8635/* Return the initial uleb128 in the die at INFO_PTR. */
8636
8637static unsigned int
d521ce57 8638peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8639{
8640 unsigned int bytes_read;
8641
8642 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8643}
8644
685af9cd
TT
8645/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8646 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8647
4bb7a0a7
DJ
8648 Return the corresponding abbrev, or NULL if the number is zero (indicating
8649 an empty DIE). In either case *BYTES_READ will be set to the length of
8650 the initial number. */
8651
8652static struct abbrev_info *
685af9cd
TT
8653peek_die_abbrev (const die_reader_specs &reader,
8654 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8655{
685af9cd 8656 dwarf2_cu *cu = reader.cu;
5e22e966 8657 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8658 unsigned int abbrev_number
8659 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8660
8661 if (abbrev_number == 0)
8662 return NULL;
8663
685af9cd 8664 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8665 if (!abbrev)
8666 {
422b9917 8667 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8668 " at offset %s [in module %s]"),
422b9917 8669 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8670 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8671 }
8672
8673 return abbrev;
8674}
8675
93311388
DE
8676/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8677 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8678 DIE. Any children of the skipped DIEs will also be skipped. */
8679
d521ce57
TT
8680static const gdb_byte *
8681skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8682{
4bb7a0a7
DJ
8683 while (1)
8684 {
685af9cd
TT
8685 unsigned int bytes_read;
8686 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8687
4bb7a0a7
DJ
8688 if (abbrev == NULL)
8689 return info_ptr + bytes_read;
8690 else
dee91e82 8691 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8692 }
8693}
8694
93311388
DE
8695/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8696 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8697 abbrev corresponding to that skipped uleb128 should be passed in
8698 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8699 children. */
8700
d521ce57
TT
8701static const gdb_byte *
8702skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8703 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8704{
8705 unsigned int bytes_read;
8706 struct attribute attr;
dee91e82
DE
8707 bfd *abfd = reader->abfd;
8708 struct dwarf2_cu *cu = reader->cu;
d521ce57 8709 const gdb_byte *buffer = reader->buffer;
f664829e 8710 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8711 unsigned int form, i;
8712
8713 for (i = 0; i < abbrev->num_attrs; i++)
8714 {
8715 /* The only abbrev we care about is DW_AT_sibling. */
8716 if (abbrev->attrs[i].name == DW_AT_sibling)
8717 {
18a8505e
AT
8718 bool ignored;
8719 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr,
8720 &ignored);
4bb7a0a7 8721 if (attr.form == DW_FORM_ref_addr)
b98664d3 8722 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8723 else
b9502d3f 8724 {
0826b30a 8725 sect_offset off = attr.get_ref_die_offset ();
9c541725 8726 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8727
8728 if (sibling_ptr < info_ptr)
b98664d3 8729 complaint (_("DW_AT_sibling points backwards"));
22869d73 8730 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8731 reader->die_section->overflow_complaint ();
b9502d3f
WN
8732 else
8733 return sibling_ptr;
8734 }
4bb7a0a7
DJ
8735 }
8736
8737 /* If it isn't DW_AT_sibling, skip this attribute. */
8738 form = abbrev->attrs[i].form;
8739 skip_attribute:
8740 switch (form)
8741 {
4bb7a0a7 8742 case DW_FORM_ref_addr:
ae411497
TT
8743 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8744 and later it is offset sized. */
8745 if (cu->header.version == 2)
8746 info_ptr += cu->header.addr_size;
8747 else
8748 info_ptr += cu->header.offset_size;
8749 break;
36586728
TT
8750 case DW_FORM_GNU_ref_alt:
8751 info_ptr += cu->header.offset_size;
8752 break;
ae411497 8753 case DW_FORM_addr:
4bb7a0a7
DJ
8754 info_ptr += cu->header.addr_size;
8755 break;
8756 case DW_FORM_data1:
8757 case DW_FORM_ref1:
8758 case DW_FORM_flag:
8fe0f950 8759 case DW_FORM_strx1:
4bb7a0a7
DJ
8760 info_ptr += 1;
8761 break;
2dc7f7b3 8762 case DW_FORM_flag_present:
43988095 8763 case DW_FORM_implicit_const:
2dc7f7b3 8764 break;
4bb7a0a7
DJ
8765 case DW_FORM_data2:
8766 case DW_FORM_ref2:
8fe0f950 8767 case DW_FORM_strx2:
4bb7a0a7
DJ
8768 info_ptr += 2;
8769 break;
8fe0f950
AT
8770 case DW_FORM_strx3:
8771 info_ptr += 3;
8772 break;
4bb7a0a7
DJ
8773 case DW_FORM_data4:
8774 case DW_FORM_ref4:
8fe0f950 8775 case DW_FORM_strx4:
4bb7a0a7
DJ
8776 info_ptr += 4;
8777 break;
8778 case DW_FORM_data8:
8779 case DW_FORM_ref8:
55f1336d 8780 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
8781 info_ptr += 8;
8782 break;
0224619f
JK
8783 case DW_FORM_data16:
8784 info_ptr += 16;
8785 break;
4bb7a0a7 8786 case DW_FORM_string:
9b1c24c8 8787 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
8788 info_ptr += bytes_read;
8789 break;
2dc7f7b3 8790 case DW_FORM_sec_offset:
4bb7a0a7 8791 case DW_FORM_strp:
36586728 8792 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
8793 info_ptr += cu->header.offset_size;
8794 break;
2dc7f7b3 8795 case DW_FORM_exprloc:
4bb7a0a7
DJ
8796 case DW_FORM_block:
8797 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8798 info_ptr += bytes_read;
8799 break;
8800 case DW_FORM_block1:
8801 info_ptr += 1 + read_1_byte (abfd, info_ptr);
8802 break;
8803 case DW_FORM_block2:
8804 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
8805 break;
8806 case DW_FORM_block4:
8807 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
8808 break;
336d760d 8809 case DW_FORM_addrx:
cf532bd1 8810 case DW_FORM_strx:
4bb7a0a7
DJ
8811 case DW_FORM_sdata:
8812 case DW_FORM_udata:
8813 case DW_FORM_ref_udata:
3019eac3
DE
8814 case DW_FORM_GNU_addr_index:
8815 case DW_FORM_GNU_str_index:
18a8505e 8816 case DW_FORM_rnglistx:
41144253 8817 case DW_FORM_loclistx:
d521ce57 8818 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
8819 break;
8820 case DW_FORM_indirect:
8821 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8822 info_ptr += bytes_read;
8823 /* We need to continue parsing from here, so just go back to
8824 the top. */
8825 goto skip_attribute;
8826
8827 default:
3e43a32a
MS
8828 error (_("Dwarf Error: Cannot handle %s "
8829 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
8830 dwarf_form_name (form),
8831 bfd_get_filename (abfd));
8832 }
8833 }
8834
8835 if (abbrev->has_children)
dee91e82 8836 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
8837 else
8838 return info_ptr;
8839}
8840
93311388 8841/* Locate ORIG_PDI's sibling.
dee91e82 8842 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 8843
d521ce57 8844static const gdb_byte *
dee91e82
DE
8845locate_pdi_sibling (const struct die_reader_specs *reader,
8846 struct partial_die_info *orig_pdi,
d521ce57 8847 const gdb_byte *info_ptr)
91c24f0a
DC
8848{
8849 /* Do we know the sibling already? */
72bf9492 8850
91c24f0a
DC
8851 if (orig_pdi->sibling)
8852 return orig_pdi->sibling;
8853
8854 /* Are there any children to deal with? */
8855
8856 if (!orig_pdi->has_children)
8857 return info_ptr;
8858
4bb7a0a7 8859 /* Skip the children the long way. */
91c24f0a 8860
dee91e82 8861 return skip_children (reader, info_ptr);
91c24f0a
DC
8862}
8863
257e7a09 8864/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 8865 not NULL. */
c906108c 8866
891813be
TT
8867void
8868dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 8869{
ed2dc618
SM
8870 struct dwarf2_per_objfile *dwarf2_per_objfile
8871 = get_dwarf2_per_objfile (objfile);
8872
af758d11
SM
8873 gdb_assert (!dwarf2_per_objfile->symtab_set_p (per_cu_data));
8874
077cbab2
TT
8875 /* If this psymtab is constructed from a debug-only objfile, the
8876 has_section_at_zero flag will not necessarily be correct. We
8877 can get the correct value for this flag by looking at the data
8878 associated with the (presumably stripped) associated objfile. */
8879 if (objfile->separate_debug_objfile_backlink)
c906108c 8880 {
077cbab2
TT
8881 struct dwarf2_per_objfile *dpo_backlink
8882 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 8883
5989a64e
SM
8884 dwarf2_per_objfile->per_bfd->has_section_at_zero
8885 = dpo_backlink->per_bfd->has_section_at_zero;
077cbab2 8886 }
98bfdba5 8887
8566b89b 8888 expand_psymtab (objfile);
95554aad 8889
ed2dc618 8890 process_cu_includes (dwarf2_per_objfile);
c906108c 8891}
9cdd5dbd
DE
8892\f
8893/* Reading in full CUs. */
c906108c 8894
10b3939b
DJ
8895/* Add PER_CU to the queue. */
8896
8897static void
120ce1b5
SM
8898queue_comp_unit (dwarf2_per_cu_data *per_cu,
8899 dwarf2_per_objfile *per_objfile,
95554aad 8900 enum language pretend_language)
10b3939b 8901{
10b3939b 8902 per_cu->queued = 1;
120ce1b5 8903 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
8904}
8905
89e63ee4
DE
8906/* If PER_CU is not yet queued, add it to the queue.
8907 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
8908 dependency.
0907af0c 8909 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
8910 meaning either PER_CU is already queued or it is already loaded.
8911
8912 N.B. There is an invariant here that if a CU is queued then it is loaded.
8913 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
8914
8915static int
89e63ee4 8916maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
8917 dwarf2_per_cu_data *per_cu,
8918 dwarf2_per_objfile *per_objfile,
0907af0c
DE
8919 enum language pretend_language)
8920{
8921 /* We may arrive here during partial symbol reading, if we need full
8922 DIEs to process an unusual case (e.g. template arguments). Do
8923 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 8924 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 8925 {
7188ed02
SM
8926 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8927
8928 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
8929 return 1;
8930 return 0;
8931 }
8932
8933 /* Mark the dependence relation so that we don't flush PER_CU
8934 too early. */
89e63ee4
DE
8935 if (dependent_cu != NULL)
8936 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
8937
8938 /* If it's already on the queue, we have nothing to do. */
8939 if (per_cu->queued)
8940 return 0;
8941
8942 /* If the compilation unit is already loaded, just mark it as
8943 used. */
7188ed02
SM
8944 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
8945 if (cu != nullptr)
0907af0c 8946 {
7188ed02 8947 cu->last_used = 0;
0907af0c
DE
8948 return 0;
8949 }
8950
8951 /* Add it to the queue. */
120ce1b5 8952 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
8953
8954 return 1;
8955}
8956
10b3939b
DJ
8957/* Process the queue. */
8958
8959static void
ed2dc618 8960process_queue (struct dwarf2_per_objfile *dwarf2_per_objfile)
10b3939b 8961{
b4f54984 8962 if (dwarf_read_debug)
45cfd468
DE
8963 {
8964 fprintf_unfiltered (gdb_stdlog,
8965 "Expanding one or more symtabs of objfile %s ...\n",
4262abfb 8966 objfile_name (dwarf2_per_objfile->objfile));
45cfd468
DE
8967 }
8968
03dd20cc
DJ
8969 /* The queue starts out with one item, but following a DIE reference
8970 may load a new CU, adding it to the end of the queue. */
5989a64e 8971 while (!dwarf2_per_objfile->per_bfd->queue.empty ())
10b3939b 8972 {
5989a64e 8973 dwarf2_queue_item &item = dwarf2_per_objfile->per_bfd->queue.front ();
7188ed02 8974 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 8975
7188ed02 8976 if (!dwarf2_per_objfile->symtab_set_p (per_cu))
f4dc4d17 8977 {
7188ed02 8978 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
f4dc4d17 8979
7188ed02
SM
8980 /* Skip dummy CUs. */
8981 if (cu != nullptr)
73be47f5 8982 {
7188ed02
SM
8983 unsigned int debug_print_threshold;
8984 char buf[100];
8985
8986 if (per_cu->is_debug_types)
8987 {
8988 struct signatured_type *sig_type =
8989 (struct signatured_type *) per_cu;
8990
8991 sprintf (buf, "TU %s at offset %s",
8992 hex_string (sig_type->signature),
8993 sect_offset_str (per_cu->sect_off));
8994 /* There can be 100s of TUs.
8995 Only print them in verbose mode. */
8996 debug_print_threshold = 2;
8997 }
8998 else
8999 {
9000 sprintf (buf, "CU at offset %s",
9001 sect_offset_str (per_cu->sect_off));
9002 debug_print_threshold = 1;
9003 }
247f5c4f 9004
7188ed02
SM
9005 if (dwarf_read_debug >= debug_print_threshold)
9006 fprintf_unfiltered (gdb_stdlog, "Expanding symtab of %s\n", buf);
f4dc4d17 9007
7188ed02
SM
9008 if (per_cu->is_debug_types)
9009 process_full_type_unit (cu, item.pretend_language);
9010 else
9011 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9012
7188ed02
SM
9013 if (dwarf_read_debug >= debug_print_threshold)
9014 fprintf_unfiltered (gdb_stdlog, "Done expanding %s\n", buf);
9015 }
f4dc4d17 9016 }
10b3939b 9017
7188ed02 9018 per_cu->queued = 0;
5989a64e 9019 dwarf2_per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9020 }
9021
b4f54984 9022 if (dwarf_read_debug)
45cfd468
DE
9023 {
9024 fprintf_unfiltered (gdb_stdlog, "Done expanding symtabs of %s.\n",
4262abfb 9025 objfile_name (dwarf2_per_objfile->objfile));
45cfd468 9026 }
10b3939b
DJ
9027}
9028
10b3939b
DJ
9029/* Read in full symbols for PST, and anything it depends on. */
9030
8566b89b
TT
9031void
9032dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9033{
af758d11 9034 gdb_assert (!readin_p (objfile));
95554aad 9035
48993951 9036 expand_dependencies (objfile);
aaa75496 9037
97a1449a
SM
9038 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9039 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9040 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9041}
9042
af758d11
SM
9043/* See psympriv.h. */
9044
9045bool
9046dwarf2_psymtab::readin_p (struct objfile *objfile) const
9047{
9048 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9049 return per_objfile->symtab_set_p (per_cu_data);
9050}
9051
9052/* See psympriv.h. */
9053
9054compunit_symtab *
9055dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9056{
9057 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9058 return per_objfile->get_symtab (per_cu_data);
9059}
9060
dee91e82
DE
9061/* Trivial hash function for die_info: the hash value of a DIE
9062 is its offset in .debug_info for this objfile. */
10b3939b 9063
dee91e82
DE
9064static hashval_t
9065die_hash (const void *item)
10b3939b 9066{
9a3c8263 9067 const struct die_info *die = (const struct die_info *) item;
6502dd73 9068
9c541725 9069 return to_underlying (die->sect_off);
dee91e82 9070}
63d06c5c 9071
dee91e82
DE
9072/* Trivial comparison function for die_info structures: two DIEs
9073 are equal if they have the same offset. */
98bfdba5 9074
dee91e82
DE
9075static int
9076die_eq (const void *item_lhs, const void *item_rhs)
9077{
9a3c8263
SM
9078 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9079 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9080
9c541725 9081 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9082}
c906108c 9083
c0ab21c2 9084/* Load the DIEs associated with PER_CU into memory. */
c906108c 9085
dee91e82 9086static void
ab432490
SM
9087load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9088 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
9089 bool skip_partial,
9090 enum language pretend_language)
dee91e82 9091{
c0ab21c2
TT
9092 gdb_assert (! this_cu->is_debug_types);
9093
7188ed02
SM
9094 dwarf2_cu *existing_cu = per_objfile->get_cu (this_cu);
9095 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9096 if (reader.dummy_p)
9097 return;
9098
9099 struct dwarf2_cu *cu = reader.cu;
9100 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9101
dee91e82
DE
9102 gdb_assert (cu->die_hash == NULL);
9103 cu->die_hash =
9104 htab_create_alloc_ex (cu->header.length / 12,
9105 die_hash,
9106 die_eq,
9107 NULL,
9108 &cu->comp_unit_obstack,
9109 hashtab_obstack_allocate,
9110 dummy_obstack_deallocate);
e142c38c 9111
3e225074 9112 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9113 reader.comp_unit_die->child
9114 = read_die_and_siblings (&reader, reader.info_ptr,
9115 &info_ptr, reader.comp_unit_die);
9116 cu->dies = reader.comp_unit_die;
dee91e82 9117 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9118
9119 /* We try not to read any attributes in this function, because not
9cdd5dbd 9120 all CUs needed for references have been loaded yet, and symbol
10b3939b 9121 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9122 or we won't be able to build types correctly.
9123 Similarly, if we do not read the producer, we can not apply
9124 producer-specific interpretation. */
c0ab21c2 9125 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9126
9127 reader.keep ();
10b3939b
DJ
9128}
9129
3da10d80
KS
9130/* Add a DIE to the delayed physname list. */
9131
9132static void
9133add_to_method_list (struct type *type, int fnfield_index, int index,
9134 const char *name, struct die_info *die,
9135 struct dwarf2_cu *cu)
9136{
9137 struct delayed_method_info mi;
9138 mi.type = type;
9139 mi.fnfield_index = fnfield_index;
9140 mi.index = index;
9141 mi.name = name;
9142 mi.die = die;
c89b44cd 9143 cu->method_list.push_back (mi);
3da10d80
KS
9144}
9145
3693fdb3
PA
9146/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9147 "const" / "volatile". If so, decrements LEN by the length of the
9148 modifier and return true. Otherwise return false. */
9149
9150template<size_t N>
9151static bool
9152check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9153{
9154 size_t mod_len = sizeof (mod) - 1;
9155 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9156 {
9157 len -= mod_len;
9158 return true;
9159 }
9160 return false;
9161}
9162
3da10d80
KS
9163/* Compute the physnames of any methods on the CU's method list.
9164
9165 The computation of method physnames is delayed in order to avoid the
9166 (bad) condition that one of the method's formal parameters is of an as yet
9167 incomplete type. */
9168
9169static void
9170compute_delayed_physnames (struct dwarf2_cu *cu)
9171{
3693fdb3 9172 /* Only C++ delays computing physnames. */
c89b44cd 9173 if (cu->method_list.empty ())
3693fdb3
PA
9174 return;
9175 gdb_assert (cu->language == language_cplus);
9176
52941706 9177 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9178 {
1d06ead6 9179 const char *physname;
3da10d80 9180 struct fn_fieldlist *fn_flp
c89b44cd
TT
9181 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9182 physname = dwarf2_physname (mi.name, mi.die, cu);
9183 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9184 = physname ? physname : "";
3693fdb3
PA
9185
9186 /* Since there's no tag to indicate whether a method is a
9187 const/volatile overload, extract that information out of the
9188 demangled name. */
9189 if (physname != NULL)
9190 {
9191 size_t len = strlen (physname);
9192
9193 while (1)
9194 {
9195 if (physname[len] == ')') /* shortcut */
9196 break;
9197 else if (check_modifier (physname, len, " const"))
c89b44cd 9198 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9199 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9200 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9201 else
9202 break;
9203 }
9204 }
3da10d80 9205 }
c89b44cd
TT
9206
9207 /* The list is no longer needed. */
9208 cu->method_list.clear ();
3da10d80
KS
9209}
9210
a766d390
DE
9211/* Go objects should be embedded in a DW_TAG_module DIE,
9212 and it's not clear if/how imported objects will appear.
9213 To keep Go support simple until that's worked out,
9214 go back through what we've read and create something usable.
9215 We could do this while processing each DIE, and feels kinda cleaner,
9216 but that way is more invasive.
9217 This is to, for example, allow the user to type "p var" or "b main"
9218 without having to specify the package name, and allow lookups
9219 of module.object to work in contexts that use the expression
9220 parser. */
9221
9222static void
9223fixup_go_packaging (struct dwarf2_cu *cu)
9224{
421d1616 9225 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9226 struct pending *list;
9227 int i;
9228
c24bdb02 9229 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9230 list != NULL;
9231 list = list->next)
a766d390
DE
9232 {
9233 for (i = 0; i < list->nsyms; ++i)
9234 {
9235 struct symbol *sym = list->symbol[i];
9236
c1b5c1eb 9237 if (sym->language () == language_go
a766d390
DE
9238 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9239 {
421d1616
TT
9240 gdb::unique_xmalloc_ptr<char> this_package_name
9241 (go_symbol_package_name (sym));
a766d390
DE
9242
9243 if (this_package_name == NULL)
9244 continue;
9245 if (package_name == NULL)
421d1616 9246 package_name = std::move (this_package_name);
a766d390
DE
9247 else
9248 {
5e22e966 9249 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9250 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9251 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9252 (symbol_symtab (sym) != NULL
9253 ? symtab_to_filename_for_display
9254 (symbol_symtab (sym))
e3b94546 9255 : objfile_name (objfile)),
421d1616 9256 this_package_name.get (), package_name.get ());
a766d390
DE
9257 }
9258 }
9259 }
9260 }
9261
9262 if (package_name != NULL)
9263 {
5e22e966 9264 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9265 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9266 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9267 saved_package_name);
a766d390
DE
9268 struct symbol *sym;
9269
8c14c3a3 9270 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9271 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9272 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9273 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9274 e.g., "main" finds the "main" module and not C's main(). */
9275 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9276 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9277 SYMBOL_TYPE (sym) = type;
9278
c24bdb02 9279 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9280 }
9281}
9282
c9317f21
TT
9283/* Allocate a fully-qualified name consisting of the two parts on the
9284 obstack. */
9285
9286static const char *
9287rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9288{
9289 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9290}
9291
9c6a1327
TT
9292/* A helper that allocates a variant part to attach to a Rust enum
9293 type. OBSTACK is where the results should be allocated. TYPE is
9294 the type we're processing. DISCRIMINANT_INDEX is the index of the
9295 discriminant. It must be the index of one of the fields of TYPE.
9296 DEFAULT_INDEX is the index of the default field; or -1 if there is
9297 no default. RANGES is indexed by "effective" field number (the
9298 field index, but omitting the discriminant and default fields) and
9299 must hold the discriminant values used by the variants. Note that
9300 RANGES must have a lifetime at least as long as OBSTACK -- either
9301 already allocated on it, or static. */
c9317f21 9302
9c6a1327
TT
9303static void
9304alloc_rust_variant (struct obstack *obstack, struct type *type,
9305 int discriminant_index, int default_index,
9306 gdb::array_view<discriminant_range> ranges)
9307{
9308 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. Those
9309 must be handled by the caller. */
9310 gdb_assert (discriminant_index >= 0
1f704f76 9311 && discriminant_index < type->num_fields ());
c9317f21 9312 gdb_assert (default_index == -1
1f704f76 9313 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9314
9c6a1327 9315 /* We have one variant for each non-discriminant field. */
1f704f76 9316 int n_variants = type->num_fields () - 1;
c9317f21 9317
9c6a1327
TT
9318 variant *variants = new (obstack) variant[n_variants];
9319 int var_idx = 0;
9320 int range_idx = 0;
1f704f76 9321 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9322 {
9323 if (i == discriminant_index)
9324 continue;
c9317f21 9325
9c6a1327
TT
9326 variants[var_idx].first_field = i;
9327 variants[var_idx].last_field = i + 1;
9328
9329 /* The default field does not need a range, but other fields do.
9330 We skipped the discriminant above. */
9331 if (i != default_index)
9332 {
9333 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9334 ++range_idx;
9335 }
c9317f21 9336
9c6a1327
TT
9337 ++var_idx;
9338 }
9339
9340 gdb_assert (range_idx == ranges.size ());
9341 gdb_assert (var_idx == n_variants);
9342
9343 variant_part *part = new (obstack) variant_part;
9344 part->discriminant_index = discriminant_index;
9345 part->is_unsigned = TYPE_UNSIGNED (TYPE_FIELD_TYPE (type,
9346 discriminant_index));
9347 part->variants = gdb::array_view<variant> (variants, n_variants);
9348
9349 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9350 gdb::array_view<variant_part> *prop_value
9351 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9352
9c6a1327
TT
9353 struct dynamic_prop prop;
9354 prop.kind = PROP_VARIANT_PARTS;
9355 prop.data.variant_parts = prop_value;
9356
5c54719c 9357 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9358}
9359
9360/* Some versions of rustc emitted enums in an unusual way.
9361
9362 Ordinary enums were emitted as unions. The first element of each
9363 structure in the union was named "RUST$ENUM$DISR". This element
9364 held the discriminant.
9365
9366 These versions of Rust also implemented the "non-zero"
9367 optimization. When the enum had two values, and one is empty and
9368 the other holds a pointer that cannot be zero, the pointer is used
9369 as the discriminant, with a zero value meaning the empty variant.
9370 Here, the union's first member is of the form
9371 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9372 where the fieldnos are the indices of the fields that should be
9373 traversed in order to find the field (which may be several fields deep)
9374 and the variantname is the name of the variant of the case when the
9375 field is zero.
9376
9377 This function recognizes whether TYPE is of one of these forms,
9378 and, if so, smashes it to be a variant type. */
9379
9380static void
9381quirk_rust_enum (struct type *type, struct objfile *objfile)
9382{
78134374 9383 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9384
9385 /* We don't need to deal with empty enums. */
1f704f76 9386 if (type->num_fields () == 0)
c9317f21
TT
9387 return;
9388
9389#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9390 if (type->num_fields () == 1
c9317f21
TT
9391 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9392 {
9393 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9394
9395 /* Decode the field name to find the offset of the
9396 discriminant. */
9397 ULONGEST bit_offset = 0;
9398 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
9399 while (name[0] >= '0' && name[0] <= '9')
9400 {
9401 char *tail;
9402 unsigned long index = strtoul (name, &tail, 10);
9403 name = tail;
9404 if (*name != '$'
1f704f76 9405 || index >= field_type->num_fields ()
c9317f21
TT
9406 || (TYPE_FIELD_LOC_KIND (field_type, index)
9407 != FIELD_LOC_KIND_BITPOS))
9408 {
b98664d3 9409 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9410 "[in module %s]"),
9411 TYPE_FIELD_NAME (type, 0),
9412 objfile_name (objfile));
9413 return;
9414 }
9415 ++name;
9416
9417 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
9418 field_type = TYPE_FIELD_TYPE (field_type, index);
9419 }
9420
9c6a1327
TT
9421 /* Smash this type to be a structure type. We have to do this
9422 because the type has already been recorded. */
67607e24 9423 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9424 type->set_num_fields (3);
9c6a1327 9425 /* Save the field we care about. */
ceacbf6e 9426 struct field saved_field = type->field (0);
3cabb6b0
SM
9427 type->set_fields
9428 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9429
9c6a1327
TT
9430 /* Put the discriminant at index 0. */
9431 TYPE_FIELD_TYPE (type, 0) = field_type;
9432 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9433 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9434 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9435
9436 /* The order of fields doesn't really matter, so put the real
9437 field at index 1 and the data-less field at index 2. */
ceacbf6e 9438 type->field (1) = saved_field;
9c6a1327 9439 TYPE_FIELD_NAME (type, 1)
7d93a1e0 9440 = rust_last_path_segment (TYPE_FIELD_TYPE (type, 1)->name ());
d0e39ea2 9441 TYPE_FIELD_TYPE (type, 1)->set_name
7d93a1e0 9442 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9443 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9444
9445 const char *dataless_name
7d93a1e0 9446 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9447 name);
9448 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9449 dataless_name);
9c6a1327 9450 TYPE_FIELD_TYPE (type, 2) = dataless_type;
c9317f21
TT
9451 /* NAME points into the original discriminant name, which
9452 already has the correct lifetime. */
9c6a1327 9453 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9454 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9455
9c6a1327
TT
9456 /* Indicate that this is a variant type. */
9457 static discriminant_range ranges[1] = { { 0, 0 } };
9458 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9459 }
77c2dba3
TT
9460 /* A union with a single anonymous field is probably an old-style
9461 univariant enum. */
1f704f76 9462 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9463 {
c9317f21
TT
9464 /* Smash this type to be a structure type. We have to do this
9465 because the type has already been recorded. */
67607e24 9466 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9467
9c6a1327 9468 struct type *field_type = TYPE_FIELD_TYPE (type, 0);
c9317f21 9469 const char *variant_name
7d93a1e0 9470 = rust_last_path_segment (field_type->name ());
9c6a1327 9471 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9472 field_type->set_name
9473 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9474 type->name (), variant_name));
c9317f21
TT
9475 }
9476 else
9477 {
9478 struct type *disr_type = nullptr;
1f704f76 9479 for (int i = 0; i < type->num_fields (); ++i)
c9317f21
TT
9480 {
9481 disr_type = TYPE_FIELD_TYPE (type, i);
9482
78134374 9483 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9484 {
9485 /* All fields of a true enum will be structs. */
9486 return;
9487 }
1f704f76 9488 else if (disr_type->num_fields () == 0)
c9317f21
TT
9489 {
9490 /* Could be data-less variant, so keep going. */
a037790e 9491 disr_type = nullptr;
c9317f21
TT
9492 }
9493 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9494 "RUST$ENUM$DISR") != 0)
9495 {
9496 /* Not a Rust enum. */
9497 return;
9498 }
9499 else
9500 {
9501 /* Found one. */
9502 break;
9503 }
9504 }
9505
9506 /* If we got here without a discriminant, then it's probably
9507 just a union. */
9508 if (disr_type == nullptr)
9509 return;
9510
9511 /* Smash this type to be a structure type. We have to do this
9512 because the type has already been recorded. */
67607e24 9513 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9514
9c6a1327 9515 /* Make space for the discriminant field. */
ceacbf6e 9516 struct field *disr_field = &disr_type->field (0);
9c6a1327 9517 field *new_fields
1f704f76 9518 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9519 * sizeof (struct field)));
80fc5e77 9520 memcpy (new_fields + 1, type->fields (),
1f704f76 9521 type->num_fields () * sizeof (struct field));
3cabb6b0 9522 type->set_fields (new_fields);
1f704f76 9523 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9524
9525 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9526 type->field (0) = *disr_field;
9c6a1327
TT
9527 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9528 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9529
9530 /* We need a way to find the correct discriminant given a
9531 variant name. For convenience we build a map here. */
9532 struct type *enum_type = FIELD_TYPE (*disr_field);
9533 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9534 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9535 {
9536 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9537 {
9538 const char *name
9539 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9540 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9541 }
9542 }
9543
1f704f76 9544 int n_fields = type->num_fields ();
9c6a1327
TT
9545 /* We don't need a range entry for the discriminant, but we do
9546 need one for every other field, as there is no default
9547 variant. */
9548 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9549 discriminant_range,
9550 n_fields - 1);
c9317f21
TT
9551 /* Skip the discriminant here. */
9552 for (int i = 1; i < n_fields; ++i)
9553 {
9554 /* Find the final word in the name of this variant's type.
9555 That name can be used to look up the correct
9556 discriminant. */
9557 const char *variant_name
7d93a1e0 9558 = rust_last_path_segment (TYPE_FIELD_TYPE (type, i)->name ());
c9317f21
TT
9559
9560 auto iter = discriminant_map.find (variant_name);
9561 if (iter != discriminant_map.end ())
9c6a1327
TT
9562 {
9563 ranges[i].low = iter->second;
9564 ranges[i].high = iter->second;
9565 }
c9317f21 9566
bedda9ac 9567 /* Remove the discriminant field, if it exists. */
9c6a1327 9568 struct type *sub_type = TYPE_FIELD_TYPE (type, i);
1f704f76 9569 if (sub_type->num_fields () > 0)
bedda9ac 9570 {
5e33d5f4 9571 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9572 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9573 }
9c6a1327 9574 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9575 sub_type->set_name
9576 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9577 type->name (), variant_name));
c9317f21 9578 }
9c6a1327
TT
9579
9580 /* Indicate that this is a variant type. */
9581 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1,
9582 gdb::array_view<discriminant_range> (ranges,
9583 n_fields - 1));
c9317f21
TT
9584 }
9585}
9586
9587/* Rewrite some Rust unions to be structures with variants parts. */
9588
9589static void
9590rust_union_quirks (struct dwarf2_cu *cu)
9591{
9592 gdb_assert (cu->language == language_rust);
52941706 9593 for (type *type_ : cu->rust_unions)
5e22e966 9594 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9595 /* We don't need this any more. */
9596 cu->rust_unions.clear ();
c9317f21
TT
9597}
9598
8adb8487
TT
9599/* See read.h. */
9600
9601type_unit_group_unshareable *
9602dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9603{
9604 auto iter = this->m_type_units.find (tu_group);
9605 if (iter != this->m_type_units.end ())
9606 return iter->second.get ();
9607
9608 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9609 type_unit_group_unshareable *result = uniq.get ();
9610 this->m_type_units[tu_group] = std::move (uniq);
9611 return result;
9612}
9613
e286671b
TT
9614struct type *
9615dwarf2_per_objfile::get_type_for_signatured_type
9616 (signatured_type *sig_type) const
9617{
9618 auto iter = this->m_type_map.find (sig_type);
9619 if (iter == this->m_type_map.end ())
9620 return nullptr;
9621
9622 return iter->second;
9623}
9624
9625void dwarf2_per_objfile::set_type_for_signatured_type
9626 (signatured_type *sig_type, struct type *type)
9627{
9628 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9629
9630 this->m_type_map[sig_type] = type;
9631}
9632
95554aad
TT
9633/* A helper function for computing the list of all symbol tables
9634 included by PER_CU. */
9635
9636static void
4c39bc03 9637recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9638 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9639 dwarf2_per_cu_data *per_cu,
9640 dwarf2_per_objfile *per_objfile,
43f3e411 9641 struct compunit_symtab *immediate_parent)
95554aad 9642{
af758d11 9643 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9644 if (*slot != NULL)
9645 {
9646 /* This inclusion and its children have been processed. */
9647 return;
9648 }
9649
9650 *slot = per_cu;
af758d11 9651
95554aad 9652 /* Only add a CU if it has a symbol table. */
43182c09 9653 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9654 if (cust != NULL)
ec94af83
DE
9655 {
9656 /* If this is a type unit only add its symbol table if we haven't
9657 seen it yet (type unit per_cu's can share symtabs). */
9658 if (per_cu->is_debug_types)
9659 {
43f3e411 9660 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9661 if (*slot == NULL)
9662 {
43f3e411 9663 *slot = cust;
4c39bc03 9664 result->push_back (cust);
43f3e411
DE
9665 if (cust->user == NULL)
9666 cust->user = immediate_parent;
ec94af83
DE
9667 }
9668 }
9669 else
f9125b6c 9670 {
4c39bc03 9671 result->push_back (cust);
43f3e411
DE
9672 if (cust->user == NULL)
9673 cust->user = immediate_parent;
f9125b6c 9674 }
ec94af83 9675 }
95554aad 9676
ae640021
AB
9677 if (!per_cu->imported_symtabs_empty ())
9678 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9679 {
9680 recursively_compute_inclusions (result, all_children,
43182c09
SM
9681 all_type_symtabs, ptr, per_objfile,
9682 cust);
ae640021 9683 }
95554aad
TT
9684}
9685
43f3e411 9686/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9687 PER_CU. */
9688
9689static void
43182c09
SM
9690compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9691 dwarf2_per_objfile *per_objfile)
95554aad 9692{
f4dc4d17
DE
9693 gdb_assert (! per_cu->is_debug_types);
9694
ae640021 9695 if (!per_cu->imported_symtabs_empty ())
95554aad 9696 {
ae640021 9697 int len;
4c39bc03 9698 std::vector<compunit_symtab *> result_symtabs;
ec94af83 9699 htab_t all_children, all_type_symtabs;
43182c09 9700 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9701
9702 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9703 if (cust == NULL)
95554aad
TT
9704 return;
9705
9706 all_children = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9707 NULL, xcalloc, xfree);
ec94af83
DE
9708 all_type_symtabs = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
9709 NULL, xcalloc, xfree);
95554aad 9710
ae640021 9711 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83
DE
9712 {
9713 recursively_compute_inclusions (&result_symtabs, all_children,
43182c09
SM
9714 all_type_symtabs, ptr, per_objfile,
9715 cust);
ec94af83 9716 }
95554aad 9717
ec94af83 9718 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9719 len = result_symtabs.size ();
43f3e411 9720 cust->includes
f6e649dd 9721 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9722 struct compunit_symtab *, len + 1);
4c39bc03
TT
9723 memcpy (cust->includes, result_symtabs.data (),
9724 len * sizeof (compunit_symtab *));
43f3e411 9725 cust->includes[len] = NULL;
95554aad 9726
95554aad 9727 htab_delete (all_children);
ec94af83 9728 htab_delete (all_type_symtabs);
95554aad
TT
9729 }
9730}
9731
9732/* Compute the 'includes' field for the symtabs of all the CUs we just
9733 read. */
9734
9735static void
ed2dc618 9736process_cu_includes (struct dwarf2_per_objfile *dwarf2_per_objfile)
95554aad 9737{
5989a64e 9738 for (dwarf2_per_cu_data *iter : dwarf2_per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9739 {
9740 if (! iter->is_debug_types)
43182c09 9741 compute_compunit_symtab_includes (iter, dwarf2_per_objfile);
f4dc4d17 9742 }
95554aad 9743
5989a64e 9744 dwarf2_per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
9745}
9746
8fc0b21d 9747/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
9748 already been loaded into memory. */
9749
9750static void
8fc0b21d 9751process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 9752{
8fc0b21d 9753 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 9754 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 9755 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 9756 CORE_ADDR lowpc, highpc;
43f3e411 9757 struct compunit_symtab *cust;
10b3939b 9758 CORE_ADDR baseaddr;
4359dff1 9759 struct block *static_block;
3e29f34a 9760 CORE_ADDR addr;
10b3939b 9761
b3b3bada 9762 baseaddr = objfile->text_section_offset ();
10b3939b 9763
c89b44cd
TT
9764 /* Clear the list here in case something was left over. */
9765 cu->method_list.clear ();
10b3939b 9766
95554aad
TT
9767 cu->language = pretend_language;
9768 cu->language_defn = language_def (cu->language);
9769
c906108c 9770 /* Do line number decoding in read_file_scope () */
10b3939b 9771 process_die (cu->dies, cu);
c906108c 9772
a766d390
DE
9773 /* For now fudge the Go package. */
9774 if (cu->language == language_go)
9775 fixup_go_packaging (cu);
9776
5f48f8f3 9777 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
9778 should be complete, and it should now be safe to compute all of the
9779 physnames. */
9780 compute_delayed_physnames (cu);
3da10d80 9781
c9317f21
TT
9782 if (cu->language == language_rust)
9783 rust_union_quirks (cu);
9784
fae299cd
DC
9785 /* Some compilers don't define a DW_AT_high_pc attribute for the
9786 compilation unit. If the DW_AT_high_pc is missing, synthesize
9787 it, by scanning the DIE's below the compilation unit. */
10b3939b 9788 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 9789
3e29f34a 9790 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 9791 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
9792
9793 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
9794 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
9795 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
9796 addrmap to help ensure it has an accurate map of pc values belonging to
9797 this comp unit. */
9798 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
9799
c24bdb02 9800 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
9801 SECT_OFF_TEXT (objfile),
9802 0);
c906108c 9803
43f3e411 9804 if (cust != NULL)
c906108c 9805 {
df15bd07 9806 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 9807
8be455d7
JK
9808 /* Set symtab language to language from DW_AT_language. If the
9809 compilation is from a C file generated by language preprocessors, do
9810 not set the language if it was already deduced by start_subfile. */
43f3e411 9811 if (!(cu->language == language_c
40e3ad0e 9812 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 9813 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
9814
9815 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
9816 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
9817 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
9818 there were bugs in prologue debug info, fixed later in GCC-4.5
9819 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
9820
9821 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
9822 needed, it would be wrong due to missing DW_AT_producer there.
9823
9824 Still one can confuse GDB by using non-standard GCC compilation
9825 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 9826 */
ab260dad 9827 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 9828 cust->locations_valid = 1;
e0d00bc7
JK
9829
9830 if (gcc_4_minor >= 5)
43f3e411 9831 cust->epilogue_unwind_valid = 1;
96408a79 9832
43f3e411 9833 cust->call_site_htab = cu->call_site_htab;
c906108c 9834 }
9291a0cd 9835
8fc0b21d 9836 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
c906108c 9837
95554aad 9838 /* Push it for inclusion processing later. */
8fc0b21d 9839 dwarf2_per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
9840
9841 /* Not needed any more. */
c24bdb02 9842 cu->reset_builder ();
f4dc4d17 9843}
45cfd468 9844
8fc0b21d 9845/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
9846 already been loaded into memory. */
9847
9848static void
8fc0b21d 9849process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
9850 enum language pretend_language)
9851{
8fc0b21d 9852 dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 9853 struct objfile *objfile = dwarf2_per_objfile->objfile;
43f3e411 9854 struct compunit_symtab *cust;
0186c6a7
DE
9855 struct signatured_type *sig_type;
9856
8fc0b21d
SM
9857 gdb_assert (cu->per_cu->is_debug_types);
9858 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 9859
c89b44cd
TT
9860 /* Clear the list here in case something was left over. */
9861 cu->method_list.clear ();
f4dc4d17 9862
f4dc4d17
DE
9863 cu->language = pretend_language;
9864 cu->language_defn = language_def (cu->language);
9865
9866 /* The symbol tables are set up in read_type_unit_scope. */
9867 process_die (cu->dies, cu);
9868
9869 /* For now fudge the Go package. */
9870 if (cu->language == language_go)
9871 fixup_go_packaging (cu);
9872
5f48f8f3 9873 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
9874 should be complete, and it should now be safe to compute all of the
9875 physnames. */
9876 compute_delayed_physnames (cu);
f4dc4d17 9877
c9317f21
TT
9878 if (cu->language == language_rust)
9879 rust_union_quirks (cu);
9880
f4dc4d17
DE
9881 /* TUs share symbol tables.
9882 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
9883 of it with end_expandable_symtab. Otherwise, complete the addition of
9884 this TU's symbols to the existing symtab. */
8adb8487
TT
9885 type_unit_group_unshareable *tug_unshare =
9886 dwarf2_per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
9887 if (tug_unshare->compunit_symtab == NULL)
45cfd468 9888 {
c24bdb02
KS
9889 buildsym_compunit *builder = cu->get_builder ();
9890 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 9891 tug_unshare->compunit_symtab = cust;
f4dc4d17 9892
43f3e411 9893 if (cust != NULL)
f4dc4d17
DE
9894 {
9895 /* Set symtab language to language from DW_AT_language. If the
9896 compilation is from a C file generated by language preprocessors,
9897 do not set the language if it was already deduced by
9898 start_subfile. */
43f3e411
DE
9899 if (!(cu->language == language_c
9900 && COMPUNIT_FILETABS (cust)->language != language_c))
9901 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
9902 }
9903 }
9904 else
9905 {
c24bdb02 9906 cu->get_builder ()->augment_type_symtab ();
8adb8487 9907 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
9908 }
9909
8fc0b21d 9910 dwarf2_per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
9911
9912 /* Not needed any more. */
c24bdb02 9913 cu->reset_builder ();
c906108c
SS
9914}
9915
95554aad
TT
9916/* Process an imported unit DIE. */
9917
9918static void
9919process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
9920{
9921 struct attribute *attr;
9922
f4dc4d17
DE
9923 /* For now we don't handle imported units in type units. */
9924 if (cu->per_cu->is_debug_types)
9925 {
9926 error (_("Dwarf Error: DW_TAG_imported_unit is not"
9927 " supported in type units [in module %s]"),
5e22e966 9928 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
9929 }
9930
95554aad
TT
9931 attr = dwarf2_attr (die, DW_AT_import, cu);
9932 if (attr != NULL)
9933 {
0826b30a 9934 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 9935 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 9936 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 9937 dwarf2_per_cu_data *per_cu
ab432490 9938 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 9939
58990295
TV
9940 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
9941 into another compilation unit, at root level. Regard this as a hint,
9942 and ignore it. */
9943 if (die->parent && die->parent->parent == NULL
9944 && per_cu->unit_type == DW_UT_compile
9945 && per_cu->lang == language_cplus)
9946 return;
9947
69d751e3 9948 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 9949 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
ab432490 9950 load_full_comp_unit (per_cu, per_objfile, false, cu->language);
95554aad 9951
ae640021 9952 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
9953 }
9954}
9955
4c8aa72d
PA
9956/* RAII object that represents a process_die scope: i.e.,
9957 starts/finishes processing a DIE. */
9958class process_die_scope
adde2bff 9959{
4c8aa72d
PA
9960public:
9961 process_die_scope (die_info *die, dwarf2_cu *cu)
9962 : m_die (die), m_cu (cu)
9963 {
9964 /* We should only be processing DIEs not already in process. */
9965 gdb_assert (!m_die->in_process);
9966 m_die->in_process = true;
9967 }
8c3cb9fa 9968
4c8aa72d
PA
9969 ~process_die_scope ()
9970 {
9971 m_die->in_process = false;
9972
9973 /* If we're done processing the DIE for the CU that owns the line
9974 header, we don't need the line header anymore. */
9975 if (m_cu->line_header_die_owner == m_die)
9976 {
9977 delete m_cu->line_header;
9978 m_cu->line_header = NULL;
9979 m_cu->line_header_die_owner = NULL;
9980 }
9981 }
9982
9983private:
9984 die_info *m_die;
9985 dwarf2_cu *m_cu;
9986};
adde2bff 9987
c906108c
SS
9988/* Process a die and its children. */
9989
9990static void
e7c27a73 9991process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 9992{
4c8aa72d 9993 process_die_scope scope (die, cu);
adde2bff 9994
c906108c
SS
9995 switch (die->tag)
9996 {
9997 case DW_TAG_padding:
9998 break;
9999 case DW_TAG_compile_unit:
95554aad 10000 case DW_TAG_partial_unit:
e7c27a73 10001 read_file_scope (die, cu);
c906108c 10002 break;
348e048f
DE
10003 case DW_TAG_type_unit:
10004 read_type_unit_scope (die, cu);
10005 break;
c906108c 10006 case DW_TAG_subprogram:
0a4b0913
AB
10007 /* Nested subprograms in Fortran get a prefix. */
10008 if (cu->language == language_fortran
10009 && die->parent != NULL
10010 && die->parent->tag == DW_TAG_subprogram)
10011 cu->processing_has_namespace_info = true;
10012 /* Fall through. */
c906108c 10013 case DW_TAG_inlined_subroutine:
edb3359d 10014 read_func_scope (die, cu);
c906108c
SS
10015 break;
10016 case DW_TAG_lexical_block:
14898363
L
10017 case DW_TAG_try_block:
10018 case DW_TAG_catch_block:
e7c27a73 10019 read_lexical_block_scope (die, cu);
c906108c 10020 break;
216f72a1 10021 case DW_TAG_call_site:
96408a79
SA
10022 case DW_TAG_GNU_call_site:
10023 read_call_site_scope (die, cu);
10024 break;
c906108c 10025 case DW_TAG_class_type:
680b30c7 10026 case DW_TAG_interface_type:
c906108c
SS
10027 case DW_TAG_structure_type:
10028 case DW_TAG_union_type:
134d01f1 10029 process_structure_scope (die, cu);
c906108c
SS
10030 break;
10031 case DW_TAG_enumeration_type:
134d01f1 10032 process_enumeration_scope (die, cu);
c906108c 10033 break;
134d01f1 10034
f792889a
DJ
10035 /* These dies have a type, but processing them does not create
10036 a symbol or recurse to process the children. Therefore we can
10037 read them on-demand through read_type_die. */
c906108c 10038 case DW_TAG_subroutine_type:
72019c9c 10039 case DW_TAG_set_type:
c906108c 10040 case DW_TAG_array_type:
c906108c 10041 case DW_TAG_pointer_type:
c906108c 10042 case DW_TAG_ptr_to_member_type:
c906108c 10043 case DW_TAG_reference_type:
4297a3f0 10044 case DW_TAG_rvalue_reference_type:
c906108c 10045 case DW_TAG_string_type:
c906108c 10046 break;
134d01f1 10047
c906108c 10048 case DW_TAG_base_type:
a02abb62 10049 case DW_TAG_subrange_type:
cb249c71 10050 case DW_TAG_typedef:
134d01f1
DJ
10051 /* Add a typedef symbol for the type definition, if it has a
10052 DW_AT_name. */
f792889a 10053 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10054 break;
c906108c 10055 case DW_TAG_common_block:
e7c27a73 10056 read_common_block (die, cu);
c906108c
SS
10057 break;
10058 case DW_TAG_common_inclusion:
10059 break;
d9fa45fe 10060 case DW_TAG_namespace:
9068261f 10061 cu->processing_has_namespace_info = true;
e7c27a73 10062 read_namespace (die, cu);
d9fa45fe 10063 break;
5d7cb8df 10064 case DW_TAG_module:
9068261f 10065 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10066 read_module (die, cu);
10067 break;
d9fa45fe 10068 case DW_TAG_imported_declaration:
9068261f 10069 cu->processing_has_namespace_info = true;
74921315
KS
10070 if (read_namespace_alias (die, cu))
10071 break;
86a73007
TT
10072 /* The declaration is not a global namespace alias. */
10073 /* Fall through. */
d9fa45fe 10074 case DW_TAG_imported_module:
9068261f 10075 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10076 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10077 || cu->language != language_fortran))
b98664d3 10078 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10079 dwarf_tag_name (die->tag));
10080 read_import_statement (die, cu);
d9fa45fe 10081 break;
95554aad
TT
10082
10083 case DW_TAG_imported_unit:
10084 process_imported_unit_die (die, cu);
10085 break;
10086
71a3c369
TT
10087 case DW_TAG_variable:
10088 read_variable (die, cu);
10089 break;
10090
c906108c 10091 default:
e7c27a73 10092 new_symbol (die, NULL, cu);
c906108c
SS
10093 break;
10094 }
10095}
ca69b9e6
DE
10096\f
10097/* DWARF name computation. */
c906108c 10098
94af9270
KS
10099/* A helper function for dwarf2_compute_name which determines whether DIE
10100 needs to have the name of the scope prepended to the name listed in the
10101 die. */
10102
10103static int
10104die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10105{
1c809c68
TT
10106 struct attribute *attr;
10107
94af9270
KS
10108 switch (die->tag)
10109 {
10110 case DW_TAG_namespace:
10111 case DW_TAG_typedef:
10112 case DW_TAG_class_type:
10113 case DW_TAG_interface_type:
10114 case DW_TAG_structure_type:
10115 case DW_TAG_union_type:
10116 case DW_TAG_enumeration_type:
10117 case DW_TAG_enumerator:
10118 case DW_TAG_subprogram:
08a76f8a 10119 case DW_TAG_inlined_subroutine:
94af9270 10120 case DW_TAG_member:
74921315 10121 case DW_TAG_imported_declaration:
94af9270
KS
10122 return 1;
10123
10124 case DW_TAG_variable:
c2b0a229 10125 case DW_TAG_constant:
94af9270
KS
10126 /* We only need to prefix "globally" visible variables. These include
10127 any variable marked with DW_AT_external or any variable that
10128 lives in a namespace. [Variables in anonymous namespaces
10129 require prefixing, but they are not DW_AT_external.] */
10130
10131 if (dwarf2_attr (die, DW_AT_specification, cu))
10132 {
10133 struct dwarf2_cu *spec_cu = cu;
9a619af0 10134
94af9270
KS
10135 return die_needs_namespace (die_specification (die, &spec_cu),
10136 spec_cu);
10137 }
10138
1c809c68 10139 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10140 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10141 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10142 return 0;
10143 /* A variable in a lexical block of some kind does not need a
10144 namespace, even though in C++ such variables may be external
10145 and have a mangled name. */
10146 if (die->parent->tag == DW_TAG_lexical_block
10147 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10148 || die->parent->tag == DW_TAG_catch_block
10149 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10150 return 0;
10151 return 1;
94af9270
KS
10152
10153 default:
10154 return 0;
10155 }
10156}
10157
73b9be8b
KS
10158/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10159 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10160 defined for the given DIE. */
10161
10162static struct attribute *
10163dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10164{
10165 struct attribute *attr;
10166
10167 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10168 if (attr == NULL)
10169 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10170
10171 return attr;
10172}
10173
10174/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10175 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10176 defined for the given DIE. */
10177
10178static const char *
10179dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10180{
10181 const char *linkage_name;
10182
10183 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10184 if (linkage_name == NULL)
10185 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10186
787de330
TT
10187 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10188 See https://github.com/rust-lang/rust/issues/32925. */
10189 if (cu->language == language_rust && linkage_name != NULL
10190 && strchr (linkage_name, '{') != NULL)
10191 linkage_name = NULL;
10192
73b9be8b
KS
10193 return linkage_name;
10194}
10195
94af9270 10196/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10197 compute the physname for the object, which include a method's:
9c37b5ae 10198 - formal parameters (C++),
a766d390 10199 - receiver type (Go),
a766d390
DE
10200
10201 The term "physname" is a bit confusing.
10202 For C++, for example, it is the demangled name.
10203 For Go, for example, it's the mangled name.
94af9270 10204
af6b7be1
JB
10205 For Ada, return the DIE's linkage name rather than the fully qualified
10206 name. PHYSNAME is ignored..
10207
5989a64e 10208 The result is allocated on the objfile->per_bfd's obstack and
45940949 10209 canonicalized. */
94af9270
KS
10210
10211static const char *
15d034d0
TT
10212dwarf2_compute_name (const char *name,
10213 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10214 int physname)
10215{
5e22e966 10216 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10217
94af9270
KS
10218 if (name == NULL)
10219 name = dwarf2_name (die, cu);
10220
2ee7123e
DE
10221 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10222 but otherwise compute it by typename_concat inside GDB.
10223 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10224 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10225 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10226 will set the demangled name to the result of dwarf2_full_name, and it is
10227 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10228 if (cu->language == language_ada
10229 || (cu->language == language_fortran && physname))
10230 {
10231 /* For Ada unit, we prefer the linkage name over the name, as
10232 the former contains the exported name, which the user expects
10233 to be able to reference. Ideally, we want the user to be able
10234 to reference this entity using either natural or linkage name,
10235 but we haven't started looking at this enhancement yet. */
73b9be8b 10236 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10237
2ee7123e
DE
10238 if (linkage_name != NULL)
10239 return linkage_name;
f55ee35c
JK
10240 }
10241
94af9270
KS
10242 /* These are the only languages we know how to qualify names in. */
10243 if (name != NULL
9c37b5ae 10244 && (cu->language == language_cplus
c44af4eb
TT
10245 || cu->language == language_fortran || cu->language == language_d
10246 || cu->language == language_rust))
94af9270
KS
10247 {
10248 if (die_needs_namespace (die, cu))
10249 {
0d5cff50 10250 const char *prefix;
34a68019 10251 const char *canonical_name = NULL;
94af9270 10252
d7e74731
PA
10253 string_file buf;
10254
94af9270 10255 prefix = determine_prefix (die, cu);
94af9270
KS
10256 if (*prefix != '\0')
10257 {
43816ebc
TT
10258 gdb::unique_xmalloc_ptr<char> prefixed_name
10259 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10260
43816ebc 10261 buf.puts (prefixed_name.get ());
94af9270
KS
10262 }
10263 else
d7e74731 10264 buf.puts (name);
94af9270 10265
98bfdba5
PA
10266 /* Template parameters may be specified in the DIE's DW_AT_name, or
10267 as children with DW_TAG_template_type_param or
10268 DW_TAG_value_type_param. If the latter, add them to the name
10269 here. If the name already has template parameters, then
10270 skip this step; some versions of GCC emit both, and
10271 it is more efficient to use the pre-computed name.
10272
10273 Something to keep in mind about this process: it is very
10274 unlikely, or in some cases downright impossible, to produce
10275 something that will match the mangled name of a function.
10276 If the definition of the function has the same debug info,
10277 we should be able to match up with it anyway. But fallbacks
10278 using the minimal symbol, for instance to find a method
10279 implemented in a stripped copy of libstdc++, will not work.
10280 If we do not have debug info for the definition, we will have to
10281 match them up some other way.
10282
10283 When we do name matching there is a related problem with function
10284 templates; two instantiated function templates are allowed to
10285 differ only by their return types, which we do not add here. */
10286
10287 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10288 {
10289 struct attribute *attr;
10290 struct die_info *child;
10291 int first = 1;
10292
10293 die->building_fullname = 1;
10294
10295 for (child = die->child; child != NULL; child = child->sibling)
10296 {
10297 struct type *type;
12df843f 10298 LONGEST value;
d521ce57 10299 const gdb_byte *bytes;
98bfdba5
PA
10300 struct dwarf2_locexpr_baton *baton;
10301 struct value *v;
10302
10303 if (child->tag != DW_TAG_template_type_param
10304 && child->tag != DW_TAG_template_value_param)
10305 continue;
10306
10307 if (first)
10308 {
d7e74731 10309 buf.puts ("<");
98bfdba5
PA
10310 first = 0;
10311 }
10312 else
d7e74731 10313 buf.puts (", ");
98bfdba5
PA
10314
10315 attr = dwarf2_attr (child, DW_AT_type, cu);
10316 if (attr == NULL)
10317 {
b98664d3 10318 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10319 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10320 continue;
10321 }
10322 type = die_type (child, cu);
10323
10324 if (child->tag == DW_TAG_template_type_param)
10325 {
c1ec8cea
TT
10326 c_print_type (type, "", &buf, -1, 0, cu->language,
10327 &type_print_raw_options);
98bfdba5
PA
10328 continue;
10329 }
10330
10331 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10332 if (attr == NULL)
10333 {
b98664d3 10334 complaint (_("template parameter missing "
3e43a32a 10335 "DW_AT_const_value"));
d7e74731 10336 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10337 continue;
10338 }
10339
10340 dwarf2_const_value_attr (attr, type, name,
10341 &cu->comp_unit_obstack, cu,
10342 &value, &bytes, &baton);
10343
10344 if (TYPE_NOSIGN (type))
10345 /* GDB prints characters as NUMBER 'CHAR'. If that's
10346 changed, this can use value_print instead. */
d7e74731 10347 c_printchar (value, type, &buf);
98bfdba5
PA
10348 else
10349 {
10350 struct value_print_options opts;
10351
10352 if (baton != NULL)
10353 v = dwarf2_evaluate_loc_desc (type, NULL,
10354 baton->data,
10355 baton->size,
9f47c707
SM
10356 baton->per_cu,
10357 baton->per_objfile);
98bfdba5
PA
10358 else if (bytes != NULL)
10359 {
10360 v = allocate_value (type);
10361 memcpy (value_contents_writeable (v), bytes,
10362 TYPE_LENGTH (type));
10363 }
10364 else
10365 v = value_from_longest (type, value);
10366
3e43a32a
MS
10367 /* Specify decimal so that we do not depend on
10368 the radix. */
98bfdba5
PA
10369 get_formatted_print_options (&opts, 'd');
10370 opts.raw = 1;
d7e74731 10371 value_print (v, &buf, &opts);
98bfdba5 10372 release_value (v);
98bfdba5
PA
10373 }
10374 }
10375
10376 die->building_fullname = 0;
10377
10378 if (!first)
10379 {
10380 /* Close the argument list, with a space if necessary
10381 (nested templates). */
d7e74731
PA
10382 if (!buf.empty () && buf.string ().back () == '>')
10383 buf.puts (" >");
98bfdba5 10384 else
d7e74731 10385 buf.puts (">");
98bfdba5
PA
10386 }
10387 }
10388
9c37b5ae 10389 /* For C++ methods, append formal parameter type
94af9270 10390 information, if PHYSNAME. */
6e70227d 10391
94af9270 10392 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10393 && cu->language == language_cplus)
94af9270
KS
10394 {
10395 struct type *type = read_type_die (die, cu);
10396
d7e74731 10397 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10398 &type_print_raw_options);
94af9270 10399
9c37b5ae 10400 if (cu->language == language_cplus)
94af9270 10401 {
60430eff
DJ
10402 /* Assume that an artificial first parameter is
10403 "this", but do not crash if it is not. RealView
10404 marks unnamed (and thus unused) parameters as
10405 artificial; there is no way to differentiate
10406 the two cases. */
1f704f76 10407 if (type->num_fields () > 0
94af9270 10408 && TYPE_FIELD_ARTIFICIAL (type, 0)
78134374 10409 && TYPE_FIELD_TYPE (type, 0)->code () == TYPE_CODE_PTR
3e43a32a
MS
10410 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
10411 0))))
d7e74731 10412 buf.puts (" const");
94af9270
KS
10413 }
10414 }
10415
d7e74731 10416 const std::string &intermediate_name = buf.string ();
94af9270
KS
10417
10418 if (cu->language == language_cplus)
34a68019 10419 canonical_name
322a8516 10420 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10421 objfile);
34a68019
TT
10422
10423 /* If we only computed INTERMEDIATE_NAME, or if
10424 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10425 intern it. */
322a8516 10426 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10427 name = objfile->intern (intermediate_name);
34a68019
TT
10428 else
10429 name = canonical_name;
94af9270
KS
10430 }
10431 }
10432
10433 return name;
10434}
10435
0114d602
DJ
10436/* Return the fully qualified name of DIE, based on its DW_AT_name.
10437 If scope qualifiers are appropriate they will be added. The result
34a68019 10438 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10439 not have a name. NAME may either be from a previous call to
10440 dwarf2_name or NULL.
10441
9c37b5ae 10442 The output string will be canonicalized (if C++). */
0114d602
DJ
10443
10444static const char *
15d034d0 10445dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10446{
94af9270
KS
10447 return dwarf2_compute_name (name, die, cu, 0);
10448}
0114d602 10449
94af9270
KS
10450/* Construct a physname for the given DIE in CU. NAME may either be
10451 from a previous call to dwarf2_name or NULL. The result will be
10452 allocated on the objfile_objstack or NULL if the DIE does not have a
10453 name.
0114d602 10454
9c37b5ae 10455 The output string will be canonicalized (if C++). */
0114d602 10456
94af9270 10457static const char *
15d034d0 10458dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10459{
5e22e966 10460 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10461 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10462 int need_copy = 1;
10463
10464 /* In this case dwarf2_compute_name is just a shortcut not building anything
10465 on its own. */
10466 if (!die_needs_namespace (die, cu))
10467 return dwarf2_compute_name (name, die, cu, 1);
10468
906bb4c5
TT
10469 if (cu->language != language_rust)
10470 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10471
10472 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10473 has computed. */
791afaa2 10474 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10475 if (mangled != NULL)
900e11f9 10476 {
900e11f9 10477
59cc4834
JB
10478 if (language_def (cu->language)->la_store_sym_names_in_linkage_form_p)
10479 {
10480 /* Do nothing (do not demangle the symbol name). */
10481 }
10482 else if (cu->language == language_go)
a766d390 10483 {
5e2db402
TT
10484 /* This is a lie, but we already lie to the caller new_symbol.
10485 new_symbol assumes we return the mangled name.
a766d390 10486 This just undoes that lie until things are cleaned up. */
a766d390
DE
10487 }
10488 else
10489 {
0eb876f5
JB
10490 /* Use DMGL_RET_DROP for C++ template functions to suppress
10491 their return type. It is easier for GDB users to search
10492 for such functions as `name(params)' than `long name(params)'.
10493 In such case the minimal symbol names do not match the full
10494 symbol names but for template functions there is never a need
10495 to look up their definition from their declaration so
10496 the only disadvantage remains the minimal symbol variant
10497 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10498 demangled.reset (gdb_demangle (mangled,
10499 (DMGL_PARAMS | DMGL_ANSI
10500 | DMGL_RET_DROP)));
a766d390 10501 }
900e11f9 10502 if (demangled)
791afaa2 10503 canon = demangled.get ();
900e11f9
JK
10504 else
10505 {
10506 canon = mangled;
10507 need_copy = 0;
10508 }
10509 }
10510
10511 if (canon == NULL || check_physname)
10512 {
10513 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10514
10515 if (canon != NULL && strcmp (physname, canon) != 0)
10516 {
10517 /* It may not mean a bug in GDB. The compiler could also
10518 compute DW_AT_linkage_name incorrectly. But in such case
10519 GDB would need to be bug-to-bug compatible. */
10520
b98664d3 10521 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10522 "(from linkage <%s>) - DIE at %s [in module %s]"),
10523 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10524 objfile_name (objfile));
900e11f9
JK
10525
10526 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10527 is available here - over computed PHYSNAME. It is safer
10528 against both buggy GDB and buggy compilers. */
10529
10530 retval = canon;
10531 }
10532 else
10533 {
10534 retval = physname;
10535 need_copy = 0;
10536 }
10537 }
10538 else
10539 retval = canon;
10540
10541 if (need_copy)
be1e3d3e 10542 retval = objfile->intern (retval);
900e11f9 10543
900e11f9 10544 return retval;
0114d602
DJ
10545}
10546
74921315
KS
10547/* Inspect DIE in CU for a namespace alias. If one exists, record
10548 a new symbol for it.
10549
10550 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10551
10552static int
10553read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10554{
10555 struct attribute *attr;
10556
10557 /* If the die does not have a name, this is not a namespace
10558 alias. */
10559 attr = dwarf2_attr (die, DW_AT_name, cu);
10560 if (attr != NULL)
10561 {
10562 int num;
10563 struct die_info *d = die;
10564 struct dwarf2_cu *imported_cu = cu;
10565
10566 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10567 keep inspecting DIEs until we hit the underlying import. */
10568#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10569 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10570 {
10571 attr = dwarf2_attr (d, DW_AT_import, cu);
10572 if (attr == NULL)
10573 break;
10574
10575 d = follow_die_ref (d, attr, &imported_cu);
10576 if (d->tag != DW_TAG_imported_declaration)
10577 break;
10578 }
10579
10580 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10581 {
b98664d3 10582 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10583 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10584 return 0;
10585 }
10586
10587 if (attr != NULL)
10588 {
10589 struct type *type;
0826b30a 10590 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10591
aa66c379 10592 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10593 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10594 {
10595 /* This declaration is a global namespace alias. Add
10596 a symbol for it whose type is the aliased namespace. */
10597 new_symbol (die, type, cu);
10598 return 1;
10599 }
10600 }
10601 }
10602
10603 return 0;
10604}
10605
22cee43f 10606/* Return the using directives repository (global or local?) to use in the
804d2729 10607 current context for CU.
22cee43f
PMR
10608
10609 For Ada, imported declarations can materialize renamings, which *may* be
10610 global. However it is impossible (for now?) in DWARF to distinguish
10611 "external" imported declarations and "static" ones. As all imported
10612 declarations seem to be static in all other languages, make them all CU-wide
10613 global only in Ada. */
10614
10615static struct using_direct **
804d2729 10616using_directives (struct dwarf2_cu *cu)
22cee43f 10617{
c24bdb02
KS
10618 if (cu->language == language_ada
10619 && cu->get_builder ()->outermost_context_p ())
10620 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10621 else
c24bdb02 10622 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10623}
10624
27aa8d6a
SW
10625/* Read the import statement specified by the given die and record it. */
10626
10627static void
10628read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10629{
5e22e966 10630 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10631 struct attribute *import_attr;
32019081 10632 struct die_info *imported_die, *child_die;
de4affc9 10633 struct dwarf2_cu *imported_cu;
27aa8d6a 10634 const char *imported_name;
794684b6 10635 const char *imported_name_prefix;
13387711
SW
10636 const char *canonical_name;
10637 const char *import_alias;
10638 const char *imported_declaration = NULL;
794684b6 10639 const char *import_prefix;
eb1e02fd 10640 std::vector<const char *> excludes;
13387711 10641
27aa8d6a
SW
10642 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10643 if (import_attr == NULL)
10644 {
b98664d3 10645 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10646 dwarf_tag_name (die->tag));
10647 return;
10648 }
10649
de4affc9
CC
10650 imported_cu = cu;
10651 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10652 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10653 if (imported_name == NULL)
10654 {
10655 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10656
10657 The import in the following code:
10658 namespace A
10659 {
10660 typedef int B;
10661 }
10662
10663 int main ()
10664 {
10665 using A::B;
10666 B b;
10667 return b;
10668 }
10669
10670 ...
10671 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10672 <52> DW_AT_decl_file : 1
10673 <53> DW_AT_decl_line : 6
10674 <54> DW_AT_import : <0x75>
10675 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10676 <59> DW_AT_name : B
10677 <5b> DW_AT_decl_file : 1
10678 <5c> DW_AT_decl_line : 2
10679 <5d> DW_AT_type : <0x6e>
10680 ...
10681 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10682 <76> DW_AT_byte_size : 4
10683 <77> DW_AT_encoding : 5 (signed)
10684
10685 imports the wrong die ( 0x75 instead of 0x58 ).
10686 This case will be ignored until the gcc bug is fixed. */
10687 return;
10688 }
10689
82856980
SW
10690 /* Figure out the local name after import. */
10691 import_alias = dwarf2_name (die, cu);
27aa8d6a 10692
794684b6
SW
10693 /* Figure out where the statement is being imported to. */
10694 import_prefix = determine_prefix (die, cu);
10695
10696 /* Figure out what the scope of the imported die is and prepend it
10697 to the name of the imported die. */
de4affc9 10698 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10699
f55ee35c
JK
10700 if (imported_die->tag != DW_TAG_namespace
10701 && imported_die->tag != DW_TAG_module)
794684b6 10702 {
13387711
SW
10703 imported_declaration = imported_name;
10704 canonical_name = imported_name_prefix;
794684b6 10705 }
13387711 10706 else if (strlen (imported_name_prefix) > 0)
12aaed36 10707 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10708 imported_name_prefix,
10709 (cu->language == language_d ? "." : "::"),
10710 imported_name, (char *) NULL);
13387711
SW
10711 else
10712 canonical_name = imported_name;
794684b6 10713
32019081
JK
10714 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10715 for (child_die = die->child; child_die && child_die->tag;
436c571c 10716 child_die = child_die->sibling)
32019081
JK
10717 {
10718 /* DWARF-4: A Fortran use statement with a “rename list” may be
10719 represented by an imported module entry with an import attribute
10720 referring to the module and owned entries corresponding to those
10721 entities that are renamed as part of being imported. */
10722
10723 if (child_die->tag != DW_TAG_imported_declaration)
10724 {
b98664d3 10725 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10726 "- DIE at %s [in module %s]"),
10727 sect_offset_str (child_die->sect_off),
10728 objfile_name (objfile));
32019081
JK
10729 continue;
10730 }
10731
10732 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10733 if (import_attr == NULL)
10734 {
b98664d3 10735 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10736 dwarf_tag_name (child_die->tag));
10737 continue;
10738 }
10739
10740 imported_cu = cu;
10741 imported_die = follow_die_ref_or_sig (child_die, import_attr,
10742 &imported_cu);
10743 imported_name = dwarf2_name (imported_die, imported_cu);
10744 if (imported_name == NULL)
10745 {
b98664d3 10746 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
10747 "imported name - DIE at %s [in module %s]"),
10748 sect_offset_str (child_die->sect_off),
10749 objfile_name (objfile));
32019081
JK
10750 continue;
10751 }
10752
eb1e02fd 10753 excludes.push_back (imported_name);
32019081
JK
10754
10755 process_die (child_die, cu);
10756 }
10757
804d2729 10758 add_using_directive (using_directives (cu),
22cee43f
PMR
10759 import_prefix,
10760 canonical_name,
10761 import_alias,
10762 imported_declaration,
10763 excludes,
10764 0,
10765 &objfile->objfile_obstack);
27aa8d6a
SW
10766}
10767
5230b05a
WT
10768/* ICC<14 does not output the required DW_AT_declaration on incomplete
10769 types, but gives them a size of zero. Starting with version 14,
10770 ICC is compatible with GCC. */
10771
9068261f 10772static bool
5230b05a
WT
10773producer_is_icc_lt_14 (struct dwarf2_cu *cu)
10774{
10775 if (!cu->checked_producer)
10776 check_producer (cu);
10777
10778 return cu->producer_is_icc_lt_14;
10779}
10780
eb77c9df
AB
10781/* ICC generates a DW_AT_type for C void functions. This was observed on
10782 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
10783 which says that void functions should not have a DW_AT_type. */
10784
10785static bool
10786producer_is_icc (struct dwarf2_cu *cu)
10787{
10788 if (!cu->checked_producer)
10789 check_producer (cu);
10790
10791 return cu->producer_is_icc;
10792}
10793
1b80a9fa
JK
10794/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
10795 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
10796 this, it was first present in GCC release 4.3.0. */
10797
9068261f 10798static bool
1b80a9fa
JK
10799producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
10800{
10801 if (!cu->checked_producer)
10802 check_producer (cu);
10803
10804 return cu->producer_is_gcc_lt_4_3;
10805}
10806
d721ba37
PA
10807static file_and_directory
10808find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 10809{
d721ba37
PA
10810 file_and_directory res;
10811
9291a0cd
TT
10812 /* Find the filename. Do not use dwarf2_name here, since the filename
10813 is not a source language identifier. */
d721ba37
PA
10814 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
10815 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 10816
d721ba37
PA
10817 if (res.comp_dir == NULL
10818 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
10819 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 10820 {
d721ba37
PA
10821 res.comp_dir_storage = ldirname (res.name);
10822 if (!res.comp_dir_storage.empty ())
10823 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 10824 }
d721ba37 10825 if (res.comp_dir != NULL)
9291a0cd
TT
10826 {
10827 /* Irix 6.2 native cc prepends <machine>.: to the compilation
10828 directory, get rid of it. */
d721ba37 10829 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 10830
d721ba37
PA
10831 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
10832 res.comp_dir = cp + 1;
9291a0cd
TT
10833 }
10834
d721ba37
PA
10835 if (res.name == NULL)
10836 res.name = "<unknown>";
10837
10838 return res;
9291a0cd
TT
10839}
10840
f4dc4d17
DE
10841/* Handle DW_AT_stmt_list for a compilation unit.
10842 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
10843 COMP_DIR is the compilation directory. LOWPC is passed to
10844 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
10845
10846static void
10847handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 10848 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 10849{
5e22e966 10850 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
2ab95328 10851 struct attribute *attr;
527f3840
JK
10852 struct line_header line_header_local;
10853 hashval_t line_header_local_hash;
527f3840
JK
10854 void **slot;
10855 int decode_mapping;
2ab95328 10856
f4dc4d17
DE
10857 gdb_assert (! cu->per_cu->is_debug_types);
10858
2ab95328 10859 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
527f3840
JK
10860 if (attr == NULL)
10861 return;
10862
9c541725 10863 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
527f3840
JK
10864
10865 /* The line header hash table is only created if needed (it exists to
10866 prevent redundant reading of the line table for partial_units).
10867 If we're given a partial_unit, we'll need it. If we're given a
10868 compile_unit, then use the line header hash table if it's already
10869 created, but don't create one just yet. */
10870
5989a64e 10871 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL
527f3840 10872 && die->tag == DW_TAG_partial_unit)
2ab95328 10873 {
5989a64e 10874 dwarf2_per_objfile->per_bfd->line_header_hash
d15acc42
TT
10875 .reset (htab_create_alloc (127, line_header_hash_voidp,
10876 line_header_eq_voidp,
10877 free_line_header_voidp,
10878 xcalloc, xfree));
527f3840 10879 }
2ab95328 10880
9c541725 10881 line_header_local.sect_off = line_offset;
527f3840
JK
10882 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
10883 line_header_local_hash = line_header_hash (&line_header_local);
5989a64e 10884 if (dwarf2_per_objfile->per_bfd->line_header_hash != NULL)
527f3840 10885 {
5989a64e 10886 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10887 &line_header_local,
10888 line_header_local_hash, NO_INSERT);
10889
10890 /* For DW_TAG_compile_unit we need info like symtab::linetable which
10891 is not present in *SLOT (since if there is something in *SLOT then
10892 it will be for a partial_unit). */
10893 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 10894 {
527f3840 10895 gdb_assert (*slot != NULL);
9a3c8263 10896 cu->line_header = (struct line_header *) *slot;
527f3840 10897 return;
dee91e82 10898 }
2ab95328 10899 }
527f3840
JK
10900
10901 /* dwarf_decode_line_header does not yet provide sufficient information.
10902 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
10903 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
10904 if (lh == NULL)
527f3840 10905 return;
4c8aa72d
PA
10906
10907 cu->line_header = lh.release ();
10908 cu->line_header_die_owner = die;
527f3840 10909
5989a64e 10910 if (dwarf2_per_objfile->per_bfd->line_header_hash == NULL)
527f3840
JK
10911 slot = NULL;
10912 else
10913 {
5989a64e 10914 slot = htab_find_slot_with_hash (dwarf2_per_objfile->per_bfd->line_header_hash.get (),
527f3840
JK
10915 &line_header_local,
10916 line_header_local_hash, INSERT);
10917 gdb_assert (slot != NULL);
10918 }
10919 if (slot != NULL && *slot == NULL)
10920 {
10921 /* This newly decoded line number information unit will be owned
10922 by line_header_hash hash table. */
10923 *slot = cu->line_header;
4c8aa72d 10924 cu->line_header_die_owner = NULL;
527f3840
JK
10925 }
10926 else
10927 {
10928 /* We cannot free any current entry in (*slot) as that struct line_header
10929 may be already used by multiple CUs. Create only temporary decoded
10930 line_header for this CU - it may happen at most once for each line
10931 number information unit. And if we're not using line_header_hash
10932 then this is what we want as well. */
10933 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
10934 }
10935 decode_mapping = (die->tag != DW_TAG_partial_unit);
10936 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
10937 decode_mapping);
fff8551c 10938
2ab95328
TT
10939}
10940
95554aad 10941/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 10942
c906108c 10943static void
e7c27a73 10944read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10945{
5e22e966 10946 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 10947 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 10948 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 10949 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
10950 CORE_ADDR highpc = ((CORE_ADDR) 0);
10951 struct attribute *attr;
c906108c 10952 struct die_info *child_die;
e142c38c 10953 CORE_ADDR baseaddr;
6e70227d 10954
380618d6 10955 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 10956 baseaddr = objfile->text_section_offset ();
c906108c 10957
fae299cd 10958 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
10959
10960 /* If we didn't find a lowpc, set it to highpc to avoid complaints
10961 from finish_block. */
2acceee2 10962 if (lowpc == ((CORE_ADDR) -1))
c906108c 10963 lowpc = highpc;
3e29f34a 10964 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 10965
d721ba37 10966 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 10967
f4b8a18d
KW
10968 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
10969 standardised yet. As a workaround for the language detection we fall
10970 back to the DW_AT_producer string. */
10971 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
10972 cu->language = language_opencl;
10973
3019eac3
DE
10974 /* Similar hack for Go. */
10975 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
10976 set_cu_language (DW_LANG_Go, cu);
10977
c24bdb02 10978 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
10979
10980 /* Decode line number information if present. We do this before
10981 processing child DIEs, so that the line header table is available
10982 for DW_AT_decl_file. */
d721ba37 10983 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
10984
10985 /* Process all dies in compilation unit. */
10986 if (die->child != NULL)
10987 {
10988 child_die = die->child;
10989 while (child_die && child_die->tag)
10990 {
10991 process_die (child_die, cu);
436c571c 10992 child_die = child_die->sibling;
3019eac3
DE
10993 }
10994 }
10995
10996 /* Decode macro information, if present. Dwarf 2 macro information
10997 refers to information in the line number info statement program
10998 header, so we can only read it if we've read the header
10999 successfully. */
0af92d60
JK
11000 attr = dwarf2_attr (die, DW_AT_macros, cu);
11001 if (attr == NULL)
11002 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
3019eac3
DE
11003 if (attr && cu->line_header)
11004 {
11005 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11006 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11007
43f3e411 11008 dwarf_decode_macros (cu, DW_UNSND (attr), 1);
3019eac3
DE
11009 }
11010 else
11011 {
11012 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
11013 if (attr && cu->line_header)
11014 {
11015 unsigned int macro_offset = DW_UNSND (attr);
11016
43f3e411 11017 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11018 }
11019 }
3019eac3
DE
11020}
11021
c24bdb02
KS
11022void
11023dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11024{
f4dc4d17
DE
11025 struct type_unit_group *tu_group;
11026 int first_time;
3019eac3 11027 struct attribute *attr;
9c541725 11028 unsigned int i;
0186c6a7 11029 struct signatured_type *sig_type;
3019eac3 11030
f4dc4d17 11031 gdb_assert (per_cu->is_debug_types);
0186c6a7 11032 sig_type = (struct signatured_type *) per_cu;
3019eac3 11033
c24bdb02 11034 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11035
f4dc4d17 11036 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11037 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11038 if (sig_type->type_unit_group == NULL)
c24bdb02 11039 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11040 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11041
11042 /* If we've already processed this stmt_list there's no real need to
11043 do it again, we could fake it and just recreate the part we need
11044 (file name,index -> symtab mapping). If data shows this optimization
11045 is useful we can do it then. */
8adb8487
TT
11046 type_unit_group_unshareable *tug_unshare
11047 = per_objfile->get_type_unit_group_unshareable (tu_group);
11048 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11049
11050 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11051 debug info. */
fff8551c 11052 line_header_up lh;
f4dc4d17 11053 if (attr != NULL)
3019eac3 11054 {
9c541725 11055 sect_offset line_offset = (sect_offset) DW_UNSND (attr);
c24bdb02 11056 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11057 }
11058 if (lh == NULL)
11059 {
11060 if (first_time)
c24bdb02 11061 start_symtab ("", NULL, 0);
f4dc4d17
DE
11062 else
11063 {
8adb8487 11064 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11065 gdb_assert (m_builder == nullptr);
8adb8487 11066 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11067 m_builder.reset (new struct buildsym_compunit
11068 (COMPUNIT_OBJFILE (cust), "",
11069 COMPUNIT_DIRNAME (cust),
11070 compunit_language (cust),
11071 0, cust));
770479f2 11072 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11073 }
f4dc4d17 11074 return;
3019eac3
DE
11075 }
11076
c24bdb02
KS
11077 line_header = lh.release ();
11078 line_header_die_owner = die;
3019eac3 11079
f4dc4d17
DE
11080 if (first_time)
11081 {
c24bdb02 11082 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11083
1fd60fc0
DE
11084 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11085 still initializing it, and our caller (a few levels up)
11086 process_full_type_unit still needs to know if this is the first
11087 time. */
11088
8adb8487 11089 tug_unshare->symtabs
4ac93832
TT
11090 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11091 struct symtab *, line_header->file_names_size ());
3019eac3 11092
7ba99d21
AT
11093 auto &file_names = line_header->file_names ();
11094 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11095 {
7ba99d21 11096 file_entry &fe = file_names[i];
c24bdb02
KS
11097 dwarf2_start_subfile (this, fe.name,
11098 fe.include_dir (line_header));
11099 buildsym_compunit *b = get_builder ();
11100 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11101 {
4c8aa72d
PA
11102 /* NOTE: start_subfile will recognize when it's been
11103 passed a file it has already seen. So we can't
11104 assume there's a simple mapping from
11105 cu->line_header->file_names to subfiles, plus
11106 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11107 b->get_current_subfile ()->symtab
11108 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11109 }
11110
c24bdb02 11111 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11112 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11113 }
11114 }
11115 else
3019eac3 11116 {
c24bdb02 11117 gdb_assert (m_builder == nullptr);
8adb8487 11118 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11119 m_builder.reset (new struct buildsym_compunit
11120 (COMPUNIT_OBJFILE (cust), "",
11121 COMPUNIT_DIRNAME (cust),
11122 compunit_language (cust),
11123 0, cust));
770479f2 11124 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11125
7ba99d21
AT
11126 auto &file_names = line_header->file_names ();
11127 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11128 {
7ba99d21 11129 file_entry &fe = file_names[i];
8adb8487 11130 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11131 }
3019eac3
DE
11132 }
11133
f4dc4d17
DE
11134 /* The main symtab is allocated last. Type units don't have DW_AT_name
11135 so they don't have a "real" (so to speak) symtab anyway.
11136 There is later code that will assign the main symtab to all symbols
11137 that don't have one. We need to handle the case of a symbol with a
11138 missing symtab (DW_AT_decl_file) anyway. */
11139}
3019eac3 11140
f4dc4d17
DE
11141/* Process DW_TAG_type_unit.
11142 For TUs we want to skip the first top level sibling if it's not the
11143 actual type being defined by this TU. In this case the first top
11144 level sibling is there to provide context only. */
3019eac3 11145
f4dc4d17
DE
11146static void
11147read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11148{
11149 struct die_info *child_die;
3019eac3 11150
f4dc4d17
DE
11151 prepare_one_comp_unit (cu, die, language_minimal);
11152
11153 /* Initialize (or reinitialize) the machinery for building symtabs.
11154 We do this before processing child DIEs, so that the line header table
11155 is available for DW_AT_decl_file. */
c24bdb02 11156 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11157
11158 if (die->child != NULL)
11159 {
11160 child_die = die->child;
11161 while (child_die && child_die->tag)
11162 {
11163 process_die (child_die, cu);
436c571c 11164 child_die = child_die->sibling;
f4dc4d17
DE
11165 }
11166 }
3019eac3
DE
11167}
11168\f
80626a55
DE
11169/* DWO/DWP files.
11170
11171 http://gcc.gnu.org/wiki/DebugFission
11172 http://gcc.gnu.org/wiki/DebugFissionDWP
11173
11174 To simplify handling of both DWO files ("object" files with the DWARF info)
11175 and DWP files (a file with the DWOs packaged up into one file), we treat
11176 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11177
11178static hashval_t
11179hash_dwo_file (const void *item)
11180{
9a3c8263 11181 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11182 hashval_t hash;
3019eac3 11183
a2ce51a0
DE
11184 hash = htab_hash_string (dwo_file->dwo_name);
11185 if (dwo_file->comp_dir != NULL)
11186 hash += htab_hash_string (dwo_file->comp_dir);
11187 return hash;
3019eac3
DE
11188}
11189
11190static int
11191eq_dwo_file (const void *item_lhs, const void *item_rhs)
11192{
9a3c8263
SM
11193 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11194 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11195
a2ce51a0
DE
11196 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11197 return 0;
11198 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11199 return lhs->comp_dir == rhs->comp_dir;
11200 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11201}
11202
11203/* Allocate a hash table for DWO files. */
11204
51ac9db5 11205static htab_up
298e9637 11206allocate_dwo_file_hash_table ()
3019eac3 11207{
51ac9db5
SM
11208 auto delete_dwo_file = [] (void *item)
11209 {
11210 struct dwo_file *dwo_file = (struct dwo_file *) item;
11211
11212 delete dwo_file;
11213 };
11214
bc68fb19
TT
11215 return htab_up (htab_create_alloc (41,
11216 hash_dwo_file,
11217 eq_dwo_file,
11218 delete_dwo_file,
11219 xcalloc, xfree));
3019eac3
DE
11220}
11221
80626a55
DE
11222/* Lookup DWO file DWO_NAME. */
11223
11224static void **
ed2dc618
SM
11225lookup_dwo_file_slot (struct dwarf2_per_objfile *dwarf2_per_objfile,
11226 const char *dwo_name,
11227 const char *comp_dir)
80626a55
DE
11228{
11229 struct dwo_file find_entry;
11230 void **slot;
11231
5989a64e
SM
11232 if (dwarf2_per_objfile->per_bfd->dwo_files == NULL)
11233 dwarf2_per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11234
0ac5b59e
DE
11235 find_entry.dwo_name = dwo_name;
11236 find_entry.comp_dir = comp_dir;
5989a64e 11237 slot = htab_find_slot (dwarf2_per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11238 INSERT);
80626a55
DE
11239
11240 return slot;
11241}
11242
3019eac3
DE
11243static hashval_t
11244hash_dwo_unit (const void *item)
11245{
9a3c8263 11246 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11247
11248 /* This drops the top 32 bits of the id, but is ok for a hash. */
11249 return dwo_unit->signature;
11250}
11251
11252static int
11253eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11254{
9a3c8263
SM
11255 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11256 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11257
11258 /* The signature is assumed to be unique within the DWO file.
11259 So while object file CU dwo_id's always have the value zero,
11260 that's OK, assuming each object file DWO file has only one CU,
11261 and that's the rule for now. */
11262 return lhs->signature == rhs->signature;
11263}
11264
11265/* Allocate a hash table for DWO CUs,TUs.
11266 There is one of these tables for each of CUs,TUs for each DWO file. */
11267
b0b6a987 11268static htab_up
298e9637 11269allocate_dwo_unit_table ()
3019eac3
DE
11270{
11271 /* Start out with a pretty small number.
11272 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11273 return htab_up (htab_create_alloc (3,
11274 hash_dwo_unit,
11275 eq_dwo_unit,
11276 NULL, xcalloc, xfree));
3019eac3
DE
11277}
11278
19c3d4c9 11279/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11280
11281static void
19c3d4c9
DE
11282create_dwo_cu_reader (const struct die_reader_specs *reader,
11283 const gdb_byte *info_ptr,
11284 struct die_info *comp_unit_die,
c0ab21c2
TT
11285 struct dwo_file *dwo_file,
11286 struct dwo_unit *dwo_unit)
3019eac3
DE
11287{
11288 struct dwarf2_cu *cu = reader->cu;
9c541725 11289 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11290 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11291
a084a2a6
AT
11292 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11293 if (!signature.has_value ())
3019eac3 11294 {
b98664d3 11295 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11296 " its dwo_id [in module %s]"),
9d8780f0 11297 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11298 return;
11299 }
11300
3019eac3 11301 dwo_unit->dwo_file = dwo_file;
a084a2a6 11302 dwo_unit->signature = *signature;
8a0459fd 11303 dwo_unit->section = section;
9c541725 11304 dwo_unit->sect_off = sect_off;
3019eac3
DE
11305 dwo_unit->length = cu->per_cu->length;
11306
b4f54984 11307 if (dwarf_read_debug)
9d8780f0
SM
11308 fprintf_unfiltered (gdb_stdlog, " offset %s, dwo_id %s\n",
11309 sect_offset_str (sect_off),
9c541725 11310 hex_string (dwo_unit->signature));
3019eac3
DE
11311}
11312
33c5cd75 11313/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11314 Note: This function processes DWO files only, not DWP files. */
3019eac3 11315
33c5cd75 11316static void
ed2dc618 11317create_cus_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e 11318 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11319 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3
DE
11320{
11321 struct objfile *objfile = dwarf2_per_objfile->objfile;
1859c670 11322 dwarf2_per_bfd *per_bfd = dwarf2_per_objfile->per_bfd;
d521ce57 11323 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11324
96b79293 11325 section.read (objfile);
33c5cd75 11326 info_ptr = section.buffer;
3019eac3
DE
11327
11328 if (info_ptr == NULL)
33c5cd75 11329 return;
3019eac3 11330
b4f54984 11331 if (dwarf_read_debug)
19c3d4c9
DE
11332 {
11333 fprintf_unfiltered (gdb_stdlog, "Reading %s for %s:\n",
96b79293
TT
11334 section.get_name (),
11335 section.get_file_name ());
19c3d4c9 11336 }
3019eac3 11337
33c5cd75 11338 end_ptr = info_ptr + section.size;
3019eac3
DE
11339 while (info_ptr < end_ptr)
11340 {
11341 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11342 struct dwo_unit read_unit {};
33c5cd75
DB
11343 struct dwo_unit *dwo_unit;
11344 void **slot;
11345 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11346
11347 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11348 per_cu.per_bfd = per_bfd;
3019eac3 11349 per_cu.is_debug_types = 0;
33c5cd75
DB
11350 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11351 per_cu.section = &section;
11352
ab432490 11353 cutu_reader reader (&per_cu, dwarf2_per_objfile, cu, &dwo_file);
c0ab21c2
TT
11354 if (!reader.dummy_p)
11355 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11356 &dwo_file, &read_unit);
33c5cd75
DB
11357 info_ptr += per_cu.length;
11358
11359 // If the unit could not be parsed, skip it.
c0ab21c2 11360 if (read_unit.dwo_file == NULL)
33c5cd75 11361 continue;
3019eac3 11362
33c5cd75 11363 if (cus_htab == NULL)
298e9637 11364 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11365
1859c670 11366 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11367 struct dwo_unit);
c0ab21c2 11368 *dwo_unit = read_unit;
b0b6a987 11369 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11370 gdb_assert (slot != NULL);
11371 if (*slot != NULL)
19c3d4c9 11372 {
33c5cd75
DB
11373 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11374 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11375
b98664d3 11376 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11377 " the entry at offset %s, signature %s"),
11378 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11379 hex_string (dwo_unit->signature));
19c3d4c9 11380 }
33c5cd75 11381 *slot = (void *)dwo_unit;
3019eac3 11382 }
3019eac3
DE
11383}
11384
80626a55
DE
11385/* DWP file .debug_{cu,tu}_index section format:
11386 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
11387
d2415c6c
DE
11388 DWP Version 1:
11389
80626a55
DE
11390 Both index sections have the same format, and serve to map a 64-bit
11391 signature to a set of section numbers. Each section begins with a header,
11392 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11393 indexes, and a pool of 32-bit section numbers. The index sections will be
11394 aligned at 8-byte boundaries in the file.
11395
d2415c6c
DE
11396 The index section header consists of:
11397
11398 V, 32 bit version number
11399 -, 32 bits unused
11400 N, 32 bit number of compilation units or type units in the index
11401 M, 32 bit number of slots in the hash table
80626a55 11402
d2415c6c 11403 Numbers are recorded using the byte order of the application binary.
80626a55 11404
d2415c6c
DE
11405 The hash table begins at offset 16 in the section, and consists of an array
11406 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11407 order of the application binary). Unused slots in the hash table are 0.
11408 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11409
d2415c6c
DE
11410 The parallel table begins immediately after the hash table
11411 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11412 array of 32-bit indexes (using the byte order of the application binary),
11413 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11414 table contains a 32-bit index into the pool of section numbers. For unused
11415 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11416
73869dc2
DE
11417 The pool of section numbers begins immediately following the hash table
11418 (at offset 16 + 12 * M from the beginning of the section). The pool of
11419 section numbers consists of an array of 32-bit words (using the byte order
11420 of the application binary). Each item in the array is indexed starting
11421 from 0. The hash table entry provides the index of the first section
11422 number in the set. Additional section numbers in the set follow, and the
11423 set is terminated by a 0 entry (section number 0 is not used in ELF).
11424
11425 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11426 section must be the first entry in the set, and the .debug_abbrev.dwo must
11427 be the second entry. Other members of the set may follow in any order.
11428
11429 ---
11430
11431 DWP Version 2:
11432
11433 DWP Version 2 combines all the .debug_info, etc. sections into one,
11434 and the entries in the index tables are now offsets into these sections.
11435 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11436 section.
11437
11438 Index Section Contents:
11439 Header
11440 Hash Table of Signatures dwp_hash_table.hash_table
11441 Parallel Table of Indices dwp_hash_table.unit_table
11442 Table of Section Offsets dwp_hash_table.v2.{section_ids,offsets}
11443 Table of Section Sizes dwp_hash_table.v2.sizes
11444
11445 The index section header consists of:
11446
11447 V, 32 bit version number
11448 L, 32 bit number of columns in the table of section offsets
11449 N, 32 bit number of compilation units or type units in the index
11450 M, 32 bit number of slots in the hash table
11451
11452 Numbers are recorded using the byte order of the application binary.
11453
11454 The hash table has the same format as version 1.
11455 The parallel table of indices has the same format as version 1,
11456 except that the entries are origin-1 indices into the table of sections
11457 offsets and the table of section sizes.
11458
11459 The table of offsets begins immediately following the parallel table
11460 (at offset 16 + 12 * M from the beginning of the section). The table is
11461 a two-dimensional array of 32-bit words (using the byte order of the
11462 application binary), with L columns and N+1 rows, in row-major order.
11463 Each row in the array is indexed starting from 0. The first row provides
11464 a key to the remaining rows: each column in this row provides an identifier
11465 for a debug section, and the offsets in the same column of subsequent rows
11466 refer to that section. The section identifiers are:
11467
11468 DW_SECT_INFO 1 .debug_info.dwo
11469 DW_SECT_TYPES 2 .debug_types.dwo
11470 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11471 DW_SECT_LINE 4 .debug_line.dwo
11472 DW_SECT_LOC 5 .debug_loc.dwo
11473 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11474 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11475 DW_SECT_MACRO 8 .debug_macro.dwo
11476
11477 The offsets provided by the CU and TU index sections are the base offsets
11478 for the contributions made by each CU or TU to the corresponding section
11479 in the package file. Each CU and TU header contains an abbrev_offset
11480 field, used to find the abbreviations table for that CU or TU within the
11481 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11482 be interpreted as relative to the base offset given in the index section.
11483 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11484 should be interpreted as relative to the base offset for .debug_line.dwo,
11485 and offsets into other debug sections obtained from DWARF attributes should
11486 also be interpreted as relative to the corresponding base offset.
11487
11488 The table of sizes begins immediately following the table of offsets.
11489 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11490 with L columns and N rows, in row-major order. Each row in the array is
11491 indexed starting from 1 (row 0 is shared by the two tables).
11492
11493 ---
11494
11495 Hash table lookup is handled the same in version 1 and 2:
11496
11497 We assume that N and M will not exceed 2^32 - 1.
11498 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11499
d2415c6c
DE
11500 Given a 64-bit compilation unit signature or a type signature S, an entry
11501 in the hash table is located as follows:
80626a55 11502
d2415c6c
DE
11503 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11504 the low-order k bits all set to 1.
80626a55 11505
d2415c6c 11506 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11507
d2415c6c
DE
11508 3) If the hash table entry at index H matches the signature, use that
11509 entry. If the hash table entry at index H is unused (all zeroes),
11510 terminate the search: the signature is not present in the table.
80626a55 11511
d2415c6c 11512 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11513
d2415c6c 11514 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11515 to stop at an unused slot or find the match. */
80626a55
DE
11516
11517/* Create a hash table to map DWO IDs to their CU/TU entry in
11518 .debug_{info,types}.dwo in DWP_FILE.
11519 Returns NULL if there isn't one.
11520 Note: This function processes DWP files only, not DWO files. */
11521
11522static struct dwp_hash_table *
ed2dc618
SM
11523create_dwp_hash_table (struct dwarf2_per_objfile *dwarf2_per_objfile,
11524 struct dwp_file *dwp_file, int is_debug_types)
80626a55
DE
11525{
11526 struct objfile *objfile = dwarf2_per_objfile->objfile;
400174b1 11527 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11528 const gdb_byte *index_ptr, *index_end;
80626a55 11529 struct dwarf2_section_info *index;
73869dc2 11530 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11531 struct dwp_hash_table *htab;
11532
11533 if (is_debug_types)
11534 index = &dwp_file->sections.tu_index;
11535 else
11536 index = &dwp_file->sections.cu_index;
11537
96b79293 11538 if (index->empty ())
80626a55 11539 return NULL;
96b79293 11540 index->read (objfile);
80626a55
DE
11541
11542 index_ptr = index->buffer;
11543 index_end = index_ptr + index->size;
11544
11545 version = read_4_bytes (dbfd, index_ptr);
73869dc2
DE
11546 index_ptr += 4;
11547 if (version == 2)
11548 nr_columns = read_4_bytes (dbfd, index_ptr);
11549 else
11550 nr_columns = 0;
11551 index_ptr += 4;
80626a55
DE
11552 nr_units = read_4_bytes (dbfd, index_ptr);
11553 index_ptr += 4;
11554 nr_slots = read_4_bytes (dbfd, index_ptr);
11555 index_ptr += 4;
11556
73869dc2 11557 if (version != 1 && version != 2)
80626a55 11558 {
21aa081e 11559 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11560 " [in module %s]"),
21aa081e 11561 pulongest (version), dwp_file->name);
80626a55
DE
11562 }
11563 if (nr_slots != (nr_slots & -nr_slots))
11564 {
21aa081e 11565 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11566 " is not power of 2 [in module %s]"),
21aa081e 11567 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11568 }
11569
5989a64e 11570 htab = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11571 htab->version = version;
11572 htab->nr_columns = nr_columns;
80626a55
DE
11573 htab->nr_units = nr_units;
11574 htab->nr_slots = nr_slots;
11575 htab->hash_table = index_ptr;
11576 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11577
11578 /* Exit early if the table is empty. */
11579 if (nr_slots == 0 || nr_units == 0
11580 || (version == 2 && nr_columns == 0))
11581 {
11582 /* All must be zero. */
11583 if (nr_slots != 0 || nr_units != 0
11584 || (version == 2 && nr_columns != 0))
11585 {
b98664d3 11586 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11587 " all zero [in modules %s]"),
11588 dwp_file->name);
11589 }
11590 return htab;
11591 }
11592
11593 if (version == 1)
11594 {
11595 htab->section_pool.v1.indices =
11596 htab->unit_table + sizeof (uint32_t) * nr_slots;
11597 /* It's harder to decide whether the section is too small in v1.
11598 V1 is deprecated anyway so we punt. */
11599 }
11600 else
11601 {
11602 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11603 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11604 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11605 /* Reverse map for error checking. */
11606 int ids_seen[DW_SECT_MAX + 1];
11607 int i;
11608
11609 if (nr_columns < 2)
11610 {
11611 error (_("Dwarf Error: bad DWP hash table, too few columns"
11612 " in section table [in module %s]"),
11613 dwp_file->name);
11614 }
11615 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11616 {
11617 error (_("Dwarf Error: bad DWP hash table, too many columns"
11618 " in section table [in module %s]"),
11619 dwp_file->name);
11620 }
04fd5eed
GB
11621 memset (ids, 255, sizeof_ids);
11622 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11623 for (i = 0; i < nr_columns; ++i)
11624 {
11625 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11626
11627 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11628 {
11629 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11630 " in section table [in module %s]"),
11631 id, dwp_file->name);
11632 }
11633 if (ids_seen[id] != -1)
11634 {
11635 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11636 " id %d in section table [in module %s]"),
11637 id, dwp_file->name);
11638 }
11639 ids_seen[id] = i;
11640 ids[i] = id;
11641 }
11642 /* Must have exactly one info or types section. */
11643 if (((ids_seen[DW_SECT_INFO] != -1)
11644 + (ids_seen[DW_SECT_TYPES] != -1))
11645 != 1)
11646 {
11647 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11648 " DWO info/types section [in module %s]"),
11649 dwp_file->name);
11650 }
11651 /* Must have an abbrev section. */
11652 if (ids_seen[DW_SECT_ABBREV] == -1)
11653 {
11654 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11655 " section [in module %s]"),
11656 dwp_file->name);
11657 }
11658 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11659 htab->section_pool.v2.sizes =
11660 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11661 * nr_units * nr_columns);
11662 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11663 * nr_units * nr_columns))
11664 > index_end)
11665 {
11666 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11667 " [in module %s]"),
11668 dwp_file->name);
11669 }
11670 }
80626a55
DE
11671
11672 return htab;
11673}
11674
11675/* Update SECTIONS with the data from SECTP.
11676
11677 This function is like the other "locate" section routines that are
11678 passed to bfd_map_over_sections, but in this context the sections to
73869dc2 11679 read comes from the DWP V1 hash table, not the full ELF section table.
80626a55
DE
11680
11681 The result is non-zero for success, or zero if an error was found. */
11682
11683static int
73869dc2
DE
11684locate_v1_virtual_dwo_sections (asection *sectp,
11685 struct virtual_v1_dwo_sections *sections)
80626a55
DE
11686{
11687 const struct dwop_section_names *names = &dwop_section_names;
11688
11689 if (section_is_p (sectp->name, &names->abbrev_dwo))
11690 {
11691 /* There can be only one. */
049412e3 11692 if (sections->abbrev.s.section != NULL)
80626a55 11693 return 0;
049412e3 11694 sections->abbrev.s.section = sectp;
fd361982 11695 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
11696 }
11697 else if (section_is_p (sectp->name, &names->info_dwo)
11698 || section_is_p (sectp->name, &names->types_dwo))
11699 {
11700 /* There can be only one. */
049412e3 11701 if (sections->info_or_types.s.section != NULL)
80626a55 11702 return 0;
049412e3 11703 sections->info_or_types.s.section = sectp;
fd361982 11704 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
11705 }
11706 else if (section_is_p (sectp->name, &names->line_dwo))
11707 {
11708 /* There can be only one. */
049412e3 11709 if (sections->line.s.section != NULL)
80626a55 11710 return 0;
049412e3 11711 sections->line.s.section = sectp;
fd361982 11712 sections->line.size = bfd_section_size (sectp);
80626a55
DE
11713 }
11714 else if (section_is_p (sectp->name, &names->loc_dwo))
11715 {
11716 /* There can be only one. */
049412e3 11717 if (sections->loc.s.section != NULL)
80626a55 11718 return 0;
049412e3 11719 sections->loc.s.section = sectp;
fd361982 11720 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
11721 }
11722 else if (section_is_p (sectp->name, &names->macinfo_dwo))
11723 {
11724 /* There can be only one. */
049412e3 11725 if (sections->macinfo.s.section != NULL)
80626a55 11726 return 0;
049412e3 11727 sections->macinfo.s.section = sectp;
fd361982 11728 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
11729 }
11730 else if (section_is_p (sectp->name, &names->macro_dwo))
11731 {
11732 /* There can be only one. */
049412e3 11733 if (sections->macro.s.section != NULL)
80626a55 11734 return 0;
049412e3 11735 sections->macro.s.section = sectp;
fd361982 11736 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
11737 }
11738 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
11739 {
11740 /* There can be only one. */
049412e3 11741 if (sections->str_offsets.s.section != NULL)
80626a55 11742 return 0;
049412e3 11743 sections->str_offsets.s.section = sectp;
fd361982 11744 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
11745 }
11746 else
11747 {
11748 /* No other kind of section is valid. */
11749 return 0;
11750 }
11751
11752 return 1;
11753}
11754
73869dc2
DE
11755/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11756 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11757 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11758 This is for DWP version 1 files. */
80626a55
DE
11759
11760static struct dwo_unit *
ed2dc618
SM
11761create_dwo_unit_in_dwp_v1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11762 struct dwp_file *dwp_file,
73869dc2
DE
11763 uint32_t unit_index,
11764 const char *comp_dir,
11765 ULONGEST signature, int is_debug_types)
80626a55 11766{
73869dc2
DE
11767 const struct dwp_hash_table *dwp_htab =
11768 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11769 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
11770 const char *kind = is_debug_types ? "TU" : "CU";
11771 struct dwo_file *dwo_file;
11772 struct dwo_unit *dwo_unit;
73869dc2 11773 struct virtual_v1_dwo_sections sections;
80626a55 11774 void **dwo_file_slot;
80626a55
DE
11775 int i;
11776
73869dc2
DE
11777 gdb_assert (dwp_file->version == 1);
11778
b4f54984 11779 if (dwarf_read_debug)
80626a55 11780 {
73869dc2 11781 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V1 file: %s\n",
80626a55 11782 kind,
73869dc2 11783 pulongest (unit_index), hex_string (signature),
80626a55
DE
11784 dwp_file->name);
11785 }
11786
19ac8c2e 11787 /* Fetch the sections of this DWO unit.
80626a55
DE
11788 Put a limit on the number of sections we look for so that bad data
11789 doesn't cause us to loop forever. */
11790
73869dc2 11791#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
11792 (1 /* .debug_info or .debug_types */ \
11793 + 1 /* .debug_abbrev */ \
11794 + 1 /* .debug_line */ \
11795 + 1 /* .debug_loc */ \
11796 + 1 /* .debug_str_offsets */ \
19ac8c2e 11797 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
11798 + 1 /* trailing zero */)
11799
11800 memset (&sections, 0, sizeof (sections));
80626a55 11801
73869dc2 11802 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
11803 {
11804 asection *sectp;
11805 uint32_t section_nr =
11806 read_4_bytes (dbfd,
73869dc2
DE
11807 dwp_htab->section_pool.v1.indices
11808 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
11809
11810 if (section_nr == 0)
11811 break;
11812 if (section_nr >= dwp_file->num_sections)
11813 {
11814 error (_("Dwarf Error: bad DWP hash table, section number too large"
11815 " [in module %s]"),
11816 dwp_file->name);
11817 }
11818
11819 sectp = dwp_file->elf_sections[section_nr];
73869dc2 11820 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
11821 {
11822 error (_("Dwarf Error: bad DWP hash table, invalid section found"
11823 " [in module %s]"),
11824 dwp_file->name);
11825 }
11826 }
11827
11828 if (i < 2
96b79293
TT
11829 || sections.info_or_types.empty ()
11830 || sections.abbrev.empty ())
80626a55
DE
11831 {
11832 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
11833 " [in module %s]"),
11834 dwp_file->name);
11835 }
73869dc2 11836 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
11837 {
11838 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
11839 " [in module %s]"),
11840 dwp_file->name);
11841 }
11842
11843 /* It's easier for the rest of the code if we fake a struct dwo_file and
11844 have dwo_unit "live" in that. At least for now.
11845
11846 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 11847 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
11848 file, we can combine them back into a virtual DWO file to save space
11849 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
11850 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
11851
791afaa2
TT
11852 std::string virtual_dwo_name =
11853 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
11854 sections.abbrev.get_id (),
11855 sections.line.get_id (),
11856 sections.loc.get_id (),
11857 sections.str_offsets.get_id ());
80626a55 11858 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
11859 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
11860 virtual_dwo_name.c_str (),
11861 comp_dir);
80626a55
DE
11862 /* Create one if necessary. */
11863 if (*dwo_file_slot == NULL)
11864 {
b4f54984 11865 if (dwarf_read_debug)
80626a55
DE
11866 {
11867 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 11868 virtual_dwo_name.c_str ());
80626a55 11869 }
51ac9db5 11870 dwo_file = new struct dwo_file;
45940949 11871 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 11872 dwo_file->comp_dir = comp_dir;
80626a55
DE
11873 dwo_file->sections.abbrev = sections.abbrev;
11874 dwo_file->sections.line = sections.line;
11875 dwo_file->sections.loc = sections.loc;
11876 dwo_file->sections.macinfo = sections.macinfo;
11877 dwo_file->sections.macro = sections.macro;
11878 dwo_file->sections.str_offsets = sections.str_offsets;
11879 /* The "str" section is global to the entire DWP file. */
11880 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 11881 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
11882 there's no need to record it in dwo_file.
11883 Also, we can't simply record type sections in dwo_file because
11884 we record a pointer into the vector in dwo_unit. As we collect more
11885 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
11886 for it, invalidating all copies of pointers into the previous
11887 contents. */
80626a55
DE
11888 *dwo_file_slot = dwo_file;
11889 }
11890 else
11891 {
b4f54984 11892 if (dwarf_read_debug)
80626a55
DE
11893 {
11894 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 11895 virtual_dwo_name.c_str ());
80626a55 11896 }
9a3c8263 11897 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 11898 }
80626a55 11899
5989a64e 11900 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
11901 dwo_unit->dwo_file = dwo_file;
11902 dwo_unit->signature = signature;
8d749320 11903 dwo_unit->section =
5989a64e 11904 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 11905 *dwo_unit->section = sections.info_or_types;
57d63ce2 11906 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
11907
11908 return dwo_unit;
11909}
11910
73869dc2
DE
11911/* Subroutine of create_dwo_unit_in_dwp_v2 to simplify it.
11912 Given a pointer to the containing section SECTION, and OFFSET,SIZE of the
11913 piece within that section used by a TU/CU, return a virtual section
11914 of just that piece. */
11915
11916static struct dwarf2_section_info
ed2dc618
SM
11917create_dwp_v2_section (struct dwarf2_per_objfile *dwarf2_per_objfile,
11918 struct dwarf2_section_info *section,
73869dc2
DE
11919 bfd_size_type offset, bfd_size_type size)
11920{
11921 struct dwarf2_section_info result;
11922 asection *sectp;
11923
11924 gdb_assert (section != NULL);
11925 gdb_assert (!section->is_virtual);
11926
11927 memset (&result, 0, sizeof (result));
11928 result.s.containing_section = section;
dc4ccb6f 11929 result.is_virtual = true;
73869dc2
DE
11930
11931 if (size == 0)
11932 return result;
11933
96b79293 11934 sectp = section->get_bfd_section ();
73869dc2
DE
11935
11936 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
11937 bounds of the real section. This is a pretty-rare event, so just
11938 flag an error (easier) instead of a warning and trying to cope. */
11939 if (sectp == NULL
fd361982 11940 || offset + size > bfd_section_size (sectp))
73869dc2 11941 {
73869dc2
DE
11942 error (_("Dwarf Error: Bad DWP V2 section info, doesn't fit"
11943 " in section %s [in module %s]"),
fd361982 11944 sectp ? bfd_section_name (sectp) : "<unknown>",
73869dc2
DE
11945 objfile_name (dwarf2_per_objfile->objfile));
11946 }
11947
11948 result.virtual_offset = offset;
11949 result.size = size;
11950 return result;
11951}
11952
11953/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
11954 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
11955 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
11956 This is for DWP version 2 files. */
11957
11958static struct dwo_unit *
ed2dc618
SM
11959create_dwo_unit_in_dwp_v2 (struct dwarf2_per_objfile *dwarf2_per_objfile,
11960 struct dwp_file *dwp_file,
73869dc2
DE
11961 uint32_t unit_index,
11962 const char *comp_dir,
11963 ULONGEST signature, int is_debug_types)
11964{
73869dc2
DE
11965 const struct dwp_hash_table *dwp_htab =
11966 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 11967 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
11968 const char *kind = is_debug_types ? "TU" : "CU";
11969 struct dwo_file *dwo_file;
11970 struct dwo_unit *dwo_unit;
11971 struct virtual_v2_dwo_sections sections;
11972 void **dwo_file_slot;
73869dc2
DE
11973 int i;
11974
11975 gdb_assert (dwp_file->version == 2);
11976
b4f54984 11977 if (dwarf_read_debug)
73869dc2
DE
11978 {
11979 fprintf_unfiltered (gdb_stdlog, "Reading %s %s/%s in DWP V2 file: %s\n",
11980 kind,
11981 pulongest (unit_index), hex_string (signature),
11982 dwp_file->name);
11983 }
11984
11985 /* Fetch the section offsets of this DWO unit. */
11986
11987 memset (&sections, 0, sizeof (sections));
73869dc2
DE
11988
11989 for (i = 0; i < dwp_htab->nr_columns; ++i)
11990 {
11991 uint32_t offset = read_4_bytes (dbfd,
11992 dwp_htab->section_pool.v2.offsets
11993 + (((unit_index - 1) * dwp_htab->nr_columns
11994 + i)
11995 * sizeof (uint32_t)));
11996 uint32_t size = read_4_bytes (dbfd,
11997 dwp_htab->section_pool.v2.sizes
11998 + (((unit_index - 1) * dwp_htab->nr_columns
11999 + i)
12000 * sizeof (uint32_t)));
12001
12002 switch (dwp_htab->section_pool.v2.section_ids[i])
12003 {
12004 case DW_SECT_INFO:
12005 case DW_SECT_TYPES:
12006 sections.info_or_types_offset = offset;
12007 sections.info_or_types_size = size;
12008 break;
12009 case DW_SECT_ABBREV:
12010 sections.abbrev_offset = offset;
12011 sections.abbrev_size = size;
12012 break;
12013 case DW_SECT_LINE:
12014 sections.line_offset = offset;
12015 sections.line_size = size;
12016 break;
12017 case DW_SECT_LOC:
12018 sections.loc_offset = offset;
12019 sections.loc_size = size;
12020 break;
12021 case DW_SECT_STR_OFFSETS:
12022 sections.str_offsets_offset = offset;
12023 sections.str_offsets_size = size;
12024 break;
12025 case DW_SECT_MACINFO:
12026 sections.macinfo_offset = offset;
12027 sections.macinfo_size = size;
12028 break;
12029 case DW_SECT_MACRO:
12030 sections.macro_offset = offset;
12031 sections.macro_size = size;
12032 break;
12033 }
12034 }
12035
12036 /* It's easier for the rest of the code if we fake a struct dwo_file and
12037 have dwo_unit "live" in that. At least for now.
12038
12039 The DWP file can be made up of a random collection of CUs and TUs.
12040 However, for each CU + set of TUs that came from the same original DWO
12041 file, we can combine them back into a virtual DWO file to save space
12042 (fewer struct dwo_file objects to allocate). Remember that for really
12043 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12044
791afaa2
TT
12045 std::string virtual_dwo_name =
12046 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12047 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12048 (long) (sections.line_size ? sections.line_offset : 0),
12049 (long) (sections.loc_size ? sections.loc_offset : 0),
12050 (long) (sections.str_offsets_size
12051 ? sections.str_offsets_offset : 0));
73869dc2 12052 /* Can we use an existing virtual DWO file? */
ed2dc618
SM
12053 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12054 virtual_dwo_name.c_str (),
12055 comp_dir);
73869dc2
DE
12056 /* Create one if necessary. */
12057 if (*dwo_file_slot == NULL)
12058 {
b4f54984 12059 if (dwarf_read_debug)
73869dc2
DE
12060 {
12061 fprintf_unfiltered (gdb_stdlog, "Creating virtual DWO: %s\n",
791afaa2 12062 virtual_dwo_name.c_str ());
73869dc2 12063 }
51ac9db5 12064 dwo_file = new struct dwo_file;
45940949 12065 dwo_file->dwo_name = dwarf2_per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12066 dwo_file->comp_dir = comp_dir;
12067 dwo_file->sections.abbrev =
ed2dc618 12068 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.abbrev,
73869dc2
DE
12069 sections.abbrev_offset, sections.abbrev_size);
12070 dwo_file->sections.line =
ed2dc618 12071 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.line,
73869dc2
DE
12072 sections.line_offset, sections.line_size);
12073 dwo_file->sections.loc =
ed2dc618 12074 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.loc,
73869dc2
DE
12075 sections.loc_offset, sections.loc_size);
12076 dwo_file->sections.macinfo =
ed2dc618 12077 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macinfo,
73869dc2
DE
12078 sections.macinfo_offset, sections.macinfo_size);
12079 dwo_file->sections.macro =
ed2dc618 12080 create_dwp_v2_section (dwarf2_per_objfile, &dwp_file->sections.macro,
73869dc2
DE
12081 sections.macro_offset, sections.macro_size);
12082 dwo_file->sections.str_offsets =
ed2dc618
SM
12083 create_dwp_v2_section (dwarf2_per_objfile,
12084 &dwp_file->sections.str_offsets,
73869dc2
DE
12085 sections.str_offsets_offset,
12086 sections.str_offsets_size);
12087 /* The "str" section is global to the entire DWP file. */
12088 dwo_file->sections.str = dwp_file->sections.str;
12089 /* The info or types section is assigned below to dwo_unit,
12090 there's no need to record it in dwo_file.
12091 Also, we can't simply record type sections in dwo_file because
12092 we record a pointer into the vector in dwo_unit. As we collect more
12093 types we'll grow the vector and eventually have to reallocate space
12094 for it, invalidating all copies of pointers into the previous
12095 contents. */
12096 *dwo_file_slot = dwo_file;
12097 }
12098 else
12099 {
b4f54984 12100 if (dwarf_read_debug)
73869dc2
DE
12101 {
12102 fprintf_unfiltered (gdb_stdlog, "Using existing virtual DWO: %s\n",
791afaa2 12103 virtual_dwo_name.c_str ());
73869dc2 12104 }
9a3c8263 12105 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12106 }
73869dc2 12107
5989a64e 12108 dwo_unit = OBSTACK_ZALLOC (&dwarf2_per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12109 dwo_unit->dwo_file = dwo_file;
12110 dwo_unit->signature = signature;
8d749320 12111 dwo_unit->section =
5989a64e 12112 XOBNEW (&dwarf2_per_objfile->per_bfd->obstack, struct dwarf2_section_info);
ed2dc618
SM
12113 *dwo_unit->section = create_dwp_v2_section (dwarf2_per_objfile,
12114 is_debug_types
73869dc2
DE
12115 ? &dwp_file->sections.types
12116 : &dwp_file->sections.info,
12117 sections.info_or_types_offset,
12118 sections.info_or_types_size);
12119 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12120
12121 return dwo_unit;
12122}
12123
57d63ce2
DE
12124/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12125 Returns NULL if the signature isn't found. */
80626a55
DE
12126
12127static struct dwo_unit *
ed2dc618
SM
12128lookup_dwo_unit_in_dwp (struct dwarf2_per_objfile *dwarf2_per_objfile,
12129 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12130 ULONGEST signature, int is_debug_types)
80626a55 12131{
57d63ce2
DE
12132 const struct dwp_hash_table *dwp_htab =
12133 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12134 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12135 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12136 uint32_t hash = signature & mask;
12137 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12138 unsigned int i;
12139 void **slot;
870f88f7 12140 struct dwo_unit find_dwo_cu;
80626a55
DE
12141
12142 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12143 find_dwo_cu.signature = signature;
19ac8c2e 12144 slot = htab_find_slot (is_debug_types
48b490f2
TT
12145 ? dwp_file->loaded_tus.get ()
12146 : dwp_file->loaded_cus.get (),
19ac8c2e 12147 &find_dwo_cu, INSERT);
80626a55
DE
12148
12149 if (*slot != NULL)
9a3c8263 12150 return (struct dwo_unit *) *slot;
80626a55
DE
12151
12152 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12153 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12154 {
12155 ULONGEST signature_in_table;
12156
12157 signature_in_table =
57d63ce2 12158 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12159 if (signature_in_table == signature)
12160 {
57d63ce2
DE
12161 uint32_t unit_index =
12162 read_4_bytes (dbfd,
12163 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12164
73869dc2
DE
12165 if (dwp_file->version == 1)
12166 {
ed2dc618
SM
12167 *slot = create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile,
12168 dwp_file, unit_index,
73869dc2
DE
12169 comp_dir, signature,
12170 is_debug_types);
12171 }
12172 else
12173 {
ed2dc618
SM
12174 *slot = create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile,
12175 dwp_file, unit_index,
73869dc2
DE
12176 comp_dir, signature,
12177 is_debug_types);
12178 }
9a3c8263 12179 return (struct dwo_unit *) *slot;
80626a55
DE
12180 }
12181 if (signature_in_table == 0)
12182 return NULL;
12183 hash = (hash + hash2) & mask;
12184 }
12185
12186 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12187 " [in module %s]"),
12188 dwp_file->name);
12189}
12190
ab5088bf 12191/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12192 Open the file specified by FILE_NAME and hand it off to BFD for
12193 preliminary analysis. Return a newly initialized bfd *, which
12194 includes a canonicalized copy of FILE_NAME.
80626a55 12195 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12196 SEARCH_CWD is true if the current directory is to be searched.
12197 It will be searched before debug-file-directory.
13aaf454
DE
12198 If successful, the file is added to the bfd include table of the
12199 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12200 If unable to find/open the file, return NULL.
3019eac3
DE
12201 NOTE: This function is derived from symfile_bfd_open. */
12202
192b62ce 12203static gdb_bfd_ref_ptr
ed2dc618
SM
12204try_open_dwop_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12205 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12206{
24b9144d 12207 int desc;
9c02c129
DE
12208 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12209 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12210 to debug_file_directory. */
e0cc99a6 12211 const char *search_path;
9c02c129
DE
12212 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12213
e0cc99a6 12214 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12215 if (search_cwd)
12216 {
12217 if (*debug_file_directory != '\0')
e0cc99a6
TT
12218 {
12219 search_path_holder.reset (concat (".", dirname_separator_string,
12220 debug_file_directory,
12221 (char *) NULL));
12222 search_path = search_path_holder.get ();
12223 }
6ac97d4c 12224 else
e0cc99a6 12225 search_path = ".";
6ac97d4c 12226 }
9c02c129 12227 else
e0cc99a6 12228 search_path = debug_file_directory;
3019eac3 12229
24b9144d 12230 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12231 if (is_dwp)
12232 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12233
12234 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12235 desc = openp (search_path, flags, file_name,
3019eac3
DE
12236 O_RDONLY | O_BINARY, &absolute_name);
12237 if (desc < 0)
12238 return NULL;
12239
e0cc99a6
TT
12240 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12241 gnutarget, desc));
9c02c129
DE
12242 if (sym_bfd == NULL)
12243 return NULL;
192b62ce 12244 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12245
192b62ce
TT
12246 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12247 return NULL;
3019eac3 12248
13aaf454
DE
12249 /* Success. Record the bfd as having been included by the objfile's bfd.
12250 This is important because things like demangled_names_hash lives in the
12251 objfile's per_bfd space and may have references to things like symbol
12252 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
192b62ce 12253 gdb_bfd_record_inclusion (dwarf2_per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12254
3019eac3
DE
12255 return sym_bfd;
12256}
12257
ab5088bf 12258/* Try to open DWO file FILE_NAME.
3019eac3
DE
12259 COMP_DIR is the DW_AT_comp_dir attribute.
12260 The result is the bfd handle of the file.
12261 If there is a problem finding or opening the file, return NULL.
12262 Upon success, the canonicalized path of the file is stored in the bfd,
12263 same as symfile_bfd_open. */
12264
192b62ce 12265static gdb_bfd_ref_ptr
ed2dc618
SM
12266open_dwo_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12267 const char *file_name, const char *comp_dir)
3019eac3 12268{
80626a55 12269 if (IS_ABSOLUTE_PATH (file_name))
ed2dc618
SM
12270 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12271 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12272
12273 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12274
12275 if (comp_dir != NULL)
12276 {
43816ebc
TT
12277 gdb::unique_xmalloc_ptr<char> path_to_try
12278 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12279
12280 /* NOTE: If comp_dir is a relative path, this will also try the
12281 search path, which seems useful. */
ed2dc618 12282 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile,
43816ebc 12283 path_to_try.get (),
ed2dc618 12284 0 /*is_dwp*/,
192b62ce 12285 1 /*search_cwd*/));
3019eac3
DE
12286 if (abfd != NULL)
12287 return abfd;
12288 }
12289
12290 /* That didn't work, try debug-file-directory, which, despite its name,
12291 is a list of paths. */
12292
12293 if (*debug_file_directory == '\0')
12294 return NULL;
12295
ed2dc618
SM
12296 return try_open_dwop_file (dwarf2_per_objfile, file_name,
12297 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12298}
12299
80626a55
DE
12300/* This function is mapped across the sections and remembers the offset and
12301 size of each of the DWO debugging sections we are interested in. */
12302
12303static void
12304dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp, void *dwo_sections_ptr)
12305{
9a3c8263 12306 struct dwo_sections *dwo_sections = (struct dwo_sections *) dwo_sections_ptr;
80626a55
DE
12307 const struct dwop_section_names *names = &dwop_section_names;
12308
12309 if (section_is_p (sectp->name, &names->abbrev_dwo))
12310 {
049412e3 12311 dwo_sections->abbrev.s.section = sectp;
fd361982 12312 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12313 }
12314 else if (section_is_p (sectp->name, &names->info_dwo))
12315 {
049412e3 12316 dwo_sections->info.s.section = sectp;
fd361982 12317 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12318 }
12319 else if (section_is_p (sectp->name, &names->line_dwo))
12320 {
049412e3 12321 dwo_sections->line.s.section = sectp;
fd361982 12322 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12323 }
12324 else if (section_is_p (sectp->name, &names->loc_dwo))
12325 {
049412e3 12326 dwo_sections->loc.s.section = sectp;
fd361982 12327 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12328 }
41144253 12329 else if (section_is_p (sectp->name, &names->loclists_dwo))
12330 {
12331 dwo_sections->loclists.s.section = sectp;
12332 dwo_sections->loclists.size = bfd_section_size (sectp);
12333 }
80626a55
DE
12334 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12335 {
049412e3 12336 dwo_sections->macinfo.s.section = sectp;
fd361982 12337 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12338 }
12339 else if (section_is_p (sectp->name, &names->macro_dwo))
12340 {
049412e3 12341 dwo_sections->macro.s.section = sectp;
fd361982 12342 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12343 }
12344 else if (section_is_p (sectp->name, &names->str_dwo))
12345 {
049412e3 12346 dwo_sections->str.s.section = sectp;
fd361982 12347 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12348 }
12349 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12350 {
049412e3 12351 dwo_sections->str_offsets.s.section = sectp;
fd361982 12352 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12353 }
12354 else if (section_is_p (sectp->name, &names->types_dwo))
12355 {
12356 struct dwarf2_section_info type_section;
12357
12358 memset (&type_section, 0, sizeof (type_section));
049412e3 12359 type_section.s.section = sectp;
fd361982 12360 type_section.size = bfd_section_size (sectp);
fd5866f6 12361 dwo_sections->types.push_back (type_section);
80626a55
DE
12362 }
12363}
12364
ab5088bf 12365/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12366 by PER_CU. This is for the non-DWP case.
80626a55 12367 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12368
12369static struct dwo_file *
4ab09049
SM
12370open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12371 const char *comp_dir)
3019eac3 12372{
4ab09049 12373 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12374
fb1eb2f9 12375 gdb_bfd_ref_ptr dbfd = open_dwo_file (dwarf2_per_objfile, dwo_name, comp_dir);
80626a55
DE
12376 if (dbfd == NULL)
12377 {
b4f54984 12378 if (dwarf_read_debug)
80626a55
DE
12379 fprintf_unfiltered (gdb_stdlog, "DWO file not found: %s\n", dwo_name);
12380 return NULL;
12381 }
263db9a1 12382
51ac9db5 12383 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12384 dwo_file->dwo_name = dwo_name;
12385 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12386 dwo_file->dbfd = std::move (dbfd);
3019eac3 12387
fb1eb2f9 12388 bfd_map_over_sections (dwo_file->dbfd.get (), dwarf2_locate_dwo_sections,
192b62ce 12389 &dwo_file->sections);
3019eac3 12390
4ab09049 12391 create_cus_hash_table (dwarf2_per_objfile, cu, *dwo_file,
18a8505e 12392 dwo_file->sections.info, dwo_file->cus);
3019eac3 12393
263db9a1 12394 create_debug_types_hash_table (dwarf2_per_objfile, dwo_file.get (),
ed2dc618 12395 dwo_file->sections.types, dwo_file->tus);
3019eac3 12396
b4f54984 12397 if (dwarf_read_debug)
80626a55
DE
12398 fprintf_unfiltered (gdb_stdlog, "DWO file found: %s\n", dwo_name);
12399
263db9a1 12400 return dwo_file.release ();
3019eac3
DE
12401}
12402
80626a55 12403/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12404 size of each of the DWP debugging sections common to version 1 and 2 that
12405 we are interested in. */
3019eac3 12406
80626a55 12407static void
73869dc2
DE
12408dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
12409 void *dwp_file_ptr)
3019eac3 12410{
9a3c8263 12411 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
80626a55
DE
12412 const struct dwop_section_names *names = &dwop_section_names;
12413 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12414
80626a55 12415 /* Record the ELF section number for later lookup: this is what the
73869dc2 12416 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12417 gdb_assert (elf_section_nr < dwp_file->num_sections);
12418 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12419
80626a55
DE
12420 /* Look for specific sections that we need. */
12421 if (section_is_p (sectp->name, &names->str_dwo))
12422 {
049412e3 12423 dwp_file->sections.str.s.section = sectp;
fd361982 12424 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12425 }
12426 else if (section_is_p (sectp->name, &names->cu_index))
12427 {
049412e3 12428 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12429 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12430 }
12431 else if (section_is_p (sectp->name, &names->tu_index))
12432 {
049412e3 12433 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12434 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12435 }
12436}
3019eac3 12437
73869dc2
DE
12438/* This function is mapped across the sections and remembers the offset and
12439 size of each of the DWP version 2 debugging sections that we are interested
12440 in. This is split into a separate function because we don't know if we
12441 have version 1 or 2 until we parse the cu_index/tu_index sections. */
12442
12443static void
12444dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12445{
9a3c8263 12446 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12447 const struct dwop_section_names *names = &dwop_section_names;
12448 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12449
12450 /* Record the ELF section number for later lookup: this is what the
12451 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12452 gdb_assert (elf_section_nr < dwp_file->num_sections);
12453 dwp_file->elf_sections[elf_section_nr] = sectp;
12454
12455 /* Look for specific sections that we need. */
12456 if (section_is_p (sectp->name, &names->abbrev_dwo))
12457 {
049412e3 12458 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12459 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12460 }
12461 else if (section_is_p (sectp->name, &names->info_dwo))
12462 {
049412e3 12463 dwp_file->sections.info.s.section = sectp;
fd361982 12464 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12465 }
12466 else if (section_is_p (sectp->name, &names->line_dwo))
12467 {
049412e3 12468 dwp_file->sections.line.s.section = sectp;
fd361982 12469 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12470 }
12471 else if (section_is_p (sectp->name, &names->loc_dwo))
12472 {
049412e3 12473 dwp_file->sections.loc.s.section = sectp;
fd361982 12474 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12475 }
12476 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12477 {
049412e3 12478 dwp_file->sections.macinfo.s.section = sectp;
fd361982 12479 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
12480 }
12481 else if (section_is_p (sectp->name, &names->macro_dwo))
12482 {
049412e3 12483 dwp_file->sections.macro.s.section = sectp;
fd361982 12484 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
12485 }
12486 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12487 {
049412e3 12488 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 12489 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
12490 }
12491 else if (section_is_p (sectp->name, &names->types_dwo))
12492 {
049412e3 12493 dwp_file->sections.types.s.section = sectp;
fd361982 12494 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
12495 }
12496}
12497
80626a55 12498/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 12499
80626a55
DE
12500static hashval_t
12501hash_dwp_loaded_cutus (const void *item)
12502{
9a3c8263 12503 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 12504
80626a55
DE
12505 /* This drops the top 32 bits of the signature, but is ok for a hash. */
12506 return dwo_unit->signature;
3019eac3
DE
12507}
12508
80626a55 12509/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 12510
80626a55
DE
12511static int
12512eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 12513{
9a3c8263
SM
12514 const struct dwo_unit *dua = (const struct dwo_unit *) a;
12515 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 12516
80626a55
DE
12517 return dua->signature == dub->signature;
12518}
3019eac3 12519
80626a55 12520/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 12521
48b490f2 12522static htab_up
298e9637 12523allocate_dwp_loaded_cutus_table ()
80626a55 12524{
48b490f2
TT
12525 return htab_up (htab_create_alloc (3,
12526 hash_dwp_loaded_cutus,
12527 eq_dwp_loaded_cutus,
12528 NULL, xcalloc, xfree));
80626a55 12529}
3019eac3 12530
ab5088bf
DE
12531/* Try to open DWP file FILE_NAME.
12532 The result is the bfd handle of the file.
12533 If there is a problem finding or opening the file, return NULL.
12534 Upon success, the canonicalized path of the file is stored in the bfd,
12535 same as symfile_bfd_open. */
12536
192b62ce 12537static gdb_bfd_ref_ptr
ed2dc618
SM
12538open_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile,
12539 const char *file_name)
ab5088bf 12540{
ed2dc618
SM
12541 gdb_bfd_ref_ptr abfd (try_open_dwop_file (dwarf2_per_objfile, file_name,
12542 1 /*is_dwp*/,
192b62ce 12543 1 /*search_cwd*/));
6ac97d4c
DE
12544 if (abfd != NULL)
12545 return abfd;
12546
12547 /* Work around upstream bug 15652.
12548 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
12549 [Whether that's a "bug" is debatable, but it is getting in our way.]
12550 We have no real idea where the dwp file is, because gdb's realpath-ing
12551 of the executable's path may have discarded the needed info.
12552 [IWBN if the dwp file name was recorded in the executable, akin to
12553 .gnu_debuglink, but that doesn't exist yet.]
12554 Strip the directory from FILE_NAME and search again. */
12555 if (*debug_file_directory != '\0')
12556 {
12557 /* Don't implicitly search the current directory here.
12558 If the user wants to search "." to handle this case,
12559 it must be added to debug-file-directory. */
ed2dc618
SM
12560 return try_open_dwop_file (dwarf2_per_objfile,
12561 lbasename (file_name), 1 /*is_dwp*/,
6ac97d4c
DE
12562 0 /*search_cwd*/);
12563 }
12564
12565 return NULL;
ab5088bf
DE
12566}
12567
80626a55
DE
12568/* Initialize the use of the DWP file for the current objfile.
12569 By convention the name of the DWP file is ${objfile}.dwp.
12570 The result is NULL if it can't be found. */
a766d390 12571
400174b1 12572static std::unique_ptr<struct dwp_file>
ed2dc618 12573open_and_init_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
80626a55
DE
12574{
12575 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55 12576
82bf32bc
JK
12577 /* Try to find first .dwp for the binary file before any symbolic links
12578 resolving. */
6c447423
DE
12579
12580 /* If the objfile is a debug file, find the name of the real binary
12581 file and get the name of dwp file from there. */
d721ba37 12582 std::string dwp_name;
6c447423
DE
12583 if (objfile->separate_debug_objfile_backlink != NULL)
12584 {
12585 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
12586 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 12587
d721ba37 12588 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
12589 }
12590 else
d721ba37
PA
12591 dwp_name = objfile->original_name;
12592
12593 dwp_name += ".dwp";
80626a55 12594
ed2dc618 12595 gdb_bfd_ref_ptr dbfd (open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ()));
82bf32bc
JK
12596 if (dbfd == NULL
12597 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
12598 {
12599 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
12600 dwp_name = objfile_name (objfile);
12601 dwp_name += ".dwp";
ed2dc618 12602 dbfd = open_dwp_file (dwarf2_per_objfile, dwp_name.c_str ());
82bf32bc
JK
12603 }
12604
80626a55
DE
12605 if (dbfd == NULL)
12606 {
b4f54984 12607 if (dwarf_read_debug)
d721ba37 12608 fprintf_unfiltered (gdb_stdlog, "DWP file not found: %s\n", dwp_name.c_str ());
400174b1 12609 return std::unique_ptr<dwp_file> ();
3019eac3 12610 }
400174b1
TT
12611
12612 const char *name = bfd_get_filename (dbfd.get ());
12613 std::unique_ptr<struct dwp_file> dwp_file
12614 (new struct dwp_file (name, std::move (dbfd)));
c906108c 12615
0a0f4c01 12616 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 12617 dwp_file->elf_sections =
5989a64e 12618 OBSTACK_CALLOC (&dwarf2_per_objfile->per_bfd->obstack,
80626a55
DE
12619 dwp_file->num_sections, asection *);
12620
400174b1
TT
12621 bfd_map_over_sections (dwp_file->dbfd.get (),
12622 dwarf2_locate_common_dwp_sections,
12623 dwp_file.get ());
80626a55 12624
400174b1
TT
12625 dwp_file->cus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12626 0);
80626a55 12627
400174b1
TT
12628 dwp_file->tus = create_dwp_hash_table (dwarf2_per_objfile, dwp_file.get (),
12629 1);
80626a55 12630
73869dc2 12631 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
12632 if (dwp_file->cus && dwp_file->tus
12633 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
12634 {
12635 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 12636 pretty bizarre. We use pulongest here because that's the established
4d65956b 12637 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
12638 error (_("Dwarf Error: DWP file CU version %s doesn't match"
12639 " TU version %s [in DWP file %s]"),
12640 pulongest (dwp_file->cus->version),
d721ba37 12641 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 12642 }
08302ed2
DE
12643
12644 if (dwp_file->cus)
12645 dwp_file->version = dwp_file->cus->version;
12646 else if (dwp_file->tus)
12647 dwp_file->version = dwp_file->tus->version;
12648 else
12649 dwp_file->version = 2;
73869dc2
DE
12650
12651 if (dwp_file->version == 2)
400174b1
TT
12652 bfd_map_over_sections (dwp_file->dbfd.get (),
12653 dwarf2_locate_v2_dwp_sections,
12654 dwp_file.get ());
73869dc2 12655
298e9637
SM
12656 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
12657 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 12658
b4f54984 12659 if (dwarf_read_debug)
80626a55
DE
12660 {
12661 fprintf_unfiltered (gdb_stdlog, "DWP file found: %s\n", dwp_file->name);
12662 fprintf_unfiltered (gdb_stdlog,
21aa081e
PA
12663 " %s CUs, %s TUs\n",
12664 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
12665 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
12666 }
12667
12668 return dwp_file;
3019eac3 12669}
c906108c 12670
ab5088bf
DE
12671/* Wrapper around open_and_init_dwp_file, only open it once. */
12672
12673static struct dwp_file *
ed2dc618 12674get_dwp_file (struct dwarf2_per_objfile *dwarf2_per_objfile)
ab5088bf 12675{
5989a64e 12676 if (! dwarf2_per_objfile->per_bfd->dwp_checked)
ab5088bf 12677 {
5989a64e 12678 dwarf2_per_objfile->per_bfd->dwp_file
ed2dc618 12679 = open_and_init_dwp_file (dwarf2_per_objfile);
5989a64e 12680 dwarf2_per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 12681 }
5989a64e 12682 return dwarf2_per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
12683}
12684
80626a55
DE
12685/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
12686 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
12687 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 12688 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
12689 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
12690
12691 This is called, for example, when wanting to read a variable with a
12692 complex location. Therefore we don't want to do file i/o for every call.
12693 Therefore we don't want to look for a DWO file on every call.
12694 Therefore we first see if we've already seen SIGNATURE in a DWP file,
12695 then we check if we've already seen DWO_NAME, and only THEN do we check
12696 for a DWO file.
12697
1c658ad5 12698 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 12699 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 12700
3019eac3 12701static struct dwo_unit *
4ab09049 12702lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 12703 ULONGEST signature, int is_debug_types)
3019eac3 12704{
4ab09049 12705 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 12706 struct objfile *objfile = dwarf2_per_objfile->objfile;
80626a55
DE
12707 const char *kind = is_debug_types ? "TU" : "CU";
12708 void **dwo_file_slot;
3019eac3 12709 struct dwo_file *dwo_file;
80626a55 12710 struct dwp_file *dwp_file;
cb1df416 12711
6a506a2d
DE
12712 /* First see if there's a DWP file.
12713 If we have a DWP file but didn't find the DWO inside it, don't
12714 look for the original DWO file. It makes gdb behave differently
12715 depending on whether one is debugging in the build tree. */
cf2c3c16 12716
ed2dc618 12717 dwp_file = get_dwp_file (dwarf2_per_objfile);
80626a55 12718 if (dwp_file != NULL)
cf2c3c16 12719 {
80626a55
DE
12720 const struct dwp_hash_table *dwp_htab =
12721 is_debug_types ? dwp_file->tus : dwp_file->cus;
12722
12723 if (dwp_htab != NULL)
12724 {
12725 struct dwo_unit *dwo_cutu =
ed2dc618 12726 lookup_dwo_unit_in_dwp (dwarf2_per_objfile, dwp_file, comp_dir,
57d63ce2 12727 signature, is_debug_types);
80626a55
DE
12728
12729 if (dwo_cutu != NULL)
12730 {
b4f54984 12731 if (dwarf_read_debug)
80626a55
DE
12732 {
12733 fprintf_unfiltered (gdb_stdlog,
12734 "Virtual DWO %s %s found: @%s\n",
12735 kind, hex_string (signature),
12736 host_address_to_string (dwo_cutu));
12737 }
12738 return dwo_cutu;
12739 }
12740 }
12741 }
6a506a2d 12742 else
80626a55 12743 {
6a506a2d 12744 /* No DWP file, look for the DWO file. */
80626a55 12745
ed2dc618
SM
12746 dwo_file_slot = lookup_dwo_file_slot (dwarf2_per_objfile,
12747 dwo_name, comp_dir);
6a506a2d 12748 if (*dwo_file_slot == NULL)
80626a55 12749 {
6a506a2d 12750 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 12751 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 12752 }
6a506a2d 12753 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 12754 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 12755
6a506a2d 12756 if (dwo_file != NULL)
19c3d4c9 12757 {
6a506a2d
DE
12758 struct dwo_unit *dwo_cutu = NULL;
12759
12760 if (is_debug_types && dwo_file->tus)
12761 {
12762 struct dwo_unit find_dwo_cutu;
12763
12764 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12765 find_dwo_cutu.signature = signature;
9a3c8263 12766 dwo_cutu
b0b6a987
TT
12767 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
12768 &find_dwo_cutu);
6a506a2d 12769 }
33c5cd75 12770 else if (!is_debug_types && dwo_file->cus)
80626a55 12771 {
33c5cd75
DB
12772 struct dwo_unit find_dwo_cutu;
12773
12774 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
12775 find_dwo_cutu.signature = signature;
b0b6a987 12776 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 12777 &find_dwo_cutu);
6a506a2d
DE
12778 }
12779
12780 if (dwo_cutu != NULL)
12781 {
b4f54984 12782 if (dwarf_read_debug)
6a506a2d
DE
12783 {
12784 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) found: @%s\n",
12785 kind, dwo_name, hex_string (signature),
12786 host_address_to_string (dwo_cutu));
12787 }
12788 return dwo_cutu;
80626a55
DE
12789 }
12790 }
2e276125 12791 }
9cdd5dbd 12792
80626a55
DE
12793 /* We didn't find it. This could mean a dwo_id mismatch, or
12794 someone deleted the DWO/DWP file, or the search path isn't set up
12795 correctly to find the file. */
12796
b4f54984 12797 if (dwarf_read_debug)
80626a55
DE
12798 {
12799 fprintf_unfiltered (gdb_stdlog, "DWO %s %s(%s) not found\n",
12800 kind, dwo_name, hex_string (signature));
12801 }
3019eac3 12802
6656a72d
DE
12803 /* This is a warning and not a complaint because it can be caused by
12804 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
12805 {
12806 /* Print the name of the DWP file if we looked there, helps the user
12807 better diagnose the problem. */
791afaa2 12808 std::string dwp_text;
43942612
DE
12809
12810 if (dwp_file != NULL)
791afaa2
TT
12811 dwp_text = string_printf (" [in DWP file %s]",
12812 lbasename (dwp_file->name));
43942612 12813
9d8780f0 12814 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 12815 " [in module %s]"),
4ab09049
SM
12816 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
12817 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 12818 }
3019eac3 12819 return NULL;
5fb290d7
DJ
12820}
12821
80626a55
DE
12822/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
12823 See lookup_dwo_cutu_unit for details. */
12824
12825static struct dwo_unit *
4ab09049 12826lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
12827 ULONGEST signature)
12828{
4ab09049
SM
12829 gdb_assert (!cu->per_cu->is_debug_types);
12830
12831 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
12832}
12833
12834/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
12835 See lookup_dwo_cutu_unit for details. */
12836
12837static struct dwo_unit *
4ab09049 12838lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 12839{
4ab09049
SM
12840 gdb_assert (cu->per_cu->is_debug_types);
12841
12842 signatured_type *sig_type = (signatured_type *) cu->per_cu;
12843
12844 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
12845}
12846
89e63ee4
DE
12847/* Traversal function for queue_and_load_all_dwo_tus. */
12848
12849static int
12850queue_and_load_dwo_tu (void **slot, void *info)
12851{
12852 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 12853 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 12854 ULONGEST signature = dwo_unit->signature;
d460f660 12855 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
12856
12857 if (sig_type != NULL)
12858 {
12859 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
12860
12861 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
12862 a real dependency of PER_CU on SIG_TYPE. That is detected later
12863 while processing PER_CU. */
120ce1b5 12864 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
12865 load_full_type_unit (sig_cu, cu->per_objfile);
12866 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
12867 }
12868
12869 return 1;
12870}
12871
1b555f17 12872/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
12873 The DWO may have the only definition of the type, though it may not be
12874 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
12875 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
12876
12877static void
1b555f17 12878queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
12879{
12880 struct dwo_unit *dwo_unit;
12881 struct dwo_file *dwo_file;
12882
1b555f17
SM
12883 gdb_assert (cu != nullptr);
12884 gdb_assert (!cu->per_cu->is_debug_types);
12885 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 12886
1b555f17 12887 dwo_unit = cu->dwo_unit;
89e63ee4
DE
12888 gdb_assert (dwo_unit != NULL);
12889
12890 dwo_file = dwo_unit->dwo_file;
12891 if (dwo_file->tus != NULL)
1b555f17 12892 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
12893}
12894
3019eac3 12895/* Read in various DIEs. */
348e048f 12896
d389af10 12897/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
12898 Inherit only the children of the DW_AT_abstract_origin DIE not being
12899 already referenced by DW_AT_abstract_origin from the children of the
12900 current DIE. */
d389af10
JK
12901
12902static void
12903inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
12904{
12905 struct die_info *child_die;
791afaa2 12906 sect_offset *offsetp;
d389af10
JK
12907 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
12908 struct die_info *origin_die;
12909 /* Iterator of the ORIGIN_DIE children. */
12910 struct die_info *origin_child_die;
d389af10 12911 struct attribute *attr;
cd02d79d
PA
12912 struct dwarf2_cu *origin_cu;
12913 struct pending **origin_previous_list_in_scope;
d389af10
JK
12914
12915 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
12916 if (!attr)
12917 return;
12918
cd02d79d
PA
12919 /* Note that following die references may follow to a die in a
12920 different cu. */
12921
12922 origin_cu = cu;
12923 origin_die = follow_die_ref (die, attr, &origin_cu);
12924
12925 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
12926 symbols in. */
12927 origin_previous_list_in_scope = origin_cu->list_in_scope;
12928 origin_cu->list_in_scope = cu->list_in_scope;
12929
edb3359d
DJ
12930 if (die->tag != origin_die->tag
12931 && !(die->tag == DW_TAG_inlined_subroutine
12932 && origin_die->tag == DW_TAG_subprogram))
b98664d3 12933 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
12934 sect_offset_str (die->sect_off),
12935 sect_offset_str (origin_die->sect_off));
d389af10 12936
791afaa2 12937 std::vector<sect_offset> offsets;
d389af10 12938
3ea89b92
PMR
12939 for (child_die = die->child;
12940 child_die && child_die->tag;
436c571c 12941 child_die = child_die->sibling)
3ea89b92
PMR
12942 {
12943 struct die_info *child_origin_die;
12944 struct dwarf2_cu *child_origin_cu;
12945
12946 /* We are trying to process concrete instance entries:
216f72a1 12947 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
12948 it's not relevant to our analysis here. i.e. detecting DIEs that are
12949 present in the abstract instance but not referenced in the concrete
12950 one. */
216f72a1
JK
12951 if (child_die->tag == DW_TAG_call_site
12952 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
12953 continue;
12954
c38f313d
DJ
12955 /* For each CHILD_DIE, find the corresponding child of
12956 ORIGIN_DIE. If there is more than one layer of
12957 DW_AT_abstract_origin, follow them all; there shouldn't be,
12958 but GCC versions at least through 4.4 generate this (GCC PR
12959 40573). */
3ea89b92
PMR
12960 child_origin_die = child_die;
12961 child_origin_cu = cu;
c38f313d
DJ
12962 while (1)
12963 {
cd02d79d
PA
12964 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
12965 child_origin_cu);
c38f313d
DJ
12966 if (attr == NULL)
12967 break;
cd02d79d
PA
12968 child_origin_die = follow_die_ref (child_origin_die, attr,
12969 &child_origin_cu);
c38f313d
DJ
12970 }
12971
d389af10
JK
12972 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
12973 counterpart may exist. */
c38f313d 12974 if (child_origin_die != child_die)
d389af10 12975 {
edb3359d
DJ
12976 if (child_die->tag != child_origin_die->tag
12977 && !(child_die->tag == DW_TAG_inlined_subroutine
12978 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 12979 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12980 "different tags"),
9d8780f0
SM
12981 sect_offset_str (child_die->sect_off),
12982 sect_offset_str (child_origin_die->sect_off));
c38f313d 12983 if (child_origin_die->parent != origin_die)
b98664d3 12984 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 12985 "different parents"),
9d8780f0
SM
12986 sect_offset_str (child_die->sect_off),
12987 sect_offset_str (child_origin_die->sect_off));
c38f313d 12988 else
791afaa2 12989 offsets.push_back (child_origin_die->sect_off);
d389af10 12990 }
d389af10 12991 }
791afaa2
TT
12992 std::sort (offsets.begin (), offsets.end ());
12993 sect_offset *offsets_end = offsets.data () + offsets.size ();
12994 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 12995 if (offsetp[-1] == *offsetp)
b98664d3 12996 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
12997 "to DIE %s as their abstract origin"),
12998 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 12999
791afaa2 13000 offsetp = offsets.data ();
d389af10
JK
13001 origin_child_die = origin_die->child;
13002 while (origin_child_die && origin_child_die->tag)
13003 {
13004 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13005 while (offsetp < offsets_end
9c541725 13006 && *offsetp < origin_child_die->sect_off)
d389af10 13007 offsetp++;
b64f50a1 13008 if (offsetp >= offsets_end
9c541725 13009 || *offsetp > origin_child_die->sect_off)
d389af10 13010 {
adde2bff
DE
13011 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13012 Check whether we're already processing ORIGIN_CHILD_DIE.
13013 This can happen with mutually referenced abstract_origins.
13014 PR 16581. */
13015 if (!origin_child_die->in_process)
13016 process_die (origin_child_die, origin_cu);
d389af10 13017 }
436c571c 13018 origin_child_die = origin_child_die->sibling;
d389af10 13019 }
cd02d79d 13020 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13021
13022 if (cu != origin_cu)
13023 compute_delayed_physnames (origin_cu);
d389af10
JK
13024}
13025
c906108c 13026static void
e7c27a73 13027read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13028{
5e22e966 13029 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13030 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13031 struct context_stack *newobj;
c906108c
SS
13032 CORE_ADDR lowpc;
13033 CORE_ADDR highpc;
13034 struct die_info *child_die;
edb3359d 13035 struct attribute *attr, *call_line, *call_file;
15d034d0 13036 const char *name;
e142c38c 13037 CORE_ADDR baseaddr;
801e3a5b 13038 struct block *block;
edb3359d 13039 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13040 std::vector<struct symbol *> template_args;
34eaf542 13041 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13042
13043 if (inlined_func)
13044 {
13045 /* If we do not have call site information, we can't show the
13046 caller of this inlined function. That's too confusing, so
13047 only use the scope for local variables. */
13048 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13049 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13050 if (call_line == NULL || call_file == NULL)
13051 {
13052 read_lexical_block_scope (die, cu);
13053 return;
13054 }
13055 }
c906108c 13056
b3b3bada 13057 baseaddr = objfile->text_section_offset ();
e142c38c 13058
94af9270 13059 name = dwarf2_name (die, cu);
c906108c 13060
e8d05480
JB
13061 /* Ignore functions with missing or empty names. These are actually
13062 illegal according to the DWARF standard. */
13063 if (name == NULL)
13064 {
b98664d3 13065 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13066 sect_offset_str (die->sect_off));
e8d05480
JB
13067 return;
13068 }
13069
13070 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13071 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13072 <= PC_BOUNDS_INVALID)
e8d05480 13073 {
ae4d0c03
PM
13074 attr = dwarf2_attr (die, DW_AT_external, cu);
13075 if (!attr || !DW_UNSND (attr))
b98664d3 13076 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13077 "for subprogram DIE at %s"),
13078 sect_offset_str (die->sect_off));
e8d05480
JB
13079 return;
13080 }
c906108c 13081
3e29f34a
MR
13082 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13083 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13084
34eaf542
TT
13085 /* If we have any template arguments, then we must allocate a
13086 different sort of symbol. */
436c571c 13087 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13088 {
13089 if (child_die->tag == DW_TAG_template_type_param
13090 || child_die->tag == DW_TAG_template_value_param)
13091 {
8c14c3a3 13092 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13093 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13094 break;
13095 }
13096 }
13097
c24bdb02 13098 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13099 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13100 (struct symbol *) templ_func);
4c2df51b 13101
81873cc8 13102 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13103 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13104 cu->language);
13105
4cecd739
DJ
13106 /* If there is a location expression for DW_AT_frame_base, record
13107 it. */
e142c38c 13108 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13109 if (attr != nullptr)
fe978cb0 13110 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13111
63e43d3a
PMR
13112 /* If there is a location for the static link, record it. */
13113 newobj->static_link = NULL;
13114 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13115 if (attr != nullptr)
63e43d3a 13116 {
224c3ddb
SM
13117 newobj->static_link
13118 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13119 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13120 cu->addr_type ());
63e43d3a
PMR
13121 }
13122
c24bdb02 13123 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13124
639d11d3 13125 if (die->child != NULL)
c906108c 13126 {
639d11d3 13127 child_die = die->child;
c906108c
SS
13128 while (child_die && child_die->tag)
13129 {
34eaf542
TT
13130 if (child_die->tag == DW_TAG_template_type_param
13131 || child_die->tag == DW_TAG_template_value_param)
13132 {
13133 struct symbol *arg = new_symbol (child_die, NULL, cu);
13134
f1078f66 13135 if (arg != NULL)
2f4732b0 13136 template_args.push_back (arg);
34eaf542
TT
13137 }
13138 else
13139 process_die (child_die, cu);
436c571c 13140 child_die = child_die->sibling;
c906108c
SS
13141 }
13142 }
13143
d389af10
JK
13144 inherit_abstract_dies (die, cu);
13145
4a811a97
UW
13146 /* If we have a DW_AT_specification, we might need to import using
13147 directives from the context of the specification DIE. See the
13148 comment in determine_prefix. */
13149 if (cu->language == language_cplus
13150 && dwarf2_attr (die, DW_AT_specification, cu))
13151 {
13152 struct dwarf2_cu *spec_cu = cu;
13153 struct die_info *spec_die = die_specification (die, &spec_cu);
13154
13155 while (spec_die)
13156 {
13157 child_die = spec_die->child;
13158 while (child_die && child_die->tag)
13159 {
13160 if (child_die->tag == DW_TAG_imported_module)
13161 process_die (child_die, spec_cu);
436c571c 13162 child_die = child_die->sibling;
4a811a97
UW
13163 }
13164
13165 /* In some cases, GCC generates specification DIEs that
13166 themselves contain DW_AT_specification attributes. */
13167 spec_die = die_specification (spec_die, &spec_cu);
13168 }
13169 }
13170
c24bdb02 13171 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13172 /* Make a block for the local symbols within. */
c24bdb02 13173 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13174 cstk.static_link, lowpc, highpc);
801e3a5b 13175
df8a16a1 13176 /* For C++, set the block's scope. */
45280282
IB
13177 if ((cu->language == language_cplus
13178 || cu->language == language_fortran
c44af4eb
TT
13179 || cu->language == language_d
13180 || cu->language == language_rust)
4d4ec4e5 13181 && cu->processing_has_namespace_info)
195a3f6c
TT
13182 block_set_scope (block, determine_prefix (die, cu),
13183 &objfile->objfile_obstack);
df8a16a1 13184
801e3a5b
JB
13185 /* If we have address ranges, record them. */
13186 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13187
a60f3166 13188 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13189
34eaf542 13190 /* Attach template arguments to function. */
2f4732b0 13191 if (!template_args.empty ())
34eaf542
TT
13192 {
13193 gdb_assert (templ_func != NULL);
13194
2f4732b0 13195 templ_func->n_template_arguments = template_args.size ();
34eaf542 13196 templ_func->template_arguments
8d749320
SM
13197 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
13198 templ_func->n_template_arguments);
34eaf542 13199 memcpy (templ_func->template_arguments,
2f4732b0 13200 template_args.data (),
34eaf542 13201 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13202
13203 /* Make sure that the symtab is set on the new symbols. Even
13204 though they don't appear in this symtab directly, other parts
13205 of gdb assume that symbols do, and this is reasonably
13206 true. */
8634679f 13207 for (symbol *sym : template_args)
3e1d3d8c 13208 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13209 }
13210
208d8187
JB
13211 /* In C++, we can have functions nested inside functions (e.g., when
13212 a function declares a class that has methods). This means that
13213 when we finish processing a function scope, we may need to go
13214 back to building a containing block's symbol lists. */
c24bdb02
KS
13215 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13216 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13217
921e78cf
JB
13218 /* If we've finished processing a top-level function, subsequent
13219 symbols go in the file symbol list. */
c24bdb02
KS
13220 if (cu->get_builder ()->outermost_context_p ())
13221 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13222}
13223
13224/* Process all the DIES contained within a lexical block scope. Start
13225 a new scope, process the dies, and then close the scope. */
13226
13227static void
e7c27a73 13228read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13229{
5e22e966 13230 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13231 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13232 CORE_ADDR lowpc, highpc;
13233 struct die_info *child_die;
e142c38c
DJ
13234 CORE_ADDR baseaddr;
13235
b3b3bada 13236 baseaddr = objfile->text_section_offset ();
c906108c
SS
13237
13238 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13239 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13240 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13241 be nasty. Might be easier to properly extend generic blocks to
af34e669 13242 describe ranges. */
e385593e
JK
13243 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13244 {
13245 case PC_BOUNDS_NOT_PRESENT:
13246 /* DW_TAG_lexical_block has no attributes, process its children as if
13247 there was no wrapping by that DW_TAG_lexical_block.
13248 GCC does no longer produces such DWARF since GCC r224161. */
13249 for (child_die = die->child;
13250 child_die != NULL && child_die->tag;
436c571c 13251 child_die = child_die->sibling)
4f7bc5ed
TT
13252 {
13253 /* We might already be processing this DIE. This can happen
13254 in an unusual circumstance -- where a subroutine A
13255 appears lexically in another subroutine B, but A actually
13256 inlines B. The recursion is broken here, rather than in
13257 inherit_abstract_dies, because it seems better to simply
13258 drop concrete children here. */
13259 if (!child_die->in_process)
13260 process_die (child_die, cu);
13261 }
e385593e
JK
13262 return;
13263 case PC_BOUNDS_INVALID:
13264 return;
13265 }
3e29f34a
MR
13266 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13267 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13268
c24bdb02 13269 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13270 if (die->child != NULL)
c906108c 13271 {
639d11d3 13272 child_die = die->child;
c906108c
SS
13273 while (child_die && child_die->tag)
13274 {
e7c27a73 13275 process_die (child_die, cu);
436c571c 13276 child_die = child_die->sibling;
c906108c
SS
13277 }
13278 }
3ea89b92 13279 inherit_abstract_dies (die, cu);
c24bdb02 13280 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13281
c24bdb02
KS
13282 if (*cu->get_builder ()->get_local_symbols () != NULL
13283 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13284 {
801e3a5b 13285 struct block *block
c24bdb02 13286 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13287 cstk.start_addr, highpc);
801e3a5b
JB
13288
13289 /* Note that recording ranges after traversing children, as we
13290 do here, means that recording a parent's ranges entails
13291 walking across all its children's ranges as they appear in
13292 the address map, which is quadratic behavior.
13293
13294 It would be nicer to record the parent's ranges before
13295 traversing its children, simply overriding whatever you find
13296 there. But since we don't even decide whether to create a
13297 block until after we've traversed its children, that's hard
13298 to do. */
13299 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13300 }
c24bdb02
KS
13301 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13302 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13303}
13304
216f72a1 13305/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13306
13307static void
13308read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13309{
5e22e966 13310 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13311 struct objfile *objfile = per_objfile->objfile;
08feed99 13312 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13313 CORE_ADDR pc, baseaddr;
13314 struct attribute *attr;
13315 struct call_site *call_site, call_site_local;
13316 void **slot;
13317 int nparams;
13318 struct die_info *child_die;
13319
b3b3bada 13320 baseaddr = objfile->text_section_offset ();
96408a79 13321
216f72a1
JK
13322 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13323 if (attr == NULL)
13324 {
13325 /* This was a pre-DWARF-5 GNU extension alias
13326 for DW_AT_call_return_pc. */
13327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13328 }
96408a79
SA
13329 if (!attr)
13330 {
b98664d3 13331 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13332 "DIE %s [in module %s]"),
13333 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13334 return;
13335 }
cd6c91b4 13336 pc = attr->value_as_address () + baseaddr;
3e29f34a 13337 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13338
13339 if (cu->call_site_htab == NULL)
13340 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13341 NULL, &objfile->objfile_obstack,
13342 hashtab_obstack_allocate, NULL);
13343 call_site_local.pc = pc;
13344 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13345 if (*slot != NULL)
13346 {
b98664d3 13347 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13348 "DIE %s [in module %s]"),
13349 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13350 objfile_name (objfile));
96408a79
SA
13351 return;
13352 }
13353
13354 /* Count parameters at the caller. */
13355
13356 nparams = 0;
13357 for (child_die = die->child; child_die && child_die->tag;
436c571c 13358 child_die = child_die->sibling)
96408a79 13359 {
216f72a1
JK
13360 if (child_die->tag != DW_TAG_call_site_parameter
13361 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13362 {
b98664d3 13363 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13364 "DW_TAG_call_site child DIE %s [in module %s]"),
13365 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13366 objfile_name (objfile));
96408a79
SA
13367 continue;
13368 }
13369
13370 nparams++;
13371 }
13372
224c3ddb
SM
13373 call_site
13374 = ((struct call_site *)
13375 obstack_alloc (&objfile->objfile_obstack,
13376 sizeof (*call_site)
13377 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13378 *slot = call_site;
13379 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13380 call_site->pc = pc;
13381
216f72a1
JK
13382 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13383 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13384 {
13385 struct die_info *func_die;
13386
13387 /* Skip also over DW_TAG_inlined_subroutine. */
13388 for (func_die = die->parent;
13389 func_die && func_die->tag != DW_TAG_subprogram
13390 && func_die->tag != DW_TAG_subroutine_type;
13391 func_die = func_die->parent);
13392
216f72a1
JK
13393 /* DW_AT_call_all_calls is a superset
13394 of DW_AT_call_all_tail_calls. */
96408a79 13395 if (func_die
216f72a1 13396 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
96408a79 13397 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13398 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13399 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13400 {
13401 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13402 not complete. But keep CALL_SITE for look ups via call_site_htab,
13403 both the initial caller containing the real return address PC and
13404 the final callee containing the current PC of a chain of tail
13405 calls do not need to have the tail call list complete. But any
13406 function candidate for a virtual tail call frame searched via
13407 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13408 determined unambiguously. */
13409 }
13410 else
13411 {
13412 struct type *func_type = NULL;
13413
13414 if (func_die)
13415 func_type = get_die_type (func_die, cu);
13416 if (func_type != NULL)
13417 {
78134374 13418 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13419
13420 /* Enlist this call site to the function. */
13421 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13422 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13423 }
13424 else
b98664d3 13425 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13426 "DIE %s [in module %s]"),
13427 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13428 }
13429 }
13430
216f72a1
JK
13431 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13432 if (attr == NULL)
13433 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13434 if (attr == NULL)
13435 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 13436 if (attr == NULL)
216f72a1
JK
13437 {
13438 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
13439 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13440 }
96408a79 13441 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
4fc6c0d5 13442 if (!attr || (attr->form_is_block () && DW_BLOCK (attr)->size == 0))
96408a79 13443 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 13444 else if (attr->form_is_block ())
96408a79
SA
13445 {
13446 struct dwarf2_locexpr_baton *dlbaton;
13447
8d749320 13448 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
96408a79
SA
13449 dlbaton->data = DW_BLOCK (attr)->data;
13450 dlbaton->size = DW_BLOCK (attr)->size;
a50264ba 13451 dlbaton->per_objfile = per_objfile;
96408a79
SA
13452 dlbaton->per_cu = cu->per_cu;
13453
13454 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
13455 }
cd6c91b4 13456 else if (attr->form_is_ref ())
96408a79 13457 {
96408a79
SA
13458 struct dwarf2_cu *target_cu = cu;
13459 struct die_info *target_die;
13460
ac9ec31b 13461 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 13462 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
13463 if (die_is_declaration (target_die, target_cu))
13464 {
7d45c7c3 13465 const char *target_physname;
9112db09
JK
13466
13467 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 13468 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 13469 if (target_physname == NULL)
9112db09 13470 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 13471 if (target_physname == NULL)
b98664d3 13472 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13473 "physname, for referencing DIE %s [in module %s]"),
13474 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13475 else
7d455152 13476 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
13477 }
13478 else
13479 {
13480 CORE_ADDR lowpc;
13481
13482 /* DW_AT_entry_pc should be preferred. */
3a2b436a 13483 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 13484 <= PC_BOUNDS_INVALID)
b98664d3 13485 complaint (_("DW_AT_call_target target DIE has invalid "
9d8780f0
SM
13486 "low pc, for referencing DIE %s [in module %s]"),
13487 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 13488 else
3e29f34a
MR
13489 {
13490 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13491 SET_FIELD_PHYSADDR (call_site->target, lowpc);
13492 }
96408a79
SA
13493 }
13494 }
13495 else
b98664d3 13496 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
13497 "block nor reference, for DIE %s [in module %s]"),
13498 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13499
13500 call_site->per_cu = cu->per_cu;
9f47c707 13501 call_site->per_objfile = per_objfile;
96408a79
SA
13502
13503 for (child_die = die->child;
13504 child_die && child_die->tag;
436c571c 13505 child_die = child_die->sibling)
96408a79 13506 {
96408a79 13507 struct call_site_parameter *parameter;
1788b2d3 13508 struct attribute *loc, *origin;
96408a79 13509
216f72a1
JK
13510 if (child_die->tag != DW_TAG_call_site_parameter
13511 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
13512 {
13513 /* Already printed the complaint above. */
13514 continue;
13515 }
13516
13517 gdb_assert (call_site->parameter_count < nparams);
13518 parameter = &call_site->parameter[call_site->parameter_count];
13519
1788b2d3
JK
13520 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
13521 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 13522 register is contained in DW_AT_call_value. */
96408a79 13523
24c5c679 13524 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
13525 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
13526 if (origin == NULL)
13527 {
13528 /* This was a pre-DWARF-5 GNU extension alias
13529 for DW_AT_call_parameter. */
13530 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
13531 }
cd6c91b4 13532 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 13533 {
1788b2d3 13534 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 13535
0826b30a 13536 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 13537 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
13538 {
13539 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
13540 binding can be done only inside one CU. Such referenced DIE
13541 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 13542 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
13543 "DW_TAG_call_site child DIE %s [in module %s]"),
13544 sect_offset_str (child_die->sect_off),
9c541725 13545 objfile_name (objfile));
d76b7dbc
JK
13546 continue;
13547 }
9c541725
PA
13548 parameter->u.param_cu_off
13549 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 13550 }
4fc6c0d5 13551 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 13552 {
b98664d3 13553 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
13554 "DW_TAG_call_site child DIE %s [in module %s]"),
13555 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
13556 continue;
13557 }
24c5c679 13558 else
96408a79 13559 {
24c5c679
JK
13560 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
13561 (DW_BLOCK (loc)->data, &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size]);
13562 if (parameter->u.dwarf_reg != -1)
13563 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
13564 else if (dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (loc)->data,
13565 &DW_BLOCK (loc)->data[DW_BLOCK (loc)->size],
13566 &parameter->u.fb_offset))
13567 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
13568 else
13569 {
b98664d3 13570 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 13571 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 13572 "DW_TAG_call_site child DIE %s "
24c5c679 13573 "[in module %s]"),
9d8780f0 13574 sect_offset_str (child_die->sect_off),
9c541725 13575 objfile_name (objfile));
24c5c679
JK
13576 continue;
13577 }
96408a79
SA
13578 }
13579
216f72a1
JK
13580 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
13581 if (attr == NULL)
13582 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 13583 if (attr == NULL || !attr->form_is_block ())
96408a79 13584 {
b98664d3 13585 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
13586 "DW_TAG_call_site child DIE %s [in module %s]"),
13587 sect_offset_str (child_die->sect_off),
9c541725 13588 objfile_name (objfile));
96408a79
SA
13589 continue;
13590 }
13591 parameter->value = DW_BLOCK (attr)->data;
13592 parameter->value_size = DW_BLOCK (attr)->size;
13593
13594 /* Parameters are not pre-cleared by memset above. */
13595 parameter->data_value = NULL;
13596 parameter->data_value_size = 0;
13597 call_site->parameter_count++;
13598
216f72a1
JK
13599 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
13600 if (attr == NULL)
13601 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 13602 if (attr != nullptr)
96408a79 13603 {
4fc6c0d5 13604 if (!attr->form_is_block ())
b98664d3 13605 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
13606 "DW_TAG_call_site child DIE %s [in module %s]"),
13607 sect_offset_str (child_die->sect_off),
9c541725 13608 objfile_name (objfile));
96408a79
SA
13609 else
13610 {
13611 parameter->data_value = DW_BLOCK (attr)->data;
13612 parameter->data_value_size = DW_BLOCK (attr)->size;
13613 }
13614 }
13615 }
13616}
13617
71a3c369
TT
13618/* Helper function for read_variable. If DIE represents a virtual
13619 table, then return the type of the concrete object that is
13620 associated with the virtual table. Otherwise, return NULL. */
13621
13622static struct type *
13623rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
13624{
13625 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
13626 if (attr == NULL)
13627 return NULL;
13628
13629 /* Find the type DIE. */
13630 struct die_info *type_die = NULL;
13631 struct dwarf2_cu *type_cu = cu;
13632
cd6c91b4 13633 if (attr->form_is_ref ())
71a3c369
TT
13634 type_die = follow_die_ref (die, attr, &type_cu);
13635 if (type_die == NULL)
13636 return NULL;
13637
13638 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
13639 return NULL;
13640 return die_containing_type (type_die, type_cu);
13641}
13642
13643/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
13644
13645static void
13646read_variable (struct die_info *die, struct dwarf2_cu *cu)
13647{
13648 struct rust_vtable_symbol *storage = NULL;
13649
13650 if (cu->language == language_rust)
13651 {
13652 struct type *containing_type = rust_containing_type (die, cu);
13653
13654 if (containing_type != NULL)
13655 {
5e22e966 13656 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 13657
8c14c3a3 13658 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 13659 storage->concrete_type = containing_type;
cf724bc9 13660 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
13661 }
13662 }
13663
e4a62c65
TV
13664 struct symbol *res = new_symbol (die, NULL, cu, storage);
13665 struct attribute *abstract_origin
13666 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13667 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
13668 if (res == NULL && loc && abstract_origin)
13669 {
13670 /* We have a variable without a name, but with a location and an abstract
13671 origin. This may be a concrete instance of an abstract variable
13672 referenced from an DW_OP_GNU_variable_value, so save it to find it back
13673 later. */
13674 struct dwarf2_cu *origin_cu = cu;
13675 struct die_info *origin_die
13676 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
13677 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13678 per_objfile->per_bfd->abstract_to_concrete
13679 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 13680 }
71a3c369
TT
13681}
13682
43988095
JK
13683/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
13684 reading .debug_rnglists.
13685 Callback's type should be:
13686 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
13687 Return true if the attributes are present and valid, otherwise,
13688 return false. */
13689
13690template <typename Callback>
13691static bool
13692dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
13693 Callback &&callback)
13694{
5e22e966 13695 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 13696 struct objfile *objfile = dwarf2_per_objfile->objfile;
43988095 13697 bfd *obfd = objfile->obfd;
43988095 13698 /* Base address selection entry. */
2b24b6e4 13699 gdb::optional<CORE_ADDR> base;
43988095 13700 const gdb_byte *buffer;
43988095
JK
13701 CORE_ADDR baseaddr;
13702 bool overflow = false;
13703
43988095
JK
13704 base = cu->base_address;
13705
5989a64e
SM
13706 dwarf2_per_objfile->per_bfd->rnglists.read (objfile);
13707 if (offset >= dwarf2_per_objfile->per_bfd->rnglists.size)
43988095 13708 {
b98664d3 13709 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
13710 offset);
13711 return false;
13712 }
5989a64e 13713 buffer = dwarf2_per_objfile->per_bfd->rnglists.buffer + offset;
43988095 13714
b3b3bada 13715 baseaddr = objfile->text_section_offset ();
43988095
JK
13716
13717 while (1)
13718 {
7814882a
JK
13719 /* Initialize it due to a false compiler warning. */
13720 CORE_ADDR range_beginning = 0, range_end = 0;
5989a64e
SM
13721 const gdb_byte *buf_end = (dwarf2_per_objfile->per_bfd->rnglists.buffer
13722 + dwarf2_per_objfile->per_bfd->rnglists.size);
43988095
JK
13723 unsigned int bytes_read;
13724
13725 if (buffer == buf_end)
13726 {
13727 overflow = true;
13728 break;
13729 }
13730 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
13731 switch (rlet)
13732 {
13733 case DW_RLE_end_of_list:
13734 break;
13735 case DW_RLE_base_address:
13736 if (buffer + cu->header.addr_size > buf_end)
13737 {
13738 overflow = true;
13739 break;
13740 }
c8a7a66f 13741 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13742 buffer += bytes_read;
13743 break;
13744 case DW_RLE_start_length:
13745 if (buffer + cu->header.addr_size > buf_end)
13746 {
13747 overflow = true;
13748 break;
13749 }
c8a7a66f
TT
13750 range_beginning = cu->header.read_address (obfd, buffer,
13751 &bytes_read);
43988095
JK
13752 buffer += bytes_read;
13753 range_end = (range_beginning
13754 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
13755 buffer += bytes_read;
13756 if (buffer > buf_end)
13757 {
13758 overflow = true;
13759 break;
13760 }
13761 break;
13762 case DW_RLE_offset_pair:
13763 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13764 buffer += bytes_read;
13765 if (buffer > buf_end)
13766 {
13767 overflow = true;
13768 break;
13769 }
13770 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
13771 buffer += bytes_read;
13772 if (buffer > buf_end)
13773 {
13774 overflow = true;
13775 break;
13776 }
13777 break;
13778 case DW_RLE_start_end:
13779 if (buffer + 2 * cu->header.addr_size > buf_end)
13780 {
13781 overflow = true;
13782 break;
13783 }
c8a7a66f
TT
13784 range_beginning = cu->header.read_address (obfd, buffer,
13785 &bytes_read);
43988095 13786 buffer += bytes_read;
c8a7a66f 13787 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
13788 buffer += bytes_read;
13789 break;
13790 default:
b98664d3 13791 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13792 return false;
13793 }
13794 if (rlet == DW_RLE_end_of_list || overflow)
13795 break;
13796 if (rlet == DW_RLE_base_address)
13797 continue;
13798
2b24b6e4 13799 if (!base.has_value ())
43988095
JK
13800 {
13801 /* We have no valid base address for the ranges
13802 data. */
b98664d3 13803 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
13804 return false;
13805 }
13806
13807 if (range_beginning > range_end)
13808 {
13809 /* Inverted range entries are invalid. */
b98664d3 13810 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
13811 return false;
13812 }
13813
13814 /* Empty range entries have no effect. */
13815 if (range_beginning == range_end)
13816 continue;
13817
2b24b6e4
TT
13818 range_beginning += *base;
13819 range_end += *base;
43988095
JK
13820
13821 /* A not-uncommon case of bad debug info.
13822 Don't pollute the addrmap with bad data. */
13823 if (range_beginning + baseaddr == 0
5989a64e 13824 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
43988095 13825 {
b98664d3 13826 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
13827 " [in module %s]"), objfile_name (objfile));
13828 continue;
13829 }
13830
13831 callback (range_beginning, range_end);
13832 }
13833
13834 if (overflow)
13835 {
b98664d3 13836 complaint (_("Offset %d is not terminated "
43988095
JK
13837 "for DW_AT_ranges attribute"),
13838 offset);
13839 return false;
13840 }
13841
13842 return true;
13843}
13844
13845/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
13846 Callback's type should be:
13847 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 13848 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 13849
43988095 13850template <typename Callback>
43039443 13851static int
5f46c5a5 13852dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu,
43988095 13853 Callback &&callback)
43039443 13854{
5e22e966
SM
13855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13856 struct objfile *objfile = per_objfile->objfile;
43039443
JK
13857 struct comp_unit_head *cu_header = &cu->header;
13858 bfd *obfd = objfile->obfd;
13859 unsigned int addr_size = cu_header->addr_size;
13860 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
13861 /* Base address selection entry. */
2b24b6e4 13862 gdb::optional<CORE_ADDR> base;
43039443 13863 unsigned int dummy;
d521ce57 13864 const gdb_byte *buffer;
ff013f42 13865 CORE_ADDR baseaddr;
43039443 13866
43988095
JK
13867 if (cu_header->version >= 5)
13868 return dwarf2_rnglists_process (offset, cu, callback);
13869
d00adf39 13870 base = cu->base_address;
43039443 13871
5e22e966
SM
13872 per_objfile->per_bfd->ranges.read (objfile);
13873 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 13874 {
b98664d3 13875 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
13876 offset);
13877 return 0;
13878 }
5e22e966 13879 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 13880
b3b3bada 13881 baseaddr = objfile->text_section_offset ();
ff013f42 13882
43039443
JK
13883 while (1)
13884 {
13885 CORE_ADDR range_beginning, range_end;
13886
c8a7a66f 13887 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 13888 buffer += addr_size;
c8a7a66f 13889 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
13890 buffer += addr_size;
13891 offset += 2 * addr_size;
13892
13893 /* An end of list marker is a pair of zero addresses. */
13894 if (range_beginning == 0 && range_end == 0)
13895 /* Found the end of list entry. */
13896 break;
13897
13898 /* Each base address selection entry is a pair of 2 values.
13899 The first is the largest possible address, the second is
13900 the base address. Check for a base address here. */
13901 if ((range_beginning & mask) == mask)
13902 {
28d2bfb9
AB
13903 /* If we found the largest possible address, then we already
13904 have the base address in range_end. */
13905 base = range_end;
43039443
JK
13906 continue;
13907 }
13908
2b24b6e4 13909 if (!base.has_value ())
43039443
JK
13910 {
13911 /* We have no valid base address for the ranges
13912 data. */
b98664d3 13913 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
13914 return 0;
13915 }
13916
9277c30c
UW
13917 if (range_beginning > range_end)
13918 {
13919 /* Inverted range entries are invalid. */
b98664d3 13920 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
13921 return 0;
13922 }
13923
13924 /* Empty range entries have no effect. */
13925 if (range_beginning == range_end)
13926 continue;
13927
2b24b6e4
TT
13928 range_beginning += *base;
13929 range_end += *base;
43039443 13930
01093045
DE
13931 /* A not-uncommon case of bad debug info.
13932 Don't pollute the addrmap with bad data. */
13933 if (range_beginning + baseaddr == 0
5e22e966 13934 && !per_objfile->per_bfd->has_section_at_zero)
01093045 13935 {
b98664d3 13936 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 13937 " [in module %s]"), objfile_name (objfile));
01093045
DE
13938 continue;
13939 }
13940
5f46c5a5
JK
13941 callback (range_beginning, range_end);
13942 }
13943
13944 return 1;
13945}
13946
13947/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
13948 Return 1 if the attributes are present and valid, otherwise, return 0.
13949 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
13950
13951static int
13952dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
13953 CORE_ADDR *high_return, struct dwarf2_cu *cu,
891813be 13954 dwarf2_psymtab *ranges_pst)
5f46c5a5 13955{
5e22e966 13956 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13957 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 13958 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
13959 int low_set = 0;
13960 CORE_ADDR low = 0;
13961 CORE_ADDR high = 0;
13962 int retval;
13963
13964 retval = dwarf2_ranges_process (offset, cu,
13965 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
13966 {
9277c30c 13967 if (ranges_pst != NULL)
3e29f34a
MR
13968 {
13969 CORE_ADDR lowpc;
13970 CORE_ADDR highpc;
13971
79748972
TT
13972 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13973 range_beginning + baseaddr)
13974 - baseaddr);
13975 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
13976 range_end + baseaddr)
13977 - baseaddr);
d320c2b5
TT
13978 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
13979 lowpc, highpc - 1, ranges_pst);
3e29f34a 13980 }
ff013f42 13981
43039443
JK
13982 /* FIXME: This is recording everything as a low-high
13983 segment of consecutive addresses. We should have a
13984 data structure for discontiguous block ranges
13985 instead. */
13986 if (! low_set)
13987 {
13988 low = range_beginning;
13989 high = range_end;
13990 low_set = 1;
13991 }
13992 else
13993 {
13994 if (range_beginning < low)
13995 low = range_beginning;
13996 if (range_end > high)
13997 high = range_end;
13998 }
5f46c5a5
JK
13999 });
14000 if (!retval)
14001 return 0;
43039443
JK
14002
14003 if (! low_set)
14004 /* If the first entry is an end-of-list marker, the range
14005 describes an empty scope, i.e. no instructions. */
14006 return 0;
14007
14008 if (low_return)
14009 *low_return = low;
14010 if (high_return)
14011 *high_return = high;
14012 return 1;
14013}
14014
3a2b436a
JK
14015/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14016 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14017 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14018
3a2b436a 14019static enum pc_bounds_kind
af34e669 14020dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14021 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14022 dwarf2_psymtab *pst)
c906108c 14023{
5e22e966 14024 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
c906108c 14025 struct attribute *attr;
91da1414 14026 struct attribute *attr_high;
af34e669
DJ
14027 CORE_ADDR low = 0;
14028 CORE_ADDR high = 0;
e385593e 14029 enum pc_bounds_kind ret;
c906108c 14030
91da1414
MW
14031 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14032 if (attr_high)
af34e669 14033 {
e142c38c 14034 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14035 if (attr != nullptr)
91da1414 14036 {
cd6c91b4
TT
14037 low = attr->value_as_address ();
14038 high = attr_high->value_as_address ();
14039 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14040 high += low;
91da1414 14041 }
af34e669
DJ
14042 else
14043 /* Found high w/o low attribute. */
e385593e 14044 return PC_BOUNDS_INVALID;
af34e669
DJ
14045
14046 /* Found consecutive range of addresses. */
3a2b436a 14047 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14048 }
c906108c 14049 else
af34e669 14050 {
e142c38c 14051 attr = dwarf2_attr (die, DW_AT_ranges, cu);
af34e669
DJ
14052 if (attr != NULL)
14053 {
18a8505e 14054 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14055 We take advantage of the fact that DW_AT_ranges does not appear
14056 in DW_TAG_compile_unit of DWO files. */
14057 int need_ranges_base = die->tag != DW_TAG_compile_unit;
14058 unsigned int ranges_offset = (DW_UNSND (attr)
14059 + (need_ranges_base
14060 ? cu->ranges_base
14061 : 0));
2e3cf129 14062
af34e669 14063 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14064 .debug_ranges section. */
2e3cf129 14065 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst))
e385593e 14066 return PC_BOUNDS_INVALID;
43039443 14067 /* Found discontinuous range of addresses. */
3a2b436a 14068 ret = PC_BOUNDS_RANGES;
af34e669 14069 }
e385593e
JK
14070 else
14071 return PC_BOUNDS_NOT_PRESENT;
af34e669 14072 }
c906108c 14073
48fbe735 14074 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14075 if (high <= low)
e385593e 14076 return PC_BOUNDS_INVALID;
c906108c
SS
14077
14078 /* When using the GNU linker, .gnu.linkonce. sections are used to
14079 eliminate duplicate copies of functions and vtables and such.
14080 The linker will arbitrarily choose one and discard the others.
14081 The AT_*_pc values for such functions refer to local labels in
14082 these sections. If the section from that file was discarded, the
14083 labels are not in the output, so the relocs get a value of 0.
14084 If this is a discarded function, mark the pc bounds as invalid,
14085 so that GDB will ignore it. */
5989a64e 14086 if (low == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
e385593e 14087 return PC_BOUNDS_INVALID;
c906108c
SS
14088
14089 *lowpc = low;
96408a79
SA
14090 if (highpc)
14091 *highpc = high;
af34e669 14092 return ret;
c906108c
SS
14093}
14094
b084d499
JB
14095/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14096 its low and high PC addresses. Do nothing if these addresses could not
14097 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14098 and HIGHPC to the high address if greater than HIGHPC. */
14099
14100static void
14101dwarf2_get_subprogram_pc_bounds (struct die_info *die,
14102 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14103 struct dwarf2_cu *cu)
14104{
14105 CORE_ADDR low, high;
14106 struct die_info *child = die->child;
14107
e385593e 14108 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14109 {
325fac50
PA
14110 *lowpc = std::min (*lowpc, low);
14111 *highpc = std::max (*highpc, high);
b084d499
JB
14112 }
14113
14114 /* If the language does not allow nested subprograms (either inside
14115 subprograms or lexical blocks), we're done. */
14116 if (cu->language != language_ada)
14117 return;
6e70227d 14118
b084d499
JB
14119 /* Check all the children of the given DIE. If it contains nested
14120 subprograms, then check their pc bounds. Likewise, we need to
14121 check lexical blocks as well, as they may also contain subprogram
14122 definitions. */
14123 while (child && child->tag)
14124 {
14125 if (child->tag == DW_TAG_subprogram
14126 || child->tag == DW_TAG_lexical_block)
14127 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14128 child = child->sibling;
b084d499
JB
14129 }
14130}
14131
fae299cd
DC
14132/* Get the low and high pc's represented by the scope DIE, and store
14133 them in *LOWPC and *HIGHPC. If the correct values can't be
14134 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14135
14136static void
14137get_scope_pc_bounds (struct die_info *die,
14138 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14139 struct dwarf2_cu *cu)
14140{
14141 CORE_ADDR best_low = (CORE_ADDR) -1;
14142 CORE_ADDR best_high = (CORE_ADDR) 0;
14143 CORE_ADDR current_low, current_high;
14144
3a2b436a 14145 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14146 >= PC_BOUNDS_RANGES)
fae299cd
DC
14147 {
14148 best_low = current_low;
14149 best_high = current_high;
14150 }
14151 else
14152 {
14153 struct die_info *child = die->child;
14154
14155 while (child && child->tag)
14156 {
14157 switch (child->tag) {
14158 case DW_TAG_subprogram:
b084d499 14159 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14160 break;
14161 case DW_TAG_namespace:
f55ee35c 14162 case DW_TAG_module:
fae299cd
DC
14163 /* FIXME: carlton/2004-01-16: Should we do this for
14164 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14165 that current GCC's always emit the DIEs corresponding
14166 to definitions of methods of classes as children of a
14167 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14168 the DIEs giving the declarations, which could be
14169 anywhere). But I don't see any reason why the
14170 standards says that they have to be there. */
14171 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14172
14173 if (current_low != ((CORE_ADDR) -1))
14174 {
325fac50
PA
14175 best_low = std::min (best_low, current_low);
14176 best_high = std::max (best_high, current_high);
fae299cd
DC
14177 }
14178 break;
14179 default:
0963b4bd 14180 /* Ignore. */
fae299cd
DC
14181 break;
14182 }
14183
436c571c 14184 child = child->sibling;
fae299cd
DC
14185 }
14186 }
14187
14188 *lowpc = best_low;
14189 *highpc = best_high;
14190}
14191
801e3a5b
JB
14192/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14193 in DIE. */
380bca97 14194
801e3a5b
JB
14195static void
14196dwarf2_record_block_ranges (struct die_info *die, struct block *block,
14197 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
14198{
5e22e966 14199 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14200 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14201 struct attribute *attr;
91da1414 14202 struct attribute *attr_high;
801e3a5b 14203
91da1414
MW
14204 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14205 if (attr_high)
801e3a5b 14206 {
801e3a5b 14207 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14208 if (attr != nullptr)
801e3a5b 14209 {
cd6c91b4
TT
14210 CORE_ADDR low = attr->value_as_address ();
14211 CORE_ADDR high = attr_high->value_as_address ();
31aa7e4e 14212
cd6c91b4 14213 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14214 high += low;
9a619af0 14215
3e29f34a
MR
14216 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14217 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14218 cu->get_builder ()->record_block_range (block, low, high - 1);
801e3a5b
JB
14219 }
14220 }
14221
14222 attr = dwarf2_attr (die, DW_AT_ranges, cu);
435d3d88 14223 if (attr != nullptr)
801e3a5b 14224 {
18a8505e 14225 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259
DE
14226 We take advantage of the fact that DW_AT_ranges does not appear
14227 in DW_TAG_compile_unit of DWO files. */
14228 int need_ranges_base = die->tag != DW_TAG_compile_unit;
801e3a5b
JB
14229
14230 /* The value of the DW_AT_ranges attribute is the offset of the
14231 address range list in the .debug_ranges section. */
ab435259
DE
14232 unsigned long offset = (DW_UNSND (attr)
14233 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14234
2d5f09ec 14235 std::vector<blockrange> blockvec;
5f46c5a5
JK
14236 dwarf2_ranges_process (offset, cu,
14237 [&] (CORE_ADDR start, CORE_ADDR end)
14238 {
58fdfd2c
JK
14239 start += baseaddr;
14240 end += baseaddr;
5f46c5a5
JK
14241 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14242 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14243 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14244 blockvec.emplace_back (start, end);
5f46c5a5 14245 });
2d5f09ec
KB
14246
14247 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14248 }
14249}
14250
685b1105
JK
14251/* Check whether the producer field indicates either of GCC < 4.6, or the
14252 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14253
685b1105
JK
14254static void
14255check_producer (struct dwarf2_cu *cu)
60d5a603 14256{
38360086 14257 int major, minor;
60d5a603
JK
14258
14259 if (cu->producer == NULL)
14260 {
14261 /* For unknown compilers expect their behavior is DWARF version
14262 compliant.
14263
14264 GCC started to support .debug_types sections by -gdwarf-4 since
14265 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14266 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14267 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14268 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14269 }
b1ffba5a 14270 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14271 {
38360086
MW
14272 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14273 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14274 }
5230b05a 14275 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14276 {
14277 cu->producer_is_icc = true;
14278 cu->producer_is_icc_lt_14 = major < 14;
14279 }
c258c396
JD
14280 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14281 cu->producer_is_codewarrior = true;
685b1105
JK
14282 else
14283 {
14284 /* For other non-GCC compilers, expect their behavior is DWARF version
14285 compliant. */
60d5a603
JK
14286 }
14287
9068261f 14288 cu->checked_producer = true;
685b1105 14289}
ba919b58 14290
685b1105
JK
14291/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14292 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14293 during 4.6.0 experimental. */
14294
9068261f 14295static bool
685b1105
JK
14296producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14297{
14298 if (!cu->checked_producer)
14299 check_producer (cu);
14300
14301 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14302}
14303
c258c396
JD
14304
14305/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14306 with incorrect is_stmt attributes. */
14307
14308static bool
14309producer_is_codewarrior (struct dwarf2_cu *cu)
14310{
14311 if (!cu->checked_producer)
14312 check_producer (cu);
14313
14314 return cu->producer_is_codewarrior;
14315}
14316
405feb71 14317/* Return the default accessibility type if it is not overridden by
60d5a603
JK
14318 DW_AT_accessibility. */
14319
14320static enum dwarf_access_attribute
14321dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
14322{
14323 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14324 {
14325 /* The default DWARF 2 accessibility for members is public, the default
14326 accessibility for inheritance is private. */
14327
14328 if (die->tag != DW_TAG_inheritance)
14329 return DW_ACCESS_public;
14330 else
14331 return DW_ACCESS_private;
14332 }
14333 else
14334 {
14335 /* DWARF 3+ defines the default accessibility a different way. The same
14336 rules apply now for DW_TAG_inheritance as for the members and it only
14337 depends on the container kind. */
14338
14339 if (die->parent->tag == DW_TAG_class_type)
14340 return DW_ACCESS_private;
14341 else
14342 return DW_ACCESS_public;
14343 }
14344}
14345
74ac6d43
TT
14346/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14347 offset. If the attribute was not found return 0, otherwise return
14348 1. If it was found but could not properly be handled, set *OFFSET
14349 to 0. */
14350
14351static int
14352handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14353 LONGEST *offset)
14354{
14355 struct attribute *attr;
14356
14357 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14358 if (attr != NULL)
14359 {
14360 *offset = 0;
14361
14362 /* Note that we do not check for a section offset first here.
14363 This is because DW_AT_data_member_location is new in DWARF 4,
14364 so if we see it, we can assume that a constant form is really
14365 a constant and not a section offset. */
cd6c91b4 14366 if (attr->form_is_constant ())
0826b30a 14367 *offset = attr->constant_value (0);
cd6c91b4 14368 else if (attr->form_is_section_offset ())
74ac6d43 14369 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 14370 else if (attr->form_is_block ())
74ac6d43
TT
14371 *offset = decode_locdesc (DW_BLOCK (attr), cu);
14372 else
14373 dwarf2_complex_location_expr_complaint ();
14374
14375 return 1;
14376 }
14377
14378 return 0;
14379}
14380
7d79de9a
TT
14381/* Look for DW_AT_data_member_location and store the results in FIELD. */
14382
14383static void
14384handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14385 struct field *field)
14386{
14387 struct attribute *attr;
14388
14389 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14390 if (attr != NULL)
14391 {
14392 if (attr->form_is_constant ())
14393 {
14394 LONGEST offset = attr->constant_value (0);
14395 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14396 }
14397 else if (attr->form_is_section_offset ())
14398 dwarf2_complex_location_expr_complaint ();
14399 else if (attr->form_is_block ())
14400 {
14401 bool handled;
14402 CORE_ADDR offset = decode_locdesc (DW_BLOCK (attr), cu, &handled);
14403 if (handled)
14404 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
14405 else
14406 {
5e22e966
SM
14407 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14408 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
14409 struct dwarf2_locexpr_baton *dlbaton
14410 = XOBNEW (&objfile->objfile_obstack,
14411 struct dwarf2_locexpr_baton);
14412 dlbaton->data = DW_BLOCK (attr)->data;
14413 dlbaton->size = DW_BLOCK (attr)->size;
14414 /* When using this baton, we want to compute the address
14415 of the field, not the value. This is why
14416 is_reference is set to false here. */
14417 dlbaton->is_reference = false;
5e22e966 14418 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
14419 dlbaton->per_cu = cu->per_cu;
14420
14421 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
14422 }
14423 }
14424 else
14425 dwarf2_complex_location_expr_complaint ();
14426 }
14427}
14428
c906108c
SS
14429/* Add an aggregate field to the field list. */
14430
14431static void
107d2387 14432dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 14433 struct dwarf2_cu *cu)
6e70227d 14434{
5e22e966 14435 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14436 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
14437 struct nextfield *new_field;
14438 struct attribute *attr;
14439 struct field *fp;
15d034d0 14440 const char *fieldname = "";
c906108c 14441
7d0ccb61
DJ
14442 if (die->tag == DW_TAG_inheritance)
14443 {
be2daae6
TT
14444 fip->baseclasses.emplace_back ();
14445 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
14446 }
14447 else
14448 {
be2daae6
TT
14449 fip->fields.emplace_back ();
14450 new_field = &fip->fields.back ();
7d0ccb61 14451 }
be2daae6 14452
9c6a1327
TT
14453 new_field->offset = die->sect_off;
14454
e142c38c 14455 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 14456 if (attr != nullptr)
c906108c 14457 new_field->accessibility = DW_UNSND (attr);
60d5a603
JK
14458 else
14459 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
c906108c
SS
14460 if (new_field->accessibility != DW_ACCESS_public)
14461 fip->non_public_fields = 1;
60d5a603 14462
e142c38c 14463 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 14464 if (attr != nullptr)
c906108c 14465 new_field->virtuality = DW_UNSND (attr);
60d5a603
JK
14466 else
14467 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
14468
14469 fp = &new_field->field;
a9a9bd0f 14470
e142c38c 14471 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 14472 {
a9a9bd0f 14473 /* Data member other than a C++ static data member. */
6e70227d 14474
c906108c 14475 /* Get type of field. */
e7c27a73 14476 fp->type = die_type (die, cu);
c906108c 14477
d6a843b5 14478 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 14479
c906108c 14480 /* Get bit size of field (zero if none). */
e142c38c 14481 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 14482 if (attr != nullptr)
c906108c
SS
14483 {
14484 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
14485 }
14486 else
14487 {
14488 FIELD_BITSIZE (*fp) = 0;
14489 }
14490
14491 /* Get bit offset of field. */
7d79de9a 14492 handle_data_member_location (die, cu, fp);
e142c38c 14493 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
435d3d88 14494 if (attr != nullptr)
c906108c 14495 {
d5a22e77 14496 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
14497 {
14498 /* For big endian bits, the DW_AT_bit_offset gives the
c5aa993b
JM
14499 additional bit offset from the MSB of the containing
14500 anonymous object to the MSB of the field. We don't
14501 have to do anything special since we don't need to
14502 know the size of the anonymous object. */
f41f5e61 14503 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
c906108c
SS
14504 }
14505 else
14506 {
14507 /* For little endian bits, compute the bit offset to the
c5aa993b
JM
14508 MSB of the anonymous object, subtract off the number of
14509 bits from the MSB of the field to the MSB of the
14510 object, and then subtract off the number of bits of
14511 the field itself. The result is the bit offset of
14512 the LSB of the field. */
c906108c
SS
14513 int anonymous_size;
14514 int bit_offset = DW_UNSND (attr);
14515
e142c38c 14516 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 14517 if (attr != nullptr)
c906108c
SS
14518 {
14519 /* The size of the anonymous object containing
14520 the bit field is explicit, so use the
14521 indicated size (in bytes). */
14522 anonymous_size = DW_UNSND (attr);
14523 }
14524 else
14525 {
14526 /* The size of the anonymous object containing
14527 the bit field must be inferred from the type
14528 attribute of the data member containing the
14529 bit field. */
14530 anonymous_size = TYPE_LENGTH (fp->type);
14531 }
f41f5e61
PA
14532 SET_FIELD_BITPOS (*fp,
14533 (FIELD_BITPOS (*fp)
14534 + anonymous_size * bits_per_byte
14535 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
14536 }
14537 }
da5b30da
AA
14538 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
14539 if (attr != NULL)
14540 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 14541 + attr->constant_value (0)));
c906108c
SS
14542
14543 /* Get name of field. */
39cbfefa
DJ
14544 fieldname = dwarf2_name (die, cu);
14545 if (fieldname == NULL)
14546 fieldname = "";
d8151005
DJ
14547
14548 /* The name is already allocated along with this objfile, so we don't
14549 need to duplicate it for the type. */
14550 fp->name = fieldname;
c906108c
SS
14551
14552 /* Change accessibility for artificial fields (e.g. virtual table
c5aa993b 14553 pointer or virtual base class pointer) to private. */
e142c38c 14554 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 14555 {
d48cc9dd 14556 FIELD_ARTIFICIAL (*fp) = 1;
c906108c
SS
14557 new_field->accessibility = DW_ACCESS_private;
14558 fip->non_public_fields = 1;
14559 }
14560 }
a9a9bd0f 14561 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 14562 {
a9a9bd0f
DC
14563 /* C++ static member. */
14564
14565 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
14566 is a declaration, but all versions of G++ as of this writing
14567 (so through at least 3.2.1) incorrectly generate
14568 DW_TAG_variable tags. */
6e70227d 14569
ff355380 14570 const char *physname;
c906108c 14571
a9a9bd0f 14572 /* Get name of field. */
39cbfefa
DJ
14573 fieldname = dwarf2_name (die, cu);
14574 if (fieldname == NULL)
c906108c
SS
14575 return;
14576
254e6b9e 14577 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
14578 if (attr
14579 /* Only create a symbol if this is an external value.
14580 new_symbol checks this and puts the value in the global symbol
14581 table, which we want. If it is not external, new_symbol
14582 will try to put the value in cu->list_in_scope which is wrong. */
14583 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
14584 {
14585 /* A static const member, not much different than an enum as far as
14586 we're concerned, except that we can support more types. */
14587 new_symbol (die, NULL, cu);
14588 }
14589
2df3850c 14590 /* Get physical name. */
ff355380 14591 physname = dwarf2_physname (fieldname, die, cu);
c906108c 14592
d8151005
DJ
14593 /* The name is already allocated along with this objfile, so we don't
14594 need to duplicate it for the type. */
14595 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
e7c27a73 14596 FIELD_TYPE (*fp) = die_type (die, cu);
d8151005 14597 FIELD_NAME (*fp) = fieldname;
c906108c
SS
14598 }
14599 else if (die->tag == DW_TAG_inheritance)
14600 {
74ac6d43 14601 /* C++ base class field. */
7d79de9a 14602 handle_data_member_location (die, cu, fp);
c906108c 14603 FIELD_BITSIZE (*fp) = 0;
e7c27a73 14604 FIELD_TYPE (*fp) = die_type (die, cu);
7d93a1e0 14605 FIELD_NAME (*fp) = fp->type->name ();
c906108c 14606 }
2ddeaf8a
TT
14607 else
14608 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
14609}
14610
883fd55a
KS
14611/* Can the type given by DIE define another type? */
14612
14613static bool
14614type_can_define_types (const struct die_info *die)
14615{
14616 switch (die->tag)
14617 {
14618 case DW_TAG_typedef:
14619 case DW_TAG_class_type:
14620 case DW_TAG_structure_type:
14621 case DW_TAG_union_type:
14622 case DW_TAG_enumeration_type:
14623 return true;
14624
14625 default:
14626 return false;
14627 }
14628}
14629
14630/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
14631
14632static void
883fd55a
KS
14633dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
14634 struct dwarf2_cu *cu)
6e70227d 14635{
be2daae6
TT
14636 struct decl_field fp;
14637 memset (&fp, 0, sizeof (fp));
98751a41 14638
883fd55a 14639 gdb_assert (type_can_define_types (die));
98751a41 14640
883fd55a 14641 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
14642 fp.name = dwarf2_name (die, cu);
14643 fp.type = read_type_die (die, cu);
98751a41 14644
c191a687
KS
14645 /* Save accessibility. */
14646 enum dwarf_access_attribute accessibility;
14647 struct attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14648 if (attr != NULL)
14649 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
14650 else
14651 accessibility = dwarf2_default_access_attribute (die, cu);
14652 switch (accessibility)
14653 {
14654 case DW_ACCESS_public:
14655 /* The assumed value if neither private nor protected. */
14656 break;
14657 case DW_ACCESS_private:
be2daae6 14658 fp.is_private = 1;
c191a687
KS
14659 break;
14660 case DW_ACCESS_protected:
be2daae6 14661 fp.is_protected = 1;
c191a687
KS
14662 break;
14663 default:
b98664d3 14664 complaint (_("Unhandled DW_AT_accessibility value (%x)"), accessibility);
c191a687
KS
14665 }
14666
883fd55a 14667 if (die->tag == DW_TAG_typedef)
be2daae6 14668 fip->typedef_field_list.push_back (fp);
883fd55a 14669 else
be2daae6 14670 fip->nested_types_list.push_back (fp);
98751a41
JK
14671}
14672
9c6a1327
TT
14673/* A convenience typedef that's used when finding the discriminant
14674 field for a variant part. */
1b95cdb7
SM
14675typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
14676 offset_map_type;
9c6a1327
TT
14677
14678/* Compute the discriminant range for a given variant. OBSTACK is
14679 where the results will be stored. VARIANT is the variant to
14680 process. IS_UNSIGNED indicates whether the discriminant is signed
14681 or unsigned. */
14682
14683static const gdb::array_view<discriminant_range>
14684convert_variant_range (struct obstack *obstack, const variant_field &variant,
14685 bool is_unsigned)
14686{
14687 std::vector<discriminant_range> ranges;
14688
14689 if (variant.default_branch)
14690 return {};
14691
14692 if (variant.discr_list_data == nullptr)
14693 {
14694 discriminant_range r
14695 = {variant.discriminant_value, variant.discriminant_value};
14696 ranges.push_back (r);
14697 }
14698 else
14699 {
14700 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
14701 variant.discr_list_data->size);
14702 while (!data.empty ())
14703 {
14704 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
14705 {
14706 complaint (_("invalid discriminant marker: %d"), data[0]);
14707 break;
14708 }
14709 bool is_range = data[0] == DW_DSC_range;
14710 data = data.slice (1);
14711
14712 ULONGEST low, high;
14713 unsigned int bytes_read;
14714
14715 if (data.empty ())
14716 {
14717 complaint (_("DW_AT_discr_list missing low value"));
14718 break;
14719 }
14720 if (is_unsigned)
14721 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
14722 else
14723 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
14724 &bytes_read);
14725 data = data.slice (bytes_read);
14726
14727 if (is_range)
14728 {
14729 if (data.empty ())
14730 {
14731 complaint (_("DW_AT_discr_list missing high value"));
14732 break;
14733 }
14734 if (is_unsigned)
14735 high = read_unsigned_leb128 (nullptr, data.data (),
14736 &bytes_read);
14737 else
14738 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
14739 &bytes_read);
14740 data = data.slice (bytes_read);
14741 }
14742 else
14743 high = low;
14744
14745 ranges.push_back ({ low, high });
14746 }
14747 }
14748
14749 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
14750 ranges.size ());
14751 std::copy (ranges.begin (), ranges.end (), result);
14752 return gdb::array_view<discriminant_range> (result, ranges.size ());
14753}
14754
14755static const gdb::array_view<variant_part> create_variant_parts
14756 (struct obstack *obstack,
14757 const offset_map_type &offset_map,
14758 struct field_info *fi,
14759 const std::vector<variant_part_builder> &variant_parts);
14760
14761/* Fill in a "struct variant" for a given variant field. RESULT is
14762 the variant to fill in. OBSTACK is where any needed allocations
14763 will be done. OFFSET_MAP holds the mapping from section offsets to
14764 fields for the type. FI describes the fields of the type we're
14765 processing. FIELD is the variant field we're converting. */
14766
14767static void
14768create_one_variant (variant &result, struct obstack *obstack,
14769 const offset_map_type &offset_map,
14770 struct field_info *fi, const variant_field &field)
14771{
14772 result.discriminants = convert_variant_range (obstack, field, false);
14773 result.first_field = field.first_field + fi->baseclasses.size ();
14774 result.last_field = field.last_field + fi->baseclasses.size ();
14775 result.parts = create_variant_parts (obstack, offset_map, fi,
14776 field.variant_parts);
14777}
14778
14779/* Fill in a "struct variant_part" for a given variant part. RESULT
14780 is the variant part to fill in. OBSTACK is where any needed
14781 allocations will be done. OFFSET_MAP holds the mapping from
14782 section offsets to fields for the type. FI describes the fields of
14783 the type we're processing. BUILDER is the variant part to be
14784 converted. */
14785
14786static void
14787create_one_variant_part (variant_part &result,
14788 struct obstack *obstack,
14789 const offset_map_type &offset_map,
14790 struct field_info *fi,
14791 const variant_part_builder &builder)
14792{
14793 auto iter = offset_map.find (builder.discriminant_offset);
14794 if (iter == offset_map.end ())
14795 {
14796 result.discriminant_index = -1;
14797 /* Doesn't matter. */
14798 result.is_unsigned = false;
14799 }
14800 else
14801 {
14802 result.discriminant_index = iter->second;
14803 result.is_unsigned
14804 = TYPE_UNSIGNED (FIELD_TYPE
14805 (fi->fields[result.discriminant_index].field));
14806 }
14807
14808 size_t n = builder.variants.size ();
14809 variant *output = new (obstack) variant[n];
14810 for (size_t i = 0; i < n; ++i)
14811 create_one_variant (output[i], obstack, offset_map, fi,
14812 builder.variants[i]);
14813
14814 result.variants = gdb::array_view<variant> (output, n);
14815}
14816
14817/* Create a vector of variant parts that can be attached to a type.
14818 OBSTACK is where any needed allocations will be done. OFFSET_MAP
14819 holds the mapping from section offsets to fields for the type. FI
14820 describes the fields of the type we're processing. VARIANT_PARTS
14821 is the vector to convert. */
14822
14823static const gdb::array_view<variant_part>
14824create_variant_parts (struct obstack *obstack,
14825 const offset_map_type &offset_map,
14826 struct field_info *fi,
14827 const std::vector<variant_part_builder> &variant_parts)
14828{
14829 if (variant_parts.empty ())
14830 return {};
14831
14832 size_t n = variant_parts.size ();
14833 variant_part *result = new (obstack) variant_part[n];
14834 for (size_t i = 0; i < n; ++i)
14835 create_one_variant_part (result[i], obstack, offset_map, fi,
14836 variant_parts[i]);
14837
14838 return gdb::array_view<variant_part> (result, n);
14839}
14840
14841/* Compute the variant part vector for FIP, attaching it to TYPE when
14842 done. */
14843
14844static void
14845add_variant_property (struct field_info *fip, struct type *type,
14846 struct dwarf2_cu *cu)
14847{
14848 /* Map section offsets of fields to their field index. Note the
14849 field index here does not take the number of baseclasses into
14850 account. */
14851 offset_map_type offset_map;
14852 for (int i = 0; i < fip->fields.size (); ++i)
14853 offset_map[fip->fields[i].offset] = i;
14854
5e22e966 14855 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
14856 gdb::array_view<variant_part> parts
14857 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
14858 fip->variant_parts);
14859
14860 struct dynamic_prop prop;
14861 prop.kind = PROP_VARIANT_PARTS;
14862 prop.data.variant_parts
14863 = ((gdb::array_view<variant_part> *)
14864 obstack_copy (&objfile->objfile_obstack, &parts, sizeof (parts)));
14865
5c54719c 14866 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
14867}
14868
c906108c
SS
14869/* Create the vector of fields, and attach it to the type. */
14870
14871static void
fba45db2 14872dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 14873 struct dwarf2_cu *cu)
c906108c 14874{
317f7127 14875 int nfields = fip->nfields ();
c906108c
SS
14876
14877 /* Record the field count, allocate space for the array of fields,
14878 and create blank accessibility bitfields if necessary. */
5e33d5f4 14879 type->set_num_fields (nfields);
3cabb6b0
SM
14880 type->set_fields
14881 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 14882
b4ba55a1 14883 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
14884 {
14885 ALLOCATE_CPLUS_STRUCT_TYPE (type);
14886
14887 TYPE_FIELD_PRIVATE_BITS (type) =
14888 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14889 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
14890
14891 TYPE_FIELD_PROTECTED_BITS (type) =
14892 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14893 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
14894
774b6a14
TT
14895 TYPE_FIELD_IGNORE_BITS (type) =
14896 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
14897 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
14898 }
14899
14900 /* If the type has baseclasses, allocate and clear a bit vector for
14901 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 14902 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 14903 {
be2daae6 14904 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 14905 unsigned char *pointer;
c906108c
SS
14906
14907 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 14908 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 14909 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
14910 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
14911 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
14912 }
14913
9c6a1327
TT
14914 if (!fip->variant_parts.empty ())
14915 add_variant_property (fip, type, cu);
2ddeaf8a 14916
be2daae6
TT
14917 /* Copy the saved-up fields into the field vector. */
14918 for (int i = 0; i < nfields; ++i)
c906108c 14919 {
be2daae6
TT
14920 struct nextfield &field
14921 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
14922 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 14923
ceacbf6e 14924 type->field (i) = field.field;
be2daae6 14925 switch (field.accessibility)
c906108c 14926 {
c5aa993b 14927 case DW_ACCESS_private:
b4ba55a1 14928 if (cu->language != language_ada)
be2daae6 14929 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 14930 break;
c906108c 14931
c5aa993b 14932 case DW_ACCESS_protected:
b4ba55a1 14933 if (cu->language != language_ada)
be2daae6 14934 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 14935 break;
c906108c 14936
c5aa993b
JM
14937 case DW_ACCESS_public:
14938 break;
c906108c 14939
c5aa993b
JM
14940 default:
14941 /* Unknown accessibility. Complain and treat it as public. */
14942 {
b98664d3 14943 complaint (_("unsupported accessibility %d"),
be2daae6 14944 field.accessibility);
c5aa993b
JM
14945 }
14946 break;
c906108c 14947 }
be2daae6 14948 if (i < fip->baseclasses.size ())
c906108c 14949 {
be2daae6 14950 switch (field.virtuality)
c906108c 14951 {
c5aa993b
JM
14952 case DW_VIRTUALITY_virtual:
14953 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 14954 if (cu->language == language_ada)
a73c6dcd 14955 error (_("unexpected virtuality in component of Ada type"));
be2daae6 14956 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 14957 break;
c906108c
SS
14958 }
14959 }
c906108c
SS
14960 }
14961}
14962
7d27a96d
TT
14963/* Return true if this member function is a constructor, false
14964 otherwise. */
14965
14966static int
14967dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
14968{
14969 const char *fieldname;
fe978cb0 14970 const char *type_name;
7d27a96d
TT
14971 int len;
14972
14973 if (die->parent == NULL)
14974 return 0;
14975
14976 if (die->parent->tag != DW_TAG_structure_type
14977 && die->parent->tag != DW_TAG_union_type
14978 && die->parent->tag != DW_TAG_class_type)
14979 return 0;
14980
14981 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
14982 type_name = dwarf2_name (die->parent, cu);
14983 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
14984 return 0;
14985
14986 len = strlen (fieldname);
fe978cb0
PA
14987 return (strncmp (fieldname, type_name, len) == 0
14988 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
14989}
14990
e35000a7
TBA
14991/* Check if the given VALUE is a recognized enum
14992 dwarf_defaulted_attribute constant according to DWARF5 spec,
14993 Table 7.24. */
14994
14995static bool
14996is_valid_DW_AT_defaulted (ULONGEST value)
14997{
14998 switch (value)
14999 {
15000 case DW_DEFAULTED_no:
15001 case DW_DEFAULTED_in_class:
15002 case DW_DEFAULTED_out_of_class:
15003 return true;
15004 }
15005
3142e908 15006 complaint (_("unrecognized DW_AT_defaulted value (%s)"), pulongest (value));
e35000a7
TBA
15007 return false;
15008}
15009
c906108c
SS
15010/* Add a member function to the proper fieldlist. */
15011
15012static void
107d2387 15013dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15014 struct type *type, struct dwarf2_cu *cu)
c906108c 15015{
5e22e966 15016 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15017 struct attribute *attr;
c906108c 15018 int i;
be2daae6 15019 struct fnfieldlist *flp = nullptr;
c906108c 15020 struct fn_field *fnp;
15d034d0 15021 const char *fieldname;
f792889a 15022 struct type *this_type;
60d5a603 15023 enum dwarf_access_attribute accessibility;
c906108c 15024
b4ba55a1 15025 if (cu->language == language_ada)
a73c6dcd 15026 error (_("unexpected member function in Ada type"));
b4ba55a1 15027
2df3850c 15028 /* Get name of member function. */
39cbfefa
DJ
15029 fieldname = dwarf2_name (die, cu);
15030 if (fieldname == NULL)
2df3850c 15031 return;
c906108c 15032
c906108c 15033 /* Look up member function name in fieldlist. */
be2daae6 15034 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15035 {
27bfe10e 15036 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15037 {
15038 flp = &fip->fnfieldlists[i];
15039 break;
15040 }
c906108c
SS
15041 }
15042
be2daae6
TT
15043 /* Create a new fnfieldlist if necessary. */
15044 if (flp == nullptr)
c906108c 15045 {
be2daae6
TT
15046 fip->fnfieldlists.emplace_back ();
15047 flp = &fip->fnfieldlists.back ();
c906108c 15048 flp->name = fieldname;
be2daae6 15049 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15050 }
15051
be2daae6
TT
15052 /* Create a new member function field and add it to the vector of
15053 fnfieldlists. */
15054 flp->fnfields.emplace_back ();
15055 fnp = &flp->fnfields.back ();
3da10d80
KS
15056
15057 /* Delay processing of the physname until later. */
9c37b5ae 15058 if (cu->language == language_cplus)
be2daae6
TT
15059 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15060 die, cu);
3da10d80
KS
15061 else
15062 {
1d06ead6 15063 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15064 fnp->physname = physname ? physname : "";
15065 }
15066
c906108c 15067 fnp->type = alloc_type (objfile);
f792889a 15068 this_type = read_type_die (die, cu);
78134374 15069 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15070 {
1f704f76 15071 int nparams = this_type->num_fields ();
c906108c 15072
f792889a 15073 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15074 of the method itself (TYPE_CODE_METHOD). */
15075 smash_to_method_type (fnp->type, type,
f792889a 15076 TYPE_TARGET_TYPE (this_type),
80fc5e77 15077 this_type->fields (),
1f704f76 15078 this_type->num_fields (),
f792889a 15079 TYPE_VARARGS (this_type));
c906108c
SS
15080
15081 /* Handle static member functions.
c5aa993b 15082 Dwarf2 has no clean way to discern C++ static and non-static
0963b4bd
MS
15083 member functions. G++ helps GDB by marking the first
15084 parameter for non-static member functions (which is the this
15085 pointer) as artificial. We obtain this information from
15086 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15087 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15088 fnp->voffset = VOFFSET_STATIC;
15089 }
15090 else
b98664d3 15091 complaint (_("member function type missing for '%s'"),
3da10d80 15092 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15093
15094 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15095 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15096 fnp->fcontext = die_containing_type (die, cu);
c906108c 15097
3e43a32a
MS
15098 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15099 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15100
15101 /* Get accessibility. */
e142c38c 15102 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
435d3d88 15103 if (attr != nullptr)
aead7601 15104 accessibility = (enum dwarf_access_attribute) DW_UNSND (attr);
60d5a603
JK
15105 else
15106 accessibility = dwarf2_default_access_attribute (die, cu);
15107 switch (accessibility)
c906108c 15108 {
60d5a603
JK
15109 case DW_ACCESS_private:
15110 fnp->is_private = 1;
15111 break;
15112 case DW_ACCESS_protected:
15113 fnp->is_protected = 1;
15114 break;
c906108c
SS
15115 }
15116
b02dede2 15117 /* Check for artificial methods. */
e142c38c 15118 attr = dwarf2_attr (die, DW_AT_artificial, cu);
b02dede2
DJ
15119 if (attr && DW_UNSND (attr) != 0)
15120 fnp->is_artificial = 1;
15121
e35000a7
TBA
15122 /* Check for defaulted methods. */
15123 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
15124 if (attr != nullptr && is_valid_DW_AT_defaulted (DW_UNSND (attr)))
15125 fnp->defaulted = (enum dwarf_defaulted_attribute) DW_UNSND (attr);
15126
15127 /* Check for deleted methods. */
15128 attr = dwarf2_attr (die, DW_AT_deleted, cu);
15129 if (attr != nullptr && DW_UNSND (attr) != 0)
15130 fnp->is_deleted = 1;
15131
7d27a96d
TT
15132 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15133
0d564a31 15134 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15135 function. For older versions of GCC, this is an offset in the
15136 appropriate virtual table, as specified by DW_AT_containing_type.
15137 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15138 to the object address. */
15139
e142c38c 15140 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15141 if (attr != nullptr)
8e19ed76 15142 {
4fc6c0d5 15143 if (attr->form_is_block () && DW_BLOCK (attr)->size > 0)
8e19ed76 15144 {
aec5aa8b
TT
15145 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
15146 {
15147 /* Old-style GCC. */
15148 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
15149 }
15150 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
15151 || (DW_BLOCK (attr)->size > 1
15152 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
15153 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
15154 {
aec5aa8b
TT
15155 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
15156 if ((fnp->voffset % cu->header.addr_size) != 0)
15157 dwarf2_complex_location_expr_complaint ();
15158 else
15159 fnp->voffset /= cu->header.addr_size;
15160 fnp->voffset += 2;
15161 }
15162 else
15163 dwarf2_complex_location_expr_complaint ();
15164
15165 if (!fnp->fcontext)
7e993ebf
KS
15166 {
15167 /* If there is no `this' field and no DW_AT_containing_type,
15168 we cannot actually find a base class context for the
15169 vtable! */
1f704f76 15170 if (this_type->num_fields () == 0
7e993ebf
KS
15171 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15172 {
b98664d3 15173 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15174 "function \"%s\" (offset %s)"),
15175 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15176 }
15177 else
15178 {
15179 fnp->fcontext
15180 = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
15181 }
15182 }
aec5aa8b 15183 }
cd6c91b4 15184 else if (attr->form_is_section_offset ())
8e19ed76 15185 {
4d3c2250 15186 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
15187 }
15188 else
15189 {
4d3c2250
KB
15190 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15191 fieldname);
8e19ed76 15192 }
0d564a31 15193 }
d48cc9dd
DJ
15194 else
15195 {
15196 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
15197 if (attr && DW_UNSND (attr))
15198 {
15199 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15200 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15201 "but the vtable offset is not specified"),
9d8780f0 15202 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15203 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15204 TYPE_CPLUS_DYNAMIC (type) = 1;
15205 }
15206 }
c906108c
SS
15207}
15208
15209/* Create the vector of member function fields, and attach it to the type. */
15210
15211static void
fba45db2 15212dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15213 struct dwarf2_cu *cu)
c906108c 15214{
b4ba55a1 15215 if (cu->language == language_ada)
a73c6dcd 15216 error (_("unexpected member functions in Ada type"));
b4ba55a1 15217
c906108c
SS
15218 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15219 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15220 TYPE_ALLOC (type,
15221 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15222
be2daae6 15223 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15224 {
be2daae6 15225 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15226 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15227
be2daae6
TT
15228 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15229 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15230 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15231 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15232
15233 for (int k = 0; k < nf.fnfields.size (); ++k)
15234 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15235 }
15236
be2daae6 15237 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15238}
15239
1168df01
JB
15240/* Returns non-zero if NAME is the name of a vtable member in CU's
15241 language, zero otherwise. */
15242static int
15243is_vtable_name (const char *name, struct dwarf2_cu *cu)
15244{
15245 static const char vptr[] = "_vptr";
15246
9c37b5ae
TT
15247 /* Look for the C++ form of the vtable. */
15248 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15249 return 1;
15250
15251 return 0;
15252}
15253
c0dd20ea 15254/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15255 functions, with the ABI-specified layout. If TYPE describes
15256 such a structure, smash it into a member function type.
61049d3b
DJ
15257
15258 GCC shouldn't do this; it should just output pointer to member DIEs.
15259 This is GCC PR debug/28767. */
c0dd20ea 15260
0b92b5bb
TT
15261static void
15262quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15263{
09e2d7c7 15264 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15265
15266 /* Check for a structure with no name and two children. */
1f704f76 15267 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15268 return;
c0dd20ea
DJ
15269
15270 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15271 if (TYPE_FIELD_NAME (type, 0) == NULL
15272 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15273 || TYPE_FIELD_NAME (type, 1) == NULL
15274 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15275 return;
c0dd20ea
DJ
15276
15277 /* Find the type of the method. */
0b92b5bb 15278 pfn_type = TYPE_FIELD_TYPE (type, 0);
c0dd20ea 15279 if (pfn_type == NULL
78134374
SM
15280 || pfn_type->code () != TYPE_CODE_PTR
15281 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15282 return;
c0dd20ea
DJ
15283
15284 /* Look for the "this" argument. */
15285 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15286 if (pfn_type->num_fields () == 0
0b92b5bb 15287 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
78134374 15288 || TYPE_FIELD_TYPE (pfn_type, 0)->code () != TYPE_CODE_PTR)
0b92b5bb 15289 return;
c0dd20ea 15290
09e2d7c7 15291 self_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
0b92b5bb 15292 new_type = alloc_type (objfile);
09e2d7c7 15293 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15294 pfn_type->fields (), pfn_type->num_fields (),
c0dd20ea 15295 TYPE_VARARGS (pfn_type));
0b92b5bb 15296 smash_to_methodptr_type (type, new_type);
c0dd20ea 15297}
1168df01 15298
2b4424c3
TT
15299/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15300 appropriate error checking and issuing complaints if there is a
15301 problem. */
15302
15303static ULONGEST
15304get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15305{
15306 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15307
15308 if (attr == nullptr)
15309 return 0;
15310
cd6c91b4 15311 if (!attr->form_is_constant ())
2b4424c3 15312 {
b98664d3 15313 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15314 " - DIE at %s [in module %s]"),
15315 sect_offset_str (die->sect_off),
5e22e966 15316 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15317 return 0;
15318 }
15319
15320 ULONGEST align;
15321 if (attr->form == DW_FORM_sdata)
15322 {
15323 LONGEST val = DW_SND (attr);
15324 if (val < 0)
15325 {
b98664d3 15326 complaint (_("DW_AT_alignment value must not be negative"
2b4424c3
TT
15327 " - DIE at %s [in module %s]"),
15328 sect_offset_str (die->sect_off),
5e22e966 15329 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15330 return 0;
15331 }
15332 align = val;
15333 }
15334 else
15335 align = DW_UNSND (attr);
15336
15337 if (align == 0)
15338 {
b98664d3 15339 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15340 " - DIE at %s [in module %s]"),
15341 sect_offset_str (die->sect_off),
5e22e966 15342 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15343 return 0;
15344 }
15345 if ((align & (align - 1)) != 0)
15346 {
b98664d3 15347 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15348 " - DIE at %s [in module %s]"),
15349 sect_offset_str (die->sect_off),
5e22e966 15350 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15351 return 0;
15352 }
15353
15354 return align;
15355}
15356
15357/* If the DIE has a DW_AT_alignment attribute, use its value to set
15358 the alignment for TYPE. */
15359
15360static void
15361maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
15362 struct type *type)
15363{
15364 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 15365 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
15366 " - DIE at %s [in module %s]"),
15367 sect_offset_str (die->sect_off),
5e22e966 15368 objfile_name (cu->per_objfile->objfile));
2b4424c3 15369}
685b1105 15370
e35000a7
TBA
15371/* Check if the given VALUE is a valid enum dwarf_calling_convention
15372 constant for a type, according to DWARF5 spec, Table 5.5. */
15373
15374static bool
15375is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
15376{
15377 switch (value)
15378 {
15379 case DW_CC_normal:
15380 case DW_CC_pass_by_reference:
15381 case DW_CC_pass_by_value:
15382 return true;
15383
15384 default:
15385 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15386 "(%s) for a type"), pulongest (value));
e35000a7
TBA
15387 return false;
15388 }
15389}
15390
d0922fcf
TBA
15391/* Check if the given VALUE is a valid enum dwarf_calling_convention
15392 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
15393 also according to GNU-specific values (see include/dwarf2.h). */
15394
15395static bool
15396is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
15397{
15398 switch (value)
15399 {
15400 case DW_CC_normal:
15401 case DW_CC_program:
15402 case DW_CC_nocall:
15403 return true;
15404
15405 case DW_CC_GNU_renesas_sh:
15406 case DW_CC_GNU_borland_fastcall_i386:
15407 case DW_CC_GDB_IBM_OpenCL:
15408 return true;
15409
15410 default:
15411 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 15412 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
15413 return false;
15414 }
15415}
15416
c906108c 15417/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
15418 (definition) to create a type for the structure or union. Fill in
15419 the type's name and general properties; the members will not be
83655187
DE
15420 processed until process_structure_scope. A symbol table entry for
15421 the type will also not be done until process_structure_scope (assuming
15422 the type has a name).
c906108c 15423
c767944b
DJ
15424 NOTE: we need to call these functions regardless of whether or not the
15425 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 15426 structure or union. This gets the type entered into our set of
83655187 15427 user defined types. */
c906108c 15428
f792889a 15429static struct type *
134d01f1 15430read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 15431{
5e22e966 15432 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
15433 struct type *type;
15434 struct attribute *attr;
15d034d0 15435 const char *name;
c906108c 15436
348e048f
DE
15437 /* If the definition of this type lives in .debug_types, read that type.
15438 Don't follow DW_AT_specification though, that will take us back up
15439 the chain and we want to go down. */
052c8bb8 15440 attr = die->attr (DW_AT_signature);
435d3d88 15441 if (attr != nullptr)
348e048f 15442 {
ac9ec31b 15443 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 15444
ac9ec31b 15445 /* The type's CU may not be the same as CU.
02142a6c 15446 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
15447 return set_die_type (die, type, cu);
15448 }
15449
c0dd20ea 15450 type = alloc_type (objfile);
c906108c 15451 INIT_CPLUS_SPECIFIC (type);
93311388 15452
39cbfefa
DJ
15453 name = dwarf2_name (die, cu);
15454 if (name != NULL)
c906108c 15455 {
987504bb 15456 if (cu->language == language_cplus
c44af4eb
TT
15457 || cu->language == language_d
15458 || cu->language == language_rust)
63d06c5c 15459 {
15d034d0 15460 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
15461
15462 /* dwarf2_full_name might have already finished building the DIE's
15463 type. If so, there is no need to continue. */
15464 if (get_die_type (die, cu) != NULL)
15465 return get_die_type (die, cu);
15466
d0e39ea2 15467 type->set_name (full_name);
63d06c5c
DC
15468 }
15469 else
15470 {
d8151005
DJ
15471 /* The name is already allocated along with this objfile, so
15472 we don't need to duplicate it for the type. */
d0e39ea2 15473 type->set_name (name);
63d06c5c 15474 }
c906108c
SS
15475 }
15476
15477 if (die->tag == DW_TAG_structure_type)
15478 {
67607e24 15479 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15480 }
15481 else if (die->tag == DW_TAG_union_type)
15482 {
67607e24 15483 type->set_code (TYPE_CODE_UNION);
c906108c
SS
15484 }
15485 else
15486 {
67607e24 15487 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
15488 }
15489
0cc2414c
TT
15490 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
15491 TYPE_DECLARED_CLASS (type) = 1;
15492
e35000a7
TBA
15493 /* Store the calling convention in the type if it's available in
15494 the die. Otherwise the calling convention remains set to
15495 the default value DW_CC_normal. */
15496 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
15497 if (attr != nullptr
15498 && is_valid_DW_AT_calling_convention_for_type (DW_UNSND (attr)))
15499 {
15500 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15501 TYPE_CPLUS_CALLING_CONVENTION (type)
15502 = (enum dwarf_calling_convention) (DW_UNSND (attr));
15503 }
15504
e142c38c 15505 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 15506 if (attr != nullptr)
c906108c 15507 {
cd6c91b4 15508 if (attr->form_is_constant ())
155bfbd3
JB
15509 TYPE_LENGTH (type) = DW_UNSND (attr);
15510 else
15511 {
f8e89861 15512 struct dynamic_prop prop;
293e7e51 15513 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 15514 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
155bfbd3
JB
15515 TYPE_LENGTH (type) = 0;
15516 }
c906108c
SS
15517 }
15518 else
15519 {
15520 TYPE_LENGTH (type) = 0;
15521 }
15522
2b4424c3
TT
15523 maybe_set_alignment (cu, die, type);
15524
5230b05a 15525 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 15526 {
5230b05a
WT
15527 /* ICC<14 does not output the required DW_AT_declaration on
15528 incomplete types, but gives them a size of zero. */
422b1cb0 15529 TYPE_STUB (type) = 1;
685b1105
JK
15530 }
15531 else
15532 TYPE_STUB_SUPPORTED (type) = 1;
15533
dc718098 15534 if (die_is_declaration (die, cu))
876cecd0 15535 TYPE_STUB (type) = 1;
a6c727b2
DJ
15536 else if (attr == NULL && die->child == NULL
15537 && producer_is_realview (cu->producer))
15538 /* RealView does not output the required DW_AT_declaration
15539 on incomplete types. */
15540 TYPE_STUB (type) = 1;
dc718098 15541
c906108c
SS
15542 /* We need to add the type field to the die immediately so we don't
15543 infinitely recurse when dealing with pointers to the structure
0963b4bd 15544 type within the structure itself. */
1c379e20 15545 set_die_type (die, type, cu);
c906108c 15546
7e314c57
JK
15547 /* set_die_type should be already done. */
15548 set_descriptive_type (type, die, cu);
15549
c767944b
DJ
15550 return type;
15551}
15552
9c6a1327
TT
15553static void handle_struct_member_die
15554 (struct die_info *child_die,
15555 struct type *type,
15556 struct field_info *fi,
15557 std::vector<struct symbol *> *template_args,
15558 struct dwarf2_cu *cu);
15559
15560/* A helper for handle_struct_member_die that handles
15561 DW_TAG_variant_part. */
15562
15563static void
15564handle_variant_part (struct die_info *die, struct type *type,
15565 struct field_info *fi,
15566 std::vector<struct symbol *> *template_args,
15567 struct dwarf2_cu *cu)
15568{
15569 variant_part_builder *new_part;
15570 if (fi->current_variant_part == nullptr)
15571 {
15572 fi->variant_parts.emplace_back ();
15573 new_part = &fi->variant_parts.back ();
15574 }
15575 else if (!fi->current_variant_part->processing_variant)
15576 {
15577 complaint (_("nested DW_TAG_variant_part seen "
15578 "- DIE at %s [in module %s]"),
15579 sect_offset_str (die->sect_off),
5e22e966 15580 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15581 return;
15582 }
15583 else
15584 {
15585 variant_field &current = fi->current_variant_part->variants.back ();
15586 current.variant_parts.emplace_back ();
15587 new_part = &current.variant_parts.back ();
15588 }
15589
15590 /* When we recurse, we want callees to add to this new variant
15591 part. */
15592 scoped_restore save_current_variant_part
15593 = make_scoped_restore (&fi->current_variant_part, new_part);
15594
15595 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
15596 if (discr == NULL)
15597 {
15598 /* It's a univariant form, an extension we support. */
15599 }
15600 else if (discr->form_is_ref ())
15601 {
15602 struct dwarf2_cu *target_cu = cu;
15603 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
15604
15605 new_part->discriminant_offset = target_die->sect_off;
15606 }
15607 else
15608 {
15609 complaint (_("DW_AT_discr does not have DIE reference form"
15610 " - DIE at %s [in module %s]"),
15611 sect_offset_str (die->sect_off),
5e22e966 15612 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15613 }
15614
15615 for (die_info *child_die = die->child;
15616 child_die != NULL;
15617 child_die = child_die->sibling)
15618 handle_struct_member_die (child_die, type, fi, template_args, cu);
15619}
15620
15621/* A helper for handle_struct_member_die that handles
15622 DW_TAG_variant. */
15623
15624static void
15625handle_variant (struct die_info *die, struct type *type,
15626 struct field_info *fi,
15627 std::vector<struct symbol *> *template_args,
15628 struct dwarf2_cu *cu)
15629{
15630 if (fi->current_variant_part == nullptr)
15631 {
15632 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
15633 "- DIE at %s [in module %s]"),
15634 sect_offset_str (die->sect_off),
5e22e966 15635 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15636 return;
15637 }
15638 if (fi->current_variant_part->processing_variant)
15639 {
15640 complaint (_("nested DW_TAG_variant seen "
15641 "- DIE at %s [in module %s]"),
15642 sect_offset_str (die->sect_off),
5e22e966 15643 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
15644 return;
15645 }
15646
15647 scoped_restore save_processing_variant
15648 = make_scoped_restore (&fi->current_variant_part->processing_variant,
15649 true);
15650
15651 fi->current_variant_part->variants.emplace_back ();
15652 variant_field &variant = fi->current_variant_part->variants.back ();
15653 variant.first_field = fi->fields.size ();
15654
15655 /* In a variant we want to get the discriminant and also add a
15656 field for our sole member child. */
15657 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
15658 if (discr == nullptr)
15659 {
15660 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
15661 if (discr == nullptr || DW_BLOCK (discr)->size == 0)
15662 variant.default_branch = true;
15663 else
15664 variant.discr_list_data = DW_BLOCK (discr);
15665 }
15666 else
15667 variant.discriminant_value = DW_UNSND (discr);
15668
15669 for (die_info *variant_child = die->child;
15670 variant_child != NULL;
15671 variant_child = variant_child->sibling)
15672 handle_struct_member_die (variant_child, type, fi, template_args, cu);
15673
15674 variant.last_field = fi->fields.size ();
15675}
15676
2ddeaf8a
TT
15677/* A helper for process_structure_scope that handles a single member
15678 DIE. */
15679
15680static void
15681handle_struct_member_die (struct die_info *child_die, struct type *type,
15682 struct field_info *fi,
15683 std::vector<struct symbol *> *template_args,
15684 struct dwarf2_cu *cu)
15685{
15686 if (child_die->tag == DW_TAG_member
9c6a1327 15687 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
15688 {
15689 /* NOTE: carlton/2002-11-05: A C++ static data member
15690 should be a DW_TAG_member that is a declaration, but
15691 all versions of G++ as of this writing (so through at
15692 least 3.2.1) incorrectly generate DW_TAG_variable
15693 tags for them instead. */
15694 dwarf2_add_field (fi, child_die, cu);
15695 }
15696 else if (child_die->tag == DW_TAG_subprogram)
15697 {
15698 /* Rust doesn't have member functions in the C++ sense.
15699 However, it does emit ordinary functions as children
15700 of a struct DIE. */
15701 if (cu->language == language_rust)
15702 read_func_scope (child_die, cu);
15703 else
15704 {
15705 /* C++ member function. */
15706 dwarf2_add_member_fn (fi, child_die, type, cu);
15707 }
15708 }
15709 else if (child_die->tag == DW_TAG_inheritance)
15710 {
15711 /* C++ base class field. */
15712 dwarf2_add_field (fi, child_die, cu);
15713 }
15714 else if (type_can_define_types (child_die))
15715 dwarf2_add_type_defn (fi, child_die, cu);
15716 else if (child_die->tag == DW_TAG_template_type_param
15717 || child_die->tag == DW_TAG_template_value_param)
15718 {
15719 struct symbol *arg = new_symbol (child_die, NULL, cu);
15720
15721 if (arg != NULL)
15722 template_args->push_back (arg);
15723 }
9c6a1327
TT
15724 else if (child_die->tag == DW_TAG_variant_part)
15725 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 15726 else if (child_die->tag == DW_TAG_variant)
9c6a1327 15727 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
15728}
15729
c767944b
DJ
15730/* Finish creating a structure or union type, including filling in
15731 its members and creating a symbol for it. */
15732
15733static void
15734process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
15735{
5e22e966 15736 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 15737 struct die_info *child_die;
c767944b
DJ
15738 struct type *type;
15739
15740 type = get_die_type (die, cu);
15741 if (type == NULL)
15742 type = read_structure_type (die, cu);
15743
3e1d3d8c 15744 bool has_template_parameters = false;
e142c38c 15745 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
15746 {
15747 struct field_info fi;
2f4732b0 15748 std::vector<struct symbol *> template_args;
c906108c 15749
639d11d3 15750 child_die = die->child;
c906108c
SS
15751
15752 while (child_die && child_die->tag)
15753 {
2ddeaf8a 15754 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 15755 child_die = child_die->sibling;
c906108c
SS
15756 }
15757
34eaf542 15758 /* Attach template arguments to type. */
2f4732b0 15759 if (!template_args.empty ())
34eaf542 15760 {
3e1d3d8c 15761 has_template_parameters = true;
34eaf542 15762 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 15763 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 15764 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
15765 = XOBNEWVEC (&objfile->objfile_obstack,
15766 struct symbol *,
15767 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 15768 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 15769 template_args.data (),
34eaf542
TT
15770 (TYPE_N_TEMPLATE_ARGUMENTS (type)
15771 * sizeof (struct symbol *)));
34eaf542
TT
15772 }
15773
c906108c 15774 /* Attach fields and member functions to the type. */
317f7127 15775 if (fi.nfields () > 0)
e7c27a73 15776 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 15777 if (!fi.fnfieldlists.empty ())
c906108c 15778 {
e7c27a73 15779 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 15780
c5aa993b 15781 /* Get the type which refers to the base class (possibly this
c906108c 15782 class itself) which contains the vtable pointer for the current
0d564a31
DJ
15783 class from the DW_AT_containing_type attribute. This use of
15784 DW_AT_containing_type is a GNU extension. */
c906108c 15785
e142c38c 15786 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 15787 {
e7c27a73 15788 struct type *t = die_containing_type (die, cu);
c906108c 15789
ae6ae975 15790 set_type_vptr_basetype (type, t);
c906108c
SS
15791 if (type == t)
15792 {
c906108c
SS
15793 int i;
15794
15795 /* Our own class provides vtbl ptr. */
1f704f76 15796 for (i = t->num_fields () - 1;
c906108c
SS
15797 i >= TYPE_N_BASECLASSES (t);
15798 --i)
15799 {
0d5cff50 15800 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 15801
1168df01 15802 if (is_vtable_name (fieldname, cu))
c906108c 15803 {
ae6ae975 15804 set_type_vptr_fieldno (type, i);
c906108c
SS
15805 break;
15806 }
15807 }
15808
15809 /* Complain if virtual function table field not found. */
15810 if (i < TYPE_N_BASECLASSES (t))
b98664d3 15811 complaint (_("virtual function table pointer "
3e43a32a 15812 "not found when defining class '%s'"),
7d93a1e0 15813 type->name () ? type->name () : "");
c906108c
SS
15814 }
15815 else
15816 {
ae6ae975 15817 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
15818 }
15819 }
f6235d4c 15820 else if (cu->producer
61012eef 15821 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
15822 {
15823 /* The IBM XLC compiler does not provide direct indication
15824 of the containing type, but the vtable pointer is
15825 always named __vfp. */
15826
15827 int i;
15828
1f704f76 15829 for (i = type->num_fields () - 1;
f6235d4c
EZ
15830 i >= TYPE_N_BASECLASSES (type);
15831 --i)
15832 {
15833 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
15834 {
ae6ae975
DE
15835 set_type_vptr_fieldno (type, i);
15836 set_type_vptr_basetype (type, type);
f6235d4c
EZ
15837 break;
15838 }
15839 }
15840 }
c906108c 15841 }
98751a41
JK
15842
15843 /* Copy fi.typedef_field_list linked list elements content into the
15844 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 15845 if (!fi.typedef_field_list.empty ())
98751a41 15846 {
be2daae6 15847 int count = fi.typedef_field_list.size ();
98751a41 15848
a0d7a4ff 15849 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 15850 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 15851 = ((struct decl_field *)
be2daae6
TT
15852 TYPE_ALLOC (type,
15853 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
15854 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 15855
be2daae6
TT
15856 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
15857 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 15858 }
c767944b 15859
883fd55a
KS
15860 /* Copy fi.nested_types_list linked list elements content into the
15861 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 15862 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 15863 {
be2daae6 15864 int count = fi.nested_types_list.size ();
883fd55a
KS
15865
15866 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15867 TYPE_NESTED_TYPES_ARRAY (type)
15868 = ((struct decl_field *)
be2daae6
TT
15869 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
15870 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 15871
be2daae6
TT
15872 for (int i = 0; i < fi.nested_types_list.size (); ++i)
15873 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 15874 }
c906108c 15875 }
63d06c5c 15876
bb5ed363 15877 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
15878 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
15879 cu->rust_unions.push_back (type);
0b92b5bb 15880
90aeadfc
DC
15881 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
15882 snapshots) has been known to create a die giving a declaration
15883 for a class that has, as a child, a die giving a definition for a
15884 nested class. So we have to process our children even if the
15885 current die is a declaration. Normally, of course, a declaration
15886 won't have any children at all. */
134d01f1 15887
ca040673
DE
15888 child_die = die->child;
15889
90aeadfc
DC
15890 while (child_die != NULL && child_die->tag)
15891 {
15892 if (child_die->tag == DW_TAG_member
15893 || child_die->tag == DW_TAG_variable
34eaf542
TT
15894 || child_die->tag == DW_TAG_inheritance
15895 || child_die->tag == DW_TAG_template_value_param
15896 || child_die->tag == DW_TAG_template_type_param)
134d01f1 15897 {
90aeadfc 15898 /* Do nothing. */
134d01f1 15899 }
90aeadfc
DC
15900 else
15901 process_die (child_die, cu);
134d01f1 15902
436c571c 15903 child_die = child_die->sibling;
134d01f1
DJ
15904 }
15905
fa4028e9
JB
15906 /* Do not consider external references. According to the DWARF standard,
15907 these DIEs are identified by the fact that they have no byte_size
15908 attribute, and a declaration attribute. */
15909 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
15910 || !die_is_declaration (die, cu)
15911 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
15912 {
15913 struct symbol *sym = new_symbol (die, type, cu);
15914
15915 if (has_template_parameters)
15916 {
a776957c
TT
15917 struct symtab *symtab;
15918 if (sym != nullptr)
15919 symtab = symbol_symtab (sym);
15920 else if (cu->line_header != nullptr)
15921 {
15922 /* Any related symtab will do. */
15923 symtab
7ba99d21 15924 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
15925 }
15926 else
15927 {
15928 symtab = nullptr;
15929 complaint (_("could not find suitable "
15930 "symtab for template parameter"
15931 " - DIE at %s [in module %s]"),
15932 sect_offset_str (die->sect_off),
15933 objfile_name (objfile));
15934 }
15935
15936 if (symtab != nullptr)
15937 {
15938 /* Make sure that the symtab is set on the new symbols.
15939 Even though they don't appear in this symtab directly,
15940 other parts of gdb assume that symbols do, and this is
15941 reasonably true. */
15942 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
15943 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
15944 }
3e1d3d8c
TT
15945 }
15946 }
134d01f1
DJ
15947}
15948
ed6acedd
TT
15949/* Assuming DIE is an enumeration type, and TYPE is its associated
15950 type, update TYPE using some information only available in DIE's
15951 children. In particular, the fields are computed. */
55426c9d
JB
15952
15953static void
15954update_enumeration_type_from_children (struct die_info *die,
15955 struct type *type,
15956 struct dwarf2_cu *cu)
15957{
60f7655a 15958 struct die_info *child_die;
55426c9d
JB
15959 int unsigned_enum = 1;
15960 int flag_enum = 1;
55426c9d 15961
8268c778 15962 auto_obstack obstack;
ed6acedd 15963 std::vector<struct field> fields;
55426c9d 15964
60f7655a
DE
15965 for (child_die = die->child;
15966 child_die != NULL && child_die->tag;
436c571c 15967 child_die = child_die->sibling)
55426c9d
JB
15968 {
15969 struct attribute *attr;
15970 LONGEST value;
15971 const gdb_byte *bytes;
15972 struct dwarf2_locexpr_baton *baton;
15973 const char *name;
60f7655a 15974
55426c9d
JB
15975 if (child_die->tag != DW_TAG_enumerator)
15976 continue;
15977
15978 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
15979 if (attr == NULL)
15980 continue;
15981
15982 name = dwarf2_name (child_die, cu);
15983 if (name == NULL)
15984 name = "<anonymous enumerator>";
15985
15986 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
15987 &value, &bytes, &baton);
15988 if (value < 0)
15989 {
15990 unsigned_enum = 0;
15991 flag_enum = 0;
15992 }
55426c9d 15993 else
edd45eb0
SM
15994 {
15995 if (count_one_bits_ll (value) >= 2)
15996 flag_enum = 0;
edd45eb0 15997 }
55426c9d 15998
ed6acedd
TT
15999 fields.emplace_back ();
16000 struct field &field = fields.back ();
16001 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16002 SET_FIELD_ENUMVAL (field, value);
16003 }
16004
16005 if (!fields.empty ())
16006 {
5e33d5f4 16007 type->set_num_fields (fields.size ());
3cabb6b0
SM
16008 type->set_fields
16009 ((struct field *)
16010 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16011 memcpy (type->fields (), fields.data (),
ed6acedd 16012 sizeof (struct field) * fields.size ());
55426c9d
JB
16013 }
16014
16015 if (unsigned_enum)
16016 TYPE_UNSIGNED (type) = 1;
16017 if (flag_enum)
16018 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16019}
16020
134d01f1
DJ
16021/* Given a DW_AT_enumeration_type die, set its type. We do not
16022 complete the type's fields yet, or create any symbols. */
c906108c 16023
f792889a 16024static struct type *
134d01f1 16025read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16026{
5e22e966 16027 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16028 struct type *type;
c906108c 16029 struct attribute *attr;
0114d602 16030 const char *name;
134d01f1 16031
348e048f
DE
16032 /* If the definition of this type lives in .debug_types, read that type.
16033 Don't follow DW_AT_specification though, that will take us back up
16034 the chain and we want to go down. */
052c8bb8 16035 attr = die->attr (DW_AT_signature);
435d3d88 16036 if (attr != nullptr)
348e048f 16037 {
ac9ec31b 16038 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16039
ac9ec31b 16040 /* The type's CU may not be the same as CU.
02142a6c 16041 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16042 return set_die_type (die, type, cu);
16043 }
16044
c906108c
SS
16045 type = alloc_type (objfile);
16046
67607e24 16047 type->set_code (TYPE_CODE_ENUM);
94af9270 16048 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16049 if (name != NULL)
d0e39ea2 16050 type->set_name (name);
c906108c 16051
0626fc76
TT
16052 attr = dwarf2_attr (die, DW_AT_type, cu);
16053 if (attr != NULL)
16054 {
16055 struct type *underlying_type = die_type (die, cu);
16056
16057 TYPE_TARGET_TYPE (type) = underlying_type;
16058 }
16059
e142c38c 16060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16061 if (attr != nullptr)
c906108c
SS
16062 {
16063 TYPE_LENGTH (type) = DW_UNSND (attr);
16064 }
16065 else
16066 {
16067 TYPE_LENGTH (type) = 0;
16068 }
16069
2b4424c3
TT
16070 maybe_set_alignment (cu, die, type);
16071
137033e9
JB
16072 /* The enumeration DIE can be incomplete. In Ada, any type can be
16073 declared as private in the package spec, and then defined only
16074 inside the package body. Such types are known as Taft Amendment
16075 Types. When another package uses such a type, an incomplete DIE
16076 may be generated by the compiler. */
02eb380e 16077 if (die_is_declaration (die, cu))
876cecd0 16078 TYPE_STUB (type) = 1;
02eb380e 16079
0626fc76
TT
16080 /* If this type has an underlying type that is not a stub, then we
16081 may use its attributes. We always use the "unsigned" attribute
16082 in this situation, because ordinarily we guess whether the type
16083 is unsigned -- but the guess can be wrong and the underlying type
16084 can tell us the reality. However, we defer to a local size
16085 attribute if one exists, because this lets the compiler override
16086 the underlying type if needed. */
16087 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_STUB (TYPE_TARGET_TYPE (type)))
16088 {
9e7c9a03
HD
16089 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16090 underlying_type = check_typedef (underlying_type);
16091 TYPE_UNSIGNED (type) = TYPE_UNSIGNED (underlying_type);
0626fc76 16092 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16093 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
2b4424c3 16094 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16095 && TYPE_RAW_ALIGN (underlying_type) != 0)
16096 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16097 }
16098
3d567982
TT
16099 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16100
ed6acedd
TT
16101 set_die_type (die, type, cu);
16102
16103 /* Finish the creation of this type by using the enum's children.
16104 Note that, as usual, this must come after set_die_type to avoid
16105 infinite recursion when trying to compute the names of the
16106 enumerators. */
16107 update_enumeration_type_from_children (die, type, cu);
16108
16109 return type;
134d01f1
DJ
16110}
16111
16112/* Given a pointer to a die which begins an enumeration, process all
16113 the dies that define the members of the enumeration, and create the
16114 symbol for the enumeration type.
16115
16116 NOTE: We reverse the order of the element list. */
16117
16118static void
16119process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16120{
f792889a 16121 struct type *this_type;
134d01f1 16122
f792889a
DJ
16123 this_type = get_die_type (die, cu);
16124 if (this_type == NULL)
16125 this_type = read_enumeration_type (die, cu);
9dc481d3 16126
639d11d3 16127 if (die->child != NULL)
c906108c 16128 {
9dc481d3 16129 struct die_info *child_die;
15d034d0 16130 const char *name;
9dc481d3 16131
639d11d3 16132 child_die = die->child;
c906108c
SS
16133 while (child_die && child_die->tag)
16134 {
16135 if (child_die->tag != DW_TAG_enumerator)
16136 {
e7c27a73 16137 process_die (child_die, cu);
c906108c
SS
16138 }
16139 else
16140 {
39cbfefa
DJ
16141 name = dwarf2_name (child_die, cu);
16142 if (name)
ed6acedd 16143 new_symbol (child_die, this_type, cu);
c906108c
SS
16144 }
16145
436c571c 16146 child_die = child_die->sibling;
c906108c 16147 }
c906108c 16148 }
134d01f1 16149
6c83ed52
TT
16150 /* If we are reading an enum from a .debug_types unit, and the enum
16151 is a declaration, and the enum is not the signatured type in the
16152 unit, then we do not want to add a symbol for it. Adding a
16153 symbol would in some cases obscure the true definition of the
16154 enum, giving users an incomplete type when the definition is
16155 actually available. Note that we do not want to do this for all
16156 enums which are just declarations, because C++0x allows forward
16157 enum declarations. */
3019eac3 16158 if (cu->per_cu->is_debug_types
6c83ed52
TT
16159 && die_is_declaration (die, cu))
16160 {
52dc124a 16161 struct signatured_type *sig_type;
6c83ed52 16162
c0f78cd4 16163 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16164 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16165 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16166 return;
16167 }
16168
f792889a 16169 new_symbol (die, this_type, cu);
c906108c
SS
16170}
16171
16172/* Extract all information from a DW_TAG_array_type DIE and put it in
16173 the DIE's type field. For now, this only handles one dimensional
16174 arrays. */
16175
f792889a 16176static struct type *
e7c27a73 16177read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16178{
5e22e966 16179 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16180 struct die_info *child_die;
7e314c57 16181 struct type *type;
c906108c 16182 struct type *element_type, *range_type, *index_type;
c906108c 16183 struct attribute *attr;
15d034d0 16184 const char *name;
a405673c 16185 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 16186 unsigned int bit_stride = 0;
c906108c 16187
e7c27a73 16188 element_type = die_type (die, cu);
c906108c 16189
7e314c57
JK
16190 /* The die_type call above may have already set the type for this DIE. */
16191 type = get_die_type (die, cu);
16192 if (type)
16193 return type;
16194
dc53a7ad
JB
16195 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
16196 if (attr != NULL)
a405673c
JB
16197 {
16198 int stride_ok;
293e7e51 16199 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
16200
16201 byte_stride_prop
16202 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
16203 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
16204 prop_type);
a405673c
JB
16205 if (!stride_ok)
16206 {
b98664d3 16207 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
16208 " - DIE at %s [in module %s]"),
16209 sect_offset_str (die->sect_off),
5e22e966 16210 objfile_name (cu->per_objfile->objfile));
a405673c
JB
16211 /* Ignore this attribute. We will likely not be able to print
16212 arrays of this type correctly, but there is little we can do
16213 to help if we cannot read the attribute's value. */
16214 byte_stride_prop = NULL;
16215 }
16216 }
dc53a7ad
JB
16217
16218 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
16219 if (attr != NULL)
16220 bit_stride = DW_UNSND (attr);
16221
c906108c
SS
16222 /* Irix 6.2 native cc creates array types without children for
16223 arrays with unspecified length. */
639d11d3 16224 if (die->child == NULL)
c906108c 16225 {
46bf5051 16226 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 16227 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 16228 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 16229 byte_stride_prop, bit_stride);
f792889a 16230 return set_die_type (die, type, cu);
c906108c
SS
16231 }
16232
791afaa2 16233 std::vector<struct type *> range_types;
639d11d3 16234 child_die = die->child;
c906108c
SS
16235 while (child_die && child_die->tag)
16236 {
16237 if (child_die->tag == DW_TAG_subrange_type)
16238 {
f792889a 16239 struct type *child_type = read_type_die (child_die, cu);
9a619af0 16240
f792889a 16241 if (child_type != NULL)
a02abb62 16242 {
0963b4bd
MS
16243 /* The range type was succesfully read. Save it for the
16244 array type creation. */
791afaa2 16245 range_types.push_back (child_type);
a02abb62 16246 }
c906108c 16247 }
436c571c 16248 child_die = child_die->sibling;
c906108c
SS
16249 }
16250
16251 /* Dwarf2 dimensions are output from left to right, create the
16252 necessary array types in backwards order. */
7ca2d3a3 16253
c906108c 16254 type = element_type;
7ca2d3a3
DL
16255
16256 if (read_array_order (die, cu) == DW_ORD_col_major)
16257 {
16258 int i = 0;
9a619af0 16259
791afaa2 16260 while (i < range_types.size ())
dc53a7ad 16261 type = create_array_type_with_stride (NULL, type, range_types[i++],
a405673c 16262 byte_stride_prop, bit_stride);
7ca2d3a3
DL
16263 }
16264 else
16265 {
791afaa2 16266 size_t ndim = range_types.size ();
7ca2d3a3 16267 while (ndim-- > 0)
dc53a7ad 16268 type = create_array_type_with_stride (NULL, type, range_types[ndim],
a405673c 16269 byte_stride_prop, bit_stride);
7ca2d3a3 16270 }
c906108c 16271
f5f8a009
EZ
16272 /* Understand Dwarf2 support for vector types (like they occur on
16273 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
16274 array type. This is not part of the Dwarf2/3 standard yet, but a
16275 custom vendor extension. The main difference between a regular
16276 array and the vector variant is that vectors are passed by value
16277 to functions. */
e142c38c 16278 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 16279 if (attr != nullptr)
ea37ba09 16280 make_vector_type (type);
f5f8a009 16281
dbc98a8b
KW
16282 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
16283 implementation may choose to implement triple vectors using this
16284 attribute. */
16285 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16286 if (attr != nullptr)
dbc98a8b
KW
16287 {
16288 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
16289 TYPE_LENGTH (type) = DW_UNSND (attr);
16290 else
b98664d3 16291 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 16292 "than the total size of elements"));
dbc98a8b
KW
16293 }
16294
39cbfefa
DJ
16295 name = dwarf2_name (die, cu);
16296 if (name)
d0e39ea2 16297 type->set_name (name);
6e70227d 16298
2b4424c3
TT
16299 maybe_set_alignment (cu, die, type);
16300
0963b4bd 16301 /* Install the type in the die. */
7e314c57
JK
16302 set_die_type (die, type, cu);
16303
16304 /* set_die_type should be already done. */
b4ba55a1
JB
16305 set_descriptive_type (type, die, cu);
16306
7e314c57 16307 return type;
c906108c
SS
16308}
16309
7ca2d3a3 16310static enum dwarf_array_dim_ordering
6e70227d 16311read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
16312{
16313 struct attribute *attr;
16314
16315 attr = dwarf2_attr (die, DW_AT_ordering, cu);
16316
435d3d88 16317 if (attr != nullptr)
aead7601 16318 return (enum dwarf_array_dim_ordering) DW_SND (attr);
7ca2d3a3 16319
0963b4bd
MS
16320 /* GNU F77 is a special case, as at 08/2004 array type info is the
16321 opposite order to the dwarf2 specification, but data is still
16322 laid out as per normal fortran.
7ca2d3a3 16323
0963b4bd
MS
16324 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
16325 version checking. */
7ca2d3a3 16326
905e0470
PM
16327 if (cu->language == language_fortran
16328 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
16329 {
16330 return DW_ORD_row_major;
16331 }
16332
6e70227d 16333 switch (cu->language_defn->la_array_ordering)
7ca2d3a3
DL
16334 {
16335 case array_column_major:
16336 return DW_ORD_col_major;
16337 case array_row_major:
16338 default:
16339 return DW_ORD_row_major;
16340 };
16341}
16342
72019c9c 16343/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 16344 the DIE's type field. */
72019c9c 16345
f792889a 16346static struct type *
72019c9c
GM
16347read_set_type (struct die_info *die, struct dwarf2_cu *cu)
16348{
7e314c57
JK
16349 struct type *domain_type, *set_type;
16350 struct attribute *attr;
f792889a 16351
7e314c57
JK
16352 domain_type = die_type (die, cu);
16353
16354 /* The die_type call above may have already set the type for this DIE. */
16355 set_type = get_die_type (die, cu);
16356 if (set_type)
16357 return set_type;
16358
16359 set_type = create_set_type (NULL, domain_type);
16360
16361 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16362 if (attr != nullptr)
d09039dd 16363 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7e314c57 16364
2b4424c3
TT
16365 maybe_set_alignment (cu, die, set_type);
16366
f792889a 16367 return set_die_type (die, set_type, cu);
72019c9c 16368}
7ca2d3a3 16369
0971de02
TT
16370/* A helper for read_common_block that creates a locexpr baton.
16371 SYM is the symbol which we are marking as computed.
16372 COMMON_DIE is the DIE for the common block.
16373 COMMON_LOC is the location expression attribute for the common
16374 block itself.
16375 MEMBER_LOC is the location expression attribute for the particular
16376 member of the common block that we are processing.
16377 CU is the CU from which the above come. */
16378
16379static void
16380mark_common_block_symbol_computed (struct symbol *sym,
16381 struct die_info *common_die,
16382 struct attribute *common_loc,
16383 struct attribute *member_loc,
16384 struct dwarf2_cu *cu)
16385{
5e22e966 16386 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 16387 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
16388 struct dwarf2_locexpr_baton *baton;
16389 gdb_byte *ptr;
16390 unsigned int cu_off;
08feed99 16391 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
16392 LONGEST offset = 0;
16393
16394 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
16395 gdb_assert (common_loc->form_is_block ());
16396 gdb_assert (member_loc->form_is_block ()
cd6c91b4 16397 || member_loc->form_is_constant ());
0971de02 16398
8d749320 16399 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 16400 baton->per_objfile = per_objfile;
0971de02
TT
16401 baton->per_cu = cu->per_cu;
16402 gdb_assert (baton->per_cu);
16403
16404 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
16405
cd6c91b4 16406 if (member_loc->form_is_constant ())
0971de02 16407 {
0826b30a 16408 offset = member_loc->constant_value (0);
0971de02
TT
16409 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
16410 }
16411 else
16412 baton->size += DW_BLOCK (member_loc)->size;
16413
224c3ddb 16414 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
16415 baton->data = ptr;
16416
16417 *ptr++ = DW_OP_call4;
9c541725 16418 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
16419 store_unsigned_integer (ptr, 4, byte_order, cu_off);
16420 ptr += 4;
16421
cd6c91b4 16422 if (member_loc->form_is_constant ())
0971de02
TT
16423 {
16424 *ptr++ = DW_OP_addr;
16425 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
16426 ptr += cu->header.addr_size;
16427 }
16428 else
16429 {
16430 /* We have to copy the data here, because DW_OP_call4 will only
16431 use a DW_AT_location attribute. */
16432 memcpy (ptr, DW_BLOCK (member_loc)->data, DW_BLOCK (member_loc)->size);
16433 ptr += DW_BLOCK (member_loc)->size;
16434 }
16435
16436 *ptr++ = DW_OP_plus;
16437 gdb_assert (ptr - baton->data == baton->size);
16438
0971de02 16439 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 16440 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
16441}
16442
4357ac6c
TT
16443/* Create appropriate locally-scoped variables for all the
16444 DW_TAG_common_block entries. Also create a struct common_block
16445 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 16446 is used to separate the common blocks name namespace from regular
4357ac6c 16447 variable names. */
c906108c
SS
16448
16449static void
e7c27a73 16450read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16451{
0971de02
TT
16452 struct attribute *attr;
16453
16454 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 16455 if (attr != nullptr)
0971de02
TT
16456 {
16457 /* Support the .debug_loc offsets. */
4fc6c0d5 16458 if (attr->form_is_block ())
0971de02
TT
16459 {
16460 /* Ok. */
16461 }
cd6c91b4 16462 else if (attr->form_is_section_offset ())
0971de02
TT
16463 {
16464 dwarf2_complex_location_expr_complaint ();
16465 attr = NULL;
16466 }
16467 else
16468 {
16469 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
16470 "common block member");
16471 attr = NULL;
16472 }
16473 }
16474
639d11d3 16475 if (die->child != NULL)
c906108c 16476 {
5e22e966 16477 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
16478 struct die_info *child_die;
16479 size_t n_entries = 0, size;
16480 struct common_block *common_block;
16481 struct symbol *sym;
74ac6d43 16482
4357ac6c
TT
16483 for (child_die = die->child;
16484 child_die && child_die->tag;
436c571c 16485 child_die = child_die->sibling)
4357ac6c
TT
16486 ++n_entries;
16487
16488 size = (sizeof (struct common_block)
16489 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
16490 common_block
16491 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
16492 size);
4357ac6c
TT
16493 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
16494 common_block->n_entries = 0;
16495
16496 for (child_die = die->child;
16497 child_die && child_die->tag;
436c571c 16498 child_die = child_die->sibling)
4357ac6c
TT
16499 {
16500 /* Create the symbol in the DW_TAG_common_block block in the current
16501 symbol scope. */
e7c27a73 16502 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
16503 if (sym != NULL)
16504 {
16505 struct attribute *member_loc;
16506
16507 common_block->contents[common_block->n_entries++] = sym;
16508
16509 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
16510 cu);
16511 if (member_loc)
16512 {
16513 /* GDB has handled this for a long time, but it is
16514 not specified by DWARF. It seems to have been
16515 emitted by gfortran at least as recently as:
16516 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 16517 complaint (_("Variable in common block has "
0971de02 16518 "DW_AT_data_member_location "
9d8780f0
SM
16519 "- DIE at %s [in module %s]"),
16520 sect_offset_str (child_die->sect_off),
518817b3 16521 objfile_name (objfile));
0971de02 16522
cd6c91b4 16523 if (member_loc->form_is_section_offset ())
0971de02 16524 dwarf2_complex_location_expr_complaint ();
cd6c91b4 16525 else if (member_loc->form_is_constant ()
4fc6c0d5 16526 || member_loc->form_is_block ())
0971de02 16527 {
435d3d88 16528 if (attr != nullptr)
0971de02
TT
16529 mark_common_block_symbol_computed (sym, die, attr,
16530 member_loc, cu);
16531 }
16532 else
16533 dwarf2_complex_location_expr_complaint ();
16534 }
16535 }
c906108c 16536 }
4357ac6c
TT
16537
16538 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
16539 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
16540 }
16541}
16542
0114d602 16543/* Create a type for a C++ namespace. */
d9fa45fe 16544
0114d602
DJ
16545static struct type *
16546read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 16547{
5e22e966 16548 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16549 const char *previous_prefix, *name;
9219021c 16550 int is_anonymous;
0114d602
DJ
16551 struct type *type;
16552
16553 /* For extensions, reuse the type of the original namespace. */
16554 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
16555 {
16556 struct die_info *ext_die;
16557 struct dwarf2_cu *ext_cu = cu;
9a619af0 16558
0114d602
DJ
16559 ext_die = dwarf2_extension (die, &ext_cu);
16560 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
16561
16562 /* EXT_CU may not be the same as CU.
02142a6c 16563 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
16564 return set_die_type (die, type, cu);
16565 }
9219021c 16566
e142c38c 16567 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
16568
16569 /* Now build the name of the current namespace. */
16570
0114d602
DJ
16571 previous_prefix = determine_prefix (die, cu);
16572 if (previous_prefix[0] != '\0')
16573 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 16574 previous_prefix, name, 0, cu);
0114d602
DJ
16575
16576 /* Create the type. */
19f392bc 16577 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 16578
60531b24 16579 return set_die_type (die, type, cu);
0114d602
DJ
16580}
16581
22cee43f 16582/* Read a namespace scope. */
0114d602
DJ
16583
16584static void
16585read_namespace (struct die_info *die, struct dwarf2_cu *cu)
16586{
5e22e966 16587 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 16588 int is_anonymous;
9219021c 16589
5c4e30ca
DC
16590 /* Add a symbol associated to this if we haven't seen the namespace
16591 before. Also, add a using directive if it's an anonymous
16592 namespace. */
9219021c 16593
f2f0e013 16594 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
16595 {
16596 struct type *type;
16597
0114d602 16598 type = read_type_die (die, cu);
e7c27a73 16599 new_symbol (die, type, cu);
5c4e30ca 16600
e8e80198 16601 namespace_name (die, &is_anonymous, cu);
5c4e30ca 16602 if (is_anonymous)
0114d602
DJ
16603 {
16604 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 16605
eb1e02fd 16606 std::vector<const char *> excludes;
804d2729 16607 add_using_directive (using_directives (cu),
7d93a1e0 16608 previous_prefix, type->name (), NULL,
eb1e02fd 16609 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 16610 }
5c4e30ca 16611 }
9219021c 16612
639d11d3 16613 if (die->child != NULL)
d9fa45fe 16614 {
639d11d3 16615 struct die_info *child_die = die->child;
6e70227d 16616
d9fa45fe
DC
16617 while (child_die && child_die->tag)
16618 {
e7c27a73 16619 process_die (child_die, cu);
436c571c 16620 child_die = child_die->sibling;
d9fa45fe
DC
16621 }
16622 }
38d518c9
EZ
16623}
16624
f55ee35c
JK
16625/* Read a Fortran module as type. This DIE can be only a declaration used for
16626 imported module. Still we need that type as local Fortran "use ... only"
16627 declaration imports depend on the created type in determine_prefix. */
16628
16629static struct type *
16630read_module_type (struct die_info *die, struct dwarf2_cu *cu)
16631{
5e22e966 16632 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 16633 const char *module_name;
f55ee35c
JK
16634 struct type *type;
16635
16636 module_name = dwarf2_name (die, cu);
19f392bc 16637 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 16638
f55ee35c
JK
16639 return set_die_type (die, type, cu);
16640}
16641
5d7cb8df
JK
16642/* Read a Fortran module. */
16643
16644static void
16645read_module (struct die_info *die, struct dwarf2_cu *cu)
16646{
16647 struct die_info *child_die = die->child;
530e8392
KB
16648 struct type *type;
16649
16650 type = read_type_die (die, cu);
16651 new_symbol (die, type, cu);
5d7cb8df 16652
5d7cb8df
JK
16653 while (child_die && child_die->tag)
16654 {
16655 process_die (child_die, cu);
436c571c 16656 child_die = child_die->sibling;
5d7cb8df
JK
16657 }
16658}
16659
38d518c9
EZ
16660/* Return the name of the namespace represented by DIE. Set
16661 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
16662 namespace. */
16663
16664static const char *
e142c38c 16665namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
16666{
16667 struct die_info *current_die;
16668 const char *name = NULL;
16669
16670 /* Loop through the extensions until we find a name. */
16671
16672 for (current_die = die;
16673 current_die != NULL;
f2f0e013 16674 current_die = dwarf2_extension (die, &cu))
38d518c9 16675 {
96553a0c
DE
16676 /* We don't use dwarf2_name here so that we can detect the absence
16677 of a name -> anonymous namespace. */
7d45c7c3 16678 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 16679
38d518c9
EZ
16680 if (name != NULL)
16681 break;
16682 }
16683
16684 /* Is it an anonymous namespace? */
16685
16686 *is_anonymous = (name == NULL);
16687 if (*is_anonymous)
2b1dbab0 16688 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
16689
16690 return name;
d9fa45fe
DC
16691}
16692
c906108c
SS
16693/* Extract all information from a DW_TAG_pointer_type DIE and add to
16694 the user defined type vector. */
16695
f792889a 16696static struct type *
e7c27a73 16697read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16698{
5e22e966 16699 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 16700 struct comp_unit_head *cu_header = &cu->header;
c906108c 16701 struct type *type;
8b2dbe47
KB
16702 struct attribute *attr_byte_size;
16703 struct attribute *attr_address_class;
16704 int byte_size, addr_class;
7e314c57
JK
16705 struct type *target_type;
16706
16707 target_type = die_type (die, cu);
c906108c 16708
7e314c57
JK
16709 /* The die_type call above may have already set the type for this DIE. */
16710 type = get_die_type (die, cu);
16711 if (type)
16712 return type;
16713
16714 type = lookup_pointer_type (target_type);
8b2dbe47 16715
e142c38c 16716 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47
KB
16717 if (attr_byte_size)
16718 byte_size = DW_UNSND (attr_byte_size);
c906108c 16719 else
8b2dbe47
KB
16720 byte_size = cu_header->addr_size;
16721
e142c38c 16722 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47
KB
16723 if (attr_address_class)
16724 addr_class = DW_UNSND (attr_address_class);
16725 else
16726 addr_class = DW_ADDR_none;
16727
2b4424c3
TT
16728 ULONGEST alignment = get_alignment (cu, die);
16729
16730 /* If the pointer size, alignment, or address class is different
16731 than the default, create a type variant marked as such and set
16732 the length accordingly. */
16733 if (TYPE_LENGTH (type) != byte_size
16734 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
16735 && alignment != TYPE_RAW_ALIGN (type))
16736 || addr_class != DW_ADDR_none)
c906108c 16737 {
5e2b427d 16738 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47
KB
16739 {
16740 int type_flags;
16741
849957d9 16742 type_flags = gdbarch_address_class_type_flags
5e2b427d 16743 (gdbarch, byte_size, addr_class);
876cecd0
TT
16744 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
16745 == 0);
8b2dbe47
KB
16746 type = make_type_with_address_space (type, type_flags);
16747 }
16748 else if (TYPE_LENGTH (type) != byte_size)
16749 {
b98664d3 16750 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 16751 }
2b4424c3
TT
16752 else if (TYPE_RAW_ALIGN (type) != alignment)
16753 {
b98664d3 16754 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
16755 " - DIE at %s [in module %s]"),
16756 sect_offset_str (die->sect_off),
5e22e966 16757 objfile_name (cu->per_objfile->objfile));
2b4424c3 16758 }
6e70227d 16759 else
9a619af0
MS
16760 {
16761 /* Should we also complain about unhandled address classes? */
16762 }
c906108c 16763 }
8b2dbe47
KB
16764
16765 TYPE_LENGTH (type) = byte_size;
2b4424c3 16766 set_type_align (type, alignment);
f792889a 16767 return set_die_type (die, type, cu);
c906108c
SS
16768}
16769
16770/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
16771 the user defined type vector. */
16772
f792889a 16773static struct type *
e7c27a73 16774read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
16775{
16776 struct type *type;
16777 struct type *to_type;
16778 struct type *domain;
16779
e7c27a73
DJ
16780 to_type = die_type (die, cu);
16781 domain = die_containing_type (die, cu);
0d5de010 16782
7e314c57
JK
16783 /* The calls above may have already set the type for this DIE. */
16784 type = get_die_type (die, cu);
16785 if (type)
16786 return type;
16787
78134374 16788 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 16789 type = lookup_methodptr_type (to_type);
78134374 16790 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 16791 {
5e22e966 16792 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
16793
16794 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 16795 to_type->fields (), to_type->num_fields (),
7078baeb
TT
16796 TYPE_VARARGS (to_type));
16797 type = lookup_methodptr_type (new_type);
16798 }
0d5de010
DJ
16799 else
16800 type = lookup_memberptr_type (to_type, domain);
c906108c 16801
f792889a 16802 return set_die_type (die, type, cu);
c906108c
SS
16803}
16804
4297a3f0 16805/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
16806 the user defined type vector. */
16807
f792889a 16808static struct type *
4297a3f0
AV
16809read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
16810 enum type_code refcode)
c906108c 16811{
e7c27a73 16812 struct comp_unit_head *cu_header = &cu->header;
7e314c57 16813 struct type *type, *target_type;
c906108c
SS
16814 struct attribute *attr;
16815
4297a3f0
AV
16816 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
16817
7e314c57
JK
16818 target_type = die_type (die, cu);
16819
16820 /* The die_type call above may have already set the type for this DIE. */
16821 type = get_die_type (die, cu);
16822 if (type)
16823 return type;
16824
4297a3f0 16825 type = lookup_reference_type (target_type, refcode);
e142c38c 16826 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16827 if (attr != nullptr)
c906108c
SS
16828 {
16829 TYPE_LENGTH (type) = DW_UNSND (attr);
16830 }
16831 else
16832 {
107d2387 16833 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 16834 }
2b4424c3 16835 maybe_set_alignment (cu, die, type);
f792889a 16836 return set_die_type (die, type, cu);
c906108c
SS
16837}
16838
cf363f18
MW
16839/* Add the given cv-qualifiers to the element type of the array. GCC
16840 outputs DWARF type qualifiers that apply to an array, not the
16841 element type. But GDB relies on the array element type to carry
16842 the cv-qualifiers. This mimics section 6.7.3 of the C99
16843 specification. */
16844
16845static struct type *
16846add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
16847 struct type *base_type, int cnst, int voltl)
16848{
16849 struct type *el_type, *inner_array;
16850
16851 base_type = copy_type (base_type);
16852 inner_array = base_type;
16853
78134374 16854 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16855 {
16856 TYPE_TARGET_TYPE (inner_array) =
16857 copy_type (TYPE_TARGET_TYPE (inner_array));
16858 inner_array = TYPE_TARGET_TYPE (inner_array);
16859 }
16860
16861 el_type = TYPE_TARGET_TYPE (inner_array);
16862 cnst |= TYPE_CONST (el_type);
16863 voltl |= TYPE_VOLATILE (el_type);
16864 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
16865
16866 return set_die_type (die, base_type, cu);
16867}
16868
f792889a 16869static struct type *
e7c27a73 16870read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16871{
f792889a 16872 struct type *base_type, *cv_type;
c906108c 16873
e7c27a73 16874 base_type = die_type (die, cu);
7e314c57
JK
16875
16876 /* The die_type call above may have already set the type for this DIE. */
16877 cv_type = get_die_type (die, cu);
16878 if (cv_type)
16879 return cv_type;
16880
2f608a3a
KW
16881 /* In case the const qualifier is applied to an array type, the element type
16882 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 16883 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 16884 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 16885
f792889a
DJ
16886 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
16887 return set_die_type (die, cv_type, cu);
c906108c
SS
16888}
16889
f792889a 16890static struct type *
e7c27a73 16891read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16892{
f792889a 16893 struct type *base_type, *cv_type;
c906108c 16894
e7c27a73 16895 base_type = die_type (die, cu);
7e314c57
JK
16896
16897 /* The die_type call above may have already set the type for this DIE. */
16898 cv_type = get_die_type (die, cu);
16899 if (cv_type)
16900 return cv_type;
16901
cf363f18
MW
16902 /* In case the volatile qualifier is applied to an array type, the
16903 element type is so qualified, not the array type (section 6.7.3
16904 of C99). */
78134374 16905 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
16906 return add_array_cv_type (die, cu, base_type, 0, 1);
16907
f792889a
DJ
16908 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
16909 return set_die_type (die, cv_type, cu);
c906108c
SS
16910}
16911
06d66ee9
TT
16912/* Handle DW_TAG_restrict_type. */
16913
16914static struct type *
16915read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
16916{
16917 struct type *base_type, *cv_type;
16918
16919 base_type = die_type (die, cu);
16920
16921 /* The die_type call above may have already set the type for this DIE. */
16922 cv_type = get_die_type (die, cu);
16923 if (cv_type)
16924 return cv_type;
16925
16926 cv_type = make_restrict_type (base_type);
16927 return set_die_type (die, cv_type, cu);
16928}
16929
a2c2acaf
MW
16930/* Handle DW_TAG_atomic_type. */
16931
16932static struct type *
16933read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
16934{
16935 struct type *base_type, *cv_type;
16936
16937 base_type = die_type (die, cu);
16938
16939 /* The die_type call above may have already set the type for this DIE. */
16940 cv_type = get_die_type (die, cu);
16941 if (cv_type)
16942 return cv_type;
16943
16944 cv_type = make_atomic_type (base_type);
16945 return set_die_type (die, cv_type, cu);
16946}
16947
c906108c
SS
16948/* Extract all information from a DW_TAG_string_type DIE and add to
16949 the user defined type vector. It isn't really a user defined type,
16950 but it behaves like one, with other DIE's using an AT_user_def_type
16951 attribute to reference it. */
16952
f792889a 16953static struct type *
e7c27a73 16954read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16955{
5e22e966 16956 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 16957 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
16958 struct type *type, *range_type, *index_type, *char_type;
16959 struct attribute *attr;
216a7e6b
AB
16960 struct dynamic_prop prop;
16961 bool length_is_constant = true;
16962 LONGEST length;
16963
16964 /* There are a couple of places where bit sizes might be made use of
16965 when parsing a DW_TAG_string_type, however, no producer that we know
16966 of make use of these. Handling bit sizes that are a multiple of the
16967 byte size is easy enough, but what about other bit sizes? Lets deal
16968 with that problem when we have to. Warn about these attributes being
16969 unsupported, then parse the type and ignore them like we always
16970 have. */
16971 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
16972 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
16973 {
16974 static bool warning_printed = false;
16975 if (!warning_printed)
16976 {
16977 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
16978 "currently supported on DW_TAG_string_type."));
16979 warning_printed = true;
16980 }
16981 }
c906108c 16982
e142c38c 16983 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 16984 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
16985 {
16986 /* The string length describes the location at which the length of
16987 the string can be found. The size of the length field can be
16988 specified with one of the attributes below. */
16989 struct type *prop_type;
16990 struct attribute *len
16991 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
16992 if (len == nullptr)
16993 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 16994 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
16995 {
16996 /* Pass 0 as the default as we know this attribute is constant
16997 and the default value will not be returned. */
0826b30a 16998 LONGEST sz = len->constant_value (0);
293e7e51 16999 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17000 }
17001 else
17002 {
17003 /* If the size is not specified then we assume it is the size of
17004 an address on this target. */
293e7e51 17005 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17006 }
17007
17008 /* Convert the attribute into a dynamic property. */
17009 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17010 length = 1;
17011 else
17012 length_is_constant = false;
17013 }
17014 else if (attr != nullptr)
17015 {
17016 /* This DW_AT_string_length just contains the length with no
17017 indirection. There's no need to create a dynamic property in this
17018 case. Pass 0 for the default value as we know it will not be
17019 returned in this case. */
0826b30a 17020 length = attr->constant_value (0);
216a7e6b
AB
17021 }
17022 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17023 {
216a7e6b 17024 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17025 length = attr->constant_value (1);
c906108c
SS
17026 }
17027 else
17028 {
216a7e6b
AB
17029 /* Use 1 as a fallback length if we have nothing else. */
17030 length = 1;
c906108c 17031 }
6ccb9162 17032
46bf5051 17033 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17034 if (length_is_constant)
17035 range_type = create_static_range_type (NULL, index_type, 1, length);
17036 else
17037 {
17038 struct dynamic_prop low_bound;
17039
17040 low_bound.kind = PROP_CONST;
17041 low_bound.data.const_val = 1;
17042 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17043 }
3b7538c0
UW
17044 char_type = language_string_char_type (cu->language_defn, gdbarch);
17045 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17046
f792889a 17047 return set_die_type (die, type, cu);
c906108c
SS
17048}
17049
4d804846
JB
17050/* Assuming that DIE corresponds to a function, returns nonzero
17051 if the function is prototyped. */
17052
17053static int
17054prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17055{
17056 struct attribute *attr;
17057
17058 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
17059 if (attr && (DW_UNSND (attr) != 0))
17060 return 1;
17061
17062 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17063 is only meaningful for C, but the concept also extends to other
4d804846
JB
17064 languages that allow unprototyped functions (Eg: Objective C).
17065 For all other languages, assume that functions are always
17066 prototyped. */
17067 if (cu->language != language_c
17068 && cu->language != language_objc
17069 && cu->language != language_opencl)
17070 return 1;
17071
17072 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17073 prototyped and unprototyped functions; default to prototyped,
17074 since that is more common in modern code (and RealView warns
17075 about unprototyped functions). */
17076 if (producer_is_realview (cu->producer))
17077 return 1;
17078
17079 return 0;
17080}
17081
c906108c
SS
17082/* Handle DIES due to C code like:
17083
17084 struct foo
c5aa993b
JM
17085 {
17086 int (*funcp)(int a, long l);
17087 int b;
17088 };
c906108c 17089
0963b4bd 17090 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 17091
f792889a 17092static struct type *
e7c27a73 17093read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17094{
5e22e966 17095 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
17096 struct type *type; /* Type that this function returns. */
17097 struct type *ftype; /* Function that returns above type. */
c906108c
SS
17098 struct attribute *attr;
17099
e7c27a73 17100 type = die_type (die, cu);
7e314c57
JK
17101
17102 /* The die_type call above may have already set the type for this DIE. */
17103 ftype = get_die_type (die, cu);
17104 if (ftype)
17105 return ftype;
17106
0c8b41f1 17107 ftype = lookup_function_type (type);
c906108c 17108
4d804846 17109 if (prototyped_function_p (die, cu))
a6c727b2 17110 TYPE_PROTOTYPED (ftype) = 1;
c906108c 17111
c055b101
CV
17112 /* Store the calling convention in the type if it's available in
17113 the subroutine die. Otherwise set the calling convention to
17114 the default value DW_CC_normal. */
17115 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf
TBA
17116 if (attr != nullptr
17117 && is_valid_DW_AT_calling_convention_for_subroutine (DW_UNSND (attr)))
17118 TYPE_CALLING_CONVENTION (ftype)
17119 = (enum dwarf_calling_convention) (DW_UNSND (attr));
54fcddd0
UW
17120 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
17121 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
17122 else
17123 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 17124
743649fd
MW
17125 /* Record whether the function returns normally to its caller or not
17126 if the DWARF producer set that information. */
17127 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
17128 if (attr && (DW_UNSND (attr) != 0))
17129 TYPE_NO_RETURN (ftype) = 1;
17130
76c10ea2
GM
17131 /* We need to add the subroutine type to the die immediately so
17132 we don't infinitely recurse when dealing with parameters
0963b4bd 17133 declared as the same subroutine type. */
76c10ea2 17134 set_die_type (die, ftype, cu);
6e70227d 17135
639d11d3 17136 if (die->child != NULL)
c906108c 17137 {
bb5ed363 17138 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 17139 struct die_info *child_die;
8072405b 17140 int nparams, iparams;
c906108c
SS
17141
17142 /* Count the number of parameters.
17143 FIXME: GDB currently ignores vararg functions, but knows about
17144 vararg member functions. */
8072405b 17145 nparams = 0;
639d11d3 17146 child_die = die->child;
c906108c
SS
17147 while (child_die && child_die->tag)
17148 {
17149 if (child_die->tag == DW_TAG_formal_parameter)
17150 nparams++;
17151 else if (child_die->tag == DW_TAG_unspecified_parameters)
876cecd0 17152 TYPE_VARARGS (ftype) = 1;
436c571c 17153 child_die = child_die->sibling;
c906108c
SS
17154 }
17155
17156 /* Allocate storage for parameters and fill them in. */
5e33d5f4 17157 ftype->set_num_fields (nparams);
3cabb6b0
SM
17158 ftype->set_fields
17159 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 17160
8072405b
JK
17161 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
17162 even if we error out during the parameters reading below. */
17163 for (iparams = 0; iparams < nparams; iparams++)
17164 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
17165
17166 iparams = 0;
639d11d3 17167 child_die = die->child;
c906108c
SS
17168 while (child_die && child_die->tag)
17169 {
17170 if (child_die->tag == DW_TAG_formal_parameter)
17171 {
3ce3b1ba
PA
17172 struct type *arg_type;
17173
17174 /* DWARF version 2 has no clean way to discern C++
17175 static and non-static member functions. G++ helps
17176 GDB by marking the first parameter for non-static
17177 member functions (which is the this pointer) as
17178 artificial. We pass this information to
17179 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
17180
17181 DWARF version 3 added DW_AT_object_pointer, which GCC
17182 4.5 does not yet generate. */
e142c38c 17183 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 17184 if (attr != nullptr)
c906108c
SS
17185 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
17186 else
9c37b5ae 17187 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
17188 arg_type = die_type (child_die, cu);
17189
17190 /* RealView does not mark THIS as const, which the testsuite
17191 expects. GCC marks THIS as const in method definitions,
17192 but not in the class specifications (GCC PR 43053). */
17193 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
17194 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
17195 {
17196 int is_this = 0;
17197 struct dwarf2_cu *arg_cu = cu;
17198 const char *name = dwarf2_name (child_die, cu);
17199
17200 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 17201 if (attr != nullptr)
3ce3b1ba
PA
17202 {
17203 /* If the compiler emits this, use it. */
17204 if (follow_die_ref (die, attr, &arg_cu) == child_die)
17205 is_this = 1;
17206 }
17207 else if (name && strcmp (name, "this") == 0)
17208 /* Function definitions will have the argument names. */
17209 is_this = 1;
17210 else if (name == NULL && iparams == 0)
17211 /* Declarations may not have the names, so like
17212 elsewhere in GDB, assume an artificial first
17213 argument is "this". */
17214 is_this = 1;
17215
17216 if (is_this)
17217 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
17218 arg_type, 0);
17219 }
17220
17221 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
c906108c
SS
17222 iparams++;
17223 }
436c571c 17224 child_die = child_die->sibling;
c906108c
SS
17225 }
17226 }
17227
76c10ea2 17228 return ftype;
c906108c
SS
17229}
17230
f792889a 17231static struct type *
e7c27a73 17232read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17233{
5e22e966 17234 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17235 const char *name = NULL;
3c8e0968 17236 struct type *this_type, *target_type;
c906108c 17237
94af9270 17238 name = dwarf2_full_name (NULL, die, cu);
19f392bc
UW
17239 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
17240 TYPE_TARGET_STUB (this_type) = 1;
f792889a 17241 set_die_type (die, this_type, cu);
3c8e0968
DE
17242 target_type = die_type (die, cu);
17243 if (target_type != this_type)
17244 TYPE_TARGET_TYPE (this_type) = target_type;
17245 else
17246 {
17247 /* Self-referential typedefs are, it seems, not allowed by the DWARF
17248 spec and cause infinite loops in GDB. */
b98664d3 17249 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
17250 "- DIE at %s [in module %s]"),
17251 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
17252 TYPE_TARGET_TYPE (this_type) = NULL;
17253 }
e4003a34
TV
17254 if (name == NULL)
17255 {
17256 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
17257 anonymous typedefs, which is, strictly speaking, invalid DWARF.
17258 Handle these by just returning the target type, rather than
17259 constructing an anonymous typedef type and trying to handle this
17260 elsewhere. */
17261 set_die_type (die, target_type, cu);
17262 return target_type;
17263 }
f792889a 17264 return this_type;
c906108c
SS
17265}
17266
9b790ce7
UW
17267/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
17268 (which may be different from NAME) to the architecture back-end to allow
17269 it to guess the correct format if necessary. */
17270
17271static struct type *
17272dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 17273 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 17274{
08feed99 17275 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
17276 const struct floatformat **format;
17277 struct type *type;
17278
17279 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
17280 if (format)
103a685e 17281 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 17282 else
77b7c781 17283 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
17284
17285 return type;
17286}
17287
eb77c9df
AB
17288/* Allocate an integer type of size BITS and name NAME. */
17289
17290static struct type *
17291dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
17292 int bits, int unsigned_p, const char *name)
17293{
17294 struct type *type;
17295
17296 /* Versions of Intel's C Compiler generate an integer type called "void"
17297 instead of using DW_TAG_unspecified_type. This has been seen on
17298 at least versions 14, 17, and 18. */
35ee2dc2
AB
17299 if (bits == 0 && producer_is_icc (cu) && name != nullptr
17300 && strcmp (name, "void") == 0)
eb77c9df
AB
17301 type = objfile_type (objfile)->builtin_void;
17302 else
17303 type = init_integer_type (objfile, bits, unsigned_p, name);
17304
17305 return type;
17306}
17307
8bdc1658
AB
17308/* Initialise and return a floating point type of size BITS suitable for
17309 use as a component of a complex number. The NAME_HINT is passed through
17310 when initialising the floating point type and is the name of the complex
17311 type.
17312
17313 As DWARF doesn't currently provide an explicit name for the components
17314 of a complex number, but it can be helpful to have these components
17315 named, we try to select a suitable name based on the size of the
17316 component. */
17317static struct type *
17318dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
17319 struct objfile *objfile,
103a685e
TT
17320 int bits, const char *name_hint,
17321 enum bfd_endian byte_order)
8bdc1658 17322{
08feed99 17323 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
17324 struct type *tt = nullptr;
17325
35add35e
AB
17326 /* Try to find a suitable floating point builtin type of size BITS.
17327 We're going to use the name of this type as the name for the complex
17328 target type that we are about to create. */
1db455a7 17329 switch (cu->language)
8bdc1658 17330 {
1db455a7
AB
17331 case language_fortran:
17332 switch (bits)
17333 {
17334 case 32:
17335 tt = builtin_f_type (gdbarch)->builtin_real;
17336 break;
17337 case 64:
17338 tt = builtin_f_type (gdbarch)->builtin_real_s8;
17339 break;
17340 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17341 case 128:
17342 tt = builtin_f_type (gdbarch)->builtin_real_s16;
17343 break;
17344 }
8bdc1658 17345 break;
1db455a7
AB
17346 default:
17347 switch (bits)
17348 {
17349 case 32:
17350 tt = builtin_type (gdbarch)->builtin_float;
17351 break;
17352 case 64:
17353 tt = builtin_type (gdbarch)->builtin_double;
17354 break;
17355 case 96: /* The x86-32 ABI specifies 96-bit long double. */
17356 case 128:
17357 tt = builtin_type (gdbarch)->builtin_long_double;
17358 break;
17359 }
8bdc1658
AB
17360 break;
17361 }
17362
35add35e
AB
17363 /* If the type we found doesn't match the size we were looking for, then
17364 pretend we didn't find a type at all, the complex target type we
17365 create will then be nameless. */
a12e5744 17366 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
17367 tt = nullptr;
17368
7d93a1e0 17369 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 17370 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
17371}
17372
c906108c
SS
17373/* Find a representation of a given base type and install
17374 it in the TYPE field of the die. */
17375
f792889a 17376static struct type *
e7c27a73 17377read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17378{
5e22e966 17379 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
17380 struct type *type;
17381 struct attribute *attr;
19f392bc 17382 int encoding = 0, bits = 0;
15d034d0 17383 const char *name;
34877895 17384 gdbarch *arch;
c906108c 17385
e142c38c 17386 attr = dwarf2_attr (die, DW_AT_encoding, cu);
435d3d88 17387 if (attr != nullptr)
34877895 17388 encoding = DW_UNSND (attr);
e142c38c 17389 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17390 if (attr != nullptr)
34877895 17391 bits = DW_UNSND (attr) * TARGET_CHAR_BIT;
39cbfefa 17392 name = dwarf2_name (die, cu);
6ccb9162 17393 if (!name)
34877895 17394 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 17395
08feed99 17396 arch = objfile->arch ();
103a685e
TT
17397 enum bfd_endian byte_order = gdbarch_byte_order (arch);
17398
34877895
PJ
17399 attr = dwarf2_attr (die, DW_AT_endianity, cu);
17400 if (attr)
103a685e
TT
17401 {
17402 int endianity = DW_UNSND (attr);
17403
17404 switch (endianity)
17405 {
17406 case DW_END_big:
17407 byte_order = BFD_ENDIAN_BIG;
17408 break;
17409 case DW_END_little:
17410 byte_order = BFD_ENDIAN_LITTLE;
17411 break;
17412 default:
17413 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
17414 break;
17415 }
17416 }
6ccb9162
UW
17417
17418 switch (encoding)
c906108c 17419 {
6ccb9162
UW
17420 case DW_ATE_address:
17421 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 17422 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 17423 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
17424 break;
17425 case DW_ATE_boolean:
19f392bc 17426 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
17427 break;
17428 case DW_ATE_complex_float:
103a685e
TT
17429 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
17430 byte_order);
78134374 17431 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
17432 {
17433 if (name == nullptr)
17434 {
17435 struct obstack *obstack
5e22e966 17436 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 17437 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
17438 nullptr);
17439 }
17440 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
17441 }
17442 else
17443 type = init_complex_type (name, type);
6ccb9162
UW
17444 break;
17445 case DW_ATE_decimal_float:
19f392bc 17446 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
17447 break;
17448 case DW_ATE_float:
103a685e 17449 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
17450 break;
17451 case DW_ATE_signed:
eb77c9df 17452 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17453 break;
17454 case DW_ATE_unsigned:
3b2b8fea
TT
17455 if (cu->language == language_fortran
17456 && name
61012eef 17457 && startswith (name, "character("))
19f392bc
UW
17458 type = init_character_type (objfile, bits, 1, name);
17459 else
eb77c9df 17460 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
17461 break;
17462 case DW_ATE_signed_char:
6e70227d 17463 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
17464 || cu->language == language_pascal
17465 || cu->language == language_fortran)
19f392bc
UW
17466 type = init_character_type (objfile, bits, 0, name);
17467 else
eb77c9df 17468 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
17469 break;
17470 case DW_ATE_unsigned_char:
868a0084 17471 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 17472 || cu->language == language_pascal
c44af4eb
TT
17473 || cu->language == language_fortran
17474 || cu->language == language_rust)
19f392bc
UW
17475 type = init_character_type (objfile, bits, 1, name);
17476 else
eb77c9df 17477 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 17478 break;
75079b2b 17479 case DW_ATE_UTF:
53e710ac 17480 {
53e710ac
PA
17481 if (bits == 16)
17482 type = builtin_type (arch)->builtin_char16;
17483 else if (bits == 32)
17484 type = builtin_type (arch)->builtin_char32;
17485 else
17486 {
b98664d3 17487 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 17488 bits);
eb77c9df 17489 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
17490 }
17491 return set_die_type (die, type, cu);
17492 }
75079b2b
TT
17493 break;
17494
6ccb9162 17495 default:
b98664d3 17496 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 17497 dwarf_type_encoding_name (encoding));
77b7c781 17498 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 17499 break;
c906108c 17500 }
6ccb9162 17501
0114d602 17502 if (name && strcmp (name, "char") == 0)
876cecd0 17503 TYPE_NOSIGN (type) = 1;
0114d602 17504
2b4424c3
TT
17505 maybe_set_alignment (cu, die, type);
17506
103a685e 17507 TYPE_ENDIANITY_NOT_DEFAULT (type) = gdbarch_byte_order (arch) != byte_order;
34877895 17508
f792889a 17509 return set_die_type (die, type, cu);
c906108c
SS
17510}
17511
80180f79
SA
17512/* Parse dwarf attribute if it's a block, reference or constant and put the
17513 resulting value of the attribute into struct bound_prop.
17514 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
17515
17516static int
17517attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
17518 struct dwarf2_cu *cu, struct dynamic_prop *prop,
17519 struct type *default_type)
80180f79
SA
17520{
17521 struct dwarf2_property_baton *baton;
5e22e966 17522 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
17523 struct objfile *objfile = per_objfile->objfile;
17524 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 17525
9a49df9d
AB
17526 gdb_assert (default_type != NULL);
17527
80180f79
SA
17528 if (attr == NULL || prop == NULL)
17529 return 0;
17530
4fc6c0d5 17531 if (attr->form_is_block ())
80180f79 17532 {
8d749320 17533 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17534 baton->property_type = default_type;
80180f79 17535 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17536 baton->locexpr.per_objfile = per_objfile;
80180f79
SA
17537 baton->locexpr.size = DW_BLOCK (attr)->size;
17538 baton->locexpr.data = DW_BLOCK (attr)->data;
216a7e6b
AB
17539 switch (attr->name)
17540 {
17541 case DW_AT_string_length:
17542 baton->locexpr.is_reference = true;
17543 break;
17544 default:
17545 baton->locexpr.is_reference = false;
17546 break;
17547 }
80180f79
SA
17548 prop->data.baton = baton;
17549 prop->kind = PROP_LOCEXPR;
17550 gdb_assert (prop->data.baton != NULL);
17551 }
cd6c91b4 17552 else if (attr->form_is_ref ())
80180f79
SA
17553 {
17554 struct dwarf2_cu *target_cu = cu;
17555 struct die_info *target_die;
17556 struct attribute *target_attr;
17557
17558 target_die = follow_die_ref (die, attr, &target_cu);
17559 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
17560 if (target_attr == NULL)
17561 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
17562 target_cu);
80180f79
SA
17563 if (target_attr == NULL)
17564 return 0;
17565
df25ebbd 17566 switch (target_attr->name)
80180f79 17567 {
df25ebbd 17568 case DW_AT_location:
cd6c91b4 17569 if (target_attr->form_is_section_offset ())
df25ebbd 17570 {
8d749320 17571 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17572 baton->property_type = die_type (target_die, target_cu);
df25ebbd
JB
17573 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
17574 prop->data.baton = baton;
17575 prop->kind = PROP_LOCLIST;
17576 gdb_assert (prop->data.baton != NULL);
17577 }
4fc6c0d5 17578 else if (target_attr->form_is_block ())
df25ebbd 17579 {
8d749320 17580 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17581 baton->property_type = die_type (target_die, target_cu);
df25ebbd 17582 baton->locexpr.per_cu = cu->per_cu;
a50264ba 17583 baton->locexpr.per_objfile = per_objfile;
df25ebbd
JB
17584 baton->locexpr.size = DW_BLOCK (target_attr)->size;
17585 baton->locexpr.data = DW_BLOCK (target_attr)->data;
9a49df9d 17586 baton->locexpr.is_reference = true;
df25ebbd
JB
17587 prop->data.baton = baton;
17588 prop->kind = PROP_LOCEXPR;
17589 gdb_assert (prop->data.baton != NULL);
17590 }
17591 else
17592 {
17593 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17594 "dynamic property");
17595 return 0;
17596 }
17597 break;
17598 case DW_AT_data_member_location:
17599 {
17600 LONGEST offset;
17601
17602 if (!handle_data_member_location (target_die, target_cu,
17603 &offset))
17604 return 0;
17605
8d749320 17606 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 17607 baton->property_type = read_type_die (target_die->parent,
6ad395a7 17608 target_cu);
df25ebbd
JB
17609 baton->offset_info.offset = offset;
17610 baton->offset_info.type = die_type (target_die, target_cu);
17611 prop->data.baton = baton;
17612 prop->kind = PROP_ADDR_OFFSET;
17613 break;
17614 }
80180f79
SA
17615 }
17616 }
cd6c91b4 17617 else if (attr->form_is_constant ())
80180f79 17618 {
0826b30a 17619 prop->data.const_val = attr->constant_value (0);
80180f79
SA
17620 prop->kind = PROP_CONST;
17621 }
17622 else
17623 {
17624 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
17625 dwarf2_name (die, cu));
17626 return 0;
17627 }
17628
17629 return 1;
17630}
17631
09ba997f 17632/* See read.h. */
9a49df9d 17633
09ba997f 17634struct type *
293e7e51 17635dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 17636{
9a49df9d
AB
17637 struct type *int_type;
17638
17639 /* Helper macro to examine the various builtin types. */
11a8b164
AB
17640#define TRY_TYPE(F) \
17641 int_type = (unsigned_p \
17642 ? objfile_type (objfile)->builtin_unsigned_ ## F \
17643 : objfile_type (objfile)->builtin_ ## F); \
17644 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
17645 return int_type
17646
17647 TRY_TYPE (char);
17648 TRY_TYPE (short);
17649 TRY_TYPE (int);
17650 TRY_TYPE (long);
17651 TRY_TYPE (long_long);
17652
17653#undef TRY_TYPE
17654
17655 gdb_assert_not_reached ("unable to find suitable integer type");
17656}
17657
09ba997f 17658/* See read.h. */
11a8b164 17659
09ba997f 17660struct type *
293e7e51 17661dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 17662{
293e7e51
SM
17663 int addr_size = this->per_cu->addr_size ();
17664 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
17665}
17666
b86352cf
AB
17667/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
17668 present (which is valid) then compute the default type based on the
17669 compilation units address size. */
17670
17671static struct type *
17672read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
17673{
17674 struct type *index_type = die_type (die, cu);
17675
17676 /* Dwarf-2 specifications explicitly allows to create subrange types
17677 without specifying a base type.
17678 In that case, the base type must be set to the type of
17679 the lower bound, upper bound or count, in that order, if any of these
17680 three attributes references an object that has a type.
17681 If no base type is found, the Dwarf-2 specifications say that
17682 a signed integer type of size equal to the size of an address should
17683 be used.
17684 For the following C code: `extern char gdb_int [];'
17685 GCC produces an empty range DIE.
17686 FIXME: muller/2010-05-28: Possible references to object for low bound,
17687 high bound or count are not yet handled by this code. */
78134374 17688 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 17689 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
17690
17691 return index_type;
17692}
17693
a02abb62
JB
17694/* Read the given DW_AT_subrange DIE. */
17695
f792889a 17696static struct type *
a02abb62
JB
17697read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
17698{
4c9ad8c2 17699 struct type *base_type, *orig_base_type;
a02abb62
JB
17700 struct type *range_type;
17701 struct attribute *attr;
729efb13 17702 struct dynamic_prop low, high;
4fae6e18 17703 int low_default_is_valid;
c451ebe5 17704 int high_bound_is_count = 0;
15d034d0 17705 const char *name;
d359392f 17706 ULONGEST negative_mask;
e77813c8 17707
b86352cf
AB
17708 orig_base_type = read_subrange_index_type (die, cu);
17709
4c9ad8c2
TT
17710 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
17711 whereas the real type might be. So, we use ORIG_BASE_TYPE when
17712 creating the range type, but we use the result of check_typedef
17713 when examining properties of the type. */
17714 base_type = check_typedef (orig_base_type);
a02abb62 17715
7e314c57
JK
17716 /* The die_type call above may have already set the type for this DIE. */
17717 range_type = get_die_type (die, cu);
17718 if (range_type)
17719 return range_type;
17720
729efb13
SA
17721 low.kind = PROP_CONST;
17722 high.kind = PROP_CONST;
17723 high.data.const_val = 0;
17724
4fae6e18
JK
17725 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
17726 omitting DW_AT_lower_bound. */
17727 switch (cu->language)
6e70227d 17728 {
4fae6e18
JK
17729 case language_c:
17730 case language_cplus:
729efb13 17731 low.data.const_val = 0;
4fae6e18
JK
17732 low_default_is_valid = 1;
17733 break;
17734 case language_fortran:
729efb13 17735 low.data.const_val = 1;
4fae6e18
JK
17736 low_default_is_valid = 1;
17737 break;
17738 case language_d:
4fae6e18 17739 case language_objc:
c44af4eb 17740 case language_rust:
729efb13 17741 low.data.const_val = 0;
4fae6e18
JK
17742 low_default_is_valid = (cu->header.version >= 4);
17743 break;
17744 case language_ada:
17745 case language_m2:
17746 case language_pascal:
729efb13 17747 low.data.const_val = 1;
4fae6e18
JK
17748 low_default_is_valid = (cu->header.version >= 4);
17749 break;
17750 default:
729efb13 17751 low.data.const_val = 0;
4fae6e18
JK
17752 low_default_is_valid = 0;
17753 break;
a02abb62
JB
17754 }
17755
e142c38c 17756 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 17757 if (attr != nullptr)
9a49df9d 17758 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 17759 else if (!low_default_is_valid)
b98664d3 17760 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
17761 "- DIE at %s [in module %s]"),
17762 sect_offset_str (die->sect_off),
5e22e966 17763 objfile_name (cu->per_objfile->objfile));
a02abb62 17764
506f5c41
TV
17765 struct attribute *attr_ub, *attr_count;
17766 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 17767 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 17768 {
506f5c41 17769 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 17770 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 17771 {
c451ebe5
SA
17772 /* If bounds are constant do the final calculation here. */
17773 if (low.kind == PROP_CONST && high.kind == PROP_CONST)
17774 high.data.const_val = low.data.const_val + high.data.const_val - 1;
17775 else
17776 high_bound_is_count = 1;
c2ff108b 17777 }
506f5c41
TV
17778 else
17779 {
17780 if (attr_ub != NULL)
17781 complaint (_("Unresolved DW_AT_upper_bound "
17782 "- DIE at %s [in module %s]"),
17783 sect_offset_str (die->sect_off),
5e22e966 17784 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
17785 if (attr_count != NULL)
17786 complaint (_("Unresolved DW_AT_count "
17787 "- DIE at %s [in module %s]"),
17788 sect_offset_str (die->sect_off),
5e22e966 17789 objfile_name (cu->per_objfile->objfile));
506f5c41 17790 }
e77813c8 17791 }
a02abb62 17792
4e962e74
TT
17793 LONGEST bias = 0;
17794 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 17795 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 17796 bias = bias_attr->constant_value (0);
4e962e74 17797
dbb9c2b1
JB
17798 /* Normally, the DWARF producers are expected to use a signed
17799 constant form (Eg. DW_FORM_sdata) to express negative bounds.
17800 But this is unfortunately not always the case, as witnessed
17801 with GCC, for instance, where the ambiguous DW_FORM_dataN form
17802 is used instead. To work around that ambiguity, we treat
17803 the bounds as signed, and thus sign-extend their values, when
17804 the base type is signed. */
6e70227d 17805 negative_mask =
d359392f 17806 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
729efb13
SA
17807 if (low.kind == PROP_CONST
17808 && !TYPE_UNSIGNED (base_type) && (low.data.const_val & negative_mask))
17809 low.data.const_val |= negative_mask;
17810 if (high.kind == PROP_CONST
17811 && !TYPE_UNSIGNED (base_type) && (high.data.const_val & negative_mask))
17812 high.data.const_val |= negative_mask;
43bbcdc2 17813
5bbd8269
AB
17814 /* Check for bit and byte strides. */
17815 struct dynamic_prop byte_stride_prop;
17816 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
17817 if (attr_byte_stride != nullptr)
17818 {
293e7e51 17819 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17820 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
17821 prop_type);
17822 }
17823
17824 struct dynamic_prop bit_stride_prop;
17825 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
17826 if (attr_bit_stride != nullptr)
17827 {
17828 /* It only makes sense to have either a bit or byte stride. */
17829 if (attr_byte_stride != nullptr)
17830 {
17831 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
17832 "- DIE at %s [in module %s]"),
17833 sect_offset_str (die->sect_off),
5e22e966 17834 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
17835 attr_bit_stride = nullptr;
17836 }
17837 else
17838 {
293e7e51 17839 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
17840 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
17841 prop_type);
17842 }
17843 }
17844
17845 if (attr_byte_stride != nullptr
17846 || attr_bit_stride != nullptr)
17847 {
17848 bool byte_stride_p = (attr_byte_stride != nullptr);
17849 struct dynamic_prop *stride
17850 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
17851
17852 range_type
17853 = create_range_type_with_stride (NULL, orig_base_type, &low,
17854 &high, bias, stride, byte_stride_p);
17855 }
17856 else
17857 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 17858
c451ebe5
SA
17859 if (high_bound_is_count)
17860 TYPE_RANGE_DATA (range_type)->flag_upper_bound_is_count = 1;
17861
c2ff108b
JK
17862 /* Ada expects an empty array on no boundary attributes. */
17863 if (attr == NULL && cu->language != language_ada)
729efb13 17864 TYPE_HIGH_BOUND_KIND (range_type) = PROP_UNDEFINED;
c2ff108b 17865
39cbfefa
DJ
17866 name = dwarf2_name (die, cu);
17867 if (name)
d0e39ea2 17868 range_type->set_name (name);
6e70227d 17869
e142c38c 17870 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17871 if (attr != nullptr)
a02abb62
JB
17872 TYPE_LENGTH (range_type) = DW_UNSND (attr);
17873
2b4424c3
TT
17874 maybe_set_alignment (cu, die, range_type);
17875
7e314c57
JK
17876 set_die_type (die, range_type, cu);
17877
17878 /* set_die_type should be already done. */
b4ba55a1
JB
17879 set_descriptive_type (range_type, die, cu);
17880
7e314c57 17881 return range_type;
a02abb62 17882}
6e70227d 17883
f792889a 17884static struct type *
81a17f79
JB
17885read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
17886{
17887 struct type *type;
81a17f79 17888
5e22e966 17889 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 17890 type->set_name (dwarf2_name (die, cu));
81a17f79 17891
74a2f8ff 17892 /* In Ada, an unspecified type is typically used when the description
85102364 17893 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
17894 such a type, we treat it as a stub, and try to resolve it later on,
17895 when needed. */
17896 if (cu->language == language_ada)
17897 TYPE_STUB (type) = 1;
17898
f792889a 17899 return set_die_type (die, type, cu);
81a17f79 17900}
a02abb62 17901
639d11d3
DC
17902/* Read a single die and all its descendents. Set the die's sibling
17903 field to NULL; set other fields in the die correctly, and set all
17904 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
17905 location of the info_ptr after reading all of those dies. PARENT
17906 is the parent of the die in question. */
17907
17908static struct die_info *
dee91e82 17909read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
17910 const gdb_byte *info_ptr,
17911 const gdb_byte **new_info_ptr,
dee91e82 17912 struct die_info *parent)
639d11d3
DC
17913{
17914 struct die_info *die;
d521ce57 17915 const gdb_byte *cur_ptr;
639d11d3 17916
3e225074 17917 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
17918 if (die == NULL)
17919 {
17920 *new_info_ptr = cur_ptr;
17921 return NULL;
17922 }
93311388 17923 store_in_ref_table (die, reader->cu);
639d11d3 17924
3e225074 17925 if (die->has_children)
bf6af496 17926 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
17927 else
17928 {
17929 die->child = NULL;
17930 *new_info_ptr = cur_ptr;
17931 }
17932
17933 die->sibling = NULL;
17934 die->parent = parent;
17935 return die;
17936}
17937
17938/* Read a die, all of its descendents, and all of its siblings; set
17939 all of the fields of all of the dies correctly. Arguments are as
17940 in read_die_and_children. */
17941
17942static struct die_info *
bf6af496 17943read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
17944 const gdb_byte *info_ptr,
17945 const gdb_byte **new_info_ptr,
bf6af496 17946 struct die_info *parent)
639d11d3
DC
17947{
17948 struct die_info *first_die, *last_sibling;
d521ce57 17949 const gdb_byte *cur_ptr;
639d11d3 17950
c906108c 17951 cur_ptr = info_ptr;
639d11d3
DC
17952 first_die = last_sibling = NULL;
17953
17954 while (1)
c906108c 17955 {
639d11d3 17956 struct die_info *die
dee91e82 17957 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 17958
1d325ec1 17959 if (die == NULL)
c906108c 17960 {
639d11d3
DC
17961 *new_info_ptr = cur_ptr;
17962 return first_die;
c906108c 17963 }
1d325ec1
DJ
17964
17965 if (!first_die)
17966 first_die = die;
c906108c 17967 else
1d325ec1
DJ
17968 last_sibling->sibling = die;
17969
17970 last_sibling = die;
c906108c 17971 }
c906108c
SS
17972}
17973
bf6af496
DE
17974/* Read a die, all of its descendents, and all of its siblings; set
17975 all of the fields of all of the dies correctly. Arguments are as
17976 in read_die_and_children.
17977 This the main entry point for reading a DIE and all its children. */
17978
17979static struct die_info *
17980read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
17981 const gdb_byte *info_ptr,
17982 const gdb_byte **new_info_ptr,
bf6af496
DE
17983 struct die_info *parent)
17984{
17985 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
17986 new_info_ptr, parent);
17987
b4f54984 17988 if (dwarf_die_debug)
bf6af496
DE
17989 {
17990 fprintf_unfiltered (gdb_stdlog,
17991 "Read die from %s@0x%x of %s:\n",
96b79293 17992 reader->die_section->get_name (),
bf6af496
DE
17993 (unsigned) (info_ptr - reader->die_section->buffer),
17994 bfd_get_filename (reader->abfd));
b4f54984 17995 dump_die (die, dwarf_die_debug);
bf6af496
DE
17996 }
17997
17998 return die;
17999}
18000
3019eac3
DE
18001/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
18002 attributes.
18003 The caller is responsible for filling in the extra attributes
18004 and updating (*DIEP)->num_attrs.
18005 Set DIEP to point to a newly allocated die with its information,
3e225074 18006 except for its child, sibling, and parent fields. */
93311388 18007
d521ce57 18008static const gdb_byte *
3019eac3 18009read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 18010 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 18011 int num_extra_attrs)
93311388 18012{
b64f50a1 18013 unsigned int abbrev_number, bytes_read, i;
93311388
DE
18014 struct abbrev_info *abbrev;
18015 struct die_info *die;
18016 struct dwarf2_cu *cu = reader->cu;
18017 bfd *abfd = reader->abfd;
18018
9c541725 18019 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
18020 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
18021 info_ptr += bytes_read;
18022 if (!abbrev_number)
18023 {
18024 *diep = NULL;
93311388
DE
18025 return info_ptr;
18026 }
18027
685af9cd 18028 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 18029 if (!abbrev)
348e048f
DE
18030 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
18031 abbrev_number,
18032 bfd_get_filename (abfd));
18033
3019eac3 18034 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 18035 die->sect_off = sect_off;
93311388
DE
18036 die->tag = abbrev->tag;
18037 die->abbrev = abbrev_number;
3e225074 18038 die->has_children = abbrev->has_children;
93311388 18039
3019eac3
DE
18040 /* Make the result usable.
18041 The caller needs to update num_attrs after adding the extra
18042 attributes. */
93311388
DE
18043 die->num_attrs = abbrev->num_attrs;
18044
18a8505e 18045 std::vector<int> indexes_that_need_reprocess;
93311388 18046 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e
AT
18047 {
18048 bool need_reprocess;
18049 info_ptr =
18050 read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
18051 info_ptr, &need_reprocess);
18052 if (need_reprocess)
18053 indexes_that_need_reprocess.push_back (i);
18054 }
18055
052c8bb8 18056 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
18a8505e
AT
18057 if (attr != nullptr)
18058 cu->str_offsets_base = DW_UNSND (attr);
93311388 18059
41144253 18060 attr = die->attr (DW_AT_loclists_base);
18061 if (attr != nullptr)
18062 cu->loclist_base = DW_UNSND (attr);
18063
a39fdb41 18064 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
18065 if (maybe_addr_base.has_value ())
18066 cu->addr_base = *maybe_addr_base;
18067 for (int index : indexes_that_need_reprocess)
18068 read_attribute_reprocess (reader, &die->attrs[index]);
93311388 18069 *diep = die;
93311388
DE
18070 return info_ptr;
18071}
18072
3019eac3
DE
18073/* Read a die and all its attributes.
18074 Set DIEP to point to a newly allocated die with its information,
3e225074 18075 except for its child, sibling, and parent fields. */
3019eac3 18076
d521ce57 18077static const gdb_byte *
3019eac3 18078read_full_die (const struct die_reader_specs *reader,
3e225074 18079 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 18080{
d521ce57 18081 const gdb_byte *result;
bf6af496 18082
3e225074 18083 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 18084
b4f54984 18085 if (dwarf_die_debug)
bf6af496
DE
18086 {
18087 fprintf_unfiltered (gdb_stdlog,
18088 "Read die from %s@0x%x of %s:\n",
96b79293 18089 reader->die_section->get_name (),
bf6af496
DE
18090 (unsigned) (info_ptr - reader->die_section->buffer),
18091 bfd_get_filename (reader->abfd));
b4f54984 18092 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
18093 }
18094
18095 return result;
3019eac3 18096}
433df2d4 18097\f
c906108c 18098
72bf9492
DJ
18099/* Returns nonzero if TAG represents a type that we might generate a partial
18100 symbol for. */
18101
18102static int
18103is_type_tag_for_partial (int tag)
18104{
18105 switch (tag)
18106 {
18107#if 0
18108 /* Some types that would be reasonable to generate partial symbols for,
18109 that we don't at present. */
18110 case DW_TAG_array_type:
18111 case DW_TAG_file_type:
18112 case DW_TAG_ptr_to_member_type:
18113 case DW_TAG_set_type:
18114 case DW_TAG_string_type:
18115 case DW_TAG_subroutine_type:
18116#endif
18117 case DW_TAG_base_type:
18118 case DW_TAG_class_type:
680b30c7 18119 case DW_TAG_interface_type:
72bf9492
DJ
18120 case DW_TAG_enumeration_type:
18121 case DW_TAG_structure_type:
18122 case DW_TAG_subrange_type:
18123 case DW_TAG_typedef:
18124 case DW_TAG_union_type:
18125 return 1;
18126 default:
18127 return 0;
18128 }
18129}
18130
18131/* Load all DIEs that are interesting for partial symbols into memory. */
18132
18133static struct partial_die_info *
dee91e82 18134load_partial_dies (const struct die_reader_specs *reader,
d521ce57 18135 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 18136{
dee91e82 18137 struct dwarf2_cu *cu = reader->cu;
5e22e966 18138 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 18139 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 18140 unsigned int bytes_read;
5afb4e99 18141 unsigned int load_all = 0;
72bf9492
DJ
18142 int nesting_level = 1;
18143
18144 parent_die = NULL;
18145 last_die = NULL;
18146
7adf1e79
DE
18147 gdb_assert (cu->per_cu != NULL);
18148 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
18149 load_all = 1;
18150
72bf9492
DJ
18151 cu->partial_dies
18152 = htab_create_alloc_ex (cu->header.length / 12,
18153 partial_die_hash,
18154 partial_die_eq,
18155 NULL,
18156 &cu->comp_unit_obstack,
18157 hashtab_obstack_allocate,
18158 dummy_obstack_deallocate);
18159
72bf9492
DJ
18160 while (1)
18161 {
685af9cd 18162 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
18163
18164 /* A NULL abbrev means the end of a series of children. */
18165 if (abbrev == NULL)
18166 {
18167 if (--nesting_level == 0)
cd9983dd
YQ
18168 return first_die;
18169
72bf9492
DJ
18170 info_ptr += bytes_read;
18171 last_die = parent_die;
18172 parent_die = parent_die->die_parent;
18173 continue;
18174 }
18175
98bfdba5
PA
18176 /* Check for template arguments. We never save these; if
18177 they're seen, we just mark the parent, and go on our way. */
18178 if (parent_die != NULL
18179 && cu->language == language_cplus
18180 && (abbrev->tag == DW_TAG_template_type_param
18181 || abbrev->tag == DW_TAG_template_value_param))
18182 {
18183 parent_die->has_template_arguments = 1;
18184
18185 if (!load_all)
18186 {
18187 /* We don't need a partial DIE for the template argument. */
dee91e82 18188 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18189 continue;
18190 }
18191 }
18192
0d99eb77 18193 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
18194 Skip their other children. */
18195 if (!load_all
18196 && cu->language == language_cplus
18197 && parent_die != NULL
18198 && parent_die->tag == DW_TAG_subprogram)
18199 {
dee91e82 18200 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
18201 continue;
18202 }
18203
5afb4e99
DJ
18204 /* Check whether this DIE is interesting enough to save. Normally
18205 we would not be interested in members here, but there may be
18206 later variables referencing them via DW_AT_specification (for
18207 static members). */
18208 if (!load_all
18209 && !is_type_tag_for_partial (abbrev->tag)
72929c62 18210 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
18211 && abbrev->tag != DW_TAG_enumerator
18212 && abbrev->tag != DW_TAG_subprogram
b1dc1806 18213 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 18214 && abbrev->tag != DW_TAG_lexical_block
72bf9492 18215 && abbrev->tag != DW_TAG_variable
5afb4e99 18216 && abbrev->tag != DW_TAG_namespace
f55ee35c 18217 && abbrev->tag != DW_TAG_module
95554aad 18218 && abbrev->tag != DW_TAG_member
74921315
KS
18219 && abbrev->tag != DW_TAG_imported_unit
18220 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
18221 {
18222 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18223 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
18224 continue;
18225 }
18226
6f06d47b
YQ
18227 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
18228 abbrev);
cd9983dd 18229
48fbe735 18230 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
18231
18232 /* This two-pass algorithm for processing partial symbols has a
18233 high cost in cache pressure. Thus, handle some simple cases
18234 here which cover the majority of C partial symbols. DIEs
18235 which neither have specification tags in them, nor could have
18236 specification tags elsewhere pointing at them, can simply be
18237 processed and discarded.
18238
18239 This segment is also optional; scan_partial_symbols and
18240 add_partial_symbol will handle these DIEs if we chain
18241 them in normally. When compilers which do not emit large
18242 quantities of duplicate debug information are more common,
18243 this code can probably be removed. */
18244
18245 /* Any complete simple types at the top level (pretty much all
18246 of them, for a language without namespaces), can be processed
18247 directly. */
18248 if (parent_die == NULL
cd9983dd
YQ
18249 && pdi.has_specification == 0
18250 && pdi.is_declaration == 0
18251 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
18252 || pdi.tag == DW_TAG_base_type
18253 || pdi.tag == DW_TAG_subrange_type))
72bf9492 18254 {
cd9983dd 18255 if (building_psymtab && pdi.name != NULL)
31edb802 18256 add_psymbol_to_list (pdi.name, false,
79748972 18257 VAR_DOMAIN, LOC_TYPEDEF, -1,
75aedd27 18258 psymbol_placement::STATIC,
1762568f 18259 0, cu->language, objfile);
cd9983dd 18260 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18261 continue;
18262 }
18263
d8228535
JK
18264 /* The exception for DW_TAG_typedef with has_children above is
18265 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 18266 type_name_or_error will error on such types later.
d8228535
JK
18267
18268 GDB skipped children of DW_TAG_typedef by the shortcut above and then
18269 it could not find the child DIEs referenced later, this is checked
18270 above. In correct DWARF DW_TAG_typedef should have no children. */
18271
cd9983dd 18272 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 18273 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 18274 "- DIE at %s [in module %s]"),
cd9983dd 18275 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 18276
72bf9492
DJ
18277 /* If we're at the second level, and we're an enumerator, and
18278 our parent has no specification (meaning possibly lives in a
18279 namespace elsewhere), then we can add the partial symbol now
18280 instead of queueing it. */
cd9983dd 18281 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
18282 && parent_die != NULL
18283 && parent_die->die_parent == NULL
18284 && parent_die->tag == DW_TAG_enumeration_type
18285 && parent_die->has_specification == 0)
18286 {
cd9983dd 18287 if (pdi.name == NULL)
b98664d3 18288 complaint (_("malformed enumerator DIE ignored"));
72bf9492 18289 else if (building_psymtab)
31edb802 18290 add_psymbol_to_list (pdi.name, false,
79748972 18291 VAR_DOMAIN, LOC_CONST, -1,
9c37b5ae 18292 cu->language == language_cplus
75aedd27
TT
18293 ? psymbol_placement::GLOBAL
18294 : psymbol_placement::STATIC,
1762568f 18295 0, cu->language, objfile);
72bf9492 18296
cd9983dd 18297 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
18298 continue;
18299 }
18300
cd9983dd 18301 struct partial_die_info *part_die
6f06d47b 18302 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 18303
72bf9492
DJ
18304 /* We'll save this DIE so link it in. */
18305 part_die->die_parent = parent_die;
18306 part_die->die_sibling = NULL;
18307 part_die->die_child = NULL;
18308
18309 if (last_die && last_die == parent_die)
18310 last_die->die_child = part_die;
18311 else if (last_die)
18312 last_die->die_sibling = part_die;
18313
18314 last_die = part_die;
18315
18316 if (first_die == NULL)
18317 first_die = part_die;
18318
18319 /* Maybe add the DIE to the hash table. Not all DIEs that we
18320 find interesting need to be in the hash table, because we
18321 also have the parent/sibling/child chains; only those that we
18322 might refer to by offset later during partial symbol reading.
18323
18324 For now this means things that might have be the target of a
18325 DW_AT_specification, DW_AT_abstract_origin, or
18326 DW_AT_extension. DW_AT_extension will refer only to
18327 namespaces; DW_AT_abstract_origin refers to functions (and
18328 many things under the function DIE, but we do not recurse
18329 into function DIEs during partial symbol reading) and
18330 possibly variables as well; DW_AT_specification refers to
18331 declarations. Declarations ought to have the DW_AT_declaration
18332 flag. It happens that GCC forgets to put it in sometimes, but
18333 only for functions, not for types.
18334
18335 Adding more things than necessary to the hash table is harmless
18336 except for the performance cost. Adding too few will result in
5afb4e99
DJ
18337 wasted time in find_partial_die, when we reread the compilation
18338 unit with load_all_dies set. */
72bf9492 18339
5afb4e99 18340 if (load_all
72929c62 18341 || abbrev->tag == DW_TAG_constant
5afb4e99 18342 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
18343 || abbrev->tag == DW_TAG_variable
18344 || abbrev->tag == DW_TAG_namespace
18345 || part_die->is_declaration)
18346 {
18347 void **slot;
18348
18349 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
18350 to_underlying (part_die->sect_off),
18351 INSERT);
72bf9492
DJ
18352 *slot = part_die;
18353 }
18354
72bf9492 18355 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 18356 we have no reason to follow the children of structures; for other
98bfdba5
PA
18357 languages we have to, so that we can get at method physnames
18358 to infer fully qualified class names, for DW_AT_specification,
18359 and for C++ template arguments. For C++, we also look one level
18360 inside functions to find template arguments (if the name of the
18361 function does not already contain the template arguments).
bc30ff58 18362
0a4b0913
AB
18363 For Ada and Fortran, we need to scan the children of subprograms
18364 and lexical blocks as well because these languages allow the
18365 definition of nested entities that could be interesting for the
18366 debugger, such as nested subprograms for instance. */
72bf9492 18367 if (last_die->has_children
5afb4e99
DJ
18368 && (load_all
18369 || last_die->tag == DW_TAG_namespace
f55ee35c 18370 || last_die->tag == DW_TAG_module
72bf9492 18371 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
18372 || (cu->language == language_cplus
18373 && last_die->tag == DW_TAG_subprogram
18374 && (last_die->name == NULL
18375 || strchr (last_die->name, '<') == NULL))
72bf9492
DJ
18376 || (cu->language != language_c
18377 && (last_die->tag == DW_TAG_class_type
680b30c7 18378 || last_die->tag == DW_TAG_interface_type
72bf9492 18379 || last_die->tag == DW_TAG_structure_type
bc30ff58 18380 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
18381 || ((cu->language == language_ada
18382 || cu->language == language_fortran)
bc30ff58
JB
18383 && (last_die->tag == DW_TAG_subprogram
18384 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
18385 {
18386 nesting_level++;
18387 parent_die = last_die;
18388 continue;
18389 }
18390
18391 /* Otherwise we skip to the next sibling, if any. */
dee91e82 18392 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
18393
18394 /* Back to the top, do it again. */
18395 }
18396}
18397
6f06d47b
YQ
18398partial_die_info::partial_die_info (sect_offset sect_off_,
18399 struct abbrev_info *abbrev)
18400 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
18401{
18402}
18403
35cc7ed7
YQ
18404/* Read a minimal amount of information into the minimal die structure.
18405 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 18406
48fbe735
YQ
18407const gdb_byte *
18408partial_die_info::read (const struct die_reader_specs *reader,
18409 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 18410{
dee91e82 18411 struct dwarf2_cu *cu = reader->cu;
5e22e966 18412 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
fa238c03 18413 unsigned int i;
c5aa993b 18414 int has_low_pc_attr = 0;
c906108c 18415 int has_high_pc_attr = 0;
91da1414 18416 int high_pc_relative = 0;
c906108c 18417
fd0a254f 18418 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 18419 {
e7da7f8f 18420 attribute attr;
18a8505e 18421 bool need_reprocess;
e7da7f8f 18422 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i],
18a8505e
AT
18423 info_ptr, &need_reprocess);
18424 /* String and address offsets that need to do the reprocessing have
18425 already been read at this point, so there is no need to wait until
18426 the loop terminates to do the reprocessing. */
18427 if (need_reprocess)
e7da7f8f 18428 read_attribute_reprocess (reader, &attr);
c906108c 18429 /* Store the data if it is of an attribute we want to keep in a
c5aa993b 18430 partial symbol table. */
c906108c
SS
18431 switch (attr.name)
18432 {
18433 case DW_AT_name:
48fbe735 18434 switch (tag)
71c25dea
TT
18435 {
18436 case DW_TAG_compile_unit:
95554aad 18437 case DW_TAG_partial_unit:
348e048f 18438 case DW_TAG_type_unit:
71c25dea
TT
18439 /* Compilation units have a DW_AT_name that is a filename, not
18440 a source language identifier. */
18441 case DW_TAG_enumeration_type:
18442 case DW_TAG_enumerator:
18443 /* These tags always have simple identifiers already; no need
18444 to canonicalize them. */
48fbe735 18445 name = DW_STRING (&attr);
71c25dea
TT
18446 break;
18447 default:
48fbe735
YQ
18448 {
18449 struct objfile *objfile = dwarf2_per_objfile->objfile;
18450
18451 name
be1e3d3e 18452 = dwarf2_canonicalize_name (DW_STRING (&attr), cu, objfile);
48fbe735 18453 }
71c25dea
TT
18454 break;
18455 }
c906108c 18456 break;
31ef98ae 18457 case DW_AT_linkage_name:
c906108c 18458 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
18459 /* Note that both forms of linkage name might appear. We
18460 assume they will be the same, and we only store the last
18461 one we see. */
e61108c9 18462 linkage_name = attr.value_as_string ();
787de330
TT
18463 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
18464 See https://github.com/rust-lang/rust/issues/32925. */
18465 if (cu->language == language_rust && linkage_name != NULL
18466 && strchr (linkage_name, '{') != NULL)
18467 linkage_name = NULL;
c906108c
SS
18468 break;
18469 case DW_AT_low_pc:
18470 has_low_pc_attr = 1;
cd6c91b4 18471 lowpc = attr.value_as_address ();
c906108c
SS
18472 break;
18473 case DW_AT_high_pc:
18474 has_high_pc_attr = 1;
cd6c91b4
TT
18475 highpc = attr.value_as_address ();
18476 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 18477 high_pc_relative = 1;
c906108c
SS
18478 break;
18479 case DW_AT_location:
0963b4bd 18480 /* Support the .debug_loc offsets. */
4fc6c0d5 18481 if (attr.form_is_block ())
8e19ed76 18482 {
48fbe735 18483 d.locdesc = DW_BLOCK (&attr);
8e19ed76 18484 }
cd6c91b4 18485 else if (attr.form_is_section_offset ())
8e19ed76 18486 {
4d3c2250 18487 dwarf2_complex_location_expr_complaint ();
8e19ed76
PS
18488 }
18489 else
18490 {
4d3c2250
KB
18491 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18492 "partial symbol information");
8e19ed76 18493 }
c906108c 18494 break;
c906108c 18495 case DW_AT_external:
48fbe735 18496 is_external = DW_UNSND (&attr);
c906108c
SS
18497 break;
18498 case DW_AT_declaration:
48fbe735 18499 is_declaration = DW_UNSND (&attr);
c906108c
SS
18500 break;
18501 case DW_AT_type:
48fbe735 18502 has_type = 1;
c906108c
SS
18503 break;
18504 case DW_AT_abstract_origin:
18505 case DW_AT_specification:
72bf9492 18506 case DW_AT_extension:
48fbe735 18507 has_specification = 1;
0826b30a 18508 spec_offset = attr.get_ref_die_offset ();
48fbe735 18509 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 18510 || cu->per_cu->is_dwz);
c906108c
SS
18511 break;
18512 case DW_AT_sibling:
18513 /* Ignore absolute siblings, they might point outside of
18514 the current compile unit. */
18515 if (attr.form == DW_FORM_ref_addr)
b98664d3 18516 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 18517 else
b9502d3f 18518 {
48fbe735 18519 const gdb_byte *buffer = reader->buffer;
0826b30a 18520 sect_offset off = attr.get_ref_die_offset ();
9c541725 18521 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
18522
18523 if (sibling_ptr < info_ptr)
b98664d3 18524 complaint (_("DW_AT_sibling points backwards"));
22869d73 18525 else if (sibling_ptr > reader->buffer_end)
a0194fa8 18526 reader->die_section->overflow_complaint ();
b9502d3f 18527 else
48fbe735 18528 sibling = sibling_ptr;
b9502d3f 18529 }
c906108c 18530 break;
fa4028e9 18531 case DW_AT_byte_size:
48fbe735 18532 has_byte_size = 1;
fa4028e9 18533 break;
ff908ebf 18534 case DW_AT_const_value:
48fbe735 18535 has_const_value = 1;
ff908ebf 18536 break;
68511cec
CES
18537 case DW_AT_calling_convention:
18538 /* DWARF doesn't provide a way to identify a program's source-level
18539 entry point. DW_AT_calling_convention attributes are only meant
18540 to describe functions' calling conventions.
18541
18542 However, because it's a necessary piece of information in
0c1b455e
TT
18543 Fortran, and before DWARF 4 DW_CC_program was the only
18544 piece of debugging information whose definition refers to
18545 a 'main program' at all, several compilers marked Fortran
18546 main programs with DW_CC_program --- even when those
18547 functions use the standard calling conventions.
18548
18549 Although DWARF now specifies a way to provide this
18550 information, we support this practice for backward
18551 compatibility. */
68511cec 18552 if (DW_UNSND (&attr) == DW_CC_program
0c1b455e 18553 && cu->language == language_fortran)
48fbe735 18554 main_subprogram = 1;
68511cec 18555 break;
481860b3
GB
18556 case DW_AT_inline:
18557 if (DW_UNSND (&attr) == DW_INL_inlined
18558 || DW_UNSND (&attr) == DW_INL_declared_inlined)
48fbe735 18559 may_be_inlined = 1;
481860b3 18560 break;
95554aad
TT
18561
18562 case DW_AT_import:
48fbe735 18563 if (tag == DW_TAG_imported_unit)
36586728 18564 {
0826b30a 18565 d.sect_off = attr.get_ref_die_offset ();
48fbe735 18566 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
18567 || cu->per_cu->is_dwz);
18568 }
95554aad
TT
18569 break;
18570
0c1b455e 18571 case DW_AT_main_subprogram:
48fbe735 18572 main_subprogram = DW_UNSND (&attr);
0c1b455e
TT
18573 break;
18574
05caa1d2
TT
18575 case DW_AT_ranges:
18576 {
18577 /* It would be nice to reuse dwarf2_get_pc_bounds here,
18578 but that requires a full DIE, so instead we just
18579 reimplement it. */
18580 int need_ranges_base = tag != DW_TAG_compile_unit;
18581 unsigned int ranges_offset = (DW_UNSND (&attr)
18582 + (need_ranges_base
18583 ? cu->ranges_base
18584 : 0));
18585
18586 /* Value of the DW_AT_ranges attribute is the offset in the
18587 .debug_ranges section. */
18588 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
18589 nullptr))
18590 has_pc_info = 1;
18591 }
18592 break;
18593
c906108c
SS
18594 default:
18595 break;
18596 }
18597 }
18598
10d06d82
TT
18599 /* For Ada, if both the name and the linkage name appear, we prefer
18600 the latter. This lets "catch exception" work better, regardless
18601 of the order in which the name and linkage name were emitted.
18602 Really, though, this is just a workaround for the fact that gdb
18603 doesn't store both the name and the linkage name. */
18604 if (cu->language == language_ada && linkage_name != nullptr)
18605 name = linkage_name;
18606
91da1414 18607 if (high_pc_relative)
48fbe735 18608 highpc += lowpc;
91da1414 18609
9373cf26
JK
18610 if (has_low_pc_attr && has_high_pc_attr)
18611 {
18612 /* When using the GNU linker, .gnu.linkonce. sections are used to
18613 eliminate duplicate copies of functions and vtables and such.
18614 The linker will arbitrarily choose one and discard the others.
18615 The AT_*_pc values for such functions refer to local labels in
18616 these sections. If the section from that file was discarded, the
18617 labels are not in the output, so the relocs get a value of 0.
18618 If this is a discarded function, mark the pc bounds as invalid,
18619 so that GDB will ignore it. */
5989a64e 18620 if (lowpc == 0 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
9373cf26 18621 {
48fbe735 18622 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18623 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18624
b98664d3 18625 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 18626 "for DIE at %s [in module %s]"),
48fbe735
YQ
18627 paddress (gdbarch, lowpc),
18628 sect_offset_str (sect_off),
9d8780f0 18629 objfile_name (objfile));
9373cf26
JK
18630 }
18631 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 18632 else if (lowpc >= highpc)
9373cf26 18633 {
48fbe735 18634 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 18635 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 18636
b98664d3 18637 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 18638 "for DIE at %s [in module %s]"),
48fbe735
YQ
18639 paddress (gdbarch, lowpc),
18640 paddress (gdbarch, highpc),
18641 sect_offset_str (sect_off),
9c541725 18642 objfile_name (objfile));
9373cf26
JK
18643 }
18644 else
48fbe735 18645 has_pc_info = 1;
9373cf26 18646 }
85cbf3d3 18647
c906108c
SS
18648 return info_ptr;
18649}
18650
72bf9492
DJ
18651/* Find a cached partial DIE at OFFSET in CU. */
18652
d590ff25
YQ
18653struct partial_die_info *
18654dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
18655{
18656 struct partial_die_info *lookup_die = NULL;
6f06d47b 18657 struct partial_die_info part_die (sect_off);
72bf9492 18658
9a3c8263 18659 lookup_die = ((struct partial_die_info *)
d590ff25 18660 htab_find_with_hash (partial_dies, &part_die,
9c541725 18661 to_underlying (sect_off)));
72bf9492 18662
72bf9492
DJ
18663 return lookup_die;
18664}
18665
348e048f
DE
18666/* Find a partial DIE at OFFSET, which may or may not be in CU,
18667 except in the case of .debug_types DIEs which do not reference
18668 outside their CU (they do however referencing other types via
55f1336d 18669 DW_FORM_ref_sig8). */
72bf9492 18670
122cf0f2 18671static const struct cu_partial_die_info
9c541725 18672find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 18673{
5e22e966 18674 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 18675 struct objfile *objfile = dwarf2_per_objfile->objfile;
5afb4e99 18676 struct partial_die_info *pd = NULL;
72bf9492 18677
36586728 18678 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 18679 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 18680 {
d590ff25 18681 pd = cu->find_partial_die (sect_off);
5afb4e99 18682 if (pd != NULL)
fb816e8b 18683 return { cu, pd };
0d99eb77
DE
18684 /* We missed recording what we needed.
18685 Load all dies and try again. */
5afb4e99 18686 }
0d99eb77
DE
18687 else
18688 {
18689 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 18690 if (cu->per_cu->is_debug_types)
0d99eb77 18691 {
9d8780f0
SM
18692 error (_("Dwarf Error: Type Unit at offset %s contains"
18693 " external reference to offset %s [in module %s].\n"),
18694 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
18695 bfd_get_filename (objfile->obfd));
18696 }
7188ed02
SM
18697 dwarf2_per_cu_data *per_cu
18698 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
18699 dwarf2_per_objfile);
72bf9492 18700
7188ed02
SM
18701 cu = dwarf2_per_objfile->get_cu (per_cu);
18702 if (cu == NULL || cu->partial_dies == NULL)
18703 load_partial_comp_unit (per_cu, dwarf2_per_objfile, nullptr);
ae038cb0 18704
7188ed02
SM
18705 cu = dwarf2_per_objfile->get_cu (per_cu);
18706
18707 cu->last_used = 0;
18708 pd = cu->find_partial_die (sect_off);
0d99eb77 18709 }
5afb4e99 18710
dee91e82
DE
18711 /* If we didn't find it, and not all dies have been loaded,
18712 load them all and try again. */
18713
7188ed02 18714 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 18715 {
7188ed02 18716 cu->per_cu->load_all_dies = 1;
fd820528
DE
18717
18718 /* This is nasty. When we reread the DIEs, somewhere up the call chain
18719 THIS_CU->cu may already be in use. So we can't just free it and
18720 replace its DIEs with the ones we read in. Instead, we leave those
18721 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
18722 and clobber THIS_CU->cu->partial_dies with the hash table for the new
18723 set. */
7188ed02 18724 load_partial_comp_unit (cu->per_cu, dwarf2_per_objfile, cu);
5afb4e99 18725
7188ed02 18726 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
18727 }
18728
18729 if (pd == NULL)
18730 internal_error (__FILE__, __LINE__,
9d8780f0 18731 _("could not find partial DIE %s "
3e43a32a 18732 "in cache [from module %s]\n"),
9d8780f0 18733 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 18734 return { cu, pd };
72bf9492
DJ
18735}
18736
abc72ce4
DE
18737/* See if we can figure out if the class lives in a namespace. We do
18738 this by looking for a member function; its demangled name will
18739 contain namespace info, if there is any. */
18740
18741static void
18742guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
18743 struct dwarf2_cu *cu)
18744{
18745 /* NOTE: carlton/2003-10-07: Getting the info this way changes
18746 what template types look like, because the demangler
18747 frequently doesn't give the same name as the debug info. We
18748 could fix this by only using the demangled name to get the
18749 prefix (but see comment in read_structure_type). */
18750
18751 struct partial_die_info *real_pdi;
18752 struct partial_die_info *child_pdi;
18753
18754 /* If this DIE (this DIE's specification, if any) has a parent, then
18755 we should not do this. We'll prepend the parent's fully qualified
18756 name when we create the partial symbol. */
18757
18758 real_pdi = struct_pdi;
18759 while (real_pdi->has_specification)
fb816e8b 18760 {
122cf0f2
AB
18761 auto res = find_partial_die (real_pdi->spec_offset,
18762 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
18763 real_pdi = res.pdi;
18764 cu = res.cu;
18765 }
abc72ce4
DE
18766
18767 if (real_pdi->die_parent != NULL)
18768 return;
18769
18770 for (child_pdi = struct_pdi->die_child;
18771 child_pdi != NULL;
18772 child_pdi = child_pdi->die_sibling)
18773 {
18774 if (child_pdi->tag == DW_TAG_subprogram
18775 && child_pdi->linkage_name != NULL)
18776 {
43816ebc
TT
18777 gdb::unique_xmalloc_ptr<char> actual_class_name
18778 (language_class_name_from_physname (cu->language_defn,
18779 child_pdi->linkage_name));
abc72ce4
DE
18780 if (actual_class_name != NULL)
18781 {
5e22e966 18782 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18783 struct_pdi->name = objfile->intern (actual_class_name.get ());
abc72ce4
DE
18784 }
18785 break;
18786 }
18787 }
18788}
18789
25c11aca
TV
18790/* Return true if a DIE with TAG may have the DW_AT_const_value
18791 attribute. */
18792
18793static bool
18794can_have_DW_AT_const_value_p (enum dwarf_tag tag)
18795{
18796 switch (tag)
18797 {
18798 case DW_TAG_constant:
18799 case DW_TAG_enumerator:
18800 case DW_TAG_formal_parameter:
18801 case DW_TAG_template_value_param:
18802 case DW_TAG_variable:
18803 return true;
18804 }
18805
18806 return false;
18807}
18808
52356b79
YQ
18809void
18810partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 18811{
abc72ce4
DE
18812 /* Once we've fixed up a die, there's no point in doing so again.
18813 This also avoids a memory leak if we were to call
18814 guess_partial_die_structure_name multiple times. */
52356b79 18815 if (fixup_called)
abc72ce4
DE
18816 return;
18817
72bf9492
DJ
18818 /* If we found a reference attribute and the DIE has no name, try
18819 to find a name in the referred to DIE. */
18820
52356b79 18821 if (name == NULL && has_specification)
72bf9492
DJ
18822 {
18823 struct partial_die_info *spec_die;
72bf9492 18824
122cf0f2 18825 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
18826 spec_die = res.pdi;
18827 cu = res.cu;
72bf9492 18828
52356b79 18829 spec_die->fixup (cu);
72bf9492
DJ
18830
18831 if (spec_die->name)
18832 {
52356b79 18833 name = spec_die->name;
72bf9492
DJ
18834
18835 /* Copy DW_AT_external attribute if it is set. */
18836 if (spec_die->is_external)
52356b79 18837 is_external = spec_die->is_external;
72bf9492
DJ
18838 }
18839 }
18840
25c11aca
TV
18841 if (!has_const_value && has_specification
18842 && can_have_DW_AT_const_value_p (tag))
18843 {
18844 struct partial_die_info *spec_die;
18845
18846 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
18847 spec_die = res.pdi;
18848 cu = res.cu;
18849
18850 spec_die->fixup (cu);
18851
18852 if (spec_die->has_const_value)
18853 {
18854 /* Copy DW_AT_const_value attribute if it is set. */
18855 has_const_value = spec_die->has_const_value;
18856 }
18857 }
18858
72bf9492 18859 /* Set default names for some unnamed DIEs. */
72bf9492 18860
52356b79
YQ
18861 if (name == NULL && tag == DW_TAG_namespace)
18862 name = CP_ANONYMOUS_NAMESPACE_STR;
72bf9492 18863
abc72ce4
DE
18864 /* If there is no parent die to provide a namespace, and there are
18865 children, see if we can determine the namespace from their linkage
122d1940 18866 name. */
abc72ce4 18867 if (cu->language == language_cplus
5e22e966 18868 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
18869 && die_parent == NULL
18870 && has_children
18871 && (tag == DW_TAG_class_type
18872 || tag == DW_TAG_structure_type
18873 || tag == DW_TAG_union_type))
18874 guess_partial_die_structure_name (this, cu);
abc72ce4 18875
53832f31
TT
18876 /* GCC might emit a nameless struct or union that has a linkage
18877 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
52356b79
YQ
18878 if (name == NULL
18879 && (tag == DW_TAG_class_type
18880 || tag == DW_TAG_interface_type
18881 || tag == DW_TAG_structure_type
18882 || tag == DW_TAG_union_type)
18883 && linkage_name != NULL)
53832f31 18884 {
43816ebc
TT
18885 gdb::unique_xmalloc_ptr<char> demangled
18886 (gdb_demangle (linkage_name, DMGL_TYPES));
18887 if (demangled != nullptr)
53832f31 18888 {
96408a79
SA
18889 const char *base;
18890
18891 /* Strip any leading namespaces/classes, keep only the base name.
18892 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
18893 base = strrchr (demangled.get (), ':');
18894 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
18895 base++;
18896 else
43816ebc 18897 base = demangled.get ();
96408a79 18898
5e22e966 18899 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 18900 name = objfile->intern (base);
53832f31
TT
18901 }
18902 }
18903
52356b79 18904 fixup_called = 1;
72bf9492
DJ
18905}
18906
41144253 18907/* Read the .debug_loclists header contents from the given SECTION in the
18908 HEADER. */
18909static void
18910read_loclist_header (struct loclist_header *header,
18911 struct dwarf2_section_info *section)
18912{
18913 unsigned int bytes_read;
18914 bfd *abfd = section->get_bfd_owner ();
18915 const gdb_byte *info_ptr = section->buffer;
18916 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
18917 info_ptr += bytes_read;
18918 header->version = read_2_bytes (abfd, info_ptr);
18919 info_ptr += 2;
18920 header->addr_size = read_1_byte (abfd, info_ptr);
18921 info_ptr += 1;
18922 header->segment_collector_size = read_1_byte (abfd, info_ptr);
18923 info_ptr += 1;
18924 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
18925}
18926
18927/* Return the DW_AT_loclists_base value for the CU. */
18928static ULONGEST
18929lookup_loclist_base (struct dwarf2_cu *cu)
18930{
18931 /* For the .dwo unit, the loclist_base points to the first offset following
18932 the header. The header consists of the following entities-
18933 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
18934 bit format)
18935 2. version (2 bytes)
18936 3. address size (1 byte)
18937 4. segment selector size (1 byte)
18938 5. offset entry count (4 bytes)
18939 These sizes are derived as per the DWARFv5 standard. */
18940 if (cu->dwo_unit != nullptr)
18941 {
18942 if (cu->header.initial_length_size == 4)
18943 return LOCLIST_HEADER_SIZE32;
18944 return LOCLIST_HEADER_SIZE64;
18945 }
18946 return cu->loclist_base;
18947}
18948
18949/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
18950 array of offsets in the .debug_loclists section. */
18951static CORE_ADDR
18952read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
18953{
5e22e966 18954 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
41144253 18955 struct objfile *objfile = dwarf2_per_objfile->objfile;
18956 bfd *abfd = objfile->obfd;
18957 ULONGEST loclist_base = lookup_loclist_base (cu);
18958 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
18959
18960 section->read (objfile);
18961 if (section->buffer == NULL)
18962 complaint (_("DW_FORM_loclistx used without .debug_loclists "
18963 "section [in module %s]"), objfile_name (objfile));
18964 struct loclist_header header;
18965 read_loclist_header (&header, section);
18966 if (loclist_index >= header.offset_entry_count)
18967 complaint (_("DW_FORM_loclistx pointing outside of "
18968 ".debug_loclists offset array [in module %s]"),
18969 objfile_name (objfile));
18970 if (loclist_base + loclist_index * cu->header.offset_size
18971 >= section->size)
18972 complaint (_("DW_FORM_loclistx pointing outside of "
18973 ".debug_loclists section [in module %s]"),
18974 objfile_name (objfile));
18975 const gdb_byte *info_ptr
18976 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
18977
18978 if (cu->header.offset_size == 4)
18979 return bfd_get_32 (abfd, info_ptr) + loclist_base;
18980 else
18981 return bfd_get_64 (abfd, info_ptr) + loclist_base;
18982}
18983
18a8505e
AT
18984/* Process the attributes that had to be skipped in the first round. These
18985 attributes are the ones that need str_offsets_base or addr_base attributes.
18986 They could not have been processed in the first round, because at the time
18987 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
18988static void
18989read_attribute_reprocess (const struct die_reader_specs *reader,
18990 struct attribute *attr)
18a8505e
AT
18991{
18992 struct dwarf2_cu *cu = reader->cu;
18993 switch (attr->form)
18994 {
18995 case DW_FORM_addrx:
18996 case DW_FORM_GNU_addr_index:
18997 DW_ADDR (attr) = read_addr_index (cu, DW_UNSND (attr));
18998 break;
41144253 18999 case DW_FORM_loclistx:
19000 DW_UNSND (attr) = read_loclist_index (cu, DW_UNSND (attr));
19001 break;
18a8505e
AT
19002 case DW_FORM_strx:
19003 case DW_FORM_strx1:
19004 case DW_FORM_strx2:
19005 case DW_FORM_strx3:
19006 case DW_FORM_strx4:
19007 case DW_FORM_GNU_str_index:
19008 {
19009 unsigned int str_index = DW_UNSND (attr);
19010 if (reader->dwo_file != NULL)
19011 {
19012 DW_STRING (attr) = read_dwo_str_index (reader, str_index);
19013 DW_STRING_IS_CANONICAL (attr) = 0;
19014 }
19015 else
19016 {
19017 DW_STRING (attr) = read_stub_str_index (cu, str_index);
19018 DW_STRING_IS_CANONICAL (attr) = 0;
19019 }
19020 break;
19021 }
19022 default:
19023 gdb_assert_not_reached (_("Unexpected DWARF form."));
19024 }
19025}
19026
a8329558 19027/* Read an attribute value described by an attribute form. */
c906108c 19028
d521ce57 19029static const gdb_byte *
dee91e82
DE
19030read_attribute_value (const struct die_reader_specs *reader,
19031 struct attribute *attr, unsigned form,
18a8505e
AT
19032 LONGEST implicit_const, const gdb_byte *info_ptr,
19033 bool *need_reprocess)
c906108c 19034{
dee91e82 19035 struct dwarf2_cu *cu = reader->cu;
5e22e966 19036 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 19037 struct objfile *objfile = dwarf2_per_objfile->objfile;
dee91e82 19038 bfd *abfd = reader->abfd;
e7c27a73 19039 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
19040 unsigned int bytes_read;
19041 struct dwarf_block *blk;
18a8505e 19042 *need_reprocess = false;
c906108c 19043
aead7601 19044 attr->form = (enum dwarf_form) form;
a8329558 19045 switch (form)
c906108c 19046 {
c906108c 19047 case DW_FORM_ref_addr:
ae411497 19048 if (cu->header.version == 2)
c8a7a66f
TT
19049 DW_UNSND (attr) = cu->header.read_address (abfd, info_ptr,
19050 &bytes_read);
ae411497 19051 else
8266302d
TT
19052 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr,
19053 &bytes_read);
ae411497
TT
19054 info_ptr += bytes_read;
19055 break;
36586728 19056 case DW_FORM_GNU_ref_alt:
8266302d 19057 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
36586728
TT
19058 info_ptr += bytes_read;
19059 break;
ae411497 19060 case DW_FORM_addr:
08feed99
TT
19061 {
19062 struct gdbarch *gdbarch = objfile->arch ();
19063 DW_ADDR (attr) = cu->header.read_address (abfd, info_ptr, &bytes_read);
19064 DW_ADDR (attr) = gdbarch_adjust_dwarf2_addr (gdbarch, DW_ADDR (attr));
19065 info_ptr += bytes_read;
19066 }
c906108c
SS
19067 break;
19068 case DW_FORM_block2:
7b5a2f43 19069 blk = dwarf_alloc_block (cu);
c906108c
SS
19070 blk->size = read_2_bytes (abfd, info_ptr);
19071 info_ptr += 2;
19072 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19073 info_ptr += blk->size;
19074 DW_BLOCK (attr) = blk;
19075 break;
19076 case DW_FORM_block4:
7b5a2f43 19077 blk = dwarf_alloc_block (cu);
c906108c
SS
19078 blk->size = read_4_bytes (abfd, info_ptr);
19079 info_ptr += 4;
19080 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19081 info_ptr += blk->size;
19082 DW_BLOCK (attr) = blk;
19083 break;
19084 case DW_FORM_data2:
19085 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
19086 info_ptr += 2;
19087 break;
19088 case DW_FORM_data4:
19089 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
19090 info_ptr += 4;
19091 break;
19092 case DW_FORM_data8:
19093 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
19094 info_ptr += 8;
19095 break;
0224619f
JK
19096 case DW_FORM_data16:
19097 blk = dwarf_alloc_block (cu);
19098 blk->size = 16;
19099 blk->data = read_n_bytes (abfd, info_ptr, 16);
19100 info_ptr += 16;
19101 DW_BLOCK (attr) = blk;
19102 break;
2dc7f7b3 19103 case DW_FORM_sec_offset:
8266302d 19104 DW_UNSND (attr) = cu->header.read_offset (abfd, info_ptr, &bytes_read);
2dc7f7b3
TT
19105 info_ptr += bytes_read;
19106 break;
41144253 19107 case DW_FORM_loclistx:
19108 {
19109 *need_reprocess = true;
19110 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19111 info_ptr += bytes_read;
19112 }
19113 break;
c906108c 19114 case DW_FORM_string:
9b1c24c8 19115 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
8285870a 19116 DW_STRING_IS_CANONICAL (attr) = 0;
c906108c
SS
19117 info_ptr += bytes_read;
19118 break;
4bdf3d34 19119 case DW_FORM_strp:
36586728
TT
19120 if (!cu->per_cu->is_dwz)
19121 {
ed2dc618
SM
19122 DW_STRING (attr) = read_indirect_string (dwarf2_per_objfile,
19123 abfd, info_ptr, cu_header,
36586728
TT
19124 &bytes_read);
19125 DW_STRING_IS_CANONICAL (attr) = 0;
19126 info_ptr += bytes_read;
19127 break;
19128 }
19129 /* FALLTHROUGH */
43988095
JK
19130 case DW_FORM_line_strp:
19131 if (!cu->per_cu->is_dwz)
19132 {
86c0bb4c
TT
19133 DW_STRING (attr)
19134 = dwarf2_per_objfile->read_line_string (info_ptr, cu_header,
19135 &bytes_read);
43988095
JK
19136 DW_STRING_IS_CANONICAL (attr) = 0;
19137 info_ptr += bytes_read;
19138 break;
19139 }
19140 /* FALLTHROUGH */
36586728
TT
19141 case DW_FORM_GNU_strp_alt:
19142 {
c3699833 19143 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
8266302d
TT
19144 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
19145 &bytes_read);
36586728 19146
0314b390 19147 DW_STRING (attr) = dwz->read_string (objfile, str_offset);
36586728
TT
19148 DW_STRING_IS_CANONICAL (attr) = 0;
19149 info_ptr += bytes_read;
19150 }
4bdf3d34 19151 break;
2dc7f7b3 19152 case DW_FORM_exprloc:
c906108c 19153 case DW_FORM_block:
7b5a2f43 19154 blk = dwarf_alloc_block (cu);
c906108c
SS
19155 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19156 info_ptr += bytes_read;
19157 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19158 info_ptr += blk->size;
19159 DW_BLOCK (attr) = blk;
19160 break;
19161 case DW_FORM_block1:
7b5a2f43 19162 blk = dwarf_alloc_block (cu);
c906108c
SS
19163 blk->size = read_1_byte (abfd, info_ptr);
19164 info_ptr += 1;
19165 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
19166 info_ptr += blk->size;
19167 DW_BLOCK (attr) = blk;
19168 break;
19169 case DW_FORM_data1:
19170 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19171 info_ptr += 1;
19172 break;
19173 case DW_FORM_flag:
19174 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
19175 info_ptr += 1;
19176 break;
2dc7f7b3
TT
19177 case DW_FORM_flag_present:
19178 DW_UNSND (attr) = 1;
19179 break;
c906108c
SS
19180 case DW_FORM_sdata:
19181 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19182 info_ptr += bytes_read;
19183 break;
19184 case DW_FORM_udata:
18a8505e 19185 case DW_FORM_rnglistx:
c906108c
SS
19186 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19187 info_ptr += bytes_read;
19188 break;
19189 case DW_FORM_ref1:
9c541725 19190 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19191 + read_1_byte (abfd, info_ptr));
c906108c
SS
19192 info_ptr += 1;
19193 break;
19194 case DW_FORM_ref2:
9c541725 19195 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19196 + read_2_bytes (abfd, info_ptr));
c906108c
SS
19197 info_ptr += 2;
19198 break;
19199 case DW_FORM_ref4:
9c541725 19200 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19201 + read_4_bytes (abfd, info_ptr));
c906108c
SS
19202 info_ptr += 4;
19203 break;
613e1657 19204 case DW_FORM_ref8:
9c541725 19205 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19206 + read_8_bytes (abfd, info_ptr));
613e1657
KB
19207 info_ptr += 8;
19208 break;
55f1336d 19209 case DW_FORM_ref_sig8:
ac9ec31b 19210 DW_SIGNATURE (attr) = read_8_bytes (abfd, info_ptr);
348e048f
DE
19211 info_ptr += 8;
19212 break;
c906108c 19213 case DW_FORM_ref_udata:
9c541725 19214 DW_UNSND (attr) = (to_underlying (cu->header.sect_off)
4568ecf9 19215 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
19216 info_ptr += bytes_read;
19217 break;
c906108c 19218 case DW_FORM_indirect:
a8329558
KW
19219 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19220 info_ptr += bytes_read;
43988095
JK
19221 if (form == DW_FORM_implicit_const)
19222 {
19223 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
19224 info_ptr += bytes_read;
19225 }
19226 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
18a8505e 19227 info_ptr, need_reprocess);
43988095
JK
19228 break;
19229 case DW_FORM_implicit_const:
19230 DW_SND (attr) = implicit_const;
a8329558 19231 break;
336d760d 19232 case DW_FORM_addrx:
3019eac3 19233 case DW_FORM_GNU_addr_index:
18a8505e
AT
19234 *need_reprocess = true;
19235 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
3019eac3
DE
19236 info_ptr += bytes_read;
19237 break;
cf532bd1 19238 case DW_FORM_strx:
15f18d14
AT
19239 case DW_FORM_strx1:
19240 case DW_FORM_strx2:
19241 case DW_FORM_strx3:
19242 case DW_FORM_strx4:
3019eac3 19243 case DW_FORM_GNU_str_index:
3019eac3 19244 {
15f18d14
AT
19245 ULONGEST str_index;
19246 if (form == DW_FORM_strx1)
19247 {
19248 str_index = read_1_byte (abfd, info_ptr);
19249 info_ptr += 1;
19250 }
19251 else if (form == DW_FORM_strx2)
19252 {
19253 str_index = read_2_bytes (abfd, info_ptr);
19254 info_ptr += 2;
19255 }
19256 else if (form == DW_FORM_strx3)
19257 {
19258 str_index = read_3_bytes (abfd, info_ptr);
19259 info_ptr += 3;
19260 }
19261 else if (form == DW_FORM_strx4)
19262 {
19263 str_index = read_4_bytes (abfd, info_ptr);
19264 info_ptr += 4;
19265 }
19266 else
19267 {
19268 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19269 info_ptr += bytes_read;
19270 }
18a8505e
AT
19271 *need_reprocess = true;
19272 DW_UNSND (attr) = str_index;
19273 }
3019eac3 19274 break;
c906108c 19275 default:
8a3fe4f8 19276 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
19277 dwarf_form_name (form),
19278 bfd_get_filename (abfd));
c906108c 19279 }
28e94949 19280
36586728 19281 /* Super hack. */
cd6c91b4 19282 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
19283 attr->form = DW_FORM_GNU_ref_alt;
19284
28e94949
JB
19285 /* We have seen instances where the compiler tried to emit a byte
19286 size attribute of -1 which ended up being encoded as an unsigned
19287 0xffffffff. Although 0xffffffff is technically a valid size value,
19288 an object of this size seems pretty unlikely so we can relatively
19289 safely treat these cases as if the size attribute was invalid and
19290 treat them as zero by default. */
19291 if (attr->name == DW_AT_byte_size
19292 && form == DW_FORM_data4
19293 && DW_UNSND (attr) >= 0xffffffff)
01c66ae6
JB
19294 {
19295 complaint
b98664d3 19296 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
43bbcdc2 19297 hex_string (DW_UNSND (attr)));
01c66ae6
JB
19298 DW_UNSND (attr) = 0;
19299 }
28e94949 19300
c906108c
SS
19301 return info_ptr;
19302}
19303
a8329558
KW
19304/* Read an attribute described by an abbreviated attribute. */
19305
d521ce57 19306static const gdb_byte *
dee91e82
DE
19307read_attribute (const struct die_reader_specs *reader,
19308 struct attribute *attr, struct attr_abbrev *abbrev,
18a8505e 19309 const gdb_byte *info_ptr, bool *need_reprocess)
a8329558
KW
19310{
19311 attr->name = abbrev->name;
43988095 19312 return read_attribute_value (reader, attr, abbrev->form,
18a8505e
AT
19313 abbrev->implicit_const, info_ptr,
19314 need_reprocess);
a8329558
KW
19315}
19316
43988095
JK
19317/* Return pointer to string at .debug_str offset STR_OFFSET. */
19318
19319static const char *
ed2dc618 19320read_indirect_string_at_offset (struct dwarf2_per_objfile *dwarf2_per_objfile,
4f44ae6c 19321 LONGEST str_offset)
43988095 19322{
5989a64e
SM
19323 return dwarf2_per_objfile->per_bfd->str.read_string
19324 (dwarf2_per_objfile->objfile, str_offset, "DW_FORM_strp");
c906108c
SS
19325}
19326
43988095
JK
19327/* Return pointer to string at .debug_str offset as read from BUF.
19328 BUF is assumed to be in a compilation unit described by CU_HEADER.
19329 Return *BYTES_READ_PTR count of bytes read from BUF. */
19330
d521ce57 19331static const char *
ed2dc618
SM
19332read_indirect_string (struct dwarf2_per_objfile *dwarf2_per_objfile, bfd *abfd,
19333 const gdb_byte *buf,
cf2c3c16
TT
19334 const struct comp_unit_head *cu_header,
19335 unsigned int *bytes_read_ptr)
19336{
8266302d 19337 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 19338
4f44ae6c 19339 return read_indirect_string_at_offset (dwarf2_per_objfile, str_offset);
cf2c3c16
TT
19340}
19341
86c0bb4c 19342/* See read.h. */
43988095 19343
86c0bb4c
TT
19344const char *
19345dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
19346 const struct comp_unit_head *cu_header,
19347 unsigned int *bytes_read_ptr)
43988095 19348{
86c0bb4c 19349 bfd *abfd = objfile->obfd;
8266302d 19350 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 19351
5989a64e 19352 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
19353}
19354
3019eac3 19355/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 19356 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
19357 ADDR_SIZE is the size of addresses from the CU header. */
19358
19359static CORE_ADDR
ed2dc618 19360read_addr_index_1 (struct dwarf2_per_objfile *dwarf2_per_objfile,
18a8505e
AT
19361 unsigned int addr_index, gdb::optional<ULONGEST> addr_base,
19362 int addr_size)
3019eac3
DE
19363{
19364 struct objfile *objfile = dwarf2_per_objfile->objfile;
19365 bfd *abfd = objfile->obfd;
19366 const gdb_byte *info_ptr;
18a8505e 19367 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 19368
5989a64e
SM
19369 dwarf2_per_objfile->per_bfd->addr.read (objfile);
19370 if (dwarf2_per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 19371 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 19372 objfile_name (objfile));
18a8505e 19373 if (addr_base_or_zero + addr_index * addr_size
5989a64e 19374 >= dwarf2_per_objfile->per_bfd->addr.size)
3019eac3
DE
19375 error (_("DW_FORM_addr_index pointing outside of "
19376 ".debug_addr section [in module %s]"),
4262abfb 19377 objfile_name (objfile));
5989a64e 19378 info_ptr = (dwarf2_per_objfile->per_bfd->addr.buffer
18a8505e 19379 + addr_base_or_zero + addr_index * addr_size);
3019eac3
DE
19380 if (addr_size == 4)
19381 return bfd_get_32 (abfd, info_ptr);
19382 else
19383 return bfd_get_64 (abfd, info_ptr);
19384}
19385
19386/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
19387
19388static CORE_ADDR
19389read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
19390{
5e22e966 19391 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 19392 cu->addr_base, cu->header.addr_size);
3019eac3
DE
19393}
19394
19395/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
19396
19397static CORE_ADDR
d521ce57 19398read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
19399 unsigned int *bytes_read)
19400{
5e22e966 19401 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
19402 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
19403
19404 return read_addr_index (cu, addr_index);
19405}
19406
450a1bfc 19407/* See read.h. */
3019eac3
DE
19408
19409CORE_ADDR
82ca3f51
SM
19410dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
19411 dwarf2_per_objfile *dwarf2_per_objfile,
19412 unsigned int addr_index)
3019eac3 19413{
7188ed02 19414 struct dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
18a8505e 19415 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
19416 int addr_size;
19417
3019eac3
DE
19418 /* We need addr_base and addr_size.
19419 If we don't have PER_CU->cu, we have to get it.
19420 Nasty, but the alternative is storing the needed info in PER_CU,
19421 which at this point doesn't seem justified: it's not clear how frequently
19422 it would get used and it would increase the size of every PER_CU.
19423 Entry points like dwarf2_per_cu_addr_size do a similar thing
19424 so we're not in uncharted territory here.
19425 Alas we need to be a bit more complicated as addr_base is contained
19426 in the DIE.
19427
19428 We don't need to read the entire CU(/TU).
19429 We just need the header and top level die.
a1b64ce1 19430
3019eac3 19431 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 19432 For now we skip this optimization. */
3019eac3
DE
19433
19434 if (cu != NULL)
19435 {
19436 addr_base = cu->addr_base;
19437 addr_size = cu->header.addr_size;
19438 }
19439 else
19440 {
2e671100 19441 cutu_reader reader (per_cu, dwarf2_per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
19442 addr_base = reader.cu->addr_base;
19443 addr_size = reader.cu->header.addr_size;
3019eac3
DE
19444 }
19445
ed2dc618
SM
19446 return read_addr_index_1 (dwarf2_per_objfile, addr_index, addr_base,
19447 addr_size);
3019eac3
DE
19448}
19449
18a8505e
AT
19450/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
19451 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
19452 DWO file. */
3019eac3 19453
d521ce57 19454static const char *
18a8505e
AT
19455read_str_index (struct dwarf2_cu *cu,
19456 struct dwarf2_section_info *str_section,
19457 struct dwarf2_section_info *str_offsets_section,
19458 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 19459{
5e22e966 19460 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19461 struct objfile *objfile = dwarf2_per_objfile->objfile;
c5164cbc 19462 const char *objf_name = objfile_name (objfile);
3019eac3 19463 bfd *abfd = objfile->obfd;
d521ce57 19464 const gdb_byte *info_ptr;
3019eac3 19465 ULONGEST str_offset;
cf532bd1 19466 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 19467
96b79293
TT
19468 str_section->read (objfile);
19469 str_offsets_section->read (objfile);
73869dc2 19470 if (str_section->buffer == NULL)
18a8505e 19471 error (_("%s used without %s section"
9d8780f0 19472 " in CU at offset %s [in module %s]"),
96b79293 19473 form_name, str_section->get_name (),
18a8505e 19474 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19475 if (str_offsets_section->buffer == NULL)
18a8505e 19476 error (_("%s used without %s section"
9d8780f0 19477 " in CU at offset %s [in module %s]"),
96b79293 19478 form_name, str_section->get_name (),
18a8505e 19479 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19480 info_ptr = (str_offsets_section->buffer
18a8505e 19481 + str_offsets_base
3019eac3
DE
19482 + str_index * cu->header.offset_size);
19483 if (cu->header.offset_size == 4)
19484 str_offset = bfd_get_32 (abfd, info_ptr);
19485 else
19486 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 19487 if (str_offset >= str_section->size)
57d63ce2 19488 error (_("Offset from %s pointing outside of"
9d8780f0
SM
19489 " .debug_str.dwo section in CU at offset %s [in module %s]"),
19490 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 19491 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
19492}
19493
18a8505e
AT
19494/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
19495
19496static const char *
19497read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
19498{
19499 ULONGEST str_offsets_base = reader->cu->header.version >= 5
19500 ? reader->cu->header.addr_size : 0;
19501 return read_str_index (reader->cu,
19502 &reader->dwo_file->sections.str,
19503 &reader->dwo_file->sections.str_offsets,
19504 str_offsets_base, str_index);
19505}
19506
19507/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
19508
19509static const char *
19510read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
19511{
5e22e966 19512 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
19513 const char *objf_name = objfile_name (objfile);
19514 static const char form_name[] = "DW_FORM_GNU_str_index";
19515 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
19516
19517 if (!cu->str_offsets_base.has_value ())
19518 error (_("%s used in Fission stub without %s"
19519 " in CU at offset 0x%lx [in module %s]"),
19520 form_name, str_offsets_attr_name,
19521 (long) cu->header.offset_size, objf_name);
19522
19523 return read_str_index (cu,
5e22e966
SM
19524 &cu->per_objfile->per_bfd->str,
19525 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
19526 *cu->str_offsets_base, str_index);
19527}
19528
3019eac3
DE
19529/* Return the length of an LEB128 number in BUF. */
19530
19531static int
19532leb128_size (const gdb_byte *buf)
19533{
19534 const gdb_byte *begin = buf;
19535 gdb_byte byte;
19536
19537 while (1)
19538 {
19539 byte = *buf++;
19540 if ((byte & 128) == 0)
19541 return buf - begin;
19542 }
19543}
19544
c906108c 19545static void
e142c38c 19546set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
19547{
19548 switch (lang)
19549 {
19550 case DW_LANG_C89:
76bee0cc 19551 case DW_LANG_C99:
0cfd832f 19552 case DW_LANG_C11:
c906108c 19553 case DW_LANG_C:
d1be3247 19554 case DW_LANG_UPC:
e142c38c 19555 cu->language = language_c;
c906108c 19556 break;
9c37b5ae 19557 case DW_LANG_Java:
c906108c 19558 case DW_LANG_C_plus_plus:
0cfd832f
MW
19559 case DW_LANG_C_plus_plus_11:
19560 case DW_LANG_C_plus_plus_14:
e142c38c 19561 cu->language = language_cplus;
c906108c 19562 break;
6aecb9c2
JB
19563 case DW_LANG_D:
19564 cu->language = language_d;
19565 break;
c906108c
SS
19566 case DW_LANG_Fortran77:
19567 case DW_LANG_Fortran90:
b21b22e0 19568 case DW_LANG_Fortran95:
f7de9aab
MW
19569 case DW_LANG_Fortran03:
19570 case DW_LANG_Fortran08:
e142c38c 19571 cu->language = language_fortran;
c906108c 19572 break;
a766d390
DE
19573 case DW_LANG_Go:
19574 cu->language = language_go;
19575 break;
c906108c 19576 case DW_LANG_Mips_Assembler:
e142c38c 19577 cu->language = language_asm;
c906108c
SS
19578 break;
19579 case DW_LANG_Ada83:
8aaf0b47 19580 case DW_LANG_Ada95:
bc5f45f8
JB
19581 cu->language = language_ada;
19582 break;
72019c9c
GM
19583 case DW_LANG_Modula2:
19584 cu->language = language_m2;
19585 break;
fe8e67fd
PM
19586 case DW_LANG_Pascal83:
19587 cu->language = language_pascal;
19588 break;
22566fbd
DJ
19589 case DW_LANG_ObjC:
19590 cu->language = language_objc;
19591 break;
c44af4eb
TT
19592 case DW_LANG_Rust:
19593 case DW_LANG_Rust_old:
19594 cu->language = language_rust;
19595 break;
c906108c
SS
19596 case DW_LANG_Cobol74:
19597 case DW_LANG_Cobol85:
c906108c 19598 default:
e142c38c 19599 cu->language = language_minimal;
c906108c
SS
19600 break;
19601 }
e142c38c 19602 cu->language_defn = language_def (cu->language);
c906108c
SS
19603}
19604
19605/* Return the named attribute or NULL if not there. */
19606
19607static struct attribute *
e142c38c 19608dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 19609{
a48e046c 19610 for (;;)
c906108c 19611 {
a48e046c
TT
19612 unsigned int i;
19613 struct attribute *spec = NULL;
19614
19615 for (i = 0; i < die->num_attrs; ++i)
19616 {
19617 if (die->attrs[i].name == name)
19618 return &die->attrs[i];
19619 if (die->attrs[i].name == DW_AT_specification
19620 || die->attrs[i].name == DW_AT_abstract_origin)
19621 spec = &die->attrs[i];
19622 }
19623
19624 if (!spec)
19625 break;
c906108c 19626
f2f0e013 19627 die = follow_die_ref (die, spec, &cu);
f2f0e013 19628 }
c5aa993b 19629
c906108c
SS
19630 return NULL;
19631}
19632
7d45c7c3
KB
19633/* Return the string associated with a string-typed attribute, or NULL if it
19634 is either not found or is of an incorrect type. */
19635
19636static const char *
19637dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
19638{
19639 struct attribute *attr;
19640 const char *str = NULL;
19641
19642 attr = dwarf2_attr (die, name, cu);
19643
19644 if (attr != NULL)
19645 {
e61108c9
TT
19646 str = attr->value_as_string ();
19647 if (str == nullptr)
b98664d3 19648 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
19649 "DIE at %s in module %s"),
19650 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 19651 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
19652 }
19653
19654 return str;
19655}
19656
a084a2a6 19657/* Return the dwo name or NULL if not present. If present, it is in either
85102364 19658 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
19659static const char *
19660dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
19661{
19662 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
19663 if (dwo_name == nullptr)
19664 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
19665 return dwo_name;
19666}
19667
05cf31d1
JB
19668/* Return non-zero iff the attribute NAME is defined for the given DIE,
19669 and holds a non-zero value. This function should only be used for
2dc7f7b3 19670 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
19671
19672static int
19673dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
19674{
19675 struct attribute *attr = dwarf2_attr (die, name, cu);
19676
19677 return (attr && DW_UNSND (attr));
19678}
19679
3ca72b44 19680static int
e142c38c 19681die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 19682{
05cf31d1
JB
19683 /* A DIE is a declaration if it has a DW_AT_declaration attribute
19684 which value is non-zero. However, we have to be careful with
19685 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
19686 (via dwarf2_flag_true_p) follows this attribute. So we may
19687 end up accidently finding a declaration attribute that belongs
19688 to a different DIE referenced by the specification attribute,
19689 even though the given DIE does not have a declaration attribute. */
19690 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
19691 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
19692}
19693
63d06c5c 19694/* Return the die giving the specification for DIE, if there is
f2f0e013 19695 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
19696 containing the return value on output. If there is no
19697 specification, but there is an abstract origin, that is
19698 returned. */
63d06c5c
DC
19699
19700static struct die_info *
f2f0e013 19701die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 19702{
f2f0e013
DJ
19703 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
19704 *spec_cu);
63d06c5c 19705
edb3359d
DJ
19706 if (spec_attr == NULL)
19707 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
19708
63d06c5c
DC
19709 if (spec_attr == NULL)
19710 return NULL;
19711 else
f2f0e013 19712 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 19713}
c906108c 19714
527f3840
JK
19715/* Stub for free_line_header to match void * callback types. */
19716
19717static void
19718free_line_header_voidp (void *arg)
19719{
9a3c8263 19720 struct line_header *lh = (struct line_header *) arg;
527f3840 19721
fff8551c 19722 delete lh;
527f3840
JK
19723}
19724
83769d0b 19725/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
19726
19727static struct dwarf2_section_info *
19728get_debug_line_section (struct dwarf2_cu *cu)
19729{
19730 struct dwarf2_section_info *section;
5e22e966 19731 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
36586728
TT
19732
19733 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
19734 DWO file. */
19735 if (cu->dwo_unit && cu->per_cu->is_debug_types)
19736 section = &cu->dwo_unit->dwo_file->sections.line;
19737 else if (cu->per_cu->is_dwz)
19738 {
c3699833 19739 dwz_file *dwz = dwarf2_get_dwz_file (dwarf2_per_objfile->per_bfd);
36586728
TT
19740
19741 section = &dwz->line;
19742 }
19743 else
5989a64e 19744 section = &dwarf2_per_objfile->per_bfd->line;
36586728
TT
19745
19746 return section;
19747}
19748
debd256d 19749/* Read the statement program header starting at OFFSET in
3019eac3 19750 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 19751 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
19752 Returns NULL if there is a problem reading the header, e.g., if it
19753 has a version we don't understand.
debd256d
JB
19754
19755 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
19756 the returned object point into the dwarf line section buffer,
19757 and must not be freed. */
ae2de4f8 19758
fff8551c 19759static line_header_up
9c541725 19760dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 19761{
3019eac3 19762 struct dwarf2_section_info *section;
5e22e966 19763 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3 19764
36586728 19765 section = get_debug_line_section (cu);
96b79293 19766 section->read (dwarf2_per_objfile->objfile);
3019eac3 19767 if (section->buffer == NULL)
debd256d 19768 {
3019eac3 19769 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 19770 complaint (_("missing .debug_line.dwo section"));
3019eac3 19771 else
b98664d3 19772 complaint (_("missing .debug_line section"));
debd256d
JB
19773 return 0;
19774 }
19775
0df7ad3a
TT
19776 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
19777 dwarf2_per_objfile, section,
19778 &cu->header);
debd256d 19779}
c906108c 19780
c6da4cef 19781/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 19782 Return the file name of the psymtab for the given file_entry.
c6da4cef 19783 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
19784 If space for the result is malloc'd, *NAME_HOLDER will be set.
19785 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 19786
d521ce57 19787static const char *
7ba99d21 19788psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 19789 const dwarf2_psymtab *pst,
c89b44cd
TT
19790 const char *comp_dir,
19791 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 19792{
d521ce57
TT
19793 const char *include_name = fe.name;
19794 const char *include_name_to_compare = include_name;
72b9f47f 19795 const char *pst_filename;
c6da4cef
DE
19796 int file_is_pst;
19797
8c43009f 19798 const char *dir_name = fe.include_dir (lh);
c6da4cef 19799
c89b44cd 19800 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
19801 if (!IS_ABSOLUTE_PATH (include_name)
19802 && (dir_name != NULL || comp_dir != NULL))
19803 {
19804 /* Avoid creating a duplicate psymtab for PST.
19805 We do this by comparing INCLUDE_NAME and PST_FILENAME.
19806 Before we do the comparison, however, we need to account
19807 for DIR_NAME and COMP_DIR.
19808 First prepend dir_name (if non-NULL). If we still don't
19809 have an absolute path prepend comp_dir (if non-NULL).
19810 However, the directory we record in the include-file's
19811 psymtab does not contain COMP_DIR (to match the
19812 corresponding symtab(s)).
19813
19814 Example:
19815
19816 bash$ cd /tmp
19817 bash$ gcc -g ./hello.c
19818 include_name = "hello.c"
19819 dir_name = "."
19820 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
19821 DW_AT_name = "./hello.c"
19822
19823 */
c6da4cef
DE
19824
19825 if (dir_name != NULL)
19826 {
c89b44cd
TT
19827 name_holder->reset (concat (dir_name, SLASH_STRING,
19828 include_name, (char *) NULL));
19829 include_name = name_holder->get ();
c6da4cef 19830 include_name_to_compare = include_name;
c6da4cef
DE
19831 }
19832 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
19833 {
c89b44cd
TT
19834 hold_compare.reset (concat (comp_dir, SLASH_STRING,
19835 include_name, (char *) NULL));
19836 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
19837 }
19838 }
19839
19840 pst_filename = pst->filename;
c89b44cd 19841 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
19842 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
19843 {
c89b44cd
TT
19844 copied_name.reset (concat (pst->dirname, SLASH_STRING,
19845 pst_filename, (char *) NULL));
19846 pst_filename = copied_name.get ();
c6da4cef
DE
19847 }
19848
1e3fad37 19849 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 19850
c6da4cef
DE
19851 if (file_is_pst)
19852 return NULL;
19853 return include_name;
19854}
19855
d9b3de22
DE
19856/* State machine to track the state of the line number program. */
19857
6f77053d 19858class lnp_state_machine
d9b3de22 19859{
6f77053d
PA
19860public:
19861 /* Initialize a machine state for the start of a line number
19862 program. */
804d2729
TT
19863 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
19864 bool record_lines_p);
6f77053d 19865
8c43009f
PA
19866 file_entry *current_file ()
19867 {
19868 /* lh->file_names is 0-based, but the file name numbers in the
19869 statement program are 1-based. */
6f77053d
PA
19870 return m_line_header->file_name_at (m_file);
19871 }
19872
19873 /* Record the line in the state machine. END_SEQUENCE is true if
19874 we're processing the end of a sequence. */
19875 void record_line (bool end_sequence);
19876
7ab6656f
OJ
19877 /* Check ADDRESS is zero and less than UNRELOCATED_LOWPC and if true
19878 nop-out rest of the lines in this sequence. */
6f77053d
PA
19879 void check_line_address (struct dwarf2_cu *cu,
19880 const gdb_byte *line_ptr,
7ab6656f 19881 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
19882
19883 void handle_set_discriminator (unsigned int discriminator)
19884 {
19885 m_discriminator = discriminator;
19886 m_line_has_non_zero_discriminator |= discriminator != 0;
19887 }
19888
19889 /* Handle DW_LNE_set_address. */
19890 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
19891 {
19892 m_op_index = 0;
19893 address += baseaddr;
19894 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
19895 }
19896
19897 /* Handle DW_LNS_advance_pc. */
19898 void handle_advance_pc (CORE_ADDR adjust);
19899
19900 /* Handle a special opcode. */
19901 void handle_special_opcode (unsigned char op_code);
19902
19903 /* Handle DW_LNS_advance_line. */
19904 void handle_advance_line (int line_delta)
19905 {
19906 advance_line (line_delta);
19907 }
19908
19909 /* Handle DW_LNS_set_file. */
19910 void handle_set_file (file_name_index file);
19911
19912 /* Handle DW_LNS_negate_stmt. */
19913 void handle_negate_stmt ()
19914 {
19915 m_is_stmt = !m_is_stmt;
19916 }
19917
19918 /* Handle DW_LNS_const_add_pc. */
19919 void handle_const_add_pc ();
19920
19921 /* Handle DW_LNS_fixed_advance_pc. */
19922 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
19923 {
19924 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
19925 m_op_index = 0;
19926 }
19927
19928 /* Handle DW_LNS_copy. */
19929 void handle_copy ()
19930 {
19931 record_line (false);
19932 m_discriminator = 0;
19933 }
19934
19935 /* Handle DW_LNE_end_sequence. */
19936 void handle_end_sequence ()
19937 {
804d2729 19938 m_currently_recording_lines = true;
6f77053d
PA
19939 }
19940
19941private:
19942 /* Advance the line by LINE_DELTA. */
19943 void advance_line (int line_delta)
19944 {
19945 m_line += line_delta;
19946
19947 if (line_delta != 0)
19948 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
19949 }
19950
804d2729
TT
19951 struct dwarf2_cu *m_cu;
19952
6f77053d
PA
19953 gdbarch *m_gdbarch;
19954
19955 /* True if we're recording lines.
19956 Otherwise we're building partial symtabs and are just interested in
19957 finding include files mentioned by the line number program. */
19958 bool m_record_lines_p;
19959
8c43009f 19960 /* The line number header. */
6f77053d 19961 line_header *m_line_header;
8c43009f 19962
6f77053d
PA
19963 /* These are part of the standard DWARF line number state machine,
19964 and initialized according to the DWARF spec. */
d9b3de22 19965
6f77053d 19966 unsigned char m_op_index = 0;
7ba99d21
AT
19967 /* The line table index of the current file. */
19968 file_name_index m_file = 1;
6f77053d
PA
19969 unsigned int m_line = 1;
19970
19971 /* These are initialized in the constructor. */
19972
19973 CORE_ADDR m_address;
19974 bool m_is_stmt;
19975 unsigned int m_discriminator;
d9b3de22
DE
19976
19977 /* Additional bits of state we need to track. */
19978
19979 /* The last file that we called dwarf2_start_subfile for.
19980 This is only used for TLLs. */
6f77053d 19981 unsigned int m_last_file = 0;
d9b3de22 19982 /* The last file a line number was recorded for. */
6f77053d 19983 struct subfile *m_last_subfile = NULL;
d9b3de22 19984
804d2729
TT
19985 /* When true, record the lines we decode. */
19986 bool m_currently_recording_lines = false;
d9b3de22
DE
19987
19988 /* The last line number that was recorded, used to coalesce
19989 consecutive entries for the same line. This can happen, for
19990 example, when discriminators are present. PR 17276. */
6f77053d
PA
19991 unsigned int m_last_line = 0;
19992 bool m_line_has_non_zero_discriminator = false;
8c43009f 19993};
d9b3de22 19994
6f77053d
PA
19995void
19996lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
19997{
19998 CORE_ADDR addr_adj = (((m_op_index + adjust)
19999 / m_line_header->maximum_ops_per_instruction)
20000 * m_line_header->minimum_instruction_length);
20001 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20002 m_op_index = ((m_op_index + adjust)
20003 % m_line_header->maximum_ops_per_instruction);
20004}
d9b3de22 20005
6f77053d
PA
20006void
20007lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 20008{
6f77053d 20009 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
20010 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
20011 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
20012 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
20013 / m_line_header->maximum_ops_per_instruction)
20014 * m_line_header->minimum_instruction_length);
20015 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 20016 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 20017 % m_line_header->maximum_ops_per_instruction);
d9b3de22 20018
258bf0ee 20019 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
20020 advance_line (line_delta);
20021 record_line (false);
20022 m_discriminator = 0;
20023}
d9b3de22 20024
6f77053d
PA
20025void
20026lnp_state_machine::handle_set_file (file_name_index file)
20027{
20028 m_file = file;
20029
20030 const file_entry *fe = current_file ();
20031 if (fe == NULL)
20032 dwarf2_debug_line_missing_file_complaint ();
20033 else if (m_record_lines_p)
20034 {
20035 const char *dir = fe->include_dir (m_line_header);
20036
c24bdb02 20037 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20038 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 20039 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
20040 }
20041}
20042
20043void
20044lnp_state_machine::handle_const_add_pc ()
20045{
20046 CORE_ADDR adjust
20047 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
20048
20049 CORE_ADDR addr_adj
20050 = (((m_op_index + adjust)
20051 / m_line_header->maximum_ops_per_instruction)
20052 * m_line_header->minimum_instruction_length);
20053
20054 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
20055 m_op_index = ((m_op_index + adjust)
20056 % m_line_header->maximum_ops_per_instruction);
20057}
d9b3de22 20058
a05a36a5
DE
20059/* Return non-zero if we should add LINE to the line number table.
20060 LINE is the line to add, LAST_LINE is the last line that was added,
20061 LAST_SUBFILE is the subfile for LAST_LINE.
20062 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
20063 had a non-zero discriminator.
20064
20065 We have to be careful in the presence of discriminators.
20066 E.g., for this line:
20067
20068 for (i = 0; i < 100000; i++);
20069
20070 clang can emit four line number entries for that one line,
20071 each with a different discriminator.
20072 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
20073
20074 However, we want gdb to coalesce all four entries into one.
20075 Otherwise the user could stepi into the middle of the line and
20076 gdb would get confused about whether the pc really was in the
20077 middle of the line.
20078
20079 Things are further complicated by the fact that two consecutive
20080 line number entries for the same line is a heuristic used by gcc
20081 to denote the end of the prologue. So we can't just discard duplicate
20082 entries, we have to be selective about it. The heuristic we use is
20083 that we only collapse consecutive entries for the same line if at least
20084 one of those entries has a non-zero discriminator. PR 17276.
20085
20086 Note: Addresses in the line number state machine can never go backwards
20087 within one sequence, thus this coalescing is ok. */
20088
20089static int
804d2729
TT
20090dwarf_record_line_p (struct dwarf2_cu *cu,
20091 unsigned int line, unsigned int last_line,
a05a36a5
DE
20092 int line_has_non_zero_discriminator,
20093 struct subfile *last_subfile)
20094{
c24bdb02 20095 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
20096 return 1;
20097 if (line != last_line)
20098 return 1;
20099 /* Same line for the same file that we've seen already.
20100 As a last check, for pr 17276, only record the line if the line
20101 has never had a non-zero discriminator. */
20102 if (!line_has_non_zero_discriminator)
20103 return 1;
20104 return 0;
20105}
20106
804d2729
TT
20107/* Use the CU's builder to record line number LINE beginning at
20108 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
20109
20110static void
d9b3de22 20111dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 20112 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 20113 struct dwarf2_cu *cu)
252a6764
DE
20114{
20115 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
20116
27e0867f
DE
20117 if (dwarf_line_debug)
20118 {
20119 fprintf_unfiltered (gdb_stdlog,
20120 "Recording line %u, file %s, address %s\n",
20121 line, lbasename (subfile->name),
20122 paddress (gdbarch, address));
20123 }
20124
804d2729 20125 if (cu != nullptr)
8c95582d 20126 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
20127}
20128
20129/* Subroutine of dwarf_decode_lines_1 to simplify it.
20130 Mark the end of a set of line number records.
d9b3de22 20131 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
20132 If SUBFILE is NULL the request is ignored. */
20133
20134static void
20135dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 20136 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 20137{
27e0867f
DE
20138 if (subfile == NULL)
20139 return;
20140
20141 if (dwarf_line_debug)
20142 {
20143 fprintf_unfiltered (gdb_stdlog,
20144 "Finishing current line, file %s, address %s\n",
20145 lbasename (subfile->name),
20146 paddress (gdbarch, address));
20147 }
20148
8c95582d 20149 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
20150}
20151
6f77053d
PA
20152void
20153lnp_state_machine::record_line (bool end_sequence)
d9b3de22 20154{
d9b3de22
DE
20155 if (dwarf_line_debug)
20156 {
20157 fprintf_unfiltered (gdb_stdlog,
20158 "Processing actual line %u: file %u,"
94a72be7 20159 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 20160 m_line, m_file,
6f77053d 20161 paddress (m_gdbarch, m_address),
94a72be7
AB
20162 m_is_stmt, m_discriminator,
20163 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
20164 }
20165
6f77053d 20166 file_entry *fe = current_file ();
8c43009f
PA
20167
20168 if (fe == NULL)
d9b3de22
DE
20169 dwarf2_debug_line_missing_file_complaint ();
20170 /* For now we ignore lines not starting on an instruction boundary.
20171 But not when processing end_sequence for compatibility with the
20172 previous version of the code. */
6f77053d 20173 else if (m_op_index == 0 || end_sequence)
d9b3de22 20174 {
8c43009f 20175 fe->included_p = 1;
8c95582d 20176 if (m_record_lines_p)
d9b3de22 20177 {
c24bdb02 20178 if (m_last_subfile != m_cu->get_builder ()->get_current_subfile ()
804d2729 20179 || end_sequence)
d9b3de22 20180 {
804d2729
TT
20181 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
20182 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
20183 }
20184
20185 if (!end_sequence)
20186 {
8c95582d
AB
20187 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
20188
804d2729 20189 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
20190 m_line_has_non_zero_discriminator,
20191 m_last_subfile))
d9b3de22 20192 {
c24bdb02 20193 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 20194 dwarf_record_line_1 (m_gdbarch,
c24bdb02 20195 builder->get_current_subfile (),
8c95582d 20196 m_line, m_address, is_stmt,
804d2729 20197 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 20198 }
c24bdb02 20199 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 20200 m_last_line = m_line;
d9b3de22
DE
20201 }
20202 }
20203 }
20204}
20205
804d2729
TT
20206lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
20207 line_header *lh, bool record_lines_p)
d9b3de22 20208{
804d2729 20209 m_cu = cu;
6f77053d
PA
20210 m_gdbarch = arch;
20211 m_record_lines_p = record_lines_p;
20212 m_line_header = lh;
d9b3de22 20213
804d2729 20214 m_currently_recording_lines = true;
d9b3de22 20215
d9b3de22
DE
20216 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
20217 was a line entry for it so that the backend has a chance to adjust it
20218 and also record it in case it needs it. This is currently used by MIPS
20219 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
20220 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
20221 m_is_stmt = lh->default_is_stmt;
20222 m_discriminator = 0;
252a6764
DE
20223}
20224
6f77053d
PA
20225void
20226lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
20227 const gdb_byte *line_ptr,
7ab6656f 20228 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 20229{
7ab6656f
OJ
20230 /* If ADDRESS < UNRELOCATED_LOWPC then it's not a usable value, it's outside
20231 the pc range of the CU. However, we restrict the test to only ADDRESS
20232 values of zero to preserve GDB's previous behaviour which is to handle
20233 the specific case of a function being GC'd by the linker. */
924c2928 20234
7ab6656f 20235 if (address == 0 && address < unrelocated_lowpc)
924c2928
DE
20236 {
20237 /* This line table is for a function which has been
20238 GCd by the linker. Ignore it. PR gdb/12528 */
20239
5e22e966 20240 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
20241 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
20242
b98664d3 20243 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 20244 line_offset, objfile_name (objfile));
804d2729
TT
20245 m_currently_recording_lines = false;
20246 /* Note: m_currently_recording_lines is left as false until we see
20247 DW_LNE_end_sequence. */
924c2928
DE
20248 }
20249}
20250
f3f5162e 20251/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
20252 Process the line number information in LH.
20253 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
20254 program in order to set included_p for every referenced header. */
debd256d 20255
c906108c 20256static void
43f3e411
DE
20257dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
20258 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 20259{
d521ce57
TT
20260 const gdb_byte *line_ptr, *extended_end;
20261 const gdb_byte *line_end;
a8c50c1f 20262 unsigned int bytes_read, extended_len;
699ca60a 20263 unsigned char op_code, extended_op;
e142c38c 20264 CORE_ADDR baseaddr;
5e22e966 20265 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20266 bfd *abfd = objfile->obfd;
08feed99 20267 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
20268 /* True if we're recording line info (as opposed to building partial
20269 symtabs and just interested in finding include files mentioned by
20270 the line number program). */
20271 bool record_lines_p = !decode_for_pst_p;
e142c38c 20272
b3b3bada 20273 baseaddr = objfile->text_section_offset ();
c906108c 20274
debd256d
JB
20275 line_ptr = lh->statement_program_start;
20276 line_end = lh->statement_program_end;
c906108c
SS
20277
20278 /* Read the statement sequences until there's nothing left. */
20279 while (line_ptr < line_end)
20280 {
6f77053d
PA
20281 /* The DWARF line number program state machine. Reset the state
20282 machine at the start of each sequence. */
804d2729 20283 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 20284 bool end_sequence = false;
d9b3de22 20285
8c43009f 20286 if (record_lines_p)
c906108c 20287 {
8c43009f
PA
20288 /* Start a subfile for the current file of the state
20289 machine. */
20290 const file_entry *fe = state_machine.current_file ();
20291
20292 if (fe != NULL)
804d2729 20293 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
20294 }
20295
a738430d 20296 /* Decode the table. */
d9b3de22 20297 while (line_ptr < line_end && !end_sequence)
c906108c
SS
20298 {
20299 op_code = read_1_byte (abfd, line_ptr);
20300 line_ptr += 1;
9aa1fe7e 20301
debd256d 20302 if (op_code >= lh->opcode_base)
6e70227d 20303 {
8e07a239 20304 /* Special opcode. */
6f77053d 20305 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
20306 }
20307 else switch (op_code)
c906108c
SS
20308 {
20309 case DW_LNS_extended_op:
3e43a32a
MS
20310 extended_len = read_unsigned_leb128 (abfd, line_ptr,
20311 &bytes_read);
473b7be6 20312 line_ptr += bytes_read;
a8c50c1f 20313 extended_end = line_ptr + extended_len;
c906108c
SS
20314 extended_op = read_1_byte (abfd, line_ptr);
20315 line_ptr += 1;
20316 switch (extended_op)
20317 {
20318 case DW_LNE_end_sequence:
6f77053d
PA
20319 state_machine.handle_end_sequence ();
20320 end_sequence = true;
c906108c
SS
20321 break;
20322 case DW_LNE_set_address:
d9b3de22
DE
20323 {
20324 CORE_ADDR address
c8a7a66f 20325 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 20326 line_ptr += bytes_read;
6f77053d
PA
20327
20328 state_machine.check_line_address (cu, line_ptr,
7ab6656f 20329 lowpc - baseaddr, address);
6f77053d 20330 state_machine.handle_set_address (baseaddr, address);
d9b3de22 20331 }
c906108c
SS
20332 break;
20333 case DW_LNE_define_file:
debd256d 20334 {
d521ce57 20335 const char *cur_file;
ecfb656c
PA
20336 unsigned int mod_time, length;
20337 dir_index dindex;
6e70227d 20338
3e43a32a
MS
20339 cur_file = read_direct_string (abfd, line_ptr,
20340 &bytes_read);
debd256d 20341 line_ptr += bytes_read;
ecfb656c 20342 dindex = (dir_index)
debd256d
JB
20343 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20344 line_ptr += bytes_read;
20345 mod_time =
20346 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20347 line_ptr += bytes_read;
20348 length =
20349 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20350 line_ptr += bytes_read;
ecfb656c 20351 lh->add_file_name (cur_file, dindex, mod_time, length);
debd256d 20352 }
c906108c 20353 break;
d0c6ba3d 20354 case DW_LNE_set_discriminator:
6f77053d
PA
20355 {
20356 /* The discriminator is not interesting to the
20357 debugger; just ignore it. We still need to
20358 check its value though:
20359 if there are consecutive entries for the same
20360 (non-prologue) line we want to coalesce them.
20361 PR 17276. */
20362 unsigned int discr
20363 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20364 line_ptr += bytes_read;
20365
20366 state_machine.handle_set_discriminator (discr);
20367 }
d0c6ba3d 20368 break;
c906108c 20369 default:
b98664d3 20370 complaint (_("mangled .debug_line section"));
debd256d 20371 return;
c906108c 20372 }
a8c50c1f
DJ
20373 /* Make sure that we parsed the extended op correctly. If e.g.
20374 we expected a different address size than the producer used,
20375 we may have read the wrong number of bytes. */
20376 if (line_ptr != extended_end)
20377 {
b98664d3 20378 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
20379 return;
20380 }
c906108c
SS
20381 break;
20382 case DW_LNS_copy:
6f77053d 20383 state_machine.handle_copy ();
c906108c
SS
20384 break;
20385 case DW_LNS_advance_pc:
2dc7f7b3
TT
20386 {
20387 CORE_ADDR adjust
20388 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 20389 line_ptr += bytes_read;
6f77053d
PA
20390
20391 state_machine.handle_advance_pc (adjust);
2dc7f7b3 20392 }
c906108c
SS
20393 break;
20394 case DW_LNS_advance_line:
a05a36a5
DE
20395 {
20396 int line_delta
20397 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 20398 line_ptr += bytes_read;
6f77053d
PA
20399
20400 state_machine.handle_advance_line (line_delta);
a05a36a5 20401 }
c906108c
SS
20402 break;
20403 case DW_LNS_set_file:
d9b3de22 20404 {
6f77053d 20405 file_name_index file
ecfb656c
PA
20406 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
20407 &bytes_read);
d9b3de22 20408 line_ptr += bytes_read;
8c43009f 20409
6f77053d 20410 state_machine.handle_set_file (file);
d9b3de22 20411 }
c906108c
SS
20412 break;
20413 case DW_LNS_set_column:
0ad93d4f 20414 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
20415 line_ptr += bytes_read;
20416 break;
20417 case DW_LNS_negate_stmt:
6f77053d 20418 state_machine.handle_negate_stmt ();
c906108c
SS
20419 break;
20420 case DW_LNS_set_basic_block:
c906108c 20421 break;
c2c6d25f
JM
20422 /* Add to the address register of the state machine the
20423 address increment value corresponding to special opcode
a738430d
MK
20424 255. I.e., this value is scaled by the minimum
20425 instruction length since special opcode 255 would have
b021a221 20426 scaled the increment. */
c906108c 20427 case DW_LNS_const_add_pc:
6f77053d 20428 state_machine.handle_const_add_pc ();
c906108c
SS
20429 break;
20430 case DW_LNS_fixed_advance_pc:
3e29f34a 20431 {
6f77053d 20432 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 20433 line_ptr += 2;
6f77053d
PA
20434
20435 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 20436 }
c906108c 20437 break;
9aa1fe7e 20438 default:
a738430d
MK
20439 {
20440 /* Unknown standard opcode, ignore it. */
9aa1fe7e 20441 int i;
a738430d 20442
debd256d 20443 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
20444 {
20445 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
20446 line_ptr += bytes_read;
20447 }
20448 }
c906108c
SS
20449 }
20450 }
d9b3de22
DE
20451
20452 if (!end_sequence)
20453 dwarf2_debug_line_missing_end_sequence_complaint ();
20454
20455 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
20456 in which case we still finish recording the last line). */
6f77053d 20457 state_machine.record_line (true);
c906108c 20458 }
f3f5162e
DE
20459}
20460
20461/* Decode the Line Number Program (LNP) for the given line_header
20462 structure and CU. The actual information extracted and the type
20463 of structures created from the LNP depends on the value of PST.
20464
20465 1. If PST is NULL, then this procedure uses the data from the program
20466 to create all necessary symbol tables, and their linetables.
20467
20468 2. If PST is not NULL, this procedure reads the program to determine
20469 the list of files included by the unit represented by PST, and
20470 builds all the associated partial symbol tables.
20471
20472 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
20473 It is used for relative paths in the line table.
20474 NOTE: When processing partial symtabs (pst != NULL),
20475 comp_dir == pst->dirname.
20476
20477 NOTE: It is important that psymtabs have the same file name (via strcmp)
20478 as the corresponding symtab. Since COMP_DIR is not used in the name of the
20479 symtab we don't use it in the name of the psymtabs we create.
20480 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
20481 A good testcase for this is mb-inline.exp.
20482
527f3840
JK
20483 LOWPC is the lowest address in CU (or 0 if not known).
20484
20485 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
20486 for its PC<->lines mapping information. Otherwise only the filename
20487 table is read in. */
f3f5162e
DE
20488
20489static void
20490dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 20491 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 20492 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 20493{
5e22e966 20494 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 20495 const int decode_for_pst_p = (pst != NULL);
f3f5162e 20496
527f3840
JK
20497 if (decode_mapping)
20498 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
20499
20500 if (decode_for_pst_p)
20501 {
aaa75496
JB
20502 /* Now that we're done scanning the Line Header Program, we can
20503 create the psymtab of each included file. */
7ba99d21
AT
20504 for (auto &file_entry : lh->file_names ())
20505 if (file_entry.included_p == 1)
aaa75496 20506 {
c89b44cd 20507 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 20508 const char *include_name =
7ba99d21
AT
20509 psymtab_include_file_name (lh, file_entry, pst,
20510 comp_dir, &name_holder);
c6da4cef 20511 if (include_name != NULL)
aaa75496
JB
20512 dwarf2_create_include_psymtab (include_name, pst, objfile);
20513 }
20514 }
cb1df416
DJ
20515 else
20516 {
20517 /* Make sure a symtab is created for every file, even files
20518 which contain only variables (i.e. no code with associated
20519 line numbers). */
c24bdb02
KS
20520 buildsym_compunit *builder = cu->get_builder ();
20521 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 20522
7ba99d21 20523 for (auto &fe : lh->file_names ())
cb1df416 20524 {
804d2729 20525 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 20526 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 20527 {
c24bdb02 20528 builder->get_current_subfile ()->symtab
804d2729 20529 = allocate_symtab (cust,
c24bdb02 20530 builder->get_current_subfile ()->name);
43f3e411 20531 }
c24bdb02 20532 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
20533 }
20534 }
c906108c
SS
20535}
20536
20537/* Start a subfile for DWARF. FILENAME is the name of the file and
20538 DIRNAME the name of the source directory which contains FILENAME
4d663531 20539 or NULL if not known.
c906108c
SS
20540 This routine tries to keep line numbers from identical absolute and
20541 relative file names in a common subfile.
20542
20543 Using the `list' example from the GDB testsuite, which resides in
20544 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
20545 of /srcdir/list0.c yields the following debugging information for list0.c:
20546
c5aa993b 20547 DW_AT_name: /srcdir/list0.c
4d663531 20548 DW_AT_comp_dir: /compdir
357e46e7 20549 files.files[0].name: list0.h
c5aa993b 20550 files.files[0].dir: /srcdir
357e46e7 20551 files.files[1].name: list0.c
c5aa993b 20552 files.files[1].dir: /srcdir
c906108c
SS
20553
20554 The line number information for list0.c has to end up in a single
4f1520fb
FR
20555 subfile, so that `break /srcdir/list0.c:1' works as expected.
20556 start_subfile will ensure that this happens provided that we pass the
20557 concatenation of files.files[1].dir and files.files[1].name as the
20558 subfile's name. */
c906108c
SS
20559
20560static void
804d2729
TT
20561dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
20562 const char *dirname)
c906108c 20563{
43816ebc 20564 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 20565
4d663531 20566 /* In order not to lose the line information directory,
4f1520fb
FR
20567 we concatenate it to the filename when it makes sense.
20568 Note that the Dwarf3 standard says (speaking of filenames in line
20569 information): ``The directory index is ignored for file names
20570 that represent full path names''. Thus ignoring dirname in the
20571 `else' branch below isn't an issue. */
c906108c 20572
d5166ae1 20573 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 20574 {
43816ebc
TT
20575 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
20576 filename = copy.get ();
d521ce57 20577 }
c906108c 20578
c24bdb02 20579 cu->get_builder ()->start_subfile (filename);
c906108c
SS
20580}
20581
804d2729
TT
20582/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
20583 buildsym_compunit constructor. */
f4dc4d17 20584
c24bdb02
KS
20585struct compunit_symtab *
20586dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
20587 CORE_ADDR low_pc)
f4dc4d17 20588{
c24bdb02 20589 gdb_assert (m_builder == nullptr);
43f3e411 20590
c24bdb02 20591 m_builder.reset (new struct buildsym_compunit
f6e649dd 20592 (this->per_objfile->objfile,
c24bdb02 20593 name, comp_dir, language, low_pc));
93b8bea4 20594
c24bdb02 20595 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 20596
c24bdb02
KS
20597 get_builder ()->record_debugformat ("DWARF 2");
20598 get_builder ()->record_producer (producer);
f4dc4d17 20599
c24bdb02 20600 processing_has_namespace_info = false;
43f3e411 20601
c24bdb02 20602 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
20603}
20604
4c2df51b
DJ
20605static void
20606var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 20607 struct dwarf2_cu *cu)
4c2df51b 20608{
5e22e966 20609 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
20610 struct comp_unit_head *cu_header = &cu->header;
20611
4c2df51b
DJ
20612 /* NOTE drow/2003-01-30: There used to be a comment and some special
20613 code here to turn a symbol with DW_AT_external and a
20614 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
20615 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
20616 with some versions of binutils) where shared libraries could have
20617 relocations against symbols in their debug information - the
20618 minimal symbol would have the right address, but the debug info
20619 would not. It's no longer necessary, because we will explicitly
20620 apply relocations when we read in the debug information now. */
20621
20622 /* A DW_AT_location attribute with no contents indicates that a
20623 variable has been optimized away. */
4fc6c0d5 20624 if (attr->form_is_block () && DW_BLOCK (attr)->size == 0)
4c2df51b 20625 {
f1e6e072 20626 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
20627 return;
20628 }
20629
20630 /* Handle one degenerate form of location expression specially, to
20631 preserve GDB's previous behavior when section offsets are
336d760d
AT
20632 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
20633 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 20634
4fc6c0d5 20635 if (attr->form_is_block ()
3019eac3
DE
20636 && ((DW_BLOCK (attr)->data[0] == DW_OP_addr
20637 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size)
336d760d
AT
20638 || ((DW_BLOCK (attr)->data[0] == DW_OP_GNU_addr_index
20639 || DW_BLOCK (attr)->data[0] == DW_OP_addrx)
3019eac3
DE
20640 && (DW_BLOCK (attr)->size
20641 == 1 + leb128_size (&DW_BLOCK (attr)->data[1])))))
4c2df51b 20642 {
891d2f0b 20643 unsigned int dummy;
4c2df51b 20644
3019eac3 20645 if (DW_BLOCK (attr)->data[0] == DW_OP_addr)
c8a7a66f
TT
20646 SET_SYMBOL_VALUE_ADDRESS
20647 (sym, cu->header.read_address (objfile->obfd,
20648 DW_BLOCK (attr)->data + 1,
20649 &dummy));
3019eac3 20650 else
38583298
TT
20651 SET_SYMBOL_VALUE_ADDRESS
20652 (sym, read_addr_index_from_leb128 (cu, DW_BLOCK (attr)->data + 1,
20653 &dummy));
f1e6e072 20654 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
4c2df51b 20655 fixup_symbol_section (sym, objfile);
6a053cb1
TT
20656 SET_SYMBOL_VALUE_ADDRESS
20657 (sym,
20658 SYMBOL_VALUE_ADDRESS (sym)
20659 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
4c2df51b
DJ
20660 return;
20661 }
20662
20663 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
20664 expression evaluator, and use LOC_COMPUTED only when necessary
20665 (i.e. when the value of a register or memory location is
20666 referenced, or a thread-local block, etc.). Then again, it might
20667 not be worthwhile. I'm assuming that it isn't unless performance
20668 or memory numbers show me otherwise. */
20669
f1e6e072 20670 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 20671
f1e6e072 20672 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 20673 cu->has_loclist = true;
4c2df51b
DJ
20674}
20675
c906108c
SS
20676/* Given a pointer to a DWARF information entry, figure out if we need
20677 to make a symbol table entry for it, and if so, create a new entry
20678 and return a pointer to it.
20679 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
20680 used the passed type.
20681 If SPACE is not NULL, use it to hold the new symbol. If it is
20682 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
20683
20684static struct symbol *
5e2db402
TT
20685new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
20686 struct symbol *space)
c906108c 20687{
5e22e966 20688 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 20689 struct objfile *objfile = dwarf2_per_objfile->objfile;
08feed99 20690 struct gdbarch *gdbarch = objfile->arch ();
c906108c 20691 struct symbol *sym = NULL;
15d034d0 20692 const char *name;
c906108c
SS
20693 struct attribute *attr = NULL;
20694 struct attribute *attr2 = NULL;
e142c38c 20695 CORE_ADDR baseaddr;
e37fd15a
SW
20696 struct pending **list_to_add = NULL;
20697
edb3359d 20698 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 20699
b3b3bada 20700 baseaddr = objfile->text_section_offset ();
c906108c 20701
94af9270 20702 name = dwarf2_name (die, cu);
c906108c
SS
20703 if (name)
20704 {
34eaf542 20705 int suppress_add = 0;
94af9270 20706
34eaf542
TT
20707 if (space)
20708 sym = space;
20709 else
8c14c3a3 20710 sym = new (&objfile->objfile_obstack) symbol;
c906108c 20711 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
20712
20713 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 20714 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
20715 /* Fortran does not have mangling standard and the mangling does differ
20716 between gfortran, iFort etc. */
bcfe6157
TT
20717 const char *physname
20718 = (cu->language == language_fortran
20719 ? dwarf2_full_name (name, die, cu)
20720 : dwarf2_physname (name, die, cu));
20721 const char *linkagename = dw2_linkage_name (die, cu);
20722
20723 if (linkagename == nullptr || cu->language == language_ada)
20724 sym->set_linkage_name (physname);
20725 else
20726 {
20727 sym->set_demangled_name (physname, &objfile->objfile_obstack);
20728 sym->set_linkage_name (linkagename);
20729 }
f55ee35c 20730
c906108c 20731 /* Default assumptions.
c5aa993b 20732 Use the passed type or decode it from the die. */
176620f1 20733 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 20734 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
20735 if (type != NULL)
20736 SYMBOL_TYPE (sym) = type;
20737 else
e7c27a73 20738 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
20739 attr = dwarf2_attr (die,
20740 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
20741 cu);
435d3d88 20742 if (attr != nullptr)
c906108c
SS
20743 {
20744 SYMBOL_LINE (sym) = DW_UNSND (attr);
20745 }
cb1df416 20746
edb3359d
DJ
20747 attr = dwarf2_attr (die,
20748 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
20749 cu);
435d3d88 20750 if (attr != nullptr)
cb1df416 20751 {
ecfb656c 20752 file_name_index file_index = (file_name_index) DW_UNSND (attr);
8c43009f 20753 struct file_entry *fe;
9a619af0 20754
ecfb656c
PA
20755 if (cu->line_header != NULL)
20756 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
20757 else
20758 fe = NULL;
20759
20760 if (fe == NULL)
b98664d3 20761 complaint (_("file index out of range"));
8c43009f
PA
20762 else
20763 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
20764 }
20765
c906108c
SS
20766 switch (die->tag)
20767 {
20768 case DW_TAG_label:
e142c38c 20769 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 20770 if (attr != nullptr)
3e29f34a
MR
20771 {
20772 CORE_ADDR addr;
20773
cd6c91b4 20774 addr = attr->value_as_address ();
3e29f34a 20775 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 20776 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
3e29f34a 20777 }
0f5238ed
TT
20778 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
20779 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
f1e6e072 20780 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
d3cb6808 20781 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
20782 break;
20783 case DW_TAG_subprogram:
20784 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20785 finish_block. */
f1e6e072 20786 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 20787 attr2 = dwarf2_attr (die, DW_AT_external, cu);
2cfa0c8d 20788 if ((attr2 && (DW_UNSND (attr2) != 0))
0a4b0913
AB
20789 || cu->language == language_ada
20790 || cu->language == language_fortran)
c906108c 20791 {
2cfa0c8d 20792 /* Subprograms marked external are stored as a global symbol.
0a4b0913
AB
20793 Ada and Fortran subprograms, whether marked external or
20794 not, are always stored as a global symbol, because we want
20795 to be able to access them globally. For instance, we want
20796 to be able to break on a nested subprogram without having
20797 to specify the context. */
c24bdb02 20798 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
20799 }
20800 else
20801 {
e37fd15a 20802 list_to_add = cu->list_in_scope;
c906108c
SS
20803 }
20804 break;
edb3359d
DJ
20805 case DW_TAG_inlined_subroutine:
20806 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
20807 finish_block. */
f1e6e072 20808 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 20809 SYMBOL_INLINED (sym) = 1;
481860b3 20810 list_to_add = cu->list_in_scope;
edb3359d 20811 break;
34eaf542
TT
20812 case DW_TAG_template_value_param:
20813 suppress_add = 1;
20814 /* Fall through. */
72929c62 20815 case DW_TAG_constant:
c906108c 20816 case DW_TAG_variable:
254e6b9e 20817 case DW_TAG_member:
0963b4bd
MS
20818 /* Compilation with minimal debug info may result in
20819 variables with missing type entries. Change the
20820 misleading `void' type to something sensible. */
78134374 20821 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 20822 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 20823
e142c38c 20824 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
20825 /* In the case of DW_TAG_member, we should only be called for
20826 static const members. */
20827 if (die->tag == DW_TAG_member)
20828 {
3863f96c
DE
20829 /* dwarf2_add_field uses die_is_declaration,
20830 so we do the same. */
254e6b9e
DE
20831 gdb_assert (die_is_declaration (die, cu));
20832 gdb_assert (attr);
20833 }
435d3d88 20834 if (attr != nullptr)
c906108c 20835 {
e7c27a73 20836 dwarf2_const_value (attr, sym, cu);
e142c38c 20837 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 20838 if (!suppress_add)
34eaf542
TT
20839 {
20840 if (attr2 && (DW_UNSND (attr2) != 0))
c24bdb02 20841 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 20842 else
e37fd15a 20843 list_to_add = cu->list_in_scope;
34eaf542 20844 }
c906108c
SS
20845 break;
20846 }
e142c38c 20847 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20848 if (attr != nullptr)
c906108c 20849 {
e7c27a73 20850 var_decode_location (attr, sym, cu);
e142c38c 20851 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
20852
20853 /* Fortran explicitly imports any global symbols to the local
20854 scope by DW_TAG_common_block. */
20855 if (cu->language == language_fortran && die->parent
20856 && die->parent->tag == DW_TAG_common_block)
20857 attr2 = NULL;
20858
caac4577
JG
20859 if (SYMBOL_CLASS (sym) == LOC_STATIC
20860 && SYMBOL_VALUE_ADDRESS (sym) == 0
5989a64e 20861 && !dwarf2_per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
20862 {
20863 /* When a static variable is eliminated by the linker,
20864 the corresponding debug information is not stripped
20865 out, but the variable address is set to null;
20866 do not add such variables into symbol table. */
20867 }
20868 else if (attr2 && (DW_UNSND (attr2) != 0))
1c809c68 20869 {
4b610737
TT
20870 if (SYMBOL_CLASS (sym) == LOC_STATIC
20871 && (objfile->flags & OBJF_MAINLINE) == 0
5989a64e 20872 && dwarf2_per_objfile->per_bfd->can_copy)
4b610737
TT
20873 {
20874 /* A global static variable might be subject to
20875 copy relocation. We first check for a local
20876 minsym, though, because maybe the symbol was
20877 marked hidden, in which case this would not
20878 apply. */
20879 bound_minimal_symbol found
20880 = (lookup_minimal_symbol_linkage
987012b8 20881 (sym->linkage_name (), objfile));
4b610737
TT
20882 if (found.minsym != nullptr)
20883 sym->maybe_copied = 1;
20884 }
f55ee35c 20885
1c809c68
TT
20886 /* A variable with DW_AT_external is never static,
20887 but it may be block-scoped. */
804d2729 20888 list_to_add
c24bdb02
KS
20889 = ((cu->list_in_scope
20890 == cu->get_builder ()->get_file_symbols ())
20891 ? cu->get_builder ()->get_global_symbols ()
804d2729 20892 : cu->list_in_scope);
1c809c68 20893 }
c906108c 20894 else
e37fd15a 20895 list_to_add = cu->list_in_scope;
c906108c
SS
20896 }
20897 else
20898 {
20899 /* We do not know the address of this symbol.
c5aa993b
JM
20900 If it is an external symbol and we have type information
20901 for it, enter the symbol as a LOC_UNRESOLVED symbol.
20902 The address of the variable will then be determined from
20903 the minimal symbol table whenever the variable is
20904 referenced. */
e142c38c 20905 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
20906
20907 /* Fortran explicitly imports any global symbols to the local
20908 scope by DW_TAG_common_block. */
20909 if (cu->language == language_fortran && die->parent
20910 && die->parent->tag == DW_TAG_common_block)
20911 {
20912 /* SYMBOL_CLASS doesn't matter here because
20913 read_common_block is going to reset it. */
20914 if (!suppress_add)
20915 list_to_add = cu->list_in_scope;
20916 }
20917 else if (attr2 && (DW_UNSND (attr2) != 0)
20918 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 20919 {
0fe7935b
DJ
20920 /* A variable with DW_AT_external is never static, but it
20921 may be block-scoped. */
804d2729 20922 list_to_add
c24bdb02
KS
20923 = ((cu->list_in_scope
20924 == cu->get_builder ()->get_file_symbols ())
20925 ? cu->get_builder ()->get_global_symbols ()
804d2729 20926 : cu->list_in_scope);
0fe7935b 20927
f1e6e072 20928 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 20929 }
442ddf59
JK
20930 else if (!die_is_declaration (die, cu))
20931 {
20932 /* Use the default LOC_OPTIMIZED_OUT class. */
20933 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
20934 if (!suppress_add)
20935 list_to_add = cu->list_in_scope;
442ddf59 20936 }
c906108c
SS
20937 }
20938 break;
20939 case DW_TAG_formal_parameter:
a60f3166
TT
20940 {
20941 /* If we are inside a function, mark this as an argument. If
20942 not, we might be looking at an argument to an inlined function
20943 when we do not have enough information to show inlined frames;
20944 pretend it's a local variable in that case so that the user can
20945 still see it. */
804d2729 20946 struct context_stack *curr
c24bdb02 20947 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
20948 if (curr != nullptr && curr->name != nullptr)
20949 SYMBOL_IS_ARGUMENT (sym) = 1;
20950 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 20951 if (attr != nullptr)
a60f3166
TT
20952 {
20953 var_decode_location (attr, sym, cu);
20954 }
20955 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 20956 if (attr != nullptr)
a60f3166
TT
20957 {
20958 dwarf2_const_value (attr, sym, cu);
20959 }
f346a30d 20960
a60f3166
TT
20961 list_to_add = cu->list_in_scope;
20962 }
c906108c
SS
20963 break;
20964 case DW_TAG_unspecified_parameters:
20965 /* From varargs functions; gdb doesn't seem to have any
20966 interest in this information, so just ignore it for now.
20967 (FIXME?) */
20968 break;
34eaf542
TT
20969 case DW_TAG_template_type_param:
20970 suppress_add = 1;
20971 /* Fall through. */
c906108c 20972 case DW_TAG_class_type:
680b30c7 20973 case DW_TAG_interface_type:
c906108c
SS
20974 case DW_TAG_structure_type:
20975 case DW_TAG_union_type:
72019c9c 20976 case DW_TAG_set_type:
c906108c 20977 case DW_TAG_enumeration_type:
f1e6e072 20978 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 20979 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 20980
63d06c5c 20981 {
9c37b5ae 20982 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
20983 really ever be static objects: otherwise, if you try
20984 to, say, break of a class's method and you're in a file
20985 which doesn't mention that class, it won't work unless
20986 the check for all static symbols in lookup_symbol_aux
20987 saves you. See the OtherFileClass tests in
20988 gdb.c++/namespace.exp. */
20989
e37fd15a 20990 if (!suppress_add)
34eaf542 20991 {
c24bdb02 20992 buildsym_compunit *builder = cu->get_builder ();
804d2729 20993 list_to_add
c24bdb02 20994 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 20995 && cu->language == language_cplus
c24bdb02 20996 ? builder->get_global_symbols ()
804d2729 20997 : cu->list_in_scope);
63d06c5c 20998
64382290 20999 /* The semantics of C++ state that "struct foo {
9c37b5ae 21000 ... }" also defines a typedef for "foo". */
64382290 21001 if (cu->language == language_cplus
45280282 21002 || cu->language == language_ada
c44af4eb
TT
21003 || cu->language == language_d
21004 || cu->language == language_rust)
64382290
TT
21005 {
21006 /* The symbol's name is already allocated along
21007 with this objfile, so we don't need to
21008 duplicate it for the type. */
7d93a1e0 21009 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 21010 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 21011 }
63d06c5c
DC
21012 }
21013 }
c906108c
SS
21014 break;
21015 case DW_TAG_typedef:
f1e6e072 21016 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 21017 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21018 list_to_add = cu->list_in_scope;
63d06c5c 21019 break;
c906108c 21020 case DW_TAG_base_type:
a02abb62 21021 case DW_TAG_subrange_type:
f1e6e072 21022 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 21023 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 21024 list_to_add = cu->list_in_scope;
c906108c
SS
21025 break;
21026 case DW_TAG_enumerator:
e142c38c 21027 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 21028 if (attr != nullptr)
c906108c 21029 {
e7c27a73 21030 dwarf2_const_value (attr, sym, cu);
c906108c 21031 }
63d06c5c
DC
21032 {
21033 /* NOTE: carlton/2003-11-10: See comment above in the
21034 DW_TAG_class_type, etc. block. */
21035
804d2729 21036 list_to_add
c24bdb02 21037 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 21038 && cu->language == language_cplus
c24bdb02 21039 ? cu->get_builder ()->get_global_symbols ()
804d2729 21040 : cu->list_in_scope);
63d06c5c 21041 }
c906108c 21042 break;
74921315 21043 case DW_TAG_imported_declaration:
5c4e30ca 21044 case DW_TAG_namespace:
f1e6e072 21045 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 21046 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 21047 break;
530e8392
KB
21048 case DW_TAG_module:
21049 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
21050 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 21051 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 21052 break;
4357ac6c 21053 case DW_TAG_common_block:
f1e6e072 21054 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 21055 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 21056 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 21057 break;
c906108c
SS
21058 default:
21059 /* Not a tag we recognize. Hopefully we aren't processing
21060 trash data, but since we must specifically ignore things
21061 we don't recognize, there is nothing else we should do at
0963b4bd 21062 this point. */
b98664d3 21063 complaint (_("unsupported tag: '%s'"),
4d3c2250 21064 dwarf_tag_name (die->tag));
c906108c
SS
21065 break;
21066 }
df8a16a1 21067
e37fd15a
SW
21068 if (suppress_add)
21069 {
21070 sym->hash_next = objfile->template_symbols;
21071 objfile->template_symbols = sym;
21072 list_to_add = NULL;
21073 }
21074
21075 if (list_to_add != NULL)
d3cb6808 21076 add_symbol_to_list (sym, list_to_add);
e37fd15a 21077
df8a16a1
DJ
21078 /* For the benefit of old versions of GCC, check for anonymous
21079 namespaces based on the demangled name. */
4d4ec4e5 21080 if (!cu->processing_has_namespace_info
94af9270 21081 && cu->language == language_cplus)
c24bdb02 21082 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
21083 }
21084 return (sym);
21085}
21086
98bfdba5
PA
21087/* Given an attr with a DW_FORM_dataN value in host byte order,
21088 zero-extend it as appropriate for the symbol's type. The DWARF
21089 standard (v4) is not entirely clear about the meaning of using
21090 DW_FORM_dataN for a constant with a signed type, where the type is
21091 wider than the data. The conclusion of a discussion on the DWARF
21092 list was that this is unspecified. We choose to always zero-extend
21093 because that is the interpretation long in use by GCC. */
c906108c 21094
98bfdba5 21095static gdb_byte *
ff39bb5e 21096dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 21097 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 21098{
5e22e966 21099 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
21100 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
21101 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
98bfdba5
PA
21102 LONGEST l = DW_UNSND (attr);
21103
21104 if (bits < sizeof (*value) * 8)
21105 {
21106 l &= ((LONGEST) 1 << bits) - 1;
21107 *value = l;
21108 }
21109 else if (bits == sizeof (*value) * 8)
21110 *value = l;
21111 else
21112 {
224c3ddb 21113 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
21114 store_unsigned_integer (bytes, bits / 8, byte_order, l);
21115 return bytes;
21116 }
21117
21118 return NULL;
21119}
21120
21121/* Read a constant value from an attribute. Either set *VALUE, or if
21122 the value does not fit in *VALUE, set *BYTES - either already
21123 allocated on the objfile obstack, or newly allocated on OBSTACK,
21124 or, set *BATON, if we translated the constant to a location
21125 expression. */
21126
21127static void
ff39bb5e 21128dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
21129 const char *name, struct obstack *obstack,
21130 struct dwarf2_cu *cu,
d521ce57 21131 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
21132 struct dwarf2_locexpr_baton **baton)
21133{
5e22e966 21134 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 21135 struct objfile *objfile = per_objfile->objfile;
98bfdba5 21136 struct comp_unit_head *cu_header = &cu->header;
c906108c 21137 struct dwarf_block *blk;
98bfdba5
PA
21138 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
21139 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
21140
21141 *value = 0;
21142 *bytes = NULL;
21143 *baton = NULL;
c906108c
SS
21144
21145 switch (attr->form)
21146 {
21147 case DW_FORM_addr:
336d760d 21148 case DW_FORM_addrx:
3019eac3 21149 case DW_FORM_GNU_addr_index:
ac56253d 21150 {
ac56253d
TT
21151 gdb_byte *data;
21152
98bfdba5
PA
21153 if (TYPE_LENGTH (type) != cu_header->addr_size)
21154 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 21155 cu_header->addr_size,
98bfdba5 21156 TYPE_LENGTH (type));
ac56253d
TT
21157 /* Symbols of this form are reasonably rare, so we just
21158 piggyback on the existing location code rather than writing
21159 a new implementation of symbol_computed_ops. */
8d749320 21160 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 21161 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
21162 (*baton)->per_cu = cu->per_cu;
21163 gdb_assert ((*baton)->per_cu);
ac56253d 21164
98bfdba5 21165 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 21166 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 21167 (*baton)->data = data;
ac56253d
TT
21168
21169 data[0] = DW_OP_addr;
21170 store_unsigned_integer (&data[1], cu_header->addr_size,
21171 byte_order, DW_ADDR (attr));
21172 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 21173 }
c906108c 21174 break;
4ac36638 21175 case DW_FORM_string:
93b5768b 21176 case DW_FORM_strp:
cf532bd1 21177 case DW_FORM_strx:
3019eac3 21178 case DW_FORM_GNU_str_index:
36586728 21179 case DW_FORM_GNU_strp_alt:
98bfdba5
PA
21180 /* DW_STRING is already allocated on the objfile obstack, point
21181 directly to it. */
d521ce57 21182 *bytes = (const gdb_byte *) DW_STRING (attr);
93b5768b 21183 break;
c906108c
SS
21184 case DW_FORM_block1:
21185 case DW_FORM_block2:
21186 case DW_FORM_block4:
21187 case DW_FORM_block:
2dc7f7b3 21188 case DW_FORM_exprloc:
0224619f 21189 case DW_FORM_data16:
c906108c 21190 blk = DW_BLOCK (attr);
98bfdba5
PA
21191 if (TYPE_LENGTH (type) != blk->size)
21192 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
21193 TYPE_LENGTH (type));
21194 *bytes = blk->data;
c906108c 21195 break;
2df3850c
JM
21196
21197 /* The DW_AT_const_value attributes are supposed to carry the
21198 symbol's value "represented as it would be on the target
21199 architecture." By the time we get here, it's already been
21200 converted to host endianness, so we just need to sign- or
21201 zero-extend it as appropriate. */
21202 case DW_FORM_data1:
3aef2284 21203 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 21204 break;
c906108c 21205 case DW_FORM_data2:
3aef2284 21206 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 21207 break;
c906108c 21208 case DW_FORM_data4:
3aef2284 21209 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 21210 break;
c906108c 21211 case DW_FORM_data8:
3aef2284 21212 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
21213 break;
21214
c906108c 21215 case DW_FORM_sdata:
663c44ac 21216 case DW_FORM_implicit_const:
98bfdba5 21217 *value = DW_SND (attr);
2df3850c
JM
21218 break;
21219
c906108c 21220 case DW_FORM_udata:
98bfdba5 21221 *value = DW_UNSND (attr);
c906108c 21222 break;
2df3850c 21223
c906108c 21224 default:
b98664d3 21225 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 21226 dwarf_form_name (attr->form));
98bfdba5 21227 *value = 0;
c906108c
SS
21228 break;
21229 }
21230}
21231
2df3850c 21232
98bfdba5
PA
21233/* Copy constant value from an attribute to a symbol. */
21234
2df3850c 21235static void
ff39bb5e 21236dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 21237 struct dwarf2_cu *cu)
2df3850c 21238{
5e22e966 21239 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 21240 LONGEST value;
d521ce57 21241 const gdb_byte *bytes;
98bfdba5 21242 struct dwarf2_locexpr_baton *baton;
2df3850c 21243
98bfdba5 21244 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 21245 sym->print_name (),
98bfdba5
PA
21246 &objfile->objfile_obstack, cu,
21247 &value, &bytes, &baton);
2df3850c 21248
98bfdba5
PA
21249 if (baton != NULL)
21250 {
98bfdba5 21251 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 21252 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
21253 }
21254 else if (bytes != NULL)
21255 {
21256 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 21257 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
21258 }
21259 else
21260 {
21261 SYMBOL_VALUE (sym) = value;
f1e6e072 21262 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 21263 }
2df3850c
JM
21264}
21265
c906108c
SS
21266/* Return the type of the die in question using its DW_AT_type attribute. */
21267
21268static struct type *
e7c27a73 21269die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21270{
c906108c 21271 struct attribute *type_attr;
c906108c 21272
e142c38c 21273 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
21274 if (!type_attr)
21275 {
5e22e966 21276 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21277 /* A missing DW_AT_type represents a void type. */
518817b3 21278 return objfile_type (objfile)->builtin_void;
c906108c 21279 }
348e048f 21280
673bfd45 21281 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21282}
21283
b4ba55a1
JB
21284/* True iff CU's producer generates GNAT Ada auxiliary information
21285 that allows to find parallel types through that information instead
21286 of having to do expensive parallel lookups by type name. */
21287
21288static int
21289need_gnat_info (struct dwarf2_cu *cu)
21290{
de4cb04a
JB
21291 /* Assume that the Ada compiler was GNAT, which always produces
21292 the auxiliary information. */
21293 return (cu->language == language_ada);
b4ba55a1
JB
21294}
21295
b4ba55a1
JB
21296/* Return the auxiliary type of the die in question using its
21297 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
21298 attribute is not present. */
21299
21300static struct type *
21301die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
21302{
b4ba55a1 21303 struct attribute *type_attr;
b4ba55a1
JB
21304
21305 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
21306 if (!type_attr)
21307 return NULL;
21308
673bfd45 21309 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
21310}
21311
21312/* If DIE has a descriptive_type attribute, then set the TYPE's
21313 descriptive type accordingly. */
21314
21315static void
21316set_descriptive_type (struct type *type, struct die_info *die,
21317 struct dwarf2_cu *cu)
21318{
21319 struct type *descriptive_type = die_descriptive_type (die, cu);
21320
21321 if (descriptive_type)
21322 {
21323 ALLOCATE_GNAT_AUX_TYPE (type);
21324 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
21325 }
21326}
21327
c906108c
SS
21328/* Return the containing type of the die in question using its
21329 DW_AT_containing_type attribute. */
21330
21331static struct type *
e7c27a73 21332die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21333{
c906108c 21334 struct attribute *type_attr;
5e22e966 21335 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 21336
e142c38c 21337 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
21338 if (!type_attr)
21339 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 21340 "[in module %s]"), objfile_name (objfile));
33ac96f0 21341
673bfd45 21342 return lookup_die_type (die, type_attr, cu);
c906108c
SS
21343}
21344
ac9ec31b
DE
21345/* Return an error marker type to use for the ill formed type in DIE/CU. */
21346
21347static struct type *
21348build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
21349{
5e22e966 21350 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b 21351 struct objfile *objfile = dwarf2_per_objfile->objfile;
528e1572 21352 char *saved;
ac9ec31b 21353
528e1572
SM
21354 std::string message
21355 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
21356 objfile_name (objfile),
21357 sect_offset_str (cu->header.sect_off),
21358 sect_offset_str (die->sect_off));
efba19b0 21359 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 21360
19f392bc 21361 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
21362}
21363
673bfd45 21364/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
21365 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
21366 DW_AT_containing_type.
673bfd45
DE
21367 If there is no type substitute an error marker. */
21368
c906108c 21369static struct type *
ff39bb5e 21370lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 21371 struct dwarf2_cu *cu)
c906108c 21372{
5e22e966 21373 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 21374 struct objfile *objfile = dwarf2_per_objfile->objfile;
f792889a
DJ
21375 struct type *this_type;
21376
ac9ec31b
DE
21377 gdb_assert (attr->name == DW_AT_type
21378 || attr->name == DW_AT_GNAT_descriptive_type
21379 || attr->name == DW_AT_containing_type);
21380
673bfd45
DE
21381 /* First see if we have it cached. */
21382
36586728
TT
21383 if (attr->form == DW_FORM_GNU_ref_alt)
21384 {
21385 struct dwarf2_per_cu_data *per_cu;
0826b30a 21386 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 21387
ed2dc618
SM
21388 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1,
21389 dwarf2_per_objfile);
aa66c379 21390 this_type = get_die_type_at_offset (sect_off, per_cu, dwarf2_per_objfile);
36586728 21391 }
cd6c91b4 21392 else if (attr->form_is_ref ())
673bfd45 21393 {
0826b30a 21394 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 21395
aa66c379
SM
21396 this_type = get_die_type_at_offset (sect_off, cu->per_cu,
21397 dwarf2_per_objfile);
673bfd45 21398 }
55f1336d 21399 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 21400 {
ac9ec31b 21401 ULONGEST signature = DW_SIGNATURE (attr);
673bfd45 21402
ac9ec31b 21403 return get_signatured_type (die, signature, cu);
673bfd45
DE
21404 }
21405 else
21406 {
b98664d3 21407 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
21408 " at %s [in module %s]"),
21409 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 21410 objfile_name (objfile));
ac9ec31b 21411 return build_error_marker_type (cu, die);
673bfd45
DE
21412 }
21413
21414 /* If not cached we need to read it in. */
21415
21416 if (this_type == NULL)
21417 {
ac9ec31b 21418 struct die_info *type_die = NULL;
673bfd45
DE
21419 struct dwarf2_cu *type_cu = cu;
21420
cd6c91b4 21421 if (attr->form_is_ref ())
ac9ec31b
DE
21422 type_die = follow_die_ref (die, attr, &type_cu);
21423 if (type_die == NULL)
21424 return build_error_marker_type (cu, die);
21425 /* If we find the type now, it's probably because the type came
3019eac3
DE
21426 from an inter-CU reference and the type's CU got expanded before
21427 ours. */
ac9ec31b 21428 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
21429 }
21430
21431 /* If we still don't have a type use an error marker. */
21432
21433 if (this_type == NULL)
ac9ec31b 21434 return build_error_marker_type (cu, die);
673bfd45 21435
f792889a 21436 return this_type;
c906108c
SS
21437}
21438
673bfd45
DE
21439/* Return the type in DIE, CU.
21440 Returns NULL for invalid types.
21441
02142a6c 21442 This first does a lookup in die_type_hash,
673bfd45
DE
21443 and only reads the die in if necessary.
21444
21445 NOTE: This can be called when reading in partial or full symbols. */
21446
f792889a 21447static struct type *
e7c27a73 21448read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 21449{
f792889a
DJ
21450 struct type *this_type;
21451
21452 this_type = get_die_type (die, cu);
21453 if (this_type)
21454 return this_type;
21455
673bfd45
DE
21456 return read_type_die_1 (die, cu);
21457}
21458
21459/* Read the type in DIE, CU.
21460 Returns NULL for invalid types. */
21461
21462static struct type *
21463read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
21464{
21465 struct type *this_type = NULL;
21466
c906108c
SS
21467 switch (die->tag)
21468 {
21469 case DW_TAG_class_type:
680b30c7 21470 case DW_TAG_interface_type:
c906108c
SS
21471 case DW_TAG_structure_type:
21472 case DW_TAG_union_type:
f792889a 21473 this_type = read_structure_type (die, cu);
c906108c
SS
21474 break;
21475 case DW_TAG_enumeration_type:
f792889a 21476 this_type = read_enumeration_type (die, cu);
c906108c
SS
21477 break;
21478 case DW_TAG_subprogram:
21479 case DW_TAG_subroutine_type:
edb3359d 21480 case DW_TAG_inlined_subroutine:
f792889a 21481 this_type = read_subroutine_type (die, cu);
c906108c
SS
21482 break;
21483 case DW_TAG_array_type:
f792889a 21484 this_type = read_array_type (die, cu);
c906108c 21485 break;
72019c9c 21486 case DW_TAG_set_type:
f792889a 21487 this_type = read_set_type (die, cu);
72019c9c 21488 break;
c906108c 21489 case DW_TAG_pointer_type:
f792889a 21490 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
21491 break;
21492 case DW_TAG_ptr_to_member_type:
f792889a 21493 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
21494 break;
21495 case DW_TAG_reference_type:
4297a3f0
AV
21496 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
21497 break;
21498 case DW_TAG_rvalue_reference_type:
21499 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
21500 break;
21501 case DW_TAG_const_type:
f792889a 21502 this_type = read_tag_const_type (die, cu);
c906108c
SS
21503 break;
21504 case DW_TAG_volatile_type:
f792889a 21505 this_type = read_tag_volatile_type (die, cu);
c906108c 21506 break;
06d66ee9
TT
21507 case DW_TAG_restrict_type:
21508 this_type = read_tag_restrict_type (die, cu);
21509 break;
c906108c 21510 case DW_TAG_string_type:
f792889a 21511 this_type = read_tag_string_type (die, cu);
c906108c
SS
21512 break;
21513 case DW_TAG_typedef:
f792889a 21514 this_type = read_typedef (die, cu);
c906108c 21515 break;
a02abb62 21516 case DW_TAG_subrange_type:
f792889a 21517 this_type = read_subrange_type (die, cu);
a02abb62 21518 break;
c906108c 21519 case DW_TAG_base_type:
f792889a 21520 this_type = read_base_type (die, cu);
c906108c 21521 break;
81a17f79 21522 case DW_TAG_unspecified_type:
f792889a 21523 this_type = read_unspecified_type (die, cu);
81a17f79 21524 break;
0114d602
DJ
21525 case DW_TAG_namespace:
21526 this_type = read_namespace_type (die, cu);
21527 break;
f55ee35c
JK
21528 case DW_TAG_module:
21529 this_type = read_module_type (die, cu);
21530 break;
a2c2acaf
MW
21531 case DW_TAG_atomic_type:
21532 this_type = read_tag_atomic_type (die, cu);
21533 break;
c906108c 21534 default:
b98664d3 21535 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 21536 dwarf_tag_name (die->tag));
c906108c
SS
21537 break;
21538 }
63d06c5c 21539
f792889a 21540 return this_type;
63d06c5c
DC
21541}
21542
abc72ce4
DE
21543/* See if we can figure out if the class lives in a namespace. We do
21544 this by looking for a member function; its demangled name will
21545 contain namespace info, if there is any.
21546 Return the computed name or NULL.
21547 Space for the result is allocated on the objfile's obstack.
21548 This is the full-die version of guess_partial_die_structure_name.
21549 In this case we know DIE has no useful parent. */
21550
43816ebc 21551static const char *
abc72ce4
DE
21552guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
21553{
21554 struct die_info *spec_die;
21555 struct dwarf2_cu *spec_cu;
21556 struct die_info *child;
5e22e966 21557 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
21558
21559 spec_cu = cu;
21560 spec_die = die_specification (die, &spec_cu);
21561 if (spec_die != NULL)
21562 {
21563 die = spec_die;
21564 cu = spec_cu;
21565 }
21566
21567 for (child = die->child;
21568 child != NULL;
21569 child = child->sibling)
21570 {
21571 if (child->tag == DW_TAG_subprogram)
21572 {
73b9be8b 21573 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 21574
7d45c7c3 21575 if (linkage_name != NULL)
abc72ce4 21576 {
43816ebc
TT
21577 gdb::unique_xmalloc_ptr<char> actual_name
21578 (language_class_name_from_physname (cu->language_defn,
21579 linkage_name));
21580 const char *name = NULL;
abc72ce4
DE
21581
21582 if (actual_name != NULL)
21583 {
15d034d0 21584 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
21585
21586 if (die_name != NULL
43816ebc 21587 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
21588 {
21589 /* Strip off the class name from the full name.
21590 We want the prefix. */
21591 int die_name_len = strlen (die_name);
43816ebc
TT
21592 int actual_name_len = strlen (actual_name.get ());
21593 const char *ptr = actual_name.get ();
abc72ce4
DE
21594
21595 /* Test for '::' as a sanity check. */
21596 if (actual_name_len > die_name_len + 2
43816ebc 21597 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 21598 name = obstack_strndup (
e3b94546 21599 &objfile->per_bfd->storage_obstack,
43816ebc 21600 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
21601 }
21602 }
abc72ce4
DE
21603 return name;
21604 }
21605 }
21606 }
21607
21608 return NULL;
21609}
21610
96408a79
SA
21611/* GCC might emit a nameless typedef that has a linkage name. Determine the
21612 prefix part in such case. See
21613 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21614
a121b7c1 21615static const char *
96408a79
SA
21616anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
21617{
21618 struct attribute *attr;
e6a959d6 21619 const char *base;
96408a79
SA
21620
21621 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
21622 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
21623 return NULL;
21624
7d45c7c3 21625 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
21626 return NULL;
21627
73b9be8b 21628 attr = dw2_linkage_name_attr (die, cu);
96408a79
SA
21629 if (attr == NULL || DW_STRING (attr) == NULL)
21630 return NULL;
21631
21632 /* dwarf2_name had to be already called. */
21633 gdb_assert (DW_STRING_IS_CANONICAL (attr));
21634
21635 /* Strip the base name, keep any leading namespaces/classes. */
21636 base = strrchr (DW_STRING (attr), ':');
21637 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
21638 return "";
21639
5e22e966 21640 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9
TT
21641 return obstack_strndup (&objfile->per_bfd->storage_obstack,
21642 DW_STRING (attr),
21643 &base[-1] - DW_STRING (attr));
96408a79
SA
21644}
21645
fdde2d81 21646/* Return the name of the namespace/class that DIE is defined within,
0114d602 21647 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 21648
0114d602
DJ
21649 For example, if we're within the method foo() in the following
21650 code:
21651
21652 namespace N {
21653 class C {
21654 void foo () {
21655 }
21656 };
21657 }
21658
21659 then determine_prefix on foo's die will return "N::C". */
fdde2d81 21660
0d5cff50 21661static const char *
e142c38c 21662determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 21663{
5e22e966 21664 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
0114d602
DJ
21665 struct die_info *parent, *spec_die;
21666 struct dwarf2_cu *spec_cu;
21667 struct type *parent_type;
a121b7c1 21668 const char *retval;
63d06c5c 21669
9c37b5ae 21670 if (cu->language != language_cplus
c44af4eb
TT
21671 && cu->language != language_fortran && cu->language != language_d
21672 && cu->language != language_rust)
0114d602
DJ
21673 return "";
21674
96408a79
SA
21675 retval = anonymous_struct_prefix (die, cu);
21676 if (retval)
21677 return retval;
21678
0114d602
DJ
21679 /* We have to be careful in the presence of DW_AT_specification.
21680 For example, with GCC 3.4, given the code
21681
21682 namespace N {
21683 void foo() {
21684 // Definition of N::foo.
21685 }
21686 }
21687
21688 then we'll have a tree of DIEs like this:
21689
21690 1: DW_TAG_compile_unit
21691 2: DW_TAG_namespace // N
21692 3: DW_TAG_subprogram // declaration of N::foo
21693 4: DW_TAG_subprogram // definition of N::foo
21694 DW_AT_specification // refers to die #3
21695
21696 Thus, when processing die #4, we have to pretend that we're in
21697 the context of its DW_AT_specification, namely the contex of die
21698 #3. */
21699 spec_cu = cu;
21700 spec_die = die_specification (die, &spec_cu);
21701 if (spec_die == NULL)
21702 parent = die->parent;
21703 else
63d06c5c 21704 {
0114d602
DJ
21705 parent = spec_die->parent;
21706 cu = spec_cu;
63d06c5c 21707 }
0114d602
DJ
21708
21709 if (parent == NULL)
21710 return "";
98bfdba5
PA
21711 else if (parent->building_fullname)
21712 {
21713 const char *name;
21714 const char *parent_name;
21715
21716 /* It has been seen on RealView 2.2 built binaries,
21717 DW_TAG_template_type_param types actually _defined_ as
21718 children of the parent class:
21719
21720 enum E {};
21721 template class <class Enum> Class{};
21722 Class<enum E> class_e;
21723
21724 1: DW_TAG_class_type (Class)
21725 2: DW_TAG_enumeration_type (E)
21726 3: DW_TAG_enumerator (enum1:0)
21727 3: DW_TAG_enumerator (enum2:1)
21728 ...
21729 2: DW_TAG_template_type_param
21730 DW_AT_type DW_FORM_ref_udata (E)
21731
21732 Besides being broken debug info, it can put GDB into an
21733 infinite loop. Consider:
21734
21735 When we're building the full name for Class<E>, we'll start
21736 at Class, and go look over its template type parameters,
21737 finding E. We'll then try to build the full name of E, and
21738 reach here. We're now trying to build the full name of E,
21739 and look over the parent DIE for containing scope. In the
21740 broken case, if we followed the parent DIE of E, we'd again
21741 find Class, and once again go look at its template type
21742 arguments, etc., etc. Simply don't consider such parent die
21743 as source-level parent of this die (it can't be, the language
21744 doesn't allow it), and break the loop here. */
21745 name = dwarf2_name (die, cu);
21746 parent_name = dwarf2_name (parent, cu);
b98664d3 21747 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
21748 name ? name : "<unknown>",
21749 parent_name ? parent_name : "<unknown>");
21750 return "";
21751 }
63d06c5c 21752 else
0114d602
DJ
21753 switch (parent->tag)
21754 {
63d06c5c 21755 case DW_TAG_namespace:
0114d602 21756 parent_type = read_type_die (parent, cu);
acebe513
UW
21757 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
21758 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
21759 Work around this problem here. */
21760 if (cu->language == language_cplus
7d93a1e0 21761 && strcmp (parent_type->name (), "::") == 0)
acebe513 21762 return "";
0114d602 21763 /* We give a name to even anonymous namespaces. */
7d93a1e0 21764 return parent_type->name ();
63d06c5c 21765 case DW_TAG_class_type:
680b30c7 21766 case DW_TAG_interface_type:
63d06c5c 21767 case DW_TAG_structure_type:
0114d602 21768 case DW_TAG_union_type:
f55ee35c 21769 case DW_TAG_module:
0114d602 21770 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
21771 if (parent_type->name () != NULL)
21772 return parent_type->name ();
0114d602
DJ
21773 else
21774 /* An anonymous structure is only allowed non-static data
21775 members; no typedefs, no member functions, et cetera.
21776 So it does not need a prefix. */
21777 return "";
abc72ce4 21778 case DW_TAG_compile_unit:
95554aad 21779 case DW_TAG_partial_unit:
abc72ce4
DE
21780 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
21781 if (cu->language == language_cplus
5989a64e 21782 && !dwarf2_per_objfile->per_bfd->types.empty ()
abc72ce4
DE
21783 && die->child != NULL
21784 && (die->tag == DW_TAG_class_type
21785 || die->tag == DW_TAG_structure_type
21786 || die->tag == DW_TAG_union_type))
21787 {
43816ebc 21788 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
21789 if (name != NULL)
21790 return name;
21791 }
21792 return "";
0a4b0913
AB
21793 case DW_TAG_subprogram:
21794 /* Nested subroutines in Fortran get a prefix with the name
21795 of the parent's subroutine. */
21796 if (cu->language == language_fortran)
21797 {
21798 if ((die->tag == DW_TAG_subprogram)
21799 && (dwarf2_name (parent, cu) != NULL))
21800 return dwarf2_name (parent, cu);
21801 }
21802 return determine_prefix (parent, cu);
3d567982
TT
21803 case DW_TAG_enumeration_type:
21804 parent_type = read_type_die (parent, cu);
21805 if (TYPE_DECLARED_CLASS (parent_type))
21806 {
7d93a1e0
SM
21807 if (parent_type->name () != NULL)
21808 return parent_type->name ();
3d567982
TT
21809 return "";
21810 }
21811 /* Fall through. */
63d06c5c 21812 default:
8176b9b8 21813 return determine_prefix (parent, cu);
63d06c5c 21814 }
63d06c5c
DC
21815}
21816
3e43a32a
MS
21817/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
21818 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
21819 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
21820 an obconcat, otherwise allocate storage for the result. The CU argument is
21821 used to determine the language and hence, the appropriate separator. */
987504bb 21822
f55ee35c 21823#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
21824
21825static char *
f55ee35c
JK
21826typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
21827 int physname, struct dwarf2_cu *cu)
63d06c5c 21828{
f55ee35c 21829 const char *lead = "";
5c315b68 21830 const char *sep;
63d06c5c 21831
3e43a32a
MS
21832 if (suffix == NULL || suffix[0] == '\0'
21833 || prefix == NULL || prefix[0] == '\0')
987504bb 21834 sep = "";
45280282
IB
21835 else if (cu->language == language_d)
21836 {
21837 /* For D, the 'main' function could be defined in any module, but it
21838 should never be prefixed. */
21839 if (strcmp (suffix, "D main") == 0)
21840 {
21841 prefix = "";
21842 sep = "";
21843 }
21844 else
21845 sep = ".";
21846 }
f55ee35c
JK
21847 else if (cu->language == language_fortran && physname)
21848 {
21849 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
21850 DW_AT_MIPS_linkage_name is preferred and used instead. */
21851
21852 lead = "__";
21853 sep = "_MOD_";
21854 }
987504bb
JJ
21855 else
21856 sep = "::";
63d06c5c 21857
6dd47d34
DE
21858 if (prefix == NULL)
21859 prefix = "";
21860 if (suffix == NULL)
21861 suffix = "";
21862
987504bb
JJ
21863 if (obs == NULL)
21864 {
3e43a32a 21865 char *retval
224c3ddb
SM
21866 = ((char *)
21867 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 21868
f55ee35c
JK
21869 strcpy (retval, lead);
21870 strcat (retval, prefix);
6dd47d34
DE
21871 strcat (retval, sep);
21872 strcat (retval, suffix);
63d06c5c
DC
21873 return retval;
21874 }
987504bb
JJ
21875 else
21876 {
21877 /* We have an obstack. */
f55ee35c 21878 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 21879 }
63d06c5c
DC
21880}
21881
71c25dea
TT
21882/* Get name of a die, return NULL if not found. */
21883
15d034d0
TT
21884static const char *
21885dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 21886 struct objfile *objfile)
71c25dea
TT
21887{
21888 if (name && cu->language == language_cplus)
21889 {
596dc4ad
TT
21890 gdb::unique_xmalloc_ptr<char> canon_name
21891 = cp_canonicalize_string (name);
71c25dea 21892
596dc4ad
TT
21893 if (canon_name != nullptr)
21894 name = objfile->intern (canon_name.get ());
71c25dea
TT
21895 }
21896
21897 return name;
c906108c
SS
21898}
21899
96553a0c
DE
21900/* Get name of a die, return NULL if not found.
21901 Anonymous namespaces are converted to their magic string. */
9219021c 21902
15d034d0 21903static const char *
e142c38c 21904dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
21905{
21906 struct attribute *attr;
5e22e966 21907 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 21908
e142c38c 21909 attr = dwarf2_attr (die, DW_AT_name, cu);
53832f31 21910 if ((!attr || !DW_STRING (attr))
96553a0c 21911 && die->tag != DW_TAG_namespace
53832f31
TT
21912 && die->tag != DW_TAG_class_type
21913 && die->tag != DW_TAG_interface_type
21914 && die->tag != DW_TAG_structure_type
21915 && die->tag != DW_TAG_union_type)
71c25dea
TT
21916 return NULL;
21917
21918 switch (die->tag)
21919 {
21920 case DW_TAG_compile_unit:
95554aad 21921 case DW_TAG_partial_unit:
71c25dea
TT
21922 /* Compilation units have a DW_AT_name that is a filename, not
21923 a source language identifier. */
21924 case DW_TAG_enumeration_type:
21925 case DW_TAG_enumerator:
21926 /* These tags always have simple identifiers already; no need
21927 to canonicalize them. */
21928 return DW_STRING (attr);
907af001 21929
96553a0c
DE
21930 case DW_TAG_namespace:
21931 if (attr != NULL && DW_STRING (attr) != NULL)
21932 return DW_STRING (attr);
21933 return CP_ANONYMOUS_NAMESPACE_STR;
21934
907af001
UW
21935 case DW_TAG_class_type:
21936 case DW_TAG_interface_type:
21937 case DW_TAG_structure_type:
21938 case DW_TAG_union_type:
21939 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
21940 structures or unions. These were of the form "._%d" in GCC 4.1,
21941 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
21942 and GCC 4.4. We work around this problem by ignoring these. */
53832f31 21943 if (attr && DW_STRING (attr)
61012eef
GB
21944 && (startswith (DW_STRING (attr), "._")
21945 || startswith (DW_STRING (attr), "<anonymous")))
907af001 21946 return NULL;
53832f31
TT
21947
21948 /* GCC might emit a nameless typedef that has a linkage name. See
21949 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
21950 if (!attr || DW_STRING (attr) == NULL)
21951 {
73b9be8b 21952 attr = dw2_linkage_name_attr (die, cu);
53832f31
TT
21953 if (attr == NULL || DW_STRING (attr) == NULL)
21954 return NULL;
21955
df5c6c50
JK
21956 /* Avoid demangling DW_STRING (attr) the second time on a second
21957 call for the same DIE. */
21958 if (!DW_STRING_IS_CANONICAL (attr))
53832f31 21959 {
43816ebc
TT
21960 gdb::unique_xmalloc_ptr<char> demangled
21961 (gdb_demangle (DW_STRING (attr), DMGL_TYPES));
4f180d53
AT
21962 if (demangled == nullptr)
21963 return nullptr;
43816ebc 21964
be1e3d3e 21965 DW_STRING (attr) = objfile->intern (demangled.get ());
53832f31 21966 DW_STRING_IS_CANONICAL (attr) = 1;
53832f31 21967 }
67430cd0
TT
21968
21969 /* Strip any leading namespaces/classes, keep only the base name.
21970 DW_AT_name for named DIEs does not contain the prefixes. */
21971 const char *base = strrchr (DW_STRING (attr), ':');
21972 if (base && base > DW_STRING (attr) && base[-1] == ':')
21973 return &base[1];
21974 else
21975 return DW_STRING (attr);
53832f31 21976 }
907af001
UW
21977 break;
21978
71c25dea 21979 default:
907af001
UW
21980 break;
21981 }
21982
21983 if (!DW_STRING_IS_CANONICAL (attr))
21984 {
be1e3d3e
TT
21985 DW_STRING (attr) = dwarf2_canonicalize_name (DW_STRING (attr), cu,
21986 objfile);
907af001 21987 DW_STRING_IS_CANONICAL (attr) = 1;
71c25dea 21988 }
907af001 21989 return DW_STRING (attr);
9219021c
DC
21990}
21991
21992/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
21993 is none. *EXT_CU is the CU containing DIE on input, and the CU
21994 containing the return value on output. */
9219021c
DC
21995
21996static struct die_info *
f2f0e013 21997dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
21998{
21999 struct attribute *attr;
9219021c 22000
f2f0e013 22001 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
22002 if (attr == NULL)
22003 return NULL;
22004
f2f0e013 22005 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
22006}
22007
f9aca02d 22008static void
d97bc12b 22009dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
22010{
22011 unsigned int i;
22012
d97bc12b 22013 print_spaces (indent, f);
9d8780f0 22014 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 22015 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 22016 sect_offset_str (die->sect_off));
d97bc12b
DE
22017
22018 if (die->parent != NULL)
22019 {
22020 print_spaces (indent, f);
9d8780f0
SM
22021 fprintf_unfiltered (f, " parent at offset: %s\n",
22022 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
22023 }
22024
22025 print_spaces (indent, f);
22026 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 22027 dwarf_bool_name (die->child != NULL));
c906108c 22028
d97bc12b
DE
22029 print_spaces (indent, f);
22030 fprintf_unfiltered (f, " attributes:\n");
22031
c906108c
SS
22032 for (i = 0; i < die->num_attrs; ++i)
22033 {
d97bc12b
DE
22034 print_spaces (indent, f);
22035 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
22036 dwarf_attr_name (die->attrs[i].name),
22037 dwarf_form_name (die->attrs[i].form));
d97bc12b 22038
c906108c
SS
22039 switch (die->attrs[i].form)
22040 {
c906108c 22041 case DW_FORM_addr:
336d760d 22042 case DW_FORM_addrx:
3019eac3 22043 case DW_FORM_GNU_addr_index:
d97bc12b 22044 fprintf_unfiltered (f, "address: ");
5af949e3 22045 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
c906108c
SS
22046 break;
22047 case DW_FORM_block2:
22048 case DW_FORM_block4:
22049 case DW_FORM_block:
22050 case DW_FORM_block1:
56eb65bd
SP
22051 fprintf_unfiltered (f, "block: size %s",
22052 pulongest (DW_BLOCK (&die->attrs[i])->size));
c906108c 22053 break;
2dc7f7b3 22054 case DW_FORM_exprloc:
56eb65bd
SP
22055 fprintf_unfiltered (f, "expression: size %s",
22056 pulongest (DW_BLOCK (&die->attrs[i])->size));
2dc7f7b3 22057 break;
0224619f
JK
22058 case DW_FORM_data16:
22059 fprintf_unfiltered (f, "constant of 16 bytes");
22060 break;
4568ecf9
DE
22061 case DW_FORM_ref_addr:
22062 fprintf_unfiltered (f, "ref address: ");
22063 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22064 break;
36586728
TT
22065 case DW_FORM_GNU_ref_alt:
22066 fprintf_unfiltered (f, "alt ref address: ");
22067 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
22068 break;
10b3939b
DJ
22069 case DW_FORM_ref1:
22070 case DW_FORM_ref2:
22071 case DW_FORM_ref4:
4568ecf9
DE
22072 case DW_FORM_ref8:
22073 case DW_FORM_ref_udata:
d97bc12b 22074 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
4568ecf9 22075 (long) (DW_UNSND (&die->attrs[i])));
10b3939b 22076 break;
c906108c
SS
22077 case DW_FORM_data1:
22078 case DW_FORM_data2:
22079 case DW_FORM_data4:
ce5d95e1 22080 case DW_FORM_data8:
c906108c
SS
22081 case DW_FORM_udata:
22082 case DW_FORM_sdata:
43bbcdc2
PH
22083 fprintf_unfiltered (f, "constant: %s",
22084 pulongest (DW_UNSND (&die->attrs[i])));
c906108c 22085 break;
2dc7f7b3
TT
22086 case DW_FORM_sec_offset:
22087 fprintf_unfiltered (f, "section offset: %s",
22088 pulongest (DW_UNSND (&die->attrs[i])));
22089 break;
55f1336d 22090 case DW_FORM_ref_sig8:
ac9ec31b
DE
22091 fprintf_unfiltered (f, "signature: %s",
22092 hex_string (DW_SIGNATURE (&die->attrs[i])));
348e048f 22093 break;
c906108c 22094 case DW_FORM_string:
4bdf3d34 22095 case DW_FORM_strp:
43988095 22096 case DW_FORM_line_strp:
cf532bd1 22097 case DW_FORM_strx:
3019eac3 22098 case DW_FORM_GNU_str_index:
36586728 22099 case DW_FORM_GNU_strp_alt:
8285870a 22100 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c906108c 22101 DW_STRING (&die->attrs[i])
8285870a
JK
22102 ? DW_STRING (&die->attrs[i]) : "",
22103 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
c906108c
SS
22104 break;
22105 case DW_FORM_flag:
22106 if (DW_UNSND (&die->attrs[i]))
d97bc12b 22107 fprintf_unfiltered (f, "flag: TRUE");
c906108c 22108 else
d97bc12b 22109 fprintf_unfiltered (f, "flag: FALSE");
c906108c 22110 break;
2dc7f7b3
TT
22111 case DW_FORM_flag_present:
22112 fprintf_unfiltered (f, "flag: TRUE");
22113 break;
a8329558 22114 case DW_FORM_indirect:
0963b4bd
MS
22115 /* The reader will have reduced the indirect form to
22116 the "base form" so this form should not occur. */
5f48f8f3 22117 fprintf_unfiltered (f,
3e43a32a 22118 "unexpected attribute form: DW_FORM_indirect");
a8329558 22119 break;
663c44ac
JK
22120 case DW_FORM_implicit_const:
22121 fprintf_unfiltered (f, "constant: %s",
22122 plongest (DW_SND (&die->attrs[i])));
22123 break;
c906108c 22124 default:
d97bc12b 22125 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 22126 die->attrs[i].form);
d97bc12b 22127 break;
c906108c 22128 }
d97bc12b 22129 fprintf_unfiltered (f, "\n");
c906108c
SS
22130 }
22131}
22132
f9aca02d 22133static void
d97bc12b 22134dump_die_for_error (struct die_info *die)
c906108c 22135{
d97bc12b
DE
22136 dump_die_shallow (gdb_stderr, 0, die);
22137}
22138
22139static void
22140dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
22141{
22142 int indent = level * 4;
22143
22144 gdb_assert (die != NULL);
22145
22146 if (level >= max_level)
22147 return;
22148
22149 dump_die_shallow (f, indent, die);
22150
22151 if (die->child != NULL)
c906108c 22152 {
d97bc12b
DE
22153 print_spaces (indent, f);
22154 fprintf_unfiltered (f, " Children:");
22155 if (level + 1 < max_level)
22156 {
22157 fprintf_unfiltered (f, "\n");
22158 dump_die_1 (f, level + 1, max_level, die->child);
22159 }
22160 else
22161 {
3e43a32a
MS
22162 fprintf_unfiltered (f,
22163 " [not printed, max nesting level reached]\n");
d97bc12b
DE
22164 }
22165 }
22166
22167 if (die->sibling != NULL && level > 0)
22168 {
22169 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
22170 }
22171}
22172
d97bc12b
DE
22173/* This is called from the pdie macro in gdbinit.in.
22174 It's not static so gcc will keep a copy callable from gdb. */
22175
22176void
22177dump_die (struct die_info *die, int max_level)
22178{
22179 dump_die_1 (gdb_stdlog, 0, max_level, die);
22180}
22181
f9aca02d 22182static void
51545339 22183store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22184{
51545339 22185 void **slot;
c906108c 22186
9c541725
PA
22187 slot = htab_find_slot_with_hash (cu->die_hash, die,
22188 to_underlying (die->sect_off),
b64f50a1 22189 INSERT);
51545339
DJ
22190
22191 *slot = die;
c906108c
SS
22192}
22193
348e048f
DE
22194/* Follow reference or signature attribute ATTR of SRC_DIE.
22195 On entry *REF_CU is the CU of SRC_DIE.
22196 On exit *REF_CU is the CU of the result. */
22197
22198static struct die_info *
ff39bb5e 22199follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
22200 struct dwarf2_cu **ref_cu)
22201{
22202 struct die_info *die;
22203
cd6c91b4 22204 if (attr->form_is_ref ())
348e048f 22205 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 22206 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
22207 die = follow_die_sig (src_die, attr, ref_cu);
22208 else
22209 {
22210 dump_die_for_error (src_die);
22211 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 22212 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
22213 }
22214
22215 return die;
03dd20cc
DJ
22216}
22217
5c631832 22218/* Follow reference OFFSET.
673bfd45
DE
22219 On entry *REF_CU is the CU of the source die referencing OFFSET.
22220 On exit *REF_CU is the CU of the result.
22221 Returns NULL if OFFSET is invalid. */
f504f079 22222
f9aca02d 22223static struct die_info *
9c541725 22224follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 22225 struct dwarf2_cu **ref_cu)
c906108c 22226{
10b3939b 22227 struct die_info temp_die;
f2f0e013 22228 struct dwarf2_cu *target_cu, *cu = *ref_cu;
5e22e966 22229 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
10b3939b 22230
348e048f
DE
22231 gdb_assert (cu->per_cu != NULL);
22232
98bfdba5
PA
22233 target_cu = cu;
22234
3019eac3 22235 if (cu->per_cu->is_debug_types)
348e048f
DE
22236 {
22237 /* .debug_types CUs cannot reference anything outside their CU.
22238 If they need to, they have to reference a signatured type via
55f1336d 22239 DW_FORM_ref_sig8. */
4057dfde 22240 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 22241 return NULL;
348e048f 22242 }
36586728 22243 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 22244 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
22245 {
22246 struct dwarf2_per_cu_data *per_cu;
9a619af0 22247
9c541725 22248 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
ed2dc618 22249 dwarf2_per_objfile);
03dd20cc
DJ
22250
22251 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 22252 if (maybe_queue_comp_unit (cu, per_cu, dwarf2_per_objfile, cu->language))
ab432490 22253 load_full_comp_unit (per_cu, dwarf2_per_objfile, false, cu->language);
03dd20cc 22254
7188ed02 22255 target_cu = dwarf2_per_objfile->get_cu (per_cu);
10b3939b 22256 }
98bfdba5
PA
22257 else if (cu->dies == NULL)
22258 {
22259 /* We're loading full DIEs during partial symbol reading. */
5989a64e 22260 gdb_assert (dwarf2_per_objfile->per_bfd->reading_partial_symbols);
ab432490
SM
22261 load_full_comp_unit (cu->per_cu, dwarf2_per_objfile, false,
22262 language_minimal);
98bfdba5 22263 }
c906108c 22264
f2f0e013 22265 *ref_cu = target_cu;
9c541725 22266 temp_die.sect_off = sect_off;
c24bdb02
KS
22267
22268 if (target_cu != cu)
22269 target_cu->ancestor = cu;
22270
9a3c8263 22271 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
22272 &temp_die,
22273 to_underlying (sect_off));
5c631832 22274}
10b3939b 22275
5c631832
JK
22276/* Follow reference attribute ATTR of SRC_DIE.
22277 On entry *REF_CU is the CU of SRC_DIE.
22278 On exit *REF_CU is the CU of the result. */
22279
22280static struct die_info *
ff39bb5e 22281follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
22282 struct dwarf2_cu **ref_cu)
22283{
0826b30a 22284 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
22285 struct dwarf2_cu *cu = *ref_cu;
22286 struct die_info *die;
22287
9c541725 22288 die = follow_die_offset (sect_off,
36586728
TT
22289 (attr->form == DW_FORM_GNU_ref_alt
22290 || cu->per_cu->is_dwz),
22291 ref_cu);
5c631832 22292 if (!die)
9d8780f0
SM
22293 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
22294 "at %s [in module %s]"),
22295 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 22296 objfile_name (cu->per_objfile->objfile));
348e048f 22297
5c631832
JK
22298 return die;
22299}
22300
d4c9a4f8 22301/* See read.h. */
5c631832
JK
22302
22303struct dwarf2_locexpr_baton
9c541725 22304dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 22305 dwarf2_per_cu_data *per_cu,
14095eb3 22306 dwarf2_per_objfile *dwarf2_per_objfile,
8b9737bf 22307 CORE_ADDR (*get_frame_pc) (void *baton),
e4a62c65 22308 void *baton, bool resolve_abstract_p)
5c631832 22309{
5c631832
JK
22310 struct die_info *die;
22311 struct attribute *attr;
22312 struct dwarf2_locexpr_baton retval;
12359b5e 22313 struct objfile *objfile = dwarf2_per_objfile->objfile;
8cf6f0b1 22314
7188ed02 22315 dwarf2_cu *cu = dwarf2_per_objfile->get_cu (per_cu);
1b555f17
SM
22316 if (cu == nullptr)
22317 cu = load_cu (per_cu, dwarf2_per_objfile, false);
22318
22319 if (cu == nullptr)
cc12ce38
DE
22320 {
22321 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22322 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22323 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22324 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22325 }
918dd910 22326
9c541725 22327 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 22328 if (!die)
9d8780f0
SM
22329 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22330 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22331
22332 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 22333 if (!attr && resolve_abstract_p
5989a64e
SM
22334 && (dwarf2_per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
22335 != dwarf2_per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65
TV
22336 {
22337 CORE_ADDR pc = (*get_frame_pc) (baton);
b3b3bada 22338 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 22339 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 22340
3360b6e7 22341 for (const auto &cand_off
5989a64e 22342 : dwarf2_per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 22343 {
3360b6e7
TV
22344 struct dwarf2_cu *cand_cu = cu;
22345 struct die_info *cand
22346 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
22347 if (!cand
22348 || !cand->parent
e4a62c65
TV
22349 || cand->parent->tag != DW_TAG_subprogram)
22350 continue;
22351
22352 CORE_ADDR pc_low, pc_high;
22353 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
22354 if (pc_low == ((CORE_ADDR) -1))
22355 continue;
22356 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
22357 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
22358 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
22359 continue;
22360
22361 die = cand;
22362 attr = dwarf2_attr (die, DW_AT_location, cu);
22363 break;
22364 }
22365 }
22366
5c631832
JK
22367 if (!attr)
22368 {
e103e986
JK
22369 /* DWARF: "If there is no such attribute, then there is no effect.".
22370 DATA is ignored if SIZE is 0. */
5c631832 22371
e103e986 22372 retval.data = NULL;
5c631832
JK
22373 retval.size = 0;
22374 }
cd6c91b4 22375 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
22376 {
22377 struct dwarf2_loclist_baton loclist_baton;
22378 CORE_ADDR pc = (*get_frame_pc) (baton);
22379 size_t size;
22380
22381 fill_in_loclist_baton (cu, &loclist_baton, attr);
22382
22383 retval.data = dwarf2_find_location_expression (&loclist_baton,
22384 &size, pc);
22385 retval.size = size;
22386 }
5c631832
JK
22387 else
22388 {
4fc6c0d5 22389 if (!attr->form_is_block ())
9d8780f0 22390 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 22391 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 22392 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
22393
22394 retval.data = DW_BLOCK (attr)->data;
22395 retval.size = DW_BLOCK (attr)->size;
22396 }
a50264ba 22397 retval.per_objfile = dwarf2_per_objfile;
5c631832 22398 retval.per_cu = cu->per_cu;
918dd910 22399
7188ed02 22400 dwarf2_per_objfile->age_comp_units ();
918dd910 22401
5c631832 22402 return retval;
348e048f
DE
22403}
22404
d4c9a4f8 22405/* See read.h. */
8b9737bf
TT
22406
22407struct dwarf2_locexpr_baton
22408dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 22409 dwarf2_per_cu_data *per_cu,
14095eb3 22410 dwarf2_per_objfile *per_objfile,
8b9737bf
TT
22411 CORE_ADDR (*get_frame_pc) (void *baton),
22412 void *baton)
22413{
9c541725 22414 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 22415
14095eb3
SM
22416 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
22417 get_frame_pc, baton);
8b9737bf
TT
22418}
22419
b6807d98
TT
22420/* Write a constant of a given type as target-ordered bytes into
22421 OBSTACK. */
22422
22423static const gdb_byte *
22424write_constant_as_bytes (struct obstack *obstack,
22425 enum bfd_endian byte_order,
22426 struct type *type,
22427 ULONGEST value,
22428 LONGEST *len)
22429{
22430 gdb_byte *result;
22431
22432 *len = TYPE_LENGTH (type);
224c3ddb 22433 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22434 store_unsigned_integer (result, *len, byte_order, value);
22435
22436 return result;
22437}
22438
d4c9a4f8 22439/* See read.h. */
b6807d98
TT
22440
22441const gdb_byte *
9c541725 22442dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 22443 dwarf2_per_cu_data *per_cu,
14095eb3 22444 dwarf2_per_objfile *per_objfile,
d4c9a4f8 22445 obstack *obstack,
b6807d98
TT
22446 LONGEST *len)
22447{
b6807d98
TT
22448 struct die_info *die;
22449 struct attribute *attr;
22450 const gdb_byte *result = NULL;
22451 struct type *type;
22452 LONGEST value;
22453 enum bfd_endian byte_order;
14095eb3 22454 struct objfile *objfile = per_objfile->objfile;
b6807d98 22455
7188ed02 22456 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22457 if (cu == nullptr)
22458 cu = load_cu (per_cu, per_objfile, false);
22459
22460 if (cu == nullptr)
cc12ce38
DE
22461 {
22462 /* We shouldn't get here for a dummy CU, but don't crash on the user.
22463 Instead just throw an error, not much else we can do. */
9d8780f0
SM
22464 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
22465 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 22466 }
b6807d98 22467
9c541725 22468 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 22469 if (!die)
9d8780f0
SM
22470 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
22471 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
22472
22473 attr = dwarf2_attr (die, DW_AT_const_value, cu);
22474 if (attr == NULL)
22475 return NULL;
22476
e3b94546 22477 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
22478 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22479
22480 switch (attr->form)
22481 {
22482 case DW_FORM_addr:
336d760d 22483 case DW_FORM_addrx:
b6807d98
TT
22484 case DW_FORM_GNU_addr_index:
22485 {
22486 gdb_byte *tem;
22487
22488 *len = cu->header.addr_size;
224c3ddb 22489 tem = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
22490 store_unsigned_integer (tem, *len, byte_order, DW_ADDR (attr));
22491 result = tem;
22492 }
22493 break;
22494 case DW_FORM_string:
22495 case DW_FORM_strp:
cf532bd1 22496 case DW_FORM_strx:
b6807d98
TT
22497 case DW_FORM_GNU_str_index:
22498 case DW_FORM_GNU_strp_alt:
22499 /* DW_STRING is already allocated on the objfile obstack, point
22500 directly to it. */
22501 result = (const gdb_byte *) DW_STRING (attr);
22502 *len = strlen (DW_STRING (attr));
22503 break;
22504 case DW_FORM_block1:
22505 case DW_FORM_block2:
22506 case DW_FORM_block4:
22507 case DW_FORM_block:
22508 case DW_FORM_exprloc:
0224619f 22509 case DW_FORM_data16:
b6807d98
TT
22510 result = DW_BLOCK (attr)->data;
22511 *len = DW_BLOCK (attr)->size;
22512 break;
22513
22514 /* The DW_AT_const_value attributes are supposed to carry the
22515 symbol's value "represented as it would be on the target
22516 architecture." By the time we get here, it's already been
22517 converted to host endianness, so we just need to sign- or
22518 zero-extend it as appropriate. */
22519 case DW_FORM_data1:
22520 type = die_type (die, cu);
22521 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
22522 if (result == NULL)
22523 result = write_constant_as_bytes (obstack, byte_order,
22524 type, value, len);
22525 break;
22526 case DW_FORM_data2:
22527 type = die_type (die, cu);
22528 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
22529 if (result == NULL)
22530 result = write_constant_as_bytes (obstack, byte_order,
22531 type, value, len);
22532 break;
22533 case DW_FORM_data4:
22534 type = die_type (die, cu);
22535 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
22536 if (result == NULL)
22537 result = write_constant_as_bytes (obstack, byte_order,
22538 type, value, len);
22539 break;
22540 case DW_FORM_data8:
22541 type = die_type (die, cu);
22542 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
22543 if (result == NULL)
22544 result = write_constant_as_bytes (obstack, byte_order,
22545 type, value, len);
22546 break;
22547
22548 case DW_FORM_sdata:
663c44ac 22549 case DW_FORM_implicit_const:
b6807d98
TT
22550 type = die_type (die, cu);
22551 result = write_constant_as_bytes (obstack, byte_order,
22552 type, DW_SND (attr), len);
22553 break;
22554
22555 case DW_FORM_udata:
22556 type = die_type (die, cu);
22557 result = write_constant_as_bytes (obstack, byte_order,
22558 type, DW_UNSND (attr), len);
22559 break;
22560
22561 default:
b98664d3 22562 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
22563 dwarf_form_name (attr->form));
22564 break;
22565 }
22566
22567 return result;
22568}
22569
d4c9a4f8 22570/* See read.h. */
7942e96e
AA
22571
22572struct type *
9c541725 22573dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
22574 dwarf2_per_cu_data *per_cu,
22575 dwarf2_per_objfile *per_objfile)
7942e96e 22576{
7942e96e
AA
22577 struct die_info *die;
22578
7188ed02 22579 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
22580 if (cu == nullptr)
22581 cu = load_cu (per_cu, per_objfile, false);
22582
22583 if (cu == nullptr)
22584 return nullptr;
7942e96e 22585
9c541725 22586 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
22587 if (!die)
22588 return NULL;
22589
22590 return die_type (die, cu);
22591}
22592
8cb5117c 22593/* See read.h. */
8a9b8146
TT
22594
22595struct type *
b64f50a1 22596dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
22597 dwarf2_per_cu_data *per_cu,
22598 dwarf2_per_objfile *per_objfile)
8a9b8146 22599{
9c541725 22600 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 22601 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
22602}
22603
ac9ec31b 22604/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 22605 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
22606 On exit *REF_CU is the CU of the result.
22607 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
22608
22609static struct die_info *
ac9ec31b
DE
22610follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
22611 struct dwarf2_cu **ref_cu)
348e048f 22612{
348e048f 22613 struct die_info temp_die;
c24bdb02 22614 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 22615 struct die_info *die;
120ce1b5
SM
22616 dwarf2_per_objfile *dwarf2_per_objfile = (*ref_cu)->per_objfile;
22617
348e048f 22618
ac9ec31b
DE
22619 /* While it might be nice to assert sig_type->type == NULL here,
22620 we can get here for DW_AT_imported_declaration where we need
22621 the DIE not the type. */
348e048f
DE
22622
22623 /* If necessary, add it to the queue and load its DIEs. */
22624
120ce1b5
SM
22625 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, dwarf2_per_objfile,
22626 language_minimal))
22627 read_signatured_type (sig_type, dwarf2_per_objfile);
348e048f 22628
7188ed02 22629 sig_cu = dwarf2_per_objfile->get_cu (&sig_type->per_cu);
69d751e3 22630 gdb_assert (sig_cu != NULL);
9c541725
PA
22631 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
22632 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 22633 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 22634 to_underlying (temp_die.sect_off));
348e048f
DE
22635 if (die)
22636 {
796a7ff8
DE
22637 /* For .gdb_index version 7 keep track of included TUs.
22638 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
5989a64e
SM
22639 if (dwarf2_per_objfile->per_bfd->index_table != NULL
22640 && dwarf2_per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 22641 {
ae640021 22642 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
22643 }
22644
348e048f 22645 *ref_cu = sig_cu;
c24bdb02
KS
22646 if (sig_cu != cu)
22647 sig_cu->ancestor = cu;
22648
348e048f
DE
22649 return die;
22650 }
22651
ac9ec31b
DE
22652 return NULL;
22653}
22654
22655/* Follow signatured type referenced by ATTR in SRC_DIE.
22656 On entry *REF_CU is the CU of SRC_DIE.
22657 On exit *REF_CU is the CU of the result.
22658 The result is the DIE of the type.
22659 If the referenced type cannot be found an error is thrown. */
22660
22661static struct die_info *
ff39bb5e 22662follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
22663 struct dwarf2_cu **ref_cu)
22664{
22665 ULONGEST signature = DW_SIGNATURE (attr);
22666 struct signatured_type *sig_type;
22667 struct die_info *die;
22668
22669 gdb_assert (attr->form == DW_FORM_ref_sig8);
22670
a2ce51a0 22671 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
22672 /* sig_type will be NULL if the signatured type is missing from
22673 the debug info. */
22674 if (sig_type == NULL)
22675 {
22676 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22677 " from DIE at %s [in module %s]"),
22678 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22679 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22680 }
22681
22682 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
22683 if (die == NULL)
22684 {
22685 dump_die_for_error (src_die);
22686 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22687 " from DIE at %s [in module %s]"),
22688 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 22689 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
22690 }
22691
22692 return die;
22693}
22694
22695/* Get the type specified by SIGNATURE referenced in DIE/CU,
22696 reading in and processing the type unit if necessary. */
22697
22698static struct type *
22699get_signatured_type (struct die_info *die, ULONGEST signature,
22700 struct dwarf2_cu *cu)
22701{
5e22e966 22702 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ac9ec31b
DE
22703 struct signatured_type *sig_type;
22704 struct dwarf2_cu *type_cu;
22705 struct die_info *type_die;
22706 struct type *type;
22707
a2ce51a0 22708 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
22709 /* sig_type will be NULL if the signatured type is missing from
22710 the debug info. */
22711 if (sig_type == NULL)
22712 {
b98664d3 22713 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
22714 " from DIE at %s [in module %s]"),
22715 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22716 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22717 return build_error_marker_type (cu, die);
22718 }
22719
22720 /* If we already know the type we're done. */
e286671b
TT
22721 type = dwarf2_per_objfile->get_type_for_signatured_type (sig_type);
22722 if (type != nullptr)
22723 return type;
ac9ec31b
DE
22724
22725 type_cu = cu;
22726 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
22727 if (type_die != NULL)
22728 {
22729 /* N.B. We need to call get_die_type to ensure only one type for this DIE
22730 is created. This is important, for example, because for c++ classes
22731 we need TYPE_NAME set which is only done by new_symbol. Blech. */
22732 type = read_type_die (type_die, type_cu);
22733 if (type == NULL)
22734 {
b98664d3 22735 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
22736 " referenced from DIE at %s [in module %s]"),
22737 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22738 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22739 type = build_error_marker_type (cu, die);
22740 }
22741 }
22742 else
22743 {
b98664d3 22744 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
22745 " from DIE at %s [in module %s]"),
22746 hex_string (signature), sect_offset_str (die->sect_off),
4262abfb 22747 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22748 type = build_error_marker_type (cu, die);
22749 }
e286671b
TT
22750
22751 dwarf2_per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
22752
22753 return type;
22754}
22755
22756/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
22757 reading in and processing the type unit if necessary. */
22758
22759static struct type *
ff39bb5e 22760get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 22761 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
22762{
22763 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 22764 if (attr->form_is_ref ())
ac9ec31b
DE
22765 {
22766 struct dwarf2_cu *type_cu = cu;
22767 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
22768
22769 return read_type_die (type_die, type_cu);
22770 }
22771 else if (attr->form == DW_FORM_ref_sig8)
22772 {
22773 return get_signatured_type (die, DW_SIGNATURE (attr), cu);
22774 }
22775 else
22776 {
5e22e966 22777 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 22778
b98664d3 22779 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
22780 " at %s [in module %s]"),
22781 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
4262abfb 22782 objfile_name (dwarf2_per_objfile->objfile));
ac9ec31b
DE
22783 return build_error_marker_type (cu, die);
22784 }
348e048f
DE
22785}
22786
e5fe5e75 22787/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
22788
22789static void
ab432490
SM
22790load_full_type_unit (dwarf2_per_cu_data *per_cu,
22791 dwarf2_per_objfile *per_objfile)
348e048f 22792{
52dc124a 22793 struct signatured_type *sig_type;
348e048f 22794
f4dc4d17 22795 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 22796 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 22797
6721b2ec
DE
22798 /* We have the per_cu, but we need the signatured_type.
22799 Fortunately this is an easy translation. */
22800 gdb_assert (per_cu->is_debug_types);
22801 sig_type = (struct signatured_type *) per_cu;
348e048f 22802
7188ed02 22803 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22804
ab432490 22805 read_signatured_type (sig_type, per_objfile);
348e048f 22806
7188ed02 22807 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
22808}
22809
3019eac3
DE
22810/* Read in a signatured type and build its CU and DIEs.
22811 If the type is a stub for the real type in a DWO file,
22812 read in the real type from the DWO file as well. */
dee91e82
DE
22813
22814static void
ab432490
SM
22815read_signatured_type (signatured_type *sig_type,
22816 dwarf2_per_objfile *per_objfile)
dee91e82
DE
22817{
22818 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 22819
3019eac3 22820 gdb_assert (per_cu->is_debug_types);
7188ed02 22821 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 22822
2e671100 22823 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
22824
22825 if (!reader.dummy_p)
22826 {
22827 struct dwarf2_cu *cu = reader.cu;
22828 const gdb_byte *info_ptr = reader.info_ptr;
22829
22830 gdb_assert (cu->die_hash == NULL);
22831 cu->die_hash =
22832 htab_create_alloc_ex (cu->header.length / 12,
22833 die_hash,
22834 die_eq,
22835 NULL,
22836 &cu->comp_unit_obstack,
22837 hashtab_obstack_allocate,
22838 dummy_obstack_deallocate);
22839
3e225074 22840 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
22841 reader.comp_unit_die->child
22842 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
22843 reader.comp_unit_die);
22844 cu->dies = reader.comp_unit_die;
22845 /* comp_unit_die is not stored in die_hash, no need. */
22846
22847 /* We try not to read any attributes in this function, because
22848 not all CUs needed for references have been loaded yet, and
22849 symbol table processing isn't initialized. But we have to
22850 set the CU language, or we won't be able to build types
22851 correctly. Similarly, if we do not read the producer, we can
22852 not apply producer-specific interpretation. */
22853 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
22854
22855 reader.keep ();
c0ab21c2
TT
22856 }
22857
7ee85ab1 22858 sig_type->per_cu.tu_read = 1;
c906108c
SS
22859}
22860
c906108c
SS
22861/* Decode simple location descriptions.
22862 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
22863 the location and return the value. If COMPUTED is non-null, it is
22864 set to true to indicate that decoding was successful, and false
22865 otherwise. If COMPUTED is null, then this function may emit a
22866 complaint. */
c906108c
SS
22867
22868static CORE_ADDR
7d79de9a 22869decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 22870{
5e22e966 22871 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
22872 size_t i;
22873 size_t size = blk->size;
d521ce57 22874 const gdb_byte *data = blk->data;
21ae7a4d
JK
22875 CORE_ADDR stack[64];
22876 int stacki;
22877 unsigned int bytes_read, unsnd;
22878 gdb_byte op;
c906108c 22879
7d79de9a
TT
22880 if (computed != nullptr)
22881 *computed = false;
22882
21ae7a4d
JK
22883 i = 0;
22884 stacki = 0;
22885 stack[stacki] = 0;
22886 stack[++stacki] = 0;
22887
22888 while (i < size)
22889 {
22890 op = data[i++];
22891 switch (op)
22892 {
22893 case DW_OP_lit0:
22894 case DW_OP_lit1:
22895 case DW_OP_lit2:
22896 case DW_OP_lit3:
22897 case DW_OP_lit4:
22898 case DW_OP_lit5:
22899 case DW_OP_lit6:
22900 case DW_OP_lit7:
22901 case DW_OP_lit8:
22902 case DW_OP_lit9:
22903 case DW_OP_lit10:
22904 case DW_OP_lit11:
22905 case DW_OP_lit12:
22906 case DW_OP_lit13:
22907 case DW_OP_lit14:
22908 case DW_OP_lit15:
22909 case DW_OP_lit16:
22910 case DW_OP_lit17:
22911 case DW_OP_lit18:
22912 case DW_OP_lit19:
22913 case DW_OP_lit20:
22914 case DW_OP_lit21:
22915 case DW_OP_lit22:
22916 case DW_OP_lit23:
22917 case DW_OP_lit24:
22918 case DW_OP_lit25:
22919 case DW_OP_lit26:
22920 case DW_OP_lit27:
22921 case DW_OP_lit28:
22922 case DW_OP_lit29:
22923 case DW_OP_lit30:
22924 case DW_OP_lit31:
22925 stack[++stacki] = op - DW_OP_lit0;
22926 break;
f1bea926 22927
21ae7a4d
JK
22928 case DW_OP_reg0:
22929 case DW_OP_reg1:
22930 case DW_OP_reg2:
22931 case DW_OP_reg3:
22932 case DW_OP_reg4:
22933 case DW_OP_reg5:
22934 case DW_OP_reg6:
22935 case DW_OP_reg7:
22936 case DW_OP_reg8:
22937 case DW_OP_reg9:
22938 case DW_OP_reg10:
22939 case DW_OP_reg11:
22940 case DW_OP_reg12:
22941 case DW_OP_reg13:
22942 case DW_OP_reg14:
22943 case DW_OP_reg15:
22944 case DW_OP_reg16:
22945 case DW_OP_reg17:
22946 case DW_OP_reg18:
22947 case DW_OP_reg19:
22948 case DW_OP_reg20:
22949 case DW_OP_reg21:
22950 case DW_OP_reg22:
22951 case DW_OP_reg23:
22952 case DW_OP_reg24:
22953 case DW_OP_reg25:
22954 case DW_OP_reg26:
22955 case DW_OP_reg27:
22956 case DW_OP_reg28:
22957 case DW_OP_reg29:
22958 case DW_OP_reg30:
22959 case DW_OP_reg31:
22960 stack[++stacki] = op - DW_OP_reg0;
22961 if (i < size)
7d79de9a
TT
22962 {
22963 if (computed == nullptr)
22964 dwarf2_complex_location_expr_complaint ();
22965 else
22966 return 0;
22967 }
21ae7a4d 22968 break;
c906108c 22969
21ae7a4d
JK
22970 case DW_OP_regx:
22971 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
22972 i += bytes_read;
22973 stack[++stacki] = unsnd;
22974 if (i < size)
7d79de9a
TT
22975 {
22976 if (computed == nullptr)
22977 dwarf2_complex_location_expr_complaint ();
22978 else
22979 return 0;
22980 }
21ae7a4d 22981 break;
c906108c 22982
21ae7a4d 22983 case DW_OP_addr:
c8a7a66f
TT
22984 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
22985 &bytes_read);
21ae7a4d
JK
22986 i += bytes_read;
22987 break;
d53d4ac5 22988
21ae7a4d
JK
22989 case DW_OP_const1u:
22990 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
22991 i += 1;
22992 break;
22993
22994 case DW_OP_const1s:
22995 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
22996 i += 1;
22997 break;
22998
22999 case DW_OP_const2u:
23000 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
23001 i += 2;
23002 break;
23003
23004 case DW_OP_const2s:
23005 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
23006 i += 2;
23007 break;
d53d4ac5 23008
21ae7a4d
JK
23009 case DW_OP_const4u:
23010 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
23011 i += 4;
23012 break;
23013
23014 case DW_OP_const4s:
23015 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
23016 i += 4;
23017 break;
23018
585861ea
JK
23019 case DW_OP_const8u:
23020 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
23021 i += 8;
23022 break;
23023
21ae7a4d
JK
23024 case DW_OP_constu:
23025 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
23026 &bytes_read);
23027 i += bytes_read;
23028 break;
23029
23030 case DW_OP_consts:
23031 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
23032 i += bytes_read;
23033 break;
23034
23035 case DW_OP_dup:
23036 stack[stacki + 1] = stack[stacki];
23037 stacki++;
23038 break;
23039
23040 case DW_OP_plus:
23041 stack[stacki - 1] += stack[stacki];
23042 stacki--;
23043 break;
23044
23045 case DW_OP_plus_uconst:
23046 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
23047 &bytes_read);
23048 i += bytes_read;
23049 break;
23050
23051 case DW_OP_minus:
23052 stack[stacki - 1] -= stack[stacki];
23053 stacki--;
23054 break;
23055
23056 case DW_OP_deref:
23057 /* If we're not the last op, then we definitely can't encode
23058 this using GDB's address_class enum. This is valid for partial
23059 global symbols, although the variable's address will be bogus
23060 in the psymtab. */
23061 if (i < size)
7d79de9a
TT
23062 {
23063 if (computed == nullptr)
23064 dwarf2_complex_location_expr_complaint ();
23065 else
23066 return 0;
23067 }
21ae7a4d
JK
23068 break;
23069
23070 case DW_OP_GNU_push_tls_address:
4aa4e28b 23071 case DW_OP_form_tls_address:
21ae7a4d
JK
23072 /* The top of the stack has the offset from the beginning
23073 of the thread control block at which the variable is located. */
23074 /* Nothing should follow this operator, so the top of stack would
23075 be returned. */
23076 /* This is valid for partial global symbols, but the variable's
585861ea
JK
23077 address will be bogus in the psymtab. Make it always at least
23078 non-zero to not look as a variable garbage collected by linker
23079 which have DW_OP_addr 0. */
21ae7a4d 23080 if (i < size)
7d79de9a
TT
23081 {
23082 if (computed == nullptr)
23083 dwarf2_complex_location_expr_complaint ();
23084 else
23085 return 0;
23086 }
585861ea 23087 stack[stacki]++;
21ae7a4d
JK
23088 break;
23089
23090 case DW_OP_GNU_uninit:
7d79de9a
TT
23091 if (computed != nullptr)
23092 return 0;
21ae7a4d
JK
23093 break;
23094
336d760d 23095 case DW_OP_addrx:
3019eac3 23096 case DW_OP_GNU_addr_index:
49f6c839 23097 case DW_OP_GNU_const_index:
3019eac3
DE
23098 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
23099 &bytes_read);
23100 i += bytes_read;
23101 break;
23102
21ae7a4d 23103 default:
7d79de9a
TT
23104 if (computed == nullptr)
23105 {
23106 const char *name = get_DW_OP_name (op);
21ae7a4d 23107
7d79de9a
TT
23108 if (name)
23109 complaint (_("unsupported stack op: '%s'"),
23110 name);
23111 else
23112 complaint (_("unsupported stack op: '%02x'"),
23113 op);
23114 }
21ae7a4d
JK
23115
23116 return (stack[stacki]);
d53d4ac5 23117 }
3c6e0cb3 23118
21ae7a4d
JK
23119 /* Enforce maximum stack depth of SIZE-1 to avoid writing
23120 outside of the allocated space. Also enforce minimum>0. */
23121 if (stacki >= ARRAY_SIZE (stack) - 1)
23122 {
7d79de9a
TT
23123 if (computed == nullptr)
23124 complaint (_("location description stack overflow"));
21ae7a4d
JK
23125 return 0;
23126 }
23127
23128 if (stacki <= 0)
23129 {
7d79de9a
TT
23130 if (computed == nullptr)
23131 complaint (_("location description stack underflow"));
21ae7a4d
JK
23132 return 0;
23133 }
23134 }
7d79de9a
TT
23135
23136 if (computed != nullptr)
23137 *computed = true;
21ae7a4d 23138 return (stack[stacki]);
c906108c
SS
23139}
23140
23141/* memory allocation interface */
23142
c906108c 23143static struct dwarf_block *
7b5a2f43 23144dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 23145{
8d749320 23146 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
23147}
23148
c906108c 23149static struct die_info *
b60c80d6 23150dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
23151{
23152 struct die_info *die;
b60c80d6
DJ
23153 size_t size = sizeof (struct die_info);
23154
23155 if (num_attrs > 1)
23156 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 23157
b60c80d6 23158 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
23159 memset (die, 0, sizeof (struct die_info));
23160 return (die);
23161}
2e276125
JB
23162
23163\f
a036ba48 23164
c90ec28a 23165/* Macro support. */
cf2c3c16 23166
9eac9650
TT
23167/* An overload of dwarf_decode_macros that finds the correct section
23168 and ensures it is read in before calling the other overload. */
23169
23170static void
23171dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
23172 int section_is_gnu)
23173{
5e22e966 23174 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
9eac9650 23175 struct objfile *objfile = dwarf2_per_objfile->objfile;
5a0e026f 23176 const struct line_header *lh = cu->line_header;
9eac9650
TT
23177 unsigned int offset_size = cu->header.offset_size;
23178 struct dwarf2_section_info *section;
23179 const char *section_name;
23180
23181 if (cu->dwo_unit != nullptr)
23182 {
23183 if (section_is_gnu)
23184 {
23185 section = &cu->dwo_unit->dwo_file->sections.macro;
23186 section_name = ".debug_macro.dwo";
23187 }
23188 else
23189 {
23190 section = &cu->dwo_unit->dwo_file->sections.macinfo;
23191 section_name = ".debug_macinfo.dwo";
23192 }
23193 }
23194 else
23195 {
23196 if (section_is_gnu)
23197 {
5989a64e 23198 section = &dwarf2_per_objfile->per_bfd->macro;
9eac9650
TT
23199 section_name = ".debug_macro";
23200 }
23201 else
23202 {
5989a64e 23203 section = &dwarf2_per_objfile->per_bfd->macinfo;
9eac9650
TT
23204 section_name = ".debug_macinfo";
23205 }
23206 }
23207
23208 section->read (objfile);
23209 if (section->buffer == nullptr)
23210 {
23211 complaint (_("missing %s section"), section_name);
23212 return;
23213 }
23214
23215 buildsym_compunit *builder = cu->get_builder ();
23216
23217 dwarf_decode_macros (dwarf2_per_objfile, builder, section, lh,
23218 offset_size, offset, section_is_gnu);
23219}
23220
3019eac3
DE
23221/* Return the .debug_loc section to use for CU.
23222 For DWO files use .debug_loc.dwo. */
23223
23224static struct dwarf2_section_info *
23225cu_debug_loc_section (struct dwarf2_cu *cu)
23226{
5e22e966 23227 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
ed2dc618 23228
3019eac3 23229 if (cu->dwo_unit)
43988095
JK
23230 {
23231 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 23232
43988095
JK
23233 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
23234 }
5989a64e
SM
23235 return (cu->header.version >= 5 ? &dwarf2_per_objfile->per_bfd->loclists
23236 : &dwarf2_per_objfile->per_bfd->loc);
3019eac3
DE
23237}
23238
8cf6f0b1
TT
23239/* A helper function that fills in a dwarf2_loclist_baton. */
23240
23241static void
23242fill_in_loclist_baton (struct dwarf2_cu *cu,
23243 struct dwarf2_loclist_baton *baton,
ff39bb5e 23244 const struct attribute *attr)
8cf6f0b1 23245{
5e22e966 23246 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
3019eac3
DE
23247 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
23248
96b79293 23249 section->read (dwarf2_per_objfile->objfile);
8cf6f0b1 23250
a50264ba 23251 baton->per_objfile = dwarf2_per_objfile;
8cf6f0b1
TT
23252 baton->per_cu = cu->per_cu;
23253 gdb_assert (baton->per_cu);
23254 /* We don't know how long the location list is, but make sure we
23255 don't run off the edge of the section. */
3019eac3
DE
23256 baton->size = section->size - DW_UNSND (attr);
23257 baton->data = section->buffer + DW_UNSND (attr);
2b24b6e4
TT
23258 if (cu->base_address.has_value ())
23259 baton->base_address = *cu->base_address;
23260 else
23261 baton->base_address = 0;
f664829e 23262 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
23263}
23264
4c2df51b 23265static void
ff39bb5e 23266dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 23267 struct dwarf2_cu *cu, int is_block)
4c2df51b 23268{
5e22e966 23269 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
bb5ed363 23270 struct objfile *objfile = dwarf2_per_objfile->objfile;
3019eac3 23271 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 23272
cd6c91b4 23273 if (attr->form_is_section_offset ()
3019eac3 23274 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
23275 the section. If so, fall through to the complaint in the
23276 other branch. */
2c7d5afc 23277 && DW_UNSND (attr) < section->get_size (objfile))
4c2df51b 23278 {
0d53c4c4 23279 struct dwarf2_loclist_baton *baton;
4c2df51b 23280
8d749320 23281 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 23282
8cf6f0b1 23283 fill_in_loclist_baton (cu, baton, attr);
be391dca 23284
2b24b6e4 23285 if (!cu->base_address.has_value ())
b98664d3 23286 complaint (_("Location list used without "
3e43a32a 23287 "specifying the CU base address."));
4c2df51b 23288
f1e6e072
TT
23289 SYMBOL_ACLASS_INDEX (sym) = (is_block
23290 ? dwarf2_loclist_block_index
23291 : dwarf2_loclist_index);
0d53c4c4
DJ
23292 SYMBOL_LOCATION_BATON (sym) = baton;
23293 }
23294 else
23295 {
23296 struct dwarf2_locexpr_baton *baton;
23297
8d749320 23298 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 23299 baton->per_objfile = dwarf2_per_objfile;
ae0d2f24
UW
23300 baton->per_cu = cu->per_cu;
23301 gdb_assert (baton->per_cu);
0d53c4c4 23302
4fc6c0d5 23303 if (attr->form_is_block ())
0d53c4c4
DJ
23304 {
23305 /* Note that we're just copying the block's data pointer
23306 here, not the actual data. We're still pointing into the
6502dd73
DJ
23307 info_buffer for SYM's objfile; right now we never release
23308 that buffer, but when we do clean up properly this may
23309 need to change. */
0d53c4c4
DJ
23310 baton->size = DW_BLOCK (attr)->size;
23311 baton->data = DW_BLOCK (attr)->data;
23312 }
23313 else
23314 {
23315 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 23316 sym->natural_name ());
0d53c4c4 23317 baton->size = 0;
0d53c4c4 23318 }
6e70227d 23319
f1e6e072
TT
23320 SYMBOL_ACLASS_INDEX (sym) = (is_block
23321 ? dwarf2_locexpr_block_index
23322 : dwarf2_locexpr_index);
0d53c4c4
DJ
23323 SYMBOL_LOCATION_BATON (sym) = baton;
23324 }
4c2df51b 23325}
6502dd73 23326
2e6a9f79 23327/* See read.h. */
96408a79 23328
2e6a9f79
SM
23329const comp_unit_head *
23330dwarf2_per_cu_data::get_header () const
96408a79 23331{
2e6a9f79
SM
23332 if (!m_header_read_in)
23333 {
23334 const gdb_byte *info_ptr
23335 = this->section->buffer + to_underlying (this->sect_off);
96408a79 23336
2e6a9f79 23337 memset (&m_header, 0, sizeof (m_header));
96408a79 23338
2e6a9f79
SM
23339 read_comp_unit_head (&m_header, info_ptr, this->section,
23340 rcuh_kind::COMPILE);
23341 }
96408a79 23342
2e6a9f79 23343 return &m_header;
96408a79
SA
23344}
23345
09ba997f 23346/* See read.h. */
ae0d2f24 23347
98714339 23348int
09ba997f 23349dwarf2_per_cu_data::addr_size () const
ae0d2f24 23350{
2e6a9f79 23351 return this->get_header ()->addr_size;
ae0d2f24
UW
23352}
23353
09ba997f 23354/* See read.h. */
9eae7c52
TT
23355
23356int
09ba997f 23357dwarf2_per_cu_data::offset_size () const
9eae7c52 23358{
2e6a9f79 23359 return this->get_header ()->offset_size;
96408a79
SA
23360}
23361
09ba997f 23362/* See read.h. */
96408a79
SA
23363
23364int
09ba997f 23365dwarf2_per_cu_data::ref_addr_size () const
96408a79 23366{
2e6a9f79 23367 const comp_unit_head *header = this->get_header ();
96408a79 23368
2e6a9f79
SM
23369 if (header->version == 2)
23370 return header->addr_size;
96408a79 23371 else
2e6a9f79 23372 return header->offset_size;
181cebd4
JK
23373}
23374
09ba997f 23375/* See read.h. */
9aa1f1e3 23376
09ba997f 23377struct type *
293e7e51 23378dwarf2_cu::addr_type () const
9a49df9d 23379{
293e7e51 23380 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
23381 struct type *void_type = objfile_type (objfile)->builtin_void;
23382 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 23383 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
23384
23385 if (TYPE_LENGTH (addr_type) == addr_size)
23386 return addr_type;
23387
09ba997f 23388 addr_type = addr_sized_int_type (TYPE_UNSIGNED (addr_type));
9a49df9d
AB
23389 return addr_type;
23390}
23391
22b6cd70
TT
23392/* A helper function for dwarf2_find_containing_comp_unit that returns
23393 the index of the result, and that searches a vector. It will
23394 return a result even if the offset in question does not actually
23395 occur in any CU. This is separate so that it can be unit
23396 tested. */
ae038cb0 23397
22b6cd70
TT
23398static int
23399dwarf2_find_containing_comp_unit
23400 (sect_offset sect_off,
23401 unsigned int offset_in_dwz,
23402 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 23403{
ae038cb0
DJ
23404 int low, high;
23405
ae038cb0 23406 low = 0;
22b6cd70 23407 high = all_comp_units.size () - 1;
ae038cb0
DJ
23408 while (high > low)
23409 {
36586728 23410 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 23411 int mid = low + (high - low) / 2;
9a619af0 23412
22b6cd70 23413 mid_cu = all_comp_units[mid];
36586728 23414 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 23415 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 23416 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
23417 high = mid;
23418 else
23419 low = mid + 1;
23420 }
23421 gdb_assert (low == high);
22b6cd70
TT
23422 return low;
23423}
23424
23425/* Locate the .debug_info compilation unit from CU's objfile which contains
23426 the DIE at OFFSET. Raises an error on failure. */
23427
23428static struct dwarf2_per_cu_data *
23429dwarf2_find_containing_comp_unit (sect_offset sect_off,
23430 unsigned int offset_in_dwz,
23431 struct dwarf2_per_objfile *dwarf2_per_objfile)
23432{
23433 int low
23434 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
5989a64e 23435 dwarf2_per_objfile->per_bfd->all_comp_units);
22b6cd70 23436 struct dwarf2_per_cu_data *this_cu
5989a64e 23437 = dwarf2_per_objfile->per_bfd->all_comp_units[low];
22b6cd70 23438
45b8ae0c 23439 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 23440 {
36586728 23441 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 23442 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
23443 "offset %s [in module %s]"),
23444 sect_offset_str (sect_off),
ed2dc618 23445 bfd_get_filename (dwarf2_per_objfile->objfile->obfd));
10b3939b 23446
5989a64e 23447 gdb_assert (dwarf2_per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 23448 <= sect_off);
5989a64e 23449 return dwarf2_per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
23450 }
23451 else
23452 {
5989a64e 23453 if (low == dwarf2_per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 23454 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 23455 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 23456 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
23457 return this_cu;
23458 }
23459}
23460
22b6cd70
TT
23461#if GDB_SELF_TEST
23462
23463namespace selftests {
23464namespace find_containing_comp_unit {
23465
23466static void
23467run_test ()
23468{
23469 struct dwarf2_per_cu_data one {};
23470 struct dwarf2_per_cu_data two {};
23471 struct dwarf2_per_cu_data three {};
23472 struct dwarf2_per_cu_data four {};
23473
23474 one.length = 5;
23475 two.sect_off = sect_offset (one.length);
23476 two.length = 7;
23477
23478 three.length = 5;
23479 three.is_dwz = 1;
23480 four.sect_off = sect_offset (three.length);
23481 four.length = 7;
23482 four.is_dwz = 1;
23483
23484 std::vector<dwarf2_per_cu_data *> units;
23485 units.push_back (&one);
23486 units.push_back (&two);
23487 units.push_back (&three);
23488 units.push_back (&four);
23489
23490 int result;
23491
23492 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
23493 SELF_CHECK (units[result] == &one);
23494 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
23495 SELF_CHECK (units[result] == &one);
23496 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
23497 SELF_CHECK (units[result] == &two);
23498
23499 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
23500 SELF_CHECK (units[result] == &three);
23501 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
23502 SELF_CHECK (units[result] == &three);
23503 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
23504 SELF_CHECK (units[result] == &four);
23505}
23506
23507}
23508}
23509
23510#endif /* GDB_SELF_TEST */
23511
9e021579 23512/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 23513
9e021579
SM
23514dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
23515 dwarf2_per_objfile *per_objfile)
23516 : per_cu (per_cu),
23517 per_objfile (per_objfile),
9068261f
AB
23518 mark (false),
23519 has_loclist (false),
23520 checked_producer (false),
23521 producer_is_gxx_lt_4_6 (false),
23522 producer_is_gcc_lt_4_3 (false),
eb77c9df 23523 producer_is_icc (false),
9068261f 23524 producer_is_icc_lt_14 (false),
c258c396 23525 producer_is_codewarrior (false),
9068261f 23526 processing_has_namespace_info (false)
93311388 23527{
9816fde3
JK
23528}
23529
23530/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
23531
23532static void
95554aad
TT
23533prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
23534 enum language pretend_language)
9816fde3
JK
23535{
23536 struct attribute *attr;
23537
23538 /* Set the language we're debugging. */
23539 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 23540 if (attr != nullptr)
9816fde3
JK
23541 set_cu_language (DW_UNSND (attr), cu);
23542 else
9cded63f 23543 {
95554aad 23544 cu->language = pretend_language;
9cded63f
TT
23545 cu->language_defn = language_def (cu->language);
23546 }
dee91e82 23547
7d45c7c3 23548 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
23549}
23550
7188ed02 23551/* See read.h. */
ae038cb0 23552
7188ed02
SM
23553dwarf2_cu *
23554dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23555{
7188ed02
SM
23556 auto it = m_dwarf2_cus.find (per_cu);
23557 if (it == m_dwarf2_cus.end ())
23558 return nullptr;
ae038cb0 23559
7188ed02
SM
23560 return it->second;
23561}
23562
23563/* See read.h. */
23564
23565void
23566dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
23567{
23568 gdb_assert (this->get_cu (per_cu) == nullptr);
23569
23570 m_dwarf2_cus[per_cu] = cu;
23571}
23572
23573/* See read.h. */
23574
23575void
23576dwarf2_per_objfile::age_comp_units ()
23577{
23578 /* Start by clearing all marks. */
23579 for (auto pair : m_dwarf2_cus)
23580 pair.second->mark = false;
23581
23582 /* Traverse all CUs, mark them and their dependencies if used recently
23583 enough. */
23584 for (auto pair : m_dwarf2_cus)
ae038cb0 23585 {
7188ed02
SM
23586 dwarf2_cu *cu = pair.second;
23587
23588 cu->last_used++;
23589 if (cu->last_used <= dwarf_max_cache_age)
23590 dwarf2_mark (cu);
ae038cb0
DJ
23591 }
23592
7188ed02
SM
23593 /* Delete all CUs still not marked. */
23594 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 23595 {
7188ed02 23596 dwarf2_cu *cu = it->second;
ae038cb0 23597
7188ed02 23598 if (!cu->mark)
ae038cb0 23599 {
7188ed02
SM
23600 delete cu;
23601 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
23602 }
23603 else
7188ed02 23604 it++;
ae038cb0
DJ
23605 }
23606}
23607
7188ed02 23608/* See read.h. */
ae038cb0 23609
7188ed02
SM
23610void
23611dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 23612{
7188ed02
SM
23613 auto it = m_dwarf2_cus.find (per_cu);
23614 if (it == m_dwarf2_cus.end ())
23615 return;
ae038cb0 23616
7188ed02 23617 delete it->second;
ae038cb0 23618
7188ed02
SM
23619 m_dwarf2_cus.erase (it);
23620}
ae038cb0 23621
7188ed02
SM
23622dwarf2_per_objfile::~dwarf2_per_objfile ()
23623{
23624 remove_all_cus ();
ae038cb0
DJ
23625}
23626
dee91e82
DE
23627/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
23628 We store these in a hash table separate from the DIEs, and preserve them
23629 when the DIEs are flushed out of cache.
23630
23631 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 23632 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
23633 or the type may come from a DWO file. Furthermore, while it's more logical
23634 to use per_cu->section+offset, with Fission the section with the data is in
23635 the DWO file but we don't know that section at the point we need it.
23636 We have to use something in dwarf2_per_cu_data (or the pointer to it)
23637 because we can enter the lookup routine, get_die_type_at_offset, from
23638 outside this file, and thus won't necessarily have PER_CU->cu.
23639 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 23640
dee91e82 23641struct dwarf2_per_cu_offset_and_type
1c379e20 23642{
dee91e82 23643 const struct dwarf2_per_cu_data *per_cu;
9c541725 23644 sect_offset sect_off;
1c379e20
DJ
23645 struct type *type;
23646};
23647
dee91e82 23648/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23649
23650static hashval_t
dee91e82 23651per_cu_offset_and_type_hash (const void *item)
1c379e20 23652{
9a3c8263
SM
23653 const struct dwarf2_per_cu_offset_and_type *ofs
23654 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 23655
9c541725 23656 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
23657}
23658
dee91e82 23659/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
23660
23661static int
dee91e82 23662per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 23663{
9a3c8263
SM
23664 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
23665 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
23666 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
23667 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 23668
dee91e82 23669 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 23670 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
23671}
23672
23673/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
23674 table if necessary. For convenience, return TYPE.
23675
23676 The DIEs reading must have careful ordering to:
85102364 23677 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
23678 reading current DIE.
23679 * Not trying to dereference contents of still incompletely read in types
23680 while reading in other DIEs.
23681 * Enable referencing still incompletely read in types just by a pointer to
23682 the type without accessing its fields.
23683
23684 Therefore caller should follow these rules:
23685 * Try to fetch any prerequisite types we may need to build this DIE type
23686 before building the type and calling set_die_type.
e71ec853 23687 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
23688 possible before fetching more types to complete the current type.
23689 * Make the type as complete as possible before fetching more types. */
1c379e20 23690
f792889a 23691static struct type *
1c379e20
DJ
23692set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
23693{
5e22e966 23694 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
dee91e82 23695 struct dwarf2_per_cu_offset_and_type **slot, ofs;
ed2dc618 23696 struct objfile *objfile = dwarf2_per_objfile->objfile;
3cdcd0ce
JB
23697 struct attribute *attr;
23698 struct dynamic_prop prop;
1c379e20 23699
b4ba55a1
JB
23700 /* For Ada types, make sure that the gnat-specific data is always
23701 initialized (if not already set). There are a few types where
23702 we should not be doing so, because the type-specific area is
23703 already used to hold some other piece of info (eg: TYPE_CODE_FLT
23704 where the type-specific area is used to store the floatformat).
23705 But this is not a problem, because the gnat-specific information
23706 is actually not needed for these types. */
23707 if (need_gnat_info (cu)
78134374
SM
23708 && type->code () != TYPE_CODE_FUNC
23709 && type->code () != TYPE_CODE_FLT
23710 && type->code () != TYPE_CODE_METHODPTR
23711 && type->code () != TYPE_CODE_MEMBERPTR
23712 && type->code () != TYPE_CODE_METHOD
b4ba55a1
JB
23713 && !HAVE_GNAT_AUX_INFO (type))
23714 INIT_GNAT_SPECIFIC (type);
23715
3f2f83dd
KB
23716 /* Read DW_AT_allocated and set in type. */
23717 attr = dwarf2_attr (die, DW_AT_allocated, cu);
4fc6c0d5 23718 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23719 {
293e7e51 23720 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23721 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23722 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd
KB
23723 }
23724 else if (attr != NULL)
23725 {
b98664d3 23726 complaint (_("DW_AT_allocated has the wrong form (%s) at DIE %s"),
9c541725 23727 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23728 sect_offset_str (die->sect_off));
3f2f83dd
KB
23729 }
23730
23731 /* Read DW_AT_associated and set in type. */
23732 attr = dwarf2_attr (die, DW_AT_associated, cu);
4fc6c0d5 23733 if (attr != NULL && attr->form_is_block ())
3f2f83dd 23734 {
293e7e51 23735 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 23736 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
5c54719c 23737 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd
KB
23738 }
23739 else if (attr != NULL)
23740 {
b98664d3 23741 complaint (_("DW_AT_associated has the wrong form (%s) at DIE %s"),
9c541725 23742 (attr != NULL ? dwarf_form_name (attr->form) : "n/a"),
9d8780f0 23743 sect_offset_str (die->sect_off));
3f2f83dd
KB
23744 }
23745
3cdcd0ce
JB
23746 /* Read DW_AT_data_location and set in type. */
23747 attr = dwarf2_attr (die, DW_AT_data_location, cu);
293e7e51 23748 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 23749 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
3cdcd0ce 23750
ae090bdb
SM
23751 if (dwarf2_per_objfile->die_type_hash == NULL)
23752 dwarf2_per_objfile->die_type_hash
0335378b
TT
23753 = htab_up (htab_create_alloc (127,
23754 per_cu_offset_and_type_hash,
23755 per_cu_offset_and_type_eq,
23756 NULL, xcalloc, xfree));
1c379e20 23757
dee91e82 23758 ofs.per_cu = cu->per_cu;
9c541725 23759 ofs.sect_off = die->sect_off;
1c379e20 23760 ofs.type = type;
dee91e82 23761 slot = (struct dwarf2_per_cu_offset_and_type **)
ae090bdb 23762 htab_find_slot (dwarf2_per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 23763 if (*slot)
b98664d3 23764 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 23765 sect_offset_str (die->sect_off));
8d749320
SM
23766 *slot = XOBNEW (&objfile->objfile_obstack,
23767 struct dwarf2_per_cu_offset_and_type);
1c379e20 23768 **slot = ofs;
f792889a 23769 return type;
1c379e20
DJ
23770}
23771
9c541725 23772/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 23773 or return NULL if the die does not have a saved type. */
1c379e20
DJ
23774
23775static struct type *
9c541725 23776get_die_type_at_offset (sect_offset sect_off,
aa66c379
SM
23777 dwarf2_per_cu_data *per_cu,
23778 dwarf2_per_objfile *dwarf2_per_objfile)
1c379e20 23779{
dee91e82 23780 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 23781
ae090bdb 23782 if (dwarf2_per_objfile->die_type_hash == NULL)
f792889a 23783 return NULL;
1c379e20 23784
dee91e82 23785 ofs.per_cu = per_cu;
9c541725 23786 ofs.sect_off = sect_off;
9a3c8263 23787 slot = ((struct dwarf2_per_cu_offset_and_type *)
ae090bdb 23788 htab_find (dwarf2_per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
23789 if (slot)
23790 return slot->type;
23791 else
23792 return NULL;
23793}
23794
02142a6c 23795/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
23796 or return NULL if DIE does not have a saved type. */
23797
23798static struct type *
23799get_die_type (struct die_info *die, struct dwarf2_cu *cu)
23800{
aa66c379 23801 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
23802}
23803
10b3939b
DJ
23804/* Add a dependence relationship from CU to REF_PER_CU. */
23805
23806static void
23807dwarf2_add_dependence (struct dwarf2_cu *cu,
23808 struct dwarf2_per_cu_data *ref_per_cu)
23809{
23810 void **slot;
23811
23812 if (cu->dependencies == NULL)
23813 cu->dependencies
23814 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
23815 NULL, &cu->comp_unit_obstack,
23816 hashtab_obstack_allocate,
23817 dummy_obstack_deallocate);
23818
23819 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
23820 if (*slot == NULL)
23821 *slot = ref_per_cu;
23822}
1c379e20 23823
f504f079
DE
23824/* Subroutine of dwarf2_mark to pass to htab_traverse.
23825 Set the mark field in every compilation unit in the
7188ed02
SM
23826 cache that we must keep because we are keeping CU.
23827
23828 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 23829
10b3939b
DJ
23830static int
23831dwarf2_mark_helper (void **slot, void *data)
23832{
7188ed02
SM
23833 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
23834 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
23835 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
23836
23837 /* cu->dependencies references may not yet have been ever read if QUIT aborts
23838 reading of the chain. As such dependencies remain valid it is not much
23839 useful to track and undo them during QUIT cleanups. */
7188ed02 23840 if (cu == nullptr)
d07ed419
JK
23841 return 1;
23842
7188ed02 23843 if (cu->mark)
10b3939b 23844 return 1;
10b3939b 23845
7188ed02
SM
23846 cu->mark = true;
23847
23848 if (cu->dependencies != nullptr)
23849 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
23850
23851 return 1;
23852}
23853
f504f079
DE
23854/* Set the mark field in CU and in every other compilation unit in the
23855 cache that we must keep because we are keeping CU. */
23856
ae038cb0
DJ
23857static void
23858dwarf2_mark (struct dwarf2_cu *cu)
23859{
23860 if (cu->mark)
23861 return;
7188ed02 23862
9068261f 23863 cu->mark = true;
ae038cb0 23864
7188ed02
SM
23865 if (cu->dependencies != nullptr)
23866 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
23867}
23868
72bf9492
DJ
23869/* Trivial hash function for partial_die_info: the hash value of a DIE
23870 is its offset in .debug_info for this objfile. */
23871
23872static hashval_t
23873partial_die_hash (const void *item)
23874{
9a3c8263
SM
23875 const struct partial_die_info *part_die
23876 = (const struct partial_die_info *) item;
9a619af0 23877
9c541725 23878 return to_underlying (part_die->sect_off);
72bf9492
DJ
23879}
23880
23881/* Trivial comparison function for partial_die_info structures: two DIEs
23882 are equal if they have the same offset. */
23883
23884static int
23885partial_die_eq (const void *item_lhs, const void *item_rhs)
23886{
9a3c8263
SM
23887 const struct partial_die_info *part_die_lhs
23888 = (const struct partial_die_info *) item_lhs;
23889 const struct partial_die_info *part_die_rhs
23890 = (const struct partial_die_info *) item_rhs;
9a619af0 23891
9c541725 23892 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
23893}
23894
3c3bb058
AB
23895struct cmd_list_element *set_dwarf_cmdlist;
23896struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 23897
9291a0cd 23898static void
cd4fb1b2
SM
23899show_check_physname (struct ui_file *file, int from_tty,
23900 struct cmd_list_element *c, const char *value)
9291a0cd 23901{
cd4fb1b2
SM
23902 fprintf_filtered (file,
23903 _("Whether to check \"physname\" is %s.\n"),
23904 value);
9291a0cd
TT
23905}
23906
6c265988 23907void _initialize_dwarf2_read ();
cd4fb1b2 23908void
6c265988 23909_initialize_dwarf2_read ()
9291a0cd 23910{
0743fc83 23911 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 23912Set DWARF specific variables.\n\
590042fc 23913Configure DWARF variables such as the cache size."),
0743fc83
TT
23914 &set_dwarf_cmdlist, "maintenance set dwarf ",
23915 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 23916
0743fc83 23917 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
23918Show DWARF specific variables.\n\
23919Show DWARF variables such as the cache size."),
0743fc83
TT
23920 &show_dwarf_cmdlist, "maintenance show dwarf ",
23921 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 23922
cd4fb1b2
SM
23923 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
23924 &dwarf_max_cache_age, _("\
23925Set the upper bound on the age of cached DWARF compilation units."), _("\
23926Show the upper bound on the age of cached DWARF compilation units."), _("\
23927A higher limit means that cached compilation units will be stored\n\
23928in memory longer, and more total memory will be used. Zero disables\n\
23929caching, which can slow down startup."),
23930 NULL,
23931 show_dwarf_max_cache_age,
23932 &set_dwarf_cmdlist,
23933 &show_dwarf_cmdlist);
156942c7 23934
cd4fb1b2
SM
23935 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
23936Set debugging of the DWARF reader."), _("\
23937Show debugging of the DWARF reader."), _("\
23938When enabled (non-zero), debugging messages are printed during DWARF\n\
23939reading and symtab expansion. A value of 1 (one) provides basic\n\
23940information. A value greater than 1 provides more verbose information."),
23941 NULL,
23942 NULL,
23943 &setdebuglist, &showdebuglist);
9291a0cd 23944
cd4fb1b2
SM
23945 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
23946Set debugging of the DWARF DIE reader."), _("\
23947Show debugging of the DWARF DIE reader."), _("\
23948When enabled (non-zero), DIEs are dumped after they are read in.\n\
23949The value is the maximum depth to print."),
23950 NULL,
23951 NULL,
23952 &setdebuglist, &showdebuglist);
9291a0cd 23953
cd4fb1b2
SM
23954 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
23955Set debugging of the dwarf line reader."), _("\
23956Show debugging of the dwarf line reader."), _("\
23957When enabled (non-zero), line number entries are dumped as they are read in.\n\
23958A value of 1 (one) provides basic information.\n\
23959A value greater than 1 provides more verbose information."),
23960 NULL,
23961 NULL,
23962 &setdebuglist, &showdebuglist);
437afbb8 23963
cd4fb1b2
SM
23964 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
23965Set cross-checking of \"physname\" code against demangler."), _("\
23966Show cross-checking of \"physname\" code against demangler."), _("\
23967When enabled, GDB's internal \"physname\" code is checked against\n\
23968the demangler."),
23969 NULL, show_check_physname,
23970 &setdebuglist, &showdebuglist);
900e11f9 23971
e615022a
DE
23972 add_setshow_boolean_cmd ("use-deprecated-index-sections",
23973 no_class, &use_deprecated_index_sections, _("\
23974Set whether to use deprecated gdb_index sections."), _("\
23975Show whether to use deprecated gdb_index sections."), _("\
23976When enabled, deprecated .gdb_index sections are used anyway.\n\
23977Normally they are ignored either because of a missing feature or\n\
23978performance issue.\n\
23979Warning: This option must be enabled before gdb reads the file."),
23980 NULL,
23981 NULL,
23982 &setlist, &showlist);
23983
f1e6e072
TT
23984 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
23985 &dwarf2_locexpr_funcs);
23986 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
23987 &dwarf2_loclist_funcs);
23988
23989 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
23990 &dwarf2_block_frame_base_locexpr_funcs);
23991 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
23992 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
23993
23994#if GDB_SELF_TEST
23995 selftests::register_test ("dw2_expand_symtabs_matching",
23996 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
23997 selftests::register_test ("dwarf2_find_containing_comp_unit",
23998 selftests::find_containing_comp_unit::run_test);
c62446b1 23999#endif
6502dd73 24000}
This page took 8.872985 seconds and 4 git commands to generate.