Search for DWZ files in debug-file-directories as well
[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
6f738b01
SM
96/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 1. */
97
98#define dwarf_read_debug_printf(fmt, ...) \
99 do \
100 { \
101 if (dwarf_read_debug >= 1) \
102 debug_prefixed_printf ("dwarf-read", __func__, fmt, ##__VA_ARGS__); \
103 } \
104 while (0)
105
106/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
107
108#define dwarf_read_debug_printf_v(fmt, ...) \
109 do \
110 { \
111 if (dwarf_read_debug >= 2) \
112 debug_prefixed_printf ("dwarf-read", __func__, fmt, ##__VA_ARGS__); \
113 } \
114 while (0)
115
d97bc12b 116/* When non-zero, dump DIEs after they are read in. */
b4f54984 117static unsigned int dwarf_die_debug = 0;
d97bc12b 118
27e0867f 119/* When non-zero, dump line number entries as they are read in. */
8fdd972c 120unsigned int dwarf_line_debug = 0;
27e0867f 121
491144b5
CB
122/* When true, cross-check physname against demangler. */
123static bool check_physname = false;
900e11f9 124
491144b5
CB
125/* When true, do not reject deprecated .gdb_index sections. */
126static bool use_deprecated_index_sections = false;
481860b3 127
17ee85fc
TT
128/* This is used to store the data that is always per objfile. */
129static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
130
131/* These are used to store the dwarf2_per_bfd objects.
132
133 objfiles having the same BFD, which doesn't require relocations, are going to
134 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
135
136 Other objfiles are not going to share a dwarf2_per_bfd with any other
137 objfiles, so they'll have their own version kept in the _objfile_data_key
138 version. */
139static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
140static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 141
f1e6e072
TT
142/* The "aclass" indices for various kinds of computed DWARF symbols. */
143
144static int dwarf2_locexpr_index;
145static int dwarf2_loclist_index;
146static int dwarf2_locexpr_block_index;
147static int dwarf2_loclist_block_index;
148
41144253 149/* Size of .debug_loclists section header for 32-bit DWARF format. */
150#define LOCLIST_HEADER_SIZE32 12
151
152/* Size of .debug_loclists section header for 64-bit DWARF format. */
153#define LOCLIST_HEADER_SIZE64 20
154
d0ce17d8
CT
155/* Size of .debug_rnglists section header for 32-bit DWARF format. */
156#define RNGLIST_HEADER_SIZE32 12
157
158/* Size of .debug_rnglists section header for 64-bit DWARF format. */
159#define RNGLIST_HEADER_SIZE64 20
160
3f563c84
PA
161/* An index into a (C++) symbol name component in a symbol name as
162 recorded in the mapped_index's symbol table. For each C++ symbol
163 in the symbol table, we record one entry for the start of each
164 component in the symbol in a table of name components, and then
165 sort the table, in order to be able to binary search symbol names,
166 ignoring leading namespaces, both completion and regular look up.
167 For example, for symbol "A::B::C", we'll have an entry that points
168 to "A::B::C", another that points to "B::C", and another for "C".
169 Note that function symbols in GDB index have no parameter
170 information, just the function/method names. You can convert a
171 name_component to a "const char *" using the
172 'mapped_index::symbol_name_at(offset_type)' method. */
173
174struct name_component
175{
176 /* Offset in the symbol name where the component starts. Stored as
177 a (32-bit) offset instead of a pointer to save memory and improve
178 locality on 64-bit architectures. */
179 offset_type name_offset;
180
181 /* The symbol's index in the symbol and constant pool tables of a
182 mapped_index. */
183 offset_type idx;
184};
185
44ed8f3e
PA
186/* Base class containing bits shared by both .gdb_index and
187 .debug_name indexes. */
188
189struct mapped_index_base
190{
22ca247e
TT
191 mapped_index_base () = default;
192 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
193
44ed8f3e
PA
194 /* The name_component table (a sorted vector). See name_component's
195 description above. */
196 std::vector<name_component> name_components;
197
198 /* How NAME_COMPONENTS is sorted. */
199 enum case_sensitivity name_components_casing;
200
201 /* Return the number of names in the symbol table. */
202 virtual size_t symbol_name_count () const = 0;
203
204 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
205 virtual const char *symbol_name_at
206 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
207
208 /* Return whether the name at IDX in the symbol table should be
209 ignored. */
210 virtual bool symbol_name_slot_invalid (offset_type idx) const
211 {
212 return false;
213 }
214
215 /* Build the symbol name component sorted vector, if we haven't
216 yet. */
fcf23d5b 217 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
218
219 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
220 possible matches for LN_NO_PARAMS in the name component
221 vector. */
222 std::pair<std::vector<name_component>::const_iterator,
223 std::vector<name_component>::const_iterator>
3b00ef10 224 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
225 enum language lang,
226 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
227
228 /* Prevent deleting/destroying via a base class pointer. */
229protected:
230 ~mapped_index_base() = default;
231};
232
9291a0cd
TT
233/* A description of the mapped index. The file format is described in
234 a comment by the code that writes the index. */
fc898b42 235struct mapped_index final : public mapped_index_base
9291a0cd 236{
f00a2de2
PA
237 /* A slot/bucket in the symbol table hash. */
238 struct symbol_table_slot
239 {
240 const offset_type name;
241 const offset_type vec;
242 };
243
559a7a62 244 /* Index data format version. */
3063847f 245 int version = 0;
559a7a62 246
f00a2de2
PA
247 /* The address table data. */
248 gdb::array_view<const gdb_byte> address_table;
b11b1f88 249
3876f04e 250 /* The symbol table, implemented as a hash table. */
f00a2de2 251 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 252
9291a0cd 253 /* A pointer to the constant pool. */
3063847f 254 const char *constant_pool = nullptr;
3f563c84 255
44ed8f3e
PA
256 bool symbol_name_slot_invalid (offset_type idx) const override
257 {
258 const auto &bucket = this->symbol_table[idx];
9ab08412 259 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 260 }
5c58de74 261
3f563c84
PA
262 /* Convenience method to get at the name of the symbol at IDX in the
263 symbol table. */
fcf23d5b
SM
264 const char *symbol_name_at
265 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 266 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 267
44ed8f3e
PA
268 size_t symbol_name_count () const override
269 { return this->symbol_table.size (); }
9291a0cd
TT
270};
271
927aa2e7
JK
272/* A description of the mapped .debug_names.
273 Uninitialized map has CU_COUNT 0. */
fc898b42 274struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
275{
276 bfd_endian dwarf5_byte_order;
277 bool dwarf5_is_dwarf64;
278 bool augmentation_is_gdb;
279 uint8_t offset_size;
280 uint32_t cu_count = 0;
281 uint32_t tu_count, bucket_count, name_count;
282 const gdb_byte *cu_table_reordered, *tu_table_reordered;
283 const uint32_t *bucket_table_reordered, *hash_table_reordered;
284 const gdb_byte *name_table_string_offs_reordered;
285 const gdb_byte *name_table_entry_offs_reordered;
286 const gdb_byte *entry_pool;
287
288 struct index_val
289 {
290 ULONGEST dwarf_tag;
291 struct attr
292 {
293 /* Attribute name DW_IDX_*. */
294 ULONGEST dw_idx;
295
296 /* Attribute form DW_FORM_*. */
297 ULONGEST form;
298
299 /* Value if FORM is DW_FORM_implicit_const. */
300 LONGEST implicit_const;
301 };
302 std::vector<attr> attr_vec;
303 };
304
305 std::unordered_map<ULONGEST, index_val> abbrev_map;
306
fcf23d5b
SM
307 const char *namei_to_name
308 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
309
310 /* Implementation of the mapped_index_base virtual interface, for
311 the name_components cache. */
312
fcf23d5b
SM
313 const char *symbol_name_at
314 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
315 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
316
317 size_t symbol_name_count () const override
318 { return this->name_count; }
927aa2e7
JK
319};
320
cd4fb1b2 321/* See dwarf2read.h. */
ed2dc618 322
cd4fb1b2 323dwarf2_per_objfile *
ed2dc618
SM
324get_dwarf2_per_objfile (struct objfile *objfile)
325{
5bfd760d 326 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 327}
c906108c 328
251d32d9 329/* Default names of the debugging sections. */
c906108c 330
233a11ab
CS
331/* Note that if the debugging section has been compressed, it might
332 have a name like .zdebug_info. */
333
9cdd5dbd
DE
334static const struct dwarf2_debug_sections dwarf2_elf_names =
335{
251d32d9
TG
336 { ".debug_info", ".zdebug_info" },
337 { ".debug_abbrev", ".zdebug_abbrev" },
338 { ".debug_line", ".zdebug_line" },
339 { ".debug_loc", ".zdebug_loc" },
43988095 340 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 341 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 342 { ".debug_macro", ".zdebug_macro" },
251d32d9 343 { ".debug_str", ".zdebug_str" },
18a8505e 344 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 345 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 346 { ".debug_ranges", ".zdebug_ranges" },
43988095 347 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 348 { ".debug_types", ".zdebug_types" },
3019eac3 349 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
350 { ".debug_frame", ".zdebug_frame" },
351 { ".eh_frame", NULL },
24d3216f 352 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
353 { ".debug_names", ".zdebug_names" },
354 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 355 23
251d32d9 356};
c906108c 357
80626a55 358/* List of DWO/DWP sections. */
3019eac3 359
80626a55 360static const struct dwop_section_names
3019eac3
DE
361{
362 struct dwarf2_section_names abbrev_dwo;
363 struct dwarf2_section_names info_dwo;
364 struct dwarf2_section_names line_dwo;
365 struct dwarf2_section_names loc_dwo;
43988095 366 struct dwarf2_section_names loclists_dwo;
09262596
DE
367 struct dwarf2_section_names macinfo_dwo;
368 struct dwarf2_section_names macro_dwo;
d0ce17d8 369 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
370 struct dwarf2_section_names str_dwo;
371 struct dwarf2_section_names str_offsets_dwo;
372 struct dwarf2_section_names types_dwo;
80626a55
DE
373 struct dwarf2_section_names cu_index;
374 struct dwarf2_section_names tu_index;
3019eac3 375}
80626a55 376dwop_section_names =
3019eac3
DE
377{
378 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
379 { ".debug_info.dwo", ".zdebug_info.dwo" },
380 { ".debug_line.dwo", ".zdebug_line.dwo" },
381 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 382 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
383 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
384 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 385 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
386 { ".debug_str.dwo", ".zdebug_str.dwo" },
387 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
388 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
389 { ".debug_cu_index", ".zdebug_cu_index" },
390 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
391};
392
c906108c
SS
393/* local data types */
394
d0ce17d8
CT
395/* The location list and range list sections (.debug_loclists & .debug_rnglists)
396 begin with a header, which contains the following information. */
397struct loclists_rnglists_header
41144253 398{
399 /* A 4-byte or 12-byte length containing the length of the
400 set of entries for this compilation unit, not including the
401 length field itself. */
402 unsigned int length;
403
404 /* A 2-byte version identifier. */
405 short version;
406
407 /* A 1-byte unsigned integer containing the size in bytes of an address on
408 the target system. */
409 unsigned char addr_size;
410
411 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
412 on the target system. */
413 unsigned char segment_collector_size;
414
415 /* A 4-byte count of the number of offsets that follow the header. */
416 unsigned int offset_entry_count;
417};
418
3da10d80
KS
419/* Type used for delaying computation of method physnames.
420 See comments for compute_delayed_physnames. */
421struct delayed_method_info
422{
423 /* The type to which the method is attached, i.e., its parent class. */
424 struct type *type;
425
426 /* The index of the method in the type's function fieldlists. */
427 int fnfield_index;
428
429 /* The index of the method in the fieldlist. */
430 int index;
431
432 /* The name of the DIE. */
433 const char *name;
434
435 /* The DIE associated with this method. */
436 struct die_info *die;
437};
438
e7c27a73
DJ
439/* Internal state when decoding a particular compilation unit. */
440struct dwarf2_cu
441{
9e021579
SM
442 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
443 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
444
445 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
446
c24bdb02
KS
447 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
448 Create the set of symtabs used by this TU, or if this TU is sharing
449 symtabs with another TU and the symtabs have already been created
450 then restore those symtabs in the line header.
451 We don't need the pc/line-number mapping for type units. */
452 void setup_type_unit_groups (struct die_info *die);
453
454 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
455 buildsym_compunit constructor. */
456 struct compunit_symtab *start_symtab (const char *name,
457 const char *comp_dir,
458 CORE_ADDR low_pc);
459
460 /* Reset the builder. */
461 void reset_builder () { m_builder.reset (); }
462
293e7e51
SM
463 /* Return a type that is a generic pointer type, the size of which
464 matches the address size given in the compilation unit header for
465 this CU. */
466 struct type *addr_type () const;
467
468 /* Find an integer type the same size as the address size given in
469 the compilation unit header for this CU. UNSIGNED_P controls if
470 the integer is unsigned or not. */
471 struct type *addr_sized_int_type (bool unsigned_p) const;
472
d00adf39 473 /* The header of the compilation unit. */
fcd3b13d 474 struct comp_unit_head header {};
e142c38c 475
d00adf39 476 /* Base address of this compilation unit. */
2b24b6e4 477 gdb::optional<CORE_ADDR> base_address;
d00adf39 478
e142c38c 479 /* The language we are debugging. */
fcd3b13d
SM
480 enum language language = language_unknown;
481 const struct language_defn *language_defn = nullptr;
e142c38c 482
fcd3b13d 483 const char *producer = nullptr;
b0f35d58 484
c24bdb02 485private:
804d2729
TT
486 /* The symtab builder for this CU. This is only non-NULL when full
487 symbols are being read. */
c24bdb02 488 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 489
c24bdb02 490public:
e142c38c
DJ
491 /* The generic symbol table building routines have separate lists for
492 file scope symbols and all all other scopes (local scopes). So
493 we need to select the right one to pass to add_symbol_to_list().
494 We do it by keeping a pointer to the correct list in list_in_scope.
495
496 FIXME: The original dwarf code just treated the file scope as the
497 first local scope, and all other local scopes as nested local
498 scopes, and worked fine. Check to see if we really need to
499 distinguish these in buildsym.c. */
fcd3b13d 500 struct pending **list_in_scope = nullptr;
e142c38c 501
b64f50a1
JK
502 /* Hash table holding all the loaded partial DIEs
503 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 504 htab_t partial_dies = nullptr;
72bf9492
DJ
505
506 /* Storage for things with the same lifetime as this read-in compilation
507 unit, including partial DIEs. */
fcd3b13d 508 auto_obstack comp_unit_obstack;
72bf9492 509
69d751e3 510 /* Backlink to our per_cu entry. */
ae038cb0
DJ
511 struct dwarf2_per_cu_data *per_cu;
512
9e021579 513 /* The dwarf2_per_objfile that owns this. */
976ca316 514 dwarf2_per_objfile *per_objfile;
9e021579 515
ae038cb0 516 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 517 int last_used = 0;
ae038cb0 518
b64f50a1
JK
519 /* A hash table of DIE cu_offset for following references with
520 die_info->offset.sect_off as hash. */
fcd3b13d 521 htab_t die_hash = nullptr;
10b3939b
DJ
522
523 /* Full DIEs if read in. */
fcd3b13d 524 struct die_info *dies = nullptr;
10b3939b
DJ
525
526 /* A set of pointers to dwarf2_per_cu_data objects for compilation
527 units referenced by this one. Only set during full symbol processing;
528 partial symbol tables do not have dependencies. */
fcd3b13d 529 htab_t dependencies = nullptr;
10b3939b 530
cb1df416 531 /* Header data from the line table, during full symbol processing. */
fcd3b13d 532 struct line_header *line_header = nullptr;
4c8aa72d 533 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 534 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
535 this is the DW_TAG_compile_unit die for this CU. We'll hold on
536 to the line header as long as this DIE is being processed. See
537 process_die_scope. */
fcd3b13d 538 die_info *line_header_die_owner = nullptr;
cb1df416 539
3da10d80
KS
540 /* A list of methods which need to have physnames computed
541 after all type information has been read. */
c89b44cd 542 std::vector<delayed_method_info> method_list;
3da10d80 543
96408a79 544 /* To be copied to symtab->call_site_htab. */
fcd3b13d 545 htab_t call_site_htab = nullptr;
96408a79 546
034e5797
DE
547 /* Non-NULL if this CU came from a DWO file.
548 There is an invariant here that is important to remember:
549 Except for attributes copied from the top level DIE in the "main"
550 (or "stub") file in preparation for reading the DWO file
18a8505e 551 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
552 Either there isn't a DWO file (in which case this is NULL and the point
553 is moot), or there is and either we're not going to read it (in which
554 case this is NULL) or there is and we are reading it (in which case this
555 is non-NULL). */
fcd3b13d 556 struct dwo_unit *dwo_unit = nullptr;
3019eac3 557
18a8505e 558 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 559 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 560 gdb::optional<ULONGEST> addr_base;
3019eac3 561
18a8505e 562 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 563 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 564 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
565 be used without needing to know whether DWO files are in use or not.
566 N.B. This does not apply to DW_AT_ranges appearing in
567 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
568 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 569 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 570 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 571 ULONGEST ranges_base = 0;
2e3cf129 572
41144253 573 /* The DW_AT_loclists_base attribute if present. */
574 ULONGEST loclist_base = 0;
575
c9317f21
TT
576 /* When reading debug info generated by older versions of rustc, we
577 have to rewrite some union types to be struct types with a
578 variant part. This rewriting must be done after the CU is fully
579 read in, because otherwise at the point of rewriting some struct
580 type might not have been fully processed. So, we keep a list of
581 all such types here and process them after expansion. */
582 std::vector<struct type *> rust_unions;
583
18a8505e
AT
584 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
585 files, the value is implicitly zero. For DWARF 5 version DWO files, the
586 value is often implicit and is the size of the header of
587 .debug_str_offsets section (8 or 4, depending on the address size). */
588 gdb::optional<ULONGEST> str_offsets_base;
589
ae038cb0 590 /* Mark used when releasing cached dies. */
9068261f 591 bool mark : 1;
ae038cb0 592
8be455d7
JK
593 /* This CU references .debug_loc. See the symtab->locations_valid field.
594 This test is imperfect as there may exist optimized debug code not using
595 any location list and still facing inlining issues if handled as
596 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 597 bool has_loclist : 1;
ba919b58 598
9068261f 599 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
600 if all the producer_is_* fields are valid. This information is cached
601 because profiling CU expansion showed excessive time spent in
602 producer_is_gxx_lt_4_6. */
9068261f
AB
603 bool checked_producer : 1;
604 bool producer_is_gxx_lt_4_6 : 1;
605 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 606 bool producer_is_icc : 1;
9068261f 607 bool producer_is_icc_lt_14 : 1;
c258c396 608 bool producer_is_codewarrior : 1;
4d4ec4e5 609
9068261f 610 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
611 debugging info for C++ namespaces. GCC 3.3.x did not produce
612 this information, but later versions do. */
613
9068261f 614 bool processing_has_namespace_info : 1;
d590ff25
YQ
615
616 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
617
618 /* If this CU was inherited by another CU (via specification,
619 abstract_origin, etc), this is the ancestor CU. */
620 dwarf2_cu *ancestor;
621
622 /* Get the buildsym_compunit for this CU. */
623 buildsym_compunit *get_builder ()
624 {
625 /* If this CU has a builder associated with it, use that. */
626 if (m_builder != nullptr)
627 return m_builder.get ();
628
629 /* Otherwise, search ancestors for a valid builder. */
630 if (ancestor != nullptr)
631 return ancestor->get_builder ();
632
633 return nullptr;
634 }
e7c27a73
DJ
635};
636
094b34ac
DE
637/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
638 This includes type_unit_group and quick_file_names. */
639
640struct stmt_list_hash
641{
642 /* The DWO unit this table is from or NULL if there is none. */
643 struct dwo_unit *dwo_unit;
644
645 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 646 sect_offset line_sect_off;
094b34ac
DE
647};
648
5989a64e 649/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
650 an object of this type. This contains elements of type unit groups
651 that can be shared across objfiles. The non-shareable parts are in
652 type_unit_group_unshareable. */
f4dc4d17
DE
653
654struct type_unit_group
655{
0186c6a7 656 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
657 To simplify things we create an artificial CU that "includes" all the
658 type units using this stmt_list so that the rest of the code still has
197400e8 659 a "per_cu" handle on the symtab. */
094b34ac
DE
660 struct dwarf2_per_cu_data per_cu;
661
0186c6a7
DE
662 /* The TUs that share this DW_AT_stmt_list entry.
663 This is added to while parsing type units to build partial symtabs,
664 and is deleted afterwards and not used again. */
a8b3b8e9 665 std::vector<signatured_type *> *tus;
f4dc4d17 666
094b34ac
DE
667 /* The data used to construct the hash key. */
668 struct stmt_list_hash hash;
f4dc4d17
DE
669};
670
73869dc2 671/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
672
673struct dwo_sections
674{
675 struct dwarf2_section_info abbrev;
3019eac3
DE
676 struct dwarf2_section_info line;
677 struct dwarf2_section_info loc;
43988095 678 struct dwarf2_section_info loclists;
09262596
DE
679 struct dwarf2_section_info macinfo;
680 struct dwarf2_section_info macro;
d0ce17d8 681 struct dwarf2_section_info rnglists;
3019eac3
DE
682 struct dwarf2_section_info str;
683 struct dwarf2_section_info str_offsets;
80626a55
DE
684 /* In the case of a virtual DWO file, these two are unused. */
685 struct dwarf2_section_info info;
fd5866f6 686 std::vector<dwarf2_section_info> types;
3019eac3
DE
687};
688
c88ee1f0 689/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
690
691struct dwo_unit
692{
693 /* Backlink to the containing struct dwo_file. */
694 struct dwo_file *dwo_file;
695
696 /* The "id" that distinguishes this CU/TU.
697 .debug_info calls this "dwo_id", .debug_types calls this "signature".
698 Since signatures came first, we stick with it for consistency. */
699 ULONGEST signature;
700
701 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 702 struct dwarf2_section_info *section;
3019eac3 703
9c541725
PA
704 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
705 sect_offset sect_off;
3019eac3
DE
706 unsigned int length;
707
708 /* For types, offset in the type's DIE of the type defined by this TU. */
709 cu_offset type_offset_in_tu;
710};
711
73869dc2
DE
712/* include/dwarf2.h defines the DWP section codes.
713 It defines a max value but it doesn't define a min value, which we
714 use for error checking, so provide one. */
715
716enum dwp_v2_section_ids
717{
718 DW_SECT_MIN = 1
719};
720
80626a55 721/* Data for one DWO file.
57d63ce2
DE
722
723 This includes virtual DWO files (a virtual DWO file is a DWO file as it
724 appears in a DWP file). DWP files don't really have DWO files per se -
725 comdat folding of types "loses" the DWO file they came from, and from
726 a high level view DWP files appear to contain a mass of random types.
727 However, to maintain consistency with the non-DWP case we pretend DWP
728 files contain virtual DWO files, and we assign each TU with one virtual
729 DWO file (generally based on the line and abbrev section offsets -
730 a heuristic that seems to work in practice). */
3019eac3
DE
731
732struct dwo_file
733{
51ac9db5
SM
734 dwo_file () = default;
735 DISABLE_COPY_AND_ASSIGN (dwo_file);
736
18a8505e 737 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
738 For virtual DWO files the name is constructed from the section offsets
739 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
740 from related CU+TUs. */
51ac9db5 741 const char *dwo_name = nullptr;
0ac5b59e
DE
742
743 /* The DW_AT_comp_dir attribute. */
51ac9db5 744 const char *comp_dir = nullptr;
3019eac3 745
80626a55
DE
746 /* The bfd, when the file is open. Otherwise this is NULL.
747 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 748 gdb_bfd_ref_ptr dbfd;
3019eac3 749
73869dc2 750 /* The sections that make up this DWO file.
d2854d8d 751 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 752 sections (for lack of a better name). */
51ac9db5 753 struct dwo_sections sections {};
3019eac3 754
33c5cd75
DB
755 /* The CUs in the file.
756 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
757 an extension to handle LLVM's Link Time Optimization output (where
758 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 759 htab_up cus;
3019eac3
DE
760
761 /* Table of TUs in the file.
762 Each element is a struct dwo_unit. */
b0b6a987 763 htab_up tus;
3019eac3
DE
764};
765
80626a55
DE
766/* These sections are what may appear in a DWP file. */
767
768struct dwp_sections
769{
d2854d8d 770 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
771 struct dwarf2_section_info str;
772 struct dwarf2_section_info cu_index;
773 struct dwarf2_section_info tu_index;
73869dc2 774
d2854d8d 775 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
776 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
777 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
778 In DWP version 2 or 5 there is at most one copy of all these sections,
779 each section being (effectively) comprised of the concatenation of all of
780 the individual sections that exist in the version 1 format.
73869dc2
DE
781 To keep the code simple we treat each of these concatenated pieces as a
782 section itself (a virtual section?). */
783 struct dwarf2_section_info abbrev;
784 struct dwarf2_section_info info;
785 struct dwarf2_section_info line;
786 struct dwarf2_section_info loc;
d2854d8d 787 struct dwarf2_section_info loclists;
73869dc2
DE
788 struct dwarf2_section_info macinfo;
789 struct dwarf2_section_info macro;
d2854d8d 790 struct dwarf2_section_info rnglists;
73869dc2
DE
791 struct dwarf2_section_info str_offsets;
792 struct dwarf2_section_info types;
80626a55
DE
793};
794
73869dc2
DE
795/* These sections are what may appear in a virtual DWO file in DWP version 1.
796 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 797
73869dc2 798struct virtual_v1_dwo_sections
80626a55
DE
799{
800 struct dwarf2_section_info abbrev;
801 struct dwarf2_section_info line;
802 struct dwarf2_section_info loc;
803 struct dwarf2_section_info macinfo;
804 struct dwarf2_section_info macro;
805 struct dwarf2_section_info str_offsets;
806 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 807 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
808 struct dwarf2_section_info info_or_types;
809};
810
d2854d8d 811/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
812 In version 2, the sections of the DWO files are concatenated together
813 and stored in one section of that name. Thus each ELF section contains
814 several "virtual" sections. */
815
d2854d8d 816struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
817{
818 bfd_size_type abbrev_offset;
819 bfd_size_type abbrev_size;
820
821 bfd_size_type line_offset;
822 bfd_size_type line_size;
823
824 bfd_size_type loc_offset;
825 bfd_size_type loc_size;
826
d2854d8d
CT
827 bfd_size_type loclists_offset;
828 bfd_size_type loclists_size;
829
73869dc2
DE
830 bfd_size_type macinfo_offset;
831 bfd_size_type macinfo_size;
832
833 bfd_size_type macro_offset;
834 bfd_size_type macro_size;
835
d2854d8d
CT
836 bfd_size_type rnglists_offset;
837 bfd_size_type rnglists_size;
838
73869dc2
DE
839 bfd_size_type str_offsets_offset;
840 bfd_size_type str_offsets_size;
841
842 /* Each DWP hash table entry records one CU or one TU.
843 That is recorded here, and copied to dwo_unit.section. */
844 bfd_size_type info_or_types_offset;
845 bfd_size_type info_or_types_size;
846};
847
80626a55
DE
848/* Contents of DWP hash tables. */
849
850struct dwp_hash_table
851{
73869dc2 852 uint32_t version, nr_columns;
80626a55 853 uint32_t nr_units, nr_slots;
73869dc2
DE
854 const gdb_byte *hash_table, *unit_table;
855 union
856 {
857 struct
858 {
859 const gdb_byte *indices;
860 } v1;
861 struct
862 {
863 /* This is indexed by column number and gives the id of the section
864 in that column. */
865#define MAX_NR_V2_DWO_SECTIONS \
866 (1 /* .debug_info or .debug_types */ \
867 + 1 /* .debug_abbrev */ \
868 + 1 /* .debug_line */ \
869 + 1 /* .debug_loc */ \
870 + 1 /* .debug_str_offsets */ \
871 + 1 /* .debug_macro or .debug_macinfo */)
872 int section_ids[MAX_NR_V2_DWO_SECTIONS];
873 const gdb_byte *offsets;
874 const gdb_byte *sizes;
875 } v2;
d2854d8d
CT
876 struct
877 {
878 /* This is indexed by column number and gives the id of the section
879 in that column. */
880#define MAX_NR_V5_DWO_SECTIONS \
881 (1 /* .debug_info */ \
882 + 1 /* .debug_abbrev */ \
883 + 1 /* .debug_line */ \
884 + 1 /* .debug_loclists */ \
885 + 1 /* .debug_str_offsets */ \
886 + 1 /* .debug_macro */ \
887 + 1 /* .debug_rnglists */)
888 int section_ids[MAX_NR_V5_DWO_SECTIONS];
889 const gdb_byte *offsets;
890 const gdb_byte *sizes;
891 } v5;
73869dc2 892 } section_pool;
80626a55
DE
893};
894
895/* Data for one DWP file. */
896
897struct dwp_file
898{
400174b1
TT
899 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
900 : name (name_),
901 dbfd (std::move (abfd))
902 {
903 }
904
80626a55
DE
905 /* Name of the file. */
906 const char *name;
907
73869dc2 908 /* File format version. */
400174b1 909 int version = 0;
73869dc2 910
93417882 911 /* The bfd. */
400174b1 912 gdb_bfd_ref_ptr dbfd;
80626a55
DE
913
914 /* Section info for this file. */
400174b1 915 struct dwp_sections sections {};
80626a55 916
57d63ce2 917 /* Table of CUs in the file. */
400174b1 918 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
919
920 /* Table of TUs in the file. */
400174b1 921 const struct dwp_hash_table *tus = nullptr;
80626a55 922
19ac8c2e 923 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
924 htab_up loaded_cus;
925 htab_up loaded_tus;
80626a55 926
73869dc2
DE
927 /* Table to map ELF section numbers to their sections.
928 This is only needed for the DWP V1 file format. */
400174b1
TT
929 unsigned int num_sections = 0;
930 asection **elf_sections = nullptr;
80626a55
DE
931};
932
0963b4bd
MS
933/* Struct used to pass misc. parameters to read_die_and_children, et
934 al. which are used for both .debug_info and .debug_types dies.
935 All parameters here are unchanging for the life of the call. This
dee91e82 936 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
937
938struct die_reader_specs
939{
a32a8923 940 /* The bfd of die_section. */
93311388
DE
941 bfd* abfd;
942
943 /* The CU of the DIE we are parsing. */
944 struct dwarf2_cu *cu;
945
80626a55 946 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
947 struct dwo_file *dwo_file;
948
dee91e82 949 /* The section the die comes from.
3019eac3 950 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
951 struct dwarf2_section_info *die_section;
952
953 /* die_section->buffer. */
d521ce57 954 const gdb_byte *buffer;
f664829e
DE
955
956 /* The end of the buffer. */
957 const gdb_byte *buffer_end;
a2ce51a0 958
685af9cd
TT
959 /* The abbreviation table to use when reading the DIEs. */
960 struct abbrev_table *abbrev_table;
93311388
DE
961};
962
c0ab21c2
TT
963/* A subclass of die_reader_specs that holds storage and has complex
964 constructor and destructor behavior. */
965
966class cutu_reader : public die_reader_specs
967{
968public:
969
ab432490
SM
970 cutu_reader (dwarf2_per_cu_data *this_cu,
971 dwarf2_per_objfile *per_objfile,
c0ab21c2 972 struct abbrev_table *abbrev_table,
2e671100 973 dwarf2_cu *existing_cu,
c0ab21c2
TT
974 bool skip_partial);
975
976 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 977 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
978 struct dwarf2_cu *parent_cu = nullptr,
979 struct dwo_file *dwo_file = nullptr);
980
c0ab21c2
TT
981 DISABLE_COPY_AND_ASSIGN (cutu_reader);
982
983 const gdb_byte *info_ptr = nullptr;
984 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
985 bool dummy_p = false;
986
6751ebae
TT
987 /* Release the new CU, putting it on the chain. This cannot be done
988 for dummy CUs. */
989 void keep ();
990
c0ab21c2 991private:
9e021579
SM
992 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
993 dwarf2_per_objfile *per_objfile,
2e671100 994 dwarf2_cu *existing_cu);
c0ab21c2
TT
995
996 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
997 std::unique_ptr<dwarf2_cu> m_new_cu;
998
999 /* The ordinary abbreviation table. */
1000 abbrev_table_up m_abbrev_table_holder;
1001
1002 /* The DWO abbreviation table. */
1003 abbrev_table_up m_dwo_abbrev_table;
1004};
dee91e82 1005
c906108c 1006/* When we construct a partial symbol table entry we only
0963b4bd 1007 need this much information. */
6f06d47b 1008struct partial_die_info : public allocate_on_obstack
c906108c 1009 {
6f06d47b
YQ
1010 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1011
1012 /* Disable assign but still keep copy ctor, which is needed
1013 load_partial_dies. */
1014 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1015
52356b79
YQ
1016 /* Adjust the partial die before generating a symbol for it. This
1017 function may set the is_external flag or change the DIE's
1018 name. */
1019 void fixup (struct dwarf2_cu *cu);
1020
48fbe735
YQ
1021 /* Read a minimal amount of information into the minimal die
1022 structure. */
1023 const gdb_byte *read (const struct die_reader_specs *reader,
1024 const struct abbrev_info &abbrev,
1025 const gdb_byte *info_ptr);
1026
7d00ffec
TT
1027 /* Compute the name of this partial DIE. This memoizes the
1028 result, so it is safe to call multiple times. */
1029 const char *name (dwarf2_cu *cu);
1030
72bf9492 1031 /* Offset of this DIE. */
6f06d47b 1032 const sect_offset sect_off;
72bf9492
DJ
1033
1034 /* DWARF-2 tag for this DIE. */
6f06d47b 1035 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1036
72bf9492 1037 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1038 const unsigned int has_children : 1;
1039
72bf9492
DJ
1040 unsigned int is_external : 1;
1041 unsigned int is_declaration : 1;
1042 unsigned int has_type : 1;
1043 unsigned int has_specification : 1;
1044 unsigned int has_pc_info : 1;
481860b3 1045 unsigned int may_be_inlined : 1;
72bf9492 1046
0c1b455e
TT
1047 /* This DIE has been marked DW_AT_main_subprogram. */
1048 unsigned int main_subprogram : 1;
1049
72bf9492
DJ
1050 /* Flag set if the SCOPE field of this structure has been
1051 computed. */
1052 unsigned int scope_set : 1;
1053
fa4028e9
JB
1054 /* Flag set if the DIE has a byte_size attribute. */
1055 unsigned int has_byte_size : 1;
1056
ff908ebf
AW
1057 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1058 unsigned int has_const_value : 1;
1059
98bfdba5
PA
1060 /* Flag set if any of the DIE's children are template arguments. */
1061 unsigned int has_template_arguments : 1;
1062
52356b79 1063 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1064 unsigned int fixup_called : 1;
1065
36586728
TT
1066 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1067 unsigned int is_dwz : 1;
1068
1069 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1070 unsigned int spec_is_dwz : 1;
1071
7d00ffec
TT
1072 unsigned int canonical_name : 1;
1073
72bf9492 1074 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1075 sometimes a default name for unnamed DIEs. */
7d00ffec 1076 const char *raw_name = nullptr;
72bf9492 1077
abc72ce4 1078 /* The linkage name, if present. */
6f06d47b 1079 const char *linkage_name = nullptr;
abc72ce4 1080
72bf9492
DJ
1081 /* The scope to prepend to our children. This is generally
1082 allocated on the comp_unit_obstack, so will disappear
1083 when this compilation unit leaves the cache. */
6f06d47b 1084 const char *scope = nullptr;
72bf9492 1085
95554aad
TT
1086 /* Some data associated with the partial DIE. The tag determines
1087 which field is live. */
1088 union
1089 {
1090 /* The location description associated with this DIE, if any. */
1091 struct dwarf_block *locdesc;
1092 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1093 sect_offset sect_off;
6f06d47b 1094 } d {};
72bf9492
DJ
1095
1096 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1097 CORE_ADDR lowpc = 0;
1098 CORE_ADDR highpc = 0;
72bf9492 1099
93311388 1100 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1101 DW_AT_sibling, if any. */
48fbe735
YQ
1102 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1103 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1104 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1105
1106 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1107 DW_AT_specification (or DW_AT_abstract_origin or
1108 DW_AT_extension). */
6f06d47b 1109 sect_offset spec_offset {};
72bf9492
DJ
1110
1111 /* Pointers to this DIE's parent, first child, and next sibling,
1112 if any. */
6f06d47b
YQ
1113 struct partial_die_info *die_parent = nullptr;
1114 struct partial_die_info *die_child = nullptr;
1115 struct partial_die_info *die_sibling = nullptr;
1116
1117 friend struct partial_die_info *
1118 dwarf2_cu::find_partial_die (sect_offset sect_off);
1119
1120 private:
1121 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1122 partial_die_info (sect_offset sect_off)
1123 : partial_die_info (sect_off, DW_TAG_padding, 0)
1124 {
1125 }
1126
1127 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1128 int has_children_)
1129 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1130 {
1131 is_external = 0;
1132 is_declaration = 0;
1133 has_type = 0;
1134 has_specification = 0;
1135 has_pc_info = 0;
1136 may_be_inlined = 0;
1137 main_subprogram = 0;
1138 scope_set = 0;
1139 has_byte_size = 0;
1140 has_const_value = 0;
1141 has_template_arguments = 0;
1142 fixup_called = 0;
1143 is_dwz = 0;
1144 spec_is_dwz = 0;
7d00ffec 1145 canonical_name = 0;
6f06d47b 1146 }
c906108c
SS
1147 };
1148
c906108c
SS
1149/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1150 but this would require a corresponding change in unpack_field_as_long
1151 and friends. */
1152static int bits_per_byte = 8;
1153
9c6a1327
TT
1154struct variant_part_builder;
1155
1156/* When reading a variant, we track a bit more information about the
1157 field, and store it in an object of this type. */
2ddeaf8a
TT
1158
1159struct variant_field
1160{
9c6a1327
TT
1161 int first_field = -1;
1162 int last_field = -1;
1163
1164 /* A variant can contain other variant parts. */
1165 std::vector<variant_part_builder> variant_parts;
1166
2ddeaf8a
TT
1167 /* If we see a DW_TAG_variant, then this will be set if this is the
1168 default branch. */
9c6a1327
TT
1169 bool default_branch = false;
1170 /* If we see a DW_AT_discr_value, then this will be the discriminant
1171 value. */
1172 ULONGEST discriminant_value = 0;
1173 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1174 data. */
1175 struct dwarf_block *discr_list_data = nullptr;
1176};
1177
1178/* This represents a DW_TAG_variant_part. */
1179
1180struct variant_part_builder
1181{
1182 /* The offset of the discriminant field. */
1183 sect_offset discriminant_offset {};
1184
1185 /* Variants that are direct children of this variant part. */
1186 std::vector<variant_field> variants;
1187
1188 /* True if we're currently reading a variant. */
1189 bool processing_variant = false;
2ddeaf8a
TT
1190};
1191
52059ffd
TT
1192struct nextfield
1193{
be2daae6
TT
1194 int accessibility = 0;
1195 int virtuality = 0;
9c6a1327
TT
1196 /* Variant parts need to find the discriminant, which is a DIE
1197 reference. We track the section offset of each field to make
1198 this link. */
1199 sect_offset offset;
be2daae6 1200 struct field field {};
52059ffd
TT
1201};
1202
1203struct fnfieldlist
1204{
be2daae6
TT
1205 const char *name = nullptr;
1206 std::vector<struct fn_field> fnfields;
52059ffd
TT
1207};
1208
c906108c
SS
1209/* The routines that read and process dies for a C struct or C++ class
1210 pass lists of data member fields and lists of member function fields
1211 in an instance of a field_info structure, as defined below. */
1212struct field_info
2de01bdb
SM
1213{
1214 /* List of data member and baseclasses fields. */
1215 std::vector<struct nextfield> fields;
1216 std::vector<struct nextfield> baseclasses;
1217
1218 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1219 bool non_public_fields = false;
2de01bdb
SM
1220
1221 /* Member function fieldlist array, contains name of possibly overloaded
1222 member function, number of overloaded member functions and a pointer
1223 to the head of the member function field chain. */
1224 std::vector<struct fnfieldlist> fnfieldlists;
1225
1226 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1227 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1228 std::vector<struct decl_field> typedef_field_list;
1229
1230 /* Nested types defined by this class and the number of elements in this
1231 list. */
1232 std::vector<struct decl_field> nested_types_list;
1233
1234 /* If non-null, this is the variant part we are currently
1235 reading. */
1236 variant_part_builder *current_variant_part = nullptr;
1237 /* This holds all the top-level variant parts attached to the type
1238 we're reading. */
1239 std::vector<variant_part_builder> variant_parts;
1240
1241 /* Return the total number of fields (including baseclasses). */
1242 int nfields () const
c5aa993b 1243 {
2de01bdb
SM
1244 return fields.size () + baseclasses.size ();
1245 }
1246};
c906108c 1247
ae038cb0
DJ
1248/* Loaded secondary compilation units are kept in memory until they
1249 have not been referenced for the processing of this many
1250 compilation units. Set this to zero to disable caching. Cache
1251 sizes of up to at least twenty will improve startup time for
1252 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1253static int dwarf_max_cache_age = 5;
920d2a44 1254static void
b4f54984
DE
1255show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1256 struct cmd_list_element *c, const char *value)
920d2a44 1257{
3e43a32a 1258 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1259 "DWARF compilation units is %s.\n"),
920d2a44
AC
1260 value);
1261}
4390d890 1262\f
c906108c
SS
1263/* local function prototypes */
1264
918dd910
JK
1265static void dwarf2_find_base_address (struct die_info *die,
1266 struct dwarf2_cu *cu);
1267
891813be 1268static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1269 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1270 const char *name);
0018ea6f 1271
f1902523
JK
1272static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1273 const gdb_byte *info_ptr,
3e225074 1274 struct die_info *type_unit_die);
f1902523 1275
976ca316 1276static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1277
72bf9492
DJ
1278static void scan_partial_symbols (struct partial_die_info *,
1279 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1280 int, struct dwarf2_cu *);
c906108c 1281
72bf9492
DJ
1282static void add_partial_symbol (struct partial_die_info *,
1283 struct dwarf2_cu *);
63d06c5c 1284
72bf9492
DJ
1285static void add_partial_namespace (struct partial_die_info *pdi,
1286 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1287 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1288
5d7cb8df 1289static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1290 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1291 struct dwarf2_cu *cu);
1292
72bf9492
DJ
1293static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1294 struct dwarf2_cu *cu);
91c24f0a 1295
bc30ff58
JB
1296static void add_partial_subprogram (struct partial_die_info *pdi,
1297 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1298 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1299
d521ce57 1300static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1301
dee91e82 1302static struct partial_die_info *load_partial_dies
d521ce57 1303 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1304
fb816e8b
TV
1305/* A pair of partial_die_info and compilation unit. */
1306struct cu_partial_die_info
1307{
1308 /* The compilation unit of the partial_die_info. */
1309 struct dwarf2_cu *cu;
1310 /* A partial_die_info. */
1311 struct partial_die_info *pdi;
122cf0f2
AB
1312
1313 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1314 : cu (cu),
1315 pdi (pdi)
405feb71 1316 { /* Nothing. */ }
122cf0f2
AB
1317
1318private:
1319 cu_partial_die_info () = delete;
fb816e8b
TV
1320};
1321
122cf0f2
AB
1322static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1323 struct dwarf2_cu *);
72bf9492 1324
d521ce57
TT
1325static const gdb_byte *read_attribute (const struct die_reader_specs *,
1326 struct attribute *, struct attr_abbrev *,
7a5f294d 1327 const gdb_byte *);
18a8505e
AT
1328
1329static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1330 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1331
1332static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1333
976ca316
SM
1334static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1335 dwarf2_section_info *, sect_offset);
f4dc4d17 1336
ed2dc618 1337static const char *read_indirect_string
976ca316 1338 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1339 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1340
ed2dc618 1341static const char *read_indirect_string_at_offset
976ca316 1342 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1343
d521ce57
TT
1344static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1345 const gdb_byte *,
3019eac3
DE
1346 unsigned int *);
1347
18a8505e
AT
1348static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1349 ULONGEST str_index);
1350
1351static const char *read_stub_str_index (struct dwarf2_cu *cu,
1352 ULONGEST str_index);
3019eac3 1353
e142c38c 1354static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1355
e142c38c
DJ
1356static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1357 struct dwarf2_cu *);
c906108c 1358
7d45c7c3 1359static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1360 struct dwarf2_cu *cu);
7d45c7c3 1361
a084a2a6
AT
1362static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1363
05cf31d1 1364static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1365 struct dwarf2_cu *cu);
05cf31d1 1366
e142c38c 1367static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1368
e142c38c 1369static struct die_info *die_specification (struct die_info *die,
f2f0e013 1370 struct dwarf2_cu **);
63d06c5c 1371
9c541725 1372static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1373 struct dwarf2_cu *cu);
debd256d 1374
f3f5162e 1375static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1376 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1377 CORE_ADDR, int decode_mapping);
c906108c 1378
804d2729
TT
1379static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1380 const char *);
c906108c 1381
a14ed312 1382static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1383 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1384
ff39bb5e 1385static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1386 struct dwarf2_cu *);
c906108c 1387
ff39bb5e 1388static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1389 struct type *type,
1390 const char *name,
1391 struct obstack *obstack,
12df843f 1392 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1393 const gdb_byte **bytes,
98bfdba5 1394 struct dwarf2_locexpr_baton **baton);
2df3850c 1395
57567375
TT
1396static struct type *read_subrange_index_type (struct die_info *die,
1397 struct dwarf2_cu *cu);
1398
e7c27a73 1399static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1400
b4ba55a1
JB
1401static int need_gnat_info (struct dwarf2_cu *);
1402
3e43a32a
MS
1403static struct type *die_descriptive_type (struct die_info *,
1404 struct dwarf2_cu *);
b4ba55a1
JB
1405
1406static void set_descriptive_type (struct type *, struct die_info *,
1407 struct dwarf2_cu *);
1408
e7c27a73
DJ
1409static struct type *die_containing_type (struct die_info *,
1410 struct dwarf2_cu *);
c906108c 1411
ff39bb5e 1412static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1413 struct dwarf2_cu *);
c906108c 1414
f792889a 1415static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1416
673bfd45
DE
1417static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1418
0d5cff50 1419static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1420
6e70227d 1421static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1422 const char *suffix, int physname,
1423 struct dwarf2_cu *cu);
63d06c5c 1424
e7c27a73 1425static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1426
348e048f
DE
1427static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1428
e7c27a73 1429static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1430
e7c27a73 1431static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1432
96408a79
SA
1433static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1434
71a3c369
TT
1435static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1436
41144253 1437/* Return the .debug_loclists section to use for cu. */
1438static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1439
d0ce17d8
CT
1440/* Return the .debug_rnglists section to use for cu. */
1441static struct dwarf2_section_info *cu_debug_rnglists_section
1442 (struct dwarf2_cu *cu, dwarf_tag tag);
1443
3a2b436a 1444/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1445 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1446enum pc_bounds_kind
1447{
e385593e 1448 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1449 PC_BOUNDS_NOT_PRESENT,
1450
e385593e
JK
1451 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1452 were present but they do not form a valid range of PC addresses. */
1453 PC_BOUNDS_INVALID,
1454
3a2b436a
JK
1455 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1456 PC_BOUNDS_RANGES,
1457
1458 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1459 PC_BOUNDS_HIGH_LOW,
1460};
1461
1462static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1463 CORE_ADDR *, CORE_ADDR *,
1464 struct dwarf2_cu *,
891813be 1465 dwarf2_psymtab *);
c906108c 1466
fae299cd
DC
1467static void get_scope_pc_bounds (struct die_info *,
1468 CORE_ADDR *, CORE_ADDR *,
1469 struct dwarf2_cu *);
1470
801e3a5b 1471static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1472 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1473
a14ed312 1474static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1475 struct dwarf2_cu *);
c906108c 1476
a14ed312 1477static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1478 struct type *, struct dwarf2_cu *);
c906108c 1479
a14ed312 1480static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1481 struct die_info *, struct type *,
e7c27a73 1482 struct dwarf2_cu *);
c906108c 1483
a14ed312 1484static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1485 struct type *,
1486 struct dwarf2_cu *);
c906108c 1487
134d01f1 1488static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1489
e7c27a73 1490static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1491
e7c27a73 1492static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1493
5d7cb8df
JK
1494static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1495
804d2729 1496static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1497
27aa8d6a
SW
1498static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1499
74921315
KS
1500static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1501
f55ee35c
JK
1502static struct type *read_module_type (struct die_info *die,
1503 struct dwarf2_cu *cu);
1504
38d518c9 1505static const char *namespace_name (struct die_info *die,
e142c38c 1506 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1507
134d01f1 1508static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1509
7d79de9a
TT
1510static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1511 bool * = nullptr);
c906108c 1512
6e70227d 1513static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1514 struct dwarf2_cu *);
1515
bf6af496 1516static struct die_info *read_die_and_siblings_1
d521ce57 1517 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1518 struct die_info *);
639d11d3 1519
dee91e82 1520static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1521 const gdb_byte *info_ptr,
1522 const gdb_byte **new_info_ptr,
639d11d3
DC
1523 struct die_info *parent);
1524
d521ce57
TT
1525static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1526 struct die_info **, const gdb_byte *,
3e225074 1527 int);
3019eac3 1528
d521ce57 1529static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1530 struct die_info **, const gdb_byte *);
93311388 1531
e7c27a73 1532static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1533
15d034d0 1534static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1535 struct objfile *);
71c25dea 1536
15d034d0 1537static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1538
15d034d0 1539static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1540 struct die_info *die,
1541 struct dwarf2_cu *cu);
1542
ca69b9e6
DE
1543static const char *dwarf2_physname (const char *name, struct die_info *die,
1544 struct dwarf2_cu *cu);
1545
e142c38c 1546static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1547 struct dwarf2_cu **);
9219021c 1548
d97bc12b
DE
1549static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1550
1551static void dump_die_for_error (struct die_info *);
1552
1553static void dump_die_1 (struct ui_file *, int level, int max_level,
1554 struct die_info *);
c906108c 1555
d97bc12b 1556/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1557
51545339 1558static void store_in_ref_table (struct die_info *,
10b3939b 1559 struct dwarf2_cu *);
c906108c 1560
348e048f 1561static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1562 const struct attribute *,
348e048f
DE
1563 struct dwarf2_cu **);
1564
10b3939b 1565static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1566 const struct attribute *,
f2f0e013 1567 struct dwarf2_cu **);
c906108c 1568
348e048f 1569static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1570 const struct attribute *,
348e048f
DE
1571 struct dwarf2_cu **);
1572
ac9ec31b
DE
1573static struct type *get_signatured_type (struct die_info *, ULONGEST,
1574 struct dwarf2_cu *);
1575
1576static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1577 const struct attribute *,
ac9ec31b
DE
1578 struct dwarf2_cu *);
1579
ab432490
SM
1580static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1581 dwarf2_per_objfile *per_objfile);
348e048f 1582
ab432490
SM
1583static void read_signatured_type (signatured_type *sig_type,
1584 dwarf2_per_objfile *per_objfile);
348e048f 1585
63e43d3a
PMR
1586static int attr_to_dynamic_prop (const struct attribute *attr,
1587 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1588 struct dynamic_prop *prop, struct type *type);
63e43d3a 1589
c906108c
SS
1590/* memory allocation interface */
1591
7b5a2f43 1592static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1593
b60c80d6 1594static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1595
43f3e411 1596static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1597
8cf6f0b1
TT
1598static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1599 struct dwarf2_loclist_baton *baton,
ff39bb5e 1600 const struct attribute *attr);
8cf6f0b1 1601
ff39bb5e 1602static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1603 struct symbol *sym,
f1e6e072
TT
1604 struct dwarf2_cu *cu,
1605 int is_block);
4c2df51b 1606
d521ce57
TT
1607static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1608 const gdb_byte *info_ptr,
1609 struct abbrev_info *abbrev);
4bb7a0a7 1610
72bf9492
DJ
1611static hashval_t partial_die_hash (const void *item);
1612
1613static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1614
ae038cb0 1615static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1616 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1617 dwarf2_per_objfile *per_objfile);
ae038cb0 1618
9816fde3 1619static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1620 struct die_info *comp_unit_die,
1621 enum language pretend_language);
93311388 1622
f792889a 1623static struct type *set_die_type (struct die_info *, struct type *,
57567375 1624 struct dwarf2_cu *, bool = false);
1c379e20 1625
976ca316 1626static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1627
976ca316 1628static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1629
ab432490
SM
1630static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1631 dwarf2_per_objfile *per_objfile,
4a636814 1632 dwarf2_cu *existing_cu,
ab432490
SM
1633 bool skip_partial,
1634 enum language pretend_language);
10b3939b 1635
8fc0b21d 1636static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1637 enum language pretend_language);
10b3939b 1638
8fc0b21d 1639static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1640 enum language pretend_language);
f4dc4d17 1641
10b3939b
DJ
1642static void dwarf2_add_dependence (struct dwarf2_cu *,
1643 struct dwarf2_per_cu_data *);
1644
ae038cb0
DJ
1645static void dwarf2_mark (struct dwarf2_cu *);
1646
b64f50a1 1647static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1648 dwarf2_per_cu_data *per_cu,
1649 dwarf2_per_objfile *per_objfile);
673bfd45 1650
f792889a 1651static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1652
120ce1b5
SM
1653static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1654 dwarf2_per_objfile *per_objfile,
95554aad
TT
1655 enum language pretend_language);
1656
976ca316 1657static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1658
b303c6f6
AB
1659/* Class, the destructor of which frees all allocated queue entries. This
1660 will only have work to do if an error was thrown while processing the
1661 dwarf. If no error was thrown then the queue entries should have all
1662 been processed, and freed, as we went along. */
1663
1664class dwarf2_queue_guard
1665{
1666public:
39856def
TT
1667 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1668 : m_per_objfile (per_objfile)
1669 {
1670 }
b303c6f6
AB
1671
1672 /* Free any entries remaining on the queue. There should only be
1673 entries left if we hit an error while processing the dwarf. */
1674 ~dwarf2_queue_guard ()
1675 {
39856def
TT
1676 /* Ensure that no memory is allocated by the queue. */
1677 std::queue<dwarf2_queue_item> empty;
5989a64e 1678 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1679 }
b303c6f6 1680
39856def 1681 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1682
39856def
TT
1683private:
1684 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1685};
1686
39856def
TT
1687dwarf2_queue_item::~dwarf2_queue_item ()
1688{
1689 /* Anything still marked queued is likely to be in an
1690 inconsistent state, so discard it. */
1691 if (per_cu->queued)
1692 {
7188ed02 1693 per_objfile->remove_cu (per_cu);
39856def
TT
1694 per_cu->queued = 0;
1695 }
1696}
1697
d721ba37
PA
1698/* The return type of find_file_and_directory. Note, the enclosed
1699 string pointers are only valid while this object is valid. */
1700
1701struct file_and_directory
1702{
1703 /* The filename. This is never NULL. */
1704 const char *name;
1705
1706 /* The compilation directory. NULL if not known. If we needed to
1707 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1708 points directly to the DW_AT_comp_dir string attribute owned by
1709 the obstack that owns the DIE. */
1710 const char *comp_dir;
1711
1712 /* If we needed to build a new string for comp_dir, this is what
1713 owns the storage. */
1714 std::string comp_dir_storage;
1715};
1716
1717static file_and_directory find_file_and_directory (struct die_info *die,
1718 struct dwarf2_cu *cu);
9291a0cd 1719
298e9637 1720static htab_up allocate_signatured_type_table ();
1fd400ff 1721
298e9637 1722static htab_up allocate_dwo_unit_table ();
3019eac3 1723
57d63ce2 1724static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1725 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1726 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1727
976ca316 1728static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1729
3019eac3 1730static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1731 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1732 ULONGEST signature);
3019eac3
DE
1733
1734static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1735 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1736
1b555f17 1737static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1738
263db9a1
TT
1739/* A unique pointer to a dwo_file. */
1740
51ac9db5 1741typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1742
976ca316 1743static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1744
1b80a9fa 1745static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1746
1747static void free_line_header_voidp (void *arg);
4390d890
DE
1748\f
1749/* Various complaints about symbol reading that don't abort the process. */
1750
4390d890
DE
1751static void
1752dwarf2_debug_line_missing_file_complaint (void)
1753{
b98664d3 1754 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1755}
1756
1757static void
1758dwarf2_debug_line_missing_end_sequence_complaint (void)
1759{
b98664d3 1760 complaint (_(".debug_line section has line "
4390d890
DE
1761 "program sequence without an end"));
1762}
1763
1764static void
1765dwarf2_complex_location_expr_complaint (void)
1766{
b98664d3 1767 complaint (_("location expression too complex"));
4390d890
DE
1768}
1769
1770static void
1771dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1772 int arg3)
1773{
b98664d3 1774 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1775 arg1, arg2, arg3);
1776}
1777
4390d890
DE
1778static void
1779dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1780{
b98664d3 1781 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1782 arg1, arg2);
1783}
527f3840
JK
1784
1785/* Hash function for line_header_hash. */
1786
1787static hashval_t
1788line_header_hash (const struct line_header *ofs)
1789{
9c541725 1790 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1791}
1792
1793/* Hash function for htab_create_alloc_ex for line_header_hash. */
1794
1795static hashval_t
1796line_header_hash_voidp (const void *item)
1797{
9a3c8263 1798 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1799
1800 return line_header_hash (ofs);
1801}
1802
1803/* Equality function for line_header_hash. */
1804
1805static int
1806line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1807{
9a3c8263
SM
1808 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1809 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1810
9c541725 1811 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1812 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1813}
1814
4390d890 1815\f
9291a0cd 1816
330cdd98
PA
1817/* See declaration. */
1818
5989a64e
SM
1819dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1820 bool can_copy_)
c3699833
SM
1821 : obfd (obfd),
1822 can_copy (can_copy_)
330cdd98
PA
1823{
1824 if (names == NULL)
1825 names = &dwarf2_elf_names;
1826
330cdd98
PA
1827 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1828 locate_sections (obfd, sec, *names);
1829}
1830
5989a64e 1831dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1832{
b76e467d 1833 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1834 per_cu->imported_symtabs_free ();
fc8e7e75 1835
b2bdb8cf 1836 for (signatured_type *sig_type : all_type_units)
ae640021 1837 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1838
5989a64e 1839 /* Everything else should be on this->obstack. */
330cdd98
PA
1840}
1841
7188ed02 1842/* See read.h. */
330cdd98
PA
1843
1844void
7188ed02 1845dwarf2_per_objfile::remove_all_cus ()
330cdd98 1846{
7188ed02
SM
1847 for (auto pair : m_dwarf2_cus)
1848 delete pair.second;
330cdd98 1849
7188ed02 1850 m_dwarf2_cus.clear ();
330cdd98
PA
1851}
1852
11ed8cad
TT
1853/* A helper class that calls free_cached_comp_units on
1854 destruction. */
1855
1856class free_cached_comp_units
1857{
1858public:
1859
1860 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1861 : m_per_objfile (per_objfile)
1862 {
1863 }
1864
1865 ~free_cached_comp_units ()
1866 {
7188ed02 1867 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1868 }
1869
1870 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1871
1872private:
1873
1874 dwarf2_per_objfile *m_per_objfile;
1875};
1876
af758d11
SM
1877/* See read.h. */
1878
1879bool
1880dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1881{
1882 gdb_assert (per_cu->index < this->m_symtabs.size ());
1883
1884 return this->m_symtabs[per_cu->index] != nullptr;
1885}
1886
1887/* See read.h. */
1888
1889compunit_symtab *
1890dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1891{
1892 gdb_assert (per_cu->index < this->m_symtabs.size ());
1893
1894 return this->m_symtabs[per_cu->index];
1895}
1896
1897/* See read.h. */
1898
1899void
1900dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1901 compunit_symtab *symtab)
1902{
1903 gdb_assert (per_cu->index < this->m_symtabs.size ());
1904 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1905
1906 this->m_symtabs[per_cu->index] = symtab;
1907}
1908
c906108c 1909/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1910 information and return true if we have enough to do something.
1911 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1912 ELF names are used. CAN_COPY is true for formats where symbol
1913 interposition is possible and so symbol values must follow copy
1914 relocation rules. */
c906108c
SS
1915
1916int
251d32d9 1917dwarf2_has_info (struct objfile *objfile,
dda83cd7 1918 const struct dwarf2_debug_sections *names,
4b610737 1919 bool can_copy)
c906108c 1920{
97cbe998
SDJ
1921 if (objfile->flags & OBJF_READNEVER)
1922 return 0;
1923
976ca316 1924 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1925
976ca316 1926 if (per_objfile == NULL)
5989a64e 1927 {
17ee85fc
TT
1928 dwarf2_per_bfd *per_bfd;
1929
1930 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1931 doesn't require relocations and if there aren't partial symbols
17ee85fc
TT
1932 from some other reader. */
1933 if (!objfile_has_partial_symbols (objfile)
1934 && !gdb_bfd_requires_relocations (objfile->obfd))
1935 {
1936 /* See if one has been created for this BFD yet. */
1937 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1938
1939 if (per_bfd == nullptr)
1940 {
1941 /* No, create it now. */
1942 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1943 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1944 }
1945 }
1946 else
1947 {
1948 /* No sharing possible, create one specifically for this objfile. */
1949 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1950 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1951 }
5989a64e 1952
976ca316 1953 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1954 }
5bfd760d 1955
976ca316
SM
1956 return (!per_objfile->per_bfd->info.is_virtual
1957 && per_objfile->per_bfd->info.s.section != NULL
1958 && !per_objfile->per_bfd->abbrev.is_virtual
1959 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1960}
1961
251d32d9
TG
1962/* When loading sections, we look either for uncompressed section or for
1963 compressed section names. */
233a11ab
CS
1964
1965static int
251d32d9 1966section_is_p (const char *section_name,
dda83cd7 1967 const struct dwarf2_section_names *names)
233a11ab 1968{
251d32d9
TG
1969 if (names->normal != NULL
1970 && strcmp (section_name, names->normal) == 0)
1971 return 1;
1972 if (names->compressed != NULL
1973 && strcmp (section_name, names->compressed) == 0)
1974 return 1;
1975 return 0;
233a11ab
CS
1976}
1977
330cdd98 1978/* See declaration. */
c906108c 1979
330cdd98 1980void
5989a64e
SM
1981dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1982 const dwarf2_debug_sections &names)
c906108c 1983{
fd361982 1984 flagword aflag = bfd_section_flags (sectp);
251d32d9 1985
dc7650b8
JK
1986 if ((aflag & SEC_HAS_CONTENTS) == 0)
1987 {
1988 }
950b7495
KS
1989 else if (elf_section_data (sectp)->this_hdr.sh_size
1990 > bfd_get_file_size (abfd))
1991 {
1992 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1993 warning (_("Discarding section %s which has a section size (%s"
1994 ") larger than the file size [in module %s]"),
1995 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1996 bfd_get_filename (abfd));
1997 }
330cdd98 1998 else if (section_is_p (sectp->name, &names.info))
c906108c 1999 {
330cdd98 2000 this->info.s.section = sectp;
fd361982 2001 this->info.size = bfd_section_size (sectp);
c906108c 2002 }
330cdd98 2003 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 2004 {
330cdd98 2005 this->abbrev.s.section = sectp;
fd361982 2006 this->abbrev.size = bfd_section_size (sectp);
c906108c 2007 }
330cdd98 2008 else if (section_is_p (sectp->name, &names.line))
c906108c 2009 {
330cdd98 2010 this->line.s.section = sectp;
fd361982 2011 this->line.size = bfd_section_size (sectp);
c906108c 2012 }
330cdd98 2013 else if (section_is_p (sectp->name, &names.loc))
c906108c 2014 {
330cdd98 2015 this->loc.s.section = sectp;
fd361982 2016 this->loc.size = bfd_section_size (sectp);
c906108c 2017 }
330cdd98 2018 else if (section_is_p (sectp->name, &names.loclists))
43988095 2019 {
330cdd98 2020 this->loclists.s.section = sectp;
fd361982 2021 this->loclists.size = bfd_section_size (sectp);
43988095 2022 }
330cdd98 2023 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2024 {
330cdd98 2025 this->macinfo.s.section = sectp;
fd361982 2026 this->macinfo.size = bfd_section_size (sectp);
c906108c 2027 }
330cdd98 2028 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2029 {
330cdd98 2030 this->macro.s.section = sectp;
fd361982 2031 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2032 }
330cdd98 2033 else if (section_is_p (sectp->name, &names.str))
c906108c 2034 {
330cdd98 2035 this->str.s.section = sectp;
fd361982 2036 this->str.size = bfd_section_size (sectp);
c906108c 2037 }
18a8505e
AT
2038 else if (section_is_p (sectp->name, &names.str_offsets))
2039 {
2040 this->str_offsets.s.section = sectp;
2041 this->str_offsets.size = bfd_section_size (sectp);
2042 }
330cdd98 2043 else if (section_is_p (sectp->name, &names.line_str))
43988095 2044 {
330cdd98 2045 this->line_str.s.section = sectp;
fd361982 2046 this->line_str.size = bfd_section_size (sectp);
43988095 2047 }
330cdd98 2048 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2049 {
330cdd98 2050 this->addr.s.section = sectp;
fd361982 2051 this->addr.size = bfd_section_size (sectp);
3019eac3 2052 }
330cdd98 2053 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2054 {
330cdd98 2055 this->frame.s.section = sectp;
fd361982 2056 this->frame.size = bfd_section_size (sectp);
b6af0555 2057 }
330cdd98 2058 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2059 {
330cdd98 2060 this->eh_frame.s.section = sectp;
fd361982 2061 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2062 }
330cdd98 2063 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2064 {
330cdd98 2065 this->ranges.s.section = sectp;
fd361982 2066 this->ranges.size = bfd_section_size (sectp);
af34e669 2067 }
330cdd98 2068 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2069 {
330cdd98 2070 this->rnglists.s.section = sectp;
fd361982 2071 this->rnglists.size = bfd_section_size (sectp);
43988095 2072 }
330cdd98 2073 else if (section_is_p (sectp->name, &names.types))
348e048f 2074 {
8b70b953
TT
2075 struct dwarf2_section_info type_section;
2076
2077 memset (&type_section, 0, sizeof (type_section));
049412e3 2078 type_section.s.section = sectp;
fd361982 2079 type_section.size = bfd_section_size (sectp);
8b70b953 2080
fd5866f6 2081 this->types.push_back (type_section);
348e048f 2082 }
330cdd98 2083 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2084 {
330cdd98 2085 this->gdb_index.s.section = sectp;
fd361982 2086 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2087 }
927aa2e7
JK
2088 else if (section_is_p (sectp->name, &names.debug_names))
2089 {
2090 this->debug_names.s.section = sectp;
fd361982 2091 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2092 }
2093 else if (section_is_p (sectp->name, &names.debug_aranges))
2094 {
2095 this->debug_aranges.s.section = sectp;
fd361982 2096 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2097 }
dce234bc 2098
fd361982
AM
2099 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2100 && bfd_section_vma (sectp) == 0)
330cdd98 2101 this->has_section_at_zero = true;
c906108c
SS
2102}
2103
dce234bc 2104/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2105 SECTION_NAME. */
af34e669 2106
dce234bc 2107void
3017a003 2108dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2109 enum dwarf2_section_enum sect,
2110 asection **sectp, const gdb_byte **bufp,
2111 bfd_size_type *sizep)
dce234bc 2112{
976ca316 2113 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2114 struct dwarf2_section_info *info;
a3b2a86b
TT
2115
2116 /* We may see an objfile without any DWARF, in which case we just
2117 return nothing. */
976ca316 2118 if (per_objfile == NULL)
a3b2a86b
TT
2119 {
2120 *sectp = NULL;
2121 *bufp = NULL;
2122 *sizep = 0;
2123 return;
2124 }
3017a003
TG
2125 switch (sect)
2126 {
2127 case DWARF2_DEBUG_FRAME:
976ca316 2128 info = &per_objfile->per_bfd->frame;
3017a003
TG
2129 break;
2130 case DWARF2_EH_FRAME:
976ca316 2131 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2132 break;
2133 default:
2134 gdb_assert_not_reached ("unexpected section");
2135 }
dce234bc 2136
96b79293 2137 info->read (objfile);
dce234bc 2138
96b79293 2139 *sectp = info->get_bfd_section ();
dce234bc
PP
2140 *bufp = info->buffer;
2141 *sizep = info->size;
2142}
2143
36586728
TT
2144/* A helper function to find the sections for a .dwz file. */
2145
2146static void
5bb6e9dd 2147locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
36586728 2148{
36586728
TT
2149 /* Note that we only support the standard ELF names, because .dwz
2150 is ELF-only (at the time of writing). */
2151 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2152 {
049412e3 2153 dwz_file->abbrev.s.section = sectp;
fd361982 2154 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2155 }
2156 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2157 {
049412e3 2158 dwz_file->info.s.section = sectp;
fd361982 2159 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2160 }
2161 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2162 {
049412e3 2163 dwz_file->str.s.section = sectp;
fd361982 2164 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2165 }
2166 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2167 {
049412e3 2168 dwz_file->line.s.section = sectp;
fd361982 2169 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2170 }
2171 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2172 {
049412e3 2173 dwz_file->macro.s.section = sectp;
fd361982 2174 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2175 }
2ec9a5e0
TT
2176 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2177 {
049412e3 2178 dwz_file->gdb_index.s.section = sectp;
fd361982 2179 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2180 }
927aa2e7
JK
2181 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2182 {
2183 dwz_file->debug_names.s.section = sectp;
fd361982 2184 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2185 }
36586728
TT
2186}
2187
2bf3b79d
SDJ
2188/* Attempt to find a .dwz file (whose full path is represented by
2189 FILENAME) in all of the specified debug file directories provided.
2190
2191 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2192 nullptr if it could not find anything. */
2193
2194static gdb_bfd_ref_ptr
2195dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2196 size_t buildid_len)
2197{
2198 /* Let's assume that the path represented by FILENAME has the
2199 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2200 distributions do, anyway. */
2201 size_t dwz_pos = filename.find ("/.dwz/");
2202
2203 if (dwz_pos == std::string::npos)
2204 return nullptr;
2205
2206 /* This is an obvious assertion, but it's here more to educate
2207 future readers of this code that FILENAME at DWZ_POS *must*
2208 contain a directory separator. */
2209 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2210
2211 gdb_bfd_ref_ptr dwz_bfd;
2212 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2213 = dirnames_to_char_ptr_vec (debug_file_directory);
2214
2215 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2216 {
2217 /* The idea is to iterate over the
2218 debug file directories provided by the user and
2219 replace the hard-coded path in the "filename" by each
2220 debug-file-directory.
2221
2222 For example, suppose that filename is:
2223
2224 /usr/lib/debug/.dwz/foo.dwz
2225
2226 And suppose that we have "$HOME/bar" as the
2227 debug-file-directory. We would then adjust filename
2228 to look like:
2229
2230 $HOME/bar/.dwz/foo.dwz
2231
2232 which would hopefully allow us to find the alt debug
2233 file. */
2234 std::string ddir = debugdir.get ();
2235
2236 if (ddir.empty ())
2237 continue;
2238
2239 /* Make sure the current debug-file-directory ends with a
2240 directory separator. This is needed because, if FILENAME
2241 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2242 DDIR is "/usr/lib/abc", then could wrongfully skip it
2243 below. */
2244 if (!IS_DIR_SEPARATOR (ddir.back ()))
2245 ddir += SLASH_STRING;
2246
2247 /* Check whether the beginning of FILENAME is DDIR. If it is,
2248 then we are dealing with a file which we already attempted to
2249 open before, so we just skip it and continue processing the
2250 remaining debug file directories. */
2251 if (filename.size () > ddir.size ()
2252 && filename.compare (0, ddir.size (), ddir) == 0)
2253 continue;
2254
2255 /* Replace FILENAME's default debug-file-directory with
2256 DDIR. */
2257 std::string new_filename = ddir + &filename[dwz_pos + 1];
2258
2259 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2260
2261 if (dwz_bfd == nullptr)
2262 continue;
2263
2264 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2265 {
2266 dwz_bfd.reset (nullptr);
2267 continue;
2268 }
2269
2270 /* Found it. */
2271 break;
2272 }
2273
2274 return dwz_bfd;
2275}
2276
c4973306 2277/* See dwarf2read.h. */
36586728 2278
c4973306 2279struct dwz_file *
c3699833 2280dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2281{
acd13123 2282 bfd_size_type buildid_len_arg;
dc294be5
TT
2283 size_t buildid_len;
2284 bfd_byte *buildid;
36586728 2285
c3699833
SM
2286 if (per_bfd->dwz_file != NULL)
2287 return per_bfd->dwz_file.get ();
36586728 2288
4db1a1dc 2289 bfd_set_error (bfd_error_no_error);
791afaa2 2290 gdb::unique_xmalloc_ptr<char> data
c3699833 2291 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2292 &buildid_len_arg, &buildid));
4db1a1dc
TT
2293 if (data == NULL)
2294 {
2295 if (bfd_get_error () == bfd_error_no_error)
2296 return NULL;
2297 error (_("could not read '.gnu_debugaltlink' section: %s"),
2298 bfd_errmsg (bfd_get_error ()));
2299 }
791afaa2
TT
2300
2301 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2302
acd13123
TT
2303 buildid_len = (size_t) buildid_len_arg;
2304
2bf3b79d 2305 std::string filename = data.get ();
d721ba37 2306
2bf3b79d 2307 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
36586728 2308 {
14278e1f 2309 gdb::unique_xmalloc_ptr<char> abs
c3699833 2310 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2311
2bf3b79d 2312 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
36586728
TT
2313 }
2314
dc294be5
TT
2315 /* First try the file name given in the section. If that doesn't
2316 work, try to use the build-id instead. */
2bf3b79d 2317 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
dc294be5 2318 if (dwz_bfd != NULL)
36586728 2319 {
192b62ce 2320 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2321 dwz_bfd.reset (nullptr);
36586728
TT
2322 }
2323
dc294be5
TT
2324 if (dwz_bfd == NULL)
2325 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2326
2bf3b79d
SDJ
2327 if (dwz_bfd == nullptr)
2328 {
2329 /* If the user has provided us with different
2330 debug file directories, we can try them in order. */
2331 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2332 }
2333
0d79cdc4
AM
2334 if (dwz_bfd == nullptr)
2335 {
2336 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2337 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2338
2339 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2340 buildid_len,
2341 origname,
2342 &alt_filename));
2343
2344 if (fd.get () >= 0)
2345 {
2346 /* File successfully retrieved from server. */
ad80db5b 2347 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2348
2349 if (dwz_bfd == nullptr)
2350 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2351 alt_filename.get ());
2352 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2353 dwz_bfd.reset (nullptr);
2354 }
2355 }
2356
dc294be5
TT
2357 if (dwz_bfd == NULL)
2358 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2359 bfd_get_filename (per_bfd->obfd));
dc294be5 2360
7ff8cb8c
TT
2361 std::unique_ptr<struct dwz_file> result
2362 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2363
5bb6e9dd
TT
2364 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2365 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
36586728 2366
c3699833
SM
2367 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2368 per_bfd->dwz_file = std::move (result);
2369 return per_bfd->dwz_file.get ();
36586728 2370}
9291a0cd 2371\f
7b9f3c50
DE
2372/* DWARF quick_symbols_functions support. */
2373
2374/* TUs can share .debug_line entries, and there can be a lot more TUs than
2375 unique line tables, so we maintain a separate table of all .debug_line
2376 derived entries to support the sharing.
2377 All the quick functions need is the list of file names. We discard the
2378 line_header when we're done and don't need to record it here. */
2379struct quick_file_names
2380{
094b34ac
DE
2381 /* The data used to construct the hash key. */
2382 struct stmt_list_hash hash;
7b9f3c50
DE
2383
2384 /* The number of entries in file_names, real_names. */
2385 unsigned int num_file_names;
2386
2387 /* The file names from the line table, after being run through
2388 file_full_name. */
2389 const char **file_names;
2390
2391 /* The file names from the line table after being run through
2392 gdb_realpath. These are computed lazily. */
2393 const char **real_names;
2394};
2395
2396/* When using the index (and thus not using psymtabs), each CU has an
2397 object of this type. This is used to hold information needed by
2398 the various "quick" methods. */
2399struct dwarf2_per_cu_quick_data
2400{
2401 /* The file table. This can be NULL if there was no file table
2402 or it's currently not read in.
5989a64e 2403 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2404 struct quick_file_names *file_names;
2405
7b9f3c50
DE
2406 /* A temporary mark bit used when iterating over all CUs in
2407 expand_symtabs_matching. */
2408 unsigned int mark : 1;
2409
2410 /* True if we've tried to read the file table and found there isn't one.
2411 There will be no point in trying to read it again next time. */
2412 unsigned int no_file_data : 1;
2413};
2414
094b34ac
DE
2415/* Utility hash function for a stmt_list_hash. */
2416
2417static hashval_t
2418hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2419{
2420 hashval_t v = 0;
2421
2422 if (stmt_list_hash->dwo_unit != NULL)
2423 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2424 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2425 return v;
2426}
2427
2428/* Utility equality function for a stmt_list_hash. */
2429
2430static int
2431eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2432 const struct stmt_list_hash *rhs)
2433{
2434 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2435 return 0;
2436 if (lhs->dwo_unit != NULL
2437 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2438 return 0;
2439
9c541725 2440 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2441}
2442
7b9f3c50
DE
2443/* Hash function for a quick_file_names. */
2444
2445static hashval_t
2446hash_file_name_entry (const void *e)
2447{
9a3c8263
SM
2448 const struct quick_file_names *file_data
2449 = (const struct quick_file_names *) e;
7b9f3c50 2450
094b34ac 2451 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2452}
2453
2454/* Equality function for a quick_file_names. */
2455
2456static int
2457eq_file_name_entry (const void *a, const void *b)
2458{
9a3c8263
SM
2459 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2460 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2461
094b34ac 2462 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2463}
2464
2465/* Delete function for a quick_file_names. */
2466
2467static void
2468delete_file_name_entry (void *e)
2469{
9a3c8263 2470 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2471 int i;
2472
2473 for (i = 0; i < file_data->num_file_names; ++i)
2474 {
2475 xfree ((void*) file_data->file_names[i]);
2476 if (file_data->real_names)
2477 xfree ((void*) file_data->real_names[i]);
2478 }
2479
45940949
TT
2480 /* The space for the struct itself lives on the obstack, so we don't
2481 free it here. */
7b9f3c50
DE
2482}
2483
2484/* Create a quick_file_names hash table. */
2485
5895093f 2486static htab_up
7b9f3c50
DE
2487create_quick_file_names_table (unsigned int nr_initial_entries)
2488{
5895093f
TT
2489 return htab_up (htab_create_alloc (nr_initial_entries,
2490 hash_file_name_entry, eq_file_name_entry,
2491 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2492}
9291a0cd 2493
ab432490
SM
2494/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2495 function is unrelated to symtabs, symtab would have to be created afterwards.
2496 You should call age_cached_comp_units after processing the CU. */
918dd910 2497
1b555f17 2498static dwarf2_cu *
ab432490
SM
2499load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2500 bool skip_partial)
918dd910 2501{
3019eac3 2502 if (per_cu->is_debug_types)
ab432490 2503 load_full_type_unit (per_cu, per_objfile);
918dd910 2504 else
4a636814
SM
2505 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2506 skip_partial, language_minimal);
918dd910 2507
7188ed02
SM
2508 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2509 if (cu == nullptr)
1b555f17 2510 return nullptr; /* Dummy CU. */
2dc860c0 2511
7188ed02 2512 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2513
7188ed02 2514 return cu;
918dd910
JK
2515}
2516
1350c3b4 2517/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2518
9291a0cd 2519static void
97a1449a 2520dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2521 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2522{
f4dc4d17
DE
2523 /* Skip type_unit_groups, reading the type units they contain
2524 is handled elsewhere. */
197400e8 2525 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2526 return;
2527
b303c6f6
AB
2528 /* The destructor of dwarf2_queue_guard frees any entries left on
2529 the queue. After this point we're guaranteed to leave this function
2530 with the dwarf queue empty. */
bf6e5d01 2531 dwarf2_queue_guard q_guard (per_objfile);
9291a0cd 2532
976ca316 2533 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2534 {
976ca316
SM
2535 queue_comp_unit (per_cu, per_objfile, language_minimal);
2536 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2537
2538 /* If we just loaded a CU from a DWO, and we're working with an index
2539 that may badly handle TUs, load all the TUs in that DWO as well.
2540 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2541 if (!per_cu->is_debug_types
1b555f17
SM
2542 && cu != NULL
2543 && cu->dwo_unit != NULL
976ca316
SM
2544 && per_objfile->per_bfd->index_table != NULL
2545 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2546 /* DWP files aren't supported yet. */
976ca316 2547 && get_dwp_file (per_objfile) == NULL)
1b555f17 2548 queue_and_load_all_dwo_tus (cu);
95554aad 2549 }
9291a0cd 2550
976ca316 2551 process_queue (per_objfile);
9291a0cd
TT
2552
2553 /* Age the cache, releasing compilation units that have not
2554 been used recently. */
976ca316 2555 per_objfile->age_comp_units ();
9291a0cd
TT
2556}
2557
97a1449a
SM
2558/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2559 the per-objfile for which this symtab is instantiated.
2560
2561 Returns the resulting symbol table. */
2fdf6df6 2562
43f3e411 2563static struct compunit_symtab *
97a1449a 2564dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2565 dwarf2_per_objfile *per_objfile,
97a1449a 2566 bool skip_partial)
9291a0cd 2567{
976ca316 2568 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2569
976ca316 2570 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2571 {
976ca316 2572 free_cached_comp_units freer (per_objfile);
c83dd867 2573 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2574 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2575 process_cu_includes (per_objfile);
9291a0cd 2576 }
f194fefb 2577
976ca316 2578 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2579}
2580
ff4c9fec 2581/* See declaration. */
f4dc4d17 2582
ff4c9fec 2583dwarf2_per_cu_data *
5989a64e 2584dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2585{
b76e467d 2586 if (index >= this->all_comp_units.size ())
ff4c9fec 2587 {
b76e467d 2588 index -= this->all_comp_units.size ();
b2bdb8cf 2589 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2590 return &this->all_type_units[index]->per_cu;
2591 }
f4dc4d17 2592
ff4c9fec
SM
2593 return this->all_comp_units[index];
2594}
f4dc4d17 2595
ff4c9fec 2596/* See declaration. */
2fdf6df6 2597
ff4c9fec 2598dwarf2_per_cu_data *
5989a64e 2599dwarf2_per_bfd::get_cu (int index)
1fd400ff 2600{
b76e467d 2601 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2602
ff4c9fec 2603 return this->all_comp_units[index];
f4dc4d17
DE
2604}
2605
ff4c9fec 2606/* See declaration. */
f4dc4d17 2607
ff4c9fec 2608signatured_type *
5989a64e 2609dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2610{
b2bdb8cf 2611 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2612
ff4c9fec 2613 return this->all_type_units[index];
1fd400ff
TT
2614}
2615
d3473f0c
TT
2616/* See read.h. */
2617
2618dwarf2_per_cu_data *
5989a64e 2619dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2620{
2621 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2622 result->per_bfd = this;
d3473f0c
TT
2623 result->index = m_num_psymtabs++;
2624 return result;
2625}
2626
2627/* See read.h. */
2628
2629signatured_type *
5989a64e 2630dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2631{
2632 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2633 result->per_cu.per_bfd = this;
d3473f0c
TT
2634 result->per_cu.index = m_num_psymtabs++;
2635 return result;
2636}
2637
168c9250 2638/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2639 obstack, and constructed with the specified field values. */
4b514bc8
JK
2640
2641static dwarf2_per_cu_data *
168c9250
SM
2642create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2643 struct dwarf2_section_info *section,
2644 int is_dwz,
2645 sect_offset sect_off, ULONGEST length)
4b514bc8 2646{
168c9250 2647 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2648 the_cu->sect_off = sect_off;
2649 the_cu->length = length;
4b514bc8 2650 the_cu->section = section;
168c9250 2651 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2652 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2653 the_cu->is_dwz = is_dwz;
2654 return the_cu;
2655}
2656
2ec9a5e0
TT
2657/* A helper for create_cus_from_index that handles a given list of
2658 CUs. */
2fdf6df6 2659
74a0d9f6 2660static void
168c9250 2661create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2662 const gdb_byte *cu_list, offset_type n_elements,
2663 struct dwarf2_section_info *section,
b76e467d 2664 int is_dwz)
9291a0cd 2665{
12359b5e 2666 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2667 {
74a0d9f6 2668 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2669
2670 sect_offset sect_off
2671 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2672 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2673 cu_list += 2 * 8;
2674
b76e467d 2675 dwarf2_per_cu_data *per_cu
168c9250
SM
2676 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2677 length);
2678 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2679 }
9291a0cd
TT
2680}
2681
2ec9a5e0 2682/* Read the CU list from the mapped index, and use it to create all
168c9250 2683 the CU objects for PER_BFD. */
2ec9a5e0 2684
74a0d9f6 2685static void
168c9250 2686create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2687 const gdb_byte *cu_list, offset_type cu_list_elements,
2688 const gdb_byte *dwz_list, offset_type dwz_elements)
2689{
168c9250
SM
2690 gdb_assert (per_bfd->all_comp_units.empty ());
2691 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2692
168c9250
SM
2693 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2694 &per_bfd->info, 0);
2ec9a5e0
TT
2695
2696 if (dwz_elements == 0)
74a0d9f6 2697 return;
2ec9a5e0 2698
168c9250
SM
2699 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2700 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2701 &dwz->info, 1);
2ec9a5e0
TT
2702}
2703
1fd400ff 2704/* Create the signatured type hash table from the index. */
673bfd45 2705
74a0d9f6 2706static void
12359b5e 2707create_signatured_type_table_from_index
168c9250
SM
2708 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2709 const gdb_byte *bytes, offset_type elements)
1fd400ff 2710{
168c9250
SM
2711 gdb_assert (per_bfd->all_type_units.empty ());
2712 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2713
298e9637 2714 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2715
12359b5e 2716 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2717 {
52dc124a 2718 struct signatured_type *sig_type;
9c541725 2719 ULONGEST signature;
1fd400ff 2720 void **slot;
9c541725 2721 cu_offset type_offset_in_tu;
1fd400ff 2722
74a0d9f6 2723 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2724 sect_offset sect_off
2725 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2726 type_offset_in_tu
2727 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2728 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2729 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2730 bytes += 3 * 8;
2731
168c9250 2732 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2733 sig_type->signature = signature;
9c541725 2734 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2735 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2736 sig_type->per_cu.section = section;
9c541725 2737 sig_type->per_cu.sect_off = sect_off;
52dc124a 2738 sig_type->per_cu.v.quick
168c9250 2739 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2740 struct dwarf2_per_cu_quick_data);
2741
b0b6a987 2742 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2743 *slot = sig_type;
1fd400ff 2744
168c9250 2745 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2746 }
2747
168c9250 2748 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2749}
2750
927aa2e7
JK
2751/* Create the signatured type hash table from .debug_names. */
2752
2753static void
2754create_signatured_type_table_from_debug_names
976ca316 2755 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2756 const mapped_debug_names &map,
2757 struct dwarf2_section_info *section,
2758 struct dwarf2_section_info *abbrev_section)
2759{
976ca316 2760 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2761
96b79293
TT
2762 section->read (objfile);
2763 abbrev_section->read (objfile);
927aa2e7 2764
976ca316
SM
2765 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2766 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2767
298e9637 2768 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2769
2770 for (uint32_t i = 0; i < map.tu_count; ++i)
2771 {
2772 struct signatured_type *sig_type;
927aa2e7 2773 void **slot;
927aa2e7
JK
2774
2775 sect_offset sect_off
2776 = (sect_offset) (extract_unsigned_integer
2777 (map.tu_table_reordered + i * map.offset_size,
2778 map.offset_size,
2779 map.dwarf5_byte_order));
2780
2781 comp_unit_head cu_header;
976ca316 2782 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2783 abbrev_section,
927aa2e7
JK
2784 section->buffer + to_underlying (sect_off),
2785 rcuh_kind::TYPE);
2786
976ca316 2787 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2788 sig_type->signature = cu_header.signature;
2789 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2790 sig_type->per_cu.is_debug_types = 1;
2791 sig_type->per_cu.section = section;
2792 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2793 sig_type->per_cu.v.quick
976ca316 2794 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2795 struct dwarf2_per_cu_quick_data);
2796
b0b6a987 2797 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2798 *slot = sig_type;
2799
976ca316 2800 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2801 }
2802
976ca316 2803 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2804}
2805
9291a0cd
TT
2806/* Read the address map data from the mapped index, and use it to
2807 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2808
9291a0cd 2809static void
976ca316 2810create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2811 struct mapped_index *index)
9291a0cd 2812{
976ca316 2813 struct objfile *objfile = per_objfile->objfile;
08feed99 2814 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2815 const gdb_byte *iter, *end;
9291a0cd 2816 struct addrmap *mutable_map;
9291a0cd
TT
2817 CORE_ADDR baseaddr;
2818
8268c778
PA
2819 auto_obstack temp_obstack;
2820
9291a0cd
TT
2821 mutable_map = addrmap_create_mutable (&temp_obstack);
2822
f00a2de2
PA
2823 iter = index->address_table.data ();
2824 end = iter + index->address_table.size ();
9291a0cd 2825
b3b3bada 2826 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2827
2828 while (iter < end)
2829 {
2830 ULONGEST hi, lo, cu_index;
2831 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2832 iter += 8;
2833 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2834 iter += 8;
2835 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2836 iter += 4;
f652bce2 2837
24a55014 2838 if (lo > hi)
f652bce2 2839 {
b98664d3 2840 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2841 hex_string (lo), hex_string (hi));
24a55014 2842 continue;
f652bce2 2843 }
24a55014 2844
976ca316 2845 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2846 {
b98664d3 2847 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2848 (unsigned) cu_index);
24a55014 2849 continue;
f652bce2 2850 }
24a55014 2851
79748972
TT
2852 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2853 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2854 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2855 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2856 }
2857
d320c2b5 2858 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2859 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2860}
2861
927aa2e7
JK
2862/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2863 populate the objfile's psymtabs_addrmap. */
2864
2865static void
976ca316 2866create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2867 struct dwarf2_section_info *section)
2868{
976ca316 2869 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2870 bfd *abfd = objfile->obfd;
08feed99 2871 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2872 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2873
2874 auto_obstack temp_obstack;
2875 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2876
2877 std::unordered_map<sect_offset,
2878 dwarf2_per_cu_data *,
2879 gdb::hash_enum<sect_offset>>
2880 debug_info_offset_to_per_cu;
976ca316 2881 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2882 {
927aa2e7
JK
2883 const auto insertpair
2884 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2885 if (!insertpair.second)
2886 {
2887 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2888 "debug_info_offset %s, ignoring .debug_aranges."),
2889 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2890 return;
2891 }
2892 }
2893
96b79293 2894 section->read (objfile);
927aa2e7
JK
2895
2896 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2897
2898 const gdb_byte *addr = section->buffer;
2899
2900 while (addr < section->buffer + section->size)
2901 {
2902 const gdb_byte *const entry_addr = addr;
2903 unsigned int bytes_read;
2904
2905 const LONGEST entry_length = read_initial_length (abfd, addr,
2906 &bytes_read);
2907 addr += bytes_read;
2908
2909 const gdb_byte *const entry_end = addr + entry_length;
2910 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2911 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2912 if (addr + entry_length > section->buffer + section->size)
2913 {
47e3f474 2914 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2915 "length %s exceeds section length %s, "
927aa2e7 2916 "ignoring .debug_aranges."),
47e3f474
TV
2917 objfile_name (objfile),
2918 plongest (entry_addr - section->buffer),
927aa2e7
JK
2919 plongest (bytes_read + entry_length),
2920 pulongest (section->size));
2921 return;
2922 }
2923
2924 /* The version number. */
2925 const uint16_t version = read_2_bytes (abfd, addr);
2926 addr += 2;
2927 if (version != 2)
2928 {
47e3f474 2929 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2930 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2931 objfile_name (objfile),
2932 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2933 return;
2934 }
2935
2936 const uint64_t debug_info_offset
2937 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2938 addr += offset_size;
2939 const auto per_cu_it
2940 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2941 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2942 {
47e3f474 2943 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2944 "debug_info_offset %s does not exists, "
2945 "ignoring .debug_aranges."),
47e3f474
TV
2946 objfile_name (objfile),
2947 plongest (entry_addr - section->buffer),
927aa2e7
JK
2948 pulongest (debug_info_offset));
2949 return;
2950 }
2951 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2952
2953 const uint8_t address_size = *addr++;
2954 if (address_size < 1 || address_size > 8)
2955 {
47e3f474 2956 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2957 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2958 objfile_name (objfile),
2959 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2960 return;
2961 }
2962
2963 const uint8_t segment_selector_size = *addr++;
2964 if (segment_selector_size != 0)
2965 {
47e3f474 2966 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2967 "segment_selector_size %u is not supported, "
2968 "ignoring .debug_aranges."),
47e3f474
TV
2969 objfile_name (objfile),
2970 plongest (entry_addr - section->buffer),
927aa2e7
JK
2971 segment_selector_size);
2972 return;
2973 }
2974
2975 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2976 size. It is undocumented by the DWARF standard but GCC does
2977 use it. */
927aa2e7
JK
2978 for (size_t padding = ((-(addr - section->buffer))
2979 & (2 * address_size - 1));
dda83cd7 2980 padding > 0; padding--)
927aa2e7
JK
2981 if (*addr++ != 0)
2982 {
47e3f474 2983 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2984 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2985 objfile_name (objfile),
2986 plongest (entry_addr - section->buffer));
927aa2e7
JK
2987 return;
2988 }
2989
2990 for (;;)
2991 {
2992 if (addr + 2 * address_size > entry_end)
2993 {
47e3f474 2994 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2995 "address list is not properly terminated, "
2996 "ignoring .debug_aranges."),
47e3f474
TV
2997 objfile_name (objfile),
2998 plongest (entry_addr - section->buffer));
927aa2e7
JK
2999 return;
3000 }
3001 ULONGEST start = extract_unsigned_integer (addr, address_size,
3002 dwarf5_byte_order);
3003 addr += address_size;
3004 ULONGEST length = extract_unsigned_integer (addr, address_size,
3005 dwarf5_byte_order);
3006 addr += address_size;
3007 if (start == 0 && length == 0)
3008 break;
976ca316 3009 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
3010 {
3011 /* Symbol was eliminated due to a COMDAT group. */
3012 continue;
3013 }
3014 ULONGEST end = start + length;
79748972
TT
3015 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3016 - baseaddr);
3017 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3018 - baseaddr);
927aa2e7
JK
3019 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3020 }
3021 }
3022
d320c2b5 3023 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3024 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3025}
3026
9291a0cd
TT
3027/* Find a slot in the mapped index INDEX for the object named NAME.
3028 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3029 constant pool and return true. If NAME cannot be found, return
3030 false. */
2fdf6df6 3031
109483d9 3032static bool
9291a0cd
TT
3033find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3034 offset_type **vec_out)
3035{
0cf03b49 3036 offset_type hash;
9291a0cd 3037 offset_type slot, step;
559a7a62 3038 int (*cmp) (const char *, const char *);
9291a0cd 3039
791afaa2 3040 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3041 if (current_language->la_language == language_cplus
45280282
IB
3042 || current_language->la_language == language_fortran
3043 || current_language->la_language == language_d)
0cf03b49
JK
3044 {
3045 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3046 not contain any. */
a8719064 3047
72998fb3 3048 if (strchr (name, '(') != NULL)
0cf03b49 3049 {
109483d9 3050 without_params = cp_remove_params (name);
0cf03b49 3051
72998fb3 3052 if (without_params != NULL)
791afaa2 3053 name = without_params.get ();
0cf03b49
JK
3054 }
3055 }
3056
559a7a62 3057 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3058 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3059 simulate our NAME being searched is also lowercased. */
3060 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 3061 && case_sensitivity == case_sensitive_off
559a7a62
JK
3062 ? 5 : index->version),
3063 name);
3064
f00a2de2
PA
3065 slot = hash & (index->symbol_table.size () - 1);
3066 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3067 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3068
3069 for (;;)
3070 {
9291a0cd 3071 const char *str;
f00a2de2
PA
3072
3073 const auto &bucket = index->symbol_table[slot];
3074 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3075 return false;
9291a0cd 3076
f00a2de2 3077 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3078 if (!cmp (name, str))
9291a0cd
TT
3079 {
3080 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3081 + MAYBE_SWAP (bucket.vec));
109483d9 3082 return true;
9291a0cd
TT
3083 }
3084
f00a2de2 3085 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3086 }
3087}
3088
4485a1c1
SM
3089/* A helper function that reads the .gdb_index from BUFFER and fills
3090 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3091 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3092 ok to use deprecated sections.
3093
3094 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3095 out parameters that are filled in with information about the CU and
3096 TU lists in the section.
3097
4485a1c1 3098 Returns true if all went well, false otherwise. */
2fdf6df6 3099
d33bc52e 3100static bool
3810f182 3101read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3102 bool deprecated_ok,
3103 gdb::array_view<const gdb_byte> buffer,
3104 struct mapped_index *map,
3105 const gdb_byte **cu_list,
3106 offset_type *cu_list_elements,
3107 const gdb_byte **types_list,
3108 offset_type *types_list_elements)
3109{
3110 const gdb_byte *addr = &buffer[0];
82430852 3111
9291a0cd 3112 /* Version check. */
4485a1c1 3113 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3114 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3115 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3116 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3117 indices. */
831adc1f 3118 if (version < 4)
481860b3
GB
3119 {
3120 static int warning_printed = 0;
3121 if (!warning_printed)
3122 {
3123 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3124 filename);
481860b3
GB
3125 warning_printed = 1;
3126 }
3127 return 0;
3128 }
3129 /* Index version 4 uses a different hash function than index version
3130 5 and later.
3131
3132 Versions earlier than 6 did not emit psymbols for inlined
3133 functions. Using these files will cause GDB not to be able to
3134 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3135 indices unless the user has done
3136 "set use-deprecated-index-sections on". */
2ec9a5e0 3137 if (version < 6 && !deprecated_ok)
481860b3
GB
3138 {
3139 static int warning_printed = 0;
3140 if (!warning_printed)
3141 {
e615022a
DE
3142 warning (_("\
3143Skipping deprecated .gdb_index section in %s.\n\
3144Do \"set use-deprecated-index-sections on\" before the file is read\n\
3145to use the section anyway."),
2ec9a5e0 3146 filename);
481860b3
GB
3147 warning_printed = 1;
3148 }
3149 return 0;
3150 }
796a7ff8 3151 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3152 of the TU (for symbols coming from TUs),
3153 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3154 Plus gold-generated indices can have duplicate entries for global symbols,
3155 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3156 These are just performance bugs, and we can't distinguish gdb-generated
3157 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3158
481860b3 3159 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3160 longer backward compatible. */
796a7ff8 3161 if (version > 8)
594e8718 3162 return 0;
9291a0cd 3163
559a7a62 3164 map->version = version;
9291a0cd 3165
4485a1c1 3166 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3167
4485a1c1 3168 int i = 0;
2ec9a5e0
TT
3169 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3170 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3171 / 8);
1fd400ff
TT
3172 ++i;
3173
2ec9a5e0
TT
3174 *types_list = addr + MAYBE_SWAP (metadata[i]);
3175 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3176 - MAYBE_SWAP (metadata[i]))
3177 / 8);
987d643c 3178 ++i;
1fd400ff 3179
f00a2de2
PA
3180 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3181 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3182 map->address_table
3183 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3184 ++i;
3185
f00a2de2
PA
3186 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3187 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3188 map->symbol_table
3189 = gdb::array_view<mapped_index::symbol_table_slot>
3190 ((mapped_index::symbol_table_slot *) symbol_table,
3191 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3192
f00a2de2 3193 ++i;
f9d83a0b 3194 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3195
2ec9a5e0
TT
3196 return 1;
3197}
3198
4485a1c1
SM
3199/* Callback types for dwarf2_read_gdb_index. */
3200
3201typedef gdb::function_view
5989a64e 3202 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3203 get_gdb_index_contents_ftype;
3204typedef gdb::function_view
3205 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3206 get_gdb_index_contents_dwz_ftype;
3207
927aa2e7 3208/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3209 elements of all the CUs and return 1. Otherwise, return 0. */
3210
3211static int
4485a1c1 3212dwarf2_read_gdb_index
976ca316 3213 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3214 get_gdb_index_contents_ftype get_gdb_index_contents,
3215 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3216{
2ec9a5e0
TT
3217 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3218 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3219 struct dwz_file *dwz;
976ca316 3220 struct objfile *objfile = per_objfile->objfile;
f8c41851 3221 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3222
4485a1c1 3223 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3224 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3225
3226 if (main_index_contents.empty ())
3227 return 0;
3228
3063847f 3229 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3230 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3231 use_deprecated_index_sections,
3232 main_index_contents, map.get (), &cu_list,
3233 &cu_list_elements, &types_list,
3234 &types_list_elements))
2ec9a5e0
TT
3235 return 0;
3236
0fefef59 3237 /* Don't use the index if it's empty. */
3063847f 3238 if (map->symbol_table.empty ())
0fefef59
DE
3239 return 0;
3240
2ec9a5e0
TT
3241 /* If there is a .dwz file, read it so we can get its CU list as
3242 well. */
f8c41851 3243 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3244 if (dwz != NULL)
2ec9a5e0 3245 {
2ec9a5e0
TT
3246 struct mapped_index dwz_map;
3247 const gdb_byte *dwz_types_ignore;
3248 offset_type dwz_types_elements_ignore;
3249
4485a1c1
SM
3250 gdb::array_view<const gdb_byte> dwz_index_content
3251 = get_gdb_index_contents_dwz (objfile, dwz);
3252
3253 if (dwz_index_content.empty ())
3254 return 0;
3255
3810f182 3256 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3257 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3258 &dwz_list, &dwz_list_elements,
3259 &dwz_types_ignore,
3260 &dwz_types_elements_ignore))
2ec9a5e0
TT
3261 {
3262 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3263 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3264 return 0;
3265 }
3266 }
3267
f8c41851
SM
3268 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3269 dwz_list_elements);
1fd400ff 3270
8b70b953
TT
3271 if (types_list_elements)
3272 {
8b70b953
TT
3273 /* We can only handle a single .debug_types when we have an
3274 index. */
f8c41851 3275 if (per_bfd->types.size () != 1)
8b70b953
TT
3276 return 0;
3277
f8c41851 3278 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3279
f8c41851 3280 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3281 types_list_elements);
8b70b953 3282 }
9291a0cd 3283
976ca316 3284 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3285
f8c41851
SM
3286 per_bfd->index_table = std::move (map);
3287 per_bfd->using_index = 1;
3288 per_bfd->quick_file_names_table =
3289 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3290
3291 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3292 objfiles using the same BFD. */
3293 gdb_assert (per_bfd->partial_symtabs == nullptr);
3294 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3295
3296 return 1;
3297}
3298
dee91e82 3299/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3300
dee91e82
DE
3301static void
3302dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3303 const gdb_byte *info_ptr,
3e225074 3304 struct die_info *comp_unit_die)
9291a0cd 3305{
dee91e82 3306 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3307 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3308 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3309 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3310 struct attribute *attr;
7b9f3c50
DE
3311 void **slot;
3312 struct quick_file_names *qfn;
9291a0cd 3313
0186c6a7
DE
3314 gdb_assert (! this_cu->is_debug_types);
3315
07261596
TT
3316 /* Our callers never want to match partial units -- instead they
3317 will match the enclosing full CU. */
3318 if (comp_unit_die->tag == DW_TAG_partial_unit)
3319 {
3320 this_cu->v.quick->no_file_data = 1;
3321 return;
3322 }
3323
0186c6a7 3324 lh_cu = this_cu;
7b9f3c50 3325 slot = NULL;
dee91e82 3326
fff8551c 3327 line_header_up lh;
9c541725 3328 sect_offset line_offset {};
fff8551c 3329
dee91e82 3330 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3331 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3332 {
7b9f3c50
DE
3333 struct quick_file_names find_entry;
3334
d4df075e 3335 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3336
3337 /* We may have already read in this line header (TU line header sharing).
3338 If we have we're done. */
094b34ac 3339 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3340 find_entry.hash.line_sect_off = line_offset;
976ca316 3341 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3342 &find_entry, INSERT);
3343 if (*slot != NULL)
3344 {
9a3c8263 3345 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3346 return;
7b9f3c50
DE
3347 }
3348
3019eac3 3349 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3350 }
3351 if (lh == NULL)
3352 {
094b34ac 3353 lh_cu->v.quick->no_file_data = 1;
dee91e82 3354 return;
9291a0cd
TT
3355 }
3356
976ca316 3357 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3358 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3359 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3360 gdb_assert (slot != NULL);
3361 *slot = qfn;
9291a0cd 3362
d721ba37 3363 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3364
aa391654
TT
3365 int offset = 0;
3366 if (strcmp (fnd.name, "<unknown>") != 0)
3367 ++offset;
3368
7ba99d21 3369 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3370 qfn->file_names =
976ca316 3371 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3372 qfn->num_file_names);
aa391654
TT
3373 if (offset != 0)
3374 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3375 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3376 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3377 fnd.comp_dir).release ();
7b9f3c50 3378 qfn->real_names = NULL;
9291a0cd 3379
094b34ac 3380 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3381}
3382
3383/* A helper for the "quick" functions which attempts to read the line
3384 table for THIS_CU. */
3385
3386static struct quick_file_names *
ab432490
SM
3387dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3388 dwarf2_per_objfile *per_objfile)
dee91e82 3389{
0186c6a7
DE
3390 /* This should never be called for TUs. */
3391 gdb_assert (! this_cu->is_debug_types);
3392 /* Nor type unit groups. */
197400e8 3393 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3394
dee91e82
DE
3395 if (this_cu->v.quick->file_names != NULL)
3396 return this_cu->v.quick->file_names;
3397 /* If we know there is no line data, no point in looking again. */
3398 if (this_cu->v.quick->no_file_data)
3399 return NULL;
3400
ab432490 3401 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3402 if (!reader.dummy_p)
3e225074 3403 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3404
3405 if (this_cu->v.quick->no_file_data)
3406 return NULL;
3407 return this_cu->v.quick->file_names;
9291a0cd
TT
3408}
3409
3410/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3411 real path for a given file name from the line table. */
2fdf6df6 3412
9291a0cd 3413static const char *
976ca316 3414dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3415 struct quick_file_names *qfn, int index)
9291a0cd 3416{
7b9f3c50 3417 if (qfn->real_names == NULL)
976ca316 3418 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3419 qfn->num_file_names, const char *);
9291a0cd 3420
7b9f3c50 3421 if (qfn->real_names[index] == NULL)
14278e1f 3422 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3423
7b9f3c50 3424 return qfn->real_names[index];
9291a0cd
TT
3425}
3426
3427static struct symtab *
3428dw2_find_last_source_symtab (struct objfile *objfile)
3429{
976ca316
SM
3430 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3431 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3432 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3433
43f3e411
DE
3434 if (cust == NULL)
3435 return NULL;
ed2dc618 3436
43f3e411 3437 return compunit_primary_filetab (cust);
9291a0cd
TT
3438}
3439
7b9f3c50
DE
3440/* Traversal function for dw2_forget_cached_source_info. */
3441
3442static int
3443dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3444{
7b9f3c50 3445 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3446
7b9f3c50 3447 if (file_data->real_names)
9291a0cd 3448 {
7b9f3c50 3449 int i;
9291a0cd 3450
7b9f3c50 3451 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3452 {
7b9f3c50
DE
3453 xfree ((void*) file_data->real_names[i]);
3454 file_data->real_names[i] = NULL;
9291a0cd
TT
3455 }
3456 }
7b9f3c50
DE
3457
3458 return 1;
3459}
3460
3461static void
3462dw2_forget_cached_source_info (struct objfile *objfile)
3463{
976ca316 3464 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3465
976ca316 3466 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3467 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3468}
3469
f8eba3c6
TT
3470/* Helper function for dw2_map_symtabs_matching_filename that expands
3471 the symtabs and calls the iterator. */
3472
3473static int
3474dw2_map_expand_apply (struct objfile *objfile,
3475 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3476 const char *name, const char *real_path,
14bc53a8 3477 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3478{
43f3e411 3479 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3480
3481 /* Don't visit already-expanded CUs. */
af758d11
SM
3482 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3483 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3484 return 0;
3485
3486 /* This may expand more than one symtab, and we want to iterate over
3487 all of them. */
97a1449a 3488 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3489
14bc53a8
PA
3490 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3491 last_made, callback);
f8eba3c6
TT
3492}
3493
3494/* Implementation of the map_symtabs_matching_filename method. */
3495
14bc53a8
PA
3496static bool
3497dw2_map_symtabs_matching_filename
3498 (struct objfile *objfile, const char *name, const char *real_path,
3499 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3500{
c011a4f4 3501 const char *name_basename = lbasename (name);
976ca316 3502 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3503
848e3e78
DE
3504 /* The rule is CUs specify all the files, including those used by
3505 any TU, so there's no need to scan TUs here. */
f4dc4d17 3506
976ca316 3507 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3508 {
3d7bb9d9 3509 /* We only need to look at symtabs not already expanded. */
976ca316 3510 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3511 continue;
3512
976ca316 3513 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3514 if (file_data == NULL)
9291a0cd
TT
3515 continue;
3516
b76e467d 3517 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3518 {
7b9f3c50 3519 const char *this_name = file_data->file_names[j];
da235a7c 3520 const char *this_real_name;
9291a0cd 3521
af529f8f 3522 if (compare_filenames_for_search (this_name, name))
9291a0cd 3523 {
f5b95b50 3524 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3525 callback))
3526 return true;
288e77a7 3527 continue;
4aac40c8 3528 }
9291a0cd 3529
c011a4f4
DE
3530 /* Before we invoke realpath, which can get expensive when many
3531 files are involved, do a quick comparison of the basenames. */
3532 if (! basenames_may_differ
3533 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3534 continue;
3535
976ca316 3536 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3537 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3538 {
da235a7c 3539 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3540 callback))
3541 return true;
288e77a7 3542 continue;
da235a7c 3543 }
9291a0cd 3544
da235a7c
JK
3545 if (real_path != NULL)
3546 {
af529f8f
JK
3547 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3548 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3549 if (this_real_name != NULL
af529f8f 3550 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3551 {
f5b95b50 3552 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3553 callback))
3554 return true;
288e77a7 3555 continue;
9291a0cd
TT
3556 }
3557 }
3558 }
3559 }
3560
14bc53a8 3561 return false;
9291a0cd
TT
3562}
3563
da51c347
DE
3564/* Struct used to manage iterating over all CUs looking for a symbol. */
3565
3566struct dw2_symtab_iterator
9291a0cd 3567{
ed2dc618 3568 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3569 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3570 /* If set, only look for symbols that match that block. Valid values are
3571 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3572 gdb::optional<block_enum> block_index;
da51c347
DE
3573 /* The kind of symbol we're looking for. */
3574 domain_enum domain;
3575 /* The list of CUs from the index entry of the symbol,
3576 or NULL if not found. */
3577 offset_type *vec;
3578 /* The next element in VEC to look at. */
3579 int next;
3580 /* The number of elements in VEC, or zero if there is no match. */
3581 int length;
8943b874
DE
3582 /* Have we seen a global version of the symbol?
3583 If so we can ignore all further global instances.
3584 This is to work around gold/15646, inefficient gold-generated
3585 indices. */
3586 int global_seen;
da51c347 3587};
9291a0cd 3588
e5f3ece2 3589/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3590
9291a0cd 3591static void
e5f3ece2
TV
3592dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3593 dwarf2_per_objfile *per_objfile,
3594 gdb::optional<block_enum> block_index,
3595 domain_enum domain)
da51c347 3596{
976ca316 3597 iter->per_objfile = per_objfile;
da51c347
DE
3598 iter->block_index = block_index;
3599 iter->domain = domain;
3600 iter->next = 0;
8943b874 3601 iter->global_seen = 0;
e5f3ece2
TV
3602 iter->vec = NULL;
3603 iter->length = 0;
3604}
da51c347 3605
e5f3ece2
TV
3606/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3607
3608static void
3609dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3610 dwarf2_per_objfile *per_objfile,
3611 gdb::optional<block_enum> block_index,
3612 domain_enum domain,
3613 const char *name)
3614{
3615 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3616
e5f3ece2 3617 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3618 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3619 if (index == NULL)
3620 return;
3621
3622 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3623 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3624}
3625
3626/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3627
3628static void
3629dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3630 dwarf2_per_objfile *per_objfile,
3631 gdb::optional<block_enum> block_index,
3632 domain_enum domain, offset_type namei)
3633{
3634 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3635
3636 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3637 /* index is NULL if OBJF_READNOW. */
3638 if (index == NULL)
3639 return;
3640
3641 gdb_assert (!index->symbol_name_slot_invalid (namei));
3642 const auto &bucket = index->symbol_table[namei];
3643
3644 iter->vec = (offset_type *) (index->constant_pool
3645 + MAYBE_SWAP (bucket.vec));
3646 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3647}
3648
3649/* Return the next matching CU or NULL if there are no more. */
3650
3651static struct dwarf2_per_cu_data *
3652dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3653{
976ca316 3654 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3655
da51c347
DE
3656 for ( ; iter->next < iter->length; ++iter->next)
3657 {
3658 offset_type cu_index_and_attrs =
3659 MAYBE_SWAP (iter->vec[iter->next + 1]);
3660 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3661 gdb_index_symbol_kind symbol_kind =
3662 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3663 /* Only check the symbol attributes if they're present.
3664 Indices prior to version 7 don't record them,
3665 and indices >= 7 may elide them for certain symbols
3666 (gold does this). */
3667 int attrs_valid =
976ca316 3668 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3669 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3670
3190f0c6 3671 /* Don't crash on bad data. */
976ca316
SM
3672 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3673 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3674 {
b98664d3 3675 complaint (_(".gdb_index entry has bad CU index"
976ca316 3676 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3677 continue;
3678 }
3679
976ca316 3680 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3681
da51c347 3682 /* Skip if already read in. */
976ca316 3683 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3684 continue;
3685
8943b874
DE
3686 /* Check static vs global. */
3687 if (attrs_valid)
3688 {
2b79f376
SM
3689 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3690
3691 if (iter->block_index.has_value ())
3692 {
3693 bool want_static = *iter->block_index == STATIC_BLOCK;
3694
3695 if (is_static != want_static)
3696 continue;
3697 }
3698
8943b874 3699 /* Work around gold/15646. */
f030440d
TV
3700 if (!is_static
3701 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3702 {
3703 if (iter->global_seen)
3704 continue;
3705
3706 iter->global_seen = 1;
3707 }
8943b874 3708 }
da51c347
DE
3709
3710 /* Only check the symbol's kind if it has one. */
3711 if (attrs_valid)
3712 {
3713 switch (iter->domain)
3714 {
3715 case VAR_DOMAIN:
3716 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3717 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3718 /* Some types are also in VAR_DOMAIN. */
3719 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3720 continue;
3721 break;
3722 case STRUCT_DOMAIN:
3723 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3724 continue;
3725 break;
3726 case LABEL_DOMAIN:
3727 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3728 continue;
3729 break;
59c35742
AB
3730 case MODULE_DOMAIN:
3731 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3732 continue;
3733 break;
da51c347
DE
3734 default:
3735 break;
3736 }
3737 }
3738
3739 ++iter->next;
3740 return per_cu;
3741 }
3742
3743 return NULL;
3744}
3745
43f3e411 3746static struct compunit_symtab *
c7f839cb 3747dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3748 const char *name, domain_enum domain)
9291a0cd 3749{
43f3e411 3750 struct compunit_symtab *stab_best = NULL;
976ca316 3751 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3752
b5ec771e
PA
3753 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3754
ed2dc618
SM
3755 struct dw2_symtab_iterator iter;
3756 struct dwarf2_per_cu_data *per_cu;
da51c347 3757
976ca316 3758 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3759
ed2dc618
SM
3760 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3761 {
3762 struct symbol *sym, *with_opaque = NULL;
97a1449a 3763 struct compunit_symtab *stab
976ca316 3764 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3765 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3766 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3767
ed2dc618
SM
3768 sym = block_find_symbol (block, name, domain,
3769 block_find_non_opaque_type_preferred,
3770 &with_opaque);
b2e2f908 3771
ed2dc618
SM
3772 /* Some caution must be observed with overloaded functions
3773 and methods, since the index will not contain any overload
3774 information (but NAME might contain it). */
da51c347 3775
ed2dc618
SM
3776 if (sym != NULL
3777 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3778 return stab;
3779 if (with_opaque != NULL
3780 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3781 stab_best = stab;
da51c347 3782
ed2dc618 3783 /* Keep looking through other CUs. */
9291a0cd 3784 }
9291a0cd 3785
da51c347 3786 return stab_best;
9291a0cd
TT
3787}
3788
3789static void
3790dw2_print_stats (struct objfile *objfile)
3791{
976ca316
SM
3792 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3793 int total = (per_objfile->per_bfd->all_comp_units.size ()
3794 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3795 int count = 0;
9291a0cd 3796
ed2dc618 3797 for (int i = 0; i < total; ++i)
9291a0cd 3798 {
976ca316 3799 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3800
976ca316 3801 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3802 ++count;
3803 }
e4a48d9d 3804 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3805 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3806}
3807
779bd270
DE
3808/* This dumps minimal information about the index.
3809 It is called via "mt print objfiles".
3810 One use is to verify .gdb_index has been loaded by the
3811 gdb.dwarf2/gdb-index.exp testcase. */
3812
9291a0cd
TT
3813static void
3814dw2_dump (struct objfile *objfile)
3815{
976ca316 3816 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3817
976ca316 3818 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3819 printf_filtered (".gdb_index:");
976ca316 3820 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3821 {
3822 printf_filtered (" version %d\n",
976ca316 3823 per_objfile->per_bfd->index_table->version);
779bd270
DE
3824 }
3825 else
3826 printf_filtered (" faked for \"readnow\"\n");
3827 printf_filtered ("\n");
9291a0cd
TT
3828}
3829
9291a0cd
TT
3830static void
3831dw2_expand_symtabs_for_function (struct objfile *objfile,
3832 const char *func_name)
3833{
976ca316 3834 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3835
ed2dc618
SM
3836 struct dw2_symtab_iterator iter;
3837 struct dwarf2_per_cu_data *per_cu;
da51c347 3838
976ca316 3839 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3840
ed2dc618 3841 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3842 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3843
9291a0cd
TT
3844}
3845
3846static void
3847dw2_expand_all_symtabs (struct objfile *objfile)
3848{
976ca316
SM
3849 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3850 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3851 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3852
ed2dc618 3853 for (int i = 0; i < total_units; ++i)
9291a0cd 3854 {
976ca316 3855 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3856
58f0c718
TT
3857 /* We don't want to directly expand a partial CU, because if we
3858 read it with the wrong language, then assertion failures can
3859 be triggered later on. See PR symtab/23010. So, tell
3860 dw2_instantiate_symtab to skip partial CUs -- any important
3861 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3862 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3863 }
3864}
3865
3866static void
652a8996
JK
3867dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3868 const char *fullname)
9291a0cd 3869{
976ca316 3870 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3871
3872 /* We don't need to consider type units here.
3873 This is only called for examining code, e.g. expand_line_sal.
3874 There can be an order of magnitude (or more) more type units
3875 than comp units, and we avoid them if we can. */
3876
976ca316 3877 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3878 {
3d7bb9d9 3879 /* We only need to look at symtabs not already expanded. */
976ca316 3880 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3881 continue;
3882
976ca316 3883 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3884 if (file_data == NULL)
9291a0cd
TT
3885 continue;
3886
b76e467d 3887 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3888 {
652a8996
JK
3889 const char *this_fullname = file_data->file_names[j];
3890
3891 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3892 {
976ca316 3893 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3894 break;
3895 }
3896 }
3897 }
3898}
3899
9a0bacfb
TV
3900static void
3901dw2_expand_symtabs_matching_symbol
3902 (mapped_index_base &index,
3903 const lookup_name_info &lookup_name_in,
3904 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3905 enum search_domain kind,
fcf23d5b
SM
3906 gdb::function_view<bool (offset_type)> match_callback,
3907 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3908
3909static void
3910dw2_expand_symtabs_matching_one
97a1449a
SM
3911 (dwarf2_per_cu_data *per_cu,
3912 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3913 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3914 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3915
9291a0cd 3916static void
199b4314
TT
3917dw2_map_matching_symbols
3918 (struct objfile *objfile,
b054970d 3919 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3920 int global,
3921 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3922 symbol_compare_ftype *ordered_compare)
9291a0cd 3923{
1aa98955 3924 /* Used for Ada. */
976ca316 3925 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3926
9a0bacfb
TV
3927 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3928
976ca316 3929 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3930 {
976ca316 3931 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3932
9a0bacfb
TV
3933 const char *match_name = name.ada ().lookup_name ().c_str ();
3934 auto matcher = [&] (const char *symname)
3935 {
3936 if (ordered_compare == nullptr)
3937 return true;
3938 return ordered_compare (symname, match_name) == 0;
3939 };
3940
3941 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3942 [&] (offset_type namei)
3943 {
3944 struct dw2_symtab_iterator iter;
3945 struct dwarf2_per_cu_data *per_cu;
3946
976ca316 3947 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3948 namei);
9a0bacfb 3949 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3950 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3951 nullptr);
9a0bacfb 3952 return true;
976ca316 3953 }, per_objfile);
9a0bacfb
TV
3954 }
3955 else
3956 {
3957 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3958 proceed assuming all symtabs have been read in. */
3959 }
1aa98955
TV
3960
3961 for (compunit_symtab *cust : objfile->compunits ())
3962 {
3963 const struct block *block;
3964
3965 if (cust == NULL)
3966 continue;
3967 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3968 if (!iterate_over_symbols_terminated (block, name,
3969 domain, callback))
3970 return;
3971 }
9291a0cd
TT
3972}
3973
e1ef7d7a
PA
3974/* Starting from a search name, return the string that finds the upper
3975 bound of all strings that start with SEARCH_NAME in a sorted name
3976 list. Returns the empty string to indicate that the upper bound is
3977 the end of the list. */
3978
3979static std::string
3980make_sort_after_prefix_name (const char *search_name)
3981{
3982 /* When looking to complete "func", we find the upper bound of all
3983 symbols that start with "func" by looking for where we'd insert
3984 the closest string that would follow "func" in lexicographical
3985 order. Usually, that's "func"-with-last-character-incremented,
3986 i.e. "fund". Mind non-ASCII characters, though. Usually those
3987 will be UTF-8 multi-byte sequences, but we can't be certain.
3988 Especially mind the 0xff character, which is a valid character in
3989 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3990 rule out compilers allowing it in identifiers. Note that
3991 conveniently, strcmp/strcasecmp are specified to compare
3992 characters interpreted as unsigned char. So what we do is treat
3993 the whole string as a base 256 number composed of a sequence of
3994 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3995 to 0, and carries 1 to the following more-significant position.
3996 If the very first character in SEARCH_NAME ends up incremented
3997 and carries/overflows, then the upper bound is the end of the
3998 list. The string after the empty string is also the empty
3999 string.
4000
4001 Some examples of this operation:
4002
4003 SEARCH_NAME => "+1" RESULT
4004
4005 "abc" => "abd"
4006 "ab\xff" => "ac"
4007 "\xff" "a" "\xff" => "\xff" "b"
4008 "\xff" => ""
4009 "\xff\xff" => ""
4010 "" => ""
4011
4012 Then, with these symbols for example:
4013
4014 func
4015 func1
4016 fund
4017
4018 completing "func" looks for symbols between "func" and
4019 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4020 which finds "func" and "func1", but not "fund".
4021
4022 And with:
4023
4024 funcÿ (Latin1 'ÿ' [0xff])
4025 funcÿ1
4026 fund
4027
4028 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4029 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4030
4031 And with:
4032
4033 ÿÿ (Latin1 'ÿ' [0xff])
4034 ÿÿ1
4035
4036 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4037 the end of the list.
4038 */
4039 std::string after = search_name;
4040 while (!after.empty () && (unsigned char) after.back () == 0xff)
4041 after.pop_back ();
4042 if (!after.empty ())
4043 after.back () = (unsigned char) after.back () + 1;
4044 return after;
4045}
4046
5c58de74 4047/* See declaration. */
61d96d7e 4048
5c58de74
PA
4049std::pair<std::vector<name_component>::const_iterator,
4050 std::vector<name_component>::const_iterator>
44ed8f3e 4051mapped_index_base::find_name_components_bounds
fcf23d5b
SM
4052 (const lookup_name_info &lookup_name_without_params, language lang,
4053 dwarf2_per_objfile *per_objfile) const
3f563c84 4054{
5c58de74
PA
4055 auto *name_cmp
4056 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4057
3b00ef10 4058 const char *lang_name
e0802d59 4059 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 4060
3f563c84
PA
4061 /* Comparison function object for lower_bound that matches against a
4062 given symbol name. */
4063 auto lookup_compare_lower = [&] (const name_component &elem,
4064 const char *name)
4065 {
fcf23d5b 4066 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4067 const char *elem_name = elem_qualified + elem.name_offset;
4068 return name_cmp (elem_name, name) < 0;
4069 };
4070
4071 /* Comparison function object for upper_bound that matches against a
4072 given symbol name. */
4073 auto lookup_compare_upper = [&] (const char *name,
4074 const name_component &elem)
4075 {
fcf23d5b 4076 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4077 const char *elem_name = elem_qualified + elem.name_offset;
4078 return name_cmp (name, elem_name) < 0;
4079 };
4080
5c58de74
PA
4081 auto begin = this->name_components.begin ();
4082 auto end = this->name_components.end ();
3f563c84
PA
4083
4084 /* Find the lower bound. */
4085 auto lower = [&] ()
4086 {
3b00ef10 4087 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4088 return begin;
4089 else
3b00ef10 4090 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4091 } ();
4092
4093 /* Find the upper bound. */
4094 auto upper = [&] ()
4095 {
5c58de74 4096 if (lookup_name_without_params.completion_mode ())
3f563c84 4097 {
e1ef7d7a
PA
4098 /* In completion mode, we want UPPER to point past all
4099 symbols names that have the same prefix. I.e., with
4100 these symbols, and completing "func":
4101
4102 function << lower bound
4103 function1
4104 other_function << upper bound
4105
4106 We find the upper bound by looking for the insertion
4107 point of "func"-with-last-character-incremented,
4108 i.e. "fund". */
3b00ef10 4109 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4110 if (after.empty ())
3f563c84 4111 return end;
e6b2f5ef
PA
4112 return std::lower_bound (lower, end, after.c_str (),
4113 lookup_compare_lower);
3f563c84
PA
4114 }
4115 else
3b00ef10 4116 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4117 } ();
4118
5c58de74
PA
4119 return {lower, upper};
4120}
4121
4122/* See declaration. */
4123
4124void
fcf23d5b 4125mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4126{
4127 if (!this->name_components.empty ())
4128 return;
4129
4130 this->name_components_casing = case_sensitivity;
4131 auto *name_cmp
4132 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4133
4134 /* The code below only knows how to break apart components of C++
4135 symbol names (and other languages that use '::' as
3b00ef10 4136 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4137 auto count = this->symbol_name_count ();
4138 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4139 {
44ed8f3e 4140 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4141 continue;
4142
fcf23d5b 4143 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4144
4145 /* Add each name component to the name component table. */
4146 unsigned int previous_len = 0;
3b00ef10
TT
4147
4148 if (strstr (name, "::") != nullptr)
4149 {
4150 for (unsigned int current_len = cp_find_first_component (name);
4151 name[current_len] != '\0';
4152 current_len += cp_find_first_component (name + current_len))
4153 {
4154 gdb_assert (name[current_len] == ':');
4155 this->name_components.push_back ({previous_len, idx});
4156 /* Skip the '::'. */
4157 current_len += 2;
4158 previous_len = current_len;
4159 }
4160 }
4161 else
5c58de74 4162 {
3b00ef10
TT
4163 /* Handle the Ada encoded (aka mangled) form here. */
4164 for (const char *iter = strstr (name, "__");
4165 iter != nullptr;
4166 iter = strstr (iter, "__"))
4167 {
4168 this->name_components.push_back ({previous_len, idx});
4169 iter += 2;
4170 previous_len = iter - name;
4171 }
5c58de74 4172 }
3b00ef10 4173
5c58de74
PA
4174 this->name_components.push_back ({previous_len, idx});
4175 }
4176
4177 /* Sort name_components elements by name. */
4178 auto name_comp_compare = [&] (const name_component &left,
4179 const name_component &right)
4180 {
fcf23d5b
SM
4181 const char *left_qualified
4182 = this->symbol_name_at (left.idx, per_objfile);
4183 const char *right_qualified
4184 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4185
4186 const char *left_name = left_qualified + left.name_offset;
4187 const char *right_name = right_qualified + right.name_offset;
4188
4189 return name_cmp (left_name, right_name) < 0;
4190 };
4191
4192 std::sort (this->name_components.begin (),
4193 this->name_components.end (),
4194 name_comp_compare);
4195}
4196
4197/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4198 mapped_index_base instead of the containing objfile. This is split
4199 to a separate function in order to be able to unit test the
4200 name_components matching using a mock mapped_index_base. For each
5c58de74 4201 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4202 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4203
4204static void
4205dw2_expand_symtabs_matching_symbol
44ed8f3e 4206 (mapped_index_base &index,
5c58de74
PA
4207 const lookup_name_info &lookup_name_in,
4208 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4209 enum search_domain kind,
fcf23d5b
SM
4210 gdb::function_view<bool (offset_type)> match_callback,
4211 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4212{
4213 lookup_name_info lookup_name_without_params
4214 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4215
4216 /* Build the symbol name component sorted vector, if we haven't
4217 yet. */
fcf23d5b 4218 index.build_name_components (per_objfile);
5c58de74 4219
3f563c84
PA
4220 /* The same symbol may appear more than once in the range though.
4221 E.g., if we're looking for symbols that complete "w", and we have
4222 a symbol named "w1::w2", we'll find the two name components for
4223 that same symbol in the range. To be sure we only call the
4224 callback once per symbol, we first collect the symbol name
4225 indexes that matched in a temporary vector and ignore
4226 duplicates. */
4227 std::vector<offset_type> matches;
3f563c84 4228
3b00ef10
TT
4229 struct name_and_matcher
4230 {
4231 symbol_name_matcher_ftype *matcher;
ecc6c606 4232 const char *name;
3b00ef10
TT
4233
4234 bool operator== (const name_and_matcher &other) const
3f563c84 4235 {
ecc6c606 4236 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4237 }
4238 };
4239
4240 /* A vector holding all the different symbol name matchers, for all
4241 languages. */
4242 std::vector<name_and_matcher> matchers;
4243
4244 for (int i = 0; i < nr_languages; i++)
4245 {
4246 enum language lang_e = (enum language) i;
4247
4248 const language_defn *lang = language_def (lang_e);
4249 symbol_name_matcher_ftype *name_matcher
c9debfb9 4250 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4251
3b00ef10 4252 name_and_matcher key {
dda83cd7 4253 name_matcher,
3b00ef10
TT
4254 lookup_name_without_params.language_lookup_name (lang_e)
4255 };
4256
4257 /* Don't insert the same comparison routine more than once.
4258 Note that we do this linear walk. This is not a problem in
4259 practice because the number of supported languages is
4260 low. */
4261 if (std::find (matchers.begin (), matchers.end (), key)
4262 != matchers.end ())
9291a0cd 4263 continue;
3b00ef10
TT
4264 matchers.push_back (std::move (key));
4265
4266 auto bounds
4267 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4268 lang_e, per_objfile);
3b00ef10
TT
4269
4270 /* Now for each symbol name in range, check to see if we have a name
4271 match, and if so, call the MATCH_CALLBACK callback. */
4272
4273 for (; bounds.first != bounds.second; ++bounds.first)
4274 {
fcf23d5b
SM
4275 const char *qualified
4276 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4277
4278 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4279 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4280 continue;
9291a0cd 4281
3b00ef10
TT
4282 matches.push_back (bounds.first->idx);
4283 }
3f563c84
PA
4284 }
4285
4286 std::sort (matches.begin (), matches.end ());
4287
4288 /* Finally call the callback, once per match. */
4289 ULONGEST prev = -1;
4290 for (offset_type idx : matches)
4291 {
4292 if (prev != idx)
4293 {
3b00ef10
TT
4294 if (!match_callback (idx))
4295 break;
3f563c84
PA
4296 prev = idx;
4297 }
4298 }
4299
4300 /* Above we use a type wider than idx's for 'prev', since 0 and
4301 (offset_type)-1 are both possible values. */
4302 static_assert (sizeof (prev) > sizeof (offset_type), "");
4303}
4304
c62446b1
PA
4305#if GDB_SELF_TEST
4306
4307namespace selftests { namespace dw2_expand_symtabs_matching {
4308
a3c5fafd
PA
4309/* A mock .gdb_index/.debug_names-like name index table, enough to
4310 exercise dw2_expand_symtabs_matching_symbol, which works with the
4311 mapped_index_base interface. Builds an index from the symbol list
4312 passed as parameter to the constructor. */
4313class mock_mapped_index : public mapped_index_base
c62446b1
PA
4314{
4315public:
a3c5fafd
PA
4316 mock_mapped_index (gdb::array_view<const char *> symbols)
4317 : m_symbol_table (symbols)
c62446b1
PA
4318 {}
4319
a3c5fafd 4320 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4321
a3c5fafd 4322 /* Return the number of names in the symbol table. */
632e107b 4323 size_t symbol_name_count () const override
c62446b1 4324 {
a3c5fafd 4325 return m_symbol_table.size ();
c62446b1
PA
4326 }
4327
a3c5fafd 4328 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4329 const char *symbol_name_at
4330 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4331 {
4332 return m_symbol_table[idx];
4333 }
c62446b1 4334
a3c5fafd
PA
4335private:
4336 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4337};
4338
4339/* Convenience function that converts a NULL pointer to a "<null>"
4340 string, to pass to print routines. */
4341
4342static const char *
4343string_or_null (const char *str)
4344{
4345 return str != NULL ? str : "<null>";
4346}
4347
4348/* Check if a lookup_name_info built from
4349 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4350 index. EXPECTED_LIST is the list of expected matches, in expected
4351 matching order. If no match expected, then an empty list is
4352 specified. Returns true on success. On failure prints a warning
4353 indicating the file:line that failed, and returns false. */
4354
4355static bool
4356check_match (const char *file, int line,
4357 mock_mapped_index &mock_index,
4358 const char *name, symbol_name_match_type match_type,
4359 bool completion_mode,
fcf23d5b
SM
4360 std::initializer_list<const char *> expected_list,
4361 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4362{
4363 lookup_name_info lookup_name (name, match_type, completion_mode);
4364
4365 bool matched = true;
4366
4367 auto mismatch = [&] (const char *expected_str,
4368 const char *got)
4369 {
4370 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4371 "expected=\"%s\", got=\"%s\"\n"),
4372 file, line,
4373 (match_type == symbol_name_match_type::FULL
4374 ? "FULL" : "WILD"),
4375 name, string_or_null (expected_str), string_or_null (got));
4376 matched = false;
4377 };
4378
4379 auto expected_it = expected_list.begin ();
4380 auto expected_end = expected_list.end ();
4381
a3c5fafd 4382 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4383 NULL, ALL_DOMAIN,
4384 [&] (offset_type idx)
4385 {
fcf23d5b 4386 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4387 const char *expected_str
4388 = expected_it == expected_end ? NULL : *expected_it++;
4389
4390 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4391 mismatch (expected_str, matched_name);
3b00ef10 4392 return true;
fcf23d5b 4393 }, per_objfile);
c62446b1
PA
4394
4395 const char *expected_str
4396 = expected_it == expected_end ? NULL : *expected_it++;
4397 if (expected_str != NULL)
4398 mismatch (expected_str, NULL);
4399
4400 return matched;
4401}
4402
4403/* The symbols added to the mock mapped_index for testing (in
4404 canonical form). */
4405static const char *test_symbols[] = {
4406 "function",
4407 "std::bar",
4408 "std::zfunction",
4409 "std::zfunction2",
4410 "w1::w2",
4411 "ns::foo<char*>",
4412 "ns::foo<int>",
4413 "ns::foo<long>",
a20714ff
PA
4414 "ns2::tmpl<int>::foo2",
4415 "(anonymous namespace)::A::B::C",
c62446b1 4416
e1ef7d7a
PA
4417 /* These are used to check that the increment-last-char in the
4418 matching algorithm for completion doesn't match "t1_fund" when
4419 completing "t1_func". */
4420 "t1_func",
4421 "t1_func1",
4422 "t1_fund",
4423 "t1_fund1",
4424
4425 /* A UTF-8 name with multi-byte sequences to make sure that
4426 cp-name-parser understands this as a single identifier ("função"
4427 is "function" in PT). */
4428 u8"u8função",
4429
4430 /* \377 (0xff) is Latin1 'ÿ'. */
4431 "yfunc\377",
4432
4433 /* \377 (0xff) is Latin1 'ÿ'. */
4434 "\377",
4435 "\377\377123",
4436
c62446b1
PA
4437 /* A name with all sorts of complications. Starts with "z" to make
4438 it easier for the completion tests below. */
4439#define Z_SYM_NAME \
4440 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4441 "::tuple<(anonymous namespace)::ui*, " \
4442 "std::default_delete<(anonymous namespace)::ui>, void>"
4443
4444 Z_SYM_NAME
4445};
4446
a3c5fafd
PA
4447/* Returns true if the mapped_index_base::find_name_component_bounds
4448 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4449 in completion mode. */
5c58de74
PA
4450
4451static bool
a3c5fafd 4452check_find_bounds_finds (mapped_index_base &index,
5c58de74 4453 const char *search_name,
fcf23d5b
SM
4454 gdb::array_view<const char *> expected_syms,
4455 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4456{
4457 lookup_name_info lookup_name (search_name,
4458 symbol_name_match_type::FULL, true);
4459
3b00ef10 4460 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4461 language_cplus,
4462 per_objfile);
5c58de74
PA
4463
4464 size_t distance = std::distance (bounds.first, bounds.second);
4465 if (distance != expected_syms.size ())
4466 return false;
4467
4468 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4469 {
4470 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4471 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4472 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4473 return false;
4474 }
4475
4476 return true;
4477}
4478
4479/* Test the lower-level mapped_index::find_name_component_bounds
4480 method. */
4481
c62446b1 4482static void
5c58de74
PA
4483test_mapped_index_find_name_component_bounds ()
4484{
4485 mock_mapped_index mock_index (test_symbols);
4486
fcf23d5b 4487 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4488
4489 /* Test the lower-level mapped_index::find_name_component_bounds
4490 method in completion mode. */
4491 {
4492 static const char *expected_syms[] = {
4493 "t1_func",
4494 "t1_func1",
5c58de74
PA
4495 };
4496
fcf23d5b
SM
4497 SELF_CHECK (check_find_bounds_finds
4498 (mock_index, "t1_func", expected_syms,
4499 NULL /* per_objfile */));
5c58de74
PA
4500 }
4501
4502 /* Check that the increment-last-char in the name matching algorithm
4503 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4504 {
4505 static const char *expected_syms1[] = {
4506 "\377",
4507 "\377\377123",
4508 };
fcf23d5b
SM
4509 SELF_CHECK (check_find_bounds_finds
4510 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4511
4512 static const char *expected_syms2[] = {
4513 "\377\377123",
4514 };
fcf23d5b
SM
4515 SELF_CHECK (check_find_bounds_finds
4516 (mock_index, "\377\377", expected_syms2,
4517 NULL /* per_objfile */));
5c58de74
PA
4518 }
4519}
4520
4521/* Test dw2_expand_symtabs_matching_symbol. */
4522
4523static void
4524test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4525{
4526 mock_mapped_index mock_index (test_symbols);
4527
4528 /* We let all tests run until the end even if some fails, for debug
4529 convenience. */
4530 bool any_mismatch = false;
4531
4532 /* Create the expected symbols list (an initializer_list). Needed
4533 because lists have commas, and we need to pass them to CHECK,
4534 which is a macro. */
4535#define EXPECT(...) { __VA_ARGS__ }
4536
4537 /* Wrapper for check_match that passes down the current
4538 __FILE__/__LINE__. */
4539#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4540 any_mismatch |= !check_match (__FILE__, __LINE__, \
4541 mock_index, \
4542 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4543 EXPECTED_LIST, NULL)
c62446b1
PA
4544
4545 /* Identity checks. */
4546 for (const char *sym : test_symbols)
4547 {
4548 /* Should be able to match all existing symbols. */
4549 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4550 EXPECT (sym));
4551
4552 /* Should be able to match all existing symbols with
4553 parameters. */
4554 std::string with_params = std::string (sym) + "(int)";
4555 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4556 EXPECT (sym));
4557
4558 /* Should be able to match all existing symbols with
4559 parameters and qualifiers. */
4560 with_params = std::string (sym) + " ( int ) const";
4561 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4562 EXPECT (sym));
4563
4564 /* This should really find sym, but cp-name-parser.y doesn't
4565 know about lvalue/rvalue qualifiers yet. */
4566 with_params = std::string (sym) + " ( int ) &&";
4567 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4568 {});
4569 }
4570
e1ef7d7a
PA
4571 /* Check that the name matching algorithm for completion doesn't get
4572 confused with Latin1 'ÿ' / 0xff. */
4573 {
4574 static const char str[] = "\377";
4575 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4576 EXPECT ("\377", "\377\377123"));
4577 }
4578
4579 /* Check that the increment-last-char in the matching algorithm for
4580 completion doesn't match "t1_fund" when completing "t1_func". */
4581 {
4582 static const char str[] = "t1_func";
4583 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4584 EXPECT ("t1_func", "t1_func1"));
4585 }
4586
c62446b1
PA
4587 /* Check that completion mode works at each prefix of the expected
4588 symbol name. */
4589 {
4590 static const char str[] = "function(int)";
4591 size_t len = strlen (str);
4592 std::string lookup;
4593
4594 for (size_t i = 1; i < len; i++)
4595 {
4596 lookup.assign (str, i);
4597 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4598 EXPECT ("function"));
4599 }
4600 }
4601
4602 /* While "w" is a prefix of both components, the match function
4603 should still only be called once. */
4604 {
4605 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4606 EXPECT ("w1::w2"));
a20714ff
PA
4607 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4608 EXPECT ("w1::w2"));
c62446b1
PA
4609 }
4610
4611 /* Same, with a "complicated" symbol. */
4612 {
4613 static const char str[] = Z_SYM_NAME;
4614 size_t len = strlen (str);
4615 std::string lookup;
4616
4617 for (size_t i = 1; i < len; i++)
4618 {
4619 lookup.assign (str, i);
4620 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4621 EXPECT (Z_SYM_NAME));
4622 }
4623 }
4624
4625 /* In FULL mode, an incomplete symbol doesn't match. */
4626 {
4627 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4628 {});
4629 }
4630
4631 /* A complete symbol with parameters matches any overload, since the
4632 index has no overload info. */
4633 {
4634 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4635 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4636 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4637 EXPECT ("std::zfunction", "std::zfunction2"));
4638 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4639 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4640 }
4641
4642 /* Check that whitespace is ignored appropriately. A symbol with a
4643 template argument list. */
4644 {
4645 static const char expected[] = "ns::foo<int>";
4646 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4647 EXPECT (expected));
a20714ff
PA
4648 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4649 EXPECT (expected));
c62446b1
PA
4650 }
4651
4652 /* Check that whitespace is ignored appropriately. A symbol with a
4653 template argument list that includes a pointer. */
4654 {
4655 static const char expected[] = "ns::foo<char*>";
4656 /* Try both completion and non-completion modes. */
4657 static const bool completion_mode[2] = {false, true};
4658 for (size_t i = 0; i < 2; i++)
4659 {
4660 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4661 completion_mode[i], EXPECT (expected));
a20714ff
PA
4662 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4663 completion_mode[i], EXPECT (expected));
c62446b1
PA
4664
4665 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4666 completion_mode[i], EXPECT (expected));
a20714ff
PA
4667 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4668 completion_mode[i], EXPECT (expected));
c62446b1
PA
4669 }
4670 }
4671
4672 {
4673 /* Check method qualifiers are ignored. */
4674 static const char expected[] = "ns::foo<char*>";
4675 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4676 symbol_name_match_type::FULL, true, EXPECT (expected));
4677 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4678 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4679 CHECK_MATCH ("foo < char * > ( int ) const",
4680 symbol_name_match_type::WILD, true, EXPECT (expected));
4681 CHECK_MATCH ("foo < char * > ( int ) &&",
4682 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4683 }
4684
4685 /* Test lookup names that don't match anything. */
4686 {
a20714ff
PA
4687 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4688 {});
4689
c62446b1
PA
4690 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4691 {});
4692 }
4693
a20714ff
PA
4694 /* Some wild matching tests, exercising "(anonymous namespace)",
4695 which should not be confused with a parameter list. */
4696 {
4697 static const char *syms[] = {
4698 "A::B::C",
4699 "B::C",
4700 "C",
4701 "A :: B :: C ( int )",
4702 "B :: C ( int )",
4703 "C ( int )",
4704 };
4705
4706 for (const char *s : syms)
4707 {
4708 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4709 EXPECT ("(anonymous namespace)::A::B::C"));
4710 }
4711 }
4712
4713 {
4714 static const char expected[] = "ns2::tmpl<int>::foo2";
4715 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4716 EXPECT (expected));
4717 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4718 EXPECT (expected));
4719 }
4720
c62446b1
PA
4721 SELF_CHECK (!any_mismatch);
4722
4723#undef EXPECT
4724#undef CHECK_MATCH
4725}
4726
5c58de74
PA
4727static void
4728run_test ()
4729{
4730 test_mapped_index_find_name_component_bounds ();
4731 test_dw2_expand_symtabs_matching_symbol ();
4732}
4733
c62446b1
PA
4734}} // namespace selftests::dw2_expand_symtabs_matching
4735
4736#endif /* GDB_SELF_TEST */
4737
4b514bc8
JK
4738/* If FILE_MATCHER is NULL or if PER_CU has
4739 dwarf2_per_cu_quick_data::MARK set (see
4740 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4741 EXPANSION_NOTIFY on it. */
4742
4743static void
4744dw2_expand_symtabs_matching_one
97a1449a
SM
4745 (dwarf2_per_cu_data *per_cu,
4746 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4747 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4748 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4749{
4750 if (file_matcher == NULL || per_cu->v.quick->mark)
4751 {
af758d11 4752 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4753
97a1449a
SM
4754 compunit_symtab *symtab
4755 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4756 gdb_assert (symtab != nullptr);
4b514bc8 4757
af758d11
SM
4758 if (expansion_notify != NULL && symtab_was_null)
4759 expansion_notify (symtab);
4b514bc8
JK
4760 }
4761}
4762
3f563c84
PA
4763/* Helper for dw2_expand_matching symtabs. Called on each symbol
4764 matched, to expand corresponding CUs that were marked. IDX is the
4765 index of the symbol name that matched. */
4766
4767static void
4768dw2_expand_marked_cus
976ca316 4769 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4770 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4771 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4772 search_domain kind)
4773{
3f563c84
PA
4774 offset_type *vec, vec_len, vec_idx;
4775 bool global_seen = false;
976ca316 4776 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4777
61920122 4778 vec = (offset_type *) (index.constant_pool
f00a2de2 4779 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4780 vec_len = MAYBE_SWAP (vec[0]);
4781 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4782 {
61920122
PA
4783 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4784 /* This value is only valid for index versions >= 7. */
4785 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4786 gdb_index_symbol_kind symbol_kind =
4787 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4788 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4789 /* Only check the symbol attributes if they're present.
4790 Indices prior to version 7 don't record them,
4791 and indices >= 7 may elide them for certain symbols
4792 (gold does this). */
4793 int attrs_valid =
4794 (index.version >= 7
4795 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4796
4797 /* Work around gold/15646. */
f030440d
TV
4798 if (attrs_valid
4799 && !is_static
4800 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4801 {
f030440d 4802 if (global_seen)
61920122 4803 continue;
f030440d
TV
4804
4805 global_seen = true;
61920122 4806 }
3190f0c6 4807
61920122
PA
4808 /* Only check the symbol's kind if it has one. */
4809 if (attrs_valid)
4810 {
4811 switch (kind)
8943b874 4812 {
61920122
PA
4813 case VARIABLES_DOMAIN:
4814 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4815 continue;
4816 break;
4817 case FUNCTIONS_DOMAIN:
4818 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4819 continue;
61920122
PA
4820 break;
4821 case TYPES_DOMAIN:
4822 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4823 continue;
4824 break;
59c35742
AB
4825 case MODULES_DOMAIN:
4826 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4827 continue;
4828 break;
61920122
PA
4829 default:
4830 break;
8943b874 4831 }
61920122 4832 }
8943b874 4833
61920122 4834 /* Don't crash on bad data. */
976ca316
SM
4835 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4836 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4837 {
b98664d3 4838 complaint (_(".gdb_index entry has bad CU index"
976ca316 4839 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4840 continue;
4841 }
4842
976ca316
SM
4843 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4844 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4845 expansion_notify);
61920122
PA
4846 }
4847}
4848
4b514bc8
JK
4849/* If FILE_MATCHER is non-NULL, set all the
4850 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4851 that match FILE_MATCHER. */
4852
61920122 4853static void
4b514bc8 4854dw_expand_symtabs_matching_file_matcher
976ca316 4855 (dwarf2_per_objfile *per_objfile,
ed2dc618 4856 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4857{
4b514bc8 4858 if (file_matcher == NULL)
61920122
PA
4859 return;
4860
4b514bc8
JK
4861 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4862 htab_eq_pointer,
4863 NULL, xcalloc, xfree));
4864 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4865 htab_eq_pointer,
4866 NULL, xcalloc, xfree));
61920122 4867
4b514bc8
JK
4868 /* The rule is CUs specify all the files, including those used by
4869 any TU, so there's no need to scan TUs here. */
61920122 4870
976ca316 4871 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4872 {
927aa2e7
JK
4873 QUIT;
4874
4875 per_cu->v.quick->mark = 0;
4876
4877 /* We only need to look at symtabs not already expanded. */
976ca316 4878 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4879 continue;
4880
976ca316 4881 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4882 if (file_data == NULL)
4883 continue;
4884
4885 if (htab_find (visited_not_found.get (), file_data) != NULL)
4886 continue;
4887 else if (htab_find (visited_found.get (), file_data) != NULL)
4888 {
4889 per_cu->v.quick->mark = 1;
4890 continue;
4891 }
4892
b76e467d 4893 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4894 {
4895 const char *this_real_name;
4896
4897 if (file_matcher (file_data->file_names[j], false))
4898 {
4899 per_cu->v.quick->mark = 1;
4900 break;
4901 }
4902
4903 /* Before we invoke realpath, which can get expensive when many
4904 files are involved, do a quick comparison of the basenames. */
4905 if (!basenames_may_differ
4906 && !file_matcher (lbasename (file_data->file_names[j]),
4907 true))
4908 continue;
4909
976ca316 4910 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4911 if (file_matcher (this_real_name, false))
4912 {
4913 per_cu->v.quick->mark = 1;
4914 break;
4915 }
4916 }
4917
b76e467d
SM
4918 void **slot = htab_find_slot (per_cu->v.quick->mark
4919 ? visited_found.get ()
4920 : visited_not_found.get (),
4921 file_data, INSERT);
927aa2e7
JK
4922 *slot = file_data;
4923 }
4924}
4925
4926static void
4927dw2_expand_symtabs_matching
4928 (struct objfile *objfile,
4929 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4930 const lookup_name_info *lookup_name,
927aa2e7
JK
4931 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4932 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4933 enum search_domain kind)
4934{
976ca316 4935 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4936
4937 /* index_table is NULL if OBJF_READNOW. */
976ca316 4938 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4939 return;
4940
976ca316 4941 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4942
c1a66c06
TV
4943 if (symbol_matcher == NULL && lookup_name == NULL)
4944 {
976ca316 4945 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4946 {
4947 QUIT;
4948
976ca316 4949 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4950 file_matcher, expansion_notify);
c1a66c06
TV
4951 }
4952 return;
4953 }
4954
976ca316 4955 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4956
c1a66c06 4957 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4958 symbol_matcher,
4959 kind, [&] (offset_type idx)
4960 {
976ca316
SM
4961 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4962 kind);
3b00ef10 4963 return true;
976ca316 4964 }, per_objfile);
927aa2e7
JK
4965}
4966
4967/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4968 symtab. */
4969
4970static struct compunit_symtab *
4971recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4972 CORE_ADDR pc)
4973{
4974 int i;
4975
4976 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4977 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4978 return cust;
4979
4980 if (cust->includes == NULL)
4981 return NULL;
4982
4983 for (i = 0; cust->includes[i]; ++i)
4984 {
4985 struct compunit_symtab *s = cust->includes[i];
4986
4987 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4988 if (s != NULL)
4989 return s;
4990 }
4991
4992 return NULL;
4993}
4994
4995static struct compunit_symtab *
4996dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4997 struct bound_minimal_symbol msymbol,
4998 CORE_ADDR pc,
4999 struct obj_section *section,
5000 int warn_if_readin)
5001{
5002 struct dwarf2_per_cu_data *data;
5003 struct compunit_symtab *result;
5004
d320c2b5 5005 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
5006 return NULL;
5007
b3b3bada 5008 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5009 data = (struct dwarf2_per_cu_data *) addrmap_find
5010 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5011 if (!data)
5012 return NULL;
5013
af758d11
SM
5014 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5015 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 5016 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 5017 paddress (objfile->arch (), pc));
927aa2e7 5018
97a1449a
SM
5019 result = recursively_find_pc_sect_compunit_symtab
5020 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5021
927aa2e7
JK
5022 gdb_assert (result != NULL);
5023 return result;
5024}
5025
5026static void
5027dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5028 void *data, int need_fullname)
5029{
976ca316 5030 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 5031
976ca316 5032 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 5033 {
976ca316 5034 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
5035
5036 htab_up visited (htab_create_alloc (10,
5037 htab_hash_pointer, htab_eq_pointer,
5038 NULL, xcalloc, xfree));
5039
5040 /* The rule is CUs specify all the files, including those used
5041 by any TU, so there's no need to scan TUs here. We can
5042 ignore file names coming from already-expanded CUs. */
5043
976ca316 5044 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5045 {
976ca316 5046 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5047 {
5048 void **slot = htab_find_slot (visited.get (),
5049 per_cu->v.quick->file_names,
5050 INSERT);
5051
5052 *slot = per_cu->v.quick->file_names;
5053 }
5054 }
5055
976ca316 5056 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5057 {
927aa2e7 5058 /* We only need to look at symtabs not already expanded. */
976ca316 5059 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5060 continue;
5061
ab432490 5062 quick_file_names *file_data
976ca316 5063 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
5064 if (file_data == NULL)
5065 continue;
5066
b76e467d 5067 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5068 if (*slot)
5069 {
5070 /* Already visited. */
5071 continue;
5072 }
5073 *slot = file_data;
5074
5075 for (int j = 0; j < file_data->num_file_names; ++j)
5076 {
5077 const char *filename = file_data->file_names[j];
976ca316 5078 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5079 }
5080 }
5081 }
5082
976ca316 5083 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5084 {
5085 gdb::unique_xmalloc_ptr<char> this_real_name;
5086
5087 if (need_fullname)
5088 this_real_name = gdb_realpath (filename);
5089 (*fun) (filename, this_real_name.get (), data);
5090 });
5091}
5092
5093static int
5094dw2_has_symbols (struct objfile *objfile)
5095{
5096 return 1;
5097}
5098
5099const struct quick_symbol_functions dwarf2_gdb_index_functions =
5100{
5101 dw2_has_symbols,
5102 dw2_find_last_source_symtab,
5103 dw2_forget_cached_source_info,
5104 dw2_map_symtabs_matching_filename,
5105 dw2_lookup_symbol,
d3214198 5106 NULL,
927aa2e7
JK
5107 dw2_print_stats,
5108 dw2_dump,
927aa2e7
JK
5109 dw2_expand_symtabs_for_function,
5110 dw2_expand_all_symtabs,
5111 dw2_expand_symtabs_with_fullname,
5112 dw2_map_matching_symbols,
5113 dw2_expand_symtabs_matching,
5114 dw2_find_pc_sect_compunit_symtab,
5115 NULL,
5116 dw2_map_symbol_filenames
5117};
5118
5119/* DWARF-5 debug_names reader. */
5120
5121/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5122static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5123
5124/* A helper function that reads the .debug_names section in SECTION
5125 and fills in MAP. FILENAME is the name of the file containing the
5126 section; it is used for error reporting.
5127
5128 Returns true if all went well, false otherwise. */
5129
5130static bool
5131read_debug_names_from_section (struct objfile *objfile,
5132 const char *filename,
5133 struct dwarf2_section_info *section,
5134 mapped_debug_names &map)
5135{
96b79293 5136 if (section->empty ())
927aa2e7
JK
5137 return false;
5138
5139 /* Older elfutils strip versions could keep the section in the main
5140 executable while splitting it for the separate debug info file. */
96b79293 5141 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5142 return false;
5143
96b79293 5144 section->read (objfile);
927aa2e7 5145
08feed99 5146 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5147
5148 const gdb_byte *addr = section->buffer;
5149
96b79293 5150 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5151
5152 unsigned int bytes_read;
5153 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5154 addr += bytes_read;
5155
5156 map.dwarf5_is_dwarf64 = bytes_read != 4;
5157 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5158 if (bytes_read + length != section->size)
5159 {
5160 /* There may be multiple per-CU indices. */
5161 warning (_("Section .debug_names in %s length %s does not match "
5162 "section length %s, ignoring .debug_names."),
5163 filename, plongest (bytes_read + length),
5164 pulongest (section->size));
5165 return false;
5166 }
5167
5168 /* The version number. */
5169 uint16_t version = read_2_bytes (abfd, addr);
5170 addr += 2;
5171 if (version != 5)
5172 {
5173 warning (_("Section .debug_names in %s has unsupported version %d, "
5174 "ignoring .debug_names."),
5175 filename, version);
5176 return false;
5177 }
5178
5179 /* Padding. */
5180 uint16_t padding = read_2_bytes (abfd, addr);
5181 addr += 2;
5182 if (padding != 0)
5183 {
5184 warning (_("Section .debug_names in %s has unsupported padding %d, "
5185 "ignoring .debug_names."),
5186 filename, padding);
5187 return false;
5188 }
5189
5190 /* comp_unit_count - The number of CUs in the CU list. */
5191 map.cu_count = read_4_bytes (abfd, addr);
5192 addr += 4;
5193
5194 /* local_type_unit_count - The number of TUs in the local TU
5195 list. */
5196 map.tu_count = read_4_bytes (abfd, addr);
5197 addr += 4;
5198
5199 /* foreign_type_unit_count - The number of TUs in the foreign TU
5200 list. */
5201 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5202 addr += 4;
5203 if (foreign_tu_count != 0)
5204 {
5205 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5206 "ignoring .debug_names."),
5207 filename, static_cast<unsigned long> (foreign_tu_count));
5208 return false;
5209 }
5210
5211 /* bucket_count - The number of hash buckets in the hash lookup
5212 table. */
5213 map.bucket_count = read_4_bytes (abfd, addr);
5214 addr += 4;
5215
5216 /* name_count - The number of unique names in the index. */
5217 map.name_count = read_4_bytes (abfd, addr);
5218 addr += 4;
5219
5220 /* abbrev_table_size - The size in bytes of the abbreviations
5221 table. */
5222 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5223 addr += 4;
5224
5225 /* augmentation_string_size - The size in bytes of the augmentation
5226 string. This value is rounded up to a multiple of 4. */
5227 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5228 addr += 4;
5229 map.augmentation_is_gdb = ((augmentation_string_size
5230 == sizeof (dwarf5_augmentation))
5231 && memcmp (addr, dwarf5_augmentation,
5232 sizeof (dwarf5_augmentation)) == 0);
5233 augmentation_string_size += (-augmentation_string_size) & 3;
5234 addr += augmentation_string_size;
5235
5236 /* List of CUs */
5237 map.cu_table_reordered = addr;
5238 addr += map.cu_count * map.offset_size;
5239
5240 /* List of Local TUs */
5241 map.tu_table_reordered = addr;
5242 addr += map.tu_count * map.offset_size;
5243
5244 /* Hash Lookup Table */
5245 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5246 addr += map.bucket_count * 4;
5247 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5248 addr += map.name_count * 4;
5249
5250 /* Name Table */
5251 map.name_table_string_offs_reordered = addr;
5252 addr += map.name_count * map.offset_size;
5253 map.name_table_entry_offs_reordered = addr;
5254 addr += map.name_count * map.offset_size;
5255
5256 const gdb_byte *abbrev_table_start = addr;
5257 for (;;)
5258 {
927aa2e7
JK
5259 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5260 addr += bytes_read;
5261 if (index_num == 0)
5262 break;
5263
5264 const auto insertpair
5265 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5266 if (!insertpair.second)
5267 {
5268 warning (_("Section .debug_names in %s has duplicate index %s, "
5269 "ignoring .debug_names."),
5270 filename, pulongest (index_num));
5271 return false;
5272 }
5273 mapped_debug_names::index_val &indexval = insertpair.first->second;
5274 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5275 addr += bytes_read;
5276
5277 for (;;)
5278 {
5279 mapped_debug_names::index_val::attr attr;
5280 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5281 addr += bytes_read;
5282 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5283 addr += bytes_read;
5284 if (attr.form == DW_FORM_implicit_const)
5285 {
5286 attr.implicit_const = read_signed_leb128 (abfd, addr,
5287 &bytes_read);
5288 addr += bytes_read;
5289 }
5290 if (attr.dw_idx == 0 && attr.form == 0)
5291 break;
5292 indexval.attr_vec.push_back (std::move (attr));
5293 }
5294 }
5295 if (addr != abbrev_table_start + abbrev_table_size)
5296 {
5297 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5298 "of size %s vs. written as %u, ignoring .debug_names."),
5299 filename, plongest (addr - abbrev_table_start),
5300 abbrev_table_size);
927aa2e7
JK
5301 return false;
5302 }
5303 map.entry_pool = addr;
5304
5305 return true;
5306}
5307
5308/* A helper for create_cus_from_debug_names that handles the MAP's CU
5309 list. */
5310
5311static void
168c9250 5312create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5313 const mapped_debug_names &map,
5314 dwarf2_section_info &section,
b76e467d 5315 bool is_dwz)
927aa2e7 5316{
3ee6bb11
TV
5317 if (!map.augmentation_is_gdb)
5318 {
5319 for (uint32_t i = 0; i < map.cu_count; ++i)
5320 {
5321 sect_offset sect_off
5322 = (sect_offset) (extract_unsigned_integer
5323 (map.cu_table_reordered + i * map.offset_size,
5324 map.offset_size,
5325 map.dwarf5_byte_order));
5326 /* We don't know the length of the CU, because the CU list in a
5327 .debug_names index can be incomplete, so we can't use the start of
5328 the next CU as end of this CU. We create the CUs here with length 0,
5329 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5330 dwarf2_per_cu_data *per_cu
168c9250
SM
5331 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5332 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5333 }
5334 }
5335
927aa2e7
JK
5336 sect_offset sect_off_prev;
5337 for (uint32_t i = 0; i <= map.cu_count; ++i)
5338 {
5339 sect_offset sect_off_next;
5340 if (i < map.cu_count)
5341 {
5342 sect_off_next
5343 = (sect_offset) (extract_unsigned_integer
5344 (map.cu_table_reordered + i * map.offset_size,
5345 map.offset_size,
5346 map.dwarf5_byte_order));
5347 }
5348 else
5349 sect_off_next = (sect_offset) section.size;
5350 if (i >= 1)
5351 {
5352 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5353 dwarf2_per_cu_data *per_cu
168c9250 5354 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5355 sect_off_prev, length);
168c9250 5356 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5357 }
5358 sect_off_prev = sect_off_next;
5359 }
5360}
5361
5362/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5363 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5364
5365static void
168c9250 5366create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5367 const mapped_debug_names &map,
5368 const mapped_debug_names &dwz_map)
5369{
168c9250
SM
5370 gdb_assert (per_bfd->all_comp_units.empty ());
5371 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5372
168c9250 5373 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5374 false /* is_dwz */);
927aa2e7
JK
5375
5376 if (dwz_map.cu_count == 0)
5377 return;
5378
168c9250
SM
5379 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5380 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5381 true /* is_dwz */);
927aa2e7
JK
5382}
5383
5384/* Read .debug_names. If everything went ok, initialize the "quick"
5385 elements of all the CUs and return true. Otherwise, return false. */
5386
5387static bool
976ca316 5388dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5389{
fcf23d5b
SM
5390 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5391 mapped_debug_names dwz_map;
976ca316 5392 struct objfile *objfile = per_objfile->objfile;
f8c41851 5393 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5394
5395 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5396 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5397 return false;
5398
5399 /* Don't use the index if it's empty. */
22ca247e 5400 if (map->name_count == 0)
927aa2e7
JK
5401 return false;
5402
5403 /* If there is a .dwz file, read it so we can get its CU list as
5404 well. */
f8c41851 5405 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5406 if (dwz != NULL)
5407 {
5408 if (!read_debug_names_from_section (objfile,
00f93c44 5409 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5410 &dwz->debug_names, dwz_map))
5411 {
5412 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5413 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5414 return false;
5415 }
5416 }
5417
f8c41851 5418 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5419
22ca247e 5420 if (map->tu_count != 0)
927aa2e7
JK
5421 {
5422 /* We can only handle a single .debug_types when we have an
5423 index. */
f8c41851 5424 if (per_bfd->types.size () != 1)
927aa2e7
JK
5425 return false;
5426
f8c41851 5427 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5428
5429 create_signatured_type_table_from_debug_names
f8c41851 5430 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5431 }
5432
f8c41851 5433 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5434
f8c41851
SM
5435 per_bfd->debug_names_table = std::move (map);
5436 per_bfd->using_index = 1;
5437 per_bfd->quick_file_names_table =
976ca316 5438 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5439
f8c41851
SM
5440 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5441 objfiles using the same BFD. */
5442 gdb_assert (per_bfd->partial_symtabs == nullptr);
5443 per_bfd->partial_symtabs = objfile->partial_symtabs;
5444
927aa2e7
JK
5445 return true;
5446}
5447
927aa2e7
JK
5448/* Type used to manage iterating over all CUs looking for a symbol for
5449 .debug_names. */
5450
5451class dw2_debug_names_iterator
5452{
5453public:
927aa2e7 5454 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5455 gdb::optional<block_enum> block_index,
5456 domain_enum domain,
fcf23d5b 5457 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5458 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5459 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5460 m_per_objfile (per_objfile)
927aa2e7
JK
5461 {}
5462
5463 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5464 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5465 : m_map (map),
5466 m_search (search),
fcf23d5b
SM
5467 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5468 m_per_objfile (per_objfile)
927aa2e7
JK
5469 {}
5470
3b00ef10
TT
5471 dw2_debug_names_iterator (const mapped_debug_names &map,
5472 block_enum block_index, domain_enum domain,
fcf23d5b 5473 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5474 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5475 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5476 m_per_objfile (per_objfile)
3b00ef10
TT
5477 {}
5478
927aa2e7
JK
5479 /* Return the next matching CU or NULL if there are no more. */
5480 dwarf2_per_cu_data *next ();
5481
5482private:
5483 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5484 const char *name,
5485 dwarf2_per_objfile *per_objfile);
927aa2e7 5486 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5487 uint32_t namei,
5488 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5489
5490 /* The internalized form of .debug_names. */
5491 const mapped_debug_names &m_map;
5492
2b79f376
SM
5493 /* If set, only look for symbols that match that block. Valid values are
5494 GLOBAL_BLOCK and STATIC_BLOCK. */
5495 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5496
5497 /* The kind of symbol we're looking for. */
5498 const domain_enum m_domain = UNDEF_DOMAIN;
5499 const search_domain m_search = ALL_DOMAIN;
5500
5501 /* The list of CUs from the index entry of the symbol, or NULL if
5502 not found. */
5503 const gdb_byte *m_addr;
fcf23d5b
SM
5504
5505 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5506};
5507
5508const char *
fcf23d5b 5509mapped_debug_names::namei_to_name
976ca316 5510 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5511{
5512 const ULONGEST namei_string_offs
5513 = extract_unsigned_integer ((name_table_string_offs_reordered
5514 + namei * offset_size),
5515 offset_size,
5516 dwarf5_byte_order);
976ca316 5517 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5518}
5519
5520/* Find a slot in .debug_names for the object named NAME. If NAME is
5521 found, return pointer to its pool data. If NAME cannot be found,
5522 return NULL. */
5523
5524const gdb_byte *
5525dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5526 (const mapped_debug_names &map, const char *name,
5527 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5528{
5529 int (*cmp) (const char *, const char *);
5530
54ee4252 5531 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5532 if (current_language->la_language == language_cplus
5533 || current_language->la_language == language_fortran
5534 || current_language->la_language == language_d)
5535 {
5536 /* NAME is already canonical. Drop any qualifiers as
5537 .debug_names does not contain any. */
5538
5539 if (strchr (name, '(') != NULL)
5540 {
54ee4252 5541 without_params = cp_remove_params (name);
927aa2e7 5542 if (without_params != NULL)
54ee4252 5543 name = without_params.get ();
927aa2e7
JK
5544 }
5545 }
5546
5547 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5548
5549 const uint32_t full_hash = dwarf5_djb_hash (name);
5550 uint32_t namei
5551 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5552 (map.bucket_table_reordered
5553 + (full_hash % map.bucket_count)), 4,
5554 map.dwarf5_byte_order);
5555 if (namei == 0)
5556 return NULL;
5557 --namei;
5558 if (namei >= map.name_count)
5559 {
b98664d3 5560 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5561 "[in module %s]"),
5562 namei, map.name_count,
fcf23d5b 5563 objfile_name (per_objfile->objfile));
927aa2e7
JK
5564 return NULL;
5565 }
5566
5567 for (;;)
5568 {
5569 const uint32_t namei_full_hash
5570 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5571 (map.hash_table_reordered + namei), 4,
5572 map.dwarf5_byte_order);
5573 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5574 return NULL;
5575
5576 if (full_hash == namei_full_hash)
5577 {
fcf23d5b 5578 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5579
5580#if 0 /* An expensive sanity check. */
5581 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5582 {
b98664d3 5583 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5584 "[in module %s]"),
5585 namei, objfile_name (dwarf2_per_objfile->objfile));
5586 return NULL;
5587 }
5588#endif
5589
5590 if (cmp (namei_string, name) == 0)
5591 {
5592 const ULONGEST namei_entry_offs
5593 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5594 + namei * map.offset_size),
5595 map.offset_size, map.dwarf5_byte_order);
5596 return map.entry_pool + namei_entry_offs;
5597 }
5598 }
5599
5600 ++namei;
5601 if (namei >= map.name_count)
5602 return NULL;
5603 }
5604}
5605
5606const gdb_byte *
5607dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5608 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5609{
5610 if (namei >= map.name_count)
5611 {
b98664d3 5612 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5613 "[in module %s]"),
5614 namei, map.name_count,
fcf23d5b 5615 objfile_name (per_objfile->objfile));
927aa2e7
JK
5616 return NULL;
5617 }
5618
5619 const ULONGEST namei_entry_offs
5620 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5621 + namei * map.offset_size),
5622 map.offset_size, map.dwarf5_byte_order);
5623 return map.entry_pool + namei_entry_offs;
5624}
5625
5626/* See dw2_debug_names_iterator. */
5627
5628dwarf2_per_cu_data *
5629dw2_debug_names_iterator::next ()
5630{
5631 if (m_addr == NULL)
5632 return NULL;
5633
fcf23d5b
SM
5634 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5635 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5636 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5637
5638 again:
5639
5640 unsigned int bytes_read;
5641 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5642 m_addr += bytes_read;
5643 if (abbrev == 0)
5644 return NULL;
5645
5646 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5647 if (indexval_it == m_map.abbrev_map.cend ())
5648 {
b98664d3 5649 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5650 "[in module %s]"),
ed2dc618 5651 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5652 return NULL;
5653 }
5654 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5655 enum class symbol_linkage {
5656 unknown,
5657 static_,
5658 extern_,
23c13d42 5659 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5660 dwarf2_per_cu_data *per_cu = NULL;
5661 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5662 {
5663 ULONGEST ull;
5664 switch (attr.form)
5665 {
5666 case DW_FORM_implicit_const:
5667 ull = attr.implicit_const;
5668 break;
5669 case DW_FORM_flag_present:
5670 ull = 1;
5671 break;
5672 case DW_FORM_udata:
5673 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5674 m_addr += bytes_read;
5675 break;
6dc55ce9 5676 case DW_FORM_ref4:
5677 ull = read_4_bytes (abfd, m_addr);
5678 m_addr += 4;
5679 break;
5680 case DW_FORM_ref8:
5681 ull = read_8_bytes (abfd, m_addr);
5682 m_addr += 8;
5683 break;
5684 case DW_FORM_ref_sig8:
5685 ull = read_8_bytes (abfd, m_addr);
5686 m_addr += 8;
5687 break;
927aa2e7 5688 default:
b98664d3 5689 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5690 dwarf_form_name (attr.form),
ed2dc618 5691 objfile_name (objfile));
927aa2e7
JK
5692 return NULL;
5693 }
5694 switch (attr.dw_idx)
5695 {
5696 case DW_IDX_compile_unit:
5697 /* Don't crash on bad data. */
fcf23d5b 5698 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5699 {
b98664d3 5700 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5701 " [in module %s]"),
5702 pulongest (ull),
fcf23d5b 5703 objfile_name (objfile));
927aa2e7
JK
5704 continue;
5705 }
fcf23d5b 5706 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5707 break;
8af5c486
JK
5708 case DW_IDX_type_unit:
5709 /* Don't crash on bad data. */
fcf23d5b 5710 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5711 {
b98664d3 5712 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5713 " [in module %s]"),
5714 pulongest (ull),
fcf23d5b 5715 objfile_name (objfile));
8af5c486
JK
5716 continue;
5717 }
fcf23d5b 5718 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5719 break;
6dc55ce9 5720 case DW_IDX_die_offset:
5721 /* In a per-CU index (as opposed to a per-module index), index
5722 entries without CU attribute implicitly refer to the single CU. */
5723 if (per_cu == NULL)
fcf23d5b 5724 per_cu = per_bfd->get_cu (0);
6dc55ce9 5725 break;
927aa2e7
JK
5726 case DW_IDX_GNU_internal:
5727 if (!m_map.augmentation_is_gdb)
5728 break;
23c13d42 5729 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5730 break;
5731 case DW_IDX_GNU_external:
5732 if (!m_map.augmentation_is_gdb)
5733 break;
23c13d42 5734 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5735 break;
5736 }
5737 }
5738
5739 /* Skip if already read in. */
fcf23d5b 5740 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5741 goto again;
5742
5743 /* Check static vs global. */
23c13d42 5744 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5745 {
2b79f376 5746 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5747 const bool symbol_is_static =
5748 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5749 if (want_static != symbol_is_static)
2b79f376 5750 goto again;
927aa2e7
JK
5751 }
5752
5753 /* Match dw2_symtab_iter_next, symbol_kind
5754 and debug_names::psymbol_tag. */
5755 switch (m_domain)
5756 {
5757 case VAR_DOMAIN:
5758 switch (indexval.dwarf_tag)
5759 {
5760 case DW_TAG_variable:
5761 case DW_TAG_subprogram:
5762 /* Some types are also in VAR_DOMAIN. */
5763 case DW_TAG_typedef:
5764 case DW_TAG_structure_type:
5765 break;
5766 default:
5767 goto again;
5768 }
5769 break;
5770 case STRUCT_DOMAIN:
5771 switch (indexval.dwarf_tag)
5772 {
5773 case DW_TAG_typedef:
5774 case DW_TAG_structure_type:
5775 break;
5776 default:
5777 goto again;
5778 }
5779 break;
5780 case LABEL_DOMAIN:
5781 switch (indexval.dwarf_tag)
5782 {
5783 case 0:
5784 case DW_TAG_variable:
5785 break;
5786 default:
5787 goto again;
5788 }
5789 break;
59c35742
AB
5790 case MODULE_DOMAIN:
5791 switch (indexval.dwarf_tag)
5792 {
5793 case DW_TAG_module:
5794 break;
5795 default:
5796 goto again;
5797 }
5798 break;
927aa2e7
JK
5799 default:
5800 break;
5801 }
5802
5803 /* Match dw2_expand_symtabs_matching, symbol_kind and
5804 debug_names::psymbol_tag. */
5805 switch (m_search)
4b514bc8 5806 {
927aa2e7
JK
5807 case VARIABLES_DOMAIN:
5808 switch (indexval.dwarf_tag)
4b514bc8 5809 {
927aa2e7
JK
5810 case DW_TAG_variable:
5811 break;
5812 default:
5813 goto again;
4b514bc8 5814 }
927aa2e7
JK
5815 break;
5816 case FUNCTIONS_DOMAIN:
5817 switch (indexval.dwarf_tag)
4b514bc8 5818 {
927aa2e7
JK
5819 case DW_TAG_subprogram:
5820 break;
5821 default:
5822 goto again;
4b514bc8 5823 }
927aa2e7
JK
5824 break;
5825 case TYPES_DOMAIN:
5826 switch (indexval.dwarf_tag)
5827 {
5828 case DW_TAG_typedef:
5829 case DW_TAG_structure_type:
5830 break;
5831 default:
5832 goto again;
5833 }
5834 break;
59c35742
AB
5835 case MODULES_DOMAIN:
5836 switch (indexval.dwarf_tag)
5837 {
5838 case DW_TAG_module:
5839 break;
5840 default:
5841 goto again;
5842 }
927aa2e7
JK
5843 default:
5844 break;
4b514bc8 5845 }
927aa2e7
JK
5846
5847 return per_cu;
4b514bc8 5848}
61920122 5849
927aa2e7 5850static struct compunit_symtab *
c7f839cb 5851dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5852 const char *name, domain_enum domain)
4b514bc8 5853{
976ca316 5854 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5855
976ca316 5856 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5857 if (!mapp)
61920122 5858 {
927aa2e7
JK
5859 /* index is NULL if OBJF_READNOW. */
5860 return NULL;
5861 }
5862 const auto &map = *mapp;
9291a0cd 5863
976ca316 5864 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5865
927aa2e7
JK
5866 struct compunit_symtab *stab_best = NULL;
5867 struct dwarf2_per_cu_data *per_cu;
5868 while ((per_cu = iter.next ()) != NULL)
5869 {
5870 struct symbol *sym, *with_opaque = NULL;
97a1449a 5871 compunit_symtab *stab
976ca316 5872 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5873 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5874 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5875
927aa2e7
JK
5876 sym = block_find_symbol (block, name, domain,
5877 block_find_non_opaque_type_preferred,
5878 &with_opaque);
9703b513 5879
927aa2e7
JK
5880 /* Some caution must be observed with overloaded functions and
5881 methods, since the index will not contain any overload
5882 information (but NAME might contain it). */
a3ec0bb1 5883
927aa2e7 5884 if (sym != NULL
987012b8 5885 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5886 return stab;
5887 if (with_opaque != NULL
987012b8 5888 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5889 stab_best = stab;
9703b513 5890
927aa2e7 5891 /* Keep looking through other CUs. */
9703b513
TT
5892 }
5893
927aa2e7 5894 return stab_best;
9703b513
TT
5895}
5896
927aa2e7
JK
5897/* This dumps minimal information about .debug_names. It is called
5898 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5899 uses this to verify that .debug_names has been loaded. */
9291a0cd 5900
927aa2e7
JK
5901static void
5902dw2_debug_names_dump (struct objfile *objfile)
5903{
976ca316 5904 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5905
976ca316 5906 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5907 printf_filtered (".debug_names:");
976ca316 5908 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5909 printf_filtered (" exists\n");
5910 else
5911 printf_filtered (" faked for \"readnow\"\n");
5912 printf_filtered ("\n");
9291a0cd
TT
5913}
5914
9291a0cd 5915static void
927aa2e7
JK
5916dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5917 const char *func_name)
9291a0cd 5918{
976ca316 5919 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5920
976ca316
SM
5921 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5922 if (per_objfile->per_bfd->debug_names_table)
24c79950 5923 {
976ca316 5924 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5925
fcf23d5b 5926 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5927 per_objfile);
24c79950 5928
927aa2e7
JK
5929 struct dwarf2_per_cu_data *per_cu;
5930 while ((per_cu = iter.next ()) != NULL)
976ca316 5931 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5932 }
5933}
24c79950 5934
3b00ef10
TT
5935static void
5936dw2_debug_names_map_matching_symbols
5937 (struct objfile *objfile,
5938 const lookup_name_info &name, domain_enum domain,
5939 int global,
5940 gdb::function_view<symbol_found_callback_ftype> callback,
5941 symbol_compare_ftype *ordered_compare)
5942{
976ca316 5943 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5944
5945 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5946 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5947 return;
5948
976ca316 5949 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5950 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5951
5952 const char *match_name = name.ada ().lookup_name ().c_str ();
5953 auto matcher = [&] (const char *symname)
5954 {
5955 if (ordered_compare == nullptr)
5956 return true;
5957 return ordered_compare (symname, match_name) == 0;
5958 };
5959
5960 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5961 [&] (offset_type namei)
5962 {
5963 /* The name was matched, now expand corresponding CUs that were
5964 marked. */
fcf23d5b 5965 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5966 per_objfile);
3b00ef10
TT
5967
5968 struct dwarf2_per_cu_data *per_cu;
5969 while ((per_cu = iter.next ()) != NULL)
976ca316 5970 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5971 nullptr);
3b00ef10 5972 return true;
976ca316 5973 }, per_objfile);
3b00ef10
TT
5974
5975 /* It's a shame we couldn't do this inside the
5976 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5977 that have already been expanded. Instead, this loop matches what
5978 the psymtab code does. */
976ca316 5979 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5980 {
976ca316 5981 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5982 if (symtab != nullptr)
3b00ef10
TT
5983 {
5984 const struct block *block
af758d11 5985 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5986 if (!iterate_over_symbols_terminated (block, name,
5987 domain, callback))
5988 break;
5989 }
5990 }
5991}
5992
927aa2e7
JK
5993static void
5994dw2_debug_names_expand_symtabs_matching
5995 (struct objfile *objfile,
5996 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5997 const lookup_name_info *lookup_name,
927aa2e7
JK
5998 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5999 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
6000 enum search_domain kind)
6001{
976ca316 6002 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 6003
927aa2e7 6004 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 6005 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 6006 return;
9291a0cd 6007
976ca316 6008 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 6009
c1a66c06
TV
6010 if (symbol_matcher == NULL && lookup_name == NULL)
6011 {
976ca316 6012 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
6013 {
6014 QUIT;
6015
976ca316
SM
6016 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6017 expansion_notify);
c1a66c06
TV
6018 }
6019 return;
6020 }
6021
976ca316 6022 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 6023
c1a66c06 6024 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
6025 symbol_matcher,
6026 kind, [&] (offset_type namei)
927aa2e7 6027 {
927aa2e7
JK
6028 /* The name was matched, now expand corresponding CUs that were
6029 marked. */
976ca316 6030 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 6031
927aa2e7
JK
6032 struct dwarf2_per_cu_data *per_cu;
6033 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
6034 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6035 expansion_notify);
3b00ef10 6036 return true;
976ca316 6037 }, per_objfile);
9291a0cd
TT
6038}
6039
927aa2e7 6040const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6041{
6042 dw2_has_symbols,
6043 dw2_find_last_source_symtab,
6044 dw2_forget_cached_source_info,
f8eba3c6 6045 dw2_map_symtabs_matching_filename,
927aa2e7 6046 dw2_debug_names_lookup_symbol,
d3214198 6047 NULL,
9291a0cd 6048 dw2_print_stats,
927aa2e7 6049 dw2_debug_names_dump,
927aa2e7 6050 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6051 dw2_expand_all_symtabs,
652a8996 6052 dw2_expand_symtabs_with_fullname,
3b00ef10 6053 dw2_debug_names_map_matching_symbols,
927aa2e7 6054 dw2_debug_names_expand_symtabs_matching,
43f3e411 6055 dw2_find_pc_sect_compunit_symtab,
71a3c369 6056 NULL,
9291a0cd
TT
6057 dw2_map_symbol_filenames
6058};
6059
4485a1c1 6060/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 6061 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
6062
6063template <typename T>
6064static gdb::array_view<const gdb_byte>
6065get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6066{
6067 dwarf2_section_info *section = &section_owner->gdb_index;
6068
96b79293 6069 if (section->empty ())
4485a1c1
SM
6070 return {};
6071
6072 /* Older elfutils strip versions could keep the section in the main
6073 executable while splitting it for the separate debug info file. */
96b79293 6074 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
6075 return {};
6076
96b79293 6077 section->read (obj);
4485a1c1 6078
8bebfcda
PA
6079 /* dwarf2_section_info::size is a bfd_size_type, while
6080 gdb::array_view works with size_t. On 32-bit hosts, with
6081 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6082 is 32-bit. So we need an explicit narrowing conversion here.
6083 This is fine, because it's impossible to allocate or mmap an
6084 array/buffer larger than what size_t can represent. */
6085 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6086}
6087
87d6a7aa
SM
6088/* Lookup the index cache for the contents of the index associated to
6089 DWARF2_OBJ. */
6090
6091static gdb::array_view<const gdb_byte>
5989a64e 6092get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
6093{
6094 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6095 if (build_id == nullptr)
6096 return {};
6097
6098 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 6099 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
6100}
6101
6102/* Same as the above, but for DWZ. */
6103
6104static gdb::array_view<const gdb_byte>
6105get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6106{
6107 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6108 if (build_id == nullptr)
6109 return {};
6110
6111 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6112}
6113
3c0aa29a 6114/* See symfile.h. */
9291a0cd 6115
3c0aa29a
PA
6116bool
6117dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6118{
976ca316
SM
6119 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6120 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6121
9291a0cd
TT
6122 /* If we're about to read full symbols, don't bother with the
6123 indices. In this case we also don't care if some other debug
6124 format is making psymtabs, because they are all about to be
6125 expanded anyway. */
6126 if ((objfile->flags & OBJF_READNOW))
6127 {
17ee85fc
TT
6128 /* When using READNOW, the using_index flag (set below) indicates that
6129 PER_BFD was already initialized, when we loaded some other objfile. */
6130 if (per_bfd->using_index)
6131 {
6132 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6133 per_objfile->resize_symtabs ();
17ee85fc
TT
6134 return true;
6135 }
6136
6137 per_bfd->using_index = 1;
976ca316
SM
6138 create_all_comp_units (per_objfile);
6139 create_all_type_units (per_objfile);
17ee85fc
TT
6140 per_bfd->quick_file_names_table
6141 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6142 per_objfile->resize_symtabs ();
9291a0cd 6143
17ee85fc
TT
6144 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6145 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6146 {
17ee85fc 6147 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6148
17ee85fc 6149 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6150 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6151 }
6152
6153 /* Return 1 so that gdb sees the "quick" functions. However,
6154 these functions will be no-ops because we will have expanded
6155 all symtabs. */
3c0aa29a
PA
6156 *index_kind = dw_index_kind::GDB_INDEX;
6157 return true;
9291a0cd
TT
6158 }
6159
17ee85fc
TT
6160 /* Was a debug names index already read when we processed an objfile sharing
6161 PER_BFD? */
6162 if (per_bfd->debug_names_table != nullptr)
6163 {
6164 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6165 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6166 per_objfile->resize_symtabs ();
17ee85fc
TT
6167 return true;
6168 }
6169
6170 /* Was a GDB index already read when we processed an objfile sharing
6171 PER_BFD? */
6172 if (per_bfd->index_table != nullptr)
6173 {
6174 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6175 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6176 per_objfile->resize_symtabs ();
17ee85fc
TT
6177 return true;
6178 }
6179
efb763a5
SM
6180 /* There might already be partial symtabs built for this BFD. This happens
6181 when loading the same binary twice with the index-cache enabled. If so,
6182 don't try to read an index. The objfile / per_objfile initialization will
6183 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6184 code path. */
6185 if (per_bfd->partial_symtabs != nullptr)
6186 return false;
6187
976ca316 6188 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6189 {
6190 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6191 per_objfile->resize_symtabs ();
3c0aa29a
PA
6192 return true;
6193 }
927aa2e7 6194
976ca316 6195 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6196 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6197 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6198 {
6199 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6200 per_objfile->resize_symtabs ();
3c0aa29a
PA
6201 return true;
6202 }
9291a0cd 6203
87d6a7aa 6204 /* ... otherwise, try to find the index in the index cache. */
976ca316 6205 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6206 get_gdb_index_contents_from_cache,
6207 get_gdb_index_contents_from_cache_dwz))
6208 {
6209 global_index_cache.hit ();
6210 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6211 per_objfile->resize_symtabs ();
87d6a7aa
SM
6212 return true;
6213 }
6214
6215 global_index_cache.miss ();
3c0aa29a 6216 return false;
9291a0cd
TT
6217}
6218
6219\f
6220
dce234bc
PP
6221/* Build a partial symbol table. */
6222
6223void
f29dff0a 6224dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6225{
976ca316
SM
6226 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6227 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6228
6229 if (per_bfd->partial_symtabs != nullptr)
6230 {
6231 /* Partial symbols were already read, so now we can simply
6232 attach them. */
6233 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6234 per_objfile->resize_symtabs ();
17ee85fc
TT
6235 return;
6236 }
c9bf0622 6237
a70b8144 6238 try
c9bf0622
TT
6239 {
6240 /* This isn't really ideal: all the data we allocate on the
6241 objfile's obstack is still uselessly kept around. However,
6242 freeing it seems unsafe. */
906768f9 6243 psymtab_discarder psymtabs (objfile);
976ca316 6244 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6245 psymtabs.keep ();
87d6a7aa 6246
976ca316 6247 per_objfile->resize_symtabs ();
af758d11 6248
87d6a7aa 6249 /* (maybe) store an index in the cache. */
976ca316 6250 global_index_cache.store (per_objfile);
c9bf0622 6251 }
230d2906 6252 catch (const gdb_exception_error &except)
492d29ea
PA
6253 {
6254 exception_print (gdb_stderr, except);
6255 }
17ee85fc
TT
6256
6257 /* Finish by setting the local reference to partial symtabs, so that
6258 we don't try to read them again if reading another objfile with the same
6259 BFD. If we can't in fact share, this won't make a difference anyway as
6260 the dwarf2_per_bfd object won't be shared. */
6261 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6262}
c906108c 6263
3b80fe9b
DE
6264/* Find the base address of the compilation unit for range lists and
6265 location lists. It will normally be specified by DW_AT_low_pc.
6266 In DWARF-3 draft 4, the base address could be overridden by
6267 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6268 compilation units with discontinuous ranges. */
6269
6270static void
6271dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6272{
6273 struct attribute *attr;
6274
2b24b6e4 6275 cu->base_address.reset ();
3b80fe9b
DE
6276
6277 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6278 if (attr != nullptr)
95f982e5 6279 cu->base_address = attr->as_address ();
3b80fe9b
DE
6280 else
6281 {
6282 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6283 if (attr != nullptr)
95f982e5 6284 cu->base_address = attr->as_address ();
3b80fe9b
DE
6285 }
6286}
6287
36586728
TT
6288/* Helper function that returns the proper abbrev section for
6289 THIS_CU. */
6290
6291static struct dwarf2_section_info *
6292get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6293{
6294 struct dwarf2_section_info *abbrev;
c3699833 6295 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6296
6297 if (this_cu->is_dwz)
c3699833 6298 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6299 else
c3699833 6300 abbrev = &per_bfd->abbrev;
36586728
TT
6301
6302 return abbrev;
6303}
6304
f4dc4d17
DE
6305/* Fetch the abbreviation table offset from a comp or type unit header. */
6306
6307static sect_offset
976ca316 6308read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6309 struct dwarf2_section_info *section,
9c541725 6310 sect_offset sect_off)
f4dc4d17 6311{
96b79293 6312 bfd *abfd = section->get_bfd_owner ();
d521ce57 6313 const gdb_byte *info_ptr;
ac298888 6314 unsigned int initial_length_size, offset_size;
43988095 6315 uint16_t version;
f4dc4d17 6316
976ca316 6317 section->read (per_objfile->objfile);
9c541725 6318 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6319 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6320 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6321 info_ptr += initial_length_size;
6322
6323 version = read_2_bytes (abfd, info_ptr);
6324 info_ptr += 2;
6325 if (version >= 5)
6326 {
6327 /* Skip unit type and address size. */
6328 info_ptr += 2;
6329 }
6330
24aa364d 6331 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6332}
6333
b83470bf
TT
6334/* A partial symtab that is used only for include files. */
6335struct dwarf2_include_psymtab : public partial_symtab
6336{
6337 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6338 : partial_symtab (filename, objfile)
6339 {
6340 }
6341
6342 void read_symtab (struct objfile *objfile) override
6343 {
194d088f
TV
6344 /* It's an include file, no symbols to read for it.
6345 Everything is in the includer symtab. */
6346
6347 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6348 expansion of the includer psymtab. We use the dependencies[0] field to
6349 model the includer. But if we go the regular route of calling
6350 expand_psymtab here, and having expand_psymtab call expand_dependencies
6351 to expand the includer, we'll only use expand_psymtab on the includer
6352 (making it a non-toplevel psymtab), while if we expand the includer via
6353 another path, we'll use read_symtab (making it a toplevel psymtab).
6354 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6355 psymtab, and trigger read_symtab on the includer here directly. */
6356 includer ()->read_symtab (objfile);
b83470bf
TT
6357 }
6358
6359 void expand_psymtab (struct objfile *objfile) override
6360 {
194d088f
TV
6361 /* This is not called by read_symtab, and should not be called by any
6362 expand_dependencies. */
6363 gdb_assert (false);
b83470bf
TT
6364 }
6365
5717c425 6366 bool readin_p (struct objfile *objfile) const override
b83470bf 6367 {
5717c425 6368 return includer ()->readin_p (objfile);
b83470bf
TT
6369 }
6370
5717c425 6371 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6372 {
6373 return nullptr;
6374 }
6375
6376private:
194d088f
TV
6377 partial_symtab *includer () const
6378 {
6379 /* An include psymtab has exactly one dependency: the psymtab that
6380 includes it. */
6381 gdb_assert (this->number_of_dependencies == 1);
6382 return this->dependencies[0];
6383 }
b83470bf
TT
6384};
6385
aaa75496
JB
6386/* Allocate a new partial symtab for file named NAME and mark this new
6387 partial symtab as being an include of PST. */
6388
6389static void
891813be 6390dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6391 struct objfile *objfile)
aaa75496 6392{
b83470bf 6393 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6394
fbd9ab74 6395 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6396 subpst->dirname = pst->dirname;
fbd9ab74 6397
a9342b62 6398 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6399 subpst->dependencies[0] = pst;
6400 subpst->number_of_dependencies = 1;
aaa75496
JB
6401}
6402
6403/* Read the Line Number Program data and extract the list of files
6404 included by the source file represented by PST. Build an include
d85a05f0 6405 partial symtab for each of these included files. */
aaa75496
JB
6406
6407static void
6408dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6409 struct die_info *die,
891813be 6410 dwarf2_psymtab *pst)
aaa75496 6411{
fff8551c 6412 line_header_up lh;
d85a05f0 6413 struct attribute *attr;
aaa75496 6414
d85a05f0 6415 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6416 if (attr != nullptr && attr->form_is_unsigned ())
6417 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6418 if (lh == NULL)
6419 return; /* No linetable, so no includes. */
6420
79748972
TT
6421 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6422 that we pass in the raw text_low here; that is ok because we're
6423 only decoding the line table to make include partial symtabs, and
6424 so the addresses aren't really used. */
4ae976d1 6425 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6426 pst->raw_text_low (), 1);
aaa75496
JB
6427}
6428
348e048f 6429static hashval_t
52dc124a 6430hash_signatured_type (const void *item)
348e048f 6431{
9a3c8263
SM
6432 const struct signatured_type *sig_type
6433 = (const struct signatured_type *) item;
9a619af0 6434
348e048f 6435 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6436 return sig_type->signature;
348e048f
DE
6437}
6438
6439static int
52dc124a 6440eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6441{
9a3c8263
SM
6442 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6443 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6444
348e048f
DE
6445 return lhs->signature == rhs->signature;
6446}
6447
1fd400ff
TT
6448/* Allocate a hash table for signatured types. */
6449
b0b6a987 6450static htab_up
298e9637 6451allocate_signatured_type_table ()
1fd400ff 6452{
b0b6a987
TT
6453 return htab_up (htab_create_alloc (41,
6454 hash_signatured_type,
6455 eq_signatured_type,
6456 NULL, xcalloc, xfree));
1fd400ff
TT
6457}
6458
d467dd73 6459/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6460
6461static int
d467dd73 6462add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6463{
9a3c8263 6464 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6465 std::vector<signatured_type *> *all_type_units
6466 = (std::vector<signatured_type *> *) datum;
1fd400ff 6467
b2bdb8cf 6468 all_type_units->push_back (sigt);
1fd400ff
TT
6469
6470 return 1;
6471}
6472
78d4d2c5 6473/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6474 and fill them into TYPES_HTAB. It will process only type units,
6475 therefore DW_UT_type. */
c88ee1f0 6476
78d4d2c5 6477static void
976ca316 6478create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6479 struct dwo_file *dwo_file,
b0b6a987 6480 dwarf2_section_info *section, htab_up &types_htab,
43988095 6481 rcuh_kind section_kind)
348e048f 6482{
976ca316 6483 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6484 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6485 bfd *abfd;
6486 const gdb_byte *info_ptr, *end_ptr;
348e048f 6487
4bdcc0c1
DE
6488 abbrev_section = (dwo_file != NULL
6489 ? &dwo_file->sections.abbrev
976ca316 6490 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6491
6f738b01
SM
6492 dwarf_read_debug_printf ("Reading %s for %s:",
6493 section->get_name (),
6494 abbrev_section->get_file_name ());
09406207 6495
96b79293 6496 section->read (objfile);
78d4d2c5 6497 info_ptr = section->buffer;
348e048f 6498
78d4d2c5
JK
6499 if (info_ptr == NULL)
6500 return;
348e048f 6501
78d4d2c5
JK
6502 /* We can't set abfd until now because the section may be empty or
6503 not present, in which case the bfd is unknown. */
96b79293 6504 abfd = section->get_bfd_owner ();
348e048f 6505
c0ab21c2
TT
6506 /* We don't use cutu_reader here because we don't need to read
6507 any dies: the signature is in the header. */
3019eac3 6508
78d4d2c5
JK
6509 end_ptr = info_ptr + section->size;
6510 while (info_ptr < end_ptr)
6511 {
78d4d2c5
JK
6512 struct signatured_type *sig_type;
6513 struct dwo_unit *dwo_tu;
6514 void **slot;
6515 const gdb_byte *ptr = info_ptr;
6516 struct comp_unit_head header;
6517 unsigned int length;
8b70b953 6518
9c541725 6519 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6520
a49dd8dd
JK
6521 /* Initialize it due to a false compiler warning. */
6522 header.signature = -1;
9c541725 6523 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6524
78d4d2c5
JK
6525 /* We need to read the type's signature in order to build the hash
6526 table, but we don't need anything else just yet. */
348e048f 6527
976ca316 6528 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6529 abbrev_section, ptr, section_kind);
348e048f 6530
4057dfde 6531 length = header.get_length ();
6caca83c 6532
78d4d2c5
JK
6533 /* Skip dummy type units. */
6534 if (ptr >= info_ptr + length
43988095 6535 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6536 || (header.unit_type != DW_UT_type
6537 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6538 {
6539 info_ptr += length;
6540 continue;
6541 }
dee91e82 6542
78d4d2c5
JK
6543 if (types_htab == NULL)
6544 {
6545 if (dwo_file)
298e9637 6546 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6547 else
298e9637 6548 types_htab = allocate_signatured_type_table ();
78d4d2c5 6549 }
8b70b953 6550
78d4d2c5
JK
6551 if (dwo_file)
6552 {
6553 sig_type = NULL;
976ca316 6554 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6555 dwo_tu->dwo_file = dwo_file;
43988095 6556 dwo_tu->signature = header.signature;
9c541725 6557 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6558 dwo_tu->section = section;
9c541725 6559 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6560 dwo_tu->length = length;
6561 }
6562 else
6563 {
6564 /* N.B.: type_offset is not usable if this type uses a DWO file.
6565 The real type_offset is in the DWO file. */
6566 dwo_tu = NULL;
976ca316 6567 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6568 sig_type->signature = header.signature;
9c541725 6569 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6570 sig_type->per_cu.is_debug_types = 1;
6571 sig_type->per_cu.section = section;
9c541725 6572 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6573 sig_type->per_cu.length = length;
6574 }
6575
b0b6a987 6576 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6577 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6578 INSERT);
6579 gdb_assert (slot != NULL);
6580 if (*slot != NULL)
6581 {
9c541725 6582 sect_offset dup_sect_off;
0349ea22 6583
3019eac3
DE
6584 if (dwo_file)
6585 {
78d4d2c5
JK
6586 const struct dwo_unit *dup_tu
6587 = (const struct dwo_unit *) *slot;
6588
9c541725 6589 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6590 }
6591 else
6592 {
78d4d2c5
JK
6593 const struct signatured_type *dup_tu
6594 = (const struct signatured_type *) *slot;
6595
9c541725 6596 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6597 }
8b70b953 6598
b98664d3 6599 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6600 " the entry at offset %s, signature %s"),
6601 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6602 hex_string (header.signature));
78d4d2c5
JK
6603 }
6604 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6605
6f738b01
SM
6606 dwarf_read_debug_printf_v (" offset %s, signature %s",
6607 sect_offset_str (sect_off),
6608 hex_string (header.signature));
3019eac3 6609
78d4d2c5
JK
6610 info_ptr += length;
6611 }
6612}
3019eac3 6613
78d4d2c5
JK
6614/* Create the hash table of all entries in the .debug_types
6615 (or .debug_types.dwo) section(s).
6616 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6617 otherwise it is NULL.
b3c8eb43 6618
78d4d2c5 6619 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6620
78d4d2c5 6621 Note: This function processes DWO files only, not DWP files. */
348e048f 6622
78d4d2c5 6623static void
976ca316 6624create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6625 struct dwo_file *dwo_file,
fd5866f6 6626 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6627 htab_up &types_htab)
78d4d2c5 6628{
fd5866f6 6629 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6630 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6631 rcuh_kind::TYPE);
3019eac3
DE
6632}
6633
6634/* Create the hash table of all entries in the .debug_types section,
6635 and initialize all_type_units.
6636 The result is zero if there is an error (e.g. missing .debug_types section),
6637 otherwise non-zero. */
6638
6639static int
976ca316 6640create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6641{
b0b6a987 6642 htab_up types_htab;
3019eac3 6643
976ca316
SM
6644 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6645 types_htab, rcuh_kind::COMPILE);
6646 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6647 types_htab);
3019eac3
DE
6648 if (types_htab == NULL)
6649 {
976ca316 6650 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6651 return 0;
6652 }
6653
976ca316 6654 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6655
976ca316
SM
6656 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6657 per_objfile->per_bfd->all_type_units.reserve
6658 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6659
976ca316 6660 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6661 add_signatured_type_cu_to_table,
976ca316 6662 &per_objfile->per_bfd->all_type_units);
1fd400ff 6663
348e048f
DE
6664 return 1;
6665}
6666
5989a64e 6667/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6668 If SLOT is non-NULL, it is the entry to use in the hash table.
6669 Otherwise we find one. */
6670
6671static struct signatured_type *
976ca316 6672add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6673{
976ca316
SM
6674 if (per_objfile->per_bfd->all_type_units.size ()
6675 == per_objfile->per_bfd->all_type_units.capacity ())
6676 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6677
976ca316 6678 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6679
976ca316 6680 per_objfile->resize_symtabs ();
af758d11 6681
976ca316 6682 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6683 sig_type->signature = sig;
6684 sig_type->per_cu.is_debug_types = 1;
976ca316 6685 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6686 {
6687 sig_type->per_cu.v.quick =
976ca316 6688 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6689 struct dwarf2_per_cu_quick_data);
6690 }
6691
6692 if (slot == NULL)
6693 {
976ca316 6694 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6695 sig_type, INSERT);
6696 }
6697 gdb_assert (*slot == NULL);
6698 *slot = sig_type;
6699 /* The rest of sig_type must be filled in by the caller. */
6700 return sig_type;
6701}
6702
a2ce51a0
DE
6703/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6704 Fill in SIG_ENTRY with DWO_ENTRY. */
6705
6706static void
976ca316 6707fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6708 struct signatured_type *sig_entry,
6709 struct dwo_unit *dwo_entry)
6710{
976ca316 6711 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6712
7ee85ab1 6713 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6714 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6715 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6716 if (per_bfd->using_index)
6aa5f3a6
DE
6717 {
6718 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6719 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6720 }
6721 else
6722 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6723 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6724 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6725 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6726 gdb_assert (sig_entry->dwo_unit == NULL);
6727
6728 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6729 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6730 sig_entry->per_cu.length = dwo_entry->length;
6731 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6732 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6733 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6734 sig_entry->dwo_unit = dwo_entry;
6735}
6736
6737/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6738 If we haven't read the TU yet, create the signatured_type data structure
6739 for a TU to be read in directly from a DWO file, bypassing the stub.
6740 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6741 using .gdb_index, then when reading a CU we want to stay in the DWO file
6742 containing that CU. Otherwise we could end up reading several other DWO
6743 files (due to comdat folding) to process the transitive closure of all the
6744 mentioned TUs, and that can be slow. The current DWO file will have every
6745 type signature that it needs.
a2ce51a0
DE
6746 We only do this for .gdb_index because in the psymtab case we already have
6747 to read all the DWOs to build the type unit groups. */
6748
6749static struct signatured_type *
6750lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6751{
976ca316 6752 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6753 struct dwo_file *dwo_file;
6754 struct dwo_unit find_dwo_entry, *dwo_entry;
6755 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6756 void **slot;
a2ce51a0 6757
976ca316 6758 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6759
6aa5f3a6
DE
6760 /* If TU skeletons have been removed then we may not have read in any
6761 TUs yet. */
976ca316
SM
6762 if (per_objfile->per_bfd->signatured_types == NULL)
6763 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6764
6765 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6766 Use the global signatured_types array to do our own comdat-folding
6767 of types. If this is the first time we're reading this TU, and
6768 the TU has an entry in .gdb_index, replace the recorded data from
6769 .gdb_index with this TU. */
a2ce51a0 6770
a2ce51a0 6771 find_sig_entry.signature = sig;
976ca316 6772 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6773 &find_sig_entry, INSERT);
9a3c8263 6774 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6775
6776 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6777 read. Don't reassign the global entry to point to this DWO if that's
6778 the case. Also note that if the TU is already being read, it may not
6779 have come from a DWO, the program may be a mix of Fission-compiled
6780 code and non-Fission-compiled code. */
6781
6782 /* Have we already tried to read this TU?
6783 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6784 needn't exist in the global table yet). */
6785 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6786 return sig_entry;
6787
6aa5f3a6
DE
6788 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6789 dwo_unit of the TU itself. */
6790 dwo_file = cu->dwo_unit->dwo_file;
6791
a2ce51a0
DE
6792 /* Ok, this is the first time we're reading this TU. */
6793 if (dwo_file->tus == NULL)
6794 return NULL;
6795 find_dwo_entry.signature = sig;
b0b6a987
TT
6796 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6797 &find_dwo_entry);
a2ce51a0
DE
6798 if (dwo_entry == NULL)
6799 return NULL;
6800
6aa5f3a6
DE
6801 /* If the global table doesn't have an entry for this TU, add one. */
6802 if (sig_entry == NULL)
976ca316 6803 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6804
976ca316 6805 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6806 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6807 return sig_entry;
6808}
6809
a2ce51a0
DE
6810/* Subroutine of lookup_signatured_type.
6811 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6812 then try the DWP file. If the TU stub (skeleton) has been removed then
6813 it won't be in .gdb_index. */
a2ce51a0
DE
6814
6815static struct signatured_type *
6816lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6817{
976ca316
SM
6818 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6819 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6820 struct dwo_unit *dwo_entry;
6821 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6822 void **slot;
a2ce51a0 6823
976ca316 6824 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6825 gdb_assert (dwp_file != NULL);
6826
6aa5f3a6
DE
6827 /* If TU skeletons have been removed then we may not have read in any
6828 TUs yet. */
976ca316
SM
6829 if (per_objfile->per_bfd->signatured_types == NULL)
6830 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6831
6aa5f3a6 6832 find_sig_entry.signature = sig;
976ca316 6833 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6834 &find_sig_entry, INSERT);
9a3c8263 6835 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6836
6837 /* Have we already tried to read this TU?
6838 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6839 needn't exist in the global table yet). */
6840 if (sig_entry != NULL)
6841 return sig_entry;
6842
a2ce51a0
DE
6843 if (dwp_file->tus == NULL)
6844 return NULL;
976ca316
SM
6845 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6846 1 /* is_debug_types */);
a2ce51a0
DE
6847 if (dwo_entry == NULL)
6848 return NULL;
6849
976ca316
SM
6850 sig_entry = add_type_unit (per_objfile, sig, slot);
6851 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6852
a2ce51a0
DE
6853 return sig_entry;
6854}
6855
380bca97 6856/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6857 Returns NULL if signature SIG is not present in the table.
6858 It is up to the caller to complain about this. */
348e048f
DE
6859
6860static struct signatured_type *
a2ce51a0 6861lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6862{
976ca316 6863 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6864
976ca316 6865 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6866 {
6867 /* We're in a DWO/DWP file, and we're using .gdb_index.
6868 These cases require special processing. */
976ca316 6869 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6870 return lookup_dwo_signatured_type (cu, sig);
6871 else
6872 return lookup_dwp_signatured_type (cu, sig);
6873 }
6874 else
6875 {
6876 struct signatured_type find_entry, *entry;
348e048f 6877
976ca316 6878 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6879 return NULL;
6880 find_entry.signature = sig;
9a3c8263 6881 entry = ((struct signatured_type *)
976ca316 6882 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6883 &find_entry));
a2ce51a0
DE
6884 return entry;
6885 }
348e048f 6886}
18a8505e 6887
42e7ad6c 6888/* Low level DIE reading support. */
348e048f 6889
d85a05f0
DJ
6890/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6891
6892static void
6893init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6894 struct dwarf2_cu *cu,
3019eac3 6895 struct dwarf2_section_info *section,
685af9cd
TT
6896 struct dwo_file *dwo_file,
6897 struct abbrev_table *abbrev_table)
d85a05f0 6898{
fceca515 6899 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6900 reader->abfd = section->get_bfd_owner ();
d85a05f0 6901 reader->cu = cu;
3019eac3 6902 reader->dwo_file = dwo_file;
dee91e82
DE
6903 reader->die_section = section;
6904 reader->buffer = section->buffer;
f664829e 6905 reader->buffer_end = section->buffer + section->size;
685af9cd 6906 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6907}
6908
c0ab21c2 6909/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6910 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6911 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6912 already.
6913
6914 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6915 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6916 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6917 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6918 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6919 STUB_COMP_DIR may be non-NULL.
3e225074 6920 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6921 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6922 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6923 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6924 kept around for at least as long as *RESULT_READER.
6925
b0c7bfa9
DE
6926 The result is non-zero if a valid (non-dummy) DIE was found. */
6927
6928static int
4ab09049 6929read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6930 struct dwo_unit *dwo_unit,
b0c7bfa9 6931 struct die_info *stub_comp_unit_die,
a2ce51a0 6932 const char *stub_comp_dir,
b0c7bfa9 6933 struct die_reader_specs *result_reader,
d521ce57 6934 const gdb_byte **result_info_ptr,
b0c7bfa9 6935 struct die_info **result_comp_unit_die,
685af9cd 6936 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6937{
976ca316 6938 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6939 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6940 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6941 bfd *abfd;
d521ce57 6942 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6943 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6944 int i,num_extra_attrs;
6945 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6946 struct die_info *comp_unit_die;
6947
b0aeadb3
DE
6948 /* At most one of these may be provided. */
6949 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6950
b0c7bfa9
DE
6951 /* These attributes aren't processed until later:
6952 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6953 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6954 referenced later. However, these attributes are found in the stub
6955 which we won't have later. In order to not impose this complication
6956 on the rest of the code, we read them here and copy them to the
6957 DWO CU/TU die. */
b0c7bfa9
DE
6958
6959 stmt_list = NULL;
6960 low_pc = NULL;
6961 high_pc = NULL;
6962 ranges = NULL;
6963 comp_dir = NULL;
6964
6965 if (stub_comp_unit_die != NULL)
6966 {
6967 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6968 DWO file. */
4ab09049 6969 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6970 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6971 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6972 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6973 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6974 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6975
a39fdb41 6976 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6977
18a8505e
AT
6978 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6979 here (if needed). We need the value before we can process
6980 DW_AT_ranges. */
a39fdb41 6981 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6982 }
a2ce51a0
DE
6983 else if (stub_comp_dir != NULL)
6984 {
6985 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6986 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6987 comp_dir->name = DW_AT_comp_dir;
6988 comp_dir->form = DW_FORM_string;
c6481205 6989 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6990 }
b0c7bfa9
DE
6991
6992 /* Set up for reading the DWO CU/TU. */
6993 cu->dwo_unit = dwo_unit;
685af9cd 6994 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6995 section->read (objfile);
6996 abfd = section->get_bfd_owner ();
9c541725
PA
6997 begin_info_ptr = info_ptr = (section->buffer
6998 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6999 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 7000
4ab09049 7001 if (per_cu->is_debug_types)
b0c7bfa9 7002 {
4ab09049 7003 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 7004
976ca316
SM
7005 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7006 section, dwo_abbrev_section,
43988095 7007 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7008 /* This is not an assert because it can be caused by bad debug info. */
43988095 7009 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7010 {
7011 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7012 " TU at offset %s [in module %s]"),
a2ce51a0 7013 hex_string (sig_type->signature),
43988095 7014 hex_string (cu->header.signature),
9d8780f0 7015 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7016 bfd_get_filename (abfd));
7017 }
9c541725 7018 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7019 /* For DWOs coming from DWP files, we don't know the CU length
7020 nor the type's offset in the TU until now. */
4057dfde 7021 dwo_unit->length = cu->header.get_length ();
9c541725 7022 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7023
7024 /* Establish the type offset that can be used to lookup the type.
7025 For DWO files, we don't know it until now. */
9c541725
PA
7026 sig_type->type_offset_in_section
7027 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7028 }
7029 else
7030 {
976ca316
SM
7031 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7032 section, dwo_abbrev_section,
43988095 7033 info_ptr, rcuh_kind::COMPILE);
9c541725 7034 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7035 /* For DWOs coming from DWP files, we don't know the CU length
7036 until now. */
4057dfde 7037 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
7038 }
7039
606decb2 7040 dwo_abbrev_section->read (objfile);
685af9cd 7041 *result_dwo_abbrev_table
606decb2 7042 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
7043 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7044 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7045
7046 /* Read in the die, but leave space to copy over the attributes
7047 from the stub. This has the benefit of simplifying the rest of
7048 the code - all the work to maintain the illusion of a single
7049 DW_TAG_{compile,type}_unit DIE is done here. */
7050 num_extra_attrs = ((stmt_list != NULL)
7051 + (low_pc != NULL)
7052 + (high_pc != NULL)
7053 + (ranges != NULL)
7054 + (comp_dir != NULL));
7055 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 7056 num_extra_attrs);
b0c7bfa9
DE
7057
7058 /* Copy over the attributes from the stub to the DIE we just read in. */
7059 comp_unit_die = *result_comp_unit_die;
7060 i = comp_unit_die->num_attrs;
7061 if (stmt_list != NULL)
7062 comp_unit_die->attrs[i++] = *stmt_list;
7063 if (low_pc != NULL)
7064 comp_unit_die->attrs[i++] = *low_pc;
7065 if (high_pc != NULL)
7066 comp_unit_die->attrs[i++] = *high_pc;
7067 if (ranges != NULL)
7068 comp_unit_die->attrs[i++] = *ranges;
7069 if (comp_dir != NULL)
7070 comp_unit_die->attrs[i++] = *comp_dir;
7071 comp_unit_die->num_attrs += num_extra_attrs;
7072
b4f54984 7073 if (dwarf_die_debug)
bf6af496
DE
7074 {
7075 fprintf_unfiltered (gdb_stdlog,
7076 "Read die from %s@0x%x of %s:\n",
96b79293 7077 section->get_name (),
bf6af496
DE
7078 (unsigned) (begin_info_ptr - section->buffer),
7079 bfd_get_filename (abfd));
b4f54984 7080 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7081 }
7082
b0c7bfa9
DE
7083 /* Skip dummy compilation units. */
7084 if (info_ptr >= begin_info_ptr + dwo_unit->length
7085 || peek_abbrev_code (abfd, info_ptr) == 0)
7086 return 0;
7087
7088 *result_info_ptr = info_ptr;
7089 return 1;
7090}
7091
a084a2a6
AT
7092/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7093 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7094 signature is part of the header. */
7095static gdb::optional<ULONGEST>
7096lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7097{
7098 if (cu->header.version >= 5)
7099 return cu->header.signature;
7100 struct attribute *attr;
7101 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7102 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7103 return gdb::optional<ULONGEST> ();
529908cb 7104 return attr->as_unsigned ();
a084a2a6
AT
7105}
7106
c0ab21c2 7107/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7108 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7109 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7110
7111static struct dwo_unit *
4ab09049 7112lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7113{
4ab09049 7114 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7115 struct dwo_unit *dwo_unit;
c0ab21c2 7116 const char *comp_dir;
b0c7bfa9 7117
a2ce51a0
DE
7118 gdb_assert (cu != NULL);
7119
b0c7bfa9 7120 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7121 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7122 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7123
4ab09049
SM
7124 if (per_cu->is_debug_types)
7125 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7126 else
7127 {
a084a2a6 7128 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7129
a084a2a6 7130 if (!signature.has_value ())
b0c7bfa9
DE
7131 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7132 " [in module %s]"),
4ab09049
SM
7133 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7134
7135 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7136 }
7137
b0c7bfa9
DE
7138 return dwo_unit;
7139}
7140
c0ab21c2 7141/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7142 See it for a description of the parameters.
fcd3b13d 7143 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7144
c0ab21c2 7145void
9e021579
SM
7146cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7147 dwarf2_per_objfile *per_objfile,
2e671100 7148 dwarf2_cu *existing_cu)
a2ce51a0 7149{
a2ce51a0 7150 struct signatured_type *sig_type;
a2ce51a0
DE
7151
7152 /* Verify we can do the following downcast, and that we have the
7153 data we need. */
7154 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7155 sig_type = (struct signatured_type *) this_cu;
7156 gdb_assert (sig_type->dwo_unit != NULL);
7157
2e671100
SM
7158 dwarf2_cu *cu;
7159
7160 if (existing_cu != nullptr)
6aa5f3a6 7161 {
2e671100
SM
7162 cu = existing_cu;
7163 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7164 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7165 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7166 }
7167 else
7168 {
7188ed02 7169 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7170 in per_objfile yet. */
7188ed02 7171 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7172 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7173 cu = m_new_cu.get ();
6aa5f3a6
DE
7174 }
7175
7176 /* A future optimization, if needed, would be to use an existing
7177 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7178 could share abbrev tables. */
a2ce51a0 7179
2e671100 7180 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7181 NULL /* stub_comp_unit_die */,
7182 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7183 this, &info_ptr,
3e225074 7184 &comp_unit_die,
c0ab21c2 7185 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7186 {
7187 /* Dummy die. */
c0ab21c2 7188 dummy_p = true;
a2ce51a0 7189 }
a2ce51a0
DE
7190}
7191
fd820528 7192/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7193 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7194
f4dc4d17
DE
7195 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7196 Otherwise the table specified in the comp unit header is read in and used.
7197 This is an optimization for when we already have the abbrev table.
7198
2e671100
SM
7199 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7200 allocated. */
aaa75496 7201
ab432490 7202cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7203 dwarf2_per_objfile *per_objfile,
c0ab21c2 7204 struct abbrev_table *abbrev_table,
2e671100 7205 dwarf2_cu *existing_cu,
c0ab21c2
TT
7206 bool skip_partial)
7207 : die_reader_specs {},
6751ebae 7208 m_this_cu (this_cu)
c906108c 7209{
976ca316 7210 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7211 struct dwarf2_section_info *section = this_cu->section;
96b79293 7212 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7213 const gdb_byte *begin_info_ptr;
dee91e82 7214 struct signatured_type *sig_type = NULL;
4bdcc0c1 7215 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7216 /* Non-zero if CU currently points to a DWO file and we need to
7217 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7218 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7219 int rereading_dwo_cu = 0;
c906108c 7220
b4f54984 7221 if (dwarf_die_debug)
9d8780f0 7222 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7223 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7224 sect_offset_str (this_cu->sect_off));
09406207 7225
a2ce51a0
DE
7226 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7227 file (instead of going through the stub), short-circuit all of this. */
7228 if (this_cu->reading_dwo_directly)
7229 {
7230 /* Narrow down the scope of possibilities to have to understand. */
7231 gdb_assert (this_cu->is_debug_types);
7232 gdb_assert (abbrev_table == NULL);
976ca316 7233 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7234 return;
7235 }
7236
dee91e82 7237 /* This is cheap if the section is already read in. */
96b79293 7238 section->read (objfile);
dee91e82 7239
9c541725 7240 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7241
7242 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7243
2e671100
SM
7244 dwarf2_cu *cu;
7245
7246 if (existing_cu != nullptr)
dee91e82 7247 {
2e671100 7248 cu = existing_cu;
42e7ad6c
DE
7249 /* If this CU is from a DWO file we need to start over, we need to
7250 refetch the attributes from the skeleton CU.
7251 This could be optimized by retrieving those attributes from when we
7252 were here the first time: the previous comp_unit_die was stored in
7253 comp_unit_obstack. But there's no data yet that we need this
7254 optimization. */
7255 if (cu->dwo_unit != NULL)
7256 rereading_dwo_cu = 1;
dee91e82
DE
7257 }
7258 else
7259 {
7188ed02 7260 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7261 in per_objfile yet. */
976ca316
SM
7262 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7263 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7264 cu = m_new_cu.get ();
42e7ad6c 7265 }
dee91e82 7266
b0c7bfa9 7267 /* Get the header. */
9c541725 7268 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7269 {
7270 /* We already have the header, there's no need to read it in again. */
9c541725 7271 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7272 }
7273 else
7274 {
3019eac3 7275 if (this_cu->is_debug_types)
dee91e82 7276 {
976ca316
SM
7277 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7278 section, abbrev_section,
7279 info_ptr, rcuh_kind::TYPE);
dee91e82 7280
42e7ad6c
DE
7281 /* Since per_cu is the first member of struct signatured_type,
7282 we can go from a pointer to one to a pointer to the other. */
7283 sig_type = (struct signatured_type *) this_cu;
43988095 7284 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7285 gdb_assert (sig_type->type_offset_in_tu
7286 == cu->header.type_cu_offset_in_tu);
7287 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7288
42e7ad6c
DE
7289 /* LENGTH has not been set yet for type units if we're
7290 using .gdb_index. */
4057dfde 7291 this_cu->length = cu->header.get_length ();
3019eac3
DE
7292
7293 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7294 sig_type->type_offset_in_section =
7295 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7296
7297 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7298 }
7299 else
7300 {
976ca316
SM
7301 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7302 section, abbrev_section,
43988095
JK
7303 info_ptr,
7304 rcuh_kind::COMPILE);
dee91e82 7305
9c541725 7306 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7307 if (this_cu->length == 0)
7308 this_cu->length = cu->header.get_length ();
7309 else
7310 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7311 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7312 }
7313 }
10b3939b 7314
6caca83c 7315 /* Skip dummy compilation units. */
dee91e82 7316 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7317 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7318 {
7319 dummy_p = true;
7320 return;
7321 }
6caca83c 7322
433df2d4
DE
7323 /* If we don't have them yet, read the abbrevs for this compilation unit.
7324 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7325 done. */
f4dc4d17 7326 if (abbrev_table != NULL)
685af9cd
TT
7327 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7328 else
f4dc4d17 7329 {
606decb2 7330 abbrev_section->read (objfile);
c0ab21c2 7331 m_abbrev_table_holder
606decb2 7332 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7333 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7334 }
af703f96 7335
dee91e82 7336 /* Read the top level CU/TU die. */
c0ab21c2 7337 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7338 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7339
58f0c718 7340 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7341 {
7342 dummy_p = true;
7343 return;
7344 }
58f0c718 7345
b0c7bfa9 7346 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7347 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7348 table from the DWO file and pass the ownership over to us. It will be
7349 referenced from READER, so we must make sure to free it after we're done
7350 with READER.
7351
b0c7bfa9
DE
7352 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7353 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7354 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7355 if (dwo_name != nullptr)
3019eac3 7356 {
3019eac3 7357 struct dwo_unit *dwo_unit;
b0c7bfa9 7358 struct die_info *dwo_comp_unit_die;
3019eac3 7359
3e225074 7360 if (comp_unit_die->has_children)
6a506a2d 7361 {
b98664d3 7362 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7363 " has children (offset %s) [in module %s]"),
7364 sect_offset_str (this_cu->sect_off),
7365 bfd_get_filename (abfd));
6a506a2d 7366 }
4ab09049 7367 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7368 if (dwo_unit != NULL)
3019eac3 7369 {
4ab09049 7370 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7371 comp_unit_die, NULL,
c0ab21c2 7372 this, &info_ptr,
3e225074 7373 &dwo_comp_unit_die,
c0ab21c2 7374 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7375 {
7376 /* Dummy die. */
c0ab21c2 7377 dummy_p = true;
6a506a2d
DE
7378 return;
7379 }
7380 comp_unit_die = dwo_comp_unit_die;
7381 }
7382 else
7383 {
7384 /* Yikes, we couldn't find the rest of the DIE, we only have
7385 the stub. A complaint has already been logged. There's
7386 not much more we can do except pass on the stub DIE to
7387 die_reader_func. We don't want to throw an error on bad
7388 debug info. */
3019eac3
DE
7389 }
7390 }
c0ab21c2 7391}
3019eac3 7392
6751ebae
TT
7393void
7394cutu_reader::keep ()
c0ab21c2 7395{
b0c7bfa9 7396 /* Done, clean up. */
6751ebae
TT
7397 gdb_assert (!dummy_p);
7398 if (m_new_cu != NULL)
348e048f 7399 {
7188ed02 7400 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7401 now. */
7188ed02
SM
7402 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7403 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7404 }
dee91e82
DE
7405}
7406
18a8505e
AT
7407/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7408 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7409 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7410
7411 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7412 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7413
7414 We fill in THIS_CU->length.
7415
dee91e82 7416 THIS_CU->cu is always freed when done.
3019eac3 7417 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7418 to care whether it refers to the "main" CU or the DWO CU.
7419
7420 When parent_cu is passed, it is used to provide a default value for
7421 str_offsets_base and addr_base from the parent. */
dee91e82 7422
ab432490 7423cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7424 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7425 struct dwarf2_cu *parent_cu,
7426 struct dwo_file *dwo_file)
7427 : die_reader_specs {},
7428 m_this_cu (this_cu)
dee91e82 7429{
976ca316 7430 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7431 struct dwarf2_section_info *section = this_cu->section;
96b79293 7432 bfd *abfd = section->get_bfd_owner ();
33e80786 7433 struct dwarf2_section_info *abbrev_section;
d521ce57 7434 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7435
b4f54984 7436 if (dwarf_die_debug)
9d8780f0 7437 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7438 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7439 sect_offset_str (this_cu->sect_off));
09406207 7440
976ca316 7441 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7442
33e80786
DE
7443 abbrev_section = (dwo_file != NULL
7444 ? &dwo_file->sections.abbrev
7445 : get_abbrev_section_for_cu (this_cu));
7446
dee91e82 7447 /* This is cheap if the section is already read in. */
96b79293 7448 section->read (objfile);
dee91e82 7449
976ca316 7450 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7451
9c541725 7452 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7453 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7454 section, abbrev_section, info_ptr,
43988095
JK
7455 (this_cu->is_debug_types
7456 ? rcuh_kind::TYPE
7457 : rcuh_kind::COMPILE));
dee91e82 7458
18a8505e
AT
7459 if (parent_cu != nullptr)
7460 {
c0ab21c2
TT
7461 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7462 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7463 }
4057dfde 7464 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7465
7466 /* Skip dummy compilation units. */
7467 if (info_ptr >= begin_info_ptr + this_cu->length
7468 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7469 {
7470 dummy_p = true;
7471 return;
7472 }
72bf9492 7473
606decb2 7474 abbrev_section->read (objfile);
c0ab21c2 7475 m_abbrev_table_holder
606decb2 7476 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7477
c0ab21c2
TT
7478 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7479 m_abbrev_table_holder.get ());
3e225074 7480 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7481}
7482
0018ea6f
DE
7483\f
7484/* Type Unit Groups.
dee91e82 7485
0018ea6f
DE
7486 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7487 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7488 so that all types coming from the same compilation (.o file) are grouped
7489 together. A future step could be to put the types in the same symtab as
7490 the CU the types ultimately came from. */
ff013f42 7491
f4dc4d17
DE
7492static hashval_t
7493hash_type_unit_group (const void *item)
7494{
9a3c8263
SM
7495 const struct type_unit_group *tu_group
7496 = (const struct type_unit_group *) item;
f4dc4d17 7497
094b34ac 7498 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7499}
348e048f
DE
7500
7501static int
f4dc4d17 7502eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7503{
9a3c8263
SM
7504 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7505 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7506
094b34ac 7507 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7508}
348e048f 7509
f4dc4d17
DE
7510/* Allocate a hash table for type unit groups. */
7511
eaa5fa8b 7512static htab_up
298e9637 7513allocate_type_unit_groups_table ()
f4dc4d17 7514{
eaa5fa8b
TT
7515 return htab_up (htab_create_alloc (3,
7516 hash_type_unit_group,
7517 eq_type_unit_group,
7518 NULL, xcalloc, xfree));
f4dc4d17 7519}
dee91e82 7520
f4dc4d17
DE
7521/* Type units that don't have DW_AT_stmt_list are grouped into their own
7522 partial symtabs. We combine several TUs per psymtab to not let the size
7523 of any one psymtab grow too big. */
7524#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7525#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7526
094b34ac 7527/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7528 Create the type_unit_group object used to hold one or more TUs. */
7529
7530static struct type_unit_group *
094b34ac 7531create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7532{
976ca316
SM
7533 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7534 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7535 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7536 struct type_unit_group *tu_group;
f4dc4d17 7537
976ca316 7538 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7539 per_cu = &tu_group->per_cu;
1859c670 7540 per_cu->per_bfd = per_bfd;
f4dc4d17 7541
1859c670 7542 if (per_bfd->using_index)
094b34ac 7543 {
1859c670 7544 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7545 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7546 }
7547 else
7548 {
9c541725 7549 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7550 dwarf2_psymtab *pst;
528e1572 7551 std::string name;
094b34ac
DE
7552
7553 /* Give the symtab a useful name for debug purposes. */
7554 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7555 name = string_printf ("<type_units_%d>",
7556 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7557 else
528e1572 7558 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7559
976ca316 7560 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7561 pst->anonymous = true;
094b34ac 7562 }
f4dc4d17 7563
094b34ac 7564 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7565 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7566
7567 return tu_group;
7568}
7569
094b34ac
DE
7570/* Look up the type_unit_group for type unit CU, and create it if necessary.
7571 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7572
7573static struct type_unit_group *
ff39bb5e 7574get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7575{
976ca316
SM
7576 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7577 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7578 struct type_unit_group *tu_group;
7579 void **slot;
7580 unsigned int line_offset;
7581 struct type_unit_group type_unit_group_for_lookup;
7582
976ca316
SM
7583 if (per_objfile->per_bfd->type_unit_groups == NULL)
7584 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7585
7586 /* Do we need to create a new group, or can we use an existing one? */
7587
529908cb 7588 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7589 {
529908cb 7590 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7591 ++tu_stats->nr_symtab_sharers;
7592 }
7593 else
7594 {
7595 /* Ugh, no stmt_list. Rare, but we have to handle it.
7596 We can do various things here like create one group per TU or
7597 spread them over multiple groups to split up the expansion work.
7598 To avoid worst case scenarios (too many groups or too large groups)
7599 we, umm, group them in bunches. */
7600 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7601 | (tu_stats->nr_stmt_less_type_units
7602 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7603 ++tu_stats->nr_stmt_less_type_units;
7604 }
7605
094b34ac 7606 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7607 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7608 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7609 &type_unit_group_for_lookup, INSERT);
7610 if (*slot != NULL)
7611 {
9a3c8263 7612 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7613 gdb_assert (tu_group != NULL);
7614 }
7615 else
7616 {
9c541725 7617 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7618 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7619 *slot = tu_group;
7620 ++tu_stats->nr_symtabs;
7621 }
7622
7623 return tu_group;
7624}
0018ea6f
DE
7625\f
7626/* Partial symbol tables. */
7627
7628/* Create a psymtab named NAME and assign it to PER_CU.
7629
7630 The caller must fill in the following details:
7631 dirname, textlow, texthigh. */
7632
891813be 7633static dwarf2_psymtab *
7aa104c4
SM
7634create_partial_symtab (dwarf2_per_cu_data *per_cu,
7635 dwarf2_per_objfile *per_objfile,
7636 const char *name)
0018ea6f 7637{
7aa104c4 7638 struct objfile *objfile = per_objfile->objfile;
891813be 7639 dwarf2_psymtab *pst;
0018ea6f 7640
9f4e76a4 7641 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7642
6d94535f 7643 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7644
7645 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7646 per_cu->v.psymtab = pst;
7647
7648 return pst;
7649}
7650
c0ab21c2 7651/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7652
7653static void
7654process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7655 const gdb_byte *info_ptr,
0018ea6f 7656 struct die_info *comp_unit_die,
c0ab21c2 7657 enum language pretend_language)
0018ea6f
DE
7658{
7659 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7660 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7661 struct objfile *objfile = per_objfile->objfile;
08feed99 7662 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7663 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7664 CORE_ADDR baseaddr;
7665 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7666 dwarf2_psymtab *pst;
3a2b436a 7667 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7668 const char *filename;
0018ea6f 7669
0018ea6f
DE
7670 gdb_assert (! per_cu->is_debug_types);
7671
c0ab21c2 7672 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7673
0018ea6f 7674 /* Allocate a new partial symbol table structure. */
2e927613
TV
7675 gdb::unique_xmalloc_ptr<char> debug_filename;
7676 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7677 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7678 if (filename == NULL)
0018ea6f 7679 filename = "";
2e927613
TV
7680 else if (strcmp (filename, artificial) == 0)
7681 {
7682 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7683 sect_offset_str (per_cu->sect_off),
7684 (char *) NULL));
2e927613
TV
7685 filename = debug_filename.get ();
7686 }
0018ea6f 7687
7aa104c4 7688 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7689
7690 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7691 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7692
b3b3bada 7693 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7694
7695 dwarf2_find_base_address (comp_unit_die, cu);
7696
7697 /* Possibly set the default values of LOWPC and HIGHPC from
7698 `DW_AT_ranges'. */
3a2b436a
JK
7699 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7700 &best_highpc, cu, pst);
7701 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7702 {
7703 CORE_ADDR low
7704 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7705 - baseaddr);
7706 CORE_ADDR high
7707 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7708 - baseaddr - 1);
7709 /* Store the contiguous range if it is not empty; it can be
7710 empty for CUs with no code. */
d320c2b5
TT
7711 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7712 low, high, pst);
79748972 7713 }
0018ea6f
DE
7714
7715 /* Check if comp unit has_children.
7716 If so, read the rest of the partial symbols from this comp unit.
7717 If not, there's no more debug_info for this comp unit. */
3e225074 7718 if (comp_unit_die->has_children)
0018ea6f
DE
7719 {
7720 struct partial_die_info *first_die;
7721 CORE_ADDR lowpc, highpc;
7722
7723 lowpc = ((CORE_ADDR) -1);
7724 highpc = ((CORE_ADDR) 0);
7725
7726 first_die = load_partial_dies (reader, info_ptr, 1);
7727
7728 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7729 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7730
7731 /* If we didn't find a lowpc, set it to highpc to avoid
7732 complaints from `maint check'. */
7733 if (lowpc == ((CORE_ADDR) -1))
7734 lowpc = highpc;
7735
7736 /* If the compilation unit didn't have an explicit address range,
7737 then use the information extracted from its child dies. */
e385593e 7738 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7739 {
7740 best_lowpc = lowpc;
7741 best_highpc = highpc;
7742 }
7743 }
4ae976d1 7744 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7745 best_lowpc + baseaddr)
7746 - baseaddr);
4ae976d1 7747 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7748 best_highpc + baseaddr)
7749 - baseaddr);
0018ea6f 7750
ae7754b2 7751 pst->end ();
0018ea6f 7752
ae640021 7753 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7754 {
7755 int i;
ae640021 7756 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7757
7758 /* Fill in 'dependencies' here; we fill in 'users' in a
7759 post-pass. */
7760 pst->number_of_dependencies = len;
a9342b62
TT
7761 pst->dependencies
7762 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7763 for (i = 0; i < len; ++i)
7764 {
7765 pst->dependencies[i]
7766 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7767 }
0018ea6f 7768
ae640021 7769 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7770 }
7771
7772 /* Get the list of files included in the current compilation unit,
7773 and build a psymtab for each of them. */
7774 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7775
6f738b01
SM
7776 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7777 ", %d global, %d static syms",
7778 per_cu->is_debug_types ? "type" : "comp",
7779 sect_offset_str (per_cu->sect_off),
7780 paddress (gdbarch, pst->text_low (objfile)),
7781 paddress (gdbarch, pst->text_high (objfile)),
7782 (int) pst->global_psymbols.size (),
7783 (int) pst->static_psymbols.size ());
0018ea6f
DE
7784}
7785
7786/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7787 Process compilation unit THIS_CU for a psymtab. */
7788
7789static void
ab432490
SM
7790process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7791 dwarf2_per_objfile *per_objfile,
135f5437 7792 bool want_partial_unit,
b93601f3 7793 enum language pretend_language)
0018ea6f
DE
7794{
7795 /* If this compilation unit was already read in, free the
7796 cached copy in order to read it in again. This is
7797 necessary because we skipped some symbols when we first
7798 read in the compilation unit (see load_partial_dies).
7799 This problem could be avoided, but the benefit is unclear. */
7188ed02 7800 per_objfile->remove_cu (this_cu);
0018ea6f 7801
2e671100 7802 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7803
58990295
TV
7804 switch (reader.comp_unit_die->tag)
7805 {
7806 case DW_TAG_compile_unit:
7807 this_cu->unit_type = DW_UT_compile;
7808 break;
7809 case DW_TAG_partial_unit:
7810 this_cu->unit_type = DW_UT_partial;
7811 break;
7812 default:
7813 abort ();
7814 }
7815
c0ab21c2 7816 if (reader.dummy_p)
f1902523 7817 {
c0ab21c2 7818 /* Nothing. */
f1902523 7819 }
c0ab21c2 7820 else if (this_cu->is_debug_types)
3e225074
TT
7821 build_type_psymtabs_reader (&reader, reader.info_ptr,
7822 reader.comp_unit_die);
135f5437
TT
7823 else if (want_partial_unit
7824 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7825 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7826 reader.comp_unit_die,
c0ab21c2 7827 pretend_language);
0018ea6f 7828
7188ed02 7829 this_cu->lang = reader.cu->language;
58990295 7830
0018ea6f 7831 /* Age out any secondary CUs. */
7188ed02 7832 per_objfile->age_comp_units ();
0018ea6f 7833}
f4dc4d17
DE
7834
7835/* Reader function for build_type_psymtabs. */
7836
7837static void
7838build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7839 const gdb_byte *info_ptr,
3e225074 7840 struct die_info *type_unit_die)
f4dc4d17 7841{
976ca316 7842 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7843 struct dwarf2_cu *cu = reader->cu;
7844 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7845 struct signatured_type *sig_type;
f4dc4d17
DE
7846 struct type_unit_group *tu_group;
7847 struct attribute *attr;
7848 struct partial_die_info *first_die;
7849 CORE_ADDR lowpc, highpc;
891813be 7850 dwarf2_psymtab *pst;
f4dc4d17 7851
0186c6a7
DE
7852 gdb_assert (per_cu->is_debug_types);
7853 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7854
3e225074 7855 if (! type_unit_die->has_children)
f4dc4d17
DE
7856 return;
7857
052c8bb8 7858 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7859 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7860
df07e2c7 7861 if (tu_group->tus == nullptr)
a8b3b8e9 7862 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7863 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7864
7865 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7866 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7867 pst->anonymous = true;
f4dc4d17
DE
7868
7869 first_die = load_partial_dies (reader, info_ptr, 1);
7870
7871 lowpc = (CORE_ADDR) -1;
7872 highpc = (CORE_ADDR) 0;
7873 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7874
ae7754b2 7875 pst->end ();
f4dc4d17
DE
7876}
7877
73051182
DE
7878/* Struct used to sort TUs by their abbreviation table offset. */
7879
7880struct tu_abbrev_offset
7881{
b2bdb8cf
SM
7882 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7883 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7884 {}
7885
7886 signatured_type *sig_type;
73051182
DE
7887 sect_offset abbrev_offset;
7888};
7889
484cf504 7890/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7891
484cf504
TT
7892static bool
7893sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7894 const struct tu_abbrev_offset &b)
73051182 7895{
484cf504 7896 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7897}
7898
7899/* Efficiently read all the type units.
7900 This does the bulk of the work for build_type_psymtabs.
7901
7902 The efficiency is because we sort TUs by the abbrev table they use and
7903 only read each abbrev table once. In one program there are 200K TUs
7904 sharing 8K abbrev tables.
7905
7906 The main purpose of this function is to support building the
5989a64e 7907 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7908 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7909 can collapse the search space by grouping them by stmt_list.
7910 The savings can be significant, in the same program from above the 200K TUs
7911 share 8K stmt_list tables.
7912
7913 FUNC is expected to call get_type_unit_group, which will create the
7914 struct type_unit_group if necessary and add it to
5989a64e 7915 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7916
7917static void
976ca316 7918build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7919{
976ca316 7920 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7921 abbrev_table_up abbrev_table;
73051182 7922 sect_offset abbrev_offset;
73051182
DE
7923
7924 /* It's up to the caller to not call us multiple times. */
976ca316 7925 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7926
976ca316 7927 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7928 return;
7929
7930 /* TUs typically share abbrev tables, and there can be way more TUs than
7931 abbrev tables. Sort by abbrev table to reduce the number of times we
7932 read each abbrev table in.
7933 Alternatives are to punt or to maintain a cache of abbrev tables.
7934 This is simpler and efficient enough for now.
7935
7936 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7937 symtab to use). Typically TUs with the same abbrev offset have the same
7938 stmt_list value too so in practice this should work well.
7939
7940 The basic algorithm here is:
7941
7942 sort TUs by abbrev table
7943 for each TU with same abbrev table:
7944 read abbrev table if first user
7945 read TU top level DIE
7946 [IWBN if DWO skeletons had DW_AT_stmt_list]
7947 call FUNC */
7948
6f738b01 7949 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7950
7951 /* Sort in a separate table to maintain the order of all_type_units
7952 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7953 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7954 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7955
976ca316 7956 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7957 sorted_by_abbrev.emplace_back
976ca316 7958 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7959 sig_type->per_cu.sect_off));
73051182 7960
484cf504
TT
7961 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7962 sort_tu_by_abbrev_offset);
73051182 7963
9c541725 7964 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7965
b2bdb8cf 7966 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7967 {
73051182
DE
7968 /* Switch to the next abbrev table if necessary. */
7969 if (abbrev_table == NULL
b2bdb8cf 7970 || tu.abbrev_offset != abbrev_offset)
73051182 7971 {
b2bdb8cf 7972 abbrev_offset = tu.abbrev_offset;
606decb2 7973 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7974 abbrev_table =
606decb2 7975 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7976 ++tu_stats->nr_uniq_abbrev_tables;
7977 }
7978
976ca316 7979 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7980 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7981 if (!reader.dummy_p)
7982 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7983 reader.comp_unit_die);
73051182 7984 }
6aa5f3a6 7985}
73051182 7986
6aa5f3a6
DE
7987/* Print collected type unit statistics. */
7988
7989static void
976ca316 7990print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7991{
976ca316 7992 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7993
6f738b01
SM
7994 dwarf_read_debug_printf ("Type unit statistics:");
7995 dwarf_read_debug_printf (" %zu TUs",
7996 per_objfile->per_bfd->all_type_units.size ());
7997 dwarf_read_debug_printf (" %d uniq abbrev tables",
7998 tu_stats->nr_uniq_abbrev_tables);
7999 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
8000 tu_stats->nr_symtabs);
8001 dwarf_read_debug_printf (" %d symtab sharers",
8002 tu_stats->nr_symtab_sharers);
8003 dwarf_read_debug_printf (" %d type units without a stmt_list",
8004 tu_stats->nr_stmt_less_type_units);
8005 dwarf_read_debug_printf (" %d all_type_units reallocs",
8006 tu_stats->nr_all_type_units_reallocs);
73051182
DE
8007}
8008
f4dc4d17
DE
8009/* Traversal function for build_type_psymtabs. */
8010
8011static int
8012build_type_psymtab_dependencies (void **slot, void *info)
8013{
976ca316
SM
8014 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8015 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 8016 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8017 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8018 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8019 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8020 int i;
8021
8022 gdb_assert (len > 0);
197400e8 8023 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
8024
8025 pst->number_of_dependencies = len;
a9342b62 8026 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8027 for (i = 0; i < len; ++i)
f4dc4d17 8028 {
df07e2c7 8029 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8030 gdb_assert (iter->per_cu.is_debug_types);
8031 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8032 iter->type_unit_group = tu_group;
f4dc4d17
DE
8033 }
8034
df07e2c7
AB
8035 delete tu_group->tus;
8036 tu_group->tus = nullptr;
348e048f
DE
8037
8038 return 1;
8039}
8040
8041/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8042 Build partial symbol tables for the .debug_types comp-units. */
8043
8044static void
976ca316 8045build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 8046{
976ca316 8047 if (! create_all_type_units (per_objfile))
348e048f
DE
8048 return;
8049
976ca316 8050 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 8051}
f4dc4d17 8052
6aa5f3a6
DE
8053/* Traversal function for process_skeletonless_type_unit.
8054 Read a TU in a DWO file and build partial symbols for it. */
8055
8056static int
8057process_skeletonless_type_unit (void **slot, void *info)
8058{
8059 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 8060 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
8061 struct signatured_type find_entry, *entry;
8062
8063 /* If this TU doesn't exist in the global table, add it and read it in. */
8064
976ca316
SM
8065 if (per_objfile->per_bfd->signatured_types == NULL)
8066 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
8067
8068 find_entry.signature = dwo_unit->signature;
976ca316 8069 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 8070 &find_entry, INSERT);
6aa5f3a6
DE
8071 /* If we've already seen this type there's nothing to do. What's happening
8072 is we're doing our own version of comdat-folding here. */
8073 if (*slot != NULL)
8074 return 1;
8075
8076 /* This does the job that create_all_type_units would have done for
8077 this TU. */
976ca316
SM
8078 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8079 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8080 *slot = entry;
8081
8082 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 8083 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
8084 if (!reader.dummy_p)
8085 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8086 reader.comp_unit_die);
6aa5f3a6
DE
8087
8088 return 1;
8089}
8090
8091/* Traversal function for process_skeletonless_type_units. */
8092
8093static int
8094process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8095{
8096 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8097
8098 if (dwo_file->tus != NULL)
b0b6a987
TT
8099 htab_traverse_noresize (dwo_file->tus.get (),
8100 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8101
8102 return 1;
8103}
8104
8105/* Scan all TUs of DWO files, verifying we've processed them.
8106 This is needed in case a TU was emitted without its skeleton.
8107 Note: This can't be done until we know what all the DWO files are. */
8108
8109static void
976ca316 8110process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8111{
8112 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8113 if (get_dwp_file (per_objfile) == NULL
8114 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8115 {
976ca316 8116 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8117 process_dwo_file_for_skeletonless_type_units,
976ca316 8118 per_objfile);
6aa5f3a6 8119 }
348e048f
DE
8120}
8121
ed2dc618 8122/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8123
8124static void
976ca316 8125set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8126{
976ca316 8127 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8128 {
891813be 8129 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8130
36586728
TT
8131 if (pst == NULL)
8132 continue;
8133
b76e467d 8134 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8135 {
8136 /* Set the 'user' field only if it is not already set. */
8137 if (pst->dependencies[j]->user == NULL)
8138 pst->dependencies[j]->user = pst;
8139 }
8140 }
8141}
8142
93311388
DE
8143/* Build the partial symbol table by doing a quick pass through the
8144 .debug_info and .debug_abbrev sections. */
72bf9492 8145
93311388 8146static void
976ca316 8147dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8148{
976ca316 8149 struct objfile *objfile = per_objfile->objfile;
93311388 8150
6f738b01
SM
8151 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8152 objfile_name (objfile));
45cfd468 8153
76935768 8154 scoped_restore restore_reading_psyms
976ca316 8155 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8156 true);
98bfdba5 8157
976ca316 8158 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8159
93311388
DE
8160 /* Any cached compilation units will be linked by the per-objfile
8161 read_in_chain. Make sure to free them when we're done. */
976ca316 8162 free_cached_comp_units freer (per_objfile);
72bf9492 8163
976ca316 8164 build_type_psymtabs (per_objfile);
348e048f 8165
976ca316 8166 create_all_comp_units (per_objfile);
c906108c 8167
60606b2c
TT
8168 /* Create a temporary address map on a temporary obstack. We later
8169 copy this to the final obstack. */
8268c778 8170 auto_obstack temp_obstack;
791afaa2
TT
8171
8172 scoped_restore save_psymtabs_addrmap
d320c2b5 8173 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8174 addrmap_create_mutable (&temp_obstack));
72bf9492 8175
976ca316 8176 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8177 {
8178 if (per_cu->v.psymtab != NULL)
8179 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8180 continue;
976ca316 8181 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8182 language_minimal);
3d5afab3 8183 }
ff013f42 8184
6aa5f3a6 8185 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8186 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8187
8188 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8189 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8190 {
976ca316
SM
8191 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8192 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8193 }
8194
6f738b01 8195 if (dwarf_read_debug > 0)
976ca316 8196 print_tu_stats (per_objfile);
6aa5f3a6 8197
976ca316 8198 set_partial_user (per_objfile);
95554aad 8199
d320c2b5
TT
8200 objfile->partial_symtabs->psymtabs_addrmap
8201 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8202 objfile->partial_symtabs->obstack ());
791afaa2
TT
8203 /* At this point we want to keep the address map. */
8204 save_psymtabs_addrmap.release ();
ff013f42 8205
6f738b01
SM
8206 dwarf_read_debug_printf ("Done building psymtabs of %s",
8207 objfile_name (objfile));
ae038cb0
DJ
8208}
8209
dee91e82
DE
8210/* Load the partial DIEs for a secondary CU into memory.
8211 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8212
dee91e82 8213static void
ab432490 8214load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8215 dwarf2_per_objfile *per_objfile,
8216 dwarf2_cu *existing_cu)
dee91e82 8217{
2e671100 8218 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8219
8220 if (!reader.dummy_p)
8221 {
8222 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8223 language_minimal);
8224
8225 /* Check if comp unit has_children.
8226 If so, read the rest of the partial symbols from this comp unit.
8227 If not, there's no more debug_info for this comp unit. */
3e225074 8228 if (reader.comp_unit_die->has_children)
c0ab21c2 8229 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8230
8231 reader.keep ();
c0ab21c2 8232 }
ae038cb0
DJ
8233}
8234
ae038cb0 8235static void
976ca316 8236read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8237 struct dwarf2_section_info *section,
f1902523 8238 struct dwarf2_section_info *abbrev_section,
b76e467d 8239 unsigned int is_dwz)
ae038cb0 8240{
d521ce57 8241 const gdb_byte *info_ptr;
976ca316 8242 struct objfile *objfile = per_objfile->objfile;
be391dca 8243
6f738b01
SM
8244 dwarf_read_debug_printf ("Reading %s for %s",
8245 section->get_name (),
8246 section->get_file_name ());
bf6af496 8247
96b79293 8248 section->read (objfile);
ae038cb0 8249
36586728 8250 info_ptr = section->buffer;
6e70227d 8251
36586728 8252 while (info_ptr < section->buffer + section->size)
ae038cb0 8253 {
ae038cb0 8254 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8255
9c541725 8256 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8257
f1902523 8258 comp_unit_head cu_header;
976ca316 8259 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8260 abbrev_section, info_ptr,
8261 rcuh_kind::COMPILE);
ae038cb0
DJ
8262
8263 /* Save the compilation unit for later lookup. */
f1902523 8264 if (cu_header.unit_type != DW_UT_type)
976ca316 8265 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8266 else
8267 {
976ca316 8268 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8269 sig_type->signature = cu_header.signature;
8270 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8271 this_cu = &sig_type->per_cu;
8272 }
8273 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8274 this_cu->sect_off = sect_off;
f1902523 8275 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8276 this_cu->is_dwz = is_dwz;
8a0459fd 8277 this_cu->section = section;
ae038cb0 8278
976ca316 8279 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8280
8281 info_ptr = info_ptr + this_cu->length;
8282 }
36586728
TT
8283}
8284
8285/* Create a list of all compilation units in OBJFILE.
8286 This is only done for -readnow and building partial symtabs. */
8287
8288static void
976ca316 8289create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8290{
976ca316
SM
8291 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8292 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8293 &per_objfile->per_bfd->abbrev, 0);
36586728 8294
976ca316 8295 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8296 if (dwz != NULL)
976ca316 8297 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8298}
8299
5734ee8b 8300/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8301 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8302 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8303 DW_AT_ranges). See the comments of add_partial_subprogram on how
8304 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8305
72bf9492
DJ
8306static void
8307scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8308 CORE_ADDR *highpc, int set_addrmap,
8309 struct dwarf2_cu *cu)
c906108c 8310{
72bf9492 8311 struct partial_die_info *pdi;
c906108c 8312
91c24f0a
DC
8313 /* Now, march along the PDI's, descending into ones which have
8314 interesting children but skipping the children of the other ones,
8315 until we reach the end of the compilation unit. */
c906108c 8316
72bf9492 8317 pdi = first_die;
91c24f0a 8318
72bf9492
DJ
8319 while (pdi != NULL)
8320 {
52356b79 8321 pdi->fixup (cu);
c906108c 8322
f55ee35c 8323 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8324 children, so we need to look at them. Ditto for anonymous
8325 enums. */
933c6fe4 8326
7d00ffec 8327 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8328 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8329 || pdi->tag == DW_TAG_imported_unit
8330 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8331 {
72bf9492 8332 switch (pdi->tag)
c906108c
SS
8333 {
8334 case DW_TAG_subprogram:
b1dc1806 8335 case DW_TAG_inlined_subroutine:
cdc07690 8336 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8337 if (cu->language == language_cplus)
8338 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8339 set_addrmap, cu);
c906108c 8340 break;
72929c62 8341 case DW_TAG_constant:
c906108c
SS
8342 case DW_TAG_variable:
8343 case DW_TAG_typedef:
91c24f0a 8344 case DW_TAG_union_type:
317d2668
TV
8345 if (!pdi->is_declaration
8346 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8347 {
72bf9492 8348 add_partial_symbol (pdi, cu);
63d06c5c
DC
8349 }
8350 break;
c906108c 8351 case DW_TAG_class_type:
680b30c7 8352 case DW_TAG_interface_type:
c906108c 8353 case DW_TAG_structure_type:
72bf9492 8354 if (!pdi->is_declaration)
c906108c 8355 {
72bf9492 8356 add_partial_symbol (pdi, cu);
c906108c 8357 }
b7fee5a3
KS
8358 if ((cu->language == language_rust
8359 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8360 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8361 set_addrmap, cu);
c906108c 8362 break;
91c24f0a 8363 case DW_TAG_enumeration_type:
72bf9492
DJ
8364 if (!pdi->is_declaration)
8365 add_partial_enumeration (pdi, cu);
c906108c
SS
8366 break;
8367 case DW_TAG_base_type:
dda83cd7 8368 case DW_TAG_subrange_type:
c906108c 8369 /* File scope base type definitions are added to the partial
dda83cd7 8370 symbol table. */
72bf9492 8371 add_partial_symbol (pdi, cu);
c906108c 8372 break;
d9fa45fe 8373 case DW_TAG_namespace:
cdc07690 8374 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8375 break;
5d7cb8df 8376 case DW_TAG_module:
59c35742
AB
8377 if (!pdi->is_declaration)
8378 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8379 break;
95554aad
TT
8380 case DW_TAG_imported_unit:
8381 {
8382 struct dwarf2_per_cu_data *per_cu;
8383
f4dc4d17
DE
8384 /* For now we don't handle imported units in type units. */
8385 if (cu->per_cu->is_debug_types)
8386 {
8387 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8388 " supported in type units [in module %s]"),
5e22e966 8389 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8390 }
8391
e3b94546 8392 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8393 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8394
8395 /* Go read the partial unit, if needed. */
8396 if (per_cu->v.psymtab == NULL)
ab432490
SM
8397 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8398 cu->language);
95554aad 8399
ae640021 8400 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8401 }
8402 break;
74921315
KS
8403 case DW_TAG_imported_declaration:
8404 add_partial_symbol (pdi, cu);
8405 break;
c906108c
SS
8406 default:
8407 break;
8408 }
8409 }
8410
72bf9492
DJ
8411 /* If the die has a sibling, skip to the sibling. */
8412
8413 pdi = pdi->die_sibling;
8414 }
8415}
8416
8417/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8418
72bf9492 8419 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8420 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8421 Enumerators are an exception; they use the scope of their parent
8422 enumeration type, i.e. the name of the enumeration type is not
8423 prepended to the enumerator.
91c24f0a 8424
72bf9492
DJ
8425 There are two complexities. One is DW_AT_specification; in this
8426 case "parent" means the parent of the target of the specification,
8427 instead of the direct parent of the DIE. The other is compilers
8428 which do not emit DW_TAG_namespace; in this case we try to guess
8429 the fully qualified name of structure types from their members'
8430 linkage names. This must be done using the DIE's children rather
8431 than the children of any DW_AT_specification target. We only need
8432 to do this for structures at the top level, i.e. if the target of
8433 any DW_AT_specification (if any; otherwise the DIE itself) does not
8434 have a parent. */
8435
8436/* Compute the scope prefix associated with PDI's parent, in
8437 compilation unit CU. The result will be allocated on CU's
8438 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8439 field. NULL is returned if no prefix is necessary. */
15d034d0 8440static const char *
72bf9492
DJ
8441partial_die_parent_scope (struct partial_die_info *pdi,
8442 struct dwarf2_cu *cu)
8443{
15d034d0 8444 const char *grandparent_scope;
72bf9492 8445 struct partial_die_info *parent, *real_pdi;
91c24f0a 8446
72bf9492
DJ
8447 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8448 then this means the parent of the specification DIE. */
8449
8450 real_pdi = pdi;
72bf9492 8451 while (real_pdi->has_specification)
fb816e8b 8452 {
122cf0f2
AB
8453 auto res = find_partial_die (real_pdi->spec_offset,
8454 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8455 real_pdi = res.pdi;
8456 cu = res.cu;
8457 }
72bf9492
DJ
8458
8459 parent = real_pdi->die_parent;
8460 if (parent == NULL)
8461 return NULL;
8462
8463 if (parent->scope_set)
8464 return parent->scope;
8465
52356b79 8466 parent->fixup (cu);
72bf9492 8467
10b3939b 8468 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8469
acebe513
UW
8470 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8471 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8472 Work around this problem here. */
8473 if (cu->language == language_cplus
6e70227d 8474 && parent->tag == DW_TAG_namespace
7d00ffec 8475 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8476 && grandparent_scope == NULL)
8477 {
8478 parent->scope = NULL;
8479 parent->scope_set = 1;
8480 return NULL;
8481 }
8482
0a4b0913 8483 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8484 if (pdi->tag == DW_TAG_enumerator)
8485 /* Enumerators should not get the name of the enumeration as a prefix. */
8486 parent->scope = grandparent_scope;
8487 else if (parent->tag == DW_TAG_namespace
f55ee35c 8488 || parent->tag == DW_TAG_module
72bf9492
DJ
8489 || parent->tag == DW_TAG_structure_type
8490 || parent->tag == DW_TAG_class_type
680b30c7 8491 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8492 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8493 || parent->tag == DW_TAG_enumeration_type
8494 || (cu->language == language_fortran
8495 && parent->tag == DW_TAG_subprogram
8496 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8497 {
8498 if (grandparent_scope == NULL)
7d00ffec 8499 parent->scope = parent->name (cu);
72bf9492 8500 else
3e43a32a
MS
8501 parent->scope = typename_concat (&cu->comp_unit_obstack,
8502 grandparent_scope,
7d00ffec 8503 parent->name (cu), 0, cu);
72bf9492 8504 }
72bf9492
DJ
8505 else
8506 {
8507 /* FIXME drow/2004-04-01: What should we be doing with
8508 function-local names? For partial symbols, we should probably be
8509 ignoring them. */
fa9c3fa0
TT
8510 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8511 dwarf_tag_name (parent->tag),
8512 sect_offset_str (pdi->sect_off));
72bf9492 8513 parent->scope = grandparent_scope;
c906108c
SS
8514 }
8515
72bf9492
DJ
8516 parent->scope_set = 1;
8517 return parent->scope;
8518}
8519
8520/* Return the fully scoped name associated with PDI, from compilation unit
8521 CU. The result will be allocated with malloc. */
4568ecf9 8522
43816ebc 8523static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8524partial_die_full_name (struct partial_die_info *pdi,
8525 struct dwarf2_cu *cu)
8526{
15d034d0 8527 const char *parent_scope;
72bf9492 8528
98bfdba5
PA
8529 /* If this is a template instantiation, we can not work out the
8530 template arguments from partial DIEs. So, unfortunately, we have
8531 to go through the full DIEs. At least any work we do building
8532 types here will be reused if full symbols are loaded later. */
8533 if (pdi->has_template_arguments)
8534 {
52356b79 8535 pdi->fixup (cu);
98bfdba5 8536
7d00ffec 8537 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8538 {
8539 struct die_info *die;
8540 struct attribute attr;
8541 struct dwarf2_cu *ref_cu = cu;
8542
b64f50a1 8543 /* DW_FORM_ref_addr is using section offset. */
b4069958 8544 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8545 attr.form = DW_FORM_ref_addr;
9c541725 8546 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8547 die = follow_die_ref (NULL, &attr, &ref_cu);
8548
43816ebc 8549 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8550 }
8551 }
8552
72bf9492
DJ
8553 parent_scope = partial_die_parent_scope (pdi, cu);
8554 if (parent_scope == NULL)
8555 return NULL;
8556 else
43816ebc 8557 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8558 pdi->name (cu),
8559 0, cu));
c906108c
SS
8560}
8561
8562static void
72bf9492 8563add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8564{
976ca316
SM
8565 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8566 struct objfile *objfile = per_objfile->objfile;
08feed99 8567 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8568 CORE_ADDR addr = 0;
15d034d0 8569 const char *actual_name = NULL;
e142c38c
DJ
8570 CORE_ADDR baseaddr;
8571
b3b3bada 8572 baseaddr = objfile->text_section_offset ();
c906108c 8573
43816ebc
TT
8574 gdb::unique_xmalloc_ptr<char> built_actual_name
8575 = partial_die_full_name (pdi, cu);
15d034d0 8576 if (built_actual_name != NULL)
43816ebc 8577 actual_name = built_actual_name.get ();
63d06c5c 8578
72bf9492 8579 if (actual_name == NULL)
7d00ffec 8580 actual_name = pdi->name (cu);
72bf9492 8581
76e288d1
TT
8582 partial_symbol psymbol;
8583 memset (&psymbol, 0, sizeof (psymbol));
8584 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8585 psymbol.ginfo.section = -1;
8586
8587 /* The code below indicates that the psymbol should be installed by
8588 setting this. */
8589 gdb::optional<psymbol_placement> where;
8590
c906108c
SS
8591 switch (pdi->tag)
8592 {
b1dc1806 8593 case DW_TAG_inlined_subroutine:
c906108c 8594 case DW_TAG_subprogram:
79748972
TT
8595 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8596 - baseaddr);
0a4b0913
AB
8597 if (pdi->is_external
8598 || cu->language == language_ada
8599 || (cu->language == language_fortran
8600 && pdi->die_parent != NULL
8601 && pdi->die_parent->tag == DW_TAG_subprogram))
8602 {
dda83cd7
SM
8603 /* Normally, only "external" DIEs are part of the global scope.
8604 But in Ada and Fortran, we want to be able to access nested
8605 procedures globally. So all Ada and Fortran subprograms are
8606 stored in the global scope. */
76e288d1 8607 where = psymbol_placement::GLOBAL;
c906108c
SS
8608 }
8609 else
76e288d1
TT
8610 where = psymbol_placement::STATIC;
8611
8612 psymbol.domain = VAR_DOMAIN;
8613 psymbol.aclass = LOC_BLOCK;
8614 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8615 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8616
8617 if (pdi->main_subprogram && actual_name != NULL)
8618 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8619 break;
72929c62 8620 case DW_TAG_constant:
76e288d1
TT
8621 psymbol.domain = VAR_DOMAIN;
8622 psymbol.aclass = LOC_STATIC;
8623 where = (pdi->is_external
8624 ? psymbol_placement::GLOBAL
8625 : psymbol_placement::STATIC);
72929c62 8626 break;
c906108c 8627 case DW_TAG_variable:
95554aad
TT
8628 if (pdi->d.locdesc)
8629 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8630
95554aad 8631 if (pdi->d.locdesc
caac4577 8632 && addr == 0
976ca316 8633 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8634 {
8635 /* A global or static variable may also have been stripped
8636 out by the linker if unused, in which case its address
8637 will be nullified; do not add such variables into partial
8638 symbol table then. */
8639 }
8640 else if (pdi->is_external)
c906108c
SS
8641 {
8642 /* Global Variable.
8643 Don't enter into the minimal symbol tables as there is
8644 a minimal symbol table entry from the ELF symbols already.
8645 Enter into partial symbol table if it has a location
8646 descriptor or a type.
8647 If the location descriptor is missing, new_symbol will create
8648 a LOC_UNRESOLVED symbol, the address of the variable will then
8649 be determined from the minimal symbol table whenever the variable
8650 is referenced.
8651 The address for the partial symbol table entry is not
8652 used by GDB, but it comes in handy for debugging partial symbol
8653 table building. */
8654
95554aad 8655 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8656 {
8657 psymbol.domain = VAR_DOMAIN;
8658 psymbol.aclass = LOC_STATIC;
8659 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8660 psymbol.ginfo.value.address = addr;
8661 where = psymbol_placement::GLOBAL;
8662 }
c906108c
SS
8663 }
8664 else
8665 {
ff908ebf
AW
8666 int has_loc = pdi->d.locdesc != NULL;
8667
8668 /* Static Variable. Skip symbols whose value we cannot know (those
8669 without location descriptors or constant values). */
8670 if (!has_loc && !pdi->has_const_value)
43816ebc 8671 return;
ff908ebf 8672
76e288d1
TT
8673 psymbol.domain = VAR_DOMAIN;
8674 psymbol.aclass = LOC_STATIC;
8675 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8676 if (has_loc)
8677 psymbol.ginfo.value.address = addr;
8678 where = psymbol_placement::STATIC;
c906108c
SS
8679 }
8680 break;
d8f62e84 8681 case DW_TAG_array_type:
c906108c
SS
8682 case DW_TAG_typedef:
8683 case DW_TAG_base_type:
a02abb62 8684 case DW_TAG_subrange_type:
76e288d1
TT
8685 psymbol.domain = VAR_DOMAIN;
8686 psymbol.aclass = LOC_TYPEDEF;
8687 where = psymbol_placement::STATIC;
c906108c 8688 break;
74921315 8689 case DW_TAG_imported_declaration:
72bf9492 8690 case DW_TAG_namespace:
76e288d1
TT
8691 psymbol.domain = VAR_DOMAIN;
8692 psymbol.aclass = LOC_TYPEDEF;
8693 where = psymbol_placement::GLOBAL;
72bf9492 8694 break;
530e8392 8695 case DW_TAG_module:
a5fd13a9 8696 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8697 available without any name. If so, we skip the module as it
8698 doesn't bring any value. */
a5fd13a9 8699 if (actual_name != nullptr)
76e288d1
TT
8700 {
8701 psymbol.domain = MODULE_DOMAIN;
8702 psymbol.aclass = LOC_TYPEDEF;
8703 where = psymbol_placement::GLOBAL;
8704 }
530e8392 8705 break;
c906108c 8706 case DW_TAG_class_type:
680b30c7 8707 case DW_TAG_interface_type:
c906108c
SS
8708 case DW_TAG_structure_type:
8709 case DW_TAG_union_type:
8710 case DW_TAG_enumeration_type:
fa4028e9 8711 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8712 about "Structure, Union, and Class Type Entries": "An incomplete
8713 structure, union or class type is represented by a structure,
8714 union or class entry that does not have a byte size attribute
8715 and that has a DW_AT_declaration attribute." */
fa4028e9 8716 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8717 return;
fa4028e9 8718
63d06c5c
DC
8719 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8720 static vs. global. */
76e288d1
TT
8721 psymbol.domain = STRUCT_DOMAIN;
8722 psymbol.aclass = LOC_TYPEDEF;
8723 where = (cu->language == language_cplus
8724 ? psymbol_placement::GLOBAL
8725 : psymbol_placement::STATIC);
c906108c
SS
8726 break;
8727 case DW_TAG_enumerator:
76e288d1
TT
8728 psymbol.domain = VAR_DOMAIN;
8729 psymbol.aclass = LOC_CONST;
8730 where = (cu->language == language_cplus
8731 ? psymbol_placement::GLOBAL
8732 : psymbol_placement::STATIC);
c906108c
SS
8733 break;
8734 default:
8735 break;
8736 }
76e288d1
TT
8737
8738 if (where.has_value ())
8739 {
f049a313
TT
8740 if (built_actual_name != nullptr)
8741 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8742 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8743 psymbol.ginfo.set_linkage_name (actual_name);
8744 else
8745 {
8746 psymbol.ginfo.set_demangled_name (actual_name,
8747 &objfile->objfile_obstack);
8748 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8749 }
932539d7 8750 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8751 }
c906108c
SS
8752}
8753
5c4e30ca
DC
8754/* Read a partial die corresponding to a namespace; also, add a symbol
8755 corresponding to that namespace to the symbol table. NAMESPACE is
8756 the name of the enclosing namespace. */
91c24f0a 8757
72bf9492
DJ
8758static void
8759add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8760 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8761 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8762{
72bf9492 8763 /* Add a symbol for the namespace. */
e7c27a73 8764
72bf9492 8765 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8766
8767 /* Now scan partial symbols in that namespace. */
8768
91c24f0a 8769 if (pdi->has_children)
cdc07690 8770 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8771}
8772
5d7cb8df
JK
8773/* Read a partial die corresponding to a Fortran module. */
8774
8775static void
8776add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8777 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8778{
530e8392
KB
8779 /* Add a symbol for the namespace. */
8780
8781 add_partial_symbol (pdi, cu);
8782
f55ee35c 8783 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8784
8785 if (pdi->has_children)
cdc07690 8786 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8787}
8788
b1dc1806
XR
8789/* Read a partial die corresponding to a subprogram or an inlined
8790 subprogram and create a partial symbol for that subprogram.
8791 When the CU language allows it, this routine also defines a partial
8792 symbol for each nested subprogram that this subprogram contains.
8793 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8794 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8795
cdc07690
YQ
8796 PDI may also be a lexical block, in which case we simply search
8797 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8798 Again, this is only performed when the CU language allows this
8799 type of definitions. */
8800
8801static void
8802add_partial_subprogram (struct partial_die_info *pdi,
8803 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8804 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8805{
b1dc1806 8806 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8807 {
8808 if (pdi->has_pc_info)
dda83cd7
SM
8809 {
8810 if (pdi->lowpc < *lowpc)
8811 *lowpc = pdi->lowpc;
8812 if (pdi->highpc > *highpc)
8813 *highpc = pdi->highpc;
cdc07690 8814 if (set_addrmap)
5734ee8b 8815 {
5e22e966 8816 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8817 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8818 CORE_ADDR baseaddr;
b926417a
TT
8819 CORE_ADDR this_highpc;
8820 CORE_ADDR this_lowpc;
5734ee8b 8821
b3b3bada 8822 baseaddr = objfile->text_section_offset ();
b926417a
TT
8823 this_lowpc
8824 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8825 pdi->lowpc + baseaddr)
8826 - baseaddr);
8827 this_highpc
8828 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8829 pdi->highpc + baseaddr)
8830 - baseaddr);
d320c2b5 8831 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8832 this_lowpc, this_highpc - 1,
9291a0cd 8833 cu->per_cu->v.psymtab);
5734ee8b 8834 }
dda83cd7 8835 }
481860b3
GB
8836
8837 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8838 {
dda83cd7 8839 if (!pdi->is_declaration)
e8d05480
JB
8840 /* Ignore subprogram DIEs that do not have a name, they are
8841 illegal. Do not emit a complaint at this point, we will
8842 do so when we convert this psymtab into a symtab. */
7d00ffec 8843 if (pdi->name (cu))
e8d05480 8844 add_partial_symbol (pdi, cu);
dda83cd7 8845 }
bc30ff58 8846 }
6e70227d 8847
bc30ff58
JB
8848 if (! pdi->has_children)
8849 return;
8850
0a4b0913 8851 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8852 {
8853 pdi = pdi->die_child;
8854 while (pdi != NULL)
8855 {
52356b79 8856 pdi->fixup (cu);
bc30ff58 8857 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8858 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8859 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8860 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8861 pdi = pdi->die_sibling;
8862 }
8863 }
8864}
8865
91c24f0a
DC
8866/* Read a partial die corresponding to an enumeration type. */
8867
72bf9492
DJ
8868static void
8869add_partial_enumeration (struct partial_die_info *enum_pdi,
8870 struct dwarf2_cu *cu)
91c24f0a 8871{
72bf9492 8872 struct partial_die_info *pdi;
91c24f0a 8873
7d00ffec 8874 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8875 add_partial_symbol (enum_pdi, cu);
8876
8877 pdi = enum_pdi->die_child;
8878 while (pdi)
91c24f0a 8879 {
7d00ffec 8880 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8881 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8882 else
72bf9492
DJ
8883 add_partial_symbol (pdi, cu);
8884 pdi = pdi->die_sibling;
91c24f0a 8885 }
91c24f0a
DC
8886}
8887
6caca83c
CC
8888/* Return the initial uleb128 in the die at INFO_PTR. */
8889
8890static unsigned int
d521ce57 8891peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8892{
8893 unsigned int bytes_read;
8894
8895 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8896}
8897
685af9cd
TT
8898/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8899 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8900
4bb7a0a7
DJ
8901 Return the corresponding abbrev, or NULL if the number is zero (indicating
8902 an empty DIE). In either case *BYTES_READ will be set to the length of
8903 the initial number. */
8904
8905static struct abbrev_info *
685af9cd
TT
8906peek_die_abbrev (const die_reader_specs &reader,
8907 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8908{
685af9cd 8909 dwarf2_cu *cu = reader.cu;
5e22e966 8910 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8911 unsigned int abbrev_number
8912 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8913
8914 if (abbrev_number == 0)
8915 return NULL;
8916
685af9cd 8917 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8918 if (!abbrev)
8919 {
422b9917 8920 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8921 " at offset %s [in module %s]"),
422b9917 8922 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8923 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8924 }
8925
8926 return abbrev;
8927}
8928
93311388
DE
8929/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8930 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8931 DIE. Any children of the skipped DIEs will also be skipped. */
8932
d521ce57
TT
8933static const gdb_byte *
8934skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8935{
4bb7a0a7
DJ
8936 while (1)
8937 {
685af9cd
TT
8938 unsigned int bytes_read;
8939 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8940
4bb7a0a7
DJ
8941 if (abbrev == NULL)
8942 return info_ptr + bytes_read;
8943 else
dee91e82 8944 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8945 }
8946}
8947
93311388
DE
8948/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8949 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8950 abbrev corresponding to that skipped uleb128 should be passed in
8951 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8952 children. */
8953
d521ce57
TT
8954static const gdb_byte *
8955skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8956 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8957{
8958 unsigned int bytes_read;
8959 struct attribute attr;
dee91e82
DE
8960 bfd *abfd = reader->abfd;
8961 struct dwarf2_cu *cu = reader->cu;
d521ce57 8962 const gdb_byte *buffer = reader->buffer;
f664829e 8963 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8964 unsigned int form, i;
8965
8966 for (i = 0; i < abbrev->num_attrs; i++)
8967 {
8968 /* The only abbrev we care about is DW_AT_sibling. */
8969 if (abbrev->attrs[i].name == DW_AT_sibling)
8970 {
7a5f294d 8971 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8972 if (attr.form == DW_FORM_ref_addr)
b98664d3 8973 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8974 else
b9502d3f 8975 {
0826b30a 8976 sect_offset off = attr.get_ref_die_offset ();
9c541725 8977 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8978
8979 if (sibling_ptr < info_ptr)
b98664d3 8980 complaint (_("DW_AT_sibling points backwards"));
22869d73 8981 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8982 reader->die_section->overflow_complaint ();
b9502d3f
WN
8983 else
8984 return sibling_ptr;
8985 }
4bb7a0a7
DJ
8986 }
8987
8988 /* If it isn't DW_AT_sibling, skip this attribute. */
8989 form = abbrev->attrs[i].form;
8990 skip_attribute:
8991 switch (form)
8992 {
4bb7a0a7 8993 case DW_FORM_ref_addr:
ae411497
TT
8994 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8995 and later it is offset sized. */
8996 if (cu->header.version == 2)
8997 info_ptr += cu->header.addr_size;
8998 else
8999 info_ptr += cu->header.offset_size;
9000 break;
36586728
TT
9001 case DW_FORM_GNU_ref_alt:
9002 info_ptr += cu->header.offset_size;
9003 break;
ae411497 9004 case DW_FORM_addr:
4bb7a0a7
DJ
9005 info_ptr += cu->header.addr_size;
9006 break;
9007 case DW_FORM_data1:
9008 case DW_FORM_ref1:
9009 case DW_FORM_flag:
8fe0f950 9010 case DW_FORM_strx1:
4bb7a0a7
DJ
9011 info_ptr += 1;
9012 break;
2dc7f7b3 9013 case DW_FORM_flag_present:
43988095 9014 case DW_FORM_implicit_const:
2dc7f7b3 9015 break;
4bb7a0a7
DJ
9016 case DW_FORM_data2:
9017 case DW_FORM_ref2:
8fe0f950 9018 case DW_FORM_strx2:
4bb7a0a7
DJ
9019 info_ptr += 2;
9020 break;
8fe0f950
AT
9021 case DW_FORM_strx3:
9022 info_ptr += 3;
9023 break;
4bb7a0a7
DJ
9024 case DW_FORM_data4:
9025 case DW_FORM_ref4:
8fe0f950 9026 case DW_FORM_strx4:
4bb7a0a7
DJ
9027 info_ptr += 4;
9028 break;
9029 case DW_FORM_data8:
9030 case DW_FORM_ref8:
55f1336d 9031 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9032 info_ptr += 8;
9033 break;
0224619f
JK
9034 case DW_FORM_data16:
9035 info_ptr += 16;
9036 break;
4bb7a0a7 9037 case DW_FORM_string:
9b1c24c8 9038 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9039 info_ptr += bytes_read;
9040 break;
2dc7f7b3 9041 case DW_FORM_sec_offset:
4bb7a0a7 9042 case DW_FORM_strp:
36586728 9043 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9044 info_ptr += cu->header.offset_size;
9045 break;
2dc7f7b3 9046 case DW_FORM_exprloc:
4bb7a0a7
DJ
9047 case DW_FORM_block:
9048 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9049 info_ptr += bytes_read;
9050 break;
9051 case DW_FORM_block1:
9052 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9053 break;
9054 case DW_FORM_block2:
9055 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9056 break;
9057 case DW_FORM_block4:
9058 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9059 break;
336d760d 9060 case DW_FORM_addrx:
cf532bd1 9061 case DW_FORM_strx:
4bb7a0a7
DJ
9062 case DW_FORM_sdata:
9063 case DW_FORM_udata:
9064 case DW_FORM_ref_udata:
3019eac3
DE
9065 case DW_FORM_GNU_addr_index:
9066 case DW_FORM_GNU_str_index:
18a8505e 9067 case DW_FORM_rnglistx:
41144253 9068 case DW_FORM_loclistx:
d521ce57 9069 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9070 break;
9071 case DW_FORM_indirect:
9072 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9073 info_ptr += bytes_read;
9074 /* We need to continue parsing from here, so just go back to
9075 the top. */
9076 goto skip_attribute;
9077
9078 default:
3e43a32a
MS
9079 error (_("Dwarf Error: Cannot handle %s "
9080 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9081 dwarf_form_name (form),
9082 bfd_get_filename (abfd));
9083 }
9084 }
9085
9086 if (abbrev->has_children)
dee91e82 9087 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9088 else
9089 return info_ptr;
9090}
9091
93311388 9092/* Locate ORIG_PDI's sibling.
dee91e82 9093 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9094
d521ce57 9095static const gdb_byte *
dee91e82
DE
9096locate_pdi_sibling (const struct die_reader_specs *reader,
9097 struct partial_die_info *orig_pdi,
d521ce57 9098 const gdb_byte *info_ptr)
91c24f0a
DC
9099{
9100 /* Do we know the sibling already? */
72bf9492 9101
91c24f0a
DC
9102 if (orig_pdi->sibling)
9103 return orig_pdi->sibling;
9104
9105 /* Are there any children to deal with? */
9106
9107 if (!orig_pdi->has_children)
9108 return info_ptr;
9109
4bb7a0a7 9110 /* Skip the children the long way. */
91c24f0a 9111
dee91e82 9112 return skip_children (reader, info_ptr);
91c24f0a
DC
9113}
9114
257e7a09 9115/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9116 not NULL. */
c906108c 9117
891813be
TT
9118void
9119dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9120{
976ca316 9121 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9122
976ca316 9123 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9124
077cbab2
TT
9125 /* If this psymtab is constructed from a debug-only objfile, the
9126 has_section_at_zero flag will not necessarily be correct. We
9127 can get the correct value for this flag by looking at the data
9128 associated with the (presumably stripped) associated objfile. */
9129 if (objfile->separate_debug_objfile_backlink)
c906108c 9130 {
976ca316 9131 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9132 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9133
976ca316
SM
9134 per_objfile->per_bfd->has_section_at_zero
9135 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9136 }
98bfdba5 9137
8566b89b 9138 expand_psymtab (objfile);
95554aad 9139
976ca316 9140 process_cu_includes (per_objfile);
c906108c 9141}
9cdd5dbd
DE
9142\f
9143/* Reading in full CUs. */
c906108c 9144
10b3939b
DJ
9145/* Add PER_CU to the queue. */
9146
9147static void
120ce1b5
SM
9148queue_comp_unit (dwarf2_per_cu_data *per_cu,
9149 dwarf2_per_objfile *per_objfile,
95554aad 9150 enum language pretend_language)
10b3939b 9151{
10b3939b 9152 per_cu->queued = 1;
120ce1b5 9153 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9154}
9155
89e63ee4
DE
9156/* If PER_CU is not yet queued, add it to the queue.
9157 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9158 dependency.
0907af0c 9159 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9160 meaning either PER_CU is already queued or it is already loaded.
9161
9162 N.B. There is an invariant here that if a CU is queued then it is loaded.
9163 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9164
9165static int
89e63ee4 9166maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9167 dwarf2_per_cu_data *per_cu,
9168 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9169 enum language pretend_language)
9170{
9171 /* We may arrive here during partial symbol reading, if we need full
9172 DIEs to process an unusual case (e.g. template arguments). Do
9173 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9174 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9175 {
7188ed02
SM
9176 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9177
9178 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9179 return 1;
9180 return 0;
9181 }
9182
9183 /* Mark the dependence relation so that we don't flush PER_CU
9184 too early. */
89e63ee4
DE
9185 if (dependent_cu != NULL)
9186 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9187
9188 /* If it's already on the queue, we have nothing to do. */
9189 if (per_cu->queued)
9190 return 0;
9191
9192 /* If the compilation unit is already loaded, just mark it as
9193 used. */
7188ed02
SM
9194 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9195 if (cu != nullptr)
0907af0c 9196 {
7188ed02 9197 cu->last_used = 0;
0907af0c
DE
9198 return 0;
9199 }
9200
9201 /* Add it to the queue. */
120ce1b5 9202 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9203
9204 return 1;
9205}
9206
10b3939b
DJ
9207/* Process the queue. */
9208
9209static void
976ca316 9210process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9211{
6f738b01
SM
9212 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9213 objfile_name (per_objfile->objfile));
45cfd468 9214
03dd20cc
DJ
9215 /* The queue starts out with one item, but following a DIE reference
9216 may load a new CU, adding it to the end of the queue. */
976ca316 9217 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9218 {
976ca316 9219 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9220 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9221
976ca316 9222 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9223 {
976ca316 9224 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9225
7188ed02
SM
9226 /* Skip dummy CUs. */
9227 if (cu != nullptr)
73be47f5 9228 {
7188ed02
SM
9229 unsigned int debug_print_threshold;
9230 char buf[100];
9231
9232 if (per_cu->is_debug_types)
9233 {
9234 struct signatured_type *sig_type =
9235 (struct signatured_type *) per_cu;
9236
9237 sprintf (buf, "TU %s at offset %s",
9238 hex_string (sig_type->signature),
9239 sect_offset_str (per_cu->sect_off));
9240 /* There can be 100s of TUs.
9241 Only print them in verbose mode. */
9242 debug_print_threshold = 2;
9243 }
9244 else
9245 {
9246 sprintf (buf, "CU at offset %s",
9247 sect_offset_str (per_cu->sect_off));
9248 debug_print_threshold = 1;
9249 }
247f5c4f 9250
7188ed02 9251 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9252 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9253
7188ed02
SM
9254 if (per_cu->is_debug_types)
9255 process_full_type_unit (cu, item.pretend_language);
9256 else
9257 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9258
7188ed02 9259 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9260 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9261 }
f4dc4d17 9262 }
10b3939b 9263
7188ed02 9264 per_cu->queued = 0;
976ca316 9265 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9266 }
9267
6f738b01
SM
9268 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9269 objfile_name (per_objfile->objfile));
10b3939b
DJ
9270}
9271
10b3939b
DJ
9272/* Read in full symbols for PST, and anything it depends on. */
9273
8566b89b
TT
9274void
9275dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9276{
af758d11 9277 gdb_assert (!readin_p (objfile));
95554aad 9278
17ee85fc
TT
9279 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9280 free_cached_comp_units freer (per_objfile);
48993951 9281 expand_dependencies (objfile);
aaa75496 9282
97a1449a 9283 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9284 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9285}
9286
af758d11
SM
9287/* See psympriv.h. */
9288
9289bool
9290dwarf2_psymtab::readin_p (struct objfile *objfile) const
9291{
9292 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9293 return per_objfile->symtab_set_p (per_cu_data);
9294}
9295
9296/* See psympriv.h. */
9297
9298compunit_symtab *
9299dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9300{
9301 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9302 return per_objfile->get_symtab (per_cu_data);
9303}
9304
dee91e82
DE
9305/* Trivial hash function for die_info: the hash value of a DIE
9306 is its offset in .debug_info for this objfile. */
10b3939b 9307
dee91e82
DE
9308static hashval_t
9309die_hash (const void *item)
10b3939b 9310{
9a3c8263 9311 const struct die_info *die = (const struct die_info *) item;
6502dd73 9312
9c541725 9313 return to_underlying (die->sect_off);
dee91e82 9314}
63d06c5c 9315
dee91e82
DE
9316/* Trivial comparison function for die_info structures: two DIEs
9317 are equal if they have the same offset. */
98bfdba5 9318
dee91e82
DE
9319static int
9320die_eq (const void *item_lhs, const void *item_rhs)
9321{
9a3c8263
SM
9322 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9323 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9324
9c541725 9325 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9326}
c906108c 9327
4a636814
SM
9328/* Load the DIEs associated with PER_CU into memory.
9329
9330 In some cases, the caller, while reading partial symbols, will need to load
9331 the full symbols for the CU for some reason. It will already have a
9332 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9333 rather than creating a new one. */
c906108c 9334
dee91e82 9335static void
ab432490
SM
9336load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9337 dwarf2_per_objfile *per_objfile,
4a636814 9338 dwarf2_cu *existing_cu,
c0ab21c2
TT
9339 bool skip_partial,
9340 enum language pretend_language)
dee91e82 9341{
c0ab21c2
TT
9342 gdb_assert (! this_cu->is_debug_types);
9343
7188ed02 9344 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9345 if (reader.dummy_p)
9346 return;
9347
9348 struct dwarf2_cu *cu = reader.cu;
9349 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9350
dee91e82
DE
9351 gdb_assert (cu->die_hash == NULL);
9352 cu->die_hash =
9353 htab_create_alloc_ex (cu->header.length / 12,
9354 die_hash,
9355 die_eq,
9356 NULL,
9357 &cu->comp_unit_obstack,
9358 hashtab_obstack_allocate,
9359 dummy_obstack_deallocate);
e142c38c 9360
3e225074 9361 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9362 reader.comp_unit_die->child
9363 = read_die_and_siblings (&reader, reader.info_ptr,
9364 &info_ptr, reader.comp_unit_die);
9365 cu->dies = reader.comp_unit_die;
dee91e82 9366 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9367
9368 /* We try not to read any attributes in this function, because not
9cdd5dbd 9369 all CUs needed for references have been loaded yet, and symbol
10b3939b 9370 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9371 or we won't be able to build types correctly.
9372 Similarly, if we do not read the producer, we can not apply
9373 producer-specific interpretation. */
c0ab21c2 9374 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9375
9376 reader.keep ();
10b3939b
DJ
9377}
9378
3da10d80
KS
9379/* Add a DIE to the delayed physname list. */
9380
9381static void
9382add_to_method_list (struct type *type, int fnfield_index, int index,
9383 const char *name, struct die_info *die,
9384 struct dwarf2_cu *cu)
9385{
9386 struct delayed_method_info mi;
9387 mi.type = type;
9388 mi.fnfield_index = fnfield_index;
9389 mi.index = index;
9390 mi.name = name;
9391 mi.die = die;
c89b44cd 9392 cu->method_list.push_back (mi);
3da10d80
KS
9393}
9394
3693fdb3
PA
9395/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9396 "const" / "volatile". If so, decrements LEN by the length of the
9397 modifier and return true. Otherwise return false. */
9398
9399template<size_t N>
9400static bool
9401check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9402{
9403 size_t mod_len = sizeof (mod) - 1;
9404 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9405 {
9406 len -= mod_len;
9407 return true;
9408 }
9409 return false;
9410}
9411
3da10d80
KS
9412/* Compute the physnames of any methods on the CU's method list.
9413
9414 The computation of method physnames is delayed in order to avoid the
9415 (bad) condition that one of the method's formal parameters is of an as yet
9416 incomplete type. */
9417
9418static void
9419compute_delayed_physnames (struct dwarf2_cu *cu)
9420{
3693fdb3 9421 /* Only C++ delays computing physnames. */
c89b44cd 9422 if (cu->method_list.empty ())
3693fdb3
PA
9423 return;
9424 gdb_assert (cu->language == language_cplus);
9425
52941706 9426 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9427 {
1d06ead6 9428 const char *physname;
3da10d80 9429 struct fn_fieldlist *fn_flp
c89b44cd
TT
9430 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9431 physname = dwarf2_physname (mi.name, mi.die, cu);
9432 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9433 = physname ? physname : "";
3693fdb3
PA
9434
9435 /* Since there's no tag to indicate whether a method is a
9436 const/volatile overload, extract that information out of the
9437 demangled name. */
9438 if (physname != NULL)
9439 {
9440 size_t len = strlen (physname);
9441
9442 while (1)
9443 {
9444 if (physname[len] == ')') /* shortcut */
9445 break;
9446 else if (check_modifier (physname, len, " const"))
c89b44cd 9447 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9448 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9449 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9450 else
9451 break;
9452 }
9453 }
3da10d80 9454 }
c89b44cd
TT
9455
9456 /* The list is no longer needed. */
9457 cu->method_list.clear ();
3da10d80
KS
9458}
9459
a766d390
DE
9460/* Go objects should be embedded in a DW_TAG_module DIE,
9461 and it's not clear if/how imported objects will appear.
9462 To keep Go support simple until that's worked out,
9463 go back through what we've read and create something usable.
9464 We could do this while processing each DIE, and feels kinda cleaner,
9465 but that way is more invasive.
9466 This is to, for example, allow the user to type "p var" or "b main"
9467 without having to specify the package name, and allow lookups
9468 of module.object to work in contexts that use the expression
9469 parser. */
9470
9471static void
9472fixup_go_packaging (struct dwarf2_cu *cu)
9473{
421d1616 9474 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9475 struct pending *list;
9476 int i;
9477
c24bdb02 9478 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9479 list != NULL;
9480 list = list->next)
a766d390
DE
9481 {
9482 for (i = 0; i < list->nsyms; ++i)
9483 {
9484 struct symbol *sym = list->symbol[i];
9485
c1b5c1eb 9486 if (sym->language () == language_go
a766d390
DE
9487 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9488 {
421d1616
TT
9489 gdb::unique_xmalloc_ptr<char> this_package_name
9490 (go_symbol_package_name (sym));
a766d390
DE
9491
9492 if (this_package_name == NULL)
9493 continue;
9494 if (package_name == NULL)
421d1616 9495 package_name = std::move (this_package_name);
a766d390
DE
9496 else
9497 {
5e22e966 9498 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9499 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9500 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9501 (symbol_symtab (sym) != NULL
9502 ? symtab_to_filename_for_display
9503 (symbol_symtab (sym))
e3b94546 9504 : objfile_name (objfile)),
421d1616 9505 this_package_name.get (), package_name.get ());
a766d390
DE
9506 }
9507 }
9508 }
9509 }
9510
9511 if (package_name != NULL)
9512 {
5e22e966 9513 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9514 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9515 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9516 saved_package_name);
a766d390
DE
9517 struct symbol *sym;
9518
8c14c3a3 9519 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9520 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9521 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9522 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9523 e.g., "main" finds the "main" module and not C's main(). */
9524 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9525 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9526 SYMBOL_TYPE (sym) = type;
9527
c24bdb02 9528 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9529 }
9530}
9531
c9317f21
TT
9532/* Allocate a fully-qualified name consisting of the two parts on the
9533 obstack. */
9534
9535static const char *
9536rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9537{
9538 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9539}
9540
9c6a1327
TT
9541/* A helper that allocates a variant part to attach to a Rust enum
9542 type. OBSTACK is where the results should be allocated. TYPE is
9543 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9544 discriminant. It must be the index of one of the fields of TYPE,
9545 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9546 DEFAULT_INDEX is the index of the default field; or -1 if there is
9547 no default. RANGES is indexed by "effective" field number (the
9548 field index, but omitting the discriminant and default fields) and
9549 must hold the discriminant values used by the variants. Note that
9550 RANGES must have a lifetime at least as long as OBSTACK -- either
9551 already allocated on it, or static. */
c9317f21 9552
9c6a1327
TT
9553static void
9554alloc_rust_variant (struct obstack *obstack, struct type *type,
9555 int discriminant_index, int default_index,
9556 gdb::array_view<discriminant_range> ranges)
9557{
57d02173
TT
9558 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9559 gdb_assert (discriminant_index == -1
9560 || (discriminant_index >= 0
9561 && discriminant_index < type->num_fields ()));
c9317f21 9562 gdb_assert (default_index == -1
1f704f76 9563 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9564
9c6a1327 9565 /* We have one variant for each non-discriminant field. */
57d02173
TT
9566 int n_variants = type->num_fields ();
9567 if (discriminant_index != -1)
9568 --n_variants;
c9317f21 9569
9c6a1327
TT
9570 variant *variants = new (obstack) variant[n_variants];
9571 int var_idx = 0;
9572 int range_idx = 0;
1f704f76 9573 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9574 {
9575 if (i == discriminant_index)
9576 continue;
c9317f21 9577
9c6a1327
TT
9578 variants[var_idx].first_field = i;
9579 variants[var_idx].last_field = i + 1;
9580
9581 /* The default field does not need a range, but other fields do.
9582 We skipped the discriminant above. */
9583 if (i != default_index)
9584 {
9585 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9586 ++range_idx;
9587 }
c9317f21 9588
9c6a1327
TT
9589 ++var_idx;
9590 }
9591
9592 gdb_assert (range_idx == ranges.size ());
9593 gdb_assert (var_idx == n_variants);
9594
9595 variant_part *part = new (obstack) variant_part;
9596 part->discriminant_index = discriminant_index;
57d02173
TT
9597 /* If there is no discriminant, then whether it is signed is of no
9598 consequence. */
9599 part->is_unsigned
9600 = (discriminant_index == -1
9601 ? false
c6d940a9 9602 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9603 part->variants = gdb::array_view<variant> (variants, n_variants);
9604
9605 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9606 gdb::array_view<variant_part> *prop_value
9607 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9608
9c6a1327 9609 struct dynamic_prop prop;
8c2e4e06 9610 prop.set_variant_parts (prop_value);
9c6a1327 9611
5c54719c 9612 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9613}
9614
9615/* Some versions of rustc emitted enums in an unusual way.
9616
9617 Ordinary enums were emitted as unions. The first element of each
9618 structure in the union was named "RUST$ENUM$DISR". This element
9619 held the discriminant.
9620
9621 These versions of Rust also implemented the "non-zero"
9622 optimization. When the enum had two values, and one is empty and
9623 the other holds a pointer that cannot be zero, the pointer is used
9624 as the discriminant, with a zero value meaning the empty variant.
9625 Here, the union's first member is of the form
9626 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9627 where the fieldnos are the indices of the fields that should be
9628 traversed in order to find the field (which may be several fields deep)
9629 and the variantname is the name of the variant of the case when the
9630 field is zero.
9631
9632 This function recognizes whether TYPE is of one of these forms,
9633 and, if so, smashes it to be a variant type. */
9634
9635static void
9636quirk_rust_enum (struct type *type, struct objfile *objfile)
9637{
78134374 9638 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9639
9640 /* We don't need to deal with empty enums. */
1f704f76 9641 if (type->num_fields () == 0)
c9317f21
TT
9642 return;
9643
9644#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9645 if (type->num_fields () == 1
c9317f21
TT
9646 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9647 {
9648 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9649
9650 /* Decode the field name to find the offset of the
9651 discriminant. */
9652 ULONGEST bit_offset = 0;
940da03e 9653 struct type *field_type = type->field (0).type ();
c9317f21
TT
9654 while (name[0] >= '0' && name[0] <= '9')
9655 {
9656 char *tail;
9657 unsigned long index = strtoul (name, &tail, 10);
9658 name = tail;
9659 if (*name != '$'
1f704f76 9660 || index >= field_type->num_fields ()
c9317f21
TT
9661 || (TYPE_FIELD_LOC_KIND (field_type, index)
9662 != FIELD_LOC_KIND_BITPOS))
9663 {
b98664d3 9664 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9665 "[in module %s]"),
9666 TYPE_FIELD_NAME (type, 0),
9667 objfile_name (objfile));
9668 return;
9669 }
9670 ++name;
9671
9672 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9673 field_type = field_type->field (index).type ();
c9317f21
TT
9674 }
9675
9c6a1327
TT
9676 /* Smash this type to be a structure type. We have to do this
9677 because the type has already been recorded. */
67607e24 9678 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9679 type->set_num_fields (3);
9c6a1327 9680 /* Save the field we care about. */
ceacbf6e 9681 struct field saved_field = type->field (0);
3cabb6b0
SM
9682 type->set_fields
9683 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9684
9c6a1327 9685 /* Put the discriminant at index 0. */
5d14b6e5 9686 type->field (0).set_type (field_type);
9c6a1327
TT
9687 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9688 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9689 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9690
9691 /* The order of fields doesn't really matter, so put the real
9692 field at index 1 and the data-less field at index 2. */
ceacbf6e 9693 type->field (1) = saved_field;
9c6a1327 9694 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9695 = rust_last_path_segment (type->field (1).type ()->name ());
9696 type->field (1).type ()->set_name
7d93a1e0 9697 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9698 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9699
9700 const char *dataless_name
7d93a1e0 9701 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9702 name);
9703 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9704 dataless_name);
5d14b6e5 9705 type->field (2).set_type (dataless_type);
c9317f21
TT
9706 /* NAME points into the original discriminant name, which
9707 already has the correct lifetime. */
9c6a1327 9708 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9709 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9710
9c6a1327
TT
9711 /* Indicate that this is a variant type. */
9712 static discriminant_range ranges[1] = { { 0, 0 } };
9713 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9714 }
77c2dba3
TT
9715 /* A union with a single anonymous field is probably an old-style
9716 univariant enum. */
1f704f76 9717 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9718 {
c9317f21
TT
9719 /* Smash this type to be a structure type. We have to do this
9720 because the type has already been recorded. */
67607e24 9721 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9722
940da03e 9723 struct type *field_type = type->field (0).type ();
c9317f21 9724 const char *variant_name
7d93a1e0 9725 = rust_last_path_segment (field_type->name ());
9c6a1327 9726 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9727 field_type->set_name
9728 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9729 type->name (), variant_name));
57d02173
TT
9730
9731 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9732 }
9733 else
9734 {
9735 struct type *disr_type = nullptr;
1f704f76 9736 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9737 {
940da03e 9738 disr_type = type->field (i).type ();
c9317f21 9739
78134374 9740 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9741 {
9742 /* All fields of a true enum will be structs. */
9743 return;
9744 }
1f704f76 9745 else if (disr_type->num_fields () == 0)
c9317f21
TT
9746 {
9747 /* Could be data-less variant, so keep going. */
a037790e 9748 disr_type = nullptr;
c9317f21
TT
9749 }
9750 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9751 "RUST$ENUM$DISR") != 0)
9752 {
9753 /* Not a Rust enum. */
9754 return;
9755 }
9756 else
9757 {
9758 /* Found one. */
9759 break;
9760 }
9761 }
9762
9763 /* If we got here without a discriminant, then it's probably
9764 just a union. */
9765 if (disr_type == nullptr)
9766 return;
9767
9768 /* Smash this type to be a structure type. We have to do this
9769 because the type has already been recorded. */
67607e24 9770 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9771
9c6a1327 9772 /* Make space for the discriminant field. */
ceacbf6e 9773 struct field *disr_field = &disr_type->field (0);
9c6a1327 9774 field *new_fields
1f704f76 9775 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9776 * sizeof (struct field)));
80fc5e77 9777 memcpy (new_fields + 1, type->fields (),
1f704f76 9778 type->num_fields () * sizeof (struct field));
3cabb6b0 9779 type->set_fields (new_fields);
1f704f76 9780 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9781
9782 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9783 type->field (0) = *disr_field;
9c6a1327
TT
9784 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9785 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9786
9787 /* We need a way to find the correct discriminant given a
9788 variant name. For convenience we build a map here. */
b6cdac4b 9789 struct type *enum_type = disr_field->type ();
c9317f21 9790 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9791 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9792 {
9793 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9794 {
9795 const char *name
9796 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9797 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9798 }
9799 }
9800
1f704f76 9801 int n_fields = type->num_fields ();
9c6a1327
TT
9802 /* We don't need a range entry for the discriminant, but we do
9803 need one for every other field, as there is no default
9804 variant. */
9805 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9806 discriminant_range,
9807 n_fields - 1);
c9317f21
TT
9808 /* Skip the discriminant here. */
9809 for (int i = 1; i < n_fields; ++i)
9810 {
9811 /* Find the final word in the name of this variant's type.
9812 That name can be used to look up the correct
9813 discriminant. */
9814 const char *variant_name
940da03e 9815 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9816
9817 auto iter = discriminant_map.find (variant_name);
9818 if (iter != discriminant_map.end ())
9c6a1327 9819 {
57d02173
TT
9820 ranges[i - 1].low = iter->second;
9821 ranges[i - 1].high = iter->second;
9c6a1327 9822 }
c9317f21 9823
57d02173
TT
9824 /* In Rust, each element should have the size of the
9825 enclosing enum. */
9826 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9827
bedda9ac 9828 /* Remove the discriminant field, if it exists. */
940da03e 9829 struct type *sub_type = type->field (i).type ();
1f704f76 9830 if (sub_type->num_fields () > 0)
bedda9ac 9831 {
5e33d5f4 9832 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9833 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9834 }
9c6a1327 9835 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9836 sub_type->set_name
9837 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9838 type->name (), variant_name));
c9317f21 9839 }
9c6a1327
TT
9840
9841 /* Indicate that this is a variant type. */
a1520ad8 9842 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9843 gdb::array_view<discriminant_range> (ranges,
9844 n_fields - 1));
c9317f21
TT
9845 }
9846}
9847
9848/* Rewrite some Rust unions to be structures with variants parts. */
9849
9850static void
9851rust_union_quirks (struct dwarf2_cu *cu)
9852{
9853 gdb_assert (cu->language == language_rust);
52941706 9854 for (type *type_ : cu->rust_unions)
5e22e966 9855 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9856 /* We don't need this any more. */
9857 cu->rust_unions.clear ();
c9317f21
TT
9858}
9859
8adb8487
TT
9860/* See read.h. */
9861
9862type_unit_group_unshareable *
9863dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9864{
9865 auto iter = this->m_type_units.find (tu_group);
9866 if (iter != this->m_type_units.end ())
9867 return iter->second.get ();
9868
9869 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9870 type_unit_group_unshareable *result = uniq.get ();
9871 this->m_type_units[tu_group] = std::move (uniq);
9872 return result;
9873}
9874
e286671b
TT
9875struct type *
9876dwarf2_per_objfile::get_type_for_signatured_type
9877 (signatured_type *sig_type) const
9878{
9879 auto iter = this->m_type_map.find (sig_type);
9880 if (iter == this->m_type_map.end ())
9881 return nullptr;
9882
9883 return iter->second;
9884}
9885
9886void dwarf2_per_objfile::set_type_for_signatured_type
9887 (signatured_type *sig_type, struct type *type)
9888{
9889 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9890
9891 this->m_type_map[sig_type] = type;
9892}
9893
95554aad
TT
9894/* A helper function for computing the list of all symbol tables
9895 included by PER_CU. */
9896
9897static void
4c39bc03 9898recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9899 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9900 dwarf2_per_cu_data *per_cu,
9901 dwarf2_per_objfile *per_objfile,
43f3e411 9902 struct compunit_symtab *immediate_parent)
95554aad 9903{
af758d11 9904 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9905 if (*slot != NULL)
9906 {
9907 /* This inclusion and its children have been processed. */
9908 return;
9909 }
9910
9911 *slot = per_cu;
af758d11 9912
95554aad 9913 /* Only add a CU if it has a symbol table. */
43182c09 9914 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9915 if (cust != NULL)
ec94af83
DE
9916 {
9917 /* If this is a type unit only add its symbol table if we haven't
9918 seen it yet (type unit per_cu's can share symtabs). */
9919 if (per_cu->is_debug_types)
9920 {
43f3e411 9921 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9922 if (*slot == NULL)
9923 {
43f3e411 9924 *slot = cust;
4c39bc03 9925 result->push_back (cust);
43f3e411
DE
9926 if (cust->user == NULL)
9927 cust->user = immediate_parent;
ec94af83
DE
9928 }
9929 }
9930 else
f9125b6c 9931 {
4c39bc03 9932 result->push_back (cust);
43f3e411
DE
9933 if (cust->user == NULL)
9934 cust->user = immediate_parent;
f9125b6c 9935 }
ec94af83 9936 }
95554aad 9937
ae640021
AB
9938 if (!per_cu->imported_symtabs_empty ())
9939 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9940 {
9941 recursively_compute_inclusions (result, all_children,
43182c09
SM
9942 all_type_symtabs, ptr, per_objfile,
9943 cust);
ae640021 9944 }
95554aad
TT
9945}
9946
43f3e411 9947/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9948 PER_CU. */
9949
9950static void
43182c09
SM
9951compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9952 dwarf2_per_objfile *per_objfile)
95554aad 9953{
f4dc4d17
DE
9954 gdb_assert (! per_cu->is_debug_types);
9955
ae640021 9956 if (!per_cu->imported_symtabs_empty ())
95554aad 9957 {
ae640021 9958 int len;
4c39bc03 9959 std::vector<compunit_symtab *> result_symtabs;
43182c09 9960 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9961
9962 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9963 if (cust == NULL)
95554aad
TT
9964 return;
9965
280a9412
TT
9966 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9967 htab_eq_pointer,
9968 NULL, xcalloc, xfree));
9969 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9970 htab_eq_pointer,
9971 NULL, xcalloc, xfree));
95554aad 9972
ae640021 9973 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9974 {
280a9412
TT
9975 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9976 all_type_symtabs.get (), ptr,
9977 per_objfile, cust);
ec94af83 9978 }
95554aad 9979
ec94af83 9980 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9981 len = result_symtabs.size ();
43f3e411 9982 cust->includes
f6e649dd 9983 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9984 struct compunit_symtab *, len + 1);
4c39bc03
TT
9985 memcpy (cust->includes, result_symtabs.data (),
9986 len * sizeof (compunit_symtab *));
43f3e411 9987 cust->includes[len] = NULL;
95554aad
TT
9988 }
9989}
9990
9991/* Compute the 'includes' field for the symtabs of all the CUs we just
9992 read. */
9993
9994static void
976ca316 9995process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9996{
976ca316 9997 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9998 {
9999 if (! iter->is_debug_types)
976ca316 10000 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 10001 }
95554aad 10002
976ca316 10003 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
10004}
10005
8fc0b21d 10006/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
10007 already been loaded into memory. */
10008
10009static void
8fc0b21d 10010process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 10011{
976ca316
SM
10012 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10013 struct objfile *objfile = per_objfile->objfile;
08feed99 10014 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 10015 CORE_ADDR lowpc, highpc;
43f3e411 10016 struct compunit_symtab *cust;
10b3939b 10017 CORE_ADDR baseaddr;
4359dff1 10018 struct block *static_block;
3e29f34a 10019 CORE_ADDR addr;
10b3939b 10020
b3b3bada 10021 baseaddr = objfile->text_section_offset ();
10b3939b 10022
c89b44cd
TT
10023 /* Clear the list here in case something was left over. */
10024 cu->method_list.clear ();
10b3939b 10025
95554aad
TT
10026 cu->language = pretend_language;
10027 cu->language_defn = language_def (cu->language);
10028
1c47ec3e
TV
10029 dwarf2_find_base_address (cu->dies, cu);
10030
c906108c 10031 /* Do line number decoding in read_file_scope () */
10b3939b 10032 process_die (cu->dies, cu);
c906108c 10033
a766d390
DE
10034 /* For now fudge the Go package. */
10035 if (cu->language == language_go)
10036 fixup_go_packaging (cu);
10037
5f48f8f3 10038 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10039 should be complete, and it should now be safe to compute all of the
10040 physnames. */
10041 compute_delayed_physnames (cu);
3da10d80 10042
c9317f21
TT
10043 if (cu->language == language_rust)
10044 rust_union_quirks (cu);
10045
fae299cd
DC
10046 /* Some compilers don't define a DW_AT_high_pc attribute for the
10047 compilation unit. If the DW_AT_high_pc is missing, synthesize
10048 it, by scanning the DIE's below the compilation unit. */
10b3939b 10049 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10050
3e29f34a 10051 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10052 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10053
10054 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10055 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10056 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10057 addrmap to help ensure it has an accurate map of pc values belonging to
10058 this comp unit. */
10059 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10060
c24bdb02 10061 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10062 SECT_OFF_TEXT (objfile),
10063 0);
c906108c 10064
43f3e411 10065 if (cust != NULL)
c906108c 10066 {
df15bd07 10067 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10068
8be455d7
JK
10069 /* Set symtab language to language from DW_AT_language. If the
10070 compilation is from a C file generated by language preprocessors, do
10071 not set the language if it was already deduced by start_subfile. */
43f3e411 10072 if (!(cu->language == language_c
40e3ad0e 10073 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10074 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10075
10076 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10077 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10078 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10079 there were bugs in prologue debug info, fixed later in GCC-4.5
10080 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10081
10082 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10083 needed, it would be wrong due to missing DW_AT_producer there.
10084
10085 Still one can confuse GDB by using non-standard GCC compilation
10086 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10087 */
ab260dad 10088 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10089 cust->locations_valid = 1;
e0d00bc7
JK
10090
10091 if (gcc_4_minor >= 5)
43f3e411 10092 cust->epilogue_unwind_valid = 1;
96408a79 10093
43f3e411 10094 cust->call_site_htab = cu->call_site_htab;
c906108c 10095 }
9291a0cd 10096
976ca316 10097 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10098
95554aad 10099 /* Push it for inclusion processing later. */
976ca316 10100 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10101
10102 /* Not needed any more. */
c24bdb02 10103 cu->reset_builder ();
f4dc4d17 10104}
45cfd468 10105
8fc0b21d 10106/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10107 already been loaded into memory. */
10108
10109static void
8fc0b21d 10110process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10111 enum language pretend_language)
10112{
976ca316
SM
10113 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10114 struct objfile *objfile = per_objfile->objfile;
43f3e411 10115 struct compunit_symtab *cust;
0186c6a7
DE
10116 struct signatured_type *sig_type;
10117
8fc0b21d
SM
10118 gdb_assert (cu->per_cu->is_debug_types);
10119 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10120
c89b44cd
TT
10121 /* Clear the list here in case something was left over. */
10122 cu->method_list.clear ();
f4dc4d17 10123
f4dc4d17
DE
10124 cu->language = pretend_language;
10125 cu->language_defn = language_def (cu->language);
10126
10127 /* The symbol tables are set up in read_type_unit_scope. */
10128 process_die (cu->dies, cu);
10129
10130 /* For now fudge the Go package. */
10131 if (cu->language == language_go)
10132 fixup_go_packaging (cu);
10133
5f48f8f3 10134 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10135 should be complete, and it should now be safe to compute all of the
10136 physnames. */
10137 compute_delayed_physnames (cu);
f4dc4d17 10138
c9317f21
TT
10139 if (cu->language == language_rust)
10140 rust_union_quirks (cu);
10141
f4dc4d17
DE
10142 /* TUs share symbol tables.
10143 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10144 of it with end_expandable_symtab. Otherwise, complete the addition of
10145 this TU's symbols to the existing symtab. */
8adb8487 10146 type_unit_group_unshareable *tug_unshare =
976ca316 10147 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10148 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10149 {
c24bdb02
KS
10150 buildsym_compunit *builder = cu->get_builder ();
10151 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10152 tug_unshare->compunit_symtab = cust;
f4dc4d17 10153
43f3e411 10154 if (cust != NULL)
f4dc4d17
DE
10155 {
10156 /* Set symtab language to language from DW_AT_language. If the
10157 compilation is from a C file generated by language preprocessors,
10158 do not set the language if it was already deduced by
10159 start_subfile. */
43f3e411
DE
10160 if (!(cu->language == language_c
10161 && COMPUNIT_FILETABS (cust)->language != language_c))
10162 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10163 }
10164 }
10165 else
10166 {
c24bdb02 10167 cu->get_builder ()->augment_type_symtab ();
8adb8487 10168 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10169 }
10170
976ca316 10171 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10172
10173 /* Not needed any more. */
c24bdb02 10174 cu->reset_builder ();
c906108c
SS
10175}
10176
95554aad
TT
10177/* Process an imported unit DIE. */
10178
10179static void
10180process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10181{
10182 struct attribute *attr;
10183
f4dc4d17
DE
10184 /* For now we don't handle imported units in type units. */
10185 if (cu->per_cu->is_debug_types)
10186 {
10187 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10188 " supported in type units [in module %s]"),
5e22e966 10189 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10190 }
10191
95554aad
TT
10192 attr = dwarf2_attr (die, DW_AT_import, cu);
10193 if (attr != NULL)
10194 {
0826b30a 10195 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10196 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10197 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10198 dwarf2_per_cu_data *per_cu
ab432490 10199 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10200
58990295
TV
10201 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10202 into another compilation unit, at root level. Regard this as a hint,
10203 and ignore it. */
10204 if (die->parent && die->parent->parent == NULL
10205 && per_cu->unit_type == DW_UT_compile
10206 && per_cu->lang == language_cplus)
10207 return;
10208
69d751e3 10209 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10210 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10211 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10212 false, cu->language);
95554aad 10213
ae640021 10214 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10215 }
10216}
10217
4c8aa72d
PA
10218/* RAII object that represents a process_die scope: i.e.,
10219 starts/finishes processing a DIE. */
10220class process_die_scope
adde2bff 10221{
4c8aa72d
PA
10222public:
10223 process_die_scope (die_info *die, dwarf2_cu *cu)
10224 : m_die (die), m_cu (cu)
10225 {
10226 /* We should only be processing DIEs not already in process. */
10227 gdb_assert (!m_die->in_process);
10228 m_die->in_process = true;
10229 }
8c3cb9fa 10230
4c8aa72d
PA
10231 ~process_die_scope ()
10232 {
10233 m_die->in_process = false;
10234
10235 /* If we're done processing the DIE for the CU that owns the line
10236 header, we don't need the line header anymore. */
10237 if (m_cu->line_header_die_owner == m_die)
10238 {
10239 delete m_cu->line_header;
10240 m_cu->line_header = NULL;
10241 m_cu->line_header_die_owner = NULL;
10242 }
10243 }
10244
10245private:
10246 die_info *m_die;
10247 dwarf2_cu *m_cu;
10248};
adde2bff 10249
c906108c
SS
10250/* Process a die and its children. */
10251
10252static void
e7c27a73 10253process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10254{
4c8aa72d 10255 process_die_scope scope (die, cu);
adde2bff 10256
c906108c
SS
10257 switch (die->tag)
10258 {
10259 case DW_TAG_padding:
10260 break;
10261 case DW_TAG_compile_unit:
95554aad 10262 case DW_TAG_partial_unit:
e7c27a73 10263 read_file_scope (die, cu);
c906108c 10264 break;
348e048f
DE
10265 case DW_TAG_type_unit:
10266 read_type_unit_scope (die, cu);
10267 break;
c906108c 10268 case DW_TAG_subprogram:
0a4b0913
AB
10269 /* Nested subprograms in Fortran get a prefix. */
10270 if (cu->language == language_fortran
10271 && die->parent != NULL
10272 && die->parent->tag == DW_TAG_subprogram)
10273 cu->processing_has_namespace_info = true;
10274 /* Fall through. */
c906108c 10275 case DW_TAG_inlined_subroutine:
edb3359d 10276 read_func_scope (die, cu);
c906108c
SS
10277 break;
10278 case DW_TAG_lexical_block:
14898363
L
10279 case DW_TAG_try_block:
10280 case DW_TAG_catch_block:
e7c27a73 10281 read_lexical_block_scope (die, cu);
c906108c 10282 break;
216f72a1 10283 case DW_TAG_call_site:
96408a79
SA
10284 case DW_TAG_GNU_call_site:
10285 read_call_site_scope (die, cu);
10286 break;
c906108c 10287 case DW_TAG_class_type:
680b30c7 10288 case DW_TAG_interface_type:
c906108c
SS
10289 case DW_TAG_structure_type:
10290 case DW_TAG_union_type:
134d01f1 10291 process_structure_scope (die, cu);
c906108c
SS
10292 break;
10293 case DW_TAG_enumeration_type:
134d01f1 10294 process_enumeration_scope (die, cu);
c906108c 10295 break;
134d01f1 10296
f792889a
DJ
10297 /* These dies have a type, but processing them does not create
10298 a symbol or recurse to process the children. Therefore we can
10299 read them on-demand through read_type_die. */
c906108c 10300 case DW_TAG_subroutine_type:
72019c9c 10301 case DW_TAG_set_type:
c906108c 10302 case DW_TAG_pointer_type:
c906108c 10303 case DW_TAG_ptr_to_member_type:
c906108c 10304 case DW_TAG_reference_type:
4297a3f0 10305 case DW_TAG_rvalue_reference_type:
c906108c 10306 case DW_TAG_string_type:
c906108c 10307 break;
134d01f1 10308
d8f62e84
TT
10309 case DW_TAG_array_type:
10310 /* We only need to handle this case for Ada -- in other
10311 languages, it's normal for the compiler to emit a typedef
10312 instead. */
10313 if (cu->language != language_ada)
10314 break;
10315 /* FALLTHROUGH */
c906108c 10316 case DW_TAG_base_type:
a02abb62 10317 case DW_TAG_subrange_type:
cb249c71 10318 case DW_TAG_typedef:
134d01f1 10319 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10320 DW_AT_name. */
f792889a 10321 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10322 break;
c906108c 10323 case DW_TAG_common_block:
e7c27a73 10324 read_common_block (die, cu);
c906108c
SS
10325 break;
10326 case DW_TAG_common_inclusion:
10327 break;
d9fa45fe 10328 case DW_TAG_namespace:
9068261f 10329 cu->processing_has_namespace_info = true;
e7c27a73 10330 read_namespace (die, cu);
d9fa45fe 10331 break;
5d7cb8df 10332 case DW_TAG_module:
9068261f 10333 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10334 read_module (die, cu);
10335 break;
d9fa45fe 10336 case DW_TAG_imported_declaration:
9068261f 10337 cu->processing_has_namespace_info = true;
74921315
KS
10338 if (read_namespace_alias (die, cu))
10339 break;
86a73007
TT
10340 /* The declaration is not a global namespace alias. */
10341 /* Fall through. */
d9fa45fe 10342 case DW_TAG_imported_module:
9068261f 10343 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10344 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10345 || cu->language != language_fortran))
b98664d3 10346 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10347 dwarf_tag_name (die->tag));
10348 read_import_statement (die, cu);
d9fa45fe 10349 break;
95554aad
TT
10350
10351 case DW_TAG_imported_unit:
10352 process_imported_unit_die (die, cu);
10353 break;
10354
71a3c369
TT
10355 case DW_TAG_variable:
10356 read_variable (die, cu);
10357 break;
10358
c906108c 10359 default:
e7c27a73 10360 new_symbol (die, NULL, cu);
c906108c
SS
10361 break;
10362 }
10363}
ca69b9e6
DE
10364\f
10365/* DWARF name computation. */
c906108c 10366
94af9270
KS
10367/* A helper function for dwarf2_compute_name which determines whether DIE
10368 needs to have the name of the scope prepended to the name listed in the
10369 die. */
10370
10371static int
10372die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10373{
1c809c68
TT
10374 struct attribute *attr;
10375
94af9270
KS
10376 switch (die->tag)
10377 {
10378 case DW_TAG_namespace:
10379 case DW_TAG_typedef:
10380 case DW_TAG_class_type:
10381 case DW_TAG_interface_type:
10382 case DW_TAG_structure_type:
10383 case DW_TAG_union_type:
10384 case DW_TAG_enumeration_type:
10385 case DW_TAG_enumerator:
10386 case DW_TAG_subprogram:
08a76f8a 10387 case DW_TAG_inlined_subroutine:
94af9270 10388 case DW_TAG_member:
74921315 10389 case DW_TAG_imported_declaration:
94af9270
KS
10390 return 1;
10391
10392 case DW_TAG_variable:
c2b0a229 10393 case DW_TAG_constant:
94af9270
KS
10394 /* We only need to prefix "globally" visible variables. These include
10395 any variable marked with DW_AT_external or any variable that
10396 lives in a namespace. [Variables in anonymous namespaces
10397 require prefixing, but they are not DW_AT_external.] */
10398
10399 if (dwarf2_attr (die, DW_AT_specification, cu))
10400 {
10401 struct dwarf2_cu *spec_cu = cu;
9a619af0 10402
94af9270
KS
10403 return die_needs_namespace (die_specification (die, &spec_cu),
10404 spec_cu);
10405 }
10406
1c809c68 10407 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10408 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10409 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10410 return 0;
10411 /* A variable in a lexical block of some kind does not need a
10412 namespace, even though in C++ such variables may be external
10413 and have a mangled name. */
10414 if (die->parent->tag == DW_TAG_lexical_block
10415 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10416 || die->parent->tag == DW_TAG_catch_block
10417 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10418 return 0;
10419 return 1;
94af9270
KS
10420
10421 default:
10422 return 0;
10423 }
10424}
10425
73b9be8b
KS
10426/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10427 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10428 defined for the given DIE. */
10429
10430static struct attribute *
10431dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10432{
10433 struct attribute *attr;
10434
10435 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10436 if (attr == NULL)
10437 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10438
10439 return attr;
10440}
10441
10442/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10443 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10444 defined for the given DIE. */
10445
10446static const char *
10447dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10448{
10449 const char *linkage_name;
10450
10451 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10452 if (linkage_name == NULL)
10453 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10454
787de330
TT
10455 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10456 See https://github.com/rust-lang/rust/issues/32925. */
10457 if (cu->language == language_rust && linkage_name != NULL
10458 && strchr (linkage_name, '{') != NULL)
10459 linkage_name = NULL;
10460
73b9be8b
KS
10461 return linkage_name;
10462}
10463
94af9270 10464/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10465 compute the physname for the object, which include a method's:
9c37b5ae 10466 - formal parameters (C++),
a766d390 10467 - receiver type (Go),
a766d390
DE
10468
10469 The term "physname" is a bit confusing.
10470 For C++, for example, it is the demangled name.
10471 For Go, for example, it's the mangled name.
94af9270 10472
af6b7be1
JB
10473 For Ada, return the DIE's linkage name rather than the fully qualified
10474 name. PHYSNAME is ignored..
10475
5989a64e 10476 The result is allocated on the objfile->per_bfd's obstack and
45940949 10477 canonicalized. */
94af9270
KS
10478
10479static const char *
15d034d0
TT
10480dwarf2_compute_name (const char *name,
10481 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10482 int physname)
10483{
5e22e966 10484 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10485
94af9270
KS
10486 if (name == NULL)
10487 name = dwarf2_name (die, cu);
10488
2ee7123e
DE
10489 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10490 but otherwise compute it by typename_concat inside GDB.
10491 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10492 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10493 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10494 will set the demangled name to the result of dwarf2_full_name, and it is
10495 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10496 if (cu->language == language_ada
10497 || (cu->language == language_fortran && physname))
10498 {
10499 /* For Ada unit, we prefer the linkage name over the name, as
10500 the former contains the exported name, which the user expects
10501 to be able to reference. Ideally, we want the user to be able
10502 to reference this entity using either natural or linkage name,
10503 but we haven't started looking at this enhancement yet. */
73b9be8b 10504 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10505
2ee7123e
DE
10506 if (linkage_name != NULL)
10507 return linkage_name;
f55ee35c
JK
10508 }
10509
94af9270
KS
10510 /* These are the only languages we know how to qualify names in. */
10511 if (name != NULL
9c37b5ae 10512 && (cu->language == language_cplus
c44af4eb
TT
10513 || cu->language == language_fortran || cu->language == language_d
10514 || cu->language == language_rust))
94af9270
KS
10515 {
10516 if (die_needs_namespace (die, cu))
10517 {
0d5cff50 10518 const char *prefix;
34a68019 10519 const char *canonical_name = NULL;
94af9270 10520
d7e74731
PA
10521 string_file buf;
10522
94af9270 10523 prefix = determine_prefix (die, cu);
94af9270
KS
10524 if (*prefix != '\0')
10525 {
43816ebc
TT
10526 gdb::unique_xmalloc_ptr<char> prefixed_name
10527 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10528
43816ebc 10529 buf.puts (prefixed_name.get ());
94af9270
KS
10530 }
10531 else
d7e74731 10532 buf.puts (name);
94af9270 10533
98bfdba5
PA
10534 /* Template parameters may be specified in the DIE's DW_AT_name, or
10535 as children with DW_TAG_template_type_param or
10536 DW_TAG_value_type_param. If the latter, add them to the name
10537 here. If the name already has template parameters, then
10538 skip this step; some versions of GCC emit both, and
10539 it is more efficient to use the pre-computed name.
10540
10541 Something to keep in mind about this process: it is very
10542 unlikely, or in some cases downright impossible, to produce
10543 something that will match the mangled name of a function.
10544 If the definition of the function has the same debug info,
10545 we should be able to match up with it anyway. But fallbacks
10546 using the minimal symbol, for instance to find a method
10547 implemented in a stripped copy of libstdc++, will not work.
10548 If we do not have debug info for the definition, we will have to
10549 match them up some other way.
10550
10551 When we do name matching there is a related problem with function
10552 templates; two instantiated function templates are allowed to
10553 differ only by their return types, which we do not add here. */
10554
10555 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10556 {
10557 struct attribute *attr;
10558 struct die_info *child;
10559 int first = 1;
10560
10561 die->building_fullname = 1;
10562
10563 for (child = die->child; child != NULL; child = child->sibling)
10564 {
10565 struct type *type;
12df843f 10566 LONGEST value;
d521ce57 10567 const gdb_byte *bytes;
98bfdba5
PA
10568 struct dwarf2_locexpr_baton *baton;
10569 struct value *v;
10570
10571 if (child->tag != DW_TAG_template_type_param
10572 && child->tag != DW_TAG_template_value_param)
10573 continue;
10574
10575 if (first)
10576 {
d7e74731 10577 buf.puts ("<");
98bfdba5
PA
10578 first = 0;
10579 }
10580 else
d7e74731 10581 buf.puts (", ");
98bfdba5
PA
10582
10583 attr = dwarf2_attr (child, DW_AT_type, cu);
10584 if (attr == NULL)
10585 {
b98664d3 10586 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10587 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10588 continue;
10589 }
10590 type = die_type (child, cu);
10591
10592 if (child->tag == DW_TAG_template_type_param)
10593 {
c1ec8cea
TT
10594 c_print_type (type, "", &buf, -1, 0, cu->language,
10595 &type_print_raw_options);
98bfdba5
PA
10596 continue;
10597 }
10598
10599 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10600 if (attr == NULL)
10601 {
b98664d3 10602 complaint (_("template parameter missing "
3e43a32a 10603 "DW_AT_const_value"));
d7e74731 10604 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10605 continue;
10606 }
10607
10608 dwarf2_const_value_attr (attr, type, name,
10609 &cu->comp_unit_obstack, cu,
10610 &value, &bytes, &baton);
10611
20ce4123 10612 if (type->has_no_signedness ())
98bfdba5
PA
10613 /* GDB prints characters as NUMBER 'CHAR'. If that's
10614 changed, this can use value_print instead. */
d7e74731 10615 c_printchar (value, type, &buf);
98bfdba5
PA
10616 else
10617 {
10618 struct value_print_options opts;
10619
10620 if (baton != NULL)
10621 v = dwarf2_evaluate_loc_desc (type, NULL,
10622 baton->data,
10623 baton->size,
9f47c707
SM
10624 baton->per_cu,
10625 baton->per_objfile);
98bfdba5
PA
10626 else if (bytes != NULL)
10627 {
10628 v = allocate_value (type);
10629 memcpy (value_contents_writeable (v), bytes,
10630 TYPE_LENGTH (type));
10631 }
10632 else
10633 v = value_from_longest (type, value);
10634
3e43a32a
MS
10635 /* Specify decimal so that we do not depend on
10636 the radix. */
98bfdba5
PA
10637 get_formatted_print_options (&opts, 'd');
10638 opts.raw = 1;
d7e74731 10639 value_print (v, &buf, &opts);
98bfdba5 10640 release_value (v);
98bfdba5
PA
10641 }
10642 }
10643
10644 die->building_fullname = 0;
10645
10646 if (!first)
10647 {
10648 /* Close the argument list, with a space if necessary
10649 (nested templates). */
d7e74731
PA
10650 if (!buf.empty () && buf.string ().back () == '>')
10651 buf.puts (" >");
98bfdba5 10652 else
d7e74731 10653 buf.puts (">");
98bfdba5
PA
10654 }
10655 }
10656
9c37b5ae 10657 /* For C++ methods, append formal parameter type
94af9270 10658 information, if PHYSNAME. */
6e70227d 10659
94af9270 10660 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10661 && cu->language == language_cplus)
94af9270
KS
10662 {
10663 struct type *type = read_type_die (die, cu);
10664
d7e74731 10665 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10666 &type_print_raw_options);
94af9270 10667
9c37b5ae 10668 if (cu->language == language_cplus)
94af9270 10669 {
60430eff
DJ
10670 /* Assume that an artificial first parameter is
10671 "this", but do not crash if it is not. RealView
10672 marks unnamed (and thus unused) parameters as
10673 artificial; there is no way to differentiate
10674 the two cases. */
1f704f76 10675 if (type->num_fields () > 0
94af9270 10676 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10677 && type->field (0).type ()->code () == TYPE_CODE_PTR
10678 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10679 buf.puts (" const");
94af9270
KS
10680 }
10681 }
10682
d7e74731 10683 const std::string &intermediate_name = buf.string ();
94af9270
KS
10684
10685 if (cu->language == language_cplus)
34a68019 10686 canonical_name
322a8516 10687 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10688 objfile);
34a68019
TT
10689
10690 /* If we only computed INTERMEDIATE_NAME, or if
10691 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10692 intern it. */
322a8516 10693 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10694 name = objfile->intern (intermediate_name);
34a68019
TT
10695 else
10696 name = canonical_name;
94af9270
KS
10697 }
10698 }
10699
10700 return name;
10701}
10702
0114d602
DJ
10703/* Return the fully qualified name of DIE, based on its DW_AT_name.
10704 If scope qualifiers are appropriate they will be added. The result
34a68019 10705 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10706 not have a name. NAME may either be from a previous call to
10707 dwarf2_name or NULL.
10708
9c37b5ae 10709 The output string will be canonicalized (if C++). */
0114d602
DJ
10710
10711static const char *
15d034d0 10712dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10713{
94af9270
KS
10714 return dwarf2_compute_name (name, die, cu, 0);
10715}
0114d602 10716
94af9270
KS
10717/* Construct a physname for the given DIE in CU. NAME may either be
10718 from a previous call to dwarf2_name or NULL. The result will be
10719 allocated on the objfile_objstack or NULL if the DIE does not have a
10720 name.
0114d602 10721
9c37b5ae 10722 The output string will be canonicalized (if C++). */
0114d602 10723
94af9270 10724static const char *
15d034d0 10725dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10726{
5e22e966 10727 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10728 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10729 int need_copy = 1;
10730
10731 /* In this case dwarf2_compute_name is just a shortcut not building anything
10732 on its own. */
10733 if (!die_needs_namespace (die, cu))
10734 return dwarf2_compute_name (name, die, cu, 1);
10735
906bb4c5
TT
10736 if (cu->language != language_rust)
10737 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10738
10739 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10740 has computed. */
791afaa2 10741 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10742 if (mangled != NULL)
900e11f9 10743 {
900e11f9 10744
d3355e4d 10745 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10746 {
10747 /* Do nothing (do not demangle the symbol name). */
10748 }
a766d390
DE
10749 else
10750 {
0eb876f5
JB
10751 /* Use DMGL_RET_DROP for C++ template functions to suppress
10752 their return type. It is easier for GDB users to search
10753 for such functions as `name(params)' than `long name(params)'.
10754 In such case the minimal symbol names do not match the full
10755 symbol names but for template functions there is never a need
10756 to look up their definition from their declaration so
10757 the only disadvantage remains the minimal symbol variant
10758 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10759 demangled.reset (gdb_demangle (mangled,
10760 (DMGL_PARAMS | DMGL_ANSI
10761 | DMGL_RET_DROP)));
a766d390 10762 }
900e11f9 10763 if (demangled)
791afaa2 10764 canon = demangled.get ();
900e11f9
JK
10765 else
10766 {
10767 canon = mangled;
10768 need_copy = 0;
10769 }
10770 }
10771
10772 if (canon == NULL || check_physname)
10773 {
10774 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10775
10776 if (canon != NULL && strcmp (physname, canon) != 0)
10777 {
10778 /* It may not mean a bug in GDB. The compiler could also
10779 compute DW_AT_linkage_name incorrectly. But in such case
10780 GDB would need to be bug-to-bug compatible. */
10781
b98664d3 10782 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10783 "(from linkage <%s>) - DIE at %s [in module %s]"),
10784 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10785 objfile_name (objfile));
900e11f9
JK
10786
10787 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10788 is available here - over computed PHYSNAME. It is safer
10789 against both buggy GDB and buggy compilers. */
10790
10791 retval = canon;
10792 }
10793 else
10794 {
10795 retval = physname;
10796 need_copy = 0;
10797 }
10798 }
10799 else
10800 retval = canon;
10801
10802 if (need_copy)
be1e3d3e 10803 retval = objfile->intern (retval);
900e11f9 10804
900e11f9 10805 return retval;
0114d602
DJ
10806}
10807
74921315
KS
10808/* Inspect DIE in CU for a namespace alias. If one exists, record
10809 a new symbol for it.
10810
10811 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10812
10813static int
10814read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10815{
10816 struct attribute *attr;
10817
10818 /* If the die does not have a name, this is not a namespace
10819 alias. */
10820 attr = dwarf2_attr (die, DW_AT_name, cu);
10821 if (attr != NULL)
10822 {
10823 int num;
10824 struct die_info *d = die;
10825 struct dwarf2_cu *imported_cu = cu;
10826
10827 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10828 keep inspecting DIEs until we hit the underlying import. */
10829#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10830 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10831 {
10832 attr = dwarf2_attr (d, DW_AT_import, cu);
10833 if (attr == NULL)
10834 break;
10835
10836 d = follow_die_ref (d, attr, &imported_cu);
10837 if (d->tag != DW_TAG_imported_declaration)
10838 break;
10839 }
10840
10841 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10842 {
b98664d3 10843 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10844 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10845 return 0;
10846 }
10847
10848 if (attr != NULL)
10849 {
10850 struct type *type;
0826b30a 10851 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10852
aa66c379 10853 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10854 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10855 {
10856 /* This declaration is a global namespace alias. Add
10857 a symbol for it whose type is the aliased namespace. */
10858 new_symbol (die, type, cu);
10859 return 1;
10860 }
10861 }
10862 }
10863
10864 return 0;
10865}
10866
22cee43f 10867/* Return the using directives repository (global or local?) to use in the
804d2729 10868 current context for CU.
22cee43f
PMR
10869
10870 For Ada, imported declarations can materialize renamings, which *may* be
10871 global. However it is impossible (for now?) in DWARF to distinguish
10872 "external" imported declarations and "static" ones. As all imported
10873 declarations seem to be static in all other languages, make them all CU-wide
10874 global only in Ada. */
10875
10876static struct using_direct **
804d2729 10877using_directives (struct dwarf2_cu *cu)
22cee43f 10878{
c24bdb02
KS
10879 if (cu->language == language_ada
10880 && cu->get_builder ()->outermost_context_p ())
10881 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10882 else
c24bdb02 10883 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10884}
10885
27aa8d6a
SW
10886/* Read the import statement specified by the given die and record it. */
10887
10888static void
10889read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10890{
5e22e966 10891 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10892 struct attribute *import_attr;
32019081 10893 struct die_info *imported_die, *child_die;
de4affc9 10894 struct dwarf2_cu *imported_cu;
27aa8d6a 10895 const char *imported_name;
794684b6 10896 const char *imported_name_prefix;
13387711
SW
10897 const char *canonical_name;
10898 const char *import_alias;
10899 const char *imported_declaration = NULL;
794684b6 10900 const char *import_prefix;
eb1e02fd 10901 std::vector<const char *> excludes;
13387711 10902
27aa8d6a
SW
10903 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10904 if (import_attr == NULL)
10905 {
b98664d3 10906 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10907 dwarf_tag_name (die->tag));
10908 return;
10909 }
10910
de4affc9
CC
10911 imported_cu = cu;
10912 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10913 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10914 if (imported_name == NULL)
10915 {
10916 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10917
dda83cd7
SM
10918 The import in the following code:
10919 namespace A
10920 {
10921 typedef int B;
10922 }
10923
10924 int main ()
10925 {
10926 using A::B;
10927 B b;
10928 return b;
10929 }
10930
10931 ...
10932 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10933 <52> DW_AT_decl_file : 1
10934 <53> DW_AT_decl_line : 6
10935 <54> DW_AT_import : <0x75>
10936 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10937 <59> DW_AT_name : B
10938 <5b> DW_AT_decl_file : 1
10939 <5c> DW_AT_decl_line : 2
10940 <5d> DW_AT_type : <0x6e>
10941 ...
10942 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10943 <76> DW_AT_byte_size : 4
10944 <77> DW_AT_encoding : 5 (signed)
10945
10946 imports the wrong die ( 0x75 instead of 0x58 ).
10947 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10948 return;
10949 }
10950
82856980
SW
10951 /* Figure out the local name after import. */
10952 import_alias = dwarf2_name (die, cu);
27aa8d6a 10953
794684b6
SW
10954 /* Figure out where the statement is being imported to. */
10955 import_prefix = determine_prefix (die, cu);
10956
10957 /* Figure out what the scope of the imported die is and prepend it
10958 to the name of the imported die. */
de4affc9 10959 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10960
f55ee35c
JK
10961 if (imported_die->tag != DW_TAG_namespace
10962 && imported_die->tag != DW_TAG_module)
794684b6 10963 {
13387711
SW
10964 imported_declaration = imported_name;
10965 canonical_name = imported_name_prefix;
794684b6 10966 }
13387711 10967 else if (strlen (imported_name_prefix) > 0)
12aaed36 10968 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10969 imported_name_prefix,
10970 (cu->language == language_d ? "." : "::"),
10971 imported_name, (char *) NULL);
13387711
SW
10972 else
10973 canonical_name = imported_name;
794684b6 10974
32019081
JK
10975 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10976 for (child_die = die->child; child_die && child_die->tag;
436c571c 10977 child_die = child_die->sibling)
32019081
JK
10978 {
10979 /* DWARF-4: A Fortran use statement with a “rename list” may be
10980 represented by an imported module entry with an import attribute
10981 referring to the module and owned entries corresponding to those
10982 entities that are renamed as part of being imported. */
10983
10984 if (child_die->tag != DW_TAG_imported_declaration)
10985 {
b98664d3 10986 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10987 "- DIE at %s [in module %s]"),
10988 sect_offset_str (child_die->sect_off),
10989 objfile_name (objfile));
32019081
JK
10990 continue;
10991 }
10992
10993 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10994 if (import_attr == NULL)
10995 {
b98664d3 10996 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10997 dwarf_tag_name (child_die->tag));
10998 continue;
10999 }
11000
11001 imported_cu = cu;
11002 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11003 &imported_cu);
11004 imported_name = dwarf2_name (imported_die, imported_cu);
11005 if (imported_name == NULL)
11006 {
b98664d3 11007 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11008 "imported name - DIE at %s [in module %s]"),
11009 sect_offset_str (child_die->sect_off),
11010 objfile_name (objfile));
32019081
JK
11011 continue;
11012 }
11013
eb1e02fd 11014 excludes.push_back (imported_name);
32019081
JK
11015
11016 process_die (child_die, cu);
11017 }
11018
804d2729 11019 add_using_directive (using_directives (cu),
22cee43f
PMR
11020 import_prefix,
11021 canonical_name,
11022 import_alias,
11023 imported_declaration,
11024 excludes,
11025 0,
11026 &objfile->objfile_obstack);
27aa8d6a
SW
11027}
11028
5230b05a
WT
11029/* ICC<14 does not output the required DW_AT_declaration on incomplete
11030 types, but gives them a size of zero. Starting with version 14,
11031 ICC is compatible with GCC. */
11032
9068261f 11033static bool
5230b05a
WT
11034producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11035{
11036 if (!cu->checked_producer)
11037 check_producer (cu);
11038
11039 return cu->producer_is_icc_lt_14;
11040}
11041
eb77c9df
AB
11042/* ICC generates a DW_AT_type for C void functions. This was observed on
11043 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11044 which says that void functions should not have a DW_AT_type. */
11045
11046static bool
11047producer_is_icc (struct dwarf2_cu *cu)
11048{
11049 if (!cu->checked_producer)
11050 check_producer (cu);
11051
11052 return cu->producer_is_icc;
11053}
11054
1b80a9fa
JK
11055/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11056 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11057 this, it was first present in GCC release 4.3.0. */
11058
9068261f 11059static bool
1b80a9fa
JK
11060producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11061{
11062 if (!cu->checked_producer)
11063 check_producer (cu);
11064
11065 return cu->producer_is_gcc_lt_4_3;
11066}
11067
d721ba37
PA
11068static file_and_directory
11069find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11070{
d721ba37
PA
11071 file_and_directory res;
11072
9291a0cd
TT
11073 /* Find the filename. Do not use dwarf2_name here, since the filename
11074 is not a source language identifier. */
d721ba37
PA
11075 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11076 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11077
d721ba37
PA
11078 if (res.comp_dir == NULL
11079 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11080 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11081 {
d721ba37
PA
11082 res.comp_dir_storage = ldirname (res.name);
11083 if (!res.comp_dir_storage.empty ())
11084 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11085 }
d721ba37 11086 if (res.comp_dir != NULL)
9291a0cd
TT
11087 {
11088 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11089 directory, get rid of it. */
d721ba37 11090 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11091
d721ba37
PA
11092 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11093 res.comp_dir = cp + 1;
9291a0cd
TT
11094 }
11095
d721ba37
PA
11096 if (res.name == NULL)
11097 res.name = "<unknown>";
11098
11099 return res;
9291a0cd
TT
11100}
11101
f4dc4d17
DE
11102/* Handle DW_AT_stmt_list for a compilation unit.
11103 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11104 COMP_DIR is the compilation directory. LOWPC is passed to
11105 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11106
11107static void
11108handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11109 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11110{
976ca316 11111 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11112 struct attribute *attr;
527f3840
JK
11113 struct line_header line_header_local;
11114 hashval_t line_header_local_hash;
527f3840
JK
11115 void **slot;
11116 int decode_mapping;
2ab95328 11117
f4dc4d17
DE
11118 gdb_assert (! cu->per_cu->is_debug_types);
11119
2ab95328 11120 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11121 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11122 return;
11123
d4df075e 11124 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11125
11126 /* The line header hash table is only created if needed (it exists to
11127 prevent redundant reading of the line table for partial_units).
11128 If we're given a partial_unit, we'll need it. If we're given a
11129 compile_unit, then use the line header hash table if it's already
11130 created, but don't create one just yet. */
11131
976ca316 11132 if (per_objfile->line_header_hash == NULL
527f3840 11133 && die->tag == DW_TAG_partial_unit)
2ab95328 11134 {
976ca316 11135 per_objfile->line_header_hash
d15acc42
TT
11136 .reset (htab_create_alloc (127, line_header_hash_voidp,
11137 line_header_eq_voidp,
11138 free_line_header_voidp,
11139 xcalloc, xfree));
527f3840 11140 }
2ab95328 11141
9c541725 11142 line_header_local.sect_off = line_offset;
527f3840
JK
11143 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11144 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11145 if (per_objfile->line_header_hash != NULL)
527f3840 11146 {
976ca316 11147 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11148 &line_header_local,
11149 line_header_local_hash, NO_INSERT);
11150
11151 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11152 is not present in *SLOT (since if there is something in *SLOT then
11153 it will be for a partial_unit). */
11154 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11155 {
527f3840 11156 gdb_assert (*slot != NULL);
9a3c8263 11157 cu->line_header = (struct line_header *) *slot;
527f3840 11158 return;
dee91e82 11159 }
2ab95328 11160 }
527f3840
JK
11161
11162 /* dwarf_decode_line_header does not yet provide sufficient information.
11163 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11164 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11165 if (lh == NULL)
527f3840 11166 return;
4c8aa72d
PA
11167
11168 cu->line_header = lh.release ();
11169 cu->line_header_die_owner = die;
527f3840 11170
976ca316 11171 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11172 slot = NULL;
11173 else
11174 {
976ca316 11175 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11176 &line_header_local,
11177 line_header_local_hash, INSERT);
11178 gdb_assert (slot != NULL);
11179 }
11180 if (slot != NULL && *slot == NULL)
11181 {
11182 /* This newly decoded line number information unit will be owned
11183 by line_header_hash hash table. */
11184 *slot = cu->line_header;
4c8aa72d 11185 cu->line_header_die_owner = NULL;
527f3840
JK
11186 }
11187 else
11188 {
11189 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11190 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11191 line_header for this CU - it may happen at most once for each line
11192 number information unit. And if we're not using line_header_hash
11193 then this is what we want as well. */
11194 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11195 }
11196 decode_mapping = (die->tag != DW_TAG_partial_unit);
11197 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11198 decode_mapping);
fff8551c 11199
2ab95328
TT
11200}
11201
95554aad 11202/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11203
c906108c 11204static void
e7c27a73 11205read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11206{
976ca316
SM
11207 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11208 struct objfile *objfile = per_objfile->objfile;
08feed99 11209 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11210 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11211 CORE_ADDR highpc = ((CORE_ADDR) 0);
11212 struct attribute *attr;
c906108c 11213 struct die_info *child_die;
e142c38c 11214 CORE_ADDR baseaddr;
6e70227d 11215
380618d6 11216 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11217 baseaddr = objfile->text_section_offset ();
c906108c 11218
fae299cd 11219 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11220
11221 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11222 from finish_block. */
2acceee2 11223 if (lowpc == ((CORE_ADDR) -1))
c906108c 11224 lowpc = highpc;
3e29f34a 11225 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11226
d721ba37 11227 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11228
f4b8a18d
KW
11229 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11230 standardised yet. As a workaround for the language detection we fall
11231 back to the DW_AT_producer string. */
11232 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11233 cu->language = language_opencl;
11234
3019eac3
DE
11235 /* Similar hack for Go. */
11236 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11237 set_cu_language (DW_LANG_Go, cu);
11238
c24bdb02 11239 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11240
11241 /* Decode line number information if present. We do this before
11242 processing child DIEs, so that the line header table is available
11243 for DW_AT_decl_file. */
d721ba37 11244 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11245
11246 /* Process all dies in compilation unit. */
11247 if (die->child != NULL)
11248 {
11249 child_die = die->child;
11250 while (child_die && child_die->tag)
11251 {
11252 process_die (child_die, cu);
436c571c 11253 child_die = child_die->sibling;
3019eac3
DE
11254 }
11255 }
11256
11257 /* Decode macro information, if present. Dwarf 2 macro information
11258 refers to information in the line number info statement program
11259 header, so we can only read it if we've read the header
11260 successfully. */
0af92d60
JK
11261 attr = dwarf2_attr (die, DW_AT_macros, cu);
11262 if (attr == NULL)
11263 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11264 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11265 {
11266 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11267 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11268
529908cb 11269 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11270 }
11271 else
11272 {
11273 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11274 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11275 {
529908cb 11276 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11277
43f3e411 11278 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11279 }
11280 }
3019eac3
DE
11281}
11282
c24bdb02
KS
11283void
11284dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11285{
f4dc4d17
DE
11286 struct type_unit_group *tu_group;
11287 int first_time;
3019eac3 11288 struct attribute *attr;
9c541725 11289 unsigned int i;
0186c6a7 11290 struct signatured_type *sig_type;
3019eac3 11291
f4dc4d17 11292 gdb_assert (per_cu->is_debug_types);
0186c6a7 11293 sig_type = (struct signatured_type *) per_cu;
3019eac3 11294
c24bdb02 11295 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11296
f4dc4d17 11297 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11298 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11299 if (sig_type->type_unit_group == NULL)
c24bdb02 11300 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11301 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11302
11303 /* If we've already processed this stmt_list there's no real need to
11304 do it again, we could fake it and just recreate the part we need
11305 (file name,index -> symtab mapping). If data shows this optimization
11306 is useful we can do it then. */
8adb8487
TT
11307 type_unit_group_unshareable *tug_unshare
11308 = per_objfile->get_type_unit_group_unshareable (tu_group);
11309 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11310
11311 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11312 debug info. */
fff8551c 11313 line_header_up lh;
d4df075e 11314 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11315 {
d4df075e 11316 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11317 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11318 }
11319 if (lh == NULL)
11320 {
11321 if (first_time)
c24bdb02 11322 start_symtab ("", NULL, 0);
f4dc4d17
DE
11323 else
11324 {
8adb8487 11325 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11326 gdb_assert (m_builder == nullptr);
8adb8487 11327 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11328 m_builder.reset (new struct buildsym_compunit
11329 (COMPUNIT_OBJFILE (cust), "",
11330 COMPUNIT_DIRNAME (cust),
11331 compunit_language (cust),
11332 0, cust));
770479f2 11333 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11334 }
f4dc4d17 11335 return;
3019eac3
DE
11336 }
11337
c24bdb02
KS
11338 line_header = lh.release ();
11339 line_header_die_owner = die;
3019eac3 11340
f4dc4d17
DE
11341 if (first_time)
11342 {
c24bdb02 11343 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11344
1fd60fc0
DE
11345 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11346 still initializing it, and our caller (a few levels up)
11347 process_full_type_unit still needs to know if this is the first
11348 time. */
11349
8adb8487 11350 tug_unshare->symtabs
4ac93832
TT
11351 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11352 struct symtab *, line_header->file_names_size ());
3019eac3 11353
7ba99d21
AT
11354 auto &file_names = line_header->file_names ();
11355 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11356 {
7ba99d21 11357 file_entry &fe = file_names[i];
c24bdb02
KS
11358 dwarf2_start_subfile (this, fe.name,
11359 fe.include_dir (line_header));
11360 buildsym_compunit *b = get_builder ();
11361 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11362 {
4c8aa72d
PA
11363 /* NOTE: start_subfile will recognize when it's been
11364 passed a file it has already seen. So we can't
11365 assume there's a simple mapping from
11366 cu->line_header->file_names to subfiles, plus
11367 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11368 b->get_current_subfile ()->symtab
11369 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11370 }
11371
c24bdb02 11372 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11373 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11374 }
11375 }
11376 else
3019eac3 11377 {
c24bdb02 11378 gdb_assert (m_builder == nullptr);
8adb8487 11379 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11380 m_builder.reset (new struct buildsym_compunit
11381 (COMPUNIT_OBJFILE (cust), "",
11382 COMPUNIT_DIRNAME (cust),
11383 compunit_language (cust),
11384 0, cust));
770479f2 11385 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11386
7ba99d21
AT
11387 auto &file_names = line_header->file_names ();
11388 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11389 {
7ba99d21 11390 file_entry &fe = file_names[i];
8adb8487 11391 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11392 }
3019eac3
DE
11393 }
11394
f4dc4d17
DE
11395 /* The main symtab is allocated last. Type units don't have DW_AT_name
11396 so they don't have a "real" (so to speak) symtab anyway.
11397 There is later code that will assign the main symtab to all symbols
11398 that don't have one. We need to handle the case of a symbol with a
11399 missing symtab (DW_AT_decl_file) anyway. */
11400}
3019eac3 11401
f4dc4d17
DE
11402/* Process DW_TAG_type_unit.
11403 For TUs we want to skip the first top level sibling if it's not the
11404 actual type being defined by this TU. In this case the first top
11405 level sibling is there to provide context only. */
3019eac3 11406
f4dc4d17
DE
11407static void
11408read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11409{
11410 struct die_info *child_die;
3019eac3 11411
f4dc4d17
DE
11412 prepare_one_comp_unit (cu, die, language_minimal);
11413
11414 /* Initialize (or reinitialize) the machinery for building symtabs.
11415 We do this before processing child DIEs, so that the line header table
11416 is available for DW_AT_decl_file. */
c24bdb02 11417 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11418
11419 if (die->child != NULL)
11420 {
11421 child_die = die->child;
11422 while (child_die && child_die->tag)
11423 {
11424 process_die (child_die, cu);
436c571c 11425 child_die = child_die->sibling;
f4dc4d17
DE
11426 }
11427 }
3019eac3
DE
11428}
11429\f
80626a55
DE
11430/* DWO/DWP files.
11431
11432 http://gcc.gnu.org/wiki/DebugFission
11433 http://gcc.gnu.org/wiki/DebugFissionDWP
11434
11435 To simplify handling of both DWO files ("object" files with the DWARF info)
11436 and DWP files (a file with the DWOs packaged up into one file), we treat
11437 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11438
11439static hashval_t
11440hash_dwo_file (const void *item)
11441{
9a3c8263 11442 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11443 hashval_t hash;
3019eac3 11444
a2ce51a0
DE
11445 hash = htab_hash_string (dwo_file->dwo_name);
11446 if (dwo_file->comp_dir != NULL)
11447 hash += htab_hash_string (dwo_file->comp_dir);
11448 return hash;
3019eac3
DE
11449}
11450
11451static int
11452eq_dwo_file (const void *item_lhs, const void *item_rhs)
11453{
9a3c8263
SM
11454 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11455 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11456
a2ce51a0
DE
11457 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11458 return 0;
11459 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11460 return lhs->comp_dir == rhs->comp_dir;
11461 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11462}
11463
11464/* Allocate a hash table for DWO files. */
11465
51ac9db5 11466static htab_up
298e9637 11467allocate_dwo_file_hash_table ()
3019eac3 11468{
51ac9db5
SM
11469 auto delete_dwo_file = [] (void *item)
11470 {
11471 struct dwo_file *dwo_file = (struct dwo_file *) item;
11472
11473 delete dwo_file;
11474 };
11475
bc68fb19
TT
11476 return htab_up (htab_create_alloc (41,
11477 hash_dwo_file,
11478 eq_dwo_file,
11479 delete_dwo_file,
11480 xcalloc, xfree));
3019eac3
DE
11481}
11482
80626a55
DE
11483/* Lookup DWO file DWO_NAME. */
11484
11485static void **
976ca316 11486lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11487 const char *dwo_name,
11488 const char *comp_dir)
80626a55
DE
11489{
11490 struct dwo_file find_entry;
11491 void **slot;
11492
976ca316
SM
11493 if (per_objfile->per_bfd->dwo_files == NULL)
11494 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11495
0ac5b59e
DE
11496 find_entry.dwo_name = dwo_name;
11497 find_entry.comp_dir = comp_dir;
976ca316 11498 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11499 INSERT);
80626a55
DE
11500
11501 return slot;
11502}
11503
3019eac3
DE
11504static hashval_t
11505hash_dwo_unit (const void *item)
11506{
9a3c8263 11507 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11508
11509 /* This drops the top 32 bits of the id, but is ok for a hash. */
11510 return dwo_unit->signature;
11511}
11512
11513static int
11514eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11515{
9a3c8263
SM
11516 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11517 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11518
11519 /* The signature is assumed to be unique within the DWO file.
11520 So while object file CU dwo_id's always have the value zero,
11521 that's OK, assuming each object file DWO file has only one CU,
11522 and that's the rule for now. */
11523 return lhs->signature == rhs->signature;
11524}
11525
11526/* Allocate a hash table for DWO CUs,TUs.
11527 There is one of these tables for each of CUs,TUs for each DWO file. */
11528
b0b6a987 11529static htab_up
298e9637 11530allocate_dwo_unit_table ()
3019eac3
DE
11531{
11532 /* Start out with a pretty small number.
11533 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11534 return htab_up (htab_create_alloc (3,
11535 hash_dwo_unit,
11536 eq_dwo_unit,
11537 NULL, xcalloc, xfree));
3019eac3
DE
11538}
11539
19c3d4c9 11540/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11541
11542static void
19c3d4c9
DE
11543create_dwo_cu_reader (const struct die_reader_specs *reader,
11544 const gdb_byte *info_ptr,
11545 struct die_info *comp_unit_die,
c0ab21c2
TT
11546 struct dwo_file *dwo_file,
11547 struct dwo_unit *dwo_unit)
3019eac3
DE
11548{
11549 struct dwarf2_cu *cu = reader->cu;
9c541725 11550 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11551 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11552
a084a2a6
AT
11553 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11554 if (!signature.has_value ())
3019eac3 11555 {
b98664d3 11556 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11557 " its dwo_id [in module %s]"),
9d8780f0 11558 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11559 return;
11560 }
11561
3019eac3 11562 dwo_unit->dwo_file = dwo_file;
a084a2a6 11563 dwo_unit->signature = *signature;
8a0459fd 11564 dwo_unit->section = section;
9c541725 11565 dwo_unit->sect_off = sect_off;
3019eac3
DE
11566 dwo_unit->length = cu->per_cu->length;
11567
6f738b01
SM
11568 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11569 sect_offset_str (sect_off),
11570 hex_string (dwo_unit->signature));
3019eac3
DE
11571}
11572
33c5cd75 11573/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11574 Note: This function processes DWO files only, not DWP files. */
3019eac3 11575
33c5cd75 11576static void
976ca316 11577create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11578 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11579 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11580{
976ca316
SM
11581 struct objfile *objfile = per_objfile->objfile;
11582 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11583 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11584
96b79293 11585 section.read (objfile);
33c5cd75 11586 info_ptr = section.buffer;
3019eac3
DE
11587
11588 if (info_ptr == NULL)
33c5cd75 11589 return;
3019eac3 11590
6f738b01
SM
11591 dwarf_read_debug_printf ("Reading %s for %s:",
11592 section.get_name (),
11593 section.get_file_name ());
3019eac3 11594
33c5cd75 11595 end_ptr = info_ptr + section.size;
3019eac3
DE
11596 while (info_ptr < end_ptr)
11597 {
11598 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11599 struct dwo_unit read_unit {};
33c5cd75
DB
11600 struct dwo_unit *dwo_unit;
11601 void **slot;
11602 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11603
11604 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11605 per_cu.per_bfd = per_bfd;
3019eac3 11606 per_cu.is_debug_types = 0;
33c5cd75
DB
11607 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11608 per_cu.section = &section;
11609
976ca316 11610 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11611 if (!reader.dummy_p)
11612 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11613 &dwo_file, &read_unit);
33c5cd75
DB
11614 info_ptr += per_cu.length;
11615
11616 // If the unit could not be parsed, skip it.
c0ab21c2 11617 if (read_unit.dwo_file == NULL)
33c5cd75 11618 continue;
3019eac3 11619
33c5cd75 11620 if (cus_htab == NULL)
298e9637 11621 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11622
1859c670 11623 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11624 struct dwo_unit);
c0ab21c2 11625 *dwo_unit = read_unit;
b0b6a987 11626 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11627 gdb_assert (slot != NULL);
11628 if (*slot != NULL)
19c3d4c9 11629 {
33c5cd75
DB
11630 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11631 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11632
b98664d3 11633 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11634 " the entry at offset %s, signature %s"),
11635 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11636 hex_string (dwo_unit->signature));
19c3d4c9 11637 }
33c5cd75 11638 *slot = (void *)dwo_unit;
3019eac3 11639 }
3019eac3
DE
11640}
11641
80626a55
DE
11642/* DWP file .debug_{cu,tu}_index section format:
11643 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11644 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11645
11646 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11647 officially standard DWP format was published with DWARF v5 and is called
11648 Version 5. There are no versions 3 or 4.
80626a55 11649
d2415c6c
DE
11650 DWP Version 1:
11651
80626a55
DE
11652 Both index sections have the same format, and serve to map a 64-bit
11653 signature to a set of section numbers. Each section begins with a header,
11654 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11655 indexes, and a pool of 32-bit section numbers. The index sections will be
11656 aligned at 8-byte boundaries in the file.
11657
d2415c6c
DE
11658 The index section header consists of:
11659
11660 V, 32 bit version number
11661 -, 32 bits unused
11662 N, 32 bit number of compilation units or type units in the index
11663 M, 32 bit number of slots in the hash table
80626a55 11664
d2415c6c 11665 Numbers are recorded using the byte order of the application binary.
80626a55 11666
d2415c6c
DE
11667 The hash table begins at offset 16 in the section, and consists of an array
11668 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11669 order of the application binary). Unused slots in the hash table are 0.
11670 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11671
d2415c6c
DE
11672 The parallel table begins immediately after the hash table
11673 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11674 array of 32-bit indexes (using the byte order of the application binary),
11675 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11676 table contains a 32-bit index into the pool of section numbers. For unused
11677 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11678
73869dc2
DE
11679 The pool of section numbers begins immediately following the hash table
11680 (at offset 16 + 12 * M from the beginning of the section). The pool of
11681 section numbers consists of an array of 32-bit words (using the byte order
11682 of the application binary). Each item in the array is indexed starting
11683 from 0. The hash table entry provides the index of the first section
11684 number in the set. Additional section numbers in the set follow, and the
11685 set is terminated by a 0 entry (section number 0 is not used in ELF).
11686
11687 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11688 section must be the first entry in the set, and the .debug_abbrev.dwo must
11689 be the second entry. Other members of the set may follow in any order.
11690
11691 ---
11692
d2854d8d 11693 DWP Versions 2 and 5:
73869dc2 11694
d2854d8d 11695 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11696 and the entries in the index tables are now offsets into these sections.
11697 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11698 section.
11699
11700 Index Section Contents:
11701 Header
11702 Hash Table of Signatures dwp_hash_table.hash_table
11703 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11704 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11705 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11706
11707 The index section header consists of:
11708
11709 V, 32 bit version number
11710 L, 32 bit number of columns in the table of section offsets
11711 N, 32 bit number of compilation units or type units in the index
11712 M, 32 bit number of slots in the hash table
11713
11714 Numbers are recorded using the byte order of the application binary.
11715
11716 The hash table has the same format as version 1.
11717 The parallel table of indices has the same format as version 1,
11718 except that the entries are origin-1 indices into the table of sections
11719 offsets and the table of section sizes.
11720
11721 The table of offsets begins immediately following the parallel table
11722 (at offset 16 + 12 * M from the beginning of the section). The table is
11723 a two-dimensional array of 32-bit words (using the byte order of the
11724 application binary), with L columns and N+1 rows, in row-major order.
11725 Each row in the array is indexed starting from 0. The first row provides
11726 a key to the remaining rows: each column in this row provides an identifier
11727 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11728 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11729
11730 DW_SECT_INFO 1 .debug_info.dwo
11731 DW_SECT_TYPES 2 .debug_types.dwo
11732 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11733 DW_SECT_LINE 4 .debug_line.dwo
11734 DW_SECT_LOC 5 .debug_loc.dwo
11735 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11736 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11737 DW_SECT_MACRO 8 .debug_macro.dwo
11738
d2854d8d
CT
11739 The section identifiers for Version 5 are:
11740
11741 DW_SECT_INFO_V5 1 .debug_info.dwo
11742 DW_SECT_RESERVED_V5 2 --
11743 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11744 DW_SECT_LINE_V5 4 .debug_line.dwo
11745 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11746 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11747 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11748 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11749
73869dc2
DE
11750 The offsets provided by the CU and TU index sections are the base offsets
11751 for the contributions made by each CU or TU to the corresponding section
11752 in the package file. Each CU and TU header contains an abbrev_offset
11753 field, used to find the abbreviations table for that CU or TU within the
11754 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11755 be interpreted as relative to the base offset given in the index section.
11756 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11757 should be interpreted as relative to the base offset for .debug_line.dwo,
11758 and offsets into other debug sections obtained from DWARF attributes should
11759 also be interpreted as relative to the corresponding base offset.
11760
11761 The table of sizes begins immediately following the table of offsets.
11762 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11763 with L columns and N rows, in row-major order. Each row in the array is
11764 indexed starting from 1 (row 0 is shared by the two tables).
11765
11766 ---
11767
11768 Hash table lookup is handled the same in version 1 and 2:
11769
11770 We assume that N and M will not exceed 2^32 - 1.
11771 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11772
d2415c6c
DE
11773 Given a 64-bit compilation unit signature or a type signature S, an entry
11774 in the hash table is located as follows:
80626a55 11775
d2415c6c
DE
11776 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11777 the low-order k bits all set to 1.
80626a55 11778
d2415c6c 11779 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11780
d2415c6c
DE
11781 3) If the hash table entry at index H matches the signature, use that
11782 entry. If the hash table entry at index H is unused (all zeroes),
11783 terminate the search: the signature is not present in the table.
80626a55 11784
d2415c6c 11785 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11786
d2415c6c 11787 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11788 to stop at an unused slot or find the match. */
80626a55
DE
11789
11790/* Create a hash table to map DWO IDs to their CU/TU entry in
11791 .debug_{info,types}.dwo in DWP_FILE.
11792 Returns NULL if there isn't one.
11793 Note: This function processes DWP files only, not DWO files. */
11794
11795static struct dwp_hash_table *
976ca316 11796create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11797 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11798{
976ca316 11799 struct objfile *objfile = per_objfile->objfile;
400174b1 11800 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11801 const gdb_byte *index_ptr, *index_end;
80626a55 11802 struct dwarf2_section_info *index;
73869dc2 11803 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11804 struct dwp_hash_table *htab;
11805
11806 if (is_debug_types)
11807 index = &dwp_file->sections.tu_index;
11808 else
11809 index = &dwp_file->sections.cu_index;
11810
96b79293 11811 if (index->empty ())
80626a55 11812 return NULL;
96b79293 11813 index->read (objfile);
80626a55
DE
11814
11815 index_ptr = index->buffer;
11816 index_end = index_ptr + index->size;
11817
d2854d8d
CT
11818 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11819 For now it's safe to just read 4 bytes (particularly as it's difficult to
11820 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11821 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11822 index_ptr += 4;
d2854d8d 11823 if (version == 2 || version == 5)
73869dc2
DE
11824 nr_columns = read_4_bytes (dbfd, index_ptr);
11825 else
11826 nr_columns = 0;
11827 index_ptr += 4;
80626a55
DE
11828 nr_units = read_4_bytes (dbfd, index_ptr);
11829 index_ptr += 4;
11830 nr_slots = read_4_bytes (dbfd, index_ptr);
11831 index_ptr += 4;
11832
d2854d8d 11833 if (version != 1 && version != 2 && version != 5)
80626a55 11834 {
21aa081e 11835 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11836 " [in module %s]"),
21aa081e 11837 pulongest (version), dwp_file->name);
80626a55
DE
11838 }
11839 if (nr_slots != (nr_slots & -nr_slots))
11840 {
21aa081e 11841 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11842 " is not power of 2 [in module %s]"),
21aa081e 11843 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11844 }
11845
976ca316 11846 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11847 htab->version = version;
11848 htab->nr_columns = nr_columns;
80626a55
DE
11849 htab->nr_units = nr_units;
11850 htab->nr_slots = nr_slots;
11851 htab->hash_table = index_ptr;
11852 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11853
11854 /* Exit early if the table is empty. */
11855 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11856 || (version == 2 && nr_columns == 0)
11857 || (version == 5 && nr_columns == 0))
73869dc2
DE
11858 {
11859 /* All must be zero. */
11860 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11861 || (version == 2 && nr_columns != 0)
11862 || (version == 5 && nr_columns != 0))
73869dc2 11863 {
b98664d3 11864 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11865 " all zero [in modules %s]"),
11866 dwp_file->name);
11867 }
11868 return htab;
11869 }
11870
11871 if (version == 1)
11872 {
11873 htab->section_pool.v1.indices =
11874 htab->unit_table + sizeof (uint32_t) * nr_slots;
11875 /* It's harder to decide whether the section is too small in v1.
11876 V1 is deprecated anyway so we punt. */
11877 }
d2854d8d 11878 else if (version == 2)
73869dc2
DE
11879 {
11880 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11881 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11882 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11883 /* Reverse map for error checking. */
11884 int ids_seen[DW_SECT_MAX + 1];
11885 int i;
11886
11887 if (nr_columns < 2)
11888 {
11889 error (_("Dwarf Error: bad DWP hash table, too few columns"
11890 " in section table [in module %s]"),
11891 dwp_file->name);
11892 }
11893 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11894 {
11895 error (_("Dwarf Error: bad DWP hash table, too many columns"
11896 " in section table [in module %s]"),
11897 dwp_file->name);
11898 }
04fd5eed
GB
11899 memset (ids, 255, sizeof_ids);
11900 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11901 for (i = 0; i < nr_columns; ++i)
11902 {
11903 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11904
11905 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11906 {
11907 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11908 " in section table [in module %s]"),
11909 id, dwp_file->name);
11910 }
11911 if (ids_seen[id] != -1)
11912 {
11913 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11914 " id %d in section table [in module %s]"),
11915 id, dwp_file->name);
11916 }
11917 ids_seen[id] = i;
11918 ids[i] = id;
11919 }
11920 /* Must have exactly one info or types section. */
11921 if (((ids_seen[DW_SECT_INFO] != -1)
11922 + (ids_seen[DW_SECT_TYPES] != -1))
11923 != 1)
11924 {
11925 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11926 " DWO info/types section [in module %s]"),
11927 dwp_file->name);
11928 }
11929 /* Must have an abbrev section. */
11930 if (ids_seen[DW_SECT_ABBREV] == -1)
11931 {
11932 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11933 " section [in module %s]"),
11934 dwp_file->name);
11935 }
11936 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11937 htab->section_pool.v2.sizes =
11938 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11939 * nr_units * nr_columns);
11940 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11941 * nr_units * nr_columns))
11942 > index_end)
11943 {
11944 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11945 " [in module %s]"),
11946 dwp_file->name);
11947 }
11948 }
d2854d8d
CT
11949 else /* version == 5 */
11950 {
11951 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11952 int *ids = htab->section_pool.v5.section_ids;
11953 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11954 /* Reverse map for error checking. */
11955 int ids_seen[DW_SECT_MAX_V5 + 1];
11956
11957 if (nr_columns < 2)
11958 {
11959 error (_("Dwarf Error: bad DWP hash table, too few columns"
11960 " in section table [in module %s]"),
11961 dwp_file->name);
11962 }
11963 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11964 {
11965 error (_("Dwarf Error: bad DWP hash table, too many columns"
11966 " in section table [in module %s]"),
11967 dwp_file->name);
11968 }
11969 memset (ids, 255, sizeof_ids);
11970 memset (ids_seen, 255, sizeof (ids_seen));
11971 for (int i = 0; i < nr_columns; ++i)
11972 {
11973 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11974
11975 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11976 {
11977 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11978 " in section table [in module %s]"),
11979 id, dwp_file->name);
11980 }
11981 if (ids_seen[id] != -1)
11982 {
11983 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11984 " id %d in section table [in module %s]"),
11985 id, dwp_file->name);
11986 }
11987 ids_seen[id] = i;
11988 ids[i] = id;
11989 }
11990 /* Must have seen an info section. */
11991 if (ids_seen[DW_SECT_INFO_V5] == -1)
11992 {
11993 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11994 " DWO info/types section [in module %s]"),
11995 dwp_file->name);
11996 }
11997 /* Must have an abbrev section. */
11998 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11999 {
12000 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
12001 " section [in module %s]"),
12002 dwp_file->name);
12003 }
12004 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12005 htab->section_pool.v5.sizes
12006 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12007 * nr_units * nr_columns);
12008 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12009 * nr_units * nr_columns))
12010 > index_end)
12011 {
12012 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12013 " [in module %s]"),
12014 dwp_file->name);
12015 }
12016 }
80626a55
DE
12017
12018 return htab;
12019}
12020
12021/* Update SECTIONS with the data from SECTP.
12022
5bb6e9dd
TT
12023 This function is like the other "locate" section routines, but in
12024 this context the sections to read comes from the DWP V1 hash table,
12025 not the full ELF section table.
80626a55
DE
12026
12027 The result is non-zero for success, or zero if an error was found. */
12028
12029static int
73869dc2
DE
12030locate_v1_virtual_dwo_sections (asection *sectp,
12031 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12032{
12033 const struct dwop_section_names *names = &dwop_section_names;
12034
12035 if (section_is_p (sectp->name, &names->abbrev_dwo))
12036 {
12037 /* There can be only one. */
049412e3 12038 if (sections->abbrev.s.section != NULL)
80626a55 12039 return 0;
049412e3 12040 sections->abbrev.s.section = sectp;
fd361982 12041 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12042 }
12043 else if (section_is_p (sectp->name, &names->info_dwo)
12044 || section_is_p (sectp->name, &names->types_dwo))
12045 {
12046 /* There can be only one. */
049412e3 12047 if (sections->info_or_types.s.section != NULL)
80626a55 12048 return 0;
049412e3 12049 sections->info_or_types.s.section = sectp;
fd361982 12050 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12051 }
12052 else if (section_is_p (sectp->name, &names->line_dwo))
12053 {
12054 /* There can be only one. */
049412e3 12055 if (sections->line.s.section != NULL)
80626a55 12056 return 0;
049412e3 12057 sections->line.s.section = sectp;
fd361982 12058 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12059 }
12060 else if (section_is_p (sectp->name, &names->loc_dwo))
12061 {
12062 /* There can be only one. */
049412e3 12063 if (sections->loc.s.section != NULL)
80626a55 12064 return 0;
049412e3 12065 sections->loc.s.section = sectp;
fd361982 12066 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12067 }
12068 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12069 {
12070 /* There can be only one. */
049412e3 12071 if (sections->macinfo.s.section != NULL)
80626a55 12072 return 0;
049412e3 12073 sections->macinfo.s.section = sectp;
fd361982 12074 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12075 }
12076 else if (section_is_p (sectp->name, &names->macro_dwo))
12077 {
12078 /* There can be only one. */
049412e3 12079 if (sections->macro.s.section != NULL)
80626a55 12080 return 0;
049412e3 12081 sections->macro.s.section = sectp;
fd361982 12082 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12083 }
12084 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12085 {
12086 /* There can be only one. */
049412e3 12087 if (sections->str_offsets.s.section != NULL)
80626a55 12088 return 0;
049412e3 12089 sections->str_offsets.s.section = sectp;
fd361982 12090 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12091 }
12092 else
12093 {
12094 /* No other kind of section is valid. */
12095 return 0;
12096 }
12097
12098 return 1;
12099}
12100
73869dc2
DE
12101/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12102 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12103 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12104 This is for DWP version 1 files. */
80626a55
DE
12105
12106static struct dwo_unit *
976ca316 12107create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12108 struct dwp_file *dwp_file,
73869dc2
DE
12109 uint32_t unit_index,
12110 const char *comp_dir,
12111 ULONGEST signature, int is_debug_types)
80626a55 12112{
73869dc2
DE
12113 const struct dwp_hash_table *dwp_htab =
12114 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12115 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12116 const char *kind = is_debug_types ? "TU" : "CU";
12117 struct dwo_file *dwo_file;
12118 struct dwo_unit *dwo_unit;
73869dc2 12119 struct virtual_v1_dwo_sections sections;
80626a55 12120 void **dwo_file_slot;
80626a55
DE
12121 int i;
12122
73869dc2
DE
12123 gdb_assert (dwp_file->version == 1);
12124
6f738b01
SM
12125 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12126 kind, pulongest (unit_index), hex_string (signature),
12127 dwp_file->name);
80626a55 12128
19ac8c2e 12129 /* Fetch the sections of this DWO unit.
80626a55
DE
12130 Put a limit on the number of sections we look for so that bad data
12131 doesn't cause us to loop forever. */
12132
73869dc2 12133#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12134 (1 /* .debug_info or .debug_types */ \
12135 + 1 /* .debug_abbrev */ \
12136 + 1 /* .debug_line */ \
12137 + 1 /* .debug_loc */ \
12138 + 1 /* .debug_str_offsets */ \
19ac8c2e 12139 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12140 + 1 /* trailing zero */)
12141
12142 memset (&sections, 0, sizeof (sections));
80626a55 12143
73869dc2 12144 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12145 {
12146 asection *sectp;
12147 uint32_t section_nr =
12148 read_4_bytes (dbfd,
73869dc2
DE
12149 dwp_htab->section_pool.v1.indices
12150 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12151
12152 if (section_nr == 0)
12153 break;
12154 if (section_nr >= dwp_file->num_sections)
12155 {
12156 error (_("Dwarf Error: bad DWP hash table, section number too large"
12157 " [in module %s]"),
12158 dwp_file->name);
12159 }
12160
12161 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12162 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12163 {
12164 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12165 " [in module %s]"),
12166 dwp_file->name);
12167 }
12168 }
12169
12170 if (i < 2
96b79293
TT
12171 || sections.info_or_types.empty ()
12172 || sections.abbrev.empty ())
80626a55
DE
12173 {
12174 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12175 " [in module %s]"),
12176 dwp_file->name);
12177 }
73869dc2 12178 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12179 {
12180 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12181 " [in module %s]"),
12182 dwp_file->name);
12183 }
12184
12185 /* It's easier for the rest of the code if we fake a struct dwo_file and
12186 have dwo_unit "live" in that. At least for now.
12187
12188 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12189 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12190 file, we can combine them back into a virtual DWO file to save space
12191 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12192 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12193
791afaa2
TT
12194 std::string virtual_dwo_name =
12195 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12196 sections.abbrev.get_id (),
12197 sections.line.get_id (),
12198 sections.loc.get_id (),
12199 sections.str_offsets.get_id ());
80626a55 12200 /* Can we use an existing virtual DWO file? */
976ca316 12201 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12202 comp_dir);
80626a55
DE
12203 /* Create one if necessary. */
12204 if (*dwo_file_slot == NULL)
12205 {
6f738b01
SM
12206 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12207 virtual_dwo_name.c_str ());
12208
51ac9db5 12209 dwo_file = new struct dwo_file;
976ca316 12210 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12211 dwo_file->comp_dir = comp_dir;
80626a55
DE
12212 dwo_file->sections.abbrev = sections.abbrev;
12213 dwo_file->sections.line = sections.line;
12214 dwo_file->sections.loc = sections.loc;
12215 dwo_file->sections.macinfo = sections.macinfo;
12216 dwo_file->sections.macro = sections.macro;
12217 dwo_file->sections.str_offsets = sections.str_offsets;
12218 /* The "str" section is global to the entire DWP file. */
12219 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12220 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12221 there's no need to record it in dwo_file.
12222 Also, we can't simply record type sections in dwo_file because
12223 we record a pointer into the vector in dwo_unit. As we collect more
12224 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12225 for it, invalidating all copies of pointers into the previous
12226 contents. */
80626a55
DE
12227 *dwo_file_slot = dwo_file;
12228 }
12229 else
12230 {
6f738b01
SM
12231 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12232 virtual_dwo_name.c_str ());
12233
9a3c8263 12234 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12235 }
80626a55 12236
976ca316 12237 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12238 dwo_unit->dwo_file = dwo_file;
12239 dwo_unit->signature = signature;
8d749320 12240 dwo_unit->section =
976ca316 12241 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12242 *dwo_unit->section = sections.info_or_types;
57d63ce2 12243 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12244
12245 return dwo_unit;
12246}
12247
d2854d8d
CT
12248/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12249 simplify them. Given a pointer to the containing section SECTION, and
12250 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12251 virtual section of just that piece. */
73869dc2
DE
12252
12253static struct dwarf2_section_info
d2854d8d
CT
12254create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12255 struct dwarf2_section_info *section,
12256 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12257{
12258 struct dwarf2_section_info result;
12259 asection *sectp;
12260
12261 gdb_assert (section != NULL);
12262 gdb_assert (!section->is_virtual);
12263
12264 memset (&result, 0, sizeof (result));
12265 result.s.containing_section = section;
dc4ccb6f 12266 result.is_virtual = true;
73869dc2
DE
12267
12268 if (size == 0)
12269 return result;
12270
96b79293 12271 sectp = section->get_bfd_section ();
73869dc2
DE
12272
12273 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12274 bounds of the real section. This is a pretty-rare event, so just
12275 flag an error (easier) instead of a warning and trying to cope. */
12276 if (sectp == NULL
fd361982 12277 || offset + size > bfd_section_size (sectp))
73869dc2 12278 {
d2854d8d 12279 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12280 " in section %s [in module %s]"),
fd361982 12281 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12282 objfile_name (per_objfile->objfile));
73869dc2
DE
12283 }
12284
12285 result.virtual_offset = offset;
12286 result.size = size;
12287 return result;
12288}
12289
12290/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12291 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12292 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12293 This is for DWP version 2 files. */
12294
12295static struct dwo_unit *
976ca316 12296create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12297 struct dwp_file *dwp_file,
73869dc2
DE
12298 uint32_t unit_index,
12299 const char *comp_dir,
12300 ULONGEST signature, int is_debug_types)
12301{
73869dc2
DE
12302 const struct dwp_hash_table *dwp_htab =
12303 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12304 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12305 const char *kind = is_debug_types ? "TU" : "CU";
12306 struct dwo_file *dwo_file;
12307 struct dwo_unit *dwo_unit;
d2854d8d 12308 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12309 void **dwo_file_slot;
73869dc2
DE
12310 int i;
12311
12312 gdb_assert (dwp_file->version == 2);
12313
6f738b01
SM
12314 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12315 kind, pulongest (unit_index), hex_string (signature),
12316 dwp_file->name);
73869dc2
DE
12317
12318 /* Fetch the section offsets of this DWO unit. */
12319
12320 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12321
12322 for (i = 0; i < dwp_htab->nr_columns; ++i)
12323 {
12324 uint32_t offset = read_4_bytes (dbfd,
12325 dwp_htab->section_pool.v2.offsets
12326 + (((unit_index - 1) * dwp_htab->nr_columns
12327 + i)
12328 * sizeof (uint32_t)));
12329 uint32_t size = read_4_bytes (dbfd,
12330 dwp_htab->section_pool.v2.sizes
12331 + (((unit_index - 1) * dwp_htab->nr_columns
12332 + i)
12333 * sizeof (uint32_t)));
12334
12335 switch (dwp_htab->section_pool.v2.section_ids[i])
12336 {
12337 case DW_SECT_INFO:
12338 case DW_SECT_TYPES:
12339 sections.info_or_types_offset = offset;
12340 sections.info_or_types_size = size;
12341 break;
12342 case DW_SECT_ABBREV:
12343 sections.abbrev_offset = offset;
12344 sections.abbrev_size = size;
12345 break;
12346 case DW_SECT_LINE:
12347 sections.line_offset = offset;
12348 sections.line_size = size;
12349 break;
12350 case DW_SECT_LOC:
12351 sections.loc_offset = offset;
12352 sections.loc_size = size;
12353 break;
12354 case DW_SECT_STR_OFFSETS:
12355 sections.str_offsets_offset = offset;
12356 sections.str_offsets_size = size;
12357 break;
12358 case DW_SECT_MACINFO:
12359 sections.macinfo_offset = offset;
12360 sections.macinfo_size = size;
12361 break;
12362 case DW_SECT_MACRO:
12363 sections.macro_offset = offset;
12364 sections.macro_size = size;
12365 break;
12366 }
12367 }
12368
12369 /* It's easier for the rest of the code if we fake a struct dwo_file and
12370 have dwo_unit "live" in that. At least for now.
12371
12372 The DWP file can be made up of a random collection of CUs and TUs.
12373 However, for each CU + set of TUs that came from the same original DWO
12374 file, we can combine them back into a virtual DWO file to save space
12375 (fewer struct dwo_file objects to allocate). Remember that for really
12376 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12377
791afaa2
TT
12378 std::string virtual_dwo_name =
12379 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12380 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12381 (long) (sections.line_size ? sections.line_offset : 0),
12382 (long) (sections.loc_size ? sections.loc_offset : 0),
12383 (long) (sections.str_offsets_size
12384 ? sections.str_offsets_offset : 0));
73869dc2 12385 /* Can we use an existing virtual DWO file? */
976ca316 12386 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12387 comp_dir);
73869dc2
DE
12388 /* Create one if necessary. */
12389 if (*dwo_file_slot == NULL)
12390 {
6f738b01
SM
12391 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12392 virtual_dwo_name.c_str ());
12393
51ac9db5 12394 dwo_file = new struct dwo_file;
976ca316 12395 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12396 dwo_file->comp_dir = comp_dir;
12397 dwo_file->sections.abbrev =
d2854d8d
CT
12398 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12399 sections.abbrev_offset,
12400 sections.abbrev_size);
73869dc2 12401 dwo_file->sections.line =
d2854d8d
CT
12402 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12403 sections.line_offset,
12404 sections.line_size);
73869dc2 12405 dwo_file->sections.loc =
d2854d8d
CT
12406 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12407 sections.loc_offset, sections.loc_size);
73869dc2 12408 dwo_file->sections.macinfo =
d2854d8d
CT
12409 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12410 sections.macinfo_offset,
12411 sections.macinfo_size);
73869dc2 12412 dwo_file->sections.macro =
d2854d8d
CT
12413 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12414 sections.macro_offset,
12415 sections.macro_size);
73869dc2 12416 dwo_file->sections.str_offsets =
d2854d8d
CT
12417 create_dwp_v2_or_v5_section (per_objfile,
12418 &dwp_file->sections.str_offsets,
12419 sections.str_offsets_offset,
12420 sections.str_offsets_size);
73869dc2
DE
12421 /* The "str" section is global to the entire DWP file. */
12422 dwo_file->sections.str = dwp_file->sections.str;
12423 /* The info or types section is assigned below to dwo_unit,
12424 there's no need to record it in dwo_file.
12425 Also, we can't simply record type sections in dwo_file because
12426 we record a pointer into the vector in dwo_unit. As we collect more
12427 types we'll grow the vector and eventually have to reallocate space
12428 for it, invalidating all copies of pointers into the previous
12429 contents. */
12430 *dwo_file_slot = dwo_file;
12431 }
12432 else
12433 {
6f738b01
SM
12434 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12435 virtual_dwo_name.c_str ());
12436
9a3c8263 12437 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12438 }
73869dc2 12439
976ca316 12440 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12441 dwo_unit->dwo_file = dwo_file;
12442 dwo_unit->signature = signature;
8d749320 12443 dwo_unit->section =
976ca316 12444 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12445 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12446 (per_objfile,
d2854d8d
CT
12447 is_debug_types
12448 ? &dwp_file->sections.types
12449 : &dwp_file->sections.info,
12450 sections.info_or_types_offset,
12451 sections.info_or_types_size);
12452 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12453
12454 return dwo_unit;
12455}
12456
12457/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12458 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12459 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12460 This is for DWP version 5 files. */
12461
12462static struct dwo_unit *
12463create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12464 struct dwp_file *dwp_file,
12465 uint32_t unit_index,
12466 const char *comp_dir,
12467 ULONGEST signature, int is_debug_types)
12468{
12469 const struct dwp_hash_table *dwp_htab
12470 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12471 bfd *dbfd = dwp_file->dbfd.get ();
12472 const char *kind = is_debug_types ? "TU" : "CU";
12473 struct dwo_file *dwo_file;
12474 struct dwo_unit *dwo_unit;
12475 struct virtual_v2_or_v5_dwo_sections sections {};
12476 void **dwo_file_slot;
12477
12478 gdb_assert (dwp_file->version == 5);
12479
6f738b01
SM
12480 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12481 kind, pulongest (unit_index), hex_string (signature),
12482 dwp_file->name);
d2854d8d
CT
12483
12484 /* Fetch the section offsets of this DWO unit. */
12485
12486 /* memset (&sections, 0, sizeof (sections)); */
12487
12488 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12489 {
12490 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12491 dwp_htab->section_pool.v5.offsets
12492 + (((unit_index - 1)
12493 * dwp_htab->nr_columns
12494 + i)
12495 * sizeof (uint32_t)));
d2854d8d 12496 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12497 dwp_htab->section_pool.v5.sizes
12498 + (((unit_index - 1) * dwp_htab->nr_columns
12499 + i)
12500 * sizeof (uint32_t)));
d2854d8d
CT
12501
12502 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12503 {
12504 case DW_SECT_ABBREV_V5:
12505 sections.abbrev_offset = offset;
12506 sections.abbrev_size = size;
12507 break;
12508 case DW_SECT_INFO_V5:
12509 sections.info_or_types_offset = offset;
12510 sections.info_or_types_size = size;
12511 break;
12512 case DW_SECT_LINE_V5:
12513 sections.line_offset = offset;
12514 sections.line_size = size;
12515 break;
12516 case DW_SECT_LOCLISTS_V5:
12517 sections.loclists_offset = offset;
12518 sections.loclists_size = size;
12519 break;
12520 case DW_SECT_MACRO_V5:
12521 sections.macro_offset = offset;
12522 sections.macro_size = size;
12523 break;
12524 case DW_SECT_RNGLISTS_V5:
12525 sections.rnglists_offset = offset;
12526 sections.rnglists_size = size;
12527 break;
12528 case DW_SECT_STR_OFFSETS_V5:
12529 sections.str_offsets_offset = offset;
12530 sections.str_offsets_size = size;
12531 break;
12532 case DW_SECT_RESERVED_V5:
12533 default:
12534 break;
12535 }
d2854d8d
CT
12536 }
12537
12538 /* It's easier for the rest of the code if we fake a struct dwo_file and
12539 have dwo_unit "live" in that. At least for now.
12540
12541 The DWP file can be made up of a random collection of CUs and TUs.
12542 However, for each CU + set of TUs that came from the same original DWO
12543 file, we can combine them back into a virtual DWO file to save space
12544 (fewer struct dwo_file objects to allocate). Remember that for really
12545 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12546
12547 std::string virtual_dwo_name =
12548 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12549 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12550 (long) (sections.line_size ? sections.line_offset : 0),
12551 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12552 (long) (sections.str_offsets_size
12553 ? sections.str_offsets_offset : 0),
12554 (long) (sections.macro_size ? sections.macro_offset : 0),
12555 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12556 /* Can we use an existing virtual DWO file? */
12557 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12558 virtual_dwo_name.c_str (),
12559 comp_dir);
d2854d8d
CT
12560 /* Create one if necessary. */
12561 if (*dwo_file_slot == NULL)
12562 {
6f738b01
SM
12563 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12564 virtual_dwo_name.c_str ());
12565
d2854d8d
CT
12566 dwo_file = new struct dwo_file;
12567 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12568 dwo_file->comp_dir = comp_dir;
12569 dwo_file->sections.abbrev =
dda83cd7
SM
12570 create_dwp_v2_or_v5_section (per_objfile,
12571 &dwp_file->sections.abbrev,
12572 sections.abbrev_offset,
12573 sections.abbrev_size);
d2854d8d 12574 dwo_file->sections.line =
dda83cd7
SM
12575 create_dwp_v2_or_v5_section (per_objfile,
12576 &dwp_file->sections.line,
12577 sections.line_offset, sections.line_size);
d2854d8d 12578 dwo_file->sections.macro =
dda83cd7
SM
12579 create_dwp_v2_or_v5_section (per_objfile,
12580 &dwp_file->sections.macro,
12581 sections.macro_offset,
12582 sections.macro_size);
d2854d8d 12583 dwo_file->sections.loclists =
dda83cd7
SM
12584 create_dwp_v2_or_v5_section (per_objfile,
12585 &dwp_file->sections.loclists,
12586 sections.loclists_offset,
12587 sections.loclists_size);
d2854d8d 12588 dwo_file->sections.rnglists =
dda83cd7
SM
12589 create_dwp_v2_or_v5_section (per_objfile,
12590 &dwp_file->sections.rnglists,
12591 sections.rnglists_offset,
12592 sections.rnglists_size);
d2854d8d 12593 dwo_file->sections.str_offsets =
dda83cd7
SM
12594 create_dwp_v2_or_v5_section (per_objfile,
12595 &dwp_file->sections.str_offsets,
12596 sections.str_offsets_offset,
12597 sections.str_offsets_size);
d2854d8d
CT
12598 /* The "str" section is global to the entire DWP file. */
12599 dwo_file->sections.str = dwp_file->sections.str;
12600 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12601 there's no need to record it in dwo_file.
12602 Also, we can't simply record type sections in dwo_file because
12603 we record a pointer into the vector in dwo_unit. As we collect more
12604 types we'll grow the vector and eventually have to reallocate space
12605 for it, invalidating all copies of pointers into the previous
12606 contents. */
d2854d8d
CT
12607 *dwo_file_slot = dwo_file;
12608 }
12609 else
12610 {
6f738b01
SM
12611 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12612 virtual_dwo_name.c_str ());
12613
d2854d8d
CT
12614 dwo_file = (struct dwo_file *) *dwo_file_slot;
12615 }
12616
12617 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12618 dwo_unit->dwo_file = dwo_file;
12619 dwo_unit->signature = signature;
12620 dwo_unit->section
12621 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12622 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12623 &dwp_file->sections.info,
12624 sections.info_or_types_offset,
12625 sections.info_or_types_size);
73869dc2
DE
12626 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12627
12628 return dwo_unit;
12629}
12630
57d63ce2
DE
12631/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12632 Returns NULL if the signature isn't found. */
80626a55
DE
12633
12634static struct dwo_unit *
976ca316 12635lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12636 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12637 ULONGEST signature, int is_debug_types)
80626a55 12638{
57d63ce2
DE
12639 const struct dwp_hash_table *dwp_htab =
12640 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12641 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12642 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12643 uint32_t hash = signature & mask;
12644 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12645 unsigned int i;
12646 void **slot;
870f88f7 12647 struct dwo_unit find_dwo_cu;
80626a55
DE
12648
12649 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12650 find_dwo_cu.signature = signature;
19ac8c2e 12651 slot = htab_find_slot (is_debug_types
48b490f2
TT
12652 ? dwp_file->loaded_tus.get ()
12653 : dwp_file->loaded_cus.get (),
19ac8c2e 12654 &find_dwo_cu, INSERT);
80626a55
DE
12655
12656 if (*slot != NULL)
9a3c8263 12657 return (struct dwo_unit *) *slot;
80626a55
DE
12658
12659 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12660 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12661 {
12662 ULONGEST signature_in_table;
12663
12664 signature_in_table =
57d63ce2 12665 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12666 if (signature_in_table == signature)
12667 {
57d63ce2
DE
12668 uint32_t unit_index =
12669 read_4_bytes (dbfd,
12670 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12671
73869dc2
DE
12672 if (dwp_file->version == 1)
12673 {
976ca316
SM
12674 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12675 unit_index, comp_dir,
12676 signature, is_debug_types);
73869dc2 12677 }
d2854d8d 12678 else if (dwp_file->version == 2)
73869dc2 12679 {
976ca316
SM
12680 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12681 unit_index, comp_dir,
12682 signature, is_debug_types);
73869dc2 12683 }
d2854d8d
CT
12684 else /* version == 5 */
12685 {
12686 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12687 unit_index, comp_dir,
12688 signature, is_debug_types);
12689 }
9a3c8263 12690 return (struct dwo_unit *) *slot;
80626a55
DE
12691 }
12692 if (signature_in_table == 0)
12693 return NULL;
12694 hash = (hash + hash2) & mask;
12695 }
12696
12697 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12698 " [in module %s]"),
12699 dwp_file->name);
12700}
12701
ab5088bf 12702/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12703 Open the file specified by FILE_NAME and hand it off to BFD for
12704 preliminary analysis. Return a newly initialized bfd *, which
12705 includes a canonicalized copy of FILE_NAME.
80626a55 12706 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12707 SEARCH_CWD is true if the current directory is to be searched.
12708 It will be searched before debug-file-directory.
13aaf454
DE
12709 If successful, the file is added to the bfd include table of the
12710 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12711 If unable to find/open the file, return NULL.
3019eac3
DE
12712 NOTE: This function is derived from symfile_bfd_open. */
12713
192b62ce 12714static gdb_bfd_ref_ptr
976ca316 12715try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12716 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12717{
24b9144d 12718 int desc;
9c02c129
DE
12719 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12720 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12721 to debug_file_directory. */
e0cc99a6 12722 const char *search_path;
9c02c129
DE
12723 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12724
e0cc99a6 12725 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12726 if (search_cwd)
12727 {
12728 if (*debug_file_directory != '\0')
e0cc99a6
TT
12729 {
12730 search_path_holder.reset (concat (".", dirname_separator_string,
12731 debug_file_directory,
12732 (char *) NULL));
12733 search_path = search_path_holder.get ();
12734 }
6ac97d4c 12735 else
e0cc99a6 12736 search_path = ".";
6ac97d4c 12737 }
9c02c129 12738 else
e0cc99a6 12739 search_path = debug_file_directory;
3019eac3 12740
24b9144d 12741 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12742 if (is_dwp)
12743 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12744
12745 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12746 desc = openp (search_path, flags, file_name,
3019eac3
DE
12747 O_RDONLY | O_BINARY, &absolute_name);
12748 if (desc < 0)
12749 return NULL;
12750
e0cc99a6
TT
12751 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12752 gnutarget, desc));
9c02c129
DE
12753 if (sym_bfd == NULL)
12754 return NULL;
192b62ce 12755 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12756
192b62ce
TT
12757 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12758 return NULL;
3019eac3 12759
13aaf454
DE
12760 /* Success. Record the bfd as having been included by the objfile's bfd.
12761 This is important because things like demangled_names_hash lives in the
12762 objfile's per_bfd space and may have references to things like symbol
12763 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12764 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12765
3019eac3
DE
12766 return sym_bfd;
12767}
12768
ab5088bf 12769/* Try to open DWO file FILE_NAME.
3019eac3
DE
12770 COMP_DIR is the DW_AT_comp_dir attribute.
12771 The result is the bfd handle of the file.
12772 If there is a problem finding or opening the file, return NULL.
12773 Upon success, the canonicalized path of the file is stored in the bfd,
12774 same as symfile_bfd_open. */
12775
192b62ce 12776static gdb_bfd_ref_ptr
976ca316 12777open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12778 const char *file_name, const char *comp_dir)
3019eac3 12779{
80626a55 12780 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12781 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12782 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12783
12784 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12785
12786 if (comp_dir != NULL)
12787 {
43816ebc
TT
12788 gdb::unique_xmalloc_ptr<char> path_to_try
12789 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12790
12791 /* NOTE: If comp_dir is a relative path, this will also try the
12792 search path, which seems useful. */
976ca316 12793 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12794 0 /*is_dwp*/,
192b62ce 12795 1 /*search_cwd*/));
3019eac3
DE
12796 if (abfd != NULL)
12797 return abfd;
12798 }
12799
12800 /* That didn't work, try debug-file-directory, which, despite its name,
12801 is a list of paths. */
12802
12803 if (*debug_file_directory == '\0')
12804 return NULL;
12805
976ca316 12806 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12807 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12808}
12809
80626a55
DE
12810/* This function is mapped across the sections and remembers the offset and
12811 size of each of the DWO debugging sections we are interested in. */
12812
12813static void
5bb6e9dd
TT
12814dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12815 dwo_sections *dwo_sections)
80626a55 12816{
80626a55
DE
12817 const struct dwop_section_names *names = &dwop_section_names;
12818
12819 if (section_is_p (sectp->name, &names->abbrev_dwo))
12820 {
049412e3 12821 dwo_sections->abbrev.s.section = sectp;
fd361982 12822 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12823 }
12824 else if (section_is_p (sectp->name, &names->info_dwo))
12825 {
049412e3 12826 dwo_sections->info.s.section = sectp;
fd361982 12827 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12828 }
12829 else if (section_is_p (sectp->name, &names->line_dwo))
12830 {
049412e3 12831 dwo_sections->line.s.section = sectp;
fd361982 12832 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12833 }
12834 else if (section_is_p (sectp->name, &names->loc_dwo))
12835 {
049412e3 12836 dwo_sections->loc.s.section = sectp;
fd361982 12837 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12838 }
41144253 12839 else if (section_is_p (sectp->name, &names->loclists_dwo))
12840 {
12841 dwo_sections->loclists.s.section = sectp;
12842 dwo_sections->loclists.size = bfd_section_size (sectp);
12843 }
80626a55
DE
12844 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12845 {
049412e3 12846 dwo_sections->macinfo.s.section = sectp;
fd361982 12847 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12848 }
12849 else if (section_is_p (sectp->name, &names->macro_dwo))
12850 {
049412e3 12851 dwo_sections->macro.s.section = sectp;
fd361982 12852 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12853 }
d0ce17d8
CT
12854 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12855 {
12856 dwo_sections->rnglists.s.section = sectp;
12857 dwo_sections->rnglists.size = bfd_section_size (sectp);
12858 }
80626a55
DE
12859 else if (section_is_p (sectp->name, &names->str_dwo))
12860 {
049412e3 12861 dwo_sections->str.s.section = sectp;
fd361982 12862 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12863 }
12864 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12865 {
049412e3 12866 dwo_sections->str_offsets.s.section = sectp;
fd361982 12867 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12868 }
12869 else if (section_is_p (sectp->name, &names->types_dwo))
12870 {
12871 struct dwarf2_section_info type_section;
12872
12873 memset (&type_section, 0, sizeof (type_section));
049412e3 12874 type_section.s.section = sectp;
fd361982 12875 type_section.size = bfd_section_size (sectp);
fd5866f6 12876 dwo_sections->types.push_back (type_section);
80626a55
DE
12877 }
12878}
12879
ab5088bf 12880/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12881 by PER_CU. This is for the non-DWP case.
80626a55 12882 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12883
12884static struct dwo_file *
4ab09049
SM
12885open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12886 const char *comp_dir)
3019eac3 12887{
976ca316 12888 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12889
976ca316 12890 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12891 if (dbfd == NULL)
12892 {
6f738b01
SM
12893 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12894
80626a55
DE
12895 return NULL;
12896 }
263db9a1 12897
51ac9db5 12898 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12899 dwo_file->dwo_name = dwo_name;
12900 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12901 dwo_file->dbfd = std::move (dbfd);
3019eac3 12902
5bb6e9dd
TT
12903 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12904 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12905 &dwo_file->sections);
3019eac3 12906
976ca316
SM
12907 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12908 dwo_file->cus);
3019eac3 12909
d2854d8d
CT
12910 if (cu->per_cu->dwarf_version < 5)
12911 {
12912 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12913 dwo_file->sections.types, dwo_file->tus);
12914 }
12915 else
12916 {
12917 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12918 &dwo_file->sections.info, dwo_file->tus,
12919 rcuh_kind::TYPE);
12920 }
3019eac3 12921
6f738b01 12922 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12923
263db9a1 12924 return dwo_file.release ();
3019eac3
DE
12925}
12926
80626a55 12927/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12928 size of each of the DWP debugging sections common to version 1 and 2 that
12929 we are interested in. */
3019eac3 12930
80626a55 12931static void
73869dc2 12932dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12933 dwp_file *dwp_file)
3019eac3 12934{
80626a55
DE
12935 const struct dwop_section_names *names = &dwop_section_names;
12936 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12937
80626a55 12938 /* Record the ELF section number for later lookup: this is what the
73869dc2 12939 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12940 gdb_assert (elf_section_nr < dwp_file->num_sections);
12941 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12942
80626a55
DE
12943 /* Look for specific sections that we need. */
12944 if (section_is_p (sectp->name, &names->str_dwo))
12945 {
049412e3 12946 dwp_file->sections.str.s.section = sectp;
fd361982 12947 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12948 }
12949 else if (section_is_p (sectp->name, &names->cu_index))
12950 {
049412e3 12951 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12952 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12953 }
12954 else if (section_is_p (sectp->name, &names->tu_index))
12955 {
049412e3 12956 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12957 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12958 }
12959}
3019eac3 12960
73869dc2
DE
12961/* This function is mapped across the sections and remembers the offset and
12962 size of each of the DWP version 2 debugging sections that we are interested
12963 in. This is split into a separate function because we don't know if we
d2854d8d 12964 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12965
12966static void
12967dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12968{
9a3c8263 12969 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12970 const struct dwop_section_names *names = &dwop_section_names;
12971 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12972
12973 /* Record the ELF section number for later lookup: this is what the
12974 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12975 gdb_assert (elf_section_nr < dwp_file->num_sections);
12976 dwp_file->elf_sections[elf_section_nr] = sectp;
12977
12978 /* Look for specific sections that we need. */
12979 if (section_is_p (sectp->name, &names->abbrev_dwo))
12980 {
049412e3 12981 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12982 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12983 }
12984 else if (section_is_p (sectp->name, &names->info_dwo))
12985 {
049412e3 12986 dwp_file->sections.info.s.section = sectp;
fd361982 12987 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12988 }
12989 else if (section_is_p (sectp->name, &names->line_dwo))
12990 {
049412e3 12991 dwp_file->sections.line.s.section = sectp;
fd361982 12992 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12993 }
12994 else if (section_is_p (sectp->name, &names->loc_dwo))
12995 {
049412e3 12996 dwp_file->sections.loc.s.section = sectp;
fd361982 12997 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12998 }
12999 else if (section_is_p (sectp->name, &names->macinfo_dwo))
13000 {
049412e3 13001 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13002 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13003 }
13004 else if (section_is_p (sectp->name, &names->macro_dwo))
13005 {
049412e3 13006 dwp_file->sections.macro.s.section = sectp;
fd361982 13007 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13008 }
13009 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13010 {
049412e3 13011 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13012 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13013 }
13014 else if (section_is_p (sectp->name, &names->types_dwo))
13015 {
049412e3 13016 dwp_file->sections.types.s.section = sectp;
fd361982 13017 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13018 }
13019}
13020
d2854d8d
CT
13021/* This function is mapped across the sections and remembers the offset and
13022 size of each of the DWP version 5 debugging sections that we are interested
13023 in. This is split into a separate function because we don't know if we
13024 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13025
13026static void
13027dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13028{
13029 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13030 const struct dwop_section_names *names = &dwop_section_names;
13031 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13032
13033 /* Record the ELF section number for later lookup: this is what the
13034 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13035 gdb_assert (elf_section_nr < dwp_file->num_sections);
13036 dwp_file->elf_sections[elf_section_nr] = sectp;
13037
13038 /* Look for specific sections that we need. */
13039 if (section_is_p (sectp->name, &names->abbrev_dwo))
13040 {
13041 dwp_file->sections.abbrev.s.section = sectp;
13042 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13043 }
13044 else if (section_is_p (sectp->name, &names->info_dwo))
13045 {
13046 dwp_file->sections.info.s.section = sectp;
13047 dwp_file->sections.info.size = bfd_section_size (sectp);
13048 }
13049 else if (section_is_p (sectp->name, &names->line_dwo))
13050 {
13051 dwp_file->sections.line.s.section = sectp;
13052 dwp_file->sections.line.size = bfd_section_size (sectp);
13053 }
13054 else if (section_is_p (sectp->name, &names->loclists_dwo))
13055 {
13056 dwp_file->sections.loclists.s.section = sectp;
13057 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13058 }
13059 else if (section_is_p (sectp->name, &names->macro_dwo))
13060 {
13061 dwp_file->sections.macro.s.section = sectp;
13062 dwp_file->sections.macro.size = bfd_section_size (sectp);
13063 }
13064 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13065 {
13066 dwp_file->sections.rnglists.s.section = sectp;
13067 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13068 }
13069 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13070 {
13071 dwp_file->sections.str_offsets.s.section = sectp;
13072 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13073 }
13074}
13075
80626a55 13076/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13077
80626a55
DE
13078static hashval_t
13079hash_dwp_loaded_cutus (const void *item)
13080{
9a3c8263 13081 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13082
80626a55
DE
13083 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13084 return dwo_unit->signature;
3019eac3
DE
13085}
13086
80626a55 13087/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13088
80626a55
DE
13089static int
13090eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13091{
9a3c8263
SM
13092 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13093 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13094
80626a55
DE
13095 return dua->signature == dub->signature;
13096}
3019eac3 13097
80626a55 13098/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13099
48b490f2 13100static htab_up
298e9637 13101allocate_dwp_loaded_cutus_table ()
80626a55 13102{
48b490f2
TT
13103 return htab_up (htab_create_alloc (3,
13104 hash_dwp_loaded_cutus,
13105 eq_dwp_loaded_cutus,
13106 NULL, xcalloc, xfree));
80626a55 13107}
3019eac3 13108
ab5088bf
DE
13109/* Try to open DWP file FILE_NAME.
13110 The result is the bfd handle of the file.
13111 If there is a problem finding or opening the file, return NULL.
13112 Upon success, the canonicalized path of the file is stored in the bfd,
13113 same as symfile_bfd_open. */
13114
192b62ce 13115static gdb_bfd_ref_ptr
976ca316 13116open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13117{
976ca316 13118 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13119 1 /*is_dwp*/,
192b62ce 13120 1 /*search_cwd*/));
6ac97d4c
DE
13121 if (abfd != NULL)
13122 return abfd;
13123
13124 /* Work around upstream bug 15652.
13125 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13126 [Whether that's a "bug" is debatable, but it is getting in our way.]
13127 We have no real idea where the dwp file is, because gdb's realpath-ing
13128 of the executable's path may have discarded the needed info.
13129 [IWBN if the dwp file name was recorded in the executable, akin to
13130 .gnu_debuglink, but that doesn't exist yet.]
13131 Strip the directory from FILE_NAME and search again. */
13132 if (*debug_file_directory != '\0')
13133 {
13134 /* Don't implicitly search the current directory here.
13135 If the user wants to search "." to handle this case,
13136 it must be added to debug-file-directory. */
976ca316
SM
13137 return try_open_dwop_file (per_objfile, lbasename (file_name),
13138 1 /*is_dwp*/,
6ac97d4c
DE
13139 0 /*search_cwd*/);
13140 }
13141
13142 return NULL;
ab5088bf
DE
13143}
13144
80626a55
DE
13145/* Initialize the use of the DWP file for the current objfile.
13146 By convention the name of the DWP file is ${objfile}.dwp.
13147 The result is NULL if it can't be found. */
a766d390 13148
400174b1 13149static std::unique_ptr<struct dwp_file>
976ca316 13150open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13151{
976ca316 13152 struct objfile *objfile = per_objfile->objfile;
80626a55 13153
82bf32bc
JK
13154 /* Try to find first .dwp for the binary file before any symbolic links
13155 resolving. */
6c447423
DE
13156
13157 /* If the objfile is a debug file, find the name of the real binary
13158 file and get the name of dwp file from there. */
d721ba37 13159 std::string dwp_name;
6c447423
DE
13160 if (objfile->separate_debug_objfile_backlink != NULL)
13161 {
13162 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13163 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13164
d721ba37 13165 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13166 }
13167 else
d721ba37
PA
13168 dwp_name = objfile->original_name;
13169
13170 dwp_name += ".dwp";
80626a55 13171
976ca316 13172 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13173 if (dbfd == NULL
13174 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13175 {
13176 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13177 dwp_name = objfile_name (objfile);
13178 dwp_name += ".dwp";
976ca316 13179 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13180 }
13181
80626a55
DE
13182 if (dbfd == NULL)
13183 {
6f738b01
SM
13184 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13185
400174b1 13186 return std::unique_ptr<dwp_file> ();
3019eac3 13187 }
400174b1
TT
13188
13189 const char *name = bfd_get_filename (dbfd.get ());
13190 std::unique_ptr<struct dwp_file> dwp_file
13191 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13192
0a0f4c01 13193 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13194 dwp_file->elf_sections =
976ca316 13195 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13196 dwp_file->num_sections, asection *);
13197
5bb6e9dd
TT
13198 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13199 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13200 dwp_file.get ());
80626a55 13201
976ca316 13202 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13203
976ca316 13204 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13205
73869dc2 13206 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13207 if (dwp_file->cus && dwp_file->tus
13208 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13209 {
13210 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13211 pretty bizarre. We use pulongest here because that's the established
4d65956b 13212 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13213 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13214 " TU version %s [in DWP file %s]"),
13215 pulongest (dwp_file->cus->version),
d721ba37 13216 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13217 }
08302ed2
DE
13218
13219 if (dwp_file->cus)
13220 dwp_file->version = dwp_file->cus->version;
13221 else if (dwp_file->tus)
13222 dwp_file->version = dwp_file->tus->version;
13223 else
13224 dwp_file->version = 2;
73869dc2 13225
5bb6e9dd
TT
13226 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13227 {
13228 if (dwp_file->version == 2)
13229 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13230 dwp_file.get ());
13231 else
13232 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13233 dwp_file.get ());
13234 }
73869dc2 13235
298e9637
SM
13236 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13237 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13238
6f738b01
SM
13239 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13240 dwarf_read_debug_printf (" %s CUs, %s TUs",
13241 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13242 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13243
13244 return dwp_file;
3019eac3 13245}
c906108c 13246
ab5088bf
DE
13247/* Wrapper around open_and_init_dwp_file, only open it once. */
13248
13249static struct dwp_file *
976ca316 13250get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13251{
976ca316 13252 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13253 {
976ca316
SM
13254 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13255 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13256 }
976ca316 13257 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13258}
13259
80626a55
DE
13260/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13261 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13262 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13263 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13264 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13265
13266 This is called, for example, when wanting to read a variable with a
13267 complex location. Therefore we don't want to do file i/o for every call.
13268 Therefore we don't want to look for a DWO file on every call.
13269 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13270 then we check if we've already seen DWO_NAME, and only THEN do we check
13271 for a DWO file.
13272
1c658ad5 13273 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13274 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13275
3019eac3 13276static struct dwo_unit *
4ab09049 13277lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13278 ULONGEST signature, int is_debug_types)
3019eac3 13279{
976ca316
SM
13280 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13281 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13282 const char *kind = is_debug_types ? "TU" : "CU";
13283 void **dwo_file_slot;
3019eac3 13284 struct dwo_file *dwo_file;
80626a55 13285 struct dwp_file *dwp_file;
cb1df416 13286
6a506a2d
DE
13287 /* First see if there's a DWP file.
13288 If we have a DWP file but didn't find the DWO inside it, don't
13289 look for the original DWO file. It makes gdb behave differently
13290 depending on whether one is debugging in the build tree. */
cf2c3c16 13291
976ca316 13292 dwp_file = get_dwp_file (per_objfile);
80626a55 13293 if (dwp_file != NULL)
cf2c3c16 13294 {
80626a55
DE
13295 const struct dwp_hash_table *dwp_htab =
13296 is_debug_types ? dwp_file->tus : dwp_file->cus;
13297
13298 if (dwp_htab != NULL)
13299 {
13300 struct dwo_unit *dwo_cutu =
976ca316
SM
13301 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13302 is_debug_types);
80626a55
DE
13303
13304 if (dwo_cutu != NULL)
13305 {
6f738b01
SM
13306 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13307 kind, hex_string (signature),
13308 host_address_to_string (dwo_cutu));
13309
80626a55
DE
13310 return dwo_cutu;
13311 }
13312 }
13313 }
6a506a2d 13314 else
80626a55 13315 {
6a506a2d 13316 /* No DWP file, look for the DWO file. */
80626a55 13317
976ca316 13318 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13319 if (*dwo_file_slot == NULL)
80626a55 13320 {
6a506a2d 13321 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13322 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13323 }
6a506a2d 13324 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13325 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13326
6a506a2d 13327 if (dwo_file != NULL)
19c3d4c9 13328 {
6a506a2d
DE
13329 struct dwo_unit *dwo_cutu = NULL;
13330
13331 if (is_debug_types && dwo_file->tus)
13332 {
13333 struct dwo_unit find_dwo_cutu;
13334
13335 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13336 find_dwo_cutu.signature = signature;
9a3c8263 13337 dwo_cutu
b0b6a987
TT
13338 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13339 &find_dwo_cutu);
6a506a2d 13340 }
33c5cd75 13341 else if (!is_debug_types && dwo_file->cus)
80626a55 13342 {
33c5cd75
DB
13343 struct dwo_unit find_dwo_cutu;
13344
13345 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13346 find_dwo_cutu.signature = signature;
b0b6a987 13347 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13348 &find_dwo_cutu);
6a506a2d
DE
13349 }
13350
13351 if (dwo_cutu != NULL)
13352 {
6f738b01
SM
13353 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13354 kind, dwo_name, hex_string (signature),
13355 host_address_to_string (dwo_cutu));
13356
6a506a2d 13357 return dwo_cutu;
80626a55
DE
13358 }
13359 }
2e276125 13360 }
9cdd5dbd 13361
80626a55
DE
13362 /* We didn't find it. This could mean a dwo_id mismatch, or
13363 someone deleted the DWO/DWP file, or the search path isn't set up
13364 correctly to find the file. */
13365
6f738b01
SM
13366 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13367 kind, dwo_name, hex_string (signature));
3019eac3 13368
6656a72d
DE
13369 /* This is a warning and not a complaint because it can be caused by
13370 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13371 {
13372 /* Print the name of the DWP file if we looked there, helps the user
13373 better diagnose the problem. */
791afaa2 13374 std::string dwp_text;
43942612
DE
13375
13376 if (dwp_file != NULL)
791afaa2
TT
13377 dwp_text = string_printf (" [in DWP file %s]",
13378 lbasename (dwp_file->name));
43942612 13379
9d8780f0 13380 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13381 " [in module %s]"),
4ab09049
SM
13382 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13383 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13384 }
3019eac3 13385 return NULL;
5fb290d7
DJ
13386}
13387
80626a55
DE
13388/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13389 See lookup_dwo_cutu_unit for details. */
13390
13391static struct dwo_unit *
4ab09049 13392lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13393 ULONGEST signature)
13394{
4ab09049
SM
13395 gdb_assert (!cu->per_cu->is_debug_types);
13396
13397 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13398}
13399
13400/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13401 See lookup_dwo_cutu_unit for details. */
13402
13403static struct dwo_unit *
4ab09049 13404lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13405{
4ab09049
SM
13406 gdb_assert (cu->per_cu->is_debug_types);
13407
13408 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13409
13410 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13411}
13412
89e63ee4
DE
13413/* Traversal function for queue_and_load_all_dwo_tus. */
13414
13415static int
13416queue_and_load_dwo_tu (void **slot, void *info)
13417{
13418 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13419 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13420 ULONGEST signature = dwo_unit->signature;
d460f660 13421 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13422
13423 if (sig_type != NULL)
13424 {
13425 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13426
13427 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13428 a real dependency of PER_CU on SIG_TYPE. That is detected later
13429 while processing PER_CU. */
120ce1b5 13430 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13431 load_full_type_unit (sig_cu, cu->per_objfile);
13432 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13433 }
13434
13435 return 1;
13436}
13437
1b555f17 13438/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13439 The DWO may have the only definition of the type, though it may not be
13440 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13441 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13442
13443static void
1b555f17 13444queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13445{
13446 struct dwo_unit *dwo_unit;
13447 struct dwo_file *dwo_file;
13448
1b555f17
SM
13449 gdb_assert (cu != nullptr);
13450 gdb_assert (!cu->per_cu->is_debug_types);
13451 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13452
1b555f17 13453 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13454 gdb_assert (dwo_unit != NULL);
13455
13456 dwo_file = dwo_unit->dwo_file;
13457 if (dwo_file->tus != NULL)
1b555f17 13458 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13459}
13460
3019eac3 13461/* Read in various DIEs. */
348e048f 13462
d389af10 13463/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13464 Inherit only the children of the DW_AT_abstract_origin DIE not being
13465 already referenced by DW_AT_abstract_origin from the children of the
13466 current DIE. */
d389af10
JK
13467
13468static void
13469inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13470{
13471 struct die_info *child_die;
791afaa2 13472 sect_offset *offsetp;
d389af10
JK
13473 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13474 struct die_info *origin_die;
13475 /* Iterator of the ORIGIN_DIE children. */
13476 struct die_info *origin_child_die;
d389af10 13477 struct attribute *attr;
cd02d79d
PA
13478 struct dwarf2_cu *origin_cu;
13479 struct pending **origin_previous_list_in_scope;
d389af10
JK
13480
13481 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13482 if (!attr)
13483 return;
13484
cd02d79d
PA
13485 /* Note that following die references may follow to a die in a
13486 different cu. */
13487
13488 origin_cu = cu;
13489 origin_die = follow_die_ref (die, attr, &origin_cu);
13490
13491 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13492 symbols in. */
13493 origin_previous_list_in_scope = origin_cu->list_in_scope;
13494 origin_cu->list_in_scope = cu->list_in_scope;
13495
edb3359d
DJ
13496 if (die->tag != origin_die->tag
13497 && !(die->tag == DW_TAG_inlined_subroutine
13498 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13499 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13500 sect_offset_str (die->sect_off),
13501 sect_offset_str (origin_die->sect_off));
d389af10 13502
791afaa2 13503 std::vector<sect_offset> offsets;
d389af10 13504
3ea89b92
PMR
13505 for (child_die = die->child;
13506 child_die && child_die->tag;
436c571c 13507 child_die = child_die->sibling)
3ea89b92
PMR
13508 {
13509 struct die_info *child_origin_die;
13510 struct dwarf2_cu *child_origin_cu;
13511
13512 /* We are trying to process concrete instance entries:
216f72a1 13513 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13514 it's not relevant to our analysis here. i.e. detecting DIEs that are
13515 present in the abstract instance but not referenced in the concrete
13516 one. */
216f72a1 13517 if (child_die->tag == DW_TAG_call_site
dda83cd7 13518 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13519 continue;
13520
c38f313d
DJ
13521 /* For each CHILD_DIE, find the corresponding child of
13522 ORIGIN_DIE. If there is more than one layer of
13523 DW_AT_abstract_origin, follow them all; there shouldn't be,
13524 but GCC versions at least through 4.4 generate this (GCC PR
13525 40573). */
3ea89b92
PMR
13526 child_origin_die = child_die;
13527 child_origin_cu = cu;
c38f313d
DJ
13528 while (1)
13529 {
cd02d79d
PA
13530 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13531 child_origin_cu);
c38f313d
DJ
13532 if (attr == NULL)
13533 break;
cd02d79d
PA
13534 child_origin_die = follow_die_ref (child_origin_die, attr,
13535 &child_origin_cu);
c38f313d
DJ
13536 }
13537
d389af10
JK
13538 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13539 counterpart may exist. */
c38f313d 13540 if (child_origin_die != child_die)
d389af10 13541 {
edb3359d
DJ
13542 if (child_die->tag != child_origin_die->tag
13543 && !(child_die->tag == DW_TAG_inlined_subroutine
13544 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13545 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13546 "different tags"),
9d8780f0
SM
13547 sect_offset_str (child_die->sect_off),
13548 sect_offset_str (child_origin_die->sect_off));
c38f313d 13549 if (child_origin_die->parent != origin_die)
b98664d3 13550 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13551 "different parents"),
9d8780f0
SM
13552 sect_offset_str (child_die->sect_off),
13553 sect_offset_str (child_origin_die->sect_off));
c38f313d 13554 else
791afaa2 13555 offsets.push_back (child_origin_die->sect_off);
d389af10 13556 }
d389af10 13557 }
791afaa2
TT
13558 std::sort (offsets.begin (), offsets.end ());
13559 sect_offset *offsets_end = offsets.data () + offsets.size ();
13560 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13561 if (offsetp[-1] == *offsetp)
b98664d3 13562 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13563 "to DIE %s as their abstract origin"),
13564 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13565
791afaa2 13566 offsetp = offsets.data ();
d389af10
JK
13567 origin_child_die = origin_die->child;
13568 while (origin_child_die && origin_child_die->tag)
13569 {
13570 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13571 while (offsetp < offsets_end
9c541725 13572 && *offsetp < origin_child_die->sect_off)
d389af10 13573 offsetp++;
b64f50a1 13574 if (offsetp >= offsets_end
9c541725 13575 || *offsetp > origin_child_die->sect_off)
d389af10 13576 {
adde2bff
DE
13577 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13578 Check whether we're already processing ORIGIN_CHILD_DIE.
13579 This can happen with mutually referenced abstract_origins.
13580 PR 16581. */
13581 if (!origin_child_die->in_process)
13582 process_die (origin_child_die, origin_cu);
d389af10 13583 }
436c571c 13584 origin_child_die = origin_child_die->sibling;
d389af10 13585 }
cd02d79d 13586 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13587
13588 if (cu != origin_cu)
13589 compute_delayed_physnames (origin_cu);
d389af10
JK
13590}
13591
c906108c 13592static void
e7c27a73 13593read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13594{
5e22e966 13595 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13596 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13597 struct context_stack *newobj;
c906108c
SS
13598 CORE_ADDR lowpc;
13599 CORE_ADDR highpc;
13600 struct die_info *child_die;
edb3359d 13601 struct attribute *attr, *call_line, *call_file;
15d034d0 13602 const char *name;
e142c38c 13603 CORE_ADDR baseaddr;
801e3a5b 13604 struct block *block;
edb3359d 13605 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13606 std::vector<struct symbol *> template_args;
34eaf542 13607 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13608
13609 if (inlined_func)
13610 {
13611 /* If we do not have call site information, we can't show the
13612 caller of this inlined function. That's too confusing, so
13613 only use the scope for local variables. */
13614 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13615 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13616 if (call_line == NULL || call_file == NULL)
13617 {
13618 read_lexical_block_scope (die, cu);
13619 return;
13620 }
13621 }
c906108c 13622
b3b3bada 13623 baseaddr = objfile->text_section_offset ();
e142c38c 13624
94af9270 13625 name = dwarf2_name (die, cu);
c906108c 13626
e8d05480
JB
13627 /* Ignore functions with missing or empty names. These are actually
13628 illegal according to the DWARF standard. */
13629 if (name == NULL)
13630 {
b98664d3 13631 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13632 sect_offset_str (die->sect_off));
e8d05480
JB
13633 return;
13634 }
13635
13636 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13637 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13638 <= PC_BOUNDS_INVALID)
e8d05480 13639 {
ae4d0c03 13640 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13641 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13642 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13643 "for subprogram DIE at %s"),
13644 sect_offset_str (die->sect_off));
e8d05480
JB
13645 return;
13646 }
c906108c 13647
3e29f34a
MR
13648 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13649 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13650
34eaf542
TT
13651 /* If we have any template arguments, then we must allocate a
13652 different sort of symbol. */
436c571c 13653 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13654 {
13655 if (child_die->tag == DW_TAG_template_type_param
13656 || child_die->tag == DW_TAG_template_value_param)
13657 {
8c14c3a3 13658 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13659 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13660 break;
13661 }
13662 }
13663
c24bdb02 13664 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13665 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13666 (struct symbol *) templ_func);
4c2df51b 13667
81873cc8 13668 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13669 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13670 cu->language);
13671
4cecd739
DJ
13672 /* If there is a location expression for DW_AT_frame_base, record
13673 it. */
e142c38c 13674 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13675 if (attr != nullptr)
fe978cb0 13676 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13677
63e43d3a
PMR
13678 /* If there is a location for the static link, record it. */
13679 newobj->static_link = NULL;
13680 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13681 if (attr != nullptr)
63e43d3a 13682 {
224c3ddb
SM
13683 newobj->static_link
13684 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13685 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13686 cu->addr_type ());
63e43d3a
PMR
13687 }
13688
c24bdb02 13689 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13690
639d11d3 13691 if (die->child != NULL)
c906108c 13692 {
639d11d3 13693 child_die = die->child;
c906108c
SS
13694 while (child_die && child_die->tag)
13695 {
34eaf542
TT
13696 if (child_die->tag == DW_TAG_template_type_param
13697 || child_die->tag == DW_TAG_template_value_param)
13698 {
13699 struct symbol *arg = new_symbol (child_die, NULL, cu);
13700
f1078f66 13701 if (arg != NULL)
2f4732b0 13702 template_args.push_back (arg);
34eaf542
TT
13703 }
13704 else
13705 process_die (child_die, cu);
436c571c 13706 child_die = child_die->sibling;
c906108c
SS
13707 }
13708 }
13709
d389af10
JK
13710 inherit_abstract_dies (die, cu);
13711
4a811a97
UW
13712 /* If we have a DW_AT_specification, we might need to import using
13713 directives from the context of the specification DIE. See the
13714 comment in determine_prefix. */
13715 if (cu->language == language_cplus
13716 && dwarf2_attr (die, DW_AT_specification, cu))
13717 {
13718 struct dwarf2_cu *spec_cu = cu;
13719 struct die_info *spec_die = die_specification (die, &spec_cu);
13720
13721 while (spec_die)
13722 {
13723 child_die = spec_die->child;
13724 while (child_die && child_die->tag)
13725 {
13726 if (child_die->tag == DW_TAG_imported_module)
13727 process_die (child_die, spec_cu);
436c571c 13728 child_die = child_die->sibling;
4a811a97
UW
13729 }
13730
13731 /* In some cases, GCC generates specification DIEs that
13732 themselves contain DW_AT_specification attributes. */
13733 spec_die = die_specification (spec_die, &spec_cu);
13734 }
13735 }
13736
c24bdb02 13737 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13738 /* Make a block for the local symbols within. */
c24bdb02 13739 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13740 cstk.static_link, lowpc, highpc);
801e3a5b 13741
df8a16a1 13742 /* For C++, set the block's scope. */
45280282
IB
13743 if ((cu->language == language_cplus
13744 || cu->language == language_fortran
c44af4eb
TT
13745 || cu->language == language_d
13746 || cu->language == language_rust)
4d4ec4e5 13747 && cu->processing_has_namespace_info)
195a3f6c
TT
13748 block_set_scope (block, determine_prefix (die, cu),
13749 &objfile->objfile_obstack);
df8a16a1 13750
801e3a5b
JB
13751 /* If we have address ranges, record them. */
13752 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13753
a60f3166 13754 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13755
34eaf542 13756 /* Attach template arguments to function. */
2f4732b0 13757 if (!template_args.empty ())
34eaf542
TT
13758 {
13759 gdb_assert (templ_func != NULL);
13760
2f4732b0 13761 templ_func->n_template_arguments = template_args.size ();
34eaf542 13762 templ_func->template_arguments
dda83cd7 13763 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13764 templ_func->n_template_arguments);
34eaf542 13765 memcpy (templ_func->template_arguments,
2f4732b0 13766 template_args.data (),
34eaf542 13767 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13768
13769 /* Make sure that the symtab is set on the new symbols. Even
13770 though they don't appear in this symtab directly, other parts
13771 of gdb assume that symbols do, and this is reasonably
13772 true. */
8634679f 13773 for (symbol *sym : template_args)
3e1d3d8c 13774 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13775 }
13776
208d8187
JB
13777 /* In C++, we can have functions nested inside functions (e.g., when
13778 a function declares a class that has methods). This means that
13779 when we finish processing a function scope, we may need to go
13780 back to building a containing block's symbol lists. */
c24bdb02
KS
13781 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13782 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13783
921e78cf
JB
13784 /* If we've finished processing a top-level function, subsequent
13785 symbols go in the file symbol list. */
c24bdb02
KS
13786 if (cu->get_builder ()->outermost_context_p ())
13787 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13788}
13789
13790/* Process all the DIES contained within a lexical block scope. Start
13791 a new scope, process the dies, and then close the scope. */
13792
13793static void
e7c27a73 13794read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13795{
5e22e966 13796 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13797 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13798 CORE_ADDR lowpc, highpc;
13799 struct die_info *child_die;
e142c38c
DJ
13800 CORE_ADDR baseaddr;
13801
b3b3bada 13802 baseaddr = objfile->text_section_offset ();
c906108c
SS
13803
13804 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13805 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13806 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13807 be nasty. Might be easier to properly extend generic blocks to
af34e669 13808 describe ranges. */
e385593e
JK
13809 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13810 {
13811 case PC_BOUNDS_NOT_PRESENT:
13812 /* DW_TAG_lexical_block has no attributes, process its children as if
13813 there was no wrapping by that DW_TAG_lexical_block.
13814 GCC does no longer produces such DWARF since GCC r224161. */
13815 for (child_die = die->child;
13816 child_die != NULL && child_die->tag;
436c571c 13817 child_die = child_die->sibling)
4f7bc5ed
TT
13818 {
13819 /* We might already be processing this DIE. This can happen
13820 in an unusual circumstance -- where a subroutine A
13821 appears lexically in another subroutine B, but A actually
13822 inlines B. The recursion is broken here, rather than in
13823 inherit_abstract_dies, because it seems better to simply
13824 drop concrete children here. */
13825 if (!child_die->in_process)
13826 process_die (child_die, cu);
13827 }
e385593e
JK
13828 return;
13829 case PC_BOUNDS_INVALID:
13830 return;
13831 }
3e29f34a
MR
13832 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13833 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13834
c24bdb02 13835 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13836 if (die->child != NULL)
c906108c 13837 {
639d11d3 13838 child_die = die->child;
c906108c
SS
13839 while (child_die && child_die->tag)
13840 {
e7c27a73 13841 process_die (child_die, cu);
436c571c 13842 child_die = child_die->sibling;
c906108c
SS
13843 }
13844 }
3ea89b92 13845 inherit_abstract_dies (die, cu);
c24bdb02 13846 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13847
c24bdb02
KS
13848 if (*cu->get_builder ()->get_local_symbols () != NULL
13849 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13850 {
801e3a5b 13851 struct block *block
dda83cd7 13852 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13853 cstk.start_addr, highpc);
801e3a5b
JB
13854
13855 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13856 do here, means that recording a parent's ranges entails
13857 walking across all its children's ranges as they appear in
13858 the address map, which is quadratic behavior.
13859
13860 It would be nicer to record the parent's ranges before
13861 traversing its children, simply overriding whatever you find
13862 there. But since we don't even decide whether to create a
13863 block until after we've traversed its children, that's hard
13864 to do. */
801e3a5b 13865 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13866 }
c24bdb02
KS
13867 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13868 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13869}
13870
216f72a1 13871/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13872
13873static void
13874read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13875{
5e22e966 13876 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13877 struct objfile *objfile = per_objfile->objfile;
08feed99 13878 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13879 CORE_ADDR pc, baseaddr;
13880 struct attribute *attr;
13881 struct call_site *call_site, call_site_local;
13882 void **slot;
13883 int nparams;
13884 struct die_info *child_die;
13885
b3b3bada 13886 baseaddr = objfile->text_section_offset ();
96408a79 13887
216f72a1
JK
13888 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13889 if (attr == NULL)
13890 {
13891 /* This was a pre-DWARF-5 GNU extension alias
13892 for DW_AT_call_return_pc. */
13893 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13894 }
96408a79
SA
13895 if (!attr)
13896 {
b98664d3 13897 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13898 "DIE %s [in module %s]"),
13899 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13900 return;
13901 }
95f982e5 13902 pc = attr->as_address () + baseaddr;
3e29f34a 13903 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13904
13905 if (cu->call_site_htab == NULL)
13906 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13907 NULL, &objfile->objfile_obstack,
13908 hashtab_obstack_allocate, NULL);
13909 call_site_local.pc = pc;
13910 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13911 if (*slot != NULL)
13912 {
b98664d3 13913 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13914 "DIE %s [in module %s]"),
13915 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13916 objfile_name (objfile));
96408a79
SA
13917 return;
13918 }
13919
13920 /* Count parameters at the caller. */
13921
13922 nparams = 0;
13923 for (child_die = die->child; child_die && child_die->tag;
436c571c 13924 child_die = child_die->sibling)
96408a79 13925 {
216f72a1 13926 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13927 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13928 {
b98664d3 13929 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13930 "DW_TAG_call_site child DIE %s [in module %s]"),
13931 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13932 objfile_name (objfile));
96408a79
SA
13933 continue;
13934 }
13935
13936 nparams++;
13937 }
13938
224c3ddb
SM
13939 call_site
13940 = ((struct call_site *)
13941 obstack_alloc (&objfile->objfile_obstack,
13942 sizeof (*call_site)
13943 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13944 *slot = call_site;
13945 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13946 call_site->pc = pc;
13947
216f72a1
JK
13948 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13949 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13950 {
13951 struct die_info *func_die;
13952
13953 /* Skip also over DW_TAG_inlined_subroutine. */
13954 for (func_die = die->parent;
13955 func_die && func_die->tag != DW_TAG_subprogram
13956 && func_die->tag != DW_TAG_subroutine_type;
13957 func_die = func_die->parent);
13958
216f72a1
JK
13959 /* DW_AT_call_all_calls is a superset
13960 of DW_AT_call_all_tail_calls. */
96408a79 13961 if (func_die
dda83cd7
SM
13962 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13963 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13964 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13965 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13966 {
13967 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13968 not complete. But keep CALL_SITE for look ups via call_site_htab,
13969 both the initial caller containing the real return address PC and
13970 the final callee containing the current PC of a chain of tail
13971 calls do not need to have the tail call list complete. But any
13972 function candidate for a virtual tail call frame searched via
13973 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13974 determined unambiguously. */
13975 }
13976 else
13977 {
13978 struct type *func_type = NULL;
13979
13980 if (func_die)
13981 func_type = get_die_type (func_die, cu);
13982 if (func_type != NULL)
13983 {
78134374 13984 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13985
13986 /* Enlist this call site to the function. */
13987 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13988 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13989 }
13990 else
b98664d3 13991 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13992 "DIE %s [in module %s]"),
13993 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13994 }
13995 }
13996
216f72a1
JK
13997 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13998 if (attr == NULL)
13999 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
14000 if (attr == NULL)
14001 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14002 if (attr == NULL)
216f72a1
JK
14003 {
14004 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14005 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14006 }
96408a79 14007 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 14008 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 14009 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 14010 else if (attr->form_is_block ())
96408a79
SA
14011 {
14012 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 14013 struct dwarf_block *block = attr->as_block ();
96408a79 14014
8d749320 14015 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
14016 dlbaton->data = block->data;
14017 dlbaton->size = block->size;
a50264ba 14018 dlbaton->per_objfile = per_objfile;
96408a79
SA
14019 dlbaton->per_cu = cu->per_cu;
14020
14021 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14022 }
cd6c91b4 14023 else if (attr->form_is_ref ())
96408a79 14024 {
96408a79
SA
14025 struct dwarf2_cu *target_cu = cu;
14026 struct die_info *target_die;
14027
ac9ec31b 14028 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 14029 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
14030 if (die_is_declaration (target_die, target_cu))
14031 {
7d45c7c3 14032 const char *target_physname;
9112db09
JK
14033
14034 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14035 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14036 if (target_physname == NULL)
9112db09 14037 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14038 if (target_physname == NULL)
b98664d3 14039 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14040 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 14041 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14042 else
7d455152 14043 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14044 }
14045 else
14046 {
14047 CORE_ADDR lowpc;
14048
14049 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14050 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14051 <= PC_BOUNDS_INVALID)
b98664d3 14052 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14053 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 14054 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14055 else
3e29f34a
MR
14056 {
14057 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14058 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14059 }
96408a79
SA
14060 }
14061 }
14062 else
b98664d3 14063 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14064 "block nor reference, for DIE %s [in module %s]"),
14065 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14066
14067 call_site->per_cu = cu->per_cu;
9f47c707 14068 call_site->per_objfile = per_objfile;
96408a79
SA
14069
14070 for (child_die = die->child;
14071 child_die && child_die->tag;
436c571c 14072 child_die = child_die->sibling)
96408a79 14073 {
96408a79 14074 struct call_site_parameter *parameter;
1788b2d3 14075 struct attribute *loc, *origin;
96408a79 14076
216f72a1 14077 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14078 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14079 {
14080 /* Already printed the complaint above. */
14081 continue;
14082 }
14083
14084 gdb_assert (call_site->parameter_count < nparams);
14085 parameter = &call_site->parameter[call_site->parameter_count];
14086
1788b2d3
JK
14087 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14088 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14089 register is contained in DW_AT_call_value. */
96408a79 14090
24c5c679 14091 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14092 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14093 if (origin == NULL)
14094 {
14095 /* This was a pre-DWARF-5 GNU extension alias
14096 for DW_AT_call_parameter. */
14097 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14098 }
cd6c91b4 14099 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14100 {
1788b2d3 14101 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14102
0826b30a 14103 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14104 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14105 {
14106 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14107 binding can be done only inside one CU. Such referenced DIE
14108 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14109 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14110 "DW_TAG_call_site child DIE %s [in module %s]"),
14111 sect_offset_str (child_die->sect_off),
9c541725 14112 objfile_name (objfile));
d76b7dbc
JK
14113 continue;
14114 }
9c541725
PA
14115 parameter->u.param_cu_off
14116 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14117 }
4fc6c0d5 14118 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14119 {
b98664d3 14120 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14121 "DW_TAG_call_site child DIE %s [in module %s]"),
14122 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14123 continue;
14124 }
24c5c679 14125 else
96408a79 14126 {
9d2246fc
TT
14127 struct dwarf_block *block = loc->as_block ();
14128
24c5c679 14129 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14130 (block->data, &block->data[block->size]);
24c5c679
JK
14131 if (parameter->u.dwarf_reg != -1)
14132 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14133 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14134 &block->data[block->size],
24c5c679
JK
14135 &parameter->u.fb_offset))
14136 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14137 else
14138 {
b98664d3 14139 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14140 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14141 "DW_TAG_call_site child DIE %s "
24c5c679 14142 "[in module %s]"),
9d8780f0 14143 sect_offset_str (child_die->sect_off),
9c541725 14144 objfile_name (objfile));
24c5c679
JK
14145 continue;
14146 }
96408a79
SA
14147 }
14148
216f72a1
JK
14149 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14150 if (attr == NULL)
14151 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14152 if (attr == NULL || !attr->form_is_block ())
96408a79 14153 {
b98664d3 14154 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14155 "DW_TAG_call_site child DIE %s [in module %s]"),
14156 sect_offset_str (child_die->sect_off),
9c541725 14157 objfile_name (objfile));
96408a79
SA
14158 continue;
14159 }
9d2246fc
TT
14160
14161 struct dwarf_block *block = attr->as_block ();
14162 parameter->value = block->data;
14163 parameter->value_size = block->size;
96408a79
SA
14164
14165 /* Parameters are not pre-cleared by memset above. */
14166 parameter->data_value = NULL;
14167 parameter->data_value_size = 0;
14168 call_site->parameter_count++;
14169
216f72a1
JK
14170 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14171 if (attr == NULL)
14172 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14173 if (attr != nullptr)
96408a79 14174 {
4fc6c0d5 14175 if (!attr->form_is_block ())
b98664d3 14176 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14177 "DW_TAG_call_site child DIE %s [in module %s]"),
14178 sect_offset_str (child_die->sect_off),
9c541725 14179 objfile_name (objfile));
96408a79
SA
14180 else
14181 {
9d2246fc
TT
14182 block = attr->as_block ();
14183 parameter->data_value = block->data;
14184 parameter->data_value_size = block->size;
96408a79
SA
14185 }
14186 }
14187 }
14188}
14189
71a3c369
TT
14190/* Helper function for read_variable. If DIE represents a virtual
14191 table, then return the type of the concrete object that is
14192 associated with the virtual table. Otherwise, return NULL. */
14193
14194static struct type *
14195rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14196{
14197 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14198 if (attr == NULL)
14199 return NULL;
14200
14201 /* Find the type DIE. */
14202 struct die_info *type_die = NULL;
14203 struct dwarf2_cu *type_cu = cu;
14204
cd6c91b4 14205 if (attr->form_is_ref ())
71a3c369
TT
14206 type_die = follow_die_ref (die, attr, &type_cu);
14207 if (type_die == NULL)
14208 return NULL;
14209
14210 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14211 return NULL;
14212 return die_containing_type (type_die, type_cu);
14213}
14214
14215/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14216
14217static void
14218read_variable (struct die_info *die, struct dwarf2_cu *cu)
14219{
14220 struct rust_vtable_symbol *storage = NULL;
14221
14222 if (cu->language == language_rust)
14223 {
14224 struct type *containing_type = rust_containing_type (die, cu);
14225
14226 if (containing_type != NULL)
14227 {
5e22e966 14228 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14229
8c14c3a3 14230 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14231 storage->concrete_type = containing_type;
cf724bc9 14232 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14233 }
14234 }
14235
e4a62c65
TV
14236 struct symbol *res = new_symbol (die, NULL, cu, storage);
14237 struct attribute *abstract_origin
14238 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14239 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14240 if (res == NULL && loc && abstract_origin)
14241 {
14242 /* We have a variable without a name, but with a location and an abstract
14243 origin. This may be a concrete instance of an abstract variable
14244 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14245 later. */
14246 struct dwarf2_cu *origin_cu = cu;
14247 struct die_info *origin_die
14248 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14249 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14250 per_objfile->per_bfd->abstract_to_concrete
14251 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14252 }
71a3c369
TT
14253}
14254
43988095
JK
14255/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14256 reading .debug_rnglists.
14257 Callback's type should be:
14258 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14259 Return true if the attributes are present and valid, otherwise,
14260 return false. */
14261
14262template <typename Callback>
14263static bool
14264dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14265 dwarf_tag tag, Callback &&callback)
43988095 14266{
976ca316
SM
14267 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14268 struct objfile *objfile = per_objfile->objfile;
43988095 14269 bfd *obfd = objfile->obfd;
43988095 14270 /* Base address selection entry. */
2b24b6e4 14271 gdb::optional<CORE_ADDR> base;
43988095 14272 const gdb_byte *buffer;
43988095
JK
14273 CORE_ADDR baseaddr;
14274 bool overflow = false;
d0ce17d8
CT
14275 ULONGEST addr_index;
14276 struct dwarf2_section_info *rnglists_section;
43988095 14277
43988095 14278 base = cu->base_address;
d0ce17d8
CT
14279 rnglists_section = cu_debug_rnglists_section (cu, tag);
14280 rnglists_section->read (objfile);
43988095 14281
d0ce17d8 14282 if (offset >= rnglists_section->size)
43988095 14283 {
b98664d3 14284 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14285 offset);
14286 return false;
14287 }
d0ce17d8 14288 buffer = rnglists_section->buffer + offset;
43988095 14289
b3b3bada 14290 baseaddr = objfile->text_section_offset ();
43988095
JK
14291
14292 while (1)
14293 {
7814882a
JK
14294 /* Initialize it due to a false compiler warning. */
14295 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14296 const gdb_byte *buf_end = (rnglists_section->buffer
14297 + rnglists_section->size);
43988095
JK
14298 unsigned int bytes_read;
14299
14300 if (buffer == buf_end)
14301 {
14302 overflow = true;
14303 break;
14304 }
14305 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14306 switch (rlet)
14307 {
14308 case DW_RLE_end_of_list:
14309 break;
14310 case DW_RLE_base_address:
14311 if (buffer + cu->header.addr_size > buf_end)
14312 {
14313 overflow = true;
14314 break;
14315 }
c8a7a66f 14316 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14317 buffer += bytes_read;
14318 break;
dda83cd7
SM
14319 case DW_RLE_base_addressx:
14320 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14321 buffer += bytes_read;
14322 base = read_addr_index (cu, addr_index);
14323 break;
43988095
JK
14324 case DW_RLE_start_length:
14325 if (buffer + cu->header.addr_size > buf_end)
14326 {
14327 overflow = true;
14328 break;
14329 }
c8a7a66f
TT
14330 range_beginning = cu->header.read_address (obfd, buffer,
14331 &bytes_read);
43988095
JK
14332 buffer += bytes_read;
14333 range_end = (range_beginning
14334 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14335 buffer += bytes_read;
14336 if (buffer > buf_end)
14337 {
14338 overflow = true;
14339 break;
14340 }
14341 break;
d0ce17d8 14342 case DW_RLE_startx_length:
dda83cd7
SM
14343 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14344 buffer += bytes_read;
14345 range_beginning = read_addr_index (cu, addr_index);
14346 if (buffer > buf_end)
14347 {
14348 overflow = true;
14349 break;
14350 }
14351 range_end = (range_beginning
14352 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14353 buffer += bytes_read;
14354 break;
43988095
JK
14355 case DW_RLE_offset_pair:
14356 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14357 buffer += bytes_read;
14358 if (buffer > buf_end)
14359 {
14360 overflow = true;
14361 break;
14362 }
14363 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14364 buffer += bytes_read;
14365 if (buffer > buf_end)
14366 {
14367 overflow = true;
14368 break;
14369 }
14370 break;
14371 case DW_RLE_start_end:
14372 if (buffer + 2 * cu->header.addr_size > buf_end)
14373 {
14374 overflow = true;
14375 break;
14376 }
c8a7a66f
TT
14377 range_beginning = cu->header.read_address (obfd, buffer,
14378 &bytes_read);
43988095 14379 buffer += bytes_read;
c8a7a66f 14380 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14381 buffer += bytes_read;
14382 break;
d0ce17d8 14383 case DW_RLE_startx_endx:
dda83cd7
SM
14384 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14385 buffer += bytes_read;
14386 range_beginning = read_addr_index (cu, addr_index);
14387 if (buffer > buf_end)
14388 {
14389 overflow = true;
14390 break;
14391 }
14392 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14393 buffer += bytes_read;
14394 range_end = read_addr_index (cu, addr_index);
14395 break;
43988095 14396 default:
b98664d3 14397 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14398 return false;
14399 }
14400 if (rlet == DW_RLE_end_of_list || overflow)
14401 break;
14402 if (rlet == DW_RLE_base_address)
14403 continue;
14404
43988095
JK
14405 if (range_beginning > range_end)
14406 {
14407 /* Inverted range entries are invalid. */
b98664d3 14408 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14409 return false;
14410 }
14411
14412 /* Empty range entries have no effect. */
14413 if (range_beginning == range_end)
14414 continue;
14415
d0ce17d8
CT
14416 /* Only DW_RLE_offset_pair needs the base address added. */
14417 if (rlet == DW_RLE_offset_pair)
14418 {
14419 if (!base.has_value ())
14420 {
14421 /* We have no valid base address for the DW_RLE_offset_pair. */
14422 complaint (_("Invalid .debug_rnglists data (no base address for "
14423 "DW_RLE_offset_pair)"));
14424 return false;
14425 }
14426
14427 range_beginning += *base;
14428 range_end += *base;
14429 }
43988095
JK
14430
14431 /* A not-uncommon case of bad debug info.
14432 Don't pollute the addrmap with bad data. */
14433 if (range_beginning + baseaddr == 0
976ca316 14434 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14435 {
b98664d3 14436 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14437 " [in module %s]"), objfile_name (objfile));
14438 continue;
14439 }
14440
14441 callback (range_beginning, range_end);
14442 }
14443
14444 if (overflow)
14445 {
b98664d3 14446 complaint (_("Offset %d is not terminated "
43988095
JK
14447 "for DW_AT_ranges attribute"),
14448 offset);
14449 return false;
14450 }
14451
14452 return true;
14453}
14454
14455/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14456 Callback's type should be:
14457 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14458 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14459
43988095 14460template <typename Callback>
43039443 14461static int
d0ce17d8 14462dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14463 Callback &&callback)
43039443 14464{
5e22e966
SM
14465 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14466 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14467 struct comp_unit_head *cu_header = &cu->header;
14468 bfd *obfd = objfile->obfd;
14469 unsigned int addr_size = cu_header->addr_size;
14470 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14471 /* Base address selection entry. */
2b24b6e4 14472 gdb::optional<CORE_ADDR> base;
43039443 14473 unsigned int dummy;
d521ce57 14474 const gdb_byte *buffer;
ff013f42 14475 CORE_ADDR baseaddr;
43039443 14476
43988095 14477 if (cu_header->version >= 5)
d0ce17d8 14478 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14479
d00adf39 14480 base = cu->base_address;
43039443 14481
5e22e966
SM
14482 per_objfile->per_bfd->ranges.read (objfile);
14483 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14484 {
b98664d3 14485 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14486 offset);
14487 return 0;
14488 }
5e22e966 14489 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14490
b3b3bada 14491 baseaddr = objfile->text_section_offset ();
ff013f42 14492
43039443
JK
14493 while (1)
14494 {
14495 CORE_ADDR range_beginning, range_end;
14496
c8a7a66f 14497 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14498 buffer += addr_size;
c8a7a66f 14499 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14500 buffer += addr_size;
14501 offset += 2 * addr_size;
14502
14503 /* An end of list marker is a pair of zero addresses. */
14504 if (range_beginning == 0 && range_end == 0)
14505 /* Found the end of list entry. */
14506 break;
14507
14508 /* Each base address selection entry is a pair of 2 values.
14509 The first is the largest possible address, the second is
14510 the base address. Check for a base address here. */
14511 if ((range_beginning & mask) == mask)
14512 {
28d2bfb9
AB
14513 /* If we found the largest possible address, then we already
14514 have the base address in range_end. */
14515 base = range_end;
43039443
JK
14516 continue;
14517 }
14518
2b24b6e4 14519 if (!base.has_value ())
43039443
JK
14520 {
14521 /* We have no valid base address for the ranges
14522 data. */
b98664d3 14523 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14524 return 0;
14525 }
14526
9277c30c
UW
14527 if (range_beginning > range_end)
14528 {
14529 /* Inverted range entries are invalid. */
b98664d3 14530 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14531 return 0;
14532 }
14533
14534 /* Empty range entries have no effect. */
14535 if (range_beginning == range_end)
14536 continue;
14537
2b24b6e4
TT
14538 range_beginning += *base;
14539 range_end += *base;
43039443 14540
01093045
DE
14541 /* A not-uncommon case of bad debug info.
14542 Don't pollute the addrmap with bad data. */
14543 if (range_beginning + baseaddr == 0
5e22e966 14544 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14545 {
b98664d3 14546 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14547 " [in module %s]"), objfile_name (objfile));
01093045
DE
14548 continue;
14549 }
14550
5f46c5a5
JK
14551 callback (range_beginning, range_end);
14552 }
14553
14554 return 1;
14555}
14556
14557/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14558 Return 1 if the attributes are present and valid, otherwise, return 0.
14559 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14560
14561static int
14562dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14563 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14564 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14565{
5e22e966 14566 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14567 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14568 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14569 int low_set = 0;
14570 CORE_ADDR low = 0;
14571 CORE_ADDR high = 0;
14572 int retval;
14573
d0ce17d8 14574 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14575 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14576 {
9277c30c 14577 if (ranges_pst != NULL)
3e29f34a
MR
14578 {
14579 CORE_ADDR lowpc;
14580 CORE_ADDR highpc;
14581
79748972
TT
14582 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14583 range_beginning + baseaddr)
14584 - baseaddr);
14585 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14586 range_end + baseaddr)
14587 - baseaddr);
d320c2b5
TT
14588 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14589 lowpc, highpc - 1, ranges_pst);
3e29f34a 14590 }
ff013f42 14591
43039443
JK
14592 /* FIXME: This is recording everything as a low-high
14593 segment of consecutive addresses. We should have a
14594 data structure for discontiguous block ranges
14595 instead. */
14596 if (! low_set)
14597 {
14598 low = range_beginning;
14599 high = range_end;
14600 low_set = 1;
14601 }
14602 else
14603 {
14604 if (range_beginning < low)
14605 low = range_beginning;
14606 if (range_end > high)
14607 high = range_end;
14608 }
5f46c5a5
JK
14609 });
14610 if (!retval)
14611 return 0;
43039443
JK
14612
14613 if (! low_set)
14614 /* If the first entry is an end-of-list marker, the range
14615 describes an empty scope, i.e. no instructions. */
14616 return 0;
14617
14618 if (low_return)
14619 *low_return = low;
14620 if (high_return)
14621 *high_return = high;
14622 return 1;
14623}
14624
3a2b436a
JK
14625/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14626 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14627 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14628
3a2b436a 14629static enum pc_bounds_kind
af34e669 14630dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14631 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14632 dwarf2_psymtab *pst)
c906108c 14633{
976ca316 14634 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14635 struct attribute *attr;
91da1414 14636 struct attribute *attr_high;
af34e669
DJ
14637 CORE_ADDR low = 0;
14638 CORE_ADDR high = 0;
e385593e 14639 enum pc_bounds_kind ret;
c906108c 14640
91da1414
MW
14641 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14642 if (attr_high)
af34e669 14643 {
e142c38c 14644 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14645 if (attr != nullptr)
dda83cd7 14646 {
95f982e5
TT
14647 low = attr->as_address ();
14648 high = attr_high->as_address ();
cd6c91b4 14649 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14650 high += low;
91da1414 14651 }
af34e669
DJ
14652 else
14653 /* Found high w/o low attribute. */
e385593e 14654 return PC_BOUNDS_INVALID;
af34e669
DJ
14655
14656 /* Found consecutive range of addresses. */
3a2b436a 14657 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14658 }
c906108c 14659 else
af34e669 14660 {
e142c38c 14661 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14662 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14663 {
18a8505e 14664 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14665 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14666 in DW_TAG_compile_unit of DWO files.
14667
14668 Attributes of the form DW_FORM_rnglistx have already had their
14669 value changed by read_rnglist_index and already include
14670 DW_AT_rnglists_base, so don't need to add the ranges base,
14671 either. */
14672 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14673 && attr->form != DW_FORM_rnglistx);
529908cb 14674 unsigned int ranges_offset = (attr->as_unsigned ()
ab435259
DE
14675 + (need_ranges_base
14676 ? cu->ranges_base
14677 : 0));
2e3cf129 14678
af34e669 14679 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14680 .debug_ranges section. */
d0ce17d8
CT
14681 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14682 die->tag))
e385593e 14683 return PC_BOUNDS_INVALID;
43039443 14684 /* Found discontinuous range of addresses. */
3a2b436a 14685 ret = PC_BOUNDS_RANGES;
af34e669 14686 }
e385593e
JK
14687 else
14688 return PC_BOUNDS_NOT_PRESENT;
af34e669 14689 }
c906108c 14690
48fbe735 14691 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14692 if (high <= low)
e385593e 14693 return PC_BOUNDS_INVALID;
c906108c
SS
14694
14695 /* When using the GNU linker, .gnu.linkonce. sections are used to
14696 eliminate duplicate copies of functions and vtables and such.
14697 The linker will arbitrarily choose one and discard the others.
14698 The AT_*_pc values for such functions refer to local labels in
14699 these sections. If the section from that file was discarded, the
14700 labels are not in the output, so the relocs get a value of 0.
14701 If this is a discarded function, mark the pc bounds as invalid,
14702 so that GDB will ignore it. */
976ca316 14703 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14704 return PC_BOUNDS_INVALID;
c906108c
SS
14705
14706 *lowpc = low;
96408a79
SA
14707 if (highpc)
14708 *highpc = high;
af34e669 14709 return ret;
c906108c
SS
14710}
14711
b084d499
JB
14712/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14713 its low and high PC addresses. Do nothing if these addresses could not
14714 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14715 and HIGHPC to the high address if greater than HIGHPC. */
14716
14717static void
14718dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14719 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14720 struct dwarf2_cu *cu)
b084d499
JB
14721{
14722 CORE_ADDR low, high;
14723 struct die_info *child = die->child;
14724
e385593e 14725 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14726 {
325fac50
PA
14727 *lowpc = std::min (*lowpc, low);
14728 *highpc = std::max (*highpc, high);
b084d499
JB
14729 }
14730
14731 /* If the language does not allow nested subprograms (either inside
14732 subprograms or lexical blocks), we're done. */
14733 if (cu->language != language_ada)
14734 return;
6e70227d 14735
b084d499
JB
14736 /* Check all the children of the given DIE. If it contains nested
14737 subprograms, then check their pc bounds. Likewise, we need to
14738 check lexical blocks as well, as they may also contain subprogram
14739 definitions. */
14740 while (child && child->tag)
14741 {
14742 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14743 || child->tag == DW_TAG_lexical_block)
14744 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14745 child = child->sibling;
b084d499
JB
14746 }
14747}
14748
fae299cd
DC
14749/* Get the low and high pc's represented by the scope DIE, and store
14750 them in *LOWPC and *HIGHPC. If the correct values can't be
14751 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14752
14753static void
14754get_scope_pc_bounds (struct die_info *die,
14755 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14756 struct dwarf2_cu *cu)
14757{
14758 CORE_ADDR best_low = (CORE_ADDR) -1;
14759 CORE_ADDR best_high = (CORE_ADDR) 0;
14760 CORE_ADDR current_low, current_high;
14761
3a2b436a 14762 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14763 >= PC_BOUNDS_RANGES)
fae299cd
DC
14764 {
14765 best_low = current_low;
14766 best_high = current_high;
14767 }
14768 else
14769 {
14770 struct die_info *child = die->child;
14771
14772 while (child && child->tag)
14773 {
14774 switch (child->tag) {
14775 case DW_TAG_subprogram:
dda83cd7 14776 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14777 break;
14778 case DW_TAG_namespace:
f55ee35c 14779 case DW_TAG_module:
fae299cd
DC
14780 /* FIXME: carlton/2004-01-16: Should we do this for
14781 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14782 that current GCC's always emit the DIEs corresponding
14783 to definitions of methods of classes as children of a
14784 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14785 the DIEs giving the declarations, which could be
14786 anywhere). But I don't see any reason why the
14787 standards says that they have to be there. */
14788 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14789
14790 if (current_low != ((CORE_ADDR) -1))
14791 {
325fac50
PA
14792 best_low = std::min (best_low, current_low);
14793 best_high = std::max (best_high, current_high);
fae299cd
DC
14794 }
14795 break;
14796 default:
0963b4bd 14797 /* Ignore. */
fae299cd
DC
14798 break;
14799 }
14800
436c571c 14801 child = child->sibling;
fae299cd
DC
14802 }
14803 }
14804
14805 *lowpc = best_low;
14806 *highpc = best_high;
14807}
14808
801e3a5b
JB
14809/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14810 in DIE. */
380bca97 14811
801e3a5b
JB
14812static void
14813dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14814 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14815{
5e22e966 14816 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14817 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14818 struct attribute *attr;
91da1414 14819 struct attribute *attr_high;
801e3a5b 14820
91da1414
MW
14821 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14822 if (attr_high)
801e3a5b 14823 {
801e3a5b 14824 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14825 if (attr != nullptr)
dda83cd7 14826 {
95f982e5
TT
14827 CORE_ADDR low = attr->as_address ();
14828 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14829
cd6c91b4 14830 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14831 high += low;
9a619af0 14832
3e29f34a
MR
14833 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14834 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14835 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14836 }
801e3a5b
JB
14837 }
14838
14839 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14840 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14841 {
18a8505e 14842 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14843 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14844 in DW_TAG_compile_unit of DWO files.
14845
14846 Attributes of the form DW_FORM_rnglistx have already had their
14847 value changed by read_rnglist_index and already include
14848 DW_AT_rnglists_base, so don't need to add the ranges base,
14849 either. */
14850 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14851 && attr->form != DW_FORM_rnglistx);
801e3a5b
JB
14852
14853 /* The value of the DW_AT_ranges attribute is the offset of the
dda83cd7 14854 address range list in the .debug_ranges section. */
529908cb 14855 unsigned long offset = (attr->as_unsigned ()
ab435259 14856 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14857
2d5f09ec 14858 std::vector<blockrange> blockvec;
d0ce17d8 14859 dwarf2_ranges_process (offset, cu, die->tag,
5f46c5a5
JK
14860 [&] (CORE_ADDR start, CORE_ADDR end)
14861 {
58fdfd2c
JK
14862 start += baseaddr;
14863 end += baseaddr;
5f46c5a5
JK
14864 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14865 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14866 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14867 blockvec.emplace_back (start, end);
5f46c5a5 14868 });
2d5f09ec
KB
14869
14870 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14871 }
14872}
14873
685b1105
JK
14874/* Check whether the producer field indicates either of GCC < 4.6, or the
14875 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14876
685b1105
JK
14877static void
14878check_producer (struct dwarf2_cu *cu)
60d5a603 14879{
38360086 14880 int major, minor;
60d5a603
JK
14881
14882 if (cu->producer == NULL)
14883 {
14884 /* For unknown compilers expect their behavior is DWARF version
14885 compliant.
14886
14887 GCC started to support .debug_types sections by -gdwarf-4 since
14888 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14889 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14890 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14891 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14892 }
b1ffba5a 14893 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14894 {
38360086
MW
14895 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14896 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14897 }
5230b05a 14898 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14899 {
14900 cu->producer_is_icc = true;
14901 cu->producer_is_icc_lt_14 = major < 14;
14902 }
c258c396
JD
14903 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14904 cu->producer_is_codewarrior = true;
685b1105
JK
14905 else
14906 {
14907 /* For other non-GCC compilers, expect their behavior is DWARF version
14908 compliant. */
60d5a603
JK
14909 }
14910
9068261f 14911 cu->checked_producer = true;
685b1105 14912}
ba919b58 14913
685b1105
JK
14914/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14915 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14916 during 4.6.0 experimental. */
14917
9068261f 14918static bool
685b1105
JK
14919producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14920{
14921 if (!cu->checked_producer)
14922 check_producer (cu);
14923
14924 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14925}
14926
c258c396
JD
14927
14928/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14929 with incorrect is_stmt attributes. */
14930
14931static bool
14932producer_is_codewarrior (struct dwarf2_cu *cu)
14933{
14934 if (!cu->checked_producer)
14935 check_producer (cu);
14936
14937 return cu->producer_is_codewarrior;
14938}
14939
bf23a268
TT
14940/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14941 If that attribute is not available, return the appropriate
14942 default. */
60d5a603
JK
14943
14944static enum dwarf_access_attribute
bf23a268 14945dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14946{
bf23a268
TT
14947 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14948 if (attr != nullptr)
14949 {
14950 LONGEST value = attr->constant_value (-1);
14951 if (value == DW_ACCESS_public
14952 || value == DW_ACCESS_protected
14953 || value == DW_ACCESS_private)
14954 return (dwarf_access_attribute) value;
14955 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14956 plongest (value));
14957 }
14958
60d5a603
JK
14959 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14960 {
14961 /* The default DWARF 2 accessibility for members is public, the default
14962 accessibility for inheritance is private. */
14963
14964 if (die->tag != DW_TAG_inheritance)
14965 return DW_ACCESS_public;
14966 else
14967 return DW_ACCESS_private;
14968 }
14969 else
14970 {
14971 /* DWARF 3+ defines the default accessibility a different way. The same
14972 rules apply now for DW_TAG_inheritance as for the members and it only
14973 depends on the container kind. */
14974
14975 if (die->parent->tag == DW_TAG_class_type)
14976 return DW_ACCESS_private;
14977 else
14978 return DW_ACCESS_public;
14979 }
14980}
14981
74ac6d43
TT
14982/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14983 offset. If the attribute was not found return 0, otherwise return
14984 1. If it was found but could not properly be handled, set *OFFSET
14985 to 0. */
14986
14987static int
14988handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14989 LONGEST *offset)
14990{
14991 struct attribute *attr;
14992
14993 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14994 if (attr != NULL)
14995 {
14996 *offset = 0;
14997
14998 /* Note that we do not check for a section offset first here.
14999 This is because DW_AT_data_member_location is new in DWARF 4,
15000 so if we see it, we can assume that a constant form is really
15001 a constant and not a section offset. */
cd6c91b4 15002 if (attr->form_is_constant ())
0826b30a 15003 *offset = attr->constant_value (0);
cd6c91b4 15004 else if (attr->form_is_section_offset ())
74ac6d43 15005 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 15006 else if (attr->form_is_block ())
9d2246fc 15007 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
15008 else
15009 dwarf2_complex_location_expr_complaint ();
15010
15011 return 1;
15012 }
15013
15014 return 0;
15015}
15016
7d79de9a
TT
15017/* Look for DW_AT_data_member_location and store the results in FIELD. */
15018
15019static void
15020handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15021 struct field *field)
15022{
15023 struct attribute *attr;
15024
15025 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15026 if (attr != NULL)
15027 {
15028 if (attr->form_is_constant ())
15029 {
15030 LONGEST offset = attr->constant_value (0);
15031 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15032 }
15033 else if (attr->form_is_section_offset ())
15034 dwarf2_complex_location_expr_complaint ();
15035 else if (attr->form_is_block ())
15036 {
15037 bool handled;
9d2246fc 15038 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
15039 if (handled)
15040 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15041 else
15042 {
5e22e966
SM
15043 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15044 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
15045 struct dwarf2_locexpr_baton *dlbaton
15046 = XOBNEW (&objfile->objfile_obstack,
15047 struct dwarf2_locexpr_baton);
9d2246fc
TT
15048 dlbaton->data = attr->as_block ()->data;
15049 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
15050 /* When using this baton, we want to compute the address
15051 of the field, not the value. This is why
15052 is_reference is set to false here. */
15053 dlbaton->is_reference = false;
5e22e966 15054 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
15055 dlbaton->per_cu = cu->per_cu;
15056
15057 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15058 }
15059 }
15060 else
15061 dwarf2_complex_location_expr_complaint ();
15062 }
15063}
15064
c906108c
SS
15065/* Add an aggregate field to the field list. */
15066
15067static void
107d2387 15068dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15069 struct dwarf2_cu *cu)
6e70227d 15070{
5e22e966 15071 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15072 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15073 struct nextfield *new_field;
15074 struct attribute *attr;
15075 struct field *fp;
15d034d0 15076 const char *fieldname = "";
c906108c 15077
7d0ccb61
DJ
15078 if (die->tag == DW_TAG_inheritance)
15079 {
be2daae6
TT
15080 fip->baseclasses.emplace_back ();
15081 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15082 }
15083 else
15084 {
be2daae6
TT
15085 fip->fields.emplace_back ();
15086 new_field = &fip->fields.back ();
7d0ccb61 15087 }
be2daae6 15088
9c6a1327
TT
15089 new_field->offset = die->sect_off;
15090
bf23a268 15091 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15092 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15093 fip->non_public_fields = true;
60d5a603 15094
e142c38c 15095 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15096 if (attr != nullptr)
23dca5c3 15097 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15098 else
15099 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15100
15101 fp = &new_field->field;
a9a9bd0f 15102
e142c38c 15103 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15104 {
a9a9bd0f 15105 /* Data member other than a C++ static data member. */
6e70227d 15106
c906108c 15107 /* Get type of field. */
5d14b6e5 15108 fp->set_type (die_type (die, cu));
c906108c 15109
d6a843b5 15110 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15111
c906108c 15112 /* Get bit size of field (zero if none). */
e142c38c 15113 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15114 if (attr != nullptr)
c906108c 15115 {
529908cb 15116 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15117 }
15118 else
15119 {
15120 FIELD_BITSIZE (*fp) = 0;
15121 }
15122
15123 /* Get bit offset of field. */
7d79de9a 15124 handle_data_member_location (die, cu, fp);
e142c38c 15125 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15126 if (attr != nullptr && attr->form_is_constant ())
c906108c 15127 {
d5a22e77 15128 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15129 {
15130 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15131 additional bit offset from the MSB of the containing
15132 anonymous object to the MSB of the field. We don't
15133 have to do anything special since we don't need to
15134 know the size of the anonymous object. */
529908cb 15135 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15136 + attr->constant_value (0)));
c906108c
SS
15137 }
15138 else
15139 {
15140 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15141 MSB of the anonymous object, subtract off the number of
15142 bits from the MSB of the field to the MSB of the
15143 object, and then subtract off the number of bits of
15144 the field itself. The result is the bit offset of
15145 the LSB of the field. */
c906108c 15146 int anonymous_size;
7c184d33 15147 int bit_offset = attr->constant_value (0);
c906108c 15148
e142c38c 15149 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15150 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15151 {
15152 /* The size of the anonymous object containing
15153 the bit field is explicit, so use the
15154 indicated size (in bytes). */
7c184d33 15155 anonymous_size = attr->constant_value (0);
c906108c
SS
15156 }
15157 else
15158 {
15159 /* The size of the anonymous object containing
15160 the bit field must be inferred from the type
15161 attribute of the data member containing the
15162 bit field. */
5d14b6e5 15163 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15164 }
f41f5e61
PA
15165 SET_FIELD_BITPOS (*fp,
15166 (FIELD_BITPOS (*fp)
15167 + anonymous_size * bits_per_byte
15168 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15169 }
15170 }
da5b30da
AA
15171 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15172 if (attr != NULL)
15173 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15174 + attr->constant_value (0)));
c906108c
SS
15175
15176 /* Get name of field. */
39cbfefa
DJ
15177 fieldname = dwarf2_name (die, cu);
15178 if (fieldname == NULL)
15179 fieldname = "";
d8151005
DJ
15180
15181 /* The name is already allocated along with this objfile, so we don't
15182 need to duplicate it for the type. */
15183 fp->name = fieldname;
c906108c
SS
15184
15185 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15186 pointer or virtual base class pointer) to private. */
e142c38c 15187 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15188 {
d48cc9dd 15189 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15190 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15191 fip->non_public_fields = true;
c906108c
SS
15192 }
15193 }
a9a9bd0f 15194 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15195 {
a9a9bd0f
DC
15196 /* C++ static member. */
15197
15198 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15199 is a declaration, but all versions of G++ as of this writing
15200 (so through at least 3.2.1) incorrectly generate
15201 DW_TAG_variable tags. */
6e70227d 15202
ff355380 15203 const char *physname;
c906108c 15204
a9a9bd0f 15205 /* Get name of field. */
39cbfefa
DJ
15206 fieldname = dwarf2_name (die, cu);
15207 if (fieldname == NULL)
c906108c
SS
15208 return;
15209
254e6b9e 15210 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15211 if (attr
15212 /* Only create a symbol if this is an external value.
15213 new_symbol checks this and puts the value in the global symbol
15214 table, which we want. If it is not external, new_symbol
15215 will try to put the value in cu->list_in_scope which is wrong. */
15216 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15217 {
15218 /* A static const member, not much different than an enum as far as
15219 we're concerned, except that we can support more types. */
15220 new_symbol (die, NULL, cu);
15221 }
15222
2df3850c 15223 /* Get physical name. */
ff355380 15224 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15225
d8151005
DJ
15226 /* The name is already allocated along with this objfile, so we don't
15227 need to duplicate it for the type. */
15228 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15229 fp->set_type (die_type (die, cu));
d8151005 15230 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15231 }
15232 else if (die->tag == DW_TAG_inheritance)
15233 {
74ac6d43 15234 /* C++ base class field. */
7d79de9a 15235 handle_data_member_location (die, cu, fp);
c906108c 15236 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15237 fp->set_type (die_type (die, cu));
15238 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15239 }
2ddeaf8a
TT
15240 else
15241 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15242}
15243
883fd55a
KS
15244/* Can the type given by DIE define another type? */
15245
15246static bool
15247type_can_define_types (const struct die_info *die)
15248{
15249 switch (die->tag)
15250 {
15251 case DW_TAG_typedef:
15252 case DW_TAG_class_type:
15253 case DW_TAG_structure_type:
15254 case DW_TAG_union_type:
15255 case DW_TAG_enumeration_type:
15256 return true;
15257
15258 default:
15259 return false;
15260 }
15261}
15262
15263/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15264
15265static void
883fd55a
KS
15266dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15267 struct dwarf2_cu *cu)
6e70227d 15268{
be2daae6
TT
15269 struct decl_field fp;
15270 memset (&fp, 0, sizeof (fp));
98751a41 15271
883fd55a 15272 gdb_assert (type_can_define_types (die));
98751a41 15273
883fd55a 15274 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15275 fp.name = dwarf2_name (die, cu);
15276 fp.type = read_type_die (die, cu);
98751a41 15277
c191a687 15278 /* Save accessibility. */
bf23a268 15279 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15280 switch (accessibility)
15281 {
15282 case DW_ACCESS_public:
15283 /* The assumed value if neither private nor protected. */
15284 break;
15285 case DW_ACCESS_private:
be2daae6 15286 fp.is_private = 1;
c191a687
KS
15287 break;
15288 case DW_ACCESS_protected:
be2daae6 15289 fp.is_protected = 1;
c191a687 15290 break;
c191a687
KS
15291 }
15292
883fd55a 15293 if (die->tag == DW_TAG_typedef)
be2daae6 15294 fip->typedef_field_list.push_back (fp);
883fd55a 15295 else
be2daae6 15296 fip->nested_types_list.push_back (fp);
98751a41
JK
15297}
15298
9c6a1327
TT
15299/* A convenience typedef that's used when finding the discriminant
15300 field for a variant part. */
1b95cdb7
SM
15301typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15302 offset_map_type;
9c6a1327
TT
15303
15304/* Compute the discriminant range for a given variant. OBSTACK is
15305 where the results will be stored. VARIANT is the variant to
15306 process. IS_UNSIGNED indicates whether the discriminant is signed
15307 or unsigned. */
15308
15309static const gdb::array_view<discriminant_range>
15310convert_variant_range (struct obstack *obstack, const variant_field &variant,
15311 bool is_unsigned)
15312{
15313 std::vector<discriminant_range> ranges;
15314
15315 if (variant.default_branch)
15316 return {};
15317
15318 if (variant.discr_list_data == nullptr)
15319 {
15320 discriminant_range r
15321 = {variant.discriminant_value, variant.discriminant_value};
15322 ranges.push_back (r);
15323 }
15324 else
15325 {
15326 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15327 variant.discr_list_data->size);
15328 while (!data.empty ())
15329 {
15330 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15331 {
15332 complaint (_("invalid discriminant marker: %d"), data[0]);
15333 break;
15334 }
15335 bool is_range = data[0] == DW_DSC_range;
15336 data = data.slice (1);
15337
15338 ULONGEST low, high;
15339 unsigned int bytes_read;
15340
15341 if (data.empty ())
15342 {
15343 complaint (_("DW_AT_discr_list missing low value"));
15344 break;
15345 }
15346 if (is_unsigned)
15347 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15348 else
15349 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15350 &bytes_read);
15351 data = data.slice (bytes_read);
15352
15353 if (is_range)
15354 {
15355 if (data.empty ())
15356 {
15357 complaint (_("DW_AT_discr_list missing high value"));
15358 break;
15359 }
15360 if (is_unsigned)
15361 high = read_unsigned_leb128 (nullptr, data.data (),
15362 &bytes_read);
15363 else
15364 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15365 &bytes_read);
15366 data = data.slice (bytes_read);
15367 }
15368 else
15369 high = low;
15370
15371 ranges.push_back ({ low, high });
15372 }
15373 }
15374
15375 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15376 ranges.size ());
15377 std::copy (ranges.begin (), ranges.end (), result);
15378 return gdb::array_view<discriminant_range> (result, ranges.size ());
15379}
15380
15381static const gdb::array_view<variant_part> create_variant_parts
15382 (struct obstack *obstack,
15383 const offset_map_type &offset_map,
15384 struct field_info *fi,
15385 const std::vector<variant_part_builder> &variant_parts);
15386
15387/* Fill in a "struct variant" for a given variant field. RESULT is
15388 the variant to fill in. OBSTACK is where any needed allocations
15389 will be done. OFFSET_MAP holds the mapping from section offsets to
15390 fields for the type. FI describes the fields of the type we're
15391 processing. FIELD is the variant field we're converting. */
15392
15393static void
15394create_one_variant (variant &result, struct obstack *obstack,
15395 const offset_map_type &offset_map,
15396 struct field_info *fi, const variant_field &field)
15397{
15398 result.discriminants = convert_variant_range (obstack, field, false);
15399 result.first_field = field.first_field + fi->baseclasses.size ();
15400 result.last_field = field.last_field + fi->baseclasses.size ();
15401 result.parts = create_variant_parts (obstack, offset_map, fi,
15402 field.variant_parts);
15403}
15404
15405/* Fill in a "struct variant_part" for a given variant part. RESULT
15406 is the variant part to fill in. OBSTACK is where any needed
15407 allocations will be done. OFFSET_MAP holds the mapping from
15408 section offsets to fields for the type. FI describes the fields of
15409 the type we're processing. BUILDER is the variant part to be
15410 converted. */
15411
15412static void
15413create_one_variant_part (variant_part &result,
15414 struct obstack *obstack,
15415 const offset_map_type &offset_map,
15416 struct field_info *fi,
15417 const variant_part_builder &builder)
15418{
15419 auto iter = offset_map.find (builder.discriminant_offset);
15420 if (iter == offset_map.end ())
15421 {
15422 result.discriminant_index = -1;
15423 /* Doesn't matter. */
15424 result.is_unsigned = false;
15425 }
15426 else
15427 {
15428 result.discriminant_index = iter->second;
15429 result.is_unsigned
c6d940a9 15430 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15431 }
15432
15433 size_t n = builder.variants.size ();
15434 variant *output = new (obstack) variant[n];
15435 for (size_t i = 0; i < n; ++i)
15436 create_one_variant (output[i], obstack, offset_map, fi,
15437 builder.variants[i]);
15438
15439 result.variants = gdb::array_view<variant> (output, n);
15440}
15441
15442/* Create a vector of variant parts that can be attached to a type.
15443 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15444 holds the mapping from section offsets to fields for the type. FI
15445 describes the fields of the type we're processing. VARIANT_PARTS
15446 is the vector to convert. */
15447
15448static const gdb::array_view<variant_part>
15449create_variant_parts (struct obstack *obstack,
15450 const offset_map_type &offset_map,
15451 struct field_info *fi,
15452 const std::vector<variant_part_builder> &variant_parts)
15453{
15454 if (variant_parts.empty ())
15455 return {};
15456
15457 size_t n = variant_parts.size ();
15458 variant_part *result = new (obstack) variant_part[n];
15459 for (size_t i = 0; i < n; ++i)
15460 create_one_variant_part (result[i], obstack, offset_map, fi,
15461 variant_parts[i]);
15462
15463 return gdb::array_view<variant_part> (result, n);
15464}
15465
15466/* Compute the variant part vector for FIP, attaching it to TYPE when
15467 done. */
15468
15469static void
15470add_variant_property (struct field_info *fip, struct type *type,
15471 struct dwarf2_cu *cu)
15472{
15473 /* Map section offsets of fields to their field index. Note the
15474 field index here does not take the number of baseclasses into
15475 account. */
15476 offset_map_type offset_map;
15477 for (int i = 0; i < fip->fields.size (); ++i)
15478 offset_map[fip->fields[i].offset] = i;
15479
5e22e966 15480 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15481 gdb::array_view<variant_part> parts
15482 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15483 fip->variant_parts);
15484
15485 struct dynamic_prop prop;
8c2e4e06
SM
15486 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15487 obstack_copy (&objfile->objfile_obstack, &parts,
15488 sizeof (parts)));
9c6a1327 15489
5c54719c 15490 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15491}
15492
c906108c
SS
15493/* Create the vector of fields, and attach it to the type. */
15494
15495static void
fba45db2 15496dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15497 struct dwarf2_cu *cu)
c906108c 15498{
317f7127 15499 int nfields = fip->nfields ();
c906108c
SS
15500
15501 /* Record the field count, allocate space for the array of fields,
15502 and create blank accessibility bitfields if necessary. */
5e33d5f4 15503 type->set_num_fields (nfields);
3cabb6b0
SM
15504 type->set_fields
15505 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15506
b4ba55a1 15507 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15508 {
15509 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15510
15511 TYPE_FIELD_PRIVATE_BITS (type) =
15512 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15513 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15514
15515 TYPE_FIELD_PROTECTED_BITS (type) =
15516 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15517 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15518
774b6a14
TT
15519 TYPE_FIELD_IGNORE_BITS (type) =
15520 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15521 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15522 }
15523
15524 /* If the type has baseclasses, allocate and clear a bit vector for
15525 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15526 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15527 {
be2daae6 15528 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15529 unsigned char *pointer;
c906108c
SS
15530
15531 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15532 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15533 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15534 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15535 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15536 }
15537
9c6a1327
TT
15538 if (!fip->variant_parts.empty ())
15539 add_variant_property (fip, type, cu);
2ddeaf8a 15540
be2daae6
TT
15541 /* Copy the saved-up fields into the field vector. */
15542 for (int i = 0; i < nfields; ++i)
c906108c 15543 {
be2daae6
TT
15544 struct nextfield &field
15545 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15546 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15547
ceacbf6e 15548 type->field (i) = field.field;
be2daae6 15549 switch (field.accessibility)
c906108c 15550 {
c5aa993b 15551 case DW_ACCESS_private:
b4ba55a1 15552 if (cu->language != language_ada)
be2daae6 15553 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15554 break;
c906108c 15555
c5aa993b 15556 case DW_ACCESS_protected:
b4ba55a1 15557 if (cu->language != language_ada)
be2daae6 15558 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15559 break;
c906108c 15560
c5aa993b
JM
15561 case DW_ACCESS_public:
15562 break;
c906108c 15563
c5aa993b
JM
15564 default:
15565 /* Unknown accessibility. Complain and treat it as public. */
15566 {
b98664d3 15567 complaint (_("unsupported accessibility %d"),
be2daae6 15568 field.accessibility);
c5aa993b
JM
15569 }
15570 break;
c906108c 15571 }
be2daae6 15572 if (i < fip->baseclasses.size ())
c906108c 15573 {
be2daae6 15574 switch (field.virtuality)
c906108c 15575 {
c5aa993b
JM
15576 case DW_VIRTUALITY_virtual:
15577 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15578 if (cu->language == language_ada)
a73c6dcd 15579 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15580 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15581 break;
c906108c
SS
15582 }
15583 }
c906108c
SS
15584 }
15585}
15586
7d27a96d
TT
15587/* Return true if this member function is a constructor, false
15588 otherwise. */
15589
15590static int
15591dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15592{
15593 const char *fieldname;
fe978cb0 15594 const char *type_name;
7d27a96d
TT
15595 int len;
15596
15597 if (die->parent == NULL)
15598 return 0;
15599
15600 if (die->parent->tag != DW_TAG_structure_type
15601 && die->parent->tag != DW_TAG_union_type
15602 && die->parent->tag != DW_TAG_class_type)
15603 return 0;
15604
15605 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15606 type_name = dwarf2_name (die->parent, cu);
15607 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15608 return 0;
15609
15610 len = strlen (fieldname);
fe978cb0
PA
15611 return (strncmp (fieldname, type_name, len) == 0
15612 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15613}
15614
c906108c
SS
15615/* Add a member function to the proper fieldlist. */
15616
15617static void
107d2387 15618dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15619 struct type *type, struct dwarf2_cu *cu)
c906108c 15620{
5e22e966 15621 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15622 struct attribute *attr;
c906108c 15623 int i;
be2daae6 15624 struct fnfieldlist *flp = nullptr;
c906108c 15625 struct fn_field *fnp;
15d034d0 15626 const char *fieldname;
f792889a 15627 struct type *this_type;
c906108c 15628
b4ba55a1 15629 if (cu->language == language_ada)
a73c6dcd 15630 error (_("unexpected member function in Ada type"));
b4ba55a1 15631
2df3850c 15632 /* Get name of member function. */
39cbfefa
DJ
15633 fieldname = dwarf2_name (die, cu);
15634 if (fieldname == NULL)
2df3850c 15635 return;
c906108c 15636
c906108c 15637 /* Look up member function name in fieldlist. */
be2daae6 15638 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15639 {
27bfe10e 15640 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15641 {
15642 flp = &fip->fnfieldlists[i];
15643 break;
15644 }
c906108c
SS
15645 }
15646
be2daae6
TT
15647 /* Create a new fnfieldlist if necessary. */
15648 if (flp == nullptr)
c906108c 15649 {
be2daae6
TT
15650 fip->fnfieldlists.emplace_back ();
15651 flp = &fip->fnfieldlists.back ();
c906108c 15652 flp->name = fieldname;
be2daae6 15653 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15654 }
15655
be2daae6
TT
15656 /* Create a new member function field and add it to the vector of
15657 fnfieldlists. */
15658 flp->fnfields.emplace_back ();
15659 fnp = &flp->fnfields.back ();
3da10d80
KS
15660
15661 /* Delay processing of the physname until later. */
9c37b5ae 15662 if (cu->language == language_cplus)
be2daae6
TT
15663 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15664 die, cu);
3da10d80
KS
15665 else
15666 {
1d06ead6 15667 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15668 fnp->physname = physname ? physname : "";
15669 }
15670
c906108c 15671 fnp->type = alloc_type (objfile);
f792889a 15672 this_type = read_type_die (die, cu);
78134374 15673 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15674 {
1f704f76 15675 int nparams = this_type->num_fields ();
c906108c 15676
f792889a 15677 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15678 of the method itself (TYPE_CODE_METHOD). */
15679 smash_to_method_type (fnp->type, type,
f792889a 15680 TYPE_TARGET_TYPE (this_type),
80fc5e77 15681 this_type->fields (),
1f704f76 15682 this_type->num_fields (),
a409645d 15683 this_type->has_varargs ());
c906108c
SS
15684
15685 /* Handle static member functions.
dda83cd7
SM
15686 Dwarf2 has no clean way to discern C++ static and non-static
15687 member functions. G++ helps GDB by marking the first
15688 parameter for non-static member functions (which is the this
15689 pointer) as artificial. We obtain this information from
15690 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15691 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15692 fnp->voffset = VOFFSET_STATIC;
15693 }
15694 else
b98664d3 15695 complaint (_("member function type missing for '%s'"),
3da10d80 15696 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15697
15698 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15699 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15700 fnp->fcontext = die_containing_type (die, cu);
c906108c 15701
3e43a32a
MS
15702 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15703 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15704
15705 /* Get accessibility. */
bf23a268 15706 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15707 switch (accessibility)
c906108c 15708 {
60d5a603
JK
15709 case DW_ACCESS_private:
15710 fnp->is_private = 1;
15711 break;
15712 case DW_ACCESS_protected:
15713 fnp->is_protected = 1;
15714 break;
c906108c
SS
15715 }
15716
b02dede2 15717 /* Check for artificial methods. */
e142c38c 15718 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15719 if (attr && attr->as_boolean ())
b02dede2
DJ
15720 fnp->is_artificial = 1;
15721
e35000a7
TBA
15722 /* Check for defaulted methods. */
15723 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15724 if (attr != nullptr)
15725 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15726
15727 /* Check for deleted methods. */
15728 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15729 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15730 fnp->is_deleted = 1;
15731
7d27a96d
TT
15732 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15733
0d564a31 15734 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15735 function. For older versions of GCC, this is an offset in the
15736 appropriate virtual table, as specified by DW_AT_containing_type.
15737 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15738 to the object address. */
15739
e142c38c 15740 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15741 if (attr != nullptr)
8e19ed76 15742 {
9d2246fc 15743 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15744 {
9d2246fc
TT
15745 struct dwarf_block *block = attr->as_block ();
15746
15747 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15748 {
15749 /* Old-style GCC. */
9d2246fc 15750 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15751 }
9d2246fc
TT
15752 else if (block->data[0] == DW_OP_deref
15753 || (block->size > 1
15754 && block->data[0] == DW_OP_deref_size
15755 && block->data[1] == cu->header.addr_size))
aec5aa8b 15756 {
9d2246fc 15757 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15758 if ((fnp->voffset % cu->header.addr_size) != 0)
15759 dwarf2_complex_location_expr_complaint ();
15760 else
15761 fnp->voffset /= cu->header.addr_size;
15762 fnp->voffset += 2;
15763 }
15764 else
15765 dwarf2_complex_location_expr_complaint ();
15766
15767 if (!fnp->fcontext)
7e993ebf
KS
15768 {
15769 /* If there is no `this' field and no DW_AT_containing_type,
15770 we cannot actually find a base class context for the
15771 vtable! */
1f704f76 15772 if (this_type->num_fields () == 0
7e993ebf
KS
15773 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15774 {
b98664d3 15775 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15776 "function \"%s\" (offset %s)"),
15777 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15778 }
15779 else
15780 {
15781 fnp->fcontext
940da03e 15782 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15783 }
15784 }
aec5aa8b 15785 }
cd6c91b4 15786 else if (attr->form_is_section_offset ())
dda83cd7 15787 {
4d3c2250 15788 dwarf2_complex_location_expr_complaint ();
dda83cd7 15789 }
8e19ed76 15790 else
dda83cd7 15791 {
4d3c2250
KB
15792 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15793 fieldname);
dda83cd7 15794 }
0d564a31 15795 }
d48cc9dd
DJ
15796 else
15797 {
15798 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15799 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15800 {
15801 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15802 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15803 "but the vtable offset is not specified"),
9d8780f0 15804 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15805 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15806 TYPE_CPLUS_DYNAMIC (type) = 1;
15807 }
15808 }
c906108c
SS
15809}
15810
15811/* Create the vector of member function fields, and attach it to the type. */
15812
15813static void
fba45db2 15814dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15815 struct dwarf2_cu *cu)
c906108c 15816{
b4ba55a1 15817 if (cu->language == language_ada)
a73c6dcd 15818 error (_("unexpected member functions in Ada type"));
b4ba55a1 15819
c906108c
SS
15820 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15821 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15822 TYPE_ALLOC (type,
15823 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15824
be2daae6 15825 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15826 {
be2daae6 15827 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15828 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15829
be2daae6
TT
15830 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15831 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15832 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15833 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15834
15835 for (int k = 0; k < nf.fnfields.size (); ++k)
15836 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15837 }
15838
be2daae6 15839 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15840}
15841
1168df01
JB
15842/* Returns non-zero if NAME is the name of a vtable member in CU's
15843 language, zero otherwise. */
15844static int
15845is_vtable_name (const char *name, struct dwarf2_cu *cu)
15846{
15847 static const char vptr[] = "_vptr";
15848
9c37b5ae
TT
15849 /* Look for the C++ form of the vtable. */
15850 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15851 return 1;
15852
15853 return 0;
15854}
15855
c0dd20ea 15856/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15857 functions, with the ABI-specified layout. If TYPE describes
15858 such a structure, smash it into a member function type.
61049d3b
DJ
15859
15860 GCC shouldn't do this; it should just output pointer to member DIEs.
15861 This is GCC PR debug/28767. */
c0dd20ea 15862
0b92b5bb
TT
15863static void
15864quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15865{
09e2d7c7 15866 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15867
15868 /* Check for a structure with no name and two children. */
1f704f76 15869 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15870 return;
c0dd20ea
DJ
15871
15872 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15873 if (TYPE_FIELD_NAME (type, 0) == NULL
15874 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15875 || TYPE_FIELD_NAME (type, 1) == NULL
15876 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15877 return;
c0dd20ea
DJ
15878
15879 /* Find the type of the method. */
940da03e 15880 pfn_type = type->field (0).type ();
c0dd20ea 15881 if (pfn_type == NULL
78134374
SM
15882 || pfn_type->code () != TYPE_CODE_PTR
15883 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15884 return;
c0dd20ea
DJ
15885
15886 /* Look for the "this" argument. */
15887 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15888 if (pfn_type->num_fields () == 0
940da03e
SM
15889 /* || pfn_type->field (0).type () == NULL */
15890 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15891 return;
c0dd20ea 15892
940da03e 15893 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15894 new_type = alloc_type (objfile);
09e2d7c7 15895 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15896 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15897 pfn_type->has_varargs ());
0b92b5bb 15898 smash_to_methodptr_type (type, new_type);
c0dd20ea 15899}
1168df01 15900
57567375
TT
15901/* While some versions of GCC will generate complicated DWARF for an
15902 array (see quirk_ada_thick_pointer), more recent versions were
15903 modified to emit an explicit thick pointer structure. However, in
15904 this case, the array still has DWARF expressions for its ranges,
15905 and these must be ignored. */
15906
15907static void
15908quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15909 struct type *type)
15910{
15911 gdb_assert (cu->language == language_ada);
15912
15913 /* Check for a structure with two children. */
15914 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15915 return;
15916
15917 /* Check for P_ARRAY and P_BOUNDS members. */
15918 if (TYPE_FIELD_NAME (type, 0) == NULL
15919 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15920 || TYPE_FIELD_NAME (type, 1) == NULL
15921 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15922 return;
15923
15924 /* Make sure we're looking at a pointer to an array. */
15925 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15926 return;
15927 struct type *ary_type = TYPE_TARGET_TYPE (type->field (0).type ());
15928
15929 while (ary_type->code () == TYPE_CODE_ARRAY)
15930 {
15931 /* The Ada code already knows how to handle these types, so all
15932 that we need to do is turn the bounds into static bounds. */
15933 struct type *index_type = ary_type->index_type ();
15934
15935 index_type->bounds ()->low.set_const_val (1);
15936 index_type->bounds ()->high.set_const_val (0);
15937
15938 /* Handle multi-dimensional arrays. */
15939 ary_type = TYPE_TARGET_TYPE (ary_type);
15940 }
15941}
15942
2b4424c3
TT
15943/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15944 appropriate error checking and issuing complaints if there is a
15945 problem. */
15946
15947static ULONGEST
15948get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15949{
15950 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15951
15952 if (attr == nullptr)
15953 return 0;
15954
cd6c91b4 15955 if (!attr->form_is_constant ())
2b4424c3 15956 {
b98664d3 15957 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
15958 " - DIE at %s [in module %s]"),
15959 sect_offset_str (die->sect_off),
5e22e966 15960 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15961 return 0;
15962 }
15963
529908cb
TT
15964 LONGEST val = attr->constant_value (0);
15965 if (val < 0)
2b4424c3 15966 {
529908cb
TT
15967 complaint (_("DW_AT_alignment value must not be negative"
15968 " - DIE at %s [in module %s]"),
15969 sect_offset_str (die->sect_off),
15970 objfile_name (cu->per_objfile->objfile));
15971 return 0;
2b4424c3 15972 }
529908cb 15973 ULONGEST align = val;
2b4424c3
TT
15974
15975 if (align == 0)
15976 {
b98664d3 15977 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
15978 " - DIE at %s [in module %s]"),
15979 sect_offset_str (die->sect_off),
5e22e966 15980 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15981 return 0;
15982 }
15983 if ((align & (align - 1)) != 0)
15984 {
b98664d3 15985 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
15986 " - DIE at %s [in module %s]"),
15987 sect_offset_str (die->sect_off),
5e22e966 15988 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
15989 return 0;
15990 }
15991
15992 return align;
15993}
15994
15995/* If the DIE has a DW_AT_alignment attribute, use its value to set
15996 the alignment for TYPE. */
15997
15998static void
15999maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16000 struct type *type)
16001{
16002 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 16003 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
16004 " - DIE at %s [in module %s]"),
16005 sect_offset_str (die->sect_off),
5e22e966 16006 objfile_name (cu->per_objfile->objfile));
2b4424c3 16007}
685b1105 16008
e35000a7
TBA
16009/* Check if the given VALUE is a valid enum dwarf_calling_convention
16010 constant for a type, according to DWARF5 spec, Table 5.5. */
16011
16012static bool
16013is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16014{
16015 switch (value)
16016 {
16017 case DW_CC_normal:
16018 case DW_CC_pass_by_reference:
16019 case DW_CC_pass_by_value:
16020 return true;
16021
16022 default:
16023 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16024 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16025 return false;
16026 }
16027}
16028
d0922fcf
TBA
16029/* Check if the given VALUE is a valid enum dwarf_calling_convention
16030 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16031 also according to GNU-specific values (see include/dwarf2.h). */
16032
16033static bool
16034is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16035{
16036 switch (value)
16037 {
16038 case DW_CC_normal:
16039 case DW_CC_program:
16040 case DW_CC_nocall:
16041 return true;
16042
16043 case DW_CC_GNU_renesas_sh:
16044 case DW_CC_GNU_borland_fastcall_i386:
16045 case DW_CC_GDB_IBM_OpenCL:
16046 return true;
16047
16048 default:
16049 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16050 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16051 return false;
16052 }
16053}
16054
c906108c 16055/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16056 (definition) to create a type for the structure or union. Fill in
16057 the type's name and general properties; the members will not be
83655187
DE
16058 processed until process_structure_scope. A symbol table entry for
16059 the type will also not be done until process_structure_scope (assuming
16060 the type has a name).
c906108c 16061
c767944b
DJ
16062 NOTE: we need to call these functions regardless of whether or not the
16063 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16064 structure or union. This gets the type entered into our set of
83655187 16065 user defined types. */
c906108c 16066
f792889a 16067static struct type *
134d01f1 16068read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16069{
5e22e966 16070 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16071 struct type *type;
16072 struct attribute *attr;
15d034d0 16073 const char *name;
c906108c 16074
348e048f
DE
16075 /* If the definition of this type lives in .debug_types, read that type.
16076 Don't follow DW_AT_specification though, that will take us back up
16077 the chain and we want to go down. */
052c8bb8 16078 attr = die->attr (DW_AT_signature);
435d3d88 16079 if (attr != nullptr)
348e048f 16080 {
ac9ec31b 16081 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16082
ac9ec31b 16083 /* The type's CU may not be the same as CU.
02142a6c 16084 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16085 return set_die_type (die, type, cu);
16086 }
16087
c0dd20ea 16088 type = alloc_type (objfile);
c906108c 16089 INIT_CPLUS_SPECIFIC (type);
93311388 16090
39cbfefa
DJ
16091 name = dwarf2_name (die, cu);
16092 if (name != NULL)
c906108c 16093 {
987504bb 16094 if (cu->language == language_cplus
c44af4eb
TT
16095 || cu->language == language_d
16096 || cu->language == language_rust)
63d06c5c 16097 {
15d034d0 16098 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16099
16100 /* dwarf2_full_name might have already finished building the DIE's
16101 type. If so, there is no need to continue. */
16102 if (get_die_type (die, cu) != NULL)
16103 return get_die_type (die, cu);
16104
d0e39ea2 16105 type->set_name (full_name);
63d06c5c
DC
16106 }
16107 else
16108 {
d8151005
DJ
16109 /* The name is already allocated along with this objfile, so
16110 we don't need to duplicate it for the type. */
d0e39ea2 16111 type->set_name (name);
63d06c5c 16112 }
c906108c
SS
16113 }
16114
16115 if (die->tag == DW_TAG_structure_type)
16116 {
67607e24 16117 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16118 }
16119 else if (die->tag == DW_TAG_union_type)
16120 {
67607e24 16121 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16122 }
16123 else
16124 {
67607e24 16125 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16126 }
16127
0cc2414c
TT
16128 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16129 TYPE_DECLARED_CLASS (type) = 1;
16130
e35000a7
TBA
16131 /* Store the calling convention in the type if it's available in
16132 the die. Otherwise the calling convention remains set to
16133 the default value DW_CC_normal. */
16134 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16135 if (attr != nullptr
529908cb 16136 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16137 {
16138 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16139 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16140 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16141 }
16142
e142c38c 16143 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16144 if (attr != nullptr)
c906108c 16145 {
cd6c91b4 16146 if (attr->form_is_constant ())
dda83cd7 16147 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16148 else
16149 {
f8e89861 16150 struct dynamic_prop prop;
293e7e51 16151 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16152 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16153 TYPE_LENGTH (type) = 0;
155bfbd3 16154 }
c906108c
SS
16155 }
16156 else
16157 {
16158 TYPE_LENGTH (type) = 0;
16159 }
16160
2b4424c3
TT
16161 maybe_set_alignment (cu, die, type);
16162
5230b05a 16163 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16164 {
5230b05a
WT
16165 /* ICC<14 does not output the required DW_AT_declaration on
16166 incomplete types, but gives them a size of zero. */
b4b73759 16167 type->set_is_stub (true);
685b1105
JK
16168 }
16169 else
9baccff6 16170 type->set_stub_is_supported (true);
685b1105 16171
dc718098 16172 if (die_is_declaration (die, cu))
b4b73759 16173 type->set_is_stub (true);
a6c727b2
DJ
16174 else if (attr == NULL && die->child == NULL
16175 && producer_is_realview (cu->producer))
16176 /* RealView does not output the required DW_AT_declaration
16177 on incomplete types. */
b4b73759 16178 type->set_is_stub (true);
dc718098 16179
c906108c
SS
16180 /* We need to add the type field to the die immediately so we don't
16181 infinitely recurse when dealing with pointers to the structure
0963b4bd 16182 type within the structure itself. */
1c379e20 16183 set_die_type (die, type, cu);
c906108c 16184
7e314c57
JK
16185 /* set_die_type should be already done. */
16186 set_descriptive_type (type, die, cu);
16187
c767944b
DJ
16188 return type;
16189}
16190
9c6a1327
TT
16191static void handle_struct_member_die
16192 (struct die_info *child_die,
16193 struct type *type,
16194 struct field_info *fi,
16195 std::vector<struct symbol *> *template_args,
16196 struct dwarf2_cu *cu);
16197
16198/* A helper for handle_struct_member_die that handles
16199 DW_TAG_variant_part. */
16200
16201static void
16202handle_variant_part (struct die_info *die, struct type *type,
16203 struct field_info *fi,
16204 std::vector<struct symbol *> *template_args,
16205 struct dwarf2_cu *cu)
16206{
16207 variant_part_builder *new_part;
16208 if (fi->current_variant_part == nullptr)
16209 {
16210 fi->variant_parts.emplace_back ();
16211 new_part = &fi->variant_parts.back ();
16212 }
16213 else if (!fi->current_variant_part->processing_variant)
16214 {
16215 complaint (_("nested DW_TAG_variant_part seen "
16216 "- DIE at %s [in module %s]"),
16217 sect_offset_str (die->sect_off),
5e22e966 16218 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16219 return;
16220 }
16221 else
16222 {
16223 variant_field &current = fi->current_variant_part->variants.back ();
16224 current.variant_parts.emplace_back ();
16225 new_part = &current.variant_parts.back ();
16226 }
16227
16228 /* When we recurse, we want callees to add to this new variant
16229 part. */
16230 scoped_restore save_current_variant_part
16231 = make_scoped_restore (&fi->current_variant_part, new_part);
16232
16233 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16234 if (discr == NULL)
16235 {
16236 /* It's a univariant form, an extension we support. */
16237 }
16238 else if (discr->form_is_ref ())
16239 {
16240 struct dwarf2_cu *target_cu = cu;
16241 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16242
16243 new_part->discriminant_offset = target_die->sect_off;
16244 }
16245 else
16246 {
16247 complaint (_("DW_AT_discr does not have DIE reference form"
16248 " - DIE at %s [in module %s]"),
16249 sect_offset_str (die->sect_off),
5e22e966 16250 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16251 }
16252
16253 for (die_info *child_die = die->child;
16254 child_die != NULL;
16255 child_die = child_die->sibling)
16256 handle_struct_member_die (child_die, type, fi, template_args, cu);
16257}
16258
16259/* A helper for handle_struct_member_die that handles
16260 DW_TAG_variant. */
16261
16262static void
16263handle_variant (struct die_info *die, struct type *type,
16264 struct field_info *fi,
16265 std::vector<struct symbol *> *template_args,
16266 struct dwarf2_cu *cu)
16267{
16268 if (fi->current_variant_part == nullptr)
16269 {
16270 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16271 "- DIE at %s [in module %s]"),
16272 sect_offset_str (die->sect_off),
5e22e966 16273 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16274 return;
16275 }
16276 if (fi->current_variant_part->processing_variant)
16277 {
16278 complaint (_("nested DW_TAG_variant seen "
16279 "- DIE at %s [in module %s]"),
16280 sect_offset_str (die->sect_off),
5e22e966 16281 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16282 return;
16283 }
16284
16285 scoped_restore save_processing_variant
16286 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16287 true);
16288
16289 fi->current_variant_part->variants.emplace_back ();
16290 variant_field &variant = fi->current_variant_part->variants.back ();
16291 variant.first_field = fi->fields.size ();
16292
16293 /* In a variant we want to get the discriminant and also add a
16294 field for our sole member child. */
16295 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16296 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16297 {
16298 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16299 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16300 variant.default_branch = true;
16301 else
9d2246fc 16302 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16303 }
16304 else
cae21f8e 16305 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16306
16307 for (die_info *variant_child = die->child;
16308 variant_child != NULL;
16309 variant_child = variant_child->sibling)
16310 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16311
16312 variant.last_field = fi->fields.size ();
16313}
16314
2ddeaf8a
TT
16315/* A helper for process_structure_scope that handles a single member
16316 DIE. */
16317
16318static void
16319handle_struct_member_die (struct die_info *child_die, struct type *type,
16320 struct field_info *fi,
16321 std::vector<struct symbol *> *template_args,
16322 struct dwarf2_cu *cu)
16323{
16324 if (child_die->tag == DW_TAG_member
9c6a1327 16325 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16326 {
16327 /* NOTE: carlton/2002-11-05: A C++ static data member
16328 should be a DW_TAG_member that is a declaration, but
16329 all versions of G++ as of this writing (so through at
16330 least 3.2.1) incorrectly generate DW_TAG_variable
16331 tags for them instead. */
16332 dwarf2_add_field (fi, child_die, cu);
16333 }
16334 else if (child_die->tag == DW_TAG_subprogram)
16335 {
16336 /* Rust doesn't have member functions in the C++ sense.
16337 However, it does emit ordinary functions as children
16338 of a struct DIE. */
16339 if (cu->language == language_rust)
16340 read_func_scope (child_die, cu);
16341 else
16342 {
16343 /* C++ member function. */
16344 dwarf2_add_member_fn (fi, child_die, type, cu);
16345 }
16346 }
16347 else if (child_die->tag == DW_TAG_inheritance)
16348 {
16349 /* C++ base class field. */
16350 dwarf2_add_field (fi, child_die, cu);
16351 }
16352 else if (type_can_define_types (child_die))
16353 dwarf2_add_type_defn (fi, child_die, cu);
16354 else if (child_die->tag == DW_TAG_template_type_param
16355 || child_die->tag == DW_TAG_template_value_param)
16356 {
16357 struct symbol *arg = new_symbol (child_die, NULL, cu);
16358
16359 if (arg != NULL)
16360 template_args->push_back (arg);
16361 }
9c6a1327
TT
16362 else if (child_die->tag == DW_TAG_variant_part)
16363 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16364 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16365 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16366}
16367
c767944b
DJ
16368/* Finish creating a structure or union type, including filling in
16369 its members and creating a symbol for it. */
16370
16371static void
16372process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16373{
5e22e966 16374 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16375 struct die_info *child_die;
c767944b
DJ
16376 struct type *type;
16377
16378 type = get_die_type (die, cu);
16379 if (type == NULL)
16380 type = read_structure_type (die, cu);
16381
3e1d3d8c 16382 bool has_template_parameters = false;
e142c38c 16383 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16384 {
16385 struct field_info fi;
2f4732b0 16386 std::vector<struct symbol *> template_args;
c906108c 16387
639d11d3 16388 child_die = die->child;
c906108c
SS
16389
16390 while (child_die && child_die->tag)
16391 {
2ddeaf8a 16392 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16393 child_die = child_die->sibling;
c906108c
SS
16394 }
16395
34eaf542 16396 /* Attach template arguments to type. */
2f4732b0 16397 if (!template_args.empty ())
34eaf542 16398 {
3e1d3d8c 16399 has_template_parameters = true;
34eaf542 16400 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16401 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16402 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16403 = XOBNEWVEC (&objfile->objfile_obstack,
16404 struct symbol *,
16405 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16406 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16407 template_args.data (),
34eaf542
TT
16408 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16409 * sizeof (struct symbol *)));
34eaf542
TT
16410 }
16411
c906108c 16412 /* Attach fields and member functions to the type. */
317f7127 16413 if (fi.nfields () > 0)
e7c27a73 16414 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16415 if (!fi.fnfieldlists.empty ())
c906108c 16416 {
e7c27a73 16417 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16418
c5aa993b 16419 /* Get the type which refers to the base class (possibly this
c906108c 16420 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16421 class from the DW_AT_containing_type attribute. This use of
16422 DW_AT_containing_type is a GNU extension. */
c906108c 16423
e142c38c 16424 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16425 {
e7c27a73 16426 struct type *t = die_containing_type (die, cu);
c906108c 16427
ae6ae975 16428 set_type_vptr_basetype (type, t);
c906108c
SS
16429 if (type == t)
16430 {
c906108c
SS
16431 int i;
16432
16433 /* Our own class provides vtbl ptr. */
1f704f76 16434 for (i = t->num_fields () - 1;
c906108c
SS
16435 i >= TYPE_N_BASECLASSES (t);
16436 --i)
16437 {
0d5cff50 16438 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16439
dda83cd7 16440 if (is_vtable_name (fieldname, cu))
c906108c 16441 {
ae6ae975 16442 set_type_vptr_fieldno (type, i);
c906108c
SS
16443 break;
16444 }
16445 }
16446
16447 /* Complain if virtual function table field not found. */
16448 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16449 complaint (_("virtual function table pointer "
3e43a32a 16450 "not found when defining class '%s'"),
7d93a1e0 16451 type->name () ? type->name () : "");
c906108c
SS
16452 }
16453 else
16454 {
ae6ae975 16455 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16456 }
16457 }
f6235d4c 16458 else if (cu->producer
61012eef 16459 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16460 {
16461 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16462 of the containing type, but the vtable pointer is
16463 always named __vfp. */
f6235d4c
EZ
16464
16465 int i;
16466
1f704f76 16467 for (i = type->num_fields () - 1;
f6235d4c
EZ
16468 i >= TYPE_N_BASECLASSES (type);
16469 --i)
16470 {
16471 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16472 {
ae6ae975
DE
16473 set_type_vptr_fieldno (type, i);
16474 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16475 break;
16476 }
16477 }
16478 }
c906108c 16479 }
98751a41
JK
16480
16481 /* Copy fi.typedef_field_list linked list elements content into the
16482 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16483 if (!fi.typedef_field_list.empty ())
98751a41 16484 {
be2daae6 16485 int count = fi.typedef_field_list.size ();
98751a41 16486
a0d7a4ff 16487 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16488 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16489 = ((struct decl_field *)
be2daae6
TT
16490 TYPE_ALLOC (type,
16491 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16492 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16493
be2daae6
TT
16494 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16495 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16496 }
c767944b 16497
883fd55a
KS
16498 /* Copy fi.nested_types_list linked list elements content into the
16499 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16500 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16501 {
be2daae6 16502 int count = fi.nested_types_list.size ();
883fd55a
KS
16503
16504 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16505 TYPE_NESTED_TYPES_ARRAY (type)
16506 = ((struct decl_field *)
be2daae6
TT
16507 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16508 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16509
be2daae6
TT
16510 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16511 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16512 }
c906108c 16513 }
63d06c5c 16514
bb5ed363 16515 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16516 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16517 cu->rust_unions.push_back (type);
57567375
TT
16518 else if (cu->language == language_ada)
16519 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16520
90aeadfc
DC
16521 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16522 snapshots) has been known to create a die giving a declaration
16523 for a class that has, as a child, a die giving a definition for a
16524 nested class. So we have to process our children even if the
16525 current die is a declaration. Normally, of course, a declaration
16526 won't have any children at all. */
134d01f1 16527
ca040673
DE
16528 child_die = die->child;
16529
90aeadfc
DC
16530 while (child_die != NULL && child_die->tag)
16531 {
16532 if (child_die->tag == DW_TAG_member
16533 || child_die->tag == DW_TAG_variable
34eaf542
TT
16534 || child_die->tag == DW_TAG_inheritance
16535 || child_die->tag == DW_TAG_template_value_param
16536 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16537 {
90aeadfc 16538 /* Do nothing. */
134d01f1 16539 }
90aeadfc
DC
16540 else
16541 process_die (child_die, cu);
134d01f1 16542
436c571c 16543 child_die = child_die->sibling;
134d01f1
DJ
16544 }
16545
fa4028e9
JB
16546 /* Do not consider external references. According to the DWARF standard,
16547 these DIEs are identified by the fact that they have no byte_size
16548 attribute, and a declaration attribute. */
16549 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16550 || !die_is_declaration (die, cu)
16551 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16552 {
16553 struct symbol *sym = new_symbol (die, type, cu);
16554
16555 if (has_template_parameters)
16556 {
a776957c
TT
16557 struct symtab *symtab;
16558 if (sym != nullptr)
16559 symtab = symbol_symtab (sym);
16560 else if (cu->line_header != nullptr)
16561 {
16562 /* Any related symtab will do. */
16563 symtab
7ba99d21 16564 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16565 }
16566 else
16567 {
16568 symtab = nullptr;
16569 complaint (_("could not find suitable "
16570 "symtab for template parameter"
16571 " - DIE at %s [in module %s]"),
16572 sect_offset_str (die->sect_off),
16573 objfile_name (objfile));
16574 }
16575
16576 if (symtab != nullptr)
16577 {
16578 /* Make sure that the symtab is set on the new symbols.
16579 Even though they don't appear in this symtab directly,
16580 other parts of gdb assume that symbols do, and this is
16581 reasonably true. */
16582 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16583 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16584 }
3e1d3d8c
TT
16585 }
16586 }
134d01f1
DJ
16587}
16588
ed6acedd
TT
16589/* Assuming DIE is an enumeration type, and TYPE is its associated
16590 type, update TYPE using some information only available in DIE's
16591 children. In particular, the fields are computed. */
55426c9d
JB
16592
16593static void
16594update_enumeration_type_from_children (struct die_info *die,
16595 struct type *type,
16596 struct dwarf2_cu *cu)
16597{
60f7655a 16598 struct die_info *child_die;
55426c9d
JB
16599 int unsigned_enum = 1;
16600 int flag_enum = 1;
55426c9d 16601
8268c778 16602 auto_obstack obstack;
ed6acedd 16603 std::vector<struct field> fields;
55426c9d 16604
60f7655a
DE
16605 for (child_die = die->child;
16606 child_die != NULL && child_die->tag;
436c571c 16607 child_die = child_die->sibling)
55426c9d
JB
16608 {
16609 struct attribute *attr;
16610 LONGEST value;
16611 const gdb_byte *bytes;
16612 struct dwarf2_locexpr_baton *baton;
16613 const char *name;
60f7655a 16614
55426c9d
JB
16615 if (child_die->tag != DW_TAG_enumerator)
16616 continue;
16617
16618 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16619 if (attr == NULL)
16620 continue;
16621
16622 name = dwarf2_name (child_die, cu);
16623 if (name == NULL)
16624 name = "<anonymous enumerator>";
16625
16626 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16627 &value, &bytes, &baton);
16628 if (value < 0)
16629 {
16630 unsigned_enum = 0;
16631 flag_enum = 0;
16632 }
55426c9d 16633 else
edd45eb0
SM
16634 {
16635 if (count_one_bits_ll (value) >= 2)
16636 flag_enum = 0;
edd45eb0 16637 }
55426c9d 16638
ed6acedd
TT
16639 fields.emplace_back ();
16640 struct field &field = fields.back ();
16641 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16642 SET_FIELD_ENUMVAL (field, value);
16643 }
16644
16645 if (!fields.empty ())
16646 {
5e33d5f4 16647 type->set_num_fields (fields.size ());
3cabb6b0
SM
16648 type->set_fields
16649 ((struct field *)
16650 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16651 memcpy (type->fields (), fields.data (),
ed6acedd 16652 sizeof (struct field) * fields.size ());
55426c9d
JB
16653 }
16654
16655 if (unsigned_enum)
653223d3
SM
16656 type->set_is_unsigned (true);
16657
55426c9d
JB
16658 if (flag_enum)
16659 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16660}
16661
134d01f1
DJ
16662/* Given a DW_AT_enumeration_type die, set its type. We do not
16663 complete the type's fields yet, or create any symbols. */
c906108c 16664
f792889a 16665static struct type *
134d01f1 16666read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16667{
5e22e966 16668 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16669 struct type *type;
c906108c 16670 struct attribute *attr;
0114d602 16671 const char *name;
134d01f1 16672
348e048f
DE
16673 /* If the definition of this type lives in .debug_types, read that type.
16674 Don't follow DW_AT_specification though, that will take us back up
16675 the chain and we want to go down. */
052c8bb8 16676 attr = die->attr (DW_AT_signature);
435d3d88 16677 if (attr != nullptr)
348e048f 16678 {
ac9ec31b 16679 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16680
ac9ec31b 16681 /* The type's CU may not be the same as CU.
02142a6c 16682 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16683 return set_die_type (die, type, cu);
16684 }
16685
c906108c
SS
16686 type = alloc_type (objfile);
16687
67607e24 16688 type->set_code (TYPE_CODE_ENUM);
94af9270 16689 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16690 if (name != NULL)
d0e39ea2 16691 type->set_name (name);
c906108c 16692
0626fc76
TT
16693 attr = dwarf2_attr (die, DW_AT_type, cu);
16694 if (attr != NULL)
16695 {
16696 struct type *underlying_type = die_type (die, cu);
16697
16698 TYPE_TARGET_TYPE (type) = underlying_type;
16699 }
16700
e142c38c 16701 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16702 if (attr != nullptr)
c906108c 16703 {
529908cb 16704 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16705 }
16706 else
16707 {
16708 TYPE_LENGTH (type) = 0;
16709 }
16710
2b4424c3
TT
16711 maybe_set_alignment (cu, die, type);
16712
137033e9
JB
16713 /* The enumeration DIE can be incomplete. In Ada, any type can be
16714 declared as private in the package spec, and then defined only
16715 inside the package body. Such types are known as Taft Amendment
16716 Types. When another package uses such a type, an incomplete DIE
16717 may be generated by the compiler. */
02eb380e 16718 if (die_is_declaration (die, cu))
b4b73759 16719 type->set_is_stub (true);
02eb380e 16720
0626fc76
TT
16721 /* If this type has an underlying type that is not a stub, then we
16722 may use its attributes. We always use the "unsigned" attribute
16723 in this situation, because ordinarily we guess whether the type
16724 is unsigned -- but the guess can be wrong and the underlying type
16725 can tell us the reality. However, we defer to a local size
16726 attribute if one exists, because this lets the compiler override
16727 the underlying type if needed. */
e46d3488 16728 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16729 {
9e7c9a03
HD
16730 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16731 underlying_type = check_typedef (underlying_type);
653223d3
SM
16732
16733 type->set_is_unsigned (underlying_type->is_unsigned ());
16734
0626fc76 16735 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16736 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16737
2b4424c3 16738 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16739 && TYPE_RAW_ALIGN (underlying_type) != 0)
16740 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16741 }
16742
3d567982
TT
16743 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16744
ed6acedd
TT
16745 set_die_type (die, type, cu);
16746
16747 /* Finish the creation of this type by using the enum's children.
16748 Note that, as usual, this must come after set_die_type to avoid
16749 infinite recursion when trying to compute the names of the
16750 enumerators. */
16751 update_enumeration_type_from_children (die, type, cu);
16752
16753 return type;
134d01f1
DJ
16754}
16755
16756/* Given a pointer to a die which begins an enumeration, process all
16757 the dies that define the members of the enumeration, and create the
16758 symbol for the enumeration type.
16759
16760 NOTE: We reverse the order of the element list. */
16761
16762static void
16763process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16764{
f792889a 16765 struct type *this_type;
134d01f1 16766
f792889a
DJ
16767 this_type = get_die_type (die, cu);
16768 if (this_type == NULL)
16769 this_type = read_enumeration_type (die, cu);
9dc481d3 16770
639d11d3 16771 if (die->child != NULL)
c906108c 16772 {
9dc481d3 16773 struct die_info *child_die;
15d034d0 16774 const char *name;
9dc481d3 16775
639d11d3 16776 child_die = die->child;
c906108c
SS
16777 while (child_die && child_die->tag)
16778 {
16779 if (child_die->tag != DW_TAG_enumerator)
16780 {
e7c27a73 16781 process_die (child_die, cu);
c906108c
SS
16782 }
16783 else
16784 {
39cbfefa
DJ
16785 name = dwarf2_name (child_die, cu);
16786 if (name)
ed6acedd 16787 new_symbol (child_die, this_type, cu);
c906108c
SS
16788 }
16789
436c571c 16790 child_die = child_die->sibling;
c906108c 16791 }
c906108c 16792 }
134d01f1 16793
6c83ed52
TT
16794 /* If we are reading an enum from a .debug_types unit, and the enum
16795 is a declaration, and the enum is not the signatured type in the
16796 unit, then we do not want to add a symbol for it. Adding a
16797 symbol would in some cases obscure the true definition of the
16798 enum, giving users an incomplete type when the definition is
16799 actually available. Note that we do not want to do this for all
16800 enums which are just declarations, because C++0x allows forward
16801 enum declarations. */
3019eac3 16802 if (cu->per_cu->is_debug_types
6c83ed52
TT
16803 && die_is_declaration (die, cu))
16804 {
52dc124a 16805 struct signatured_type *sig_type;
6c83ed52 16806
c0f78cd4 16807 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16808 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16809 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16810 return;
16811 }
16812
f792889a 16813 new_symbol (die, this_type, cu);
c906108c
SS
16814}
16815
57567375
TT
16816/* Helper function for quirk_ada_thick_pointer that examines a bounds
16817 expression for an index type and finds the corresponding field
16818 offset in the hidden "P_BOUNDS" structure. Returns true on success
16819 and updates *FIELD, false if it fails to recognize an
16820 expression. */
16821
16822static bool
16823recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16824 int *bounds_offset, struct field *field,
16825 struct dwarf2_cu *cu)
16826{
16827 struct attribute *attr = dwarf2_attr (die, name, cu);
16828 if (attr == nullptr || !attr->form_is_block ())
16829 return false;
16830
16831 const struct dwarf_block *block = attr->as_block ();
16832 const gdb_byte *start = block->data;
16833 const gdb_byte *end = block->data + block->size;
16834
16835 /* The expression to recognize generally looks like:
16836
16837 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16838 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16839
16840 However, the second "plus_uconst" may be missing:
16841
16842 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16843 DW_OP_deref_size: 4)
16844
16845 This happens when the field is at the start of the structure.
16846
16847 Also, the final deref may not be sized:
16848
16849 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16850 DW_OP_deref)
16851
16852 This happens when the size of the index type happens to be the
16853 same as the architecture's word size. This can occur with or
16854 without the second plus_uconst. */
16855
16856 if (end - start < 2)
16857 return false;
16858 if (*start++ != DW_OP_push_object_address)
16859 return false;
16860 if (*start++ != DW_OP_plus_uconst)
16861 return false;
16862
16863 uint64_t this_bound_off;
16864 start = gdb_read_uleb128 (start, end, &this_bound_off);
16865 if (start == nullptr || (int) this_bound_off != this_bound_off)
16866 return false;
16867 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16868 is consistent among all bounds. */
16869 if (*bounds_offset == -1)
16870 *bounds_offset = this_bound_off;
16871 else if (*bounds_offset != this_bound_off)
16872 return false;
16873
16874 if (start == end || *start++ != DW_OP_deref)
16875 return false;
16876
16877 int offset = 0;
16878 if (start ==end)
16879 return false;
16880 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16881 {
16882 /* This means an offset of 0. */
16883 }
16884 else if (*start++ != DW_OP_plus_uconst)
16885 return false;
16886 else
16887 {
16888 /* The size is the parameter to DW_OP_plus_uconst. */
16889 uint64_t val;
16890 start = gdb_read_uleb128 (start, end, &val);
16891 if (start == nullptr)
16892 return false;
16893 if ((int) val != val)
16894 return false;
16895 offset = val;
16896 }
16897
16898 if (start == end)
16899 return false;
16900
16901 uint64_t size;
16902 if (*start == DW_OP_deref_size)
16903 {
16904 start = gdb_read_uleb128 (start + 1, end, &size);
16905 if (start == nullptr)
16906 return false;
16907 }
16908 else if (*start == DW_OP_deref)
16909 {
16910 size = cu->header.addr_size;
16911 ++start;
16912 }
16913 else
16914 return false;
16915
16916 SET_FIELD_BITPOS (*field, 8 * offset);
16917 if (size != TYPE_LENGTH (field->type ()))
16918 FIELD_BITSIZE (*field) = 8 * size;
16919
16920 return true;
16921}
16922
16923/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16924 some kinds of Ada arrays:
16925
16926 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16927 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16928 <11e0> DW_AT_data_location: 2 byte block: 97 6
16929 (DW_OP_push_object_address; DW_OP_deref)
16930 <11e3> DW_AT_type : <0x1173>
16931 <11e7> DW_AT_sibling : <0x1201>
16932 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16933 <11ec> DW_AT_type : <0x1206>
16934 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16935 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16936 DW_OP_deref_size: 4)
16937 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16938 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16939 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16940
16941 This actually represents a "thick pointer", which is a structure
16942 with two elements: one that is a pointer to the array data, and one
16943 that is a pointer to another structure; this second structure holds
16944 the array bounds.
16945
16946 This returns a new type on success, or nullptr if this didn't
16947 recognize the type. */
16948
16949static struct type *
16950quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16951 struct type *type)
16952{
16953 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16954 /* So far we've only seen this with block form. */
16955 if (attr == nullptr || !attr->form_is_block ())
16956 return nullptr;
16957
16958 /* Note that this will fail if the structure layout is changed by
16959 the compiler. However, we have no good way to recognize some
16960 other layout, because we don't know what expression the compiler
16961 might choose to emit should this happen. */
16962 struct dwarf_block *blk = attr->as_block ();
16963 if (blk->size != 2
16964 || blk->data[0] != DW_OP_push_object_address
16965 || blk->data[1] != DW_OP_deref)
16966 return nullptr;
16967
16968 int bounds_offset = -1;
16969 int max_align = -1;
16970 std::vector<struct field> range_fields;
16971 for (struct die_info *child_die = die->child;
16972 child_die;
16973 child_die = child_die->sibling)
16974 {
16975 if (child_die->tag == DW_TAG_subrange_type)
16976 {
16977 struct type *underlying = read_subrange_index_type (child_die, cu);
16978
16979 int this_align = type_align (underlying);
16980 if (this_align > max_align)
16981 max_align = this_align;
16982
16983 range_fields.emplace_back ();
16984 range_fields.emplace_back ();
16985
16986 struct field &lower = range_fields[range_fields.size () - 2];
16987 struct field &upper = range_fields[range_fields.size () - 1];
16988
16989 lower.set_type (underlying);
16990 FIELD_ARTIFICIAL (lower) = 1;
16991
16992 upper.set_type (underlying);
16993 FIELD_ARTIFICIAL (upper) = 1;
16994
16995 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
16996 &bounds_offset, &lower, cu)
16997 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
16998 &bounds_offset, &upper, cu))
16999 return nullptr;
17000 }
17001 }
17002
17003 /* This shouldn't really happen, but double-check that we found
17004 where the bounds are stored. */
17005 if (bounds_offset == -1)
17006 return nullptr;
17007
17008 struct objfile *objfile = cu->per_objfile->objfile;
17009 for (int i = 0; i < range_fields.size (); i += 2)
17010 {
17011 char name[20];
17012
17013 /* Set the name of each field in the bounds. */
17014 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17015 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17016 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17017 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17018 }
17019
17020 struct type *bounds = alloc_type (objfile);
17021 bounds->set_code (TYPE_CODE_STRUCT);
17022
17023 bounds->set_num_fields (range_fields.size ());
17024 bounds->set_fields
17025 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17026 * sizeof (struct field))));
17027 memcpy (bounds->fields (), range_fields.data (),
17028 bounds->num_fields () * sizeof (struct field));
17029
17030 int last_fieldno = range_fields.size () - 1;
17031 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17032 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17033 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17034
17035 /* Rewrite the existing array type in place. Specifically, we
17036 remove any dynamic properties we might have read, and we replace
17037 the index types. */
17038 struct type *iter = type;
17039 for (int i = 0; i < range_fields.size (); i += 2)
17040 {
17041 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17042 iter->main_type->dyn_prop_list = nullptr;
17043 iter->set_index_type
17044 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17045 iter = TYPE_TARGET_TYPE (iter);
17046 }
17047
17048 struct type *result = alloc_type (objfile);
17049 result->set_code (TYPE_CODE_STRUCT);
17050
17051 result->set_num_fields (2);
17052 result->set_fields
17053 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17054 * sizeof (struct field))));
17055
17056 /* The names are chosen to coincide with what the compiler does with
17057 -fgnat-encodings=all, which the Ada code in gdb already
17058 understands. */
17059 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17060 result->field (0).set_type (lookup_pointer_type (type));
17061
17062 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17063 result->field (1).set_type (lookup_pointer_type (bounds));
17064 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17065
17066 result->set_name (type->name ());
17067 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17068 + TYPE_LENGTH (result->field (1).type ()));
17069
17070 return result;
17071}
17072
c906108c
SS
17073/* Extract all information from a DW_TAG_array_type DIE and put it in
17074 the DIE's type field. For now, this only handles one dimensional
17075 arrays. */
17076
f792889a 17077static struct type *
e7c27a73 17078read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17079{
5e22e966 17080 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17081 struct die_info *child_die;
7e314c57 17082 struct type *type;
c906108c 17083 struct type *element_type, *range_type, *index_type;
c906108c 17084 struct attribute *attr;
15d034d0 17085 const char *name;
a405673c 17086 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17087 unsigned int bit_stride = 0;
c906108c 17088
e7c27a73 17089 element_type = die_type (die, cu);
c906108c 17090
7e314c57
JK
17091 /* The die_type call above may have already set the type for this DIE. */
17092 type = get_die_type (die, cu);
17093 if (type)
17094 return type;
17095
dc53a7ad
JB
17096 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17097 if (attr != NULL)
a405673c
JB
17098 {
17099 int stride_ok;
293e7e51 17100 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17101
17102 byte_stride_prop
17103 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17104 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17105 prop_type);
a405673c
JB
17106 if (!stride_ok)
17107 {
b98664d3 17108 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17109 " - DIE at %s [in module %s]"),
17110 sect_offset_str (die->sect_off),
5e22e966 17111 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17112 /* Ignore this attribute. We will likely not be able to print
17113 arrays of this type correctly, but there is little we can do
17114 to help if we cannot read the attribute's value. */
17115 byte_stride_prop = NULL;
17116 }
17117 }
dc53a7ad
JB
17118
17119 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17120 if (attr != NULL)
529908cb 17121 bit_stride = attr->constant_value (0);
dc53a7ad 17122
c906108c
SS
17123 /* Irix 6.2 native cc creates array types without children for
17124 arrays with unspecified length. */
639d11d3 17125 if (die->child == NULL)
c906108c 17126 {
46bf5051 17127 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17128 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17129 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17130 byte_stride_prop, bit_stride);
f792889a 17131 return set_die_type (die, type, cu);
c906108c
SS
17132 }
17133
791afaa2 17134 std::vector<struct type *> range_types;
639d11d3 17135 child_die = die->child;
c906108c
SS
17136 while (child_die && child_die->tag)
17137 {
17138 if (child_die->tag == DW_TAG_subrange_type)
17139 {
f792889a 17140 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17141
dda83cd7
SM
17142 if (child_type != NULL)
17143 {
0963b4bd 17144 /* The range type was succesfully read. Save it for the
dda83cd7 17145 array type creation. */
791afaa2 17146 range_types.push_back (child_type);
dda83cd7 17147 }
c906108c 17148 }
436c571c 17149 child_die = child_die->sibling;
c906108c
SS
17150 }
17151
17152 /* Dwarf2 dimensions are output from left to right, create the
17153 necessary array types in backwards order. */
7ca2d3a3 17154
c906108c 17155 type = element_type;
7ca2d3a3
DL
17156
17157 if (read_array_order (die, cu) == DW_ORD_col_major)
17158 {
17159 int i = 0;
9a619af0 17160
791afaa2 17161 while (i < range_types.size ())
10f6a3ad
TT
17162 {
17163 type = create_array_type_with_stride (NULL, type, range_types[i++],
17164 byte_stride_prop, bit_stride);
17165 bit_stride = 0;
17166 byte_stride_prop = nullptr;
17167 }
7ca2d3a3
DL
17168 }
17169 else
17170 {
791afaa2 17171 size_t ndim = range_types.size ();
7ca2d3a3 17172 while (ndim-- > 0)
10f6a3ad
TT
17173 {
17174 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17175 byte_stride_prop, bit_stride);
17176 bit_stride = 0;
17177 byte_stride_prop = nullptr;
17178 }
7ca2d3a3 17179 }
c906108c 17180
f5f8a009
EZ
17181 /* Understand Dwarf2 support for vector types (like they occur on
17182 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17183 array type. This is not part of the Dwarf2/3 standard yet, but a
17184 custom vendor extension. The main difference between a regular
17185 array and the vector variant is that vectors are passed by value
17186 to functions. */
e142c38c 17187 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17188 if (attr != nullptr)
ea37ba09 17189 make_vector_type (type);
f5f8a009 17190
dbc98a8b
KW
17191 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17192 implementation may choose to implement triple vectors using this
17193 attribute. */
17194 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17195 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17196 {
529908cb
TT
17197 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17198 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17199 else
b98664d3 17200 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17201 "than the total size of elements"));
dbc98a8b
KW
17202 }
17203
39cbfefa
DJ
17204 name = dwarf2_name (die, cu);
17205 if (name)
d0e39ea2 17206 type->set_name (name);
6e70227d 17207
2b4424c3
TT
17208 maybe_set_alignment (cu, die, type);
17209
57567375
TT
17210 struct type *replacement_type = nullptr;
17211 if (cu->language == language_ada)
17212 {
17213 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17214 if (replacement_type != nullptr)
17215 type = replacement_type;
17216 }
17217
0963b4bd 17218 /* Install the type in the die. */
57567375 17219 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17220
17221 /* set_die_type should be already done. */
b4ba55a1
JB
17222 set_descriptive_type (type, die, cu);
17223
7e314c57 17224 return type;
c906108c
SS
17225}
17226
7ca2d3a3 17227static enum dwarf_array_dim_ordering
6e70227d 17228read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17229{
17230 struct attribute *attr;
17231
17232 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17233
435d3d88 17234 if (attr != nullptr)
1bc397c5
TT
17235 {
17236 LONGEST val = attr->constant_value (-1);
17237 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17238 return (enum dwarf_array_dim_ordering) val;
17239 }
7ca2d3a3 17240
0963b4bd
MS
17241 /* GNU F77 is a special case, as at 08/2004 array type info is the
17242 opposite order to the dwarf2 specification, but data is still
17243 laid out as per normal fortran.
7ca2d3a3 17244
0963b4bd
MS
17245 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17246 version checking. */
7ca2d3a3 17247
905e0470
PM
17248 if (cu->language == language_fortran
17249 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17250 {
17251 return DW_ORD_row_major;
17252 }
17253
3a3440fb 17254 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17255 {
17256 case array_column_major:
17257 return DW_ORD_col_major;
17258 case array_row_major:
17259 default:
17260 return DW_ORD_row_major;
17261 };
17262}
17263
72019c9c 17264/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17265 the DIE's type field. */
72019c9c 17266
f792889a 17267static struct type *
72019c9c
GM
17268read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17269{
7e314c57
JK
17270 struct type *domain_type, *set_type;
17271 struct attribute *attr;
f792889a 17272
7e314c57
JK
17273 domain_type = die_type (die, cu);
17274
17275 /* The die_type call above may have already set the type for this DIE. */
17276 set_type = get_die_type (die, cu);
17277 if (set_type)
17278 return set_type;
17279
17280 set_type = create_set_type (NULL, domain_type);
17281
17282 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17283 if (attr != nullptr && attr->form_is_unsigned ())
17284 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17285
2b4424c3
TT
17286 maybe_set_alignment (cu, die, set_type);
17287
f792889a 17288 return set_die_type (die, set_type, cu);
72019c9c 17289}
7ca2d3a3 17290
0971de02
TT
17291/* A helper for read_common_block that creates a locexpr baton.
17292 SYM is the symbol which we are marking as computed.
17293 COMMON_DIE is the DIE for the common block.
17294 COMMON_LOC is the location expression attribute for the common
17295 block itself.
17296 MEMBER_LOC is the location expression attribute for the particular
17297 member of the common block that we are processing.
17298 CU is the CU from which the above come. */
17299
17300static void
17301mark_common_block_symbol_computed (struct symbol *sym,
17302 struct die_info *common_die,
17303 struct attribute *common_loc,
17304 struct attribute *member_loc,
17305 struct dwarf2_cu *cu)
17306{
5e22e966 17307 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17308 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17309 struct dwarf2_locexpr_baton *baton;
17310 gdb_byte *ptr;
17311 unsigned int cu_off;
08feed99 17312 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17313 LONGEST offset = 0;
17314
17315 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17316 gdb_assert (common_loc->form_is_block ());
17317 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17318 || member_loc->form_is_constant ());
0971de02 17319
8d749320 17320 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17321 baton->per_objfile = per_objfile;
0971de02
TT
17322 baton->per_cu = cu->per_cu;
17323 gdb_assert (baton->per_cu);
17324
17325 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17326
cd6c91b4 17327 if (member_loc->form_is_constant ())
0971de02 17328 {
0826b30a 17329 offset = member_loc->constant_value (0);
0971de02
TT
17330 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17331 }
17332 else
9d2246fc 17333 baton->size += member_loc->as_block ()->size;
0971de02 17334
224c3ddb 17335 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17336 baton->data = ptr;
17337
17338 *ptr++ = DW_OP_call4;
9c541725 17339 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17340 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17341 ptr += 4;
17342
cd6c91b4 17343 if (member_loc->form_is_constant ())
0971de02
TT
17344 {
17345 *ptr++ = DW_OP_addr;
17346 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17347 ptr += cu->header.addr_size;
17348 }
17349 else
17350 {
17351 /* We have to copy the data here, because DW_OP_call4 will only
17352 use a DW_AT_location attribute. */
9d2246fc
TT
17353 struct dwarf_block *block = member_loc->as_block ();
17354 memcpy (ptr, block->data, block->size);
17355 ptr += block->size;
0971de02
TT
17356 }
17357
17358 *ptr++ = DW_OP_plus;
17359 gdb_assert (ptr - baton->data == baton->size);
17360
0971de02 17361 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17362 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17363}
17364
4357ac6c
TT
17365/* Create appropriate locally-scoped variables for all the
17366 DW_TAG_common_block entries. Also create a struct common_block
17367 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17368 is used to separate the common blocks name namespace from regular
4357ac6c 17369 variable names. */
c906108c
SS
17370
17371static void
e7c27a73 17372read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17373{
0971de02
TT
17374 struct attribute *attr;
17375
17376 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17377 if (attr != nullptr)
0971de02
TT
17378 {
17379 /* Support the .debug_loc offsets. */
4fc6c0d5 17380 if (attr->form_is_block ())
dda83cd7 17381 {
0971de02 17382 /* Ok. */
dda83cd7 17383 }
cd6c91b4 17384 else if (attr->form_is_section_offset ())
dda83cd7 17385 {
0971de02
TT
17386 dwarf2_complex_location_expr_complaint ();
17387 attr = NULL;
dda83cd7 17388 }
0971de02 17389 else
dda83cd7 17390 {
0971de02
TT
17391 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17392 "common block member");
17393 attr = NULL;
dda83cd7 17394 }
0971de02
TT
17395 }
17396
639d11d3 17397 if (die->child != NULL)
c906108c 17398 {
5e22e966 17399 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17400 struct die_info *child_die;
17401 size_t n_entries = 0, size;
17402 struct common_block *common_block;
17403 struct symbol *sym;
74ac6d43 17404
4357ac6c
TT
17405 for (child_die = die->child;
17406 child_die && child_die->tag;
436c571c 17407 child_die = child_die->sibling)
4357ac6c
TT
17408 ++n_entries;
17409
17410 size = (sizeof (struct common_block)
17411 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17412 common_block
17413 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17414 size);
4357ac6c
TT
17415 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17416 common_block->n_entries = 0;
17417
17418 for (child_die = die->child;
17419 child_die && child_die->tag;
436c571c 17420 child_die = child_die->sibling)
4357ac6c
TT
17421 {
17422 /* Create the symbol in the DW_TAG_common_block block in the current
17423 symbol scope. */
e7c27a73 17424 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17425 if (sym != NULL)
17426 {
17427 struct attribute *member_loc;
17428
17429 common_block->contents[common_block->n_entries++] = sym;
17430
17431 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17432 cu);
17433 if (member_loc)
17434 {
17435 /* GDB has handled this for a long time, but it is
17436 not specified by DWARF. It seems to have been
17437 emitted by gfortran at least as recently as:
17438 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17439 complaint (_("Variable in common block has "
0971de02 17440 "DW_AT_data_member_location "
9d8780f0
SM
17441 "- DIE at %s [in module %s]"),
17442 sect_offset_str (child_die->sect_off),
518817b3 17443 objfile_name (objfile));
0971de02 17444
cd6c91b4 17445 if (member_loc->form_is_section_offset ())
0971de02 17446 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17447 else if (member_loc->form_is_constant ()
4fc6c0d5 17448 || member_loc->form_is_block ())
0971de02 17449 {
435d3d88 17450 if (attr != nullptr)
0971de02
TT
17451 mark_common_block_symbol_computed (sym, die, attr,
17452 member_loc, cu);
17453 }
17454 else
17455 dwarf2_complex_location_expr_complaint ();
17456 }
17457 }
c906108c 17458 }
4357ac6c
TT
17459
17460 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17461 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17462 }
17463}
17464
0114d602 17465/* Create a type for a C++ namespace. */
d9fa45fe 17466
0114d602
DJ
17467static struct type *
17468read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17469{
5e22e966 17470 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17471 const char *previous_prefix, *name;
9219021c 17472 int is_anonymous;
0114d602
DJ
17473 struct type *type;
17474
17475 /* For extensions, reuse the type of the original namespace. */
17476 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17477 {
17478 struct die_info *ext_die;
17479 struct dwarf2_cu *ext_cu = cu;
9a619af0 17480
0114d602
DJ
17481 ext_die = dwarf2_extension (die, &ext_cu);
17482 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17483
17484 /* EXT_CU may not be the same as CU.
02142a6c 17485 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17486 return set_die_type (die, type, cu);
17487 }
9219021c 17488
e142c38c 17489 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17490
17491 /* Now build the name of the current namespace. */
17492
0114d602
DJ
17493 previous_prefix = determine_prefix (die, cu);
17494 if (previous_prefix[0] != '\0')
17495 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17496 previous_prefix, name, 0, cu);
0114d602
DJ
17497
17498 /* Create the type. */
19f392bc 17499 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17500
60531b24 17501 return set_die_type (die, type, cu);
0114d602
DJ
17502}
17503
22cee43f 17504/* Read a namespace scope. */
0114d602
DJ
17505
17506static void
17507read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17508{
5e22e966 17509 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17510 int is_anonymous;
9219021c 17511
5c4e30ca
DC
17512 /* Add a symbol associated to this if we haven't seen the namespace
17513 before. Also, add a using directive if it's an anonymous
17514 namespace. */
9219021c 17515
f2f0e013 17516 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17517 {
17518 struct type *type;
17519
0114d602 17520 type = read_type_die (die, cu);
e7c27a73 17521 new_symbol (die, type, cu);
5c4e30ca 17522
e8e80198 17523 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17524 if (is_anonymous)
0114d602
DJ
17525 {
17526 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17527
eb1e02fd 17528 std::vector<const char *> excludes;
804d2729 17529 add_using_directive (using_directives (cu),
7d93a1e0 17530 previous_prefix, type->name (), NULL,
eb1e02fd 17531 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17532 }
5c4e30ca 17533 }
9219021c 17534
639d11d3 17535 if (die->child != NULL)
d9fa45fe 17536 {
639d11d3 17537 struct die_info *child_die = die->child;
6e70227d 17538
d9fa45fe
DC
17539 while (child_die && child_die->tag)
17540 {
e7c27a73 17541 process_die (child_die, cu);
436c571c 17542 child_die = child_die->sibling;
d9fa45fe
DC
17543 }
17544 }
38d518c9
EZ
17545}
17546
f55ee35c
JK
17547/* Read a Fortran module as type. This DIE can be only a declaration used for
17548 imported module. Still we need that type as local Fortran "use ... only"
17549 declaration imports depend on the created type in determine_prefix. */
17550
17551static struct type *
17552read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17553{
5e22e966 17554 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17555 const char *module_name;
f55ee35c
JK
17556 struct type *type;
17557
17558 module_name = dwarf2_name (die, cu);
19f392bc 17559 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17560
f55ee35c
JK
17561 return set_die_type (die, type, cu);
17562}
17563
5d7cb8df
JK
17564/* Read a Fortran module. */
17565
17566static void
17567read_module (struct die_info *die, struct dwarf2_cu *cu)
17568{
17569 struct die_info *child_die = die->child;
530e8392
KB
17570 struct type *type;
17571
17572 type = read_type_die (die, cu);
17573 new_symbol (die, type, cu);
5d7cb8df 17574
5d7cb8df
JK
17575 while (child_die && child_die->tag)
17576 {
17577 process_die (child_die, cu);
436c571c 17578 child_die = child_die->sibling;
5d7cb8df
JK
17579 }
17580}
17581
38d518c9
EZ
17582/* Return the name of the namespace represented by DIE. Set
17583 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17584 namespace. */
17585
17586static const char *
e142c38c 17587namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17588{
17589 struct die_info *current_die;
17590 const char *name = NULL;
17591
17592 /* Loop through the extensions until we find a name. */
17593
17594 for (current_die = die;
17595 current_die != NULL;
f2f0e013 17596 current_die = dwarf2_extension (die, &cu))
38d518c9 17597 {
96553a0c
DE
17598 /* We don't use dwarf2_name here so that we can detect the absence
17599 of a name -> anonymous namespace. */
7d45c7c3 17600 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17601
38d518c9
EZ
17602 if (name != NULL)
17603 break;
17604 }
17605
17606 /* Is it an anonymous namespace? */
17607
17608 *is_anonymous = (name == NULL);
17609 if (*is_anonymous)
2b1dbab0 17610 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17611
17612 return name;
d9fa45fe
DC
17613}
17614
c906108c
SS
17615/* Extract all information from a DW_TAG_pointer_type DIE and add to
17616 the user defined type vector. */
17617
f792889a 17618static struct type *
e7c27a73 17619read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17620{
5e22e966 17621 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17622 struct comp_unit_head *cu_header = &cu->header;
c906108c 17623 struct type *type;
8b2dbe47
KB
17624 struct attribute *attr_byte_size;
17625 struct attribute *attr_address_class;
17626 int byte_size, addr_class;
7e314c57
JK
17627 struct type *target_type;
17628
17629 target_type = die_type (die, cu);
c906108c 17630
7e314c57
JK
17631 /* The die_type call above may have already set the type for this DIE. */
17632 type = get_die_type (die, cu);
17633 if (type)
17634 return type;
17635
17636 type = lookup_pointer_type (target_type);
8b2dbe47 17637
e142c38c 17638 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17639 if (attr_byte_size)
529908cb 17640 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17641 else
8b2dbe47
KB
17642 byte_size = cu_header->addr_size;
17643
e142c38c 17644 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17645 if (attr_address_class)
529908cb 17646 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17647 else
17648 addr_class = DW_ADDR_none;
17649
2b4424c3
TT
17650 ULONGEST alignment = get_alignment (cu, die);
17651
17652 /* If the pointer size, alignment, or address class is different
17653 than the default, create a type variant marked as such and set
17654 the length accordingly. */
17655 if (TYPE_LENGTH (type) != byte_size
17656 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17657 && alignment != TYPE_RAW_ALIGN (type))
17658 || addr_class != DW_ADDR_none)
c906108c 17659 {
5e2b427d 17660 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17661 {
314ad88d
PA
17662 type_instance_flags type_flags
17663 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17664 addr_class);
876cecd0
TT
17665 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17666 == 0);
8b2dbe47
KB
17667 type = make_type_with_address_space (type, type_flags);
17668 }
17669 else if (TYPE_LENGTH (type) != byte_size)
17670 {
b98664d3 17671 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17672 }
2b4424c3
TT
17673 else if (TYPE_RAW_ALIGN (type) != alignment)
17674 {
b98664d3 17675 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17676 " - DIE at %s [in module %s]"),
17677 sect_offset_str (die->sect_off),
5e22e966 17678 objfile_name (cu->per_objfile->objfile));
2b4424c3 17679 }
6e70227d 17680 else
9a619af0
MS
17681 {
17682 /* Should we also complain about unhandled address classes? */
17683 }
c906108c 17684 }
8b2dbe47
KB
17685
17686 TYPE_LENGTH (type) = byte_size;
2b4424c3 17687 set_type_align (type, alignment);
f792889a 17688 return set_die_type (die, type, cu);
c906108c
SS
17689}
17690
17691/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17692 the user defined type vector. */
17693
f792889a 17694static struct type *
e7c27a73 17695read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17696{
17697 struct type *type;
17698 struct type *to_type;
17699 struct type *domain;
17700
e7c27a73
DJ
17701 to_type = die_type (die, cu);
17702 domain = die_containing_type (die, cu);
0d5de010 17703
7e314c57
JK
17704 /* The calls above may have already set the type for this DIE. */
17705 type = get_die_type (die, cu);
17706 if (type)
17707 return type;
17708
78134374 17709 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17710 type = lookup_methodptr_type (to_type);
78134374 17711 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17712 {
5e22e966 17713 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17714
17715 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17716 to_type->fields (), to_type->num_fields (),
a409645d 17717 to_type->has_varargs ());
7078baeb
TT
17718 type = lookup_methodptr_type (new_type);
17719 }
0d5de010
DJ
17720 else
17721 type = lookup_memberptr_type (to_type, domain);
c906108c 17722
f792889a 17723 return set_die_type (die, type, cu);
c906108c
SS
17724}
17725
4297a3f0 17726/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17727 the user defined type vector. */
17728
f792889a 17729static struct type *
4297a3f0 17730read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17731 enum type_code refcode)
c906108c 17732{
e7c27a73 17733 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17734 struct type *type, *target_type;
c906108c
SS
17735 struct attribute *attr;
17736
4297a3f0
AV
17737 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17738
7e314c57
JK
17739 target_type = die_type (die, cu);
17740
17741 /* The die_type call above may have already set the type for this DIE. */
17742 type = get_die_type (die, cu);
17743 if (type)
17744 return type;
17745
4297a3f0 17746 type = lookup_reference_type (target_type, refcode);
e142c38c 17747 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17748 if (attr != nullptr)
c906108c 17749 {
529908cb 17750 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17751 }
17752 else
17753 {
107d2387 17754 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17755 }
2b4424c3 17756 maybe_set_alignment (cu, die, type);
f792889a 17757 return set_die_type (die, type, cu);
c906108c
SS
17758}
17759
cf363f18
MW
17760/* Add the given cv-qualifiers to the element type of the array. GCC
17761 outputs DWARF type qualifiers that apply to an array, not the
17762 element type. But GDB relies on the array element type to carry
17763 the cv-qualifiers. This mimics section 6.7.3 of the C99
17764 specification. */
17765
17766static struct type *
17767add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17768 struct type *base_type, int cnst, int voltl)
17769{
17770 struct type *el_type, *inner_array;
17771
17772 base_type = copy_type (base_type);
17773 inner_array = base_type;
17774
78134374 17775 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17776 {
17777 TYPE_TARGET_TYPE (inner_array) =
17778 copy_type (TYPE_TARGET_TYPE (inner_array));
17779 inner_array = TYPE_TARGET_TYPE (inner_array);
17780 }
17781
17782 el_type = TYPE_TARGET_TYPE (inner_array);
17783 cnst |= TYPE_CONST (el_type);
17784 voltl |= TYPE_VOLATILE (el_type);
17785 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17786
17787 return set_die_type (die, base_type, cu);
17788}
17789
f792889a 17790static struct type *
e7c27a73 17791read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17792{
f792889a 17793 struct type *base_type, *cv_type;
c906108c 17794
e7c27a73 17795 base_type = die_type (die, cu);
7e314c57
JK
17796
17797 /* The die_type call above may have already set the type for this DIE. */
17798 cv_type = get_die_type (die, cu);
17799 if (cv_type)
17800 return cv_type;
17801
2f608a3a
KW
17802 /* In case the const qualifier is applied to an array type, the element type
17803 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17804 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17805 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17806
f792889a
DJ
17807 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17808 return set_die_type (die, cv_type, cu);
c906108c
SS
17809}
17810
f792889a 17811static struct type *
e7c27a73 17812read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17813{
f792889a 17814 struct type *base_type, *cv_type;
c906108c 17815
e7c27a73 17816 base_type = die_type (die, cu);
7e314c57
JK
17817
17818 /* The die_type call above may have already set the type for this DIE. */
17819 cv_type = get_die_type (die, cu);
17820 if (cv_type)
17821 return cv_type;
17822
cf363f18
MW
17823 /* In case the volatile qualifier is applied to an array type, the
17824 element type is so qualified, not the array type (section 6.7.3
17825 of C99). */
78134374 17826 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17827 return add_array_cv_type (die, cu, base_type, 0, 1);
17828
f792889a
DJ
17829 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17830 return set_die_type (die, cv_type, cu);
c906108c
SS
17831}
17832
06d66ee9
TT
17833/* Handle DW_TAG_restrict_type. */
17834
17835static struct type *
17836read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17837{
17838 struct type *base_type, *cv_type;
17839
17840 base_type = die_type (die, cu);
17841
17842 /* The die_type call above may have already set the type for this DIE. */
17843 cv_type = get_die_type (die, cu);
17844 if (cv_type)
17845 return cv_type;
17846
17847 cv_type = make_restrict_type (base_type);
17848 return set_die_type (die, cv_type, cu);
17849}
17850
a2c2acaf
MW
17851/* Handle DW_TAG_atomic_type. */
17852
17853static struct type *
17854read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17855{
17856 struct type *base_type, *cv_type;
17857
17858 base_type = die_type (die, cu);
17859
17860 /* The die_type call above may have already set the type for this DIE. */
17861 cv_type = get_die_type (die, cu);
17862 if (cv_type)
17863 return cv_type;
17864
17865 cv_type = make_atomic_type (base_type);
17866 return set_die_type (die, cv_type, cu);
17867}
17868
c906108c
SS
17869/* Extract all information from a DW_TAG_string_type DIE and add to
17870 the user defined type vector. It isn't really a user defined type,
17871 but it behaves like one, with other DIE's using an AT_user_def_type
17872 attribute to reference it. */
17873
f792889a 17874static struct type *
e7c27a73 17875read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17876{
5e22e966 17877 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17878 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17879 struct type *type, *range_type, *index_type, *char_type;
17880 struct attribute *attr;
216a7e6b
AB
17881 struct dynamic_prop prop;
17882 bool length_is_constant = true;
17883 LONGEST length;
17884
17885 /* There are a couple of places where bit sizes might be made use of
17886 when parsing a DW_TAG_string_type, however, no producer that we know
17887 of make use of these. Handling bit sizes that are a multiple of the
17888 byte size is easy enough, but what about other bit sizes? Lets deal
17889 with that problem when we have to. Warn about these attributes being
17890 unsupported, then parse the type and ignore them like we always
17891 have. */
17892 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17893 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17894 {
17895 static bool warning_printed = false;
17896 if (!warning_printed)
17897 {
17898 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17899 "currently supported on DW_TAG_string_type."));
17900 warning_printed = true;
17901 }
17902 }
c906108c 17903
e142c38c 17904 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17905 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17906 {
17907 /* The string length describes the location at which the length of
17908 the string can be found. The size of the length field can be
17909 specified with one of the attributes below. */
17910 struct type *prop_type;
17911 struct attribute *len
17912 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17913 if (len == nullptr)
17914 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17915 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17916 {
17917 /* Pass 0 as the default as we know this attribute is constant
17918 and the default value will not be returned. */
0826b30a 17919 LONGEST sz = len->constant_value (0);
293e7e51 17920 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17921 }
17922 else
17923 {
17924 /* If the size is not specified then we assume it is the size of
17925 an address on this target. */
293e7e51 17926 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17927 }
17928
17929 /* Convert the attribute into a dynamic property. */
17930 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17931 length = 1;
17932 else
17933 length_is_constant = false;
17934 }
17935 else if (attr != nullptr)
17936 {
17937 /* This DW_AT_string_length just contains the length with no
17938 indirection. There's no need to create a dynamic property in this
17939 case. Pass 0 for the default value as we know it will not be
17940 returned in this case. */
0826b30a 17941 length = attr->constant_value (0);
216a7e6b
AB
17942 }
17943 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17944 {
216a7e6b 17945 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17946 length = attr->constant_value (1);
c906108c
SS
17947 }
17948 else
17949 {
216a7e6b
AB
17950 /* Use 1 as a fallback length if we have nothing else. */
17951 length = 1;
c906108c 17952 }
6ccb9162 17953
46bf5051 17954 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17955 if (length_is_constant)
17956 range_type = create_static_range_type (NULL, index_type, 1, length);
17957 else
17958 {
17959 struct dynamic_prop low_bound;
17960
8c2e4e06 17961 low_bound.set_const_val (1);
216a7e6b
AB
17962 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
17963 }
3b7538c0
UW
17964 char_type = language_string_char_type (cu->language_defn, gdbarch);
17965 type = create_string_type (NULL, char_type, range_type);
6ccb9162 17966
f792889a 17967 return set_die_type (die, type, cu);
c906108c
SS
17968}
17969
4d804846
JB
17970/* Assuming that DIE corresponds to a function, returns nonzero
17971 if the function is prototyped. */
17972
17973static int
17974prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
17975{
17976 struct attribute *attr;
17977
17978 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 17979 if (attr && attr->as_boolean ())
4d804846
JB
17980 return 1;
17981
17982 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 17983 is only meaningful for C, but the concept also extends to other
4d804846
JB
17984 languages that allow unprototyped functions (Eg: Objective C).
17985 For all other languages, assume that functions are always
17986 prototyped. */
17987 if (cu->language != language_c
17988 && cu->language != language_objc
17989 && cu->language != language_opencl)
17990 return 1;
17991
17992 /* RealView does not emit DW_AT_prototyped. We can not distinguish
17993 prototyped and unprototyped functions; default to prototyped,
17994 since that is more common in modern code (and RealView warns
17995 about unprototyped functions). */
17996 if (producer_is_realview (cu->producer))
17997 return 1;
17998
17999 return 0;
18000}
18001
c906108c
SS
18002/* Handle DIES due to C code like:
18003
18004 struct foo
c5aa993b
JM
18005 {
18006 int (*funcp)(int a, long l);
18007 int b;
18008 };
c906108c 18009
0963b4bd 18010 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 18011
f792889a 18012static struct type *
e7c27a73 18013read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18014{
5e22e966 18015 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18016 struct type *type; /* Type that this function returns. */
18017 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18018 struct attribute *attr;
18019
e7c27a73 18020 type = die_type (die, cu);
7e314c57
JK
18021
18022 /* The die_type call above may have already set the type for this DIE. */
18023 ftype = get_die_type (die, cu);
18024 if (ftype)
18025 return ftype;
18026
0c8b41f1 18027 ftype = lookup_function_type (type);
c906108c 18028
4d804846 18029 if (prototyped_function_p (die, cu))
27e69b7a 18030 ftype->set_is_prototyped (true);
c906108c 18031
c055b101
CV
18032 /* Store the calling convention in the type if it's available in
18033 the subroutine die. Otherwise set the calling convention to
18034 the default value DW_CC_normal. */
18035 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18036 if (attr != nullptr
529908cb 18037 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18038 TYPE_CALLING_CONVENTION (ftype)
529908cb 18039 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18040 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18041 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18042 else
18043 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18044
743649fd
MW
18045 /* Record whether the function returns normally to its caller or not
18046 if the DWARF producer set that information. */
18047 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18048 if (attr && attr->as_boolean ())
743649fd
MW
18049 TYPE_NO_RETURN (ftype) = 1;
18050
76c10ea2
GM
18051 /* We need to add the subroutine type to the die immediately so
18052 we don't infinitely recurse when dealing with parameters
0963b4bd 18053 declared as the same subroutine type. */
76c10ea2 18054 set_die_type (die, ftype, cu);
6e70227d 18055
639d11d3 18056 if (die->child != NULL)
c906108c 18057 {
bb5ed363 18058 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18059 struct die_info *child_die;
8072405b 18060 int nparams, iparams;
c906108c
SS
18061
18062 /* Count the number of parameters.
dda83cd7
SM
18063 FIXME: GDB currently ignores vararg functions, but knows about
18064 vararg member functions. */
8072405b 18065 nparams = 0;
639d11d3 18066 child_die = die->child;
c906108c
SS
18067 while (child_die && child_die->tag)
18068 {
18069 if (child_die->tag == DW_TAG_formal_parameter)
18070 nparams++;
18071 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18072 ftype->set_has_varargs (true);
18073
436c571c 18074 child_die = child_die->sibling;
c906108c
SS
18075 }
18076
18077 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18078 ftype->set_num_fields (nparams);
3cabb6b0
SM
18079 ftype->set_fields
18080 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18081
8072405b
JK
18082 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18083 even if we error out during the parameters reading below. */
18084 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18085 ftype->field (iparams).set_type (void_type);
8072405b
JK
18086
18087 iparams = 0;
639d11d3 18088 child_die = die->child;
c906108c
SS
18089 while (child_die && child_die->tag)
18090 {
18091 if (child_die->tag == DW_TAG_formal_parameter)
18092 {
3ce3b1ba
PA
18093 struct type *arg_type;
18094
18095 /* DWARF version 2 has no clean way to discern C++
18096 static and non-static member functions. G++ helps
18097 GDB by marking the first parameter for non-static
18098 member functions (which is the this pointer) as
18099 artificial. We pass this information to
18100 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18101
18102 DWARF version 3 added DW_AT_object_pointer, which GCC
18103 4.5 does not yet generate. */
e142c38c 18104 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18105 if (attr != nullptr)
c45bc3f8 18106 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18107 else
9c37b5ae 18108 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18109 arg_type = die_type (child_die, cu);
18110
18111 /* RealView does not mark THIS as const, which the testsuite
18112 expects. GCC marks THIS as const in method definitions,
18113 but not in the class specifications (GCC PR 43053). */
18114 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18115 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18116 {
18117 int is_this = 0;
18118 struct dwarf2_cu *arg_cu = cu;
18119 const char *name = dwarf2_name (child_die, cu);
18120
18121 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18122 if (attr != nullptr)
3ce3b1ba
PA
18123 {
18124 /* If the compiler emits this, use it. */
18125 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18126 is_this = 1;
18127 }
18128 else if (name && strcmp (name, "this") == 0)
18129 /* Function definitions will have the argument names. */
18130 is_this = 1;
18131 else if (name == NULL && iparams == 0)
18132 /* Declarations may not have the names, so like
18133 elsewhere in GDB, assume an artificial first
18134 argument is "this". */
18135 is_this = 1;
18136
18137 if (is_this)
18138 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18139 arg_type, 0);
18140 }
18141
5d14b6e5 18142 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18143 iparams++;
18144 }
436c571c 18145 child_die = child_die->sibling;
c906108c
SS
18146 }
18147 }
18148
76c10ea2 18149 return ftype;
c906108c
SS
18150}
18151
f792889a 18152static struct type *
e7c27a73 18153read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18154{
5e22e966 18155 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18156 const char *name = NULL;
3c8e0968 18157 struct type *this_type, *target_type;
c906108c 18158
94af9270 18159 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18160 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18161 this_type->set_target_is_stub (true);
f792889a 18162 set_die_type (die, this_type, cu);
3c8e0968
DE
18163 target_type = die_type (die, cu);
18164 if (target_type != this_type)
18165 TYPE_TARGET_TYPE (this_type) = target_type;
18166 else
18167 {
18168 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18169 spec and cause infinite loops in GDB. */
b98664d3 18170 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18171 "- DIE at %s [in module %s]"),
18172 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18173 TYPE_TARGET_TYPE (this_type) = NULL;
18174 }
e4003a34
TV
18175 if (name == NULL)
18176 {
18177 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18178 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18179 Handle these by just returning the target type, rather than
18180 constructing an anonymous typedef type and trying to handle this
18181 elsewhere. */
18182 set_die_type (die, target_type, cu);
18183 return target_type;
18184 }
f792889a 18185 return this_type;
c906108c
SS
18186}
18187
09584414
JB
18188/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18189 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18190
18191 If the numerator and/or numerator attribute is missing,
18192 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18193 untouched. */
18194
18195static void
18196get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
18197 LONGEST *numerator, LONGEST *denominator)
18198{
18199 struct attribute *num_attr, *denom_attr;
18200
18201 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18202 if (num_attr == nullptr)
18203 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18204 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18205
18206 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18207 if (denom_attr == nullptr)
18208 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18209 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18210
18211 if (num_attr == nullptr || denom_attr == nullptr)
18212 return;
18213
18214 *numerator = num_attr->constant_value (1);
18215 *denominator = denom_attr->constant_value (1);
18216}
18217
18218/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18219 rational constant, rather than a signed one.
18220
18221 If the rational constant has a negative value, a complaint
18222 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18223
18224static void
18225get_dwarf2_unsigned_rational_constant (struct die_info *die,
18226 struct dwarf2_cu *cu,
18227 ULONGEST *numerator,
18228 ULONGEST *denominator)
18229{
18230 LONGEST num = 1, denom = 1;
18231
18232 get_dwarf2_rational_constant (die, cu, &num, &denom);
18233 if (num < 0 && denom < 0)
18234 {
18235 num = -num;
18236 denom = -denom;
18237 }
18238 else if (num < 0)
18239 {
18240 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18241 " in DIE at %s"),
18242 sect_offset_str (die->sect_off));
18243 return;
18244 }
18245 else if (denom < 0)
18246 {
18247 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18248 " in DIE at %s"),
18249 sect_offset_str (die->sect_off));
18250 return;
18251 }
18252
18253 *numerator = num;
18254 *denominator = denom;
18255}
18256
18257/* Assuming DIE corresponds to a fixed point type, finish the creation
2a12c336 18258 of the corresponding TYPE by setting its type-specific data.
09584414
JB
18259 CU is the DIE's CU. */
18260
18261static void
18262finish_fixed_point_type (struct type *type, struct die_info *die,
18263 struct dwarf2_cu *cu)
18264{
18265 struct attribute *attr;
18266 /* Numerator and denominator of our fixed-point type's scaling factor.
18267 The default is a scaling factor of 1, which we use as a fallback
18268 when we are not able to decode it (problem with the debugging info,
18269 unsupported forms, bug in GDB, etc...). Using that as the default
18270 allows us to at least print the unscaled value, which might still
18271 be useful to a user. */
18272 ULONGEST scale_num = 1;
18273 ULONGEST scale_denom = 1;
18274
18275 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18276 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18277
18278 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18279 if (!attr)
18280 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18281 if (!attr)
18282 attr = dwarf2_attr (die, DW_AT_small, cu);
18283
18284 if (attr == nullptr)
18285 {
18286 /* Scaling factor not found. Assume a scaling factor of 1,
18287 and hope for the best. At least the user will be able to see
18288 the encoded value. */
18289 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18290 sect_offset_str (die->sect_off));
18291 }
18292 else if (attr->name == DW_AT_binary_scale)
18293 {
18294 LONGEST scale_exp = attr->constant_value (0);
18295 ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18296
a43b29c9 18297 *num_or_denom = 1 << std::abs (scale_exp);
09584414
JB
18298 }
18299 else if (attr->name == DW_AT_decimal_scale)
18300 {
18301 LONGEST scale_exp = attr->constant_value (0);
18302 ULONGEST *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
18303
a43b29c9 18304 *num_or_denom = uinteger_pow (10, std::abs (scale_exp));
09584414
JB
18305 }
18306 else if (attr->name == DW_AT_small)
18307 {
18308 struct die_info *scale_die;
18309 struct dwarf2_cu *scale_cu = cu;
18310
18311 scale_die = follow_die_ref (die, attr, &scale_cu);
18312 if (scale_die->tag == DW_TAG_constant)
18313 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18314 &scale_num, &scale_denom);
18315 else
18316 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18317 " (DIE at %s)"),
18318 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18319 }
18320 else
18321 {
18322 complaint (_("unsupported scale attribute %s for fixed-point type"
18323 " (DIE at %s)"),
18324 dwarf_attr_name (attr->name),
18325 sect_offset_str (die->sect_off));
18326 }
18327
2a12c336 18328 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
09584414
JB
18329
18330 gdb_mpz tmp_z (scale_num);
18331 mpz_set (mpq_numref (scaling_factor.val), tmp_z.val);
18332
18333 tmp_z = scale_denom;
18334 mpz_set (mpq_denref (scaling_factor.val), tmp_z.val);
18335
18336 mpq_canonicalize (scaling_factor.val);
18337}
18338
9b790ce7
UW
18339/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18340 (which may be different from NAME) to the architecture back-end to allow
18341 it to guess the correct format if necessary. */
18342
18343static struct type *
18344dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18345 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18346{
08feed99 18347 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18348 const struct floatformat **format;
18349 struct type *type;
18350
18351 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18352 if (format)
103a685e 18353 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18354 else
77b7c781 18355 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18356
18357 return type;
18358}
18359
eb77c9df
AB
18360/* Allocate an integer type of size BITS and name NAME. */
18361
18362static struct type *
18363dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18364 int bits, int unsigned_p, const char *name)
18365{
18366 struct type *type;
18367
18368 /* Versions of Intel's C Compiler generate an integer type called "void"
18369 instead of using DW_TAG_unspecified_type. This has been seen on
18370 at least versions 14, 17, and 18. */
35ee2dc2
AB
18371 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18372 && strcmp (name, "void") == 0)
eb77c9df
AB
18373 type = objfile_type (objfile)->builtin_void;
18374 else
18375 type = init_integer_type (objfile, bits, unsigned_p, name);
18376
18377 return type;
18378}
18379
09584414
JB
18380/* Return true if DIE has a DW_AT_small attribute whose value is
18381 a constant rational, where both the numerator and denominator
18382 are equal to zero.
18383
18384 CU is the DIE's Compilation Unit. */
18385
18386static bool
18387has_zero_over_zero_small_attribute (struct die_info *die,
18388 struct dwarf2_cu *cu)
18389{
18390 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18391 if (attr == nullptr)
18392 return false;
18393
18394 struct dwarf2_cu *scale_cu = cu;
18395 struct die_info *scale_die
18396 = follow_die_ref (die, attr, &scale_cu);
18397
18398 if (scale_die->tag != DW_TAG_constant)
18399 return false;
18400
18401 LONGEST num = 1, denom = 1;
18402 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
18403 return (num == 0 && denom == 0);
18404}
18405
8bdc1658
AB
18406/* Initialise and return a floating point type of size BITS suitable for
18407 use as a component of a complex number. The NAME_HINT is passed through
18408 when initialising the floating point type and is the name of the complex
18409 type.
18410
18411 As DWARF doesn't currently provide an explicit name for the components
18412 of a complex number, but it can be helpful to have these components
18413 named, we try to select a suitable name based on the size of the
18414 component. */
18415static struct type *
18416dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18417 struct objfile *objfile,
103a685e
TT
18418 int bits, const char *name_hint,
18419 enum bfd_endian byte_order)
8bdc1658 18420{
08feed99 18421 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18422 struct type *tt = nullptr;
18423
35add35e
AB
18424 /* Try to find a suitable floating point builtin type of size BITS.
18425 We're going to use the name of this type as the name for the complex
18426 target type that we are about to create. */
1db455a7 18427 switch (cu->language)
8bdc1658 18428 {
1db455a7
AB
18429 case language_fortran:
18430 switch (bits)
18431 {
18432 case 32:
18433 tt = builtin_f_type (gdbarch)->builtin_real;
18434 break;
18435 case 64:
18436 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18437 break;
18438 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18439 case 128:
18440 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18441 break;
18442 }
8bdc1658 18443 break;
1db455a7
AB
18444 default:
18445 switch (bits)
18446 {
18447 case 32:
18448 tt = builtin_type (gdbarch)->builtin_float;
18449 break;
18450 case 64:
18451 tt = builtin_type (gdbarch)->builtin_double;
18452 break;
18453 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18454 case 128:
18455 tt = builtin_type (gdbarch)->builtin_long_double;
18456 break;
18457 }
8bdc1658
AB
18458 break;
18459 }
18460
35add35e
AB
18461 /* If the type we found doesn't match the size we were looking for, then
18462 pretend we didn't find a type at all, the complex target type we
18463 create will then be nameless. */
a12e5744 18464 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18465 tt = nullptr;
18466
7d93a1e0 18467 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18468 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18469}
18470
c906108c
SS
18471/* Find a representation of a given base type and install
18472 it in the TYPE field of the die. */
18473
f792889a 18474static struct type *
e7c27a73 18475read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18476{
5e22e966 18477 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18478 struct type *type;
18479 struct attribute *attr;
19f392bc 18480 int encoding = 0, bits = 0;
15d034d0 18481 const char *name;
34877895 18482 gdbarch *arch;
c906108c 18483
e142c38c 18484 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18485 if (attr != nullptr && attr->form_is_constant ())
18486 encoding = attr->constant_value (0);
e142c38c 18487 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18488 if (attr != nullptr)
529908cb 18489 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18490 name = dwarf2_name (die, cu);
6ccb9162 18491 if (!name)
34877895 18492 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18493
08feed99 18494 arch = objfile->arch ();
103a685e
TT
18495 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18496
34877895 18497 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18498 if (attr != nullptr && attr->form_is_constant ())
103a685e 18499 {
529908cb 18500 int endianity = attr->constant_value (0);
103a685e
TT
18501
18502 switch (endianity)
18503 {
18504 case DW_END_big:
18505 byte_order = BFD_ENDIAN_BIG;
18506 break;
18507 case DW_END_little:
18508 byte_order = BFD_ENDIAN_LITTLE;
18509 break;
18510 default:
18511 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18512 break;
18513 }
18514 }
6ccb9162 18515
09584414
JB
18516 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18517 && cu->language == language_ada
18518 && has_zero_over_zero_small_attribute (die, cu))
18519 {
18520 /* brobecker/2018-02-24: This is a fixed point type for which
18521 the scaling factor is represented as fraction whose value
18522 does not make sense (zero divided by zero), so we should
18523 normally never see these. However, there is a small category
18524 of fixed point types for which GNAT is unable to provide
18525 the scaling factor via the standard DWARF mechanisms, and
18526 for which the info is provided via the GNAT encodings instead.
18527 This is likely what this DIE is about.
18528
18529 Ideally, GNAT should be declaring this type the same way
18530 it declares other fixed point types when using the legacy
18531 GNAT encoding, which is to use a simple signed or unsigned
18532 base type. A report to the GNAT team has been created to
18533 look into it. In the meantime, pretend this type is a simple
18534 signed or unsigned integral, rather than a fixed point type,
18535 to avoid any confusion later on as to how to process this type. */
18536 encoding = (encoding == DW_ATE_signed_fixed
18537 ? DW_ATE_signed
18538 : DW_ATE_unsigned);
18539 }
18540
6ccb9162 18541 switch (encoding)
c906108c 18542 {
6ccb9162
UW
18543 case DW_ATE_address:
18544 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18545 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18546 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18547 break;
18548 case DW_ATE_boolean:
19f392bc 18549 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18550 break;
18551 case DW_ATE_complex_float:
103a685e
TT
18552 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18553 byte_order);
78134374 18554 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18555 {
18556 if (name == nullptr)
18557 {
18558 struct obstack *obstack
5e22e966 18559 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18560 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18561 nullptr);
18562 }
18563 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18564 }
18565 else
18566 type = init_complex_type (name, type);
6ccb9162
UW
18567 break;
18568 case DW_ATE_decimal_float:
19f392bc 18569 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18570 break;
18571 case DW_ATE_float:
103a685e 18572 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18573 break;
18574 case DW_ATE_signed:
eb77c9df 18575 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18576 break;
18577 case DW_ATE_unsigned:
3b2b8fea
TT
18578 if (cu->language == language_fortran
18579 && name
61012eef 18580 && startswith (name, "character("))
19f392bc
UW
18581 type = init_character_type (objfile, bits, 1, name);
18582 else
eb77c9df 18583 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18584 break;
18585 case DW_ATE_signed_char:
6e70227d 18586 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18587 || cu->language == language_pascal
18588 || cu->language == language_fortran)
19f392bc
UW
18589 type = init_character_type (objfile, bits, 0, name);
18590 else
eb77c9df 18591 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18592 break;
18593 case DW_ATE_unsigned_char:
868a0084 18594 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18595 || cu->language == language_pascal
c44af4eb
TT
18596 || cu->language == language_fortran
18597 || cu->language == language_rust)
19f392bc
UW
18598 type = init_character_type (objfile, bits, 1, name);
18599 else
eb77c9df 18600 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18601 break;
75079b2b 18602 case DW_ATE_UTF:
53e710ac 18603 {
53e710ac
PA
18604 if (bits == 16)
18605 type = builtin_type (arch)->builtin_char16;
18606 else if (bits == 32)
18607 type = builtin_type (arch)->builtin_char32;
18608 else
18609 {
b98664d3 18610 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18611 bits);
eb77c9df 18612 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18613 }
18614 return set_die_type (die, type, cu);
18615 }
75079b2b 18616 break;
09584414
JB
18617 case DW_ATE_signed_fixed:
18618 type = init_fixed_point_type (objfile, bits, 0, name);
18619 finish_fixed_point_type (type, die, cu);
18620 break;
18621 case DW_ATE_unsigned_fixed:
18622 type = init_fixed_point_type (objfile, bits, 1, name);
18623 finish_fixed_point_type (type, die, cu);
18624 break;
75079b2b 18625
6ccb9162 18626 default:
b98664d3 18627 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18628 dwarf_type_encoding_name (encoding));
77b7c781 18629 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18630 break;
c906108c 18631 }
6ccb9162 18632
0114d602 18633 if (name && strcmp (name, "char") == 0)
15152a54 18634 type->set_has_no_signedness (true);
0114d602 18635
2b4424c3
TT
18636 maybe_set_alignment (cu, die, type);
18637
db558e34 18638 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18639
20a5fcbd
TT
18640 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18641 {
18642 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18643 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18644 {
529908cb 18645 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18646 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18647 /* Only use the attributes if they make sense together. */
18648 if (attr == nullptr
529908cb
TT
18649 || (attr->as_unsigned () + real_bit_size
18650 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18651 {
18652 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18653 = real_bit_size;
18654 if (attr != nullptr)
18655 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18656 = attr->as_unsigned ();
20a5fcbd
TT
18657 }
18658 }
18659 }
18660
f792889a 18661 return set_die_type (die, type, cu);
c906108c
SS
18662}
18663
80180f79
SA
18664/* Parse dwarf attribute if it's a block, reference or constant and put the
18665 resulting value of the attribute into struct bound_prop.
18666 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18667
18668static int
18669attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18670 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18671 struct type *default_type)
80180f79
SA
18672{
18673 struct dwarf2_property_baton *baton;
5e22e966 18674 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18675 struct objfile *objfile = per_objfile->objfile;
18676 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18677
9a49df9d
AB
18678 gdb_assert (default_type != NULL);
18679
80180f79
SA
18680 if (attr == NULL || prop == NULL)
18681 return 0;
18682
4fc6c0d5 18683 if (attr->form_is_block ())
80180f79 18684 {
8d749320 18685 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18686 baton->property_type = default_type;
80180f79 18687 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18688 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18689
18690 struct dwarf_block *block = attr->as_block ();
18691 baton->locexpr.size = block->size;
18692 baton->locexpr.data = block->data;
216a7e6b
AB
18693 switch (attr->name)
18694 {
18695 case DW_AT_string_length:
18696 baton->locexpr.is_reference = true;
18697 break;
18698 default:
18699 baton->locexpr.is_reference = false;
18700 break;
18701 }
8c2e4e06
SM
18702
18703 prop->set_locexpr (baton);
18704 gdb_assert (prop->baton () != NULL);
80180f79 18705 }
cd6c91b4 18706 else if (attr->form_is_ref ())
80180f79
SA
18707 {
18708 struct dwarf2_cu *target_cu = cu;
18709 struct die_info *target_die;
18710 struct attribute *target_attr;
18711
18712 target_die = follow_die_ref (die, attr, &target_cu);
18713 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18714 if (target_attr == NULL)
18715 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18716 target_cu);
80180f79
SA
18717 if (target_attr == NULL)
18718 return 0;
18719
df25ebbd 18720 switch (target_attr->name)
80180f79 18721 {
df25ebbd 18722 case DW_AT_location:
cd6c91b4 18723 if (target_attr->form_is_section_offset ())
df25ebbd 18724 {
8d749320 18725 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18726 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18727 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18728 prop->set_loclist (baton);
18729 gdb_assert (prop->baton () != NULL);
df25ebbd 18730 }
4fc6c0d5 18731 else if (target_attr->form_is_block ())
df25ebbd 18732 {
8d749320 18733 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18734 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18735 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18736 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18737 struct dwarf_block *block = target_attr->as_block ();
18738 baton->locexpr.size = block->size;
18739 baton->locexpr.data = block->data;
9a49df9d 18740 baton->locexpr.is_reference = true;
8c2e4e06
SM
18741 prop->set_locexpr (baton);
18742 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18743 }
18744 else
18745 {
18746 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18747 "dynamic property");
18748 return 0;
18749 }
18750 break;
18751 case DW_AT_data_member_location:
18752 {
18753 LONGEST offset;
18754
18755 if (!handle_data_member_location (target_die, target_cu,
18756 &offset))
18757 return 0;
18758
8d749320 18759 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18760 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18761 target_cu);
df25ebbd
JB
18762 baton->offset_info.offset = offset;
18763 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18764 prop->set_addr_offset (baton);
df25ebbd
JB
18765 break;
18766 }
80180f79
SA
18767 }
18768 }
cd6c91b4 18769 else if (attr->form_is_constant ())
8c2e4e06 18770 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18771 else
18772 {
18773 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18774 dwarf2_name (die, cu));
18775 return 0;
18776 }
18777
18778 return 1;
18779}
18780
09ba997f 18781/* See read.h. */
9a49df9d 18782
09ba997f 18783struct type *
293e7e51 18784dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18785{
9a49df9d
AB
18786 struct type *int_type;
18787
18788 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18789#define TRY_TYPE(F) \
18790 int_type = (unsigned_p \
18791 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18792 : objfile_type (objfile)->builtin_ ## F); \
18793 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18794 return int_type
18795
18796 TRY_TYPE (char);
18797 TRY_TYPE (short);
18798 TRY_TYPE (int);
18799 TRY_TYPE (long);
18800 TRY_TYPE (long_long);
18801
18802#undef TRY_TYPE
18803
18804 gdb_assert_not_reached ("unable to find suitable integer type");
18805}
18806
09ba997f 18807/* See read.h. */
11a8b164 18808
09ba997f 18809struct type *
293e7e51 18810dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18811{
293e7e51
SM
18812 int addr_size = this->per_cu->addr_size ();
18813 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18814}
18815
b86352cf
AB
18816/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18817 present (which is valid) then compute the default type based on the
18818 compilation units address size. */
18819
18820static struct type *
18821read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18822{
18823 struct type *index_type = die_type (die, cu);
18824
18825 /* Dwarf-2 specifications explicitly allows to create subrange types
18826 without specifying a base type.
18827 In that case, the base type must be set to the type of
18828 the lower bound, upper bound or count, in that order, if any of these
18829 three attributes references an object that has a type.
18830 If no base type is found, the Dwarf-2 specifications say that
18831 a signed integer type of size equal to the size of an address should
18832 be used.
18833 For the following C code: `extern char gdb_int [];'
18834 GCC produces an empty range DIE.
18835 FIXME: muller/2010-05-28: Possible references to object for low bound,
18836 high bound or count are not yet handled by this code. */
78134374 18837 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18838 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18839
18840 return index_type;
18841}
18842
a02abb62
JB
18843/* Read the given DW_AT_subrange DIE. */
18844
f792889a 18845static struct type *
a02abb62
JB
18846read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18847{
4c9ad8c2 18848 struct type *base_type, *orig_base_type;
a02abb62
JB
18849 struct type *range_type;
18850 struct attribute *attr;
729efb13 18851 struct dynamic_prop low, high;
4fae6e18 18852 int low_default_is_valid;
c451ebe5 18853 int high_bound_is_count = 0;
15d034d0 18854 const char *name;
d359392f 18855 ULONGEST negative_mask;
e77813c8 18856
b86352cf
AB
18857 orig_base_type = read_subrange_index_type (die, cu);
18858
4c9ad8c2
TT
18859 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18860 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18861 creating the range type, but we use the result of check_typedef
18862 when examining properties of the type. */
18863 base_type = check_typedef (orig_base_type);
a02abb62 18864
7e314c57
JK
18865 /* The die_type call above may have already set the type for this DIE. */
18866 range_type = get_die_type (die, cu);
18867 if (range_type)
18868 return range_type;
18869
8c2e4e06 18870 high.set_const_val (0);
729efb13 18871
4fae6e18
JK
18872 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18873 omitting DW_AT_lower_bound. */
18874 switch (cu->language)
6e70227d 18875 {
4fae6e18
JK
18876 case language_c:
18877 case language_cplus:
8c2e4e06 18878 low.set_const_val (0);
4fae6e18
JK
18879 low_default_is_valid = 1;
18880 break;
18881 case language_fortran:
8c2e4e06 18882 low.set_const_val (1);
4fae6e18
JK
18883 low_default_is_valid = 1;
18884 break;
18885 case language_d:
4fae6e18 18886 case language_objc:
c44af4eb 18887 case language_rust:
8c2e4e06 18888 low.set_const_val (0);
4fae6e18
JK
18889 low_default_is_valid = (cu->header.version >= 4);
18890 break;
18891 case language_ada:
18892 case language_m2:
18893 case language_pascal:
8c2e4e06 18894 low.set_const_val (1);
4fae6e18
JK
18895 low_default_is_valid = (cu->header.version >= 4);
18896 break;
18897 default:
8c2e4e06 18898 low.set_const_val (0);
4fae6e18
JK
18899 low_default_is_valid = 0;
18900 break;
a02abb62
JB
18901 }
18902
e142c38c 18903 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18904 if (attr != nullptr)
9a49df9d 18905 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18906 else if (!low_default_is_valid)
b98664d3 18907 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18908 "- DIE at %s [in module %s]"),
18909 sect_offset_str (die->sect_off),
5e22e966 18910 objfile_name (cu->per_objfile->objfile));
a02abb62 18911
506f5c41
TV
18912 struct attribute *attr_ub, *attr_count;
18913 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18914 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18915 {
506f5c41 18916 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18917 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18918 {
c451ebe5 18919 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
18920 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
18921 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
18922 else
18923 high_bound_is_count = 1;
c2ff108b 18924 }
506f5c41
TV
18925 else
18926 {
18927 if (attr_ub != NULL)
18928 complaint (_("Unresolved DW_AT_upper_bound "
18929 "- DIE at %s [in module %s]"),
18930 sect_offset_str (die->sect_off),
5e22e966 18931 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
18932 if (attr_count != NULL)
18933 complaint (_("Unresolved DW_AT_count "
18934 "- DIE at %s [in module %s]"),
18935 sect_offset_str (die->sect_off),
5e22e966 18936 objfile_name (cu->per_objfile->objfile));
506f5c41 18937 }
e77813c8 18938 }
a02abb62 18939
4e962e74
TT
18940 LONGEST bias = 0;
18941 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 18942 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 18943 bias = bias_attr->constant_value (0);
4e962e74 18944
dbb9c2b1
JB
18945 /* Normally, the DWARF producers are expected to use a signed
18946 constant form (Eg. DW_FORM_sdata) to express negative bounds.
18947 But this is unfortunately not always the case, as witnessed
18948 with GCC, for instance, where the ambiguous DW_FORM_dataN form
18949 is used instead. To work around that ambiguity, we treat
18950 the bounds as signed, and thus sign-extend their values, when
18951 the base type is signed. */
6e70227d 18952 negative_mask =
d359392f 18953 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 18954 if (low.kind () == PROP_CONST
c6d940a9 18955 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
18956 low.set_const_val (low.const_val () | negative_mask);
18957 if (high.kind () == PROP_CONST
c6d940a9 18958 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 18959 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 18960
5bbd8269
AB
18961 /* Check for bit and byte strides. */
18962 struct dynamic_prop byte_stride_prop;
18963 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
18964 if (attr_byte_stride != nullptr)
18965 {
293e7e51 18966 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18967 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
18968 prop_type);
18969 }
18970
18971 struct dynamic_prop bit_stride_prop;
18972 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
18973 if (attr_bit_stride != nullptr)
18974 {
18975 /* It only makes sense to have either a bit or byte stride. */
18976 if (attr_byte_stride != nullptr)
18977 {
18978 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
18979 "- DIE at %s [in module %s]"),
18980 sect_offset_str (die->sect_off),
5e22e966 18981 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
18982 attr_bit_stride = nullptr;
18983 }
18984 else
18985 {
293e7e51 18986 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
18987 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
18988 prop_type);
18989 }
18990 }
18991
18992 if (attr_byte_stride != nullptr
18993 || attr_bit_stride != nullptr)
18994 {
18995 bool byte_stride_p = (attr_byte_stride != nullptr);
18996 struct dynamic_prop *stride
18997 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
18998
18999 range_type
19000 = create_range_type_with_stride (NULL, orig_base_type, &low,
19001 &high, bias, stride, byte_stride_p);
19002 }
19003 else
19004 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19005
c451ebe5 19006 if (high_bound_is_count)
599088e3 19007 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19008
c2ff108b
JK
19009 /* Ada expects an empty array on no boundary attributes. */
19010 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19011 range_type->bounds ()->high.set_undefined ();
c2ff108b 19012
39cbfefa
DJ
19013 name = dwarf2_name (die, cu);
19014 if (name)
d0e39ea2 19015 range_type->set_name (name);
6e70227d 19016
e142c38c 19017 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19018 if (attr != nullptr)
529908cb 19019 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19020
2b4424c3
TT
19021 maybe_set_alignment (cu, die, range_type);
19022
7e314c57
JK
19023 set_die_type (die, range_type, cu);
19024
19025 /* set_die_type should be already done. */
b4ba55a1
JB
19026 set_descriptive_type (range_type, die, cu);
19027
7e314c57 19028 return range_type;
a02abb62 19029}
6e70227d 19030
f792889a 19031static struct type *
81a17f79
JB
19032read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19033{
19034 struct type *type;
81a17f79 19035
5e22e966 19036 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19037 type->set_name (dwarf2_name (die, cu));
81a17f79 19038
74a2f8ff 19039 /* In Ada, an unspecified type is typically used when the description
85102364 19040 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19041 such a type, we treat it as a stub, and try to resolve it later on,
19042 when needed. */
19043 if (cu->language == language_ada)
b4b73759 19044 type->set_is_stub (true);
74a2f8ff 19045
f792889a 19046 return set_die_type (die, type, cu);
81a17f79 19047}
a02abb62 19048
639d11d3
DC
19049/* Read a single die and all its descendents. Set the die's sibling
19050 field to NULL; set other fields in the die correctly, and set all
19051 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19052 location of the info_ptr after reading all of those dies. PARENT
19053 is the parent of the die in question. */
19054
19055static struct die_info *
dee91e82 19056read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19057 const gdb_byte *info_ptr,
19058 const gdb_byte **new_info_ptr,
dee91e82 19059 struct die_info *parent)
639d11d3
DC
19060{
19061 struct die_info *die;
d521ce57 19062 const gdb_byte *cur_ptr;
639d11d3 19063
3e225074 19064 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19065 if (die == NULL)
19066 {
19067 *new_info_ptr = cur_ptr;
19068 return NULL;
19069 }
93311388 19070 store_in_ref_table (die, reader->cu);
639d11d3 19071
3e225074 19072 if (die->has_children)
bf6af496 19073 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19074 else
19075 {
19076 die->child = NULL;
19077 *new_info_ptr = cur_ptr;
19078 }
19079
19080 die->sibling = NULL;
19081 die->parent = parent;
19082 return die;
19083}
19084
19085/* Read a die, all of its descendents, and all of its siblings; set
19086 all of the fields of all of the dies correctly. Arguments are as
19087 in read_die_and_children. */
19088
19089static struct die_info *
bf6af496 19090read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19091 const gdb_byte *info_ptr,
19092 const gdb_byte **new_info_ptr,
bf6af496 19093 struct die_info *parent)
639d11d3
DC
19094{
19095 struct die_info *first_die, *last_sibling;
d521ce57 19096 const gdb_byte *cur_ptr;
639d11d3 19097
c906108c 19098 cur_ptr = info_ptr;
639d11d3
DC
19099 first_die = last_sibling = NULL;
19100
19101 while (1)
c906108c 19102 {
639d11d3 19103 struct die_info *die
dee91e82 19104 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19105
1d325ec1 19106 if (die == NULL)
c906108c 19107 {
639d11d3
DC
19108 *new_info_ptr = cur_ptr;
19109 return first_die;
c906108c 19110 }
1d325ec1
DJ
19111
19112 if (!first_die)
19113 first_die = die;
c906108c 19114 else
1d325ec1
DJ
19115 last_sibling->sibling = die;
19116
19117 last_sibling = die;
c906108c 19118 }
c906108c
SS
19119}
19120
bf6af496
DE
19121/* Read a die, all of its descendents, and all of its siblings; set
19122 all of the fields of all of the dies correctly. Arguments are as
19123 in read_die_and_children.
19124 This the main entry point for reading a DIE and all its children. */
19125
19126static struct die_info *
19127read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19128 const gdb_byte *info_ptr,
19129 const gdb_byte **new_info_ptr,
bf6af496
DE
19130 struct die_info *parent)
19131{
19132 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19133 new_info_ptr, parent);
19134
b4f54984 19135 if (dwarf_die_debug)
bf6af496
DE
19136 {
19137 fprintf_unfiltered (gdb_stdlog,
19138 "Read die from %s@0x%x of %s:\n",
96b79293 19139 reader->die_section->get_name (),
bf6af496
DE
19140 (unsigned) (info_ptr - reader->die_section->buffer),
19141 bfd_get_filename (reader->abfd));
b4f54984 19142 dump_die (die, dwarf_die_debug);
bf6af496
DE
19143 }
19144
19145 return die;
19146}
19147
3019eac3
DE
19148/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19149 attributes.
19150 The caller is responsible for filling in the extra attributes
19151 and updating (*DIEP)->num_attrs.
19152 Set DIEP to point to a newly allocated die with its information,
3e225074 19153 except for its child, sibling, and parent fields. */
93311388 19154
d521ce57 19155static const gdb_byte *
3019eac3 19156read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19157 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19158 int num_extra_attrs)
93311388 19159{
b64f50a1 19160 unsigned int abbrev_number, bytes_read, i;
93311388
DE
19161 struct abbrev_info *abbrev;
19162 struct die_info *die;
19163 struct dwarf2_cu *cu = reader->cu;
19164 bfd *abfd = reader->abfd;
19165
9c541725 19166 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19167 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19168 info_ptr += bytes_read;
19169 if (!abbrev_number)
19170 {
19171 *diep = NULL;
93311388
DE
19172 return info_ptr;
19173 }
19174
685af9cd 19175 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19176 if (!abbrev)
348e048f
DE
19177 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19178 abbrev_number,
19179 bfd_get_filename (abfd));
19180
3019eac3 19181 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19182 die->sect_off = sect_off;
93311388
DE
19183 die->tag = abbrev->tag;
19184 die->abbrev = abbrev_number;
3e225074 19185 die->has_children = abbrev->has_children;
93311388 19186
3019eac3
DE
19187 /* Make the result usable.
19188 The caller needs to update num_attrs after adding the extra
19189 attributes. */
93311388
DE
19190 die->num_attrs = abbrev->num_attrs;
19191
7a5f294d 19192 bool any_need_reprocess = false;
93311388 19193 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19194 {
7a5f294d
TT
19195 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19196 info_ptr);
19197 if (die->attrs[i].requires_reprocessing_p ())
19198 any_need_reprocess = true;
18a8505e
AT
19199 }
19200
052c8bb8 19201 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19202 if (attr != nullptr && attr->form_is_unsigned ())
19203 cu->str_offsets_base = attr->as_unsigned ();
93311388 19204
41144253 19205 attr = die->attr (DW_AT_loclists_base);
19206 if (attr != nullptr)
529908cb 19207 cu->loclist_base = attr->as_unsigned ();
41144253 19208
a39fdb41 19209 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19210 if (maybe_addr_base.has_value ())
19211 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19212
19213 attr = die->attr (DW_AT_rnglists_base);
19214 if (attr != nullptr)
529908cb 19215 cu->ranges_base = attr->as_unsigned ();
d0ce17d8 19216
7a5f294d
TT
19217 if (any_need_reprocess)
19218 {
19219 for (i = 0; i < abbrev->num_attrs; ++i)
19220 {
19221 if (die->attrs[i].requires_reprocessing_p ())
19222 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19223 }
19224 }
93311388 19225 *diep = die;
93311388
DE
19226 return info_ptr;
19227}
19228
3019eac3
DE
19229/* Read a die and all its attributes.
19230 Set DIEP to point to a newly allocated die with its information,
3e225074 19231 except for its child, sibling, and parent fields. */
3019eac3 19232
d521ce57 19233static const gdb_byte *
3019eac3 19234read_full_die (const struct die_reader_specs *reader,
3e225074 19235 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19236{
d521ce57 19237 const gdb_byte *result;
bf6af496 19238
3e225074 19239 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19240
b4f54984 19241 if (dwarf_die_debug)
bf6af496
DE
19242 {
19243 fprintf_unfiltered (gdb_stdlog,
19244 "Read die from %s@0x%x of %s:\n",
96b79293 19245 reader->die_section->get_name (),
bf6af496
DE
19246 (unsigned) (info_ptr - reader->die_section->buffer),
19247 bfd_get_filename (reader->abfd));
b4f54984 19248 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19249 }
19250
19251 return result;
3019eac3 19252}
433df2d4 19253\f
c906108c 19254
72bf9492
DJ
19255/* Returns nonzero if TAG represents a type that we might generate a partial
19256 symbol for. */
19257
19258static int
d8f62e84 19259is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19260{
19261 switch (tag)
19262 {
19263#if 0
19264 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19265 that we don't at present. Note that normally this does not
19266 matter, mainly because C compilers don't give names to these
19267 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19268 case DW_TAG_file_type:
19269 case DW_TAG_ptr_to_member_type:
19270 case DW_TAG_set_type:
19271 case DW_TAG_string_type:
19272 case DW_TAG_subroutine_type:
19273#endif
d8f62e84
TT
19274
19275 /* GNAT may emit an array with a name, but no typedef, so we
19276 need to make a symbol in this case. */
19277 case DW_TAG_array_type:
19278 return lang == language_ada;
19279
72bf9492
DJ
19280 case DW_TAG_base_type:
19281 case DW_TAG_class_type:
680b30c7 19282 case DW_TAG_interface_type:
72bf9492
DJ
19283 case DW_TAG_enumeration_type:
19284 case DW_TAG_structure_type:
19285 case DW_TAG_subrange_type:
19286 case DW_TAG_typedef:
19287 case DW_TAG_union_type:
19288 return 1;
19289 default:
19290 return 0;
19291 }
19292}
19293
19294/* Load all DIEs that are interesting for partial symbols into memory. */
19295
19296static struct partial_die_info *
dee91e82 19297load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19298 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19299{
dee91e82 19300 struct dwarf2_cu *cu = reader->cu;
5e22e966 19301 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19302 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19303 unsigned int bytes_read;
5afb4e99 19304 unsigned int load_all = 0;
72bf9492
DJ
19305 int nesting_level = 1;
19306
19307 parent_die = NULL;
19308 last_die = NULL;
19309
7adf1e79
DE
19310 gdb_assert (cu->per_cu != NULL);
19311 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19312 load_all = 1;
19313
72bf9492
DJ
19314 cu->partial_dies
19315 = htab_create_alloc_ex (cu->header.length / 12,
19316 partial_die_hash,
19317 partial_die_eq,
19318 NULL,
19319 &cu->comp_unit_obstack,
19320 hashtab_obstack_allocate,
19321 dummy_obstack_deallocate);
19322
72bf9492
DJ
19323 while (1)
19324 {
685af9cd 19325 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
19326
19327 /* A NULL abbrev means the end of a series of children. */
19328 if (abbrev == NULL)
19329 {
19330 if (--nesting_level == 0)
cd9983dd
YQ
19331 return first_die;
19332
72bf9492
DJ
19333 info_ptr += bytes_read;
19334 last_die = parent_die;
19335 parent_die = parent_die->die_parent;
19336 continue;
19337 }
19338
98bfdba5
PA
19339 /* Check for template arguments. We never save these; if
19340 they're seen, we just mark the parent, and go on our way. */
19341 if (parent_die != NULL
19342 && cu->language == language_cplus
19343 && (abbrev->tag == DW_TAG_template_type_param
19344 || abbrev->tag == DW_TAG_template_value_param))
19345 {
19346 parent_die->has_template_arguments = 1;
19347
19348 if (!load_all)
19349 {
19350 /* We don't need a partial DIE for the template argument. */
dee91e82 19351 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19352 continue;
19353 }
19354 }
19355
0d99eb77 19356 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19357 Skip their other children. */
19358 if (!load_all
19359 && cu->language == language_cplus
19360 && parent_die != NULL
f9b5d5ea
TV
19361 && parent_die->tag == DW_TAG_subprogram
19362 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19363 {
dee91e82 19364 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19365 continue;
19366 }
19367
5afb4e99
DJ
19368 /* Check whether this DIE is interesting enough to save. Normally
19369 we would not be interested in members here, but there may be
19370 later variables referencing them via DW_AT_specification (for
19371 static members). */
19372 if (!load_all
d8f62e84 19373 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19374 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19375 && abbrev->tag != DW_TAG_enumerator
19376 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19377 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19378 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19379 && abbrev->tag != DW_TAG_variable
5afb4e99 19380 && abbrev->tag != DW_TAG_namespace
f55ee35c 19381 && abbrev->tag != DW_TAG_module
95554aad 19382 && abbrev->tag != DW_TAG_member
74921315
KS
19383 && abbrev->tag != DW_TAG_imported_unit
19384 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19385 {
19386 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19387 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19388 continue;
19389 }
19390
6f06d47b
YQ
19391 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19392 abbrev);
cd9983dd 19393
48fbe735 19394 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19395
19396 /* This two-pass algorithm for processing partial symbols has a
19397 high cost in cache pressure. Thus, handle some simple cases
19398 here which cover the majority of C partial symbols. DIEs
19399 which neither have specification tags in them, nor could have
19400 specification tags elsewhere pointing at them, can simply be
19401 processed and discarded.
19402
19403 This segment is also optional; scan_partial_symbols and
19404 add_partial_symbol will handle these DIEs if we chain
19405 them in normally. When compilers which do not emit large
19406 quantities of duplicate debug information are more common,
19407 this code can probably be removed. */
19408
19409 /* Any complete simple types at the top level (pretty much all
19410 of them, for a language without namespaces), can be processed
19411 directly. */
19412 if (parent_die == NULL
cd9983dd
YQ
19413 && pdi.has_specification == 0
19414 && pdi.is_declaration == 0
19415 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19416 || pdi.tag == DW_TAG_base_type
d8f62e84 19417 || pdi.tag == DW_TAG_array_type
cd9983dd 19418 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19419 {
7d00ffec 19420 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19421 add_partial_symbol (&pdi, cu);
19422
cd9983dd 19423 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19424 continue;
19425 }
19426
d8228535
JK
19427 /* The exception for DW_TAG_typedef with has_children above is
19428 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19429 type_name_or_error will error on such types later.
d8228535
JK
19430
19431 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19432 it could not find the child DIEs referenced later, this is checked
19433 above. In correct DWARF DW_TAG_typedef should have no children. */
19434
cd9983dd 19435 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19436 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19437 "- DIE at %s [in module %s]"),
cd9983dd 19438 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19439
72bf9492
DJ
19440 /* If we're at the second level, and we're an enumerator, and
19441 our parent has no specification (meaning possibly lives in a
19442 namespace elsewhere), then we can add the partial symbol now
19443 instead of queueing it. */
cd9983dd 19444 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19445 && parent_die != NULL
19446 && parent_die->die_parent == NULL
19447 && parent_die->tag == DW_TAG_enumeration_type
19448 && parent_die->has_specification == 0)
19449 {
7d00ffec 19450 if (pdi.raw_name == NULL)
b98664d3 19451 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19452 else if (building_psymtab)
f0fbb768 19453 add_partial_symbol (&pdi, cu);
72bf9492 19454
cd9983dd 19455 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19456 continue;
19457 }
19458
cd9983dd 19459 struct partial_die_info *part_die
6f06d47b 19460 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19461
72bf9492
DJ
19462 /* We'll save this DIE so link it in. */
19463 part_die->die_parent = parent_die;
19464 part_die->die_sibling = NULL;
19465 part_die->die_child = NULL;
19466
19467 if (last_die && last_die == parent_die)
19468 last_die->die_child = part_die;
19469 else if (last_die)
19470 last_die->die_sibling = part_die;
19471
19472 last_die = part_die;
19473
19474 if (first_die == NULL)
19475 first_die = part_die;
19476
19477 /* Maybe add the DIE to the hash table. Not all DIEs that we
19478 find interesting need to be in the hash table, because we
19479 also have the parent/sibling/child chains; only those that we
19480 might refer to by offset later during partial symbol reading.
19481
19482 For now this means things that might have be the target of a
19483 DW_AT_specification, DW_AT_abstract_origin, or
19484 DW_AT_extension. DW_AT_extension will refer only to
19485 namespaces; DW_AT_abstract_origin refers to functions (and
19486 many things under the function DIE, but we do not recurse
19487 into function DIEs during partial symbol reading) and
19488 possibly variables as well; DW_AT_specification refers to
19489 declarations. Declarations ought to have the DW_AT_declaration
19490 flag. It happens that GCC forgets to put it in sometimes, but
19491 only for functions, not for types.
19492
19493 Adding more things than necessary to the hash table is harmless
19494 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19495 wasted time in find_partial_die, when we reread the compilation
19496 unit with load_all_dies set. */
72bf9492 19497
5afb4e99 19498 if (load_all
72929c62 19499 || abbrev->tag == DW_TAG_constant
5afb4e99 19500 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19501 || abbrev->tag == DW_TAG_variable
19502 || abbrev->tag == DW_TAG_namespace
19503 || part_die->is_declaration)
19504 {
19505 void **slot;
19506
19507 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19508 to_underlying (part_die->sect_off),
19509 INSERT);
72bf9492
DJ
19510 *slot = part_die;
19511 }
19512
72bf9492 19513 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19514 we have no reason to follow the children of structures; for other
98bfdba5
PA
19515 languages we have to, so that we can get at method physnames
19516 to infer fully qualified class names, for DW_AT_specification,
19517 and for C++ template arguments. For C++, we also look one level
19518 inside functions to find template arguments (if the name of the
19519 function does not already contain the template arguments).
bc30ff58 19520
0a4b0913
AB
19521 For Ada and Fortran, we need to scan the children of subprograms
19522 and lexical blocks as well because these languages allow the
19523 definition of nested entities that could be interesting for the
19524 debugger, such as nested subprograms for instance. */
72bf9492 19525 if (last_die->has_children
5afb4e99
DJ
19526 && (load_all
19527 || last_die->tag == DW_TAG_namespace
f55ee35c 19528 || last_die->tag == DW_TAG_module
72bf9492 19529 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19530 || (cu->language == language_cplus
19531 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19532 && (last_die->raw_name == NULL
19533 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19534 || (cu->language != language_c
19535 && (last_die->tag == DW_TAG_class_type
680b30c7 19536 || last_die->tag == DW_TAG_interface_type
72bf9492 19537 || last_die->tag == DW_TAG_structure_type
bc30ff58 19538 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19539 || ((cu->language == language_ada
19540 || cu->language == language_fortran)
bc30ff58
JB
19541 && (last_die->tag == DW_TAG_subprogram
19542 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19543 {
19544 nesting_level++;
19545 parent_die = last_die;
19546 continue;
19547 }
19548
19549 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19550 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19551
19552 /* Back to the top, do it again. */
19553 }
19554}
19555
6f06d47b
YQ
19556partial_die_info::partial_die_info (sect_offset sect_off_,
19557 struct abbrev_info *abbrev)
19558 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19559{
19560}
19561
7d00ffec
TT
19562/* See class definition. */
19563
19564const char *
19565partial_die_info::name (dwarf2_cu *cu)
19566{
19567 if (!canonical_name && raw_name != nullptr)
19568 {
19569 struct objfile *objfile = cu->per_objfile->objfile;
19570 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19571 canonical_name = 1;
19572 }
19573
19574 return raw_name;
19575}
19576
35cc7ed7
YQ
19577/* Read a minimal amount of information into the minimal die structure.
19578 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19579
48fbe735
YQ
19580const gdb_byte *
19581partial_die_info::read (const struct die_reader_specs *reader,
19582 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19583{
dee91e82 19584 struct dwarf2_cu *cu = reader->cu;
976ca316 19585 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19586 unsigned int i;
c5aa993b 19587 int has_low_pc_attr = 0;
c906108c 19588 int has_high_pc_attr = 0;
91da1414 19589 int high_pc_relative = 0;
c906108c 19590
fd0a254f 19591 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19592 {
e7da7f8f 19593 attribute attr;
7a5f294d 19594 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19595 /* String and address offsets that need to do the reprocessing have
dda83cd7 19596 already been read at this point, so there is no need to wait until
18a8505e 19597 the loop terminates to do the reprocessing. */
7a5f294d 19598 if (attr.requires_reprocessing_p ())
d0ce17d8 19599 read_attribute_reprocess (reader, &attr, tag);
c906108c 19600 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19601 partial symbol table. */
c906108c
SS
19602 switch (attr.name)
19603 {
19604 case DW_AT_name:
48fbe735 19605 switch (tag)
71c25dea
TT
19606 {
19607 case DW_TAG_compile_unit:
95554aad 19608 case DW_TAG_partial_unit:
348e048f 19609 case DW_TAG_type_unit:
71c25dea
TT
19610 /* Compilation units have a DW_AT_name that is a filename, not
19611 a source language identifier. */
19612 case DW_TAG_enumeration_type:
19613 case DW_TAG_enumerator:
19614 /* These tags always have simple identifiers already; no need
19615 to canonicalize them. */
7d00ffec 19616 canonical_name = 1;
2c830f54 19617 raw_name = attr.as_string ();
71c25dea
TT
19618 break;
19619 default:
7d00ffec 19620 canonical_name = 0;
2c830f54 19621 raw_name = attr.as_string ();
71c25dea
TT
19622 break;
19623 }
c906108c 19624 break;
31ef98ae 19625 case DW_AT_linkage_name:
c906108c 19626 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19627 /* Note that both forms of linkage name might appear. We
19628 assume they will be the same, and we only store the last
19629 one we see. */
95f982e5 19630 linkage_name = attr.as_string ();
c906108c
SS
19631 break;
19632 case DW_AT_low_pc:
19633 has_low_pc_attr = 1;
95f982e5 19634 lowpc = attr.as_address ();
c906108c
SS
19635 break;
19636 case DW_AT_high_pc:
19637 has_high_pc_attr = 1;
95f982e5 19638 highpc = attr.as_address ();
cd6c91b4 19639 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19640 high_pc_relative = 1;
c906108c
SS
19641 break;
19642 case DW_AT_location:
dda83cd7
SM
19643 /* Support the .debug_loc offsets. */
19644 if (attr.form_is_block ())
19645 {
9d2246fc 19646 d.locdesc = attr.as_block ();
dda83cd7
SM
19647 }
19648 else if (attr.form_is_section_offset ())
19649 {
4d3c2250 19650 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19651 }
19652 else
19653 {
4d3c2250
KB
19654 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19655 "partial symbol information");
dda83cd7 19656 }
c906108c 19657 break;
c906108c 19658 case DW_AT_external:
c45bc3f8 19659 is_external = attr.as_boolean ();
c906108c
SS
19660 break;
19661 case DW_AT_declaration:
c45bc3f8 19662 is_declaration = attr.as_boolean ();
c906108c
SS
19663 break;
19664 case DW_AT_type:
48fbe735 19665 has_type = 1;
c906108c
SS
19666 break;
19667 case DW_AT_abstract_origin:
19668 case DW_AT_specification:
72bf9492 19669 case DW_AT_extension:
48fbe735 19670 has_specification = 1;
0826b30a 19671 spec_offset = attr.get_ref_die_offset ();
48fbe735 19672 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19673 || cu->per_cu->is_dwz);
c906108c
SS
19674 break;
19675 case DW_AT_sibling:
19676 /* Ignore absolute siblings, they might point outside of
19677 the current compile unit. */
19678 if (attr.form == DW_FORM_ref_addr)
b98664d3 19679 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19680 else
b9502d3f 19681 {
48fbe735 19682 const gdb_byte *buffer = reader->buffer;
0826b30a 19683 sect_offset off = attr.get_ref_die_offset ();
9c541725 19684 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19685
19686 if (sibling_ptr < info_ptr)
b98664d3 19687 complaint (_("DW_AT_sibling points backwards"));
22869d73 19688 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19689 reader->die_section->overflow_complaint ();
b9502d3f 19690 else
48fbe735 19691 sibling = sibling_ptr;
b9502d3f 19692 }
c906108c 19693 break;
dda83cd7
SM
19694 case DW_AT_byte_size:
19695 has_byte_size = 1;
19696 break;
19697 case DW_AT_const_value:
19698 has_const_value = 1;
19699 break;
68511cec
CES
19700 case DW_AT_calling_convention:
19701 /* DWARF doesn't provide a way to identify a program's source-level
19702 entry point. DW_AT_calling_convention attributes are only meant
19703 to describe functions' calling conventions.
19704
19705 However, because it's a necessary piece of information in
0c1b455e
TT
19706 Fortran, and before DWARF 4 DW_CC_program was the only
19707 piece of debugging information whose definition refers to
19708 a 'main program' at all, several compilers marked Fortran
19709 main programs with DW_CC_program --- even when those
19710 functions use the standard calling conventions.
19711
19712 Although DWARF now specifies a way to provide this
19713 information, we support this practice for backward
19714 compatibility. */
529908cb 19715 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19716 && cu->language == language_fortran)
48fbe735 19717 main_subprogram = 1;
68511cec 19718 break;
481860b3 19719 case DW_AT_inline:
529908cb
TT
19720 {
19721 LONGEST value = attr.constant_value (-1);
19722 if (value == DW_INL_inlined
19723 || value == DW_INL_declared_inlined)
19724 may_be_inlined = 1;
19725 }
481860b3 19726 break;
95554aad
TT
19727
19728 case DW_AT_import:
48fbe735 19729 if (tag == DW_TAG_imported_unit)
36586728 19730 {
0826b30a 19731 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19732 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19733 || cu->per_cu->is_dwz);
19734 }
95554aad
TT
19735 break;
19736
0c1b455e 19737 case DW_AT_main_subprogram:
c45bc3f8 19738 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19739 break;
19740
05caa1d2
TT
19741 case DW_AT_ranges:
19742 {
d0ce17d8
CT
19743 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19744 skeleton. We take advantage of the fact the DW_AT_ranges
19745 does not appear in DW_TAG_compile_unit of DWO files.
19746
19747 Attributes of the form DW_FORM_rnglistx have already had
dda83cd7 19748 their value changed by read_rnglist_index and already
d0ce17d8
CT
19749 include DW_AT_rnglists_base, so don't need to add the ranges
19750 base, either. */
19751 int need_ranges_base = (tag != DW_TAG_compile_unit
19752 && attr.form != DW_FORM_rnglistx);
529908cb
TT
19753 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19754 but that requires a full DIE, so instead we just
19755 reimplement it. */
19756 unsigned int ranges_offset = (attr.constant_value (0)
05caa1d2
TT
19757 + (need_ranges_base
19758 ? cu->ranges_base
19759 : 0));
19760
19761 /* Value of the DW_AT_ranges attribute is the offset in the
19762 .debug_ranges section. */
19763 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19764 nullptr, tag))
05caa1d2
TT
19765 has_pc_info = 1;
19766 }
19767 break;
19768
c906108c
SS
19769 default:
19770 break;
19771 }
19772 }
19773
10d06d82
TT
19774 /* For Ada, if both the name and the linkage name appear, we prefer
19775 the latter. This lets "catch exception" work better, regardless
19776 of the order in which the name and linkage name were emitted.
19777 Really, though, this is just a workaround for the fact that gdb
19778 doesn't store both the name and the linkage name. */
19779 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19780 raw_name = linkage_name;
10d06d82 19781
91da1414 19782 if (high_pc_relative)
48fbe735 19783 highpc += lowpc;
91da1414 19784
9373cf26
JK
19785 if (has_low_pc_attr && has_high_pc_attr)
19786 {
19787 /* When using the GNU linker, .gnu.linkonce. sections are used to
19788 eliminate duplicate copies of functions and vtables and such.
19789 The linker will arbitrarily choose one and discard the others.
19790 The AT_*_pc values for such functions refer to local labels in
19791 these sections. If the section from that file was discarded, the
19792 labels are not in the output, so the relocs get a value of 0.
19793 If this is a discarded function, mark the pc bounds as invalid,
19794 so that GDB will ignore it. */
976ca316 19795 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19796 {
976ca316 19797 struct objfile *objfile = per_objfile->objfile;
08feed99 19798 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19799
b98664d3 19800 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19801 "for DIE at %s [in module %s]"),
48fbe735
YQ
19802 paddress (gdbarch, lowpc),
19803 sect_offset_str (sect_off),
9d8780f0 19804 objfile_name (objfile));
9373cf26
JK
19805 }
19806 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19807 else if (lowpc >= highpc)
9373cf26 19808 {
976ca316 19809 struct objfile *objfile = per_objfile->objfile;
08feed99 19810 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19811
b98664d3 19812 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19813 "for DIE at %s [in module %s]"),
48fbe735
YQ
19814 paddress (gdbarch, lowpc),
19815 paddress (gdbarch, highpc),
19816 sect_offset_str (sect_off),
9c541725 19817 objfile_name (objfile));
9373cf26
JK
19818 }
19819 else
48fbe735 19820 has_pc_info = 1;
9373cf26 19821 }
85cbf3d3 19822
c906108c
SS
19823 return info_ptr;
19824}
19825
72bf9492
DJ
19826/* Find a cached partial DIE at OFFSET in CU. */
19827
d590ff25
YQ
19828struct partial_die_info *
19829dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19830{
19831 struct partial_die_info *lookup_die = NULL;
6f06d47b 19832 struct partial_die_info part_die (sect_off);
72bf9492 19833
9a3c8263 19834 lookup_die = ((struct partial_die_info *)
d590ff25 19835 htab_find_with_hash (partial_dies, &part_die,
9c541725 19836 to_underlying (sect_off)));
72bf9492 19837
72bf9492
DJ
19838 return lookup_die;
19839}
19840
348e048f
DE
19841/* Find a partial DIE at OFFSET, which may or may not be in CU,
19842 except in the case of .debug_types DIEs which do not reference
19843 outside their CU (they do however referencing other types via
55f1336d 19844 DW_FORM_ref_sig8). */
72bf9492 19845
122cf0f2 19846static const struct cu_partial_die_info
9c541725 19847find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19848{
976ca316
SM
19849 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19850 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19851 struct partial_die_info *pd = NULL;
72bf9492 19852
36586728 19853 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19854 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19855 {
d590ff25 19856 pd = cu->find_partial_die (sect_off);
5afb4e99 19857 if (pd != NULL)
fb816e8b 19858 return { cu, pd };
0d99eb77
DE
19859 /* We missed recording what we needed.
19860 Load all dies and try again. */
5afb4e99 19861 }
0d99eb77
DE
19862 else
19863 {
19864 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19865 if (cu->per_cu->is_debug_types)
0d99eb77 19866 {
9d8780f0
SM
19867 error (_("Dwarf Error: Type Unit at offset %s contains"
19868 " external reference to offset %s [in module %s].\n"),
19869 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19870 bfd_get_filename (objfile->obfd));
19871 }
7188ed02
SM
19872 dwarf2_per_cu_data *per_cu
19873 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19874 per_objfile);
72bf9492 19875
976ca316 19876 cu = per_objfile->get_cu (per_cu);
7188ed02 19877 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19878 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19879
976ca316 19880 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19881
19882 cu->last_used = 0;
19883 pd = cu->find_partial_die (sect_off);
0d99eb77 19884 }
5afb4e99 19885
dee91e82
DE
19886 /* If we didn't find it, and not all dies have been loaded,
19887 load them all and try again. */
19888
7188ed02 19889 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19890 {
7188ed02 19891 cu->per_cu->load_all_dies = 1;
fd820528
DE
19892
19893 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19894 THIS_CU->cu may already be in use. So we can't just free it and
19895 replace its DIEs with the ones we read in. Instead, we leave those
19896 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19897 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19898 set. */
976ca316 19899 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19900
7188ed02 19901 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19902 }
19903
19904 if (pd == NULL)
521894aa 19905 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19906 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19907 return { cu, pd };
72bf9492
DJ
19908}
19909
abc72ce4
DE
19910/* See if we can figure out if the class lives in a namespace. We do
19911 this by looking for a member function; its demangled name will
19912 contain namespace info, if there is any. */
19913
19914static void
19915guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19916 struct dwarf2_cu *cu)
19917{
19918 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19919 what template types look like, because the demangler
19920 frequently doesn't give the same name as the debug info. We
19921 could fix this by only using the demangled name to get the
19922 prefix (but see comment in read_structure_type). */
19923
19924 struct partial_die_info *real_pdi;
19925 struct partial_die_info *child_pdi;
19926
19927 /* If this DIE (this DIE's specification, if any) has a parent, then
19928 we should not do this. We'll prepend the parent's fully qualified
19929 name when we create the partial symbol. */
19930
19931 real_pdi = struct_pdi;
19932 while (real_pdi->has_specification)
fb816e8b 19933 {
122cf0f2
AB
19934 auto res = find_partial_die (real_pdi->spec_offset,
19935 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
19936 real_pdi = res.pdi;
19937 cu = res.cu;
19938 }
abc72ce4
DE
19939
19940 if (real_pdi->die_parent != NULL)
19941 return;
19942
19943 for (child_pdi = struct_pdi->die_child;
19944 child_pdi != NULL;
19945 child_pdi = child_pdi->die_sibling)
19946 {
19947 if (child_pdi->tag == DW_TAG_subprogram
19948 && child_pdi->linkage_name != NULL)
19949 {
43816ebc 19950 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
19951 (cu->language_defn->class_name_from_physname
19952 (child_pdi->linkage_name));
abc72ce4
DE
19953 if (actual_class_name != NULL)
19954 {
5e22e966 19955 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
19956 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
19957 struct_pdi->canonical_name = 1;
abc72ce4
DE
19958 }
19959 break;
19960 }
19961 }
19962}
19963
25c11aca
TV
19964/* Return true if a DIE with TAG may have the DW_AT_const_value
19965 attribute. */
19966
19967static bool
19968can_have_DW_AT_const_value_p (enum dwarf_tag tag)
19969{
19970 switch (tag)
19971 {
19972 case DW_TAG_constant:
19973 case DW_TAG_enumerator:
19974 case DW_TAG_formal_parameter:
19975 case DW_TAG_template_value_param:
19976 case DW_TAG_variable:
19977 return true;
19978 }
19979
19980 return false;
19981}
19982
52356b79
YQ
19983void
19984partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 19985{
abc72ce4
DE
19986 /* Once we've fixed up a die, there's no point in doing so again.
19987 This also avoids a memory leak if we were to call
19988 guess_partial_die_structure_name multiple times. */
52356b79 19989 if (fixup_called)
abc72ce4
DE
19990 return;
19991
72bf9492
DJ
19992 /* If we found a reference attribute and the DIE has no name, try
19993 to find a name in the referred to DIE. */
19994
7d00ffec 19995 if (raw_name == NULL && has_specification)
72bf9492
DJ
19996 {
19997 struct partial_die_info *spec_die;
72bf9492 19998
122cf0f2 19999 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20000 spec_die = res.pdi;
20001 cu = res.cu;
72bf9492 20002
52356b79 20003 spec_die->fixup (cu);
72bf9492 20004
7d00ffec 20005 if (spec_die->raw_name)
72bf9492 20006 {
7d00ffec
TT
20007 raw_name = spec_die->raw_name;
20008 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20009
20010 /* Copy DW_AT_external attribute if it is set. */
20011 if (spec_die->is_external)
52356b79 20012 is_external = spec_die->is_external;
72bf9492
DJ
20013 }
20014 }
20015
25c11aca
TV
20016 if (!has_const_value && has_specification
20017 && can_have_DW_AT_const_value_p (tag))
20018 {
20019 struct partial_die_info *spec_die;
20020
20021 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20022 spec_die = res.pdi;
20023 cu = res.cu;
20024
20025 spec_die->fixup (cu);
20026
20027 if (spec_die->has_const_value)
20028 {
20029 /* Copy DW_AT_const_value attribute if it is set. */
20030 has_const_value = spec_die->has_const_value;
20031 }
20032 }
20033
72bf9492 20034 /* Set default names for some unnamed DIEs. */
72bf9492 20035
7d00ffec
TT
20036 if (raw_name == NULL && tag == DW_TAG_namespace)
20037 {
20038 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20039 canonical_name = 1;
20040 }
72bf9492 20041
abc72ce4
DE
20042 /* If there is no parent die to provide a namespace, and there are
20043 children, see if we can determine the namespace from their linkage
122d1940 20044 name. */
abc72ce4 20045 if (cu->language == language_cplus
5e22e966 20046 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20047 && die_parent == NULL
20048 && has_children
20049 && (tag == DW_TAG_class_type
20050 || tag == DW_TAG_structure_type
20051 || tag == DW_TAG_union_type))
20052 guess_partial_die_structure_name (this, cu);
abc72ce4 20053
53832f31
TT
20054 /* GCC might emit a nameless struct or union that has a linkage
20055 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20056 if (raw_name == NULL
52356b79
YQ
20057 && (tag == DW_TAG_class_type
20058 || tag == DW_TAG_interface_type
20059 || tag == DW_TAG_structure_type
20060 || tag == DW_TAG_union_type)
20061 && linkage_name != NULL)
53832f31 20062 {
43816ebc
TT
20063 gdb::unique_xmalloc_ptr<char> demangled
20064 (gdb_demangle (linkage_name, DMGL_TYPES));
20065 if (demangled != nullptr)
53832f31 20066 {
96408a79
SA
20067 const char *base;
20068
20069 /* Strip any leading namespaces/classes, keep only the base name.
20070 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20071 base = strrchr (demangled.get (), ':');
20072 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20073 base++;
20074 else
43816ebc 20075 base = demangled.get ();
96408a79 20076
5e22e966 20077 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20078 raw_name = objfile->intern (base);
20079 canonical_name = 1;
53832f31
TT
20080 }
20081 }
20082
52356b79 20083 fixup_called = 1;
72bf9492
DJ
20084}
20085
d0ce17d8
CT
20086/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
20087 contents from the given SECTION in the HEADER. */
41144253 20088static void
d0ce17d8
CT
20089read_loclists_rnglists_header (struct loclists_rnglists_header *header,
20090 struct dwarf2_section_info *section)
41144253 20091{
20092 unsigned int bytes_read;
20093 bfd *abfd = section->get_bfd_owner ();
20094 const gdb_byte *info_ptr = section->buffer;
20095 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20096 info_ptr += bytes_read;
20097 header->version = read_2_bytes (abfd, info_ptr);
20098 info_ptr += 2;
20099 header->addr_size = read_1_byte (abfd, info_ptr);
20100 info_ptr += 1;
20101 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20102 info_ptr += 1;
20103 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20104}
20105
20106/* Return the DW_AT_loclists_base value for the CU. */
20107static ULONGEST
20108lookup_loclist_base (struct dwarf2_cu *cu)
20109{
20110 /* For the .dwo unit, the loclist_base points to the first offset following
20111 the header. The header consists of the following entities-
20112 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20113 bit format)
20114 2. version (2 bytes)
20115 3. address size (1 byte)
20116 4. segment selector size (1 byte)
20117 5. offset entry count (4 bytes)
20118 These sizes are derived as per the DWARFv5 standard. */
20119 if (cu->dwo_unit != nullptr)
20120 {
20121 if (cu->header.initial_length_size == 4)
20122 return LOCLIST_HEADER_SIZE32;
20123 return LOCLIST_HEADER_SIZE64;
20124 }
20125 return cu->loclist_base;
20126}
20127
20128/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20129 array of offsets in the .debug_loclists section. */
20130static CORE_ADDR
20131read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20132{
976ca316
SM
20133 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20134 struct objfile *objfile = per_objfile->objfile;
41144253 20135 bfd *abfd = objfile->obfd;
20136 ULONGEST loclist_base = lookup_loclist_base (cu);
20137 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20138
20139 section->read (objfile);
20140 if (section->buffer == NULL)
20141 complaint (_("DW_FORM_loclistx used without .debug_loclists "
dda83cd7 20142 "section [in module %s]"), objfile_name (objfile));
d0ce17d8
CT
20143 struct loclists_rnglists_header header;
20144 read_loclists_rnglists_header (&header, section);
41144253 20145 if (loclist_index >= header.offset_entry_count)
20146 complaint (_("DW_FORM_loclistx pointing outside of "
dda83cd7
SM
20147 ".debug_loclists offset array [in module %s]"),
20148 objfile_name (objfile));
41144253 20149 if (loclist_base + loclist_index * cu->header.offset_size
20150 >= section->size)
20151 complaint (_("DW_FORM_loclistx pointing outside of "
dda83cd7
SM
20152 ".debug_loclists section [in module %s]"),
20153 objfile_name (objfile));
41144253 20154 const gdb_byte *info_ptr
20155 = section->buffer + loclist_base + loclist_index * cu->header.offset_size;
20156
20157 if (cu->header.offset_size == 4)
20158 return bfd_get_32 (abfd, info_ptr) + loclist_base;
20159 else
20160 return bfd_get_64 (abfd, info_ptr) + loclist_base;
20161}
20162
d0ce17d8
CT
20163/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20164 array of offsets in the .debug_rnglists section. */
20165static CORE_ADDR
20166read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20167 dwarf_tag tag)
20168{
20169 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20170 struct objfile *objfile = dwarf2_per_objfile->objfile;
20171 bfd *abfd = objfile->obfd;
20172 ULONGEST rnglist_header_size =
20173 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20174 : RNGLIST_HEADER_SIZE64);
20175 ULONGEST rnglist_base =
20176 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
20177 ULONGEST start_offset =
20178 rnglist_base + rnglist_index * cu->header.offset_size;
20179
20180 /* Get rnglists section. */
20181 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20182
20183 /* Read the rnglists section content. */
20184 section->read (objfile);
20185 if (section->buffer == nullptr)
20186 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20187 "[in module %s]"),
20188 objfile_name (objfile));
20189
20190 /* Verify the rnglist index is valid. */
20191 struct loclists_rnglists_header header;
20192 read_loclists_rnglists_header (&header, section);
20193 if (rnglist_index >= header.offset_entry_count)
20194 error (_("DW_FORM_rnglistx index pointing outside of "
20195 ".debug_rnglists offset array [in module %s]"),
20196 objfile_name (objfile));
20197
20198 /* Validate that the offset is within the section's range. */
20199 if (start_offset >= section->size)
20200 error (_("DW_FORM_rnglistx pointing outside of "
dda83cd7 20201 ".debug_rnglists section [in module %s]"),
d0ce17d8
CT
20202 objfile_name (objfile));
20203
20204 /* Validate that reading won't go beyond the end of the section. */
20205 if (start_offset + cu->header.offset_size > rnglist_base + section->size)
20206 error (_("Reading DW_FORM_rnglistx index beyond end of"
20207 ".debug_rnglists section [in module %s]"),
20208 objfile_name (objfile));
20209
20210 const gdb_byte *info_ptr = section->buffer + start_offset;
20211
20212 if (cu->header.offset_size == 4)
20213 return read_4_bytes (abfd, info_ptr) + rnglist_base;
20214 else
20215 return read_8_bytes (abfd, info_ptr) + rnglist_base;
20216}
20217
18a8505e
AT
20218/* Process the attributes that had to be skipped in the first round. These
20219 attributes are the ones that need str_offsets_base or addr_base attributes.
20220 They could not have been processed in the first round, because at the time
20221 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20222static void
20223read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20224 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20225{
20226 struct dwarf2_cu *cu = reader->cu;
20227 switch (attr->form)
20228 {
20229 case DW_FORM_addrx:
20230 case DW_FORM_GNU_addr_index:
36d378cf
TT
20231 attr->set_address (read_addr_index (cu,
20232 attr->as_unsigned_reprocess ()));
dda83cd7 20233 break;
41144253 20234 case DW_FORM_loclistx:
529908cb 20235 attr->set_address (read_loclist_index (cu, attr->as_unsigned ()));
41144253 20236 break;
d0ce17d8 20237 case DW_FORM_rnglistx:
529908cb 20238 attr->set_address (read_rnglist_index (cu, attr->as_unsigned (), tag));
dda83cd7 20239 break;
18a8505e
AT
20240 case DW_FORM_strx:
20241 case DW_FORM_strx1:
20242 case DW_FORM_strx2:
20243 case DW_FORM_strx3:
20244 case DW_FORM_strx4:
20245 case DW_FORM_GNU_str_index:
20246 {
fe56917a 20247 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20248 gdb_assert (!attr->canonical_string_p ());
18a8505e 20249 if (reader->dwo_file != NULL)
c6481205
TT
20250 attr->set_string_noncanonical (read_dwo_str_index (reader,
20251 str_index));
18a8505e 20252 else
c6481205
TT
20253 attr->set_string_noncanonical (read_stub_str_index (cu,
20254 str_index));
18a8505e
AT
20255 break;
20256 }
20257 default:
20258 gdb_assert_not_reached (_("Unexpected DWARF form."));
20259 }
20260}
20261
a8329558 20262/* Read an attribute value described by an attribute form. */
c906108c 20263
d521ce57 20264static const gdb_byte *
dee91e82
DE
20265read_attribute_value (const struct die_reader_specs *reader,
20266 struct attribute *attr, unsigned form,
7a5f294d 20267 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20268{
dee91e82 20269 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20270 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20271 struct objfile *objfile = per_objfile->objfile;
dee91e82 20272 bfd *abfd = reader->abfd;
e7c27a73 20273 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20274 unsigned int bytes_read;
20275 struct dwarf_block *blk;
20276
aead7601 20277 attr->form = (enum dwarf_form) form;
a8329558 20278 switch (form)
c906108c 20279 {
c906108c 20280 case DW_FORM_ref_addr:
ae411497 20281 if (cu->header.version == 2)
414ad644
TT
20282 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20283 &bytes_read));
ae411497 20284 else
414ad644
TT
20285 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20286 &bytes_read));
ae411497
TT
20287 info_ptr += bytes_read;
20288 break;
36586728 20289 case DW_FORM_GNU_ref_alt:
414ad644
TT
20290 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20291 &bytes_read));
36586728
TT
20292 info_ptr += bytes_read;
20293 break;
ae411497 20294 case DW_FORM_addr:
08feed99
TT
20295 {
20296 struct gdbarch *gdbarch = objfile->arch ();
36d378cf
TT
20297 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20298 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20299 attr->set_address (addr);
08feed99
TT
20300 info_ptr += bytes_read;
20301 }
c906108c
SS
20302 break;
20303 case DW_FORM_block2:
7b5a2f43 20304 blk = dwarf_alloc_block (cu);
c906108c
SS
20305 blk->size = read_2_bytes (abfd, info_ptr);
20306 info_ptr += 2;
20307 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20308 info_ptr += blk->size;
9d2246fc 20309 attr->set_block (blk);
c906108c
SS
20310 break;
20311 case DW_FORM_block4:
7b5a2f43 20312 blk = dwarf_alloc_block (cu);
c906108c
SS
20313 blk->size = read_4_bytes (abfd, info_ptr);
20314 info_ptr += 4;
20315 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20316 info_ptr += blk->size;
9d2246fc 20317 attr->set_block (blk);
c906108c
SS
20318 break;
20319 case DW_FORM_data2:
414ad644 20320 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20321 info_ptr += 2;
20322 break;
20323 case DW_FORM_data4:
414ad644 20324 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20325 info_ptr += 4;
20326 break;
20327 case DW_FORM_data8:
414ad644 20328 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20329 info_ptr += 8;
20330 break;
0224619f
JK
20331 case DW_FORM_data16:
20332 blk = dwarf_alloc_block (cu);
20333 blk->size = 16;
20334 blk->data = read_n_bytes (abfd, info_ptr, 16);
20335 info_ptr += 16;
9d2246fc 20336 attr->set_block (blk);
0224619f 20337 break;
2dc7f7b3 20338 case DW_FORM_sec_offset:
414ad644
TT
20339 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20340 &bytes_read));
2dc7f7b3
TT
20341 info_ptr += bytes_read;
20342 break;
41144253 20343 case DW_FORM_loclistx:
20344 {
7a5f294d
TT
20345 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20346 &bytes_read));
20347 info_ptr += bytes_read;
41144253 20348 }
20349 break;
c906108c 20350 case DW_FORM_string:
c6481205
TT
20351 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20352 &bytes_read));
c906108c
SS
20353 info_ptr += bytes_read;
20354 break;
4bdf3d34 20355 case DW_FORM_strp:
36586728
TT
20356 if (!cu->per_cu->is_dwz)
20357 {
c6481205
TT
20358 attr->set_string_noncanonical
20359 (read_indirect_string (per_objfile,
20360 abfd, info_ptr, cu_header,
20361 &bytes_read));
36586728
TT
20362 info_ptr += bytes_read;
20363 break;
20364 }
20365 /* FALLTHROUGH */
43988095
JK
20366 case DW_FORM_line_strp:
20367 if (!cu->per_cu->is_dwz)
20368 {
c6481205
TT
20369 attr->set_string_noncanonical
20370 (per_objfile->read_line_string (info_ptr, cu_header,
20371 &bytes_read));
43988095
JK
20372 info_ptr += bytes_read;
20373 break;
20374 }
20375 /* FALLTHROUGH */
36586728
TT
20376 case DW_FORM_GNU_strp_alt:
20377 {
976ca316 20378 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
20379 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20380 &bytes_read);
36586728 20381
c6481205
TT
20382 attr->set_string_noncanonical
20383 (dwz->read_string (objfile, str_offset));
36586728
TT
20384 info_ptr += bytes_read;
20385 }
4bdf3d34 20386 break;
2dc7f7b3 20387 case DW_FORM_exprloc:
c906108c 20388 case DW_FORM_block:
7b5a2f43 20389 blk = dwarf_alloc_block (cu);
c906108c
SS
20390 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20391 info_ptr += bytes_read;
20392 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20393 info_ptr += blk->size;
9d2246fc 20394 attr->set_block (blk);
c906108c
SS
20395 break;
20396 case DW_FORM_block1:
7b5a2f43 20397 blk = dwarf_alloc_block (cu);
c906108c
SS
20398 blk->size = read_1_byte (abfd, info_ptr);
20399 info_ptr += 1;
20400 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20401 info_ptr += blk->size;
9d2246fc 20402 attr->set_block (blk);
c906108c
SS
20403 break;
20404 case DW_FORM_data1:
c906108c 20405 case DW_FORM_flag:
414ad644 20406 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20407 info_ptr += 1;
20408 break;
2dc7f7b3 20409 case DW_FORM_flag_present:
414ad644 20410 attr->set_unsigned (1);
2dc7f7b3 20411 break;
c906108c 20412 case DW_FORM_sdata:
1bc397c5 20413 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20414 info_ptr += bytes_read;
20415 break;
18a8505e 20416 case DW_FORM_rnglistx:
7a5f294d
TT
20417 {
20418 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20419 &bytes_read));
20420 info_ptr += bytes_read;
20421 }
20422 break;
d0ce17d8 20423 case DW_FORM_udata:
414ad644 20424 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20425 info_ptr += bytes_read;
20426 break;
20427 case DW_FORM_ref1:
414ad644
TT
20428 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20429 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20430 info_ptr += 1;
20431 break;
20432 case DW_FORM_ref2:
414ad644
TT
20433 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20434 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20435 info_ptr += 2;
20436 break;
20437 case DW_FORM_ref4:
414ad644
TT
20438 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20439 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20440 info_ptr += 4;
20441 break;
613e1657 20442 case DW_FORM_ref8:
414ad644
TT
20443 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20444 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20445 info_ptr += 8;
20446 break;
55f1336d 20447 case DW_FORM_ref_sig8:
630ed6b9 20448 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20449 info_ptr += 8;
20450 break;
c906108c 20451 case DW_FORM_ref_udata:
414ad644
TT
20452 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20453 + read_unsigned_leb128 (abfd, info_ptr,
20454 &bytes_read)));
c906108c
SS
20455 info_ptr += bytes_read;
20456 break;
c906108c 20457 case DW_FORM_indirect:
a8329558
KW
20458 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20459 info_ptr += bytes_read;
43988095
JK
20460 if (form == DW_FORM_implicit_const)
20461 {
20462 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20463 info_ptr += bytes_read;
20464 }
20465 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20466 info_ptr);
43988095
JK
20467 break;
20468 case DW_FORM_implicit_const:
1bc397c5 20469 attr->set_signed (implicit_const);
a8329558 20470 break;
336d760d 20471 case DW_FORM_addrx:
3019eac3 20472 case DW_FORM_GNU_addr_index:
fe56917a
TT
20473 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20474 &bytes_read));
3019eac3
DE
20475 info_ptr += bytes_read;
20476 break;
cf532bd1 20477 case DW_FORM_strx:
15f18d14
AT
20478 case DW_FORM_strx1:
20479 case DW_FORM_strx2:
20480 case DW_FORM_strx3:
20481 case DW_FORM_strx4:
3019eac3 20482 case DW_FORM_GNU_str_index:
3019eac3 20483 {
15f18d14
AT
20484 ULONGEST str_index;
20485 if (form == DW_FORM_strx1)
20486 {
20487 str_index = read_1_byte (abfd, info_ptr);
20488 info_ptr += 1;
20489 }
20490 else if (form == DW_FORM_strx2)
20491 {
20492 str_index = read_2_bytes (abfd, info_ptr);
20493 info_ptr += 2;
20494 }
20495 else if (form == DW_FORM_strx3)
20496 {
20497 str_index = read_3_bytes (abfd, info_ptr);
20498 info_ptr += 3;
20499 }
20500 else if (form == DW_FORM_strx4)
20501 {
20502 str_index = read_4_bytes (abfd, info_ptr);
20503 info_ptr += 4;
20504 }
20505 else
20506 {
20507 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20508 info_ptr += bytes_read;
20509 }
fe56917a 20510 attr->set_unsigned_reprocess (str_index);
7a5f294d 20511 }
3019eac3 20512 break;
c906108c 20513 default:
8a3fe4f8 20514 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20515 dwarf_form_name (form),
20516 bfd_get_filename (abfd));
c906108c 20517 }
28e94949 20518
36586728 20519 /* Super hack. */
cd6c91b4 20520 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20521 attr->form = DW_FORM_GNU_ref_alt;
20522
28e94949
JB
20523 /* We have seen instances where the compiler tried to emit a byte
20524 size attribute of -1 which ended up being encoded as an unsigned
20525 0xffffffff. Although 0xffffffff is technically a valid size value,
20526 an object of this size seems pretty unlikely so we can relatively
20527 safely treat these cases as if the size attribute was invalid and
20528 treat them as zero by default. */
20529 if (attr->name == DW_AT_byte_size
20530 && form == DW_FORM_data4
529908cb 20531 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20532 {
20533 complaint
dda83cd7
SM
20534 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20535 hex_string (attr->as_unsigned ()));
414ad644 20536 attr->set_unsigned (0);
01c66ae6 20537 }
28e94949 20538
c906108c
SS
20539 return info_ptr;
20540}
20541
a8329558
KW
20542/* Read an attribute described by an abbreviated attribute. */
20543
d521ce57 20544static const gdb_byte *
dee91e82
DE
20545read_attribute (const struct die_reader_specs *reader,
20546 struct attribute *attr, struct attr_abbrev *abbrev,
7a5f294d 20547 const gdb_byte *info_ptr)
a8329558
KW
20548{
20549 attr->name = abbrev->name;
c6481205 20550 attr->string_is_canonical = 0;
fe56917a 20551 attr->requires_reprocessing = 0;
43988095 20552 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20553 abbrev->implicit_const, info_ptr);
a8329558
KW
20554}
20555
43988095
JK
20556/* Return pointer to string at .debug_str offset STR_OFFSET. */
20557
20558static const char *
976ca316 20559read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20560 LONGEST str_offset)
43988095 20561{
976ca316
SM
20562 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20563 str_offset, "DW_FORM_strp");
c906108c
SS
20564}
20565
43988095
JK
20566/* Return pointer to string at .debug_str offset as read from BUF.
20567 BUF is assumed to be in a compilation unit described by CU_HEADER.
20568 Return *BYTES_READ_PTR count of bytes read from BUF. */
20569
d521ce57 20570static const char *
976ca316 20571read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20572 const gdb_byte *buf,
cf2c3c16
TT
20573 const struct comp_unit_head *cu_header,
20574 unsigned int *bytes_read_ptr)
20575{
8266302d 20576 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20577
976ca316 20578 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20579}
20580
86c0bb4c 20581/* See read.h. */
43988095 20582
86c0bb4c
TT
20583const char *
20584dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20585 const struct comp_unit_head *cu_header,
20586 unsigned int *bytes_read_ptr)
43988095 20587{
86c0bb4c 20588 bfd *abfd = objfile->obfd;
8266302d 20589 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20590
5989a64e 20591 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20592}
20593
3019eac3 20594/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20595 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20596 ADDR_SIZE is the size of addresses from the CU header. */
20597
20598static CORE_ADDR
976ca316
SM
20599read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20600 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20601{
976ca316 20602 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20603 bfd *abfd = objfile->obfd;
20604 const gdb_byte *info_ptr;
18a8505e 20605 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20606
976ca316
SM
20607 per_objfile->per_bfd->addr.read (objfile);
20608 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20609 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20610 objfile_name (objfile));
18a8505e 20611 if (addr_base_or_zero + addr_index * addr_size
976ca316 20612 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20613 error (_("DW_FORM_addr_index pointing outside of "
20614 ".debug_addr section [in module %s]"),
4262abfb 20615 objfile_name (objfile));
976ca316
SM
20616 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20617 + addr_index * addr_size);
3019eac3
DE
20618 if (addr_size == 4)
20619 return bfd_get_32 (abfd, info_ptr);
20620 else
20621 return bfd_get_64 (abfd, info_ptr);
20622}
20623
20624/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20625
20626static CORE_ADDR
20627read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20628{
5e22e966 20629 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20630 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20631}
20632
20633/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20634
20635static CORE_ADDR
d521ce57 20636read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20637 unsigned int *bytes_read)
20638{
5e22e966 20639 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20640 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20641
20642 return read_addr_index (cu, addr_index);
20643}
20644
450a1bfc 20645/* See read.h. */
3019eac3
DE
20646
20647CORE_ADDR
82ca3f51 20648dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20649 dwarf2_per_objfile *per_objfile,
82ca3f51 20650 unsigned int addr_index)
3019eac3 20651{
976ca316 20652 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20653 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20654 int addr_size;
20655
3019eac3
DE
20656 /* We need addr_base and addr_size.
20657 If we don't have PER_CU->cu, we have to get it.
20658 Nasty, but the alternative is storing the needed info in PER_CU,
20659 which at this point doesn't seem justified: it's not clear how frequently
20660 it would get used and it would increase the size of every PER_CU.
20661 Entry points like dwarf2_per_cu_addr_size do a similar thing
20662 so we're not in uncharted territory here.
20663 Alas we need to be a bit more complicated as addr_base is contained
20664 in the DIE.
20665
20666 We don't need to read the entire CU(/TU).
20667 We just need the header and top level die.
a1b64ce1 20668
3019eac3 20669 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20670 For now we skip this optimization. */
3019eac3
DE
20671
20672 if (cu != NULL)
20673 {
20674 addr_base = cu->addr_base;
20675 addr_size = cu->header.addr_size;
20676 }
20677 else
20678 {
976ca316 20679 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20680 addr_base = reader.cu->addr_base;
20681 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20682 }
20683
976ca316 20684 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20685}
20686
18a8505e
AT
20687/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20688 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20689 DWO file. */
3019eac3 20690
d521ce57 20691static const char *
18a8505e
AT
20692read_str_index (struct dwarf2_cu *cu,
20693 struct dwarf2_section_info *str_section,
20694 struct dwarf2_section_info *str_offsets_section,
20695 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20696{
976ca316
SM
20697 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20698 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20699 const char *objf_name = objfile_name (objfile);
3019eac3 20700 bfd *abfd = objfile->obfd;
d521ce57 20701 const gdb_byte *info_ptr;
3019eac3 20702 ULONGEST str_offset;
cf532bd1 20703 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20704
96b79293
TT
20705 str_section->read (objfile);
20706 str_offsets_section->read (objfile);
73869dc2 20707 if (str_section->buffer == NULL)
18a8505e 20708 error (_("%s used without %s section"
9d8780f0 20709 " in CU at offset %s [in module %s]"),
96b79293 20710 form_name, str_section->get_name (),
dda83cd7 20711 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20712 if (str_offsets_section->buffer == NULL)
18a8505e 20713 error (_("%s used without %s section"
9d8780f0 20714 " in CU at offset %s [in module %s]"),
96b79293 20715 form_name, str_section->get_name (),
dda83cd7 20716 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20717 info_ptr = (str_offsets_section->buffer
18a8505e 20718 + str_offsets_base
3019eac3
DE
20719 + str_index * cu->header.offset_size);
20720 if (cu->header.offset_size == 4)
20721 str_offset = bfd_get_32 (abfd, info_ptr);
20722 else
20723 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20724 if (str_offset >= str_section->size)
57d63ce2 20725 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20726 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20727 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20728 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20729}
20730
18a8505e
AT
20731/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20732
20733static const char *
20734read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20735{
20736 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20737 ? reader->cu->header.addr_size : 0;
20738 return read_str_index (reader->cu,
20739 &reader->dwo_file->sections.str,
20740 &reader->dwo_file->sections.str_offsets,
20741 str_offsets_base, str_index);
20742}
20743
20744/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20745
20746static const char *
20747read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20748{
5e22e966 20749 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20750 const char *objf_name = objfile_name (objfile);
20751 static const char form_name[] = "DW_FORM_GNU_str_index";
20752 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20753
20754 if (!cu->str_offsets_base.has_value ())
20755 error (_("%s used in Fission stub without %s"
20756 " in CU at offset 0x%lx [in module %s]"),
20757 form_name, str_offsets_attr_name,
20758 (long) cu->header.offset_size, objf_name);
20759
20760 return read_str_index (cu,
5e22e966
SM
20761 &cu->per_objfile->per_bfd->str,
20762 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20763 *cu->str_offsets_base, str_index);
20764}
20765
3019eac3
DE
20766/* Return the length of an LEB128 number in BUF. */
20767
20768static int
20769leb128_size (const gdb_byte *buf)
20770{
20771 const gdb_byte *begin = buf;
20772 gdb_byte byte;
20773
20774 while (1)
20775 {
20776 byte = *buf++;
20777 if ((byte & 128) == 0)
20778 return buf - begin;
20779 }
20780}
20781
c906108c 20782static void
e142c38c 20783set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20784{
20785 switch (lang)
20786 {
20787 case DW_LANG_C89:
76bee0cc 20788 case DW_LANG_C99:
0cfd832f 20789 case DW_LANG_C11:
c906108c 20790 case DW_LANG_C:
d1be3247 20791 case DW_LANG_UPC:
e142c38c 20792 cu->language = language_c;
c906108c 20793 break;
9c37b5ae 20794 case DW_LANG_Java:
c906108c 20795 case DW_LANG_C_plus_plus:
0cfd832f
MW
20796 case DW_LANG_C_plus_plus_11:
20797 case DW_LANG_C_plus_plus_14:
e142c38c 20798 cu->language = language_cplus;
c906108c 20799 break;
6aecb9c2
JB
20800 case DW_LANG_D:
20801 cu->language = language_d;
20802 break;
c906108c
SS
20803 case DW_LANG_Fortran77:
20804 case DW_LANG_Fortran90:
b21b22e0 20805 case DW_LANG_Fortran95:
f7de9aab
MW
20806 case DW_LANG_Fortran03:
20807 case DW_LANG_Fortran08:
e142c38c 20808 cu->language = language_fortran;
c906108c 20809 break;
a766d390
DE
20810 case DW_LANG_Go:
20811 cu->language = language_go;
20812 break;
c906108c 20813 case DW_LANG_Mips_Assembler:
e142c38c 20814 cu->language = language_asm;
c906108c
SS
20815 break;
20816 case DW_LANG_Ada83:
8aaf0b47 20817 case DW_LANG_Ada95:
bc5f45f8
JB
20818 cu->language = language_ada;
20819 break;
72019c9c
GM
20820 case DW_LANG_Modula2:
20821 cu->language = language_m2;
20822 break;
fe8e67fd
PM
20823 case DW_LANG_Pascal83:
20824 cu->language = language_pascal;
20825 break;
22566fbd
DJ
20826 case DW_LANG_ObjC:
20827 cu->language = language_objc;
20828 break;
c44af4eb
TT
20829 case DW_LANG_Rust:
20830 case DW_LANG_Rust_old:
20831 cu->language = language_rust;
20832 break;
c906108c
SS
20833 case DW_LANG_Cobol74:
20834 case DW_LANG_Cobol85:
c906108c 20835 default:
e142c38c 20836 cu->language = language_minimal;
c906108c
SS
20837 break;
20838 }
e142c38c 20839 cu->language_defn = language_def (cu->language);
c906108c
SS
20840}
20841
20842/* Return the named attribute or NULL if not there. */
20843
20844static struct attribute *
e142c38c 20845dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20846{
a48e046c 20847 for (;;)
c906108c 20848 {
a48e046c
TT
20849 unsigned int i;
20850 struct attribute *spec = NULL;
20851
20852 for (i = 0; i < die->num_attrs; ++i)
20853 {
20854 if (die->attrs[i].name == name)
20855 return &die->attrs[i];
20856 if (die->attrs[i].name == DW_AT_specification
20857 || die->attrs[i].name == DW_AT_abstract_origin)
20858 spec = &die->attrs[i];
20859 }
20860
20861 if (!spec)
20862 break;
c906108c 20863
f2f0e013 20864 die = follow_die_ref (die, spec, &cu);
f2f0e013 20865 }
c5aa993b 20866
c906108c
SS
20867 return NULL;
20868}
20869
7d45c7c3
KB
20870/* Return the string associated with a string-typed attribute, or NULL if it
20871 is either not found or is of an incorrect type. */
20872
20873static const char *
20874dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
20875{
20876 struct attribute *attr;
20877 const char *str = NULL;
20878
20879 attr = dwarf2_attr (die, name, cu);
20880
20881 if (attr != NULL)
20882 {
95f982e5 20883 str = attr->as_string ();
e61108c9 20884 if (str == nullptr)
dda83cd7 20885 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
20886 "DIE at %s in module %s"),
20887 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 20888 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
20889 }
20890
20891 return str;
20892}
20893
a084a2a6 20894/* Return the dwo name or NULL if not present. If present, it is in either
85102364 20895 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
20896static const char *
20897dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
20898{
20899 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
20900 if (dwo_name == nullptr)
20901 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
20902 return dwo_name;
20903}
20904
05cf31d1
JB
20905/* Return non-zero iff the attribute NAME is defined for the given DIE,
20906 and holds a non-zero value. This function should only be used for
2dc7f7b3 20907 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
20908
20909static int
20910dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
20911{
20912 struct attribute *attr = dwarf2_attr (die, name, cu);
20913
c45bc3f8 20914 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
20915}
20916
3ca72b44 20917static int
e142c38c 20918die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 20919{
05cf31d1
JB
20920 /* A DIE is a declaration if it has a DW_AT_declaration attribute
20921 which value is non-zero. However, we have to be careful with
20922 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
20923 (via dwarf2_flag_true_p) follows this attribute. So we may
20924 end up accidently finding a declaration attribute that belongs
20925 to a different DIE referenced by the specification attribute,
20926 even though the given DIE does not have a declaration attribute. */
20927 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
20928 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
20929}
20930
63d06c5c 20931/* Return the die giving the specification for DIE, if there is
f2f0e013 20932 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
20933 containing the return value on output. If there is no
20934 specification, but there is an abstract origin, that is
20935 returned. */
63d06c5c
DC
20936
20937static struct die_info *
f2f0e013 20938die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 20939{
f2f0e013
DJ
20940 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
20941 *spec_cu);
63d06c5c 20942
edb3359d
DJ
20943 if (spec_attr == NULL)
20944 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
20945
63d06c5c
DC
20946 if (spec_attr == NULL)
20947 return NULL;
20948 else
f2f0e013 20949 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 20950}
c906108c 20951
527f3840
JK
20952/* Stub for free_line_header to match void * callback types. */
20953
20954static void
20955free_line_header_voidp (void *arg)
20956{
9a3c8263 20957 struct line_header *lh = (struct line_header *) arg;
527f3840 20958
fff8551c 20959 delete lh;
527f3840
JK
20960}
20961
83769d0b 20962/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
20963
20964static struct dwarf2_section_info *
20965get_debug_line_section (struct dwarf2_cu *cu)
20966{
20967 struct dwarf2_section_info *section;
976ca316 20968 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
20969
20970 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
20971 DWO file. */
20972 if (cu->dwo_unit && cu->per_cu->is_debug_types)
20973 section = &cu->dwo_unit->dwo_file->sections.line;
20974 else if (cu->per_cu->is_dwz)
20975 {
976ca316 20976 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
20977
20978 section = &dwz->line;
20979 }
20980 else
976ca316 20981 section = &per_objfile->per_bfd->line;
36586728
TT
20982
20983 return section;
20984}
20985
debd256d 20986/* Read the statement program header starting at OFFSET in
3019eac3 20987 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 20988 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
20989 Returns NULL if there is a problem reading the header, e.g., if it
20990 has a version we don't understand.
debd256d
JB
20991
20992 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
20993 the returned object point into the dwarf line section buffer,
20994 and must not be freed. */
ae2de4f8 20995
fff8551c 20996static line_header_up
9c541725 20997dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 20998{
3019eac3 20999 struct dwarf2_section_info *section;
976ca316 21000 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21001
36586728 21002 section = get_debug_line_section (cu);
976ca316 21003 section->read (per_objfile->objfile);
3019eac3 21004 if (section->buffer == NULL)
debd256d 21005 {
3019eac3 21006 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21007 complaint (_("missing .debug_line.dwo section"));
3019eac3 21008 else
b98664d3 21009 complaint (_("missing .debug_line section"));
debd256d
JB
21010 return 0;
21011 }
21012
0df7ad3a 21013 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21014 per_objfile, section, &cu->header);
debd256d 21015}
c906108c 21016
c6da4cef 21017/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21018 Return the file name of the psymtab for the given file_entry.
c6da4cef 21019 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21020 If space for the result is malloc'd, *NAME_HOLDER will be set.
21021 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21022
d521ce57 21023static const char *
7ba99d21 21024psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21025 const dwarf2_psymtab *pst,
c89b44cd
TT
21026 const char *comp_dir,
21027 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21028{
d521ce57
TT
21029 const char *include_name = fe.name;
21030 const char *include_name_to_compare = include_name;
72b9f47f 21031 const char *pst_filename;
c6da4cef
DE
21032 int file_is_pst;
21033
8c43009f 21034 const char *dir_name = fe.include_dir (lh);
c6da4cef 21035
c89b44cd 21036 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21037 if (!IS_ABSOLUTE_PATH (include_name)
21038 && (dir_name != NULL || comp_dir != NULL))
21039 {
21040 /* Avoid creating a duplicate psymtab for PST.
21041 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21042 Before we do the comparison, however, we need to account
21043 for DIR_NAME and COMP_DIR.
21044 First prepend dir_name (if non-NULL). If we still don't
21045 have an absolute path prepend comp_dir (if non-NULL).
21046 However, the directory we record in the include-file's
21047 psymtab does not contain COMP_DIR (to match the
21048 corresponding symtab(s)).
21049
21050 Example:
21051
21052 bash$ cd /tmp
21053 bash$ gcc -g ./hello.c
21054 include_name = "hello.c"
21055 dir_name = "."
21056 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21057 DW_AT_name = "./hello.c"
21058
21059 */
c6da4cef
DE
21060
21061 if (dir_name != NULL)
21062 {
c89b44cd
TT
21063 name_holder->reset (concat (dir_name, SLASH_STRING,
21064 include_name, (char *) NULL));
21065 include_name = name_holder->get ();
c6da4cef 21066 include_name_to_compare = include_name;
c6da4cef
DE
21067 }
21068 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21069 {
c89b44cd
TT
21070 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21071 include_name, (char *) NULL));
21072 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21073 }
21074 }
21075
21076 pst_filename = pst->filename;
c89b44cd 21077 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21078 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21079 {
c89b44cd
TT
21080 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21081 pst_filename, (char *) NULL));
21082 pst_filename = copied_name.get ();
c6da4cef
DE
21083 }
21084
1e3fad37 21085 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21086
c6da4cef
DE
21087 if (file_is_pst)
21088 return NULL;
21089 return include_name;
21090}
21091
d9b3de22
DE
21092/* State machine to track the state of the line number program. */
21093
6f77053d 21094class lnp_state_machine
d9b3de22 21095{
6f77053d
PA
21096public:
21097 /* Initialize a machine state for the start of a line number
21098 program. */
804d2729
TT
21099 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21100 bool record_lines_p);
6f77053d 21101
8c43009f
PA
21102 file_entry *current_file ()
21103 {
21104 /* lh->file_names is 0-based, but the file name numbers in the
21105 statement program are 1-based. */
6f77053d
PA
21106 return m_line_header->file_name_at (m_file);
21107 }
21108
21109 /* Record the line in the state machine. END_SEQUENCE is true if
21110 we're processing the end of a sequence. */
21111 void record_line (bool end_sequence);
21112
a8caed5d 21113 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21114 nop-out rest of the lines in this sequence. */
6f77053d
PA
21115 void check_line_address (struct dwarf2_cu *cu,
21116 const gdb_byte *line_ptr,
7ab6656f 21117 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21118
21119 void handle_set_discriminator (unsigned int discriminator)
21120 {
21121 m_discriminator = discriminator;
21122 m_line_has_non_zero_discriminator |= discriminator != 0;
21123 }
21124
21125 /* Handle DW_LNE_set_address. */
21126 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21127 {
21128 m_op_index = 0;
21129 address += baseaddr;
21130 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21131 }
21132
21133 /* Handle DW_LNS_advance_pc. */
21134 void handle_advance_pc (CORE_ADDR adjust);
21135
21136 /* Handle a special opcode. */
21137 void handle_special_opcode (unsigned char op_code);
21138
21139 /* Handle DW_LNS_advance_line. */
21140 void handle_advance_line (int line_delta)
21141 {
21142 advance_line (line_delta);
21143 }
21144
21145 /* Handle DW_LNS_set_file. */
21146 void handle_set_file (file_name_index file);
21147
21148 /* Handle DW_LNS_negate_stmt. */
21149 void handle_negate_stmt ()
21150 {
21151 m_is_stmt = !m_is_stmt;
21152 }
21153
21154 /* Handle DW_LNS_const_add_pc. */
21155 void handle_const_add_pc ();
21156
21157 /* Handle DW_LNS_fixed_advance_pc. */
21158 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21159 {
21160 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21161 m_op_index = 0;
21162 }
21163
21164 /* Handle DW_LNS_copy. */
21165 void handle_copy ()
21166 {
21167 record_line (false);
21168 m_discriminator = 0;
21169 }
21170
21171 /* Handle DW_LNE_end_sequence. */
21172 void handle_end_sequence ()
21173 {
804d2729 21174 m_currently_recording_lines = true;
6f77053d
PA
21175 }
21176
21177private:
21178 /* Advance the line by LINE_DELTA. */
21179 void advance_line (int line_delta)
21180 {
21181 m_line += line_delta;
21182
21183 if (line_delta != 0)
21184 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21185 }
21186
804d2729
TT
21187 struct dwarf2_cu *m_cu;
21188
6f77053d
PA
21189 gdbarch *m_gdbarch;
21190
21191 /* True if we're recording lines.
21192 Otherwise we're building partial symtabs and are just interested in
21193 finding include files mentioned by the line number program. */
21194 bool m_record_lines_p;
21195
8c43009f 21196 /* The line number header. */
6f77053d 21197 line_header *m_line_header;
8c43009f 21198
6f77053d
PA
21199 /* These are part of the standard DWARF line number state machine,
21200 and initialized according to the DWARF spec. */
d9b3de22 21201
6f77053d 21202 unsigned char m_op_index = 0;
7ba99d21
AT
21203 /* The line table index of the current file. */
21204 file_name_index m_file = 1;
6f77053d
PA
21205 unsigned int m_line = 1;
21206
21207 /* These are initialized in the constructor. */
21208
21209 CORE_ADDR m_address;
21210 bool m_is_stmt;
21211 unsigned int m_discriminator;
d9b3de22
DE
21212
21213 /* Additional bits of state we need to track. */
21214
21215 /* The last file that we called dwarf2_start_subfile for.
21216 This is only used for TLLs. */
6f77053d 21217 unsigned int m_last_file = 0;
d9b3de22 21218 /* The last file a line number was recorded for. */
6f77053d 21219 struct subfile *m_last_subfile = NULL;
d9b3de22 21220
1313c56e
AB
21221 /* The address of the last line entry. */
21222 CORE_ADDR m_last_address;
21223
21224 /* Set to true when a previous line at the same address (using
21225 m_last_address) had m_is_stmt true. This is reset to false when a
21226 line entry at a new address (m_address different to m_last_address) is
21227 processed. */
21228 bool m_stmt_at_address = false;
21229
804d2729
TT
21230 /* When true, record the lines we decode. */
21231 bool m_currently_recording_lines = false;
d9b3de22
DE
21232
21233 /* The last line number that was recorded, used to coalesce
21234 consecutive entries for the same line. This can happen, for
21235 example, when discriminators are present. PR 17276. */
6f77053d
PA
21236 unsigned int m_last_line = 0;
21237 bool m_line_has_non_zero_discriminator = false;
8c43009f 21238};
d9b3de22 21239
6f77053d
PA
21240void
21241lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21242{
21243 CORE_ADDR addr_adj = (((m_op_index + adjust)
21244 / m_line_header->maximum_ops_per_instruction)
21245 * m_line_header->minimum_instruction_length);
21246 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21247 m_op_index = ((m_op_index + adjust)
21248 % m_line_header->maximum_ops_per_instruction);
21249}
d9b3de22 21250
6f77053d
PA
21251void
21252lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21253{
6f77053d 21254 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21255 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21256 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21257 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21258 / m_line_header->maximum_ops_per_instruction)
21259 * m_line_header->minimum_instruction_length);
21260 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21261 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21262 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21263
258bf0ee 21264 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21265 advance_line (line_delta);
21266 record_line (false);
21267 m_discriminator = 0;
21268}
d9b3de22 21269
6f77053d
PA
21270void
21271lnp_state_machine::handle_set_file (file_name_index file)
21272{
21273 m_file = file;
21274
21275 const file_entry *fe = current_file ();
21276 if (fe == NULL)
21277 dwarf2_debug_line_missing_file_complaint ();
21278 else if (m_record_lines_p)
21279 {
21280 const char *dir = fe->include_dir (m_line_header);
21281
c24bdb02 21282 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21283 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21284 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21285 }
21286}
21287
21288void
21289lnp_state_machine::handle_const_add_pc ()
21290{
21291 CORE_ADDR adjust
21292 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21293
21294 CORE_ADDR addr_adj
21295 = (((m_op_index + adjust)
21296 / m_line_header->maximum_ops_per_instruction)
21297 * m_line_header->minimum_instruction_length);
21298
21299 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21300 m_op_index = ((m_op_index + adjust)
21301 % m_line_header->maximum_ops_per_instruction);
21302}
d9b3de22 21303
a05a36a5
DE
21304/* Return non-zero if we should add LINE to the line number table.
21305 LINE is the line to add, LAST_LINE is the last line that was added,
21306 LAST_SUBFILE is the subfile for LAST_LINE.
21307 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21308 had a non-zero discriminator.
21309
21310 We have to be careful in the presence of discriminators.
21311 E.g., for this line:
21312
21313 for (i = 0; i < 100000; i++);
21314
21315 clang can emit four line number entries for that one line,
21316 each with a different discriminator.
21317 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21318
21319 However, we want gdb to coalesce all four entries into one.
21320 Otherwise the user could stepi into the middle of the line and
21321 gdb would get confused about whether the pc really was in the
21322 middle of the line.
21323
21324 Things are further complicated by the fact that two consecutive
21325 line number entries for the same line is a heuristic used by gcc
21326 to denote the end of the prologue. So we can't just discard duplicate
21327 entries, we have to be selective about it. The heuristic we use is
21328 that we only collapse consecutive entries for the same line if at least
21329 one of those entries has a non-zero discriminator. PR 17276.
21330
21331 Note: Addresses in the line number state machine can never go backwards
21332 within one sequence, thus this coalescing is ok. */
21333
21334static int
804d2729
TT
21335dwarf_record_line_p (struct dwarf2_cu *cu,
21336 unsigned int line, unsigned int last_line,
a05a36a5
DE
21337 int line_has_non_zero_discriminator,
21338 struct subfile *last_subfile)
21339{
c24bdb02 21340 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21341 return 1;
21342 if (line != last_line)
21343 return 1;
21344 /* Same line for the same file that we've seen already.
21345 As a last check, for pr 17276, only record the line if the line
21346 has never had a non-zero discriminator. */
21347 if (!line_has_non_zero_discriminator)
21348 return 1;
21349 return 0;
21350}
21351
804d2729
TT
21352/* Use the CU's builder to record line number LINE beginning at
21353 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21354
21355static void
d9b3de22 21356dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21357 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21358 struct dwarf2_cu *cu)
252a6764
DE
21359{
21360 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21361
27e0867f
DE
21362 if (dwarf_line_debug)
21363 {
21364 fprintf_unfiltered (gdb_stdlog,
21365 "Recording line %u, file %s, address %s\n",
21366 line, lbasename (subfile->name),
21367 paddress (gdbarch, address));
21368 }
21369
804d2729 21370 if (cu != nullptr)
8c95582d 21371 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21372}
21373
21374/* Subroutine of dwarf_decode_lines_1 to simplify it.
21375 Mark the end of a set of line number records.
d9b3de22 21376 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21377 If SUBFILE is NULL the request is ignored. */
21378
21379static void
21380dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21381 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21382{
27e0867f
DE
21383 if (subfile == NULL)
21384 return;
21385
21386 if (dwarf_line_debug)
21387 {
21388 fprintf_unfiltered (gdb_stdlog,
21389 "Finishing current line, file %s, address %s\n",
21390 lbasename (subfile->name),
21391 paddress (gdbarch, address));
21392 }
21393
8c95582d 21394 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21395}
21396
6f77053d
PA
21397void
21398lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21399{
d9b3de22
DE
21400 if (dwarf_line_debug)
21401 {
21402 fprintf_unfiltered (gdb_stdlog,
21403 "Processing actual line %u: file %u,"
94a72be7 21404 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21405 m_line, m_file,
6f77053d 21406 paddress (m_gdbarch, m_address),
94a72be7
AB
21407 m_is_stmt, m_discriminator,
21408 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21409 }
21410
6f77053d 21411 file_entry *fe = current_file ();
8c43009f
PA
21412
21413 if (fe == NULL)
d9b3de22
DE
21414 dwarf2_debug_line_missing_file_complaint ();
21415 /* For now we ignore lines not starting on an instruction boundary.
21416 But not when processing end_sequence for compatibility with the
21417 previous version of the code. */
6f77053d 21418 else if (m_op_index == 0 || end_sequence)
d9b3de22 21419 {
8c43009f 21420 fe->included_p = 1;
8c95582d 21421 if (m_record_lines_p)
d9b3de22 21422 {
1313c56e
AB
21423 /* When we switch files we insert an end maker in the first file,
21424 switch to the second file and add a new line entry. The
21425 problem is that the end marker inserted in the first file will
21426 discard any previous line entries at the same address. If the
21427 line entries in the first file are marked as is-stmt, while
21428 the new line in the second file is non-stmt, then this means
21429 the end marker will discard is-stmt lines so we can have a
21430 non-stmt line. This means that there are less addresses at
21431 which the user can insert a breakpoint.
21432
21433 To improve this we track the last address in m_last_address,
21434 and whether we have seen an is-stmt at this address. Then
21435 when switching files, if we have seen a stmt at the current
21436 address, and we are switching to create a non-stmt line, then
21437 discard the new line. */
21438 bool file_changed
21439 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21440 bool ignore_this_line
dda83cd7
SM
21441 = ((file_changed && !end_sequence && m_last_address == m_address
21442 && !m_is_stmt && m_stmt_at_address)
21443 || (!end_sequence && m_line == 0));
1313c56e
AB
21444
21445 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21446 {
804d2729
TT
21447 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21448 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21449 }
21450
1313c56e 21451 if (!end_sequence && !ignore_this_line)
d9b3de22 21452 {
8c95582d
AB
21453 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21454
804d2729 21455 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21456 m_line_has_non_zero_discriminator,
21457 m_last_subfile))
d9b3de22 21458 {
c24bdb02 21459 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21460 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21461 builder->get_current_subfile (),
8c95582d 21462 m_line, m_address, is_stmt,
804d2729 21463 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21464 }
c24bdb02 21465 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21466 m_last_line = m_line;
d9b3de22
DE
21467 }
21468 }
21469 }
1313c56e
AB
21470
21471 /* Track whether we have seen any m_is_stmt true at m_address in case we
21472 have multiple line table entries all at m_address. */
21473 if (m_last_address != m_address)
21474 {
21475 m_stmt_at_address = false;
21476 m_last_address = m_address;
21477 }
21478 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21479}
21480
804d2729
TT
21481lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21482 line_header *lh, bool record_lines_p)
d9b3de22 21483{
804d2729 21484 m_cu = cu;
6f77053d
PA
21485 m_gdbarch = arch;
21486 m_record_lines_p = record_lines_p;
21487 m_line_header = lh;
d9b3de22 21488
804d2729 21489 m_currently_recording_lines = true;
d9b3de22 21490
d9b3de22
DE
21491 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21492 was a line entry for it so that the backend has a chance to adjust it
21493 and also record it in case it needs it. This is currently used by MIPS
21494 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21495 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21496 m_is_stmt = lh->default_is_stmt;
21497 m_discriminator = 0;
1313c56e
AB
21498
21499 m_last_address = m_address;
21500 m_stmt_at_address = false;
252a6764
DE
21501}
21502
6f77053d
PA
21503void
21504lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21505 const gdb_byte *line_ptr,
7ab6656f 21506 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21507{
a8caed5d
FS
21508 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21509 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21510 located at 0x0. In this case, additionally check that if
21511 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21512
a8caed5d
FS
21513 if ((address == 0 && address < unrelocated_lowpc)
21514 || address == (CORE_ADDR) -1)
924c2928
DE
21515 {
21516 /* This line table is for a function which has been
21517 GCd by the linker. Ignore it. PR gdb/12528 */
21518
5e22e966 21519 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21520 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21521
b98664d3 21522 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21523 line_offset, objfile_name (objfile));
804d2729
TT
21524 m_currently_recording_lines = false;
21525 /* Note: m_currently_recording_lines is left as false until we see
21526 DW_LNE_end_sequence. */
924c2928
DE
21527 }
21528}
21529
f3f5162e 21530/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21531 Process the line number information in LH.
21532 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21533 program in order to set included_p for every referenced header. */
debd256d 21534
c906108c 21535static void
43f3e411
DE
21536dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21537 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21538{
d521ce57
TT
21539 const gdb_byte *line_ptr, *extended_end;
21540 const gdb_byte *line_end;
a8c50c1f 21541 unsigned int bytes_read, extended_len;
699ca60a 21542 unsigned char op_code, extended_op;
e142c38c 21543 CORE_ADDR baseaddr;
5e22e966 21544 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21545 bfd *abfd = objfile->obfd;
08feed99 21546 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21547 /* True if we're recording line info (as opposed to building partial
21548 symtabs and just interested in finding include files mentioned by
21549 the line number program). */
21550 bool record_lines_p = !decode_for_pst_p;
e142c38c 21551
b3b3bada 21552 baseaddr = objfile->text_section_offset ();
c906108c 21553
debd256d
JB
21554 line_ptr = lh->statement_program_start;
21555 line_end = lh->statement_program_end;
c906108c
SS
21556
21557 /* Read the statement sequences until there's nothing left. */
21558 while (line_ptr < line_end)
21559 {
6f77053d
PA
21560 /* The DWARF line number program state machine. Reset the state
21561 machine at the start of each sequence. */
804d2729 21562 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21563 bool end_sequence = false;
d9b3de22 21564
8c43009f 21565 if (record_lines_p)
c906108c 21566 {
8c43009f
PA
21567 /* Start a subfile for the current file of the state
21568 machine. */
21569 const file_entry *fe = state_machine.current_file ();
21570
21571 if (fe != NULL)
804d2729 21572 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21573 }
21574
a738430d 21575 /* Decode the table. */
d9b3de22 21576 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21577 {
21578 op_code = read_1_byte (abfd, line_ptr);
21579 line_ptr += 1;
9aa1fe7e 21580
debd256d 21581 if (op_code >= lh->opcode_base)
6e70227d 21582 {
8e07a239 21583 /* Special opcode. */
6f77053d 21584 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21585 }
21586 else switch (op_code)
c906108c
SS
21587 {
21588 case DW_LNS_extended_op:
3e43a32a
MS
21589 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21590 &bytes_read);
473b7be6 21591 line_ptr += bytes_read;
a8c50c1f 21592 extended_end = line_ptr + extended_len;
c906108c
SS
21593 extended_op = read_1_byte (abfd, line_ptr);
21594 line_ptr += 1;
8f34b746
TV
21595 if (DW_LNE_lo_user <= extended_op
21596 && extended_op <= DW_LNE_hi_user)
21597 {
21598 /* Vendor extension, ignore. */
21599 line_ptr = extended_end;
21600 break;
21601 }
c906108c
SS
21602 switch (extended_op)
21603 {
21604 case DW_LNE_end_sequence:
6f77053d
PA
21605 state_machine.handle_end_sequence ();
21606 end_sequence = true;
c906108c
SS
21607 break;
21608 case DW_LNE_set_address:
d9b3de22
DE
21609 {
21610 CORE_ADDR address
c8a7a66f 21611 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21612 line_ptr += bytes_read;
6f77053d
PA
21613
21614 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21615 lowpc - baseaddr, address);
6f77053d 21616 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21617 }
c906108c
SS
21618 break;
21619 case DW_LNE_define_file:
dda83cd7
SM
21620 {
21621 const char *cur_file;
ecfb656c
PA
21622 unsigned int mod_time, length;
21623 dir_index dindex;
6e70227d 21624
dda83cd7 21625 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21626 &bytes_read);
dda83cd7
SM
21627 line_ptr += bytes_read;
21628 dindex = (dir_index)
21629 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21630 line_ptr += bytes_read;
21631 mod_time =
21632 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21633 line_ptr += bytes_read;
21634 length =
21635 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21636 line_ptr += bytes_read;
21637 lh->add_file_name (cur_file, dindex, mod_time, length);
21638 }
c906108c 21639 break;
d0c6ba3d 21640 case DW_LNE_set_discriminator:
6f77053d
PA
21641 {
21642 /* The discriminator is not interesting to the
21643 debugger; just ignore it. We still need to
21644 check its value though:
21645 if there are consecutive entries for the same
21646 (non-prologue) line we want to coalesce them.
21647 PR 17276. */
21648 unsigned int discr
21649 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21650 line_ptr += bytes_read;
21651
21652 state_machine.handle_set_discriminator (discr);
21653 }
d0c6ba3d 21654 break;
c906108c 21655 default:
b98664d3 21656 complaint (_("mangled .debug_line section"));
debd256d 21657 return;
c906108c 21658 }
a8c50c1f
DJ
21659 /* Make sure that we parsed the extended op correctly. If e.g.
21660 we expected a different address size than the producer used,
21661 we may have read the wrong number of bytes. */
21662 if (line_ptr != extended_end)
21663 {
b98664d3 21664 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21665 return;
21666 }
c906108c
SS
21667 break;
21668 case DW_LNS_copy:
6f77053d 21669 state_machine.handle_copy ();
c906108c
SS
21670 break;
21671 case DW_LNS_advance_pc:
2dc7f7b3
TT
21672 {
21673 CORE_ADDR adjust
21674 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21675 line_ptr += bytes_read;
6f77053d
PA
21676
21677 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21678 }
c906108c
SS
21679 break;
21680 case DW_LNS_advance_line:
a05a36a5
DE
21681 {
21682 int line_delta
21683 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21684 line_ptr += bytes_read;
6f77053d
PA
21685
21686 state_machine.handle_advance_line (line_delta);
a05a36a5 21687 }
c906108c
SS
21688 break;
21689 case DW_LNS_set_file:
d9b3de22 21690 {
6f77053d 21691 file_name_index file
ecfb656c
PA
21692 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21693 &bytes_read);
d9b3de22 21694 line_ptr += bytes_read;
8c43009f 21695
6f77053d 21696 state_machine.handle_set_file (file);
d9b3de22 21697 }
c906108c
SS
21698 break;
21699 case DW_LNS_set_column:
0ad93d4f 21700 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21701 line_ptr += bytes_read;
21702 break;
21703 case DW_LNS_negate_stmt:
6f77053d 21704 state_machine.handle_negate_stmt ();
c906108c
SS
21705 break;
21706 case DW_LNS_set_basic_block:
c906108c 21707 break;
c2c6d25f
JM
21708 /* Add to the address register of the state machine the
21709 address increment value corresponding to special opcode
a738430d
MK
21710 255. I.e., this value is scaled by the minimum
21711 instruction length since special opcode 255 would have
b021a221 21712 scaled the increment. */
c906108c 21713 case DW_LNS_const_add_pc:
6f77053d 21714 state_machine.handle_const_add_pc ();
c906108c
SS
21715 break;
21716 case DW_LNS_fixed_advance_pc:
3e29f34a 21717 {
6f77053d 21718 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21719 line_ptr += 2;
6f77053d
PA
21720
21721 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21722 }
c906108c 21723 break;
9aa1fe7e 21724 default:
a738430d
MK
21725 {
21726 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21727 int i;
a738430d 21728
debd256d 21729 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21730 {
21731 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21732 line_ptr += bytes_read;
21733 }
21734 }
c906108c
SS
21735 }
21736 }
d9b3de22
DE
21737
21738 if (!end_sequence)
21739 dwarf2_debug_line_missing_end_sequence_complaint ();
21740
21741 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21742 in which case we still finish recording the last line). */
6f77053d 21743 state_machine.record_line (true);
c906108c 21744 }
f3f5162e
DE
21745}
21746
21747/* Decode the Line Number Program (LNP) for the given line_header
21748 structure and CU. The actual information extracted and the type
21749 of structures created from the LNP depends on the value of PST.
21750
21751 1. If PST is NULL, then this procedure uses the data from the program
21752 to create all necessary symbol tables, and their linetables.
21753
21754 2. If PST is not NULL, this procedure reads the program to determine
21755 the list of files included by the unit represented by PST, and
21756 builds all the associated partial symbol tables.
21757
21758 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21759 It is used for relative paths in the line table.
21760 NOTE: When processing partial symtabs (pst != NULL),
21761 comp_dir == pst->dirname.
21762
21763 NOTE: It is important that psymtabs have the same file name (via strcmp)
21764 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21765 symtab we don't use it in the name of the psymtabs we create.
21766 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21767 A good testcase for this is mb-inline.exp.
21768
527f3840
JK
21769 LOWPC is the lowest address in CU (or 0 if not known).
21770
21771 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21772 for its PC<->lines mapping information. Otherwise only the filename
21773 table is read in. */
f3f5162e
DE
21774
21775static void
21776dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21777 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21778 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21779{
5e22e966 21780 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21781 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21782
527f3840
JK
21783 if (decode_mapping)
21784 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21785
21786 if (decode_for_pst_p)
21787 {
aaa75496 21788 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21789 create the psymtab of each included file. */
7ba99d21 21790 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21791 if (file_entry.included_p == 1)
21792 {
c89b44cd 21793 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21794 const char *include_name =
7ba99d21
AT
21795 psymtab_include_file_name (lh, file_entry, pst,
21796 comp_dir, &name_holder);
c6da4cef 21797 if (include_name != NULL)
dda83cd7
SM
21798 dwarf2_create_include_psymtab (include_name, pst, objfile);
21799 }
aaa75496 21800 }
cb1df416
DJ
21801 else
21802 {
21803 /* Make sure a symtab is created for every file, even files
21804 which contain only variables (i.e. no code with associated
21805 line numbers). */
c24bdb02
KS
21806 buildsym_compunit *builder = cu->get_builder ();
21807 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21808
7ba99d21 21809 for (auto &fe : lh->file_names ())
cb1df416 21810 {
804d2729 21811 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21812 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21813 {
c24bdb02 21814 builder->get_current_subfile ()->symtab
804d2729 21815 = allocate_symtab (cust,
c24bdb02 21816 builder->get_current_subfile ()->name);
43f3e411 21817 }
c24bdb02 21818 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21819 }
21820 }
c906108c
SS
21821}
21822
21823/* Start a subfile for DWARF. FILENAME is the name of the file and
21824 DIRNAME the name of the source directory which contains FILENAME
4d663531 21825 or NULL if not known.
c906108c
SS
21826 This routine tries to keep line numbers from identical absolute and
21827 relative file names in a common subfile.
21828
21829 Using the `list' example from the GDB testsuite, which resides in
21830 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21831 of /srcdir/list0.c yields the following debugging information for list0.c:
21832
c5aa993b 21833 DW_AT_name: /srcdir/list0.c
4d663531 21834 DW_AT_comp_dir: /compdir
357e46e7 21835 files.files[0].name: list0.h
c5aa993b 21836 files.files[0].dir: /srcdir
357e46e7 21837 files.files[1].name: list0.c
c5aa993b 21838 files.files[1].dir: /srcdir
c906108c
SS
21839
21840 The line number information for list0.c has to end up in a single
4f1520fb
FR
21841 subfile, so that `break /srcdir/list0.c:1' works as expected.
21842 start_subfile will ensure that this happens provided that we pass the
21843 concatenation of files.files[1].dir and files.files[1].name as the
21844 subfile's name. */
c906108c
SS
21845
21846static void
804d2729
TT
21847dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21848 const char *dirname)
c906108c 21849{
43816ebc 21850 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21851
4d663531 21852 /* In order not to lose the line information directory,
4f1520fb
FR
21853 we concatenate it to the filename when it makes sense.
21854 Note that the Dwarf3 standard says (speaking of filenames in line
21855 information): ``The directory index is ignored for file names
21856 that represent full path names''. Thus ignoring dirname in the
21857 `else' branch below isn't an issue. */
c906108c 21858
d5166ae1 21859 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21860 {
43816ebc
TT
21861 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21862 filename = copy.get ();
d521ce57 21863 }
c906108c 21864
c24bdb02 21865 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21866}
21867
804d2729
TT
21868/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21869 buildsym_compunit constructor. */
f4dc4d17 21870
c24bdb02
KS
21871struct compunit_symtab *
21872dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21873 CORE_ADDR low_pc)
f4dc4d17 21874{
c24bdb02 21875 gdb_assert (m_builder == nullptr);
43f3e411 21876
c24bdb02 21877 m_builder.reset (new struct buildsym_compunit
f6e649dd 21878 (this->per_objfile->objfile,
c24bdb02 21879 name, comp_dir, language, low_pc));
93b8bea4 21880
c24bdb02 21881 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 21882
c24bdb02
KS
21883 get_builder ()->record_debugformat ("DWARF 2");
21884 get_builder ()->record_producer (producer);
f4dc4d17 21885
c24bdb02 21886 processing_has_namespace_info = false;
43f3e411 21887
c24bdb02 21888 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
21889}
21890
4c2df51b
DJ
21891static void
21892var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 21893 struct dwarf2_cu *cu)
4c2df51b 21894{
5e22e966 21895 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
21896 struct comp_unit_head *cu_header = &cu->header;
21897
4c2df51b
DJ
21898 /* NOTE drow/2003-01-30: There used to be a comment and some special
21899 code here to turn a symbol with DW_AT_external and a
21900 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
21901 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
21902 with some versions of binutils) where shared libraries could have
21903 relocations against symbols in their debug information - the
21904 minimal symbol would have the right address, but the debug info
21905 would not. It's no longer necessary, because we will explicitly
21906 apply relocations when we read in the debug information now. */
21907
21908 /* A DW_AT_location attribute with no contents indicates that a
21909 variable has been optimized away. */
9d2246fc 21910 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 21911 {
f1e6e072 21912 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
21913 return;
21914 }
21915
21916 /* Handle one degenerate form of location expression specially, to
21917 preserve GDB's previous behavior when section offsets are
336d760d
AT
21918 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
21919 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 21920
9d2246fc
TT
21921 if (attr->form_is_block ())
21922 {
21923 struct dwarf_block *block = attr->as_block ();
21924
21925 if ((block->data[0] == DW_OP_addr
21926 && block->size == 1 + cu_header->addr_size)
21927 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 21928 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
21929 && (block->size
21930 == 1 + leb128_size (&block->data[1]))))
21931 {
21932 unsigned int dummy;
21933
21934 if (block->data[0] == DW_OP_addr)
21935 SET_SYMBOL_VALUE_ADDRESS
21936 (sym, cu->header.read_address (objfile->obfd,
21937 block->data + 1,
38583298 21938 &dummy));
9d2246fc
TT
21939 else
21940 SET_SYMBOL_VALUE_ADDRESS
21941 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
21942 &dummy));
21943 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
21944 fixup_symbol_section (sym, objfile);
21945 SET_SYMBOL_VALUE_ADDRESS
21946 (sym,
21947 SYMBOL_VALUE_ADDRESS (sym)
21948 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
21949 return;
21950 }
4c2df51b
DJ
21951 }
21952
21953 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
21954 expression evaluator, and use LOC_COMPUTED only when necessary
21955 (i.e. when the value of a register or memory location is
21956 referenced, or a thread-local block, etc.). Then again, it might
21957 not be worthwhile. I'm assuming that it isn't unless performance
21958 or memory numbers show me otherwise. */
21959
f1e6e072 21960 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 21961
f1e6e072 21962 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 21963 cu->has_loclist = true;
4c2df51b
DJ
21964}
21965
c906108c
SS
21966/* Given a pointer to a DWARF information entry, figure out if we need
21967 to make a symbol table entry for it, and if so, create a new entry
21968 and return a pointer to it.
21969 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
21970 used the passed type.
21971 If SPACE is not NULL, use it to hold the new symbol. If it is
21972 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
21973
21974static struct symbol *
5e2db402
TT
21975new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
21976 struct symbol *space)
c906108c 21977{
976ca316
SM
21978 dwarf2_per_objfile *per_objfile = cu->per_objfile;
21979 struct objfile *objfile = per_objfile->objfile;
08feed99 21980 struct gdbarch *gdbarch = objfile->arch ();
c906108c 21981 struct symbol *sym = NULL;
15d034d0 21982 const char *name;
c906108c
SS
21983 struct attribute *attr = NULL;
21984 struct attribute *attr2 = NULL;
e142c38c 21985 CORE_ADDR baseaddr;
e37fd15a
SW
21986 struct pending **list_to_add = NULL;
21987
edb3359d 21988 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 21989
b3b3bada 21990 baseaddr = objfile->text_section_offset ();
c906108c 21991
94af9270 21992 name = dwarf2_name (die, cu);
c906108c
SS
21993 if (name)
21994 {
34eaf542 21995 int suppress_add = 0;
94af9270 21996
34eaf542
TT
21997 if (space)
21998 sym = space;
21999 else
8c14c3a3 22000 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22001 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22002
22003 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22004 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22005 /* Fortran does not have mangling standard and the mangling does differ
22006 between gfortran, iFort etc. */
bcfe6157
TT
22007 const char *physname
22008 = (cu->language == language_fortran
22009 ? dwarf2_full_name (name, die, cu)
22010 : dwarf2_physname (name, die, cu));
22011 const char *linkagename = dw2_linkage_name (die, cu);
22012
22013 if (linkagename == nullptr || cu->language == language_ada)
22014 sym->set_linkage_name (physname);
22015 else
22016 {
22017 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22018 sym->set_linkage_name (linkagename);
22019 }
f55ee35c 22020
c906108c 22021 /* Default assumptions.
dda83cd7 22022 Use the passed type or decode it from the die. */
176620f1 22023 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22024 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22025 if (type != NULL)
22026 SYMBOL_TYPE (sym) = type;
22027 else
e7c27a73 22028 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22029 attr = dwarf2_attr (die,
22030 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22031 cu);
435d3d88 22032 if (attr != nullptr)
529908cb 22033 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22034
edb3359d
DJ
22035 attr = dwarf2_attr (die,
22036 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22037 cu);
529908cb 22038 if (attr != nullptr && attr->form_is_unsigned ())
cb1df416 22039 {
529908cb
TT
22040 file_name_index file_index
22041 = (file_name_index) attr->as_unsigned ();
8c43009f 22042 struct file_entry *fe;
9a619af0 22043
ecfb656c
PA
22044 if (cu->line_header != NULL)
22045 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22046 else
22047 fe = NULL;
22048
22049 if (fe == NULL)
b98664d3 22050 complaint (_("file index out of range"));
8c43009f
PA
22051 else
22052 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22053 }
22054
c906108c
SS
22055 switch (die->tag)
22056 {
22057 case DW_TAG_label:
e142c38c 22058 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22059 if (attr != nullptr)
3e29f34a
MR
22060 {
22061 CORE_ADDR addr;
22062
95f982e5 22063 addr = attr->as_address ();
3e29f34a 22064 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22065 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22066 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22067 }
8f5c6526
TV
22068 else
22069 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22070 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22071 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22072 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22073 break;
22074 case DW_TAG_subprogram:
22075 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22076 finish_block. */
f1e6e072 22077 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22078 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22079 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22080 || cu->language == language_ada
22081 || cu->language == language_fortran)
c906108c 22082 {
dda83cd7
SM
22083 /* Subprograms marked external are stored as a global symbol.
22084 Ada and Fortran subprograms, whether marked external or
22085 not, are always stored as a global symbol, because we want
22086 to be able to access them globally. For instance, we want
22087 to be able to break on a nested subprogram without having
22088 to specify the context. */
c24bdb02 22089 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22090 }
22091 else
22092 {
e37fd15a 22093 list_to_add = cu->list_in_scope;
c906108c
SS
22094 }
22095 break;
edb3359d
DJ
22096 case DW_TAG_inlined_subroutine:
22097 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22098 finish_block. */
f1e6e072 22099 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22100 SYMBOL_INLINED (sym) = 1;
481860b3 22101 list_to_add = cu->list_in_scope;
edb3359d 22102 break;
34eaf542
TT
22103 case DW_TAG_template_value_param:
22104 suppress_add = 1;
22105 /* Fall through. */
72929c62 22106 case DW_TAG_constant:
c906108c 22107 case DW_TAG_variable:
254e6b9e 22108 case DW_TAG_member:
0963b4bd
MS
22109 /* Compilation with minimal debug info may result in
22110 variables with missing type entries. Change the
22111 misleading `void' type to something sensible. */
78134374 22112 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22113 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22114
e142c38c 22115 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22116 /* In the case of DW_TAG_member, we should only be called for
22117 static const members. */
22118 if (die->tag == DW_TAG_member)
22119 {
3863f96c
DE
22120 /* dwarf2_add_field uses die_is_declaration,
22121 so we do the same. */
254e6b9e
DE
22122 gdb_assert (die_is_declaration (die, cu));
22123 gdb_assert (attr);
22124 }
435d3d88 22125 if (attr != nullptr)
c906108c 22126 {
e7c27a73 22127 dwarf2_const_value (attr, sym, cu);
e142c38c 22128 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22129 if (!suppress_add)
34eaf542 22130 {
c45bc3f8 22131 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22132 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22133 else
e37fd15a 22134 list_to_add = cu->list_in_scope;
34eaf542 22135 }
c906108c
SS
22136 break;
22137 }
e142c38c 22138 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22139 if (attr != nullptr)
c906108c 22140 {
e7c27a73 22141 var_decode_location (attr, sym, cu);
e142c38c 22142 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22143
22144 /* Fortran explicitly imports any global symbols to the local
22145 scope by DW_TAG_common_block. */
22146 if (cu->language == language_fortran && die->parent
22147 && die->parent->tag == DW_TAG_common_block)
22148 attr2 = NULL;
22149
caac4577
JG
22150 if (SYMBOL_CLASS (sym) == LOC_STATIC
22151 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22152 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22153 {
22154 /* When a static variable is eliminated by the linker,
22155 the corresponding debug information is not stripped
22156 out, but the variable address is set to null;
22157 do not add such variables into symbol table. */
22158 }
c45bc3f8 22159 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22160 {
4b610737
TT
22161 if (SYMBOL_CLASS (sym) == LOC_STATIC
22162 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22163 && per_objfile->per_bfd->can_copy)
4b610737
TT
22164 {
22165 /* A global static variable might be subject to
22166 copy relocation. We first check for a local
22167 minsym, though, because maybe the symbol was
22168 marked hidden, in which case this would not
22169 apply. */
22170 bound_minimal_symbol found
22171 = (lookup_minimal_symbol_linkage
987012b8 22172 (sym->linkage_name (), objfile));
4b610737
TT
22173 if (found.minsym != nullptr)
22174 sym->maybe_copied = 1;
22175 }
f55ee35c 22176
1c809c68
TT
22177 /* A variable with DW_AT_external is never static,
22178 but it may be block-scoped. */
804d2729 22179 list_to_add
c24bdb02
KS
22180 = ((cu->list_in_scope
22181 == cu->get_builder ()->get_file_symbols ())
22182 ? cu->get_builder ()->get_global_symbols ()
804d2729 22183 : cu->list_in_scope);
1c809c68 22184 }
c906108c 22185 else
e37fd15a 22186 list_to_add = cu->list_in_scope;
c906108c
SS
22187 }
22188 else
22189 {
22190 /* We do not know the address of this symbol.
dda83cd7
SM
22191 If it is an external symbol and we have type information
22192 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22193 The address of the variable will then be determined from
22194 the minimal symbol table whenever the variable is
22195 referenced. */
e142c38c 22196 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22197
22198 /* Fortran explicitly imports any global symbols to the local
22199 scope by DW_TAG_common_block. */
22200 if (cu->language == language_fortran && die->parent
22201 && die->parent->tag == DW_TAG_common_block)
22202 {
22203 /* SYMBOL_CLASS doesn't matter here because
22204 read_common_block is going to reset it. */
22205 if (!suppress_add)
22206 list_to_add = cu->list_in_scope;
22207 }
c45bc3f8 22208 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22209 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22210 {
0fe7935b
DJ
22211 /* A variable with DW_AT_external is never static, but it
22212 may be block-scoped. */
804d2729 22213 list_to_add
c24bdb02
KS
22214 = ((cu->list_in_scope
22215 == cu->get_builder ()->get_file_symbols ())
22216 ? cu->get_builder ()->get_global_symbols ()
804d2729 22217 : cu->list_in_scope);
0fe7935b 22218
f1e6e072 22219 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22220 }
442ddf59
JK
22221 else if (!die_is_declaration (die, cu))
22222 {
22223 /* Use the default LOC_OPTIMIZED_OUT class. */
22224 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22225 if (!suppress_add)
22226 list_to_add = cu->list_in_scope;
442ddf59 22227 }
c906108c
SS
22228 }
22229 break;
22230 case DW_TAG_formal_parameter:
a60f3166
TT
22231 {
22232 /* If we are inside a function, mark this as an argument. If
22233 not, we might be looking at an argument to an inlined function
22234 when we do not have enough information to show inlined frames;
22235 pretend it's a local variable in that case so that the user can
22236 still see it. */
804d2729 22237 struct context_stack *curr
c24bdb02 22238 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22239 if (curr != nullptr && curr->name != nullptr)
22240 SYMBOL_IS_ARGUMENT (sym) = 1;
22241 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22242 if (attr != nullptr)
a60f3166
TT
22243 {
22244 var_decode_location (attr, sym, cu);
22245 }
22246 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22247 if (attr != nullptr)
a60f3166
TT
22248 {
22249 dwarf2_const_value (attr, sym, cu);
22250 }
f346a30d 22251
a60f3166
TT
22252 list_to_add = cu->list_in_scope;
22253 }
c906108c
SS
22254 break;
22255 case DW_TAG_unspecified_parameters:
22256 /* From varargs functions; gdb doesn't seem to have any
22257 interest in this information, so just ignore it for now.
22258 (FIXME?) */
22259 break;
34eaf542
TT
22260 case DW_TAG_template_type_param:
22261 suppress_add = 1;
22262 /* Fall through. */
c906108c 22263 case DW_TAG_class_type:
680b30c7 22264 case DW_TAG_interface_type:
c906108c
SS
22265 case DW_TAG_structure_type:
22266 case DW_TAG_union_type:
72019c9c 22267 case DW_TAG_set_type:
c906108c 22268 case DW_TAG_enumeration_type:
f1e6e072 22269 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22270 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22271
63d06c5c 22272 {
9c37b5ae 22273 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22274 really ever be static objects: otherwise, if you try
22275 to, say, break of a class's method and you're in a file
22276 which doesn't mention that class, it won't work unless
22277 the check for all static symbols in lookup_symbol_aux
22278 saves you. See the OtherFileClass tests in
22279 gdb.c++/namespace.exp. */
22280
e37fd15a 22281 if (!suppress_add)
34eaf542 22282 {
c24bdb02 22283 buildsym_compunit *builder = cu->get_builder ();
804d2729 22284 list_to_add
c24bdb02 22285 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22286 && cu->language == language_cplus
c24bdb02 22287 ? builder->get_global_symbols ()
804d2729 22288 : cu->list_in_scope);
63d06c5c 22289
64382290 22290 /* The semantics of C++ state that "struct foo {
9c37b5ae 22291 ... }" also defines a typedef for "foo". */
64382290 22292 if (cu->language == language_cplus
45280282 22293 || cu->language == language_ada
c44af4eb
TT
22294 || cu->language == language_d
22295 || cu->language == language_rust)
64382290
TT
22296 {
22297 /* The symbol's name is already allocated along
22298 with this objfile, so we don't need to
22299 duplicate it for the type. */
7d93a1e0 22300 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22301 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22302 }
63d06c5c
DC
22303 }
22304 }
c906108c
SS
22305 break;
22306 case DW_TAG_typedef:
f1e6e072 22307 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22308 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22309 list_to_add = cu->list_in_scope;
63d06c5c 22310 break;
d8f62e84 22311 case DW_TAG_array_type:
c906108c 22312 case DW_TAG_base_type:
dda83cd7 22313 case DW_TAG_subrange_type:
f1e6e072 22314 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22315 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22316 list_to_add = cu->list_in_scope;
c906108c
SS
22317 break;
22318 case DW_TAG_enumerator:
e142c38c 22319 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22320 if (attr != nullptr)
c906108c 22321 {
e7c27a73 22322 dwarf2_const_value (attr, sym, cu);
c906108c 22323 }
63d06c5c
DC
22324 {
22325 /* NOTE: carlton/2003-11-10: See comment above in the
22326 DW_TAG_class_type, etc. block. */
22327
804d2729 22328 list_to_add
c24bdb02 22329 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22330 && cu->language == language_cplus
c24bdb02 22331 ? cu->get_builder ()->get_global_symbols ()
804d2729 22332 : cu->list_in_scope);
63d06c5c 22333 }
c906108c 22334 break;
74921315 22335 case DW_TAG_imported_declaration:
5c4e30ca 22336 case DW_TAG_namespace:
f1e6e072 22337 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22338 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22339 break;
530e8392
KB
22340 case DW_TAG_module:
22341 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22342 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22343 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22344 break;
4357ac6c 22345 case DW_TAG_common_block:
f1e6e072 22346 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22347 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22348 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22349 break;
c906108c
SS
22350 default:
22351 /* Not a tag we recognize. Hopefully we aren't processing
22352 trash data, but since we must specifically ignore things
22353 we don't recognize, there is nothing else we should do at
0963b4bd 22354 this point. */
b98664d3 22355 complaint (_("unsupported tag: '%s'"),
4d3c2250 22356 dwarf_tag_name (die->tag));
c906108c
SS
22357 break;
22358 }
df8a16a1 22359
e37fd15a
SW
22360 if (suppress_add)
22361 {
22362 sym->hash_next = objfile->template_symbols;
22363 objfile->template_symbols = sym;
22364 list_to_add = NULL;
22365 }
22366
22367 if (list_to_add != NULL)
d3cb6808 22368 add_symbol_to_list (sym, list_to_add);
e37fd15a 22369
df8a16a1
DJ
22370 /* For the benefit of old versions of GCC, check for anonymous
22371 namespaces based on the demangled name. */
4d4ec4e5 22372 if (!cu->processing_has_namespace_info
94af9270 22373 && cu->language == language_cplus)
c24bdb02 22374 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22375 }
22376 return (sym);
22377}
22378
98bfdba5
PA
22379/* Given an attr with a DW_FORM_dataN value in host byte order,
22380 zero-extend it as appropriate for the symbol's type. The DWARF
22381 standard (v4) is not entirely clear about the meaning of using
22382 DW_FORM_dataN for a constant with a signed type, where the type is
22383 wider than the data. The conclusion of a discussion on the DWARF
22384 list was that this is unspecified. We choose to always zero-extend
22385 because that is the interpretation long in use by GCC. */
c906108c 22386
98bfdba5 22387static gdb_byte *
ff39bb5e 22388dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22389 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22390{
5e22e966 22391 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22392 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22393 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22394 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22395
22396 if (bits < sizeof (*value) * 8)
22397 {
22398 l &= ((LONGEST) 1 << bits) - 1;
22399 *value = l;
22400 }
22401 else if (bits == sizeof (*value) * 8)
22402 *value = l;
22403 else
22404 {
224c3ddb 22405 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22406 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22407 return bytes;
22408 }
22409
22410 return NULL;
22411}
22412
22413/* Read a constant value from an attribute. Either set *VALUE, or if
22414 the value does not fit in *VALUE, set *BYTES - either already
22415 allocated on the objfile obstack, or newly allocated on OBSTACK,
22416 or, set *BATON, if we translated the constant to a location
22417 expression. */
22418
22419static void
ff39bb5e 22420dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22421 const char *name, struct obstack *obstack,
22422 struct dwarf2_cu *cu,
d521ce57 22423 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22424 struct dwarf2_locexpr_baton **baton)
22425{
5e22e966 22426 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22427 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22428 struct comp_unit_head *cu_header = &cu->header;
c906108c 22429 struct dwarf_block *blk;
98bfdba5
PA
22430 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22431 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22432
22433 *value = 0;
22434 *bytes = NULL;
22435 *baton = NULL;
c906108c
SS
22436
22437 switch (attr->form)
22438 {
22439 case DW_FORM_addr:
336d760d 22440 case DW_FORM_addrx:
3019eac3 22441 case DW_FORM_GNU_addr_index:
ac56253d 22442 {
ac56253d
TT
22443 gdb_byte *data;
22444
98bfdba5
PA
22445 if (TYPE_LENGTH (type) != cu_header->addr_size)
22446 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22447 cu_header->addr_size,
98bfdba5 22448 TYPE_LENGTH (type));
ac56253d
TT
22449 /* Symbols of this form are reasonably rare, so we just
22450 piggyback on the existing location code rather than writing
22451 a new implementation of symbol_computed_ops. */
8d749320 22452 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22453 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22454 (*baton)->per_cu = cu->per_cu;
22455 gdb_assert ((*baton)->per_cu);
ac56253d 22456
98bfdba5 22457 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22458 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22459 (*baton)->data = data;
ac56253d
TT
22460
22461 data[0] = DW_OP_addr;
22462 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22463 byte_order, attr->as_address ());
ac56253d 22464 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22465 }
c906108c 22466 break;
4ac36638 22467 case DW_FORM_string:
93b5768b 22468 case DW_FORM_strp:
cf532bd1 22469 case DW_FORM_strx:
3019eac3 22470 case DW_FORM_GNU_str_index:
36586728 22471 case DW_FORM_GNU_strp_alt:
c6481205 22472 /* The string is already allocated on the objfile obstack, point
98bfdba5 22473 directly to it. */
2c830f54 22474 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22475 break;
c906108c
SS
22476 case DW_FORM_block1:
22477 case DW_FORM_block2:
22478 case DW_FORM_block4:
22479 case DW_FORM_block:
2dc7f7b3 22480 case DW_FORM_exprloc:
0224619f 22481 case DW_FORM_data16:
9d2246fc 22482 blk = attr->as_block ();
98bfdba5
PA
22483 if (TYPE_LENGTH (type) != blk->size)
22484 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22485 TYPE_LENGTH (type));
22486 *bytes = blk->data;
c906108c 22487 break;
2df3850c
JM
22488
22489 /* The DW_AT_const_value attributes are supposed to carry the
22490 symbol's value "represented as it would be on the target
22491 architecture." By the time we get here, it's already been
22492 converted to host endianness, so we just need to sign- or
22493 zero-extend it as appropriate. */
22494 case DW_FORM_data1:
3aef2284 22495 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22496 break;
c906108c 22497 case DW_FORM_data2:
3aef2284 22498 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22499 break;
c906108c 22500 case DW_FORM_data4:
3aef2284 22501 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22502 break;
c906108c 22503 case DW_FORM_data8:
3aef2284 22504 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22505 break;
22506
c906108c 22507 case DW_FORM_sdata:
663c44ac 22508 case DW_FORM_implicit_const:
1bc397c5 22509 *value = attr->as_signed ();
2df3850c
JM
22510 break;
22511
c906108c 22512 case DW_FORM_udata:
529908cb 22513 *value = attr->as_unsigned ();
c906108c 22514 break;
2df3850c 22515
c906108c 22516 default:
b98664d3 22517 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22518 dwarf_form_name (attr->form));
98bfdba5 22519 *value = 0;
c906108c
SS
22520 break;
22521 }
22522}
22523
2df3850c 22524
98bfdba5
PA
22525/* Copy constant value from an attribute to a symbol. */
22526
2df3850c 22527static void
ff39bb5e 22528dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22529 struct dwarf2_cu *cu)
2df3850c 22530{
5e22e966 22531 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22532 LONGEST value;
d521ce57 22533 const gdb_byte *bytes;
98bfdba5 22534 struct dwarf2_locexpr_baton *baton;
2df3850c 22535
98bfdba5 22536 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22537 sym->print_name (),
98bfdba5
PA
22538 &objfile->objfile_obstack, cu,
22539 &value, &bytes, &baton);
2df3850c 22540
98bfdba5
PA
22541 if (baton != NULL)
22542 {
98bfdba5 22543 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22544 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22545 }
22546 else if (bytes != NULL)
22547 {
22548 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22549 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22550 }
22551 else
22552 {
22553 SYMBOL_VALUE (sym) = value;
f1e6e072 22554 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22555 }
2df3850c
JM
22556}
22557
c906108c
SS
22558/* Return the type of the die in question using its DW_AT_type attribute. */
22559
22560static struct type *
e7c27a73 22561die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22562{
c906108c 22563 struct attribute *type_attr;
c906108c 22564
e142c38c 22565 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22566 if (!type_attr)
22567 {
5e22e966 22568 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22569 /* A missing DW_AT_type represents a void type. */
518817b3 22570 return objfile_type (objfile)->builtin_void;
c906108c 22571 }
348e048f 22572
673bfd45 22573 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22574}
22575
b4ba55a1
JB
22576/* True iff CU's producer generates GNAT Ada auxiliary information
22577 that allows to find parallel types through that information instead
22578 of having to do expensive parallel lookups by type name. */
22579
22580static int
22581need_gnat_info (struct dwarf2_cu *cu)
22582{
de4cb04a
JB
22583 /* Assume that the Ada compiler was GNAT, which always produces
22584 the auxiliary information. */
22585 return (cu->language == language_ada);
b4ba55a1
JB
22586}
22587
b4ba55a1
JB
22588/* Return the auxiliary type of the die in question using its
22589 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22590 attribute is not present. */
22591
22592static struct type *
22593die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22594{
b4ba55a1 22595 struct attribute *type_attr;
b4ba55a1
JB
22596
22597 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22598 if (!type_attr)
22599 return NULL;
22600
673bfd45 22601 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22602}
22603
22604/* If DIE has a descriptive_type attribute, then set the TYPE's
22605 descriptive type accordingly. */
22606
22607static void
22608set_descriptive_type (struct type *type, struct die_info *die,
22609 struct dwarf2_cu *cu)
22610{
22611 struct type *descriptive_type = die_descriptive_type (die, cu);
22612
22613 if (descriptive_type)
22614 {
22615 ALLOCATE_GNAT_AUX_TYPE (type);
22616 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22617 }
22618}
22619
c906108c
SS
22620/* Return the containing type of the die in question using its
22621 DW_AT_containing_type attribute. */
22622
22623static struct type *
e7c27a73 22624die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22625{
c906108c 22626 struct attribute *type_attr;
5e22e966 22627 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22628
e142c38c 22629 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22630 if (!type_attr)
22631 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22632 "[in module %s]"), objfile_name (objfile));
33ac96f0 22633
673bfd45 22634 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22635}
22636
ac9ec31b
DE
22637/* Return an error marker type to use for the ill formed type in DIE/CU. */
22638
22639static struct type *
22640build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22641{
976ca316
SM
22642 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22643 struct objfile *objfile = per_objfile->objfile;
528e1572 22644 char *saved;
ac9ec31b 22645
528e1572
SM
22646 std::string message
22647 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22648 objfile_name (objfile),
22649 sect_offset_str (cu->header.sect_off),
22650 sect_offset_str (die->sect_off));
efba19b0 22651 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22652
19f392bc 22653 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22654}
22655
673bfd45 22656/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22657 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22658 DW_AT_containing_type.
673bfd45
DE
22659 If there is no type substitute an error marker. */
22660
c906108c 22661static struct type *
ff39bb5e 22662lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22663 struct dwarf2_cu *cu)
c906108c 22664{
976ca316
SM
22665 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22666 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22667 struct type *this_type;
22668
ac9ec31b
DE
22669 gdb_assert (attr->name == DW_AT_type
22670 || attr->name == DW_AT_GNAT_descriptive_type
22671 || attr->name == DW_AT_containing_type);
22672
673bfd45
DE
22673 /* First see if we have it cached. */
22674
36586728
TT
22675 if (attr->form == DW_FORM_GNU_ref_alt)
22676 {
22677 struct dwarf2_per_cu_data *per_cu;
0826b30a 22678 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22679
976ca316
SM
22680 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22681 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22682 }
cd6c91b4 22683 else if (attr->form_is_ref ())
673bfd45 22684 {
0826b30a 22685 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22686
976ca316 22687 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22688 }
55f1336d 22689 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22690 {
630ed6b9 22691 ULONGEST signature = attr->as_signature ();
673bfd45 22692
ac9ec31b 22693 return get_signatured_type (die, signature, cu);
673bfd45
DE
22694 }
22695 else
22696 {
b98664d3 22697 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22698 " at %s [in module %s]"),
22699 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22700 objfile_name (objfile));
ac9ec31b 22701 return build_error_marker_type (cu, die);
673bfd45
DE
22702 }
22703
22704 /* If not cached we need to read it in. */
22705
22706 if (this_type == NULL)
22707 {
ac9ec31b 22708 struct die_info *type_die = NULL;
673bfd45
DE
22709 struct dwarf2_cu *type_cu = cu;
22710
cd6c91b4 22711 if (attr->form_is_ref ())
ac9ec31b
DE
22712 type_die = follow_die_ref (die, attr, &type_cu);
22713 if (type_die == NULL)
22714 return build_error_marker_type (cu, die);
22715 /* If we find the type now, it's probably because the type came
3019eac3
DE
22716 from an inter-CU reference and the type's CU got expanded before
22717 ours. */
ac9ec31b 22718 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22719 }
22720
22721 /* If we still don't have a type use an error marker. */
22722
22723 if (this_type == NULL)
ac9ec31b 22724 return build_error_marker_type (cu, die);
673bfd45 22725
f792889a 22726 return this_type;
c906108c
SS
22727}
22728
673bfd45
DE
22729/* Return the type in DIE, CU.
22730 Returns NULL for invalid types.
22731
02142a6c 22732 This first does a lookup in die_type_hash,
673bfd45
DE
22733 and only reads the die in if necessary.
22734
22735 NOTE: This can be called when reading in partial or full symbols. */
22736
f792889a 22737static struct type *
e7c27a73 22738read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22739{
f792889a
DJ
22740 struct type *this_type;
22741
22742 this_type = get_die_type (die, cu);
22743 if (this_type)
22744 return this_type;
22745
673bfd45
DE
22746 return read_type_die_1 (die, cu);
22747}
22748
22749/* Read the type in DIE, CU.
22750 Returns NULL for invalid types. */
22751
22752static struct type *
22753read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22754{
22755 struct type *this_type = NULL;
22756
c906108c
SS
22757 switch (die->tag)
22758 {
22759 case DW_TAG_class_type:
680b30c7 22760 case DW_TAG_interface_type:
c906108c
SS
22761 case DW_TAG_structure_type:
22762 case DW_TAG_union_type:
f792889a 22763 this_type = read_structure_type (die, cu);
c906108c
SS
22764 break;
22765 case DW_TAG_enumeration_type:
f792889a 22766 this_type = read_enumeration_type (die, cu);
c906108c
SS
22767 break;
22768 case DW_TAG_subprogram:
22769 case DW_TAG_subroutine_type:
edb3359d 22770 case DW_TAG_inlined_subroutine:
f792889a 22771 this_type = read_subroutine_type (die, cu);
c906108c
SS
22772 break;
22773 case DW_TAG_array_type:
f792889a 22774 this_type = read_array_type (die, cu);
c906108c 22775 break;
72019c9c 22776 case DW_TAG_set_type:
f792889a 22777 this_type = read_set_type (die, cu);
72019c9c 22778 break;
c906108c 22779 case DW_TAG_pointer_type:
f792889a 22780 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22781 break;
22782 case DW_TAG_ptr_to_member_type:
f792889a 22783 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22784 break;
22785 case DW_TAG_reference_type:
4297a3f0
AV
22786 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22787 break;
22788 case DW_TAG_rvalue_reference_type:
22789 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22790 break;
22791 case DW_TAG_const_type:
f792889a 22792 this_type = read_tag_const_type (die, cu);
c906108c
SS
22793 break;
22794 case DW_TAG_volatile_type:
f792889a 22795 this_type = read_tag_volatile_type (die, cu);
c906108c 22796 break;
06d66ee9
TT
22797 case DW_TAG_restrict_type:
22798 this_type = read_tag_restrict_type (die, cu);
22799 break;
c906108c 22800 case DW_TAG_string_type:
f792889a 22801 this_type = read_tag_string_type (die, cu);
c906108c
SS
22802 break;
22803 case DW_TAG_typedef:
f792889a 22804 this_type = read_typedef (die, cu);
c906108c 22805 break;
a02abb62 22806 case DW_TAG_subrange_type:
f792889a 22807 this_type = read_subrange_type (die, cu);
a02abb62 22808 break;
c906108c 22809 case DW_TAG_base_type:
f792889a 22810 this_type = read_base_type (die, cu);
c906108c 22811 break;
81a17f79 22812 case DW_TAG_unspecified_type:
f792889a 22813 this_type = read_unspecified_type (die, cu);
81a17f79 22814 break;
0114d602
DJ
22815 case DW_TAG_namespace:
22816 this_type = read_namespace_type (die, cu);
22817 break;
f55ee35c
JK
22818 case DW_TAG_module:
22819 this_type = read_module_type (die, cu);
22820 break;
a2c2acaf
MW
22821 case DW_TAG_atomic_type:
22822 this_type = read_tag_atomic_type (die, cu);
22823 break;
c906108c 22824 default:
b98664d3 22825 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22826 dwarf_tag_name (die->tag));
c906108c
SS
22827 break;
22828 }
63d06c5c 22829
f792889a 22830 return this_type;
63d06c5c
DC
22831}
22832
abc72ce4
DE
22833/* See if we can figure out if the class lives in a namespace. We do
22834 this by looking for a member function; its demangled name will
22835 contain namespace info, if there is any.
22836 Return the computed name or NULL.
22837 Space for the result is allocated on the objfile's obstack.
22838 This is the full-die version of guess_partial_die_structure_name.
22839 In this case we know DIE has no useful parent. */
22840
43816ebc 22841static const char *
abc72ce4
DE
22842guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22843{
22844 struct die_info *spec_die;
22845 struct dwarf2_cu *spec_cu;
22846 struct die_info *child;
5e22e966 22847 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22848
22849 spec_cu = cu;
22850 spec_die = die_specification (die, &spec_cu);
22851 if (spec_die != NULL)
22852 {
22853 die = spec_die;
22854 cu = spec_cu;
22855 }
22856
22857 for (child = die->child;
22858 child != NULL;
22859 child = child->sibling)
22860 {
22861 if (child->tag == DW_TAG_subprogram)
22862 {
73b9be8b 22863 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22864
7d45c7c3 22865 if (linkage_name != NULL)
abc72ce4 22866 {
43816ebc 22867 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22868 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22869 const char *name = NULL;
abc72ce4
DE
22870
22871 if (actual_name != NULL)
22872 {
15d034d0 22873 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22874
22875 if (die_name != NULL
43816ebc 22876 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
22877 {
22878 /* Strip off the class name from the full name.
22879 We want the prefix. */
22880 int die_name_len = strlen (die_name);
43816ebc
TT
22881 int actual_name_len = strlen (actual_name.get ());
22882 const char *ptr = actual_name.get ();
abc72ce4
DE
22883
22884 /* Test for '::' as a sanity check. */
22885 if (actual_name_len > die_name_len + 2
43816ebc 22886 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 22887 name = obstack_strndup (
e3b94546 22888 &objfile->per_bfd->storage_obstack,
43816ebc 22889 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
22890 }
22891 }
abc72ce4
DE
22892 return name;
22893 }
22894 }
22895 }
22896
22897 return NULL;
22898}
22899
96408a79
SA
22900/* GCC might emit a nameless typedef that has a linkage name. Determine the
22901 prefix part in such case. See
22902 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
22903
a121b7c1 22904static const char *
96408a79
SA
22905anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
22906{
22907 struct attribute *attr;
e6a959d6 22908 const char *base;
96408a79
SA
22909
22910 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
22911 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
22912 return NULL;
22913
7d45c7c3 22914 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
22915 return NULL;
22916
73b9be8b 22917 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
22918 const char *attr_name = attr->as_string ();
22919 if (attr == NULL || attr_name == NULL)
96408a79
SA
22920 return NULL;
22921
22922 /* dwarf2_name had to be already called. */
3b64bf15 22923 gdb_assert (attr->canonical_string_p ());
96408a79
SA
22924
22925 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
22926 base = strrchr (attr_name, ':');
22927 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
22928 return "";
22929
5e22e966 22930 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 22931 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
22932 attr_name,
22933 &base[-1] - attr_name);
96408a79
SA
22934}
22935
fdde2d81 22936/* Return the name of the namespace/class that DIE is defined within,
0114d602 22937 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 22938
0114d602
DJ
22939 For example, if we're within the method foo() in the following
22940 code:
22941
22942 namespace N {
22943 class C {
22944 void foo () {
22945 }
22946 };
22947 }
22948
22949 then determine_prefix on foo's die will return "N::C". */
fdde2d81 22950
0d5cff50 22951static const char *
e142c38c 22952determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 22953{
976ca316 22954 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
22955 struct die_info *parent, *spec_die;
22956 struct dwarf2_cu *spec_cu;
22957 struct type *parent_type;
a121b7c1 22958 const char *retval;
63d06c5c 22959
9c37b5ae 22960 if (cu->language != language_cplus
c44af4eb
TT
22961 && cu->language != language_fortran && cu->language != language_d
22962 && cu->language != language_rust)
0114d602
DJ
22963 return "";
22964
96408a79
SA
22965 retval = anonymous_struct_prefix (die, cu);
22966 if (retval)
22967 return retval;
22968
0114d602
DJ
22969 /* We have to be careful in the presence of DW_AT_specification.
22970 For example, with GCC 3.4, given the code
22971
22972 namespace N {
22973 void foo() {
22974 // Definition of N::foo.
22975 }
22976 }
22977
22978 then we'll have a tree of DIEs like this:
22979
22980 1: DW_TAG_compile_unit
22981 2: DW_TAG_namespace // N
22982 3: DW_TAG_subprogram // declaration of N::foo
22983 4: DW_TAG_subprogram // definition of N::foo
22984 DW_AT_specification // refers to die #3
22985
22986 Thus, when processing die #4, we have to pretend that we're in
22987 the context of its DW_AT_specification, namely the contex of die
22988 #3. */
22989 spec_cu = cu;
22990 spec_die = die_specification (die, &spec_cu);
22991 if (spec_die == NULL)
22992 parent = die->parent;
22993 else
63d06c5c 22994 {
0114d602
DJ
22995 parent = spec_die->parent;
22996 cu = spec_cu;
63d06c5c 22997 }
0114d602
DJ
22998
22999 if (parent == NULL)
23000 return "";
98bfdba5
PA
23001 else if (parent->building_fullname)
23002 {
23003 const char *name;
23004 const char *parent_name;
23005
23006 /* It has been seen on RealView 2.2 built binaries,
23007 DW_TAG_template_type_param types actually _defined_ as
23008 children of the parent class:
23009
23010 enum E {};
23011 template class <class Enum> Class{};
23012 Class<enum E> class_e;
23013
dda83cd7
SM
23014 1: DW_TAG_class_type (Class)
23015 2: DW_TAG_enumeration_type (E)
23016 3: DW_TAG_enumerator (enum1:0)
23017 3: DW_TAG_enumerator (enum2:1)
23018 ...
23019 2: DW_TAG_template_type_param
23020 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23021
23022 Besides being broken debug info, it can put GDB into an
23023 infinite loop. Consider:
23024
23025 When we're building the full name for Class<E>, we'll start
23026 at Class, and go look over its template type parameters,
23027 finding E. We'll then try to build the full name of E, and
23028 reach here. We're now trying to build the full name of E,
23029 and look over the parent DIE for containing scope. In the
23030 broken case, if we followed the parent DIE of E, we'd again
23031 find Class, and once again go look at its template type
23032 arguments, etc., etc. Simply don't consider such parent die
23033 as source-level parent of this die (it can't be, the language
23034 doesn't allow it), and break the loop here. */
23035 name = dwarf2_name (die, cu);
23036 parent_name = dwarf2_name (parent, cu);
b98664d3 23037 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23038 name ? name : "<unknown>",
23039 parent_name ? parent_name : "<unknown>");
23040 return "";
23041 }
63d06c5c 23042 else
0114d602
DJ
23043 switch (parent->tag)
23044 {
63d06c5c 23045 case DW_TAG_namespace:
0114d602 23046 parent_type = read_type_die (parent, cu);
acebe513
UW
23047 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23048 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23049 Work around this problem here. */
23050 if (cu->language == language_cplus
7d93a1e0 23051 && strcmp (parent_type->name (), "::") == 0)
acebe513 23052 return "";
0114d602 23053 /* We give a name to even anonymous namespaces. */
7d93a1e0 23054 return parent_type->name ();
63d06c5c 23055 case DW_TAG_class_type:
680b30c7 23056 case DW_TAG_interface_type:
63d06c5c 23057 case DW_TAG_structure_type:
0114d602 23058 case DW_TAG_union_type:
f55ee35c 23059 case DW_TAG_module:
0114d602 23060 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23061 if (parent_type->name () != NULL)
23062 return parent_type->name ();
0114d602
DJ
23063 else
23064 /* An anonymous structure is only allowed non-static data
23065 members; no typedefs, no member functions, et cetera.
23066 So it does not need a prefix. */
23067 return "";
abc72ce4 23068 case DW_TAG_compile_unit:
95554aad 23069 case DW_TAG_partial_unit:
abc72ce4
DE
23070 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23071 if (cu->language == language_cplus
976ca316 23072 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23073 && die->child != NULL
23074 && (die->tag == DW_TAG_class_type
23075 || die->tag == DW_TAG_structure_type
23076 || die->tag == DW_TAG_union_type))
23077 {
43816ebc 23078 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23079 if (name != NULL)
23080 return name;
23081 }
23082 return "";
0a4b0913
AB
23083 case DW_TAG_subprogram:
23084 /* Nested subroutines in Fortran get a prefix with the name
23085 of the parent's subroutine. */
23086 if (cu->language == language_fortran)
23087 {
23088 if ((die->tag == DW_TAG_subprogram)
23089 && (dwarf2_name (parent, cu) != NULL))
23090 return dwarf2_name (parent, cu);
23091 }
23092 return determine_prefix (parent, cu);
3d567982
TT
23093 case DW_TAG_enumeration_type:
23094 parent_type = read_type_die (parent, cu);
23095 if (TYPE_DECLARED_CLASS (parent_type))
23096 {
7d93a1e0
SM
23097 if (parent_type->name () != NULL)
23098 return parent_type->name ();
3d567982
TT
23099 return "";
23100 }
23101 /* Fall through. */
63d06c5c 23102 default:
8176b9b8 23103 return determine_prefix (parent, cu);
63d06c5c 23104 }
63d06c5c
DC
23105}
23106
3e43a32a
MS
23107/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23108 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23109 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23110 an obconcat, otherwise allocate storage for the result. The CU argument is
23111 used to determine the language and hence, the appropriate separator. */
987504bb 23112
f55ee35c 23113#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23114
23115static char *
f55ee35c 23116typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23117 int physname, struct dwarf2_cu *cu)
63d06c5c 23118{
f55ee35c 23119 const char *lead = "";
5c315b68 23120 const char *sep;
63d06c5c 23121
3e43a32a
MS
23122 if (suffix == NULL || suffix[0] == '\0'
23123 || prefix == NULL || prefix[0] == '\0')
987504bb 23124 sep = "";
45280282
IB
23125 else if (cu->language == language_d)
23126 {
23127 /* For D, the 'main' function could be defined in any module, but it
23128 should never be prefixed. */
23129 if (strcmp (suffix, "D main") == 0)
23130 {
23131 prefix = "";
23132 sep = "";
23133 }
23134 else
23135 sep = ".";
23136 }
f55ee35c
JK
23137 else if (cu->language == language_fortran && physname)
23138 {
23139 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23140 DW_AT_MIPS_linkage_name is preferred and used instead. */
23141
23142 lead = "__";
23143 sep = "_MOD_";
23144 }
987504bb
JJ
23145 else
23146 sep = "::";
63d06c5c 23147
6dd47d34
DE
23148 if (prefix == NULL)
23149 prefix = "";
23150 if (suffix == NULL)
23151 suffix = "";
23152
987504bb
JJ
23153 if (obs == NULL)
23154 {
3e43a32a 23155 char *retval
224c3ddb
SM
23156 = ((char *)
23157 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23158
f55ee35c
JK
23159 strcpy (retval, lead);
23160 strcat (retval, prefix);
6dd47d34
DE
23161 strcat (retval, sep);
23162 strcat (retval, suffix);
63d06c5c
DC
23163 return retval;
23164 }
987504bb
JJ
23165 else
23166 {
23167 /* We have an obstack. */
f55ee35c 23168 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23169 }
63d06c5c
DC
23170}
23171
71c25dea
TT
23172/* Get name of a die, return NULL if not found. */
23173
15d034d0
TT
23174static const char *
23175dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23176 struct objfile *objfile)
71c25dea
TT
23177{
23178 if (name && cu->language == language_cplus)
23179 {
596dc4ad
TT
23180 gdb::unique_xmalloc_ptr<char> canon_name
23181 = cp_canonicalize_string (name);
71c25dea 23182
596dc4ad
TT
23183 if (canon_name != nullptr)
23184 name = objfile->intern (canon_name.get ());
71c25dea
TT
23185 }
23186
23187 return name;
c906108c
SS
23188}
23189
96553a0c
DE
23190/* Get name of a die, return NULL if not found.
23191 Anonymous namespaces are converted to their magic string. */
9219021c 23192
15d034d0 23193static const char *
e142c38c 23194dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23195{
23196 struct attribute *attr;
5e22e966 23197 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23198
e142c38c 23199 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23200 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23201 if (attr_name == nullptr
96553a0c 23202 && die->tag != DW_TAG_namespace
53832f31
TT
23203 && die->tag != DW_TAG_class_type
23204 && die->tag != DW_TAG_interface_type
23205 && die->tag != DW_TAG_structure_type
23206 && die->tag != DW_TAG_union_type)
71c25dea
TT
23207 return NULL;
23208
23209 switch (die->tag)
23210 {
23211 case DW_TAG_compile_unit:
95554aad 23212 case DW_TAG_partial_unit:
71c25dea
TT
23213 /* Compilation units have a DW_AT_name that is a filename, not
23214 a source language identifier. */
23215 case DW_TAG_enumeration_type:
23216 case DW_TAG_enumerator:
23217 /* These tags always have simple identifiers already; no need
23218 to canonicalize them. */
2c830f54 23219 return attr_name;
907af001 23220
96553a0c 23221 case DW_TAG_namespace:
2c830f54
TT
23222 if (attr_name != nullptr)
23223 return attr_name;
96553a0c
DE
23224 return CP_ANONYMOUS_NAMESPACE_STR;
23225
907af001
UW
23226 case DW_TAG_class_type:
23227 case DW_TAG_interface_type:
23228 case DW_TAG_structure_type:
23229 case DW_TAG_union_type:
23230 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23231 structures or unions. These were of the form "._%d" in GCC 4.1,
23232 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23233 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23234 if (attr_name != nullptr
23235 && (startswith (attr_name, "._")
23236 || startswith (attr_name, "<anonymous")))
907af001 23237 return NULL;
53832f31
TT
23238
23239 /* GCC might emit a nameless typedef that has a linkage name. See
23240 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23241 if (!attr || attr_name == NULL)
53832f31 23242 {
73b9be8b 23243 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23244 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23245 if (attr == NULL || attr_name == NULL)
53832f31
TT
23246 return NULL;
23247
2c830f54 23248 /* Avoid demangling attr_name the second time on a second
df5c6c50 23249 call for the same DIE. */
3b64bf15 23250 if (!attr->canonical_string_p ())
53832f31 23251 {
43816ebc 23252 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23253 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23254 if (demangled == nullptr)
23255 return nullptr;
43816ebc 23256
c6481205 23257 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23258 attr_name = attr->as_string ();
53832f31 23259 }
67430cd0 23260
2c830f54
TT
23261 /* Strip any leading namespaces/classes, keep only the
23262 base name. DW_AT_name for named DIEs does not
23263 contain the prefixes. */
23264 const char *base = strrchr (attr_name, ':');
23265 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23266 return &base[1];
23267 else
2c830f54 23268 return attr_name;
53832f31 23269 }
907af001
UW
23270 break;
23271
71c25dea 23272 default:
907af001
UW
23273 break;
23274 }
23275
3b64bf15 23276 if (!attr->canonical_string_p ())
c6481205
TT
23277 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23278 objfile));
2c830f54 23279 return attr->as_string ();
9219021c
DC
23280}
23281
23282/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23283 is none. *EXT_CU is the CU containing DIE on input, and the CU
23284 containing the return value on output. */
9219021c
DC
23285
23286static struct die_info *
f2f0e013 23287dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23288{
23289 struct attribute *attr;
9219021c 23290
f2f0e013 23291 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23292 if (attr == NULL)
23293 return NULL;
23294
f2f0e013 23295 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23296}
23297
f9aca02d 23298static void
d97bc12b 23299dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23300{
23301 unsigned int i;
23302
d97bc12b 23303 print_spaces (indent, f);
9d8780f0 23304 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23305 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23306 sect_offset_str (die->sect_off));
d97bc12b
DE
23307
23308 if (die->parent != NULL)
23309 {
23310 print_spaces (indent, f);
9d8780f0
SM
23311 fprintf_unfiltered (f, " parent at offset: %s\n",
23312 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23313 }
23314
23315 print_spaces (indent, f);
23316 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23317 dwarf_bool_name (die->child != NULL));
c906108c 23318
d97bc12b
DE
23319 print_spaces (indent, f);
23320 fprintf_unfiltered (f, " attributes:\n");
23321
c906108c
SS
23322 for (i = 0; i < die->num_attrs; ++i)
23323 {
d97bc12b
DE
23324 print_spaces (indent, f);
23325 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23326 dwarf_attr_name (die->attrs[i].name),
23327 dwarf_form_name (die->attrs[i].form));
d97bc12b 23328
c906108c
SS
23329 switch (die->attrs[i].form)
23330 {
c906108c 23331 case DW_FORM_addr:
336d760d 23332 case DW_FORM_addrx:
3019eac3 23333 case DW_FORM_GNU_addr_index:
d97bc12b 23334 fprintf_unfiltered (f, "address: ");
36d378cf 23335 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23336 break;
23337 case DW_FORM_block2:
23338 case DW_FORM_block4:
23339 case DW_FORM_block:
23340 case DW_FORM_block1:
56eb65bd 23341 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23342 pulongest (die->attrs[i].as_block ()->size));
c906108c 23343 break;
2dc7f7b3 23344 case DW_FORM_exprloc:
56eb65bd 23345 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23346 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23347 break;
0224619f
JK
23348 case DW_FORM_data16:
23349 fprintf_unfiltered (f, "constant of 16 bytes");
23350 break;
4568ecf9
DE
23351 case DW_FORM_ref_addr:
23352 fprintf_unfiltered (f, "ref address: ");
529908cb 23353 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23354 break;
36586728
TT
23355 case DW_FORM_GNU_ref_alt:
23356 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23357 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23358 break;
10b3939b
DJ
23359 case DW_FORM_ref1:
23360 case DW_FORM_ref2:
23361 case DW_FORM_ref4:
4568ecf9
DE
23362 case DW_FORM_ref8:
23363 case DW_FORM_ref_udata:
d97bc12b 23364 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23365 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23366 break;
c906108c
SS
23367 case DW_FORM_data1:
23368 case DW_FORM_data2:
23369 case DW_FORM_data4:
ce5d95e1 23370 case DW_FORM_data8:
c906108c 23371 case DW_FORM_udata:
43bbcdc2 23372 fprintf_unfiltered (f, "constant: %s",
529908cb 23373 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23374 break;
2dc7f7b3
TT
23375 case DW_FORM_sec_offset:
23376 fprintf_unfiltered (f, "section offset: %s",
529908cb 23377 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23378 break;
55f1336d 23379 case DW_FORM_ref_sig8:
ac9ec31b 23380 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23381 hex_string (die->attrs[i].as_signature ()));
348e048f 23382 break;
c906108c 23383 case DW_FORM_string:
4bdf3d34 23384 case DW_FORM_strp:
43988095 23385 case DW_FORM_line_strp:
cf532bd1 23386 case DW_FORM_strx:
3019eac3 23387 case DW_FORM_GNU_str_index:
36586728 23388 case DW_FORM_GNU_strp_alt:
8285870a 23389 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23390 die->attrs[i].as_string ()
23391 ? die->attrs[i].as_string () : "",
23392 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23393 break;
23394 case DW_FORM_flag:
c45bc3f8 23395 if (die->attrs[i].as_boolean ())
d97bc12b 23396 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23397 else
d97bc12b 23398 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23399 break;
2dc7f7b3
TT
23400 case DW_FORM_flag_present:
23401 fprintf_unfiltered (f, "flag: TRUE");
23402 break;
a8329558 23403 case DW_FORM_indirect:
0963b4bd
MS
23404 /* The reader will have reduced the indirect form to
23405 the "base form" so this form should not occur. */
5f48f8f3 23406 fprintf_unfiltered (f,
3e43a32a 23407 "unexpected attribute form: DW_FORM_indirect");
a8329558 23408 break;
1bc397c5 23409 case DW_FORM_sdata:
663c44ac
JK
23410 case DW_FORM_implicit_const:
23411 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23412 plongest (die->attrs[i].as_signed ()));
663c44ac 23413 break;
c906108c 23414 default:
d97bc12b 23415 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23416 die->attrs[i].form);
d97bc12b 23417 break;
c906108c 23418 }
d97bc12b 23419 fprintf_unfiltered (f, "\n");
c906108c
SS
23420 }
23421}
23422
f9aca02d 23423static void
d97bc12b 23424dump_die_for_error (struct die_info *die)
c906108c 23425{
d97bc12b
DE
23426 dump_die_shallow (gdb_stderr, 0, die);
23427}
23428
23429static void
23430dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23431{
23432 int indent = level * 4;
23433
23434 gdb_assert (die != NULL);
23435
23436 if (level >= max_level)
23437 return;
23438
23439 dump_die_shallow (f, indent, die);
23440
23441 if (die->child != NULL)
c906108c 23442 {
d97bc12b
DE
23443 print_spaces (indent, f);
23444 fprintf_unfiltered (f, " Children:");
23445 if (level + 1 < max_level)
23446 {
23447 fprintf_unfiltered (f, "\n");
23448 dump_die_1 (f, level + 1, max_level, die->child);
23449 }
23450 else
23451 {
3e43a32a
MS
23452 fprintf_unfiltered (f,
23453 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23454 }
23455 }
23456
23457 if (die->sibling != NULL && level > 0)
23458 {
23459 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23460 }
23461}
23462
d97bc12b
DE
23463/* This is called from the pdie macro in gdbinit.in.
23464 It's not static so gcc will keep a copy callable from gdb. */
23465
23466void
23467dump_die (struct die_info *die, int max_level)
23468{
23469 dump_die_1 (gdb_stdlog, 0, max_level, die);
23470}
23471
f9aca02d 23472static void
51545339 23473store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23474{
51545339 23475 void **slot;
c906108c 23476
9c541725
PA
23477 slot = htab_find_slot_with_hash (cu->die_hash, die,
23478 to_underlying (die->sect_off),
b64f50a1 23479 INSERT);
51545339
DJ
23480
23481 *slot = die;
c906108c
SS
23482}
23483
348e048f
DE
23484/* Follow reference or signature attribute ATTR of SRC_DIE.
23485 On entry *REF_CU is the CU of SRC_DIE.
23486 On exit *REF_CU is the CU of the result. */
23487
23488static struct die_info *
ff39bb5e 23489follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23490 struct dwarf2_cu **ref_cu)
23491{
23492 struct die_info *die;
23493
cd6c91b4 23494 if (attr->form_is_ref ())
348e048f 23495 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23496 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23497 die = follow_die_sig (src_die, attr, ref_cu);
23498 else
23499 {
23500 dump_die_for_error (src_die);
23501 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23502 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23503 }
23504
23505 return die;
03dd20cc
DJ
23506}
23507
5c631832 23508/* Follow reference OFFSET.
673bfd45
DE
23509 On entry *REF_CU is the CU of the source die referencing OFFSET.
23510 On exit *REF_CU is the CU of the result.
23511 Returns NULL if OFFSET is invalid. */
f504f079 23512
f9aca02d 23513static struct die_info *
9c541725 23514follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23515 struct dwarf2_cu **ref_cu)
c906108c 23516{
10b3939b 23517 struct die_info temp_die;
f2f0e013 23518 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23519 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23520
348e048f
DE
23521 gdb_assert (cu->per_cu != NULL);
23522
98bfdba5
PA
23523 target_cu = cu;
23524
3019eac3 23525 if (cu->per_cu->is_debug_types)
348e048f
DE
23526 {
23527 /* .debug_types CUs cannot reference anything outside their CU.
23528 If they need to, they have to reference a signatured type via
55f1336d 23529 DW_FORM_ref_sig8. */
4057dfde 23530 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23531 return NULL;
348e048f 23532 }
36586728 23533 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23534 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23535 {
23536 struct dwarf2_per_cu_data *per_cu;
9a619af0 23537
9c541725 23538 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23539 per_objfile);
03dd20cc
DJ
23540
23541 /* If necessary, add it to the queue and load its DIEs. */
976ca316 23542 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
23543 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23544 false, cu->language);
03dd20cc 23545
976ca316 23546 target_cu = per_objfile->get_cu (per_cu);
10b3939b 23547 }
98bfdba5
PA
23548 else if (cu->dies == NULL)
23549 {
23550 /* We're loading full DIEs during partial symbol reading. */
976ca316 23551 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23552 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23553 language_minimal);
98bfdba5 23554 }
c906108c 23555
f2f0e013 23556 *ref_cu = target_cu;
9c541725 23557 temp_die.sect_off = sect_off;
c24bdb02
KS
23558
23559 if (target_cu != cu)
23560 target_cu->ancestor = cu;
23561
9a3c8263 23562 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23563 &temp_die,
23564 to_underlying (sect_off));
5c631832 23565}
10b3939b 23566
5c631832
JK
23567/* Follow reference attribute ATTR of SRC_DIE.
23568 On entry *REF_CU is the CU of SRC_DIE.
23569 On exit *REF_CU is the CU of the result. */
23570
23571static struct die_info *
ff39bb5e 23572follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23573 struct dwarf2_cu **ref_cu)
23574{
0826b30a 23575 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23576 struct dwarf2_cu *cu = *ref_cu;
23577 struct die_info *die;
23578
9c541725 23579 die = follow_die_offset (sect_off,
36586728
TT
23580 (attr->form == DW_FORM_GNU_ref_alt
23581 || cu->per_cu->is_dwz),
23582 ref_cu);
5c631832 23583 if (!die)
9d8780f0
SM
23584 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23585 "at %s [in module %s]"),
23586 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23587 objfile_name (cu->per_objfile->objfile));
348e048f 23588
5c631832
JK
23589 return die;
23590}
23591
d4c9a4f8 23592/* See read.h. */
5c631832
JK
23593
23594struct dwarf2_locexpr_baton
9c541725 23595dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23596 dwarf2_per_cu_data *per_cu,
976ca316 23597 dwarf2_per_objfile *per_objfile,
041d9819
SM
23598 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23599 bool resolve_abstract_p)
5c631832 23600{
5c631832
JK
23601 struct die_info *die;
23602 struct attribute *attr;
23603 struct dwarf2_locexpr_baton retval;
976ca316 23604 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23605
976ca316 23606 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23607 if (cu == nullptr)
976ca316 23608 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23609
23610 if (cu == nullptr)
cc12ce38
DE
23611 {
23612 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23613 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23614 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23615 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23616 }
918dd910 23617
9c541725 23618 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23619 if (!die)
9d8780f0
SM
23620 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23621 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23622
23623 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23624 if (!attr && resolve_abstract_p
976ca316
SM
23625 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23626 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23627 {
041d9819 23628 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23629 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23630 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23631
3360b6e7 23632 for (const auto &cand_off
976ca316 23633 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23634 {
3360b6e7
TV
23635 struct dwarf2_cu *cand_cu = cu;
23636 struct die_info *cand
23637 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23638 if (!cand
23639 || !cand->parent
e4a62c65
TV
23640 || cand->parent->tag != DW_TAG_subprogram)
23641 continue;
23642
23643 CORE_ADDR pc_low, pc_high;
23644 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23645 if (pc_low == ((CORE_ADDR) -1))
23646 continue;
23647 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23648 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23649 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23650 continue;
23651
23652 die = cand;
23653 attr = dwarf2_attr (die, DW_AT_location, cu);
23654 break;
23655 }
23656 }
23657
5c631832
JK
23658 if (!attr)
23659 {
e103e986
JK
23660 /* DWARF: "If there is no such attribute, then there is no effect.".
23661 DATA is ignored if SIZE is 0. */
5c631832 23662
e103e986 23663 retval.data = NULL;
5c631832
JK
23664 retval.size = 0;
23665 }
cd6c91b4 23666 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23667 {
23668 struct dwarf2_loclist_baton loclist_baton;
041d9819 23669 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23670 size_t size;
23671
23672 fill_in_loclist_baton (cu, &loclist_baton, attr);
23673
23674 retval.data = dwarf2_find_location_expression (&loclist_baton,
23675 &size, pc);
23676 retval.size = size;
23677 }
5c631832
JK
23678 else
23679 {
4fc6c0d5 23680 if (!attr->form_is_block ())
9d8780f0 23681 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23682 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23683 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23684
9d2246fc
TT
23685 struct dwarf_block *block = attr->as_block ();
23686 retval.data = block->data;
23687 retval.size = block->size;
5c631832 23688 }
976ca316 23689 retval.per_objfile = per_objfile;
5c631832 23690 retval.per_cu = cu->per_cu;
918dd910 23691
976ca316 23692 per_objfile->age_comp_units ();
918dd910 23693
5c631832 23694 return retval;
348e048f
DE
23695}
23696
d4c9a4f8 23697/* See read.h. */
8b9737bf
TT
23698
23699struct dwarf2_locexpr_baton
23700dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23701 dwarf2_per_cu_data *per_cu,
14095eb3 23702 dwarf2_per_objfile *per_objfile,
041d9819 23703 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23704{
9c541725 23705 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23706
14095eb3 23707 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23708 get_frame_pc);
8b9737bf
TT
23709}
23710
b6807d98
TT
23711/* Write a constant of a given type as target-ordered bytes into
23712 OBSTACK. */
23713
23714static const gdb_byte *
23715write_constant_as_bytes (struct obstack *obstack,
23716 enum bfd_endian byte_order,
23717 struct type *type,
23718 ULONGEST value,
23719 LONGEST *len)
23720{
23721 gdb_byte *result;
23722
23723 *len = TYPE_LENGTH (type);
224c3ddb 23724 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23725 store_unsigned_integer (result, *len, byte_order, value);
23726
23727 return result;
23728}
23729
d4c9a4f8 23730/* See read.h. */
b6807d98
TT
23731
23732const gdb_byte *
9c541725 23733dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23734 dwarf2_per_cu_data *per_cu,
14095eb3 23735 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23736 obstack *obstack,
b6807d98
TT
23737 LONGEST *len)
23738{
b6807d98
TT
23739 struct die_info *die;
23740 struct attribute *attr;
23741 const gdb_byte *result = NULL;
23742 struct type *type;
23743 LONGEST value;
23744 enum bfd_endian byte_order;
14095eb3 23745 struct objfile *objfile = per_objfile->objfile;
b6807d98 23746
7188ed02 23747 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23748 if (cu == nullptr)
23749 cu = load_cu (per_cu, per_objfile, false);
23750
23751 if (cu == nullptr)
cc12ce38
DE
23752 {
23753 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23754 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23755 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23756 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23757 }
b6807d98 23758
9c541725 23759 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23760 if (!die)
9d8780f0
SM
23761 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23762 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23763
23764 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23765 if (attr == NULL)
23766 return NULL;
23767
e3b94546 23768 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23769 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23770
23771 switch (attr->form)
23772 {
23773 case DW_FORM_addr:
336d760d 23774 case DW_FORM_addrx:
b6807d98
TT
23775 case DW_FORM_GNU_addr_index:
23776 {
23777 gdb_byte *tem;
23778
23779 *len = cu->header.addr_size;
224c3ddb 23780 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23781 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23782 result = tem;
23783 }
23784 break;
23785 case DW_FORM_string:
23786 case DW_FORM_strp:
cf532bd1 23787 case DW_FORM_strx:
b6807d98
TT
23788 case DW_FORM_GNU_str_index:
23789 case DW_FORM_GNU_strp_alt:
c6481205 23790 /* The string is already allocated on the objfile obstack, point
b6807d98 23791 directly to it. */
2c830f54
TT
23792 {
23793 const char *attr_name = attr->as_string ();
23794 result = (const gdb_byte *) attr_name;
23795 *len = strlen (attr_name);
23796 }
b6807d98
TT
23797 break;
23798 case DW_FORM_block1:
23799 case DW_FORM_block2:
23800 case DW_FORM_block4:
23801 case DW_FORM_block:
23802 case DW_FORM_exprloc:
0224619f 23803 case DW_FORM_data16:
9d2246fc
TT
23804 {
23805 struct dwarf_block *block = attr->as_block ();
23806 result = block->data;
23807 *len = block->size;
23808 }
b6807d98
TT
23809 break;
23810
23811 /* The DW_AT_const_value attributes are supposed to carry the
23812 symbol's value "represented as it would be on the target
23813 architecture." By the time we get here, it's already been
23814 converted to host endianness, so we just need to sign- or
23815 zero-extend it as appropriate. */
23816 case DW_FORM_data1:
23817 type = die_type (die, cu);
23818 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23819 if (result == NULL)
23820 result = write_constant_as_bytes (obstack, byte_order,
23821 type, value, len);
23822 break;
23823 case DW_FORM_data2:
23824 type = die_type (die, cu);
23825 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23826 if (result == NULL)
23827 result = write_constant_as_bytes (obstack, byte_order,
23828 type, value, len);
23829 break;
23830 case DW_FORM_data4:
23831 type = die_type (die, cu);
23832 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23833 if (result == NULL)
23834 result = write_constant_as_bytes (obstack, byte_order,
23835 type, value, len);
23836 break;
23837 case DW_FORM_data8:
23838 type = die_type (die, cu);
23839 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23840 if (result == NULL)
23841 result = write_constant_as_bytes (obstack, byte_order,
23842 type, value, len);
23843 break;
23844
23845 case DW_FORM_sdata:
663c44ac 23846 case DW_FORM_implicit_const:
b6807d98
TT
23847 type = die_type (die, cu);
23848 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23849 type, attr->as_signed (), len);
b6807d98
TT
23850 break;
23851
23852 case DW_FORM_udata:
23853 type = die_type (die, cu);
23854 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23855 type, attr->as_unsigned (), len);
b6807d98
TT
23856 break;
23857
23858 default:
b98664d3 23859 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23860 dwarf_form_name (attr->form));
23861 break;
23862 }
23863
23864 return result;
23865}
23866
d4c9a4f8 23867/* See read.h. */
7942e96e
AA
23868
23869struct type *
9c541725 23870dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
23871 dwarf2_per_cu_data *per_cu,
23872 dwarf2_per_objfile *per_objfile)
7942e96e 23873{
7942e96e
AA
23874 struct die_info *die;
23875
7188ed02 23876 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23877 if (cu == nullptr)
23878 cu = load_cu (per_cu, per_objfile, false);
23879
23880 if (cu == nullptr)
23881 return nullptr;
7942e96e 23882
9c541725 23883 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
23884 if (!die)
23885 return NULL;
23886
23887 return die_type (die, cu);
23888}
23889
8cb5117c 23890/* See read.h. */
8a9b8146
TT
23891
23892struct type *
b64f50a1 23893dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
23894 dwarf2_per_cu_data *per_cu,
23895 dwarf2_per_objfile *per_objfile)
8a9b8146 23896{
9c541725 23897 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 23898 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
23899}
23900
ac9ec31b 23901/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 23902 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
23903 On exit *REF_CU is the CU of the result.
23904 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
23905
23906static struct die_info *
ac9ec31b
DE
23907follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
23908 struct dwarf2_cu **ref_cu)
348e048f 23909{
348e048f 23910 struct die_info temp_die;
c24bdb02 23911 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 23912 struct die_info *die;
976ca316 23913 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 23914
348e048f 23915
ac9ec31b
DE
23916 /* While it might be nice to assert sig_type->type == NULL here,
23917 we can get here for DW_AT_imported_declaration where we need
23918 the DIE not the type. */
348e048f
DE
23919
23920 /* If necessary, add it to the queue and load its DIEs. */
23921
976ca316 23922 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 23923 language_minimal))
976ca316 23924 read_signatured_type (sig_type, per_objfile);
348e048f 23925
976ca316 23926 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 23927 gdb_assert (sig_cu != NULL);
9c541725
PA
23928 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
23929 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 23930 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 23931 to_underlying (temp_die.sect_off));
348e048f
DE
23932 if (die)
23933 {
796a7ff8
DE
23934 /* For .gdb_index version 7 keep track of included TUs.
23935 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
23936 if (per_objfile->per_bfd->index_table != NULL
23937 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 23938 {
ae640021 23939 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
23940 }
23941
348e048f 23942 *ref_cu = sig_cu;
c24bdb02
KS
23943 if (sig_cu != cu)
23944 sig_cu->ancestor = cu;
23945
348e048f
DE
23946 return die;
23947 }
23948
ac9ec31b
DE
23949 return NULL;
23950}
23951
23952/* Follow signatured type referenced by ATTR in SRC_DIE.
23953 On entry *REF_CU is the CU of SRC_DIE.
23954 On exit *REF_CU is the CU of the result.
23955 The result is the DIE of the type.
23956 If the referenced type cannot be found an error is thrown. */
23957
23958static struct die_info *
ff39bb5e 23959follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
23960 struct dwarf2_cu **ref_cu)
23961{
630ed6b9 23962 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
23963 struct signatured_type *sig_type;
23964 struct die_info *die;
23965
23966 gdb_assert (attr->form == DW_FORM_ref_sig8);
23967
a2ce51a0 23968 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
23969 /* sig_type will be NULL if the signatured type is missing from
23970 the debug info. */
23971 if (sig_type == NULL)
23972 {
23973 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
23974 " from DIE at %s [in module %s]"),
23975 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23976 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23977 }
23978
23979 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
23980 if (die == NULL)
23981 {
23982 dump_die_for_error (src_die);
23983 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
23984 " from DIE at %s [in module %s]"),
23985 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 23986 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
23987 }
23988
23989 return die;
23990}
23991
23992/* Get the type specified by SIGNATURE referenced in DIE/CU,
23993 reading in and processing the type unit if necessary. */
23994
23995static struct type *
23996get_signatured_type (struct die_info *die, ULONGEST signature,
23997 struct dwarf2_cu *cu)
23998{
976ca316 23999 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24000 struct signatured_type *sig_type;
24001 struct dwarf2_cu *type_cu;
24002 struct die_info *type_die;
24003 struct type *type;
24004
a2ce51a0 24005 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24006 /* sig_type will be NULL if the signatured type is missing from
24007 the debug info. */
24008 if (sig_type == NULL)
24009 {
b98664d3 24010 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24011 " from DIE at %s [in module %s]"),
24012 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24013 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24014 return build_error_marker_type (cu, die);
24015 }
24016
24017 /* If we already know the type we're done. */
976ca316 24018 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24019 if (type != nullptr)
24020 return type;
ac9ec31b
DE
24021
24022 type_cu = cu;
24023 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24024 if (type_die != NULL)
24025 {
24026 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24027 is created. This is important, for example, because for c++ classes
24028 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24029 type = read_type_die (type_die, type_cu);
24030 if (type == NULL)
24031 {
b98664d3 24032 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24033 " referenced from DIE at %s [in module %s]"),
24034 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24035 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24036 type = build_error_marker_type (cu, die);
24037 }
24038 }
24039 else
24040 {
b98664d3 24041 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24042 " from DIE at %s [in module %s]"),
24043 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24044 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24045 type = build_error_marker_type (cu, die);
24046 }
e286671b 24047
976ca316 24048 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24049
24050 return type;
24051}
24052
24053/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24054 reading in and processing the type unit if necessary. */
24055
24056static struct type *
ff39bb5e 24057get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24058 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24059{
24060 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24061 if (attr->form_is_ref ())
ac9ec31b
DE
24062 {
24063 struct dwarf2_cu *type_cu = cu;
24064 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24065
24066 return read_type_die (type_die, type_cu);
24067 }
24068 else if (attr->form == DW_FORM_ref_sig8)
24069 {
630ed6b9 24070 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24071 }
24072 else
24073 {
976ca316 24074 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24075
b98664d3 24076 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24077 " at %s [in module %s]"),
24078 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24079 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24080 return build_error_marker_type (cu, die);
24081 }
348e048f
DE
24082}
24083
e5fe5e75 24084/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24085
24086static void
ab432490
SM
24087load_full_type_unit (dwarf2_per_cu_data *per_cu,
24088 dwarf2_per_objfile *per_objfile)
348e048f 24089{
52dc124a 24090 struct signatured_type *sig_type;
348e048f 24091
f4dc4d17 24092 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24093 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24094
6721b2ec
DE
24095 /* We have the per_cu, but we need the signatured_type.
24096 Fortunately this is an easy translation. */
24097 gdb_assert (per_cu->is_debug_types);
24098 sig_type = (struct signatured_type *) per_cu;
348e048f 24099
7188ed02 24100 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24101
ab432490 24102 read_signatured_type (sig_type, per_objfile);
348e048f 24103
7188ed02 24104 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24105}
24106
3019eac3
DE
24107/* Read in a signatured type and build its CU and DIEs.
24108 If the type is a stub for the real type in a DWO file,
24109 read in the real type from the DWO file as well. */
dee91e82
DE
24110
24111static void
ab432490
SM
24112read_signatured_type (signatured_type *sig_type,
24113 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24114{
24115 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24116
3019eac3 24117 gdb_assert (per_cu->is_debug_types);
7188ed02 24118 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24119
2e671100 24120 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24121
24122 if (!reader.dummy_p)
24123 {
24124 struct dwarf2_cu *cu = reader.cu;
24125 const gdb_byte *info_ptr = reader.info_ptr;
24126
24127 gdb_assert (cu->die_hash == NULL);
24128 cu->die_hash =
24129 htab_create_alloc_ex (cu->header.length / 12,
24130 die_hash,
24131 die_eq,
24132 NULL,
24133 &cu->comp_unit_obstack,
24134 hashtab_obstack_allocate,
24135 dummy_obstack_deallocate);
24136
3e225074 24137 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24138 reader.comp_unit_die->child
24139 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24140 reader.comp_unit_die);
24141 cu->dies = reader.comp_unit_die;
24142 /* comp_unit_die is not stored in die_hash, no need. */
24143
24144 /* We try not to read any attributes in this function, because
24145 not all CUs needed for references have been loaded yet, and
24146 symbol table processing isn't initialized. But we have to
24147 set the CU language, or we won't be able to build types
24148 correctly. Similarly, if we do not read the producer, we can
24149 not apply producer-specific interpretation. */
24150 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24151
24152 reader.keep ();
c0ab21c2
TT
24153 }
24154
7ee85ab1 24155 sig_type->per_cu.tu_read = 1;
c906108c
SS
24156}
24157
c906108c
SS
24158/* Decode simple location descriptions.
24159 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24160 the location and return the value. If COMPUTED is non-null, it is
24161 set to true to indicate that decoding was successful, and false
24162 otherwise. If COMPUTED is null, then this function may emit a
24163 complaint. */
c906108c
SS
24164
24165static CORE_ADDR
7d79de9a 24166decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24167{
5e22e966 24168 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24169 size_t i;
24170 size_t size = blk->size;
d521ce57 24171 const gdb_byte *data = blk->data;
21ae7a4d
JK
24172 CORE_ADDR stack[64];
24173 int stacki;
24174 unsigned int bytes_read, unsnd;
24175 gdb_byte op;
c906108c 24176
7d79de9a
TT
24177 if (computed != nullptr)
24178 *computed = false;
24179
21ae7a4d
JK
24180 i = 0;
24181 stacki = 0;
24182 stack[stacki] = 0;
24183 stack[++stacki] = 0;
24184
24185 while (i < size)
24186 {
24187 op = data[i++];
24188 switch (op)
24189 {
24190 case DW_OP_lit0:
24191 case DW_OP_lit1:
24192 case DW_OP_lit2:
24193 case DW_OP_lit3:
24194 case DW_OP_lit4:
24195 case DW_OP_lit5:
24196 case DW_OP_lit6:
24197 case DW_OP_lit7:
24198 case DW_OP_lit8:
24199 case DW_OP_lit9:
24200 case DW_OP_lit10:
24201 case DW_OP_lit11:
24202 case DW_OP_lit12:
24203 case DW_OP_lit13:
24204 case DW_OP_lit14:
24205 case DW_OP_lit15:
24206 case DW_OP_lit16:
24207 case DW_OP_lit17:
24208 case DW_OP_lit18:
24209 case DW_OP_lit19:
24210 case DW_OP_lit20:
24211 case DW_OP_lit21:
24212 case DW_OP_lit22:
24213 case DW_OP_lit23:
24214 case DW_OP_lit24:
24215 case DW_OP_lit25:
24216 case DW_OP_lit26:
24217 case DW_OP_lit27:
24218 case DW_OP_lit28:
24219 case DW_OP_lit29:
24220 case DW_OP_lit30:
24221 case DW_OP_lit31:
24222 stack[++stacki] = op - DW_OP_lit0;
24223 break;
f1bea926 24224
21ae7a4d
JK
24225 case DW_OP_reg0:
24226 case DW_OP_reg1:
24227 case DW_OP_reg2:
24228 case DW_OP_reg3:
24229 case DW_OP_reg4:
24230 case DW_OP_reg5:
24231 case DW_OP_reg6:
24232 case DW_OP_reg7:
24233 case DW_OP_reg8:
24234 case DW_OP_reg9:
24235 case DW_OP_reg10:
24236 case DW_OP_reg11:
24237 case DW_OP_reg12:
24238 case DW_OP_reg13:
24239 case DW_OP_reg14:
24240 case DW_OP_reg15:
24241 case DW_OP_reg16:
24242 case DW_OP_reg17:
24243 case DW_OP_reg18:
24244 case DW_OP_reg19:
24245 case DW_OP_reg20:
24246 case DW_OP_reg21:
24247 case DW_OP_reg22:
24248 case DW_OP_reg23:
24249 case DW_OP_reg24:
24250 case DW_OP_reg25:
24251 case DW_OP_reg26:
24252 case DW_OP_reg27:
24253 case DW_OP_reg28:
24254 case DW_OP_reg29:
24255 case DW_OP_reg30:
24256 case DW_OP_reg31:
24257 stack[++stacki] = op - DW_OP_reg0;
24258 if (i < size)
7d79de9a
TT
24259 {
24260 if (computed == nullptr)
24261 dwarf2_complex_location_expr_complaint ();
24262 else
24263 return 0;
24264 }
21ae7a4d 24265 break;
c906108c 24266
21ae7a4d
JK
24267 case DW_OP_regx:
24268 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24269 i += bytes_read;
24270 stack[++stacki] = unsnd;
24271 if (i < size)
7d79de9a
TT
24272 {
24273 if (computed == nullptr)
24274 dwarf2_complex_location_expr_complaint ();
24275 else
24276 return 0;
24277 }
21ae7a4d 24278 break;
c906108c 24279
21ae7a4d 24280 case DW_OP_addr:
c8a7a66f
TT
24281 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24282 &bytes_read);
21ae7a4d
JK
24283 i += bytes_read;
24284 break;
d53d4ac5 24285
21ae7a4d
JK
24286 case DW_OP_const1u:
24287 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24288 i += 1;
24289 break;
24290
24291 case DW_OP_const1s:
24292 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24293 i += 1;
24294 break;
24295
24296 case DW_OP_const2u:
24297 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24298 i += 2;
24299 break;
24300
24301 case DW_OP_const2s:
24302 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24303 i += 2;
24304 break;
d53d4ac5 24305
21ae7a4d
JK
24306 case DW_OP_const4u:
24307 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24308 i += 4;
24309 break;
24310
24311 case DW_OP_const4s:
24312 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24313 i += 4;
24314 break;
24315
585861ea
JK
24316 case DW_OP_const8u:
24317 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24318 i += 8;
24319 break;
24320
21ae7a4d
JK
24321 case DW_OP_constu:
24322 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24323 &bytes_read);
24324 i += bytes_read;
24325 break;
24326
24327 case DW_OP_consts:
24328 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24329 i += bytes_read;
24330 break;
24331
24332 case DW_OP_dup:
24333 stack[stacki + 1] = stack[stacki];
24334 stacki++;
24335 break;
24336
24337 case DW_OP_plus:
24338 stack[stacki - 1] += stack[stacki];
24339 stacki--;
24340 break;
24341
24342 case DW_OP_plus_uconst:
24343 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24344 &bytes_read);
24345 i += bytes_read;
24346 break;
24347
24348 case DW_OP_minus:
24349 stack[stacki - 1] -= stack[stacki];
24350 stacki--;
24351 break;
24352
24353 case DW_OP_deref:
24354 /* If we're not the last op, then we definitely can't encode
24355 this using GDB's address_class enum. This is valid for partial
24356 global symbols, although the variable's address will be bogus
24357 in the psymtab. */
24358 if (i < size)
7d79de9a
TT
24359 {
24360 if (computed == nullptr)
24361 dwarf2_complex_location_expr_complaint ();
24362 else
24363 return 0;
24364 }
21ae7a4d
JK
24365 break;
24366
dda83cd7 24367 case DW_OP_GNU_push_tls_address:
4aa4e28b 24368 case DW_OP_form_tls_address:
21ae7a4d
JK
24369 /* The top of the stack has the offset from the beginning
24370 of the thread control block at which the variable is located. */
24371 /* Nothing should follow this operator, so the top of stack would
24372 be returned. */
24373 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24374 address will be bogus in the psymtab. Make it always at least
24375 non-zero to not look as a variable garbage collected by linker
24376 which have DW_OP_addr 0. */
21ae7a4d 24377 if (i < size)
7d79de9a
TT
24378 {
24379 if (computed == nullptr)
24380 dwarf2_complex_location_expr_complaint ();
24381 else
24382 return 0;
24383 }
585861ea 24384 stack[stacki]++;
dda83cd7 24385 break;
21ae7a4d
JK
24386
24387 case DW_OP_GNU_uninit:
7d79de9a
TT
24388 if (computed != nullptr)
24389 return 0;
21ae7a4d
JK
24390 break;
24391
336d760d 24392 case DW_OP_addrx:
3019eac3 24393 case DW_OP_GNU_addr_index:
49f6c839 24394 case DW_OP_GNU_const_index:
3019eac3
DE
24395 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24396 &bytes_read);
24397 i += bytes_read;
24398 break;
24399
21ae7a4d 24400 default:
7d79de9a
TT
24401 if (computed == nullptr)
24402 {
24403 const char *name = get_DW_OP_name (op);
21ae7a4d 24404
7d79de9a
TT
24405 if (name)
24406 complaint (_("unsupported stack op: '%s'"),
24407 name);
24408 else
24409 complaint (_("unsupported stack op: '%02x'"),
24410 op);
24411 }
21ae7a4d
JK
24412
24413 return (stack[stacki]);
d53d4ac5 24414 }
3c6e0cb3 24415
21ae7a4d 24416 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24417 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24418 if (stacki >= ARRAY_SIZE (stack) - 1)
24419 {
7d79de9a
TT
24420 if (computed == nullptr)
24421 complaint (_("location description stack overflow"));
21ae7a4d
JK
24422 return 0;
24423 }
24424
24425 if (stacki <= 0)
24426 {
7d79de9a
TT
24427 if (computed == nullptr)
24428 complaint (_("location description stack underflow"));
21ae7a4d
JK
24429 return 0;
24430 }
24431 }
7d79de9a
TT
24432
24433 if (computed != nullptr)
24434 *computed = true;
21ae7a4d 24435 return (stack[stacki]);
c906108c
SS
24436}
24437
24438/* memory allocation interface */
24439
c906108c 24440static struct dwarf_block *
7b5a2f43 24441dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24442{
8d749320 24443 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24444}
24445
c906108c 24446static struct die_info *
b60c80d6 24447dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24448{
24449 struct die_info *die;
b60c80d6
DJ
24450 size_t size = sizeof (struct die_info);
24451
24452 if (num_attrs > 1)
24453 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24454
b60c80d6 24455 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24456 memset (die, 0, sizeof (struct die_info));
24457 return (die);
24458}
2e276125
JB
24459
24460\f
a036ba48 24461
c90ec28a 24462/* Macro support. */
cf2c3c16 24463
9eac9650
TT
24464/* An overload of dwarf_decode_macros that finds the correct section
24465 and ensures it is read in before calling the other overload. */
24466
24467static void
24468dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24469 int section_is_gnu)
24470{
976ca316
SM
24471 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24472 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24473 const struct line_header *lh = cu->line_header;
9eac9650
TT
24474 unsigned int offset_size = cu->header.offset_size;
24475 struct dwarf2_section_info *section;
24476 const char *section_name;
24477
24478 if (cu->dwo_unit != nullptr)
24479 {
24480 if (section_is_gnu)
24481 {
24482 section = &cu->dwo_unit->dwo_file->sections.macro;
24483 section_name = ".debug_macro.dwo";
24484 }
24485 else
24486 {
24487 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24488 section_name = ".debug_macinfo.dwo";
24489 }
24490 }
24491 else
24492 {
24493 if (section_is_gnu)
24494 {
976ca316 24495 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24496 section_name = ".debug_macro";
24497 }
24498 else
24499 {
976ca316 24500 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24501 section_name = ".debug_macinfo";
24502 }
24503 }
24504
24505 section->read (objfile);
24506 if (section->buffer == nullptr)
24507 {
24508 complaint (_("missing %s section"), section_name);
24509 return;
24510 }
24511
24512 buildsym_compunit *builder = cu->get_builder ();
24513
048fde1e 24514 struct dwarf2_section_info *str_offsets_section;
24515 struct dwarf2_section_info *str_section;
24516 ULONGEST str_offsets_base;
24517
24518 if (cu->dwo_unit != nullptr)
24519 {
24520 str_offsets_section = &cu->dwo_unit->dwo_file
24521 ->sections.str_offsets;
24522 str_section = &cu->dwo_unit->dwo_file->sections.str;
24523 str_offsets_base = cu->header.addr_size;
24524 }
24525 else
24526 {
24527 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24528 str_section = &per_objfile->per_bfd->str;
24529 str_offsets_base = *cu->str_offsets_base;
24530 }
24531
976ca316 24532 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24533 offset_size, offset, str_section, str_offsets_section,
24534 str_offsets_base, section_is_gnu);
9eac9650
TT
24535}
24536
3019eac3
DE
24537/* Return the .debug_loc section to use for CU.
24538 For DWO files use .debug_loc.dwo. */
24539
24540static struct dwarf2_section_info *
24541cu_debug_loc_section (struct dwarf2_cu *cu)
24542{
976ca316 24543 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24544
3019eac3 24545 if (cu->dwo_unit)
43988095
JK
24546 {
24547 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24548
43988095
JK
24549 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24550 }
976ca316
SM
24551 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24552 : &per_objfile->per_bfd->loc);
3019eac3
DE
24553}
24554
d0ce17d8
CT
24555/* Return the .debug_rnglists section to use for CU. */
24556static struct dwarf2_section_info *
24557cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24558{
24559 if (cu->header.version < 5)
24560 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24561 cu->header.version);
24562 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24563
24564 /* Make sure we read the .debug_rnglists section from the file that
24565 contains the DW_AT_ranges attribute we are reading. Normally that
24566 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24567 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24568 program. */
24569 if (cu->dwo_unit != nullptr
24570 && tag != DW_TAG_compile_unit
24571 && tag != DW_TAG_skeleton_unit)
24572 {
24573 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24574
24575 if (sections->rnglists.size > 0)
24576 return &sections->rnglists;
24577 else
24578 error (_(".debug_rnglists section is missing from .dwo file."));
24579 }
24580 return &dwarf2_per_objfile->per_bfd->rnglists;
24581}
24582
8cf6f0b1
TT
24583/* A helper function that fills in a dwarf2_loclist_baton. */
24584
24585static void
24586fill_in_loclist_baton (struct dwarf2_cu *cu,
24587 struct dwarf2_loclist_baton *baton,
ff39bb5e 24588 const struct attribute *attr)
8cf6f0b1 24589{
976ca316 24590 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24591 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24592
976ca316 24593 section->read (per_objfile->objfile);
8cf6f0b1 24594
976ca316 24595 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24596 baton->per_cu = cu->per_cu;
24597 gdb_assert (baton->per_cu);
24598 /* We don't know how long the location list is, but make sure we
24599 don't run off the edge of the section. */
d4df075e
TT
24600 baton->size = section->size - attr->as_unsigned ();
24601 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24602 if (cu->base_address.has_value ())
24603 baton->base_address = *cu->base_address;
24604 else
24605 baton->base_address = 0;
f664829e 24606 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24607}
24608
4c2df51b 24609static void
ff39bb5e 24610dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24611 struct dwarf2_cu *cu, int is_block)
4c2df51b 24612{
976ca316
SM
24613 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24614 struct objfile *objfile = per_objfile->objfile;
3019eac3 24615 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24616
cd6c91b4 24617 if (attr->form_is_section_offset ()
3019eac3 24618 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24619 the section. If so, fall through to the complaint in the
24620 other branch. */
d4df075e 24621 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24622 {
0d53c4c4 24623 struct dwarf2_loclist_baton *baton;
4c2df51b 24624
8d749320 24625 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24626
8cf6f0b1 24627 fill_in_loclist_baton (cu, baton, attr);
be391dca 24628
2b24b6e4 24629 if (!cu->base_address.has_value ())
b98664d3 24630 complaint (_("Location list used without "
3e43a32a 24631 "specifying the CU base address."));
4c2df51b 24632
f1e6e072
TT
24633 SYMBOL_ACLASS_INDEX (sym) = (is_block
24634 ? dwarf2_loclist_block_index
24635 : dwarf2_loclist_index);
0d53c4c4
DJ
24636 SYMBOL_LOCATION_BATON (sym) = baton;
24637 }
24638 else
24639 {
24640 struct dwarf2_locexpr_baton *baton;
24641
8d749320 24642 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24643 baton->per_objfile = per_objfile;
ae0d2f24
UW
24644 baton->per_cu = cu->per_cu;
24645 gdb_assert (baton->per_cu);
0d53c4c4 24646
4fc6c0d5 24647 if (attr->form_is_block ())
0d53c4c4
DJ
24648 {
24649 /* Note that we're just copying the block's data pointer
24650 here, not the actual data. We're still pointing into the
6502dd73
DJ
24651 info_buffer for SYM's objfile; right now we never release
24652 that buffer, but when we do clean up properly this may
24653 need to change. */
9d2246fc
TT
24654 struct dwarf_block *block = attr->as_block ();
24655 baton->size = block->size;
24656 baton->data = block->data;
0d53c4c4
DJ
24657 }
24658 else
24659 {
24660 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24661 sym->natural_name ());
0d53c4c4 24662 baton->size = 0;
0d53c4c4 24663 }
6e70227d 24664
f1e6e072
TT
24665 SYMBOL_ACLASS_INDEX (sym) = (is_block
24666 ? dwarf2_locexpr_block_index
24667 : dwarf2_locexpr_index);
0d53c4c4
DJ
24668 SYMBOL_LOCATION_BATON (sym) = baton;
24669 }
4c2df51b 24670}
6502dd73 24671
2e6a9f79 24672/* See read.h. */
96408a79 24673
2e6a9f79
SM
24674const comp_unit_head *
24675dwarf2_per_cu_data::get_header () const
96408a79 24676{
2e6a9f79
SM
24677 if (!m_header_read_in)
24678 {
24679 const gdb_byte *info_ptr
24680 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24681
2e6a9f79 24682 memset (&m_header, 0, sizeof (m_header));
96408a79 24683
2e6a9f79
SM
24684 read_comp_unit_head (&m_header, info_ptr, this->section,
24685 rcuh_kind::COMPILE);
24686 }
96408a79 24687
2e6a9f79 24688 return &m_header;
96408a79
SA
24689}
24690
09ba997f 24691/* See read.h. */
ae0d2f24 24692
98714339 24693int
09ba997f 24694dwarf2_per_cu_data::addr_size () const
ae0d2f24 24695{
2e6a9f79 24696 return this->get_header ()->addr_size;
ae0d2f24
UW
24697}
24698
09ba997f 24699/* See read.h. */
9eae7c52
TT
24700
24701int
09ba997f 24702dwarf2_per_cu_data::offset_size () const
9eae7c52 24703{
2e6a9f79 24704 return this->get_header ()->offset_size;
96408a79
SA
24705}
24706
09ba997f 24707/* See read.h. */
96408a79
SA
24708
24709int
09ba997f 24710dwarf2_per_cu_data::ref_addr_size () const
96408a79 24711{
2e6a9f79 24712 const comp_unit_head *header = this->get_header ();
96408a79 24713
2e6a9f79
SM
24714 if (header->version == 2)
24715 return header->addr_size;
96408a79 24716 else
2e6a9f79 24717 return header->offset_size;
181cebd4
JK
24718}
24719
09ba997f 24720/* See read.h. */
9aa1f1e3 24721
09ba997f 24722struct type *
293e7e51 24723dwarf2_cu::addr_type () const
9a49df9d 24724{
293e7e51 24725 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24726 struct type *void_type = objfile_type (objfile)->builtin_void;
24727 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24728 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24729
24730 if (TYPE_LENGTH (addr_type) == addr_size)
24731 return addr_type;
24732
c6d940a9 24733 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24734 return addr_type;
24735}
24736
22b6cd70
TT
24737/* A helper function for dwarf2_find_containing_comp_unit that returns
24738 the index of the result, and that searches a vector. It will
24739 return a result even if the offset in question does not actually
24740 occur in any CU. This is separate so that it can be unit
24741 tested. */
ae038cb0 24742
22b6cd70
TT
24743static int
24744dwarf2_find_containing_comp_unit
24745 (sect_offset sect_off,
24746 unsigned int offset_in_dwz,
24747 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24748{
ae038cb0
DJ
24749 int low, high;
24750
ae038cb0 24751 low = 0;
22b6cd70 24752 high = all_comp_units.size () - 1;
ae038cb0
DJ
24753 while (high > low)
24754 {
36586728 24755 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24756 int mid = low + (high - low) / 2;
9a619af0 24757
22b6cd70 24758 mid_cu = all_comp_units[mid];
36586728 24759 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24760 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24761 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24762 high = mid;
24763 else
24764 low = mid + 1;
24765 }
24766 gdb_assert (low == high);
22b6cd70
TT
24767 return low;
24768}
24769
24770/* Locate the .debug_info compilation unit from CU's objfile which contains
24771 the DIE at OFFSET. Raises an error on failure. */
24772
24773static struct dwarf2_per_cu_data *
24774dwarf2_find_containing_comp_unit (sect_offset sect_off,
24775 unsigned int offset_in_dwz,
976ca316 24776 dwarf2_per_objfile *per_objfile)
22b6cd70 24777{
976ca316
SM
24778 int low = dwarf2_find_containing_comp_unit
24779 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24780 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24781
45b8ae0c 24782 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24783 {
36586728 24784 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24785 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24786 "offset %s [in module %s]"),
24787 sect_offset_str (sect_off),
976ca316 24788 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24789
976ca316 24790 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24791 <= sect_off);
976ca316 24792 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24793 }
24794 else
24795 {
976ca316 24796 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24797 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24798 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24799 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24800 return this_cu;
24801 }
24802}
24803
22b6cd70
TT
24804#if GDB_SELF_TEST
24805
24806namespace selftests {
24807namespace find_containing_comp_unit {
24808
24809static void
24810run_test ()
24811{
24812 struct dwarf2_per_cu_data one {};
24813 struct dwarf2_per_cu_data two {};
24814 struct dwarf2_per_cu_data three {};
24815 struct dwarf2_per_cu_data four {};
24816
24817 one.length = 5;
24818 two.sect_off = sect_offset (one.length);
24819 two.length = 7;
24820
24821 three.length = 5;
24822 three.is_dwz = 1;
24823 four.sect_off = sect_offset (three.length);
24824 four.length = 7;
24825 four.is_dwz = 1;
24826
24827 std::vector<dwarf2_per_cu_data *> units;
24828 units.push_back (&one);
24829 units.push_back (&two);
24830 units.push_back (&three);
24831 units.push_back (&four);
24832
24833 int result;
24834
24835 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24836 SELF_CHECK (units[result] == &one);
24837 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24838 SELF_CHECK (units[result] == &one);
24839 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24840 SELF_CHECK (units[result] == &two);
24841
24842 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24843 SELF_CHECK (units[result] == &three);
24844 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24845 SELF_CHECK (units[result] == &three);
24846 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24847 SELF_CHECK (units[result] == &four);
24848}
24849
24850}
24851}
24852
24853#endif /* GDB_SELF_TEST */
24854
9e021579 24855/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24856
9e021579
SM
24857dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24858 dwarf2_per_objfile *per_objfile)
24859 : per_cu (per_cu),
24860 per_objfile (per_objfile),
9068261f
AB
24861 mark (false),
24862 has_loclist (false),
24863 checked_producer (false),
24864 producer_is_gxx_lt_4_6 (false),
24865 producer_is_gcc_lt_4_3 (false),
eb77c9df 24866 producer_is_icc (false),
9068261f 24867 producer_is_icc_lt_14 (false),
c258c396 24868 producer_is_codewarrior (false),
9068261f 24869 processing_has_namespace_info (false)
93311388 24870{
9816fde3
JK
24871}
24872
24873/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
24874
24875static void
95554aad
TT
24876prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
24877 enum language pretend_language)
9816fde3
JK
24878{
24879 struct attribute *attr;
24880
24881 /* Set the language we're debugging. */
24882 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 24883 if (attr != nullptr)
529908cb 24884 set_cu_language (attr->constant_value (0), cu);
9816fde3 24885 else
9cded63f 24886 {
95554aad 24887 cu->language = pretend_language;
9cded63f
TT
24888 cu->language_defn = language_def (cu->language);
24889 }
dee91e82 24890
7d45c7c3 24891 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
24892}
24893
7188ed02 24894/* See read.h. */
ae038cb0 24895
7188ed02
SM
24896dwarf2_cu *
24897dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24898{
7188ed02
SM
24899 auto it = m_dwarf2_cus.find (per_cu);
24900 if (it == m_dwarf2_cus.end ())
24901 return nullptr;
ae038cb0 24902
7188ed02
SM
24903 return it->second;
24904}
24905
24906/* See read.h. */
24907
24908void
24909dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
24910{
24911 gdb_assert (this->get_cu (per_cu) == nullptr);
24912
24913 m_dwarf2_cus[per_cu] = cu;
24914}
24915
24916/* See read.h. */
24917
24918void
24919dwarf2_per_objfile::age_comp_units ()
24920{
24921 /* Start by clearing all marks. */
24922 for (auto pair : m_dwarf2_cus)
24923 pair.second->mark = false;
24924
24925 /* Traverse all CUs, mark them and their dependencies if used recently
24926 enough. */
24927 for (auto pair : m_dwarf2_cus)
ae038cb0 24928 {
7188ed02
SM
24929 dwarf2_cu *cu = pair.second;
24930
24931 cu->last_used++;
24932 if (cu->last_used <= dwarf_max_cache_age)
24933 dwarf2_mark (cu);
ae038cb0
DJ
24934 }
24935
7188ed02
SM
24936 /* Delete all CUs still not marked. */
24937 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 24938 {
7188ed02 24939 dwarf2_cu *cu = it->second;
ae038cb0 24940
7188ed02 24941 if (!cu->mark)
ae038cb0 24942 {
7188ed02
SM
24943 delete cu;
24944 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
24945 }
24946 else
7188ed02 24947 it++;
ae038cb0
DJ
24948 }
24949}
24950
7188ed02 24951/* See read.h. */
ae038cb0 24952
7188ed02
SM
24953void
24954dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 24955{
7188ed02
SM
24956 auto it = m_dwarf2_cus.find (per_cu);
24957 if (it == m_dwarf2_cus.end ())
24958 return;
ae038cb0 24959
7188ed02 24960 delete it->second;
ae038cb0 24961
7188ed02
SM
24962 m_dwarf2_cus.erase (it);
24963}
ae038cb0 24964
7188ed02
SM
24965dwarf2_per_objfile::~dwarf2_per_objfile ()
24966{
24967 remove_all_cus ();
ae038cb0
DJ
24968}
24969
dee91e82
DE
24970/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
24971 We store these in a hash table separate from the DIEs, and preserve them
24972 when the DIEs are flushed out of cache.
24973
24974 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 24975 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
24976 or the type may come from a DWO file. Furthermore, while it's more logical
24977 to use per_cu->section+offset, with Fission the section with the data is in
24978 the DWO file but we don't know that section at the point we need it.
24979 We have to use something in dwarf2_per_cu_data (or the pointer to it)
24980 because we can enter the lookup routine, get_die_type_at_offset, from
24981 outside this file, and thus won't necessarily have PER_CU->cu.
24982 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 24983
dee91e82 24984struct dwarf2_per_cu_offset_and_type
1c379e20 24985{
dee91e82 24986 const struct dwarf2_per_cu_data *per_cu;
9c541725 24987 sect_offset sect_off;
1c379e20
DJ
24988 struct type *type;
24989};
24990
dee91e82 24991/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
24992
24993static hashval_t
dee91e82 24994per_cu_offset_and_type_hash (const void *item)
1c379e20 24995{
9a3c8263
SM
24996 const struct dwarf2_per_cu_offset_and_type *ofs
24997 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 24998
9c541725 24999 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25000}
25001
dee91e82 25002/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25003
25004static int
dee91e82 25005per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25006{
9a3c8263
SM
25007 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25008 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25009 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25010 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25011
dee91e82 25012 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25013 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25014}
25015
25016/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25017 table if necessary. For convenience, return TYPE.
25018
25019 The DIEs reading must have careful ordering to:
85102364 25020 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25021 reading current DIE.
25022 * Not trying to dereference contents of still incompletely read in types
25023 while reading in other DIEs.
25024 * Enable referencing still incompletely read in types just by a pointer to
25025 the type without accessing its fields.
25026
25027 Therefore caller should follow these rules:
25028 * Try to fetch any prerequisite types we may need to build this DIE type
25029 before building the type and calling set_die_type.
e71ec853 25030 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25031 possible before fetching more types to complete the current type.
25032 * Make the type as complete as possible before fetching more types. */
1c379e20 25033
f792889a 25034static struct type *
57567375
TT
25035set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25036 bool skip_data_location)
1c379e20 25037{
976ca316 25038 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25039 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25040 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25041 struct attribute *attr;
25042 struct dynamic_prop prop;
1c379e20 25043
b4ba55a1
JB
25044 /* For Ada types, make sure that the gnat-specific data is always
25045 initialized (if not already set). There are a few types where
25046 we should not be doing so, because the type-specific area is
25047 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25048 where the type-specific area is used to store the floatformat).
25049 But this is not a problem, because the gnat-specific information
25050 is actually not needed for these types. */
25051 if (need_gnat_info (cu)
78134374
SM
25052 && type->code () != TYPE_CODE_FUNC
25053 && type->code () != TYPE_CODE_FLT
25054 && type->code () != TYPE_CODE_METHODPTR
25055 && type->code () != TYPE_CODE_MEMBERPTR
25056 && type->code () != TYPE_CODE_METHOD
09584414 25057 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25058 && !HAVE_GNAT_AUX_INFO (type))
25059 INIT_GNAT_SPECIFIC (type);
25060
3f2f83dd
KB
25061 /* Read DW_AT_allocated and set in type. */
25062 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25063 if (attr != NULL)
3f2f83dd 25064 {
293e7e51 25065 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25066 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25067 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25068 }
3f2f83dd
KB
25069
25070 /* Read DW_AT_associated and set in type. */
25071 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25072 if (attr != NULL)
3f2f83dd 25073 {
293e7e51 25074 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25075 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25076 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25077 }
3f2f83dd 25078
3cdcd0ce 25079 /* Read DW_AT_data_location and set in type. */
57567375
TT
25080 if (!skip_data_location)
25081 {
25082 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25083 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25084 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25085 }
3cdcd0ce 25086
976ca316
SM
25087 if (per_objfile->die_type_hash == NULL)
25088 per_objfile->die_type_hash
0335378b
TT
25089 = htab_up (htab_create_alloc (127,
25090 per_cu_offset_and_type_hash,
25091 per_cu_offset_and_type_eq,
25092 NULL, xcalloc, xfree));
1c379e20 25093
dee91e82 25094 ofs.per_cu = cu->per_cu;
9c541725 25095 ofs.sect_off = die->sect_off;
1c379e20 25096 ofs.type = type;
dee91e82 25097 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25098 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25099 if (*slot)
b98664d3 25100 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25101 sect_offset_str (die->sect_off));
8d749320
SM
25102 *slot = XOBNEW (&objfile->objfile_obstack,
25103 struct dwarf2_per_cu_offset_and_type);
1c379e20 25104 **slot = ofs;
f792889a 25105 return type;
1c379e20
DJ
25106}
25107
9c541725 25108/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25109 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25110
25111static struct type *
9c541725 25112get_die_type_at_offset (sect_offset sect_off,
aa66c379 25113 dwarf2_per_cu_data *per_cu,
976ca316 25114 dwarf2_per_objfile *per_objfile)
1c379e20 25115{
dee91e82 25116 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25117
976ca316 25118 if (per_objfile->die_type_hash == NULL)
f792889a 25119 return NULL;
1c379e20 25120
dee91e82 25121 ofs.per_cu = per_cu;
9c541725 25122 ofs.sect_off = sect_off;
9a3c8263 25123 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25124 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25125 if (slot)
25126 return slot->type;
25127 else
25128 return NULL;
25129}
25130
02142a6c 25131/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25132 or return NULL if DIE does not have a saved type. */
25133
25134static struct type *
25135get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25136{
aa66c379 25137 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25138}
25139
10b3939b
DJ
25140/* Add a dependence relationship from CU to REF_PER_CU. */
25141
25142static void
25143dwarf2_add_dependence (struct dwarf2_cu *cu,
25144 struct dwarf2_per_cu_data *ref_per_cu)
25145{
25146 void **slot;
25147
25148 if (cu->dependencies == NULL)
25149 cu->dependencies
25150 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25151 NULL, &cu->comp_unit_obstack,
25152 hashtab_obstack_allocate,
25153 dummy_obstack_deallocate);
25154
25155 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25156 if (*slot == NULL)
25157 *slot = ref_per_cu;
25158}
1c379e20 25159
f504f079
DE
25160/* Subroutine of dwarf2_mark to pass to htab_traverse.
25161 Set the mark field in every compilation unit in the
7188ed02
SM
25162 cache that we must keep because we are keeping CU.
25163
25164 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25165
10b3939b
DJ
25166static int
25167dwarf2_mark_helper (void **slot, void *data)
25168{
7188ed02
SM
25169 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25170 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25171 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25172
25173 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25174 reading of the chain. As such dependencies remain valid it is not much
25175 useful to track and undo them during QUIT cleanups. */
7188ed02 25176 if (cu == nullptr)
d07ed419
JK
25177 return 1;
25178
7188ed02 25179 if (cu->mark)
10b3939b 25180 return 1;
10b3939b 25181
7188ed02
SM
25182 cu->mark = true;
25183
25184 if (cu->dependencies != nullptr)
25185 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25186
25187 return 1;
25188}
25189
f504f079
DE
25190/* Set the mark field in CU and in every other compilation unit in the
25191 cache that we must keep because we are keeping CU. */
25192
ae038cb0
DJ
25193static void
25194dwarf2_mark (struct dwarf2_cu *cu)
25195{
25196 if (cu->mark)
25197 return;
7188ed02 25198
9068261f 25199 cu->mark = true;
ae038cb0 25200
7188ed02
SM
25201 if (cu->dependencies != nullptr)
25202 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25203}
25204
72bf9492
DJ
25205/* Trivial hash function for partial_die_info: the hash value of a DIE
25206 is its offset in .debug_info for this objfile. */
25207
25208static hashval_t
25209partial_die_hash (const void *item)
25210{
9a3c8263
SM
25211 const struct partial_die_info *part_die
25212 = (const struct partial_die_info *) item;
9a619af0 25213
9c541725 25214 return to_underlying (part_die->sect_off);
72bf9492
DJ
25215}
25216
25217/* Trivial comparison function for partial_die_info structures: two DIEs
25218 are equal if they have the same offset. */
25219
25220static int
25221partial_die_eq (const void *item_lhs, const void *item_rhs)
25222{
9a3c8263
SM
25223 const struct partial_die_info *part_die_lhs
25224 = (const struct partial_die_info *) item_lhs;
25225 const struct partial_die_info *part_die_rhs
25226 = (const struct partial_die_info *) item_rhs;
9a619af0 25227
9c541725 25228 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25229}
25230
3c3bb058
AB
25231struct cmd_list_element *set_dwarf_cmdlist;
25232struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25233
9291a0cd 25234static void
cd4fb1b2
SM
25235show_check_physname (struct ui_file *file, int from_tty,
25236 struct cmd_list_element *c, const char *value)
9291a0cd 25237{
cd4fb1b2
SM
25238 fprintf_filtered (file,
25239 _("Whether to check \"physname\" is %s.\n"),
25240 value);
9291a0cd
TT
25241}
25242
6c265988 25243void _initialize_dwarf2_read ();
cd4fb1b2 25244void
6c265988 25245_initialize_dwarf2_read ()
9291a0cd 25246{
0743fc83 25247 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25248Set DWARF specific variables.\n\
590042fc 25249Configure DWARF variables such as the cache size."),
0743fc83
TT
25250 &set_dwarf_cmdlist, "maintenance set dwarf ",
25251 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25252
0743fc83 25253 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25254Show DWARF specific variables.\n\
25255Show DWARF variables such as the cache size."),
0743fc83
TT
25256 &show_dwarf_cmdlist, "maintenance show dwarf ",
25257 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25258
cd4fb1b2
SM
25259 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25260 &dwarf_max_cache_age, _("\
25261Set the upper bound on the age of cached DWARF compilation units."), _("\
25262Show the upper bound on the age of cached DWARF compilation units."), _("\
25263A higher limit means that cached compilation units will be stored\n\
25264in memory longer, and more total memory will be used. Zero disables\n\
25265caching, which can slow down startup."),
25266 NULL,
25267 show_dwarf_max_cache_age,
25268 &set_dwarf_cmdlist,
25269 &show_dwarf_cmdlist);
156942c7 25270
cd4fb1b2
SM
25271 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25272Set debugging of the DWARF reader."), _("\
25273Show debugging of the DWARF reader."), _("\
25274When enabled (non-zero), debugging messages are printed during DWARF\n\
25275reading and symtab expansion. A value of 1 (one) provides basic\n\
25276information. A value greater than 1 provides more verbose information."),
25277 NULL,
25278 NULL,
25279 &setdebuglist, &showdebuglist);
9291a0cd 25280
cd4fb1b2
SM
25281 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25282Set debugging of the DWARF DIE reader."), _("\
25283Show debugging of the DWARF DIE reader."), _("\
25284When enabled (non-zero), DIEs are dumped after they are read in.\n\
25285The value is the maximum depth to print."),
25286 NULL,
25287 NULL,
25288 &setdebuglist, &showdebuglist);
9291a0cd 25289
cd4fb1b2
SM
25290 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25291Set debugging of the dwarf line reader."), _("\
25292Show debugging of the dwarf line reader."), _("\
25293When enabled (non-zero), line number entries are dumped as they are read in.\n\
25294A value of 1 (one) provides basic information.\n\
25295A value greater than 1 provides more verbose information."),
25296 NULL,
25297 NULL,
25298 &setdebuglist, &showdebuglist);
437afbb8 25299
cd4fb1b2
SM
25300 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25301Set cross-checking of \"physname\" code against demangler."), _("\
25302Show cross-checking of \"physname\" code against demangler."), _("\
25303When enabled, GDB's internal \"physname\" code is checked against\n\
25304the demangler."),
25305 NULL, show_check_physname,
25306 &setdebuglist, &showdebuglist);
900e11f9 25307
e615022a
DE
25308 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25309 no_class, &use_deprecated_index_sections, _("\
25310Set whether to use deprecated gdb_index sections."), _("\
25311Show whether to use deprecated gdb_index sections."), _("\
25312When enabled, deprecated .gdb_index sections are used anyway.\n\
25313Normally they are ignored either because of a missing feature or\n\
25314performance issue.\n\
25315Warning: This option must be enabled before gdb reads the file."),
25316 NULL,
25317 NULL,
25318 &setlist, &showlist);
25319
f1e6e072
TT
25320 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25321 &dwarf2_locexpr_funcs);
25322 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25323 &dwarf2_loclist_funcs);
25324
25325 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25326 &dwarf2_block_frame_base_locexpr_funcs);
25327 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25328 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25329
25330#if GDB_SELF_TEST
25331 selftests::register_test ("dw2_expand_symtabs_matching",
25332 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25333 selftests::register_test ("dwarf2_find_containing_comp_unit",
25334 selftests::find_containing_comp_unit::run_test);
c62446b1 25335#endif
6502dd73 25336}
This page took 5.594693 seconds and 4 git commands to generate.