gdb/testsuite: add .debug_loclists tests
[deliverable/binutils-gdb.git] / gdb / dwarf2 / read.c
CommitLineData
c906108c 1/* DWARF 2 debugging format support for GDB.
917c78fc 2
3666a048 3 Copyright (C) 1994-2021 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, ...) \
74b773fc
SM
99 debug_prefixed_printf_cond (dwarf_read_debug >= 1, "dwarf-read", fmt, \
100 ##__VA_ARGS__)
6f738b01
SM
101
102/* Print a "dwarf-read" debug statement if dwarf_read_debug is >= 2. */
103
104#define dwarf_read_debug_printf_v(fmt, ...) \
74b773fc
SM
105 debug_prefixed_printf_cond (dwarf_read_debug >= 2, "dwarf-read", fmt, \
106 ##__VA_ARGS__)
6f738b01 107
d97bc12b 108/* When non-zero, dump DIEs after they are read in. */
b4f54984 109static unsigned int dwarf_die_debug = 0;
d97bc12b 110
27e0867f 111/* When non-zero, dump line number entries as they are read in. */
8fdd972c 112unsigned int dwarf_line_debug = 0;
27e0867f 113
491144b5
CB
114/* When true, cross-check physname against demangler. */
115static bool check_physname = false;
900e11f9 116
491144b5
CB
117/* When true, do not reject deprecated .gdb_index sections. */
118static bool use_deprecated_index_sections = false;
481860b3 119
17ee85fc
TT
120/* This is used to store the data that is always per objfile. */
121static const objfile_key<dwarf2_per_objfile> dwarf2_objfile_data_key;
122
123/* These are used to store the dwarf2_per_bfd objects.
124
125 objfiles having the same BFD, which doesn't require relocations, are going to
126 share a dwarf2_per_bfd object, which is held in the _bfd_data_key version.
127
128 Other objfiles are not going to share a dwarf2_per_bfd with any other
129 objfiles, so they'll have their own version kept in the _objfile_data_key
130 version. */
131static const struct bfd_key<dwarf2_per_bfd> dwarf2_per_bfd_bfd_data_key;
132static const struct objfile_key<dwarf2_per_bfd> dwarf2_per_bfd_objfile_data_key;
6502dd73 133
f1e6e072
TT
134/* The "aclass" indices for various kinds of computed DWARF symbols. */
135
136static int dwarf2_locexpr_index;
137static int dwarf2_loclist_index;
138static int dwarf2_locexpr_block_index;
139static int dwarf2_loclist_block_index;
140
41144253 141/* Size of .debug_loclists section header for 32-bit DWARF format. */
142#define LOCLIST_HEADER_SIZE32 12
143
144/* Size of .debug_loclists section header for 64-bit DWARF format. */
145#define LOCLIST_HEADER_SIZE64 20
146
d0ce17d8
CT
147/* Size of .debug_rnglists section header for 32-bit DWARF format. */
148#define RNGLIST_HEADER_SIZE32 12
149
150/* Size of .debug_rnglists section header for 64-bit DWARF format. */
151#define RNGLIST_HEADER_SIZE64 20
152
3f563c84
PA
153/* An index into a (C++) symbol name component in a symbol name as
154 recorded in the mapped_index's symbol table. For each C++ symbol
155 in the symbol table, we record one entry for the start of each
156 component in the symbol in a table of name components, and then
157 sort the table, in order to be able to binary search symbol names,
158 ignoring leading namespaces, both completion and regular look up.
159 For example, for symbol "A::B::C", we'll have an entry that points
160 to "A::B::C", another that points to "B::C", and another for "C".
161 Note that function symbols in GDB index have no parameter
162 information, just the function/method names. You can convert a
163 name_component to a "const char *" using the
164 'mapped_index::symbol_name_at(offset_type)' method. */
165
166struct name_component
167{
168 /* Offset in the symbol name where the component starts. Stored as
169 a (32-bit) offset instead of a pointer to save memory and improve
170 locality on 64-bit architectures. */
171 offset_type name_offset;
172
173 /* The symbol's index in the symbol and constant pool tables of a
174 mapped_index. */
175 offset_type idx;
176};
177
44ed8f3e
PA
178/* Base class containing bits shared by both .gdb_index and
179 .debug_name indexes. */
180
181struct mapped_index_base
182{
22ca247e
TT
183 mapped_index_base () = default;
184 DISABLE_COPY_AND_ASSIGN (mapped_index_base);
185
44ed8f3e
PA
186 /* The name_component table (a sorted vector). See name_component's
187 description above. */
188 std::vector<name_component> name_components;
189
190 /* How NAME_COMPONENTS is sorted. */
191 enum case_sensitivity name_components_casing;
192
193 /* Return the number of names in the symbol table. */
194 virtual size_t symbol_name_count () const = 0;
195
196 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
197 virtual const char *symbol_name_at
198 (offset_type idx, dwarf2_per_objfile *per_objfile) const = 0;
44ed8f3e
PA
199
200 /* Return whether the name at IDX in the symbol table should be
201 ignored. */
202 virtual bool symbol_name_slot_invalid (offset_type idx) const
203 {
204 return false;
205 }
206
207 /* Build the symbol name component sorted vector, if we haven't
208 yet. */
fcf23d5b 209 void build_name_components (dwarf2_per_objfile *per_objfile);
44ed8f3e
PA
210
211 /* Returns the lower (inclusive) and upper (exclusive) bounds of the
212 possible matches for LN_NO_PARAMS in the name component
213 vector. */
214 std::pair<std::vector<name_component>::const_iterator,
215 std::vector<name_component>::const_iterator>
3b00ef10 216 find_name_components_bounds (const lookup_name_info &ln_no_params,
fcf23d5b
SM
217 enum language lang,
218 dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
219
220 /* Prevent deleting/destroying via a base class pointer. */
221protected:
222 ~mapped_index_base() = default;
223};
224
9291a0cd
TT
225/* A description of the mapped index. The file format is described in
226 a comment by the code that writes the index. */
fc898b42 227struct mapped_index final : public mapped_index_base
9291a0cd 228{
f00a2de2
PA
229 /* A slot/bucket in the symbol table hash. */
230 struct symbol_table_slot
231 {
232 const offset_type name;
233 const offset_type vec;
234 };
235
559a7a62 236 /* Index data format version. */
3063847f 237 int version = 0;
559a7a62 238
f00a2de2
PA
239 /* The address table data. */
240 gdb::array_view<const gdb_byte> address_table;
b11b1f88 241
3876f04e 242 /* The symbol table, implemented as a hash table. */
f00a2de2 243 gdb::array_view<symbol_table_slot> symbol_table;
b11b1f88 244
9291a0cd 245 /* A pointer to the constant pool. */
3063847f 246 const char *constant_pool = nullptr;
3f563c84 247
44ed8f3e
PA
248 bool symbol_name_slot_invalid (offset_type idx) const override
249 {
250 const auto &bucket = this->symbol_table[idx];
9ab08412 251 return bucket.name == 0 && bucket.vec == 0;
44ed8f3e 252 }
5c58de74 253
3f563c84
PA
254 /* Convenience method to get at the name of the symbol at IDX in the
255 symbol table. */
fcf23d5b
SM
256 const char *symbol_name_at
257 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
f00a2de2 258 { return this->constant_pool + MAYBE_SWAP (this->symbol_table[idx].name); }
5c58de74 259
44ed8f3e
PA
260 size_t symbol_name_count () const override
261 { return this->symbol_table.size (); }
9291a0cd
TT
262};
263
927aa2e7
JK
264/* A description of the mapped .debug_names.
265 Uninitialized map has CU_COUNT 0. */
fc898b42 266struct mapped_debug_names final : public mapped_index_base
927aa2e7
JK
267{
268 bfd_endian dwarf5_byte_order;
269 bool dwarf5_is_dwarf64;
270 bool augmentation_is_gdb;
271 uint8_t offset_size;
272 uint32_t cu_count = 0;
273 uint32_t tu_count, bucket_count, name_count;
274 const gdb_byte *cu_table_reordered, *tu_table_reordered;
275 const uint32_t *bucket_table_reordered, *hash_table_reordered;
276 const gdb_byte *name_table_string_offs_reordered;
277 const gdb_byte *name_table_entry_offs_reordered;
278 const gdb_byte *entry_pool;
279
280 struct index_val
281 {
282 ULONGEST dwarf_tag;
283 struct attr
284 {
285 /* Attribute name DW_IDX_*. */
286 ULONGEST dw_idx;
287
288 /* Attribute form DW_FORM_*. */
289 ULONGEST form;
290
291 /* Value if FORM is DW_FORM_implicit_const. */
292 LONGEST implicit_const;
293 };
294 std::vector<attr> attr_vec;
295 };
296
297 std::unordered_map<ULONGEST, index_val> abbrev_map;
298
fcf23d5b
SM
299 const char *namei_to_name
300 (uint32_t namei, dwarf2_per_objfile *per_objfile) const;
44ed8f3e
PA
301
302 /* Implementation of the mapped_index_base virtual interface, for
303 the name_components cache. */
304
fcf23d5b
SM
305 const char *symbol_name_at
306 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
307 { return namei_to_name (idx, per_objfile); }
44ed8f3e
PA
308
309 size_t symbol_name_count () const override
310 { return this->name_count; }
927aa2e7
JK
311};
312
cd4fb1b2 313/* See dwarf2read.h. */
ed2dc618 314
cd4fb1b2 315dwarf2_per_objfile *
ed2dc618
SM
316get_dwarf2_per_objfile (struct objfile *objfile)
317{
5bfd760d 318 return dwarf2_objfile_data_key.get (objfile);
ed2dc618 319}
c906108c 320
251d32d9 321/* Default names of the debugging sections. */
c906108c 322
233a11ab
CS
323/* Note that if the debugging section has been compressed, it might
324 have a name like .zdebug_info. */
325
9cdd5dbd
DE
326static const struct dwarf2_debug_sections dwarf2_elf_names =
327{
251d32d9
TG
328 { ".debug_info", ".zdebug_info" },
329 { ".debug_abbrev", ".zdebug_abbrev" },
330 { ".debug_line", ".zdebug_line" },
331 { ".debug_loc", ".zdebug_loc" },
43988095 332 { ".debug_loclists", ".zdebug_loclists" },
251d32d9 333 { ".debug_macinfo", ".zdebug_macinfo" },
cf2c3c16 334 { ".debug_macro", ".zdebug_macro" },
251d32d9 335 { ".debug_str", ".zdebug_str" },
18a8505e 336 { ".debug_str_offsets", ".zdebug_str_offsets" },
43988095 337 { ".debug_line_str", ".zdebug_line_str" },
251d32d9 338 { ".debug_ranges", ".zdebug_ranges" },
43988095 339 { ".debug_rnglists", ".zdebug_rnglists" },
251d32d9 340 { ".debug_types", ".zdebug_types" },
3019eac3 341 { ".debug_addr", ".zdebug_addr" },
251d32d9
TG
342 { ".debug_frame", ".zdebug_frame" },
343 { ".eh_frame", NULL },
24d3216f 344 { ".gdb_index", ".zgdb_index" },
927aa2e7
JK
345 { ".debug_names", ".zdebug_names" },
346 { ".debug_aranges", ".zdebug_aranges" },
24d3216f 347 23
251d32d9 348};
c906108c 349
80626a55 350/* List of DWO/DWP sections. */
3019eac3 351
80626a55 352static const struct dwop_section_names
3019eac3
DE
353{
354 struct dwarf2_section_names abbrev_dwo;
355 struct dwarf2_section_names info_dwo;
356 struct dwarf2_section_names line_dwo;
357 struct dwarf2_section_names loc_dwo;
43988095 358 struct dwarf2_section_names loclists_dwo;
09262596
DE
359 struct dwarf2_section_names macinfo_dwo;
360 struct dwarf2_section_names macro_dwo;
d0ce17d8 361 struct dwarf2_section_names rnglists_dwo;
3019eac3
DE
362 struct dwarf2_section_names str_dwo;
363 struct dwarf2_section_names str_offsets_dwo;
364 struct dwarf2_section_names types_dwo;
80626a55
DE
365 struct dwarf2_section_names cu_index;
366 struct dwarf2_section_names tu_index;
3019eac3 367}
80626a55 368dwop_section_names =
3019eac3
DE
369{
370 { ".debug_abbrev.dwo", ".zdebug_abbrev.dwo" },
371 { ".debug_info.dwo", ".zdebug_info.dwo" },
372 { ".debug_line.dwo", ".zdebug_line.dwo" },
373 { ".debug_loc.dwo", ".zdebug_loc.dwo" },
43988095 374 { ".debug_loclists.dwo", ".zdebug_loclists.dwo" },
09262596
DE
375 { ".debug_macinfo.dwo", ".zdebug_macinfo.dwo" },
376 { ".debug_macro.dwo", ".zdebug_macro.dwo" },
d0ce17d8 377 { ".debug_rnglists.dwo", ".zdebug_rnglists.dwo" },
3019eac3
DE
378 { ".debug_str.dwo", ".zdebug_str.dwo" },
379 { ".debug_str_offsets.dwo", ".zdebug_str_offsets.dwo" },
380 { ".debug_types.dwo", ".zdebug_types.dwo" },
80626a55
DE
381 { ".debug_cu_index", ".zdebug_cu_index" },
382 { ".debug_tu_index", ".zdebug_tu_index" },
3019eac3
DE
383};
384
c906108c
SS
385/* local data types */
386
d0ce17d8
CT
387/* The location list and range list sections (.debug_loclists & .debug_rnglists)
388 begin with a header, which contains the following information. */
389struct loclists_rnglists_header
41144253 390{
391 /* A 4-byte or 12-byte length containing the length of the
392 set of entries for this compilation unit, not including the
393 length field itself. */
394 unsigned int length;
395
396 /* A 2-byte version identifier. */
397 short version;
398
399 /* A 1-byte unsigned integer containing the size in bytes of an address on
400 the target system. */
401 unsigned char addr_size;
402
403 /* A 1-byte unsigned integer containing the size in bytes of a segment selector
404 on the target system. */
405 unsigned char segment_collector_size;
406
407 /* A 4-byte count of the number of offsets that follow the header. */
408 unsigned int offset_entry_count;
409};
410
3da10d80
KS
411/* Type used for delaying computation of method physnames.
412 See comments for compute_delayed_physnames. */
413struct delayed_method_info
414{
415 /* The type to which the method is attached, i.e., its parent class. */
416 struct type *type;
417
418 /* The index of the method in the type's function fieldlists. */
419 int fnfield_index;
420
421 /* The index of the method in the fieldlist. */
422 int index;
423
424 /* The name of the DIE. */
425 const char *name;
426
427 /* The DIE associated with this method. */
428 struct die_info *die;
429};
430
e7c27a73
DJ
431/* Internal state when decoding a particular compilation unit. */
432struct dwarf2_cu
433{
9e021579
SM
434 explicit dwarf2_cu (dwarf2_per_cu_data *per_cu,
435 dwarf2_per_objfile *per_objfile);
fcd3b13d
SM
436
437 DISABLE_COPY_AND_ASSIGN (dwarf2_cu);
438
c24bdb02
KS
439 /* TU version of handle_DW_AT_stmt_list for read_type_unit_scope.
440 Create the set of symtabs used by this TU, or if this TU is sharing
441 symtabs with another TU and the symtabs have already been created
442 then restore those symtabs in the line header.
443 We don't need the pc/line-number mapping for type units. */
444 void setup_type_unit_groups (struct die_info *die);
445
446 /* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
447 buildsym_compunit constructor. */
448 struct compunit_symtab *start_symtab (const char *name,
449 const char *comp_dir,
450 CORE_ADDR low_pc);
451
452 /* Reset the builder. */
453 void reset_builder () { m_builder.reset (); }
454
293e7e51
SM
455 /* Return a type that is a generic pointer type, the size of which
456 matches the address size given in the compilation unit header for
457 this CU. */
458 struct type *addr_type () const;
459
460 /* Find an integer type the same size as the address size given in
461 the compilation unit header for this CU. UNSIGNED_P controls if
462 the integer is unsigned or not. */
463 struct type *addr_sized_int_type (bool unsigned_p) const;
464
d00adf39 465 /* The header of the compilation unit. */
fcd3b13d 466 struct comp_unit_head header {};
e142c38c 467
d00adf39 468 /* Base address of this compilation unit. */
2b24b6e4 469 gdb::optional<CORE_ADDR> base_address;
d00adf39 470
e142c38c 471 /* The language we are debugging. */
fcd3b13d
SM
472 enum language language = language_unknown;
473 const struct language_defn *language_defn = nullptr;
e142c38c 474
fcd3b13d 475 const char *producer = nullptr;
b0f35d58 476
c24bdb02 477private:
804d2729
TT
478 /* The symtab builder for this CU. This is only non-NULL when full
479 symbols are being read. */
c24bdb02 480 std::unique_ptr<buildsym_compunit> m_builder;
804d2729 481
c24bdb02 482public:
e142c38c
DJ
483 /* The generic symbol table building routines have separate lists for
484 file scope symbols and all all other scopes (local scopes). So
485 we need to select the right one to pass to add_symbol_to_list().
486 We do it by keeping a pointer to the correct list in list_in_scope.
487
488 FIXME: The original dwarf code just treated the file scope as the
489 first local scope, and all other local scopes as nested local
490 scopes, and worked fine. Check to see if we really need to
491 distinguish these in buildsym.c. */
fcd3b13d 492 struct pending **list_in_scope = nullptr;
e142c38c 493
b64f50a1
JK
494 /* Hash table holding all the loaded partial DIEs
495 with partial_die->offset.SECT_OFF as hash. */
fcd3b13d 496 htab_t partial_dies = nullptr;
72bf9492
DJ
497
498 /* Storage for things with the same lifetime as this read-in compilation
499 unit, including partial DIEs. */
fcd3b13d 500 auto_obstack comp_unit_obstack;
72bf9492 501
69d751e3 502 /* Backlink to our per_cu entry. */
ae038cb0
DJ
503 struct dwarf2_per_cu_data *per_cu;
504
9e021579 505 /* The dwarf2_per_objfile that owns this. */
976ca316 506 dwarf2_per_objfile *per_objfile;
9e021579 507
ae038cb0 508 /* How many compilation units ago was this CU last referenced? */
fcd3b13d 509 int last_used = 0;
ae038cb0 510
b64f50a1
JK
511 /* A hash table of DIE cu_offset for following references with
512 die_info->offset.sect_off as hash. */
fcd3b13d 513 htab_t die_hash = nullptr;
10b3939b
DJ
514
515 /* Full DIEs if read in. */
fcd3b13d 516 struct die_info *dies = nullptr;
10b3939b
DJ
517
518 /* A set of pointers to dwarf2_per_cu_data objects for compilation
519 units referenced by this one. Only set during full symbol processing;
520 partial symbol tables do not have dependencies. */
fcd3b13d 521 htab_t dependencies = nullptr;
10b3939b 522
cb1df416 523 /* Header data from the line table, during full symbol processing. */
fcd3b13d 524 struct line_header *line_header = nullptr;
4c8aa72d 525 /* Non-NULL if LINE_HEADER is owned by this DWARF_CU. Otherwise,
5989a64e 526 it's owned by dwarf2_per_bfd::line_header_hash. If non-NULL,
4c8aa72d
PA
527 this is the DW_TAG_compile_unit die for this CU. We'll hold on
528 to the line header as long as this DIE is being processed. See
529 process_die_scope. */
fcd3b13d 530 die_info *line_header_die_owner = nullptr;
cb1df416 531
3da10d80
KS
532 /* A list of methods which need to have physnames computed
533 after all type information has been read. */
c89b44cd 534 std::vector<delayed_method_info> method_list;
3da10d80 535
96408a79 536 /* To be copied to symtab->call_site_htab. */
fcd3b13d 537 htab_t call_site_htab = nullptr;
96408a79 538
034e5797
DE
539 /* Non-NULL if this CU came from a DWO file.
540 There is an invariant here that is important to remember:
541 Except for attributes copied from the top level DIE in the "main"
542 (or "stub") file in preparation for reading the DWO file
18a8505e 543 (e.g., DW_AT_addr_base), we KISS: there is only *one* CU.
034e5797
DE
544 Either there isn't a DWO file (in which case this is NULL and the point
545 is moot), or there is and either we're not going to read it (in which
546 case this is NULL) or there is and we are reading it (in which case this
547 is non-NULL). */
fcd3b13d 548 struct dwo_unit *dwo_unit = nullptr;
3019eac3 549
18a8505e 550 /* The DW_AT_addr_base (DW_AT_GNU_addr_base) attribute if present.
1dbab08b 551 Note this value comes from the Fission stub CU/TU's DIE. */
18a8505e 552 gdb::optional<ULONGEST> addr_base;
3019eac3 553
18a8505e 554 /* The DW_AT_rnglists_base attribute if present.
1dbab08b 555 Note this value comes from the Fission stub CU/TU's DIE.
2e3cf129 556 Also note that the value is zero in the non-DWO case so this value can
ab435259
DE
557 be used without needing to know whether DWO files are in use or not.
558 N.B. This does not apply to DW_AT_ranges appearing in
559 DW_TAG_compile_unit dies. This is a bit of a wart, consider if ever
560 DW_AT_ranges appeared in the DW_TAG_compile_unit of DWO DIEs: then
18a8505e 561 DW_AT_rnglists_base *would* have to be applied, and we'd have to care
ab435259 562 whether the DW_AT_ranges attribute came from the skeleton or DWO. */
fcd3b13d 563 ULONGEST ranges_base = 0;
2e3cf129 564
41144253 565 /* The DW_AT_loclists_base attribute if present. */
566 ULONGEST loclist_base = 0;
567
c9317f21
TT
568 /* When reading debug info generated by older versions of rustc, we
569 have to rewrite some union types to be struct types with a
570 variant part. This rewriting must be done after the CU is fully
571 read in, because otherwise at the point of rewriting some struct
572 type might not have been fully processed. So, we keep a list of
573 all such types here and process them after expansion. */
574 std::vector<struct type *> rust_unions;
575
18a8505e
AT
576 /* The DW_AT_str_offsets_base attribute if present. For DWARF 4 version DWO
577 files, the value is implicitly zero. For DWARF 5 version DWO files, the
578 value is often implicit and is the size of the header of
579 .debug_str_offsets section (8 or 4, depending on the address size). */
580 gdb::optional<ULONGEST> str_offsets_base;
581
ae038cb0 582 /* Mark used when releasing cached dies. */
9068261f 583 bool mark : 1;
ae038cb0 584
8be455d7
JK
585 /* This CU references .debug_loc. See the symtab->locations_valid field.
586 This test is imperfect as there may exist optimized debug code not using
587 any location list and still facing inlining issues if handled as
588 unoptimized code. For a future better test see GCC PR other/32998. */
9068261f 589 bool has_loclist : 1;
ba919b58 590
9068261f 591 /* These cache the results for producer_is_* fields. CHECKED_PRODUCER is true
1b80a9fa
JK
592 if all the producer_is_* fields are valid. This information is cached
593 because profiling CU expansion showed excessive time spent in
594 producer_is_gxx_lt_4_6. */
9068261f
AB
595 bool checked_producer : 1;
596 bool producer_is_gxx_lt_4_6 : 1;
597 bool producer_is_gcc_lt_4_3 : 1;
eb77c9df 598 bool producer_is_icc : 1;
9068261f 599 bool producer_is_icc_lt_14 : 1;
c258c396 600 bool producer_is_codewarrior : 1;
4d4ec4e5 601
9068261f 602 /* When true, the file that we're processing is known to have
4d4ec4e5
TT
603 debugging info for C++ namespaces. GCC 3.3.x did not produce
604 this information, but later versions do. */
605
9068261f 606 bool processing_has_namespace_info : 1;
d590ff25
YQ
607
608 struct partial_die_info *find_partial_die (sect_offset sect_off);
c24bdb02
KS
609
610 /* If this CU was inherited by another CU (via specification,
611 abstract_origin, etc), this is the ancestor CU. */
612 dwarf2_cu *ancestor;
613
614 /* Get the buildsym_compunit for this CU. */
615 buildsym_compunit *get_builder ()
616 {
617 /* If this CU has a builder associated with it, use that. */
618 if (m_builder != nullptr)
619 return m_builder.get ();
620
621 /* Otherwise, search ancestors for a valid builder. */
622 if (ancestor != nullptr)
623 return ancestor->get_builder ();
624
625 return nullptr;
626 }
e7c27a73
DJ
627};
628
094b34ac
DE
629/* A struct that can be used as a hash key for tables based on DW_AT_stmt_list.
630 This includes type_unit_group and quick_file_names. */
631
632struct stmt_list_hash
633{
634 /* The DWO unit this table is from or NULL if there is none. */
635 struct dwo_unit *dwo_unit;
636
637 /* Offset in .debug_line or .debug_line.dwo. */
9c541725 638 sect_offset line_sect_off;
094b34ac
DE
639};
640
5989a64e 641/* Each element of dwarf2_per_bfd->type_unit_groups is a pointer to
8adb8487
TT
642 an object of this type. This contains elements of type unit groups
643 that can be shared across objfiles. The non-shareable parts are in
644 type_unit_group_unshareable. */
f4dc4d17
DE
645
646struct type_unit_group
647{
0186c6a7 648 /* dwarf2read.c's main "handle" on a TU symtab.
f4dc4d17
DE
649 To simplify things we create an artificial CU that "includes" all the
650 type units using this stmt_list so that the rest of the code still has
197400e8 651 a "per_cu" handle on the symtab. */
094b34ac
DE
652 struct dwarf2_per_cu_data per_cu;
653
0186c6a7
DE
654 /* The TUs that share this DW_AT_stmt_list entry.
655 This is added to while parsing type units to build partial symtabs,
656 and is deleted afterwards and not used again. */
a8b3b8e9 657 std::vector<signatured_type *> *tus;
f4dc4d17 658
094b34ac
DE
659 /* The data used to construct the hash key. */
660 struct stmt_list_hash hash;
f4dc4d17
DE
661};
662
73869dc2 663/* These sections are what may appear in a (real or virtual) DWO file. */
3019eac3
DE
664
665struct dwo_sections
666{
667 struct dwarf2_section_info abbrev;
3019eac3
DE
668 struct dwarf2_section_info line;
669 struct dwarf2_section_info loc;
43988095 670 struct dwarf2_section_info loclists;
09262596
DE
671 struct dwarf2_section_info macinfo;
672 struct dwarf2_section_info macro;
d0ce17d8 673 struct dwarf2_section_info rnglists;
3019eac3
DE
674 struct dwarf2_section_info str;
675 struct dwarf2_section_info str_offsets;
80626a55
DE
676 /* In the case of a virtual DWO file, these two are unused. */
677 struct dwarf2_section_info info;
fd5866f6 678 std::vector<dwarf2_section_info> types;
3019eac3
DE
679};
680
c88ee1f0 681/* CUs/TUs in DWP/DWO files. */
3019eac3
DE
682
683struct dwo_unit
684{
685 /* Backlink to the containing struct dwo_file. */
686 struct dwo_file *dwo_file;
687
688 /* The "id" that distinguishes this CU/TU.
689 .debug_info calls this "dwo_id", .debug_types calls this "signature".
690 Since signatures came first, we stick with it for consistency. */
691 ULONGEST signature;
692
693 /* The section this CU/TU lives in, in the DWO file. */
8a0459fd 694 struct dwarf2_section_info *section;
3019eac3 695
9c541725
PA
696 /* Same as dwarf2_per_cu_data:{sect_off,length} but in the DWO section. */
697 sect_offset sect_off;
3019eac3
DE
698 unsigned int length;
699
700 /* For types, offset in the type's DIE of the type defined by this TU. */
701 cu_offset type_offset_in_tu;
702};
703
73869dc2
DE
704/* include/dwarf2.h defines the DWP section codes.
705 It defines a max value but it doesn't define a min value, which we
706 use for error checking, so provide one. */
707
708enum dwp_v2_section_ids
709{
710 DW_SECT_MIN = 1
711};
712
80626a55 713/* Data for one DWO file.
57d63ce2
DE
714
715 This includes virtual DWO files (a virtual DWO file is a DWO file as it
716 appears in a DWP file). DWP files don't really have DWO files per se -
717 comdat folding of types "loses" the DWO file they came from, and from
718 a high level view DWP files appear to contain a mass of random types.
719 However, to maintain consistency with the non-DWP case we pretend DWP
720 files contain virtual DWO files, and we assign each TU with one virtual
721 DWO file (generally based on the line and abbrev section offsets -
722 a heuristic that seems to work in practice). */
3019eac3
DE
723
724struct dwo_file
725{
51ac9db5
SM
726 dwo_file () = default;
727 DISABLE_COPY_AND_ASSIGN (dwo_file);
728
18a8505e 729 /* The DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute.
80626a55
DE
730 For virtual DWO files the name is constructed from the section offsets
731 of abbrev,line,loc,str_offsets so that we combine virtual DWO files
732 from related CU+TUs. */
51ac9db5 733 const char *dwo_name = nullptr;
0ac5b59e
DE
734
735 /* The DW_AT_comp_dir attribute. */
51ac9db5 736 const char *comp_dir = nullptr;
3019eac3 737
80626a55
DE
738 /* The bfd, when the file is open. Otherwise this is NULL.
739 This is unused(NULL) for virtual DWO files where we use dwp_file.dbfd. */
fb1eb2f9 740 gdb_bfd_ref_ptr dbfd;
3019eac3 741
73869dc2 742 /* The sections that make up this DWO file.
d2854d8d 743 Remember that for virtual DWO files in DWP V2 or DWP V5, these are virtual
73869dc2 744 sections (for lack of a better name). */
51ac9db5 745 struct dwo_sections sections {};
3019eac3 746
33c5cd75
DB
747 /* The CUs in the file.
748 Each element is a struct dwo_unit. Multiple CUs per DWO are supported as
749 an extension to handle LLVM's Link Time Optimization output (where
750 multiple source files may be compiled into a single object/dwo pair). */
b0b6a987 751 htab_up cus;
3019eac3
DE
752
753 /* Table of TUs in the file.
754 Each element is a struct dwo_unit. */
b0b6a987 755 htab_up tus;
3019eac3
DE
756};
757
80626a55
DE
758/* These sections are what may appear in a DWP file. */
759
760struct dwp_sections
761{
d2854d8d 762 /* These are used by all DWP versions (1, 2 and 5). */
80626a55
DE
763 struct dwarf2_section_info str;
764 struct dwarf2_section_info cu_index;
765 struct dwarf2_section_info tu_index;
73869dc2 766
d2854d8d 767 /* These are only used by DWP version 2 and version 5 files.
73869dc2
DE
768 In DWP version 1 the .debug_info.dwo, .debug_types.dwo, and other
769 sections are referenced by section number, and are not recorded here.
d2854d8d
CT
770 In DWP version 2 or 5 there is at most one copy of all these sections,
771 each section being (effectively) comprised of the concatenation of all of
772 the individual sections that exist in the version 1 format.
73869dc2
DE
773 To keep the code simple we treat each of these concatenated pieces as a
774 section itself (a virtual section?). */
775 struct dwarf2_section_info abbrev;
776 struct dwarf2_section_info info;
777 struct dwarf2_section_info line;
778 struct dwarf2_section_info loc;
d2854d8d 779 struct dwarf2_section_info loclists;
73869dc2
DE
780 struct dwarf2_section_info macinfo;
781 struct dwarf2_section_info macro;
d2854d8d 782 struct dwarf2_section_info rnglists;
73869dc2
DE
783 struct dwarf2_section_info str_offsets;
784 struct dwarf2_section_info types;
80626a55
DE
785};
786
73869dc2
DE
787/* These sections are what may appear in a virtual DWO file in DWP version 1.
788 A virtual DWO file is a DWO file as it appears in a DWP file. */
80626a55 789
73869dc2 790struct virtual_v1_dwo_sections
80626a55
DE
791{
792 struct dwarf2_section_info abbrev;
793 struct dwarf2_section_info line;
794 struct dwarf2_section_info loc;
795 struct dwarf2_section_info macinfo;
796 struct dwarf2_section_info macro;
797 struct dwarf2_section_info str_offsets;
798 /* Each DWP hash table entry records one CU or one TU.
8a0459fd 799 That is recorded here, and copied to dwo_unit.section. */
80626a55
DE
800 struct dwarf2_section_info info_or_types;
801};
802
d2854d8d 803/* Similar to virtual_v1_dwo_sections, but for DWP version 2 or 5.
73869dc2
DE
804 In version 2, the sections of the DWO files are concatenated together
805 and stored in one section of that name. Thus each ELF section contains
806 several "virtual" sections. */
807
d2854d8d 808struct virtual_v2_or_v5_dwo_sections
73869dc2
DE
809{
810 bfd_size_type abbrev_offset;
811 bfd_size_type abbrev_size;
812
813 bfd_size_type line_offset;
814 bfd_size_type line_size;
815
816 bfd_size_type loc_offset;
817 bfd_size_type loc_size;
818
d2854d8d
CT
819 bfd_size_type loclists_offset;
820 bfd_size_type loclists_size;
821
73869dc2
DE
822 bfd_size_type macinfo_offset;
823 bfd_size_type macinfo_size;
824
825 bfd_size_type macro_offset;
826 bfd_size_type macro_size;
827
d2854d8d
CT
828 bfd_size_type rnglists_offset;
829 bfd_size_type rnglists_size;
830
73869dc2
DE
831 bfd_size_type str_offsets_offset;
832 bfd_size_type str_offsets_size;
833
834 /* Each DWP hash table entry records one CU or one TU.
835 That is recorded here, and copied to dwo_unit.section. */
836 bfd_size_type info_or_types_offset;
837 bfd_size_type info_or_types_size;
838};
839
80626a55
DE
840/* Contents of DWP hash tables. */
841
842struct dwp_hash_table
843{
73869dc2 844 uint32_t version, nr_columns;
80626a55 845 uint32_t nr_units, nr_slots;
73869dc2
DE
846 const gdb_byte *hash_table, *unit_table;
847 union
848 {
849 struct
850 {
851 const gdb_byte *indices;
852 } v1;
853 struct
854 {
855 /* This is indexed by column number and gives the id of the section
856 in that column. */
857#define MAX_NR_V2_DWO_SECTIONS \
858 (1 /* .debug_info or .debug_types */ \
859 + 1 /* .debug_abbrev */ \
860 + 1 /* .debug_line */ \
861 + 1 /* .debug_loc */ \
862 + 1 /* .debug_str_offsets */ \
863 + 1 /* .debug_macro or .debug_macinfo */)
864 int section_ids[MAX_NR_V2_DWO_SECTIONS];
865 const gdb_byte *offsets;
866 const gdb_byte *sizes;
867 } v2;
d2854d8d
CT
868 struct
869 {
870 /* This is indexed by column number and gives the id of the section
871 in that column. */
872#define MAX_NR_V5_DWO_SECTIONS \
873 (1 /* .debug_info */ \
874 + 1 /* .debug_abbrev */ \
875 + 1 /* .debug_line */ \
876 + 1 /* .debug_loclists */ \
877 + 1 /* .debug_str_offsets */ \
878 + 1 /* .debug_macro */ \
879 + 1 /* .debug_rnglists */)
880 int section_ids[MAX_NR_V5_DWO_SECTIONS];
881 const gdb_byte *offsets;
882 const gdb_byte *sizes;
883 } v5;
73869dc2 884 } section_pool;
80626a55
DE
885};
886
887/* Data for one DWP file. */
888
889struct dwp_file
890{
400174b1
TT
891 dwp_file (const char *name_, gdb_bfd_ref_ptr &&abfd)
892 : name (name_),
893 dbfd (std::move (abfd))
894 {
895 }
896
80626a55
DE
897 /* Name of the file. */
898 const char *name;
899
73869dc2 900 /* File format version. */
400174b1 901 int version = 0;
73869dc2 902
93417882 903 /* The bfd. */
400174b1 904 gdb_bfd_ref_ptr dbfd;
80626a55
DE
905
906 /* Section info for this file. */
400174b1 907 struct dwp_sections sections {};
80626a55 908
57d63ce2 909 /* Table of CUs in the file. */
400174b1 910 const struct dwp_hash_table *cus = nullptr;
80626a55
DE
911
912 /* Table of TUs in the file. */
400174b1 913 const struct dwp_hash_table *tus = nullptr;
80626a55 914
19ac8c2e 915 /* Tables of loaded CUs/TUs. Each entry is a struct dwo_unit *. */
48b490f2
TT
916 htab_up loaded_cus;
917 htab_up loaded_tus;
80626a55 918
73869dc2
DE
919 /* Table to map ELF section numbers to their sections.
920 This is only needed for the DWP V1 file format. */
400174b1
TT
921 unsigned int num_sections = 0;
922 asection **elf_sections = nullptr;
80626a55
DE
923};
924
0963b4bd
MS
925/* Struct used to pass misc. parameters to read_die_and_children, et
926 al. which are used for both .debug_info and .debug_types dies.
927 All parameters here are unchanging for the life of the call. This
dee91e82 928 struct exists to abstract away the constant parameters of die reading. */
93311388
DE
929
930struct die_reader_specs
931{
a32a8923 932 /* The bfd of die_section. */
93311388
DE
933 bfd* abfd;
934
935 /* The CU of the DIE we are parsing. */
936 struct dwarf2_cu *cu;
937
80626a55 938 /* Non-NULL if reading a DWO file (including one packaged into a DWP). */
3019eac3
DE
939 struct dwo_file *dwo_file;
940
dee91e82 941 /* The section the die comes from.
3019eac3 942 This is either .debug_info or .debug_types, or the .dwo variants. */
dee91e82
DE
943 struct dwarf2_section_info *die_section;
944
945 /* die_section->buffer. */
d521ce57 946 const gdb_byte *buffer;
f664829e
DE
947
948 /* The end of the buffer. */
949 const gdb_byte *buffer_end;
a2ce51a0 950
685af9cd
TT
951 /* The abbreviation table to use when reading the DIEs. */
952 struct abbrev_table *abbrev_table;
93311388
DE
953};
954
c0ab21c2
TT
955/* A subclass of die_reader_specs that holds storage and has complex
956 constructor and destructor behavior. */
957
958class cutu_reader : public die_reader_specs
959{
960public:
961
ab432490
SM
962 cutu_reader (dwarf2_per_cu_data *this_cu,
963 dwarf2_per_objfile *per_objfile,
c0ab21c2 964 struct abbrev_table *abbrev_table,
2e671100 965 dwarf2_cu *existing_cu,
c0ab21c2
TT
966 bool skip_partial);
967
968 explicit cutu_reader (struct dwarf2_per_cu_data *this_cu,
ab432490 969 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
970 struct dwarf2_cu *parent_cu = nullptr,
971 struct dwo_file *dwo_file = nullptr);
972
c0ab21c2
TT
973 DISABLE_COPY_AND_ASSIGN (cutu_reader);
974
975 const gdb_byte *info_ptr = nullptr;
976 struct die_info *comp_unit_die = nullptr;
c0ab21c2
TT
977 bool dummy_p = false;
978
6751ebae
TT
979 /* Release the new CU, putting it on the chain. This cannot be done
980 for dummy CUs. */
981 void keep ();
982
c0ab21c2 983private:
9e021579
SM
984 void init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
985 dwarf2_per_objfile *per_objfile,
2e671100 986 dwarf2_cu *existing_cu);
c0ab21c2
TT
987
988 struct dwarf2_per_cu_data *m_this_cu;
c0ab21c2
TT
989 std::unique_ptr<dwarf2_cu> m_new_cu;
990
991 /* The ordinary abbreviation table. */
992 abbrev_table_up m_abbrev_table_holder;
993
994 /* The DWO abbreviation table. */
995 abbrev_table_up m_dwo_abbrev_table;
996};
dee91e82 997
c906108c 998/* When we construct a partial symbol table entry we only
0963b4bd 999 need this much information. */
6f06d47b 1000struct partial_die_info : public allocate_on_obstack
c906108c 1001 {
6f06d47b
YQ
1002 partial_die_info (sect_offset sect_off, struct abbrev_info *abbrev);
1003
1004 /* Disable assign but still keep copy ctor, which is needed
1005 load_partial_dies. */
1006 partial_die_info& operator=(const partial_die_info& rhs) = delete;
1007
52356b79
YQ
1008 /* Adjust the partial die before generating a symbol for it. This
1009 function may set the is_external flag or change the DIE's
1010 name. */
1011 void fixup (struct dwarf2_cu *cu);
1012
48fbe735
YQ
1013 /* Read a minimal amount of information into the minimal die
1014 structure. */
1015 const gdb_byte *read (const struct die_reader_specs *reader,
1016 const struct abbrev_info &abbrev,
1017 const gdb_byte *info_ptr);
1018
7d00ffec
TT
1019 /* Compute the name of this partial DIE. This memoizes the
1020 result, so it is safe to call multiple times. */
1021 const char *name (dwarf2_cu *cu);
1022
72bf9492 1023 /* Offset of this DIE. */
6f06d47b 1024 const sect_offset sect_off;
72bf9492
DJ
1025
1026 /* DWARF-2 tag for this DIE. */
6f06d47b 1027 const ENUM_BITFIELD(dwarf_tag) tag : 16;
72bf9492 1028
72bf9492 1029 /* Assorted flags describing the data found in this DIE. */
6f06d47b
YQ
1030 const unsigned int has_children : 1;
1031
72bf9492
DJ
1032 unsigned int is_external : 1;
1033 unsigned int is_declaration : 1;
1034 unsigned int has_type : 1;
1035 unsigned int has_specification : 1;
1036 unsigned int has_pc_info : 1;
481860b3 1037 unsigned int may_be_inlined : 1;
72bf9492 1038
0c1b455e
TT
1039 /* This DIE has been marked DW_AT_main_subprogram. */
1040 unsigned int main_subprogram : 1;
1041
72bf9492
DJ
1042 /* Flag set if the SCOPE field of this structure has been
1043 computed. */
1044 unsigned int scope_set : 1;
1045
fa4028e9
JB
1046 /* Flag set if the DIE has a byte_size attribute. */
1047 unsigned int has_byte_size : 1;
1048
ff908ebf
AW
1049 /* Flag set if the DIE has a DW_AT_const_value attribute. */
1050 unsigned int has_const_value : 1;
1051
98bfdba5
PA
1052 /* Flag set if any of the DIE's children are template arguments. */
1053 unsigned int has_template_arguments : 1;
1054
52356b79 1055 /* Flag set if fixup has been called on this die. */
abc72ce4
DE
1056 unsigned int fixup_called : 1;
1057
36586728
TT
1058 /* Flag set if DW_TAG_imported_unit uses DW_FORM_GNU_ref_alt. */
1059 unsigned int is_dwz : 1;
1060
1061 /* Flag set if spec_offset uses DW_FORM_GNU_ref_alt. */
1062 unsigned int spec_is_dwz : 1;
1063
7d00ffec
TT
1064 unsigned int canonical_name : 1;
1065
72bf9492 1066 /* The name of this DIE. Normally the value of DW_AT_name, but
94af9270 1067 sometimes a default name for unnamed DIEs. */
7d00ffec 1068 const char *raw_name = nullptr;
72bf9492 1069
abc72ce4 1070 /* The linkage name, if present. */
6f06d47b 1071 const char *linkage_name = nullptr;
abc72ce4 1072
72bf9492
DJ
1073 /* The scope to prepend to our children. This is generally
1074 allocated on the comp_unit_obstack, so will disappear
1075 when this compilation unit leaves the cache. */
6f06d47b 1076 const char *scope = nullptr;
72bf9492 1077
95554aad
TT
1078 /* Some data associated with the partial DIE. The tag determines
1079 which field is live. */
1080 union
1081 {
1082 /* The location description associated with this DIE, if any. */
1083 struct dwarf_block *locdesc;
1084 /* The offset of an import, for DW_TAG_imported_unit. */
9c541725 1085 sect_offset sect_off;
6f06d47b 1086 } d {};
72bf9492
DJ
1087
1088 /* If HAS_PC_INFO, the PC range associated with this DIE. */
6f06d47b
YQ
1089 CORE_ADDR lowpc = 0;
1090 CORE_ADDR highpc = 0;
72bf9492 1091
93311388 1092 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
72bf9492 1093 DW_AT_sibling, if any. */
48fbe735
YQ
1094 /* NOTE: This member isn't strictly necessary, partial_die_info::read
1095 could return DW_AT_sibling values to its caller load_partial_dies. */
6f06d47b 1096 const gdb_byte *sibling = nullptr;
72bf9492
DJ
1097
1098 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
1099 DW_AT_specification (or DW_AT_abstract_origin or
1100 DW_AT_extension). */
6f06d47b 1101 sect_offset spec_offset {};
72bf9492
DJ
1102
1103 /* Pointers to this DIE's parent, first child, and next sibling,
1104 if any. */
6f06d47b
YQ
1105 struct partial_die_info *die_parent = nullptr;
1106 struct partial_die_info *die_child = nullptr;
1107 struct partial_die_info *die_sibling = nullptr;
1108
1109 friend struct partial_die_info *
1110 dwarf2_cu::find_partial_die (sect_offset sect_off);
1111
1112 private:
1113 /* Only need to do look up in dwarf2_cu::find_partial_die. */
1114 partial_die_info (sect_offset sect_off)
1115 : partial_die_info (sect_off, DW_TAG_padding, 0)
1116 {
1117 }
1118
1119 partial_die_info (sect_offset sect_off_, enum dwarf_tag tag_,
1120 int has_children_)
1121 : sect_off (sect_off_), tag (tag_), has_children (has_children_)
1122 {
1123 is_external = 0;
1124 is_declaration = 0;
1125 has_type = 0;
1126 has_specification = 0;
1127 has_pc_info = 0;
1128 may_be_inlined = 0;
1129 main_subprogram = 0;
1130 scope_set = 0;
1131 has_byte_size = 0;
1132 has_const_value = 0;
1133 has_template_arguments = 0;
1134 fixup_called = 0;
1135 is_dwz = 0;
1136 spec_is_dwz = 0;
7d00ffec 1137 canonical_name = 0;
6f06d47b 1138 }
c906108c
SS
1139 };
1140
c906108c
SS
1141/* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
1142 but this would require a corresponding change in unpack_field_as_long
1143 and friends. */
1144static int bits_per_byte = 8;
1145
9c6a1327
TT
1146struct variant_part_builder;
1147
1148/* When reading a variant, we track a bit more information about the
1149 field, and store it in an object of this type. */
2ddeaf8a
TT
1150
1151struct variant_field
1152{
9c6a1327
TT
1153 int first_field = -1;
1154 int last_field = -1;
1155
1156 /* A variant can contain other variant parts. */
1157 std::vector<variant_part_builder> variant_parts;
1158
2ddeaf8a
TT
1159 /* If we see a DW_TAG_variant, then this will be set if this is the
1160 default branch. */
9c6a1327
TT
1161 bool default_branch = false;
1162 /* If we see a DW_AT_discr_value, then this will be the discriminant
1163 value. */
1164 ULONGEST discriminant_value = 0;
1165 /* If we see a DW_AT_discr_list, then this is a pointer to the list
1166 data. */
1167 struct dwarf_block *discr_list_data = nullptr;
1168};
1169
1170/* This represents a DW_TAG_variant_part. */
1171
1172struct variant_part_builder
1173{
1174 /* The offset of the discriminant field. */
1175 sect_offset discriminant_offset {};
1176
1177 /* Variants that are direct children of this variant part. */
1178 std::vector<variant_field> variants;
1179
1180 /* True if we're currently reading a variant. */
1181 bool processing_variant = false;
2ddeaf8a
TT
1182};
1183
52059ffd
TT
1184struct nextfield
1185{
be2daae6
TT
1186 int accessibility = 0;
1187 int virtuality = 0;
9c6a1327
TT
1188 /* Variant parts need to find the discriminant, which is a DIE
1189 reference. We track the section offset of each field to make
1190 this link. */
1191 sect_offset offset;
be2daae6 1192 struct field field {};
52059ffd
TT
1193};
1194
1195struct fnfieldlist
1196{
be2daae6
TT
1197 const char *name = nullptr;
1198 std::vector<struct fn_field> fnfields;
52059ffd
TT
1199};
1200
c906108c
SS
1201/* The routines that read and process dies for a C struct or C++ class
1202 pass lists of data member fields and lists of member function fields
1203 in an instance of a field_info structure, as defined below. */
1204struct field_info
2de01bdb
SM
1205{
1206 /* List of data member and baseclasses fields. */
1207 std::vector<struct nextfield> fields;
1208 std::vector<struct nextfield> baseclasses;
1209
1210 /* Set if the accessibility of one of the fields is not public. */
264fc0e2 1211 bool non_public_fields = false;
2de01bdb
SM
1212
1213 /* Member function fieldlist array, contains name of possibly overloaded
1214 member function, number of overloaded member functions and a pointer
1215 to the head of the member function field chain. */
1216 std::vector<struct fnfieldlist> fnfieldlists;
1217
1218 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
1219 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
1220 std::vector<struct decl_field> typedef_field_list;
1221
1222 /* Nested types defined by this class and the number of elements in this
1223 list. */
1224 std::vector<struct decl_field> nested_types_list;
1225
1226 /* If non-null, this is the variant part we are currently
1227 reading. */
1228 variant_part_builder *current_variant_part = nullptr;
1229 /* This holds all the top-level variant parts attached to the type
1230 we're reading. */
1231 std::vector<variant_part_builder> variant_parts;
1232
1233 /* Return the total number of fields (including baseclasses). */
1234 int nfields () const
c5aa993b 1235 {
2de01bdb
SM
1236 return fields.size () + baseclasses.size ();
1237 }
1238};
c906108c 1239
ae038cb0
DJ
1240/* Loaded secondary compilation units are kept in memory until they
1241 have not been referenced for the processing of this many
1242 compilation units. Set this to zero to disable caching. Cache
1243 sizes of up to at least twenty will improve startup time for
1244 typical inter-CU-reference binaries, at an obvious memory cost. */
b4f54984 1245static int dwarf_max_cache_age = 5;
920d2a44 1246static void
b4f54984
DE
1247show_dwarf_max_cache_age (struct ui_file *file, int from_tty,
1248 struct cmd_list_element *c, const char *value)
920d2a44 1249{
3e43a32a 1250 fprintf_filtered (file, _("The upper bound on the age of cached "
b4f54984 1251 "DWARF compilation units is %s.\n"),
920d2a44
AC
1252 value);
1253}
4390d890 1254\f
c906108c
SS
1255/* local function prototypes */
1256
918dd910
JK
1257static void dwarf2_find_base_address (struct die_info *die,
1258 struct dwarf2_cu *cu);
1259
891813be 1260static dwarf2_psymtab *create_partial_symtab
7aa104c4
SM
1261 (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
1262 const char *name);
0018ea6f 1263
f1902523
JK
1264static void build_type_psymtabs_reader (const struct die_reader_specs *reader,
1265 const gdb_byte *info_ptr,
3e225074 1266 struct die_info *type_unit_die);
f1902523 1267
976ca316 1268static void dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile);
c906108c 1269
72bf9492
DJ
1270static void scan_partial_symbols (struct partial_die_info *,
1271 CORE_ADDR *, CORE_ADDR *,
5734ee8b 1272 int, struct dwarf2_cu *);
c906108c 1273
72bf9492
DJ
1274static void add_partial_symbol (struct partial_die_info *,
1275 struct dwarf2_cu *);
63d06c5c 1276
72bf9492
DJ
1277static void add_partial_namespace (struct partial_die_info *pdi,
1278 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 1279 int set_addrmap, struct dwarf2_cu *cu);
63d06c5c 1280
5d7cb8df 1281static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 1282 CORE_ADDR *highpc, int set_addrmap,
5d7cb8df
JK
1283 struct dwarf2_cu *cu);
1284
72bf9492
DJ
1285static void add_partial_enumeration (struct partial_die_info *enum_pdi,
1286 struct dwarf2_cu *cu);
91c24f0a 1287
bc30ff58
JB
1288static void add_partial_subprogram (struct partial_die_info *pdi,
1289 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5734ee8b 1290 int need_pc, struct dwarf2_cu *cu);
bc30ff58 1291
d521ce57 1292static unsigned int peek_abbrev_code (bfd *, const gdb_byte *);
6caca83c 1293
dee91e82 1294static struct partial_die_info *load_partial_dies
d521ce57 1295 (const struct die_reader_specs *, const gdb_byte *, int);
72bf9492 1296
fb816e8b
TV
1297/* A pair of partial_die_info and compilation unit. */
1298struct cu_partial_die_info
1299{
1300 /* The compilation unit of the partial_die_info. */
1301 struct dwarf2_cu *cu;
1302 /* A partial_die_info. */
1303 struct partial_die_info *pdi;
122cf0f2
AB
1304
1305 cu_partial_die_info (struct dwarf2_cu *cu, struct partial_die_info *pdi)
1306 : cu (cu),
1307 pdi (pdi)
405feb71 1308 { /* Nothing. */ }
122cf0f2
AB
1309
1310private:
1311 cu_partial_die_info () = delete;
fb816e8b
TV
1312};
1313
122cf0f2
AB
1314static const struct cu_partial_die_info find_partial_die (sect_offset, int,
1315 struct dwarf2_cu *);
72bf9492 1316
d521ce57
TT
1317static const gdb_byte *read_attribute (const struct die_reader_specs *,
1318 struct attribute *, struct attr_abbrev *,
7a5f294d 1319 const gdb_byte *);
18a8505e
AT
1320
1321static void read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 1322 struct attribute *attr, dwarf_tag tag);
18a8505e
AT
1323
1324static CORE_ADDR read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index);
a8329558 1325
976ca316
SM
1326static sect_offset read_abbrev_offset (dwarf2_per_objfile *per_objfile,
1327 dwarf2_section_info *, sect_offset);
f4dc4d17 1328
ed2dc618 1329static const char *read_indirect_string
976ca316 1330 (dwarf2_per_objfile *per_objfile, bfd *, const gdb_byte *,
ed2dc618 1331 const struct comp_unit_head *, unsigned int *);
4bdf3d34 1332
ed2dc618 1333static const char *read_indirect_string_at_offset
976ca316 1334 (dwarf2_per_objfile *per_objfile, LONGEST str_offset);
927aa2e7 1335
d521ce57
TT
1336static CORE_ADDR read_addr_index_from_leb128 (struct dwarf2_cu *,
1337 const gdb_byte *,
3019eac3
DE
1338 unsigned int *);
1339
18a8505e
AT
1340static const char *read_dwo_str_index (const struct die_reader_specs *reader,
1341 ULONGEST str_index);
1342
1343static const char *read_stub_str_index (struct dwarf2_cu *cu,
1344 ULONGEST str_index);
3019eac3 1345
e142c38c 1346static void set_cu_language (unsigned int, struct dwarf2_cu *);
c906108c 1347
e142c38c
DJ
1348static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1349 struct dwarf2_cu *);
c906108c 1350
7d45c7c3 1351static const char *dwarf2_string_attr (struct die_info *die, unsigned int name,
dda83cd7 1352 struct dwarf2_cu *cu);
7d45c7c3 1353
a084a2a6
AT
1354static const char *dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu);
1355
05cf31d1 1356static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
dda83cd7 1357 struct dwarf2_cu *cu);
05cf31d1 1358
e142c38c 1359static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
3ca72b44 1360
e142c38c 1361static struct die_info *die_specification (struct die_info *die,
f2f0e013 1362 struct dwarf2_cu **);
63d06c5c 1363
9c541725 1364static line_header_up dwarf_decode_line_header (sect_offset sect_off,
fff8551c 1365 struct dwarf2_cu *cu);
debd256d 1366
f3f5162e 1367static void dwarf_decode_lines (struct line_header *, const char *,
891813be 1368 struct dwarf2_cu *, dwarf2_psymtab *,
527f3840 1369 CORE_ADDR, int decode_mapping);
c906108c 1370
804d2729
TT
1371static void dwarf2_start_subfile (struct dwarf2_cu *, const char *,
1372 const char *);
c906108c 1373
a14ed312 1374static struct symbol *new_symbol (struct die_info *, struct type *,
5e2db402 1375 struct dwarf2_cu *, struct symbol * = NULL);
34eaf542 1376
ff39bb5e 1377static void dwarf2_const_value (const struct attribute *, struct symbol *,
e7c27a73 1378 struct dwarf2_cu *);
c906108c 1379
ff39bb5e 1380static void dwarf2_const_value_attr (const struct attribute *attr,
98bfdba5
PA
1381 struct type *type,
1382 const char *name,
1383 struct obstack *obstack,
12df843f 1384 struct dwarf2_cu *cu, LONGEST *value,
d521ce57 1385 const gdb_byte **bytes,
98bfdba5 1386 struct dwarf2_locexpr_baton **baton);
2df3850c 1387
57567375
TT
1388static struct type *read_subrange_index_type (struct die_info *die,
1389 struct dwarf2_cu *cu);
1390
e7c27a73 1391static struct type *die_type (struct die_info *, struct dwarf2_cu *);
c906108c 1392
b4ba55a1
JB
1393static int need_gnat_info (struct dwarf2_cu *);
1394
3e43a32a
MS
1395static struct type *die_descriptive_type (struct die_info *,
1396 struct dwarf2_cu *);
b4ba55a1
JB
1397
1398static void set_descriptive_type (struct type *, struct die_info *,
1399 struct dwarf2_cu *);
1400
e7c27a73
DJ
1401static struct type *die_containing_type (struct die_info *,
1402 struct dwarf2_cu *);
c906108c 1403
ff39bb5e 1404static struct type *lookup_die_type (struct die_info *, const struct attribute *,
673bfd45 1405 struct dwarf2_cu *);
c906108c 1406
f792889a 1407static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
c906108c 1408
673bfd45
DE
1409static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1410
0d5cff50 1411static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
63d06c5c 1412
6e70227d 1413static char *typename_concat (struct obstack *obs, const char *prefix,
f55ee35c
JK
1414 const char *suffix, int physname,
1415 struct dwarf2_cu *cu);
63d06c5c 1416
e7c27a73 1417static void read_file_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1418
348e048f
DE
1419static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1420
e7c27a73 1421static void read_func_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1422
e7c27a73 1423static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1424
96408a79
SA
1425static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1426
71a3c369
TT
1427static void read_variable (struct die_info *die, struct dwarf2_cu *cu);
1428
41144253 1429/* Return the .debug_loclists section to use for cu. */
1430static struct dwarf2_section_info *cu_debug_loc_section (struct dwarf2_cu *cu);
1431
d0ce17d8
CT
1432/* Return the .debug_rnglists section to use for cu. */
1433static struct dwarf2_section_info *cu_debug_rnglists_section
1434 (struct dwarf2_cu *cu, dwarf_tag tag);
1435
3a2b436a 1436/* How dwarf2_get_pc_bounds constructed its *LOWPC and *HIGHPC return
e385593e 1437 values. Keep the items ordered with increasing constraints compliance. */
3a2b436a
JK
1438enum pc_bounds_kind
1439{
e385593e 1440 /* No attribute DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges was found. */
3a2b436a
JK
1441 PC_BOUNDS_NOT_PRESENT,
1442
e385593e
JK
1443 /* Some of the attributes DW_AT_low_pc, DW_AT_high_pc or DW_AT_ranges
1444 were present but they do not form a valid range of PC addresses. */
1445 PC_BOUNDS_INVALID,
1446
3a2b436a
JK
1447 /* Discontiguous range was found - that is DW_AT_ranges was found. */
1448 PC_BOUNDS_RANGES,
1449
1450 /* Contiguous range was found - DW_AT_low_pc and DW_AT_high_pc were found. */
1451 PC_BOUNDS_HIGH_LOW,
1452};
1453
1454static enum pc_bounds_kind dwarf2_get_pc_bounds (struct die_info *,
1455 CORE_ADDR *, CORE_ADDR *,
1456 struct dwarf2_cu *,
891813be 1457 dwarf2_psymtab *);
c906108c 1458
fae299cd
DC
1459static void get_scope_pc_bounds (struct die_info *,
1460 CORE_ADDR *, CORE_ADDR *,
1461 struct dwarf2_cu *);
1462
801e3a5b 1463static void dwarf2_record_block_ranges (struct die_info *, struct block *,
dda83cd7 1464 CORE_ADDR, struct dwarf2_cu *);
801e3a5b 1465
a14ed312 1466static void dwarf2_add_field (struct field_info *, struct die_info *,
e7c27a73 1467 struct dwarf2_cu *);
c906108c 1468
a14ed312 1469static void dwarf2_attach_fields_to_type (struct field_info *,
e7c27a73 1470 struct type *, struct dwarf2_cu *);
c906108c 1471
a14ed312 1472static void dwarf2_add_member_fn (struct field_info *,
e26fb1d7 1473 struct die_info *, struct type *,
e7c27a73 1474 struct dwarf2_cu *);
c906108c 1475
a14ed312 1476static void dwarf2_attach_fn_fields_to_type (struct field_info *,
3e43a32a
MS
1477 struct type *,
1478 struct dwarf2_cu *);
c906108c 1479
134d01f1 1480static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1481
e7c27a73 1482static void read_common_block (struct die_info *, struct dwarf2_cu *);
c906108c 1483
e7c27a73 1484static void read_namespace (struct die_info *die, struct dwarf2_cu *);
d9fa45fe 1485
5d7cb8df
JK
1486static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1487
804d2729 1488static struct using_direct **using_directives (struct dwarf2_cu *cu);
22cee43f 1489
27aa8d6a
SW
1490static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1491
74921315
KS
1492static int read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu);
1493
f55ee35c
JK
1494static struct type *read_module_type (struct die_info *die,
1495 struct dwarf2_cu *cu);
1496
38d518c9 1497static const char *namespace_name (struct die_info *die,
e142c38c 1498 int *is_anonymous, struct dwarf2_cu *);
38d518c9 1499
134d01f1 1500static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
c906108c 1501
7d79de9a
TT
1502static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *,
1503 bool * = nullptr);
c906108c 1504
6e70227d 1505static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
7ca2d3a3
DL
1506 struct dwarf2_cu *);
1507
bf6af496 1508static struct die_info *read_die_and_siblings_1
d521ce57 1509 (const struct die_reader_specs *, const gdb_byte *, const gdb_byte **,
bf6af496 1510 struct die_info *);
639d11d3 1511
dee91e82 1512static struct die_info *read_die_and_siblings (const struct die_reader_specs *,
d521ce57
TT
1513 const gdb_byte *info_ptr,
1514 const gdb_byte **new_info_ptr,
639d11d3
DC
1515 struct die_info *parent);
1516
d521ce57
TT
1517static const gdb_byte *read_full_die_1 (const struct die_reader_specs *,
1518 struct die_info **, const gdb_byte *,
3e225074 1519 int);
3019eac3 1520
d521ce57 1521static const gdb_byte *read_full_die (const struct die_reader_specs *,
3e225074 1522 struct die_info **, const gdb_byte *);
93311388 1523
e7c27a73 1524static void process_die (struct die_info *, struct dwarf2_cu *);
c906108c 1525
15d034d0 1526static const char *dwarf2_canonicalize_name (const char *, struct dwarf2_cu *,
be1e3d3e 1527 struct objfile *);
71c25dea 1528
15d034d0 1529static const char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
9219021c 1530
15d034d0 1531static const char *dwarf2_full_name (const char *name,
98bfdba5
PA
1532 struct die_info *die,
1533 struct dwarf2_cu *cu);
1534
ca69b9e6
DE
1535static const char *dwarf2_physname (const char *name, struct die_info *die,
1536 struct dwarf2_cu *cu);
1537
e142c38c 1538static struct die_info *dwarf2_extension (struct die_info *die,
f2f0e013 1539 struct dwarf2_cu **);
9219021c 1540
d97bc12b
DE
1541static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1542
1543static void dump_die_for_error (struct die_info *);
1544
1545static void dump_die_1 (struct ui_file *, int level, int max_level,
1546 struct die_info *);
c906108c 1547
d97bc12b 1548/*static*/ void dump_die (struct die_info *, int max_level);
c906108c 1549
51545339 1550static void store_in_ref_table (struct die_info *,
10b3939b 1551 struct dwarf2_cu *);
c906108c 1552
348e048f 1553static struct die_info *follow_die_ref_or_sig (struct die_info *,
ff39bb5e 1554 const struct attribute *,
348e048f
DE
1555 struct dwarf2_cu **);
1556
10b3939b 1557static struct die_info *follow_die_ref (struct die_info *,
ff39bb5e 1558 const struct attribute *,
f2f0e013 1559 struct dwarf2_cu **);
c906108c 1560
348e048f 1561static struct die_info *follow_die_sig (struct die_info *,
ff39bb5e 1562 const struct attribute *,
348e048f
DE
1563 struct dwarf2_cu **);
1564
ac9ec31b
DE
1565static struct type *get_signatured_type (struct die_info *, ULONGEST,
1566 struct dwarf2_cu *);
1567
1568static struct type *get_DW_AT_signature_type (struct die_info *,
ff39bb5e 1569 const struct attribute *,
ac9ec31b
DE
1570 struct dwarf2_cu *);
1571
ab432490
SM
1572static void load_full_type_unit (dwarf2_per_cu_data *per_cu,
1573 dwarf2_per_objfile *per_objfile);
348e048f 1574
ab432490
SM
1575static void read_signatured_type (signatured_type *sig_type,
1576 dwarf2_per_objfile *per_objfile);
348e048f 1577
63e43d3a
PMR
1578static int attr_to_dynamic_prop (const struct attribute *attr,
1579 struct die_info *die, struct dwarf2_cu *cu,
9a49df9d 1580 struct dynamic_prop *prop, struct type *type);
63e43d3a 1581
c906108c
SS
1582/* memory allocation interface */
1583
7b5a2f43 1584static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
c906108c 1585
b60c80d6 1586static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
c906108c 1587
43f3e411 1588static void dwarf_decode_macros (struct dwarf2_cu *, unsigned int, int);
2e276125 1589
8cf6f0b1
TT
1590static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1591 struct dwarf2_loclist_baton *baton,
ff39bb5e 1592 const struct attribute *attr);
8cf6f0b1 1593
ff39bb5e 1594static void dwarf2_symbol_mark_computed (const struct attribute *attr,
93e7bd98 1595 struct symbol *sym,
f1e6e072
TT
1596 struct dwarf2_cu *cu,
1597 int is_block);
4c2df51b 1598
d521ce57
TT
1599static const gdb_byte *skip_one_die (const struct die_reader_specs *reader,
1600 const gdb_byte *info_ptr,
1601 struct abbrev_info *abbrev);
4bb7a0a7 1602
72bf9492
DJ
1603static hashval_t partial_die_hash (const void *item);
1604
1605static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1606
ae038cb0 1607static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
ed2dc618 1608 (sect_offset sect_off, unsigned int offset_in_dwz,
976ca316 1609 dwarf2_per_objfile *per_objfile);
ae038cb0 1610
9816fde3 1611static void prepare_one_comp_unit (struct dwarf2_cu *cu,
95554aad
TT
1612 struct die_info *comp_unit_die,
1613 enum language pretend_language);
93311388 1614
f792889a 1615static struct type *set_die_type (struct die_info *, struct type *,
57567375 1616 struct dwarf2_cu *, bool = false);
1c379e20 1617
976ca316 1618static void create_all_comp_units (dwarf2_per_objfile *per_objfile);
ae038cb0 1619
976ca316 1620static int create_all_type_units (dwarf2_per_objfile *per_objfile);
1fd400ff 1621
ab432490
SM
1622static void load_full_comp_unit (dwarf2_per_cu_data *per_cu,
1623 dwarf2_per_objfile *per_objfile,
4a636814 1624 dwarf2_cu *existing_cu,
ab432490
SM
1625 bool skip_partial,
1626 enum language pretend_language);
10b3939b 1627
8fc0b21d 1628static void process_full_comp_unit (dwarf2_cu *cu,
47b14e86 1629 enum language pretend_language);
10b3939b 1630
8fc0b21d 1631static void process_full_type_unit (dwarf2_cu *cu,
47b14e86 1632 enum language pretend_language);
f4dc4d17 1633
10b3939b
DJ
1634static void dwarf2_add_dependence (struct dwarf2_cu *,
1635 struct dwarf2_per_cu_data *);
1636
ae038cb0
DJ
1637static void dwarf2_mark (struct dwarf2_cu *);
1638
b64f50a1 1639static struct type *get_die_type_at_offset (sect_offset,
aa66c379
SM
1640 dwarf2_per_cu_data *per_cu,
1641 dwarf2_per_objfile *per_objfile);
673bfd45 1642
f792889a 1643static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
72019c9c 1644
120ce1b5
SM
1645static void queue_comp_unit (dwarf2_per_cu_data *per_cu,
1646 dwarf2_per_objfile *per_objfile,
95554aad
TT
1647 enum language pretend_language);
1648
976ca316 1649static void process_queue (dwarf2_per_objfile *per_objfile);
9291a0cd 1650
b303c6f6
AB
1651/* Class, the destructor of which frees all allocated queue entries. This
1652 will only have work to do if an error was thrown while processing the
1653 dwarf. If no error was thrown then the queue entries should have all
1654 been processed, and freed, as we went along. */
1655
1656class dwarf2_queue_guard
1657{
1658public:
39856def
TT
1659 explicit dwarf2_queue_guard (dwarf2_per_objfile *per_objfile)
1660 : m_per_objfile (per_objfile)
1661 {
1662 }
b303c6f6
AB
1663
1664 /* Free any entries remaining on the queue. There should only be
1665 entries left if we hit an error while processing the dwarf. */
1666 ~dwarf2_queue_guard ()
1667 {
39856def
TT
1668 /* Ensure that no memory is allocated by the queue. */
1669 std::queue<dwarf2_queue_item> empty;
5989a64e 1670 std::swap (m_per_objfile->per_bfd->queue, empty);
39856def 1671 }
b303c6f6 1672
39856def 1673 DISABLE_COPY_AND_ASSIGN (dwarf2_queue_guard);
b303c6f6 1674
39856def
TT
1675private:
1676 dwarf2_per_objfile *m_per_objfile;
b303c6f6
AB
1677};
1678
39856def
TT
1679dwarf2_queue_item::~dwarf2_queue_item ()
1680{
1681 /* Anything still marked queued is likely to be in an
1682 inconsistent state, so discard it. */
1683 if (per_cu->queued)
1684 {
7188ed02 1685 per_objfile->remove_cu (per_cu);
39856def
TT
1686 per_cu->queued = 0;
1687 }
1688}
1689
d721ba37
PA
1690/* The return type of find_file_and_directory. Note, the enclosed
1691 string pointers are only valid while this object is valid. */
1692
1693struct file_and_directory
1694{
1695 /* The filename. This is never NULL. */
1696 const char *name;
1697
1698 /* The compilation directory. NULL if not known. If we needed to
1699 compute a new string, this points to COMP_DIR_STORAGE, otherwise,
1700 points directly to the DW_AT_comp_dir string attribute owned by
1701 the obstack that owns the DIE. */
1702 const char *comp_dir;
1703
1704 /* If we needed to build a new string for comp_dir, this is what
1705 owns the storage. */
1706 std::string comp_dir_storage;
1707};
1708
1709static file_and_directory find_file_and_directory (struct die_info *die,
1710 struct dwarf2_cu *cu);
9291a0cd 1711
298e9637 1712static htab_up allocate_signatured_type_table ();
1fd400ff 1713
298e9637 1714static htab_up allocate_dwo_unit_table ();
3019eac3 1715
57d63ce2 1716static struct dwo_unit *lookup_dwo_unit_in_dwp
976ca316
SM
1717 (dwarf2_per_objfile *per_objfile, struct dwp_file *dwp_file,
1718 const char *comp_dir, ULONGEST signature, int is_debug_types);
a2ce51a0 1719
976ca316 1720static struct dwp_file *get_dwp_file (dwarf2_per_objfile *per_objfile);
a2ce51a0 1721
3019eac3 1722static struct dwo_unit *lookup_dwo_comp_unit
4ab09049
SM
1723 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
1724 ULONGEST signature);
3019eac3
DE
1725
1726static struct dwo_unit *lookup_dwo_type_unit
4ab09049 1727 (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir);
3019eac3 1728
1b555f17 1729static void queue_and_load_all_dwo_tus (dwarf2_cu *cu);
89e63ee4 1730
263db9a1
TT
1731/* A unique pointer to a dwo_file. */
1732
51ac9db5 1733typedef std::unique_ptr<struct dwo_file> dwo_file_up;
263db9a1 1734
976ca316 1735static void process_cu_includes (dwarf2_per_objfile *per_objfile);
95554aad 1736
1b80a9fa 1737static void check_producer (struct dwarf2_cu *cu);
527f3840
JK
1738
1739static void free_line_header_voidp (void *arg);
4390d890
DE
1740\f
1741/* Various complaints about symbol reading that don't abort the process. */
1742
4390d890
DE
1743static void
1744dwarf2_debug_line_missing_file_complaint (void)
1745{
b98664d3 1746 complaint (_(".debug_line section has line data without a file"));
4390d890
DE
1747}
1748
1749static void
1750dwarf2_debug_line_missing_end_sequence_complaint (void)
1751{
b98664d3 1752 complaint (_(".debug_line section has line "
4390d890
DE
1753 "program sequence without an end"));
1754}
1755
1756static void
1757dwarf2_complex_location_expr_complaint (void)
1758{
b98664d3 1759 complaint (_("location expression too complex"));
4390d890
DE
1760}
1761
1762static void
1763dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
1764 int arg3)
1765{
b98664d3 1766 complaint (_("const value length mismatch for '%s', got %d, expected %d"),
4390d890
DE
1767 arg1, arg2, arg3);
1768}
1769
4390d890
DE
1770static void
1771dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
1772{
b98664d3 1773 complaint (_("invalid attribute class or form for '%s' in '%s'"),
4390d890
DE
1774 arg1, arg2);
1775}
527f3840
JK
1776
1777/* Hash function for line_header_hash. */
1778
1779static hashval_t
1780line_header_hash (const struct line_header *ofs)
1781{
9c541725 1782 return to_underlying (ofs->sect_off) ^ ofs->offset_in_dwz;
527f3840
JK
1783}
1784
1785/* Hash function for htab_create_alloc_ex for line_header_hash. */
1786
1787static hashval_t
1788line_header_hash_voidp (const void *item)
1789{
9a3c8263 1790 const struct line_header *ofs = (const struct line_header *) item;
527f3840
JK
1791
1792 return line_header_hash (ofs);
1793}
1794
1795/* Equality function for line_header_hash. */
1796
1797static int
1798line_header_eq_voidp (const void *item_lhs, const void *item_rhs)
1799{
9a3c8263
SM
1800 const struct line_header *ofs_lhs = (const struct line_header *) item_lhs;
1801 const struct line_header *ofs_rhs = (const struct line_header *) item_rhs;
527f3840 1802
9c541725 1803 return (ofs_lhs->sect_off == ofs_rhs->sect_off
527f3840
JK
1804 && ofs_lhs->offset_in_dwz == ofs_rhs->offset_in_dwz);
1805}
1806
4390d890 1807\f
9291a0cd 1808
330cdd98
PA
1809/* See declaration. */
1810
5989a64e
SM
1811dwarf2_per_bfd::dwarf2_per_bfd (bfd *obfd, const dwarf2_debug_sections *names,
1812 bool can_copy_)
c3699833
SM
1813 : obfd (obfd),
1814 can_copy (can_copy_)
330cdd98
PA
1815{
1816 if (names == NULL)
1817 names = &dwarf2_elf_names;
1818
330cdd98
PA
1819 for (asection *sec = obfd->sections; sec != NULL; sec = sec->next)
1820 locate_sections (obfd, sec, *names);
1821}
1822
5989a64e 1823dwarf2_per_bfd::~dwarf2_per_bfd ()
330cdd98 1824{
b76e467d 1825 for (dwarf2_per_cu_data *per_cu : all_comp_units)
ae640021 1826 per_cu->imported_symtabs_free ();
fc8e7e75 1827
b2bdb8cf 1828 for (signatured_type *sig_type : all_type_units)
ae640021 1829 sig_type->per_cu.imported_symtabs_free ();
fc8e7e75 1830
5989a64e 1831 /* Everything else should be on this->obstack. */
330cdd98
PA
1832}
1833
7188ed02 1834/* See read.h. */
330cdd98
PA
1835
1836void
7188ed02 1837dwarf2_per_objfile::remove_all_cus ()
330cdd98 1838{
7188ed02
SM
1839 for (auto pair : m_dwarf2_cus)
1840 delete pair.second;
330cdd98 1841
7188ed02 1842 m_dwarf2_cus.clear ();
330cdd98
PA
1843}
1844
11ed8cad
TT
1845/* A helper class that calls free_cached_comp_units on
1846 destruction. */
1847
1848class free_cached_comp_units
1849{
1850public:
1851
1852 explicit free_cached_comp_units (dwarf2_per_objfile *per_objfile)
1853 : m_per_objfile (per_objfile)
1854 {
1855 }
1856
1857 ~free_cached_comp_units ()
1858 {
7188ed02 1859 m_per_objfile->remove_all_cus ();
11ed8cad
TT
1860 }
1861
1862 DISABLE_COPY_AND_ASSIGN (free_cached_comp_units);
1863
1864private:
1865
1866 dwarf2_per_objfile *m_per_objfile;
1867};
1868
af758d11
SM
1869/* See read.h. */
1870
1871bool
1872dwarf2_per_objfile::symtab_set_p (const dwarf2_per_cu_data *per_cu) const
1873{
1874 gdb_assert (per_cu->index < this->m_symtabs.size ());
1875
1876 return this->m_symtabs[per_cu->index] != nullptr;
1877}
1878
1879/* See read.h. */
1880
1881compunit_symtab *
1882dwarf2_per_objfile::get_symtab (const dwarf2_per_cu_data *per_cu) const
1883{
1884 gdb_assert (per_cu->index < this->m_symtabs.size ());
1885
1886 return this->m_symtabs[per_cu->index];
1887}
1888
1889/* See read.h. */
1890
1891void
1892dwarf2_per_objfile::set_symtab (const dwarf2_per_cu_data *per_cu,
1893 compunit_symtab *symtab)
1894{
1895 gdb_assert (per_cu->index < this->m_symtabs.size ());
1896 gdb_assert (this->m_symtabs[per_cu->index] == nullptr);
1897
1898 this->m_symtabs[per_cu->index] = symtab;
1899}
1900
c906108c 1901/* Try to locate the sections we need for DWARF 2 debugging
251d32d9
TG
1902 information and return true if we have enough to do something.
1903 NAMES points to the dwarf2 section names, or is NULL if the standard
4b610737
TT
1904 ELF names are used. CAN_COPY is true for formats where symbol
1905 interposition is possible and so symbol values must follow copy
1906 relocation rules. */
c906108c
SS
1907
1908int
251d32d9 1909dwarf2_has_info (struct objfile *objfile,
dda83cd7 1910 const struct dwarf2_debug_sections *names,
4b610737 1911 bool can_copy)
c906108c 1912{
97cbe998
SDJ
1913 if (objfile->flags & OBJF_READNEVER)
1914 return 0;
1915
976ca316 1916 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 1917
976ca316 1918 if (per_objfile == NULL)
5989a64e 1919 {
17ee85fc
TT
1920 dwarf2_per_bfd *per_bfd;
1921
1922 /* We can share a "dwarf2_per_bfd" with other objfiles if the BFD
dda83cd7 1923 doesn't require relocations and if there aren't partial symbols
17ee85fc
TT
1924 from some other reader. */
1925 if (!objfile_has_partial_symbols (objfile)
1926 && !gdb_bfd_requires_relocations (objfile->obfd))
1927 {
1928 /* See if one has been created for this BFD yet. */
1929 per_bfd = dwarf2_per_bfd_bfd_data_key.get (objfile->obfd);
1930
1931 if (per_bfd == nullptr)
1932 {
1933 /* No, create it now. */
1934 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1935 dwarf2_per_bfd_bfd_data_key.set (objfile->obfd, per_bfd);
1936 }
1937 }
1938 else
1939 {
1940 /* No sharing possible, create one specifically for this objfile. */
1941 per_bfd = new dwarf2_per_bfd (objfile->obfd, names, can_copy);
1942 dwarf2_per_bfd_objfile_data_key.set (objfile, per_bfd);
1943 }
5989a64e 1944
976ca316 1945 per_objfile = dwarf2_objfile_data_key.emplace (objfile, objfile, per_bfd);
5989a64e 1946 }
5bfd760d 1947
976ca316
SM
1948 return (!per_objfile->per_bfd->info.is_virtual
1949 && per_objfile->per_bfd->info.s.section != NULL
1950 && !per_objfile->per_bfd->abbrev.is_virtual
1951 && per_objfile->per_bfd->abbrev.s.section != NULL);
73869dc2
DE
1952}
1953
251d32d9
TG
1954/* When loading sections, we look either for uncompressed section or for
1955 compressed section names. */
233a11ab
CS
1956
1957static int
251d32d9 1958section_is_p (const char *section_name,
dda83cd7 1959 const struct dwarf2_section_names *names)
233a11ab 1960{
251d32d9
TG
1961 if (names->normal != NULL
1962 && strcmp (section_name, names->normal) == 0)
1963 return 1;
1964 if (names->compressed != NULL
1965 && strcmp (section_name, names->compressed) == 0)
1966 return 1;
1967 return 0;
233a11ab
CS
1968}
1969
330cdd98 1970/* See declaration. */
c906108c 1971
330cdd98 1972void
5989a64e
SM
1973dwarf2_per_bfd::locate_sections (bfd *abfd, asection *sectp,
1974 const dwarf2_debug_sections &names)
c906108c 1975{
fd361982 1976 flagword aflag = bfd_section_flags (sectp);
251d32d9 1977
dc7650b8
JK
1978 if ((aflag & SEC_HAS_CONTENTS) == 0)
1979 {
1980 }
950b7495
KS
1981 else if (elf_section_data (sectp)->this_hdr.sh_size
1982 > bfd_get_file_size (abfd))
1983 {
1984 bfd_size_type size = elf_section_data (sectp)->this_hdr.sh_size;
1985 warning (_("Discarding section %s which has a section size (%s"
1986 ") larger than the file size [in module %s]"),
1987 bfd_section_name (sectp), phex_nz (size, sizeof (size)),
1988 bfd_get_filename (abfd));
1989 }
330cdd98 1990 else if (section_is_p (sectp->name, &names.info))
c906108c 1991 {
330cdd98 1992 this->info.s.section = sectp;
fd361982 1993 this->info.size = bfd_section_size (sectp);
c906108c 1994 }
330cdd98 1995 else if (section_is_p (sectp->name, &names.abbrev))
c906108c 1996 {
330cdd98 1997 this->abbrev.s.section = sectp;
fd361982 1998 this->abbrev.size = bfd_section_size (sectp);
c906108c 1999 }
330cdd98 2000 else if (section_is_p (sectp->name, &names.line))
c906108c 2001 {
330cdd98 2002 this->line.s.section = sectp;
fd361982 2003 this->line.size = bfd_section_size (sectp);
c906108c 2004 }
330cdd98 2005 else if (section_is_p (sectp->name, &names.loc))
c906108c 2006 {
330cdd98 2007 this->loc.s.section = sectp;
fd361982 2008 this->loc.size = bfd_section_size (sectp);
c906108c 2009 }
330cdd98 2010 else if (section_is_p (sectp->name, &names.loclists))
43988095 2011 {
330cdd98 2012 this->loclists.s.section = sectp;
fd361982 2013 this->loclists.size = bfd_section_size (sectp);
43988095 2014 }
330cdd98 2015 else if (section_is_p (sectp->name, &names.macinfo))
c906108c 2016 {
330cdd98 2017 this->macinfo.s.section = sectp;
fd361982 2018 this->macinfo.size = bfd_section_size (sectp);
c906108c 2019 }
330cdd98 2020 else if (section_is_p (sectp->name, &names.macro))
cf2c3c16 2021 {
330cdd98 2022 this->macro.s.section = sectp;
fd361982 2023 this->macro.size = bfd_section_size (sectp);
cf2c3c16 2024 }
330cdd98 2025 else if (section_is_p (sectp->name, &names.str))
c906108c 2026 {
330cdd98 2027 this->str.s.section = sectp;
fd361982 2028 this->str.size = bfd_section_size (sectp);
c906108c 2029 }
18a8505e
AT
2030 else if (section_is_p (sectp->name, &names.str_offsets))
2031 {
2032 this->str_offsets.s.section = sectp;
2033 this->str_offsets.size = bfd_section_size (sectp);
2034 }
330cdd98 2035 else if (section_is_p (sectp->name, &names.line_str))
43988095 2036 {
330cdd98 2037 this->line_str.s.section = sectp;
fd361982 2038 this->line_str.size = bfd_section_size (sectp);
43988095 2039 }
330cdd98 2040 else if (section_is_p (sectp->name, &names.addr))
3019eac3 2041 {
330cdd98 2042 this->addr.s.section = sectp;
fd361982 2043 this->addr.size = bfd_section_size (sectp);
3019eac3 2044 }
330cdd98 2045 else if (section_is_p (sectp->name, &names.frame))
b6af0555 2046 {
330cdd98 2047 this->frame.s.section = sectp;
fd361982 2048 this->frame.size = bfd_section_size (sectp);
b6af0555 2049 }
330cdd98 2050 else if (section_is_p (sectp->name, &names.eh_frame))
b6af0555 2051 {
330cdd98 2052 this->eh_frame.s.section = sectp;
fd361982 2053 this->eh_frame.size = bfd_section_size (sectp);
b6af0555 2054 }
330cdd98 2055 else if (section_is_p (sectp->name, &names.ranges))
af34e669 2056 {
330cdd98 2057 this->ranges.s.section = sectp;
fd361982 2058 this->ranges.size = bfd_section_size (sectp);
af34e669 2059 }
330cdd98 2060 else if (section_is_p (sectp->name, &names.rnglists))
43988095 2061 {
330cdd98 2062 this->rnglists.s.section = sectp;
fd361982 2063 this->rnglists.size = bfd_section_size (sectp);
43988095 2064 }
330cdd98 2065 else if (section_is_p (sectp->name, &names.types))
348e048f 2066 {
8b70b953
TT
2067 struct dwarf2_section_info type_section;
2068
2069 memset (&type_section, 0, sizeof (type_section));
049412e3 2070 type_section.s.section = sectp;
fd361982 2071 type_section.size = bfd_section_size (sectp);
8b70b953 2072
fd5866f6 2073 this->types.push_back (type_section);
348e048f 2074 }
330cdd98 2075 else if (section_is_p (sectp->name, &names.gdb_index))
9291a0cd 2076 {
330cdd98 2077 this->gdb_index.s.section = sectp;
fd361982 2078 this->gdb_index.size = bfd_section_size (sectp);
9291a0cd 2079 }
927aa2e7
JK
2080 else if (section_is_p (sectp->name, &names.debug_names))
2081 {
2082 this->debug_names.s.section = sectp;
fd361982 2083 this->debug_names.size = bfd_section_size (sectp);
927aa2e7
JK
2084 }
2085 else if (section_is_p (sectp->name, &names.debug_aranges))
2086 {
2087 this->debug_aranges.s.section = sectp;
fd361982 2088 this->debug_aranges.size = bfd_section_size (sectp);
927aa2e7 2089 }
dce234bc 2090
fd361982
AM
2091 if ((bfd_section_flags (sectp) & (SEC_LOAD | SEC_ALLOC))
2092 && bfd_section_vma (sectp) == 0)
330cdd98 2093 this->has_section_at_zero = true;
c906108c
SS
2094}
2095
dce234bc 2096/* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
0963b4bd 2097 SECTION_NAME. */
af34e669 2098
dce234bc 2099void
3017a003 2100dwarf2_get_section_info (struct objfile *objfile,
dda83cd7
SM
2101 enum dwarf2_section_enum sect,
2102 asection **sectp, const gdb_byte **bufp,
2103 bfd_size_type *sizep)
dce234bc 2104{
976ca316 2105 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
dce234bc 2106 struct dwarf2_section_info *info;
a3b2a86b
TT
2107
2108 /* We may see an objfile without any DWARF, in which case we just
2109 return nothing. */
976ca316 2110 if (per_objfile == NULL)
a3b2a86b
TT
2111 {
2112 *sectp = NULL;
2113 *bufp = NULL;
2114 *sizep = 0;
2115 return;
2116 }
3017a003
TG
2117 switch (sect)
2118 {
2119 case DWARF2_DEBUG_FRAME:
976ca316 2120 info = &per_objfile->per_bfd->frame;
3017a003
TG
2121 break;
2122 case DWARF2_EH_FRAME:
976ca316 2123 info = &per_objfile->per_bfd->eh_frame;
3017a003
TG
2124 break;
2125 default:
2126 gdb_assert_not_reached ("unexpected section");
2127 }
dce234bc 2128
96b79293 2129 info->read (objfile);
dce234bc 2130
96b79293 2131 *sectp = info->get_bfd_section ();
dce234bc
PP
2132 *bufp = info->buffer;
2133 *sizep = info->size;
2134}
2135
36586728
TT
2136/* A helper function to find the sections for a .dwz file. */
2137
2138static void
5bb6e9dd 2139locate_dwz_sections (bfd *abfd, asection *sectp, dwz_file *dwz_file)
36586728 2140{
36586728
TT
2141 /* Note that we only support the standard ELF names, because .dwz
2142 is ELF-only (at the time of writing). */
2143 if (section_is_p (sectp->name, &dwarf2_elf_names.abbrev))
2144 {
049412e3 2145 dwz_file->abbrev.s.section = sectp;
fd361982 2146 dwz_file->abbrev.size = bfd_section_size (sectp);
36586728
TT
2147 }
2148 else if (section_is_p (sectp->name, &dwarf2_elf_names.info))
2149 {
049412e3 2150 dwz_file->info.s.section = sectp;
fd361982 2151 dwz_file->info.size = bfd_section_size (sectp);
36586728
TT
2152 }
2153 else if (section_is_p (sectp->name, &dwarf2_elf_names.str))
2154 {
049412e3 2155 dwz_file->str.s.section = sectp;
fd361982 2156 dwz_file->str.size = bfd_section_size (sectp);
36586728
TT
2157 }
2158 else if (section_is_p (sectp->name, &dwarf2_elf_names.line))
2159 {
049412e3 2160 dwz_file->line.s.section = sectp;
fd361982 2161 dwz_file->line.size = bfd_section_size (sectp);
36586728
TT
2162 }
2163 else if (section_is_p (sectp->name, &dwarf2_elf_names.macro))
2164 {
049412e3 2165 dwz_file->macro.s.section = sectp;
fd361982 2166 dwz_file->macro.size = bfd_section_size (sectp);
36586728 2167 }
2ec9a5e0
TT
2168 else if (section_is_p (sectp->name, &dwarf2_elf_names.gdb_index))
2169 {
049412e3 2170 dwz_file->gdb_index.s.section = sectp;
fd361982 2171 dwz_file->gdb_index.size = bfd_section_size (sectp);
2ec9a5e0 2172 }
927aa2e7
JK
2173 else if (section_is_p (sectp->name, &dwarf2_elf_names.debug_names))
2174 {
2175 dwz_file->debug_names.s.section = sectp;
fd361982 2176 dwz_file->debug_names.size = bfd_section_size (sectp);
927aa2e7 2177 }
36586728
TT
2178}
2179
2bf3b79d
SDJ
2180/* Attempt to find a .dwz file (whose full path is represented by
2181 FILENAME) in all of the specified debug file directories provided.
2182
2183 Return the equivalent gdb_bfd_ref_ptr of the .dwz file found, or
2184 nullptr if it could not find anything. */
2185
2186static gdb_bfd_ref_ptr
2187dwz_search_other_debugdirs (std::string &filename, bfd_byte *buildid,
2188 size_t buildid_len)
2189{
2190 /* Let's assume that the path represented by FILENAME has the
2191 "/.dwz/" subpath in it. This is what (most) GNU/Linux
2192 distributions do, anyway. */
2193 size_t dwz_pos = filename.find ("/.dwz/");
2194
2195 if (dwz_pos == std::string::npos)
2196 return nullptr;
2197
2198 /* This is an obvious assertion, but it's here more to educate
2199 future readers of this code that FILENAME at DWZ_POS *must*
2200 contain a directory separator. */
2201 gdb_assert (IS_DIR_SEPARATOR (filename[dwz_pos]));
2202
2203 gdb_bfd_ref_ptr dwz_bfd;
2204 std::vector<gdb::unique_xmalloc_ptr<char>> debugdir_vec
2205 = dirnames_to_char_ptr_vec (debug_file_directory);
2206
2207 for (const gdb::unique_xmalloc_ptr<char> &debugdir : debugdir_vec)
2208 {
2209 /* The idea is to iterate over the
2210 debug file directories provided by the user and
2211 replace the hard-coded path in the "filename" by each
2212 debug-file-directory.
2213
2214 For example, suppose that filename is:
2215
2216 /usr/lib/debug/.dwz/foo.dwz
2217
2218 And suppose that we have "$HOME/bar" as the
2219 debug-file-directory. We would then adjust filename
2220 to look like:
2221
2222 $HOME/bar/.dwz/foo.dwz
2223
2224 which would hopefully allow us to find the alt debug
2225 file. */
2226 std::string ddir = debugdir.get ();
2227
2228 if (ddir.empty ())
2229 continue;
2230
2231 /* Make sure the current debug-file-directory ends with a
2232 directory separator. This is needed because, if FILENAME
2233 contains something like "/usr/lib/abcde/.dwz/foo.dwz" and
2234 DDIR is "/usr/lib/abc", then could wrongfully skip it
2235 below. */
2236 if (!IS_DIR_SEPARATOR (ddir.back ()))
2237 ddir += SLASH_STRING;
2238
2239 /* Check whether the beginning of FILENAME is DDIR. If it is,
2240 then we are dealing with a file which we already attempted to
2241 open before, so we just skip it and continue processing the
2242 remaining debug file directories. */
2243 if (filename.size () > ddir.size ()
2244 && filename.compare (0, ddir.size (), ddir) == 0)
2245 continue;
2246
2247 /* Replace FILENAME's default debug-file-directory with
2248 DDIR. */
2249 std::string new_filename = ddir + &filename[dwz_pos + 1];
2250
2251 dwz_bfd = gdb_bfd_open (new_filename.c_str (), gnutarget);
2252
2253 if (dwz_bfd == nullptr)
2254 continue;
2255
2256 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2257 {
2258 dwz_bfd.reset (nullptr);
2259 continue;
2260 }
2261
2262 /* Found it. */
2263 break;
2264 }
2265
2266 return dwz_bfd;
2267}
2268
c4973306 2269/* See dwarf2read.h. */
36586728 2270
c4973306 2271struct dwz_file *
c3699833 2272dwarf2_get_dwz_file (dwarf2_per_bfd *per_bfd)
36586728 2273{
acd13123 2274 bfd_size_type buildid_len_arg;
dc294be5
TT
2275 size_t buildid_len;
2276 bfd_byte *buildid;
36586728 2277
c3699833
SM
2278 if (per_bfd->dwz_file != NULL)
2279 return per_bfd->dwz_file.get ();
36586728 2280
4db1a1dc 2281 bfd_set_error (bfd_error_no_error);
791afaa2 2282 gdb::unique_xmalloc_ptr<char> data
c3699833 2283 (bfd_get_alt_debug_link_info (per_bfd->obfd,
791afaa2 2284 &buildid_len_arg, &buildid));
4db1a1dc
TT
2285 if (data == NULL)
2286 {
2287 if (bfd_get_error () == bfd_error_no_error)
2288 return NULL;
2289 error (_("could not read '.gnu_debugaltlink' section: %s"),
2290 bfd_errmsg (bfd_get_error ()));
2291 }
791afaa2
TT
2292
2293 gdb::unique_xmalloc_ptr<bfd_byte> buildid_holder (buildid);
36586728 2294
acd13123
TT
2295 buildid_len = (size_t) buildid_len_arg;
2296
2bf3b79d 2297 std::string filename = data.get ();
d721ba37 2298
2bf3b79d 2299 if (!IS_ABSOLUTE_PATH (filename.c_str ()))
36586728 2300 {
14278e1f 2301 gdb::unique_xmalloc_ptr<char> abs
c3699833 2302 = gdb_realpath (bfd_get_filename (per_bfd->obfd));
36586728 2303
2bf3b79d 2304 filename = ldirname (abs.get ()) + SLASH_STRING + filename;
36586728
TT
2305 }
2306
dc294be5
TT
2307 /* First try the file name given in the section. If that doesn't
2308 work, try to use the build-id instead. */
2bf3b79d 2309 gdb_bfd_ref_ptr dwz_bfd (gdb_bfd_open (filename.c_str (), gnutarget));
dc294be5 2310 if (dwz_bfd != NULL)
36586728 2311 {
192b62ce 2312 if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
0f58c9e8 2313 dwz_bfd.reset (nullptr);
36586728
TT
2314 }
2315
dc294be5
TT
2316 if (dwz_bfd == NULL)
2317 dwz_bfd = build_id_to_debug_bfd (buildid_len, buildid);
2318
2bf3b79d
SDJ
2319 if (dwz_bfd == nullptr)
2320 {
2321 /* If the user has provided us with different
2322 debug file directories, we can try them in order. */
2323 dwz_bfd = dwz_search_other_debugdirs (filename, buildid, buildid_len);
2324 }
2325
0d79cdc4
AM
2326 if (dwz_bfd == nullptr)
2327 {
2328 gdb::unique_xmalloc_ptr<char> alt_filename;
c3699833 2329 const char *origname = bfd_get_filename (per_bfd->obfd);
0d79cdc4
AM
2330
2331 scoped_fd fd (debuginfod_debuginfo_query (buildid,
2332 buildid_len,
2333 origname,
2334 &alt_filename));
2335
2336 if (fd.get () >= 0)
2337 {
2338 /* File successfully retrieved from server. */
ad80db5b 2339 dwz_bfd = gdb_bfd_open (alt_filename.get (), gnutarget);
0d79cdc4
AM
2340
2341 if (dwz_bfd == nullptr)
2342 warning (_("File \"%s\" from debuginfod cannot be opened as bfd"),
2343 alt_filename.get ());
2344 else if (!build_id_verify (dwz_bfd.get (), buildid_len, buildid))
2345 dwz_bfd.reset (nullptr);
2346 }
2347 }
2348
dc294be5
TT
2349 if (dwz_bfd == NULL)
2350 error (_("could not find '.gnu_debugaltlink' file for %s"),
c3699833 2351 bfd_get_filename (per_bfd->obfd));
dc294be5 2352
7ff8cb8c
TT
2353 std::unique_ptr<struct dwz_file> result
2354 (new struct dwz_file (std::move (dwz_bfd)));
36586728 2355
5bb6e9dd
TT
2356 for (asection *sec : gdb_bfd_sections (result->dwz_bfd))
2357 locate_dwz_sections (result->dwz_bfd.get (), sec, result.get ());
36586728 2358
c3699833
SM
2359 gdb_bfd_record_inclusion (per_bfd->obfd, result->dwz_bfd.get ());
2360 per_bfd->dwz_file = std::move (result);
2361 return per_bfd->dwz_file.get ();
36586728 2362}
9291a0cd 2363\f
7b9f3c50
DE
2364/* DWARF quick_symbols_functions support. */
2365
2366/* TUs can share .debug_line entries, and there can be a lot more TUs than
2367 unique line tables, so we maintain a separate table of all .debug_line
2368 derived entries to support the sharing.
2369 All the quick functions need is the list of file names. We discard the
2370 line_header when we're done and don't need to record it here. */
2371struct quick_file_names
2372{
094b34ac
DE
2373 /* The data used to construct the hash key. */
2374 struct stmt_list_hash hash;
7b9f3c50
DE
2375
2376 /* The number of entries in file_names, real_names. */
2377 unsigned int num_file_names;
2378
2379 /* The file names from the line table, after being run through
2380 file_full_name. */
2381 const char **file_names;
2382
2383 /* The file names from the line table after being run through
2384 gdb_realpath. These are computed lazily. */
2385 const char **real_names;
2386};
2387
2388/* When using the index (and thus not using psymtabs), each CU has an
2389 object of this type. This is used to hold information needed by
2390 the various "quick" methods. */
2391struct dwarf2_per_cu_quick_data
2392{
2393 /* The file table. This can be NULL if there was no file table
2394 or it's currently not read in.
5989a64e 2395 NOTE: This points into dwarf2_per_objfile->per_bfd->quick_file_names_table. */
7b9f3c50
DE
2396 struct quick_file_names *file_names;
2397
7b9f3c50
DE
2398 /* A temporary mark bit used when iterating over all CUs in
2399 expand_symtabs_matching. */
2400 unsigned int mark : 1;
2401
2402 /* True if we've tried to read the file table and found there isn't one.
2403 There will be no point in trying to read it again next time. */
2404 unsigned int no_file_data : 1;
2405};
2406
094b34ac
DE
2407/* Utility hash function for a stmt_list_hash. */
2408
2409static hashval_t
2410hash_stmt_list_entry (const struct stmt_list_hash *stmt_list_hash)
2411{
2412 hashval_t v = 0;
2413
2414 if (stmt_list_hash->dwo_unit != NULL)
2415 v += (uintptr_t) stmt_list_hash->dwo_unit->dwo_file;
9c541725 2416 v += to_underlying (stmt_list_hash->line_sect_off);
094b34ac
DE
2417 return v;
2418}
2419
2420/* Utility equality function for a stmt_list_hash. */
2421
2422static int
2423eq_stmt_list_entry (const struct stmt_list_hash *lhs,
2424 const struct stmt_list_hash *rhs)
2425{
2426 if ((lhs->dwo_unit != NULL) != (rhs->dwo_unit != NULL))
2427 return 0;
2428 if (lhs->dwo_unit != NULL
2429 && lhs->dwo_unit->dwo_file != rhs->dwo_unit->dwo_file)
2430 return 0;
2431
9c541725 2432 return lhs->line_sect_off == rhs->line_sect_off;
094b34ac
DE
2433}
2434
7b9f3c50
DE
2435/* Hash function for a quick_file_names. */
2436
2437static hashval_t
2438hash_file_name_entry (const void *e)
2439{
9a3c8263
SM
2440 const struct quick_file_names *file_data
2441 = (const struct quick_file_names *) e;
7b9f3c50 2442
094b34ac 2443 return hash_stmt_list_entry (&file_data->hash);
7b9f3c50
DE
2444}
2445
2446/* Equality function for a quick_file_names. */
2447
2448static int
2449eq_file_name_entry (const void *a, const void *b)
2450{
9a3c8263
SM
2451 const struct quick_file_names *ea = (const struct quick_file_names *) a;
2452 const struct quick_file_names *eb = (const struct quick_file_names *) b;
7b9f3c50 2453
094b34ac 2454 return eq_stmt_list_entry (&ea->hash, &eb->hash);
7b9f3c50
DE
2455}
2456
2457/* Delete function for a quick_file_names. */
2458
2459static void
2460delete_file_name_entry (void *e)
2461{
9a3c8263 2462 struct quick_file_names *file_data = (struct quick_file_names *) e;
7b9f3c50
DE
2463 int i;
2464
2465 for (i = 0; i < file_data->num_file_names; ++i)
2466 {
2467 xfree ((void*) file_data->file_names[i]);
2468 if (file_data->real_names)
2469 xfree ((void*) file_data->real_names[i]);
2470 }
2471
45940949
TT
2472 /* The space for the struct itself lives on the obstack, so we don't
2473 free it here. */
7b9f3c50
DE
2474}
2475
2476/* Create a quick_file_names hash table. */
2477
5895093f 2478static htab_up
7b9f3c50
DE
2479create_quick_file_names_table (unsigned int nr_initial_entries)
2480{
5895093f
TT
2481 return htab_up (htab_create_alloc (nr_initial_entries,
2482 hash_file_name_entry, eq_file_name_entry,
2483 delete_file_name_entry, xcalloc, xfree));
7b9f3c50 2484}
9291a0cd 2485
ab432490
SM
2486/* Read in CU (dwarf2_cu object) for PER_CU in the context of PER_OBJFILE. This
2487 function is unrelated to symtabs, symtab would have to be created afterwards.
2488 You should call age_cached_comp_units after processing the CU. */
918dd910 2489
1b555f17 2490static dwarf2_cu *
ab432490
SM
2491load_cu (dwarf2_per_cu_data *per_cu, dwarf2_per_objfile *per_objfile,
2492 bool skip_partial)
918dd910 2493{
3019eac3 2494 if (per_cu->is_debug_types)
ab432490 2495 load_full_type_unit (per_cu, per_objfile);
918dd910 2496 else
4a636814
SM
2497 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
2498 skip_partial, language_minimal);
918dd910 2499
7188ed02
SM
2500 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
2501 if (cu == nullptr)
1b555f17 2502 return nullptr; /* Dummy CU. */
2dc860c0 2503
7188ed02 2504 dwarf2_find_base_address (cu->dies, cu);
1b555f17 2505
7188ed02 2506 return cu;
918dd910
JK
2507}
2508
1350c3b4 2509/* Read in the symbols for PER_CU in the context of PER_OBJFILE. */
2fdf6df6 2510
9291a0cd 2511static void
97a1449a 2512dw2_do_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2513 dwarf2_per_objfile *per_objfile, bool skip_partial)
9291a0cd 2514{
f4dc4d17
DE
2515 /* Skip type_unit_groups, reading the type units they contain
2516 is handled elsewhere. */
197400e8 2517 if (per_cu->type_unit_group_p ())
f4dc4d17
DE
2518 return;
2519
b303c6f6
AB
2520 /* The destructor of dwarf2_queue_guard frees any entries left on
2521 the queue. After this point we're guaranteed to leave this function
2522 with the dwarf queue empty. */
bf6e5d01 2523 dwarf2_queue_guard q_guard (per_objfile);
9291a0cd 2524
976ca316 2525 if (!per_objfile->symtab_set_p (per_cu))
95554aad 2526 {
976ca316
SM
2527 queue_comp_unit (per_cu, per_objfile, language_minimal);
2528 dwarf2_cu *cu = load_cu (per_cu, per_objfile, skip_partial);
89e63ee4
DE
2529
2530 /* If we just loaded a CU from a DWO, and we're working with an index
2531 that may badly handle TUs, load all the TUs in that DWO as well.
2532 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
2533 if (!per_cu->is_debug_types
1b555f17
SM
2534 && cu != NULL
2535 && cu->dwo_unit != NULL
976ca316
SM
2536 && per_objfile->per_bfd->index_table != NULL
2537 && per_objfile->per_bfd->index_table->version <= 7
89e63ee4 2538 /* DWP files aren't supported yet. */
976ca316 2539 && get_dwp_file (per_objfile) == NULL)
1b555f17 2540 queue_and_load_all_dwo_tus (cu);
95554aad 2541 }
9291a0cd 2542
976ca316 2543 process_queue (per_objfile);
9291a0cd
TT
2544
2545 /* Age the cache, releasing compilation units that have not
2546 been used recently. */
976ca316 2547 per_objfile->age_comp_units ();
9291a0cd
TT
2548}
2549
97a1449a
SM
2550/* Ensure that the symbols for PER_CU have been read in. DWARF2_PER_OBJFILE is
2551 the per-objfile for which this symtab is instantiated.
2552
2553 Returns the resulting symbol table. */
2fdf6df6 2554
43f3e411 2555static struct compunit_symtab *
97a1449a 2556dw2_instantiate_symtab (dwarf2_per_cu_data *per_cu,
976ca316 2557 dwarf2_per_objfile *per_objfile,
97a1449a 2558 bool skip_partial)
9291a0cd 2559{
976ca316 2560 gdb_assert (per_objfile->per_bfd->using_index);
af758d11 2561
976ca316 2562 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd 2563 {
976ca316 2564 free_cached_comp_units freer (per_objfile);
c83dd867 2565 scoped_restore decrementer = increment_reading_symtab ();
976ca316
SM
2566 dw2_do_instantiate_symtab (per_cu, per_objfile, skip_partial);
2567 process_cu_includes (per_objfile);
9291a0cd 2568 }
f194fefb 2569
976ca316 2570 return per_objfile->get_symtab (per_cu);
9291a0cd
TT
2571}
2572
ff4c9fec 2573/* See declaration. */
f4dc4d17 2574
ff4c9fec 2575dwarf2_per_cu_data *
5989a64e 2576dwarf2_per_bfd::get_cutu (int index)
ff4c9fec 2577{
b76e467d 2578 if (index >= this->all_comp_units.size ())
ff4c9fec 2579 {
b76e467d 2580 index -= this->all_comp_units.size ();
b2bdb8cf 2581 gdb_assert (index < this->all_type_units.size ());
ff4c9fec
SM
2582 return &this->all_type_units[index]->per_cu;
2583 }
f4dc4d17 2584
ff4c9fec
SM
2585 return this->all_comp_units[index];
2586}
f4dc4d17 2587
ff4c9fec 2588/* See declaration. */
2fdf6df6 2589
ff4c9fec 2590dwarf2_per_cu_data *
5989a64e 2591dwarf2_per_bfd::get_cu (int index)
1fd400ff 2592{
b76e467d 2593 gdb_assert (index >= 0 && index < this->all_comp_units.size ());
f4dc4d17 2594
ff4c9fec 2595 return this->all_comp_units[index];
f4dc4d17
DE
2596}
2597
ff4c9fec 2598/* See declaration. */
f4dc4d17 2599
ff4c9fec 2600signatured_type *
5989a64e 2601dwarf2_per_bfd::get_tu (int index)
f4dc4d17 2602{
b2bdb8cf 2603 gdb_assert (index >= 0 && index < this->all_type_units.size ());
f4dc4d17 2604
ff4c9fec 2605 return this->all_type_units[index];
1fd400ff
TT
2606}
2607
d3473f0c
TT
2608/* See read.h. */
2609
2610dwarf2_per_cu_data *
5989a64e 2611dwarf2_per_bfd::allocate_per_cu ()
d3473f0c
TT
2612{
2613 dwarf2_per_cu_data *result = OBSTACK_ZALLOC (&obstack, dwarf2_per_cu_data);
1859c670 2614 result->per_bfd = this;
d3473f0c
TT
2615 result->index = m_num_psymtabs++;
2616 return result;
2617}
2618
2619/* See read.h. */
2620
2621signatured_type *
5989a64e 2622dwarf2_per_bfd::allocate_signatured_type ()
d3473f0c
TT
2623{
2624 signatured_type *result = OBSTACK_ZALLOC (&obstack, signatured_type);
1859c670 2625 result->per_cu.per_bfd = this;
d3473f0c
TT
2626 result->per_cu.index = m_num_psymtabs++;
2627 return result;
2628}
2629
168c9250 2630/* Return a new dwarf2_per_cu_data allocated on the per-bfd
45940949 2631 obstack, and constructed with the specified field values. */
4b514bc8
JK
2632
2633static dwarf2_per_cu_data *
168c9250
SM
2634create_cu_from_index_list (dwarf2_per_bfd *per_bfd,
2635 struct dwarf2_section_info *section,
2636 int is_dwz,
2637 sect_offset sect_off, ULONGEST length)
4b514bc8 2638{
168c9250 2639 dwarf2_per_cu_data *the_cu = per_bfd->allocate_per_cu ();
4b514bc8
JK
2640 the_cu->sect_off = sect_off;
2641 the_cu->length = length;
4b514bc8 2642 the_cu->section = section;
168c9250 2643 the_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 2644 struct dwarf2_per_cu_quick_data);
4b514bc8
JK
2645 the_cu->is_dwz = is_dwz;
2646 return the_cu;
2647}
2648
2ec9a5e0
TT
2649/* A helper for create_cus_from_index that handles a given list of
2650 CUs. */
2fdf6df6 2651
74a0d9f6 2652static void
168c9250 2653create_cus_from_index_list (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2654 const gdb_byte *cu_list, offset_type n_elements,
2655 struct dwarf2_section_info *section,
b76e467d 2656 int is_dwz)
9291a0cd 2657{
12359b5e 2658 for (offset_type i = 0; i < n_elements; i += 2)
9291a0cd 2659 {
74a0d9f6 2660 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2661
2662 sect_offset sect_off
2663 = (sect_offset) extract_unsigned_integer (cu_list, 8, BFD_ENDIAN_LITTLE);
2664 ULONGEST length = extract_unsigned_integer (cu_list + 8, 8, BFD_ENDIAN_LITTLE);
9291a0cd
TT
2665 cu_list += 2 * 8;
2666
b76e467d 2667 dwarf2_per_cu_data *per_cu
168c9250
SM
2668 = create_cu_from_index_list (per_bfd, section, is_dwz, sect_off,
2669 length);
2670 per_bfd->all_comp_units.push_back (per_cu);
9291a0cd 2671 }
9291a0cd
TT
2672}
2673
2ec9a5e0 2674/* Read the CU list from the mapped index, and use it to create all
168c9250 2675 the CU objects for PER_BFD. */
2ec9a5e0 2676
74a0d9f6 2677static void
168c9250 2678create_cus_from_index (dwarf2_per_bfd *per_bfd,
2ec9a5e0
TT
2679 const gdb_byte *cu_list, offset_type cu_list_elements,
2680 const gdb_byte *dwz_list, offset_type dwz_elements)
2681{
168c9250
SM
2682 gdb_assert (per_bfd->all_comp_units.empty ());
2683 per_bfd->all_comp_units.reserve ((cu_list_elements + dwz_elements) / 2);
2ec9a5e0 2684
168c9250
SM
2685 create_cus_from_index_list (per_bfd, cu_list, cu_list_elements,
2686 &per_bfd->info, 0);
2ec9a5e0
TT
2687
2688 if (dwz_elements == 0)
74a0d9f6 2689 return;
2ec9a5e0 2690
168c9250
SM
2691 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
2692 create_cus_from_index_list (per_bfd, dwz_list, dwz_elements,
b76e467d 2693 &dwz->info, 1);
2ec9a5e0
TT
2694}
2695
1fd400ff 2696/* Create the signatured type hash table from the index. */
673bfd45 2697
74a0d9f6 2698static void
12359b5e 2699create_signatured_type_table_from_index
168c9250
SM
2700 (dwarf2_per_bfd *per_bfd, struct dwarf2_section_info *section,
2701 const gdb_byte *bytes, offset_type elements)
1fd400ff 2702{
168c9250
SM
2703 gdb_assert (per_bfd->all_type_units.empty ());
2704 per_bfd->all_type_units.reserve (elements / 3);
1fd400ff 2705
298e9637 2706 htab_up sig_types_hash = allocate_signatured_type_table ();
1fd400ff 2707
12359b5e 2708 for (offset_type i = 0; i < elements; i += 3)
1fd400ff 2709 {
52dc124a 2710 struct signatured_type *sig_type;
9c541725 2711 ULONGEST signature;
1fd400ff 2712 void **slot;
9c541725 2713 cu_offset type_offset_in_tu;
1fd400ff 2714
74a0d9f6 2715 gdb_static_assert (sizeof (ULONGEST) >= 8);
9c541725
PA
2716 sect_offset sect_off
2717 = (sect_offset) extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
2718 type_offset_in_tu
2719 = (cu_offset) extract_unsigned_integer (bytes + 8, 8,
2720 BFD_ENDIAN_LITTLE);
1fd400ff
TT
2721 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
2722 bytes += 3 * 8;
2723
168c9250 2724 sig_type = per_bfd->allocate_signatured_type ();
52dc124a 2725 sig_type->signature = signature;
9c541725 2726 sig_type->type_offset_in_tu = type_offset_in_tu;
3019eac3 2727 sig_type->per_cu.is_debug_types = 1;
8a0459fd 2728 sig_type->per_cu.section = section;
9c541725 2729 sig_type->per_cu.sect_off = sect_off;
52dc124a 2730 sig_type->per_cu.v.quick
168c9250 2731 = OBSTACK_ZALLOC (&per_bfd->obstack,
1fd400ff
TT
2732 struct dwarf2_per_cu_quick_data);
2733
b0b6a987 2734 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
52dc124a 2735 *slot = sig_type;
1fd400ff 2736
168c9250 2737 per_bfd->all_type_units.push_back (sig_type);
1fd400ff
TT
2738 }
2739
168c9250 2740 per_bfd->signatured_types = std::move (sig_types_hash);
1fd400ff
TT
2741}
2742
927aa2e7
JK
2743/* Create the signatured type hash table from .debug_names. */
2744
2745static void
2746create_signatured_type_table_from_debug_names
976ca316 2747 (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2748 const mapped_debug_names &map,
2749 struct dwarf2_section_info *section,
2750 struct dwarf2_section_info *abbrev_section)
2751{
976ca316 2752 struct objfile *objfile = per_objfile->objfile;
ed2dc618 2753
96b79293
TT
2754 section->read (objfile);
2755 abbrev_section->read (objfile);
927aa2e7 2756
976ca316
SM
2757 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
2758 per_objfile->per_bfd->all_type_units.reserve (map.tu_count);
927aa2e7 2759
298e9637 2760 htab_up sig_types_hash = allocate_signatured_type_table ();
927aa2e7
JK
2761
2762 for (uint32_t i = 0; i < map.tu_count; ++i)
2763 {
2764 struct signatured_type *sig_type;
927aa2e7 2765 void **slot;
927aa2e7
JK
2766
2767 sect_offset sect_off
2768 = (sect_offset) (extract_unsigned_integer
2769 (map.tu_table_reordered + i * map.offset_size,
2770 map.offset_size,
2771 map.dwarf5_byte_order));
2772
2773 comp_unit_head cu_header;
976ca316 2774 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618 2775 abbrev_section,
927aa2e7
JK
2776 section->buffer + to_underlying (sect_off),
2777 rcuh_kind::TYPE);
2778
976ca316 2779 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
927aa2e7
JK
2780 sig_type->signature = cu_header.signature;
2781 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
2782 sig_type->per_cu.is_debug_types = 1;
2783 sig_type->per_cu.section = section;
2784 sig_type->per_cu.sect_off = sect_off;
927aa2e7 2785 sig_type->per_cu.v.quick
976ca316 2786 = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
927aa2e7
JK
2787 struct dwarf2_per_cu_quick_data);
2788
b0b6a987 2789 slot = htab_find_slot (sig_types_hash.get (), sig_type, INSERT);
927aa2e7
JK
2790 *slot = sig_type;
2791
976ca316 2792 per_objfile->per_bfd->all_type_units.push_back (sig_type);
927aa2e7
JK
2793 }
2794
976ca316 2795 per_objfile->per_bfd->signatured_types = std::move (sig_types_hash);
927aa2e7
JK
2796}
2797
9291a0cd
TT
2798/* Read the address map data from the mapped index, and use it to
2799 populate the objfile's psymtabs_addrmap. */
2fdf6df6 2800
9291a0cd 2801static void
976ca316 2802create_addrmap_from_index (dwarf2_per_objfile *per_objfile,
ed2dc618 2803 struct mapped_index *index)
9291a0cd 2804{
976ca316 2805 struct objfile *objfile = per_objfile->objfile;
08feed99 2806 struct gdbarch *gdbarch = objfile->arch ();
9291a0cd 2807 const gdb_byte *iter, *end;
9291a0cd 2808 struct addrmap *mutable_map;
9291a0cd
TT
2809 CORE_ADDR baseaddr;
2810
8268c778
PA
2811 auto_obstack temp_obstack;
2812
9291a0cd
TT
2813 mutable_map = addrmap_create_mutable (&temp_obstack);
2814
f00a2de2
PA
2815 iter = index->address_table.data ();
2816 end = iter + index->address_table.size ();
9291a0cd 2817
b3b3bada 2818 baseaddr = objfile->text_section_offset ();
9291a0cd
TT
2819
2820 while (iter < end)
2821 {
2822 ULONGEST hi, lo, cu_index;
2823 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2824 iter += 8;
2825 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2826 iter += 8;
2827 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2828 iter += 4;
f652bce2 2829
24a55014 2830 if (lo > hi)
f652bce2 2831 {
b98664d3 2832 complaint (_(".gdb_index address table has invalid range (%s - %s)"),
c0cd8254 2833 hex_string (lo), hex_string (hi));
24a55014 2834 continue;
f652bce2 2835 }
24a55014 2836
976ca316 2837 if (cu_index >= per_objfile->per_bfd->all_comp_units.size ())
f652bce2 2838 {
b98664d3 2839 complaint (_(".gdb_index address table has invalid CU number %u"),
f652bce2 2840 (unsigned) cu_index);
24a55014 2841 continue;
f652bce2 2842 }
24a55014 2843
79748972
TT
2844 lo = gdbarch_adjust_dwarf2_addr (gdbarch, lo + baseaddr) - baseaddr;
2845 hi = gdbarch_adjust_dwarf2_addr (gdbarch, hi + baseaddr) - baseaddr;
ed2dc618 2846 addrmap_set_empty (mutable_map, lo, hi - 1,
976ca316 2847 per_objfile->per_bfd->get_cu (cu_index));
9291a0cd
TT
2848 }
2849
d320c2b5 2850 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 2851 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
9291a0cd
TT
2852}
2853
927aa2e7
JK
2854/* Read the address map data from DWARF-5 .debug_aranges, and use it to
2855 populate the objfile's psymtabs_addrmap. */
2856
2857static void
976ca316 2858create_addrmap_from_aranges (dwarf2_per_objfile *per_objfile,
927aa2e7
JK
2859 struct dwarf2_section_info *section)
2860{
976ca316 2861 struct objfile *objfile = per_objfile->objfile;
927aa2e7 2862 bfd *abfd = objfile->obfd;
08feed99 2863 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 2864 const CORE_ADDR baseaddr = objfile->text_section_offset ();
927aa2e7
JK
2865
2866 auto_obstack temp_obstack;
2867 addrmap *mutable_map = addrmap_create_mutable (&temp_obstack);
2868
2869 std::unordered_map<sect_offset,
2870 dwarf2_per_cu_data *,
2871 gdb::hash_enum<sect_offset>>
2872 debug_info_offset_to_per_cu;
976ca316 2873 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 2874 {
927aa2e7
JK
2875 const auto insertpair
2876 = debug_info_offset_to_per_cu.emplace (per_cu->sect_off, per_cu);
2877 if (!insertpair.second)
2878 {
2879 warning (_("Section .debug_aranges in %s has duplicate "
9d8780f0
SM
2880 "debug_info_offset %s, ignoring .debug_aranges."),
2881 objfile_name (objfile), sect_offset_str (per_cu->sect_off));
927aa2e7
JK
2882 return;
2883 }
2884 }
2885
96b79293 2886 section->read (objfile);
927aa2e7
JK
2887
2888 const bfd_endian dwarf5_byte_order = gdbarch_byte_order (gdbarch);
2889
2890 const gdb_byte *addr = section->buffer;
2891
2892 while (addr < section->buffer + section->size)
2893 {
2894 const gdb_byte *const entry_addr = addr;
2895 unsigned int bytes_read;
2896
2897 const LONGEST entry_length = read_initial_length (abfd, addr,
2898 &bytes_read);
2899 addr += bytes_read;
2900
2901 const gdb_byte *const entry_end = addr + entry_length;
2902 const bool dwarf5_is_dwarf64 = bytes_read != 4;
2903 const uint8_t offset_size = dwarf5_is_dwarf64 ? 8 : 4;
2904 if (addr + entry_length > section->buffer + section->size)
2905 {
47e3f474 2906 warning (_("Section .debug_aranges in %s entry at offset %s "
dda83cd7 2907 "length %s exceeds section length %s, "
927aa2e7 2908 "ignoring .debug_aranges."),
47e3f474
TV
2909 objfile_name (objfile),
2910 plongest (entry_addr - section->buffer),
927aa2e7
JK
2911 plongest (bytes_read + entry_length),
2912 pulongest (section->size));
2913 return;
2914 }
2915
2916 /* The version number. */
2917 const uint16_t version = read_2_bytes (abfd, addr);
2918 addr += 2;
2919 if (version != 2)
2920 {
47e3f474 2921 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2922 "has unsupported version %d, ignoring .debug_aranges."),
47e3f474
TV
2923 objfile_name (objfile),
2924 plongest (entry_addr - section->buffer), version);
927aa2e7
JK
2925 return;
2926 }
2927
2928 const uint64_t debug_info_offset
2929 = extract_unsigned_integer (addr, offset_size, dwarf5_byte_order);
2930 addr += offset_size;
2931 const auto per_cu_it
2932 = debug_info_offset_to_per_cu.find (sect_offset (debug_info_offset));
2933 if (per_cu_it == debug_info_offset_to_per_cu.cend ())
2934 {
47e3f474 2935 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2936 "debug_info_offset %s does not exists, "
2937 "ignoring .debug_aranges."),
47e3f474
TV
2938 objfile_name (objfile),
2939 plongest (entry_addr - section->buffer),
927aa2e7
JK
2940 pulongest (debug_info_offset));
2941 return;
2942 }
2943 dwarf2_per_cu_data *const per_cu = per_cu_it->second;
2944
2945 const uint8_t address_size = *addr++;
2946 if (address_size < 1 || address_size > 8)
2947 {
47e3f474 2948 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2949 "address_size %u is invalid, ignoring .debug_aranges."),
47e3f474
TV
2950 objfile_name (objfile),
2951 plongest (entry_addr - section->buffer), address_size);
927aa2e7
JK
2952 return;
2953 }
2954
2955 const uint8_t segment_selector_size = *addr++;
2956 if (segment_selector_size != 0)
2957 {
47e3f474 2958 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2959 "segment_selector_size %u is not supported, "
2960 "ignoring .debug_aranges."),
47e3f474
TV
2961 objfile_name (objfile),
2962 plongest (entry_addr - section->buffer),
927aa2e7
JK
2963 segment_selector_size);
2964 return;
2965 }
2966
2967 /* Must pad to an alignment boundary that is twice the address
dda83cd7
SM
2968 size. It is undocumented by the DWARF standard but GCC does
2969 use it. */
927aa2e7
JK
2970 for (size_t padding = ((-(addr - section->buffer))
2971 & (2 * address_size - 1));
dda83cd7 2972 padding > 0; padding--)
927aa2e7
JK
2973 if (*addr++ != 0)
2974 {
47e3f474 2975 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7 2976 "padding is not zero, ignoring .debug_aranges."),
47e3f474
TV
2977 objfile_name (objfile),
2978 plongest (entry_addr - section->buffer));
927aa2e7
JK
2979 return;
2980 }
2981
2982 for (;;)
2983 {
2984 if (addr + 2 * address_size > entry_end)
2985 {
47e3f474 2986 warning (_("Section .debug_aranges in %s entry at offset %s "
927aa2e7
JK
2987 "address list is not properly terminated, "
2988 "ignoring .debug_aranges."),
47e3f474
TV
2989 objfile_name (objfile),
2990 plongest (entry_addr - section->buffer));
927aa2e7
JK
2991 return;
2992 }
2993 ULONGEST start = extract_unsigned_integer (addr, address_size,
2994 dwarf5_byte_order);
2995 addr += address_size;
2996 ULONGEST length = extract_unsigned_integer (addr, address_size,
2997 dwarf5_byte_order);
2998 addr += address_size;
2999 if (start == 0 && length == 0)
3000 break;
976ca316 3001 if (start == 0 && !per_objfile->per_bfd->has_section_at_zero)
927aa2e7
JK
3002 {
3003 /* Symbol was eliminated due to a COMDAT group. */
3004 continue;
3005 }
3006 ULONGEST end = start + length;
79748972
TT
3007 start = (gdbarch_adjust_dwarf2_addr (gdbarch, start + baseaddr)
3008 - baseaddr);
3009 end = (gdbarch_adjust_dwarf2_addr (gdbarch, end + baseaddr)
3010 - baseaddr);
927aa2e7
JK
3011 addrmap_set_empty (mutable_map, start, end - 1, per_cu);
3012 }
3013 }
3014
d320c2b5 3015 objfile->partial_symtabs->psymtabs_addrmap
5923a04c 3016 = addrmap_create_fixed (mutable_map, objfile->partial_symtabs->obstack ());
927aa2e7
JK
3017}
3018
9291a0cd
TT
3019/* Find a slot in the mapped index INDEX for the object named NAME.
3020 If NAME is found, set *VEC_OUT to point to the CU vector in the
109483d9
PA
3021 constant pool and return true. If NAME cannot be found, return
3022 false. */
2fdf6df6 3023
109483d9 3024static bool
9291a0cd
TT
3025find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
3026 offset_type **vec_out)
3027{
0cf03b49 3028 offset_type hash;
9291a0cd 3029 offset_type slot, step;
559a7a62 3030 int (*cmp) (const char *, const char *);
9291a0cd 3031
791afaa2 3032 gdb::unique_xmalloc_ptr<char> without_params;
0cf03b49 3033 if (current_language->la_language == language_cplus
45280282
IB
3034 || current_language->la_language == language_fortran
3035 || current_language->la_language == language_d)
0cf03b49
JK
3036 {
3037 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
3038 not contain any. */
a8719064 3039
72998fb3 3040 if (strchr (name, '(') != NULL)
0cf03b49 3041 {
109483d9 3042 without_params = cp_remove_params (name);
0cf03b49 3043
72998fb3 3044 if (without_params != NULL)
791afaa2 3045 name = without_params.get ();
0cf03b49
JK
3046 }
3047 }
3048
559a7a62 3049 /* Index version 4 did not support case insensitive searches. But the
feea76c2 3050 indices for case insensitive languages are built in lowercase, therefore
559a7a62
JK
3051 simulate our NAME being searched is also lowercased. */
3052 hash = mapped_index_string_hash ((index->version == 4
dda83cd7 3053 && case_sensitivity == case_sensitive_off
559a7a62
JK
3054 ? 5 : index->version),
3055 name);
3056
f00a2de2
PA
3057 slot = hash & (index->symbol_table.size () - 1);
3058 step = ((hash * 17) & (index->symbol_table.size () - 1)) | 1;
559a7a62 3059 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
9291a0cd
TT
3060
3061 for (;;)
3062 {
9291a0cd 3063 const char *str;
f00a2de2
PA
3064
3065 const auto &bucket = index->symbol_table[slot];
3066 if (bucket.name == 0 && bucket.vec == 0)
109483d9 3067 return false;
9291a0cd 3068
f00a2de2 3069 str = index->constant_pool + MAYBE_SWAP (bucket.name);
559a7a62 3070 if (!cmp (name, str))
9291a0cd
TT
3071 {
3072 *vec_out = (offset_type *) (index->constant_pool
f00a2de2 3073 + MAYBE_SWAP (bucket.vec));
109483d9 3074 return true;
9291a0cd
TT
3075 }
3076
f00a2de2 3077 slot = (slot + step) & (index->symbol_table.size () - 1);
9291a0cd
TT
3078 }
3079}
3080
4485a1c1
SM
3081/* A helper function that reads the .gdb_index from BUFFER and fills
3082 in MAP. FILENAME is the name of the file containing the data;
d33bc52e 3083 it is used for error reporting. DEPRECATED_OK is true if it is
2ec9a5e0
TT
3084 ok to use deprecated sections.
3085
3086 CU_LIST, CU_LIST_ELEMENTS, TYPES_LIST, and TYPES_LIST_ELEMENTS are
3087 out parameters that are filled in with information about the CU and
3088 TU lists in the section.
3089
4485a1c1 3090 Returns true if all went well, false otherwise. */
2fdf6df6 3091
d33bc52e 3092static bool
3810f182 3093read_gdb_index_from_buffer (const char *filename,
4485a1c1
SM
3094 bool deprecated_ok,
3095 gdb::array_view<const gdb_byte> buffer,
3096 struct mapped_index *map,
3097 const gdb_byte **cu_list,
3098 offset_type *cu_list_elements,
3099 const gdb_byte **types_list,
3100 offset_type *types_list_elements)
3101{
3102 const gdb_byte *addr = &buffer[0];
82430852 3103
9291a0cd 3104 /* Version check. */
4485a1c1 3105 offset_type version = MAYBE_SWAP (*(offset_type *) addr);
987d643c 3106 /* Versions earlier than 3 emitted every copy of a psymbol. This
a6e293d1 3107 causes the index to behave very poorly for certain requests. Version 3
831adc1f 3108 contained incomplete addrmap. So, it seems better to just ignore such
481860b3 3109 indices. */
831adc1f 3110 if (version < 4)
481860b3
GB
3111 {
3112 static int warning_printed = 0;
3113 if (!warning_printed)
3114 {
3115 warning (_("Skipping obsolete .gdb_index section in %s."),
2ec9a5e0 3116 filename);
481860b3
GB
3117 warning_printed = 1;
3118 }
3119 return 0;
3120 }
3121 /* Index version 4 uses a different hash function than index version
3122 5 and later.
3123
3124 Versions earlier than 6 did not emit psymbols for inlined
3125 functions. Using these files will cause GDB not to be able to
3126 set breakpoints on inlined functions by name, so we ignore these
e615022a
DE
3127 indices unless the user has done
3128 "set use-deprecated-index-sections on". */
2ec9a5e0 3129 if (version < 6 && !deprecated_ok)
481860b3
GB
3130 {
3131 static int warning_printed = 0;
3132 if (!warning_printed)
3133 {
e615022a
DE
3134 warning (_("\
3135Skipping deprecated .gdb_index section in %s.\n\
3136Do \"set use-deprecated-index-sections on\" before the file is read\n\
3137to use the section anyway."),
2ec9a5e0 3138 filename);
481860b3
GB
3139 warning_printed = 1;
3140 }
3141 return 0;
3142 }
796a7ff8 3143 /* Version 7 indices generated by gold refer to the CU for a symbol instead
8943b874
DE
3144 of the TU (for symbols coming from TUs),
3145 http://sourceware.org/bugzilla/show_bug.cgi?id=15021.
3146 Plus gold-generated indices can have duplicate entries for global symbols,
3147 http://sourceware.org/bugzilla/show_bug.cgi?id=15646.
3148 These are just performance bugs, and we can't distinguish gdb-generated
3149 indices from gold-generated ones, so issue no warning here. */
796a7ff8 3150
481860b3 3151 /* Indexes with higher version than the one supported by GDB may be no
594e8718 3152 longer backward compatible. */
796a7ff8 3153 if (version > 8)
594e8718 3154 return 0;
9291a0cd 3155
559a7a62 3156 map->version = version;
9291a0cd 3157
4485a1c1 3158 offset_type *metadata = (offset_type *) (addr + sizeof (offset_type));
1fd400ff 3159
4485a1c1 3160 int i = 0;
2ec9a5e0
TT
3161 *cu_list = addr + MAYBE_SWAP (metadata[i]);
3162 *cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
3163 / 8);
1fd400ff
TT
3164 ++i;
3165
2ec9a5e0
TT
3166 *types_list = addr + MAYBE_SWAP (metadata[i]);
3167 *types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
3168 - MAYBE_SWAP (metadata[i]))
3169 / 8);
987d643c 3170 ++i;
1fd400ff 3171
f00a2de2
PA
3172 const gdb_byte *address_table = addr + MAYBE_SWAP (metadata[i]);
3173 const gdb_byte *address_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3174 map->address_table
3175 = gdb::array_view<const gdb_byte> (address_table, address_table_end);
1fd400ff
TT
3176 ++i;
3177
f00a2de2
PA
3178 const gdb_byte *symbol_table = addr + MAYBE_SWAP (metadata[i]);
3179 const gdb_byte *symbol_table_end = addr + MAYBE_SWAP (metadata[i + 1]);
3180 map->symbol_table
3181 = gdb::array_view<mapped_index::symbol_table_slot>
3182 ((mapped_index::symbol_table_slot *) symbol_table,
3183 (mapped_index::symbol_table_slot *) symbol_table_end);
9291a0cd 3184
f00a2de2 3185 ++i;
f9d83a0b 3186 map->constant_pool = (char *) (addr + MAYBE_SWAP (metadata[i]));
1fd400ff 3187
2ec9a5e0
TT
3188 return 1;
3189}
3190
4485a1c1
SM
3191/* Callback types for dwarf2_read_gdb_index. */
3192
3193typedef gdb::function_view
5989a64e 3194 <gdb::array_view<const gdb_byte>(objfile *, dwarf2_per_bfd *)>
4485a1c1
SM
3195 get_gdb_index_contents_ftype;
3196typedef gdb::function_view
3197 <gdb::array_view<const gdb_byte>(objfile *, dwz_file *)>
3198 get_gdb_index_contents_dwz_ftype;
3199
927aa2e7 3200/* Read .gdb_index. If everything went ok, initialize the "quick"
2ec9a5e0
TT
3201 elements of all the CUs and return 1. Otherwise, return 0. */
3202
3203static int
4485a1c1 3204dwarf2_read_gdb_index
976ca316 3205 (dwarf2_per_objfile *per_objfile,
4485a1c1
SM
3206 get_gdb_index_contents_ftype get_gdb_index_contents,
3207 get_gdb_index_contents_dwz_ftype get_gdb_index_contents_dwz)
2ec9a5e0 3208{
2ec9a5e0
TT
3209 const gdb_byte *cu_list, *types_list, *dwz_list = NULL;
3210 offset_type cu_list_elements, types_list_elements, dwz_list_elements = 0;
4db1a1dc 3211 struct dwz_file *dwz;
976ca316 3212 struct objfile *objfile = per_objfile->objfile;
f8c41851 3213 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
2ec9a5e0 3214
4485a1c1 3215 gdb::array_view<const gdb_byte> main_index_contents
f8c41851 3216 = get_gdb_index_contents (objfile, per_bfd);
4485a1c1
SM
3217
3218 if (main_index_contents.empty ())
3219 return 0;
3220
3063847f 3221 std::unique_ptr<struct mapped_index> map (new struct mapped_index);
3810f182 3222 if (!read_gdb_index_from_buffer (objfile_name (objfile),
4485a1c1
SM
3223 use_deprecated_index_sections,
3224 main_index_contents, map.get (), &cu_list,
3225 &cu_list_elements, &types_list,
3226 &types_list_elements))
2ec9a5e0
TT
3227 return 0;
3228
0fefef59 3229 /* Don't use the index if it's empty. */
3063847f 3230 if (map->symbol_table.empty ())
0fefef59
DE
3231 return 0;
3232
2ec9a5e0
TT
3233 /* If there is a .dwz file, read it so we can get its CU list as
3234 well. */
f8c41851 3235 dwz = dwarf2_get_dwz_file (per_bfd);
4db1a1dc 3236 if (dwz != NULL)
2ec9a5e0 3237 {
2ec9a5e0
TT
3238 struct mapped_index dwz_map;
3239 const gdb_byte *dwz_types_ignore;
3240 offset_type dwz_types_elements_ignore;
3241
4485a1c1
SM
3242 gdb::array_view<const gdb_byte> dwz_index_content
3243 = get_gdb_index_contents_dwz (objfile, dwz);
3244
3245 if (dwz_index_content.empty ())
3246 return 0;
3247
3810f182 3248 if (!read_gdb_index_from_buffer (bfd_get_filename (dwz->dwz_bfd.get ()),
00f93c44 3249 1, dwz_index_content, &dwz_map,
4485a1c1
SM
3250 &dwz_list, &dwz_list_elements,
3251 &dwz_types_ignore,
3252 &dwz_types_elements_ignore))
2ec9a5e0
TT
3253 {
3254 warning (_("could not read '.gdb_index' section from %s; skipping"),
00f93c44 3255 bfd_get_filename (dwz->dwz_bfd.get ()));
2ec9a5e0
TT
3256 return 0;
3257 }
3258 }
3259
f8c41851
SM
3260 create_cus_from_index (per_bfd, cu_list, cu_list_elements, dwz_list,
3261 dwz_list_elements);
1fd400ff 3262
8b70b953
TT
3263 if (types_list_elements)
3264 {
8b70b953
TT
3265 /* We can only handle a single .debug_types when we have an
3266 index. */
f8c41851 3267 if (per_bfd->types.size () != 1)
8b70b953
TT
3268 return 0;
3269
f8c41851 3270 dwarf2_section_info *section = &per_bfd->types[0];
8b70b953 3271
f8c41851 3272 create_signatured_type_table_from_index (per_bfd, section, types_list,
168c9250 3273 types_list_elements);
8b70b953 3274 }
9291a0cd 3275
976ca316 3276 create_addrmap_from_index (per_objfile, map.get ());
9291a0cd 3277
f8c41851
SM
3278 per_bfd->index_table = std::move (map);
3279 per_bfd->using_index = 1;
3280 per_bfd->quick_file_names_table =
3281 create_quick_file_names_table (per_bfd->all_comp_units.size ());
3282
3283 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
3284 objfiles using the same BFD. */
3285 gdb_assert (per_bfd->partial_symtabs == nullptr);
3286 per_bfd->partial_symtabs = objfile->partial_symtabs;
9291a0cd
TT
3287
3288 return 1;
3289}
3290
dee91e82 3291/* die_reader_func for dw2_get_file_names. */
2fdf6df6 3292
dee91e82
DE
3293static void
3294dw2_get_file_names_reader (const struct die_reader_specs *reader,
d521ce57 3295 const gdb_byte *info_ptr,
3e225074 3296 struct die_info *comp_unit_die)
9291a0cd 3297{
dee91e82 3298 struct dwarf2_cu *cu = reader->cu;
ed2dc618 3299 struct dwarf2_per_cu_data *this_cu = cu->per_cu;
976ca316 3300 dwarf2_per_objfile *per_objfile = cu->per_objfile;
094b34ac 3301 struct dwarf2_per_cu_data *lh_cu;
9291a0cd 3302 struct attribute *attr;
7b9f3c50
DE
3303 void **slot;
3304 struct quick_file_names *qfn;
9291a0cd 3305
0186c6a7
DE
3306 gdb_assert (! this_cu->is_debug_types);
3307
07261596
TT
3308 /* Our callers never want to match partial units -- instead they
3309 will match the enclosing full CU. */
3310 if (comp_unit_die->tag == DW_TAG_partial_unit)
3311 {
3312 this_cu->v.quick->no_file_data = 1;
3313 return;
3314 }
3315
0186c6a7 3316 lh_cu = this_cu;
7b9f3c50 3317 slot = NULL;
dee91e82 3318
fff8551c 3319 line_header_up lh;
9c541725 3320 sect_offset line_offset {};
fff8551c 3321
dee91e82 3322 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, cu);
d4df075e 3323 if (attr != nullptr && attr->form_is_unsigned ())
9291a0cd 3324 {
7b9f3c50
DE
3325 struct quick_file_names find_entry;
3326
d4df075e 3327 line_offset = (sect_offset) attr->as_unsigned ();
7b9f3c50
DE
3328
3329 /* We may have already read in this line header (TU line header sharing).
3330 If we have we're done. */
094b34ac 3331 find_entry.hash.dwo_unit = cu->dwo_unit;
9c541725 3332 find_entry.hash.line_sect_off = line_offset;
976ca316 3333 slot = htab_find_slot (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50
DE
3334 &find_entry, INSERT);
3335 if (*slot != NULL)
3336 {
9a3c8263 3337 lh_cu->v.quick->file_names = (struct quick_file_names *) *slot;
dee91e82 3338 return;
7b9f3c50
DE
3339 }
3340
3019eac3 3341 lh = dwarf_decode_line_header (line_offset, cu);
9291a0cd
TT
3342 }
3343 if (lh == NULL)
3344 {
094b34ac 3345 lh_cu->v.quick->no_file_data = 1;
dee91e82 3346 return;
9291a0cd
TT
3347 }
3348
976ca316 3349 qfn = XOBNEW (&per_objfile->per_bfd->obstack, struct quick_file_names);
094b34ac 3350 qfn->hash.dwo_unit = cu->dwo_unit;
9c541725 3351 qfn->hash.line_sect_off = line_offset;
7b9f3c50
DE
3352 gdb_assert (slot != NULL);
3353 *slot = qfn;
9291a0cd 3354
d721ba37 3355 file_and_directory fnd = find_file_and_directory (comp_unit_die, cu);
9291a0cd 3356
aa391654
TT
3357 int offset = 0;
3358 if (strcmp (fnd.name, "<unknown>") != 0)
3359 ++offset;
3360
7ba99d21 3361 qfn->num_file_names = offset + lh->file_names_size ();
8d749320 3362 qfn->file_names =
976ca316 3363 XOBNEWVEC (&per_objfile->per_bfd->obstack, const char *,
45940949 3364 qfn->num_file_names);
aa391654
TT
3365 if (offset != 0)
3366 qfn->file_names[0] = xstrdup (fnd.name);
7ba99d21 3367 for (int i = 0; i < lh->file_names_size (); ++i)
03075812
TT
3368 qfn->file_names[i + offset] = lh->file_full_name (i + 1,
3369 fnd.comp_dir).release ();
7b9f3c50 3370 qfn->real_names = NULL;
9291a0cd 3371
094b34ac 3372 lh_cu->v.quick->file_names = qfn;
dee91e82
DE
3373}
3374
3375/* A helper for the "quick" functions which attempts to read the line
3376 table for THIS_CU. */
3377
3378static struct quick_file_names *
ab432490
SM
3379dw2_get_file_names (dwarf2_per_cu_data *this_cu,
3380 dwarf2_per_objfile *per_objfile)
dee91e82 3381{
0186c6a7
DE
3382 /* This should never be called for TUs. */
3383 gdb_assert (! this_cu->is_debug_types);
3384 /* Nor type unit groups. */
197400e8 3385 gdb_assert (! this_cu->type_unit_group_p ());
f4dc4d17 3386
dee91e82
DE
3387 if (this_cu->v.quick->file_names != NULL)
3388 return this_cu->v.quick->file_names;
3389 /* If we know there is no line data, no point in looking again. */
3390 if (this_cu->v.quick->no_file_data)
3391 return NULL;
3392
ab432490 3393 cutu_reader reader (this_cu, per_objfile);
c0ab21c2 3394 if (!reader.dummy_p)
3e225074 3395 dw2_get_file_names_reader (&reader, reader.info_ptr, reader.comp_unit_die);
dee91e82
DE
3396
3397 if (this_cu->v.quick->no_file_data)
3398 return NULL;
3399 return this_cu->v.quick->file_names;
9291a0cd
TT
3400}
3401
3402/* A helper for the "quick" functions which computes and caches the
7b9f3c50 3403 real path for a given file name from the line table. */
2fdf6df6 3404
9291a0cd 3405static const char *
976ca316 3406dw2_get_real_path (dwarf2_per_objfile *per_objfile,
7b9f3c50 3407 struct quick_file_names *qfn, int index)
9291a0cd 3408{
7b9f3c50 3409 if (qfn->real_names == NULL)
976ca316 3410 qfn->real_names = OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
26f2dc30 3411 qfn->num_file_names, const char *);
9291a0cd 3412
7b9f3c50 3413 if (qfn->real_names[index] == NULL)
14278e1f 3414 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]).release ();
9291a0cd 3415
7b9f3c50 3416 return qfn->real_names[index];
9291a0cd
TT
3417}
3418
3419static struct symtab *
3420dw2_find_last_source_symtab (struct objfile *objfile)
3421{
976ca316
SM
3422 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3423 dwarf2_per_cu_data *dwarf_cu = per_objfile->per_bfd->all_comp_units.back ();
3424 compunit_symtab *cust = dw2_instantiate_symtab (dwarf_cu, per_objfile, false);
ae2de4f8 3425
43f3e411
DE
3426 if (cust == NULL)
3427 return NULL;
ed2dc618 3428
43f3e411 3429 return compunit_primary_filetab (cust);
9291a0cd
TT
3430}
3431
7b9f3c50
DE
3432/* Traversal function for dw2_forget_cached_source_info. */
3433
3434static int
3435dw2_free_cached_file_names (void **slot, void *info)
9291a0cd 3436{
7b9f3c50 3437 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
9291a0cd 3438
7b9f3c50 3439 if (file_data->real_names)
9291a0cd 3440 {
7b9f3c50 3441 int i;
9291a0cd 3442
7b9f3c50 3443 for (i = 0; i < file_data->num_file_names; ++i)
9291a0cd 3444 {
7b9f3c50
DE
3445 xfree ((void*) file_data->real_names[i]);
3446 file_data->real_names[i] = NULL;
9291a0cd
TT
3447 }
3448 }
7b9f3c50
DE
3449
3450 return 1;
3451}
3452
3453static void
3454dw2_forget_cached_source_info (struct objfile *objfile)
3455{
976ca316 3456 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
7b9f3c50 3457
976ca316 3458 htab_traverse_noresize (per_objfile->per_bfd->quick_file_names_table.get (),
7b9f3c50 3459 dw2_free_cached_file_names, NULL);
9291a0cd
TT
3460}
3461
f8eba3c6
TT
3462/* Helper function for dw2_map_symtabs_matching_filename that expands
3463 the symtabs and calls the iterator. */
3464
3465static int
3466dw2_map_expand_apply (struct objfile *objfile,
3467 struct dwarf2_per_cu_data *per_cu,
f5b95b50 3468 const char *name, const char *real_path,
14bc53a8 3469 gdb::function_view<bool (symtab *)> callback)
f8eba3c6 3470{
43f3e411 3471 struct compunit_symtab *last_made = objfile->compunit_symtabs;
f8eba3c6
TT
3472
3473 /* Don't visit already-expanded CUs. */
af758d11
SM
3474 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3475 if (per_objfile->symtab_set_p (per_cu))
f8eba3c6
TT
3476 return 0;
3477
3478 /* This may expand more than one symtab, and we want to iterate over
3479 all of them. */
97a1449a 3480 dw2_instantiate_symtab (per_cu, per_objfile, false);
f8eba3c6 3481
14bc53a8
PA
3482 return iterate_over_some_symtabs (name, real_path, objfile->compunit_symtabs,
3483 last_made, callback);
f8eba3c6
TT
3484}
3485
3486/* Implementation of the map_symtabs_matching_filename method. */
3487
14bc53a8
PA
3488static bool
3489dw2_map_symtabs_matching_filename
3490 (struct objfile *objfile, const char *name, const char *real_path,
3491 gdb::function_view<bool (symtab *)> callback)
9291a0cd 3492{
c011a4f4 3493 const char *name_basename = lbasename (name);
976ca316 3494 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 3495
848e3e78
DE
3496 /* The rule is CUs specify all the files, including those used by
3497 any TU, so there's no need to scan TUs here. */
f4dc4d17 3498
976ca316 3499 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3500 {
3d7bb9d9 3501 /* We only need to look at symtabs not already expanded. */
976ca316 3502 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3503 continue;
3504
976ca316 3505 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3506 if (file_data == NULL)
9291a0cd
TT
3507 continue;
3508
b76e467d 3509 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3510 {
7b9f3c50 3511 const char *this_name = file_data->file_names[j];
da235a7c 3512 const char *this_real_name;
9291a0cd 3513
af529f8f 3514 if (compare_filenames_for_search (this_name, name))
9291a0cd 3515 {
f5b95b50 3516 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3517 callback))
3518 return true;
288e77a7 3519 continue;
4aac40c8 3520 }
9291a0cd 3521
c011a4f4
DE
3522 /* Before we invoke realpath, which can get expensive when many
3523 files are involved, do a quick comparison of the basenames. */
3524 if (! basenames_may_differ
3525 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
3526 continue;
3527
976ca316 3528 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
da235a7c 3529 if (compare_filenames_for_search (this_real_name, name))
9291a0cd 3530 {
da235a7c 3531 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3532 callback))
3533 return true;
288e77a7 3534 continue;
da235a7c 3535 }
9291a0cd 3536
da235a7c
JK
3537 if (real_path != NULL)
3538 {
af529f8f
JK
3539 gdb_assert (IS_ABSOLUTE_PATH (real_path));
3540 gdb_assert (IS_ABSOLUTE_PATH (name));
7b9f3c50 3541 if (this_real_name != NULL
af529f8f 3542 && FILENAME_CMP (real_path, this_real_name) == 0)
9291a0cd 3543 {
f5b95b50 3544 if (dw2_map_expand_apply (objfile, per_cu, name, real_path,
14bc53a8
PA
3545 callback))
3546 return true;
288e77a7 3547 continue;
9291a0cd
TT
3548 }
3549 }
3550 }
3551 }
3552
14bc53a8 3553 return false;
9291a0cd
TT
3554}
3555
da51c347
DE
3556/* Struct used to manage iterating over all CUs looking for a symbol. */
3557
3558struct dw2_symtab_iterator
9291a0cd 3559{
ed2dc618 3560 /* The dwarf2_per_objfile owning the CUs we are iterating on. */
976ca316 3561 dwarf2_per_objfile *per_objfile;
2b79f376
SM
3562 /* If set, only look for symbols that match that block. Valid values are
3563 GLOBAL_BLOCK and STATIC_BLOCK. */
c7f839cb 3564 gdb::optional<block_enum> block_index;
da51c347
DE
3565 /* The kind of symbol we're looking for. */
3566 domain_enum domain;
3567 /* The list of CUs from the index entry of the symbol,
3568 or NULL if not found. */
3569 offset_type *vec;
3570 /* The next element in VEC to look at. */
3571 int next;
3572 /* The number of elements in VEC, or zero if there is no match. */
3573 int length;
8943b874
DE
3574 /* Have we seen a global version of the symbol?
3575 If so we can ignore all further global instances.
3576 This is to work around gold/15646, inefficient gold-generated
3577 indices. */
3578 int global_seen;
da51c347 3579};
9291a0cd 3580
e5f3ece2 3581/* Initialize the index symtab iterator ITER, common part. */
2fdf6df6 3582
9291a0cd 3583static void
e5f3ece2
TV
3584dw2_symtab_iter_init_common (struct dw2_symtab_iterator *iter,
3585 dwarf2_per_objfile *per_objfile,
3586 gdb::optional<block_enum> block_index,
3587 domain_enum domain)
da51c347 3588{
976ca316 3589 iter->per_objfile = per_objfile;
da51c347
DE
3590 iter->block_index = block_index;
3591 iter->domain = domain;
3592 iter->next = 0;
8943b874 3593 iter->global_seen = 0;
e5f3ece2
TV
3594 iter->vec = NULL;
3595 iter->length = 0;
3596}
da51c347 3597
e5f3ece2
TV
3598/* Initialize the index symtab iterator ITER, const char *NAME variant. */
3599
3600static void
3601dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3602 dwarf2_per_objfile *per_objfile,
3603 gdb::optional<block_enum> block_index,
3604 domain_enum domain,
3605 const char *name)
3606{
3607 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
ed2dc618 3608
e5f3ece2 3609 mapped_index *index = per_objfile->per_bfd->index_table.get ();
ed2dc618 3610 /* index is NULL if OBJF_READNOW. */
e5f3ece2
TV
3611 if (index == NULL)
3612 return;
3613
3614 if (find_slot_in_mapped_hash (index, name, &iter->vec))
da51c347 3615 iter->length = MAYBE_SWAP (*iter->vec);
e5f3ece2
TV
3616}
3617
3618/* Initialize the index symtab iterator ITER, offset_type NAMEI variant. */
3619
3620static void
3621dw2_symtab_iter_init (struct dw2_symtab_iterator *iter,
3622 dwarf2_per_objfile *per_objfile,
3623 gdb::optional<block_enum> block_index,
3624 domain_enum domain, offset_type namei)
3625{
3626 dw2_symtab_iter_init_common (iter, per_objfile, block_index, domain);
3627
3628 mapped_index *index = per_objfile->per_bfd->index_table.get ();
3629 /* index is NULL if OBJF_READNOW. */
3630 if (index == NULL)
3631 return;
3632
3633 gdb_assert (!index->symbol_name_slot_invalid (namei));
3634 const auto &bucket = index->symbol_table[namei];
3635
3636 iter->vec = (offset_type *) (index->constant_pool
3637 + MAYBE_SWAP (bucket.vec));
3638 iter->length = MAYBE_SWAP (*iter->vec);
da51c347
DE
3639}
3640
3641/* Return the next matching CU or NULL if there are no more. */
3642
3643static struct dwarf2_per_cu_data *
3644dw2_symtab_iter_next (struct dw2_symtab_iterator *iter)
3645{
976ca316 3646 dwarf2_per_objfile *per_objfile = iter->per_objfile;
ed2dc618 3647
da51c347
DE
3648 for ( ; iter->next < iter->length; ++iter->next)
3649 {
3650 offset_type cu_index_and_attrs =
3651 MAYBE_SWAP (iter->vec[iter->next + 1]);
3652 offset_type cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
da51c347
DE
3653 gdb_index_symbol_kind symbol_kind =
3654 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
3655 /* Only check the symbol attributes if they're present.
3656 Indices prior to version 7 don't record them,
3657 and indices >= 7 may elide them for certain symbols
3658 (gold does this). */
3659 int attrs_valid =
976ca316 3660 (per_objfile->per_bfd->index_table->version >= 7
da51c347
DE
3661 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
3662
3190f0c6 3663 /* Don't crash on bad data. */
976ca316
SM
3664 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
3665 + per_objfile->per_bfd->all_type_units.size ()))
3190f0c6 3666 {
b98664d3 3667 complaint (_(".gdb_index entry has bad CU index"
976ca316 3668 " [in module %s]"), objfile_name (per_objfile->objfile));
3190f0c6
DE
3669 continue;
3670 }
3671
976ca316 3672 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
3190f0c6 3673
da51c347 3674 /* Skip if already read in. */
976ca316 3675 if (per_objfile->symtab_set_p (per_cu))
da51c347
DE
3676 continue;
3677
8943b874
DE
3678 /* Check static vs global. */
3679 if (attrs_valid)
3680 {
2b79f376
SM
3681 bool is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
3682
3683 if (iter->block_index.has_value ())
3684 {
3685 bool want_static = *iter->block_index == STATIC_BLOCK;
3686
3687 if (is_static != want_static)
3688 continue;
3689 }
3690
8943b874 3691 /* Work around gold/15646. */
f030440d
TV
3692 if (!is_static
3693 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
3694 {
3695 if (iter->global_seen)
3696 continue;
3697
3698 iter->global_seen = 1;
3699 }
8943b874 3700 }
da51c347
DE
3701
3702 /* Only check the symbol's kind if it has one. */
3703 if (attrs_valid)
3704 {
3705 switch (iter->domain)
3706 {
3707 case VAR_DOMAIN:
3708 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE
3709 && symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION
3710 /* Some types are also in VAR_DOMAIN. */
3711 && symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3712 continue;
3713 break;
3714 case STRUCT_DOMAIN:
3715 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
3716 continue;
3717 break;
3718 case LABEL_DOMAIN:
3719 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3720 continue;
3721 break;
59c35742
AB
3722 case MODULE_DOMAIN:
3723 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
3724 continue;
3725 break;
da51c347
DE
3726 default:
3727 break;
3728 }
3729 }
3730
3731 ++iter->next;
3732 return per_cu;
3733 }
3734
3735 return NULL;
3736}
3737
43f3e411 3738static struct compunit_symtab *
c7f839cb 3739dw2_lookup_symbol (struct objfile *objfile, block_enum block_index,
da51c347 3740 const char *name, domain_enum domain)
9291a0cd 3741{
43f3e411 3742 struct compunit_symtab *stab_best = NULL;
976ca316 3743 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 3744
b5ec771e
PA
3745 lookup_name_info lookup_name (name, symbol_name_match_type::FULL);
3746
ed2dc618
SM
3747 struct dw2_symtab_iterator iter;
3748 struct dwarf2_per_cu_data *per_cu;
da51c347 3749
976ca316 3750 dw2_symtab_iter_init (&iter, per_objfile, block_index, domain, name);
9291a0cd 3751
ed2dc618
SM
3752 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
3753 {
3754 struct symbol *sym, *with_opaque = NULL;
97a1449a 3755 struct compunit_symtab *stab
976ca316 3756 = dw2_instantiate_symtab (per_cu, per_objfile, false);
ed2dc618 3757 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 3758 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
da51c347 3759
ed2dc618
SM
3760 sym = block_find_symbol (block, name, domain,
3761 block_find_non_opaque_type_preferred,
3762 &with_opaque);
b2e2f908 3763
ed2dc618
SM
3764 /* Some caution must be observed with overloaded functions
3765 and methods, since the index will not contain any overload
3766 information (but NAME might contain it). */
da51c347 3767
ed2dc618
SM
3768 if (sym != NULL
3769 && SYMBOL_MATCHES_SEARCH_NAME (sym, lookup_name))
3770 return stab;
3771 if (with_opaque != NULL
3772 && SYMBOL_MATCHES_SEARCH_NAME (with_opaque, lookup_name))
3773 stab_best = stab;
da51c347 3774
ed2dc618 3775 /* Keep looking through other CUs. */
9291a0cd 3776 }
9291a0cd 3777
da51c347 3778 return stab_best;
9291a0cd
TT
3779}
3780
3781static void
3782dw2_print_stats (struct objfile *objfile)
3783{
976ca316
SM
3784 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3785 int total = (per_objfile->per_bfd->all_comp_units.size ()
3786 + per_objfile->per_bfd->all_type_units.size ());
ed2dc618 3787 int count = 0;
9291a0cd 3788
ed2dc618 3789 for (int i = 0; i < total; ++i)
9291a0cd 3790 {
976ca316 3791 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3792
976ca316 3793 if (!per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3794 ++count;
3795 }
e4a48d9d 3796 printf_filtered (_(" Number of read CUs: %d\n"), total - count);
9291a0cd
TT
3797 printf_filtered (_(" Number of unread CUs: %d\n"), count);
3798}
3799
779bd270
DE
3800/* This dumps minimal information about the index.
3801 It is called via "mt print objfiles".
3802 One use is to verify .gdb_index has been loaded by the
3803 gdb.dwarf2/gdb-index.exp testcase. */
3804
9291a0cd
TT
3805static void
3806dw2_dump (struct objfile *objfile)
3807{
976ca316 3808 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 3809
976ca316 3810 gdb_assert (per_objfile->per_bfd->using_index);
779bd270 3811 printf_filtered (".gdb_index:");
976ca316 3812 if (per_objfile->per_bfd->index_table != NULL)
779bd270
DE
3813 {
3814 printf_filtered (" version %d\n",
976ca316 3815 per_objfile->per_bfd->index_table->version);
779bd270
DE
3816 }
3817 else
3818 printf_filtered (" faked for \"readnow\"\n");
3819 printf_filtered ("\n");
9291a0cd
TT
3820}
3821
9291a0cd
TT
3822static void
3823dw2_expand_symtabs_for_function (struct objfile *objfile,
3824 const char *func_name)
3825{
976ca316 3826 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
da51c347 3827
ed2dc618
SM
3828 struct dw2_symtab_iterator iter;
3829 struct dwarf2_per_cu_data *per_cu;
da51c347 3830
976ca316 3831 dw2_symtab_iter_init (&iter, per_objfile, {}, VAR_DOMAIN, func_name);
da51c347 3832
ed2dc618 3833 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3834 dw2_instantiate_symtab (per_cu, per_objfile, false);
da51c347 3835
9291a0cd
TT
3836}
3837
3838static void
3839dw2_expand_all_symtabs (struct objfile *objfile)
3840{
976ca316
SM
3841 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3842 int total_units = (per_objfile->per_bfd->all_comp_units.size ()
3843 + per_objfile->per_bfd->all_type_units.size ());
9291a0cd 3844
ed2dc618 3845 for (int i = 0; i < total_units; ++i)
9291a0cd 3846 {
976ca316 3847 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (i);
9291a0cd 3848
58f0c718
TT
3849 /* We don't want to directly expand a partial CU, because if we
3850 read it with the wrong language, then assertion failures can
3851 be triggered later on. See PR symtab/23010. So, tell
3852 dw2_instantiate_symtab to skip partial CUs -- any important
3853 partial CU will be read via DW_TAG_imported_unit anyway. */
976ca316 3854 dw2_instantiate_symtab (per_cu, per_objfile, true);
9291a0cd
TT
3855 }
3856}
3857
3858static void
652a8996
JK
3859dw2_expand_symtabs_with_fullname (struct objfile *objfile,
3860 const char *fullname)
9291a0cd 3861{
976ca316 3862 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
d4637a04
DE
3863
3864 /* We don't need to consider type units here.
3865 This is only called for examining code, e.g. expand_line_sal.
3866 There can be an order of magnitude (or more) more type units
3867 than comp units, and we avoid them if we can. */
3868
976ca316 3869 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
9291a0cd 3870 {
3d7bb9d9 3871 /* We only need to look at symtabs not already expanded. */
976ca316 3872 if (per_objfile->symtab_set_p (per_cu))
9291a0cd
TT
3873 continue;
3874
976ca316 3875 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
7b9f3c50 3876 if (file_data == NULL)
9291a0cd
TT
3877 continue;
3878
b76e467d 3879 for (int j = 0; j < file_data->num_file_names; ++j)
9291a0cd 3880 {
652a8996
JK
3881 const char *this_fullname = file_data->file_names[j];
3882
3883 if (filename_cmp (this_fullname, fullname) == 0)
9291a0cd 3884 {
976ca316 3885 dw2_instantiate_symtab (per_cu, per_objfile, false);
9291a0cd
TT
3886 break;
3887 }
3888 }
3889 }
3890}
3891
9a0bacfb
TV
3892static void
3893dw2_expand_symtabs_matching_symbol
3894 (mapped_index_base &index,
3895 const lookup_name_info &lookup_name_in,
3896 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
3897 enum search_domain kind,
fcf23d5b
SM
3898 gdb::function_view<bool (offset_type)> match_callback,
3899 dwarf2_per_objfile *per_objfile);
9a0bacfb
TV
3900
3901static void
3902dw2_expand_symtabs_matching_one
97a1449a
SM
3903 (dwarf2_per_cu_data *per_cu,
3904 dwarf2_per_objfile *per_objfile,
9a0bacfb
TV
3905 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
3906 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify);
3907
9291a0cd 3908static void
199b4314
TT
3909dw2_map_matching_symbols
3910 (struct objfile *objfile,
b054970d 3911 const lookup_name_info &name, domain_enum domain,
199b4314
TT
3912 int global,
3913 gdb::function_view<symbol_found_callback_ftype> callback,
199b4314 3914 symbol_compare_ftype *ordered_compare)
9291a0cd 3915{
1aa98955 3916 /* Used for Ada. */
976ca316 3917 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
1aa98955 3918
9a0bacfb
TV
3919 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
3920
976ca316 3921 if (per_objfile->per_bfd->index_table != nullptr)
1aa98955 3922 {
976ca316 3923 mapped_index &index = *per_objfile->per_bfd->index_table;
1aa98955 3924
9a0bacfb
TV
3925 const char *match_name = name.ada ().lookup_name ().c_str ();
3926 auto matcher = [&] (const char *symname)
3927 {
3928 if (ordered_compare == nullptr)
3929 return true;
3930 return ordered_compare (symname, match_name) == 0;
3931 };
3932
3933 dw2_expand_symtabs_matching_symbol (index, name, matcher, ALL_DOMAIN,
3934 [&] (offset_type namei)
3935 {
3936 struct dw2_symtab_iterator iter;
3937 struct dwarf2_per_cu_data *per_cu;
3938
976ca316 3939 dw2_symtab_iter_init (&iter, per_objfile, block_kind, domain,
e5f3ece2 3940 namei);
9a0bacfb 3941 while ((per_cu = dw2_symtab_iter_next (&iter)) != NULL)
976ca316 3942 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 3943 nullptr);
9a0bacfb 3944 return true;
976ca316 3945 }, per_objfile);
9a0bacfb
TV
3946 }
3947 else
3948 {
3949 /* We have -readnow: no .gdb_index, but no partial symtabs either. So,
3950 proceed assuming all symtabs have been read in. */
3951 }
1aa98955
TV
3952
3953 for (compunit_symtab *cust : objfile->compunits ())
3954 {
3955 const struct block *block;
3956
3957 if (cust == NULL)
3958 continue;
3959 block = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (cust), block_kind);
3960 if (!iterate_over_symbols_terminated (block, name,
3961 domain, callback))
3962 return;
3963 }
9291a0cd
TT
3964}
3965
e1ef7d7a
PA
3966/* Starting from a search name, return the string that finds the upper
3967 bound of all strings that start with SEARCH_NAME in a sorted name
3968 list. Returns the empty string to indicate that the upper bound is
3969 the end of the list. */
3970
3971static std::string
3972make_sort_after_prefix_name (const char *search_name)
3973{
3974 /* When looking to complete "func", we find the upper bound of all
3975 symbols that start with "func" by looking for where we'd insert
3976 the closest string that would follow "func" in lexicographical
3977 order. Usually, that's "func"-with-last-character-incremented,
3978 i.e. "fund". Mind non-ASCII characters, though. Usually those
3979 will be UTF-8 multi-byte sequences, but we can't be certain.
3980 Especially mind the 0xff character, which is a valid character in
3981 non-UTF-8 source character sets (e.g. Latin1 'ÿ'), and we can't
3982 rule out compilers allowing it in identifiers. Note that
3983 conveniently, strcmp/strcasecmp are specified to compare
3984 characters interpreted as unsigned char. So what we do is treat
3985 the whole string as a base 256 number composed of a sequence of
3986 base 256 "digits" and add 1 to it. I.e., adding 1 to 0xff wraps
3987 to 0, and carries 1 to the following more-significant position.
3988 If the very first character in SEARCH_NAME ends up incremented
3989 and carries/overflows, then the upper bound is the end of the
3990 list. The string after the empty string is also the empty
3991 string.
3992
3993 Some examples of this operation:
3994
3995 SEARCH_NAME => "+1" RESULT
3996
3997 "abc" => "abd"
3998 "ab\xff" => "ac"
3999 "\xff" "a" "\xff" => "\xff" "b"
4000 "\xff" => ""
4001 "\xff\xff" => ""
4002 "" => ""
4003
4004 Then, with these symbols for example:
4005
4006 func
4007 func1
4008 fund
4009
4010 completing "func" looks for symbols between "func" and
4011 "func"-with-last-character-incremented, i.e. "fund" (exclusive),
4012 which finds "func" and "func1", but not "fund".
4013
4014 And with:
4015
4016 funcÿ (Latin1 'ÿ' [0xff])
4017 funcÿ1
4018 fund
4019
4020 completing "funcÿ" looks for symbols between "funcÿ" and "fund"
4021 (exclusive), which finds "funcÿ" and "funcÿ1", but not "fund".
4022
4023 And with:
4024
4025 ÿÿ (Latin1 'ÿ' [0xff])
4026 ÿÿ1
4027
4028 completing "ÿ" or "ÿÿ" looks for symbols between between "ÿÿ" and
4029 the end of the list.
4030 */
4031 std::string after = search_name;
4032 while (!after.empty () && (unsigned char) after.back () == 0xff)
4033 after.pop_back ();
4034 if (!after.empty ())
4035 after.back () = (unsigned char) after.back () + 1;
4036 return after;
4037}
4038
5c58de74 4039/* See declaration. */
61d96d7e 4040
5c58de74
PA
4041std::pair<std::vector<name_component>::const_iterator,
4042 std::vector<name_component>::const_iterator>
44ed8f3e 4043mapped_index_base::find_name_components_bounds
fcf23d5b
SM
4044 (const lookup_name_info &lookup_name_without_params, language lang,
4045 dwarf2_per_objfile *per_objfile) const
3f563c84 4046{
5c58de74
PA
4047 auto *name_cmp
4048 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
3f563c84 4049
3b00ef10 4050 const char *lang_name
e0802d59 4051 = lookup_name_without_params.language_lookup_name (lang);
9291a0cd 4052
3f563c84
PA
4053 /* Comparison function object for lower_bound that matches against a
4054 given symbol name. */
4055 auto lookup_compare_lower = [&] (const name_component &elem,
4056 const char *name)
4057 {
fcf23d5b 4058 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4059 const char *elem_name = elem_qualified + elem.name_offset;
4060 return name_cmp (elem_name, name) < 0;
4061 };
4062
4063 /* Comparison function object for upper_bound that matches against a
4064 given symbol name. */
4065 auto lookup_compare_upper = [&] (const char *name,
4066 const name_component &elem)
4067 {
fcf23d5b 4068 const char *elem_qualified = this->symbol_name_at (elem.idx, per_objfile);
3f563c84
PA
4069 const char *elem_name = elem_qualified + elem.name_offset;
4070 return name_cmp (name, elem_name) < 0;
4071 };
4072
5c58de74
PA
4073 auto begin = this->name_components.begin ();
4074 auto end = this->name_components.end ();
3f563c84
PA
4075
4076 /* Find the lower bound. */
4077 auto lower = [&] ()
4078 {
3b00ef10 4079 if (lookup_name_without_params.completion_mode () && lang_name[0] == '\0')
3f563c84
PA
4080 return begin;
4081 else
3b00ef10 4082 return std::lower_bound (begin, end, lang_name, lookup_compare_lower);
3f563c84
PA
4083 } ();
4084
4085 /* Find the upper bound. */
4086 auto upper = [&] ()
4087 {
5c58de74 4088 if (lookup_name_without_params.completion_mode ())
3f563c84 4089 {
e1ef7d7a
PA
4090 /* In completion mode, we want UPPER to point past all
4091 symbols names that have the same prefix. I.e., with
4092 these symbols, and completing "func":
4093
4094 function << lower bound
4095 function1
4096 other_function << upper bound
4097
4098 We find the upper bound by looking for the insertion
4099 point of "func"-with-last-character-incremented,
4100 i.e. "fund". */
3b00ef10 4101 std::string after = make_sort_after_prefix_name (lang_name);
e1ef7d7a 4102 if (after.empty ())
3f563c84 4103 return end;
e6b2f5ef
PA
4104 return std::lower_bound (lower, end, after.c_str (),
4105 lookup_compare_lower);
3f563c84
PA
4106 }
4107 else
3b00ef10 4108 return std::upper_bound (lower, end, lang_name, lookup_compare_upper);
3f563c84
PA
4109 } ();
4110
5c58de74
PA
4111 return {lower, upper};
4112}
4113
4114/* See declaration. */
4115
4116void
fcf23d5b 4117mapped_index_base::build_name_components (dwarf2_per_objfile *per_objfile)
5c58de74
PA
4118{
4119 if (!this->name_components.empty ())
4120 return;
4121
4122 this->name_components_casing = case_sensitivity;
4123 auto *name_cmp
4124 = this->name_components_casing == case_sensitive_on ? strcmp : strcasecmp;
4125
4126 /* The code below only knows how to break apart components of C++
4127 symbol names (and other languages that use '::' as
3b00ef10 4128 namespace/module separator) and Ada symbol names. */
44ed8f3e
PA
4129 auto count = this->symbol_name_count ();
4130 for (offset_type idx = 0; idx < count; idx++)
5c58de74 4131 {
44ed8f3e 4132 if (this->symbol_name_slot_invalid (idx))
5c58de74
PA
4133 continue;
4134
fcf23d5b 4135 const char *name = this->symbol_name_at (idx, per_objfile);
5c58de74
PA
4136
4137 /* Add each name component to the name component table. */
4138 unsigned int previous_len = 0;
3b00ef10
TT
4139
4140 if (strstr (name, "::") != nullptr)
4141 {
4142 for (unsigned int current_len = cp_find_first_component (name);
4143 name[current_len] != '\0';
4144 current_len += cp_find_first_component (name + current_len))
4145 {
4146 gdb_assert (name[current_len] == ':');
4147 this->name_components.push_back ({previous_len, idx});
4148 /* Skip the '::'. */
4149 current_len += 2;
4150 previous_len = current_len;
4151 }
4152 }
4153 else
5c58de74 4154 {
3b00ef10
TT
4155 /* Handle the Ada encoded (aka mangled) form here. */
4156 for (const char *iter = strstr (name, "__");
4157 iter != nullptr;
4158 iter = strstr (iter, "__"))
4159 {
4160 this->name_components.push_back ({previous_len, idx});
4161 iter += 2;
4162 previous_len = iter - name;
4163 }
5c58de74 4164 }
3b00ef10 4165
5c58de74
PA
4166 this->name_components.push_back ({previous_len, idx});
4167 }
4168
4169 /* Sort name_components elements by name. */
4170 auto name_comp_compare = [&] (const name_component &left,
4171 const name_component &right)
4172 {
fcf23d5b
SM
4173 const char *left_qualified
4174 = this->symbol_name_at (left.idx, per_objfile);
4175 const char *right_qualified
4176 = this->symbol_name_at (right.idx, per_objfile);
5c58de74
PA
4177
4178 const char *left_name = left_qualified + left.name_offset;
4179 const char *right_name = right_qualified + right.name_offset;
4180
4181 return name_cmp (left_name, right_name) < 0;
4182 };
4183
4184 std::sort (this->name_components.begin (),
4185 this->name_components.end (),
4186 name_comp_compare);
4187}
4188
4189/* Helper for dw2_expand_symtabs_matching that works with a
44ed8f3e
PA
4190 mapped_index_base instead of the containing objfile. This is split
4191 to a separate function in order to be able to unit test the
4192 name_components matching using a mock mapped_index_base. For each
5c58de74 4193 symbol name that matches, calls MATCH_CALLBACK, passing it the
44ed8f3e 4194 symbol's index in the mapped_index_base symbol table. */
5c58de74
PA
4195
4196static void
4197dw2_expand_symtabs_matching_symbol
44ed8f3e 4198 (mapped_index_base &index,
5c58de74
PA
4199 const lookup_name_info &lookup_name_in,
4200 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4201 enum search_domain kind,
fcf23d5b
SM
4202 gdb::function_view<bool (offset_type)> match_callback,
4203 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4204{
4205 lookup_name_info lookup_name_without_params
4206 = lookup_name_in.make_ignore_params ();
5c58de74
PA
4207
4208 /* Build the symbol name component sorted vector, if we haven't
4209 yet. */
fcf23d5b 4210 index.build_name_components (per_objfile);
5c58de74 4211
3f563c84
PA
4212 /* The same symbol may appear more than once in the range though.
4213 E.g., if we're looking for symbols that complete "w", and we have
4214 a symbol named "w1::w2", we'll find the two name components for
4215 that same symbol in the range. To be sure we only call the
4216 callback once per symbol, we first collect the symbol name
4217 indexes that matched in a temporary vector and ignore
4218 duplicates. */
4219 std::vector<offset_type> matches;
3f563c84 4220
3b00ef10
TT
4221 struct name_and_matcher
4222 {
4223 symbol_name_matcher_ftype *matcher;
ecc6c606 4224 const char *name;
3b00ef10
TT
4225
4226 bool operator== (const name_and_matcher &other) const
3f563c84 4227 {
ecc6c606 4228 return matcher == other.matcher && strcmp (name, other.name) == 0;
3b00ef10
TT
4229 }
4230 };
4231
4232 /* A vector holding all the different symbol name matchers, for all
4233 languages. */
4234 std::vector<name_and_matcher> matchers;
4235
4236 for (int i = 0; i < nr_languages; i++)
4237 {
4238 enum language lang_e = (enum language) i;
4239
4240 const language_defn *lang = language_def (lang_e);
4241 symbol_name_matcher_ftype *name_matcher
c9debfb9 4242 = lang->get_symbol_name_matcher (lookup_name_without_params);
3f563c84 4243
3b00ef10 4244 name_and_matcher key {
dda83cd7 4245 name_matcher,
3b00ef10
TT
4246 lookup_name_without_params.language_lookup_name (lang_e)
4247 };
4248
4249 /* Don't insert the same comparison routine more than once.
4250 Note that we do this linear walk. This is not a problem in
4251 practice because the number of supported languages is
4252 low. */
4253 if (std::find (matchers.begin (), matchers.end (), key)
4254 != matchers.end ())
9291a0cd 4255 continue;
3b00ef10
TT
4256 matchers.push_back (std::move (key));
4257
4258 auto bounds
4259 = index.find_name_components_bounds (lookup_name_without_params,
fcf23d5b 4260 lang_e, per_objfile);
3b00ef10
TT
4261
4262 /* Now for each symbol name in range, check to see if we have a name
4263 match, and if so, call the MATCH_CALLBACK callback. */
4264
4265 for (; bounds.first != bounds.second; ++bounds.first)
4266 {
fcf23d5b
SM
4267 const char *qualified
4268 = index.symbol_name_at (bounds.first->idx, per_objfile);
3b00ef10
TT
4269
4270 if (!name_matcher (qualified, lookup_name_without_params, NULL)
4271 || (symbol_matcher != NULL && !symbol_matcher (qualified)))
4272 continue;
9291a0cd 4273
3b00ef10
TT
4274 matches.push_back (bounds.first->idx);
4275 }
3f563c84
PA
4276 }
4277
4278 std::sort (matches.begin (), matches.end ());
4279
4280 /* Finally call the callback, once per match. */
4281 ULONGEST prev = -1;
4282 for (offset_type idx : matches)
4283 {
4284 if (prev != idx)
4285 {
3b00ef10
TT
4286 if (!match_callback (idx))
4287 break;
3f563c84
PA
4288 prev = idx;
4289 }
4290 }
4291
4292 /* Above we use a type wider than idx's for 'prev', since 0 and
4293 (offset_type)-1 are both possible values. */
4294 static_assert (sizeof (prev) > sizeof (offset_type), "");
4295}
4296
c62446b1
PA
4297#if GDB_SELF_TEST
4298
4299namespace selftests { namespace dw2_expand_symtabs_matching {
4300
a3c5fafd
PA
4301/* A mock .gdb_index/.debug_names-like name index table, enough to
4302 exercise dw2_expand_symtabs_matching_symbol, which works with the
4303 mapped_index_base interface. Builds an index from the symbol list
4304 passed as parameter to the constructor. */
4305class mock_mapped_index : public mapped_index_base
c62446b1
PA
4306{
4307public:
a3c5fafd
PA
4308 mock_mapped_index (gdb::array_view<const char *> symbols)
4309 : m_symbol_table (symbols)
c62446b1
PA
4310 {}
4311
a3c5fafd 4312 DISABLE_COPY_AND_ASSIGN (mock_mapped_index);
c62446b1 4313
a3c5fafd 4314 /* Return the number of names in the symbol table. */
632e107b 4315 size_t symbol_name_count () const override
c62446b1 4316 {
a3c5fafd 4317 return m_symbol_table.size ();
c62446b1
PA
4318 }
4319
a3c5fafd 4320 /* Get the name of the symbol at IDX in the symbol table. */
fcf23d5b
SM
4321 const char *symbol_name_at
4322 (offset_type idx, dwarf2_per_objfile *per_objfile) const override
a3c5fafd
PA
4323 {
4324 return m_symbol_table[idx];
4325 }
c62446b1 4326
a3c5fafd
PA
4327private:
4328 gdb::array_view<const char *> m_symbol_table;
c62446b1
PA
4329};
4330
4331/* Convenience function that converts a NULL pointer to a "<null>"
4332 string, to pass to print routines. */
4333
4334static const char *
4335string_or_null (const char *str)
4336{
4337 return str != NULL ? str : "<null>";
4338}
4339
4340/* Check if a lookup_name_info built from
4341 NAME/MATCH_TYPE/COMPLETION_MODE matches the symbols in the mock
4342 index. EXPECTED_LIST is the list of expected matches, in expected
4343 matching order. If no match expected, then an empty list is
4344 specified. Returns true on success. On failure prints a warning
4345 indicating the file:line that failed, and returns false. */
4346
4347static bool
4348check_match (const char *file, int line,
4349 mock_mapped_index &mock_index,
4350 const char *name, symbol_name_match_type match_type,
4351 bool completion_mode,
fcf23d5b
SM
4352 std::initializer_list<const char *> expected_list,
4353 dwarf2_per_objfile *per_objfile)
c62446b1
PA
4354{
4355 lookup_name_info lookup_name (name, match_type, completion_mode);
4356
4357 bool matched = true;
4358
4359 auto mismatch = [&] (const char *expected_str,
4360 const char *got)
4361 {
4362 warning (_("%s:%d: match_type=%s, looking-for=\"%s\", "
4363 "expected=\"%s\", got=\"%s\"\n"),
4364 file, line,
4365 (match_type == symbol_name_match_type::FULL
4366 ? "FULL" : "WILD"),
4367 name, string_or_null (expected_str), string_or_null (got));
4368 matched = false;
4369 };
4370
4371 auto expected_it = expected_list.begin ();
4372 auto expected_end = expected_list.end ();
4373
a3c5fafd 4374 dw2_expand_symtabs_matching_symbol (mock_index, lookup_name,
c62446b1
PA
4375 NULL, ALL_DOMAIN,
4376 [&] (offset_type idx)
4377 {
fcf23d5b 4378 const char *matched_name = mock_index.symbol_name_at (idx, per_objfile);
c62446b1
PA
4379 const char *expected_str
4380 = expected_it == expected_end ? NULL : *expected_it++;
4381
4382 if (expected_str == NULL || strcmp (expected_str, matched_name) != 0)
4383 mismatch (expected_str, matched_name);
3b00ef10 4384 return true;
fcf23d5b 4385 }, per_objfile);
c62446b1
PA
4386
4387 const char *expected_str
4388 = expected_it == expected_end ? NULL : *expected_it++;
4389 if (expected_str != NULL)
4390 mismatch (expected_str, NULL);
4391
4392 return matched;
4393}
4394
4395/* The symbols added to the mock mapped_index for testing (in
4396 canonical form). */
4397static const char *test_symbols[] = {
4398 "function",
4399 "std::bar",
4400 "std::zfunction",
4401 "std::zfunction2",
4402 "w1::w2",
4403 "ns::foo<char*>",
4404 "ns::foo<int>",
4405 "ns::foo<long>",
a20714ff
PA
4406 "ns2::tmpl<int>::foo2",
4407 "(anonymous namespace)::A::B::C",
c62446b1 4408
e1ef7d7a
PA
4409 /* These are used to check that the increment-last-char in the
4410 matching algorithm for completion doesn't match "t1_fund" when
4411 completing "t1_func". */
4412 "t1_func",
4413 "t1_func1",
4414 "t1_fund",
4415 "t1_fund1",
4416
4417 /* A UTF-8 name with multi-byte sequences to make sure that
4418 cp-name-parser understands this as a single identifier ("função"
4419 is "function" in PT). */
4420 u8"u8função",
4421
4422 /* \377 (0xff) is Latin1 'ÿ'. */
4423 "yfunc\377",
4424
4425 /* \377 (0xff) is Latin1 'ÿ'. */
4426 "\377",
4427 "\377\377123",
4428
c62446b1
PA
4429 /* A name with all sorts of complications. Starts with "z" to make
4430 it easier for the completion tests below. */
4431#define Z_SYM_NAME \
4432 "z::std::tuple<(anonymous namespace)::ui*, std::bar<(anonymous namespace)::ui> >" \
4433 "::tuple<(anonymous namespace)::ui*, " \
4434 "std::default_delete<(anonymous namespace)::ui>, void>"
4435
4436 Z_SYM_NAME
4437};
4438
a3c5fafd
PA
4439/* Returns true if the mapped_index_base::find_name_component_bounds
4440 method finds EXPECTED_SYMS in INDEX when looking for SEARCH_NAME,
4441 in completion mode. */
5c58de74
PA
4442
4443static bool
a3c5fafd 4444check_find_bounds_finds (mapped_index_base &index,
5c58de74 4445 const char *search_name,
fcf23d5b
SM
4446 gdb::array_view<const char *> expected_syms,
4447 dwarf2_per_objfile *per_objfile)
5c58de74
PA
4448{
4449 lookup_name_info lookup_name (search_name,
4450 symbol_name_match_type::FULL, true);
4451
3b00ef10 4452 auto bounds = index.find_name_components_bounds (lookup_name,
fcf23d5b
SM
4453 language_cplus,
4454 per_objfile);
5c58de74
PA
4455
4456 size_t distance = std::distance (bounds.first, bounds.second);
4457 if (distance != expected_syms.size ())
4458 return false;
4459
4460 for (size_t exp_elem = 0; exp_elem < distance; exp_elem++)
4461 {
4462 auto nc_elem = bounds.first + exp_elem;
fcf23d5b 4463 const char *qualified = index.symbol_name_at (nc_elem->idx, per_objfile);
5c58de74
PA
4464 if (strcmp (qualified, expected_syms[exp_elem]) != 0)
4465 return false;
4466 }
4467
4468 return true;
4469}
4470
4471/* Test the lower-level mapped_index::find_name_component_bounds
4472 method. */
4473
c62446b1 4474static void
5c58de74
PA
4475test_mapped_index_find_name_component_bounds ()
4476{
4477 mock_mapped_index mock_index (test_symbols);
4478
fcf23d5b 4479 mock_index.build_name_components (NULL /* per_objfile */);
5c58de74
PA
4480
4481 /* Test the lower-level mapped_index::find_name_component_bounds
4482 method in completion mode. */
4483 {
4484 static const char *expected_syms[] = {
4485 "t1_func",
4486 "t1_func1",
5c58de74
PA
4487 };
4488
fcf23d5b
SM
4489 SELF_CHECK (check_find_bounds_finds
4490 (mock_index, "t1_func", expected_syms,
4491 NULL /* per_objfile */));
5c58de74
PA
4492 }
4493
4494 /* Check that the increment-last-char in the name matching algorithm
4495 for completion doesn't get confused with Ansi1 'ÿ' / 0xff. */
4496 {
4497 static const char *expected_syms1[] = {
4498 "\377",
4499 "\377\377123",
4500 };
fcf23d5b
SM
4501 SELF_CHECK (check_find_bounds_finds
4502 (mock_index, "\377", expected_syms1, NULL /* per_objfile */));
5c58de74
PA
4503
4504 static const char *expected_syms2[] = {
4505 "\377\377123",
4506 };
fcf23d5b
SM
4507 SELF_CHECK (check_find_bounds_finds
4508 (mock_index, "\377\377", expected_syms2,
4509 NULL /* per_objfile */));
5c58de74
PA
4510 }
4511}
4512
4513/* Test dw2_expand_symtabs_matching_symbol. */
4514
4515static void
4516test_dw2_expand_symtabs_matching_symbol ()
c62446b1
PA
4517{
4518 mock_mapped_index mock_index (test_symbols);
4519
4520 /* We let all tests run until the end even if some fails, for debug
4521 convenience. */
4522 bool any_mismatch = false;
4523
4524 /* Create the expected symbols list (an initializer_list). Needed
4525 because lists have commas, and we need to pass them to CHECK,
4526 which is a macro. */
4527#define EXPECT(...) { __VA_ARGS__ }
4528
4529 /* Wrapper for check_match that passes down the current
4530 __FILE__/__LINE__. */
4531#define CHECK_MATCH(NAME, MATCH_TYPE, COMPLETION_MODE, EXPECTED_LIST) \
4532 any_mismatch |= !check_match (__FILE__, __LINE__, \
4533 mock_index, \
4534 NAME, MATCH_TYPE, COMPLETION_MODE, \
fcf23d5b 4535 EXPECTED_LIST, NULL)
c62446b1
PA
4536
4537 /* Identity checks. */
4538 for (const char *sym : test_symbols)
4539 {
4540 /* Should be able to match all existing symbols. */
4541 CHECK_MATCH (sym, symbol_name_match_type::FULL, false,
4542 EXPECT (sym));
4543
4544 /* Should be able to match all existing symbols with
4545 parameters. */
4546 std::string with_params = std::string (sym) + "(int)";
4547 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4548 EXPECT (sym));
4549
4550 /* Should be able to match all existing symbols with
4551 parameters and qualifiers. */
4552 with_params = std::string (sym) + " ( int ) const";
4553 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4554 EXPECT (sym));
4555
4556 /* This should really find sym, but cp-name-parser.y doesn't
4557 know about lvalue/rvalue qualifiers yet. */
4558 with_params = std::string (sym) + " ( int ) &&";
4559 CHECK_MATCH (with_params.c_str (), symbol_name_match_type::FULL, false,
4560 {});
4561 }
4562
e1ef7d7a
PA
4563 /* Check that the name matching algorithm for completion doesn't get
4564 confused with Latin1 'ÿ' / 0xff. */
4565 {
4566 static const char str[] = "\377";
4567 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4568 EXPECT ("\377", "\377\377123"));
4569 }
4570
4571 /* Check that the increment-last-char in the matching algorithm for
4572 completion doesn't match "t1_fund" when completing "t1_func". */
4573 {
4574 static const char str[] = "t1_func";
4575 CHECK_MATCH (str, symbol_name_match_type::FULL, true,
4576 EXPECT ("t1_func", "t1_func1"));
4577 }
4578
c62446b1
PA
4579 /* Check that completion mode works at each prefix of the expected
4580 symbol name. */
4581 {
4582 static const char str[] = "function(int)";
4583 size_t len = strlen (str);
4584 std::string lookup;
4585
4586 for (size_t i = 1; i < len; i++)
4587 {
4588 lookup.assign (str, i);
4589 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4590 EXPECT ("function"));
4591 }
4592 }
4593
4594 /* While "w" is a prefix of both components, the match function
4595 should still only be called once. */
4596 {
4597 CHECK_MATCH ("w", symbol_name_match_type::FULL, true,
4598 EXPECT ("w1::w2"));
a20714ff
PA
4599 CHECK_MATCH ("w", symbol_name_match_type::WILD, true,
4600 EXPECT ("w1::w2"));
c62446b1
PA
4601 }
4602
4603 /* Same, with a "complicated" symbol. */
4604 {
4605 static const char str[] = Z_SYM_NAME;
4606 size_t len = strlen (str);
4607 std::string lookup;
4608
4609 for (size_t i = 1; i < len; i++)
4610 {
4611 lookup.assign (str, i);
4612 CHECK_MATCH (lookup.c_str (), symbol_name_match_type::FULL, true,
4613 EXPECT (Z_SYM_NAME));
4614 }
4615 }
4616
4617 /* In FULL mode, an incomplete symbol doesn't match. */
4618 {
4619 CHECK_MATCH ("std::zfunction(int", symbol_name_match_type::FULL, false,
4620 {});
4621 }
4622
4623 /* A complete symbol with parameters matches any overload, since the
4624 index has no overload info. */
4625 {
4626 CHECK_MATCH ("std::zfunction(int)", symbol_name_match_type::FULL, true,
4627 EXPECT ("std::zfunction", "std::zfunction2"));
a20714ff
PA
4628 CHECK_MATCH ("zfunction(int)", symbol_name_match_type::WILD, true,
4629 EXPECT ("std::zfunction", "std::zfunction2"));
4630 CHECK_MATCH ("zfunc", symbol_name_match_type::WILD, true,
4631 EXPECT ("std::zfunction", "std::zfunction2"));
c62446b1
PA
4632 }
4633
4634 /* Check that whitespace is ignored appropriately. A symbol with a
4635 template argument list. */
4636 {
4637 static const char expected[] = "ns::foo<int>";
4638 CHECK_MATCH ("ns :: foo < int > ", symbol_name_match_type::FULL, false,
4639 EXPECT (expected));
a20714ff
PA
4640 CHECK_MATCH ("foo < int > ", symbol_name_match_type::WILD, false,
4641 EXPECT (expected));
c62446b1
PA
4642 }
4643
4644 /* Check that whitespace is ignored appropriately. A symbol with a
4645 template argument list that includes a pointer. */
4646 {
4647 static const char expected[] = "ns::foo<char*>";
4648 /* Try both completion and non-completion modes. */
4649 static const bool completion_mode[2] = {false, true};
4650 for (size_t i = 0; i < 2; i++)
4651 {
4652 CHECK_MATCH ("ns :: foo < char * >", symbol_name_match_type::FULL,
4653 completion_mode[i], EXPECT (expected));
a20714ff
PA
4654 CHECK_MATCH ("foo < char * >", symbol_name_match_type::WILD,
4655 completion_mode[i], EXPECT (expected));
c62446b1
PA
4656
4657 CHECK_MATCH ("ns :: foo < char * > (int)", symbol_name_match_type::FULL,
4658 completion_mode[i], EXPECT (expected));
a20714ff
PA
4659 CHECK_MATCH ("foo < char * > (int)", symbol_name_match_type::WILD,
4660 completion_mode[i], EXPECT (expected));
c62446b1
PA
4661 }
4662 }
4663
4664 {
4665 /* Check method qualifiers are ignored. */
4666 static const char expected[] = "ns::foo<char*>";
4667 CHECK_MATCH ("ns :: foo < char * > ( int ) const",
4668 symbol_name_match_type::FULL, true, EXPECT (expected));
4669 CHECK_MATCH ("ns :: foo < char * > ( int ) &&",
4670 symbol_name_match_type::FULL, true, EXPECT (expected));
a20714ff
PA
4671 CHECK_MATCH ("foo < char * > ( int ) const",
4672 symbol_name_match_type::WILD, true, EXPECT (expected));
4673 CHECK_MATCH ("foo < char * > ( int ) &&",
4674 symbol_name_match_type::WILD, true, EXPECT (expected));
c62446b1
PA
4675 }
4676
4677 /* Test lookup names that don't match anything. */
4678 {
a20714ff
PA
4679 CHECK_MATCH ("bar2", symbol_name_match_type::WILD, false,
4680 {});
4681
c62446b1
PA
4682 CHECK_MATCH ("doesntexist", symbol_name_match_type::FULL, false,
4683 {});
4684 }
4685
a20714ff
PA
4686 /* Some wild matching tests, exercising "(anonymous namespace)",
4687 which should not be confused with a parameter list. */
4688 {
4689 static const char *syms[] = {
4690 "A::B::C",
4691 "B::C",
4692 "C",
4693 "A :: B :: C ( int )",
4694 "B :: C ( int )",
4695 "C ( int )",
4696 };
4697
4698 for (const char *s : syms)
4699 {
4700 CHECK_MATCH (s, symbol_name_match_type::WILD, false,
4701 EXPECT ("(anonymous namespace)::A::B::C"));
4702 }
4703 }
4704
4705 {
4706 static const char expected[] = "ns2::tmpl<int>::foo2";
4707 CHECK_MATCH ("tmp", symbol_name_match_type::WILD, true,
4708 EXPECT (expected));
4709 CHECK_MATCH ("tmpl<", symbol_name_match_type::WILD, true,
4710 EXPECT (expected));
4711 }
4712
c62446b1
PA
4713 SELF_CHECK (!any_mismatch);
4714
4715#undef EXPECT
4716#undef CHECK_MATCH
4717}
4718
5c58de74
PA
4719static void
4720run_test ()
4721{
4722 test_mapped_index_find_name_component_bounds ();
4723 test_dw2_expand_symtabs_matching_symbol ();
4724}
4725
c62446b1
PA
4726}} // namespace selftests::dw2_expand_symtabs_matching
4727
4728#endif /* GDB_SELF_TEST */
4729
4b514bc8
JK
4730/* If FILE_MATCHER is NULL or if PER_CU has
4731 dwarf2_per_cu_quick_data::MARK set (see
4732 dw_expand_symtabs_matching_file_matcher), expand the CU and call
4733 EXPANSION_NOTIFY on it. */
4734
4735static void
4736dw2_expand_symtabs_matching_one
97a1449a
SM
4737 (dwarf2_per_cu_data *per_cu,
4738 dwarf2_per_objfile *per_objfile,
4b514bc8
JK
4739 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4740 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify)
4741{
4742 if (file_matcher == NULL || per_cu->v.quick->mark)
4743 {
af758d11 4744 bool symtab_was_null = !per_objfile->symtab_set_p (per_cu);
4b514bc8 4745
97a1449a
SM
4746 compunit_symtab *symtab
4747 = dw2_instantiate_symtab (per_cu, per_objfile, false);
af758d11 4748 gdb_assert (symtab != nullptr);
4b514bc8 4749
af758d11
SM
4750 if (expansion_notify != NULL && symtab_was_null)
4751 expansion_notify (symtab);
4b514bc8
JK
4752 }
4753}
4754
3f563c84
PA
4755/* Helper for dw2_expand_matching symtabs. Called on each symbol
4756 matched, to expand corresponding CUs that were marked. IDX is the
4757 index of the symbol name that matched. */
4758
4759static void
4760dw2_expand_marked_cus
976ca316 4761 (dwarf2_per_objfile *per_objfile, offset_type idx,
3f563c84
PA
4762 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
4763 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4764 search_domain kind)
4765{
3f563c84
PA
4766 offset_type *vec, vec_len, vec_idx;
4767 bool global_seen = false;
976ca316 4768 mapped_index &index = *per_objfile->per_bfd->index_table;
3f563c84 4769
61920122 4770 vec = (offset_type *) (index.constant_pool
f00a2de2 4771 + MAYBE_SWAP (index.symbol_table[idx].vec));
61920122
PA
4772 vec_len = MAYBE_SWAP (vec[0]);
4773 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
4774 {
61920122
PA
4775 offset_type cu_index_and_attrs = MAYBE_SWAP (vec[vec_idx + 1]);
4776 /* This value is only valid for index versions >= 7. */
4777 int is_static = GDB_INDEX_SYMBOL_STATIC_VALUE (cu_index_and_attrs);
4778 gdb_index_symbol_kind symbol_kind =
4779 GDB_INDEX_SYMBOL_KIND_VALUE (cu_index_and_attrs);
4780 int cu_index = GDB_INDEX_CU_VALUE (cu_index_and_attrs);
4781 /* Only check the symbol attributes if they're present.
4782 Indices prior to version 7 don't record them,
4783 and indices >= 7 may elide them for certain symbols
4784 (gold does this). */
4785 int attrs_valid =
4786 (index.version >= 7
4787 && symbol_kind != GDB_INDEX_SYMBOL_KIND_NONE);
4788
4789 /* Work around gold/15646. */
f030440d
TV
4790 if (attrs_valid
4791 && !is_static
4792 && symbol_kind == GDB_INDEX_SYMBOL_KIND_TYPE)
9291a0cd 4793 {
f030440d 4794 if (global_seen)
61920122 4795 continue;
f030440d
TV
4796
4797 global_seen = true;
61920122 4798 }
3190f0c6 4799
61920122
PA
4800 /* Only check the symbol's kind if it has one. */
4801 if (attrs_valid)
4802 {
4803 switch (kind)
8943b874 4804 {
61920122
PA
4805 case VARIABLES_DOMAIN:
4806 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_VARIABLE)
4807 continue;
4808 break;
4809 case FUNCTIONS_DOMAIN:
4810 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_FUNCTION)
8943b874 4811 continue;
61920122
PA
4812 break;
4813 case TYPES_DOMAIN:
4814 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_TYPE)
4815 continue;
4816 break;
59c35742
AB
4817 case MODULES_DOMAIN:
4818 if (symbol_kind != GDB_INDEX_SYMBOL_KIND_OTHER)
4819 continue;
4820 break;
61920122
PA
4821 default:
4822 break;
8943b874 4823 }
61920122 4824 }
8943b874 4825
61920122 4826 /* Don't crash on bad data. */
976ca316
SM
4827 if (cu_index >= (per_objfile->per_bfd->all_comp_units.size ()
4828 + per_objfile->per_bfd->all_type_units.size ()))
61920122 4829 {
b98664d3 4830 complaint (_(".gdb_index entry has bad CU index"
976ca316 4831 " [in module %s]"), objfile_name (per_objfile->objfile));
61920122
PA
4832 continue;
4833 }
4834
976ca316
SM
4835 dwarf2_per_cu_data *per_cu = per_objfile->per_bfd->get_cutu (cu_index);
4836 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
4b514bc8 4837 expansion_notify);
61920122
PA
4838 }
4839}
4840
4b514bc8
JK
4841/* If FILE_MATCHER is non-NULL, set all the
4842 dwarf2_per_cu_quick_data::MARK of the current DWARF2_PER_OBJFILE
4843 that match FILE_MATCHER. */
4844
61920122 4845static void
4b514bc8 4846dw_expand_symtabs_matching_file_matcher
976ca316 4847 (dwarf2_per_objfile *per_objfile,
ed2dc618 4848 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher)
61920122 4849{
4b514bc8 4850 if (file_matcher == NULL)
61920122
PA
4851 return;
4852
4b514bc8
JK
4853 htab_up visited_found (htab_create_alloc (10, htab_hash_pointer,
4854 htab_eq_pointer,
4855 NULL, xcalloc, xfree));
4856 htab_up visited_not_found (htab_create_alloc (10, htab_hash_pointer,
61920122
PA
4857 htab_eq_pointer,
4858 NULL, xcalloc, xfree));
61920122 4859
4b514bc8
JK
4860 /* The rule is CUs specify all the files, including those used by
4861 any TU, so there's no need to scan TUs here. */
61920122 4862
976ca316 4863 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 4864 {
927aa2e7
JK
4865 QUIT;
4866
4867 per_cu->v.quick->mark = 0;
4868
4869 /* We only need to look at symtabs not already expanded. */
976ca316 4870 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
4871 continue;
4872
976ca316 4873 quick_file_names *file_data = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
4874 if (file_data == NULL)
4875 continue;
4876
4877 if (htab_find (visited_not_found.get (), file_data) != NULL)
4878 continue;
4879 else if (htab_find (visited_found.get (), file_data) != NULL)
4880 {
4881 per_cu->v.quick->mark = 1;
4882 continue;
4883 }
4884
b76e467d 4885 for (int j = 0; j < file_data->num_file_names; ++j)
927aa2e7
JK
4886 {
4887 const char *this_real_name;
4888
4889 if (file_matcher (file_data->file_names[j], false))
4890 {
4891 per_cu->v.quick->mark = 1;
4892 break;
4893 }
4894
4895 /* Before we invoke realpath, which can get expensive when many
4896 files are involved, do a quick comparison of the basenames. */
4897 if (!basenames_may_differ
4898 && !file_matcher (lbasename (file_data->file_names[j]),
4899 true))
4900 continue;
4901
976ca316 4902 this_real_name = dw2_get_real_path (per_objfile, file_data, j);
927aa2e7
JK
4903 if (file_matcher (this_real_name, false))
4904 {
4905 per_cu->v.quick->mark = 1;
4906 break;
4907 }
4908 }
4909
b76e467d
SM
4910 void **slot = htab_find_slot (per_cu->v.quick->mark
4911 ? visited_found.get ()
4912 : visited_not_found.get (),
4913 file_data, INSERT);
927aa2e7
JK
4914 *slot = file_data;
4915 }
4916}
4917
4918static void
4919dw2_expand_symtabs_matching
4920 (struct objfile *objfile,
4921 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 4922 const lookup_name_info *lookup_name,
927aa2e7
JK
4923 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
4924 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
4925 enum search_domain kind)
4926{
976ca316 4927 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7
JK
4928
4929 /* index_table is NULL if OBJF_READNOW. */
976ca316 4930 if (!per_objfile->per_bfd->index_table)
927aa2e7
JK
4931 return;
4932
976ca316 4933 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
927aa2e7 4934
c1a66c06
TV
4935 if (symbol_matcher == NULL && lookup_name == NULL)
4936 {
976ca316 4937 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
4938 {
4939 QUIT;
4940
976ca316 4941 dw2_expand_symtabs_matching_one (per_cu, per_objfile,
97a1449a 4942 file_matcher, expansion_notify);
c1a66c06
TV
4943 }
4944 return;
4945 }
4946
976ca316 4947 mapped_index &index = *per_objfile->per_bfd->index_table;
927aa2e7 4948
c1a66c06 4949 dw2_expand_symtabs_matching_symbol (index, *lookup_name,
927aa2e7
JK
4950 symbol_matcher,
4951 kind, [&] (offset_type idx)
4952 {
976ca316
SM
4953 dw2_expand_marked_cus (per_objfile, idx, file_matcher, expansion_notify,
4954 kind);
3b00ef10 4955 return true;
976ca316 4956 }, per_objfile);
927aa2e7
JK
4957}
4958
4959/* A helper for dw2_find_pc_sect_compunit_symtab which finds the most specific
4960 symtab. */
4961
4962static struct compunit_symtab *
4963recursively_find_pc_sect_compunit_symtab (struct compunit_symtab *cust,
4964 CORE_ADDR pc)
4965{
4966 int i;
4967
4968 if (COMPUNIT_BLOCKVECTOR (cust) != NULL
4969 && blockvector_contains_pc (COMPUNIT_BLOCKVECTOR (cust), pc))
4970 return cust;
4971
4972 if (cust->includes == NULL)
4973 return NULL;
4974
4975 for (i = 0; cust->includes[i]; ++i)
4976 {
4977 struct compunit_symtab *s = cust->includes[i];
4978
4979 s = recursively_find_pc_sect_compunit_symtab (s, pc);
4980 if (s != NULL)
4981 return s;
4982 }
4983
4984 return NULL;
4985}
4986
4987static struct compunit_symtab *
4988dw2_find_pc_sect_compunit_symtab (struct objfile *objfile,
4989 struct bound_minimal_symbol msymbol,
4990 CORE_ADDR pc,
4991 struct obj_section *section,
4992 int warn_if_readin)
4993{
4994 struct dwarf2_per_cu_data *data;
4995 struct compunit_symtab *result;
4996
d320c2b5 4997 if (!objfile->partial_symtabs->psymtabs_addrmap)
927aa2e7
JK
4998 return NULL;
4999
b3b3bada 5000 CORE_ADDR baseaddr = objfile->text_section_offset ();
d320c2b5
TT
5001 data = (struct dwarf2_per_cu_data *) addrmap_find
5002 (objfile->partial_symtabs->psymtabs_addrmap, pc - baseaddr);
927aa2e7
JK
5003 if (!data)
5004 return NULL;
5005
af758d11
SM
5006 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
5007 if (warn_if_readin && per_objfile->symtab_set_p (data))
927aa2e7 5008 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
08feed99 5009 paddress (objfile->arch (), pc));
927aa2e7 5010
97a1449a
SM
5011 result = recursively_find_pc_sect_compunit_symtab
5012 (dw2_instantiate_symtab (data, per_objfile, false), pc);
5013
927aa2e7
JK
5014 gdb_assert (result != NULL);
5015 return result;
5016}
5017
5018static void
5019dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
5020 void *data, int need_fullname)
5021{
976ca316 5022 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
927aa2e7 5023
976ca316 5024 if (!per_objfile->per_bfd->filenames_cache)
927aa2e7 5025 {
976ca316 5026 per_objfile->per_bfd->filenames_cache.emplace ();
927aa2e7
JK
5027
5028 htab_up visited (htab_create_alloc (10,
5029 htab_hash_pointer, htab_eq_pointer,
5030 NULL, xcalloc, xfree));
5031
5032 /* The rule is CUs specify all the files, including those used
5033 by any TU, so there's no need to scan TUs here. We can
5034 ignore file names coming from already-expanded CUs. */
5035
976ca316 5036 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5037 {
976ca316 5038 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5039 {
5040 void **slot = htab_find_slot (visited.get (),
5041 per_cu->v.quick->file_names,
5042 INSERT);
5043
5044 *slot = per_cu->v.quick->file_names;
5045 }
5046 }
5047
976ca316 5048 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
927aa2e7 5049 {
927aa2e7 5050 /* We only need to look at symtabs not already expanded. */
976ca316 5051 if (per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5052 continue;
5053
ab432490 5054 quick_file_names *file_data
976ca316 5055 = dw2_get_file_names (per_cu, per_objfile);
927aa2e7
JK
5056 if (file_data == NULL)
5057 continue;
5058
b76e467d 5059 void **slot = htab_find_slot (visited.get (), file_data, INSERT);
927aa2e7
JK
5060 if (*slot)
5061 {
5062 /* Already visited. */
5063 continue;
5064 }
5065 *slot = file_data;
5066
5067 for (int j = 0; j < file_data->num_file_names; ++j)
5068 {
5069 const char *filename = file_data->file_names[j];
976ca316 5070 per_objfile->per_bfd->filenames_cache->seen (filename);
927aa2e7
JK
5071 }
5072 }
5073 }
5074
976ca316 5075 per_objfile->per_bfd->filenames_cache->traverse ([&] (const char *filename)
927aa2e7
JK
5076 {
5077 gdb::unique_xmalloc_ptr<char> this_real_name;
5078
5079 if (need_fullname)
5080 this_real_name = gdb_realpath (filename);
5081 (*fun) (filename, this_real_name.get (), data);
5082 });
5083}
5084
5085static int
5086dw2_has_symbols (struct objfile *objfile)
5087{
5088 return 1;
5089}
5090
5091const struct quick_symbol_functions dwarf2_gdb_index_functions =
5092{
5093 dw2_has_symbols,
5094 dw2_find_last_source_symtab,
5095 dw2_forget_cached_source_info,
5096 dw2_map_symtabs_matching_filename,
5097 dw2_lookup_symbol,
d3214198 5098 NULL,
927aa2e7
JK
5099 dw2_print_stats,
5100 dw2_dump,
927aa2e7
JK
5101 dw2_expand_symtabs_for_function,
5102 dw2_expand_all_symtabs,
5103 dw2_expand_symtabs_with_fullname,
5104 dw2_map_matching_symbols,
5105 dw2_expand_symtabs_matching,
5106 dw2_find_pc_sect_compunit_symtab,
5107 NULL,
5108 dw2_map_symbol_filenames
5109};
5110
5111/* DWARF-5 debug_names reader. */
5112
5113/* DWARF-5 augmentation string for GDB's DW_IDX_GNU_* extension. */
5114static const gdb_byte dwarf5_augmentation[] = { 'G', 'D', 'B', 0 };
5115
5116/* A helper function that reads the .debug_names section in SECTION
5117 and fills in MAP. FILENAME is the name of the file containing the
5118 section; it is used for error reporting.
5119
5120 Returns true if all went well, false otherwise. */
5121
5122static bool
5123read_debug_names_from_section (struct objfile *objfile,
5124 const char *filename,
5125 struct dwarf2_section_info *section,
5126 mapped_debug_names &map)
5127{
96b79293 5128 if (section->empty ())
927aa2e7
JK
5129 return false;
5130
5131 /* Older elfutils strip versions could keep the section in the main
5132 executable while splitting it for the separate debug info file. */
96b79293 5133 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
927aa2e7
JK
5134 return false;
5135
96b79293 5136 section->read (objfile);
927aa2e7 5137
08feed99 5138 map.dwarf5_byte_order = gdbarch_byte_order (objfile->arch ());
927aa2e7
JK
5139
5140 const gdb_byte *addr = section->buffer;
5141
96b79293 5142 bfd *const abfd = section->get_bfd_owner ();
927aa2e7
JK
5143
5144 unsigned int bytes_read;
5145 LONGEST length = read_initial_length (abfd, addr, &bytes_read);
5146 addr += bytes_read;
5147
5148 map.dwarf5_is_dwarf64 = bytes_read != 4;
5149 map.offset_size = map.dwarf5_is_dwarf64 ? 8 : 4;
5150 if (bytes_read + length != section->size)
5151 {
5152 /* There may be multiple per-CU indices. */
5153 warning (_("Section .debug_names in %s length %s does not match "
5154 "section length %s, ignoring .debug_names."),
5155 filename, plongest (bytes_read + length),
5156 pulongest (section->size));
5157 return false;
5158 }
5159
5160 /* The version number. */
5161 uint16_t version = read_2_bytes (abfd, addr);
5162 addr += 2;
5163 if (version != 5)
5164 {
5165 warning (_("Section .debug_names in %s has unsupported version %d, "
5166 "ignoring .debug_names."),
5167 filename, version);
5168 return false;
5169 }
5170
5171 /* Padding. */
5172 uint16_t padding = read_2_bytes (abfd, addr);
5173 addr += 2;
5174 if (padding != 0)
5175 {
5176 warning (_("Section .debug_names in %s has unsupported padding %d, "
5177 "ignoring .debug_names."),
5178 filename, padding);
5179 return false;
5180 }
5181
5182 /* comp_unit_count - The number of CUs in the CU list. */
5183 map.cu_count = read_4_bytes (abfd, addr);
5184 addr += 4;
5185
5186 /* local_type_unit_count - The number of TUs in the local TU
5187 list. */
5188 map.tu_count = read_4_bytes (abfd, addr);
5189 addr += 4;
5190
5191 /* foreign_type_unit_count - The number of TUs in the foreign TU
5192 list. */
5193 uint32_t foreign_tu_count = read_4_bytes (abfd, addr);
5194 addr += 4;
5195 if (foreign_tu_count != 0)
5196 {
5197 warning (_("Section .debug_names in %s has unsupported %lu foreign TUs, "
5198 "ignoring .debug_names."),
5199 filename, static_cast<unsigned long> (foreign_tu_count));
5200 return false;
5201 }
5202
5203 /* bucket_count - The number of hash buckets in the hash lookup
5204 table. */
5205 map.bucket_count = read_4_bytes (abfd, addr);
5206 addr += 4;
5207
5208 /* name_count - The number of unique names in the index. */
5209 map.name_count = read_4_bytes (abfd, addr);
5210 addr += 4;
5211
5212 /* abbrev_table_size - The size in bytes of the abbreviations
5213 table. */
5214 uint32_t abbrev_table_size = read_4_bytes (abfd, addr);
5215 addr += 4;
5216
5217 /* augmentation_string_size - The size in bytes of the augmentation
5218 string. This value is rounded up to a multiple of 4. */
5219 uint32_t augmentation_string_size = read_4_bytes (abfd, addr);
5220 addr += 4;
5221 map.augmentation_is_gdb = ((augmentation_string_size
5222 == sizeof (dwarf5_augmentation))
5223 && memcmp (addr, dwarf5_augmentation,
5224 sizeof (dwarf5_augmentation)) == 0);
5225 augmentation_string_size += (-augmentation_string_size) & 3;
5226 addr += augmentation_string_size;
5227
5228 /* List of CUs */
5229 map.cu_table_reordered = addr;
5230 addr += map.cu_count * map.offset_size;
5231
5232 /* List of Local TUs */
5233 map.tu_table_reordered = addr;
5234 addr += map.tu_count * map.offset_size;
5235
5236 /* Hash Lookup Table */
5237 map.bucket_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5238 addr += map.bucket_count * 4;
5239 map.hash_table_reordered = reinterpret_cast<const uint32_t *> (addr);
5240 addr += map.name_count * 4;
5241
5242 /* Name Table */
5243 map.name_table_string_offs_reordered = addr;
5244 addr += map.name_count * map.offset_size;
5245 map.name_table_entry_offs_reordered = addr;
5246 addr += map.name_count * map.offset_size;
5247
5248 const gdb_byte *abbrev_table_start = addr;
5249 for (;;)
5250 {
927aa2e7
JK
5251 const ULONGEST index_num = read_unsigned_leb128 (abfd, addr, &bytes_read);
5252 addr += bytes_read;
5253 if (index_num == 0)
5254 break;
5255
5256 const auto insertpair
5257 = map.abbrev_map.emplace (index_num, mapped_debug_names::index_val ());
5258 if (!insertpair.second)
5259 {
5260 warning (_("Section .debug_names in %s has duplicate index %s, "
5261 "ignoring .debug_names."),
5262 filename, pulongest (index_num));
5263 return false;
5264 }
5265 mapped_debug_names::index_val &indexval = insertpair.first->second;
5266 indexval.dwarf_tag = read_unsigned_leb128 (abfd, addr, &bytes_read);
5267 addr += bytes_read;
5268
5269 for (;;)
5270 {
5271 mapped_debug_names::index_val::attr attr;
5272 attr.dw_idx = read_unsigned_leb128 (abfd, addr, &bytes_read);
5273 addr += bytes_read;
5274 attr.form = read_unsigned_leb128 (abfd, addr, &bytes_read);
5275 addr += bytes_read;
5276 if (attr.form == DW_FORM_implicit_const)
5277 {
5278 attr.implicit_const = read_signed_leb128 (abfd, addr,
5279 &bytes_read);
5280 addr += bytes_read;
5281 }
5282 if (attr.dw_idx == 0 && attr.form == 0)
5283 break;
5284 indexval.attr_vec.push_back (std::move (attr));
5285 }
5286 }
5287 if (addr != abbrev_table_start + abbrev_table_size)
5288 {
5289 warning (_("Section .debug_names in %s has abbreviation_table "
47e3f474
TV
5290 "of size %s vs. written as %u, ignoring .debug_names."),
5291 filename, plongest (addr - abbrev_table_start),
5292 abbrev_table_size);
927aa2e7
JK
5293 return false;
5294 }
5295 map.entry_pool = addr;
5296
5297 return true;
5298}
5299
5300/* A helper for create_cus_from_debug_names that handles the MAP's CU
5301 list. */
5302
5303static void
168c9250 5304create_cus_from_debug_names_list (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5305 const mapped_debug_names &map,
5306 dwarf2_section_info &section,
b76e467d 5307 bool is_dwz)
927aa2e7 5308{
3ee6bb11
TV
5309 if (!map.augmentation_is_gdb)
5310 {
5311 for (uint32_t i = 0; i < map.cu_count; ++i)
5312 {
5313 sect_offset sect_off
5314 = (sect_offset) (extract_unsigned_integer
5315 (map.cu_table_reordered + i * map.offset_size,
5316 map.offset_size,
5317 map.dwarf5_byte_order));
5318 /* We don't know the length of the CU, because the CU list in a
5319 .debug_names index can be incomplete, so we can't use the start of
5320 the next CU as end of this CU. We create the CUs here with length 0,
5321 and in cutu_reader::cutu_reader we'll fill in the actual length. */
5322 dwarf2_per_cu_data *per_cu
168c9250
SM
5323 = create_cu_from_index_list (per_bfd, &section, is_dwz, sect_off, 0);
5324 per_bfd->all_comp_units.push_back (per_cu);
3ee6bb11
TV
5325 }
5326 }
5327
927aa2e7
JK
5328 sect_offset sect_off_prev;
5329 for (uint32_t i = 0; i <= map.cu_count; ++i)
5330 {
5331 sect_offset sect_off_next;
5332 if (i < map.cu_count)
5333 {
5334 sect_off_next
5335 = (sect_offset) (extract_unsigned_integer
5336 (map.cu_table_reordered + i * map.offset_size,
5337 map.offset_size,
5338 map.dwarf5_byte_order));
5339 }
5340 else
5341 sect_off_next = (sect_offset) section.size;
5342 if (i >= 1)
5343 {
5344 const ULONGEST length = sect_off_next - sect_off_prev;
b76e467d 5345 dwarf2_per_cu_data *per_cu
168c9250 5346 = create_cu_from_index_list (per_bfd, &section, is_dwz,
927aa2e7 5347 sect_off_prev, length);
168c9250 5348 per_bfd->all_comp_units.push_back (per_cu);
927aa2e7
JK
5349 }
5350 sect_off_prev = sect_off_next;
5351 }
5352}
5353
5354/* Read the CU list from the mapped index, and use it to create all
ed2dc618 5355 the CU objects for this dwarf2_per_objfile. */
927aa2e7
JK
5356
5357static void
168c9250 5358create_cus_from_debug_names (dwarf2_per_bfd *per_bfd,
927aa2e7
JK
5359 const mapped_debug_names &map,
5360 const mapped_debug_names &dwz_map)
5361{
168c9250
SM
5362 gdb_assert (per_bfd->all_comp_units.empty ());
5363 per_bfd->all_comp_units.reserve (map.cu_count + dwz_map.cu_count);
927aa2e7 5364
168c9250 5365 create_cus_from_debug_names_list (per_bfd, map, per_bfd->info,
b76e467d 5366 false /* is_dwz */);
927aa2e7
JK
5367
5368 if (dwz_map.cu_count == 0)
5369 return;
5370
168c9250
SM
5371 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
5372 create_cus_from_debug_names_list (per_bfd, dwz_map, dwz->info,
b76e467d 5373 true /* is_dwz */);
927aa2e7
JK
5374}
5375
5376/* Read .debug_names. If everything went ok, initialize the "quick"
5377 elements of all the CUs and return true. Otherwise, return false. */
5378
5379static bool
976ca316 5380dwarf2_read_debug_names (dwarf2_per_objfile *per_objfile)
927aa2e7 5381{
fcf23d5b
SM
5382 std::unique_ptr<mapped_debug_names> map (new mapped_debug_names);
5383 mapped_debug_names dwz_map;
976ca316 5384 struct objfile *objfile = per_objfile->objfile;
f8c41851 5385 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
927aa2e7
JK
5386
5387 if (!read_debug_names_from_section (objfile, objfile_name (objfile),
976ca316 5388 &per_objfile->per_bfd->debug_names, *map))
927aa2e7
JK
5389 return false;
5390
5391 /* Don't use the index if it's empty. */
22ca247e 5392 if (map->name_count == 0)
927aa2e7
JK
5393 return false;
5394
5395 /* If there is a .dwz file, read it so we can get its CU list as
5396 well. */
f8c41851 5397 dwz_file *dwz = dwarf2_get_dwz_file (per_bfd);
927aa2e7
JK
5398 if (dwz != NULL)
5399 {
5400 if (!read_debug_names_from_section (objfile,
00f93c44 5401 bfd_get_filename (dwz->dwz_bfd.get ()),
927aa2e7
JK
5402 &dwz->debug_names, dwz_map))
5403 {
5404 warning (_("could not read '.debug_names' section from %s; skipping"),
00f93c44 5405 bfd_get_filename (dwz->dwz_bfd.get ()));
927aa2e7
JK
5406 return false;
5407 }
5408 }
5409
f8c41851 5410 create_cus_from_debug_names (per_bfd, *map, dwz_map);
927aa2e7 5411
22ca247e 5412 if (map->tu_count != 0)
927aa2e7
JK
5413 {
5414 /* We can only handle a single .debug_types when we have an
5415 index. */
f8c41851 5416 if (per_bfd->types.size () != 1)
927aa2e7
JK
5417 return false;
5418
f8c41851 5419 dwarf2_section_info *section = &per_bfd->types[0];
927aa2e7
JK
5420
5421 create_signatured_type_table_from_debug_names
f8c41851 5422 (per_objfile, *map, section, &per_bfd->abbrev);
927aa2e7
JK
5423 }
5424
f8c41851 5425 create_addrmap_from_aranges (per_objfile, &per_bfd->debug_aranges);
927aa2e7 5426
f8c41851
SM
5427 per_bfd->debug_names_table = std::move (map);
5428 per_bfd->using_index = 1;
5429 per_bfd->quick_file_names_table =
976ca316 5430 create_quick_file_names_table (per_objfile->per_bfd->all_comp_units.size ());
927aa2e7 5431
f8c41851
SM
5432 /* Save partial symtabs in the per_bfd object, for the benefit of subsequent
5433 objfiles using the same BFD. */
5434 gdb_assert (per_bfd->partial_symtabs == nullptr);
5435 per_bfd->partial_symtabs = objfile->partial_symtabs;
5436
927aa2e7
JK
5437 return true;
5438}
5439
927aa2e7
JK
5440/* Type used to manage iterating over all CUs looking for a symbol for
5441 .debug_names. */
5442
5443class dw2_debug_names_iterator
5444{
5445public:
927aa2e7 5446 dw2_debug_names_iterator (const mapped_debug_names &map,
2b79f376
SM
5447 gdb::optional<block_enum> block_index,
5448 domain_enum domain,
fcf23d5b 5449 const char *name, dwarf2_per_objfile *per_objfile)
2b79f376 5450 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5451 m_addr (find_vec_in_debug_names (map, name, per_objfile)),
5452 m_per_objfile (per_objfile)
927aa2e7
JK
5453 {}
5454
5455 dw2_debug_names_iterator (const mapped_debug_names &map,
fcf23d5b 5456 search_domain search, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5457 : m_map (map),
5458 m_search (search),
fcf23d5b
SM
5459 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5460 m_per_objfile (per_objfile)
927aa2e7
JK
5461 {}
5462
3b00ef10
TT
5463 dw2_debug_names_iterator (const mapped_debug_names &map,
5464 block_enum block_index, domain_enum domain,
fcf23d5b 5465 uint32_t namei, dwarf2_per_objfile *per_objfile)
3b00ef10 5466 : m_map (map), m_block_index (block_index), m_domain (domain),
fcf23d5b
SM
5467 m_addr (find_vec_in_debug_names (map, namei, per_objfile)),
5468 m_per_objfile (per_objfile)
3b00ef10
TT
5469 {}
5470
927aa2e7
JK
5471 /* Return the next matching CU or NULL if there are no more. */
5472 dwarf2_per_cu_data *next ();
5473
5474private:
5475 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5476 const char *name,
5477 dwarf2_per_objfile *per_objfile);
927aa2e7 5478 static const gdb_byte *find_vec_in_debug_names (const mapped_debug_names &map,
976ca316
SM
5479 uint32_t namei,
5480 dwarf2_per_objfile *per_objfile);
927aa2e7
JK
5481
5482 /* The internalized form of .debug_names. */
5483 const mapped_debug_names &m_map;
5484
2b79f376
SM
5485 /* If set, only look for symbols that match that block. Valid values are
5486 GLOBAL_BLOCK and STATIC_BLOCK. */
5487 const gdb::optional<block_enum> m_block_index;
927aa2e7
JK
5488
5489 /* The kind of symbol we're looking for. */
5490 const domain_enum m_domain = UNDEF_DOMAIN;
5491 const search_domain m_search = ALL_DOMAIN;
5492
5493 /* The list of CUs from the index entry of the symbol, or NULL if
5494 not found. */
5495 const gdb_byte *m_addr;
fcf23d5b
SM
5496
5497 dwarf2_per_objfile *m_per_objfile;
927aa2e7
JK
5498};
5499
5500const char *
fcf23d5b 5501mapped_debug_names::namei_to_name
976ca316 5502 (uint32_t namei, dwarf2_per_objfile *per_objfile) const
927aa2e7
JK
5503{
5504 const ULONGEST namei_string_offs
5505 = extract_unsigned_integer ((name_table_string_offs_reordered
5506 + namei * offset_size),
5507 offset_size,
5508 dwarf5_byte_order);
976ca316 5509 return read_indirect_string_at_offset (per_objfile, namei_string_offs);
927aa2e7
JK
5510}
5511
5512/* Find a slot in .debug_names for the object named NAME. If NAME is
5513 found, return pointer to its pool data. If NAME cannot be found,
5514 return NULL. */
5515
5516const gdb_byte *
5517dw2_debug_names_iterator::find_vec_in_debug_names
976ca316
SM
5518 (const mapped_debug_names &map, const char *name,
5519 dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5520{
5521 int (*cmp) (const char *, const char *);
5522
54ee4252 5523 gdb::unique_xmalloc_ptr<char> without_params;
927aa2e7
JK
5524 if (current_language->la_language == language_cplus
5525 || current_language->la_language == language_fortran
5526 || current_language->la_language == language_d)
5527 {
5528 /* NAME is already canonical. Drop any qualifiers as
5529 .debug_names does not contain any. */
5530
5531 if (strchr (name, '(') != NULL)
5532 {
54ee4252 5533 without_params = cp_remove_params (name);
927aa2e7 5534 if (without_params != NULL)
54ee4252 5535 name = without_params.get ();
927aa2e7
JK
5536 }
5537 }
5538
5539 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
5540
5541 const uint32_t full_hash = dwarf5_djb_hash (name);
5542 uint32_t namei
5543 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5544 (map.bucket_table_reordered
5545 + (full_hash % map.bucket_count)), 4,
5546 map.dwarf5_byte_order);
5547 if (namei == 0)
5548 return NULL;
5549 --namei;
5550 if (namei >= map.name_count)
5551 {
b98664d3 5552 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5553 "[in module %s]"),
5554 namei, map.name_count,
fcf23d5b 5555 objfile_name (per_objfile->objfile));
927aa2e7
JK
5556 return NULL;
5557 }
5558
5559 for (;;)
5560 {
5561 const uint32_t namei_full_hash
5562 = extract_unsigned_integer (reinterpret_cast<const gdb_byte *>
5563 (map.hash_table_reordered + namei), 4,
5564 map.dwarf5_byte_order);
5565 if (full_hash % map.bucket_count != namei_full_hash % map.bucket_count)
5566 return NULL;
5567
5568 if (full_hash == namei_full_hash)
5569 {
fcf23d5b 5570 const char *const namei_string = map.namei_to_name (namei, per_objfile);
927aa2e7
JK
5571
5572#if 0 /* An expensive sanity check. */
5573 if (namei_full_hash != dwarf5_djb_hash (namei_string))
5574 {
b98664d3 5575 complaint (_("Wrong .debug_names hash for string at index %u "
927aa2e7
JK
5576 "[in module %s]"),
5577 namei, objfile_name (dwarf2_per_objfile->objfile));
5578 return NULL;
5579 }
5580#endif
5581
5582 if (cmp (namei_string, name) == 0)
5583 {
5584 const ULONGEST namei_entry_offs
5585 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5586 + namei * map.offset_size),
5587 map.offset_size, map.dwarf5_byte_order);
5588 return map.entry_pool + namei_entry_offs;
5589 }
5590 }
5591
5592 ++namei;
5593 if (namei >= map.name_count)
5594 return NULL;
5595 }
5596}
5597
5598const gdb_byte *
5599dw2_debug_names_iterator::find_vec_in_debug_names
fcf23d5b 5600 (const mapped_debug_names &map, uint32_t namei, dwarf2_per_objfile *per_objfile)
927aa2e7
JK
5601{
5602 if (namei >= map.name_count)
5603 {
b98664d3 5604 complaint (_("Wrong .debug_names with name index %u but name_count=%u "
927aa2e7
JK
5605 "[in module %s]"),
5606 namei, map.name_count,
fcf23d5b 5607 objfile_name (per_objfile->objfile));
927aa2e7
JK
5608 return NULL;
5609 }
5610
5611 const ULONGEST namei_entry_offs
5612 = extract_unsigned_integer ((map.name_table_entry_offs_reordered
5613 + namei * map.offset_size),
5614 map.offset_size, map.dwarf5_byte_order);
5615 return map.entry_pool + namei_entry_offs;
5616}
5617
5618/* See dw2_debug_names_iterator. */
5619
5620dwarf2_per_cu_data *
5621dw2_debug_names_iterator::next ()
5622{
5623 if (m_addr == NULL)
5624 return NULL;
5625
fcf23d5b
SM
5626 dwarf2_per_bfd *per_bfd = m_per_objfile->per_bfd;
5627 struct objfile *objfile = m_per_objfile->objfile;
ed2dc618 5628 bfd *const abfd = objfile->obfd;
927aa2e7
JK
5629
5630 again:
5631
5632 unsigned int bytes_read;
5633 const ULONGEST abbrev = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5634 m_addr += bytes_read;
5635 if (abbrev == 0)
5636 return NULL;
5637
5638 const auto indexval_it = m_map.abbrev_map.find (abbrev);
5639 if (indexval_it == m_map.abbrev_map.cend ())
5640 {
b98664d3 5641 complaint (_("Wrong .debug_names undefined abbrev code %s "
927aa2e7 5642 "[in module %s]"),
ed2dc618 5643 pulongest (abbrev), objfile_name (objfile));
927aa2e7
JK
5644 return NULL;
5645 }
5646 const mapped_debug_names::index_val &indexval = indexval_it->second;
beadd3e8
SM
5647 enum class symbol_linkage {
5648 unknown,
5649 static_,
5650 extern_,
23c13d42 5651 } symbol_linkage_ = symbol_linkage::unknown;
927aa2e7
JK
5652 dwarf2_per_cu_data *per_cu = NULL;
5653 for (const mapped_debug_names::index_val::attr &attr : indexval.attr_vec)
5654 {
5655 ULONGEST ull;
5656 switch (attr.form)
5657 {
5658 case DW_FORM_implicit_const:
5659 ull = attr.implicit_const;
5660 break;
5661 case DW_FORM_flag_present:
5662 ull = 1;
5663 break;
5664 case DW_FORM_udata:
5665 ull = read_unsigned_leb128 (abfd, m_addr, &bytes_read);
5666 m_addr += bytes_read;
5667 break;
6dc55ce9 5668 case DW_FORM_ref4:
5669 ull = read_4_bytes (abfd, m_addr);
5670 m_addr += 4;
5671 break;
5672 case DW_FORM_ref8:
5673 ull = read_8_bytes (abfd, m_addr);
5674 m_addr += 8;
5675 break;
5676 case DW_FORM_ref_sig8:
5677 ull = read_8_bytes (abfd, m_addr);
5678 m_addr += 8;
5679 break;
927aa2e7 5680 default:
b98664d3 5681 complaint (_("Unsupported .debug_names form %s [in module %s]"),
927aa2e7 5682 dwarf_form_name (attr.form),
ed2dc618 5683 objfile_name (objfile));
927aa2e7
JK
5684 return NULL;
5685 }
5686 switch (attr.dw_idx)
5687 {
5688 case DW_IDX_compile_unit:
5689 /* Don't crash on bad data. */
fcf23d5b 5690 if (ull >= m_per_objfile->per_bfd->all_comp_units.size ())
927aa2e7 5691 {
b98664d3 5692 complaint (_(".debug_names entry has bad CU index %s"
927aa2e7
JK
5693 " [in module %s]"),
5694 pulongest (ull),
fcf23d5b 5695 objfile_name (objfile));
927aa2e7
JK
5696 continue;
5697 }
fcf23d5b 5698 per_cu = per_bfd->get_cutu (ull);
927aa2e7 5699 break;
8af5c486
JK
5700 case DW_IDX_type_unit:
5701 /* Don't crash on bad data. */
fcf23d5b 5702 if (ull >= per_bfd->all_type_units.size ())
8af5c486 5703 {
b98664d3 5704 complaint (_(".debug_names entry has bad TU index %s"
8af5c486
JK
5705 " [in module %s]"),
5706 pulongest (ull),
fcf23d5b 5707 objfile_name (objfile));
8af5c486
JK
5708 continue;
5709 }
fcf23d5b 5710 per_cu = &per_bfd->get_tu (ull)->per_cu;
8af5c486 5711 break;
6dc55ce9 5712 case DW_IDX_die_offset:
5713 /* In a per-CU index (as opposed to a per-module index), index
5714 entries without CU attribute implicitly refer to the single CU. */
5715 if (per_cu == NULL)
fcf23d5b 5716 per_cu = per_bfd->get_cu (0);
6dc55ce9 5717 break;
927aa2e7
JK
5718 case DW_IDX_GNU_internal:
5719 if (!m_map.augmentation_is_gdb)
5720 break;
23c13d42 5721 symbol_linkage_ = symbol_linkage::static_;
927aa2e7
JK
5722 break;
5723 case DW_IDX_GNU_external:
5724 if (!m_map.augmentation_is_gdb)
5725 break;
23c13d42 5726 symbol_linkage_ = symbol_linkage::extern_;
927aa2e7
JK
5727 break;
5728 }
5729 }
5730
5731 /* Skip if already read in. */
fcf23d5b 5732 if (m_per_objfile->symtab_set_p (per_cu))
927aa2e7
JK
5733 goto again;
5734
5735 /* Check static vs global. */
23c13d42 5736 if (symbol_linkage_ != symbol_linkage::unknown && m_block_index.has_value ())
927aa2e7 5737 {
2b79f376 5738 const bool want_static = *m_block_index == STATIC_BLOCK;
23c13d42
SM
5739 const bool symbol_is_static =
5740 symbol_linkage_ == symbol_linkage::static_;
beadd3e8 5741 if (want_static != symbol_is_static)
2b79f376 5742 goto again;
927aa2e7
JK
5743 }
5744
5745 /* Match dw2_symtab_iter_next, symbol_kind
5746 and debug_names::psymbol_tag. */
5747 switch (m_domain)
5748 {
5749 case VAR_DOMAIN:
5750 switch (indexval.dwarf_tag)
5751 {
5752 case DW_TAG_variable:
5753 case DW_TAG_subprogram:
5754 /* Some types are also in VAR_DOMAIN. */
5755 case DW_TAG_typedef:
5756 case DW_TAG_structure_type:
5757 break;
5758 default:
5759 goto again;
5760 }
5761 break;
5762 case STRUCT_DOMAIN:
5763 switch (indexval.dwarf_tag)
5764 {
5765 case DW_TAG_typedef:
5766 case DW_TAG_structure_type:
5767 break;
5768 default:
5769 goto again;
5770 }
5771 break;
5772 case LABEL_DOMAIN:
5773 switch (indexval.dwarf_tag)
5774 {
5775 case 0:
5776 case DW_TAG_variable:
5777 break;
5778 default:
5779 goto again;
5780 }
5781 break;
59c35742
AB
5782 case MODULE_DOMAIN:
5783 switch (indexval.dwarf_tag)
5784 {
5785 case DW_TAG_module:
5786 break;
5787 default:
5788 goto again;
5789 }
5790 break;
927aa2e7
JK
5791 default:
5792 break;
5793 }
5794
5795 /* Match dw2_expand_symtabs_matching, symbol_kind and
5796 debug_names::psymbol_tag. */
5797 switch (m_search)
4b514bc8 5798 {
927aa2e7
JK
5799 case VARIABLES_DOMAIN:
5800 switch (indexval.dwarf_tag)
4b514bc8 5801 {
927aa2e7
JK
5802 case DW_TAG_variable:
5803 break;
5804 default:
5805 goto again;
4b514bc8 5806 }
927aa2e7
JK
5807 break;
5808 case FUNCTIONS_DOMAIN:
5809 switch (indexval.dwarf_tag)
4b514bc8 5810 {
927aa2e7
JK
5811 case DW_TAG_subprogram:
5812 break;
5813 default:
5814 goto again;
4b514bc8 5815 }
927aa2e7
JK
5816 break;
5817 case TYPES_DOMAIN:
5818 switch (indexval.dwarf_tag)
5819 {
5820 case DW_TAG_typedef:
5821 case DW_TAG_structure_type:
5822 break;
5823 default:
5824 goto again;
5825 }
5826 break;
59c35742
AB
5827 case MODULES_DOMAIN:
5828 switch (indexval.dwarf_tag)
5829 {
5830 case DW_TAG_module:
5831 break;
5832 default:
5833 goto again;
5834 }
927aa2e7
JK
5835 default:
5836 break;
4b514bc8 5837 }
927aa2e7
JK
5838
5839 return per_cu;
4b514bc8 5840}
61920122 5841
927aa2e7 5842static struct compunit_symtab *
c7f839cb 5843dw2_debug_names_lookup_symbol (struct objfile *objfile, block_enum block_index,
927aa2e7 5844 const char *name, domain_enum domain)
4b514bc8 5845{
976ca316 5846 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
61920122 5847
976ca316 5848 const auto &mapp = per_objfile->per_bfd->debug_names_table;
927aa2e7 5849 if (!mapp)
61920122 5850 {
927aa2e7
JK
5851 /* index is NULL if OBJF_READNOW. */
5852 return NULL;
5853 }
5854 const auto &map = *mapp;
9291a0cd 5855
976ca316 5856 dw2_debug_names_iterator iter (map, block_index, domain, name, per_objfile);
9703b513 5857
927aa2e7
JK
5858 struct compunit_symtab *stab_best = NULL;
5859 struct dwarf2_per_cu_data *per_cu;
5860 while ((per_cu = iter.next ()) != NULL)
5861 {
5862 struct symbol *sym, *with_opaque = NULL;
97a1449a 5863 compunit_symtab *stab
976ca316 5864 = dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7 5865 const struct blockvector *bv = COMPUNIT_BLOCKVECTOR (stab);
582942f4 5866 const struct block *block = BLOCKVECTOR_BLOCK (bv, block_index);
9703b513 5867
927aa2e7
JK
5868 sym = block_find_symbol (block, name, domain,
5869 block_find_non_opaque_type_preferred,
5870 &with_opaque);
9703b513 5871
927aa2e7
JK
5872 /* Some caution must be observed with overloaded functions and
5873 methods, since the index will not contain any overload
5874 information (but NAME might contain it). */
a3ec0bb1 5875
927aa2e7 5876 if (sym != NULL
987012b8 5877 && strcmp_iw (sym->search_name (), name) == 0)
927aa2e7
JK
5878 return stab;
5879 if (with_opaque != NULL
987012b8 5880 && strcmp_iw (with_opaque->search_name (), name) == 0)
927aa2e7 5881 stab_best = stab;
9703b513 5882
927aa2e7 5883 /* Keep looking through other CUs. */
9703b513
TT
5884 }
5885
927aa2e7 5886 return stab_best;
9703b513
TT
5887}
5888
927aa2e7
JK
5889/* This dumps minimal information about .debug_names. It is called
5890 via "mt print objfiles". The gdb.dwarf2/gdb-index.exp testcase
5891 uses this to verify that .debug_names has been loaded. */
9291a0cd 5892
927aa2e7
JK
5893static void
5894dw2_debug_names_dump (struct objfile *objfile)
5895{
976ca316 5896 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 5897
976ca316 5898 gdb_assert (per_objfile->per_bfd->using_index);
927aa2e7 5899 printf_filtered (".debug_names:");
976ca316 5900 if (per_objfile->per_bfd->debug_names_table)
927aa2e7
JK
5901 printf_filtered (" exists\n");
5902 else
5903 printf_filtered (" faked for \"readnow\"\n");
5904 printf_filtered ("\n");
9291a0cd
TT
5905}
5906
9291a0cd 5907static void
927aa2e7
JK
5908dw2_debug_names_expand_symtabs_for_function (struct objfile *objfile,
5909 const char *func_name)
9291a0cd 5910{
976ca316 5911 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ae2de4f8 5912
976ca316
SM
5913 /* per_objfile->per_bfd->debug_names_table is NULL if OBJF_READNOW. */
5914 if (per_objfile->per_bfd->debug_names_table)
24c79950 5915 {
976ca316 5916 const mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
24c79950 5917
fcf23d5b 5918 dw2_debug_names_iterator iter (map, {}, VAR_DOMAIN, func_name,
976ca316 5919 per_objfile);
24c79950 5920
927aa2e7
JK
5921 struct dwarf2_per_cu_data *per_cu;
5922 while ((per_cu = iter.next ()) != NULL)
976ca316 5923 dw2_instantiate_symtab (per_cu, per_objfile, false);
927aa2e7
JK
5924 }
5925}
24c79950 5926
3b00ef10
TT
5927static void
5928dw2_debug_names_map_matching_symbols
5929 (struct objfile *objfile,
5930 const lookup_name_info &name, domain_enum domain,
5931 int global,
5932 gdb::function_view<symbol_found_callback_ftype> callback,
5933 symbol_compare_ftype *ordered_compare)
5934{
976ca316 5935 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
3b00ef10
TT
5936
5937 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5938 if (!per_objfile->per_bfd->debug_names_table)
3b00ef10
TT
5939 return;
5940
976ca316 5941 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
3b00ef10
TT
5942 const block_enum block_kind = global ? GLOBAL_BLOCK : STATIC_BLOCK;
5943
5944 const char *match_name = name.ada ().lookup_name ().c_str ();
5945 auto matcher = [&] (const char *symname)
5946 {
5947 if (ordered_compare == nullptr)
5948 return true;
5949 return ordered_compare (symname, match_name) == 0;
5950 };
5951
5952 dw2_expand_symtabs_matching_symbol (map, name, matcher, ALL_DOMAIN,
5953 [&] (offset_type namei)
5954 {
5955 /* The name was matched, now expand corresponding CUs that were
5956 marked. */
fcf23d5b 5957 dw2_debug_names_iterator iter (map, block_kind, domain, namei,
976ca316 5958 per_objfile);
3b00ef10
TT
5959
5960 struct dwarf2_per_cu_data *per_cu;
5961 while ((per_cu = iter.next ()) != NULL)
976ca316 5962 dw2_expand_symtabs_matching_one (per_cu, per_objfile, nullptr,
97a1449a 5963 nullptr);
3b00ef10 5964 return true;
976ca316 5965 }, per_objfile);
3b00ef10
TT
5966
5967 /* It's a shame we couldn't do this inside the
5968 dw2_expand_symtabs_matching_symbol callback, but that skips CUs
5969 that have already been expanded. Instead, this loop matches what
5970 the psymtab code does. */
976ca316 5971 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3b00ef10 5972 {
976ca316 5973 compunit_symtab *symtab = per_objfile->get_symtab (per_cu);
af758d11 5974 if (symtab != nullptr)
3b00ef10
TT
5975 {
5976 const struct block *block
af758d11 5977 = BLOCKVECTOR_BLOCK (COMPUNIT_BLOCKVECTOR (symtab), block_kind);
3b00ef10
TT
5978 if (!iterate_over_symbols_terminated (block, name,
5979 domain, callback))
5980 break;
5981 }
5982 }
5983}
5984
927aa2e7
JK
5985static void
5986dw2_debug_names_expand_symtabs_matching
5987 (struct objfile *objfile,
5988 gdb::function_view<expand_symtabs_file_matcher_ftype> file_matcher,
c1a66c06 5989 const lookup_name_info *lookup_name,
927aa2e7
JK
5990 gdb::function_view<expand_symtabs_symbol_matcher_ftype> symbol_matcher,
5991 gdb::function_view<expand_symtabs_exp_notify_ftype> expansion_notify,
5992 enum search_domain kind)
5993{
976ca316 5994 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9291a0cd 5995
927aa2e7 5996 /* debug_names_table is NULL if OBJF_READNOW. */
976ca316 5997 if (!per_objfile->per_bfd->debug_names_table)
927aa2e7 5998 return;
9291a0cd 5999
976ca316 6000 dw_expand_symtabs_matching_file_matcher (per_objfile, file_matcher);
24c79950 6001
c1a66c06
TV
6002 if (symbol_matcher == NULL && lookup_name == NULL)
6003 {
976ca316 6004 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
c1a66c06
TV
6005 {
6006 QUIT;
6007
976ca316
SM
6008 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6009 expansion_notify);
c1a66c06
TV
6010 }
6011 return;
6012 }
6013
976ca316 6014 mapped_debug_names &map = *per_objfile->per_bfd->debug_names_table;
bbf2f4df 6015
c1a66c06 6016 dw2_expand_symtabs_matching_symbol (map, *lookup_name,
44ed8f3e
PA
6017 symbol_matcher,
6018 kind, [&] (offset_type namei)
927aa2e7 6019 {
927aa2e7
JK
6020 /* The name was matched, now expand corresponding CUs that were
6021 marked. */
976ca316 6022 dw2_debug_names_iterator iter (map, kind, namei, per_objfile);
bbf2f4df 6023
927aa2e7
JK
6024 struct dwarf2_per_cu_data *per_cu;
6025 while ((per_cu = iter.next ()) != NULL)
976ca316
SM
6026 dw2_expand_symtabs_matching_one (per_cu, per_objfile, file_matcher,
6027 expansion_notify);
3b00ef10 6028 return true;
976ca316 6029 }, per_objfile);
9291a0cd
TT
6030}
6031
927aa2e7 6032const struct quick_symbol_functions dwarf2_debug_names_functions =
9291a0cd
TT
6033{
6034 dw2_has_symbols,
6035 dw2_find_last_source_symtab,
6036 dw2_forget_cached_source_info,
f8eba3c6 6037 dw2_map_symtabs_matching_filename,
927aa2e7 6038 dw2_debug_names_lookup_symbol,
d3214198 6039 NULL,
9291a0cd 6040 dw2_print_stats,
927aa2e7 6041 dw2_debug_names_dump,
927aa2e7 6042 dw2_debug_names_expand_symtabs_for_function,
9291a0cd 6043 dw2_expand_all_symtabs,
652a8996 6044 dw2_expand_symtabs_with_fullname,
3b00ef10 6045 dw2_debug_names_map_matching_symbols,
927aa2e7 6046 dw2_debug_names_expand_symtabs_matching,
43f3e411 6047 dw2_find_pc_sect_compunit_symtab,
71a3c369 6048 NULL,
9291a0cd
TT
6049 dw2_map_symbol_filenames
6050};
6051
4485a1c1 6052/* Get the content of the .gdb_index section of OBJ. SECTION_OWNER should point
5989a64e 6053 to either a dwarf2_per_bfd or dwz_file object. */
4485a1c1
SM
6054
6055template <typename T>
6056static gdb::array_view<const gdb_byte>
6057get_gdb_index_contents_from_section (objfile *obj, T *section_owner)
6058{
6059 dwarf2_section_info *section = &section_owner->gdb_index;
6060
96b79293 6061 if (section->empty ())
4485a1c1
SM
6062 return {};
6063
6064 /* Older elfutils strip versions could keep the section in the main
6065 executable while splitting it for the separate debug info file. */
96b79293 6066 if ((section->get_flags () & SEC_HAS_CONTENTS) == 0)
4485a1c1
SM
6067 return {};
6068
96b79293 6069 section->read (obj);
4485a1c1 6070
8bebfcda
PA
6071 /* dwarf2_section_info::size is a bfd_size_type, while
6072 gdb::array_view works with size_t. On 32-bit hosts, with
6073 --enable-64-bit-bfd, bfd_size_type is a 64-bit type, while size_t
6074 is 32-bit. So we need an explicit narrowing conversion here.
6075 This is fine, because it's impossible to allocate or mmap an
6076 array/buffer larger than what size_t can represent. */
6077 return gdb::make_array_view (section->buffer, section->size);
4485a1c1
SM
6078}
6079
87d6a7aa
SM
6080/* Lookup the index cache for the contents of the index associated to
6081 DWARF2_OBJ. */
6082
6083static gdb::array_view<const gdb_byte>
5989a64e 6084get_gdb_index_contents_from_cache (objfile *obj, dwarf2_per_bfd *dwarf2_per_bfd)
87d6a7aa
SM
6085{
6086 const bfd_build_id *build_id = build_id_bfd_get (obj->obfd);
6087 if (build_id == nullptr)
6088 return {};
6089
6090 return global_index_cache.lookup_gdb_index (build_id,
5989a64e 6091 &dwarf2_per_bfd->index_cache_res);
87d6a7aa
SM
6092}
6093
6094/* Same as the above, but for DWZ. */
6095
6096static gdb::array_view<const gdb_byte>
6097get_gdb_index_contents_from_cache_dwz (objfile *obj, dwz_file *dwz)
6098{
6099 const bfd_build_id *build_id = build_id_bfd_get (dwz->dwz_bfd.get ());
6100 if (build_id == nullptr)
6101 return {};
6102
6103 return global_index_cache.lookup_gdb_index (build_id, &dwz->index_cache_res);
6104}
6105
3c0aa29a 6106/* See symfile.h. */
9291a0cd 6107
3c0aa29a
PA
6108bool
6109dwarf2_initialize_objfile (struct objfile *objfile, dw_index_kind *index_kind)
9291a0cd 6110{
976ca316
SM
6111 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6112 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
ed2dc618 6113
9291a0cd
TT
6114 /* If we're about to read full symbols, don't bother with the
6115 indices. In this case we also don't care if some other debug
6116 format is making psymtabs, because they are all about to be
6117 expanded anyway. */
6118 if ((objfile->flags & OBJF_READNOW))
6119 {
17ee85fc
TT
6120 /* When using READNOW, the using_index flag (set below) indicates that
6121 PER_BFD was already initialized, when we loaded some other objfile. */
6122 if (per_bfd->using_index)
6123 {
6124 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6125 per_objfile->resize_symtabs ();
17ee85fc
TT
6126 return true;
6127 }
6128
6129 per_bfd->using_index = 1;
976ca316
SM
6130 create_all_comp_units (per_objfile);
6131 create_all_type_units (per_objfile);
17ee85fc
TT
6132 per_bfd->quick_file_names_table
6133 = create_quick_file_names_table (per_bfd->all_comp_units.size ());
976ca316 6134 per_objfile->resize_symtabs ();
9291a0cd 6135
17ee85fc
TT
6136 for (int i = 0; i < (per_bfd->all_comp_units.size ()
6137 + per_bfd->all_type_units.size ()); ++i)
9291a0cd 6138 {
17ee85fc 6139 dwarf2_per_cu_data *per_cu = per_bfd->get_cutu (i);
9291a0cd 6140
17ee85fc 6141 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
e254ef6a 6142 struct dwarf2_per_cu_quick_data);
9291a0cd
TT
6143 }
6144
6145 /* Return 1 so that gdb sees the "quick" functions. However,
6146 these functions will be no-ops because we will have expanded
6147 all symtabs. */
3c0aa29a
PA
6148 *index_kind = dw_index_kind::GDB_INDEX;
6149 return true;
9291a0cd
TT
6150 }
6151
17ee85fc
TT
6152 /* Was a debug names index already read when we processed an objfile sharing
6153 PER_BFD? */
6154 if (per_bfd->debug_names_table != nullptr)
6155 {
6156 *index_kind = dw_index_kind::DEBUG_NAMES;
f8c41851 6157 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6158 per_objfile->resize_symtabs ();
17ee85fc
TT
6159 return true;
6160 }
6161
6162 /* Was a GDB index already read when we processed an objfile sharing
6163 PER_BFD? */
6164 if (per_bfd->index_table != nullptr)
6165 {
6166 *index_kind = dw_index_kind::GDB_INDEX;
f8c41851 6167 per_objfile->objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6168 per_objfile->resize_symtabs ();
17ee85fc
TT
6169 return true;
6170 }
6171
efb763a5
SM
6172 /* There might already be partial symtabs built for this BFD. This happens
6173 when loading the same binary twice with the index-cache enabled. If so,
6174 don't try to read an index. The objfile / per_objfile initialization will
6175 be completed in dwarf2_build_psymtabs, in the standard partial symtabs
6176 code path. */
6177 if (per_bfd->partial_symtabs != nullptr)
6178 return false;
6179
976ca316 6180 if (dwarf2_read_debug_names (per_objfile))
3c0aa29a
PA
6181 {
6182 *index_kind = dw_index_kind::DEBUG_NAMES;
976ca316 6183 per_objfile->resize_symtabs ();
3c0aa29a
PA
6184 return true;
6185 }
927aa2e7 6186
976ca316 6187 if (dwarf2_read_gdb_index (per_objfile,
5989a64e 6188 get_gdb_index_contents_from_section<struct dwarf2_per_bfd>,
4485a1c1 6189 get_gdb_index_contents_from_section<dwz_file>))
3c0aa29a
PA
6190 {
6191 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6192 per_objfile->resize_symtabs ();
3c0aa29a
PA
6193 return true;
6194 }
9291a0cd 6195
87d6a7aa 6196 /* ... otherwise, try to find the index in the index cache. */
976ca316 6197 if (dwarf2_read_gdb_index (per_objfile,
87d6a7aa
SM
6198 get_gdb_index_contents_from_cache,
6199 get_gdb_index_contents_from_cache_dwz))
6200 {
6201 global_index_cache.hit ();
6202 *index_kind = dw_index_kind::GDB_INDEX;
976ca316 6203 per_objfile->resize_symtabs ();
87d6a7aa
SM
6204 return true;
6205 }
6206
6207 global_index_cache.miss ();
3c0aa29a 6208 return false;
9291a0cd
TT
6209}
6210
6211\f
6212
dce234bc
PP
6213/* Build a partial symbol table. */
6214
6215void
f29dff0a 6216dwarf2_build_psymtabs (struct objfile *objfile)
dce234bc 6217{
976ca316
SM
6218 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
6219 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
17ee85fc
TT
6220
6221 if (per_bfd->partial_symtabs != nullptr)
6222 {
6223 /* Partial symbols were already read, so now we can simply
6224 attach them. */
6225 objfile->partial_symtabs = per_bfd->partial_symtabs;
976ca316 6226 per_objfile->resize_symtabs ();
17ee85fc
TT
6227 return;
6228 }
c9bf0622 6229
a70b8144 6230 try
c9bf0622
TT
6231 {
6232 /* This isn't really ideal: all the data we allocate on the
6233 objfile's obstack is still uselessly kept around. However,
6234 freeing it seems unsafe. */
906768f9 6235 psymtab_discarder psymtabs (objfile);
976ca316 6236 dwarf2_build_psymtabs_hard (per_objfile);
906768f9 6237 psymtabs.keep ();
87d6a7aa 6238
976ca316 6239 per_objfile->resize_symtabs ();
af758d11 6240
87d6a7aa 6241 /* (maybe) store an index in the cache. */
976ca316 6242 global_index_cache.store (per_objfile);
c9bf0622 6243 }
230d2906 6244 catch (const gdb_exception_error &except)
492d29ea
PA
6245 {
6246 exception_print (gdb_stderr, except);
6247 }
17ee85fc
TT
6248
6249 /* Finish by setting the local reference to partial symtabs, so that
6250 we don't try to read them again if reading another objfile with the same
6251 BFD. If we can't in fact share, this won't make a difference anyway as
6252 the dwarf2_per_bfd object won't be shared. */
6253 per_bfd->partial_symtabs = objfile->partial_symtabs;
c906108c 6254}
c906108c 6255
3b80fe9b
DE
6256/* Find the base address of the compilation unit for range lists and
6257 location lists. It will normally be specified by DW_AT_low_pc.
6258 In DWARF-3 draft 4, the base address could be overridden by
6259 DW_AT_entry_pc. It's been removed, but GCC still uses this for
6260 compilation units with discontinuous ranges. */
6261
6262static void
6263dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
6264{
6265 struct attribute *attr;
6266
2b24b6e4 6267 cu->base_address.reset ();
3b80fe9b
DE
6268
6269 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
435d3d88 6270 if (attr != nullptr)
95f982e5 6271 cu->base_address = attr->as_address ();
3b80fe9b
DE
6272 else
6273 {
6274 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 6275 if (attr != nullptr)
95f982e5 6276 cu->base_address = attr->as_address ();
3b80fe9b
DE
6277 }
6278}
6279
36586728
TT
6280/* Helper function that returns the proper abbrev section for
6281 THIS_CU. */
6282
6283static struct dwarf2_section_info *
6284get_abbrev_section_for_cu (struct dwarf2_per_cu_data *this_cu)
6285{
6286 struct dwarf2_section_info *abbrev;
c3699833 6287 dwarf2_per_bfd *per_bfd = this_cu->per_bfd;
36586728
TT
6288
6289 if (this_cu->is_dwz)
c3699833 6290 abbrev = &dwarf2_get_dwz_file (per_bfd)->abbrev;
36586728 6291 else
c3699833 6292 abbrev = &per_bfd->abbrev;
36586728
TT
6293
6294 return abbrev;
6295}
6296
f4dc4d17
DE
6297/* Fetch the abbreviation table offset from a comp or type unit header. */
6298
6299static sect_offset
976ca316 6300read_abbrev_offset (dwarf2_per_objfile *per_objfile,
ed2dc618 6301 struct dwarf2_section_info *section,
9c541725 6302 sect_offset sect_off)
f4dc4d17 6303{
96b79293 6304 bfd *abfd = section->get_bfd_owner ();
d521ce57 6305 const gdb_byte *info_ptr;
ac298888 6306 unsigned int initial_length_size, offset_size;
43988095 6307 uint16_t version;
f4dc4d17 6308
976ca316 6309 section->read (per_objfile->objfile);
9c541725 6310 info_ptr = section->buffer + to_underlying (sect_off);
ac298888 6311 read_initial_length (abfd, info_ptr, &initial_length_size);
f4dc4d17 6312 offset_size = initial_length_size == 4 ? 4 : 8;
43988095
JK
6313 info_ptr += initial_length_size;
6314
6315 version = read_2_bytes (abfd, info_ptr);
6316 info_ptr += 2;
6317 if (version >= 5)
6318 {
6319 /* Skip unit type and address size. */
6320 info_ptr += 2;
6321 }
6322
24aa364d 6323 return (sect_offset) read_offset (abfd, info_ptr, offset_size);
f4dc4d17
DE
6324}
6325
b83470bf
TT
6326/* A partial symtab that is used only for include files. */
6327struct dwarf2_include_psymtab : public partial_symtab
6328{
6329 dwarf2_include_psymtab (const char *filename, struct objfile *objfile)
6330 : partial_symtab (filename, objfile)
6331 {
6332 }
6333
6334 void read_symtab (struct objfile *objfile) override
6335 {
194d088f
TV
6336 /* It's an include file, no symbols to read for it.
6337 Everything is in the includer symtab. */
6338
6339 /* The expansion of a dwarf2_include_psymtab is just a trigger for
6340 expansion of the includer psymtab. We use the dependencies[0] field to
6341 model the includer. But if we go the regular route of calling
6342 expand_psymtab here, and having expand_psymtab call expand_dependencies
6343 to expand the includer, we'll only use expand_psymtab on the includer
6344 (making it a non-toplevel psymtab), while if we expand the includer via
6345 another path, we'll use read_symtab (making it a toplevel psymtab).
6346 So, don't pretend a dwarf2_include_psymtab is an actual toplevel
6347 psymtab, and trigger read_symtab on the includer here directly. */
6348 includer ()->read_symtab (objfile);
b83470bf
TT
6349 }
6350
6351 void expand_psymtab (struct objfile *objfile) override
6352 {
194d088f
TV
6353 /* This is not called by read_symtab, and should not be called by any
6354 expand_dependencies. */
6355 gdb_assert (false);
b83470bf
TT
6356 }
6357
5717c425 6358 bool readin_p (struct objfile *objfile) const override
b83470bf 6359 {
5717c425 6360 return includer ()->readin_p (objfile);
b83470bf
TT
6361 }
6362
5717c425 6363 compunit_symtab *get_compunit_symtab (struct objfile *objfile) const override
b83470bf
TT
6364 {
6365 return nullptr;
6366 }
6367
6368private:
194d088f
TV
6369 partial_symtab *includer () const
6370 {
6371 /* An include psymtab has exactly one dependency: the psymtab that
6372 includes it. */
6373 gdb_assert (this->number_of_dependencies == 1);
6374 return this->dependencies[0];
6375 }
b83470bf
TT
6376};
6377
aaa75496
JB
6378/* Allocate a new partial symtab for file named NAME and mark this new
6379 partial symtab as being an include of PST. */
6380
6381static void
891813be 6382dwarf2_create_include_psymtab (const char *name, dwarf2_psymtab *pst,
dda83cd7 6383 struct objfile *objfile)
aaa75496 6384{
b83470bf 6385 dwarf2_include_psymtab *subpst = new dwarf2_include_psymtab (name, objfile);
aaa75496 6386
fbd9ab74 6387 if (!IS_ABSOLUTE_PATH (subpst->filename))
45940949 6388 subpst->dirname = pst->dirname;
fbd9ab74 6389
a9342b62 6390 subpst->dependencies = objfile->partial_symtabs->allocate_dependencies (1);
aaa75496
JB
6391 subpst->dependencies[0] = pst;
6392 subpst->number_of_dependencies = 1;
aaa75496
JB
6393}
6394
6395/* Read the Line Number Program data and extract the list of files
6396 included by the source file represented by PST. Build an include
d85a05f0 6397 partial symtab for each of these included files. */
aaa75496
JB
6398
6399static void
6400dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
dee91e82 6401 struct die_info *die,
891813be 6402 dwarf2_psymtab *pst)
aaa75496 6403{
fff8551c 6404 line_header_up lh;
d85a05f0 6405 struct attribute *attr;
aaa75496 6406
d85a05f0 6407 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e
TT
6408 if (attr != nullptr && attr->form_is_unsigned ())
6409 lh = dwarf_decode_line_header ((sect_offset) attr->as_unsigned (), cu);
aaa75496
JB
6410 if (lh == NULL)
6411 return; /* No linetable, so no includes. */
6412
79748972
TT
6413 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). Also note
6414 that we pass in the raw text_low here; that is ok because we're
6415 only decoding the line table to make include partial symtabs, and
6416 so the addresses aren't really used. */
4ae976d1 6417 dwarf_decode_lines (lh.get (), pst->dirname, cu, pst,
79748972 6418 pst->raw_text_low (), 1);
aaa75496
JB
6419}
6420
348e048f 6421static hashval_t
52dc124a 6422hash_signatured_type (const void *item)
348e048f 6423{
9a3c8263
SM
6424 const struct signatured_type *sig_type
6425 = (const struct signatured_type *) item;
9a619af0 6426
348e048f 6427 /* This drops the top 32 bits of the signature, but is ok for a hash. */
52dc124a 6428 return sig_type->signature;
348e048f
DE
6429}
6430
6431static int
52dc124a 6432eq_signatured_type (const void *item_lhs, const void *item_rhs)
348e048f 6433{
9a3c8263
SM
6434 const struct signatured_type *lhs = (const struct signatured_type *) item_lhs;
6435 const struct signatured_type *rhs = (const struct signatured_type *) item_rhs;
9a619af0 6436
348e048f
DE
6437 return lhs->signature == rhs->signature;
6438}
6439
1fd400ff
TT
6440/* Allocate a hash table for signatured types. */
6441
b0b6a987 6442static htab_up
298e9637 6443allocate_signatured_type_table ()
1fd400ff 6444{
b0b6a987
TT
6445 return htab_up (htab_create_alloc (41,
6446 hash_signatured_type,
6447 eq_signatured_type,
6448 NULL, xcalloc, xfree));
1fd400ff
TT
6449}
6450
d467dd73 6451/* A helper function to add a signatured type CU to a table. */
1fd400ff
TT
6452
6453static int
d467dd73 6454add_signatured_type_cu_to_table (void **slot, void *datum)
1fd400ff 6455{
9a3c8263 6456 struct signatured_type *sigt = (struct signatured_type *) *slot;
b2bdb8cf
SM
6457 std::vector<signatured_type *> *all_type_units
6458 = (std::vector<signatured_type *> *) datum;
1fd400ff 6459
b2bdb8cf 6460 all_type_units->push_back (sigt);
1fd400ff
TT
6461
6462 return 1;
6463}
6464
78d4d2c5 6465/* A helper for create_debug_types_hash_table. Read types from SECTION
43988095
JK
6466 and fill them into TYPES_HTAB. It will process only type units,
6467 therefore DW_UT_type. */
c88ee1f0 6468
78d4d2c5 6469static void
976ca316 6470create_debug_type_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6471 struct dwo_file *dwo_file,
b0b6a987 6472 dwarf2_section_info *section, htab_up &types_htab,
43988095 6473 rcuh_kind section_kind)
348e048f 6474{
976ca316 6475 struct objfile *objfile = per_objfile->objfile;
4bdcc0c1 6476 struct dwarf2_section_info *abbrev_section;
78d4d2c5
JK
6477 bfd *abfd;
6478 const gdb_byte *info_ptr, *end_ptr;
348e048f 6479
4bdcc0c1
DE
6480 abbrev_section = (dwo_file != NULL
6481 ? &dwo_file->sections.abbrev
976ca316 6482 : &per_objfile->per_bfd->abbrev);
4bdcc0c1 6483
6f738b01
SM
6484 dwarf_read_debug_printf ("Reading %s for %s:",
6485 section->get_name (),
6486 abbrev_section->get_file_name ());
09406207 6487
96b79293 6488 section->read (objfile);
78d4d2c5 6489 info_ptr = section->buffer;
348e048f 6490
78d4d2c5
JK
6491 if (info_ptr == NULL)
6492 return;
348e048f 6493
78d4d2c5
JK
6494 /* We can't set abfd until now because the section may be empty or
6495 not present, in which case the bfd is unknown. */
96b79293 6496 abfd = section->get_bfd_owner ();
348e048f 6497
c0ab21c2
TT
6498 /* We don't use cutu_reader here because we don't need to read
6499 any dies: the signature is in the header. */
3019eac3 6500
78d4d2c5
JK
6501 end_ptr = info_ptr + section->size;
6502 while (info_ptr < end_ptr)
6503 {
78d4d2c5
JK
6504 struct signatured_type *sig_type;
6505 struct dwo_unit *dwo_tu;
6506 void **slot;
6507 const gdb_byte *ptr = info_ptr;
6508 struct comp_unit_head header;
6509 unsigned int length;
8b70b953 6510
9c541725 6511 sect_offset sect_off = (sect_offset) (ptr - section->buffer);
348e048f 6512
a49dd8dd
JK
6513 /* Initialize it due to a false compiler warning. */
6514 header.signature = -1;
9c541725 6515 header.type_cu_offset_in_tu = (cu_offset) -1;
a49dd8dd 6516
78d4d2c5
JK
6517 /* We need to read the type's signature in order to build the hash
6518 table, but we don't need anything else just yet. */
348e048f 6519
976ca316 6520 ptr = read_and_check_comp_unit_head (per_objfile, &header, section,
43988095 6521 abbrev_section, ptr, section_kind);
348e048f 6522
4057dfde 6523 length = header.get_length ();
6caca83c 6524
78d4d2c5
JK
6525 /* Skip dummy type units. */
6526 if (ptr >= info_ptr + length
43988095 6527 || peek_abbrev_code (abfd, ptr) == 0
d2854d8d
CT
6528 || (header.unit_type != DW_UT_type
6529 && header.unit_type != DW_UT_split_type))
78d4d2c5
JK
6530 {
6531 info_ptr += length;
6532 continue;
6533 }
dee91e82 6534
78d4d2c5
JK
6535 if (types_htab == NULL)
6536 {
6537 if (dwo_file)
298e9637 6538 types_htab = allocate_dwo_unit_table ();
78d4d2c5 6539 else
298e9637 6540 types_htab = allocate_signatured_type_table ();
78d4d2c5 6541 }
8b70b953 6542
78d4d2c5
JK
6543 if (dwo_file)
6544 {
6545 sig_type = NULL;
976ca316 6546 dwo_tu = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, dwo_unit);
78d4d2c5 6547 dwo_tu->dwo_file = dwo_file;
43988095 6548 dwo_tu->signature = header.signature;
9c541725 6549 dwo_tu->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5 6550 dwo_tu->section = section;
9c541725 6551 dwo_tu->sect_off = sect_off;
78d4d2c5
JK
6552 dwo_tu->length = length;
6553 }
6554 else
6555 {
6556 /* N.B.: type_offset is not usable if this type uses a DWO file.
6557 The real type_offset is in the DWO file. */
6558 dwo_tu = NULL;
976ca316 6559 sig_type = per_objfile->per_bfd->allocate_signatured_type ();
43988095 6560 sig_type->signature = header.signature;
9c541725 6561 sig_type->type_offset_in_tu = header.type_cu_offset_in_tu;
78d4d2c5
JK
6562 sig_type->per_cu.is_debug_types = 1;
6563 sig_type->per_cu.section = section;
9c541725 6564 sig_type->per_cu.sect_off = sect_off;
78d4d2c5
JK
6565 sig_type->per_cu.length = length;
6566 }
6567
b0b6a987 6568 slot = htab_find_slot (types_htab.get (),
78d4d2c5
JK
6569 dwo_file ? (void*) dwo_tu : (void *) sig_type,
6570 INSERT);
6571 gdb_assert (slot != NULL);
6572 if (*slot != NULL)
6573 {
9c541725 6574 sect_offset dup_sect_off;
0349ea22 6575
3019eac3
DE
6576 if (dwo_file)
6577 {
78d4d2c5
JK
6578 const struct dwo_unit *dup_tu
6579 = (const struct dwo_unit *) *slot;
6580
9c541725 6581 dup_sect_off = dup_tu->sect_off;
3019eac3
DE
6582 }
6583 else
6584 {
78d4d2c5
JK
6585 const struct signatured_type *dup_tu
6586 = (const struct signatured_type *) *slot;
6587
9c541725 6588 dup_sect_off = dup_tu->per_cu.sect_off;
3019eac3 6589 }
8b70b953 6590
b98664d3 6591 complaint (_("debug type entry at offset %s is duplicate to"
9d8780f0
SM
6592 " the entry at offset %s, signature %s"),
6593 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
43988095 6594 hex_string (header.signature));
78d4d2c5
JK
6595 }
6596 *slot = dwo_file ? (void *) dwo_tu : (void *) sig_type;
3019eac3 6597
6f738b01
SM
6598 dwarf_read_debug_printf_v (" offset %s, signature %s",
6599 sect_offset_str (sect_off),
6600 hex_string (header.signature));
3019eac3 6601
78d4d2c5
JK
6602 info_ptr += length;
6603 }
6604}
3019eac3 6605
78d4d2c5
JK
6606/* Create the hash table of all entries in the .debug_types
6607 (or .debug_types.dwo) section(s).
6608 If reading a DWO file, then DWO_FILE is a pointer to the DWO file object,
6609 otherwise it is NULL.
b3c8eb43 6610
78d4d2c5 6611 The result is a pointer to the hash table or NULL if there are no types.
348e048f 6612
78d4d2c5 6613 Note: This function processes DWO files only, not DWP files. */
348e048f 6614
78d4d2c5 6615static void
976ca316 6616create_debug_types_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 6617 struct dwo_file *dwo_file,
fd5866f6 6618 gdb::array_view<dwarf2_section_info> type_sections,
b0b6a987 6619 htab_up &types_htab)
78d4d2c5 6620{
fd5866f6 6621 for (dwarf2_section_info &section : type_sections)
976ca316
SM
6622 create_debug_type_hash_table (per_objfile, dwo_file, &section, types_htab,
6623 rcuh_kind::TYPE);
3019eac3
DE
6624}
6625
6626/* Create the hash table of all entries in the .debug_types section,
6627 and initialize all_type_units.
6628 The result is zero if there is an error (e.g. missing .debug_types section),
6629 otherwise non-zero. */
6630
6631static int
976ca316 6632create_all_type_units (dwarf2_per_objfile *per_objfile)
3019eac3 6633{
b0b6a987 6634 htab_up types_htab;
3019eac3 6635
976ca316
SM
6636 create_debug_type_hash_table (per_objfile, NULL, &per_objfile->per_bfd->info,
6637 types_htab, rcuh_kind::COMPILE);
6638 create_debug_types_hash_table (per_objfile, NULL, per_objfile->per_bfd->types,
6639 types_htab);
3019eac3
DE
6640 if (types_htab == NULL)
6641 {
976ca316 6642 per_objfile->per_bfd->signatured_types = NULL;
3019eac3
DE
6643 return 0;
6644 }
6645
976ca316 6646 per_objfile->per_bfd->signatured_types = std::move (types_htab);
348e048f 6647
976ca316
SM
6648 gdb_assert (per_objfile->per_bfd->all_type_units.empty ());
6649 per_objfile->per_bfd->all_type_units.reserve
6650 (htab_elements (per_objfile->per_bfd->signatured_types.get ()));
b2bdb8cf 6651
976ca316 6652 htab_traverse_noresize (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6653 add_signatured_type_cu_to_table,
976ca316 6654 &per_objfile->per_bfd->all_type_units);
1fd400ff 6655
348e048f
DE
6656 return 1;
6657}
6658
5989a64e 6659/* Add an entry for signature SIG to dwarf2_per_objfile->per_bfd->signatured_types.
6aa5f3a6
DE
6660 If SLOT is non-NULL, it is the entry to use in the hash table.
6661 Otherwise we find one. */
6662
6663static struct signatured_type *
976ca316 6664add_type_unit (dwarf2_per_objfile *per_objfile, ULONGEST sig, void **slot)
6aa5f3a6 6665{
976ca316
SM
6666 if (per_objfile->per_bfd->all_type_units.size ()
6667 == per_objfile->per_bfd->all_type_units.capacity ())
6668 ++per_objfile->per_bfd->tu_stats.nr_all_type_units_reallocs;
6aa5f3a6 6669
976ca316 6670 signatured_type *sig_type = per_objfile->per_bfd->allocate_signatured_type ();
b2bdb8cf 6671
976ca316 6672 per_objfile->resize_symtabs ();
af758d11 6673
976ca316 6674 per_objfile->per_bfd->all_type_units.push_back (sig_type);
6aa5f3a6
DE
6675 sig_type->signature = sig;
6676 sig_type->per_cu.is_debug_types = 1;
976ca316 6677 if (per_objfile->per_bfd->using_index)
6aa5f3a6
DE
6678 {
6679 sig_type->per_cu.v.quick =
976ca316 6680 OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack,
6aa5f3a6
DE
6681 struct dwarf2_per_cu_quick_data);
6682 }
6683
6684 if (slot == NULL)
6685 {
976ca316 6686 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6
DE
6687 sig_type, INSERT);
6688 }
6689 gdb_assert (*slot == NULL);
6690 *slot = sig_type;
6691 /* The rest of sig_type must be filled in by the caller. */
6692 return sig_type;
6693}
6694
a2ce51a0
DE
6695/* Subroutine of lookup_dwo_signatured_type and lookup_dwp_signatured_type.
6696 Fill in SIG_ENTRY with DWO_ENTRY. */
6697
6698static void
976ca316 6699fill_in_sig_entry_from_dwo_entry (dwarf2_per_objfile *per_objfile,
a2ce51a0
DE
6700 struct signatured_type *sig_entry,
6701 struct dwo_unit *dwo_entry)
6702{
976ca316 6703 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
1859c670 6704
7ee85ab1 6705 /* Make sure we're not clobbering something we don't expect to. */
a2ce51a0 6706 gdb_assert (! sig_entry->per_cu.queued);
976ca316 6707 gdb_assert (per_objfile->get_cu (&sig_entry->per_cu) == NULL);
1859c670 6708 if (per_bfd->using_index)
6aa5f3a6
DE
6709 {
6710 gdb_assert (sig_entry->per_cu.v.quick != NULL);
976ca316 6711 gdb_assert (!per_objfile->symtab_set_p (&sig_entry->per_cu));
6aa5f3a6
DE
6712 }
6713 else
6714 gdb_assert (sig_entry->per_cu.v.psymtab == NULL);
a2ce51a0 6715 gdb_assert (sig_entry->signature == dwo_entry->signature);
9c541725 6716 gdb_assert (to_underlying (sig_entry->type_offset_in_section) == 0);
a2ce51a0 6717 gdb_assert (sig_entry->type_unit_group == NULL);
7ee85ab1
DE
6718 gdb_assert (sig_entry->dwo_unit == NULL);
6719
6720 sig_entry->per_cu.section = dwo_entry->section;
9c541725 6721 sig_entry->per_cu.sect_off = dwo_entry->sect_off;
7ee85ab1
DE
6722 sig_entry->per_cu.length = dwo_entry->length;
6723 sig_entry->per_cu.reading_dwo_directly = 1;
1859c670 6724 sig_entry->per_cu.per_bfd = per_bfd;
a2ce51a0
DE
6725 sig_entry->type_offset_in_tu = dwo_entry->type_offset_in_tu;
6726 sig_entry->dwo_unit = dwo_entry;
6727}
6728
6729/* Subroutine of lookup_signatured_type.
7ee85ab1
DE
6730 If we haven't read the TU yet, create the signatured_type data structure
6731 for a TU to be read in directly from a DWO file, bypassing the stub.
6732 This is the "Stay in DWO Optimization": When there is no DWP file and we're
6733 using .gdb_index, then when reading a CU we want to stay in the DWO file
6734 containing that CU. Otherwise we could end up reading several other DWO
6735 files (due to comdat folding) to process the transitive closure of all the
6736 mentioned TUs, and that can be slow. The current DWO file will have every
6737 type signature that it needs.
a2ce51a0
DE
6738 We only do this for .gdb_index because in the psymtab case we already have
6739 to read all the DWOs to build the type unit groups. */
6740
6741static struct signatured_type *
6742lookup_dwo_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6743{
976ca316 6744 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a2ce51a0
DE
6745 struct dwo_file *dwo_file;
6746 struct dwo_unit find_dwo_entry, *dwo_entry;
6747 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6748 void **slot;
a2ce51a0 6749
976ca316 6750 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0 6751
6aa5f3a6
DE
6752 /* If TU skeletons have been removed then we may not have read in any
6753 TUs yet. */
976ca316
SM
6754 if (per_objfile->per_bfd->signatured_types == NULL)
6755 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0
DE
6756
6757 /* We only ever need to read in one copy of a signatured type.
6aa5f3a6
DE
6758 Use the global signatured_types array to do our own comdat-folding
6759 of types. If this is the first time we're reading this TU, and
6760 the TU has an entry in .gdb_index, replace the recorded data from
6761 .gdb_index with this TU. */
a2ce51a0 6762
a2ce51a0 6763 find_sig_entry.signature = sig;
976ca316 6764 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6765 &find_sig_entry, INSERT);
9a3c8263 6766 sig_entry = (struct signatured_type *) *slot;
7ee85ab1
DE
6767
6768 /* We can get here with the TU already read, *or* in the process of being
6aa5f3a6
DE
6769 read. Don't reassign the global entry to point to this DWO if that's
6770 the case. Also note that if the TU is already being read, it may not
6771 have come from a DWO, the program may be a mix of Fission-compiled
6772 code and non-Fission-compiled code. */
6773
6774 /* Have we already tried to read this TU?
6775 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6776 needn't exist in the global table yet). */
6777 if (sig_entry != NULL && sig_entry->per_cu.tu_read)
a2ce51a0
DE
6778 return sig_entry;
6779
6aa5f3a6
DE
6780 /* Note: cu->dwo_unit is the dwo_unit that references this TU, not the
6781 dwo_unit of the TU itself. */
6782 dwo_file = cu->dwo_unit->dwo_file;
6783
a2ce51a0
DE
6784 /* Ok, this is the first time we're reading this TU. */
6785 if (dwo_file->tus == NULL)
6786 return NULL;
6787 find_dwo_entry.signature = sig;
b0b6a987
TT
6788 dwo_entry = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
6789 &find_dwo_entry);
a2ce51a0
DE
6790 if (dwo_entry == NULL)
6791 return NULL;
6792
6aa5f3a6
DE
6793 /* If the global table doesn't have an entry for this TU, add one. */
6794 if (sig_entry == NULL)
976ca316 6795 sig_entry = add_type_unit (per_objfile, sig, slot);
6aa5f3a6 6796
976ca316 6797 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
89e63ee4 6798 sig_entry->per_cu.tu_read = 1;
a2ce51a0
DE
6799 return sig_entry;
6800}
6801
a2ce51a0
DE
6802/* Subroutine of lookup_signatured_type.
6803 Look up the type for signature SIG, and if we can't find SIG in .gdb_index
6aa5f3a6
DE
6804 then try the DWP file. If the TU stub (skeleton) has been removed then
6805 it won't be in .gdb_index. */
a2ce51a0
DE
6806
6807static struct signatured_type *
6808lookup_dwp_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
6809{
976ca316
SM
6810 dwarf2_per_objfile *per_objfile = cu->per_objfile;
6811 struct dwp_file *dwp_file = get_dwp_file (per_objfile);
a2ce51a0
DE
6812 struct dwo_unit *dwo_entry;
6813 struct signatured_type find_sig_entry, *sig_entry;
6aa5f3a6 6814 void **slot;
a2ce51a0 6815
976ca316 6816 gdb_assert (cu->dwo_unit && per_objfile->per_bfd->using_index);
a2ce51a0
DE
6817 gdb_assert (dwp_file != NULL);
6818
6aa5f3a6
DE
6819 /* If TU skeletons have been removed then we may not have read in any
6820 TUs yet. */
976ca316
SM
6821 if (per_objfile->per_bfd->signatured_types == NULL)
6822 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
a2ce51a0 6823
6aa5f3a6 6824 find_sig_entry.signature = sig;
976ca316 6825 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
6aa5f3a6 6826 &find_sig_entry, INSERT);
9a3c8263 6827 sig_entry = (struct signatured_type *) *slot;
6aa5f3a6
DE
6828
6829 /* Have we already tried to read this TU?
6830 Note: sig_entry can be NULL if the skeleton TU was removed (thus it
6831 needn't exist in the global table yet). */
6832 if (sig_entry != NULL)
6833 return sig_entry;
6834
a2ce51a0
DE
6835 if (dwp_file->tus == NULL)
6836 return NULL;
976ca316
SM
6837 dwo_entry = lookup_dwo_unit_in_dwp (per_objfile, dwp_file, NULL, sig,
6838 1 /* is_debug_types */);
a2ce51a0
DE
6839 if (dwo_entry == NULL)
6840 return NULL;
6841
976ca316
SM
6842 sig_entry = add_type_unit (per_objfile, sig, slot);
6843 fill_in_sig_entry_from_dwo_entry (per_objfile, sig_entry, dwo_entry);
a2ce51a0 6844
a2ce51a0
DE
6845 return sig_entry;
6846}
6847
380bca97 6848/* Lookup a signature based type for DW_FORM_ref_sig8.
5a8b3f62
DE
6849 Returns NULL if signature SIG is not present in the table.
6850 It is up to the caller to complain about this. */
348e048f
DE
6851
6852static struct signatured_type *
a2ce51a0 6853lookup_signatured_type (struct dwarf2_cu *cu, ULONGEST sig)
348e048f 6854{
976ca316 6855 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 6856
976ca316 6857 if (cu->dwo_unit && per_objfile->per_bfd->using_index)
a2ce51a0
DE
6858 {
6859 /* We're in a DWO/DWP file, and we're using .gdb_index.
6860 These cases require special processing. */
976ca316 6861 if (get_dwp_file (per_objfile) == NULL)
a2ce51a0
DE
6862 return lookup_dwo_signatured_type (cu, sig);
6863 else
6864 return lookup_dwp_signatured_type (cu, sig);
6865 }
6866 else
6867 {
6868 struct signatured_type find_entry, *entry;
348e048f 6869
976ca316 6870 if (per_objfile->per_bfd->signatured_types == NULL)
a2ce51a0
DE
6871 return NULL;
6872 find_entry.signature = sig;
9a3c8263 6873 entry = ((struct signatured_type *)
976ca316 6874 htab_find (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 6875 &find_entry));
a2ce51a0
DE
6876 return entry;
6877 }
348e048f 6878}
18a8505e 6879
42e7ad6c 6880/* Low level DIE reading support. */
348e048f 6881
d85a05f0
DJ
6882/* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
6883
6884static void
6885init_cu_die_reader (struct die_reader_specs *reader,
dee91e82 6886 struct dwarf2_cu *cu,
3019eac3 6887 struct dwarf2_section_info *section,
685af9cd
TT
6888 struct dwo_file *dwo_file,
6889 struct abbrev_table *abbrev_table)
d85a05f0 6890{
fceca515 6891 gdb_assert (section->readin && section->buffer != NULL);
96b79293 6892 reader->abfd = section->get_bfd_owner ();
d85a05f0 6893 reader->cu = cu;
3019eac3 6894 reader->dwo_file = dwo_file;
dee91e82
DE
6895 reader->die_section = section;
6896 reader->buffer = section->buffer;
f664829e 6897 reader->buffer_end = section->buffer + section->size;
685af9cd 6898 reader->abbrev_table = abbrev_table;
d85a05f0
DJ
6899}
6900
c0ab21c2 6901/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 6902 Read in the rest of a CU/TU top level DIE from DWO_UNIT.
c0ab21c2 6903 There's just a lot of work to do, and cutu_reader is big enough
b0c7bfa9
DE
6904 already.
6905
6906 STUB_COMP_UNIT_DIE is for the stub DIE, we copy over certain attributes
6907 from it to the DIE in the DWO. If NULL we are skipping the stub.
a2ce51a0
DE
6908 STUB_COMP_DIR is similar to STUB_COMP_UNIT_DIE: When reading a TU directly
6909 from the DWO file, bypassing the stub, it contains the DW_AT_comp_dir
c54a1dd8
DE
6910 attribute of the referencing CU. At most one of STUB_COMP_UNIT_DIE and
6911 STUB_COMP_DIR may be non-NULL.
3e225074 6912 *RESULT_READER,*RESULT_INFO_PTR,*RESULT_COMP_UNIT_DIE
b0c7bfa9 6913 are filled in with the info of the DIE from the DWO file.
685af9cd
TT
6914 *RESULT_DWO_ABBREV_TABLE will be filled in with the abbrev table allocated
6915 from the dwo. Since *RESULT_READER references this abbrev table, it must be
6916 kept around for at least as long as *RESULT_READER.
6917
b0c7bfa9
DE
6918 The result is non-zero if a valid (non-dummy) DIE was found. */
6919
6920static int
4ab09049 6921read_cutu_die_from_dwo (dwarf2_cu *cu,
b0c7bfa9 6922 struct dwo_unit *dwo_unit,
b0c7bfa9 6923 struct die_info *stub_comp_unit_die,
a2ce51a0 6924 const char *stub_comp_dir,
b0c7bfa9 6925 struct die_reader_specs *result_reader,
d521ce57 6926 const gdb_byte **result_info_ptr,
b0c7bfa9 6927 struct die_info **result_comp_unit_die,
685af9cd 6928 abbrev_table_up *result_dwo_abbrev_table)
b0c7bfa9 6929{
976ca316 6930 dwarf2_per_objfile *per_objfile = cu->per_objfile;
4ab09049 6931 dwarf2_per_cu_data *per_cu = cu->per_cu;
976ca316 6932 struct objfile *objfile = per_objfile->objfile;
b0c7bfa9 6933 bfd *abfd;
d521ce57 6934 const gdb_byte *begin_info_ptr, *info_ptr;
b0c7bfa9
DE
6935 struct attribute *comp_dir, *stmt_list, *low_pc, *high_pc, *ranges;
6936 int i,num_extra_attrs;
6937 struct dwarf2_section_info *dwo_abbrev_section;
b0c7bfa9
DE
6938 struct die_info *comp_unit_die;
6939
b0aeadb3
DE
6940 /* At most one of these may be provided. */
6941 gdb_assert ((stub_comp_unit_die != NULL) + (stub_comp_dir != NULL) <= 1);
a2ce51a0 6942
b0c7bfa9
DE
6943 /* These attributes aren't processed until later:
6944 DW_AT_stmt_list, DW_AT_low_pc, DW_AT_high_pc, DW_AT_ranges.
0d60c288
DE
6945 DW_AT_comp_dir is used now, to find the DWO file, but it is also
6946 referenced later. However, these attributes are found in the stub
6947 which we won't have later. In order to not impose this complication
6948 on the rest of the code, we read them here and copy them to the
6949 DWO CU/TU die. */
b0c7bfa9
DE
6950
6951 stmt_list = NULL;
6952 low_pc = NULL;
6953 high_pc = NULL;
6954 ranges = NULL;
6955 comp_dir = NULL;
6956
6957 if (stub_comp_unit_die != NULL)
6958 {
6959 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
6960 DWO file. */
4ab09049 6961 if (!per_cu->is_debug_types)
b0c7bfa9
DE
6962 stmt_list = dwarf2_attr (stub_comp_unit_die, DW_AT_stmt_list, cu);
6963 low_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_low_pc, cu);
6964 high_pc = dwarf2_attr (stub_comp_unit_die, DW_AT_high_pc, cu);
6965 ranges = dwarf2_attr (stub_comp_unit_die, DW_AT_ranges, cu);
6966 comp_dir = dwarf2_attr (stub_comp_unit_die, DW_AT_comp_dir, cu);
6967
a39fdb41 6968 cu->addr_base = stub_comp_unit_die->addr_base ();
b0c7bfa9 6969
18a8505e
AT
6970 /* There should be a DW_AT_rnglists_base (DW_AT_GNU_ranges_base) attribute
6971 here (if needed). We need the value before we can process
6972 DW_AT_ranges. */
a39fdb41 6973 cu->ranges_base = stub_comp_unit_die->ranges_base ();
b0c7bfa9 6974 }
a2ce51a0
DE
6975 else if (stub_comp_dir != NULL)
6976 {
6977 /* Reconstruct the comp_dir attribute to simplify the code below. */
fe56917a 6978 comp_dir = OBSTACK_ZALLOC (&cu->comp_unit_obstack, struct attribute);
a2ce51a0
DE
6979 comp_dir->name = DW_AT_comp_dir;
6980 comp_dir->form = DW_FORM_string;
c6481205 6981 comp_dir->set_string_noncanonical (stub_comp_dir);
a2ce51a0 6982 }
b0c7bfa9
DE
6983
6984 /* Set up for reading the DWO CU/TU. */
6985 cu->dwo_unit = dwo_unit;
685af9cd 6986 dwarf2_section_info *section = dwo_unit->section;
96b79293
TT
6987 section->read (objfile);
6988 abfd = section->get_bfd_owner ();
9c541725
PA
6989 begin_info_ptr = info_ptr = (section->buffer
6990 + to_underlying (dwo_unit->sect_off));
b0c7bfa9 6991 dwo_abbrev_section = &dwo_unit->dwo_file->sections.abbrev;
b0c7bfa9 6992
4ab09049 6993 if (per_cu->is_debug_types)
b0c7bfa9 6994 {
4ab09049 6995 signatured_type *sig_type = (struct signatured_type *) per_cu;
b0c7bfa9 6996
976ca316
SM
6997 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
6998 section, dwo_abbrev_section,
43988095 6999 info_ptr, rcuh_kind::TYPE);
a2ce51a0 7000 /* This is not an assert because it can be caused by bad debug info. */
43988095 7001 if (sig_type->signature != cu->header.signature)
a2ce51a0
DE
7002 {
7003 error (_("Dwarf Error: signature mismatch %s vs %s while reading"
9d8780f0 7004 " TU at offset %s [in module %s]"),
a2ce51a0 7005 hex_string (sig_type->signature),
43988095 7006 hex_string (cu->header.signature),
9d8780f0 7007 sect_offset_str (dwo_unit->sect_off),
a2ce51a0
DE
7008 bfd_get_filename (abfd));
7009 }
9c541725 7010 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7011 /* For DWOs coming from DWP files, we don't know the CU length
7012 nor the type's offset in the TU until now. */
4057dfde 7013 dwo_unit->length = cu->header.get_length ();
9c541725 7014 dwo_unit->type_offset_in_tu = cu->header.type_cu_offset_in_tu;
b0c7bfa9
DE
7015
7016 /* Establish the type offset that can be used to lookup the type.
7017 For DWO files, we don't know it until now. */
9c541725
PA
7018 sig_type->type_offset_in_section
7019 = dwo_unit->sect_off + to_underlying (dwo_unit->type_offset_in_tu);
b0c7bfa9
DE
7020 }
7021 else
7022 {
976ca316
SM
7023 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7024 section, dwo_abbrev_section,
43988095 7025 info_ptr, rcuh_kind::COMPILE);
9c541725 7026 gdb_assert (dwo_unit->sect_off == cu->header.sect_off);
b0c7bfa9
DE
7027 /* For DWOs coming from DWP files, we don't know the CU length
7028 until now. */
4057dfde 7029 dwo_unit->length = cu->header.get_length ();
b0c7bfa9
DE
7030 }
7031
606decb2 7032 dwo_abbrev_section->read (objfile);
685af9cd 7033 *result_dwo_abbrev_table
606decb2 7034 = abbrev_table::read (dwo_abbrev_section, cu->header.abbrev_sect_off);
685af9cd
TT
7035 init_cu_die_reader (result_reader, cu, section, dwo_unit->dwo_file,
7036 result_dwo_abbrev_table->get ());
b0c7bfa9
DE
7037
7038 /* Read in the die, but leave space to copy over the attributes
7039 from the stub. This has the benefit of simplifying the rest of
7040 the code - all the work to maintain the illusion of a single
7041 DW_TAG_{compile,type}_unit DIE is done here. */
7042 num_extra_attrs = ((stmt_list != NULL)
7043 + (low_pc != NULL)
7044 + (high_pc != NULL)
7045 + (ranges != NULL)
7046 + (comp_dir != NULL));
7047 info_ptr = read_full_die_1 (result_reader, result_comp_unit_die, info_ptr,
3e225074 7048 num_extra_attrs);
b0c7bfa9
DE
7049
7050 /* Copy over the attributes from the stub to the DIE we just read in. */
7051 comp_unit_die = *result_comp_unit_die;
7052 i = comp_unit_die->num_attrs;
7053 if (stmt_list != NULL)
7054 comp_unit_die->attrs[i++] = *stmt_list;
7055 if (low_pc != NULL)
7056 comp_unit_die->attrs[i++] = *low_pc;
7057 if (high_pc != NULL)
7058 comp_unit_die->attrs[i++] = *high_pc;
7059 if (ranges != NULL)
7060 comp_unit_die->attrs[i++] = *ranges;
7061 if (comp_dir != NULL)
7062 comp_unit_die->attrs[i++] = *comp_dir;
7063 comp_unit_die->num_attrs += num_extra_attrs;
7064
b4f54984 7065 if (dwarf_die_debug)
bf6af496
DE
7066 {
7067 fprintf_unfiltered (gdb_stdlog,
7068 "Read die from %s@0x%x of %s:\n",
96b79293 7069 section->get_name (),
bf6af496
DE
7070 (unsigned) (begin_info_ptr - section->buffer),
7071 bfd_get_filename (abfd));
b4f54984 7072 dump_die (comp_unit_die, dwarf_die_debug);
bf6af496
DE
7073 }
7074
b0c7bfa9
DE
7075 /* Skip dummy compilation units. */
7076 if (info_ptr >= begin_info_ptr + dwo_unit->length
7077 || peek_abbrev_code (abfd, info_ptr) == 0)
7078 return 0;
7079
7080 *result_info_ptr = info_ptr;
7081 return 1;
7082}
7083
a084a2a6
AT
7084/* Return the signature of the compile unit, if found. In DWARF 4 and before,
7085 the signature is in the DW_AT_GNU_dwo_id attribute. In DWARF 5 and later, the
7086 signature is part of the header. */
7087static gdb::optional<ULONGEST>
7088lookup_dwo_id (struct dwarf2_cu *cu, struct die_info* comp_unit_die)
7089{
7090 if (cu->header.version >= 5)
7091 return cu->header.signature;
7092 struct attribute *attr;
7093 attr = dwarf2_attr (comp_unit_die, DW_AT_GNU_dwo_id, cu);
529908cb 7094 if (attr == nullptr || !attr->form_is_unsigned ())
a084a2a6 7095 return gdb::optional<ULONGEST> ();
529908cb 7096 return attr->as_unsigned ();
a084a2a6
AT
7097}
7098
c0ab21c2 7099/* Subroutine of cutu_reader to simplify it.
b0c7bfa9 7100 Look up the DWO unit specified by COMP_UNIT_DIE of THIS_CU.
6a506a2d 7101 Returns NULL if the specified DWO unit cannot be found. */
b0c7bfa9
DE
7102
7103static struct dwo_unit *
4ab09049 7104lookup_dwo_unit (dwarf2_cu *cu, die_info *comp_unit_die, const char *dwo_name)
b0c7bfa9 7105{
4ab09049 7106 dwarf2_per_cu_data *per_cu = cu->per_cu;
b0c7bfa9 7107 struct dwo_unit *dwo_unit;
c0ab21c2 7108 const char *comp_dir;
b0c7bfa9 7109
a2ce51a0
DE
7110 gdb_assert (cu != NULL);
7111
b0c7bfa9 7112 /* Yeah, we look dwo_name up again, but it simplifies the code. */
a084a2a6 7113 dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
7d45c7c3 7114 comp_dir = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
b0c7bfa9 7115
4ab09049
SM
7116 if (per_cu->is_debug_types)
7117 dwo_unit = lookup_dwo_type_unit (cu, dwo_name, comp_dir);
b0c7bfa9
DE
7118 else
7119 {
a084a2a6 7120 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
4ab09049 7121
a084a2a6 7122 if (!signature.has_value ())
b0c7bfa9
DE
7123 error (_("Dwarf Error: missing dwo_id for dwo_name %s"
7124 " [in module %s]"),
4ab09049
SM
7125 dwo_name, bfd_get_filename (per_cu->per_bfd->obfd));
7126
7127 dwo_unit = lookup_dwo_comp_unit (cu, dwo_name, comp_dir, *signature);
b0c7bfa9
DE
7128 }
7129
b0c7bfa9
DE
7130 return dwo_unit;
7131}
7132
c0ab21c2 7133/* Subroutine of cutu_reader to simplify it.
6aa5f3a6 7134 See it for a description of the parameters.
fcd3b13d 7135 Read a TU directly from a DWO file, bypassing the stub. */
a2ce51a0 7136
c0ab21c2 7137void
9e021579
SM
7138cutu_reader::init_tu_and_read_dwo_dies (dwarf2_per_cu_data *this_cu,
7139 dwarf2_per_objfile *per_objfile,
2e671100 7140 dwarf2_cu *existing_cu)
a2ce51a0 7141{
a2ce51a0 7142 struct signatured_type *sig_type;
a2ce51a0
DE
7143
7144 /* Verify we can do the following downcast, and that we have the
7145 data we need. */
7146 gdb_assert (this_cu->is_debug_types && this_cu->reading_dwo_directly);
7147 sig_type = (struct signatured_type *) this_cu;
7148 gdb_assert (sig_type->dwo_unit != NULL);
7149
2e671100
SM
7150 dwarf2_cu *cu;
7151
7152 if (existing_cu != nullptr)
6aa5f3a6 7153 {
2e671100
SM
7154 cu = existing_cu;
7155 gdb_assert (cu->dwo_unit == sig_type->dwo_unit);
6aa5f3a6 7156 /* There's no need to do the rereading_dwo_cu handling that
c0ab21c2 7157 cutu_reader does since we don't read the stub. */
6aa5f3a6
DE
7158 }
7159 else
7160 {
7188ed02 7161 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7162 in per_objfile yet. */
7188ed02 7163 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
9e021579 7164 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
2e671100 7165 cu = m_new_cu.get ();
6aa5f3a6
DE
7166 }
7167
7168 /* A future optimization, if needed, would be to use an existing
7169 abbrev table. When reading DWOs with skeletonless TUs, all the TUs
7170 could share abbrev tables. */
a2ce51a0 7171
2e671100 7172 if (read_cutu_die_from_dwo (cu, sig_type->dwo_unit,
a2ce51a0
DE
7173 NULL /* stub_comp_unit_die */,
7174 sig_type->dwo_unit->dwo_file->comp_dir,
4ebe4877 7175 this, &info_ptr,
3e225074 7176 &comp_unit_die,
c0ab21c2 7177 &m_dwo_abbrev_table) == 0)
a2ce51a0
DE
7178 {
7179 /* Dummy die. */
c0ab21c2 7180 dummy_p = true;
a2ce51a0 7181 }
a2ce51a0
DE
7182}
7183
fd820528 7184/* Initialize a CU (or TU) and read its DIEs.
3019eac3 7185 If the CU defers to a DWO file, read the DWO file as well.
dee91e82 7186
f4dc4d17
DE
7187 ABBREV_TABLE, if non-NULL, is the abbreviation table to use.
7188 Otherwise the table specified in the comp unit header is read in and used.
7189 This is an optimization for when we already have the abbrev table.
7190
2e671100
SM
7191 If EXISTING_CU is non-NULL, then use it. Otherwise, a new CU is
7192 allocated. */
aaa75496 7193
ab432490 7194cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7195 dwarf2_per_objfile *per_objfile,
c0ab21c2 7196 struct abbrev_table *abbrev_table,
2e671100 7197 dwarf2_cu *existing_cu,
c0ab21c2
TT
7198 bool skip_partial)
7199 : die_reader_specs {},
6751ebae 7200 m_this_cu (this_cu)
c906108c 7201{
976ca316 7202 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7203 struct dwarf2_section_info *section = this_cu->section;
96b79293 7204 bfd *abfd = section->get_bfd_owner ();
c0ab21c2 7205 const gdb_byte *begin_info_ptr;
dee91e82 7206 struct signatured_type *sig_type = NULL;
4bdcc0c1 7207 struct dwarf2_section_info *abbrev_section;
42e7ad6c
DE
7208 /* Non-zero if CU currently points to a DWO file and we need to
7209 reread it. When this happens we need to reread the skeleton die
a2ce51a0 7210 before we can reread the DWO file (this only applies to CUs, not TUs). */
42e7ad6c 7211 int rereading_dwo_cu = 0;
c906108c 7212
b4f54984 7213 if (dwarf_die_debug)
9d8780f0 7214 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7215 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7216 sect_offset_str (this_cu->sect_off));
09406207 7217
a2ce51a0
DE
7218 /* If we're reading a TU directly from a DWO file, including a virtual DWO
7219 file (instead of going through the stub), short-circuit all of this. */
7220 if (this_cu->reading_dwo_directly)
7221 {
7222 /* Narrow down the scope of possibilities to have to understand. */
7223 gdb_assert (this_cu->is_debug_types);
7224 gdb_assert (abbrev_table == NULL);
976ca316 7225 init_tu_and_read_dwo_dies (this_cu, per_objfile, existing_cu);
a2ce51a0
DE
7226 return;
7227 }
7228
dee91e82 7229 /* This is cheap if the section is already read in. */
96b79293 7230 section->read (objfile);
dee91e82 7231
9c541725 7232 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
36586728
TT
7233
7234 abbrev_section = get_abbrev_section_for_cu (this_cu);
dee91e82 7235
2e671100
SM
7236 dwarf2_cu *cu;
7237
7238 if (existing_cu != nullptr)
dee91e82 7239 {
2e671100 7240 cu = existing_cu;
42e7ad6c
DE
7241 /* If this CU is from a DWO file we need to start over, we need to
7242 refetch the attributes from the skeleton CU.
7243 This could be optimized by retrieving those attributes from when we
7244 were here the first time: the previous comp_unit_die was stored in
7245 comp_unit_obstack. But there's no data yet that we need this
7246 optimization. */
7247 if (cu->dwo_unit != NULL)
7248 rereading_dwo_cu = 1;
dee91e82
DE
7249 }
7250 else
7251 {
7188ed02 7252 /* If an existing_cu is provided, a dwarf2_cu must not exist for this_cu
dda83cd7 7253 in per_objfile yet. */
976ca316
SM
7254 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
7255 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
c0ab21c2 7256 cu = m_new_cu.get ();
42e7ad6c 7257 }
dee91e82 7258
b0c7bfa9 7259 /* Get the header. */
9c541725 7260 if (to_underlying (cu->header.first_die_cu_offset) != 0 && !rereading_dwo_cu)
42e7ad6c
DE
7261 {
7262 /* We already have the header, there's no need to read it in again. */
9c541725 7263 info_ptr += to_underlying (cu->header.first_die_cu_offset);
42e7ad6c
DE
7264 }
7265 else
7266 {
3019eac3 7267 if (this_cu->is_debug_types)
dee91e82 7268 {
976ca316
SM
7269 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7270 section, abbrev_section,
7271 info_ptr, rcuh_kind::TYPE);
dee91e82 7272
42e7ad6c
DE
7273 /* Since per_cu is the first member of struct signatured_type,
7274 we can go from a pointer to one to a pointer to the other. */
7275 sig_type = (struct signatured_type *) this_cu;
43988095 7276 gdb_assert (sig_type->signature == cu->header.signature);
9c541725
PA
7277 gdb_assert (sig_type->type_offset_in_tu
7278 == cu->header.type_cu_offset_in_tu);
7279 gdb_assert (this_cu->sect_off == cu->header.sect_off);
dee91e82 7280
42e7ad6c
DE
7281 /* LENGTH has not been set yet for type units if we're
7282 using .gdb_index. */
4057dfde 7283 this_cu->length = cu->header.get_length ();
3019eac3
DE
7284
7285 /* Establish the type offset that can be used to lookup the type. */
9c541725
PA
7286 sig_type->type_offset_in_section =
7287 this_cu->sect_off + to_underlying (sig_type->type_offset_in_tu);
43988095
JK
7288
7289 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7290 }
7291 else
7292 {
976ca316
SM
7293 info_ptr = read_and_check_comp_unit_head (per_objfile, &cu->header,
7294 section, abbrev_section,
43988095
JK
7295 info_ptr,
7296 rcuh_kind::COMPILE);
dee91e82 7297
9c541725 7298 gdb_assert (this_cu->sect_off == cu->header.sect_off);
3ee6bb11
TV
7299 if (this_cu->length == 0)
7300 this_cu->length = cu->header.get_length ();
7301 else
7302 gdb_assert (this_cu->length == cu->header.get_length ());
43988095 7303 this_cu->dwarf_version = cu->header.version;
dee91e82
DE
7304 }
7305 }
10b3939b 7306
6caca83c 7307 /* Skip dummy compilation units. */
dee91e82 7308 if (info_ptr >= begin_info_ptr + this_cu->length
6caca83c 7309 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7310 {
7311 dummy_p = true;
7312 return;
7313 }
6caca83c 7314
433df2d4
DE
7315 /* If we don't have them yet, read the abbrevs for this compilation unit.
7316 And if we need to read them now, make sure they're freed when we're
c0ab21c2 7317 done. */
f4dc4d17 7318 if (abbrev_table != NULL)
685af9cd
TT
7319 gdb_assert (cu->header.abbrev_sect_off == abbrev_table->sect_off);
7320 else
f4dc4d17 7321 {
606decb2 7322 abbrev_section->read (objfile);
c0ab21c2 7323 m_abbrev_table_holder
606decb2 7324 = abbrev_table::read (abbrev_section, cu->header.abbrev_sect_off);
c0ab21c2 7325 abbrev_table = m_abbrev_table_holder.get ();
42e7ad6c 7326 }
af703f96 7327
dee91e82 7328 /* Read the top level CU/TU die. */
c0ab21c2 7329 init_cu_die_reader (this, cu, section, NULL, abbrev_table);
3e225074 7330 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
93311388 7331
58f0c718 7332 if (skip_partial && comp_unit_die->tag == DW_TAG_partial_unit)
c0ab21c2
TT
7333 {
7334 dummy_p = true;
7335 return;
7336 }
58f0c718 7337
b0c7bfa9 7338 /* If we are in a DWO stub, process it and then read in the "real" CU/TU
685af9cd
TT
7339 from the DWO file. read_cutu_die_from_dwo will allocate the abbreviation
7340 table from the DWO file and pass the ownership over to us. It will be
7341 referenced from READER, so we must make sure to free it after we're done
7342 with READER.
7343
b0c7bfa9
DE
7344 Note that if USE_EXISTING_OK != 0, and THIS_CU->cu already contains a
7345 DWO CU, that this test will fail (the attribute will not be present). */
a084a2a6 7346 const char *dwo_name = dwarf2_dwo_name (comp_unit_die, cu);
a084a2a6 7347 if (dwo_name != nullptr)
3019eac3 7348 {
3019eac3 7349 struct dwo_unit *dwo_unit;
b0c7bfa9 7350 struct die_info *dwo_comp_unit_die;
3019eac3 7351
3e225074 7352 if (comp_unit_die->has_children)
6a506a2d 7353 {
b98664d3 7354 complaint (_("compilation unit with DW_AT_GNU_dwo_name"
9d8780f0
SM
7355 " has children (offset %s) [in module %s]"),
7356 sect_offset_str (this_cu->sect_off),
7357 bfd_get_filename (abfd));
6a506a2d 7358 }
4ab09049 7359 dwo_unit = lookup_dwo_unit (cu, comp_unit_die, dwo_name);
6a506a2d 7360 if (dwo_unit != NULL)
3019eac3 7361 {
4ab09049 7362 if (read_cutu_die_from_dwo (cu, dwo_unit,
a2ce51a0 7363 comp_unit_die, NULL,
c0ab21c2 7364 this, &info_ptr,
3e225074 7365 &dwo_comp_unit_die,
c0ab21c2 7366 &m_dwo_abbrev_table) == 0)
6a506a2d
DE
7367 {
7368 /* Dummy die. */
c0ab21c2 7369 dummy_p = true;
6a506a2d
DE
7370 return;
7371 }
7372 comp_unit_die = dwo_comp_unit_die;
7373 }
7374 else
7375 {
7376 /* Yikes, we couldn't find the rest of the DIE, we only have
7377 the stub. A complaint has already been logged. There's
7378 not much more we can do except pass on the stub DIE to
7379 die_reader_func. We don't want to throw an error on bad
7380 debug info. */
3019eac3
DE
7381 }
7382 }
c0ab21c2 7383}
3019eac3 7384
6751ebae
TT
7385void
7386cutu_reader::keep ()
c0ab21c2 7387{
b0c7bfa9 7388 /* Done, clean up. */
6751ebae
TT
7389 gdb_assert (!dummy_p);
7390 if (m_new_cu != NULL)
348e048f 7391 {
7188ed02 7392 /* Save this dwarf2_cu in the per_objfile. The per_objfile owns it
dda83cd7 7393 now. */
7188ed02
SM
7394 dwarf2_per_objfile *per_objfile = m_new_cu->per_objfile;
7395 per_objfile->set_cu (m_this_cu, m_new_cu.release ());
348e048f 7396 }
dee91e82
DE
7397}
7398
18a8505e
AT
7399/* Read CU/TU THIS_CU but do not follow DW_AT_GNU_dwo_name (DW_AT_dwo_name)
7400 if present. DWO_FILE, if non-NULL, is the DWO file to read (the caller is
7401 assumed to have already done the lookup to find the DWO file).
dee91e82
DE
7402
7403 The caller is required to fill in THIS_CU->section, THIS_CU->offset, and
3019eac3 7404 THIS_CU->is_debug_types, but nothing else.
dee91e82
DE
7405
7406 We fill in THIS_CU->length.
7407
dee91e82 7408 THIS_CU->cu is always freed when done.
3019eac3 7409 This is done in order to not leave THIS_CU->cu in a state where we have
18a8505e
AT
7410 to care whether it refers to the "main" CU or the DWO CU.
7411
7412 When parent_cu is passed, it is used to provide a default value for
7413 str_offsets_base and addr_base from the parent. */
dee91e82 7414
ab432490 7415cutu_reader::cutu_reader (dwarf2_per_cu_data *this_cu,
976ca316 7416 dwarf2_per_objfile *per_objfile,
c0ab21c2
TT
7417 struct dwarf2_cu *parent_cu,
7418 struct dwo_file *dwo_file)
7419 : die_reader_specs {},
7420 m_this_cu (this_cu)
dee91e82 7421{
976ca316 7422 struct objfile *objfile = per_objfile->objfile;
8a0459fd 7423 struct dwarf2_section_info *section = this_cu->section;
96b79293 7424 bfd *abfd = section->get_bfd_owner ();
33e80786 7425 struct dwarf2_section_info *abbrev_section;
d521ce57 7426 const gdb_byte *begin_info_ptr, *info_ptr;
dee91e82 7427
b4f54984 7428 if (dwarf_die_debug)
9d8780f0 7429 fprintf_unfiltered (gdb_stdlog, "Reading %s unit at offset %s\n",
09406207 7430 this_cu->is_debug_types ? "type" : "comp",
9d8780f0 7431 sect_offset_str (this_cu->sect_off));
09406207 7432
976ca316 7433 gdb_assert (per_objfile->get_cu (this_cu) == nullptr);
dee91e82 7434
33e80786
DE
7435 abbrev_section = (dwo_file != NULL
7436 ? &dwo_file->sections.abbrev
7437 : get_abbrev_section_for_cu (this_cu));
7438
dee91e82 7439 /* This is cheap if the section is already read in. */
96b79293 7440 section->read (objfile);
dee91e82 7441
976ca316 7442 m_new_cu.reset (new dwarf2_cu (this_cu, per_objfile));
dee91e82 7443
9c541725 7444 begin_info_ptr = info_ptr = section->buffer + to_underlying (this_cu->sect_off);
976ca316
SM
7445 info_ptr = read_and_check_comp_unit_head (per_objfile, &m_new_cu->header,
7446 section, abbrev_section, info_ptr,
43988095
JK
7447 (this_cu->is_debug_types
7448 ? rcuh_kind::TYPE
7449 : rcuh_kind::COMPILE));
dee91e82 7450
18a8505e
AT
7451 if (parent_cu != nullptr)
7452 {
c0ab21c2
TT
7453 m_new_cu->str_offsets_base = parent_cu->str_offsets_base;
7454 m_new_cu->addr_base = parent_cu->addr_base;
18a8505e 7455 }
4057dfde 7456 this_cu->length = m_new_cu->header.get_length ();
dee91e82
DE
7457
7458 /* Skip dummy compilation units. */
7459 if (info_ptr >= begin_info_ptr + this_cu->length
7460 || peek_abbrev_code (abfd, info_ptr) == 0)
c0ab21c2
TT
7461 {
7462 dummy_p = true;
7463 return;
7464 }
72bf9492 7465
606decb2 7466 abbrev_section->read (objfile);
c0ab21c2 7467 m_abbrev_table_holder
606decb2 7468 = abbrev_table::read (abbrev_section, m_new_cu->header.abbrev_sect_off);
dee91e82 7469
c0ab21c2
TT
7470 init_cu_die_reader (this, m_new_cu.get (), section, dwo_file,
7471 m_abbrev_table_holder.get ());
3e225074 7472 info_ptr = read_full_die (this, &comp_unit_die, info_ptr);
dee91e82
DE
7473}
7474
0018ea6f
DE
7475\f
7476/* Type Unit Groups.
dee91e82 7477
0018ea6f
DE
7478 Type Unit Groups are a way to collapse the set of all TUs (type units) into
7479 a more manageable set. The grouping is done by DW_AT_stmt_list entry
7480 so that all types coming from the same compilation (.o file) are grouped
7481 together. A future step could be to put the types in the same symtab as
7482 the CU the types ultimately came from. */
ff013f42 7483
f4dc4d17
DE
7484static hashval_t
7485hash_type_unit_group (const void *item)
7486{
9a3c8263
SM
7487 const struct type_unit_group *tu_group
7488 = (const struct type_unit_group *) item;
f4dc4d17 7489
094b34ac 7490 return hash_stmt_list_entry (&tu_group->hash);
f4dc4d17 7491}
348e048f
DE
7492
7493static int
f4dc4d17 7494eq_type_unit_group (const void *item_lhs, const void *item_rhs)
348e048f 7495{
9a3c8263
SM
7496 const struct type_unit_group *lhs = (const struct type_unit_group *) item_lhs;
7497 const struct type_unit_group *rhs = (const struct type_unit_group *) item_rhs;
348e048f 7498
094b34ac 7499 return eq_stmt_list_entry (&lhs->hash, &rhs->hash);
f4dc4d17 7500}
348e048f 7501
f4dc4d17
DE
7502/* Allocate a hash table for type unit groups. */
7503
eaa5fa8b 7504static htab_up
298e9637 7505allocate_type_unit_groups_table ()
f4dc4d17 7506{
eaa5fa8b
TT
7507 return htab_up (htab_create_alloc (3,
7508 hash_type_unit_group,
7509 eq_type_unit_group,
7510 NULL, xcalloc, xfree));
f4dc4d17 7511}
dee91e82 7512
f4dc4d17
DE
7513/* Type units that don't have DW_AT_stmt_list are grouped into their own
7514 partial symtabs. We combine several TUs per psymtab to not let the size
7515 of any one psymtab grow too big. */
7516#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB (1 << 31)
7517#define NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE 10
dee91e82 7518
094b34ac 7519/* Helper routine for get_type_unit_group.
f4dc4d17
DE
7520 Create the type_unit_group object used to hold one or more TUs. */
7521
7522static struct type_unit_group *
094b34ac 7523create_type_unit_group (struct dwarf2_cu *cu, sect_offset line_offset_struct)
f4dc4d17 7524{
976ca316
SM
7525 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7526 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
094b34ac 7527 struct dwarf2_per_cu_data *per_cu;
f4dc4d17 7528 struct type_unit_group *tu_group;
f4dc4d17 7529
976ca316 7530 tu_group = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, type_unit_group);
094b34ac 7531 per_cu = &tu_group->per_cu;
1859c670 7532 per_cu->per_bfd = per_bfd;
f4dc4d17 7533
1859c670 7534 if (per_bfd->using_index)
094b34ac 7535 {
1859c670 7536 per_cu->v.quick = OBSTACK_ZALLOC (&per_bfd->obstack,
094b34ac 7537 struct dwarf2_per_cu_quick_data);
094b34ac
DE
7538 }
7539 else
7540 {
9c541725 7541 unsigned int line_offset = to_underlying (line_offset_struct);
891813be 7542 dwarf2_psymtab *pst;
528e1572 7543 std::string name;
094b34ac
DE
7544
7545 /* Give the symtab a useful name for debug purposes. */
7546 if ((line_offset & NO_STMT_LIST_TYPE_UNIT_PSYMTAB) != 0)
528e1572
SM
7547 name = string_printf ("<type_units_%d>",
7548 (line_offset & ~NO_STMT_LIST_TYPE_UNIT_PSYMTAB));
094b34ac 7549 else
528e1572 7550 name = string_printf ("<type_units_at_0x%x>", line_offset);
094b34ac 7551
976ca316 7552 pst = create_partial_symtab (per_cu, per_objfile, name.c_str ());
6d94535f 7553 pst->anonymous = true;
094b34ac 7554 }
f4dc4d17 7555
094b34ac 7556 tu_group->hash.dwo_unit = cu->dwo_unit;
9c541725 7557 tu_group->hash.line_sect_off = line_offset_struct;
f4dc4d17
DE
7558
7559 return tu_group;
7560}
7561
094b34ac
DE
7562/* Look up the type_unit_group for type unit CU, and create it if necessary.
7563 STMT_LIST is a DW_AT_stmt_list attribute. */
f4dc4d17
DE
7564
7565static struct type_unit_group *
ff39bb5e 7566get_type_unit_group (struct dwarf2_cu *cu, const struct attribute *stmt_list)
f4dc4d17 7567{
976ca316
SM
7568 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7569 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
f4dc4d17
DE
7570 struct type_unit_group *tu_group;
7571 void **slot;
7572 unsigned int line_offset;
7573 struct type_unit_group type_unit_group_for_lookup;
7574
976ca316
SM
7575 if (per_objfile->per_bfd->type_unit_groups == NULL)
7576 per_objfile->per_bfd->type_unit_groups = allocate_type_unit_groups_table ();
f4dc4d17
DE
7577
7578 /* Do we need to create a new group, or can we use an existing one? */
7579
529908cb 7580 if (stmt_list != nullptr && stmt_list->form_is_unsigned ())
f4dc4d17 7581 {
529908cb 7582 line_offset = stmt_list->as_unsigned ();
f4dc4d17
DE
7583 ++tu_stats->nr_symtab_sharers;
7584 }
7585 else
7586 {
7587 /* Ugh, no stmt_list. Rare, but we have to handle it.
7588 We can do various things here like create one group per TU or
7589 spread them over multiple groups to split up the expansion work.
7590 To avoid worst case scenarios (too many groups or too large groups)
7591 we, umm, group them in bunches. */
7592 line_offset = (NO_STMT_LIST_TYPE_UNIT_PSYMTAB
7593 | (tu_stats->nr_stmt_less_type_units
7594 / NO_STMT_LIST_TYPE_UNIT_PSYMTAB_SIZE));
7595 ++tu_stats->nr_stmt_less_type_units;
7596 }
7597
094b34ac 7598 type_unit_group_for_lookup.hash.dwo_unit = cu->dwo_unit;
9c541725 7599 type_unit_group_for_lookup.hash.line_sect_off = (sect_offset) line_offset;
976ca316 7600 slot = htab_find_slot (per_objfile->per_bfd->type_unit_groups.get (),
f4dc4d17
DE
7601 &type_unit_group_for_lookup, INSERT);
7602 if (*slot != NULL)
7603 {
9a3c8263 7604 tu_group = (struct type_unit_group *) *slot;
f4dc4d17
DE
7605 gdb_assert (tu_group != NULL);
7606 }
7607 else
7608 {
9c541725 7609 sect_offset line_offset_struct = (sect_offset) line_offset;
094b34ac 7610 tu_group = create_type_unit_group (cu, line_offset_struct);
f4dc4d17
DE
7611 *slot = tu_group;
7612 ++tu_stats->nr_symtabs;
7613 }
7614
7615 return tu_group;
7616}
0018ea6f
DE
7617\f
7618/* Partial symbol tables. */
7619
7620/* Create a psymtab named NAME and assign it to PER_CU.
7621
7622 The caller must fill in the following details:
7623 dirname, textlow, texthigh. */
7624
891813be 7625static dwarf2_psymtab *
7aa104c4
SM
7626create_partial_symtab (dwarf2_per_cu_data *per_cu,
7627 dwarf2_per_objfile *per_objfile,
7628 const char *name)
0018ea6f 7629{
7aa104c4 7630 struct objfile *objfile = per_objfile->objfile;
891813be 7631 dwarf2_psymtab *pst;
0018ea6f 7632
9f4e76a4 7633 pst = new dwarf2_psymtab (name, objfile, per_cu);
0018ea6f 7634
6d94535f 7635 pst->psymtabs_addrmap_supported = true;
0018ea6f
DE
7636
7637 /* This is the glue that links PST into GDB's symbol API. */
0018ea6f
DE
7638 per_cu->v.psymtab = pst;
7639
7640 return pst;
7641}
7642
c0ab21c2 7643/* DIE reader function for process_psymtab_comp_unit. */
0018ea6f
DE
7644
7645static void
7646process_psymtab_comp_unit_reader (const struct die_reader_specs *reader,
d521ce57 7647 const gdb_byte *info_ptr,
0018ea6f 7648 struct die_info *comp_unit_die,
c0ab21c2 7649 enum language pretend_language)
0018ea6f
DE
7650{
7651 struct dwarf2_cu *cu = reader->cu;
7aa104c4
SM
7652 dwarf2_per_objfile *per_objfile = cu->per_objfile;
7653 struct objfile *objfile = per_objfile->objfile;
08feed99 7654 struct gdbarch *gdbarch = objfile->arch ();
0018ea6f 7655 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0018ea6f
DE
7656 CORE_ADDR baseaddr;
7657 CORE_ADDR best_lowpc = 0, best_highpc = 0;
891813be 7658 dwarf2_psymtab *pst;
3a2b436a 7659 enum pc_bounds_kind cu_bounds_kind;
0018ea6f 7660 const char *filename;
0018ea6f 7661
0018ea6f
DE
7662 gdb_assert (! per_cu->is_debug_types);
7663
c0ab21c2 7664 prepare_one_comp_unit (cu, comp_unit_die, pretend_language);
0018ea6f 7665
0018ea6f 7666 /* Allocate a new partial symbol table structure. */
2e927613
TV
7667 gdb::unique_xmalloc_ptr<char> debug_filename;
7668 static const char artificial[] = "<artificial>";
7d45c7c3
KB
7669 filename = dwarf2_string_attr (comp_unit_die, DW_AT_name, cu);
7670 if (filename == NULL)
0018ea6f 7671 filename = "";
2e927613
TV
7672 else if (strcmp (filename, artificial) == 0)
7673 {
7674 debug_filename.reset (concat (artificial, "@",
85f0dd3c
TV
7675 sect_offset_str (per_cu->sect_off),
7676 (char *) NULL));
2e927613
TV
7677 filename = debug_filename.get ();
7678 }
0018ea6f 7679
7aa104c4 7680 pst = create_partial_symtab (per_cu, per_objfile, filename);
0018ea6f
DE
7681
7682 /* This must be done before calling dwarf2_build_include_psymtabs. */
7d45c7c3 7683 pst->dirname = dwarf2_string_attr (comp_unit_die, DW_AT_comp_dir, cu);
0018ea6f 7684
b3b3bada 7685 baseaddr = objfile->text_section_offset ();
0018ea6f
DE
7686
7687 dwarf2_find_base_address (comp_unit_die, cu);
7688
7689 /* Possibly set the default values of LOWPC and HIGHPC from
7690 `DW_AT_ranges'. */
3a2b436a
JK
7691 cu_bounds_kind = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
7692 &best_highpc, cu, pst);
7693 if (cu_bounds_kind == PC_BOUNDS_HIGH_LOW && best_lowpc < best_highpc)
79748972
TT
7694 {
7695 CORE_ADDR low
7696 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_lowpc + baseaddr)
7697 - baseaddr);
7698 CORE_ADDR high
7699 = (gdbarch_adjust_dwarf2_addr (gdbarch, best_highpc + baseaddr)
7700 - baseaddr - 1);
7701 /* Store the contiguous range if it is not empty; it can be
7702 empty for CUs with no code. */
d320c2b5
TT
7703 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
7704 low, high, pst);
79748972 7705 }
0018ea6f
DE
7706
7707 /* Check if comp unit has_children.
7708 If so, read the rest of the partial symbols from this comp unit.
7709 If not, there's no more debug_info for this comp unit. */
3e225074 7710 if (comp_unit_die->has_children)
0018ea6f
DE
7711 {
7712 struct partial_die_info *first_die;
7713 CORE_ADDR lowpc, highpc;
7714
7715 lowpc = ((CORE_ADDR) -1);
7716 highpc = ((CORE_ADDR) 0);
7717
7718 first_die = load_partial_dies (reader, info_ptr, 1);
7719
7720 scan_partial_symbols (first_die, &lowpc, &highpc,
e385593e 7721 cu_bounds_kind <= PC_BOUNDS_INVALID, cu);
0018ea6f
DE
7722
7723 /* If we didn't find a lowpc, set it to highpc to avoid
7724 complaints from `maint check'. */
7725 if (lowpc == ((CORE_ADDR) -1))
7726 lowpc = highpc;
7727
7728 /* If the compilation unit didn't have an explicit address range,
7729 then use the information extracted from its child dies. */
e385593e 7730 if (cu_bounds_kind <= PC_BOUNDS_INVALID)
0018ea6f
DE
7731 {
7732 best_lowpc = lowpc;
7733 best_highpc = highpc;
7734 }
7735 }
4ae976d1 7736 pst->set_text_low (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7737 best_lowpc + baseaddr)
7738 - baseaddr);
4ae976d1 7739 pst->set_text_high (gdbarch_adjust_dwarf2_addr (gdbarch,
79748972
TT
7740 best_highpc + baseaddr)
7741 - baseaddr);
0018ea6f 7742
ae7754b2 7743 pst->end ();
0018ea6f 7744
ae640021 7745 if (!cu->per_cu->imported_symtabs_empty ())
0018ea6f
DE
7746 {
7747 int i;
ae640021 7748 int len = cu->per_cu->imported_symtabs_size ();
0018ea6f
DE
7749
7750 /* Fill in 'dependencies' here; we fill in 'users' in a
7751 post-pass. */
7752 pst->number_of_dependencies = len;
a9342b62
TT
7753 pst->dependencies
7754 = objfile->partial_symtabs->allocate_dependencies (len);
ae640021
AB
7755 for (i = 0; i < len; ++i)
7756 {
7757 pst->dependencies[i]
7758 = cu->per_cu->imported_symtabs->at (i)->v.psymtab;
7759 }
0018ea6f 7760
ae640021 7761 cu->per_cu->imported_symtabs_free ();
0018ea6f
DE
7762 }
7763
7764 /* Get the list of files included in the current compilation unit,
7765 and build a psymtab for each of them. */
7766 dwarf2_build_include_psymtabs (cu, comp_unit_die, pst);
7767
6f738b01
SM
7768 dwarf_read_debug_printf ("Psymtab for %s unit @%s: %s - %s"
7769 ", %d global, %d static syms",
7770 per_cu->is_debug_types ? "type" : "comp",
7771 sect_offset_str (per_cu->sect_off),
7772 paddress (gdbarch, pst->text_low (objfile)),
7773 paddress (gdbarch, pst->text_high (objfile)),
7774 (int) pst->global_psymbols.size (),
7775 (int) pst->static_psymbols.size ());
0018ea6f
DE
7776}
7777
7778/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
7779 Process compilation unit THIS_CU for a psymtab. */
7780
7781static void
ab432490
SM
7782process_psymtab_comp_unit (dwarf2_per_cu_data *this_cu,
7783 dwarf2_per_objfile *per_objfile,
135f5437 7784 bool want_partial_unit,
b93601f3 7785 enum language pretend_language)
0018ea6f
DE
7786{
7787 /* If this compilation unit was already read in, free the
7788 cached copy in order to read it in again. This is
7789 necessary because we skipped some symbols when we first
7790 read in the compilation unit (see load_partial_dies).
7791 This problem could be avoided, but the benefit is unclear. */
7188ed02 7792 per_objfile->remove_cu (this_cu);
0018ea6f 7793
2e671100 7794 cutu_reader reader (this_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2 7795
58990295
TV
7796 switch (reader.comp_unit_die->tag)
7797 {
7798 case DW_TAG_compile_unit:
7799 this_cu->unit_type = DW_UT_compile;
7800 break;
7801 case DW_TAG_partial_unit:
7802 this_cu->unit_type = DW_UT_partial;
7803 break;
7804 default:
7805 abort ();
7806 }
7807
c0ab21c2 7808 if (reader.dummy_p)
f1902523 7809 {
c0ab21c2 7810 /* Nothing. */
f1902523 7811 }
c0ab21c2 7812 else if (this_cu->is_debug_types)
3e225074
TT
7813 build_type_psymtabs_reader (&reader, reader.info_ptr,
7814 reader.comp_unit_die);
135f5437
TT
7815 else if (want_partial_unit
7816 || reader.comp_unit_die->tag != DW_TAG_partial_unit)
c0ab21c2
TT
7817 process_psymtab_comp_unit_reader (&reader, reader.info_ptr,
7818 reader.comp_unit_die,
c0ab21c2 7819 pretend_language);
0018ea6f 7820
7188ed02 7821 this_cu->lang = reader.cu->language;
58990295 7822
0018ea6f 7823 /* Age out any secondary CUs. */
7188ed02 7824 per_objfile->age_comp_units ();
0018ea6f 7825}
f4dc4d17
DE
7826
7827/* Reader function for build_type_psymtabs. */
7828
7829static void
7830build_type_psymtabs_reader (const struct die_reader_specs *reader,
d521ce57 7831 const gdb_byte *info_ptr,
3e225074 7832 struct die_info *type_unit_die)
f4dc4d17 7833{
976ca316 7834 dwarf2_per_objfile *per_objfile = reader->cu->per_objfile;
f4dc4d17
DE
7835 struct dwarf2_cu *cu = reader->cu;
7836 struct dwarf2_per_cu_data *per_cu = cu->per_cu;
0186c6a7 7837 struct signatured_type *sig_type;
f4dc4d17
DE
7838 struct type_unit_group *tu_group;
7839 struct attribute *attr;
7840 struct partial_die_info *first_die;
7841 CORE_ADDR lowpc, highpc;
891813be 7842 dwarf2_psymtab *pst;
f4dc4d17 7843
0186c6a7
DE
7844 gdb_assert (per_cu->is_debug_types);
7845 sig_type = (struct signatured_type *) per_cu;
f4dc4d17 7846
3e225074 7847 if (! type_unit_die->has_children)
f4dc4d17
DE
7848 return;
7849
052c8bb8 7850 attr = type_unit_die->attr (DW_AT_stmt_list);
094b34ac 7851 tu_group = get_type_unit_group (cu, attr);
f4dc4d17 7852
df07e2c7 7853 if (tu_group->tus == nullptr)
a8b3b8e9 7854 tu_group->tus = new std::vector<signatured_type *>;
df07e2c7 7855 tu_group->tus->push_back (sig_type);
f4dc4d17
DE
7856
7857 prepare_one_comp_unit (cu, type_unit_die, language_minimal);
976ca316 7858 pst = create_partial_symtab (per_cu, per_objfile, "");
6d94535f 7859 pst->anonymous = true;
f4dc4d17
DE
7860
7861 first_die = load_partial_dies (reader, info_ptr, 1);
7862
7863 lowpc = (CORE_ADDR) -1;
7864 highpc = (CORE_ADDR) 0;
7865 scan_partial_symbols (first_die, &lowpc, &highpc, 0, cu);
7866
ae7754b2 7867 pst->end ();
f4dc4d17
DE
7868}
7869
73051182
DE
7870/* Struct used to sort TUs by their abbreviation table offset. */
7871
7872struct tu_abbrev_offset
7873{
b2bdb8cf
SM
7874 tu_abbrev_offset (signatured_type *sig_type_, sect_offset abbrev_offset_)
7875 : sig_type (sig_type_), abbrev_offset (abbrev_offset_)
7876 {}
7877
7878 signatured_type *sig_type;
73051182
DE
7879 sect_offset abbrev_offset;
7880};
7881
484cf504 7882/* Helper routine for build_type_psymtabs_1, passed to std::sort. */
73051182 7883
484cf504
TT
7884static bool
7885sort_tu_by_abbrev_offset (const struct tu_abbrev_offset &a,
7886 const struct tu_abbrev_offset &b)
73051182 7887{
484cf504 7888 return a.abbrev_offset < b.abbrev_offset;
73051182
DE
7889}
7890
7891/* Efficiently read all the type units.
7892 This does the bulk of the work for build_type_psymtabs.
7893
7894 The efficiency is because we sort TUs by the abbrev table they use and
7895 only read each abbrev table once. In one program there are 200K TUs
7896 sharing 8K abbrev tables.
7897
7898 The main purpose of this function is to support building the
5989a64e 7899 dwarf2_per_objfile->per_bfd->type_unit_groups table.
73051182
DE
7900 TUs typically share the DW_AT_stmt_list of the CU they came from, so we
7901 can collapse the search space by grouping them by stmt_list.
7902 The savings can be significant, in the same program from above the 200K TUs
7903 share 8K stmt_list tables.
7904
7905 FUNC is expected to call get_type_unit_group, which will create the
7906 struct type_unit_group if necessary and add it to
5989a64e 7907 dwarf2_per_objfile->per_bfd->type_unit_groups. */
73051182
DE
7908
7909static void
976ca316 7910build_type_psymtabs_1 (dwarf2_per_objfile *per_objfile)
73051182 7911{
976ca316 7912 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
685af9cd 7913 abbrev_table_up abbrev_table;
73051182 7914 sect_offset abbrev_offset;
73051182
DE
7915
7916 /* It's up to the caller to not call us multiple times. */
976ca316 7917 gdb_assert (per_objfile->per_bfd->type_unit_groups == NULL);
73051182 7918
976ca316 7919 if (per_objfile->per_bfd->all_type_units.empty ())
73051182
DE
7920 return;
7921
7922 /* TUs typically share abbrev tables, and there can be way more TUs than
7923 abbrev tables. Sort by abbrev table to reduce the number of times we
7924 read each abbrev table in.
7925 Alternatives are to punt or to maintain a cache of abbrev tables.
7926 This is simpler and efficient enough for now.
7927
7928 Later we group TUs by their DW_AT_stmt_list value (as this defines the
7929 symtab to use). Typically TUs with the same abbrev offset have the same
7930 stmt_list value too so in practice this should work well.
7931
7932 The basic algorithm here is:
7933
7934 sort TUs by abbrev table
7935 for each TU with same abbrev table:
7936 read abbrev table if first user
7937 read TU top level DIE
7938 [IWBN if DWO skeletons had DW_AT_stmt_list]
7939 call FUNC */
7940
6f738b01 7941 dwarf_read_debug_printf ("Building type unit groups ...");
73051182
DE
7942
7943 /* Sort in a separate table to maintain the order of all_type_units
7944 for .gdb_index: TU indices directly index all_type_units. */
b2bdb8cf 7945 std::vector<tu_abbrev_offset> sorted_by_abbrev;
976ca316 7946 sorted_by_abbrev.reserve (per_objfile->per_bfd->all_type_units.size ());
b2bdb8cf 7947
976ca316 7948 for (signatured_type *sig_type : per_objfile->per_bfd->all_type_units)
b2bdb8cf 7949 sorted_by_abbrev.emplace_back
976ca316 7950 (sig_type, read_abbrev_offset (per_objfile, sig_type->per_cu.section,
b2bdb8cf 7951 sig_type->per_cu.sect_off));
73051182 7952
484cf504
TT
7953 std::sort (sorted_by_abbrev.begin (), sorted_by_abbrev.end (),
7954 sort_tu_by_abbrev_offset);
73051182 7955
9c541725 7956 abbrev_offset = (sect_offset) ~(unsigned) 0;
73051182 7957
b2bdb8cf 7958 for (const tu_abbrev_offset &tu : sorted_by_abbrev)
73051182 7959 {
73051182
DE
7960 /* Switch to the next abbrev table if necessary. */
7961 if (abbrev_table == NULL
b2bdb8cf 7962 || tu.abbrev_offset != abbrev_offset)
73051182 7963 {
b2bdb8cf 7964 abbrev_offset = tu.abbrev_offset;
606decb2 7965 per_objfile->per_bfd->abbrev.read (per_objfile->objfile);
73051182 7966 abbrev_table =
606decb2 7967 abbrev_table::read (&per_objfile->per_bfd->abbrev, abbrev_offset);
73051182
DE
7968 ++tu_stats->nr_uniq_abbrev_tables;
7969 }
7970
976ca316 7971 cutu_reader reader (&tu.sig_type->per_cu, per_objfile,
2e671100 7972 abbrev_table.get (), nullptr, false);
c0ab21c2
TT
7973 if (!reader.dummy_p)
7974 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 7975 reader.comp_unit_die);
73051182 7976 }
6aa5f3a6 7977}
73051182 7978
6aa5f3a6
DE
7979/* Print collected type unit statistics. */
7980
7981static void
976ca316 7982print_tu_stats (dwarf2_per_objfile *per_objfile)
6aa5f3a6 7983{
976ca316 7984 struct tu_stats *tu_stats = &per_objfile->per_bfd->tu_stats;
6aa5f3a6 7985
6f738b01
SM
7986 dwarf_read_debug_printf ("Type unit statistics:");
7987 dwarf_read_debug_printf (" %zu TUs",
7988 per_objfile->per_bfd->all_type_units.size ());
7989 dwarf_read_debug_printf (" %d uniq abbrev tables",
7990 tu_stats->nr_uniq_abbrev_tables);
7991 dwarf_read_debug_printf (" %d symtabs from stmt_list entries",
7992 tu_stats->nr_symtabs);
7993 dwarf_read_debug_printf (" %d symtab sharers",
7994 tu_stats->nr_symtab_sharers);
7995 dwarf_read_debug_printf (" %d type units without a stmt_list",
7996 tu_stats->nr_stmt_less_type_units);
7997 dwarf_read_debug_printf (" %d all_type_units reallocs",
7998 tu_stats->nr_all_type_units_reallocs);
73051182
DE
7999}
8000
f4dc4d17
DE
8001/* Traversal function for build_type_psymtabs. */
8002
8003static int
8004build_type_psymtab_dependencies (void **slot, void *info)
8005{
976ca316
SM
8006 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
8007 struct objfile *objfile = per_objfile->objfile;
f4dc4d17 8008 struct type_unit_group *tu_group = (struct type_unit_group *) *slot;
094b34ac 8009 struct dwarf2_per_cu_data *per_cu = &tu_group->per_cu;
891813be 8010 dwarf2_psymtab *pst = per_cu->v.psymtab;
df07e2c7 8011 int len = (tu_group->tus == nullptr) ? 0 : tu_group->tus->size ();
f4dc4d17
DE
8012 int i;
8013
8014 gdb_assert (len > 0);
197400e8 8015 gdb_assert (per_cu->type_unit_group_p ());
f4dc4d17
DE
8016
8017 pst->number_of_dependencies = len;
a9342b62 8018 pst->dependencies = objfile->partial_symtabs->allocate_dependencies (len);
df07e2c7 8019 for (i = 0; i < len; ++i)
f4dc4d17 8020 {
df07e2c7 8021 struct signatured_type *iter = tu_group->tus->at (i);
0186c6a7
DE
8022 gdb_assert (iter->per_cu.is_debug_types);
8023 pst->dependencies[i] = iter->per_cu.v.psymtab;
796a7ff8 8024 iter->type_unit_group = tu_group;
f4dc4d17
DE
8025 }
8026
df07e2c7
AB
8027 delete tu_group->tus;
8028 tu_group->tus = nullptr;
348e048f
DE
8029
8030 return 1;
8031}
8032
8033/* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
8034 Build partial symbol tables for the .debug_types comp-units. */
8035
8036static void
976ca316 8037build_type_psymtabs (dwarf2_per_objfile *per_objfile)
348e048f 8038{
976ca316 8039 if (! create_all_type_units (per_objfile))
348e048f
DE
8040 return;
8041
976ca316 8042 build_type_psymtabs_1 (per_objfile);
6aa5f3a6 8043}
f4dc4d17 8044
6aa5f3a6
DE
8045/* Traversal function for process_skeletonless_type_unit.
8046 Read a TU in a DWO file and build partial symbols for it. */
8047
8048static int
8049process_skeletonless_type_unit (void **slot, void *info)
8050{
8051 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
976ca316 8052 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) info;
6aa5f3a6
DE
8053 struct signatured_type find_entry, *entry;
8054
8055 /* If this TU doesn't exist in the global table, add it and read it in. */
8056
976ca316
SM
8057 if (per_objfile->per_bfd->signatured_types == NULL)
8058 per_objfile->per_bfd->signatured_types = allocate_signatured_type_table ();
6aa5f3a6
DE
8059
8060 find_entry.signature = dwo_unit->signature;
976ca316 8061 slot = htab_find_slot (per_objfile->per_bfd->signatured_types.get (),
b0b6a987 8062 &find_entry, INSERT);
6aa5f3a6
DE
8063 /* If we've already seen this type there's nothing to do. What's happening
8064 is we're doing our own version of comdat-folding here. */
8065 if (*slot != NULL)
8066 return 1;
8067
8068 /* This does the job that create_all_type_units would have done for
8069 this TU. */
976ca316
SM
8070 entry = add_type_unit (per_objfile, dwo_unit->signature, slot);
8071 fill_in_sig_entry_from_dwo_entry (per_objfile, entry, dwo_unit);
6aa5f3a6
DE
8072 *slot = entry;
8073
8074 /* This does the job that build_type_psymtabs_1 would have done. */
976ca316 8075 cutu_reader reader (&entry->per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
8076 if (!reader.dummy_p)
8077 build_type_psymtabs_reader (&reader, reader.info_ptr,
3e225074 8078 reader.comp_unit_die);
6aa5f3a6
DE
8079
8080 return 1;
8081}
8082
8083/* Traversal function for process_skeletonless_type_units. */
8084
8085static int
8086process_dwo_file_for_skeletonless_type_units (void **slot, void *info)
8087{
8088 struct dwo_file *dwo_file = (struct dwo_file *) *slot;
8089
8090 if (dwo_file->tus != NULL)
b0b6a987
TT
8091 htab_traverse_noresize (dwo_file->tus.get (),
8092 process_skeletonless_type_unit, info);
6aa5f3a6
DE
8093
8094 return 1;
8095}
8096
8097/* Scan all TUs of DWO files, verifying we've processed them.
8098 This is needed in case a TU was emitted without its skeleton.
8099 Note: This can't be done until we know what all the DWO files are. */
8100
8101static void
976ca316 8102process_skeletonless_type_units (dwarf2_per_objfile *per_objfile)
6aa5f3a6
DE
8103{
8104 /* Skeletonless TUs in DWP files without .gdb_index is not supported yet. */
976ca316
SM
8105 if (get_dwp_file (per_objfile) == NULL
8106 && per_objfile->per_bfd->dwo_files != NULL)
6aa5f3a6 8107 {
976ca316 8108 htab_traverse_noresize (per_objfile->per_bfd->dwo_files.get (),
6aa5f3a6 8109 process_dwo_file_for_skeletonless_type_units,
976ca316 8110 per_objfile);
6aa5f3a6 8111 }
348e048f
DE
8112}
8113
ed2dc618 8114/* Compute the 'user' field for each psymtab in DWARF2_PER_OBJFILE. */
95554aad
TT
8115
8116static void
976ca316 8117set_partial_user (dwarf2_per_objfile *per_objfile)
95554aad 8118{
976ca316 8119 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
95554aad 8120 {
891813be 8121 dwarf2_psymtab *pst = per_cu->v.psymtab;
95554aad 8122
36586728
TT
8123 if (pst == NULL)
8124 continue;
8125
b76e467d 8126 for (int j = 0; j < pst->number_of_dependencies; ++j)
95554aad
TT
8127 {
8128 /* Set the 'user' field only if it is not already set. */
8129 if (pst->dependencies[j]->user == NULL)
8130 pst->dependencies[j]->user = pst;
8131 }
8132 }
8133}
8134
93311388
DE
8135/* Build the partial symbol table by doing a quick pass through the
8136 .debug_info and .debug_abbrev sections. */
72bf9492 8137
93311388 8138static void
976ca316 8139dwarf2_build_psymtabs_hard (dwarf2_per_objfile *per_objfile)
93311388 8140{
976ca316 8141 struct objfile *objfile = per_objfile->objfile;
93311388 8142
6f738b01
SM
8143 dwarf_read_debug_printf ("Building psymtabs of objfile %s ...",
8144 objfile_name (objfile));
45cfd468 8145
76935768 8146 scoped_restore restore_reading_psyms
976ca316 8147 = make_scoped_restore (&per_objfile->per_bfd->reading_partial_symbols,
76935768 8148 true);
98bfdba5 8149
976ca316 8150 per_objfile->per_bfd->info.read (objfile);
91c24f0a 8151
93311388
DE
8152 /* Any cached compilation units will be linked by the per-objfile
8153 read_in_chain. Make sure to free them when we're done. */
976ca316 8154 free_cached_comp_units freer (per_objfile);
72bf9492 8155
976ca316 8156 build_type_psymtabs (per_objfile);
348e048f 8157
976ca316 8158 create_all_comp_units (per_objfile);
c906108c 8159
60606b2c
TT
8160 /* Create a temporary address map on a temporary obstack. We later
8161 copy this to the final obstack. */
8268c778 8162 auto_obstack temp_obstack;
791afaa2
TT
8163
8164 scoped_restore save_psymtabs_addrmap
d320c2b5 8165 = make_scoped_restore (&objfile->partial_symtabs->psymtabs_addrmap,
791afaa2 8166 addrmap_create_mutable (&temp_obstack));
72bf9492 8167
976ca316 8168 for (dwarf2_per_cu_data *per_cu : per_objfile->per_bfd->all_comp_units)
3d5afab3
TV
8169 {
8170 if (per_cu->v.psymtab != NULL)
8171 /* In case a forward DW_TAG_imported_unit has read the CU already. */
8172 continue;
976ca316 8173 process_psymtab_comp_unit (per_cu, per_objfile, false,
ab432490 8174 language_minimal);
3d5afab3 8175 }
ff013f42 8176
6aa5f3a6 8177 /* This has to wait until we read the CUs, we need the list of DWOs. */
976ca316 8178 process_skeletonless_type_units (per_objfile);
6aa5f3a6
DE
8179
8180 /* Now that all TUs have been processed we can fill in the dependencies. */
976ca316 8181 if (per_objfile->per_bfd->type_unit_groups != NULL)
6aa5f3a6 8182 {
976ca316
SM
8183 htab_traverse_noresize (per_objfile->per_bfd->type_unit_groups.get (),
8184 build_type_psymtab_dependencies, per_objfile);
6aa5f3a6
DE
8185 }
8186
6f738b01 8187 if (dwarf_read_debug > 0)
976ca316 8188 print_tu_stats (per_objfile);
6aa5f3a6 8189
976ca316 8190 set_partial_user (per_objfile);
95554aad 8191
d320c2b5
TT
8192 objfile->partial_symtabs->psymtabs_addrmap
8193 = addrmap_create_fixed (objfile->partial_symtabs->psymtabs_addrmap,
5923a04c 8194 objfile->partial_symtabs->obstack ());
791afaa2
TT
8195 /* At this point we want to keep the address map. */
8196 save_psymtabs_addrmap.release ();
ff013f42 8197
6f738b01
SM
8198 dwarf_read_debug_printf ("Done building psymtabs of %s",
8199 objfile_name (objfile));
ae038cb0
DJ
8200}
8201
dee91e82
DE
8202/* Load the partial DIEs for a secondary CU into memory.
8203 This is also used when rereading a primary CU with load_all_dies. */
c5b7e1cb 8204
dee91e82 8205static void
ab432490 8206load_partial_comp_unit (dwarf2_per_cu_data *this_cu,
2e671100
SM
8207 dwarf2_per_objfile *per_objfile,
8208 dwarf2_cu *existing_cu)
dee91e82 8209{
2e671100 8210 cutu_reader reader (this_cu, per_objfile, nullptr, existing_cu, false);
c0ab21c2
TT
8211
8212 if (!reader.dummy_p)
8213 {
8214 prepare_one_comp_unit (reader.cu, reader.comp_unit_die,
8215 language_minimal);
8216
8217 /* Check if comp unit has_children.
8218 If so, read the rest of the partial symbols from this comp unit.
8219 If not, there's no more debug_info for this comp unit. */
3e225074 8220 if (reader.comp_unit_die->has_children)
c0ab21c2 8221 load_partial_dies (&reader, reader.info_ptr, 0);
6751ebae
TT
8222
8223 reader.keep ();
c0ab21c2 8224 }
ae038cb0
DJ
8225}
8226
ae038cb0 8227static void
976ca316 8228read_comp_units_from_section (dwarf2_per_objfile *per_objfile,
36586728 8229 struct dwarf2_section_info *section,
f1902523 8230 struct dwarf2_section_info *abbrev_section,
b76e467d 8231 unsigned int is_dwz)
ae038cb0 8232{
d521ce57 8233 const gdb_byte *info_ptr;
976ca316 8234 struct objfile *objfile = per_objfile->objfile;
be391dca 8235
6f738b01
SM
8236 dwarf_read_debug_printf ("Reading %s for %s",
8237 section->get_name (),
8238 section->get_file_name ());
bf6af496 8239
96b79293 8240 section->read (objfile);
ae038cb0 8241
36586728 8242 info_ptr = section->buffer;
6e70227d 8243
36586728 8244 while (info_ptr < section->buffer + section->size)
ae038cb0 8245 {
ae038cb0 8246 struct dwarf2_per_cu_data *this_cu;
ae038cb0 8247
9c541725 8248 sect_offset sect_off = (sect_offset) (info_ptr - section->buffer);
ae038cb0 8249
f1902523 8250 comp_unit_head cu_header;
976ca316 8251 read_and_check_comp_unit_head (per_objfile, &cu_header, section,
ed2dc618
SM
8252 abbrev_section, info_ptr,
8253 rcuh_kind::COMPILE);
ae038cb0
DJ
8254
8255 /* Save the compilation unit for later lookup. */
f1902523 8256 if (cu_header.unit_type != DW_UT_type)
976ca316 8257 this_cu = per_objfile->per_bfd->allocate_per_cu ();
f1902523
JK
8258 else
8259 {
976ca316 8260 auto sig_type = per_objfile->per_bfd->allocate_signatured_type ();
f1902523
JK
8261 sig_type->signature = cu_header.signature;
8262 sig_type->type_offset_in_tu = cu_header.type_cu_offset_in_tu;
8263 this_cu = &sig_type->per_cu;
8264 }
8265 this_cu->is_debug_types = (cu_header.unit_type == DW_UT_type);
9c541725 8266 this_cu->sect_off = sect_off;
f1902523 8267 this_cu->length = cu_header.length + cu_header.initial_length_size;
36586728 8268 this_cu->is_dwz = is_dwz;
8a0459fd 8269 this_cu->section = section;
ae038cb0 8270
976ca316 8271 per_objfile->per_bfd->all_comp_units.push_back (this_cu);
ae038cb0
DJ
8272
8273 info_ptr = info_ptr + this_cu->length;
8274 }
36586728
TT
8275}
8276
8277/* Create a list of all compilation units in OBJFILE.
8278 This is only done for -readnow and building partial symtabs. */
8279
8280static void
976ca316 8281create_all_comp_units (dwarf2_per_objfile *per_objfile)
36586728 8282{
976ca316
SM
8283 gdb_assert (per_objfile->per_bfd->all_comp_units.empty ());
8284 read_comp_units_from_section (per_objfile, &per_objfile->per_bfd->info,
8285 &per_objfile->per_bfd->abbrev, 0);
36586728 8286
976ca316 8287 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
4db1a1dc 8288 if (dwz != NULL)
976ca316 8289 read_comp_units_from_section (per_objfile, &dwz->info, &dwz->abbrev, 1);
c906108c
SS
8290}
8291
5734ee8b 8292/* Process all loaded DIEs for compilation unit CU, starting at
cdc07690 8293 FIRST_DIE. The caller should pass SET_ADDRMAP == 1 if the compilation
5734ee8b 8294 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
cdc07690
YQ
8295 DW_AT_ranges). See the comments of add_partial_subprogram on how
8296 SET_ADDRMAP is used and how *LOWPC and *HIGHPC are updated. */
c906108c 8297
72bf9492
DJ
8298static void
8299scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
cdc07690
YQ
8300 CORE_ADDR *highpc, int set_addrmap,
8301 struct dwarf2_cu *cu)
c906108c 8302{
72bf9492 8303 struct partial_die_info *pdi;
c906108c 8304
91c24f0a
DC
8305 /* Now, march along the PDI's, descending into ones which have
8306 interesting children but skipping the children of the other ones,
8307 until we reach the end of the compilation unit. */
c906108c 8308
72bf9492 8309 pdi = first_die;
91c24f0a 8310
72bf9492
DJ
8311 while (pdi != NULL)
8312 {
52356b79 8313 pdi->fixup (cu);
c906108c 8314
f55ee35c 8315 /* Anonymous namespaces or modules have no name but have interesting
91c24f0a
DC
8316 children, so we need to look at them. Ditto for anonymous
8317 enums. */
933c6fe4 8318
7d00ffec 8319 if (pdi->raw_name != NULL || pdi->tag == DW_TAG_namespace
95554aad 8320 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type
b1dc1806
XR
8321 || pdi->tag == DW_TAG_imported_unit
8322 || pdi->tag == DW_TAG_inlined_subroutine)
c906108c 8323 {
72bf9492 8324 switch (pdi->tag)
c906108c
SS
8325 {
8326 case DW_TAG_subprogram:
b1dc1806 8327 case DW_TAG_inlined_subroutine:
cdc07690 8328 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
f9b5d5ea
TV
8329 if (cu->language == language_cplus)
8330 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8331 set_addrmap, cu);
c906108c 8332 break;
72929c62 8333 case DW_TAG_constant:
c906108c
SS
8334 case DW_TAG_variable:
8335 case DW_TAG_typedef:
91c24f0a 8336 case DW_TAG_union_type:
317d2668
TV
8337 if (!pdi->is_declaration
8338 || (pdi->tag == DW_TAG_variable && pdi->is_external))
63d06c5c 8339 {
72bf9492 8340 add_partial_symbol (pdi, cu);
63d06c5c
DC
8341 }
8342 break;
c906108c 8343 case DW_TAG_class_type:
680b30c7 8344 case DW_TAG_interface_type:
c906108c 8345 case DW_TAG_structure_type:
72bf9492 8346 if (!pdi->is_declaration)
c906108c 8347 {
72bf9492 8348 add_partial_symbol (pdi, cu);
c906108c 8349 }
b7fee5a3
KS
8350 if ((cu->language == language_rust
8351 || cu->language == language_cplus) && pdi->has_children)
e98c9e7c
TT
8352 scan_partial_symbols (pdi->die_child, lowpc, highpc,
8353 set_addrmap, cu);
c906108c 8354 break;
91c24f0a 8355 case DW_TAG_enumeration_type:
72bf9492
DJ
8356 if (!pdi->is_declaration)
8357 add_partial_enumeration (pdi, cu);
c906108c
SS
8358 break;
8359 case DW_TAG_base_type:
dda83cd7 8360 case DW_TAG_subrange_type:
c906108c 8361 /* File scope base type definitions are added to the partial
dda83cd7 8362 symbol table. */
72bf9492 8363 add_partial_symbol (pdi, cu);
c906108c 8364 break;
d9fa45fe 8365 case DW_TAG_namespace:
cdc07690 8366 add_partial_namespace (pdi, lowpc, highpc, set_addrmap, cu);
91c24f0a 8367 break;
5d7cb8df 8368 case DW_TAG_module:
59c35742
AB
8369 if (!pdi->is_declaration)
8370 add_partial_module (pdi, lowpc, highpc, set_addrmap, cu);
5d7cb8df 8371 break;
95554aad
TT
8372 case DW_TAG_imported_unit:
8373 {
8374 struct dwarf2_per_cu_data *per_cu;
8375
f4dc4d17
DE
8376 /* For now we don't handle imported units in type units. */
8377 if (cu->per_cu->is_debug_types)
8378 {
8379 error (_("Dwarf Error: DW_TAG_imported_unit is not"
8380 " supported in type units [in module %s]"),
5e22e966 8381 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
8382 }
8383
e3b94546 8384 per_cu = dwarf2_find_containing_comp_unit
5e22e966 8385 (pdi->d.sect_off, pdi->is_dwz, cu->per_objfile);
95554aad
TT
8386
8387 /* Go read the partial unit, if needed. */
8388 if (per_cu->v.psymtab == NULL)
ab432490
SM
8389 process_psymtab_comp_unit (per_cu, cu->per_objfile, true,
8390 cu->language);
95554aad 8391
ae640021 8392 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
8393 }
8394 break;
74921315
KS
8395 case DW_TAG_imported_declaration:
8396 add_partial_symbol (pdi, cu);
8397 break;
c906108c
SS
8398 default:
8399 break;
8400 }
8401 }
8402
72bf9492
DJ
8403 /* If the die has a sibling, skip to the sibling. */
8404
8405 pdi = pdi->die_sibling;
8406 }
8407}
8408
8409/* Functions used to compute the fully scoped name of a partial DIE.
91c24f0a 8410
72bf9492 8411 Normally, this is simple. For C++, the parent DIE's fully scoped
9c37b5ae 8412 name is concatenated with "::" and the partial DIE's name.
72bf9492
DJ
8413 Enumerators are an exception; they use the scope of their parent
8414 enumeration type, i.e. the name of the enumeration type is not
8415 prepended to the enumerator.
91c24f0a 8416
72bf9492
DJ
8417 There are two complexities. One is DW_AT_specification; in this
8418 case "parent" means the parent of the target of the specification,
8419 instead of the direct parent of the DIE. The other is compilers
8420 which do not emit DW_TAG_namespace; in this case we try to guess
8421 the fully qualified name of structure types from their members'
8422 linkage names. This must be done using the DIE's children rather
8423 than the children of any DW_AT_specification target. We only need
8424 to do this for structures at the top level, i.e. if the target of
8425 any DW_AT_specification (if any; otherwise the DIE itself) does not
8426 have a parent. */
8427
8428/* Compute the scope prefix associated with PDI's parent, in
8429 compilation unit CU. The result will be allocated on CU's
8430 comp_unit_obstack, or a copy of the already allocated PDI->NAME
8431 field. NULL is returned if no prefix is necessary. */
15d034d0 8432static const char *
72bf9492
DJ
8433partial_die_parent_scope (struct partial_die_info *pdi,
8434 struct dwarf2_cu *cu)
8435{
15d034d0 8436 const char *grandparent_scope;
72bf9492 8437 struct partial_die_info *parent, *real_pdi;
91c24f0a 8438
72bf9492
DJ
8439 /* We need to look at our parent DIE; if we have a DW_AT_specification,
8440 then this means the parent of the specification DIE. */
8441
8442 real_pdi = pdi;
72bf9492 8443 while (real_pdi->has_specification)
fb816e8b 8444 {
122cf0f2
AB
8445 auto res = find_partial_die (real_pdi->spec_offset,
8446 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
8447 real_pdi = res.pdi;
8448 cu = res.cu;
8449 }
72bf9492
DJ
8450
8451 parent = real_pdi->die_parent;
8452 if (parent == NULL)
8453 return NULL;
8454
8455 if (parent->scope_set)
8456 return parent->scope;
8457
52356b79 8458 parent->fixup (cu);
72bf9492 8459
10b3939b 8460 grandparent_scope = partial_die_parent_scope (parent, cu);
72bf9492 8461
acebe513
UW
8462 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
8463 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
8464 Work around this problem here. */
8465 if (cu->language == language_cplus
6e70227d 8466 && parent->tag == DW_TAG_namespace
7d00ffec 8467 && strcmp (parent->name (cu), "::") == 0
acebe513
UW
8468 && grandparent_scope == NULL)
8469 {
8470 parent->scope = NULL;
8471 parent->scope_set = 1;
8472 return NULL;
8473 }
8474
0a4b0913 8475 /* Nested subroutines in Fortran get a prefix. */
9c6c53f7
SA
8476 if (pdi->tag == DW_TAG_enumerator)
8477 /* Enumerators should not get the name of the enumeration as a prefix. */
8478 parent->scope = grandparent_scope;
8479 else if (parent->tag == DW_TAG_namespace
f55ee35c 8480 || parent->tag == DW_TAG_module
72bf9492
DJ
8481 || parent->tag == DW_TAG_structure_type
8482 || parent->tag == DW_TAG_class_type
680b30c7 8483 || parent->tag == DW_TAG_interface_type
ceeb3d5a 8484 || parent->tag == DW_TAG_union_type
0a4b0913
AB
8485 || parent->tag == DW_TAG_enumeration_type
8486 || (cu->language == language_fortran
8487 && parent->tag == DW_TAG_subprogram
8488 && pdi->tag == DW_TAG_subprogram))
72bf9492
DJ
8489 {
8490 if (grandparent_scope == NULL)
7d00ffec 8491 parent->scope = parent->name (cu);
72bf9492 8492 else
3e43a32a
MS
8493 parent->scope = typename_concat (&cu->comp_unit_obstack,
8494 grandparent_scope,
7d00ffec 8495 parent->name (cu), 0, cu);
72bf9492 8496 }
72bf9492
DJ
8497 else
8498 {
8499 /* FIXME drow/2004-04-01: What should we be doing with
8500 function-local names? For partial symbols, we should probably be
8501 ignoring them. */
fa9c3fa0
TT
8502 complaint (_("unhandled containing DIE tag %s for DIE at %s"),
8503 dwarf_tag_name (parent->tag),
8504 sect_offset_str (pdi->sect_off));
72bf9492 8505 parent->scope = grandparent_scope;
c906108c
SS
8506 }
8507
72bf9492
DJ
8508 parent->scope_set = 1;
8509 return parent->scope;
8510}
8511
8512/* Return the fully scoped name associated with PDI, from compilation unit
8513 CU. The result will be allocated with malloc. */
4568ecf9 8514
43816ebc 8515static gdb::unique_xmalloc_ptr<char>
72bf9492
DJ
8516partial_die_full_name (struct partial_die_info *pdi,
8517 struct dwarf2_cu *cu)
8518{
15d034d0 8519 const char *parent_scope;
72bf9492 8520
98bfdba5
PA
8521 /* If this is a template instantiation, we can not work out the
8522 template arguments from partial DIEs. So, unfortunately, we have
8523 to go through the full DIEs. At least any work we do building
8524 types here will be reused if full symbols are loaded later. */
8525 if (pdi->has_template_arguments)
8526 {
52356b79 8527 pdi->fixup (cu);
98bfdba5 8528
7d00ffec 8529 if (pdi->name (cu) != NULL && strchr (pdi->name (cu), '<') == NULL)
98bfdba5
PA
8530 {
8531 struct die_info *die;
8532 struct attribute attr;
8533 struct dwarf2_cu *ref_cu = cu;
8534
b64f50a1 8535 /* DW_FORM_ref_addr is using section offset. */
b4069958 8536 attr.name = (enum dwarf_attribute) 0;
98bfdba5 8537 attr.form = DW_FORM_ref_addr;
9c541725 8538 attr.u.unsnd = to_underlying (pdi->sect_off);
98bfdba5
PA
8539 die = follow_die_ref (NULL, &attr, &ref_cu);
8540
43816ebc 8541 return make_unique_xstrdup (dwarf2_full_name (NULL, die, ref_cu));
98bfdba5
PA
8542 }
8543 }
8544
72bf9492
DJ
8545 parent_scope = partial_die_parent_scope (pdi, cu);
8546 if (parent_scope == NULL)
8547 return NULL;
8548 else
43816ebc 8549 return gdb::unique_xmalloc_ptr<char> (typename_concat (NULL, parent_scope,
7d00ffec
TT
8550 pdi->name (cu),
8551 0, cu));
c906108c
SS
8552}
8553
8554static void
72bf9492 8555add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
c906108c 8556{
976ca316
SM
8557 dwarf2_per_objfile *per_objfile = cu->per_objfile;
8558 struct objfile *objfile = per_objfile->objfile;
08feed99 8559 struct gdbarch *gdbarch = objfile->arch ();
c906108c 8560 CORE_ADDR addr = 0;
15d034d0 8561 const char *actual_name = NULL;
e142c38c
DJ
8562 CORE_ADDR baseaddr;
8563
b3b3bada 8564 baseaddr = objfile->text_section_offset ();
c906108c 8565
43816ebc
TT
8566 gdb::unique_xmalloc_ptr<char> built_actual_name
8567 = partial_die_full_name (pdi, cu);
15d034d0 8568 if (built_actual_name != NULL)
43816ebc 8569 actual_name = built_actual_name.get ();
63d06c5c 8570
72bf9492 8571 if (actual_name == NULL)
7d00ffec 8572 actual_name = pdi->name (cu);
72bf9492 8573
76e288d1
TT
8574 partial_symbol psymbol;
8575 memset (&psymbol, 0, sizeof (psymbol));
8576 psymbol.ginfo.set_language (cu->language, &objfile->objfile_obstack);
8577 psymbol.ginfo.section = -1;
8578
8579 /* The code below indicates that the psymbol should be installed by
8580 setting this. */
8581 gdb::optional<psymbol_placement> where;
8582
c906108c
SS
8583 switch (pdi->tag)
8584 {
b1dc1806 8585 case DW_TAG_inlined_subroutine:
c906108c 8586 case DW_TAG_subprogram:
79748972
TT
8587 addr = (gdbarch_adjust_dwarf2_addr (gdbarch, pdi->lowpc + baseaddr)
8588 - baseaddr);
0a4b0913
AB
8589 if (pdi->is_external
8590 || cu->language == language_ada
8591 || (cu->language == language_fortran
8592 && pdi->die_parent != NULL
8593 && pdi->die_parent->tag == DW_TAG_subprogram))
8594 {
dda83cd7
SM
8595 /* Normally, only "external" DIEs are part of the global scope.
8596 But in Ada and Fortran, we want to be able to access nested
8597 procedures globally. So all Ada and Fortran subprograms are
8598 stored in the global scope. */
76e288d1 8599 where = psymbol_placement::GLOBAL;
c906108c
SS
8600 }
8601 else
76e288d1
TT
8602 where = psymbol_placement::STATIC;
8603
8604 psymbol.domain = VAR_DOMAIN;
8605 psymbol.aclass = LOC_BLOCK;
8606 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8607 psymbol.ginfo.value.address = addr;
0c1b455e
TT
8608
8609 if (pdi->main_subprogram && actual_name != NULL)
8610 set_objfile_main_name (objfile, actual_name, cu->language);
c906108c 8611 break;
72929c62 8612 case DW_TAG_constant:
76e288d1
TT
8613 psymbol.domain = VAR_DOMAIN;
8614 psymbol.aclass = LOC_STATIC;
8615 where = (pdi->is_external
8616 ? psymbol_placement::GLOBAL
8617 : psymbol_placement::STATIC);
72929c62 8618 break;
c906108c 8619 case DW_TAG_variable:
95554aad
TT
8620 if (pdi->d.locdesc)
8621 addr = decode_locdesc (pdi->d.locdesc, cu);
caac4577 8622
95554aad 8623 if (pdi->d.locdesc
caac4577 8624 && addr == 0
976ca316 8625 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
8626 {
8627 /* A global or static variable may also have been stripped
8628 out by the linker if unused, in which case its address
8629 will be nullified; do not add such variables into partial
8630 symbol table then. */
8631 }
8632 else if (pdi->is_external)
c906108c
SS
8633 {
8634 /* Global Variable.
8635 Don't enter into the minimal symbol tables as there is
8636 a minimal symbol table entry from the ELF symbols already.
8637 Enter into partial symbol table if it has a location
8638 descriptor or a type.
8639 If the location descriptor is missing, new_symbol will create
8640 a LOC_UNRESOLVED symbol, the address of the variable will then
8641 be determined from the minimal symbol table whenever the variable
8642 is referenced.
8643 The address for the partial symbol table entry is not
8644 used by GDB, but it comes in handy for debugging partial symbol
8645 table building. */
8646
95554aad 8647 if (pdi->d.locdesc || pdi->has_type)
76e288d1
TT
8648 {
8649 psymbol.domain = VAR_DOMAIN;
8650 psymbol.aclass = LOC_STATIC;
8651 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8652 psymbol.ginfo.value.address = addr;
8653 where = psymbol_placement::GLOBAL;
8654 }
c906108c
SS
8655 }
8656 else
8657 {
ff908ebf
AW
8658 int has_loc = pdi->d.locdesc != NULL;
8659
8660 /* Static Variable. Skip symbols whose value we cannot know (those
8661 without location descriptors or constant values). */
8662 if (!has_loc && !pdi->has_const_value)
43816ebc 8663 return;
ff908ebf 8664
76e288d1
TT
8665 psymbol.domain = VAR_DOMAIN;
8666 psymbol.aclass = LOC_STATIC;
8667 psymbol.ginfo.section = SECT_OFF_TEXT (objfile);
8668 if (has_loc)
8669 psymbol.ginfo.value.address = addr;
8670 where = psymbol_placement::STATIC;
c906108c
SS
8671 }
8672 break;
d8f62e84 8673 case DW_TAG_array_type:
c906108c
SS
8674 case DW_TAG_typedef:
8675 case DW_TAG_base_type:
a02abb62 8676 case DW_TAG_subrange_type:
76e288d1
TT
8677 psymbol.domain = VAR_DOMAIN;
8678 psymbol.aclass = LOC_TYPEDEF;
8679 where = psymbol_placement::STATIC;
c906108c 8680 break;
74921315 8681 case DW_TAG_imported_declaration:
72bf9492 8682 case DW_TAG_namespace:
76e288d1
TT
8683 psymbol.domain = VAR_DOMAIN;
8684 psymbol.aclass = LOC_TYPEDEF;
8685 where = psymbol_placement::GLOBAL;
72bf9492 8686 break;
530e8392 8687 case DW_TAG_module:
a5fd13a9 8688 /* With Fortran 77 there might be a "BLOCK DATA" module
dda83cd7
SM
8689 available without any name. If so, we skip the module as it
8690 doesn't bring any value. */
a5fd13a9 8691 if (actual_name != nullptr)
76e288d1
TT
8692 {
8693 psymbol.domain = MODULE_DOMAIN;
8694 psymbol.aclass = LOC_TYPEDEF;
8695 where = psymbol_placement::GLOBAL;
8696 }
530e8392 8697 break;
c906108c 8698 case DW_TAG_class_type:
680b30c7 8699 case DW_TAG_interface_type:
c906108c
SS
8700 case DW_TAG_structure_type:
8701 case DW_TAG_union_type:
8702 case DW_TAG_enumeration_type:
fa4028e9 8703 /* Skip external references. The DWARF standard says in the section
dda83cd7
SM
8704 about "Structure, Union, and Class Type Entries": "An incomplete
8705 structure, union or class type is represented by a structure,
8706 union or class entry that does not have a byte size attribute
8707 and that has a DW_AT_declaration attribute." */
fa4028e9 8708 if (!pdi->has_byte_size && pdi->is_declaration)
43816ebc 8709 return;
fa4028e9 8710
63d06c5c
DC
8711 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
8712 static vs. global. */
76e288d1
TT
8713 psymbol.domain = STRUCT_DOMAIN;
8714 psymbol.aclass = LOC_TYPEDEF;
8715 where = (cu->language == language_cplus
8716 ? psymbol_placement::GLOBAL
8717 : psymbol_placement::STATIC);
c906108c
SS
8718 break;
8719 case DW_TAG_enumerator:
76e288d1
TT
8720 psymbol.domain = VAR_DOMAIN;
8721 psymbol.aclass = LOC_CONST;
8722 where = (cu->language == language_cplus
8723 ? psymbol_placement::GLOBAL
8724 : psymbol_placement::STATIC);
c906108c
SS
8725 break;
8726 default:
8727 break;
8728 }
76e288d1
TT
8729
8730 if (where.has_value ())
8731 {
f049a313
TT
8732 if (built_actual_name != nullptr)
8733 actual_name = objfile->intern (actual_name);
bcfe6157
TT
8734 if (pdi->linkage_name == nullptr || cu->language == language_ada)
8735 psymbol.ginfo.set_linkage_name (actual_name);
8736 else
8737 {
8738 psymbol.ginfo.set_demangled_name (actual_name,
8739 &objfile->objfile_obstack);
8740 psymbol.ginfo.set_linkage_name (pdi->linkage_name);
8741 }
932539d7 8742 cu->per_cu->v.psymtab->add_psymbol (psymbol, *where, objfile);
76e288d1 8743 }
c906108c
SS
8744}
8745
5c4e30ca
DC
8746/* Read a partial die corresponding to a namespace; also, add a symbol
8747 corresponding to that namespace to the symbol table. NAMESPACE is
8748 the name of the enclosing namespace. */
91c24f0a 8749
72bf9492
DJ
8750static void
8751add_partial_namespace (struct partial_die_info *pdi,
91c24f0a 8752 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8753 int set_addrmap, struct dwarf2_cu *cu)
91c24f0a 8754{
72bf9492 8755 /* Add a symbol for the namespace. */
e7c27a73 8756
72bf9492 8757 add_partial_symbol (pdi, cu);
5c4e30ca
DC
8758
8759 /* Now scan partial symbols in that namespace. */
8760
91c24f0a 8761 if (pdi->has_children)
cdc07690 8762 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
91c24f0a
DC
8763}
8764
5d7cb8df
JK
8765/* Read a partial die corresponding to a Fortran module. */
8766
8767static void
8768add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
cdc07690 8769 CORE_ADDR *highpc, int set_addrmap, struct dwarf2_cu *cu)
5d7cb8df 8770{
530e8392
KB
8771 /* Add a symbol for the namespace. */
8772
8773 add_partial_symbol (pdi, cu);
8774
f55ee35c 8775 /* Now scan partial symbols in that module. */
5d7cb8df
JK
8776
8777 if (pdi->has_children)
cdc07690 8778 scan_partial_symbols (pdi->die_child, lowpc, highpc, set_addrmap, cu);
5d7cb8df
JK
8779}
8780
b1dc1806
XR
8781/* Read a partial die corresponding to a subprogram or an inlined
8782 subprogram and create a partial symbol for that subprogram.
8783 When the CU language allows it, this routine also defines a partial
8784 symbol for each nested subprogram that this subprogram contains.
8785 If SET_ADDRMAP is true, record the covered ranges in the addrmap.
8786 Set *LOWPC and *HIGHPC to the lowest and highest PC values found in PDI.
6e70227d 8787
cdc07690
YQ
8788 PDI may also be a lexical block, in which case we simply search
8789 recursively for subprograms defined inside that lexical block.
bc30ff58
JB
8790 Again, this is only performed when the CU language allows this
8791 type of definitions. */
8792
8793static void
8794add_partial_subprogram (struct partial_die_info *pdi,
8795 CORE_ADDR *lowpc, CORE_ADDR *highpc,
cdc07690 8796 int set_addrmap, struct dwarf2_cu *cu)
bc30ff58 8797{
b1dc1806 8798 if (pdi->tag == DW_TAG_subprogram || pdi->tag == DW_TAG_inlined_subroutine)
bc30ff58
JB
8799 {
8800 if (pdi->has_pc_info)
dda83cd7
SM
8801 {
8802 if (pdi->lowpc < *lowpc)
8803 *lowpc = pdi->lowpc;
8804 if (pdi->highpc > *highpc)
8805 *highpc = pdi->highpc;
cdc07690 8806 if (set_addrmap)
5734ee8b 8807 {
5e22e966 8808 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 8809 struct gdbarch *gdbarch = objfile->arch ();
3e29f34a 8810 CORE_ADDR baseaddr;
b926417a
TT
8811 CORE_ADDR this_highpc;
8812 CORE_ADDR this_lowpc;
5734ee8b 8813
b3b3bada 8814 baseaddr = objfile->text_section_offset ();
b926417a
TT
8815 this_lowpc
8816 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8817 pdi->lowpc + baseaddr)
8818 - baseaddr);
8819 this_highpc
8820 = (gdbarch_adjust_dwarf2_addr (gdbarch,
8821 pdi->highpc + baseaddr)
8822 - baseaddr);
d320c2b5 8823 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
b926417a 8824 this_lowpc, this_highpc - 1,
9291a0cd 8825 cu->per_cu->v.psymtab);
5734ee8b 8826 }
dda83cd7 8827 }
481860b3
GB
8828
8829 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
8830 {
dda83cd7 8831 if (!pdi->is_declaration)
e8d05480
JB
8832 /* Ignore subprogram DIEs that do not have a name, they are
8833 illegal. Do not emit a complaint at this point, we will
8834 do so when we convert this psymtab into a symtab. */
7d00ffec 8835 if (pdi->name (cu))
e8d05480 8836 add_partial_symbol (pdi, cu);
dda83cd7 8837 }
bc30ff58 8838 }
6e70227d 8839
bc30ff58
JB
8840 if (! pdi->has_children)
8841 return;
8842
0a4b0913 8843 if (cu->language == language_ada || cu->language == language_fortran)
bc30ff58
JB
8844 {
8845 pdi = pdi->die_child;
8846 while (pdi != NULL)
8847 {
52356b79 8848 pdi->fixup (cu);
bc30ff58 8849 if (pdi->tag == DW_TAG_subprogram
b1dc1806 8850 || pdi->tag == DW_TAG_inlined_subroutine
bc30ff58 8851 || pdi->tag == DW_TAG_lexical_block)
cdc07690 8852 add_partial_subprogram (pdi, lowpc, highpc, set_addrmap, cu);
bc30ff58
JB
8853 pdi = pdi->die_sibling;
8854 }
8855 }
8856}
8857
91c24f0a
DC
8858/* Read a partial die corresponding to an enumeration type. */
8859
72bf9492
DJ
8860static void
8861add_partial_enumeration (struct partial_die_info *enum_pdi,
8862 struct dwarf2_cu *cu)
91c24f0a 8863{
72bf9492 8864 struct partial_die_info *pdi;
91c24f0a 8865
7d00ffec 8866 if (enum_pdi->name (cu) != NULL)
72bf9492
DJ
8867 add_partial_symbol (enum_pdi, cu);
8868
8869 pdi = enum_pdi->die_child;
8870 while (pdi)
91c24f0a 8871 {
7d00ffec 8872 if (pdi->tag != DW_TAG_enumerator || pdi->raw_name == NULL)
b98664d3 8873 complaint (_("malformed enumerator DIE ignored"));
91c24f0a 8874 else
72bf9492
DJ
8875 add_partial_symbol (pdi, cu);
8876 pdi = pdi->die_sibling;
91c24f0a 8877 }
91c24f0a
DC
8878}
8879
6caca83c
CC
8880/* Return the initial uleb128 in the die at INFO_PTR. */
8881
8882static unsigned int
d521ce57 8883peek_abbrev_code (bfd *abfd, const gdb_byte *info_ptr)
6caca83c
CC
8884{
8885 unsigned int bytes_read;
8886
8887 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8888}
8889
685af9cd
TT
8890/* Read the initial uleb128 in the die at INFO_PTR in compilation unit
8891 READER::CU. Use READER::ABBREV_TABLE to lookup any abbreviation.
8892
4bb7a0a7
DJ
8893 Return the corresponding abbrev, or NULL if the number is zero (indicating
8894 an empty DIE). In either case *BYTES_READ will be set to the length of
8895 the initial number. */
8896
8897static struct abbrev_info *
685af9cd
TT
8898peek_die_abbrev (const die_reader_specs &reader,
8899 const gdb_byte *info_ptr, unsigned int *bytes_read)
4bb7a0a7 8900{
685af9cd 8901 dwarf2_cu *cu = reader.cu;
5e22e966 8902 bfd *abfd = cu->per_objfile->objfile->obfd;
685af9cd
TT
8903 unsigned int abbrev_number
8904 = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4bb7a0a7
DJ
8905
8906 if (abbrev_number == 0)
8907 return NULL;
8908
685af9cd 8909 abbrev_info *abbrev = reader.abbrev_table->lookup_abbrev (abbrev_number);
4bb7a0a7
DJ
8910 if (!abbrev)
8911 {
422b9917 8912 error (_("Dwarf Error: Could not find abbrev number %d in %s"
9d8780f0 8913 " at offset %s [in module %s]"),
422b9917 8914 abbrev_number, cu->per_cu->is_debug_types ? "TU" : "CU",
9d8780f0 8915 sect_offset_str (cu->header.sect_off), bfd_get_filename (abfd));
4bb7a0a7
DJ
8916 }
8917
8918 return abbrev;
8919}
8920
93311388
DE
8921/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8922 Returns a pointer to the end of a series of DIEs, terminated by an empty
4bb7a0a7
DJ
8923 DIE. Any children of the skipped DIEs will also be skipped. */
8924
d521ce57
TT
8925static const gdb_byte *
8926skip_children (const struct die_reader_specs *reader, const gdb_byte *info_ptr)
4bb7a0a7 8927{
4bb7a0a7
DJ
8928 while (1)
8929 {
685af9cd
TT
8930 unsigned int bytes_read;
8931 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
8932
4bb7a0a7
DJ
8933 if (abbrev == NULL)
8934 return info_ptr + bytes_read;
8935 else
dee91e82 8936 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
4bb7a0a7
DJ
8937 }
8938}
8939
93311388
DE
8940/* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
8941 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4bb7a0a7
DJ
8942 abbrev corresponding to that skipped uleb128 should be passed in
8943 ABBREV. Returns a pointer to this DIE's sibling, skipping any
8944 children. */
8945
d521ce57
TT
8946static const gdb_byte *
8947skip_one_die (const struct die_reader_specs *reader, const gdb_byte *info_ptr,
dee91e82 8948 struct abbrev_info *abbrev)
4bb7a0a7
DJ
8949{
8950 unsigned int bytes_read;
8951 struct attribute attr;
dee91e82
DE
8952 bfd *abfd = reader->abfd;
8953 struct dwarf2_cu *cu = reader->cu;
d521ce57 8954 const gdb_byte *buffer = reader->buffer;
f664829e 8955 const gdb_byte *buffer_end = reader->buffer_end;
4bb7a0a7
DJ
8956 unsigned int form, i;
8957
8958 for (i = 0; i < abbrev->num_attrs; i++)
8959 {
8960 /* The only abbrev we care about is DW_AT_sibling. */
8961 if (abbrev->attrs[i].name == DW_AT_sibling)
8962 {
7a5f294d 8963 read_attribute (reader, &attr, &abbrev->attrs[i], info_ptr);
4bb7a0a7 8964 if (attr.form == DW_FORM_ref_addr)
b98664d3 8965 complaint (_("ignoring absolute DW_AT_sibling"));
4bb7a0a7 8966 else
b9502d3f 8967 {
0826b30a 8968 sect_offset off = attr.get_ref_die_offset ();
9c541725 8969 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
8970
8971 if (sibling_ptr < info_ptr)
b98664d3 8972 complaint (_("DW_AT_sibling points backwards"));
22869d73 8973 else if (sibling_ptr > reader->buffer_end)
a0194fa8 8974 reader->die_section->overflow_complaint ();
b9502d3f
WN
8975 else
8976 return sibling_ptr;
8977 }
4bb7a0a7
DJ
8978 }
8979
8980 /* If it isn't DW_AT_sibling, skip this attribute. */
8981 form = abbrev->attrs[i].form;
8982 skip_attribute:
8983 switch (form)
8984 {
4bb7a0a7 8985 case DW_FORM_ref_addr:
ae411497
TT
8986 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
8987 and later it is offset sized. */
8988 if (cu->header.version == 2)
8989 info_ptr += cu->header.addr_size;
8990 else
8991 info_ptr += cu->header.offset_size;
8992 break;
36586728
TT
8993 case DW_FORM_GNU_ref_alt:
8994 info_ptr += cu->header.offset_size;
8995 break;
ae411497 8996 case DW_FORM_addr:
4bb7a0a7
DJ
8997 info_ptr += cu->header.addr_size;
8998 break;
8999 case DW_FORM_data1:
9000 case DW_FORM_ref1:
9001 case DW_FORM_flag:
8fe0f950 9002 case DW_FORM_strx1:
4bb7a0a7
DJ
9003 info_ptr += 1;
9004 break;
2dc7f7b3 9005 case DW_FORM_flag_present:
43988095 9006 case DW_FORM_implicit_const:
2dc7f7b3 9007 break;
4bb7a0a7
DJ
9008 case DW_FORM_data2:
9009 case DW_FORM_ref2:
8fe0f950 9010 case DW_FORM_strx2:
4bb7a0a7
DJ
9011 info_ptr += 2;
9012 break;
8fe0f950
AT
9013 case DW_FORM_strx3:
9014 info_ptr += 3;
9015 break;
4bb7a0a7
DJ
9016 case DW_FORM_data4:
9017 case DW_FORM_ref4:
8fe0f950 9018 case DW_FORM_strx4:
4bb7a0a7
DJ
9019 info_ptr += 4;
9020 break;
9021 case DW_FORM_data8:
9022 case DW_FORM_ref8:
55f1336d 9023 case DW_FORM_ref_sig8:
4bb7a0a7
DJ
9024 info_ptr += 8;
9025 break;
0224619f
JK
9026 case DW_FORM_data16:
9027 info_ptr += 16;
9028 break;
4bb7a0a7 9029 case DW_FORM_string:
9b1c24c8 9030 read_direct_string (abfd, info_ptr, &bytes_read);
4bb7a0a7
DJ
9031 info_ptr += bytes_read;
9032 break;
2dc7f7b3 9033 case DW_FORM_sec_offset:
4bb7a0a7 9034 case DW_FORM_strp:
36586728 9035 case DW_FORM_GNU_strp_alt:
4bb7a0a7
DJ
9036 info_ptr += cu->header.offset_size;
9037 break;
2dc7f7b3 9038 case DW_FORM_exprloc:
4bb7a0a7
DJ
9039 case DW_FORM_block:
9040 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9041 info_ptr += bytes_read;
9042 break;
9043 case DW_FORM_block1:
9044 info_ptr += 1 + read_1_byte (abfd, info_ptr);
9045 break;
9046 case DW_FORM_block2:
9047 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
9048 break;
9049 case DW_FORM_block4:
9050 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
9051 break;
336d760d 9052 case DW_FORM_addrx:
cf532bd1 9053 case DW_FORM_strx:
4bb7a0a7
DJ
9054 case DW_FORM_sdata:
9055 case DW_FORM_udata:
9056 case DW_FORM_ref_udata:
3019eac3
DE
9057 case DW_FORM_GNU_addr_index:
9058 case DW_FORM_GNU_str_index:
18a8505e 9059 case DW_FORM_rnglistx:
41144253 9060 case DW_FORM_loclistx:
d521ce57 9061 info_ptr = safe_skip_leb128 (info_ptr, buffer_end);
4bb7a0a7
DJ
9062 break;
9063 case DW_FORM_indirect:
9064 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9065 info_ptr += bytes_read;
9066 /* We need to continue parsing from here, so just go back to
9067 the top. */
9068 goto skip_attribute;
9069
9070 default:
3e43a32a
MS
9071 error (_("Dwarf Error: Cannot handle %s "
9072 "in DWARF reader [in module %s]"),
4bb7a0a7
DJ
9073 dwarf_form_name (form),
9074 bfd_get_filename (abfd));
9075 }
9076 }
9077
9078 if (abbrev->has_children)
dee91e82 9079 return skip_children (reader, info_ptr);
4bb7a0a7
DJ
9080 else
9081 return info_ptr;
9082}
9083
93311388 9084/* Locate ORIG_PDI's sibling.
dee91e82 9085 INFO_PTR should point to the start of the next DIE after ORIG_PDI. */
91c24f0a 9086
d521ce57 9087static const gdb_byte *
dee91e82
DE
9088locate_pdi_sibling (const struct die_reader_specs *reader,
9089 struct partial_die_info *orig_pdi,
d521ce57 9090 const gdb_byte *info_ptr)
91c24f0a
DC
9091{
9092 /* Do we know the sibling already? */
72bf9492 9093
91c24f0a
DC
9094 if (orig_pdi->sibling)
9095 return orig_pdi->sibling;
9096
9097 /* Are there any children to deal with? */
9098
9099 if (!orig_pdi->has_children)
9100 return info_ptr;
9101
4bb7a0a7 9102 /* Skip the children the long way. */
91c24f0a 9103
dee91e82 9104 return skip_children (reader, info_ptr);
91c24f0a
DC
9105}
9106
257e7a09 9107/* Expand this partial symbol table into a full symbol table. SELF is
442e4d9c 9108 not NULL. */
c906108c 9109
891813be
TT
9110void
9111dwarf2_psymtab::read_symtab (struct objfile *objfile)
c906108c 9112{
976ca316 9113 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
ed2dc618 9114
976ca316 9115 gdb_assert (!per_objfile->symtab_set_p (per_cu_data));
af758d11 9116
077cbab2
TT
9117 /* If this psymtab is constructed from a debug-only objfile, the
9118 has_section_at_zero flag will not necessarily be correct. We
9119 can get the correct value for this flag by looking at the data
9120 associated with the (presumably stripped) associated objfile. */
9121 if (objfile->separate_debug_objfile_backlink)
c906108c 9122 {
976ca316 9123 dwarf2_per_objfile *per_objfile_backlink
077cbab2 9124 = get_dwarf2_per_objfile (objfile->separate_debug_objfile_backlink);
c906108c 9125
976ca316
SM
9126 per_objfile->per_bfd->has_section_at_zero
9127 = per_objfile_backlink->per_bfd->has_section_at_zero;
077cbab2 9128 }
98bfdba5 9129
8566b89b 9130 expand_psymtab (objfile);
95554aad 9131
976ca316 9132 process_cu_includes (per_objfile);
c906108c 9133}
9cdd5dbd
DE
9134\f
9135/* Reading in full CUs. */
c906108c 9136
10b3939b
DJ
9137/* Add PER_CU to the queue. */
9138
9139static void
120ce1b5
SM
9140queue_comp_unit (dwarf2_per_cu_data *per_cu,
9141 dwarf2_per_objfile *per_objfile,
95554aad 9142 enum language pretend_language)
10b3939b 9143{
10b3939b 9144 per_cu->queued = 1;
120ce1b5 9145 per_cu->per_bfd->queue.emplace (per_cu, per_objfile, pretend_language);
10b3939b
DJ
9146}
9147
89e63ee4
DE
9148/* If PER_CU is not yet queued, add it to the queue.
9149 If DEPENDENT_CU is non-NULL, it has a reference to PER_CU so add a
9150 dependency.
0907af0c 9151 The result is non-zero if PER_CU was queued, otherwise the result is zero
69d751e3
DE
9152 meaning either PER_CU is already queued or it is already loaded.
9153
9154 N.B. There is an invariant here that if a CU is queued then it is loaded.
9155 The caller is required to load PER_CU if we return non-zero. */
0907af0c
DE
9156
9157static int
89e63ee4 9158maybe_queue_comp_unit (struct dwarf2_cu *dependent_cu,
120ce1b5
SM
9159 dwarf2_per_cu_data *per_cu,
9160 dwarf2_per_objfile *per_objfile,
0907af0c
DE
9161 enum language pretend_language)
9162{
9163 /* We may arrive here during partial symbol reading, if we need full
9164 DIEs to process an unusual case (e.g. template arguments). Do
9165 not queue PER_CU, just tell our caller to load its DIEs. */
1859c670 9166 if (per_cu->per_bfd->reading_partial_symbols)
0907af0c 9167 {
7188ed02
SM
9168 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9169
9170 if (cu == NULL || cu->dies == NULL)
0907af0c
DE
9171 return 1;
9172 return 0;
9173 }
9174
9175 /* Mark the dependence relation so that we don't flush PER_CU
9176 too early. */
89e63ee4
DE
9177 if (dependent_cu != NULL)
9178 dwarf2_add_dependence (dependent_cu, per_cu);
0907af0c
DE
9179
9180 /* If it's already on the queue, we have nothing to do. */
9181 if (per_cu->queued)
de53369b
SM
9182 {
9183 /* Verify the invariant that if a CU is queued for expansion, its DIEs are
9184 loaded. */
9185 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
9186 return 0;
9187 }
0907af0c
DE
9188
9189 /* If the compilation unit is already loaded, just mark it as
9190 used. */
7188ed02
SM
9191 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
9192 if (cu != nullptr)
0907af0c 9193 {
7188ed02 9194 cu->last_used = 0;
0907af0c
DE
9195 return 0;
9196 }
9197
9198 /* Add it to the queue. */
120ce1b5 9199 queue_comp_unit (per_cu, per_objfile, pretend_language);
0907af0c
DE
9200
9201 return 1;
9202}
9203
10b3939b
DJ
9204/* Process the queue. */
9205
9206static void
976ca316 9207process_queue (dwarf2_per_objfile *per_objfile)
10b3939b 9208{
6f738b01
SM
9209 dwarf_read_debug_printf ("Expanding one or more symtabs of objfile %s ...",
9210 objfile_name (per_objfile->objfile));
45cfd468 9211
03dd20cc
DJ
9212 /* The queue starts out with one item, but following a DIE reference
9213 may load a new CU, adding it to the end of the queue. */
976ca316 9214 while (!per_objfile->per_bfd->queue.empty ())
10b3939b 9215 {
976ca316 9216 dwarf2_queue_item &item = per_objfile->per_bfd->queue.front ();
7188ed02 9217 dwarf2_per_cu_data *per_cu = item.per_cu;
39856def 9218
976ca316 9219 if (!per_objfile->symtab_set_p (per_cu))
f4dc4d17 9220 {
976ca316 9221 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
f4dc4d17 9222
7188ed02
SM
9223 /* Skip dummy CUs. */
9224 if (cu != nullptr)
73be47f5 9225 {
7188ed02
SM
9226 unsigned int debug_print_threshold;
9227 char buf[100];
9228
9229 if (per_cu->is_debug_types)
9230 {
9231 struct signatured_type *sig_type =
9232 (struct signatured_type *) per_cu;
9233
9234 sprintf (buf, "TU %s at offset %s",
9235 hex_string (sig_type->signature),
9236 sect_offset_str (per_cu->sect_off));
9237 /* There can be 100s of TUs.
9238 Only print them in verbose mode. */
9239 debug_print_threshold = 2;
9240 }
9241 else
9242 {
9243 sprintf (buf, "CU at offset %s",
9244 sect_offset_str (per_cu->sect_off));
9245 debug_print_threshold = 1;
9246 }
247f5c4f 9247
7188ed02 9248 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9249 dwarf_read_debug_printf ("Expanding symtab of %s", buf);
f4dc4d17 9250
7188ed02
SM
9251 if (per_cu->is_debug_types)
9252 process_full_type_unit (cu, item.pretend_language);
9253 else
9254 process_full_comp_unit (cu, item.pretend_language);
f4dc4d17 9255
7188ed02 9256 if (dwarf_read_debug >= debug_print_threshold)
6f738b01 9257 dwarf_read_debug_printf ("Done expanding %s", buf);
7188ed02 9258 }
f4dc4d17 9259 }
10b3939b 9260
7188ed02 9261 per_cu->queued = 0;
976ca316 9262 per_objfile->per_bfd->queue.pop ();
10b3939b
DJ
9263 }
9264
6f738b01
SM
9265 dwarf_read_debug_printf ("Done expanding symtabs of %s.",
9266 objfile_name (per_objfile->objfile));
10b3939b
DJ
9267}
9268
10b3939b
DJ
9269/* Read in full symbols for PST, and anything it depends on. */
9270
8566b89b
TT
9271void
9272dwarf2_psymtab::expand_psymtab (struct objfile *objfile)
c906108c 9273{
af758d11 9274 gdb_assert (!readin_p (objfile));
95554aad 9275
17ee85fc
TT
9276 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9277 free_cached_comp_units freer (per_objfile);
48993951 9278 expand_dependencies (objfile);
aaa75496 9279
97a1449a 9280 dw2_do_instantiate_symtab (per_cu_data, per_objfile, false);
5717c425 9281 gdb_assert (get_compunit_symtab (objfile) != nullptr);
10b3939b
DJ
9282}
9283
af758d11
SM
9284/* See psympriv.h. */
9285
9286bool
9287dwarf2_psymtab::readin_p (struct objfile *objfile) const
9288{
9289 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9290 return per_objfile->symtab_set_p (per_cu_data);
9291}
9292
9293/* See psympriv.h. */
9294
9295compunit_symtab *
9296dwarf2_psymtab::get_compunit_symtab (struct objfile *objfile) const
9297{
9298 dwarf2_per_objfile *per_objfile = get_dwarf2_per_objfile (objfile);
9299 return per_objfile->get_symtab (per_cu_data);
9300}
9301
dee91e82
DE
9302/* Trivial hash function for die_info: the hash value of a DIE
9303 is its offset in .debug_info for this objfile. */
10b3939b 9304
dee91e82
DE
9305static hashval_t
9306die_hash (const void *item)
10b3939b 9307{
9a3c8263 9308 const struct die_info *die = (const struct die_info *) item;
6502dd73 9309
9c541725 9310 return to_underlying (die->sect_off);
dee91e82 9311}
63d06c5c 9312
dee91e82
DE
9313/* Trivial comparison function for die_info structures: two DIEs
9314 are equal if they have the same offset. */
98bfdba5 9315
dee91e82
DE
9316static int
9317die_eq (const void *item_lhs, const void *item_rhs)
9318{
9a3c8263
SM
9319 const struct die_info *die_lhs = (const struct die_info *) item_lhs;
9320 const struct die_info *die_rhs = (const struct die_info *) item_rhs;
c906108c 9321
9c541725 9322 return die_lhs->sect_off == die_rhs->sect_off;
dee91e82 9323}
c906108c 9324
4a636814
SM
9325/* Load the DIEs associated with PER_CU into memory.
9326
9327 In some cases, the caller, while reading partial symbols, will need to load
9328 the full symbols for the CU for some reason. It will already have a
9329 dwarf2_cu object for THIS_CU and pass it as EXISTING_CU, so it can be re-used
9330 rather than creating a new one. */
c906108c 9331
dee91e82 9332static void
ab432490
SM
9333load_full_comp_unit (dwarf2_per_cu_data *this_cu,
9334 dwarf2_per_objfile *per_objfile,
4a636814 9335 dwarf2_cu *existing_cu,
c0ab21c2
TT
9336 bool skip_partial,
9337 enum language pretend_language)
dee91e82 9338{
c0ab21c2
TT
9339 gdb_assert (! this_cu->is_debug_types);
9340
7188ed02 9341 cutu_reader reader (this_cu, per_objfile, NULL, existing_cu, skip_partial);
c0ab21c2
TT
9342 if (reader.dummy_p)
9343 return;
9344
9345 struct dwarf2_cu *cu = reader.cu;
9346 const gdb_byte *info_ptr = reader.info_ptr;
6caca83c 9347
dee91e82
DE
9348 gdb_assert (cu->die_hash == NULL);
9349 cu->die_hash =
9350 htab_create_alloc_ex (cu->header.length / 12,
9351 die_hash,
9352 die_eq,
9353 NULL,
9354 &cu->comp_unit_obstack,
9355 hashtab_obstack_allocate,
9356 dummy_obstack_deallocate);
e142c38c 9357
3e225074 9358 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
9359 reader.comp_unit_die->child
9360 = read_die_and_siblings (&reader, reader.info_ptr,
9361 &info_ptr, reader.comp_unit_die);
9362 cu->dies = reader.comp_unit_die;
dee91e82 9363 /* comp_unit_die is not stored in die_hash, no need. */
10b3939b
DJ
9364
9365 /* We try not to read any attributes in this function, because not
9cdd5dbd 9366 all CUs needed for references have been loaded yet, and symbol
10b3939b 9367 table processing isn't initialized. But we have to set the CU language,
dee91e82
DE
9368 or we won't be able to build types correctly.
9369 Similarly, if we do not read the producer, we can not apply
9370 producer-specific interpretation. */
c0ab21c2 9371 prepare_one_comp_unit (cu, cu->dies, pretend_language);
6751ebae
TT
9372
9373 reader.keep ();
10b3939b
DJ
9374}
9375
3da10d80
KS
9376/* Add a DIE to the delayed physname list. */
9377
9378static void
9379add_to_method_list (struct type *type, int fnfield_index, int index,
9380 const char *name, struct die_info *die,
9381 struct dwarf2_cu *cu)
9382{
9383 struct delayed_method_info mi;
9384 mi.type = type;
9385 mi.fnfield_index = fnfield_index;
9386 mi.index = index;
9387 mi.name = name;
9388 mi.die = die;
c89b44cd 9389 cu->method_list.push_back (mi);
3da10d80
KS
9390}
9391
3693fdb3
PA
9392/* Check whether [PHYSNAME, PHYSNAME+LEN) ends with a modifier like
9393 "const" / "volatile". If so, decrements LEN by the length of the
9394 modifier and return true. Otherwise return false. */
9395
9396template<size_t N>
9397static bool
9398check_modifier (const char *physname, size_t &len, const char (&mod)[N])
9399{
9400 size_t mod_len = sizeof (mod) - 1;
9401 if (len > mod_len && startswith (physname + (len - mod_len), mod))
9402 {
9403 len -= mod_len;
9404 return true;
9405 }
9406 return false;
9407}
9408
3da10d80
KS
9409/* Compute the physnames of any methods on the CU's method list.
9410
9411 The computation of method physnames is delayed in order to avoid the
9412 (bad) condition that one of the method's formal parameters is of an as yet
9413 incomplete type. */
9414
9415static void
9416compute_delayed_physnames (struct dwarf2_cu *cu)
9417{
3693fdb3 9418 /* Only C++ delays computing physnames. */
c89b44cd 9419 if (cu->method_list.empty ())
3693fdb3
PA
9420 return;
9421 gdb_assert (cu->language == language_cplus);
9422
52941706 9423 for (const delayed_method_info &mi : cu->method_list)
3da10d80 9424 {
1d06ead6 9425 const char *physname;
3da10d80 9426 struct fn_fieldlist *fn_flp
c89b44cd
TT
9427 = &TYPE_FN_FIELDLIST (mi.type, mi.fnfield_index);
9428 physname = dwarf2_physname (mi.name, mi.die, cu);
9429 TYPE_FN_FIELD_PHYSNAME (fn_flp->fn_fields, mi.index)
005e54bb 9430 = physname ? physname : "";
3693fdb3
PA
9431
9432 /* Since there's no tag to indicate whether a method is a
9433 const/volatile overload, extract that information out of the
9434 demangled name. */
9435 if (physname != NULL)
9436 {
9437 size_t len = strlen (physname);
9438
9439 while (1)
9440 {
9441 if (physname[len] == ')') /* shortcut */
9442 break;
9443 else if (check_modifier (physname, len, " const"))
c89b44cd 9444 TYPE_FN_FIELD_CONST (fn_flp->fn_fields, mi.index) = 1;
3693fdb3 9445 else if (check_modifier (physname, len, " volatile"))
c89b44cd 9446 TYPE_FN_FIELD_VOLATILE (fn_flp->fn_fields, mi.index) = 1;
3693fdb3
PA
9447 else
9448 break;
9449 }
9450 }
3da10d80 9451 }
c89b44cd
TT
9452
9453 /* The list is no longer needed. */
9454 cu->method_list.clear ();
3da10d80
KS
9455}
9456
a766d390
DE
9457/* Go objects should be embedded in a DW_TAG_module DIE,
9458 and it's not clear if/how imported objects will appear.
9459 To keep Go support simple until that's worked out,
9460 go back through what we've read and create something usable.
9461 We could do this while processing each DIE, and feels kinda cleaner,
9462 but that way is more invasive.
9463 This is to, for example, allow the user to type "p var" or "b main"
9464 without having to specify the package name, and allow lookups
9465 of module.object to work in contexts that use the expression
9466 parser. */
9467
9468static void
9469fixup_go_packaging (struct dwarf2_cu *cu)
9470{
421d1616 9471 gdb::unique_xmalloc_ptr<char> package_name;
a766d390
DE
9472 struct pending *list;
9473 int i;
9474
c24bdb02 9475 for (list = *cu->get_builder ()->get_global_symbols ();
804d2729
TT
9476 list != NULL;
9477 list = list->next)
a766d390
DE
9478 {
9479 for (i = 0; i < list->nsyms; ++i)
9480 {
9481 struct symbol *sym = list->symbol[i];
9482
c1b5c1eb 9483 if (sym->language () == language_go
a766d390
DE
9484 && SYMBOL_CLASS (sym) == LOC_BLOCK)
9485 {
421d1616
TT
9486 gdb::unique_xmalloc_ptr<char> this_package_name
9487 (go_symbol_package_name (sym));
a766d390
DE
9488
9489 if (this_package_name == NULL)
9490 continue;
9491 if (package_name == NULL)
421d1616 9492 package_name = std::move (this_package_name);
a766d390
DE
9493 else
9494 {
5e22e966 9495 struct objfile *objfile = cu->per_objfile->objfile;
421d1616 9496 if (strcmp (package_name.get (), this_package_name.get ()) != 0)
b98664d3 9497 complaint (_("Symtab %s has objects from two different Go packages: %s and %s"),
08be3fe3
DE
9498 (symbol_symtab (sym) != NULL
9499 ? symtab_to_filename_for_display
9500 (symbol_symtab (sym))
e3b94546 9501 : objfile_name (objfile)),
421d1616 9502 this_package_name.get (), package_name.get ());
a766d390
DE
9503 }
9504 }
9505 }
9506 }
9507
9508 if (package_name != NULL)
9509 {
5e22e966 9510 struct objfile *objfile = cu->per_objfile->objfile;
be1e3d3e 9511 const char *saved_package_name = objfile->intern (package_name.get ());
19f392bc
UW
9512 struct type *type = init_type (objfile, TYPE_CODE_MODULE, 0,
9513 saved_package_name);
a766d390
DE
9514 struct symbol *sym;
9515
8c14c3a3 9516 sym = new (&objfile->objfile_obstack) symbol;
d3ecddab 9517 sym->set_language (language_go, &objfile->objfile_obstack);
4d4eaa30 9518 sym->compute_and_set_names (saved_package_name, false, objfile->per_bfd);
a766d390
DE
9519 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
9520 e.g., "main" finds the "main" module and not C's main(). */
9521 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
f1e6e072 9522 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
a766d390
DE
9523 SYMBOL_TYPE (sym) = type;
9524
c24bdb02 9525 add_symbol_to_list (sym, cu->get_builder ()->get_global_symbols ());
a766d390
DE
9526 }
9527}
9528
c9317f21
TT
9529/* Allocate a fully-qualified name consisting of the two parts on the
9530 obstack. */
9531
9532static const char *
9533rust_fully_qualify (struct obstack *obstack, const char *p1, const char *p2)
9534{
9535 return obconcat (obstack, p1, "::", p2, (char *) NULL);
9536}
9537
9c6a1327
TT
9538/* A helper that allocates a variant part to attach to a Rust enum
9539 type. OBSTACK is where the results should be allocated. TYPE is
9540 the type we're processing. DISCRIMINANT_INDEX is the index of the
57d02173
TT
9541 discriminant. It must be the index of one of the fields of TYPE,
9542 or -1 to mean there is no discriminant (univariant enum).
9c6a1327
TT
9543 DEFAULT_INDEX is the index of the default field; or -1 if there is
9544 no default. RANGES is indexed by "effective" field number (the
9545 field index, but omitting the discriminant and default fields) and
9546 must hold the discriminant values used by the variants. Note that
9547 RANGES must have a lifetime at least as long as OBSTACK -- either
9548 already allocated on it, or static. */
c9317f21 9549
9c6a1327
TT
9550static void
9551alloc_rust_variant (struct obstack *obstack, struct type *type,
9552 int discriminant_index, int default_index,
9553 gdb::array_view<discriminant_range> ranges)
9554{
57d02173
TT
9555 /* When DISCRIMINANT_INDEX == -1, we have a univariant enum. */
9556 gdb_assert (discriminant_index == -1
9557 || (discriminant_index >= 0
9558 && discriminant_index < type->num_fields ()));
c9317f21 9559 gdb_assert (default_index == -1
1f704f76 9560 || (default_index >= 0 && default_index < type->num_fields ()));
c9317f21 9561
9c6a1327 9562 /* We have one variant for each non-discriminant field. */
57d02173
TT
9563 int n_variants = type->num_fields ();
9564 if (discriminant_index != -1)
9565 --n_variants;
c9317f21 9566
9c6a1327
TT
9567 variant *variants = new (obstack) variant[n_variants];
9568 int var_idx = 0;
9569 int range_idx = 0;
1f704f76 9570 for (int i = 0; i < type->num_fields (); ++i)
9c6a1327
TT
9571 {
9572 if (i == discriminant_index)
9573 continue;
c9317f21 9574
9c6a1327
TT
9575 variants[var_idx].first_field = i;
9576 variants[var_idx].last_field = i + 1;
9577
9578 /* The default field does not need a range, but other fields do.
9579 We skipped the discriminant above. */
9580 if (i != default_index)
9581 {
9582 variants[var_idx].discriminants = ranges.slice (range_idx, 1);
9583 ++range_idx;
9584 }
c9317f21 9585
9c6a1327
TT
9586 ++var_idx;
9587 }
9588
9589 gdb_assert (range_idx == ranges.size ());
9590 gdb_assert (var_idx == n_variants);
9591
9592 variant_part *part = new (obstack) variant_part;
9593 part->discriminant_index = discriminant_index;
57d02173
TT
9594 /* If there is no discriminant, then whether it is signed is of no
9595 consequence. */
9596 part->is_unsigned
9597 = (discriminant_index == -1
9598 ? false
c6d940a9 9599 : type->field (discriminant_index).type ()->is_unsigned ());
9c6a1327
TT
9600 part->variants = gdb::array_view<variant> (variants, n_variants);
9601
9602 void *storage = obstack_alloc (obstack, sizeof (gdb::array_view<variant_part>));
9603 gdb::array_view<variant_part> *prop_value
9604 = new (storage) gdb::array_view<variant_part> (part, 1);
c9317f21 9605
9c6a1327 9606 struct dynamic_prop prop;
8c2e4e06 9607 prop.set_variant_parts (prop_value);
9c6a1327 9608
5c54719c 9609 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
c9317f21
TT
9610}
9611
9612/* Some versions of rustc emitted enums in an unusual way.
9613
9614 Ordinary enums were emitted as unions. The first element of each
9615 structure in the union was named "RUST$ENUM$DISR". This element
9616 held the discriminant.
9617
9618 These versions of Rust also implemented the "non-zero"
9619 optimization. When the enum had two values, and one is empty and
9620 the other holds a pointer that cannot be zero, the pointer is used
9621 as the discriminant, with a zero value meaning the empty variant.
9622 Here, the union's first member is of the form
9623 RUST$ENCODED$ENUM$<fieldno>$<fieldno>$...$<variantname>
9624 where the fieldnos are the indices of the fields that should be
9625 traversed in order to find the field (which may be several fields deep)
9626 and the variantname is the name of the variant of the case when the
9627 field is zero.
9628
9629 This function recognizes whether TYPE is of one of these forms,
9630 and, if so, smashes it to be a variant type. */
9631
9632static void
9633quirk_rust_enum (struct type *type, struct objfile *objfile)
9634{
78134374 9635 gdb_assert (type->code () == TYPE_CODE_UNION);
c9317f21
TT
9636
9637 /* We don't need to deal with empty enums. */
1f704f76 9638 if (type->num_fields () == 0)
c9317f21
TT
9639 return;
9640
9641#define RUST_ENUM_PREFIX "RUST$ENCODED$ENUM$"
1f704f76 9642 if (type->num_fields () == 1
c9317f21
TT
9643 && startswith (TYPE_FIELD_NAME (type, 0), RUST_ENUM_PREFIX))
9644 {
9645 const char *name = TYPE_FIELD_NAME (type, 0) + strlen (RUST_ENUM_PREFIX);
9646
9647 /* Decode the field name to find the offset of the
9648 discriminant. */
9649 ULONGEST bit_offset = 0;
940da03e 9650 struct type *field_type = type->field (0).type ();
c9317f21
TT
9651 while (name[0] >= '0' && name[0] <= '9')
9652 {
9653 char *tail;
9654 unsigned long index = strtoul (name, &tail, 10);
9655 name = tail;
9656 if (*name != '$'
1f704f76 9657 || index >= field_type->num_fields ()
c9317f21
TT
9658 || (TYPE_FIELD_LOC_KIND (field_type, index)
9659 != FIELD_LOC_KIND_BITPOS))
9660 {
b98664d3 9661 complaint (_("Could not parse Rust enum encoding string \"%s\""
c9317f21
TT
9662 "[in module %s]"),
9663 TYPE_FIELD_NAME (type, 0),
9664 objfile_name (objfile));
9665 return;
9666 }
9667 ++name;
9668
9669 bit_offset += TYPE_FIELD_BITPOS (field_type, index);
940da03e 9670 field_type = field_type->field (index).type ();
c9317f21
TT
9671 }
9672
9c6a1327
TT
9673 /* Smash this type to be a structure type. We have to do this
9674 because the type has already been recorded. */
67607e24 9675 type->set_code (TYPE_CODE_STRUCT);
5e33d5f4 9676 type->set_num_fields (3);
9c6a1327 9677 /* Save the field we care about. */
ceacbf6e 9678 struct field saved_field = type->field (0);
3cabb6b0
SM
9679 type->set_fields
9680 ((struct field *) TYPE_ZALLOC (type, 3 * sizeof (struct field)));
c9317f21 9681
9c6a1327 9682 /* Put the discriminant at index 0. */
5d14b6e5 9683 type->field (0).set_type (field_type);
9c6a1327
TT
9684 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9685 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
ceacbf6e 9686 SET_FIELD_BITPOS (type->field (0), bit_offset);
c9317f21
TT
9687
9688 /* The order of fields doesn't really matter, so put the real
9689 field at index 1 and the data-less field at index 2. */
ceacbf6e 9690 type->field (1) = saved_field;
9c6a1327 9691 TYPE_FIELD_NAME (type, 1)
940da03e
SM
9692 = rust_last_path_segment (type->field (1).type ()->name ());
9693 type->field (1).type ()->set_name
7d93a1e0 9694 (rust_fully_qualify (&objfile->objfile_obstack, type->name (),
d0e39ea2 9695 TYPE_FIELD_NAME (type, 1)));
c9317f21
TT
9696
9697 const char *dataless_name
7d93a1e0 9698 = rust_fully_qualify (&objfile->objfile_obstack, type->name (),
c9317f21
TT
9699 name);
9700 struct type *dataless_type = init_type (objfile, TYPE_CODE_VOID, 0,
9701 dataless_name);
5d14b6e5 9702 type->field (2).set_type (dataless_type);
c9317f21
TT
9703 /* NAME points into the original discriminant name, which
9704 already has the correct lifetime. */
9c6a1327 9705 TYPE_FIELD_NAME (type, 2) = name;
ceacbf6e 9706 SET_FIELD_BITPOS (type->field (2), 0);
c9317f21 9707
9c6a1327
TT
9708 /* Indicate that this is a variant type. */
9709 static discriminant_range ranges[1] = { { 0, 0 } };
9710 alloc_rust_variant (&objfile->objfile_obstack, type, 0, 1, ranges);
c9317f21 9711 }
77c2dba3
TT
9712 /* A union with a single anonymous field is probably an old-style
9713 univariant enum. */
1f704f76 9714 else if (type->num_fields () == 1 && streq (TYPE_FIELD_NAME (type, 0), ""))
c9317f21 9715 {
c9317f21
TT
9716 /* Smash this type to be a structure type. We have to do this
9717 because the type has already been recorded. */
67607e24 9718 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9719
940da03e 9720 struct type *field_type = type->field (0).type ();
c9317f21 9721 const char *variant_name
7d93a1e0 9722 = rust_last_path_segment (field_type->name ());
9c6a1327 9723 TYPE_FIELD_NAME (type, 0) = variant_name;
d0e39ea2
SM
9724 field_type->set_name
9725 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9726 type->name (), variant_name));
57d02173
TT
9727
9728 alloc_rust_variant (&objfile->objfile_obstack, type, -1, 0, {});
c9317f21
TT
9729 }
9730 else
9731 {
9732 struct type *disr_type = nullptr;
1f704f76 9733 for (int i = 0; i < type->num_fields (); ++i)
c9317f21 9734 {
940da03e 9735 disr_type = type->field (i).type ();
c9317f21 9736
78134374 9737 if (disr_type->code () != TYPE_CODE_STRUCT)
a037790e
TT
9738 {
9739 /* All fields of a true enum will be structs. */
9740 return;
9741 }
1f704f76 9742 else if (disr_type->num_fields () == 0)
c9317f21
TT
9743 {
9744 /* Could be data-less variant, so keep going. */
a037790e 9745 disr_type = nullptr;
c9317f21
TT
9746 }
9747 else if (strcmp (TYPE_FIELD_NAME (disr_type, 0),
9748 "RUST$ENUM$DISR") != 0)
9749 {
9750 /* Not a Rust enum. */
9751 return;
9752 }
9753 else
9754 {
9755 /* Found one. */
9756 break;
9757 }
9758 }
9759
9760 /* If we got here without a discriminant, then it's probably
9761 just a union. */
9762 if (disr_type == nullptr)
9763 return;
9764
9765 /* Smash this type to be a structure type. We have to do this
9766 because the type has already been recorded. */
67607e24 9767 type->set_code (TYPE_CODE_STRUCT);
c9317f21 9768
9c6a1327 9769 /* Make space for the discriminant field. */
ceacbf6e 9770 struct field *disr_field = &disr_type->field (0);
9c6a1327 9771 field *new_fields
1f704f76 9772 = (struct field *) TYPE_ZALLOC (type, ((type->num_fields () + 1)
9c6a1327 9773 * sizeof (struct field)));
80fc5e77 9774 memcpy (new_fields + 1, type->fields (),
1f704f76 9775 type->num_fields () * sizeof (struct field));
3cabb6b0 9776 type->set_fields (new_fields);
1f704f76 9777 type->set_num_fields (type->num_fields () + 1);
c9317f21
TT
9778
9779 /* Install the discriminant at index 0 in the union. */
ceacbf6e 9780 type->field (0) = *disr_field;
9c6a1327
TT
9781 TYPE_FIELD_ARTIFICIAL (type, 0) = 1;
9782 TYPE_FIELD_NAME (type, 0) = "<<discriminant>>";
c9317f21
TT
9783
9784 /* We need a way to find the correct discriminant given a
9785 variant name. For convenience we build a map here. */
b6cdac4b 9786 struct type *enum_type = disr_field->type ();
c9317f21 9787 std::unordered_map<std::string, ULONGEST> discriminant_map;
1f704f76 9788 for (int i = 0; i < enum_type->num_fields (); ++i)
c9317f21
TT
9789 {
9790 if (TYPE_FIELD_LOC_KIND (enum_type, i) == FIELD_LOC_KIND_ENUMVAL)
9791 {
9792 const char *name
9793 = rust_last_path_segment (TYPE_FIELD_NAME (enum_type, i));
9794 discriminant_map[name] = TYPE_FIELD_ENUMVAL (enum_type, i);
9795 }
9796 }
9797
1f704f76 9798 int n_fields = type->num_fields ();
9c6a1327
TT
9799 /* We don't need a range entry for the discriminant, but we do
9800 need one for every other field, as there is no default
9801 variant. */
9802 discriminant_range *ranges = XOBNEWVEC (&objfile->objfile_obstack,
9803 discriminant_range,
9804 n_fields - 1);
c9317f21
TT
9805 /* Skip the discriminant here. */
9806 for (int i = 1; i < n_fields; ++i)
9807 {
9808 /* Find the final word in the name of this variant's type.
9809 That name can be used to look up the correct
9810 discriminant. */
9811 const char *variant_name
940da03e 9812 = rust_last_path_segment (type->field (i).type ()->name ());
c9317f21
TT
9813
9814 auto iter = discriminant_map.find (variant_name);
9815 if (iter != discriminant_map.end ())
9c6a1327 9816 {
57d02173
TT
9817 ranges[i - 1].low = iter->second;
9818 ranges[i - 1].high = iter->second;
9c6a1327 9819 }
c9317f21 9820
57d02173
TT
9821 /* In Rust, each element should have the size of the
9822 enclosing enum. */
9823 TYPE_LENGTH (type->field (i).type ()) = TYPE_LENGTH (type);
9824
bedda9ac 9825 /* Remove the discriminant field, if it exists. */
940da03e 9826 struct type *sub_type = type->field (i).type ();
1f704f76 9827 if (sub_type->num_fields () > 0)
bedda9ac 9828 {
5e33d5f4 9829 sub_type->set_num_fields (sub_type->num_fields () - 1);
3cabb6b0 9830 sub_type->set_fields (sub_type->fields () + 1);
bedda9ac 9831 }
9c6a1327 9832 TYPE_FIELD_NAME (type, i) = variant_name;
d0e39ea2
SM
9833 sub_type->set_name
9834 (rust_fully_qualify (&objfile->objfile_obstack,
7d93a1e0 9835 type->name (), variant_name));
c9317f21 9836 }
9c6a1327
TT
9837
9838 /* Indicate that this is a variant type. */
a1520ad8 9839 alloc_rust_variant (&objfile->objfile_obstack, type, 0, -1,
9c6a1327
TT
9840 gdb::array_view<discriminant_range> (ranges,
9841 n_fields - 1));
c9317f21
TT
9842 }
9843}
9844
9845/* Rewrite some Rust unions to be structures with variants parts. */
9846
9847static void
9848rust_union_quirks (struct dwarf2_cu *cu)
9849{
9850 gdb_assert (cu->language == language_rust);
52941706 9851 for (type *type_ : cu->rust_unions)
5e22e966 9852 quirk_rust_enum (type_, cu->per_objfile->objfile);
2d79090e
TT
9853 /* We don't need this any more. */
9854 cu->rust_unions.clear ();
c9317f21
TT
9855}
9856
8adb8487
TT
9857/* See read.h. */
9858
9859type_unit_group_unshareable *
9860dwarf2_per_objfile::get_type_unit_group_unshareable (type_unit_group *tu_group)
9861{
9862 auto iter = this->m_type_units.find (tu_group);
9863 if (iter != this->m_type_units.end ())
9864 return iter->second.get ();
9865
9866 type_unit_group_unshareable_up uniq (new type_unit_group_unshareable);
9867 type_unit_group_unshareable *result = uniq.get ();
9868 this->m_type_units[tu_group] = std::move (uniq);
9869 return result;
9870}
9871
e286671b
TT
9872struct type *
9873dwarf2_per_objfile::get_type_for_signatured_type
9874 (signatured_type *sig_type) const
9875{
9876 auto iter = this->m_type_map.find (sig_type);
9877 if (iter == this->m_type_map.end ())
9878 return nullptr;
9879
9880 return iter->second;
9881}
9882
9883void dwarf2_per_objfile::set_type_for_signatured_type
9884 (signatured_type *sig_type, struct type *type)
9885{
9886 gdb_assert (this->m_type_map.find (sig_type) == this->m_type_map.end ());
9887
9888 this->m_type_map[sig_type] = type;
9889}
9890
95554aad
TT
9891/* A helper function for computing the list of all symbol tables
9892 included by PER_CU. */
9893
9894static void
4c39bc03 9895recursively_compute_inclusions (std::vector<compunit_symtab *> *result,
ec94af83 9896 htab_t all_children, htab_t all_type_symtabs,
43182c09
SM
9897 dwarf2_per_cu_data *per_cu,
9898 dwarf2_per_objfile *per_objfile,
43f3e411 9899 struct compunit_symtab *immediate_parent)
95554aad 9900{
af758d11 9901 void **slot = htab_find_slot (all_children, per_cu, INSERT);
95554aad
TT
9902 if (*slot != NULL)
9903 {
9904 /* This inclusion and its children have been processed. */
9905 return;
9906 }
9907
9908 *slot = per_cu;
af758d11 9909
95554aad 9910 /* Only add a CU if it has a symbol table. */
43182c09 9911 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
43f3e411 9912 if (cust != NULL)
ec94af83
DE
9913 {
9914 /* If this is a type unit only add its symbol table if we haven't
9915 seen it yet (type unit per_cu's can share symtabs). */
9916 if (per_cu->is_debug_types)
9917 {
43f3e411 9918 slot = htab_find_slot (all_type_symtabs, cust, INSERT);
ec94af83
DE
9919 if (*slot == NULL)
9920 {
43f3e411 9921 *slot = cust;
4c39bc03 9922 result->push_back (cust);
43f3e411
DE
9923 if (cust->user == NULL)
9924 cust->user = immediate_parent;
ec94af83
DE
9925 }
9926 }
9927 else
f9125b6c 9928 {
4c39bc03 9929 result->push_back (cust);
43f3e411
DE
9930 if (cust->user == NULL)
9931 cust->user = immediate_parent;
f9125b6c 9932 }
ec94af83 9933 }
95554aad 9934
ae640021
AB
9935 if (!per_cu->imported_symtabs_empty ())
9936 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
9937 {
9938 recursively_compute_inclusions (result, all_children,
43182c09
SM
9939 all_type_symtabs, ptr, per_objfile,
9940 cust);
ae640021 9941 }
95554aad
TT
9942}
9943
43f3e411 9944/* Compute the compunit_symtab 'includes' fields for the compunit_symtab of
95554aad
TT
9945 PER_CU. */
9946
9947static void
43182c09
SM
9948compute_compunit_symtab_includes (dwarf2_per_cu_data *per_cu,
9949 dwarf2_per_objfile *per_objfile)
95554aad 9950{
f4dc4d17
DE
9951 gdb_assert (! per_cu->is_debug_types);
9952
ae640021 9953 if (!per_cu->imported_symtabs_empty ())
95554aad 9954 {
ae640021 9955 int len;
4c39bc03 9956 std::vector<compunit_symtab *> result_symtabs;
43182c09 9957 compunit_symtab *cust = per_objfile->get_symtab (per_cu);
95554aad
TT
9958
9959 /* If we don't have a symtab, we can just skip this case. */
43f3e411 9960 if (cust == NULL)
95554aad
TT
9961 return;
9962
280a9412
TT
9963 htab_up all_children (htab_create_alloc (1, htab_hash_pointer,
9964 htab_eq_pointer,
9965 NULL, xcalloc, xfree));
9966 htab_up all_type_symtabs (htab_create_alloc (1, htab_hash_pointer,
9967 htab_eq_pointer,
9968 NULL, xcalloc, xfree));
95554aad 9969
ae640021 9970 for (dwarf2_per_cu_data *ptr : *per_cu->imported_symtabs)
ec94af83 9971 {
280a9412
TT
9972 recursively_compute_inclusions (&result_symtabs, all_children.get (),
9973 all_type_symtabs.get (), ptr,
9974 per_objfile, cust);
ec94af83 9975 }
95554aad 9976
ec94af83 9977 /* Now we have a transitive closure of all the included symtabs. */
4c39bc03 9978 len = result_symtabs.size ();
43f3e411 9979 cust->includes
f6e649dd 9980 = XOBNEWVEC (&per_objfile->objfile->objfile_obstack,
8d749320 9981 struct compunit_symtab *, len + 1);
4c39bc03
TT
9982 memcpy (cust->includes, result_symtabs.data (),
9983 len * sizeof (compunit_symtab *));
43f3e411 9984 cust->includes[len] = NULL;
95554aad
TT
9985 }
9986}
9987
9988/* Compute the 'includes' field for the symtabs of all the CUs we just
9989 read. */
9990
9991static void
976ca316 9992process_cu_includes (dwarf2_per_objfile *per_objfile)
95554aad 9993{
976ca316 9994 for (dwarf2_per_cu_data *iter : per_objfile->per_bfd->just_read_cus)
f4dc4d17
DE
9995 {
9996 if (! iter->is_debug_types)
976ca316 9997 compute_compunit_symtab_includes (iter, per_objfile);
f4dc4d17 9998 }
95554aad 9999
976ca316 10000 per_objfile->per_bfd->just_read_cus.clear ();
95554aad
TT
10001}
10002
8fc0b21d 10003/* Generate full symbol information for CU, whose DIEs have
10b3939b
DJ
10004 already been loaded into memory. */
10005
10006static void
8fc0b21d 10007process_full_comp_unit (dwarf2_cu *cu, enum language pretend_language)
10b3939b 10008{
976ca316
SM
10009 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10010 struct objfile *objfile = per_objfile->objfile;
08feed99 10011 struct gdbarch *gdbarch = objfile->arch ();
10b3939b 10012 CORE_ADDR lowpc, highpc;
43f3e411 10013 struct compunit_symtab *cust;
10b3939b 10014 CORE_ADDR baseaddr;
4359dff1 10015 struct block *static_block;
3e29f34a 10016 CORE_ADDR addr;
10b3939b 10017
b3b3bada 10018 baseaddr = objfile->text_section_offset ();
10b3939b 10019
c89b44cd
TT
10020 /* Clear the list here in case something was left over. */
10021 cu->method_list.clear ();
10b3939b 10022
95554aad
TT
10023 cu->language = pretend_language;
10024 cu->language_defn = language_def (cu->language);
10025
1c47ec3e
TV
10026 dwarf2_find_base_address (cu->dies, cu);
10027
c906108c 10028 /* Do line number decoding in read_file_scope () */
10b3939b 10029 process_die (cu->dies, cu);
c906108c 10030
a766d390
DE
10031 /* For now fudge the Go package. */
10032 if (cu->language == language_go)
10033 fixup_go_packaging (cu);
10034
5f48f8f3 10035 /* Now that we have processed all the DIEs in the CU, all the types
3da10d80
KS
10036 should be complete, and it should now be safe to compute all of the
10037 physnames. */
10038 compute_delayed_physnames (cu);
3da10d80 10039
c9317f21
TT
10040 if (cu->language == language_rust)
10041 rust_union_quirks (cu);
10042
fae299cd
DC
10043 /* Some compilers don't define a DW_AT_high_pc attribute for the
10044 compilation unit. If the DW_AT_high_pc is missing, synthesize
10045 it, by scanning the DIE's below the compilation unit. */
10b3939b 10046 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
c906108c 10047
3e29f34a 10048 addr = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c24bdb02 10049 static_block = cu->get_builder ()->end_symtab_get_static_block (addr, 0, 1);
4359dff1
JK
10050
10051 /* If the comp unit has DW_AT_ranges, it may have discontiguous ranges.
10052 Also, DW_AT_ranges may record ranges not belonging to any child DIEs
10053 (such as virtual method tables). Record the ranges in STATIC_BLOCK's
10054 addrmap to help ensure it has an accurate map of pc values belonging to
10055 this comp unit. */
10056 dwarf2_record_block_ranges (cu->dies, static_block, baseaddr, cu);
10057
c24bdb02 10058 cust = cu->get_builder ()->end_symtab_from_static_block (static_block,
804d2729
TT
10059 SECT_OFF_TEXT (objfile),
10060 0);
c906108c 10061
43f3e411 10062 if (cust != NULL)
c906108c 10063 {
df15bd07 10064 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4632c0d0 10065
8be455d7
JK
10066 /* Set symtab language to language from DW_AT_language. If the
10067 compilation is from a C file generated by language preprocessors, do
10068 not set the language if it was already deduced by start_subfile. */
43f3e411 10069 if (!(cu->language == language_c
40e3ad0e 10070 && COMPUNIT_FILETABS (cust)->language != language_unknown))
43f3e411 10071 COMPUNIT_FILETABS (cust)->language = cu->language;
8be455d7
JK
10072
10073 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
10074 produce DW_AT_location with location lists but it can be possibly
ab260dad
JK
10075 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
10076 there were bugs in prologue debug info, fixed later in GCC-4.5
10077 by "unwind info for epilogues" patch (which is not directly related).
8be455d7
JK
10078
10079 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
10080 needed, it would be wrong due to missing DW_AT_producer there.
10081
10082 Still one can confuse GDB by using non-standard GCC compilation
10083 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
5f48f8f3 10084 */
ab260dad 10085 if (cu->has_loclist && gcc_4_minor >= 5)
43f3e411 10086 cust->locations_valid = 1;
e0d00bc7
JK
10087
10088 if (gcc_4_minor >= 5)
43f3e411 10089 cust->epilogue_unwind_valid = 1;
96408a79 10090
43f3e411 10091 cust->call_site_htab = cu->call_site_htab;
c906108c 10092 }
9291a0cd 10093
976ca316 10094 per_objfile->set_symtab (cu->per_cu, cust);
c906108c 10095
95554aad 10096 /* Push it for inclusion processing later. */
976ca316 10097 per_objfile->per_bfd->just_read_cus.push_back (cu->per_cu);
804d2729
TT
10098
10099 /* Not needed any more. */
c24bdb02 10100 cu->reset_builder ();
f4dc4d17 10101}
45cfd468 10102
8fc0b21d 10103/* Generate full symbol information for type unit CU, whose DIEs have
f4dc4d17
DE
10104 already been loaded into memory. */
10105
10106static void
8fc0b21d 10107process_full_type_unit (dwarf2_cu *cu,
f4dc4d17
DE
10108 enum language pretend_language)
10109{
976ca316
SM
10110 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10111 struct objfile *objfile = per_objfile->objfile;
43f3e411 10112 struct compunit_symtab *cust;
0186c6a7
DE
10113 struct signatured_type *sig_type;
10114
8fc0b21d
SM
10115 gdb_assert (cu->per_cu->is_debug_types);
10116 sig_type = (struct signatured_type *) cu->per_cu;
f4dc4d17 10117
c89b44cd
TT
10118 /* Clear the list here in case something was left over. */
10119 cu->method_list.clear ();
f4dc4d17 10120
f4dc4d17
DE
10121 cu->language = pretend_language;
10122 cu->language_defn = language_def (cu->language);
10123
10124 /* The symbol tables are set up in read_type_unit_scope. */
10125 process_die (cu->dies, cu);
10126
10127 /* For now fudge the Go package. */
10128 if (cu->language == language_go)
10129 fixup_go_packaging (cu);
10130
5f48f8f3 10131 /* Now that we have processed all the DIEs in the CU, all the types
f4dc4d17
DE
10132 should be complete, and it should now be safe to compute all of the
10133 physnames. */
10134 compute_delayed_physnames (cu);
f4dc4d17 10135
c9317f21
TT
10136 if (cu->language == language_rust)
10137 rust_union_quirks (cu);
10138
f4dc4d17
DE
10139 /* TUs share symbol tables.
10140 If this is the first TU to use this symtab, complete the construction
094b34ac
DE
10141 of it with end_expandable_symtab. Otherwise, complete the addition of
10142 this TU's symbols to the existing symtab. */
8adb8487 10143 type_unit_group_unshareable *tug_unshare =
976ca316 10144 per_objfile->get_type_unit_group_unshareable (sig_type->type_unit_group);
8adb8487 10145 if (tug_unshare->compunit_symtab == NULL)
45cfd468 10146 {
c24bdb02
KS
10147 buildsym_compunit *builder = cu->get_builder ();
10148 cust = builder->end_expandable_symtab (0, SECT_OFF_TEXT (objfile));
8adb8487 10149 tug_unshare->compunit_symtab = cust;
f4dc4d17 10150
43f3e411 10151 if (cust != NULL)
f4dc4d17
DE
10152 {
10153 /* Set symtab language to language from DW_AT_language. If the
10154 compilation is from a C file generated by language preprocessors,
10155 do not set the language if it was already deduced by
10156 start_subfile. */
43f3e411
DE
10157 if (!(cu->language == language_c
10158 && COMPUNIT_FILETABS (cust)->language != language_c))
10159 COMPUNIT_FILETABS (cust)->language = cu->language;
f4dc4d17
DE
10160 }
10161 }
10162 else
10163 {
c24bdb02 10164 cu->get_builder ()->augment_type_symtab ();
8adb8487 10165 cust = tug_unshare->compunit_symtab;
f4dc4d17
DE
10166 }
10167
976ca316 10168 per_objfile->set_symtab (cu->per_cu, cust);
804d2729
TT
10169
10170 /* Not needed any more. */
c24bdb02 10171 cu->reset_builder ();
c906108c
SS
10172}
10173
95554aad
TT
10174/* Process an imported unit DIE. */
10175
10176static void
10177process_imported_unit_die (struct die_info *die, struct dwarf2_cu *cu)
10178{
10179 struct attribute *attr;
10180
f4dc4d17
DE
10181 /* For now we don't handle imported units in type units. */
10182 if (cu->per_cu->is_debug_types)
10183 {
10184 error (_("Dwarf Error: DW_TAG_imported_unit is not"
10185 " supported in type units [in module %s]"),
5e22e966 10186 objfile_name (cu->per_objfile->objfile));
f4dc4d17
DE
10187 }
10188
95554aad
TT
10189 attr = dwarf2_attr (die, DW_AT_import, cu);
10190 if (attr != NULL)
10191 {
0826b30a 10192 sect_offset sect_off = attr->get_ref_die_offset ();
9c541725 10193 bool is_dwz = (attr->form == DW_FORM_GNU_ref_alt || cu->per_cu->is_dwz);
ab432490 10194 dwarf2_per_objfile *per_objfile = cu->per_objfile;
9c541725 10195 dwarf2_per_cu_data *per_cu
ab432490 10196 = dwarf2_find_containing_comp_unit (sect_off, is_dwz, per_objfile);
95554aad 10197
58990295
TV
10198 /* We're importing a C++ compilation unit with tag DW_TAG_compile_unit
10199 into another compilation unit, at root level. Regard this as a hint,
10200 and ignore it. */
10201 if (die->parent && die->parent->parent == NULL
10202 && per_cu->unit_type == DW_UT_compile
10203 && per_cu->lang == language_cplus)
10204 return;
10205
69d751e3 10206 /* If necessary, add it to the queue and load its DIEs. */
120ce1b5 10207 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
10208 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
10209 false, cu->language);
95554aad 10210
ae640021 10211 cu->per_cu->imported_symtabs_push (per_cu);
95554aad
TT
10212 }
10213}
10214
4c8aa72d
PA
10215/* RAII object that represents a process_die scope: i.e.,
10216 starts/finishes processing a DIE. */
10217class process_die_scope
adde2bff 10218{
4c8aa72d
PA
10219public:
10220 process_die_scope (die_info *die, dwarf2_cu *cu)
10221 : m_die (die), m_cu (cu)
10222 {
10223 /* We should only be processing DIEs not already in process. */
10224 gdb_assert (!m_die->in_process);
10225 m_die->in_process = true;
10226 }
8c3cb9fa 10227
4c8aa72d
PA
10228 ~process_die_scope ()
10229 {
10230 m_die->in_process = false;
10231
10232 /* If we're done processing the DIE for the CU that owns the line
10233 header, we don't need the line header anymore. */
10234 if (m_cu->line_header_die_owner == m_die)
10235 {
10236 delete m_cu->line_header;
10237 m_cu->line_header = NULL;
10238 m_cu->line_header_die_owner = NULL;
10239 }
10240 }
10241
10242private:
10243 die_info *m_die;
10244 dwarf2_cu *m_cu;
10245};
adde2bff 10246
c906108c
SS
10247/* Process a die and its children. */
10248
10249static void
e7c27a73 10250process_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 10251{
4c8aa72d 10252 process_die_scope scope (die, cu);
adde2bff 10253
c906108c
SS
10254 switch (die->tag)
10255 {
10256 case DW_TAG_padding:
10257 break;
10258 case DW_TAG_compile_unit:
95554aad 10259 case DW_TAG_partial_unit:
e7c27a73 10260 read_file_scope (die, cu);
c906108c 10261 break;
348e048f
DE
10262 case DW_TAG_type_unit:
10263 read_type_unit_scope (die, cu);
10264 break;
c906108c 10265 case DW_TAG_subprogram:
0a4b0913
AB
10266 /* Nested subprograms in Fortran get a prefix. */
10267 if (cu->language == language_fortran
10268 && die->parent != NULL
10269 && die->parent->tag == DW_TAG_subprogram)
10270 cu->processing_has_namespace_info = true;
10271 /* Fall through. */
c906108c 10272 case DW_TAG_inlined_subroutine:
edb3359d 10273 read_func_scope (die, cu);
c906108c
SS
10274 break;
10275 case DW_TAG_lexical_block:
14898363
L
10276 case DW_TAG_try_block:
10277 case DW_TAG_catch_block:
e7c27a73 10278 read_lexical_block_scope (die, cu);
c906108c 10279 break;
216f72a1 10280 case DW_TAG_call_site:
96408a79
SA
10281 case DW_TAG_GNU_call_site:
10282 read_call_site_scope (die, cu);
10283 break;
c906108c 10284 case DW_TAG_class_type:
680b30c7 10285 case DW_TAG_interface_type:
c906108c
SS
10286 case DW_TAG_structure_type:
10287 case DW_TAG_union_type:
134d01f1 10288 process_structure_scope (die, cu);
c906108c
SS
10289 break;
10290 case DW_TAG_enumeration_type:
134d01f1 10291 process_enumeration_scope (die, cu);
c906108c 10292 break;
134d01f1 10293
f792889a
DJ
10294 /* These dies have a type, but processing them does not create
10295 a symbol or recurse to process the children. Therefore we can
10296 read them on-demand through read_type_die. */
c906108c 10297 case DW_TAG_subroutine_type:
72019c9c 10298 case DW_TAG_set_type:
c906108c 10299 case DW_TAG_pointer_type:
c906108c 10300 case DW_TAG_ptr_to_member_type:
c906108c 10301 case DW_TAG_reference_type:
4297a3f0 10302 case DW_TAG_rvalue_reference_type:
c906108c 10303 case DW_TAG_string_type:
c906108c 10304 break;
134d01f1 10305
d8f62e84
TT
10306 case DW_TAG_array_type:
10307 /* We only need to handle this case for Ada -- in other
10308 languages, it's normal for the compiler to emit a typedef
10309 instead. */
10310 if (cu->language != language_ada)
10311 break;
10312 /* FALLTHROUGH */
c906108c 10313 case DW_TAG_base_type:
a02abb62 10314 case DW_TAG_subrange_type:
cb249c71 10315 case DW_TAG_typedef:
134d01f1 10316 /* Add a typedef symbol for the type definition, if it has a
dda83cd7 10317 DW_AT_name. */
f792889a 10318 new_symbol (die, read_type_die (die, cu), cu);
a02abb62 10319 break;
c906108c 10320 case DW_TAG_common_block:
e7c27a73 10321 read_common_block (die, cu);
c906108c
SS
10322 break;
10323 case DW_TAG_common_inclusion:
10324 break;
d9fa45fe 10325 case DW_TAG_namespace:
9068261f 10326 cu->processing_has_namespace_info = true;
e7c27a73 10327 read_namespace (die, cu);
d9fa45fe 10328 break;
5d7cb8df 10329 case DW_TAG_module:
9068261f 10330 cu->processing_has_namespace_info = true;
5d7cb8df
JK
10331 read_module (die, cu);
10332 break;
d9fa45fe 10333 case DW_TAG_imported_declaration:
9068261f 10334 cu->processing_has_namespace_info = true;
74921315
KS
10335 if (read_namespace_alias (die, cu))
10336 break;
86a73007
TT
10337 /* The declaration is not a global namespace alias. */
10338 /* Fall through. */
d9fa45fe 10339 case DW_TAG_imported_module:
9068261f 10340 cu->processing_has_namespace_info = true;
27aa8d6a
SW
10341 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
10342 || cu->language != language_fortran))
b98664d3 10343 complaint (_("Tag '%s' has unexpected children"),
27aa8d6a
SW
10344 dwarf_tag_name (die->tag));
10345 read_import_statement (die, cu);
d9fa45fe 10346 break;
95554aad
TT
10347
10348 case DW_TAG_imported_unit:
10349 process_imported_unit_die (die, cu);
10350 break;
10351
71a3c369
TT
10352 case DW_TAG_variable:
10353 read_variable (die, cu);
10354 break;
10355
c906108c 10356 default:
e7c27a73 10357 new_symbol (die, NULL, cu);
c906108c
SS
10358 break;
10359 }
10360}
ca69b9e6
DE
10361\f
10362/* DWARF name computation. */
c906108c 10363
94af9270
KS
10364/* A helper function for dwarf2_compute_name which determines whether DIE
10365 needs to have the name of the scope prepended to the name listed in the
10366 die. */
10367
10368static int
10369die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
10370{
1c809c68
TT
10371 struct attribute *attr;
10372
94af9270
KS
10373 switch (die->tag)
10374 {
10375 case DW_TAG_namespace:
10376 case DW_TAG_typedef:
10377 case DW_TAG_class_type:
10378 case DW_TAG_interface_type:
10379 case DW_TAG_structure_type:
10380 case DW_TAG_union_type:
10381 case DW_TAG_enumeration_type:
10382 case DW_TAG_enumerator:
10383 case DW_TAG_subprogram:
08a76f8a 10384 case DW_TAG_inlined_subroutine:
94af9270 10385 case DW_TAG_member:
74921315 10386 case DW_TAG_imported_declaration:
94af9270
KS
10387 return 1;
10388
10389 case DW_TAG_variable:
c2b0a229 10390 case DW_TAG_constant:
94af9270
KS
10391 /* We only need to prefix "globally" visible variables. These include
10392 any variable marked with DW_AT_external or any variable that
10393 lives in a namespace. [Variables in anonymous namespaces
10394 require prefixing, but they are not DW_AT_external.] */
10395
10396 if (dwarf2_attr (die, DW_AT_specification, cu))
10397 {
10398 struct dwarf2_cu *spec_cu = cu;
9a619af0 10399
94af9270
KS
10400 return die_needs_namespace (die_specification (die, &spec_cu),
10401 spec_cu);
10402 }
10403
1c809c68 10404 attr = dwarf2_attr (die, DW_AT_external, cu);
f55ee35c
JK
10405 if (attr == NULL && die->parent->tag != DW_TAG_namespace
10406 && die->parent->tag != DW_TAG_module)
1c809c68
TT
10407 return 0;
10408 /* A variable in a lexical block of some kind does not need a
10409 namespace, even though in C++ such variables may be external
10410 and have a mangled name. */
10411 if (die->parent->tag == DW_TAG_lexical_block
10412 || die->parent->tag == DW_TAG_try_block
1054b214
TT
10413 || die->parent->tag == DW_TAG_catch_block
10414 || die->parent->tag == DW_TAG_subprogram)
1c809c68
TT
10415 return 0;
10416 return 1;
94af9270
KS
10417
10418 default:
10419 return 0;
10420 }
10421}
10422
73b9be8b
KS
10423/* Return the DIE's linkage name attribute, either DW_AT_linkage_name
10424 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10425 defined for the given DIE. */
10426
10427static struct attribute *
10428dw2_linkage_name_attr (struct die_info *die, struct dwarf2_cu *cu)
10429{
10430 struct attribute *attr;
10431
10432 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
10433 if (attr == NULL)
10434 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
10435
10436 return attr;
10437}
10438
10439/* Return the DIE's linkage name as a string, either DW_AT_linkage_name
10440 or DW_AT_MIPS_linkage_name. Returns NULL if the attribute is not
10441 defined for the given DIE. */
10442
10443static const char *
10444dw2_linkage_name (struct die_info *die, struct dwarf2_cu *cu)
10445{
10446 const char *linkage_name;
10447
10448 linkage_name = dwarf2_string_attr (die, DW_AT_linkage_name, cu);
10449 if (linkage_name == NULL)
10450 linkage_name = dwarf2_string_attr (die, DW_AT_MIPS_linkage_name, cu);
10451
787de330
TT
10452 /* rustc emits invalid values for DW_AT_linkage_name. Ignore these.
10453 See https://github.com/rust-lang/rust/issues/32925. */
10454 if (cu->language == language_rust && linkage_name != NULL
10455 && strchr (linkage_name, '{') != NULL)
10456 linkage_name = NULL;
10457
73b9be8b
KS
10458 return linkage_name;
10459}
10460
94af9270 10461/* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
a766d390 10462 compute the physname for the object, which include a method's:
9c37b5ae 10463 - formal parameters (C++),
a766d390 10464 - receiver type (Go),
a766d390
DE
10465
10466 The term "physname" is a bit confusing.
10467 For C++, for example, it is the demangled name.
10468 For Go, for example, it's the mangled name.
94af9270 10469
af6b7be1
JB
10470 For Ada, return the DIE's linkage name rather than the fully qualified
10471 name. PHYSNAME is ignored..
10472
5989a64e 10473 The result is allocated on the objfile->per_bfd's obstack and
45940949 10474 canonicalized. */
94af9270
KS
10475
10476static const char *
15d034d0
TT
10477dwarf2_compute_name (const char *name,
10478 struct die_info *die, struct dwarf2_cu *cu,
94af9270
KS
10479 int physname)
10480{
5e22e966 10481 struct objfile *objfile = cu->per_objfile->objfile;
bb5ed363 10482
94af9270
KS
10483 if (name == NULL)
10484 name = dwarf2_name (die, cu);
10485
2ee7123e
DE
10486 /* For Fortran GDB prefers DW_AT_*linkage_name for the physname if present
10487 but otherwise compute it by typename_concat inside GDB.
10488 FIXME: Actually this is not really true, or at least not always true.
4d4eaa30 10489 It's all very confusing. compute_and_set_names doesn't try to demangle
5e2db402 10490 Fortran names because there is no mangling standard. So new_symbol
2ee7123e
DE
10491 will set the demangled name to the result of dwarf2_full_name, and it is
10492 the demangled name that GDB uses if it exists. */
f55ee35c
JK
10493 if (cu->language == language_ada
10494 || (cu->language == language_fortran && physname))
10495 {
10496 /* For Ada unit, we prefer the linkage name over the name, as
10497 the former contains the exported name, which the user expects
10498 to be able to reference. Ideally, we want the user to be able
10499 to reference this entity using either natural or linkage name,
10500 but we haven't started looking at this enhancement yet. */
73b9be8b 10501 const char *linkage_name = dw2_linkage_name (die, cu);
f55ee35c 10502
2ee7123e
DE
10503 if (linkage_name != NULL)
10504 return linkage_name;
f55ee35c
JK
10505 }
10506
94af9270
KS
10507 /* These are the only languages we know how to qualify names in. */
10508 if (name != NULL
9c37b5ae 10509 && (cu->language == language_cplus
c44af4eb
TT
10510 || cu->language == language_fortran || cu->language == language_d
10511 || cu->language == language_rust))
94af9270
KS
10512 {
10513 if (die_needs_namespace (die, cu))
10514 {
0d5cff50 10515 const char *prefix;
34a68019 10516 const char *canonical_name = NULL;
94af9270 10517
d7e74731
PA
10518 string_file buf;
10519
94af9270 10520 prefix = determine_prefix (die, cu);
94af9270
KS
10521 if (*prefix != '\0')
10522 {
43816ebc
TT
10523 gdb::unique_xmalloc_ptr<char> prefixed_name
10524 (typename_concat (NULL, prefix, name, physname, cu));
9a619af0 10525
43816ebc 10526 buf.puts (prefixed_name.get ());
94af9270
KS
10527 }
10528 else
d7e74731 10529 buf.puts (name);
94af9270 10530
98bfdba5
PA
10531 /* Template parameters may be specified in the DIE's DW_AT_name, or
10532 as children with DW_TAG_template_type_param or
10533 DW_TAG_value_type_param. If the latter, add them to the name
10534 here. If the name already has template parameters, then
10535 skip this step; some versions of GCC emit both, and
10536 it is more efficient to use the pre-computed name.
10537
10538 Something to keep in mind about this process: it is very
10539 unlikely, or in some cases downright impossible, to produce
10540 something that will match the mangled name of a function.
10541 If the definition of the function has the same debug info,
10542 we should be able to match up with it anyway. But fallbacks
10543 using the minimal symbol, for instance to find a method
10544 implemented in a stripped copy of libstdc++, will not work.
10545 If we do not have debug info for the definition, we will have to
10546 match them up some other way.
10547
10548 When we do name matching there is a related problem with function
10549 templates; two instantiated function templates are allowed to
10550 differ only by their return types, which we do not add here. */
10551
10552 if (cu->language == language_cplus && strchr (name, '<') == NULL)
10553 {
10554 struct attribute *attr;
10555 struct die_info *child;
10556 int first = 1;
2c75ccb2 10557 const language_defn *cplus_lang = language_def (cu->language);
98bfdba5
PA
10558
10559 die->building_fullname = 1;
10560
10561 for (child = die->child; child != NULL; child = child->sibling)
10562 {
10563 struct type *type;
12df843f 10564 LONGEST value;
d521ce57 10565 const gdb_byte *bytes;
98bfdba5
PA
10566 struct dwarf2_locexpr_baton *baton;
10567 struct value *v;
10568
10569 if (child->tag != DW_TAG_template_type_param
10570 && child->tag != DW_TAG_template_value_param)
10571 continue;
10572
10573 if (first)
10574 {
d7e74731 10575 buf.puts ("<");
98bfdba5
PA
10576 first = 0;
10577 }
10578 else
d7e74731 10579 buf.puts (", ");
98bfdba5
PA
10580
10581 attr = dwarf2_attr (child, DW_AT_type, cu);
10582 if (attr == NULL)
10583 {
b98664d3 10584 complaint (_("template parameter missing DW_AT_type"));
d7e74731 10585 buf.puts ("UNKNOWN_TYPE");
98bfdba5
PA
10586 continue;
10587 }
10588 type = die_type (child, cu);
10589
10590 if (child->tag == DW_TAG_template_type_param)
10591 {
2c75ccb2
AB
10592 cplus_lang->print_type (type, "", &buf, -1, 0,
10593 &type_print_raw_options);
98bfdba5
PA
10594 continue;
10595 }
10596
10597 attr = dwarf2_attr (child, DW_AT_const_value, cu);
10598 if (attr == NULL)
10599 {
b98664d3 10600 complaint (_("template parameter missing "
3e43a32a 10601 "DW_AT_const_value"));
d7e74731 10602 buf.puts ("UNKNOWN_VALUE");
98bfdba5
PA
10603 continue;
10604 }
10605
10606 dwarf2_const_value_attr (attr, type, name,
10607 &cu->comp_unit_obstack, cu,
10608 &value, &bytes, &baton);
10609
20ce4123 10610 if (type->has_no_signedness ())
98bfdba5
PA
10611 /* GDB prints characters as NUMBER 'CHAR'. If that's
10612 changed, this can use value_print instead. */
2c75ccb2 10613 cplus_lang->printchar (value, type, &buf);
98bfdba5
PA
10614 else
10615 {
10616 struct value_print_options opts;
10617
10618 if (baton != NULL)
10619 v = dwarf2_evaluate_loc_desc (type, NULL,
10620 baton->data,
10621 baton->size,
9f47c707
SM
10622 baton->per_cu,
10623 baton->per_objfile);
98bfdba5
PA
10624 else if (bytes != NULL)
10625 {
10626 v = allocate_value (type);
10627 memcpy (value_contents_writeable (v), bytes,
10628 TYPE_LENGTH (type));
10629 }
10630 else
10631 v = value_from_longest (type, value);
10632
3e43a32a
MS
10633 /* Specify decimal so that we do not depend on
10634 the radix. */
98bfdba5
PA
10635 get_formatted_print_options (&opts, 'd');
10636 opts.raw = 1;
d7e74731 10637 value_print (v, &buf, &opts);
98bfdba5 10638 release_value (v);
98bfdba5
PA
10639 }
10640 }
10641
10642 die->building_fullname = 0;
10643
10644 if (!first)
10645 {
10646 /* Close the argument list, with a space if necessary
10647 (nested templates). */
d7e74731
PA
10648 if (!buf.empty () && buf.string ().back () == '>')
10649 buf.puts (" >");
98bfdba5 10650 else
d7e74731 10651 buf.puts (">");
98bfdba5
PA
10652 }
10653 }
10654
9c37b5ae 10655 /* For C++ methods, append formal parameter type
94af9270 10656 information, if PHYSNAME. */
6e70227d 10657
94af9270 10658 if (physname && die->tag == DW_TAG_subprogram
9c37b5ae 10659 && cu->language == language_cplus)
94af9270
KS
10660 {
10661 struct type *type = read_type_die (die, cu);
10662
d7e74731 10663 c_type_print_args (type, &buf, 1, cu->language,
79d43c61 10664 &type_print_raw_options);
94af9270 10665
9c37b5ae 10666 if (cu->language == language_cplus)
94af9270 10667 {
60430eff
DJ
10668 /* Assume that an artificial first parameter is
10669 "this", but do not crash if it is not. RealView
10670 marks unnamed (and thus unused) parameters as
10671 artificial; there is no way to differentiate
10672 the two cases. */
1f704f76 10673 if (type->num_fields () > 0
94af9270 10674 && TYPE_FIELD_ARTIFICIAL (type, 0)
940da03e
SM
10675 && type->field (0).type ()->code () == TYPE_CODE_PTR
10676 && TYPE_CONST (TYPE_TARGET_TYPE (type->field (0).type ())))
d7e74731 10677 buf.puts (" const");
94af9270
KS
10678 }
10679 }
10680
d7e74731 10681 const std::string &intermediate_name = buf.string ();
94af9270
KS
10682
10683 if (cu->language == language_cplus)
34a68019 10684 canonical_name
322a8516 10685 = dwarf2_canonicalize_name (intermediate_name.c_str (), cu,
be1e3d3e 10686 objfile);
34a68019
TT
10687
10688 /* If we only computed INTERMEDIATE_NAME, or if
10689 INTERMEDIATE_NAME is already canonical, then we need to
be1e3d3e 10690 intern it. */
322a8516 10691 if (canonical_name == NULL || canonical_name == intermediate_name.c_str ())
be1e3d3e 10692 name = objfile->intern (intermediate_name);
34a68019
TT
10693 else
10694 name = canonical_name;
94af9270
KS
10695 }
10696 }
10697
10698 return name;
10699}
10700
0114d602
DJ
10701/* Return the fully qualified name of DIE, based on its DW_AT_name.
10702 If scope qualifiers are appropriate they will be added. The result
34a68019 10703 will be allocated on the storage_obstack, or NULL if the DIE does
94af9270
KS
10704 not have a name. NAME may either be from a previous call to
10705 dwarf2_name or NULL.
10706
9c37b5ae 10707 The output string will be canonicalized (if C++). */
0114d602
DJ
10708
10709static const char *
15d034d0 10710dwarf2_full_name (const char *name, struct die_info *die, struct dwarf2_cu *cu)
0114d602 10711{
94af9270
KS
10712 return dwarf2_compute_name (name, die, cu, 0);
10713}
0114d602 10714
94af9270
KS
10715/* Construct a physname for the given DIE in CU. NAME may either be
10716 from a previous call to dwarf2_name or NULL. The result will be
10717 allocated on the objfile_objstack or NULL if the DIE does not have a
10718 name.
0114d602 10719
9c37b5ae 10720 The output string will be canonicalized (if C++). */
0114d602 10721
94af9270 10722static const char *
15d034d0 10723dwarf2_physname (const char *name, struct die_info *die, struct dwarf2_cu *cu)
94af9270 10724{
5e22e966 10725 struct objfile *objfile = cu->per_objfile->objfile;
900e11f9 10726 const char *retval, *mangled = NULL, *canon = NULL;
900e11f9
JK
10727 int need_copy = 1;
10728
10729 /* In this case dwarf2_compute_name is just a shortcut not building anything
10730 on its own. */
10731 if (!die_needs_namespace (die, cu))
10732 return dwarf2_compute_name (name, die, cu, 1);
10733
906bb4c5
TT
10734 if (cu->language != language_rust)
10735 mangled = dw2_linkage_name (die, cu);
900e11f9
JK
10736
10737 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
10738 has computed. */
791afaa2 10739 gdb::unique_xmalloc_ptr<char> demangled;
7d45c7c3 10740 if (mangled != NULL)
900e11f9 10741 {
900e11f9 10742
d3355e4d 10743 if (language_def (cu->language)->store_sym_names_in_linkage_form_p ())
59cc4834
JB
10744 {
10745 /* Do nothing (do not demangle the symbol name). */
10746 }
a766d390
DE
10747 else
10748 {
0eb876f5
JB
10749 /* Use DMGL_RET_DROP for C++ template functions to suppress
10750 their return type. It is easier for GDB users to search
10751 for such functions as `name(params)' than `long name(params)'.
10752 In such case the minimal symbol names do not match the full
10753 symbol names but for template functions there is never a need
10754 to look up their definition from their declaration so
10755 the only disadvantage remains the minimal symbol variant
10756 `long name(params)' does not have the proper inferior type. */
791afaa2
TT
10757 demangled.reset (gdb_demangle (mangled,
10758 (DMGL_PARAMS | DMGL_ANSI
10759 | DMGL_RET_DROP)));
a766d390 10760 }
900e11f9 10761 if (demangled)
791afaa2 10762 canon = demangled.get ();
900e11f9
JK
10763 else
10764 {
10765 canon = mangled;
10766 need_copy = 0;
10767 }
10768 }
10769
10770 if (canon == NULL || check_physname)
10771 {
10772 const char *physname = dwarf2_compute_name (name, die, cu, 1);
10773
10774 if (canon != NULL && strcmp (physname, canon) != 0)
10775 {
10776 /* It may not mean a bug in GDB. The compiler could also
10777 compute DW_AT_linkage_name incorrectly. But in such case
10778 GDB would need to be bug-to-bug compatible. */
10779
b98664d3 10780 complaint (_("Computed physname <%s> does not match demangled <%s> "
9d8780f0
SM
10781 "(from linkage <%s>) - DIE at %s [in module %s]"),
10782 physname, canon, mangled, sect_offset_str (die->sect_off),
4262abfb 10783 objfile_name (objfile));
900e11f9
JK
10784
10785 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
10786 is available here - over computed PHYSNAME. It is safer
10787 against both buggy GDB and buggy compilers. */
10788
10789 retval = canon;
10790 }
10791 else
10792 {
10793 retval = physname;
10794 need_copy = 0;
10795 }
10796 }
10797 else
10798 retval = canon;
10799
10800 if (need_copy)
be1e3d3e 10801 retval = objfile->intern (retval);
900e11f9 10802
900e11f9 10803 return retval;
0114d602
DJ
10804}
10805
74921315
KS
10806/* Inspect DIE in CU for a namespace alias. If one exists, record
10807 a new symbol for it.
10808
10809 Returns 1 if a namespace alias was recorded, 0 otherwise. */
10810
10811static int
10812read_namespace_alias (struct die_info *die, struct dwarf2_cu *cu)
10813{
10814 struct attribute *attr;
10815
10816 /* If the die does not have a name, this is not a namespace
10817 alias. */
10818 attr = dwarf2_attr (die, DW_AT_name, cu);
10819 if (attr != NULL)
10820 {
10821 int num;
10822 struct die_info *d = die;
10823 struct dwarf2_cu *imported_cu = cu;
10824
10825 /* If the compiler has nested DW_AT_imported_declaration DIEs,
10826 keep inspecting DIEs until we hit the underlying import. */
10827#define MAX_NESTED_IMPORTED_DECLARATIONS 100
10828 for (num = 0; num < MAX_NESTED_IMPORTED_DECLARATIONS; ++num)
10829 {
10830 attr = dwarf2_attr (d, DW_AT_import, cu);
10831 if (attr == NULL)
10832 break;
10833
10834 d = follow_die_ref (d, attr, &imported_cu);
10835 if (d->tag != DW_TAG_imported_declaration)
10836 break;
10837 }
10838
10839 if (num == MAX_NESTED_IMPORTED_DECLARATIONS)
10840 {
b98664d3 10841 complaint (_("DIE at %s has too many recursively imported "
9d8780f0 10842 "declarations"), sect_offset_str (d->sect_off));
74921315
KS
10843 return 0;
10844 }
10845
10846 if (attr != NULL)
10847 {
10848 struct type *type;
0826b30a 10849 sect_offset sect_off = attr->get_ref_die_offset ();
74921315 10850
aa66c379 10851 type = get_die_type_at_offset (sect_off, cu->per_cu, cu->per_objfile);
78134374 10852 if (type != NULL && type->code () == TYPE_CODE_NAMESPACE)
74921315
KS
10853 {
10854 /* This declaration is a global namespace alias. Add
10855 a symbol for it whose type is the aliased namespace. */
10856 new_symbol (die, type, cu);
10857 return 1;
10858 }
10859 }
10860 }
10861
10862 return 0;
10863}
10864
22cee43f 10865/* Return the using directives repository (global or local?) to use in the
804d2729 10866 current context for CU.
22cee43f
PMR
10867
10868 For Ada, imported declarations can materialize renamings, which *may* be
10869 global. However it is impossible (for now?) in DWARF to distinguish
10870 "external" imported declarations and "static" ones. As all imported
10871 declarations seem to be static in all other languages, make them all CU-wide
10872 global only in Ada. */
10873
10874static struct using_direct **
804d2729 10875using_directives (struct dwarf2_cu *cu)
22cee43f 10876{
c24bdb02
KS
10877 if (cu->language == language_ada
10878 && cu->get_builder ()->outermost_context_p ())
10879 return cu->get_builder ()->get_global_using_directives ();
22cee43f 10880 else
c24bdb02 10881 return cu->get_builder ()->get_local_using_directives ();
22cee43f
PMR
10882}
10883
27aa8d6a
SW
10884/* Read the import statement specified by the given die and record it. */
10885
10886static void
10887read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
10888{
5e22e966 10889 struct objfile *objfile = cu->per_objfile->objfile;
27aa8d6a 10890 struct attribute *import_attr;
32019081 10891 struct die_info *imported_die, *child_die;
de4affc9 10892 struct dwarf2_cu *imported_cu;
27aa8d6a 10893 const char *imported_name;
794684b6 10894 const char *imported_name_prefix;
13387711
SW
10895 const char *canonical_name;
10896 const char *import_alias;
10897 const char *imported_declaration = NULL;
794684b6 10898 const char *import_prefix;
eb1e02fd 10899 std::vector<const char *> excludes;
13387711 10900
27aa8d6a
SW
10901 import_attr = dwarf2_attr (die, DW_AT_import, cu);
10902 if (import_attr == NULL)
10903 {
b98664d3 10904 complaint (_("Tag '%s' has no DW_AT_import"),
27aa8d6a
SW
10905 dwarf_tag_name (die->tag));
10906 return;
10907 }
10908
de4affc9
CC
10909 imported_cu = cu;
10910 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
10911 imported_name = dwarf2_name (imported_die, imported_cu);
27aa8d6a
SW
10912 if (imported_name == NULL)
10913 {
10914 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
10915
dda83cd7
SM
10916 The import in the following code:
10917 namespace A
10918 {
10919 typedef int B;
10920 }
10921
10922 int main ()
10923 {
10924 using A::B;
10925 B b;
10926 return b;
10927 }
10928
10929 ...
10930 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
10931 <52> DW_AT_decl_file : 1
10932 <53> DW_AT_decl_line : 6
10933 <54> DW_AT_import : <0x75>
10934 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
10935 <59> DW_AT_name : B
10936 <5b> DW_AT_decl_file : 1
10937 <5c> DW_AT_decl_line : 2
10938 <5d> DW_AT_type : <0x6e>
10939 ...
10940 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
10941 <76> DW_AT_byte_size : 4
10942 <77> DW_AT_encoding : 5 (signed)
10943
10944 imports the wrong die ( 0x75 instead of 0x58 ).
10945 This case will be ignored until the gcc bug is fixed. */
27aa8d6a
SW
10946 return;
10947 }
10948
82856980
SW
10949 /* Figure out the local name after import. */
10950 import_alias = dwarf2_name (die, cu);
27aa8d6a 10951
794684b6
SW
10952 /* Figure out where the statement is being imported to. */
10953 import_prefix = determine_prefix (die, cu);
10954
10955 /* Figure out what the scope of the imported die is and prepend it
10956 to the name of the imported die. */
de4affc9 10957 imported_name_prefix = determine_prefix (imported_die, imported_cu);
794684b6 10958
f55ee35c
JK
10959 if (imported_die->tag != DW_TAG_namespace
10960 && imported_die->tag != DW_TAG_module)
794684b6 10961 {
13387711
SW
10962 imported_declaration = imported_name;
10963 canonical_name = imported_name_prefix;
794684b6 10964 }
13387711 10965 else if (strlen (imported_name_prefix) > 0)
12aaed36 10966 canonical_name = obconcat (&objfile->objfile_obstack,
45280282
IB
10967 imported_name_prefix,
10968 (cu->language == language_d ? "." : "::"),
10969 imported_name, (char *) NULL);
13387711
SW
10970 else
10971 canonical_name = imported_name;
794684b6 10972
32019081
JK
10973 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
10974 for (child_die = die->child; child_die && child_die->tag;
436c571c 10975 child_die = child_die->sibling)
32019081
JK
10976 {
10977 /* DWARF-4: A Fortran use statement with a “rename list” may be
10978 represented by an imported module entry with an import attribute
10979 referring to the module and owned entries corresponding to those
10980 entities that are renamed as part of being imported. */
10981
10982 if (child_die->tag != DW_TAG_imported_declaration)
10983 {
b98664d3 10984 complaint (_("child DW_TAG_imported_declaration expected "
9d8780f0
SM
10985 "- DIE at %s [in module %s]"),
10986 sect_offset_str (child_die->sect_off),
10987 objfile_name (objfile));
32019081
JK
10988 continue;
10989 }
10990
10991 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
10992 if (import_attr == NULL)
10993 {
b98664d3 10994 complaint (_("Tag '%s' has no DW_AT_import"),
32019081
JK
10995 dwarf_tag_name (child_die->tag));
10996 continue;
10997 }
10998
10999 imported_cu = cu;
11000 imported_die = follow_die_ref_or_sig (child_die, import_attr,
11001 &imported_cu);
11002 imported_name = dwarf2_name (imported_die, imported_cu);
11003 if (imported_name == NULL)
11004 {
b98664d3 11005 complaint (_("child DW_TAG_imported_declaration has unknown "
9d8780f0
SM
11006 "imported name - DIE at %s [in module %s]"),
11007 sect_offset_str (child_die->sect_off),
11008 objfile_name (objfile));
32019081
JK
11009 continue;
11010 }
11011
eb1e02fd 11012 excludes.push_back (imported_name);
32019081
JK
11013
11014 process_die (child_die, cu);
11015 }
11016
804d2729 11017 add_using_directive (using_directives (cu),
22cee43f
PMR
11018 import_prefix,
11019 canonical_name,
11020 import_alias,
11021 imported_declaration,
11022 excludes,
11023 0,
11024 &objfile->objfile_obstack);
27aa8d6a
SW
11025}
11026
5230b05a
WT
11027/* ICC<14 does not output the required DW_AT_declaration on incomplete
11028 types, but gives them a size of zero. Starting with version 14,
11029 ICC is compatible with GCC. */
11030
9068261f 11031static bool
5230b05a
WT
11032producer_is_icc_lt_14 (struct dwarf2_cu *cu)
11033{
11034 if (!cu->checked_producer)
11035 check_producer (cu);
11036
11037 return cu->producer_is_icc_lt_14;
11038}
11039
eb77c9df
AB
11040/* ICC generates a DW_AT_type for C void functions. This was observed on
11041 ICC 14.0.5.212, and appears to be against the DWARF spec (V5 3.3.2)
11042 which says that void functions should not have a DW_AT_type. */
11043
11044static bool
11045producer_is_icc (struct dwarf2_cu *cu)
11046{
11047 if (!cu->checked_producer)
11048 check_producer (cu);
11049
11050 return cu->producer_is_icc;
11051}
11052
1b80a9fa
JK
11053/* Check for possibly missing DW_AT_comp_dir with relative .debug_line
11054 directory paths. GCC SVN r127613 (new option -fdebug-prefix-map) fixed
11055 this, it was first present in GCC release 4.3.0. */
11056
9068261f 11057static bool
1b80a9fa
JK
11058producer_is_gcc_lt_4_3 (struct dwarf2_cu *cu)
11059{
11060 if (!cu->checked_producer)
11061 check_producer (cu);
11062
11063 return cu->producer_is_gcc_lt_4_3;
11064}
11065
d721ba37
PA
11066static file_and_directory
11067find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu)
9291a0cd 11068{
d721ba37
PA
11069 file_and_directory res;
11070
9291a0cd
TT
11071 /* Find the filename. Do not use dwarf2_name here, since the filename
11072 is not a source language identifier. */
d721ba37
PA
11073 res.name = dwarf2_string_attr (die, DW_AT_name, cu);
11074 res.comp_dir = dwarf2_string_attr (die, DW_AT_comp_dir, cu);
9291a0cd 11075
d721ba37
PA
11076 if (res.comp_dir == NULL
11077 && producer_is_gcc_lt_4_3 (cu) && res.name != NULL
11078 && IS_ABSOLUTE_PATH (res.name))
9291a0cd 11079 {
d721ba37
PA
11080 res.comp_dir_storage = ldirname (res.name);
11081 if (!res.comp_dir_storage.empty ())
11082 res.comp_dir = res.comp_dir_storage.c_str ();
9291a0cd 11083 }
d721ba37 11084 if (res.comp_dir != NULL)
9291a0cd
TT
11085 {
11086 /* Irix 6.2 native cc prepends <machine>.: to the compilation
11087 directory, get rid of it. */
d721ba37 11088 const char *cp = strchr (res.comp_dir, ':');
9291a0cd 11089
d721ba37
PA
11090 if (cp && cp != res.comp_dir && cp[-1] == '.' && cp[1] == '/')
11091 res.comp_dir = cp + 1;
9291a0cd
TT
11092 }
11093
d721ba37
PA
11094 if (res.name == NULL)
11095 res.name = "<unknown>";
11096
11097 return res;
9291a0cd
TT
11098}
11099
f4dc4d17
DE
11100/* Handle DW_AT_stmt_list for a compilation unit.
11101 DIE is the DW_TAG_compile_unit die for CU.
c3b7b696
YQ
11102 COMP_DIR is the compilation directory. LOWPC is passed to
11103 dwarf_decode_lines. See dwarf_decode_lines comments about it. */
2ab95328
TT
11104
11105static void
11106handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
c3b7b696 11107 const char *comp_dir, CORE_ADDR lowpc) /* ARI: editCase function */
2ab95328 11108{
976ca316 11109 dwarf2_per_objfile *per_objfile = cu->per_objfile;
2ab95328 11110 struct attribute *attr;
527f3840
JK
11111 struct line_header line_header_local;
11112 hashval_t line_header_local_hash;
527f3840
JK
11113 void **slot;
11114 int decode_mapping;
2ab95328 11115
f4dc4d17
DE
11116 gdb_assert (! cu->per_cu->is_debug_types);
11117
2ab95328 11118 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
d4df075e 11119 if (attr == NULL || !attr->form_is_unsigned ())
527f3840
JK
11120 return;
11121
d4df075e 11122 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
527f3840
JK
11123
11124 /* The line header hash table is only created if needed (it exists to
11125 prevent redundant reading of the line table for partial_units).
11126 If we're given a partial_unit, we'll need it. If we're given a
11127 compile_unit, then use the line header hash table if it's already
11128 created, but don't create one just yet. */
11129
976ca316 11130 if (per_objfile->line_header_hash == NULL
527f3840 11131 && die->tag == DW_TAG_partial_unit)
2ab95328 11132 {
976ca316 11133 per_objfile->line_header_hash
d15acc42
TT
11134 .reset (htab_create_alloc (127, line_header_hash_voidp,
11135 line_header_eq_voidp,
11136 free_line_header_voidp,
11137 xcalloc, xfree));
527f3840 11138 }
2ab95328 11139
9c541725 11140 line_header_local.sect_off = line_offset;
527f3840
JK
11141 line_header_local.offset_in_dwz = cu->per_cu->is_dwz;
11142 line_header_local_hash = line_header_hash (&line_header_local);
976ca316 11143 if (per_objfile->line_header_hash != NULL)
527f3840 11144 {
976ca316 11145 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11146 &line_header_local,
11147 line_header_local_hash, NO_INSERT);
11148
11149 /* For DW_TAG_compile_unit we need info like symtab::linetable which
11150 is not present in *SLOT (since if there is something in *SLOT then
11151 it will be for a partial_unit). */
11152 if (die->tag == DW_TAG_partial_unit && slot != NULL)
dee91e82 11153 {
527f3840 11154 gdb_assert (*slot != NULL);
9a3c8263 11155 cu->line_header = (struct line_header *) *slot;
527f3840 11156 return;
dee91e82 11157 }
2ab95328 11158 }
527f3840
JK
11159
11160 /* dwarf_decode_line_header does not yet provide sufficient information.
11161 We always have to call also dwarf_decode_lines for it. */
fff8551c
PA
11162 line_header_up lh = dwarf_decode_line_header (line_offset, cu);
11163 if (lh == NULL)
527f3840 11164 return;
4c8aa72d
PA
11165
11166 cu->line_header = lh.release ();
11167 cu->line_header_die_owner = die;
527f3840 11168
976ca316 11169 if (per_objfile->line_header_hash == NULL)
527f3840
JK
11170 slot = NULL;
11171 else
11172 {
976ca316 11173 slot = htab_find_slot_with_hash (per_objfile->line_header_hash.get (),
527f3840
JK
11174 &line_header_local,
11175 line_header_local_hash, INSERT);
11176 gdb_assert (slot != NULL);
11177 }
11178 if (slot != NULL && *slot == NULL)
11179 {
11180 /* This newly decoded line number information unit will be owned
11181 by line_header_hash hash table. */
11182 *slot = cu->line_header;
4c8aa72d 11183 cu->line_header_die_owner = NULL;
527f3840
JK
11184 }
11185 else
11186 {
11187 /* We cannot free any current entry in (*slot) as that struct line_header
dda83cd7 11188 may be already used by multiple CUs. Create only temporary decoded
527f3840
JK
11189 line_header for this CU - it may happen at most once for each line
11190 number information unit. And if we're not using line_header_hash
11191 then this is what we want as well. */
11192 gdb_assert (die->tag != DW_TAG_partial_unit);
527f3840
JK
11193 }
11194 decode_mapping = (die->tag != DW_TAG_partial_unit);
11195 dwarf_decode_lines (cu->line_header, comp_dir, cu, NULL, lowpc,
11196 decode_mapping);
fff8551c 11197
2ab95328
TT
11198}
11199
95554aad 11200/* Process DW_TAG_compile_unit or DW_TAG_partial_unit. */
ae2de4f8 11201
c906108c 11202static void
e7c27a73 11203read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 11204{
976ca316
SM
11205 dwarf2_per_objfile *per_objfile = cu->per_objfile;
11206 struct objfile *objfile = per_objfile->objfile;
08feed99 11207 struct gdbarch *gdbarch = objfile->arch ();
2acceee2 11208 CORE_ADDR lowpc = ((CORE_ADDR) -1);
c906108c
SS
11209 CORE_ADDR highpc = ((CORE_ADDR) 0);
11210 struct attribute *attr;
c906108c 11211 struct die_info *child_die;
e142c38c 11212 CORE_ADDR baseaddr;
6e70227d 11213
380618d6 11214 prepare_one_comp_unit (cu, die, cu->language);
b3b3bada 11215 baseaddr = objfile->text_section_offset ();
c906108c 11216
fae299cd 11217 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
c906108c
SS
11218
11219 /* If we didn't find a lowpc, set it to highpc to avoid complaints
11220 from finish_block. */
2acceee2 11221 if (lowpc == ((CORE_ADDR) -1))
c906108c 11222 lowpc = highpc;
3e29f34a 11223 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
c906108c 11224
d721ba37 11225 file_and_directory fnd = find_file_and_directory (die, cu);
e1024ff1 11226
f4b8a18d
KW
11227 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
11228 standardised yet. As a workaround for the language detection we fall
11229 back to the DW_AT_producer string. */
11230 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
11231 cu->language = language_opencl;
11232
3019eac3
DE
11233 /* Similar hack for Go. */
11234 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
11235 set_cu_language (DW_LANG_Go, cu);
11236
c24bdb02 11237 cu->start_symtab (fnd.name, fnd.comp_dir, lowpc);
3019eac3
DE
11238
11239 /* Decode line number information if present. We do this before
11240 processing child DIEs, so that the line header table is available
11241 for DW_AT_decl_file. */
d721ba37 11242 handle_DW_AT_stmt_list (die, cu, fnd.comp_dir, lowpc);
3019eac3
DE
11243
11244 /* Process all dies in compilation unit. */
11245 if (die->child != NULL)
11246 {
11247 child_die = die->child;
11248 while (child_die && child_die->tag)
11249 {
11250 process_die (child_die, cu);
436c571c 11251 child_die = child_die->sibling;
3019eac3
DE
11252 }
11253 }
11254
11255 /* Decode macro information, if present. Dwarf 2 macro information
11256 refers to information in the line number info statement program
11257 header, so we can only read it if we've read the header
11258 successfully. */
0af92d60
JK
11259 attr = dwarf2_attr (die, DW_AT_macros, cu);
11260 if (attr == NULL)
11261 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
529908cb 11262 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3
DE
11263 {
11264 if (dwarf2_attr (die, DW_AT_macro_info, cu))
b98664d3 11265 complaint (_("CU refers to both DW_AT_macros and DW_AT_macro_info"));
3019eac3 11266
529908cb 11267 dwarf_decode_macros (cu, attr->as_unsigned (), 1);
3019eac3
DE
11268 }
11269 else
11270 {
11271 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
529908cb 11272 if (attr != nullptr && attr->form_is_unsigned () && cu->line_header)
3019eac3 11273 {
529908cb 11274 unsigned int macro_offset = attr->as_unsigned ();
3019eac3 11275
43f3e411 11276 dwarf_decode_macros (cu, macro_offset, 0);
3019eac3
DE
11277 }
11278 }
3019eac3
DE
11279}
11280
c24bdb02
KS
11281void
11282dwarf2_cu::setup_type_unit_groups (struct die_info *die)
3019eac3 11283{
f4dc4d17
DE
11284 struct type_unit_group *tu_group;
11285 int first_time;
3019eac3 11286 struct attribute *attr;
9c541725 11287 unsigned int i;
0186c6a7 11288 struct signatured_type *sig_type;
3019eac3 11289
f4dc4d17 11290 gdb_assert (per_cu->is_debug_types);
0186c6a7 11291 sig_type = (struct signatured_type *) per_cu;
3019eac3 11292
c24bdb02 11293 attr = dwarf2_attr (die, DW_AT_stmt_list, this);
3019eac3 11294
f4dc4d17 11295 /* If we're using .gdb_index (includes -readnow) then
74e04d1c 11296 per_cu->type_unit_group may not have been set up yet. */
0186c6a7 11297 if (sig_type->type_unit_group == NULL)
c24bdb02 11298 sig_type->type_unit_group = get_type_unit_group (this, attr);
0186c6a7 11299 tu_group = sig_type->type_unit_group;
f4dc4d17
DE
11300
11301 /* If we've already processed this stmt_list there's no real need to
11302 do it again, we could fake it and just recreate the part we need
11303 (file name,index -> symtab mapping). If data shows this optimization
11304 is useful we can do it then. */
8adb8487
TT
11305 type_unit_group_unshareable *tug_unshare
11306 = per_objfile->get_type_unit_group_unshareable (tu_group);
11307 first_time = tug_unshare->compunit_symtab == NULL;
f4dc4d17
DE
11308
11309 /* We have to handle the case of both a missing DW_AT_stmt_list or bad
11310 debug info. */
fff8551c 11311 line_header_up lh;
d4df075e 11312 if (attr != NULL && attr->form_is_unsigned ())
3019eac3 11313 {
d4df075e 11314 sect_offset line_offset = (sect_offset) attr->as_unsigned ();
c24bdb02 11315 lh = dwarf_decode_line_header (line_offset, this);
f4dc4d17
DE
11316 }
11317 if (lh == NULL)
11318 {
11319 if (first_time)
c24bdb02 11320 start_symtab ("", NULL, 0);
f4dc4d17
DE
11321 else
11322 {
8adb8487 11323 gdb_assert (tug_unshare->symtabs == NULL);
c24bdb02 11324 gdb_assert (m_builder == nullptr);
8adb8487 11325 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11326 m_builder.reset (new struct buildsym_compunit
11327 (COMPUNIT_OBJFILE (cust), "",
11328 COMPUNIT_DIRNAME (cust),
11329 compunit_language (cust),
11330 0, cust));
770479f2 11331 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11332 }
f4dc4d17 11333 return;
3019eac3
DE
11334 }
11335
c24bdb02
KS
11336 line_header = lh.release ();
11337 line_header_die_owner = die;
3019eac3 11338
f4dc4d17
DE
11339 if (first_time)
11340 {
c24bdb02 11341 struct compunit_symtab *cust = start_symtab ("", NULL, 0);
3019eac3 11342
1fd60fc0
DE
11343 /* Note: We don't assign tu_group->compunit_symtab yet because we're
11344 still initializing it, and our caller (a few levels up)
11345 process_full_type_unit still needs to know if this is the first
11346 time. */
11347
8adb8487 11348 tug_unshare->symtabs
4ac93832
TT
11349 = XOBNEWVEC (&COMPUNIT_OBJFILE (cust)->objfile_obstack,
11350 struct symtab *, line_header->file_names_size ());
3019eac3 11351
7ba99d21
AT
11352 auto &file_names = line_header->file_names ();
11353 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11354 {
7ba99d21 11355 file_entry &fe = file_names[i];
c24bdb02
KS
11356 dwarf2_start_subfile (this, fe.name,
11357 fe.include_dir (line_header));
11358 buildsym_compunit *b = get_builder ();
11359 if (b->get_current_subfile ()->symtab == NULL)
f4dc4d17 11360 {
4c8aa72d
PA
11361 /* NOTE: start_subfile will recognize when it's been
11362 passed a file it has already seen. So we can't
11363 assume there's a simple mapping from
11364 cu->line_header->file_names to subfiles, plus
11365 cu->line_header->file_names may contain dups. */
c24bdb02
KS
11366 b->get_current_subfile ()->symtab
11367 = allocate_symtab (cust, b->get_current_subfile ()->name);
f4dc4d17
DE
11368 }
11369
c24bdb02 11370 fe.symtab = b->get_current_subfile ()->symtab;
8adb8487 11371 tug_unshare->symtabs[i] = fe.symtab;
f4dc4d17
DE
11372 }
11373 }
11374 else
3019eac3 11375 {
c24bdb02 11376 gdb_assert (m_builder == nullptr);
8adb8487 11377 struct compunit_symtab *cust = tug_unshare->compunit_symtab;
c24bdb02
KS
11378 m_builder.reset (new struct buildsym_compunit
11379 (COMPUNIT_OBJFILE (cust), "",
11380 COMPUNIT_DIRNAME (cust),
11381 compunit_language (cust),
11382 0, cust));
770479f2 11383 list_in_scope = get_builder ()->get_file_symbols ();
f4dc4d17 11384
7ba99d21
AT
11385 auto &file_names = line_header->file_names ();
11386 for (i = 0; i < file_names.size (); ++i)
f4dc4d17 11387 {
7ba99d21 11388 file_entry &fe = file_names[i];
8adb8487 11389 fe.symtab = tug_unshare->symtabs[i];
f4dc4d17 11390 }
3019eac3
DE
11391 }
11392
f4dc4d17
DE
11393 /* The main symtab is allocated last. Type units don't have DW_AT_name
11394 so they don't have a "real" (so to speak) symtab anyway.
11395 There is later code that will assign the main symtab to all symbols
11396 that don't have one. We need to handle the case of a symbol with a
11397 missing symtab (DW_AT_decl_file) anyway. */
11398}
3019eac3 11399
f4dc4d17
DE
11400/* Process DW_TAG_type_unit.
11401 For TUs we want to skip the first top level sibling if it's not the
11402 actual type being defined by this TU. In this case the first top
11403 level sibling is there to provide context only. */
3019eac3 11404
f4dc4d17
DE
11405static void
11406read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
11407{
11408 struct die_info *child_die;
3019eac3 11409
f4dc4d17
DE
11410 prepare_one_comp_unit (cu, die, language_minimal);
11411
11412 /* Initialize (or reinitialize) the machinery for building symtabs.
11413 We do this before processing child DIEs, so that the line header table
11414 is available for DW_AT_decl_file. */
c24bdb02 11415 cu->setup_type_unit_groups (die);
f4dc4d17
DE
11416
11417 if (die->child != NULL)
11418 {
11419 child_die = die->child;
11420 while (child_die && child_die->tag)
11421 {
11422 process_die (child_die, cu);
436c571c 11423 child_die = child_die->sibling;
f4dc4d17
DE
11424 }
11425 }
3019eac3
DE
11426}
11427\f
80626a55
DE
11428/* DWO/DWP files.
11429
11430 http://gcc.gnu.org/wiki/DebugFission
11431 http://gcc.gnu.org/wiki/DebugFissionDWP
11432
11433 To simplify handling of both DWO files ("object" files with the DWARF info)
11434 and DWP files (a file with the DWOs packaged up into one file), we treat
11435 DWP files as having a collection of virtual DWO files. */
3019eac3
DE
11436
11437static hashval_t
11438hash_dwo_file (const void *item)
11439{
9a3c8263 11440 const struct dwo_file *dwo_file = (const struct dwo_file *) item;
a2ce51a0 11441 hashval_t hash;
3019eac3 11442
a2ce51a0
DE
11443 hash = htab_hash_string (dwo_file->dwo_name);
11444 if (dwo_file->comp_dir != NULL)
11445 hash += htab_hash_string (dwo_file->comp_dir);
11446 return hash;
3019eac3
DE
11447}
11448
11449static int
11450eq_dwo_file (const void *item_lhs, const void *item_rhs)
11451{
9a3c8263
SM
11452 const struct dwo_file *lhs = (const struct dwo_file *) item_lhs;
11453 const struct dwo_file *rhs = (const struct dwo_file *) item_rhs;
3019eac3 11454
a2ce51a0
DE
11455 if (strcmp (lhs->dwo_name, rhs->dwo_name) != 0)
11456 return 0;
11457 if (lhs->comp_dir == NULL || rhs->comp_dir == NULL)
11458 return lhs->comp_dir == rhs->comp_dir;
11459 return strcmp (lhs->comp_dir, rhs->comp_dir) == 0;
3019eac3
DE
11460}
11461
11462/* Allocate a hash table for DWO files. */
11463
51ac9db5 11464static htab_up
298e9637 11465allocate_dwo_file_hash_table ()
3019eac3 11466{
51ac9db5
SM
11467 auto delete_dwo_file = [] (void *item)
11468 {
11469 struct dwo_file *dwo_file = (struct dwo_file *) item;
11470
11471 delete dwo_file;
11472 };
11473
bc68fb19
TT
11474 return htab_up (htab_create_alloc (41,
11475 hash_dwo_file,
11476 eq_dwo_file,
11477 delete_dwo_file,
11478 xcalloc, xfree));
3019eac3
DE
11479}
11480
80626a55
DE
11481/* Lookup DWO file DWO_NAME. */
11482
11483static void **
976ca316 11484lookup_dwo_file_slot (dwarf2_per_objfile *per_objfile,
ed2dc618
SM
11485 const char *dwo_name,
11486 const char *comp_dir)
80626a55
DE
11487{
11488 struct dwo_file find_entry;
11489 void **slot;
11490
976ca316
SM
11491 if (per_objfile->per_bfd->dwo_files == NULL)
11492 per_objfile->per_bfd->dwo_files = allocate_dwo_file_hash_table ();
80626a55 11493
0ac5b59e
DE
11494 find_entry.dwo_name = dwo_name;
11495 find_entry.comp_dir = comp_dir;
976ca316 11496 slot = htab_find_slot (per_objfile->per_bfd->dwo_files.get (), &find_entry,
51ac9db5 11497 INSERT);
80626a55
DE
11498
11499 return slot;
11500}
11501
3019eac3
DE
11502static hashval_t
11503hash_dwo_unit (const void *item)
11504{
9a3c8263 11505 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3
DE
11506
11507 /* This drops the top 32 bits of the id, but is ok for a hash. */
11508 return dwo_unit->signature;
11509}
11510
11511static int
11512eq_dwo_unit (const void *item_lhs, const void *item_rhs)
11513{
9a3c8263
SM
11514 const struct dwo_unit *lhs = (const struct dwo_unit *) item_lhs;
11515 const struct dwo_unit *rhs = (const struct dwo_unit *) item_rhs;
3019eac3
DE
11516
11517 /* The signature is assumed to be unique within the DWO file.
11518 So while object file CU dwo_id's always have the value zero,
11519 that's OK, assuming each object file DWO file has only one CU,
11520 and that's the rule for now. */
11521 return lhs->signature == rhs->signature;
11522}
11523
11524/* Allocate a hash table for DWO CUs,TUs.
11525 There is one of these tables for each of CUs,TUs for each DWO file. */
11526
b0b6a987 11527static htab_up
298e9637 11528allocate_dwo_unit_table ()
3019eac3
DE
11529{
11530 /* Start out with a pretty small number.
11531 Generally DWO files contain only one CU and maybe some TUs. */
b0b6a987
TT
11532 return htab_up (htab_create_alloc (3,
11533 hash_dwo_unit,
11534 eq_dwo_unit,
11535 NULL, xcalloc, xfree));
3019eac3
DE
11536}
11537
19c3d4c9 11538/* die_reader_func for create_dwo_cu. */
3019eac3
DE
11539
11540static void
19c3d4c9
DE
11541create_dwo_cu_reader (const struct die_reader_specs *reader,
11542 const gdb_byte *info_ptr,
11543 struct die_info *comp_unit_die,
c0ab21c2
TT
11544 struct dwo_file *dwo_file,
11545 struct dwo_unit *dwo_unit)
3019eac3
DE
11546{
11547 struct dwarf2_cu *cu = reader->cu;
9c541725 11548 sect_offset sect_off = cu->per_cu->sect_off;
8a0459fd 11549 struct dwarf2_section_info *section = cu->per_cu->section;
3019eac3 11550
a084a2a6
AT
11551 gdb::optional<ULONGEST> signature = lookup_dwo_id (cu, comp_unit_die);
11552 if (!signature.has_value ())
3019eac3 11553 {
b98664d3 11554 complaint (_("Dwarf Error: debug entry at offset %s is missing"
19c3d4c9 11555 " its dwo_id [in module %s]"),
9d8780f0 11556 sect_offset_str (sect_off), dwo_file->dwo_name);
3019eac3
DE
11557 return;
11558 }
11559
3019eac3 11560 dwo_unit->dwo_file = dwo_file;
a084a2a6 11561 dwo_unit->signature = *signature;
8a0459fd 11562 dwo_unit->section = section;
9c541725 11563 dwo_unit->sect_off = sect_off;
3019eac3
DE
11564 dwo_unit->length = cu->per_cu->length;
11565
6f738b01
SM
11566 dwarf_read_debug_printf (" offset %s, dwo_id %s",
11567 sect_offset_str (sect_off),
11568 hex_string (dwo_unit->signature));
3019eac3
DE
11569}
11570
33c5cd75 11571/* Create the dwo_units for the CUs in a DWO_FILE.
19c3d4c9 11572 Note: This function processes DWO files only, not DWP files. */
3019eac3 11573
33c5cd75 11574static void
976ca316 11575create_cus_hash_table (dwarf2_per_objfile *per_objfile,
18a8505e 11576 dwarf2_cu *cu, struct dwo_file &dwo_file,
b0b6a987 11577 dwarf2_section_info &section, htab_up &cus_htab)
3019eac3 11578{
976ca316
SM
11579 struct objfile *objfile = per_objfile->objfile;
11580 dwarf2_per_bfd *per_bfd = per_objfile->per_bfd;
d521ce57 11581 const gdb_byte *info_ptr, *end_ptr;
3019eac3 11582
96b79293 11583 section.read (objfile);
33c5cd75 11584 info_ptr = section.buffer;
3019eac3
DE
11585
11586 if (info_ptr == NULL)
33c5cd75 11587 return;
3019eac3 11588
6f738b01
SM
11589 dwarf_read_debug_printf ("Reading %s for %s:",
11590 section.get_name (),
11591 section.get_file_name ());
3019eac3 11592
33c5cd75 11593 end_ptr = info_ptr + section.size;
3019eac3
DE
11594 while (info_ptr < end_ptr)
11595 {
11596 struct dwarf2_per_cu_data per_cu;
c0ab21c2 11597 struct dwo_unit read_unit {};
33c5cd75
DB
11598 struct dwo_unit *dwo_unit;
11599 void **slot;
11600 sect_offset sect_off = (sect_offset) (info_ptr - section.buffer);
3019eac3
DE
11601
11602 memset (&per_cu, 0, sizeof (per_cu));
1859c670 11603 per_cu.per_bfd = per_bfd;
3019eac3 11604 per_cu.is_debug_types = 0;
33c5cd75
DB
11605 per_cu.sect_off = sect_offset (info_ptr - section.buffer);
11606 per_cu.section = &section;
11607
976ca316 11608 cutu_reader reader (&per_cu, per_objfile, cu, &dwo_file);
c0ab21c2
TT
11609 if (!reader.dummy_p)
11610 create_dwo_cu_reader (&reader, reader.info_ptr, reader.comp_unit_die,
3e225074 11611 &dwo_file, &read_unit);
33c5cd75
DB
11612 info_ptr += per_cu.length;
11613
11614 // If the unit could not be parsed, skip it.
c0ab21c2 11615 if (read_unit.dwo_file == NULL)
33c5cd75 11616 continue;
3019eac3 11617
33c5cd75 11618 if (cus_htab == NULL)
298e9637 11619 cus_htab = allocate_dwo_unit_table ();
19c3d4c9 11620
1859c670 11621 dwo_unit = OBSTACK_ZALLOC (&per_bfd->obstack,
45940949 11622 struct dwo_unit);
c0ab21c2 11623 *dwo_unit = read_unit;
b0b6a987 11624 slot = htab_find_slot (cus_htab.get (), dwo_unit, INSERT);
33c5cd75
DB
11625 gdb_assert (slot != NULL);
11626 if (*slot != NULL)
19c3d4c9 11627 {
33c5cd75
DB
11628 const struct dwo_unit *dup_cu = (const struct dwo_unit *)*slot;
11629 sect_offset dup_sect_off = dup_cu->sect_off;
19c3d4c9 11630
b98664d3 11631 complaint (_("debug cu entry at offset %s is duplicate to"
9d8780f0
SM
11632 " the entry at offset %s, signature %s"),
11633 sect_offset_str (sect_off), sect_offset_str (dup_sect_off),
33c5cd75 11634 hex_string (dwo_unit->signature));
19c3d4c9 11635 }
33c5cd75 11636 *slot = (void *)dwo_unit;
3019eac3 11637 }
3019eac3
DE
11638}
11639
80626a55
DE
11640/* DWP file .debug_{cu,tu}_index section format:
11641 [ref: http://gcc.gnu.org/wiki/DebugFissionDWP]
d2854d8d
CT
11642 [ref: http://dwarfstd.org/doc/DWARF5.pdf, sect 7.3.5 "DWARF Package Files"]
11643
11644 DWP Versions 1 & 2 are older, pre-standard format versions. The first
11645 officially standard DWP format was published with DWARF v5 and is called
11646 Version 5. There are no versions 3 or 4.
80626a55 11647
d2415c6c
DE
11648 DWP Version 1:
11649
80626a55
DE
11650 Both index sections have the same format, and serve to map a 64-bit
11651 signature to a set of section numbers. Each section begins with a header,
11652 followed by a hash table of 64-bit signatures, a parallel table of 32-bit
11653 indexes, and a pool of 32-bit section numbers. The index sections will be
11654 aligned at 8-byte boundaries in the file.
11655
d2415c6c
DE
11656 The index section header consists of:
11657
11658 V, 32 bit version number
11659 -, 32 bits unused
11660 N, 32 bit number of compilation units or type units in the index
11661 M, 32 bit number of slots in the hash table
80626a55 11662
d2415c6c 11663 Numbers are recorded using the byte order of the application binary.
80626a55 11664
d2415c6c
DE
11665 The hash table begins at offset 16 in the section, and consists of an array
11666 of M 64-bit slots. Each slot contains a 64-bit signature (using the byte
11667 order of the application binary). Unused slots in the hash table are 0.
11668 (We rely on the extreme unlikeliness of a signature being exactly 0.)
80626a55 11669
d2415c6c
DE
11670 The parallel table begins immediately after the hash table
11671 (at offset 16 + 8 * M from the beginning of the section), and consists of an
11672 array of 32-bit indexes (using the byte order of the application binary),
11673 corresponding 1-1 with slots in the hash table. Each entry in the parallel
11674 table contains a 32-bit index into the pool of section numbers. For unused
11675 hash table slots, the corresponding entry in the parallel table will be 0.
80626a55 11676
73869dc2
DE
11677 The pool of section numbers begins immediately following the hash table
11678 (at offset 16 + 12 * M from the beginning of the section). The pool of
11679 section numbers consists of an array of 32-bit words (using the byte order
11680 of the application binary). Each item in the array is indexed starting
11681 from 0. The hash table entry provides the index of the first section
11682 number in the set. Additional section numbers in the set follow, and the
11683 set is terminated by a 0 entry (section number 0 is not used in ELF).
11684
11685 In each set of section numbers, the .debug_info.dwo or .debug_types.dwo
11686 section must be the first entry in the set, and the .debug_abbrev.dwo must
11687 be the second entry. Other members of the set may follow in any order.
11688
11689 ---
11690
d2854d8d 11691 DWP Versions 2 and 5:
73869dc2 11692
d2854d8d 11693 DWP Versions 2 and 5 combine all the .debug_info, etc. sections into one,
73869dc2
DE
11694 and the entries in the index tables are now offsets into these sections.
11695 CU offsets begin at 0. TU offsets begin at the size of the .debug_info
11696 section.
11697
11698 Index Section Contents:
11699 Header
11700 Hash Table of Signatures dwp_hash_table.hash_table
11701 Parallel Table of Indices dwp_hash_table.unit_table
d2854d8d
CT
11702 Table of Section Offsets dwp_hash_table.{v2|v5}.{section_ids,offsets}
11703 Table of Section Sizes dwp_hash_table.{v2|v5}.sizes
73869dc2
DE
11704
11705 The index section header consists of:
11706
11707 V, 32 bit version number
11708 L, 32 bit number of columns in the table of section offsets
11709 N, 32 bit number of compilation units or type units in the index
11710 M, 32 bit number of slots in the hash table
11711
11712 Numbers are recorded using the byte order of the application binary.
11713
11714 The hash table has the same format as version 1.
11715 The parallel table of indices has the same format as version 1,
11716 except that the entries are origin-1 indices into the table of sections
11717 offsets and the table of section sizes.
11718
11719 The table of offsets begins immediately following the parallel table
11720 (at offset 16 + 12 * M from the beginning of the section). The table is
11721 a two-dimensional array of 32-bit words (using the byte order of the
11722 application binary), with L columns and N+1 rows, in row-major order.
11723 Each row in the array is indexed starting from 0. The first row provides
11724 a key to the remaining rows: each column in this row provides an identifier
11725 for a debug section, and the offsets in the same column of subsequent rows
d2854d8d 11726 refer to that section. The section identifiers for Version 2 are:
73869dc2
DE
11727
11728 DW_SECT_INFO 1 .debug_info.dwo
11729 DW_SECT_TYPES 2 .debug_types.dwo
11730 DW_SECT_ABBREV 3 .debug_abbrev.dwo
11731 DW_SECT_LINE 4 .debug_line.dwo
11732 DW_SECT_LOC 5 .debug_loc.dwo
11733 DW_SECT_STR_OFFSETS 6 .debug_str_offsets.dwo
11734 DW_SECT_MACINFO 7 .debug_macinfo.dwo
11735 DW_SECT_MACRO 8 .debug_macro.dwo
11736
d2854d8d
CT
11737 The section identifiers for Version 5 are:
11738
11739 DW_SECT_INFO_V5 1 .debug_info.dwo
11740 DW_SECT_RESERVED_V5 2 --
11741 DW_SECT_ABBREV_V5 3 .debug_abbrev.dwo
11742 DW_SECT_LINE_V5 4 .debug_line.dwo
11743 DW_SECT_LOCLISTS_V5 5 .debug_loclists.dwo
11744 DW_SECT_STR_OFFSETS_V5 6 .debug_str_offsets.dwo
11745 DW_SECT_MACRO_V5 7 .debug_macro.dwo
11746 DW_SECT_RNGLISTS_V5 8 .debug_rnglists.dwo
11747
73869dc2
DE
11748 The offsets provided by the CU and TU index sections are the base offsets
11749 for the contributions made by each CU or TU to the corresponding section
11750 in the package file. Each CU and TU header contains an abbrev_offset
11751 field, used to find the abbreviations table for that CU or TU within the
11752 contribution to the .debug_abbrev.dwo section for that CU or TU, and should
11753 be interpreted as relative to the base offset given in the index section.
11754 Likewise, offsets into .debug_line.dwo from DW_AT_stmt_list attributes
11755 should be interpreted as relative to the base offset for .debug_line.dwo,
11756 and offsets into other debug sections obtained from DWARF attributes should
11757 also be interpreted as relative to the corresponding base offset.
11758
11759 The table of sizes begins immediately following the table of offsets.
11760 Like the table of offsets, it is a two-dimensional array of 32-bit words,
11761 with L columns and N rows, in row-major order. Each row in the array is
11762 indexed starting from 1 (row 0 is shared by the two tables).
11763
11764 ---
11765
11766 Hash table lookup is handled the same in version 1 and 2:
11767
11768 We assume that N and M will not exceed 2^32 - 1.
11769 The size of the hash table, M, must be 2^k such that 2^k > 3*N/2.
11770
d2415c6c
DE
11771 Given a 64-bit compilation unit signature or a type signature S, an entry
11772 in the hash table is located as follows:
80626a55 11773
d2415c6c
DE
11774 1) Calculate a primary hash H = S & MASK(k), where MASK(k) is a mask with
11775 the low-order k bits all set to 1.
80626a55 11776
d2415c6c 11777 2) Calculate a secondary hash H' = (((S >> 32) & MASK(k)) | 1).
80626a55 11778
d2415c6c
DE
11779 3) If the hash table entry at index H matches the signature, use that
11780 entry. If the hash table entry at index H is unused (all zeroes),
11781 terminate the search: the signature is not present in the table.
80626a55 11782
d2415c6c 11783 4) Let H = (H + H') modulo M. Repeat at Step 3.
80626a55 11784
d2415c6c 11785 Because M > N and H' and M are relatively prime, the search is guaranteed
73869dc2 11786 to stop at an unused slot or find the match. */
80626a55
DE
11787
11788/* Create a hash table to map DWO IDs to their CU/TU entry in
11789 .debug_{info,types}.dwo in DWP_FILE.
11790 Returns NULL if there isn't one.
11791 Note: This function processes DWP files only, not DWO files. */
11792
11793static struct dwp_hash_table *
976ca316 11794create_dwp_hash_table (dwarf2_per_objfile *per_objfile,
ed2dc618 11795 struct dwp_file *dwp_file, int is_debug_types)
80626a55 11796{
976ca316 11797 struct objfile *objfile = per_objfile->objfile;
400174b1 11798 bfd *dbfd = dwp_file->dbfd.get ();
948f8e3d 11799 const gdb_byte *index_ptr, *index_end;
80626a55 11800 struct dwarf2_section_info *index;
73869dc2 11801 uint32_t version, nr_columns, nr_units, nr_slots;
80626a55
DE
11802 struct dwp_hash_table *htab;
11803
11804 if (is_debug_types)
11805 index = &dwp_file->sections.tu_index;
11806 else
11807 index = &dwp_file->sections.cu_index;
11808
96b79293 11809 if (index->empty ())
80626a55 11810 return NULL;
96b79293 11811 index->read (objfile);
80626a55
DE
11812
11813 index_ptr = index->buffer;
11814 index_end = index_ptr + index->size;
11815
d2854d8d
CT
11816 /* For Version 5, the version is really 2 bytes of data & 2 bytes of padding.
11817 For now it's safe to just read 4 bytes (particularly as it's difficult to
11818 tell if you're dealing with Version 5 before you've read the version). */
80626a55 11819 version = read_4_bytes (dbfd, index_ptr);
73869dc2 11820 index_ptr += 4;
d2854d8d 11821 if (version == 2 || version == 5)
73869dc2
DE
11822 nr_columns = read_4_bytes (dbfd, index_ptr);
11823 else
11824 nr_columns = 0;
11825 index_ptr += 4;
80626a55
DE
11826 nr_units = read_4_bytes (dbfd, index_ptr);
11827 index_ptr += 4;
11828 nr_slots = read_4_bytes (dbfd, index_ptr);
11829 index_ptr += 4;
11830
d2854d8d 11831 if (version != 1 && version != 2 && version != 5)
80626a55 11832 {
21aa081e 11833 error (_("Dwarf Error: unsupported DWP file version (%s)"
80626a55 11834 " [in module %s]"),
21aa081e 11835 pulongest (version), dwp_file->name);
80626a55
DE
11836 }
11837 if (nr_slots != (nr_slots & -nr_slots))
11838 {
21aa081e 11839 error (_("Dwarf Error: number of slots in DWP hash table (%s)"
80626a55 11840 " is not power of 2 [in module %s]"),
21aa081e 11841 pulongest (nr_slots), dwp_file->name);
80626a55
DE
11842 }
11843
976ca316 11844 htab = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwp_hash_table);
73869dc2
DE
11845 htab->version = version;
11846 htab->nr_columns = nr_columns;
80626a55
DE
11847 htab->nr_units = nr_units;
11848 htab->nr_slots = nr_slots;
11849 htab->hash_table = index_ptr;
11850 htab->unit_table = htab->hash_table + sizeof (uint64_t) * nr_slots;
73869dc2
DE
11851
11852 /* Exit early if the table is empty. */
11853 if (nr_slots == 0 || nr_units == 0
d2854d8d
CT
11854 || (version == 2 && nr_columns == 0)
11855 || (version == 5 && nr_columns == 0))
73869dc2
DE
11856 {
11857 /* All must be zero. */
11858 if (nr_slots != 0 || nr_units != 0
d2854d8d
CT
11859 || (version == 2 && nr_columns != 0)
11860 || (version == 5 && nr_columns != 0))
73869dc2 11861 {
b98664d3 11862 complaint (_("Empty DWP but nr_slots,nr_units,nr_columns not"
73869dc2
DE
11863 " all zero [in modules %s]"),
11864 dwp_file->name);
11865 }
11866 return htab;
11867 }
11868
11869 if (version == 1)
11870 {
11871 htab->section_pool.v1.indices =
11872 htab->unit_table + sizeof (uint32_t) * nr_slots;
11873 /* It's harder to decide whether the section is too small in v1.
11874 V1 is deprecated anyway so we punt. */
11875 }
d2854d8d 11876 else if (version == 2)
73869dc2
DE
11877 {
11878 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11879 int *ids = htab->section_pool.v2.section_ids;
04fd5eed 11880 size_t sizeof_ids = sizeof (htab->section_pool.v2.section_ids);
73869dc2
DE
11881 /* Reverse map for error checking. */
11882 int ids_seen[DW_SECT_MAX + 1];
11883 int i;
11884
11885 if (nr_columns < 2)
11886 {
11887 error (_("Dwarf Error: bad DWP hash table, too few columns"
11888 " in section table [in module %s]"),
11889 dwp_file->name);
11890 }
11891 if (nr_columns > MAX_NR_V2_DWO_SECTIONS)
11892 {
11893 error (_("Dwarf Error: bad DWP hash table, too many columns"
11894 " in section table [in module %s]"),
11895 dwp_file->name);
11896 }
04fd5eed
GB
11897 memset (ids, 255, sizeof_ids);
11898 memset (ids_seen, 255, sizeof (ids_seen));
73869dc2
DE
11899 for (i = 0; i < nr_columns; ++i)
11900 {
11901 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11902
11903 if (id < DW_SECT_MIN || id > DW_SECT_MAX)
11904 {
11905 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11906 " in section table [in module %s]"),
11907 id, dwp_file->name);
11908 }
11909 if (ids_seen[id] != -1)
11910 {
11911 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11912 " id %d in section table [in module %s]"),
11913 id, dwp_file->name);
11914 }
11915 ids_seen[id] = i;
11916 ids[i] = id;
11917 }
11918 /* Must have exactly one info or types section. */
11919 if (((ids_seen[DW_SECT_INFO] != -1)
11920 + (ids_seen[DW_SECT_TYPES] != -1))
11921 != 1)
11922 {
11923 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11924 " DWO info/types section [in module %s]"),
11925 dwp_file->name);
11926 }
11927 /* Must have an abbrev section. */
11928 if (ids_seen[DW_SECT_ABBREV] == -1)
11929 {
11930 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11931 " section [in module %s]"),
11932 dwp_file->name);
11933 }
11934 htab->section_pool.v2.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
11935 htab->section_pool.v2.sizes =
11936 htab->section_pool.v2.offsets + (sizeof (uint32_t)
11937 * nr_units * nr_columns);
11938 if ((htab->section_pool.v2.sizes + (sizeof (uint32_t)
11939 * nr_units * nr_columns))
11940 > index_end)
11941 {
11942 error (_("Dwarf Error: DWP index section is corrupt (too small)"
11943 " [in module %s]"),
11944 dwp_file->name);
11945 }
11946 }
d2854d8d
CT
11947 else /* version == 5 */
11948 {
11949 const gdb_byte *ids_ptr = htab->unit_table + sizeof (uint32_t) * nr_slots;
11950 int *ids = htab->section_pool.v5.section_ids;
11951 size_t sizeof_ids = sizeof (htab->section_pool.v5.section_ids);
11952 /* Reverse map for error checking. */
11953 int ids_seen[DW_SECT_MAX_V5 + 1];
11954
11955 if (nr_columns < 2)
11956 {
11957 error (_("Dwarf Error: bad DWP hash table, too few columns"
11958 " in section table [in module %s]"),
11959 dwp_file->name);
11960 }
11961 if (nr_columns > MAX_NR_V5_DWO_SECTIONS)
11962 {
11963 error (_("Dwarf Error: bad DWP hash table, too many columns"
11964 " in section table [in module %s]"),
11965 dwp_file->name);
11966 }
11967 memset (ids, 255, sizeof_ids);
11968 memset (ids_seen, 255, sizeof (ids_seen));
11969 for (int i = 0; i < nr_columns; ++i)
11970 {
11971 int id = read_4_bytes (dbfd, ids_ptr + i * sizeof (uint32_t));
11972
11973 if (id < DW_SECT_MIN || id > DW_SECT_MAX_V5)
11974 {
11975 error (_("Dwarf Error: bad DWP hash table, bad section id %d"
11976 " in section table [in module %s]"),
11977 id, dwp_file->name);
11978 }
11979 if (ids_seen[id] != -1)
11980 {
11981 error (_("Dwarf Error: bad DWP hash table, duplicate section"
11982 " id %d in section table [in module %s]"),
11983 id, dwp_file->name);
11984 }
11985 ids_seen[id] = i;
11986 ids[i] = id;
11987 }
11988 /* Must have seen an info section. */
11989 if (ids_seen[DW_SECT_INFO_V5] == -1)
11990 {
11991 error (_("Dwarf Error: bad DWP hash table, missing/duplicate"
11992 " DWO info/types section [in module %s]"),
11993 dwp_file->name);
11994 }
11995 /* Must have an abbrev section. */
11996 if (ids_seen[DW_SECT_ABBREV_V5] == -1)
11997 {
11998 error (_("Dwarf Error: bad DWP hash table, missing DWO abbrev"
11999 " section [in module %s]"),
12000 dwp_file->name);
12001 }
12002 htab->section_pool.v5.offsets = ids_ptr + sizeof (uint32_t) * nr_columns;
12003 htab->section_pool.v5.sizes
12004 = htab->section_pool.v5.offsets + (sizeof (uint32_t)
12005 * nr_units * nr_columns);
12006 if ((htab->section_pool.v5.sizes + (sizeof (uint32_t)
12007 * nr_units * nr_columns))
12008 > index_end)
12009 {
12010 error (_("Dwarf Error: DWP index section is corrupt (too small)"
12011 " [in module %s]"),
12012 dwp_file->name);
12013 }
12014 }
80626a55
DE
12015
12016 return htab;
12017}
12018
12019/* Update SECTIONS with the data from SECTP.
12020
5bb6e9dd
TT
12021 This function is like the other "locate" section routines, but in
12022 this context the sections to read comes from the DWP V1 hash table,
12023 not the full ELF section table.
80626a55
DE
12024
12025 The result is non-zero for success, or zero if an error was found. */
12026
12027static int
73869dc2
DE
12028locate_v1_virtual_dwo_sections (asection *sectp,
12029 struct virtual_v1_dwo_sections *sections)
80626a55
DE
12030{
12031 const struct dwop_section_names *names = &dwop_section_names;
12032
12033 if (section_is_p (sectp->name, &names->abbrev_dwo))
12034 {
12035 /* There can be only one. */
049412e3 12036 if (sections->abbrev.s.section != NULL)
80626a55 12037 return 0;
049412e3 12038 sections->abbrev.s.section = sectp;
fd361982 12039 sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12040 }
12041 else if (section_is_p (sectp->name, &names->info_dwo)
12042 || section_is_p (sectp->name, &names->types_dwo))
12043 {
12044 /* There can be only one. */
049412e3 12045 if (sections->info_or_types.s.section != NULL)
80626a55 12046 return 0;
049412e3 12047 sections->info_or_types.s.section = sectp;
fd361982 12048 sections->info_or_types.size = bfd_section_size (sectp);
80626a55
DE
12049 }
12050 else if (section_is_p (sectp->name, &names->line_dwo))
12051 {
12052 /* There can be only one. */
049412e3 12053 if (sections->line.s.section != NULL)
80626a55 12054 return 0;
049412e3 12055 sections->line.s.section = sectp;
fd361982 12056 sections->line.size = bfd_section_size (sectp);
80626a55
DE
12057 }
12058 else if (section_is_p (sectp->name, &names->loc_dwo))
12059 {
12060 /* There can be only one. */
049412e3 12061 if (sections->loc.s.section != NULL)
80626a55 12062 return 0;
049412e3 12063 sections->loc.s.section = sectp;
fd361982 12064 sections->loc.size = bfd_section_size (sectp);
80626a55
DE
12065 }
12066 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12067 {
12068 /* There can be only one. */
049412e3 12069 if (sections->macinfo.s.section != NULL)
80626a55 12070 return 0;
049412e3 12071 sections->macinfo.s.section = sectp;
fd361982 12072 sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12073 }
12074 else if (section_is_p (sectp->name, &names->macro_dwo))
12075 {
12076 /* There can be only one. */
049412e3 12077 if (sections->macro.s.section != NULL)
80626a55 12078 return 0;
049412e3 12079 sections->macro.s.section = sectp;
fd361982 12080 sections->macro.size = bfd_section_size (sectp);
80626a55
DE
12081 }
12082 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12083 {
12084 /* There can be only one. */
049412e3 12085 if (sections->str_offsets.s.section != NULL)
80626a55 12086 return 0;
049412e3 12087 sections->str_offsets.s.section = sectp;
fd361982 12088 sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12089 }
12090 else
12091 {
12092 /* No other kind of section is valid. */
12093 return 0;
12094 }
12095
12096 return 1;
12097}
12098
73869dc2
DE
12099/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12100 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12101 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12102 This is for DWP version 1 files. */
80626a55
DE
12103
12104static struct dwo_unit *
976ca316 12105create_dwo_unit_in_dwp_v1 (dwarf2_per_objfile *per_objfile,
ed2dc618 12106 struct dwp_file *dwp_file,
73869dc2
DE
12107 uint32_t unit_index,
12108 const char *comp_dir,
12109 ULONGEST signature, int is_debug_types)
80626a55 12110{
73869dc2
DE
12111 const struct dwp_hash_table *dwp_htab =
12112 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12113 bfd *dbfd = dwp_file->dbfd.get ();
80626a55
DE
12114 const char *kind = is_debug_types ? "TU" : "CU";
12115 struct dwo_file *dwo_file;
12116 struct dwo_unit *dwo_unit;
73869dc2 12117 struct virtual_v1_dwo_sections sections;
80626a55 12118 void **dwo_file_slot;
80626a55
DE
12119 int i;
12120
73869dc2
DE
12121 gdb_assert (dwp_file->version == 1);
12122
6f738b01
SM
12123 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V1 file: %s",
12124 kind, pulongest (unit_index), hex_string (signature),
12125 dwp_file->name);
80626a55 12126
19ac8c2e 12127 /* Fetch the sections of this DWO unit.
80626a55
DE
12128 Put a limit on the number of sections we look for so that bad data
12129 doesn't cause us to loop forever. */
12130
73869dc2 12131#define MAX_NR_V1_DWO_SECTIONS \
80626a55
DE
12132 (1 /* .debug_info or .debug_types */ \
12133 + 1 /* .debug_abbrev */ \
12134 + 1 /* .debug_line */ \
12135 + 1 /* .debug_loc */ \
12136 + 1 /* .debug_str_offsets */ \
19ac8c2e 12137 + 1 /* .debug_macro or .debug_macinfo */ \
80626a55
DE
12138 + 1 /* trailing zero */)
12139
12140 memset (&sections, 0, sizeof (sections));
80626a55 12141
73869dc2 12142 for (i = 0; i < MAX_NR_V1_DWO_SECTIONS; ++i)
80626a55
DE
12143 {
12144 asection *sectp;
12145 uint32_t section_nr =
12146 read_4_bytes (dbfd,
73869dc2
DE
12147 dwp_htab->section_pool.v1.indices
12148 + (unit_index + i) * sizeof (uint32_t));
80626a55
DE
12149
12150 if (section_nr == 0)
12151 break;
12152 if (section_nr >= dwp_file->num_sections)
12153 {
12154 error (_("Dwarf Error: bad DWP hash table, section number too large"
12155 " [in module %s]"),
12156 dwp_file->name);
12157 }
12158
12159 sectp = dwp_file->elf_sections[section_nr];
73869dc2 12160 if (! locate_v1_virtual_dwo_sections (sectp, &sections))
80626a55
DE
12161 {
12162 error (_("Dwarf Error: bad DWP hash table, invalid section found"
12163 " [in module %s]"),
12164 dwp_file->name);
12165 }
12166 }
12167
12168 if (i < 2
96b79293
TT
12169 || sections.info_or_types.empty ()
12170 || sections.abbrev.empty ())
80626a55
DE
12171 {
12172 error (_("Dwarf Error: bad DWP hash table, missing DWO sections"
12173 " [in module %s]"),
12174 dwp_file->name);
12175 }
73869dc2 12176 if (i == MAX_NR_V1_DWO_SECTIONS)
80626a55
DE
12177 {
12178 error (_("Dwarf Error: bad DWP hash table, too many DWO sections"
12179 " [in module %s]"),
12180 dwp_file->name);
12181 }
12182
12183 /* It's easier for the rest of the code if we fake a struct dwo_file and
12184 have dwo_unit "live" in that. At least for now.
12185
12186 The DWP file can be made up of a random collection of CUs and TUs.
c766f7ec 12187 However, for each CU + set of TUs that came from the same original DWO
57d63ce2
DE
12188 file, we can combine them back into a virtual DWO file to save space
12189 (fewer struct dwo_file objects to allocate). Remember that for really
80626a55
DE
12190 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12191
791afaa2
TT
12192 std::string virtual_dwo_name =
12193 string_printf ("virtual-dwo/%d-%d-%d-%d",
96b79293
TT
12194 sections.abbrev.get_id (),
12195 sections.line.get_id (),
12196 sections.loc.get_id (),
12197 sections.str_offsets.get_id ());
80626a55 12198 /* Can we use an existing virtual DWO file? */
976ca316 12199 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12200 comp_dir);
80626a55
DE
12201 /* Create one if necessary. */
12202 if (*dwo_file_slot == NULL)
12203 {
6f738b01
SM
12204 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12205 virtual_dwo_name.c_str ());
12206
51ac9db5 12207 dwo_file = new struct dwo_file;
976ca316 12208 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
0ac5b59e 12209 dwo_file->comp_dir = comp_dir;
80626a55
DE
12210 dwo_file->sections.abbrev = sections.abbrev;
12211 dwo_file->sections.line = sections.line;
12212 dwo_file->sections.loc = sections.loc;
12213 dwo_file->sections.macinfo = sections.macinfo;
12214 dwo_file->sections.macro = sections.macro;
12215 dwo_file->sections.str_offsets = sections.str_offsets;
12216 /* The "str" section is global to the entire DWP file. */
12217 dwo_file->sections.str = dwp_file->sections.str;
57d63ce2 12218 /* The info or types section is assigned below to dwo_unit,
80626a55
DE
12219 there's no need to record it in dwo_file.
12220 Also, we can't simply record type sections in dwo_file because
12221 we record a pointer into the vector in dwo_unit. As we collect more
12222 types we'll grow the vector and eventually have to reallocate space
57d63ce2
DE
12223 for it, invalidating all copies of pointers into the previous
12224 contents. */
80626a55
DE
12225 *dwo_file_slot = dwo_file;
12226 }
12227 else
12228 {
6f738b01
SM
12229 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12230 virtual_dwo_name.c_str ());
12231
9a3c8263 12232 dwo_file = (struct dwo_file *) *dwo_file_slot;
80626a55 12233 }
80626a55 12234
976ca316 12235 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
80626a55
DE
12236 dwo_unit->dwo_file = dwo_file;
12237 dwo_unit->signature = signature;
8d749320 12238 dwo_unit->section =
976ca316 12239 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
8a0459fd 12240 *dwo_unit->section = sections.info_or_types;
57d63ce2 12241 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
80626a55
DE
12242
12243 return dwo_unit;
12244}
12245
d2854d8d
CT
12246/* Subroutine of create_dwo_unit_in_dwp_v2 and create_dwo_unit_in_dwp_v5 to
12247 simplify them. Given a pointer to the containing section SECTION, and
12248 OFFSET,SIZE of the piece within that section used by a TU/CU, return a
12249 virtual section of just that piece. */
73869dc2
DE
12250
12251static struct dwarf2_section_info
d2854d8d
CT
12252create_dwp_v2_or_v5_section (dwarf2_per_objfile *per_objfile,
12253 struct dwarf2_section_info *section,
12254 bfd_size_type offset, bfd_size_type size)
73869dc2
DE
12255{
12256 struct dwarf2_section_info result;
12257 asection *sectp;
12258
12259 gdb_assert (section != NULL);
12260 gdb_assert (!section->is_virtual);
12261
12262 memset (&result, 0, sizeof (result));
12263 result.s.containing_section = section;
dc4ccb6f 12264 result.is_virtual = true;
73869dc2
DE
12265
12266 if (size == 0)
12267 return result;
12268
96b79293 12269 sectp = section->get_bfd_section ();
73869dc2
DE
12270
12271 /* Flag an error if the piece denoted by OFFSET,SIZE is outside the
12272 bounds of the real section. This is a pretty-rare event, so just
12273 flag an error (easier) instead of a warning and trying to cope. */
12274 if (sectp == NULL
fd361982 12275 || offset + size > bfd_section_size (sectp))
73869dc2 12276 {
d2854d8d 12277 error (_("Dwarf Error: Bad DWP V2 or V5 section info, doesn't fit"
73869dc2 12278 " in section %s [in module %s]"),
fd361982 12279 sectp ? bfd_section_name (sectp) : "<unknown>",
976ca316 12280 objfile_name (per_objfile->objfile));
73869dc2
DE
12281 }
12282
12283 result.virtual_offset = offset;
12284 result.size = size;
12285 return result;
12286}
12287
12288/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12289 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12290 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12291 This is for DWP version 2 files. */
12292
12293static struct dwo_unit *
976ca316 12294create_dwo_unit_in_dwp_v2 (dwarf2_per_objfile *per_objfile,
ed2dc618 12295 struct dwp_file *dwp_file,
73869dc2
DE
12296 uint32_t unit_index,
12297 const char *comp_dir,
12298 ULONGEST signature, int is_debug_types)
12299{
73869dc2
DE
12300 const struct dwp_hash_table *dwp_htab =
12301 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12302 bfd *dbfd = dwp_file->dbfd.get ();
73869dc2
DE
12303 const char *kind = is_debug_types ? "TU" : "CU";
12304 struct dwo_file *dwo_file;
12305 struct dwo_unit *dwo_unit;
d2854d8d 12306 struct virtual_v2_or_v5_dwo_sections sections;
73869dc2 12307 void **dwo_file_slot;
73869dc2
DE
12308 int i;
12309
12310 gdb_assert (dwp_file->version == 2);
12311
6f738b01
SM
12312 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V2 file: %s",
12313 kind, pulongest (unit_index), hex_string (signature),
12314 dwp_file->name);
73869dc2
DE
12315
12316 /* Fetch the section offsets of this DWO unit. */
12317
12318 memset (&sections, 0, sizeof (sections));
73869dc2
DE
12319
12320 for (i = 0; i < dwp_htab->nr_columns; ++i)
12321 {
12322 uint32_t offset = read_4_bytes (dbfd,
12323 dwp_htab->section_pool.v2.offsets
12324 + (((unit_index - 1) * dwp_htab->nr_columns
12325 + i)
12326 * sizeof (uint32_t)));
12327 uint32_t size = read_4_bytes (dbfd,
12328 dwp_htab->section_pool.v2.sizes
12329 + (((unit_index - 1) * dwp_htab->nr_columns
12330 + i)
12331 * sizeof (uint32_t)));
12332
12333 switch (dwp_htab->section_pool.v2.section_ids[i])
12334 {
12335 case DW_SECT_INFO:
12336 case DW_SECT_TYPES:
12337 sections.info_or_types_offset = offset;
12338 sections.info_or_types_size = size;
12339 break;
12340 case DW_SECT_ABBREV:
12341 sections.abbrev_offset = offset;
12342 sections.abbrev_size = size;
12343 break;
12344 case DW_SECT_LINE:
12345 sections.line_offset = offset;
12346 sections.line_size = size;
12347 break;
12348 case DW_SECT_LOC:
12349 sections.loc_offset = offset;
12350 sections.loc_size = size;
12351 break;
12352 case DW_SECT_STR_OFFSETS:
12353 sections.str_offsets_offset = offset;
12354 sections.str_offsets_size = size;
12355 break;
12356 case DW_SECT_MACINFO:
12357 sections.macinfo_offset = offset;
12358 sections.macinfo_size = size;
12359 break;
12360 case DW_SECT_MACRO:
12361 sections.macro_offset = offset;
12362 sections.macro_size = size;
12363 break;
12364 }
12365 }
12366
12367 /* It's easier for the rest of the code if we fake a struct dwo_file and
12368 have dwo_unit "live" in that. At least for now.
12369
12370 The DWP file can be made up of a random collection of CUs and TUs.
12371 However, for each CU + set of TUs that came from the same original DWO
12372 file, we can combine them back into a virtual DWO file to save space
12373 (fewer struct dwo_file objects to allocate). Remember that for really
12374 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12375
791afaa2
TT
12376 std::string virtual_dwo_name =
12377 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld",
12378 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12379 (long) (sections.line_size ? sections.line_offset : 0),
12380 (long) (sections.loc_size ? sections.loc_offset : 0),
12381 (long) (sections.str_offsets_size
12382 ? sections.str_offsets_offset : 0));
73869dc2 12383 /* Can we use an existing virtual DWO file? */
976ca316 12384 dwo_file_slot = lookup_dwo_file_slot (per_objfile, virtual_dwo_name.c_str (),
ed2dc618 12385 comp_dir);
73869dc2
DE
12386 /* Create one if necessary. */
12387 if (*dwo_file_slot == NULL)
12388 {
6f738b01
SM
12389 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12390 virtual_dwo_name.c_str ());
12391
51ac9db5 12392 dwo_file = new struct dwo_file;
976ca316 12393 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
73869dc2
DE
12394 dwo_file->comp_dir = comp_dir;
12395 dwo_file->sections.abbrev =
d2854d8d
CT
12396 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.abbrev,
12397 sections.abbrev_offset,
12398 sections.abbrev_size);
73869dc2 12399 dwo_file->sections.line =
d2854d8d
CT
12400 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.line,
12401 sections.line_offset,
12402 sections.line_size);
73869dc2 12403 dwo_file->sections.loc =
d2854d8d
CT
12404 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.loc,
12405 sections.loc_offset, sections.loc_size);
73869dc2 12406 dwo_file->sections.macinfo =
d2854d8d
CT
12407 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macinfo,
12408 sections.macinfo_offset,
12409 sections.macinfo_size);
73869dc2 12410 dwo_file->sections.macro =
d2854d8d
CT
12411 create_dwp_v2_or_v5_section (per_objfile, &dwp_file->sections.macro,
12412 sections.macro_offset,
12413 sections.macro_size);
73869dc2 12414 dwo_file->sections.str_offsets =
d2854d8d
CT
12415 create_dwp_v2_or_v5_section (per_objfile,
12416 &dwp_file->sections.str_offsets,
12417 sections.str_offsets_offset,
12418 sections.str_offsets_size);
73869dc2
DE
12419 /* The "str" section is global to the entire DWP file. */
12420 dwo_file->sections.str = dwp_file->sections.str;
12421 /* The info or types section is assigned below to dwo_unit,
12422 there's no need to record it in dwo_file.
12423 Also, we can't simply record type sections in dwo_file because
12424 we record a pointer into the vector in dwo_unit. As we collect more
12425 types we'll grow the vector and eventually have to reallocate space
12426 for it, invalidating all copies of pointers into the previous
12427 contents. */
12428 *dwo_file_slot = dwo_file;
12429 }
12430 else
12431 {
6f738b01
SM
12432 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12433 virtual_dwo_name.c_str ());
12434
9a3c8263 12435 dwo_file = (struct dwo_file *) *dwo_file_slot;
73869dc2 12436 }
73869dc2 12437
976ca316 12438 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
73869dc2
DE
12439 dwo_unit->dwo_file = dwo_file;
12440 dwo_unit->signature = signature;
8d749320 12441 dwo_unit->section =
976ca316 12442 XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
d2854d8d 12443 *dwo_unit->section = create_dwp_v2_or_v5_section
dda83cd7 12444 (per_objfile,
d2854d8d
CT
12445 is_debug_types
12446 ? &dwp_file->sections.types
12447 : &dwp_file->sections.info,
12448 sections.info_or_types_offset,
12449 sections.info_or_types_size);
12450 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12451
12452 return dwo_unit;
12453}
12454
12455/* Create a dwo_unit object for the DWO unit with signature SIGNATURE.
12456 UNIT_INDEX is the index of the DWO unit in the DWP hash table.
12457 COMP_DIR is the DW_AT_comp_dir attribute of the referencing CU.
12458 This is for DWP version 5 files. */
12459
12460static struct dwo_unit *
12461create_dwo_unit_in_dwp_v5 (dwarf2_per_objfile *per_objfile,
12462 struct dwp_file *dwp_file,
12463 uint32_t unit_index,
12464 const char *comp_dir,
12465 ULONGEST signature, int is_debug_types)
12466{
12467 const struct dwp_hash_table *dwp_htab
12468 = is_debug_types ? dwp_file->tus : dwp_file->cus;
12469 bfd *dbfd = dwp_file->dbfd.get ();
12470 const char *kind = is_debug_types ? "TU" : "CU";
12471 struct dwo_file *dwo_file;
12472 struct dwo_unit *dwo_unit;
12473 struct virtual_v2_or_v5_dwo_sections sections {};
12474 void **dwo_file_slot;
12475
12476 gdb_assert (dwp_file->version == 5);
12477
6f738b01
SM
12478 dwarf_read_debug_printf ("Reading %s %s/%s in DWP V5 file: %s",
12479 kind, pulongest (unit_index), hex_string (signature),
12480 dwp_file->name);
d2854d8d
CT
12481
12482 /* Fetch the section offsets of this DWO unit. */
12483
12484 /* memset (&sections, 0, sizeof (sections)); */
12485
12486 for (int i = 0; i < dwp_htab->nr_columns; ++i)
12487 {
12488 uint32_t offset = read_4_bytes (dbfd,
dda83cd7
SM
12489 dwp_htab->section_pool.v5.offsets
12490 + (((unit_index - 1)
12491 * dwp_htab->nr_columns
12492 + i)
12493 * sizeof (uint32_t)));
d2854d8d 12494 uint32_t size = read_4_bytes (dbfd,
dda83cd7
SM
12495 dwp_htab->section_pool.v5.sizes
12496 + (((unit_index - 1) * dwp_htab->nr_columns
12497 + i)
12498 * sizeof (uint32_t)));
d2854d8d
CT
12499
12500 switch (dwp_htab->section_pool.v5.section_ids[i])
dda83cd7
SM
12501 {
12502 case DW_SECT_ABBREV_V5:
12503 sections.abbrev_offset = offset;
12504 sections.abbrev_size = size;
12505 break;
12506 case DW_SECT_INFO_V5:
12507 sections.info_or_types_offset = offset;
12508 sections.info_or_types_size = size;
12509 break;
12510 case DW_SECT_LINE_V5:
12511 sections.line_offset = offset;
12512 sections.line_size = size;
12513 break;
12514 case DW_SECT_LOCLISTS_V5:
12515 sections.loclists_offset = offset;
12516 sections.loclists_size = size;
12517 break;
12518 case DW_SECT_MACRO_V5:
12519 sections.macro_offset = offset;
12520 sections.macro_size = size;
12521 break;
12522 case DW_SECT_RNGLISTS_V5:
12523 sections.rnglists_offset = offset;
12524 sections.rnglists_size = size;
12525 break;
12526 case DW_SECT_STR_OFFSETS_V5:
12527 sections.str_offsets_offset = offset;
12528 sections.str_offsets_size = size;
12529 break;
12530 case DW_SECT_RESERVED_V5:
12531 default:
12532 break;
12533 }
d2854d8d
CT
12534 }
12535
12536 /* It's easier for the rest of the code if we fake a struct dwo_file and
12537 have dwo_unit "live" in that. At least for now.
12538
12539 The DWP file can be made up of a random collection of CUs and TUs.
12540 However, for each CU + set of TUs that came from the same original DWO
12541 file, we can combine them back into a virtual DWO file to save space
12542 (fewer struct dwo_file objects to allocate). Remember that for really
12543 large apps there can be on the order of 8K CUs and 200K TUs, or more. */
12544
12545 std::string virtual_dwo_name =
12546 string_printf ("virtual-dwo/%ld-%ld-%ld-%ld-%ld-%ld",
dda83cd7
SM
12547 (long) (sections.abbrev_size ? sections.abbrev_offset : 0),
12548 (long) (sections.line_size ? sections.line_offset : 0),
12549 (long) (sections.loclists_size ? sections.loclists_offset : 0),
12550 (long) (sections.str_offsets_size
12551 ? sections.str_offsets_offset : 0),
12552 (long) (sections.macro_size ? sections.macro_offset : 0),
12553 (long) (sections.rnglists_size ? sections.rnglists_offset: 0));
d2854d8d
CT
12554 /* Can we use an existing virtual DWO file? */
12555 dwo_file_slot = lookup_dwo_file_slot (per_objfile,
dda83cd7
SM
12556 virtual_dwo_name.c_str (),
12557 comp_dir);
d2854d8d
CT
12558 /* Create one if necessary. */
12559 if (*dwo_file_slot == NULL)
12560 {
6f738b01
SM
12561 dwarf_read_debug_printf ("Creating virtual DWO: %s",
12562 virtual_dwo_name.c_str ());
12563
d2854d8d
CT
12564 dwo_file = new struct dwo_file;
12565 dwo_file->dwo_name = per_objfile->objfile->intern (virtual_dwo_name);
12566 dwo_file->comp_dir = comp_dir;
12567 dwo_file->sections.abbrev =
dda83cd7
SM
12568 create_dwp_v2_or_v5_section (per_objfile,
12569 &dwp_file->sections.abbrev,
12570 sections.abbrev_offset,
12571 sections.abbrev_size);
d2854d8d 12572 dwo_file->sections.line =
dda83cd7
SM
12573 create_dwp_v2_or_v5_section (per_objfile,
12574 &dwp_file->sections.line,
12575 sections.line_offset, sections.line_size);
d2854d8d 12576 dwo_file->sections.macro =
dda83cd7
SM
12577 create_dwp_v2_or_v5_section (per_objfile,
12578 &dwp_file->sections.macro,
12579 sections.macro_offset,
12580 sections.macro_size);
d2854d8d 12581 dwo_file->sections.loclists =
dda83cd7
SM
12582 create_dwp_v2_or_v5_section (per_objfile,
12583 &dwp_file->sections.loclists,
12584 sections.loclists_offset,
12585 sections.loclists_size);
d2854d8d 12586 dwo_file->sections.rnglists =
dda83cd7
SM
12587 create_dwp_v2_or_v5_section (per_objfile,
12588 &dwp_file->sections.rnglists,
12589 sections.rnglists_offset,
12590 sections.rnglists_size);
d2854d8d 12591 dwo_file->sections.str_offsets =
dda83cd7
SM
12592 create_dwp_v2_or_v5_section (per_objfile,
12593 &dwp_file->sections.str_offsets,
12594 sections.str_offsets_offset,
12595 sections.str_offsets_size);
d2854d8d
CT
12596 /* The "str" section is global to the entire DWP file. */
12597 dwo_file->sections.str = dwp_file->sections.str;
12598 /* The info or types section is assigned below to dwo_unit,
dda83cd7
SM
12599 there's no need to record it in dwo_file.
12600 Also, we can't simply record type sections in dwo_file because
12601 we record a pointer into the vector in dwo_unit. As we collect more
12602 types we'll grow the vector and eventually have to reallocate space
12603 for it, invalidating all copies of pointers into the previous
12604 contents. */
d2854d8d
CT
12605 *dwo_file_slot = dwo_file;
12606 }
12607 else
12608 {
6f738b01
SM
12609 dwarf_read_debug_printf ("Using existing virtual DWO: %s",
12610 virtual_dwo_name.c_str ());
12611
d2854d8d
CT
12612 dwo_file = (struct dwo_file *) *dwo_file_slot;
12613 }
12614
12615 dwo_unit = OBSTACK_ZALLOC (&per_objfile->per_bfd->obstack, struct dwo_unit);
12616 dwo_unit->dwo_file = dwo_file;
12617 dwo_unit->signature = signature;
12618 dwo_unit->section
12619 = XOBNEW (&per_objfile->per_bfd->obstack, struct dwarf2_section_info);
12620 *dwo_unit->section = create_dwp_v2_or_v5_section (per_objfile,
dda83cd7
SM
12621 &dwp_file->sections.info,
12622 sections.info_or_types_offset,
12623 sections.info_or_types_size);
73869dc2
DE
12624 /* dwo_unit->{offset,length,type_offset_in_tu} are set later. */
12625
12626 return dwo_unit;
12627}
12628
57d63ce2
DE
12629/* Lookup the DWO unit with SIGNATURE in DWP_FILE.
12630 Returns NULL if the signature isn't found. */
80626a55
DE
12631
12632static struct dwo_unit *
976ca316 12633lookup_dwo_unit_in_dwp (dwarf2_per_objfile *per_objfile,
ed2dc618 12634 struct dwp_file *dwp_file, const char *comp_dir,
57d63ce2 12635 ULONGEST signature, int is_debug_types)
80626a55 12636{
57d63ce2
DE
12637 const struct dwp_hash_table *dwp_htab =
12638 is_debug_types ? dwp_file->tus : dwp_file->cus;
400174b1 12639 bfd *dbfd = dwp_file->dbfd.get ();
57d63ce2 12640 uint32_t mask = dwp_htab->nr_slots - 1;
80626a55
DE
12641 uint32_t hash = signature & mask;
12642 uint32_t hash2 = ((signature >> 32) & mask) | 1;
12643 unsigned int i;
12644 void **slot;
870f88f7 12645 struct dwo_unit find_dwo_cu;
80626a55
DE
12646
12647 memset (&find_dwo_cu, 0, sizeof (find_dwo_cu));
12648 find_dwo_cu.signature = signature;
19ac8c2e 12649 slot = htab_find_slot (is_debug_types
48b490f2
TT
12650 ? dwp_file->loaded_tus.get ()
12651 : dwp_file->loaded_cus.get (),
19ac8c2e 12652 &find_dwo_cu, INSERT);
80626a55
DE
12653
12654 if (*slot != NULL)
9a3c8263 12655 return (struct dwo_unit *) *slot;
80626a55
DE
12656
12657 /* Use a for loop so that we don't loop forever on bad debug info. */
57d63ce2 12658 for (i = 0; i < dwp_htab->nr_slots; ++i)
80626a55
DE
12659 {
12660 ULONGEST signature_in_table;
12661
12662 signature_in_table =
57d63ce2 12663 read_8_bytes (dbfd, dwp_htab->hash_table + hash * sizeof (uint64_t));
80626a55
DE
12664 if (signature_in_table == signature)
12665 {
57d63ce2
DE
12666 uint32_t unit_index =
12667 read_4_bytes (dbfd,
12668 dwp_htab->unit_table + hash * sizeof (uint32_t));
80626a55 12669
73869dc2
DE
12670 if (dwp_file->version == 1)
12671 {
976ca316
SM
12672 *slot = create_dwo_unit_in_dwp_v1 (per_objfile, dwp_file,
12673 unit_index, comp_dir,
12674 signature, is_debug_types);
73869dc2 12675 }
d2854d8d 12676 else if (dwp_file->version == 2)
73869dc2 12677 {
976ca316
SM
12678 *slot = create_dwo_unit_in_dwp_v2 (per_objfile, dwp_file,
12679 unit_index, comp_dir,
12680 signature, is_debug_types);
73869dc2 12681 }
d2854d8d
CT
12682 else /* version == 5 */
12683 {
12684 *slot = create_dwo_unit_in_dwp_v5 (per_objfile, dwp_file,
12685 unit_index, comp_dir,
12686 signature, is_debug_types);
12687 }
9a3c8263 12688 return (struct dwo_unit *) *slot;
80626a55
DE
12689 }
12690 if (signature_in_table == 0)
12691 return NULL;
12692 hash = (hash + hash2) & mask;
12693 }
12694
12695 error (_("Dwarf Error: bad DWP hash table, lookup didn't terminate"
12696 " [in module %s]"),
12697 dwp_file->name);
12698}
12699
ab5088bf 12700/* Subroutine of open_dwo_file,open_dwp_file to simplify them.
3019eac3
DE
12701 Open the file specified by FILE_NAME and hand it off to BFD for
12702 preliminary analysis. Return a newly initialized bfd *, which
12703 includes a canonicalized copy of FILE_NAME.
80626a55 12704 If IS_DWP is TRUE, we're opening a DWP file, otherwise a DWO file.
6ac97d4c
DE
12705 SEARCH_CWD is true if the current directory is to be searched.
12706 It will be searched before debug-file-directory.
13aaf454
DE
12707 If successful, the file is added to the bfd include table of the
12708 objfile's bfd (see gdb_bfd_record_inclusion).
6ac97d4c 12709 If unable to find/open the file, return NULL.
3019eac3
DE
12710 NOTE: This function is derived from symfile_bfd_open. */
12711
192b62ce 12712static gdb_bfd_ref_ptr
976ca316 12713try_open_dwop_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12714 const char *file_name, int is_dwp, int search_cwd)
3019eac3 12715{
24b9144d 12716 int desc;
9c02c129
DE
12717 /* Blech. OPF_TRY_CWD_FIRST also disables searching the path list if
12718 FILE_NAME contains a '/'. So we can't use it. Instead prepend "."
12719 to debug_file_directory. */
e0cc99a6 12720 const char *search_path;
9c02c129
DE
12721 static const char dirname_separator_string[] = { DIRNAME_SEPARATOR, '\0' };
12722
e0cc99a6 12723 gdb::unique_xmalloc_ptr<char> search_path_holder;
6ac97d4c
DE
12724 if (search_cwd)
12725 {
12726 if (*debug_file_directory != '\0')
e0cc99a6
TT
12727 {
12728 search_path_holder.reset (concat (".", dirname_separator_string,
12729 debug_file_directory,
12730 (char *) NULL));
12731 search_path = search_path_holder.get ();
12732 }
6ac97d4c 12733 else
e0cc99a6 12734 search_path = ".";
6ac97d4c 12735 }
9c02c129 12736 else
e0cc99a6 12737 search_path = debug_file_directory;
3019eac3 12738
24b9144d 12739 openp_flags flags = OPF_RETURN_REALPATH;
80626a55
DE
12740 if (is_dwp)
12741 flags |= OPF_SEARCH_IN_PATH;
e0cc99a6
TT
12742
12743 gdb::unique_xmalloc_ptr<char> absolute_name;
9c02c129 12744 desc = openp (search_path, flags, file_name,
3019eac3
DE
12745 O_RDONLY | O_BINARY, &absolute_name);
12746 if (desc < 0)
12747 return NULL;
12748
e0cc99a6
TT
12749 gdb_bfd_ref_ptr sym_bfd (gdb_bfd_open (absolute_name.get (),
12750 gnutarget, desc));
9c02c129
DE
12751 if (sym_bfd == NULL)
12752 return NULL;
192b62ce 12753 bfd_set_cacheable (sym_bfd.get (), 1);
3019eac3 12754
192b62ce
TT
12755 if (!bfd_check_format (sym_bfd.get (), bfd_object))
12756 return NULL;
3019eac3 12757
13aaf454
DE
12758 /* Success. Record the bfd as having been included by the objfile's bfd.
12759 This is important because things like demangled_names_hash lives in the
12760 objfile's per_bfd space and may have references to things like symbol
12761 names that live in the DWO/DWP file's per_bfd space. PR 16426. */
976ca316 12762 gdb_bfd_record_inclusion (per_objfile->objfile->obfd, sym_bfd.get ());
13aaf454 12763
3019eac3
DE
12764 return sym_bfd;
12765}
12766
ab5088bf 12767/* Try to open DWO file FILE_NAME.
3019eac3
DE
12768 COMP_DIR is the DW_AT_comp_dir attribute.
12769 The result is the bfd handle of the file.
12770 If there is a problem finding or opening the file, return NULL.
12771 Upon success, the canonicalized path of the file is stored in the bfd,
12772 same as symfile_bfd_open. */
12773
192b62ce 12774static gdb_bfd_ref_ptr
976ca316 12775open_dwo_file (dwarf2_per_objfile *per_objfile,
ed2dc618 12776 const char *file_name, const char *comp_dir)
3019eac3 12777{
80626a55 12778 if (IS_ABSOLUTE_PATH (file_name))
976ca316 12779 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12780 0 /*is_dwp*/, 0 /*search_cwd*/);
3019eac3
DE
12781
12782 /* Before trying the search path, try DWO_NAME in COMP_DIR. */
12783
12784 if (comp_dir != NULL)
12785 {
43816ebc
TT
12786 gdb::unique_xmalloc_ptr<char> path_to_try
12787 (concat (comp_dir, SLASH_STRING, file_name, (char *) NULL));
3019eac3
DE
12788
12789 /* NOTE: If comp_dir is a relative path, this will also try the
12790 search path, which seems useful. */
976ca316 12791 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, path_to_try.get (),
ed2dc618 12792 0 /*is_dwp*/,
192b62ce 12793 1 /*search_cwd*/));
3019eac3
DE
12794 if (abfd != NULL)
12795 return abfd;
12796 }
12797
12798 /* That didn't work, try debug-file-directory, which, despite its name,
12799 is a list of paths. */
12800
12801 if (*debug_file_directory == '\0')
12802 return NULL;
12803
976ca316 12804 return try_open_dwop_file (per_objfile, file_name,
ed2dc618 12805 0 /*is_dwp*/, 1 /*search_cwd*/);
3019eac3
DE
12806}
12807
80626a55
DE
12808/* This function is mapped across the sections and remembers the offset and
12809 size of each of the DWO debugging sections we are interested in. */
12810
12811static void
5bb6e9dd
TT
12812dwarf2_locate_dwo_sections (bfd *abfd, asection *sectp,
12813 dwo_sections *dwo_sections)
80626a55 12814{
80626a55
DE
12815 const struct dwop_section_names *names = &dwop_section_names;
12816
12817 if (section_is_p (sectp->name, &names->abbrev_dwo))
12818 {
049412e3 12819 dwo_sections->abbrev.s.section = sectp;
fd361982 12820 dwo_sections->abbrev.size = bfd_section_size (sectp);
80626a55
DE
12821 }
12822 else if (section_is_p (sectp->name, &names->info_dwo))
12823 {
049412e3 12824 dwo_sections->info.s.section = sectp;
fd361982 12825 dwo_sections->info.size = bfd_section_size (sectp);
80626a55
DE
12826 }
12827 else if (section_is_p (sectp->name, &names->line_dwo))
12828 {
049412e3 12829 dwo_sections->line.s.section = sectp;
fd361982 12830 dwo_sections->line.size = bfd_section_size (sectp);
80626a55
DE
12831 }
12832 else if (section_is_p (sectp->name, &names->loc_dwo))
12833 {
049412e3 12834 dwo_sections->loc.s.section = sectp;
fd361982 12835 dwo_sections->loc.size = bfd_section_size (sectp);
80626a55 12836 }
41144253 12837 else if (section_is_p (sectp->name, &names->loclists_dwo))
12838 {
12839 dwo_sections->loclists.s.section = sectp;
12840 dwo_sections->loclists.size = bfd_section_size (sectp);
12841 }
80626a55
DE
12842 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12843 {
049412e3 12844 dwo_sections->macinfo.s.section = sectp;
fd361982 12845 dwo_sections->macinfo.size = bfd_section_size (sectp);
80626a55
DE
12846 }
12847 else if (section_is_p (sectp->name, &names->macro_dwo))
12848 {
049412e3 12849 dwo_sections->macro.s.section = sectp;
fd361982 12850 dwo_sections->macro.size = bfd_section_size (sectp);
80626a55 12851 }
d0ce17d8
CT
12852 else if (section_is_p (sectp->name, &names->rnglists_dwo))
12853 {
12854 dwo_sections->rnglists.s.section = sectp;
12855 dwo_sections->rnglists.size = bfd_section_size (sectp);
12856 }
80626a55
DE
12857 else if (section_is_p (sectp->name, &names->str_dwo))
12858 {
049412e3 12859 dwo_sections->str.s.section = sectp;
fd361982 12860 dwo_sections->str.size = bfd_section_size (sectp);
80626a55
DE
12861 }
12862 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
12863 {
049412e3 12864 dwo_sections->str_offsets.s.section = sectp;
fd361982 12865 dwo_sections->str_offsets.size = bfd_section_size (sectp);
80626a55
DE
12866 }
12867 else if (section_is_p (sectp->name, &names->types_dwo))
12868 {
12869 struct dwarf2_section_info type_section;
12870
12871 memset (&type_section, 0, sizeof (type_section));
049412e3 12872 type_section.s.section = sectp;
fd361982 12873 type_section.size = bfd_section_size (sectp);
fd5866f6 12874 dwo_sections->types.push_back (type_section);
80626a55
DE
12875 }
12876}
12877
ab5088bf 12878/* Initialize the use of the DWO file specified by DWO_NAME and referenced
19c3d4c9 12879 by PER_CU. This is for the non-DWP case.
80626a55 12880 The result is NULL if DWO_NAME can't be found. */
3019eac3
DE
12881
12882static struct dwo_file *
4ab09049
SM
12883open_and_init_dwo_file (dwarf2_cu *cu, const char *dwo_name,
12884 const char *comp_dir)
3019eac3 12885{
976ca316 12886 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 12887
976ca316 12888 gdb_bfd_ref_ptr dbfd = open_dwo_file (per_objfile, dwo_name, comp_dir);
80626a55
DE
12889 if (dbfd == NULL)
12890 {
6f738b01
SM
12891 dwarf_read_debug_printf ("DWO file not found: %s", dwo_name);
12892
80626a55
DE
12893 return NULL;
12894 }
263db9a1 12895
51ac9db5 12896 dwo_file_up dwo_file (new struct dwo_file);
0ac5b59e
DE
12897 dwo_file->dwo_name = dwo_name;
12898 dwo_file->comp_dir = comp_dir;
fb1eb2f9 12899 dwo_file->dbfd = std::move (dbfd);
3019eac3 12900
5bb6e9dd
TT
12901 for (asection *sec : gdb_bfd_sections (dwo_file->dbfd))
12902 dwarf2_locate_dwo_sections (dwo_file->dbfd.get (), sec,
12903 &dwo_file->sections);
3019eac3 12904
976ca316
SM
12905 create_cus_hash_table (per_objfile, cu, *dwo_file, dwo_file->sections.info,
12906 dwo_file->cus);
3019eac3 12907
d2854d8d
CT
12908 if (cu->per_cu->dwarf_version < 5)
12909 {
12910 create_debug_types_hash_table (per_objfile, dwo_file.get (),
12911 dwo_file->sections.types, dwo_file->tus);
12912 }
12913 else
12914 {
12915 create_debug_type_hash_table (per_objfile, dwo_file.get (),
12916 &dwo_file->sections.info, dwo_file->tus,
12917 rcuh_kind::TYPE);
12918 }
3019eac3 12919
6f738b01 12920 dwarf_read_debug_printf ("DWO file found: %s", dwo_name);
80626a55 12921
263db9a1 12922 return dwo_file.release ();
3019eac3
DE
12923}
12924
80626a55 12925/* This function is mapped across the sections and remembers the offset and
73869dc2
DE
12926 size of each of the DWP debugging sections common to version 1 and 2 that
12927 we are interested in. */
3019eac3 12928
80626a55 12929static void
73869dc2 12930dwarf2_locate_common_dwp_sections (bfd *abfd, asection *sectp,
5bb6e9dd 12931 dwp_file *dwp_file)
3019eac3 12932{
80626a55
DE
12933 const struct dwop_section_names *names = &dwop_section_names;
12934 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
3019eac3 12935
80626a55 12936 /* Record the ELF section number for later lookup: this is what the
73869dc2 12937 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
80626a55
DE
12938 gdb_assert (elf_section_nr < dwp_file->num_sections);
12939 dwp_file->elf_sections[elf_section_nr] = sectp;
3019eac3 12940
80626a55
DE
12941 /* Look for specific sections that we need. */
12942 if (section_is_p (sectp->name, &names->str_dwo))
12943 {
049412e3 12944 dwp_file->sections.str.s.section = sectp;
fd361982 12945 dwp_file->sections.str.size = bfd_section_size (sectp);
80626a55
DE
12946 }
12947 else if (section_is_p (sectp->name, &names->cu_index))
12948 {
049412e3 12949 dwp_file->sections.cu_index.s.section = sectp;
fd361982 12950 dwp_file->sections.cu_index.size = bfd_section_size (sectp);
80626a55
DE
12951 }
12952 else if (section_is_p (sectp->name, &names->tu_index))
12953 {
049412e3 12954 dwp_file->sections.tu_index.s.section = sectp;
fd361982 12955 dwp_file->sections.tu_index.size = bfd_section_size (sectp);
80626a55
DE
12956 }
12957}
3019eac3 12958
73869dc2
DE
12959/* This function is mapped across the sections and remembers the offset and
12960 size of each of the DWP version 2 debugging sections that we are interested
12961 in. This is split into a separate function because we don't know if we
d2854d8d 12962 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
73869dc2
DE
12963
12964static void
12965dwarf2_locate_v2_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
12966{
9a3c8263 12967 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
73869dc2
DE
12968 const struct dwop_section_names *names = &dwop_section_names;
12969 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
12970
12971 /* Record the ELF section number for later lookup: this is what the
12972 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
12973 gdb_assert (elf_section_nr < dwp_file->num_sections);
12974 dwp_file->elf_sections[elf_section_nr] = sectp;
12975
12976 /* Look for specific sections that we need. */
12977 if (section_is_p (sectp->name, &names->abbrev_dwo))
12978 {
049412e3 12979 dwp_file->sections.abbrev.s.section = sectp;
fd361982 12980 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
73869dc2
DE
12981 }
12982 else if (section_is_p (sectp->name, &names->info_dwo))
12983 {
049412e3 12984 dwp_file->sections.info.s.section = sectp;
fd361982 12985 dwp_file->sections.info.size = bfd_section_size (sectp);
73869dc2
DE
12986 }
12987 else if (section_is_p (sectp->name, &names->line_dwo))
12988 {
049412e3 12989 dwp_file->sections.line.s.section = sectp;
fd361982 12990 dwp_file->sections.line.size = bfd_section_size (sectp);
73869dc2
DE
12991 }
12992 else if (section_is_p (sectp->name, &names->loc_dwo))
12993 {
049412e3 12994 dwp_file->sections.loc.s.section = sectp;
fd361982 12995 dwp_file->sections.loc.size = bfd_section_size (sectp);
73869dc2
DE
12996 }
12997 else if (section_is_p (sectp->name, &names->macinfo_dwo))
12998 {
049412e3 12999 dwp_file->sections.macinfo.s.section = sectp;
fd361982 13000 dwp_file->sections.macinfo.size = bfd_section_size (sectp);
73869dc2
DE
13001 }
13002 else if (section_is_p (sectp->name, &names->macro_dwo))
13003 {
049412e3 13004 dwp_file->sections.macro.s.section = sectp;
fd361982 13005 dwp_file->sections.macro.size = bfd_section_size (sectp);
73869dc2
DE
13006 }
13007 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13008 {
049412e3 13009 dwp_file->sections.str_offsets.s.section = sectp;
fd361982 13010 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
73869dc2
DE
13011 }
13012 else if (section_is_p (sectp->name, &names->types_dwo))
13013 {
049412e3 13014 dwp_file->sections.types.s.section = sectp;
fd361982 13015 dwp_file->sections.types.size = bfd_section_size (sectp);
73869dc2
DE
13016 }
13017}
13018
d2854d8d
CT
13019/* This function is mapped across the sections and remembers the offset and
13020 size of each of the DWP version 5 debugging sections that we are interested
13021 in. This is split into a separate function because we don't know if we
13022 have version 1 or 2 or 5 until we parse the cu_index/tu_index sections. */
13023
13024static void
13025dwarf2_locate_v5_dwp_sections (bfd *abfd, asection *sectp, void *dwp_file_ptr)
13026{
13027 struct dwp_file *dwp_file = (struct dwp_file *) dwp_file_ptr;
13028 const struct dwop_section_names *names = &dwop_section_names;
13029 unsigned int elf_section_nr = elf_section_data (sectp)->this_idx;
13030
13031 /* Record the ELF section number for later lookup: this is what the
13032 .debug_cu_index,.debug_tu_index tables use in DWP V1. */
13033 gdb_assert (elf_section_nr < dwp_file->num_sections);
13034 dwp_file->elf_sections[elf_section_nr] = sectp;
13035
13036 /* Look for specific sections that we need. */
13037 if (section_is_p (sectp->name, &names->abbrev_dwo))
13038 {
13039 dwp_file->sections.abbrev.s.section = sectp;
13040 dwp_file->sections.abbrev.size = bfd_section_size (sectp);
13041 }
13042 else if (section_is_p (sectp->name, &names->info_dwo))
13043 {
13044 dwp_file->sections.info.s.section = sectp;
13045 dwp_file->sections.info.size = bfd_section_size (sectp);
13046 }
13047 else if (section_is_p (sectp->name, &names->line_dwo))
13048 {
13049 dwp_file->sections.line.s.section = sectp;
13050 dwp_file->sections.line.size = bfd_section_size (sectp);
13051 }
13052 else if (section_is_p (sectp->name, &names->loclists_dwo))
13053 {
13054 dwp_file->sections.loclists.s.section = sectp;
13055 dwp_file->sections.loclists.size = bfd_section_size (sectp);
13056 }
13057 else if (section_is_p (sectp->name, &names->macro_dwo))
13058 {
13059 dwp_file->sections.macro.s.section = sectp;
13060 dwp_file->sections.macro.size = bfd_section_size (sectp);
13061 }
13062 else if (section_is_p (sectp->name, &names->rnglists_dwo))
13063 {
13064 dwp_file->sections.rnglists.s.section = sectp;
13065 dwp_file->sections.rnglists.size = bfd_section_size (sectp);
13066 }
13067 else if (section_is_p (sectp->name, &names->str_offsets_dwo))
13068 {
13069 dwp_file->sections.str_offsets.s.section = sectp;
13070 dwp_file->sections.str_offsets.size = bfd_section_size (sectp);
13071 }
13072}
13073
80626a55 13074/* Hash function for dwp_file loaded CUs/TUs. */
3019eac3 13075
80626a55
DE
13076static hashval_t
13077hash_dwp_loaded_cutus (const void *item)
13078{
9a3c8263 13079 const struct dwo_unit *dwo_unit = (const struct dwo_unit *) item;
3019eac3 13080
80626a55
DE
13081 /* This drops the top 32 bits of the signature, but is ok for a hash. */
13082 return dwo_unit->signature;
3019eac3
DE
13083}
13084
80626a55 13085/* Equality function for dwp_file loaded CUs/TUs. */
3019eac3 13086
80626a55
DE
13087static int
13088eq_dwp_loaded_cutus (const void *a, const void *b)
3019eac3 13089{
9a3c8263
SM
13090 const struct dwo_unit *dua = (const struct dwo_unit *) a;
13091 const struct dwo_unit *dub = (const struct dwo_unit *) b;
3019eac3 13092
80626a55
DE
13093 return dua->signature == dub->signature;
13094}
3019eac3 13095
80626a55 13096/* Allocate a hash table for dwp_file loaded CUs/TUs. */
3019eac3 13097
48b490f2 13098static htab_up
298e9637 13099allocate_dwp_loaded_cutus_table ()
80626a55 13100{
48b490f2
TT
13101 return htab_up (htab_create_alloc (3,
13102 hash_dwp_loaded_cutus,
13103 eq_dwp_loaded_cutus,
13104 NULL, xcalloc, xfree));
80626a55 13105}
3019eac3 13106
ab5088bf
DE
13107/* Try to open DWP file FILE_NAME.
13108 The result is the bfd handle of the file.
13109 If there is a problem finding or opening the file, return NULL.
13110 Upon success, the canonicalized path of the file is stored in the bfd,
13111 same as symfile_bfd_open. */
13112
192b62ce 13113static gdb_bfd_ref_ptr
976ca316 13114open_dwp_file (dwarf2_per_objfile *per_objfile, const char *file_name)
ab5088bf 13115{
976ca316 13116 gdb_bfd_ref_ptr abfd (try_open_dwop_file (per_objfile, file_name,
ed2dc618 13117 1 /*is_dwp*/,
192b62ce 13118 1 /*search_cwd*/));
6ac97d4c
DE
13119 if (abfd != NULL)
13120 return abfd;
13121
13122 /* Work around upstream bug 15652.
13123 http://sourceware.org/bugzilla/show_bug.cgi?id=15652
13124 [Whether that's a "bug" is debatable, but it is getting in our way.]
13125 We have no real idea where the dwp file is, because gdb's realpath-ing
13126 of the executable's path may have discarded the needed info.
13127 [IWBN if the dwp file name was recorded in the executable, akin to
13128 .gnu_debuglink, but that doesn't exist yet.]
13129 Strip the directory from FILE_NAME and search again. */
13130 if (*debug_file_directory != '\0')
13131 {
13132 /* Don't implicitly search the current directory here.
13133 If the user wants to search "." to handle this case,
13134 it must be added to debug-file-directory. */
976ca316
SM
13135 return try_open_dwop_file (per_objfile, lbasename (file_name),
13136 1 /*is_dwp*/,
6ac97d4c
DE
13137 0 /*search_cwd*/);
13138 }
13139
13140 return NULL;
ab5088bf
DE
13141}
13142
80626a55
DE
13143/* Initialize the use of the DWP file for the current objfile.
13144 By convention the name of the DWP file is ${objfile}.dwp.
13145 The result is NULL if it can't be found. */
a766d390 13146
400174b1 13147static std::unique_ptr<struct dwp_file>
976ca316 13148open_and_init_dwp_file (dwarf2_per_objfile *per_objfile)
80626a55 13149{
976ca316 13150 struct objfile *objfile = per_objfile->objfile;
80626a55 13151
82bf32bc
JK
13152 /* Try to find first .dwp for the binary file before any symbolic links
13153 resolving. */
6c447423
DE
13154
13155 /* If the objfile is a debug file, find the name of the real binary
13156 file and get the name of dwp file from there. */
d721ba37 13157 std::string dwp_name;
6c447423
DE
13158 if (objfile->separate_debug_objfile_backlink != NULL)
13159 {
13160 struct objfile *backlink = objfile->separate_debug_objfile_backlink;
13161 const char *backlink_basename = lbasename (backlink->original_name);
6c447423 13162
d721ba37 13163 dwp_name = ldirname (objfile->original_name) + SLASH_STRING + backlink_basename;
6c447423
DE
13164 }
13165 else
d721ba37
PA
13166 dwp_name = objfile->original_name;
13167
13168 dwp_name += ".dwp";
80626a55 13169
976ca316 13170 gdb_bfd_ref_ptr dbfd (open_dwp_file (per_objfile, dwp_name.c_str ()));
82bf32bc
JK
13171 if (dbfd == NULL
13172 && strcmp (objfile->original_name, objfile_name (objfile)) != 0)
13173 {
13174 /* Try to find .dwp for the binary file after gdb_realpath resolving. */
d721ba37
PA
13175 dwp_name = objfile_name (objfile);
13176 dwp_name += ".dwp";
976ca316 13177 dbfd = open_dwp_file (per_objfile, dwp_name.c_str ());
82bf32bc
JK
13178 }
13179
80626a55
DE
13180 if (dbfd == NULL)
13181 {
6f738b01
SM
13182 dwarf_read_debug_printf ("DWP file not found: %s", dwp_name.c_str ());
13183
400174b1 13184 return std::unique_ptr<dwp_file> ();
3019eac3 13185 }
400174b1
TT
13186
13187 const char *name = bfd_get_filename (dbfd.get ());
13188 std::unique_ptr<struct dwp_file> dwp_file
13189 (new struct dwp_file (name, std::move (dbfd)));
c906108c 13190
0a0f4c01 13191 dwp_file->num_sections = elf_numsections (dwp_file->dbfd);
80626a55 13192 dwp_file->elf_sections =
976ca316 13193 OBSTACK_CALLOC (&per_objfile->per_bfd->obstack,
80626a55
DE
13194 dwp_file->num_sections, asection *);
13195
5bb6e9dd
TT
13196 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13197 dwarf2_locate_common_dwp_sections (dwp_file->dbfd.get (), sec,
13198 dwp_file.get ());
80626a55 13199
976ca316 13200 dwp_file->cus = create_dwp_hash_table (per_objfile, dwp_file.get (), 0);
80626a55 13201
976ca316 13202 dwp_file->tus = create_dwp_hash_table (per_objfile, dwp_file.get (), 1);
80626a55 13203
73869dc2 13204 /* The DWP file version is stored in the hash table. Oh well. */
08302ed2
DE
13205 if (dwp_file->cus && dwp_file->tus
13206 && dwp_file->cus->version != dwp_file->tus->version)
73869dc2
DE
13207 {
13208 /* Technically speaking, we should try to limp along, but this is
fbcbc3fd 13209 pretty bizarre. We use pulongest here because that's the established
4d65956b 13210 portability solution (e.g, we cannot use %u for uint32_t). */
fbcbc3fd
DE
13211 error (_("Dwarf Error: DWP file CU version %s doesn't match"
13212 " TU version %s [in DWP file %s]"),
13213 pulongest (dwp_file->cus->version),
d721ba37 13214 pulongest (dwp_file->tus->version), dwp_name.c_str ());
73869dc2 13215 }
08302ed2
DE
13216
13217 if (dwp_file->cus)
13218 dwp_file->version = dwp_file->cus->version;
13219 else if (dwp_file->tus)
13220 dwp_file->version = dwp_file->tus->version;
13221 else
13222 dwp_file->version = 2;
73869dc2 13223
5bb6e9dd
TT
13224 for (asection *sec : gdb_bfd_sections (dwp_file->dbfd))
13225 {
13226 if (dwp_file->version == 2)
13227 dwarf2_locate_v2_dwp_sections (dwp_file->dbfd.get (), sec,
13228 dwp_file.get ());
13229 else
13230 dwarf2_locate_v5_dwp_sections (dwp_file->dbfd.get (), sec,
13231 dwp_file.get ());
13232 }
73869dc2 13233
298e9637
SM
13234 dwp_file->loaded_cus = allocate_dwp_loaded_cutus_table ();
13235 dwp_file->loaded_tus = allocate_dwp_loaded_cutus_table ();
80626a55 13236
6f738b01
SM
13237 dwarf_read_debug_printf ("DWP file found: %s", dwp_file->name);
13238 dwarf_read_debug_printf (" %s CUs, %s TUs",
13239 pulongest (dwp_file->cus ? dwp_file->cus->nr_units : 0),
13240 pulongest (dwp_file->tus ? dwp_file->tus->nr_units : 0));
80626a55
DE
13241
13242 return dwp_file;
3019eac3 13243}
c906108c 13244
ab5088bf
DE
13245/* Wrapper around open_and_init_dwp_file, only open it once. */
13246
13247static struct dwp_file *
976ca316 13248get_dwp_file (dwarf2_per_objfile *per_objfile)
ab5088bf 13249{
976ca316 13250 if (!per_objfile->per_bfd->dwp_checked)
ab5088bf 13251 {
976ca316
SM
13252 per_objfile->per_bfd->dwp_file = open_and_init_dwp_file (per_objfile);
13253 per_objfile->per_bfd->dwp_checked = 1;
ab5088bf 13254 }
976ca316 13255 return per_objfile->per_bfd->dwp_file.get ();
ab5088bf
DE
13256}
13257
80626a55
DE
13258/* Subroutine of lookup_dwo_comp_unit, lookup_dwo_type_unit.
13259 Look up the CU/TU with signature SIGNATURE, either in DWO file DWO_NAME
13260 or in the DWP file for the objfile, referenced by THIS_UNIT.
3019eac3 13261 If non-NULL, comp_dir is the DW_AT_comp_dir attribute.
80626a55
DE
13262 IS_DEBUG_TYPES is non-zero if reading a TU, otherwise read a CU.
13263
13264 This is called, for example, when wanting to read a variable with a
13265 complex location. Therefore we don't want to do file i/o for every call.
13266 Therefore we don't want to look for a DWO file on every call.
13267 Therefore we first see if we've already seen SIGNATURE in a DWP file,
13268 then we check if we've already seen DWO_NAME, and only THEN do we check
13269 for a DWO file.
13270
1c658ad5 13271 The result is a pointer to the dwo_unit object or NULL if we didn't find it
80626a55 13272 (dwo_id mismatch or couldn't find the DWO/DWP file). */
debd256d 13273
3019eac3 13274static struct dwo_unit *
4ab09049 13275lookup_dwo_cutu (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55 13276 ULONGEST signature, int is_debug_types)
3019eac3 13277{
976ca316
SM
13278 dwarf2_per_objfile *per_objfile = cu->per_objfile;
13279 struct objfile *objfile = per_objfile->objfile;
80626a55
DE
13280 const char *kind = is_debug_types ? "TU" : "CU";
13281 void **dwo_file_slot;
3019eac3 13282 struct dwo_file *dwo_file;
80626a55 13283 struct dwp_file *dwp_file;
cb1df416 13284
6a506a2d
DE
13285 /* First see if there's a DWP file.
13286 If we have a DWP file but didn't find the DWO inside it, don't
13287 look for the original DWO file. It makes gdb behave differently
13288 depending on whether one is debugging in the build tree. */
cf2c3c16 13289
976ca316 13290 dwp_file = get_dwp_file (per_objfile);
80626a55 13291 if (dwp_file != NULL)
cf2c3c16 13292 {
80626a55
DE
13293 const struct dwp_hash_table *dwp_htab =
13294 is_debug_types ? dwp_file->tus : dwp_file->cus;
13295
13296 if (dwp_htab != NULL)
13297 {
13298 struct dwo_unit *dwo_cutu =
976ca316
SM
13299 lookup_dwo_unit_in_dwp (per_objfile, dwp_file, comp_dir, signature,
13300 is_debug_types);
80626a55
DE
13301
13302 if (dwo_cutu != NULL)
13303 {
6f738b01
SM
13304 dwarf_read_debug_printf ("Virtual DWO %s %s found: @%s",
13305 kind, hex_string (signature),
13306 host_address_to_string (dwo_cutu));
13307
80626a55
DE
13308 return dwo_cutu;
13309 }
13310 }
13311 }
6a506a2d 13312 else
80626a55 13313 {
6a506a2d 13314 /* No DWP file, look for the DWO file. */
80626a55 13315
976ca316 13316 dwo_file_slot = lookup_dwo_file_slot (per_objfile, dwo_name, comp_dir);
6a506a2d 13317 if (*dwo_file_slot == NULL)
80626a55 13318 {
6a506a2d 13319 /* Read in the file and build a table of the CUs/TUs it contains. */
4ab09049 13320 *dwo_file_slot = open_and_init_dwo_file (cu, dwo_name, comp_dir);
19c3d4c9 13321 }
6a506a2d 13322 /* NOTE: This will be NULL if unable to open the file. */
9a3c8263 13323 dwo_file = (struct dwo_file *) *dwo_file_slot;
3019eac3 13324
6a506a2d 13325 if (dwo_file != NULL)
19c3d4c9 13326 {
6a506a2d
DE
13327 struct dwo_unit *dwo_cutu = NULL;
13328
13329 if (is_debug_types && dwo_file->tus)
13330 {
13331 struct dwo_unit find_dwo_cutu;
13332
13333 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13334 find_dwo_cutu.signature = signature;
9a3c8263 13335 dwo_cutu
b0b6a987
TT
13336 = (struct dwo_unit *) htab_find (dwo_file->tus.get (),
13337 &find_dwo_cutu);
6a506a2d 13338 }
33c5cd75 13339 else if (!is_debug_types && dwo_file->cus)
80626a55 13340 {
33c5cd75
DB
13341 struct dwo_unit find_dwo_cutu;
13342
13343 memset (&find_dwo_cutu, 0, sizeof (find_dwo_cutu));
13344 find_dwo_cutu.signature = signature;
b0b6a987 13345 dwo_cutu = (struct dwo_unit *)htab_find (dwo_file->cus.get (),
33c5cd75 13346 &find_dwo_cutu);
6a506a2d
DE
13347 }
13348
13349 if (dwo_cutu != NULL)
13350 {
6f738b01
SM
13351 dwarf_read_debug_printf ("DWO %s %s(%s) found: @%s",
13352 kind, dwo_name, hex_string (signature),
13353 host_address_to_string (dwo_cutu));
13354
6a506a2d 13355 return dwo_cutu;
80626a55
DE
13356 }
13357 }
2e276125 13358 }
9cdd5dbd 13359
80626a55
DE
13360 /* We didn't find it. This could mean a dwo_id mismatch, or
13361 someone deleted the DWO/DWP file, or the search path isn't set up
13362 correctly to find the file. */
13363
6f738b01
SM
13364 dwarf_read_debug_printf ("DWO %s %s(%s) not found",
13365 kind, dwo_name, hex_string (signature));
3019eac3 13366
6656a72d
DE
13367 /* This is a warning and not a complaint because it can be caused by
13368 pilot error (e.g., user accidentally deleting the DWO). */
43942612
DE
13369 {
13370 /* Print the name of the DWP file if we looked there, helps the user
13371 better diagnose the problem. */
791afaa2 13372 std::string dwp_text;
43942612
DE
13373
13374 if (dwp_file != NULL)
791afaa2
TT
13375 dwp_text = string_printf (" [in DWP file %s]",
13376 lbasename (dwp_file->name));
43942612 13377
9d8780f0 13378 warning (_("Could not find DWO %s %s(%s)%s referenced by %s at offset %s"
43942612 13379 " [in module %s]"),
4ab09049
SM
13380 kind, dwo_name, hex_string (signature), dwp_text.c_str (), kind,
13381 sect_offset_str (cu->per_cu->sect_off), objfile_name (objfile));
43942612 13382 }
3019eac3 13383 return NULL;
5fb290d7
DJ
13384}
13385
80626a55
DE
13386/* Lookup the DWO CU DWO_NAME/SIGNATURE referenced from THIS_CU.
13387 See lookup_dwo_cutu_unit for details. */
13388
13389static struct dwo_unit *
4ab09049 13390lookup_dwo_comp_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir,
80626a55
DE
13391 ULONGEST signature)
13392{
4ab09049
SM
13393 gdb_assert (!cu->per_cu->is_debug_types);
13394
13395 return lookup_dwo_cutu (cu, dwo_name, comp_dir, signature, 0);
80626a55
DE
13396}
13397
13398/* Lookup the DWO TU DWO_NAME/SIGNATURE referenced from THIS_TU.
13399 See lookup_dwo_cutu_unit for details. */
13400
13401static struct dwo_unit *
4ab09049 13402lookup_dwo_type_unit (dwarf2_cu *cu, const char *dwo_name, const char *comp_dir)
80626a55 13403{
4ab09049
SM
13404 gdb_assert (cu->per_cu->is_debug_types);
13405
13406 signatured_type *sig_type = (signatured_type *) cu->per_cu;
13407
13408 return lookup_dwo_cutu (cu, dwo_name, comp_dir, sig_type->signature, 1);
80626a55
DE
13409}
13410
89e63ee4
DE
13411/* Traversal function for queue_and_load_all_dwo_tus. */
13412
13413static int
13414queue_and_load_dwo_tu (void **slot, void *info)
13415{
13416 struct dwo_unit *dwo_unit = (struct dwo_unit *) *slot;
d460f660 13417 dwarf2_cu *cu = (dwarf2_cu *) info;
89e63ee4 13418 ULONGEST signature = dwo_unit->signature;
d460f660 13419 signatured_type *sig_type = lookup_dwo_signatured_type (cu, signature);
89e63ee4
DE
13420
13421 if (sig_type != NULL)
13422 {
13423 struct dwarf2_per_cu_data *sig_cu = &sig_type->per_cu;
13424
13425 /* We pass NULL for DEPENDENT_CU because we don't yet know if there's
13426 a real dependency of PER_CU on SIG_TYPE. That is detected later
13427 while processing PER_CU. */
120ce1b5 13428 if (maybe_queue_comp_unit (NULL, sig_cu, cu->per_objfile, cu->language))
d460f660
SM
13429 load_full_type_unit (sig_cu, cu->per_objfile);
13430 cu->per_cu->imported_symtabs_push (sig_cu);
89e63ee4
DE
13431 }
13432
13433 return 1;
13434}
13435
1b555f17 13436/* Queue all TUs contained in the DWO of CU to be read in.
89e63ee4
DE
13437 The DWO may have the only definition of the type, though it may not be
13438 referenced anywhere in PER_CU. Thus we have to load *all* its TUs.
13439 http://sourceware.org/bugzilla/show_bug.cgi?id=15021 */
13440
13441static void
1b555f17 13442queue_and_load_all_dwo_tus (dwarf2_cu *cu)
89e63ee4
DE
13443{
13444 struct dwo_unit *dwo_unit;
13445 struct dwo_file *dwo_file;
13446
1b555f17
SM
13447 gdb_assert (cu != nullptr);
13448 gdb_assert (!cu->per_cu->is_debug_types);
13449 gdb_assert (get_dwp_file (cu->per_objfile) == nullptr);
89e63ee4 13450
1b555f17 13451 dwo_unit = cu->dwo_unit;
89e63ee4
DE
13452 gdb_assert (dwo_unit != NULL);
13453
13454 dwo_file = dwo_unit->dwo_file;
13455 if (dwo_file->tus != NULL)
1b555f17 13456 htab_traverse_noresize (dwo_file->tus.get (), queue_and_load_dwo_tu, cu);
89e63ee4
DE
13457}
13458
3019eac3 13459/* Read in various DIEs. */
348e048f 13460
d389af10 13461/* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
3e43a32a
MS
13462 Inherit only the children of the DW_AT_abstract_origin DIE not being
13463 already referenced by DW_AT_abstract_origin from the children of the
13464 current DIE. */
d389af10
JK
13465
13466static void
13467inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
13468{
13469 struct die_info *child_die;
791afaa2 13470 sect_offset *offsetp;
d389af10
JK
13471 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
13472 struct die_info *origin_die;
13473 /* Iterator of the ORIGIN_DIE children. */
13474 struct die_info *origin_child_die;
d389af10 13475 struct attribute *attr;
cd02d79d
PA
13476 struct dwarf2_cu *origin_cu;
13477 struct pending **origin_previous_list_in_scope;
d389af10
JK
13478
13479 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
13480 if (!attr)
13481 return;
13482
cd02d79d
PA
13483 /* Note that following die references may follow to a die in a
13484 different cu. */
13485
13486 origin_cu = cu;
13487 origin_die = follow_die_ref (die, attr, &origin_cu);
13488
13489 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
13490 symbols in. */
13491 origin_previous_list_in_scope = origin_cu->list_in_scope;
13492 origin_cu->list_in_scope = cu->list_in_scope;
13493
edb3359d
DJ
13494 if (die->tag != origin_die->tag
13495 && !(die->tag == DW_TAG_inlined_subroutine
13496 && origin_die->tag == DW_TAG_subprogram))
b98664d3 13497 complaint (_("DIE %s and its abstract origin %s have different tags"),
9d8780f0
SM
13498 sect_offset_str (die->sect_off),
13499 sect_offset_str (origin_die->sect_off));
d389af10 13500
791afaa2 13501 std::vector<sect_offset> offsets;
d389af10 13502
3ea89b92
PMR
13503 for (child_die = die->child;
13504 child_die && child_die->tag;
436c571c 13505 child_die = child_die->sibling)
3ea89b92
PMR
13506 {
13507 struct die_info *child_origin_die;
13508 struct dwarf2_cu *child_origin_cu;
13509
13510 /* We are trying to process concrete instance entries:
216f72a1 13511 DW_TAG_call_site DIEs indeed have a DW_AT_abstract_origin tag, but
3ea89b92
PMR
13512 it's not relevant to our analysis here. i.e. detecting DIEs that are
13513 present in the abstract instance but not referenced in the concrete
13514 one. */
216f72a1 13515 if (child_die->tag == DW_TAG_call_site
dda83cd7 13516 || child_die->tag == DW_TAG_GNU_call_site)
3ea89b92
PMR
13517 continue;
13518
c38f313d
DJ
13519 /* For each CHILD_DIE, find the corresponding child of
13520 ORIGIN_DIE. If there is more than one layer of
13521 DW_AT_abstract_origin, follow them all; there shouldn't be,
13522 but GCC versions at least through 4.4 generate this (GCC PR
13523 40573). */
3ea89b92
PMR
13524 child_origin_die = child_die;
13525 child_origin_cu = cu;
c38f313d
DJ
13526 while (1)
13527 {
cd02d79d
PA
13528 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
13529 child_origin_cu);
c38f313d
DJ
13530 if (attr == NULL)
13531 break;
cd02d79d
PA
13532 child_origin_die = follow_die_ref (child_origin_die, attr,
13533 &child_origin_cu);
c38f313d
DJ
13534 }
13535
d389af10
JK
13536 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
13537 counterpart may exist. */
c38f313d 13538 if (child_origin_die != child_die)
d389af10 13539 {
edb3359d
DJ
13540 if (child_die->tag != child_origin_die->tag
13541 && !(child_die->tag == DW_TAG_inlined_subroutine
13542 && child_origin_die->tag == DW_TAG_subprogram))
b98664d3 13543 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13544 "different tags"),
9d8780f0
SM
13545 sect_offset_str (child_die->sect_off),
13546 sect_offset_str (child_origin_die->sect_off));
c38f313d 13547 if (child_origin_die->parent != origin_die)
b98664d3 13548 complaint (_("Child DIE %s and its abstract origin %s have "
9c541725 13549 "different parents"),
9d8780f0
SM
13550 sect_offset_str (child_die->sect_off),
13551 sect_offset_str (child_origin_die->sect_off));
c38f313d 13552 else
791afaa2 13553 offsets.push_back (child_origin_die->sect_off);
d389af10 13554 }
d389af10 13555 }
791afaa2
TT
13556 std::sort (offsets.begin (), offsets.end ());
13557 sect_offset *offsets_end = offsets.data () + offsets.size ();
13558 for (offsetp = offsets.data () + 1; offsetp < offsets_end; offsetp++)
9c541725 13559 if (offsetp[-1] == *offsetp)
b98664d3 13560 complaint (_("Multiple children of DIE %s refer "
9d8780f0
SM
13561 "to DIE %s as their abstract origin"),
13562 sect_offset_str (die->sect_off), sect_offset_str (*offsetp));
d389af10 13563
791afaa2 13564 offsetp = offsets.data ();
d389af10
JK
13565 origin_child_die = origin_die->child;
13566 while (origin_child_die && origin_child_die->tag)
13567 {
13568 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
b64f50a1 13569 while (offsetp < offsets_end
9c541725 13570 && *offsetp < origin_child_die->sect_off)
d389af10 13571 offsetp++;
b64f50a1 13572 if (offsetp >= offsets_end
9c541725 13573 || *offsetp > origin_child_die->sect_off)
d389af10 13574 {
adde2bff
DE
13575 /* Found that ORIGIN_CHILD_DIE is really not referenced.
13576 Check whether we're already processing ORIGIN_CHILD_DIE.
13577 This can happen with mutually referenced abstract_origins.
13578 PR 16581. */
13579 if (!origin_child_die->in_process)
13580 process_die (origin_child_die, origin_cu);
d389af10 13581 }
436c571c 13582 origin_child_die = origin_child_die->sibling;
d389af10 13583 }
cd02d79d 13584 origin_cu->list_in_scope = origin_previous_list_in_scope;
8d9a2568
KB
13585
13586 if (cu != origin_cu)
13587 compute_delayed_physnames (origin_cu);
d389af10
JK
13588}
13589
c906108c 13590static void
e7c27a73 13591read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13592{
5e22e966 13593 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13594 struct gdbarch *gdbarch = objfile->arch ();
fe978cb0 13595 struct context_stack *newobj;
c906108c
SS
13596 CORE_ADDR lowpc;
13597 CORE_ADDR highpc;
13598 struct die_info *child_die;
edb3359d 13599 struct attribute *attr, *call_line, *call_file;
15d034d0 13600 const char *name;
e142c38c 13601 CORE_ADDR baseaddr;
801e3a5b 13602 struct block *block;
edb3359d 13603 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
2f4732b0 13604 std::vector<struct symbol *> template_args;
34eaf542 13605 struct template_symbol *templ_func = NULL;
edb3359d
DJ
13606
13607 if (inlined_func)
13608 {
13609 /* If we do not have call site information, we can't show the
13610 caller of this inlined function. That's too confusing, so
13611 only use the scope for local variables. */
13612 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
13613 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
13614 if (call_line == NULL || call_file == NULL)
13615 {
13616 read_lexical_block_scope (die, cu);
13617 return;
13618 }
13619 }
c906108c 13620
b3b3bada 13621 baseaddr = objfile->text_section_offset ();
e142c38c 13622
94af9270 13623 name = dwarf2_name (die, cu);
c906108c 13624
e8d05480
JB
13625 /* Ignore functions with missing or empty names. These are actually
13626 illegal according to the DWARF standard. */
13627 if (name == NULL)
13628 {
b98664d3 13629 complaint (_("missing name for subprogram DIE at %s"),
9d8780f0 13630 sect_offset_str (die->sect_off));
e8d05480
JB
13631 return;
13632 }
13633
13634 /* Ignore functions with missing or invalid low and high pc attributes. */
3a2b436a 13635 if (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL)
e385593e 13636 <= PC_BOUNDS_INVALID)
e8d05480 13637 {
ae4d0c03 13638 attr = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 13639 if (attr == nullptr || !attr->as_boolean ())
b98664d3 13640 complaint (_("cannot get low and high bounds "
9d8780f0
SM
13641 "for subprogram DIE at %s"),
13642 sect_offset_str (die->sect_off));
e8d05480
JB
13643 return;
13644 }
c906108c 13645
3e29f34a
MR
13646 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13647 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13648
34eaf542
TT
13649 /* If we have any template arguments, then we must allocate a
13650 different sort of symbol. */
436c571c 13651 for (child_die = die->child; child_die; child_die = child_die->sibling)
34eaf542
TT
13652 {
13653 if (child_die->tag == DW_TAG_template_type_param
13654 || child_die->tag == DW_TAG_template_value_param)
13655 {
8c14c3a3 13656 templ_func = new (&objfile->objfile_obstack) template_symbol;
cf724bc9 13657 templ_func->subclass = SYMBOL_TEMPLATE;
34eaf542
TT
13658 break;
13659 }
13660 }
13661
c24bdb02 13662 newobj = cu->get_builder ()->push_context (0, lowpc);
5e2db402
TT
13663 newobj->name = new_symbol (die, read_type_die (die, cu), cu,
13664 (struct symbol *) templ_func);
4c2df51b 13665
81873cc8 13666 if (dwarf2_flag_true_p (die, DW_AT_main_subprogram, cu))
987012b8 13667 set_objfile_main_name (objfile, newobj->name->linkage_name (),
81873cc8
TV
13668 cu->language);
13669
4cecd739
DJ
13670 /* If there is a location expression for DW_AT_frame_base, record
13671 it. */
e142c38c 13672 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
435d3d88 13673 if (attr != nullptr)
fe978cb0 13674 dwarf2_symbol_mark_computed (attr, newobj->name, cu, 1);
4c2df51b 13675
63e43d3a
PMR
13676 /* If there is a location for the static link, record it. */
13677 newobj->static_link = NULL;
13678 attr = dwarf2_attr (die, DW_AT_static_link, cu);
435d3d88 13679 if (attr != nullptr)
63e43d3a 13680 {
224c3ddb
SM
13681 newobj->static_link
13682 = XOBNEW (&objfile->objfile_obstack, struct dynamic_prop);
9a49df9d 13683 attr_to_dynamic_prop (attr, die, cu, newobj->static_link,
293e7e51 13684 cu->addr_type ());
63e43d3a
PMR
13685 }
13686
c24bdb02 13687 cu->list_in_scope = cu->get_builder ()->get_local_symbols ();
c906108c 13688
639d11d3 13689 if (die->child != NULL)
c906108c 13690 {
639d11d3 13691 child_die = die->child;
c906108c
SS
13692 while (child_die && child_die->tag)
13693 {
34eaf542
TT
13694 if (child_die->tag == DW_TAG_template_type_param
13695 || child_die->tag == DW_TAG_template_value_param)
13696 {
13697 struct symbol *arg = new_symbol (child_die, NULL, cu);
13698
f1078f66 13699 if (arg != NULL)
2f4732b0 13700 template_args.push_back (arg);
34eaf542
TT
13701 }
13702 else
13703 process_die (child_die, cu);
436c571c 13704 child_die = child_die->sibling;
c906108c
SS
13705 }
13706 }
13707
d389af10
JK
13708 inherit_abstract_dies (die, cu);
13709
4a811a97
UW
13710 /* If we have a DW_AT_specification, we might need to import using
13711 directives from the context of the specification DIE. See the
13712 comment in determine_prefix. */
13713 if (cu->language == language_cplus
13714 && dwarf2_attr (die, DW_AT_specification, cu))
13715 {
13716 struct dwarf2_cu *spec_cu = cu;
13717 struct die_info *spec_die = die_specification (die, &spec_cu);
13718
13719 while (spec_die)
13720 {
13721 child_die = spec_die->child;
13722 while (child_die && child_die->tag)
13723 {
13724 if (child_die->tag == DW_TAG_imported_module)
13725 process_die (child_die, spec_cu);
436c571c 13726 child_die = child_die->sibling;
4a811a97
UW
13727 }
13728
13729 /* In some cases, GCC generates specification DIEs that
13730 themselves contain DW_AT_specification attributes. */
13731 spec_die = die_specification (spec_die, &spec_cu);
13732 }
13733 }
13734
c24bdb02 13735 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13736 /* Make a block for the local symbols within. */
c24bdb02 13737 block = cu->get_builder ()->finish_block (cstk.name, cstk.old_blocks,
804d2729 13738 cstk.static_link, lowpc, highpc);
801e3a5b 13739
df8a16a1 13740 /* For C++, set the block's scope. */
45280282
IB
13741 if ((cu->language == language_cplus
13742 || cu->language == language_fortran
c44af4eb
TT
13743 || cu->language == language_d
13744 || cu->language == language_rust)
4d4ec4e5 13745 && cu->processing_has_namespace_info)
195a3f6c
TT
13746 block_set_scope (block, determine_prefix (die, cu),
13747 &objfile->objfile_obstack);
df8a16a1 13748
801e3a5b
JB
13749 /* If we have address ranges, record them. */
13750 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6e70227d 13751
a60f3166 13752 gdbarch_make_symbol_special (gdbarch, cstk.name, objfile);
3e29f34a 13753
34eaf542 13754 /* Attach template arguments to function. */
2f4732b0 13755 if (!template_args.empty ())
34eaf542
TT
13756 {
13757 gdb_assert (templ_func != NULL);
13758
2f4732b0 13759 templ_func->n_template_arguments = template_args.size ();
34eaf542 13760 templ_func->template_arguments
dda83cd7 13761 = XOBNEWVEC (&objfile->objfile_obstack, struct symbol *,
8d749320 13762 templ_func->n_template_arguments);
34eaf542 13763 memcpy (templ_func->template_arguments,
2f4732b0 13764 template_args.data (),
34eaf542 13765 (templ_func->n_template_arguments * sizeof (struct symbol *)));
3e1d3d8c
TT
13766
13767 /* Make sure that the symtab is set on the new symbols. Even
13768 though they don't appear in this symtab directly, other parts
13769 of gdb assume that symbols do, and this is reasonably
13770 true. */
8634679f 13771 for (symbol *sym : template_args)
3e1d3d8c 13772 symbol_set_symtab (sym, symbol_symtab (templ_func));
34eaf542
TT
13773 }
13774
208d8187
JB
13775 /* In C++, we can have functions nested inside functions (e.g., when
13776 a function declares a class that has methods). This means that
13777 when we finish processing a function scope, we may need to go
13778 back to building a containing block's symbol lists. */
c24bdb02
KS
13779 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13780 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
208d8187 13781
921e78cf
JB
13782 /* If we've finished processing a top-level function, subsequent
13783 symbols go in the file symbol list. */
c24bdb02
KS
13784 if (cu->get_builder ()->outermost_context_p ())
13785 cu->list_in_scope = cu->get_builder ()->get_file_symbols ();
c906108c
SS
13786}
13787
13788/* Process all the DIES contained within a lexical block scope. Start
13789 a new scope, process the dies, and then close the scope. */
13790
13791static void
e7c27a73 13792read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
c906108c 13793{
5e22e966 13794 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 13795 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
13796 CORE_ADDR lowpc, highpc;
13797 struct die_info *child_die;
e142c38c
DJ
13798 CORE_ADDR baseaddr;
13799
b3b3bada 13800 baseaddr = objfile->text_section_offset ();
c906108c
SS
13801
13802 /* Ignore blocks with missing or invalid low and high pc attributes. */
af34e669
DJ
13803 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
13804 as multiple lexical blocks? Handling children in a sane way would
6e70227d 13805 be nasty. Might be easier to properly extend generic blocks to
af34e669 13806 describe ranges. */
e385593e
JK
13807 switch (dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
13808 {
13809 case PC_BOUNDS_NOT_PRESENT:
13810 /* DW_TAG_lexical_block has no attributes, process its children as if
13811 there was no wrapping by that DW_TAG_lexical_block.
13812 GCC does no longer produces such DWARF since GCC r224161. */
13813 for (child_die = die->child;
13814 child_die != NULL && child_die->tag;
436c571c 13815 child_die = child_die->sibling)
4f7bc5ed
TT
13816 {
13817 /* We might already be processing this DIE. This can happen
13818 in an unusual circumstance -- where a subroutine A
13819 appears lexically in another subroutine B, but A actually
13820 inlines B. The recursion is broken here, rather than in
13821 inherit_abstract_dies, because it seems better to simply
13822 drop concrete children here. */
13823 if (!child_die->in_process)
13824 process_die (child_die, cu);
13825 }
e385593e
JK
13826 return;
13827 case PC_BOUNDS_INVALID:
13828 return;
13829 }
3e29f34a
MR
13830 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
13831 highpc = gdbarch_adjust_dwarf2_addr (gdbarch, highpc + baseaddr);
c906108c 13832
c24bdb02 13833 cu->get_builder ()->push_context (0, lowpc);
639d11d3 13834 if (die->child != NULL)
c906108c 13835 {
639d11d3 13836 child_die = die->child;
c906108c
SS
13837 while (child_die && child_die->tag)
13838 {
e7c27a73 13839 process_die (child_die, cu);
436c571c 13840 child_die = child_die->sibling;
c906108c
SS
13841 }
13842 }
3ea89b92 13843 inherit_abstract_dies (die, cu);
c24bdb02 13844 struct context_stack cstk = cu->get_builder ()->pop_context ();
c906108c 13845
c24bdb02
KS
13846 if (*cu->get_builder ()->get_local_symbols () != NULL
13847 || (*cu->get_builder ()->get_local_using_directives ()) != NULL)
c906108c 13848 {
801e3a5b 13849 struct block *block
dda83cd7 13850 = cu->get_builder ()->finish_block (0, cstk.old_blocks, NULL,
804d2729 13851 cstk.start_addr, highpc);
801e3a5b
JB
13852
13853 /* Note that recording ranges after traversing children, as we
dda83cd7
SM
13854 do here, means that recording a parent's ranges entails
13855 walking across all its children's ranges as they appear in
13856 the address map, which is quadratic behavior.
13857
13858 It would be nicer to record the parent's ranges before
13859 traversing its children, simply overriding whatever you find
13860 there. But since we don't even decide whether to create a
13861 block until after we've traversed its children, that's hard
13862 to do. */
801e3a5b 13863 dwarf2_record_block_ranges (die, block, baseaddr, cu);
c906108c 13864 }
c24bdb02
KS
13865 *cu->get_builder ()->get_local_symbols () = cstk.locals;
13866 cu->get_builder ()->set_local_using_directives (cstk.local_using_directives);
c906108c
SS
13867}
13868
216f72a1 13869/* Read in DW_TAG_call_site and insert it to CU->call_site_htab. */
96408a79
SA
13870
13871static void
13872read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
13873{
5e22e966 13874 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 13875 struct objfile *objfile = per_objfile->objfile;
08feed99 13876 struct gdbarch *gdbarch = objfile->arch ();
96408a79
SA
13877 CORE_ADDR pc, baseaddr;
13878 struct attribute *attr;
13879 struct call_site *call_site, call_site_local;
13880 void **slot;
13881 int nparams;
13882 struct die_info *child_die;
13883
b3b3bada 13884 baseaddr = objfile->text_section_offset ();
96408a79 13885
216f72a1
JK
13886 attr = dwarf2_attr (die, DW_AT_call_return_pc, cu);
13887 if (attr == NULL)
13888 {
13889 /* This was a pre-DWARF-5 GNU extension alias
13890 for DW_AT_call_return_pc. */
13891 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
13892 }
96408a79
SA
13893 if (!attr)
13894 {
b98664d3 13895 complaint (_("missing DW_AT_call_return_pc for DW_TAG_call_site "
9d8780f0
SM
13896 "DIE %s [in module %s]"),
13897 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13898 return;
13899 }
95f982e5 13900 pc = attr->as_address () + baseaddr;
3e29f34a 13901 pc = gdbarch_adjust_dwarf2_addr (gdbarch, pc);
96408a79
SA
13902
13903 if (cu->call_site_htab == NULL)
13904 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
13905 NULL, &objfile->objfile_obstack,
13906 hashtab_obstack_allocate, NULL);
13907 call_site_local.pc = pc;
13908 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
13909 if (*slot != NULL)
13910 {
b98664d3 13911 complaint (_("Duplicate PC %s for DW_TAG_call_site "
9d8780f0
SM
13912 "DIE %s [in module %s]"),
13913 paddress (gdbarch, pc), sect_offset_str (die->sect_off),
4262abfb 13914 objfile_name (objfile));
96408a79
SA
13915 return;
13916 }
13917
13918 /* Count parameters at the caller. */
13919
13920 nparams = 0;
13921 for (child_die = die->child; child_die && child_die->tag;
436c571c 13922 child_die = child_die->sibling)
96408a79 13923 {
216f72a1 13924 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 13925 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79 13926 {
b98664d3 13927 complaint (_("Tag %d is not DW_TAG_call_site_parameter in "
9d8780f0
SM
13928 "DW_TAG_call_site child DIE %s [in module %s]"),
13929 child_die->tag, sect_offset_str (child_die->sect_off),
4262abfb 13930 objfile_name (objfile));
96408a79
SA
13931 continue;
13932 }
13933
13934 nparams++;
13935 }
13936
224c3ddb
SM
13937 call_site
13938 = ((struct call_site *)
13939 obstack_alloc (&objfile->objfile_obstack,
13940 sizeof (*call_site)
13941 + (sizeof (*call_site->parameter) * (nparams - 1))));
96408a79
SA
13942 *slot = call_site;
13943 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
13944 call_site->pc = pc;
13945
216f72a1
JK
13946 if (dwarf2_flag_true_p (die, DW_AT_call_tail_call, cu)
13947 || dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
96408a79
SA
13948 {
13949 struct die_info *func_die;
13950
13951 /* Skip also over DW_TAG_inlined_subroutine. */
13952 for (func_die = die->parent;
13953 func_die && func_die->tag != DW_TAG_subprogram
13954 && func_die->tag != DW_TAG_subroutine_type;
13955 func_die = func_die->parent);
13956
216f72a1
JK
13957 /* DW_AT_call_all_calls is a superset
13958 of DW_AT_call_all_tail_calls. */
96408a79 13959 if (func_die
dda83cd7
SM
13960 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_calls, cu)
13961 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
216f72a1 13962 && !dwarf2_flag_true_p (func_die, DW_AT_call_all_tail_calls, cu)
96408a79
SA
13963 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
13964 {
13965 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
13966 not complete. But keep CALL_SITE for look ups via call_site_htab,
13967 both the initial caller containing the real return address PC and
13968 the final callee containing the current PC of a chain of tail
13969 calls do not need to have the tail call list complete. But any
13970 function candidate for a virtual tail call frame searched via
13971 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
13972 determined unambiguously. */
13973 }
13974 else
13975 {
13976 struct type *func_type = NULL;
13977
13978 if (func_die)
13979 func_type = get_die_type (func_die, cu);
13980 if (func_type != NULL)
13981 {
78134374 13982 gdb_assert (func_type->code () == TYPE_CODE_FUNC);
96408a79
SA
13983
13984 /* Enlist this call site to the function. */
13985 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
13986 TYPE_TAIL_CALL_LIST (func_type) = call_site;
13987 }
13988 else
b98664d3 13989 complaint (_("Cannot find function owning DW_TAG_call_site "
9d8780f0
SM
13990 "DIE %s [in module %s]"),
13991 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
13992 }
13993 }
13994
216f72a1
JK
13995 attr = dwarf2_attr (die, DW_AT_call_target, cu);
13996 if (attr == NULL)
13997 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
13998 if (attr == NULL)
13999 attr = dwarf2_attr (die, DW_AT_call_origin, cu);
96408a79 14000 if (attr == NULL)
216f72a1
JK
14001 {
14002 /* This was a pre-DWARF-5 GNU extension alias for DW_AT_call_origin. */
14003 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14004 }
96408a79 14005 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
9d2246fc 14006 if (!attr || (attr->form_is_block () && attr->as_block ()->size == 0))
96408a79 14007 /* Keep NULL DWARF_BLOCK. */;
4fc6c0d5 14008 else if (attr->form_is_block ())
96408a79
SA
14009 {
14010 struct dwarf2_locexpr_baton *dlbaton;
9d2246fc 14011 struct dwarf_block *block = attr->as_block ();
96408a79 14012
8d749320 14013 dlbaton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
9d2246fc
TT
14014 dlbaton->data = block->data;
14015 dlbaton->size = block->size;
a50264ba 14016 dlbaton->per_objfile = per_objfile;
96408a79
SA
14017 dlbaton->per_cu = cu->per_cu;
14018
14019 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
14020 }
cd6c91b4 14021 else if (attr->form_is_ref ())
96408a79 14022 {
96408a79
SA
14023 struct dwarf2_cu *target_cu = cu;
14024 struct die_info *target_die;
14025
ac9ec31b 14026 target_die = follow_die_ref (die, attr, &target_cu);
5e22e966 14027 gdb_assert (target_cu->per_objfile->objfile == objfile);
96408a79
SA
14028 if (die_is_declaration (target_die, target_cu))
14029 {
7d45c7c3 14030 const char *target_physname;
9112db09
JK
14031
14032 /* Prefer the mangled name; otherwise compute the demangled one. */
73b9be8b 14033 target_physname = dw2_linkage_name (target_die, target_cu);
7d45c7c3 14034 if (target_physname == NULL)
9112db09 14035 target_physname = dwarf2_physname (NULL, target_die, target_cu);
96408a79 14036 if (target_physname == NULL)
b98664d3 14037 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14038 "physname, for referencing DIE %s [in module %s]"),
9d8780f0 14039 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14040 else
7d455152 14041 SET_FIELD_PHYSNAME (call_site->target, target_physname);
96408a79
SA
14042 }
14043 else
14044 {
14045 CORE_ADDR lowpc;
14046
14047 /* DW_AT_entry_pc should be preferred. */
3a2b436a 14048 if (dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL)
e385593e 14049 <= PC_BOUNDS_INVALID)
b98664d3 14050 complaint (_("DW_AT_call_target target DIE has invalid "
dda83cd7 14051 "low pc, for referencing DIE %s [in module %s]"),
9d8780f0 14052 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79 14053 else
3e29f34a
MR
14054 {
14055 lowpc = gdbarch_adjust_dwarf2_addr (gdbarch, lowpc + baseaddr);
14056 SET_FIELD_PHYSADDR (call_site->target, lowpc);
14057 }
96408a79
SA
14058 }
14059 }
14060 else
b98664d3 14061 complaint (_("DW_TAG_call_site DW_AT_call_target is neither "
9d8780f0
SM
14062 "block nor reference, for DIE %s [in module %s]"),
14063 sect_offset_str (die->sect_off), objfile_name (objfile));
96408a79
SA
14064
14065 call_site->per_cu = cu->per_cu;
9f47c707 14066 call_site->per_objfile = per_objfile;
96408a79
SA
14067
14068 for (child_die = die->child;
14069 child_die && child_die->tag;
436c571c 14070 child_die = child_die->sibling)
96408a79 14071 {
96408a79 14072 struct call_site_parameter *parameter;
1788b2d3 14073 struct attribute *loc, *origin;
96408a79 14074
216f72a1 14075 if (child_die->tag != DW_TAG_call_site_parameter
dda83cd7 14076 && child_die->tag != DW_TAG_GNU_call_site_parameter)
96408a79
SA
14077 {
14078 /* Already printed the complaint above. */
14079 continue;
14080 }
14081
14082 gdb_assert (call_site->parameter_count < nparams);
14083 parameter = &call_site->parameter[call_site->parameter_count];
14084
1788b2d3
JK
14085 /* DW_AT_location specifies the register number or DW_AT_abstract_origin
14086 specifies DW_TAG_formal_parameter. Value of the data assumed for the
216f72a1 14087 register is contained in DW_AT_call_value. */
96408a79 14088
24c5c679 14089 loc = dwarf2_attr (child_die, DW_AT_location, cu);
216f72a1
JK
14090 origin = dwarf2_attr (child_die, DW_AT_call_parameter, cu);
14091 if (origin == NULL)
14092 {
14093 /* This was a pre-DWARF-5 GNU extension alias
14094 for DW_AT_call_parameter. */
14095 origin = dwarf2_attr (child_die, DW_AT_abstract_origin, cu);
14096 }
cd6c91b4 14097 if (loc == NULL && origin != NULL && origin->form_is_ref ())
1788b2d3 14098 {
1788b2d3 14099 parameter->kind = CALL_SITE_PARAMETER_PARAM_OFFSET;
9c541725 14100
0826b30a 14101 sect_offset sect_off = origin->get_ref_die_offset ();
4057dfde 14102 if (!cu->header.offset_in_cu_p (sect_off))
d76b7dbc
JK
14103 {
14104 /* As DW_OP_GNU_parameter_ref uses CU-relative offset this
14105 binding can be done only inside one CU. Such referenced DIE
14106 therefore cannot be even moved to DW_TAG_partial_unit. */
b98664d3 14107 complaint (_("DW_AT_call_parameter offset is not in CU for "
9d8780f0
SM
14108 "DW_TAG_call_site child DIE %s [in module %s]"),
14109 sect_offset_str (child_die->sect_off),
9c541725 14110 objfile_name (objfile));
d76b7dbc
JK
14111 continue;
14112 }
9c541725
PA
14113 parameter->u.param_cu_off
14114 = (cu_offset) (sect_off - cu->header.sect_off);
1788b2d3 14115 }
4fc6c0d5 14116 else if (loc == NULL || origin != NULL || !loc->form_is_block ())
96408a79 14117 {
b98664d3 14118 complaint (_("No DW_FORM_block* DW_AT_location for "
9d8780f0
SM
14119 "DW_TAG_call_site child DIE %s [in module %s]"),
14120 sect_offset_str (child_die->sect_off), objfile_name (objfile));
96408a79
SA
14121 continue;
14122 }
24c5c679 14123 else
96408a79 14124 {
9d2246fc
TT
14125 struct dwarf_block *block = loc->as_block ();
14126
24c5c679 14127 parameter->u.dwarf_reg = dwarf_block_to_dwarf_reg
9d2246fc 14128 (block->data, &block->data[block->size]);
24c5c679
JK
14129 if (parameter->u.dwarf_reg != -1)
14130 parameter->kind = CALL_SITE_PARAMETER_DWARF_REG;
9d2246fc
TT
14131 else if (dwarf_block_to_sp_offset (gdbarch, block->data,
14132 &block->data[block->size],
24c5c679
JK
14133 &parameter->u.fb_offset))
14134 parameter->kind = CALL_SITE_PARAMETER_FB_OFFSET;
14135 else
14136 {
b98664d3 14137 complaint (_("Only single DW_OP_reg or DW_OP_fbreg is supported "
24c5c679 14138 "for DW_FORM_block* DW_AT_location is supported for "
9d8780f0 14139 "DW_TAG_call_site child DIE %s "
24c5c679 14140 "[in module %s]"),
9d8780f0 14141 sect_offset_str (child_die->sect_off),
9c541725 14142 objfile_name (objfile));
24c5c679
JK
14143 continue;
14144 }
96408a79
SA
14145 }
14146
216f72a1
JK
14147 attr = dwarf2_attr (child_die, DW_AT_call_value, cu);
14148 if (attr == NULL)
14149 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
4fc6c0d5 14150 if (attr == NULL || !attr->form_is_block ())
96408a79 14151 {
b98664d3 14152 complaint (_("No DW_FORM_block* DW_AT_call_value for "
9d8780f0
SM
14153 "DW_TAG_call_site child DIE %s [in module %s]"),
14154 sect_offset_str (child_die->sect_off),
9c541725 14155 objfile_name (objfile));
96408a79
SA
14156 continue;
14157 }
9d2246fc
TT
14158
14159 struct dwarf_block *block = attr->as_block ();
14160 parameter->value = block->data;
14161 parameter->value_size = block->size;
96408a79
SA
14162
14163 /* Parameters are not pre-cleared by memset above. */
14164 parameter->data_value = NULL;
14165 parameter->data_value_size = 0;
14166 call_site->parameter_count++;
14167
216f72a1
JK
14168 attr = dwarf2_attr (child_die, DW_AT_call_data_value, cu);
14169 if (attr == NULL)
14170 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
435d3d88 14171 if (attr != nullptr)
96408a79 14172 {
4fc6c0d5 14173 if (!attr->form_is_block ())
b98664d3 14174 complaint (_("No DW_FORM_block* DW_AT_call_data_value for "
9d8780f0
SM
14175 "DW_TAG_call_site child DIE %s [in module %s]"),
14176 sect_offset_str (child_die->sect_off),
9c541725 14177 objfile_name (objfile));
96408a79
SA
14178 else
14179 {
9d2246fc
TT
14180 block = attr->as_block ();
14181 parameter->data_value = block->data;
14182 parameter->data_value_size = block->size;
96408a79
SA
14183 }
14184 }
14185 }
14186}
14187
71a3c369
TT
14188/* Helper function for read_variable. If DIE represents a virtual
14189 table, then return the type of the concrete object that is
14190 associated with the virtual table. Otherwise, return NULL. */
14191
14192static struct type *
14193rust_containing_type (struct die_info *die, struct dwarf2_cu *cu)
14194{
14195 struct attribute *attr = dwarf2_attr (die, DW_AT_type, cu);
14196 if (attr == NULL)
14197 return NULL;
14198
14199 /* Find the type DIE. */
14200 struct die_info *type_die = NULL;
14201 struct dwarf2_cu *type_cu = cu;
14202
cd6c91b4 14203 if (attr->form_is_ref ())
71a3c369
TT
14204 type_die = follow_die_ref (die, attr, &type_cu);
14205 if (type_die == NULL)
14206 return NULL;
14207
14208 if (dwarf2_attr (type_die, DW_AT_containing_type, type_cu) == NULL)
14209 return NULL;
14210 return die_containing_type (type_die, type_cu);
14211}
14212
14213/* Read a variable (DW_TAG_variable) DIE and create a new symbol. */
14214
14215static void
14216read_variable (struct die_info *die, struct dwarf2_cu *cu)
14217{
14218 struct rust_vtable_symbol *storage = NULL;
14219
14220 if (cu->language == language_rust)
14221 {
14222 struct type *containing_type = rust_containing_type (die, cu);
14223
14224 if (containing_type != NULL)
14225 {
5e22e966 14226 struct objfile *objfile = cu->per_objfile->objfile;
71a3c369 14227
8c14c3a3 14228 storage = new (&objfile->objfile_obstack) rust_vtable_symbol;
71a3c369 14229 storage->concrete_type = containing_type;
cf724bc9 14230 storage->subclass = SYMBOL_RUST_VTABLE;
71a3c369
TT
14231 }
14232 }
14233
e4a62c65
TV
14234 struct symbol *res = new_symbol (die, NULL, cu, storage);
14235 struct attribute *abstract_origin
14236 = dwarf2_attr (die, DW_AT_abstract_origin, cu);
14237 struct attribute *loc = dwarf2_attr (die, DW_AT_location, cu);
14238 if (res == NULL && loc && abstract_origin)
14239 {
14240 /* We have a variable without a name, but with a location and an abstract
14241 origin. This may be a concrete instance of an abstract variable
14242 referenced from an DW_OP_GNU_variable_value, so save it to find it back
14243 later. */
14244 struct dwarf2_cu *origin_cu = cu;
14245 struct die_info *origin_die
14246 = follow_die_ref (die, abstract_origin, &origin_cu);
5e22e966
SM
14247 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14248 per_objfile->per_bfd->abstract_to_concrete
14249 [origin_die->sect_off].push_back (die->sect_off);
e4a62c65 14250 }
71a3c369
TT
14251}
14252
43988095
JK
14253/* Call CALLBACK from DW_AT_ranges attribute value OFFSET
14254 reading .debug_rnglists.
14255 Callback's type should be:
14256 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
14257 Return true if the attributes are present and valid, otherwise,
14258 return false. */
14259
14260template <typename Callback>
14261static bool
14262dwarf2_rnglists_process (unsigned offset, struct dwarf2_cu *cu,
d0ce17d8 14263 dwarf_tag tag, Callback &&callback)
43988095 14264{
976ca316
SM
14265 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14266 struct objfile *objfile = per_objfile->objfile;
43988095 14267 bfd *obfd = objfile->obfd;
43988095 14268 /* Base address selection entry. */
2b24b6e4 14269 gdb::optional<CORE_ADDR> base;
43988095 14270 const gdb_byte *buffer;
43988095
JK
14271 CORE_ADDR baseaddr;
14272 bool overflow = false;
d0ce17d8
CT
14273 ULONGEST addr_index;
14274 struct dwarf2_section_info *rnglists_section;
43988095 14275
43988095 14276 base = cu->base_address;
d0ce17d8
CT
14277 rnglists_section = cu_debug_rnglists_section (cu, tag);
14278 rnglists_section->read (objfile);
43988095 14279
d0ce17d8 14280 if (offset >= rnglists_section->size)
43988095 14281 {
b98664d3 14282 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43988095
JK
14283 offset);
14284 return false;
14285 }
d0ce17d8 14286 buffer = rnglists_section->buffer + offset;
43988095 14287
b3b3bada 14288 baseaddr = objfile->text_section_offset ();
43988095
JK
14289
14290 while (1)
14291 {
7814882a
JK
14292 /* Initialize it due to a false compiler warning. */
14293 CORE_ADDR range_beginning = 0, range_end = 0;
d0ce17d8
CT
14294 const gdb_byte *buf_end = (rnglists_section->buffer
14295 + rnglists_section->size);
43988095
JK
14296 unsigned int bytes_read;
14297
14298 if (buffer == buf_end)
14299 {
14300 overflow = true;
14301 break;
14302 }
14303 const auto rlet = static_cast<enum dwarf_range_list_entry>(*buffer++);
14304 switch (rlet)
14305 {
14306 case DW_RLE_end_of_list:
14307 break;
14308 case DW_RLE_base_address:
14309 if (buffer + cu->header.addr_size > buf_end)
14310 {
14311 overflow = true;
14312 break;
14313 }
c8a7a66f 14314 base = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14315 buffer += bytes_read;
14316 break;
dda83cd7
SM
14317 case DW_RLE_base_addressx:
14318 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14319 buffer += bytes_read;
14320 base = read_addr_index (cu, addr_index);
14321 break;
43988095
JK
14322 case DW_RLE_start_length:
14323 if (buffer + cu->header.addr_size > buf_end)
14324 {
14325 overflow = true;
14326 break;
14327 }
c8a7a66f
TT
14328 range_beginning = cu->header.read_address (obfd, buffer,
14329 &bytes_read);
43988095
JK
14330 buffer += bytes_read;
14331 range_end = (range_beginning
14332 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14333 buffer += bytes_read;
14334 if (buffer > buf_end)
14335 {
14336 overflow = true;
14337 break;
14338 }
14339 break;
d0ce17d8 14340 case DW_RLE_startx_length:
dda83cd7
SM
14341 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14342 buffer += bytes_read;
14343 range_beginning = read_addr_index (cu, addr_index);
14344 if (buffer > buf_end)
14345 {
14346 overflow = true;
14347 break;
14348 }
14349 range_end = (range_beginning
14350 + read_unsigned_leb128 (obfd, buffer, &bytes_read));
14351 buffer += bytes_read;
14352 break;
43988095
JK
14353 case DW_RLE_offset_pair:
14354 range_beginning = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14355 buffer += bytes_read;
14356 if (buffer > buf_end)
14357 {
14358 overflow = true;
14359 break;
14360 }
14361 range_end = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14362 buffer += bytes_read;
14363 if (buffer > buf_end)
14364 {
14365 overflow = true;
14366 break;
14367 }
14368 break;
14369 case DW_RLE_start_end:
14370 if (buffer + 2 * cu->header.addr_size > buf_end)
14371 {
14372 overflow = true;
14373 break;
14374 }
c8a7a66f
TT
14375 range_beginning = cu->header.read_address (obfd, buffer,
14376 &bytes_read);
43988095 14377 buffer += bytes_read;
c8a7a66f 14378 range_end = cu->header.read_address (obfd, buffer, &bytes_read);
43988095
JK
14379 buffer += bytes_read;
14380 break;
d0ce17d8 14381 case DW_RLE_startx_endx:
dda83cd7
SM
14382 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14383 buffer += bytes_read;
14384 range_beginning = read_addr_index (cu, addr_index);
14385 if (buffer > buf_end)
14386 {
14387 overflow = true;
14388 break;
14389 }
14390 addr_index = read_unsigned_leb128 (obfd, buffer, &bytes_read);
14391 buffer += bytes_read;
14392 range_end = read_addr_index (cu, addr_index);
14393 break;
43988095 14394 default:
b98664d3 14395 complaint (_("Invalid .debug_rnglists data (no base address)"));
43988095
JK
14396 return false;
14397 }
14398 if (rlet == DW_RLE_end_of_list || overflow)
14399 break;
14400 if (rlet == DW_RLE_base_address)
14401 continue;
14402
43988095
JK
14403 if (range_beginning > range_end)
14404 {
14405 /* Inverted range entries are invalid. */
b98664d3 14406 complaint (_("Invalid .debug_rnglists data (inverted range)"));
43988095
JK
14407 return false;
14408 }
14409
14410 /* Empty range entries have no effect. */
14411 if (range_beginning == range_end)
14412 continue;
14413
d0ce17d8
CT
14414 /* Only DW_RLE_offset_pair needs the base address added. */
14415 if (rlet == DW_RLE_offset_pair)
14416 {
14417 if (!base.has_value ())
14418 {
14419 /* We have no valid base address for the DW_RLE_offset_pair. */
14420 complaint (_("Invalid .debug_rnglists data (no base address for "
14421 "DW_RLE_offset_pair)"));
14422 return false;
14423 }
14424
14425 range_beginning += *base;
14426 range_end += *base;
14427 }
43988095
JK
14428
14429 /* A not-uncommon case of bad debug info.
14430 Don't pollute the addrmap with bad data. */
14431 if (range_beginning + baseaddr == 0
976ca316 14432 && !per_objfile->per_bfd->has_section_at_zero)
43988095 14433 {
b98664d3 14434 complaint (_(".debug_rnglists entry has start address of zero"
43988095
JK
14435 " [in module %s]"), objfile_name (objfile));
14436 continue;
14437 }
14438
14439 callback (range_beginning, range_end);
14440 }
14441
14442 if (overflow)
14443 {
b98664d3 14444 complaint (_("Offset %d is not terminated "
43988095
JK
14445 "for DW_AT_ranges attribute"),
14446 offset);
14447 return false;
14448 }
14449
14450 return true;
14451}
14452
14453/* Call CALLBACK from DW_AT_ranges attribute value OFFSET reading .debug_ranges.
14454 Callback's type should be:
14455 void (CORE_ADDR range_beginning, CORE_ADDR range_end)
5f46c5a5 14456 Return 1 if the attributes are present and valid, otherwise, return 0. */
43039443 14457
43988095 14458template <typename Callback>
43039443 14459static int
d0ce17d8 14460dwarf2_ranges_process (unsigned offset, struct dwarf2_cu *cu, dwarf_tag tag,
43988095 14461 Callback &&callback)
43039443 14462{
5e22e966
SM
14463 dwarf2_per_objfile *per_objfile = cu->per_objfile;
14464 struct objfile *objfile = per_objfile->objfile;
43039443
JK
14465 struct comp_unit_head *cu_header = &cu->header;
14466 bfd *obfd = objfile->obfd;
14467 unsigned int addr_size = cu_header->addr_size;
14468 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
14469 /* Base address selection entry. */
2b24b6e4 14470 gdb::optional<CORE_ADDR> base;
43039443 14471 unsigned int dummy;
d521ce57 14472 const gdb_byte *buffer;
ff013f42 14473 CORE_ADDR baseaddr;
43039443 14474
43988095 14475 if (cu_header->version >= 5)
d0ce17d8 14476 return dwarf2_rnglists_process (offset, cu, tag, callback);
43988095 14477
d00adf39 14478 base = cu->base_address;
43039443 14479
5e22e966
SM
14480 per_objfile->per_bfd->ranges.read (objfile);
14481 if (offset >= per_objfile->per_bfd->ranges.size)
43039443 14482 {
b98664d3 14483 complaint (_("Offset %d out of bounds for DW_AT_ranges attribute"),
43039443
JK
14484 offset);
14485 return 0;
14486 }
5e22e966 14487 buffer = per_objfile->per_bfd->ranges.buffer + offset;
43039443 14488
b3b3bada 14489 baseaddr = objfile->text_section_offset ();
ff013f42 14490
43039443
JK
14491 while (1)
14492 {
14493 CORE_ADDR range_beginning, range_end;
14494
c8a7a66f 14495 range_beginning = cu->header.read_address (obfd, buffer, &dummy);
43039443 14496 buffer += addr_size;
c8a7a66f 14497 range_end = cu->header.read_address (obfd, buffer, &dummy);
43039443
JK
14498 buffer += addr_size;
14499 offset += 2 * addr_size;
14500
14501 /* An end of list marker is a pair of zero addresses. */
14502 if (range_beginning == 0 && range_end == 0)
14503 /* Found the end of list entry. */
14504 break;
14505
14506 /* Each base address selection entry is a pair of 2 values.
14507 The first is the largest possible address, the second is
14508 the base address. Check for a base address here. */
14509 if ((range_beginning & mask) == mask)
14510 {
28d2bfb9
AB
14511 /* If we found the largest possible address, then we already
14512 have the base address in range_end. */
14513 base = range_end;
43039443
JK
14514 continue;
14515 }
14516
2b24b6e4 14517 if (!base.has_value ())
43039443
JK
14518 {
14519 /* We have no valid base address for the ranges
14520 data. */
b98664d3 14521 complaint (_("Invalid .debug_ranges data (no base address)"));
43039443
JK
14522 return 0;
14523 }
14524
9277c30c
UW
14525 if (range_beginning > range_end)
14526 {
14527 /* Inverted range entries are invalid. */
b98664d3 14528 complaint (_("Invalid .debug_ranges data (inverted range)"));
9277c30c
UW
14529 return 0;
14530 }
14531
14532 /* Empty range entries have no effect. */
14533 if (range_beginning == range_end)
14534 continue;
14535
2b24b6e4
TT
14536 range_beginning += *base;
14537 range_end += *base;
43039443 14538
01093045
DE
14539 /* A not-uncommon case of bad debug info.
14540 Don't pollute the addrmap with bad data. */
14541 if (range_beginning + baseaddr == 0
5e22e966 14542 && !per_objfile->per_bfd->has_section_at_zero)
01093045 14543 {
b98664d3 14544 complaint (_(".debug_ranges entry has start address of zero"
4262abfb 14545 " [in module %s]"), objfile_name (objfile));
01093045
DE
14546 continue;
14547 }
14548
5f46c5a5
JK
14549 callback (range_beginning, range_end);
14550 }
14551
14552 return 1;
14553}
14554
14555/* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
14556 Return 1 if the attributes are present and valid, otherwise, return 0.
14557 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
14558
14559static int
14560dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
14561 CORE_ADDR *high_return, struct dwarf2_cu *cu,
d0ce17d8 14562 dwarf2_psymtab *ranges_pst, dwarf_tag tag)
5f46c5a5 14563{
5e22e966 14564 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14565 struct gdbarch *gdbarch = objfile->arch ();
b3b3bada 14566 const CORE_ADDR baseaddr = objfile->text_section_offset ();
5f46c5a5
JK
14567 int low_set = 0;
14568 CORE_ADDR low = 0;
14569 CORE_ADDR high = 0;
14570 int retval;
14571
d0ce17d8 14572 retval = dwarf2_ranges_process (offset, cu, tag,
5f46c5a5
JK
14573 [&] (CORE_ADDR range_beginning, CORE_ADDR range_end)
14574 {
9277c30c 14575 if (ranges_pst != NULL)
3e29f34a
MR
14576 {
14577 CORE_ADDR lowpc;
14578 CORE_ADDR highpc;
14579
79748972
TT
14580 lowpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14581 range_beginning + baseaddr)
14582 - baseaddr);
14583 highpc = (gdbarch_adjust_dwarf2_addr (gdbarch,
14584 range_end + baseaddr)
14585 - baseaddr);
d320c2b5
TT
14586 addrmap_set_empty (objfile->partial_symtabs->psymtabs_addrmap,
14587 lowpc, highpc - 1, ranges_pst);
3e29f34a 14588 }
ff013f42 14589
43039443
JK
14590 /* FIXME: This is recording everything as a low-high
14591 segment of consecutive addresses. We should have a
14592 data structure for discontiguous block ranges
14593 instead. */
14594 if (! low_set)
14595 {
14596 low = range_beginning;
14597 high = range_end;
14598 low_set = 1;
14599 }
14600 else
14601 {
14602 if (range_beginning < low)
14603 low = range_beginning;
14604 if (range_end > high)
14605 high = range_end;
14606 }
5f46c5a5
JK
14607 });
14608 if (!retval)
14609 return 0;
43039443
JK
14610
14611 if (! low_set)
14612 /* If the first entry is an end-of-list marker, the range
14613 describes an empty scope, i.e. no instructions. */
14614 return 0;
14615
14616 if (low_return)
14617 *low_return = low;
14618 if (high_return)
14619 *high_return = high;
14620 return 1;
14621}
14622
3a2b436a
JK
14623/* Get low and high pc attributes from a die. See enum pc_bounds_kind
14624 definition for the return value. *LOWPC and *HIGHPC are set iff
e385593e 14625 neither PC_BOUNDS_NOT_PRESENT nor PC_BOUNDS_INVALID are returned. */
380bca97 14626
3a2b436a 14627static enum pc_bounds_kind
af34e669 14628dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
d85a05f0 14629 CORE_ADDR *highpc, struct dwarf2_cu *cu,
891813be 14630 dwarf2_psymtab *pst)
c906108c 14631{
976ca316 14632 dwarf2_per_objfile *per_objfile = cu->per_objfile;
c906108c 14633 struct attribute *attr;
91da1414 14634 struct attribute *attr_high;
af34e669
DJ
14635 CORE_ADDR low = 0;
14636 CORE_ADDR high = 0;
e385593e 14637 enum pc_bounds_kind ret;
c906108c 14638
91da1414
MW
14639 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14640 if (attr_high)
af34e669 14641 {
e142c38c 14642 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14643 if (attr != nullptr)
dda83cd7 14644 {
95f982e5
TT
14645 low = attr->as_address ();
14646 high = attr_high->as_address ();
cd6c91b4 14647 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14648 high += low;
91da1414 14649 }
af34e669
DJ
14650 else
14651 /* Found high w/o low attribute. */
e385593e 14652 return PC_BOUNDS_INVALID;
af34e669
DJ
14653
14654 /* Found consecutive range of addresses. */
3a2b436a 14655 ret = PC_BOUNDS_HIGH_LOW;
af34e669 14656 }
c906108c 14657 else
af34e669 14658 {
e142c38c 14659 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14660 if (attr != nullptr && attr->form_is_unsigned ())
af34e669 14661 {
18a8505e 14662 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14663 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14664 in DW_TAG_compile_unit of DWO files.
14665
14666 Attributes of the form DW_FORM_rnglistx have already had their
14667 value changed by read_rnglist_index and already include
14668 DW_AT_rnglists_base, so don't need to add the ranges base,
14669 either. */
14670 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14671 && attr->form != DW_FORM_rnglistx);
529908cb 14672 unsigned int ranges_offset = (attr->as_unsigned ()
ab435259
DE
14673 + (need_ranges_base
14674 ? cu->ranges_base
14675 : 0));
2e3cf129 14676
af34e669 14677 /* Value of the DW_AT_ranges attribute is the offset in the
a604369a 14678 .debug_ranges section. */
d0ce17d8
CT
14679 if (!dwarf2_ranges_read (ranges_offset, &low, &high, cu, pst,
14680 die->tag))
e385593e 14681 return PC_BOUNDS_INVALID;
43039443 14682 /* Found discontinuous range of addresses. */
3a2b436a 14683 ret = PC_BOUNDS_RANGES;
af34e669 14684 }
e385593e
JK
14685 else
14686 return PC_BOUNDS_NOT_PRESENT;
af34e669 14687 }
c906108c 14688
48fbe735 14689 /* partial_die_info::read has also the strict LOW < HIGH requirement. */
9373cf26 14690 if (high <= low)
e385593e 14691 return PC_BOUNDS_INVALID;
c906108c
SS
14692
14693 /* When using the GNU linker, .gnu.linkonce. sections are used to
14694 eliminate duplicate copies of functions and vtables and such.
14695 The linker will arbitrarily choose one and discard the others.
14696 The AT_*_pc values for such functions refer to local labels in
14697 these sections. If the section from that file was discarded, the
14698 labels are not in the output, so the relocs get a value of 0.
14699 If this is a discarded function, mark the pc bounds as invalid,
14700 so that GDB will ignore it. */
976ca316 14701 if (low == 0 && !per_objfile->per_bfd->has_section_at_zero)
e385593e 14702 return PC_BOUNDS_INVALID;
c906108c
SS
14703
14704 *lowpc = low;
96408a79
SA
14705 if (highpc)
14706 *highpc = high;
af34e669 14707 return ret;
c906108c
SS
14708}
14709
b084d499
JB
14710/* Assuming that DIE represents a subprogram DIE or a lexical block, get
14711 its low and high PC addresses. Do nothing if these addresses could not
14712 be determined. Otherwise, set LOWPC to the low address if it is smaller,
14713 and HIGHPC to the high address if greater than HIGHPC. */
14714
14715static void
14716dwarf2_get_subprogram_pc_bounds (struct die_info *die,
dda83cd7
SM
14717 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14718 struct dwarf2_cu *cu)
b084d499
JB
14719{
14720 CORE_ADDR low, high;
14721 struct die_info *child = die->child;
14722
e385593e 14723 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL) >= PC_BOUNDS_RANGES)
b084d499 14724 {
325fac50
PA
14725 *lowpc = std::min (*lowpc, low);
14726 *highpc = std::max (*highpc, high);
b084d499
JB
14727 }
14728
14729 /* If the language does not allow nested subprograms (either inside
14730 subprograms or lexical blocks), we're done. */
14731 if (cu->language != language_ada)
14732 return;
6e70227d 14733
b084d499
JB
14734 /* Check all the children of the given DIE. If it contains nested
14735 subprograms, then check their pc bounds. Likewise, we need to
14736 check lexical blocks as well, as they may also contain subprogram
14737 definitions. */
14738 while (child && child->tag)
14739 {
14740 if (child->tag == DW_TAG_subprogram
dda83cd7
SM
14741 || child->tag == DW_TAG_lexical_block)
14742 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
436c571c 14743 child = child->sibling;
b084d499
JB
14744 }
14745}
14746
fae299cd
DC
14747/* Get the low and high pc's represented by the scope DIE, and store
14748 them in *LOWPC and *HIGHPC. If the correct values can't be
14749 determined, set *LOWPC to -1 and *HIGHPC to 0. */
14750
14751static void
14752get_scope_pc_bounds (struct die_info *die,
14753 CORE_ADDR *lowpc, CORE_ADDR *highpc,
14754 struct dwarf2_cu *cu)
14755{
14756 CORE_ADDR best_low = (CORE_ADDR) -1;
14757 CORE_ADDR best_high = (CORE_ADDR) 0;
14758 CORE_ADDR current_low, current_high;
14759
3a2b436a 14760 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL)
e385593e 14761 >= PC_BOUNDS_RANGES)
fae299cd
DC
14762 {
14763 best_low = current_low;
14764 best_high = current_high;
14765 }
14766 else
14767 {
14768 struct die_info *child = die->child;
14769
14770 while (child && child->tag)
14771 {
14772 switch (child->tag) {
14773 case DW_TAG_subprogram:
dda83cd7 14774 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
fae299cd
DC
14775 break;
14776 case DW_TAG_namespace:
f55ee35c 14777 case DW_TAG_module:
fae299cd
DC
14778 /* FIXME: carlton/2004-01-16: Should we do this for
14779 DW_TAG_class_type/DW_TAG_structure_type, too? I think
14780 that current GCC's always emit the DIEs corresponding
14781 to definitions of methods of classes as children of a
14782 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
14783 the DIEs giving the declarations, which could be
14784 anywhere). But I don't see any reason why the
14785 standards says that they have to be there. */
14786 get_scope_pc_bounds (child, &current_low, &current_high, cu);
14787
14788 if (current_low != ((CORE_ADDR) -1))
14789 {
325fac50
PA
14790 best_low = std::min (best_low, current_low);
14791 best_high = std::max (best_high, current_high);
fae299cd
DC
14792 }
14793 break;
14794 default:
0963b4bd 14795 /* Ignore. */
fae299cd
DC
14796 break;
14797 }
14798
436c571c 14799 child = child->sibling;
fae299cd
DC
14800 }
14801 }
14802
14803 *lowpc = best_low;
14804 *highpc = best_high;
14805}
14806
801e3a5b
JB
14807/* Record the address ranges for BLOCK, offset by BASEADDR, as given
14808 in DIE. */
380bca97 14809
801e3a5b
JB
14810static void
14811dwarf2_record_block_ranges (struct die_info *die, struct block *block,
dda83cd7 14812 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
801e3a5b 14813{
5e22e966 14814 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 14815 struct gdbarch *gdbarch = objfile->arch ();
801e3a5b 14816 struct attribute *attr;
91da1414 14817 struct attribute *attr_high;
801e3a5b 14818
91da1414
MW
14819 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
14820 if (attr_high)
801e3a5b 14821 {
801e3a5b 14822 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 14823 if (attr != nullptr)
dda83cd7 14824 {
95f982e5
TT
14825 CORE_ADDR low = attr->as_address ();
14826 CORE_ADDR high = attr_high->as_address ();
31aa7e4e 14827
cd6c91b4 14828 if (cu->header.version >= 4 && attr_high->form_is_constant ())
31aa7e4e 14829 high += low;
9a619af0 14830
3e29f34a
MR
14831 low = gdbarch_adjust_dwarf2_addr (gdbarch, low + baseaddr);
14832 high = gdbarch_adjust_dwarf2_addr (gdbarch, high + baseaddr);
c24bdb02 14833 cu->get_builder ()->record_block_range (block, low, high - 1);
dda83cd7 14834 }
801e3a5b
JB
14835 }
14836
14837 attr = dwarf2_attr (die, DW_AT_ranges, cu);
529908cb 14838 if (attr != nullptr && attr->form_is_unsigned ())
801e3a5b 14839 {
18a8505e 14840 /* DW_AT_rnglists_base does not apply to DIEs from the DWO skeleton.
ab435259 14841 We take advantage of the fact that DW_AT_ranges does not appear
d0ce17d8
CT
14842 in DW_TAG_compile_unit of DWO files.
14843
14844 Attributes of the form DW_FORM_rnglistx have already had their
14845 value changed by read_rnglist_index and already include
14846 DW_AT_rnglists_base, so don't need to add the ranges base,
14847 either. */
14848 int need_ranges_base = (die->tag != DW_TAG_compile_unit
14849 && attr->form != DW_FORM_rnglistx);
801e3a5b
JB
14850
14851 /* The value of the DW_AT_ranges attribute is the offset of the
dda83cd7 14852 address range list in the .debug_ranges section. */
529908cb 14853 unsigned long offset = (attr->as_unsigned ()
ab435259 14854 + (need_ranges_base ? cu->ranges_base : 0));
801e3a5b 14855
2d5f09ec 14856 std::vector<blockrange> blockvec;
d0ce17d8 14857 dwarf2_ranges_process (offset, cu, die->tag,
5f46c5a5
JK
14858 [&] (CORE_ADDR start, CORE_ADDR end)
14859 {
58fdfd2c
JK
14860 start += baseaddr;
14861 end += baseaddr;
5f46c5a5
JK
14862 start = gdbarch_adjust_dwarf2_addr (gdbarch, start);
14863 end = gdbarch_adjust_dwarf2_addr (gdbarch, end);
c24bdb02 14864 cu->get_builder ()->record_block_range (block, start, end - 1);
2d5f09ec 14865 blockvec.emplace_back (start, end);
5f46c5a5 14866 });
2d5f09ec
KB
14867
14868 BLOCK_RANGES(block) = make_blockranges (objfile, blockvec);
801e3a5b
JB
14869 }
14870}
14871
685b1105
JK
14872/* Check whether the producer field indicates either of GCC < 4.6, or the
14873 Intel C/C++ compiler, and cache the result in CU. */
60d5a603 14874
685b1105
JK
14875static void
14876check_producer (struct dwarf2_cu *cu)
60d5a603 14877{
38360086 14878 int major, minor;
60d5a603
JK
14879
14880 if (cu->producer == NULL)
14881 {
14882 /* For unknown compilers expect their behavior is DWARF version
14883 compliant.
14884
14885 GCC started to support .debug_types sections by -gdwarf-4 since
14886 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
14887 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
14888 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
14889 interpreted incorrectly by GDB now - GCC PR debug/48229. */
60d5a603 14890 }
b1ffba5a 14891 else if (producer_is_gcc (cu->producer, &major, &minor))
60d5a603 14892 {
38360086
MW
14893 cu->producer_is_gxx_lt_4_6 = major < 4 || (major == 4 && minor < 6);
14894 cu->producer_is_gcc_lt_4_3 = major < 4 || (major == 4 && minor < 3);
685b1105 14895 }
5230b05a 14896 else if (producer_is_icc (cu->producer, &major, &minor))
eb77c9df
AB
14897 {
14898 cu->producer_is_icc = true;
14899 cu->producer_is_icc_lt_14 = major < 14;
14900 }
c258c396
JD
14901 else if (startswith (cu->producer, "CodeWarrior S12/L-ISA"))
14902 cu->producer_is_codewarrior = true;
685b1105
JK
14903 else
14904 {
14905 /* For other non-GCC compilers, expect their behavior is DWARF version
14906 compliant. */
60d5a603
JK
14907 }
14908
9068261f 14909 cu->checked_producer = true;
685b1105 14910}
ba919b58 14911
685b1105
JK
14912/* Check for GCC PR debug/45124 fix which is not present in any G++ version up
14913 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
14914 during 4.6.0 experimental. */
14915
9068261f 14916static bool
685b1105
JK
14917producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
14918{
14919 if (!cu->checked_producer)
14920 check_producer (cu);
14921
14922 return cu->producer_is_gxx_lt_4_6;
60d5a603
JK
14923}
14924
c258c396
JD
14925
14926/* Codewarrior (at least as of version 5.0.40) generates dwarf line information
14927 with incorrect is_stmt attributes. */
14928
14929static bool
14930producer_is_codewarrior (struct dwarf2_cu *cu)
14931{
14932 if (!cu->checked_producer)
14933 check_producer (cu);
14934
14935 return cu->producer_is_codewarrior;
14936}
14937
bf23a268
TT
14938/* Return the accessibility of DIE, as given by DW_AT_accessibility.
14939 If that attribute is not available, return the appropriate
14940 default. */
60d5a603
JK
14941
14942static enum dwarf_access_attribute
bf23a268 14943dwarf2_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
60d5a603 14944{
bf23a268
TT
14945 attribute *attr = dwarf2_attr (die, DW_AT_accessibility, cu);
14946 if (attr != nullptr)
14947 {
14948 LONGEST value = attr->constant_value (-1);
14949 if (value == DW_ACCESS_public
14950 || value == DW_ACCESS_protected
14951 || value == DW_ACCESS_private)
14952 return (dwarf_access_attribute) value;
14953 complaint (_("Unhandled DW_AT_accessibility value (%s)"),
14954 plongest (value));
14955 }
14956
60d5a603
JK
14957 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
14958 {
14959 /* The default DWARF 2 accessibility for members is public, the default
14960 accessibility for inheritance is private. */
14961
14962 if (die->tag != DW_TAG_inheritance)
14963 return DW_ACCESS_public;
14964 else
14965 return DW_ACCESS_private;
14966 }
14967 else
14968 {
14969 /* DWARF 3+ defines the default accessibility a different way. The same
14970 rules apply now for DW_TAG_inheritance as for the members and it only
14971 depends on the container kind. */
14972
14973 if (die->parent->tag == DW_TAG_class_type)
14974 return DW_ACCESS_private;
14975 else
14976 return DW_ACCESS_public;
14977 }
14978}
14979
74ac6d43
TT
14980/* Look for DW_AT_data_member_location. Set *OFFSET to the byte
14981 offset. If the attribute was not found return 0, otherwise return
14982 1. If it was found but could not properly be handled, set *OFFSET
14983 to 0. */
14984
14985static int
14986handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
14987 LONGEST *offset)
14988{
14989 struct attribute *attr;
14990
14991 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
14992 if (attr != NULL)
14993 {
14994 *offset = 0;
14995
14996 /* Note that we do not check for a section offset first here.
14997 This is because DW_AT_data_member_location is new in DWARF 4,
14998 so if we see it, we can assume that a constant form is really
14999 a constant and not a section offset. */
cd6c91b4 15000 if (attr->form_is_constant ())
0826b30a 15001 *offset = attr->constant_value (0);
cd6c91b4 15002 else if (attr->form_is_section_offset ())
74ac6d43 15003 dwarf2_complex_location_expr_complaint ();
4fc6c0d5 15004 else if (attr->form_is_block ())
9d2246fc 15005 *offset = decode_locdesc (attr->as_block (), cu);
74ac6d43
TT
15006 else
15007 dwarf2_complex_location_expr_complaint ();
15008
15009 return 1;
15010 }
15011
15012 return 0;
15013}
15014
7d79de9a
TT
15015/* Look for DW_AT_data_member_location and store the results in FIELD. */
15016
15017static void
15018handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
15019 struct field *field)
15020{
15021 struct attribute *attr;
15022
15023 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
15024 if (attr != NULL)
15025 {
15026 if (attr->form_is_constant ())
15027 {
15028 LONGEST offset = attr->constant_value (0);
15029 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15030 }
15031 else if (attr->form_is_section_offset ())
15032 dwarf2_complex_location_expr_complaint ();
15033 else if (attr->form_is_block ())
15034 {
15035 bool handled;
9d2246fc 15036 CORE_ADDR offset = decode_locdesc (attr->as_block (), cu, &handled);
7d79de9a
TT
15037 if (handled)
15038 SET_FIELD_BITPOS (*field, offset * bits_per_byte);
15039 else
15040 {
5e22e966
SM
15041 dwarf2_per_objfile *per_objfile = cu->per_objfile;
15042 struct objfile *objfile = per_objfile->objfile;
7d79de9a
TT
15043 struct dwarf2_locexpr_baton *dlbaton
15044 = XOBNEW (&objfile->objfile_obstack,
15045 struct dwarf2_locexpr_baton);
9d2246fc
TT
15046 dlbaton->data = attr->as_block ()->data;
15047 dlbaton->size = attr->as_block ()->size;
7d79de9a
TT
15048 /* When using this baton, we want to compute the address
15049 of the field, not the value. This is why
15050 is_reference is set to false here. */
15051 dlbaton->is_reference = false;
5e22e966 15052 dlbaton->per_objfile = per_objfile;
7d79de9a
TT
15053 dlbaton->per_cu = cu->per_cu;
15054
15055 SET_FIELD_DWARF_BLOCK (*field, dlbaton);
15056 }
15057 }
15058 else
15059 dwarf2_complex_location_expr_complaint ();
15060 }
15061}
15062
c906108c
SS
15063/* Add an aggregate field to the field list. */
15064
15065static void
107d2387 15066dwarf2_add_field (struct field_info *fip, struct die_info *die,
e7c27a73 15067 struct dwarf2_cu *cu)
6e70227d 15068{
5e22e966 15069 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 15070 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
15071 struct nextfield *new_field;
15072 struct attribute *attr;
15073 struct field *fp;
15d034d0 15074 const char *fieldname = "";
c906108c 15075
7d0ccb61
DJ
15076 if (die->tag == DW_TAG_inheritance)
15077 {
be2daae6
TT
15078 fip->baseclasses.emplace_back ();
15079 new_field = &fip->baseclasses.back ();
7d0ccb61
DJ
15080 }
15081 else
15082 {
be2daae6
TT
15083 fip->fields.emplace_back ();
15084 new_field = &fip->fields.back ();
7d0ccb61 15085 }
be2daae6 15086
9c6a1327
TT
15087 new_field->offset = die->sect_off;
15088
bf23a268 15089 new_field->accessibility = dwarf2_access_attribute (die, cu);
c906108c 15090 if (new_field->accessibility != DW_ACCESS_public)
264fc0e2 15091 fip->non_public_fields = true;
60d5a603 15092
e142c38c 15093 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
435d3d88 15094 if (attr != nullptr)
23dca5c3 15095 new_field->virtuality = attr->as_virtuality ();
60d5a603
JK
15096 else
15097 new_field->virtuality = DW_VIRTUALITY_none;
c906108c
SS
15098
15099 fp = &new_field->field;
a9a9bd0f 15100
e142c38c 15101 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
c906108c 15102 {
a9a9bd0f 15103 /* Data member other than a C++ static data member. */
6e70227d 15104
c906108c 15105 /* Get type of field. */
5d14b6e5 15106 fp->set_type (die_type (die, cu));
c906108c 15107
d6a843b5 15108 SET_FIELD_BITPOS (*fp, 0);
01ad7f36 15109
c906108c 15110 /* Get bit size of field (zero if none). */
e142c38c 15111 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
435d3d88 15112 if (attr != nullptr)
c906108c 15113 {
529908cb 15114 FIELD_BITSIZE (*fp) = attr->constant_value (0);
c906108c
SS
15115 }
15116 else
15117 {
15118 FIELD_BITSIZE (*fp) = 0;
15119 }
15120
15121 /* Get bit offset of field. */
7d79de9a 15122 handle_data_member_location (die, cu, fp);
e142c38c 15123 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7c184d33 15124 if (attr != nullptr && attr->form_is_constant ())
c906108c 15125 {
d5a22e77 15126 if (gdbarch_byte_order (gdbarch) == BFD_ENDIAN_BIG)
c906108c
SS
15127 {
15128 /* For big endian bits, the DW_AT_bit_offset gives the
dda83cd7
SM
15129 additional bit offset from the MSB of the containing
15130 anonymous object to the MSB of the field. We don't
15131 have to do anything special since we don't need to
15132 know the size of the anonymous object. */
529908cb 15133 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
7c184d33 15134 + attr->constant_value (0)));
c906108c
SS
15135 }
15136 else
15137 {
15138 /* For little endian bits, compute the bit offset to the
dda83cd7
SM
15139 MSB of the anonymous object, subtract off the number of
15140 bits from the MSB of the field to the MSB of the
15141 object, and then subtract off the number of bits of
15142 the field itself. The result is the bit offset of
15143 the LSB of the field. */
c906108c 15144 int anonymous_size;
7c184d33 15145 int bit_offset = attr->constant_value (0);
c906108c 15146
e142c38c 15147 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7c184d33 15148 if (attr != nullptr && attr->form_is_constant ())
c906108c
SS
15149 {
15150 /* The size of the anonymous object containing
15151 the bit field is explicit, so use the
15152 indicated size (in bytes). */
7c184d33 15153 anonymous_size = attr->constant_value (0);
c906108c
SS
15154 }
15155 else
15156 {
15157 /* The size of the anonymous object containing
15158 the bit field must be inferred from the type
15159 attribute of the data member containing the
15160 bit field. */
5d14b6e5 15161 anonymous_size = TYPE_LENGTH (fp->type ());
c906108c 15162 }
f41f5e61
PA
15163 SET_FIELD_BITPOS (*fp,
15164 (FIELD_BITPOS (*fp)
15165 + anonymous_size * bits_per_byte
15166 - bit_offset - FIELD_BITSIZE (*fp)));
c906108c
SS
15167 }
15168 }
da5b30da
AA
15169 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
15170 if (attr != NULL)
15171 SET_FIELD_BITPOS (*fp, (FIELD_BITPOS (*fp)
0826b30a 15172 + attr->constant_value (0)));
c906108c
SS
15173
15174 /* Get name of field. */
39cbfefa
DJ
15175 fieldname = dwarf2_name (die, cu);
15176 if (fieldname == NULL)
15177 fieldname = "";
d8151005
DJ
15178
15179 /* The name is already allocated along with this objfile, so we don't
15180 need to duplicate it for the type. */
15181 fp->name = fieldname;
c906108c
SS
15182
15183 /* Change accessibility for artificial fields (e.g. virtual table
dda83cd7 15184 pointer or virtual base class pointer) to private. */
e142c38c 15185 if (dwarf2_attr (die, DW_AT_artificial, cu))
c906108c 15186 {
d48cc9dd 15187 FIELD_ARTIFICIAL (*fp) = 1;
c906108c 15188 new_field->accessibility = DW_ACCESS_private;
264fc0e2 15189 fip->non_public_fields = true;
c906108c
SS
15190 }
15191 }
a9a9bd0f 15192 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
c906108c 15193 {
a9a9bd0f
DC
15194 /* C++ static member. */
15195
15196 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
15197 is a declaration, but all versions of G++ as of this writing
15198 (so through at least 3.2.1) incorrectly generate
15199 DW_TAG_variable tags. */
6e70227d 15200
ff355380 15201 const char *physname;
c906108c 15202
a9a9bd0f 15203 /* Get name of field. */
39cbfefa
DJ
15204 fieldname = dwarf2_name (die, cu);
15205 if (fieldname == NULL)
c906108c
SS
15206 return;
15207
254e6b9e 15208 attr = dwarf2_attr (die, DW_AT_const_value, cu);
3863f96c
DE
15209 if (attr
15210 /* Only create a symbol if this is an external value.
15211 new_symbol checks this and puts the value in the global symbol
15212 table, which we want. If it is not external, new_symbol
15213 will try to put the value in cu->list_in_scope which is wrong. */
15214 && dwarf2_flag_true_p (die, DW_AT_external, cu))
254e6b9e
DE
15215 {
15216 /* A static const member, not much different than an enum as far as
15217 we're concerned, except that we can support more types. */
15218 new_symbol (die, NULL, cu);
15219 }
15220
2df3850c 15221 /* Get physical name. */
ff355380 15222 physname = dwarf2_physname (fieldname, die, cu);
c906108c 15223
d8151005
DJ
15224 /* The name is already allocated along with this objfile, so we don't
15225 need to duplicate it for the type. */
15226 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
5d14b6e5 15227 fp->set_type (die_type (die, cu));
d8151005 15228 FIELD_NAME (*fp) = fieldname;
c906108c
SS
15229 }
15230 else if (die->tag == DW_TAG_inheritance)
15231 {
74ac6d43 15232 /* C++ base class field. */
7d79de9a 15233 handle_data_member_location (die, cu, fp);
c906108c 15234 FIELD_BITSIZE (*fp) = 0;
5d14b6e5
SM
15235 fp->set_type (die_type (die, cu));
15236 FIELD_NAME (*fp) = fp->type ()->name ();
c906108c 15237 }
2ddeaf8a
TT
15238 else
15239 gdb_assert_not_reached ("missing case in dwarf2_add_field");
c906108c
SS
15240}
15241
883fd55a
KS
15242/* Can the type given by DIE define another type? */
15243
15244static bool
15245type_can_define_types (const struct die_info *die)
15246{
15247 switch (die->tag)
15248 {
15249 case DW_TAG_typedef:
15250 case DW_TAG_class_type:
15251 case DW_TAG_structure_type:
15252 case DW_TAG_union_type:
15253 case DW_TAG_enumeration_type:
15254 return true;
15255
15256 default:
15257 return false;
15258 }
15259}
15260
15261/* Add a type definition defined in the scope of the FIP's class. */
98751a41
JK
15262
15263static void
883fd55a
KS
15264dwarf2_add_type_defn (struct field_info *fip, struct die_info *die,
15265 struct dwarf2_cu *cu)
6e70227d 15266{
be2daae6
TT
15267 struct decl_field fp;
15268 memset (&fp, 0, sizeof (fp));
98751a41 15269
883fd55a 15270 gdb_assert (type_can_define_types (die));
98751a41 15271
883fd55a 15272 /* Get name of field. NULL is okay here, meaning an anonymous type. */
be2daae6
TT
15273 fp.name = dwarf2_name (die, cu);
15274 fp.type = read_type_die (die, cu);
98751a41 15275
c191a687 15276 /* Save accessibility. */
bf23a268 15277 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
c191a687
KS
15278 switch (accessibility)
15279 {
15280 case DW_ACCESS_public:
15281 /* The assumed value if neither private nor protected. */
15282 break;
15283 case DW_ACCESS_private:
be2daae6 15284 fp.is_private = 1;
c191a687
KS
15285 break;
15286 case DW_ACCESS_protected:
be2daae6 15287 fp.is_protected = 1;
c191a687 15288 break;
c191a687
KS
15289 }
15290
883fd55a 15291 if (die->tag == DW_TAG_typedef)
be2daae6 15292 fip->typedef_field_list.push_back (fp);
883fd55a 15293 else
be2daae6 15294 fip->nested_types_list.push_back (fp);
98751a41
JK
15295}
15296
9c6a1327
TT
15297/* A convenience typedef that's used when finding the discriminant
15298 field for a variant part. */
1b95cdb7
SM
15299typedef std::unordered_map<sect_offset, int, gdb::hash_enum<sect_offset>>
15300 offset_map_type;
9c6a1327
TT
15301
15302/* Compute the discriminant range for a given variant. OBSTACK is
15303 where the results will be stored. VARIANT is the variant to
15304 process. IS_UNSIGNED indicates whether the discriminant is signed
15305 or unsigned. */
15306
15307static const gdb::array_view<discriminant_range>
15308convert_variant_range (struct obstack *obstack, const variant_field &variant,
15309 bool is_unsigned)
15310{
15311 std::vector<discriminant_range> ranges;
15312
15313 if (variant.default_branch)
15314 return {};
15315
15316 if (variant.discr_list_data == nullptr)
15317 {
15318 discriminant_range r
15319 = {variant.discriminant_value, variant.discriminant_value};
15320 ranges.push_back (r);
15321 }
15322 else
15323 {
15324 gdb::array_view<const gdb_byte> data (variant.discr_list_data->data,
15325 variant.discr_list_data->size);
15326 while (!data.empty ())
15327 {
15328 if (data[0] != DW_DSC_range && data[0] != DW_DSC_label)
15329 {
15330 complaint (_("invalid discriminant marker: %d"), data[0]);
15331 break;
15332 }
15333 bool is_range = data[0] == DW_DSC_range;
15334 data = data.slice (1);
15335
15336 ULONGEST low, high;
15337 unsigned int bytes_read;
15338
15339 if (data.empty ())
15340 {
15341 complaint (_("DW_AT_discr_list missing low value"));
15342 break;
15343 }
15344 if (is_unsigned)
15345 low = read_unsigned_leb128 (nullptr, data.data (), &bytes_read);
15346 else
15347 low = (ULONGEST) read_signed_leb128 (nullptr, data.data (),
15348 &bytes_read);
15349 data = data.slice (bytes_read);
15350
15351 if (is_range)
15352 {
15353 if (data.empty ())
15354 {
15355 complaint (_("DW_AT_discr_list missing high value"));
15356 break;
15357 }
15358 if (is_unsigned)
15359 high = read_unsigned_leb128 (nullptr, data.data (),
15360 &bytes_read);
15361 else
15362 high = (LONGEST) read_signed_leb128 (nullptr, data.data (),
15363 &bytes_read);
15364 data = data.slice (bytes_read);
15365 }
15366 else
15367 high = low;
15368
15369 ranges.push_back ({ low, high });
15370 }
15371 }
15372
15373 discriminant_range *result = XOBNEWVEC (obstack, discriminant_range,
15374 ranges.size ());
15375 std::copy (ranges.begin (), ranges.end (), result);
15376 return gdb::array_view<discriminant_range> (result, ranges.size ());
15377}
15378
15379static const gdb::array_view<variant_part> create_variant_parts
15380 (struct obstack *obstack,
15381 const offset_map_type &offset_map,
15382 struct field_info *fi,
15383 const std::vector<variant_part_builder> &variant_parts);
15384
15385/* Fill in a "struct variant" for a given variant field. RESULT is
15386 the variant to fill in. OBSTACK is where any needed allocations
15387 will be done. OFFSET_MAP holds the mapping from section offsets to
15388 fields for the type. FI describes the fields of the type we're
15389 processing. FIELD is the variant field we're converting. */
15390
15391static void
15392create_one_variant (variant &result, struct obstack *obstack,
15393 const offset_map_type &offset_map,
15394 struct field_info *fi, const variant_field &field)
15395{
15396 result.discriminants = convert_variant_range (obstack, field, false);
15397 result.first_field = field.first_field + fi->baseclasses.size ();
15398 result.last_field = field.last_field + fi->baseclasses.size ();
15399 result.parts = create_variant_parts (obstack, offset_map, fi,
15400 field.variant_parts);
15401}
15402
15403/* Fill in a "struct variant_part" for a given variant part. RESULT
15404 is the variant part to fill in. OBSTACK is where any needed
15405 allocations will be done. OFFSET_MAP holds the mapping from
15406 section offsets to fields for the type. FI describes the fields of
15407 the type we're processing. BUILDER is the variant part to be
15408 converted. */
15409
15410static void
15411create_one_variant_part (variant_part &result,
15412 struct obstack *obstack,
15413 const offset_map_type &offset_map,
15414 struct field_info *fi,
15415 const variant_part_builder &builder)
15416{
15417 auto iter = offset_map.find (builder.discriminant_offset);
15418 if (iter == offset_map.end ())
15419 {
15420 result.discriminant_index = -1;
15421 /* Doesn't matter. */
15422 result.is_unsigned = false;
15423 }
15424 else
15425 {
15426 result.discriminant_index = iter->second;
15427 result.is_unsigned
c6d940a9 15428 = fi->fields[result.discriminant_index].field.type ()->is_unsigned ();
9c6a1327
TT
15429 }
15430
15431 size_t n = builder.variants.size ();
15432 variant *output = new (obstack) variant[n];
15433 for (size_t i = 0; i < n; ++i)
15434 create_one_variant (output[i], obstack, offset_map, fi,
15435 builder.variants[i]);
15436
15437 result.variants = gdb::array_view<variant> (output, n);
15438}
15439
15440/* Create a vector of variant parts that can be attached to a type.
15441 OBSTACK is where any needed allocations will be done. OFFSET_MAP
15442 holds the mapping from section offsets to fields for the type. FI
15443 describes the fields of the type we're processing. VARIANT_PARTS
15444 is the vector to convert. */
15445
15446static const gdb::array_view<variant_part>
15447create_variant_parts (struct obstack *obstack,
15448 const offset_map_type &offset_map,
15449 struct field_info *fi,
15450 const std::vector<variant_part_builder> &variant_parts)
15451{
15452 if (variant_parts.empty ())
15453 return {};
15454
15455 size_t n = variant_parts.size ();
15456 variant_part *result = new (obstack) variant_part[n];
15457 for (size_t i = 0; i < n; ++i)
15458 create_one_variant_part (result[i], obstack, offset_map, fi,
15459 variant_parts[i]);
15460
15461 return gdb::array_view<variant_part> (result, n);
15462}
15463
15464/* Compute the variant part vector for FIP, attaching it to TYPE when
15465 done. */
15466
15467static void
15468add_variant_property (struct field_info *fip, struct type *type,
15469 struct dwarf2_cu *cu)
15470{
15471 /* Map section offsets of fields to their field index. Note the
15472 field index here does not take the number of baseclasses into
15473 account. */
15474 offset_map_type offset_map;
15475 for (int i = 0; i < fip->fields.size (); ++i)
15476 offset_map[fip->fields[i].offset] = i;
15477
5e22e966 15478 struct objfile *objfile = cu->per_objfile->objfile;
9c6a1327
TT
15479 gdb::array_view<variant_part> parts
15480 = create_variant_parts (&objfile->objfile_obstack, offset_map, fip,
15481 fip->variant_parts);
15482
15483 struct dynamic_prop prop;
8c2e4e06
SM
15484 prop.set_variant_parts ((gdb::array_view<variant_part> *)
15485 obstack_copy (&objfile->objfile_obstack, &parts,
15486 sizeof (parts)));
9c6a1327 15487
5c54719c 15488 type->add_dyn_prop (DYN_PROP_VARIANT_PARTS, prop);
9c6a1327
TT
15489}
15490
c906108c
SS
15491/* Create the vector of fields, and attach it to the type. */
15492
15493static void
fba45db2 15494dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15495 struct dwarf2_cu *cu)
c906108c 15496{
317f7127 15497 int nfields = fip->nfields ();
c906108c
SS
15498
15499 /* Record the field count, allocate space for the array of fields,
15500 and create blank accessibility bitfields if necessary. */
5e33d5f4 15501 type->set_num_fields (nfields);
3cabb6b0
SM
15502 type->set_fields
15503 ((struct field *) TYPE_ZALLOC (type, sizeof (struct field) * nfields));
c906108c 15504
b4ba55a1 15505 if (fip->non_public_fields && cu->language != language_ada)
c906108c
SS
15506 {
15507 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15508
15509 TYPE_FIELD_PRIVATE_BITS (type) =
15510 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15511 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
15512
15513 TYPE_FIELD_PROTECTED_BITS (type) =
15514 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15515 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
15516
774b6a14
TT
15517 TYPE_FIELD_IGNORE_BITS (type) =
15518 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
15519 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
c906108c
SS
15520 }
15521
15522 /* If the type has baseclasses, allocate and clear a bit vector for
15523 TYPE_FIELD_VIRTUAL_BITS. */
be2daae6 15524 if (!fip->baseclasses.empty () && cu->language != language_ada)
c906108c 15525 {
be2daae6 15526 int num_bytes = B_BYTES (fip->baseclasses.size ());
fe1b8b76 15527 unsigned char *pointer;
c906108c
SS
15528
15529 ALLOCATE_CPLUS_STRUCT_TYPE (type);
224c3ddb 15530 pointer = (unsigned char *) TYPE_ALLOC (type, num_bytes);
fe1b8b76 15531 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
be2daae6
TT
15532 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->baseclasses.size ());
15533 TYPE_N_BASECLASSES (type) = fip->baseclasses.size ();
c906108c
SS
15534 }
15535
9c6a1327
TT
15536 if (!fip->variant_parts.empty ())
15537 add_variant_property (fip, type, cu);
2ddeaf8a 15538
be2daae6
TT
15539 /* Copy the saved-up fields into the field vector. */
15540 for (int i = 0; i < nfields; ++i)
c906108c 15541 {
be2daae6
TT
15542 struct nextfield &field
15543 = ((i < fip->baseclasses.size ()) ? fip->baseclasses[i]
15544 : fip->fields[i - fip->baseclasses.size ()]);
7d0ccb61 15545
ceacbf6e 15546 type->field (i) = field.field;
be2daae6 15547 switch (field.accessibility)
c906108c 15548 {
c5aa993b 15549 case DW_ACCESS_private:
b4ba55a1 15550 if (cu->language != language_ada)
be2daae6 15551 SET_TYPE_FIELD_PRIVATE (type, i);
c5aa993b 15552 break;
c906108c 15553
c5aa993b 15554 case DW_ACCESS_protected:
b4ba55a1 15555 if (cu->language != language_ada)
be2daae6 15556 SET_TYPE_FIELD_PROTECTED (type, i);
c5aa993b 15557 break;
c906108c 15558
c5aa993b
JM
15559 case DW_ACCESS_public:
15560 break;
c906108c 15561
c5aa993b
JM
15562 default:
15563 /* Unknown accessibility. Complain and treat it as public. */
15564 {
b98664d3 15565 complaint (_("unsupported accessibility %d"),
be2daae6 15566 field.accessibility);
c5aa993b
JM
15567 }
15568 break;
c906108c 15569 }
be2daae6 15570 if (i < fip->baseclasses.size ())
c906108c 15571 {
be2daae6 15572 switch (field.virtuality)
c906108c 15573 {
c5aa993b
JM
15574 case DW_VIRTUALITY_virtual:
15575 case DW_VIRTUALITY_pure_virtual:
b4ba55a1 15576 if (cu->language == language_ada)
a73c6dcd 15577 error (_("unexpected virtuality in component of Ada type"));
be2daae6 15578 SET_TYPE_FIELD_VIRTUAL (type, i);
c5aa993b 15579 break;
c906108c
SS
15580 }
15581 }
c906108c
SS
15582 }
15583}
15584
7d27a96d
TT
15585/* Return true if this member function is a constructor, false
15586 otherwise. */
15587
15588static int
15589dwarf2_is_constructor (struct die_info *die, struct dwarf2_cu *cu)
15590{
15591 const char *fieldname;
fe978cb0 15592 const char *type_name;
7d27a96d
TT
15593 int len;
15594
15595 if (die->parent == NULL)
15596 return 0;
15597
15598 if (die->parent->tag != DW_TAG_structure_type
15599 && die->parent->tag != DW_TAG_union_type
15600 && die->parent->tag != DW_TAG_class_type)
15601 return 0;
15602
15603 fieldname = dwarf2_name (die, cu);
fe978cb0
PA
15604 type_name = dwarf2_name (die->parent, cu);
15605 if (fieldname == NULL || type_name == NULL)
7d27a96d
TT
15606 return 0;
15607
15608 len = strlen (fieldname);
fe978cb0
PA
15609 return (strncmp (fieldname, type_name, len) == 0
15610 && (type_name[len] == '\0' || type_name[len] == '<'));
7d27a96d
TT
15611}
15612
c906108c
SS
15613/* Add a member function to the proper fieldlist. */
15614
15615static void
107d2387 15616dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
e7c27a73 15617 struct type *type, struct dwarf2_cu *cu)
c906108c 15618{
5e22e966 15619 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 15620 struct attribute *attr;
c906108c 15621 int i;
be2daae6 15622 struct fnfieldlist *flp = nullptr;
c906108c 15623 struct fn_field *fnp;
15d034d0 15624 const char *fieldname;
f792889a 15625 struct type *this_type;
c906108c 15626
b4ba55a1 15627 if (cu->language == language_ada)
a73c6dcd 15628 error (_("unexpected member function in Ada type"));
b4ba55a1 15629
2df3850c 15630 /* Get name of member function. */
39cbfefa
DJ
15631 fieldname = dwarf2_name (die, cu);
15632 if (fieldname == NULL)
2df3850c 15633 return;
c906108c 15634
c906108c 15635 /* Look up member function name in fieldlist. */
be2daae6 15636 for (i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15637 {
27bfe10e 15638 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
be2daae6
TT
15639 {
15640 flp = &fip->fnfieldlists[i];
15641 break;
15642 }
c906108c
SS
15643 }
15644
be2daae6
TT
15645 /* Create a new fnfieldlist if necessary. */
15646 if (flp == nullptr)
c906108c 15647 {
be2daae6
TT
15648 fip->fnfieldlists.emplace_back ();
15649 flp = &fip->fnfieldlists.back ();
c906108c 15650 flp->name = fieldname;
be2daae6 15651 i = fip->fnfieldlists.size () - 1;
c906108c
SS
15652 }
15653
be2daae6
TT
15654 /* Create a new member function field and add it to the vector of
15655 fnfieldlists. */
15656 flp->fnfields.emplace_back ();
15657 fnp = &flp->fnfields.back ();
3da10d80
KS
15658
15659 /* Delay processing of the physname until later. */
9c37b5ae 15660 if (cu->language == language_cplus)
be2daae6
TT
15661 add_to_method_list (type, i, flp->fnfields.size () - 1, fieldname,
15662 die, cu);
3da10d80
KS
15663 else
15664 {
1d06ead6 15665 const char *physname = dwarf2_physname (fieldname, die, cu);
3da10d80
KS
15666 fnp->physname = physname ? physname : "";
15667 }
15668
c906108c 15669 fnp->type = alloc_type (objfile);
f792889a 15670 this_type = read_type_die (die, cu);
78134374 15671 if (this_type && this_type->code () == TYPE_CODE_FUNC)
c906108c 15672 {
1f704f76 15673 int nparams = this_type->num_fields ();
c906108c 15674
f792889a 15675 /* TYPE is the domain of this method, and THIS_TYPE is the type
e26fb1d7
DC
15676 of the method itself (TYPE_CODE_METHOD). */
15677 smash_to_method_type (fnp->type, type,
f792889a 15678 TYPE_TARGET_TYPE (this_type),
80fc5e77 15679 this_type->fields (),
1f704f76 15680 this_type->num_fields (),
a409645d 15681 this_type->has_varargs ());
c906108c
SS
15682
15683 /* Handle static member functions.
dda83cd7
SM
15684 Dwarf2 has no clean way to discern C++ static and non-static
15685 member functions. G++ helps GDB by marking the first
15686 parameter for non-static member functions (which is the this
15687 pointer) as artificial. We obtain this information from
15688 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
f792889a 15689 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
c906108c
SS
15690 fnp->voffset = VOFFSET_STATIC;
15691 }
15692 else
b98664d3 15693 complaint (_("member function type missing for '%s'"),
3da10d80 15694 dwarf2_full_name (fieldname, die, cu));
c906108c
SS
15695
15696 /* Get fcontext from DW_AT_containing_type if present. */
e142c38c 15697 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
e7c27a73 15698 fnp->fcontext = die_containing_type (die, cu);
c906108c 15699
3e43a32a
MS
15700 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
15701 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
c906108c
SS
15702
15703 /* Get accessibility. */
bf23a268 15704 dwarf_access_attribute accessibility = dwarf2_access_attribute (die, cu);
60d5a603 15705 switch (accessibility)
c906108c 15706 {
60d5a603
JK
15707 case DW_ACCESS_private:
15708 fnp->is_private = 1;
15709 break;
15710 case DW_ACCESS_protected:
15711 fnp->is_protected = 1;
15712 break;
c906108c
SS
15713 }
15714
b02dede2 15715 /* Check for artificial methods. */
e142c38c 15716 attr = dwarf2_attr (die, DW_AT_artificial, cu);
c45bc3f8 15717 if (attr && attr->as_boolean ())
b02dede2
DJ
15718 fnp->is_artificial = 1;
15719
e35000a7
TBA
15720 /* Check for defaulted methods. */
15721 attr = dwarf2_attr (die, DW_AT_defaulted, cu);
e8e5c158
TT
15722 if (attr != nullptr)
15723 fnp->defaulted = attr->defaulted ();
e35000a7
TBA
15724
15725 /* Check for deleted methods. */
15726 attr = dwarf2_attr (die, DW_AT_deleted, cu);
c45bc3f8 15727 if (attr != nullptr && attr->as_boolean ())
e35000a7
TBA
15728 fnp->is_deleted = 1;
15729
7d27a96d
TT
15730 fnp->is_constructor = dwarf2_is_constructor (die, cu);
15731
0d564a31 15732 /* Get index in virtual function table if it is a virtual member
aec5aa8b
TT
15733 function. For older versions of GCC, this is an offset in the
15734 appropriate virtual table, as specified by DW_AT_containing_type.
15735 For everyone else, it is an expression to be evaluated relative
0d564a31
DJ
15736 to the object address. */
15737
e142c38c 15738 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
435d3d88 15739 if (attr != nullptr)
8e19ed76 15740 {
9d2246fc 15741 if (attr->form_is_block () && attr->as_block ()->size > 0)
dda83cd7 15742 {
9d2246fc
TT
15743 struct dwarf_block *block = attr->as_block ();
15744
15745 if (block->data[0] == DW_OP_constu)
aec5aa8b
TT
15746 {
15747 /* Old-style GCC. */
9d2246fc 15748 fnp->voffset = decode_locdesc (block, cu) + 2;
aec5aa8b 15749 }
9d2246fc
TT
15750 else if (block->data[0] == DW_OP_deref
15751 || (block->size > 1
15752 && block->data[0] == DW_OP_deref_size
15753 && block->data[1] == cu->header.addr_size))
aec5aa8b 15754 {
9d2246fc 15755 fnp->voffset = decode_locdesc (block, cu);
aec5aa8b
TT
15756 if ((fnp->voffset % cu->header.addr_size) != 0)
15757 dwarf2_complex_location_expr_complaint ();
15758 else
15759 fnp->voffset /= cu->header.addr_size;
15760 fnp->voffset += 2;
15761 }
15762 else
15763 dwarf2_complex_location_expr_complaint ();
15764
15765 if (!fnp->fcontext)
7e993ebf
KS
15766 {
15767 /* If there is no `this' field and no DW_AT_containing_type,
15768 we cannot actually find a base class context for the
15769 vtable! */
1f704f76 15770 if (this_type->num_fields () == 0
7e993ebf
KS
15771 || !TYPE_FIELD_ARTIFICIAL (this_type, 0))
15772 {
b98664d3 15773 complaint (_("cannot determine context for virtual member "
9d8780f0
SM
15774 "function \"%s\" (offset %s)"),
15775 fieldname, sect_offset_str (die->sect_off));
7e993ebf
KS
15776 }
15777 else
15778 {
15779 fnp->fcontext
940da03e 15780 = TYPE_TARGET_TYPE (this_type->field (0).type ());
7e993ebf
KS
15781 }
15782 }
aec5aa8b 15783 }
cd6c91b4 15784 else if (attr->form_is_section_offset ())
dda83cd7 15785 {
4d3c2250 15786 dwarf2_complex_location_expr_complaint ();
dda83cd7 15787 }
8e19ed76 15788 else
dda83cd7 15789 {
4d3c2250
KB
15790 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
15791 fieldname);
dda83cd7 15792 }
0d564a31 15793 }
d48cc9dd
DJ
15794 else
15795 {
15796 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
23dca5c3 15797 if (attr != nullptr && attr->as_virtuality () != DW_VIRTUALITY_none)
d48cc9dd
DJ
15798 {
15799 /* GCC does this, as of 2008-08-25; PR debug/37237. */
b98664d3 15800 complaint (_("Member function \"%s\" (offset %s) is virtual "
3e43a32a 15801 "but the vtable offset is not specified"),
9d8780f0 15802 fieldname, sect_offset_str (die->sect_off));
9655fd1a 15803 ALLOCATE_CPLUS_STRUCT_TYPE (type);
d48cc9dd
DJ
15804 TYPE_CPLUS_DYNAMIC (type) = 1;
15805 }
15806 }
c906108c
SS
15807}
15808
15809/* Create the vector of member function fields, and attach it to the type. */
15810
15811static void
fba45db2 15812dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
e7c27a73 15813 struct dwarf2_cu *cu)
c906108c 15814{
b4ba55a1 15815 if (cu->language == language_ada)
a73c6dcd 15816 error (_("unexpected member functions in Ada type"));
b4ba55a1 15817
c906108c
SS
15818 ALLOCATE_CPLUS_STRUCT_TYPE (type);
15819 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
be2daae6
TT
15820 TYPE_ALLOC (type,
15821 sizeof (struct fn_fieldlist) * fip->fnfieldlists.size ());
c906108c 15822
be2daae6 15823 for (int i = 0; i < fip->fnfieldlists.size (); i++)
c906108c 15824 {
be2daae6 15825 struct fnfieldlist &nf = fip->fnfieldlists[i];
c906108c 15826 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
c906108c 15827
be2daae6
TT
15828 TYPE_FN_FIELDLIST_NAME (type, i) = nf.name;
15829 TYPE_FN_FIELDLIST_LENGTH (type, i) = nf.fnfields.size ();
c906108c 15830 fn_flp->fn_fields = (struct fn_field *)
be2daae6
TT
15831 TYPE_ALLOC (type, sizeof (struct fn_field) * nf.fnfields.size ());
15832
15833 for (int k = 0; k < nf.fnfields.size (); ++k)
15834 fn_flp->fn_fields[k] = nf.fnfields[k];
c906108c
SS
15835 }
15836
be2daae6 15837 TYPE_NFN_FIELDS (type) = fip->fnfieldlists.size ();
c906108c
SS
15838}
15839
1168df01
JB
15840/* Returns non-zero if NAME is the name of a vtable member in CU's
15841 language, zero otherwise. */
15842static int
15843is_vtable_name (const char *name, struct dwarf2_cu *cu)
15844{
15845 static const char vptr[] = "_vptr";
15846
9c37b5ae
TT
15847 /* Look for the C++ form of the vtable. */
15848 if (startswith (name, vptr) && is_cplus_marker (name[sizeof (vptr) - 1]))
1168df01
JB
15849 return 1;
15850
15851 return 0;
15852}
15853
c0dd20ea 15854/* GCC outputs unnamed structures that are really pointers to member
0b92b5bb
TT
15855 functions, with the ABI-specified layout. If TYPE describes
15856 such a structure, smash it into a member function type.
61049d3b
DJ
15857
15858 GCC shouldn't do this; it should just output pointer to member DIEs.
15859 This is GCC PR debug/28767. */
c0dd20ea 15860
0b92b5bb
TT
15861static void
15862quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
c0dd20ea 15863{
09e2d7c7 15864 struct type *pfn_type, *self_type, *new_type;
c0dd20ea
DJ
15865
15866 /* Check for a structure with no name and two children. */
1f704f76 15867 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
0b92b5bb 15868 return;
c0dd20ea
DJ
15869
15870 /* Check for __pfn and __delta members. */
0b92b5bb
TT
15871 if (TYPE_FIELD_NAME (type, 0) == NULL
15872 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
15873 || TYPE_FIELD_NAME (type, 1) == NULL
15874 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
15875 return;
c0dd20ea
DJ
15876
15877 /* Find the type of the method. */
940da03e 15878 pfn_type = type->field (0).type ();
c0dd20ea 15879 if (pfn_type == NULL
78134374
SM
15880 || pfn_type->code () != TYPE_CODE_PTR
15881 || TYPE_TARGET_TYPE (pfn_type)->code () != TYPE_CODE_FUNC)
0b92b5bb 15882 return;
c0dd20ea
DJ
15883
15884 /* Look for the "this" argument. */
15885 pfn_type = TYPE_TARGET_TYPE (pfn_type);
1f704f76 15886 if (pfn_type->num_fields () == 0
940da03e
SM
15887 /* || pfn_type->field (0).type () == NULL */
15888 || pfn_type->field (0).type ()->code () != TYPE_CODE_PTR)
0b92b5bb 15889 return;
c0dd20ea 15890
940da03e 15891 self_type = TYPE_TARGET_TYPE (pfn_type->field (0).type ());
0b92b5bb 15892 new_type = alloc_type (objfile);
09e2d7c7 15893 smash_to_method_type (new_type, self_type, TYPE_TARGET_TYPE (pfn_type),
80fc5e77 15894 pfn_type->fields (), pfn_type->num_fields (),
a409645d 15895 pfn_type->has_varargs ());
0b92b5bb 15896 smash_to_methodptr_type (type, new_type);
c0dd20ea 15897}
1168df01 15898
e26624c6
TT
15899/* Helper for quirk_ada_thick_pointer. If TYPE is an array type that
15900 requires rewriting, then copy it and return the updated copy.
15901 Otherwise return nullptr. */
15902
15903static struct type *
15904rewrite_array_type (struct type *type)
15905{
15906 if (type->code () != TYPE_CODE_ARRAY)
15907 return nullptr;
15908
15909 struct type *index_type = type->index_type ();
15910 range_bounds *current_bounds = index_type->bounds ();
15911
15912 /* Handle multi-dimensional arrays. */
15913 struct type *new_target = rewrite_array_type (TYPE_TARGET_TYPE (type));
15914 if (new_target == nullptr)
15915 {
15916 /* Maybe we don't need to rewrite this array. */
15917 if (current_bounds->low.kind () == PROP_CONST
15918 && current_bounds->high.kind () == PROP_CONST)
15919 return nullptr;
15920 }
15921
15922 /* Either the target type was rewritten, or the bounds have to be
15923 updated. Either way we want to copy the type and update
15924 everything. */
15925 struct type *copy = copy_type (type);
15926 int nfields = copy->num_fields ();
15927 field *new_fields
15928 = ((struct field *) TYPE_ZALLOC (copy,
15929 nfields * sizeof (struct field)));
15930 memcpy (new_fields, copy->fields (), nfields * sizeof (struct field));
15931 copy->set_fields (new_fields);
15932 if (new_target != nullptr)
15933 TYPE_TARGET_TYPE (copy) = new_target;
15934
15935 struct type *index_copy = copy_type (index_type);
15936 range_bounds *bounds
15937 = (struct range_bounds *) TYPE_ZALLOC (index_copy,
15938 sizeof (range_bounds));
15939 *bounds = *current_bounds;
15940 bounds->low.set_const_val (1);
15941 bounds->high.set_const_val (0);
15942 index_copy->set_bounds (bounds);
15943 copy->set_index_type (index_copy);
15944
15945 return copy;
15946}
15947
57567375
TT
15948/* While some versions of GCC will generate complicated DWARF for an
15949 array (see quirk_ada_thick_pointer), more recent versions were
15950 modified to emit an explicit thick pointer structure. However, in
15951 this case, the array still has DWARF expressions for its ranges,
15952 and these must be ignored. */
15953
15954static void
15955quirk_ada_thick_pointer_struct (struct die_info *die, struct dwarf2_cu *cu,
15956 struct type *type)
15957{
15958 gdb_assert (cu->language == language_ada);
15959
15960 /* Check for a structure with two children. */
15961 if (type->code () != TYPE_CODE_STRUCT || type->num_fields () != 2)
15962 return;
15963
15964 /* Check for P_ARRAY and P_BOUNDS members. */
15965 if (TYPE_FIELD_NAME (type, 0) == NULL
15966 || strcmp (TYPE_FIELD_NAME (type, 0), "P_ARRAY") != 0
15967 || TYPE_FIELD_NAME (type, 1) == NULL
15968 || strcmp (TYPE_FIELD_NAME (type, 1), "P_BOUNDS") != 0)
15969 return;
15970
15971 /* Make sure we're looking at a pointer to an array. */
15972 if (type->field (0).type ()->code () != TYPE_CODE_PTR)
15973 return;
57567375 15974
e26624c6
TT
15975 /* The Ada code already knows how to handle these types, so all that
15976 we need to do is turn the bounds into static bounds. However, we
15977 don't want to rewrite existing array or index types in-place,
15978 because those may be referenced in other contexts where this
15979 rewriting is undesirable. */
15980 struct type *new_ary_type
15981 = rewrite_array_type (TYPE_TARGET_TYPE (type->field (0).type ()));
15982 if (new_ary_type != nullptr)
15983 type->field (0).set_type (lookup_pointer_type (new_ary_type));
57567375
TT
15984}
15985
2b4424c3
TT
15986/* If the DIE has a DW_AT_alignment attribute, return its value, doing
15987 appropriate error checking and issuing complaints if there is a
15988 problem. */
15989
15990static ULONGEST
15991get_alignment (struct dwarf2_cu *cu, struct die_info *die)
15992{
15993 struct attribute *attr = dwarf2_attr (die, DW_AT_alignment, cu);
15994
15995 if (attr == nullptr)
15996 return 0;
15997
cd6c91b4 15998 if (!attr->form_is_constant ())
2b4424c3 15999 {
b98664d3 16000 complaint (_("DW_AT_alignment must have constant form"
2b4424c3
TT
16001 " - DIE at %s [in module %s]"),
16002 sect_offset_str (die->sect_off),
5e22e966 16003 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16004 return 0;
16005 }
16006
529908cb
TT
16007 LONGEST val = attr->constant_value (0);
16008 if (val < 0)
2b4424c3 16009 {
529908cb
TT
16010 complaint (_("DW_AT_alignment value must not be negative"
16011 " - DIE at %s [in module %s]"),
16012 sect_offset_str (die->sect_off),
16013 objfile_name (cu->per_objfile->objfile));
16014 return 0;
2b4424c3 16015 }
529908cb 16016 ULONGEST align = val;
2b4424c3
TT
16017
16018 if (align == 0)
16019 {
b98664d3 16020 complaint (_("DW_AT_alignment value must not be zero"
2b4424c3
TT
16021 " - DIE at %s [in module %s]"),
16022 sect_offset_str (die->sect_off),
5e22e966 16023 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16024 return 0;
16025 }
16026 if ((align & (align - 1)) != 0)
16027 {
b98664d3 16028 complaint (_("DW_AT_alignment value must be a power of 2"
2b4424c3
TT
16029 " - DIE at %s [in module %s]"),
16030 sect_offset_str (die->sect_off),
5e22e966 16031 objfile_name (cu->per_objfile->objfile));
2b4424c3
TT
16032 return 0;
16033 }
16034
16035 return align;
16036}
16037
16038/* If the DIE has a DW_AT_alignment attribute, use its value to set
16039 the alignment for TYPE. */
16040
16041static void
16042maybe_set_alignment (struct dwarf2_cu *cu, struct die_info *die,
16043 struct type *type)
16044{
16045 if (!set_type_align (type, get_alignment (cu, die)))
b98664d3 16046 complaint (_("DW_AT_alignment value too large"
2b4424c3
TT
16047 " - DIE at %s [in module %s]"),
16048 sect_offset_str (die->sect_off),
5e22e966 16049 objfile_name (cu->per_objfile->objfile));
2b4424c3 16050}
685b1105 16051
e35000a7
TBA
16052/* Check if the given VALUE is a valid enum dwarf_calling_convention
16053 constant for a type, according to DWARF5 spec, Table 5.5. */
16054
16055static bool
16056is_valid_DW_AT_calling_convention_for_type (ULONGEST value)
16057{
16058 switch (value)
16059 {
16060 case DW_CC_normal:
16061 case DW_CC_pass_by_reference:
16062 case DW_CC_pass_by_value:
16063 return true;
16064
16065 default:
16066 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16067 "(%s) for a type"), pulongest (value));
e35000a7
TBA
16068 return false;
16069 }
16070}
16071
d0922fcf
TBA
16072/* Check if the given VALUE is a valid enum dwarf_calling_convention
16073 constant for a subroutine, according to DWARF5 spec, Table 3.3, and
16074 also according to GNU-specific values (see include/dwarf2.h). */
16075
16076static bool
16077is_valid_DW_AT_calling_convention_for_subroutine (ULONGEST value)
16078{
16079 switch (value)
16080 {
16081 case DW_CC_normal:
16082 case DW_CC_program:
16083 case DW_CC_nocall:
16084 return true;
16085
16086 case DW_CC_GNU_renesas_sh:
16087 case DW_CC_GNU_borland_fastcall_i386:
16088 case DW_CC_GDB_IBM_OpenCL:
16089 return true;
16090
16091 default:
16092 complaint (_("unrecognized DW_AT_calling_convention value "
3142e908 16093 "(%s) for a subroutine"), pulongest (value));
d0922fcf
TBA
16094 return false;
16095 }
16096}
16097
c906108c 16098/* Called when we find the DIE that starts a structure or union scope
c767944b
DJ
16099 (definition) to create a type for the structure or union. Fill in
16100 the type's name and general properties; the members will not be
83655187
DE
16101 processed until process_structure_scope. A symbol table entry for
16102 the type will also not be done until process_structure_scope (assuming
16103 the type has a name).
c906108c 16104
c767944b
DJ
16105 NOTE: we need to call these functions regardless of whether or not the
16106 DIE has a DW_AT_name attribute, since it might be an anonymous
c906108c 16107 structure or union. This gets the type entered into our set of
83655187 16108 user defined types. */
c906108c 16109
f792889a 16110static struct type *
134d01f1 16111read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16112{
5e22e966 16113 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
16114 struct type *type;
16115 struct attribute *attr;
15d034d0 16116 const char *name;
c906108c 16117
348e048f
DE
16118 /* If the definition of this type lives in .debug_types, read that type.
16119 Don't follow DW_AT_specification though, that will take us back up
16120 the chain and we want to go down. */
052c8bb8 16121 attr = die->attr (DW_AT_signature);
435d3d88 16122 if (attr != nullptr)
348e048f 16123 {
ac9ec31b 16124 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16125
ac9ec31b 16126 /* The type's CU may not be the same as CU.
02142a6c 16127 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16128 return set_die_type (die, type, cu);
16129 }
16130
c0dd20ea 16131 type = alloc_type (objfile);
c906108c 16132 INIT_CPLUS_SPECIFIC (type);
93311388 16133
39cbfefa
DJ
16134 name = dwarf2_name (die, cu);
16135 if (name != NULL)
c906108c 16136 {
987504bb 16137 if (cu->language == language_cplus
c44af4eb
TT
16138 || cu->language == language_d
16139 || cu->language == language_rust)
63d06c5c 16140 {
15d034d0 16141 const char *full_name = dwarf2_full_name (name, die, cu);
3da10d80
KS
16142
16143 /* dwarf2_full_name might have already finished building the DIE's
16144 type. If so, there is no need to continue. */
16145 if (get_die_type (die, cu) != NULL)
16146 return get_die_type (die, cu);
16147
d0e39ea2 16148 type->set_name (full_name);
63d06c5c
DC
16149 }
16150 else
16151 {
d8151005
DJ
16152 /* The name is already allocated along with this objfile, so
16153 we don't need to duplicate it for the type. */
d0e39ea2 16154 type->set_name (name);
63d06c5c 16155 }
c906108c
SS
16156 }
16157
16158 if (die->tag == DW_TAG_structure_type)
16159 {
67607e24 16160 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16161 }
16162 else if (die->tag == DW_TAG_union_type)
16163 {
67607e24 16164 type->set_code (TYPE_CODE_UNION);
c906108c
SS
16165 }
16166 else
16167 {
67607e24 16168 type->set_code (TYPE_CODE_STRUCT);
c906108c
SS
16169 }
16170
0cc2414c
TT
16171 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
16172 TYPE_DECLARED_CLASS (type) = 1;
16173
e35000a7
TBA
16174 /* Store the calling convention in the type if it's available in
16175 the die. Otherwise the calling convention remains set to
16176 the default value DW_CC_normal. */
16177 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
16178 if (attr != nullptr
529908cb 16179 && is_valid_DW_AT_calling_convention_for_type (attr->constant_value (0)))
e35000a7
TBA
16180 {
16181 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16182 TYPE_CPLUS_CALLING_CONVENTION (type)
529908cb 16183 = (enum dwarf_calling_convention) (attr->constant_value (0));
e35000a7
TBA
16184 }
16185
e142c38c 16186 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16187 if (attr != nullptr)
c906108c 16188 {
cd6c91b4 16189 if (attr->form_is_constant ())
dda83cd7 16190 TYPE_LENGTH (type) = attr->constant_value (0);
155bfbd3
JB
16191 else
16192 {
f8e89861 16193 struct dynamic_prop prop;
293e7e51 16194 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
5c54719c 16195 type->add_dyn_prop (DYN_PROP_BYTE_SIZE, prop);
dda83cd7 16196 TYPE_LENGTH (type) = 0;
155bfbd3 16197 }
c906108c
SS
16198 }
16199 else
16200 {
16201 TYPE_LENGTH (type) = 0;
16202 }
16203
2b4424c3
TT
16204 maybe_set_alignment (cu, die, type);
16205
5230b05a 16206 if (producer_is_icc_lt_14 (cu) && (TYPE_LENGTH (type) == 0))
685b1105 16207 {
5230b05a
WT
16208 /* ICC<14 does not output the required DW_AT_declaration on
16209 incomplete types, but gives them a size of zero. */
b4b73759 16210 type->set_is_stub (true);
685b1105
JK
16211 }
16212 else
9baccff6 16213 type->set_stub_is_supported (true);
685b1105 16214
dc718098 16215 if (die_is_declaration (die, cu))
b4b73759 16216 type->set_is_stub (true);
a6c727b2
DJ
16217 else if (attr == NULL && die->child == NULL
16218 && producer_is_realview (cu->producer))
16219 /* RealView does not output the required DW_AT_declaration
16220 on incomplete types. */
b4b73759 16221 type->set_is_stub (true);
dc718098 16222
c906108c
SS
16223 /* We need to add the type field to the die immediately so we don't
16224 infinitely recurse when dealing with pointers to the structure
0963b4bd 16225 type within the structure itself. */
1c379e20 16226 set_die_type (die, type, cu);
c906108c 16227
7e314c57
JK
16228 /* set_die_type should be already done. */
16229 set_descriptive_type (type, die, cu);
16230
c767944b
DJ
16231 return type;
16232}
16233
9c6a1327
TT
16234static void handle_struct_member_die
16235 (struct die_info *child_die,
16236 struct type *type,
16237 struct field_info *fi,
16238 std::vector<struct symbol *> *template_args,
16239 struct dwarf2_cu *cu);
16240
16241/* A helper for handle_struct_member_die that handles
16242 DW_TAG_variant_part. */
16243
16244static void
16245handle_variant_part (struct die_info *die, struct type *type,
16246 struct field_info *fi,
16247 std::vector<struct symbol *> *template_args,
16248 struct dwarf2_cu *cu)
16249{
16250 variant_part_builder *new_part;
16251 if (fi->current_variant_part == nullptr)
16252 {
16253 fi->variant_parts.emplace_back ();
16254 new_part = &fi->variant_parts.back ();
16255 }
16256 else if (!fi->current_variant_part->processing_variant)
16257 {
16258 complaint (_("nested DW_TAG_variant_part seen "
16259 "- DIE at %s [in module %s]"),
16260 sect_offset_str (die->sect_off),
5e22e966 16261 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16262 return;
16263 }
16264 else
16265 {
16266 variant_field &current = fi->current_variant_part->variants.back ();
16267 current.variant_parts.emplace_back ();
16268 new_part = &current.variant_parts.back ();
16269 }
16270
16271 /* When we recurse, we want callees to add to this new variant
16272 part. */
16273 scoped_restore save_current_variant_part
16274 = make_scoped_restore (&fi->current_variant_part, new_part);
16275
16276 struct attribute *discr = dwarf2_attr (die, DW_AT_discr, cu);
16277 if (discr == NULL)
16278 {
16279 /* It's a univariant form, an extension we support. */
16280 }
16281 else if (discr->form_is_ref ())
16282 {
16283 struct dwarf2_cu *target_cu = cu;
16284 struct die_info *target_die = follow_die_ref (die, discr, &target_cu);
16285
16286 new_part->discriminant_offset = target_die->sect_off;
16287 }
16288 else
16289 {
16290 complaint (_("DW_AT_discr does not have DIE reference form"
16291 " - DIE at %s [in module %s]"),
16292 sect_offset_str (die->sect_off),
5e22e966 16293 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16294 }
16295
16296 for (die_info *child_die = die->child;
16297 child_die != NULL;
16298 child_die = child_die->sibling)
16299 handle_struct_member_die (child_die, type, fi, template_args, cu);
16300}
16301
16302/* A helper for handle_struct_member_die that handles
16303 DW_TAG_variant. */
16304
16305static void
16306handle_variant (struct die_info *die, struct type *type,
16307 struct field_info *fi,
16308 std::vector<struct symbol *> *template_args,
16309 struct dwarf2_cu *cu)
16310{
16311 if (fi->current_variant_part == nullptr)
16312 {
16313 complaint (_("saw DW_TAG_variant outside DW_TAG_variant_part "
16314 "- DIE at %s [in module %s]"),
16315 sect_offset_str (die->sect_off),
5e22e966 16316 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16317 return;
16318 }
16319 if (fi->current_variant_part->processing_variant)
16320 {
16321 complaint (_("nested DW_TAG_variant seen "
16322 "- DIE at %s [in module %s]"),
16323 sect_offset_str (die->sect_off),
5e22e966 16324 objfile_name (cu->per_objfile->objfile));
9c6a1327
TT
16325 return;
16326 }
16327
16328 scoped_restore save_processing_variant
16329 = make_scoped_restore (&fi->current_variant_part->processing_variant,
16330 true);
16331
16332 fi->current_variant_part->variants.emplace_back ();
16333 variant_field &variant = fi->current_variant_part->variants.back ();
16334 variant.first_field = fi->fields.size ();
16335
16336 /* In a variant we want to get the discriminant and also add a
16337 field for our sole member child. */
16338 struct attribute *discr = dwarf2_attr (die, DW_AT_discr_value, cu);
cae21f8e 16339 if (discr == nullptr || !discr->form_is_constant ())
9c6a1327
TT
16340 {
16341 discr = dwarf2_attr (die, DW_AT_discr_list, cu);
9d2246fc 16342 if (discr == nullptr || discr->as_block ()->size == 0)
9c6a1327
TT
16343 variant.default_branch = true;
16344 else
9d2246fc 16345 variant.discr_list_data = discr->as_block ();
9c6a1327
TT
16346 }
16347 else
cae21f8e 16348 variant.discriminant_value = discr->constant_value (0);
9c6a1327
TT
16349
16350 for (die_info *variant_child = die->child;
16351 variant_child != NULL;
16352 variant_child = variant_child->sibling)
16353 handle_struct_member_die (variant_child, type, fi, template_args, cu);
16354
16355 variant.last_field = fi->fields.size ();
16356}
16357
2ddeaf8a
TT
16358/* A helper for process_structure_scope that handles a single member
16359 DIE. */
16360
16361static void
16362handle_struct_member_die (struct die_info *child_die, struct type *type,
16363 struct field_info *fi,
16364 std::vector<struct symbol *> *template_args,
16365 struct dwarf2_cu *cu)
16366{
16367 if (child_die->tag == DW_TAG_member
9c6a1327 16368 || child_die->tag == DW_TAG_variable)
2ddeaf8a
TT
16369 {
16370 /* NOTE: carlton/2002-11-05: A C++ static data member
16371 should be a DW_TAG_member that is a declaration, but
16372 all versions of G++ as of this writing (so through at
16373 least 3.2.1) incorrectly generate DW_TAG_variable
16374 tags for them instead. */
16375 dwarf2_add_field (fi, child_die, cu);
16376 }
16377 else if (child_die->tag == DW_TAG_subprogram)
16378 {
16379 /* Rust doesn't have member functions in the C++ sense.
16380 However, it does emit ordinary functions as children
16381 of a struct DIE. */
16382 if (cu->language == language_rust)
16383 read_func_scope (child_die, cu);
16384 else
16385 {
16386 /* C++ member function. */
16387 dwarf2_add_member_fn (fi, child_die, type, cu);
16388 }
16389 }
16390 else if (child_die->tag == DW_TAG_inheritance)
16391 {
16392 /* C++ base class field. */
16393 dwarf2_add_field (fi, child_die, cu);
16394 }
16395 else if (type_can_define_types (child_die))
16396 dwarf2_add_type_defn (fi, child_die, cu);
16397 else if (child_die->tag == DW_TAG_template_type_param
16398 || child_die->tag == DW_TAG_template_value_param)
16399 {
16400 struct symbol *arg = new_symbol (child_die, NULL, cu);
16401
16402 if (arg != NULL)
16403 template_args->push_back (arg);
16404 }
9c6a1327
TT
16405 else if (child_die->tag == DW_TAG_variant_part)
16406 handle_variant_part (child_die, type, fi, template_args, cu);
2ddeaf8a 16407 else if (child_die->tag == DW_TAG_variant)
9c6a1327 16408 handle_variant (child_die, type, fi, template_args, cu);
2ddeaf8a
TT
16409}
16410
c767944b
DJ
16411/* Finish creating a structure or union type, including filling in
16412 its members and creating a symbol for it. */
16413
16414static void
16415process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
16416{
5e22e966 16417 struct objfile *objfile = cu->per_objfile->objfile;
ca040673 16418 struct die_info *child_die;
c767944b
DJ
16419 struct type *type;
16420
16421 type = get_die_type (die, cu);
16422 if (type == NULL)
16423 type = read_structure_type (die, cu);
16424
3e1d3d8c 16425 bool has_template_parameters = false;
e142c38c 16426 if (die->child != NULL && ! die_is_declaration (die, cu))
c906108c
SS
16427 {
16428 struct field_info fi;
2f4732b0 16429 std::vector<struct symbol *> template_args;
c906108c 16430
639d11d3 16431 child_die = die->child;
c906108c
SS
16432
16433 while (child_die && child_die->tag)
16434 {
2ddeaf8a 16435 handle_struct_member_die (child_die, type, &fi, &template_args, cu);
436c571c 16436 child_die = child_die->sibling;
c906108c
SS
16437 }
16438
34eaf542 16439 /* Attach template arguments to type. */
2f4732b0 16440 if (!template_args.empty ())
34eaf542 16441 {
3e1d3d8c 16442 has_template_parameters = true;
34eaf542 16443 ALLOCATE_CPLUS_STRUCT_TYPE (type);
2f4732b0 16444 TYPE_N_TEMPLATE_ARGUMENTS (type) = template_args.size ();
34eaf542 16445 TYPE_TEMPLATE_ARGUMENTS (type)
8d749320
SM
16446 = XOBNEWVEC (&objfile->objfile_obstack,
16447 struct symbol *,
16448 TYPE_N_TEMPLATE_ARGUMENTS (type));
34eaf542 16449 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
2f4732b0 16450 template_args.data (),
34eaf542
TT
16451 (TYPE_N_TEMPLATE_ARGUMENTS (type)
16452 * sizeof (struct symbol *)));
34eaf542
TT
16453 }
16454
c906108c 16455 /* Attach fields and member functions to the type. */
317f7127 16456 if (fi.nfields () > 0)
e7c27a73 16457 dwarf2_attach_fields_to_type (&fi, type, cu);
be2daae6 16458 if (!fi.fnfieldlists.empty ())
c906108c 16459 {
e7c27a73 16460 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
c906108c 16461
c5aa993b 16462 /* Get the type which refers to the base class (possibly this
c906108c 16463 class itself) which contains the vtable pointer for the current
0d564a31
DJ
16464 class from the DW_AT_containing_type attribute. This use of
16465 DW_AT_containing_type is a GNU extension. */
c906108c 16466
e142c38c 16467 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
c906108c 16468 {
e7c27a73 16469 struct type *t = die_containing_type (die, cu);
c906108c 16470
ae6ae975 16471 set_type_vptr_basetype (type, t);
c906108c
SS
16472 if (type == t)
16473 {
c906108c
SS
16474 int i;
16475
16476 /* Our own class provides vtbl ptr. */
1f704f76 16477 for (i = t->num_fields () - 1;
c906108c
SS
16478 i >= TYPE_N_BASECLASSES (t);
16479 --i)
16480 {
0d5cff50 16481 const char *fieldname = TYPE_FIELD_NAME (t, i);
c906108c 16482
dda83cd7 16483 if (is_vtable_name (fieldname, cu))
c906108c 16484 {
ae6ae975 16485 set_type_vptr_fieldno (type, i);
c906108c
SS
16486 break;
16487 }
16488 }
16489
16490 /* Complain if virtual function table field not found. */
16491 if (i < TYPE_N_BASECLASSES (t))
b98664d3 16492 complaint (_("virtual function table pointer "
3e43a32a 16493 "not found when defining class '%s'"),
7d93a1e0 16494 type->name () ? type->name () : "");
c906108c
SS
16495 }
16496 else
16497 {
ae6ae975 16498 set_type_vptr_fieldno (type, TYPE_VPTR_FIELDNO (t));
c906108c
SS
16499 }
16500 }
f6235d4c 16501 else if (cu->producer
61012eef 16502 && startswith (cu->producer, "IBM(R) XL C/C++ Advanced Edition"))
f6235d4c
EZ
16503 {
16504 /* The IBM XLC compiler does not provide direct indication
dda83cd7
SM
16505 of the containing type, but the vtable pointer is
16506 always named __vfp. */
f6235d4c
EZ
16507
16508 int i;
16509
1f704f76 16510 for (i = type->num_fields () - 1;
f6235d4c
EZ
16511 i >= TYPE_N_BASECLASSES (type);
16512 --i)
16513 {
16514 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
16515 {
ae6ae975
DE
16516 set_type_vptr_fieldno (type, i);
16517 set_type_vptr_basetype (type, type);
f6235d4c
EZ
16518 break;
16519 }
16520 }
16521 }
c906108c 16522 }
98751a41
JK
16523
16524 /* Copy fi.typedef_field_list linked list elements content into the
16525 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
be2daae6 16526 if (!fi.typedef_field_list.empty ())
98751a41 16527 {
be2daae6 16528 int count = fi.typedef_field_list.size ();
98751a41 16529
a0d7a4ff 16530 ALLOCATE_CPLUS_STRUCT_TYPE (type);
98751a41 16531 TYPE_TYPEDEF_FIELD_ARRAY (type)
883fd55a 16532 = ((struct decl_field *)
be2daae6
TT
16533 TYPE_ALLOC (type,
16534 sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * count));
16535 TYPE_TYPEDEF_FIELD_COUNT (type) = count;
6e70227d 16536
be2daae6
TT
16537 for (int i = 0; i < fi.typedef_field_list.size (); ++i)
16538 TYPE_TYPEDEF_FIELD (type, i) = fi.typedef_field_list[i];
98751a41 16539 }
c767944b 16540
883fd55a
KS
16541 /* Copy fi.nested_types_list linked list elements content into the
16542 allocated array TYPE_NESTED_TYPES_ARRAY (type). */
be2daae6 16543 if (!fi.nested_types_list.empty () && cu->language != language_ada)
883fd55a 16544 {
be2daae6 16545 int count = fi.nested_types_list.size ();
883fd55a
KS
16546
16547 ALLOCATE_CPLUS_STRUCT_TYPE (type);
16548 TYPE_NESTED_TYPES_ARRAY (type)
16549 = ((struct decl_field *)
be2daae6
TT
16550 TYPE_ALLOC (type, sizeof (struct decl_field) * count));
16551 TYPE_NESTED_TYPES_COUNT (type) = count;
883fd55a 16552
be2daae6
TT
16553 for (int i = 0; i < fi.nested_types_list.size (); ++i)
16554 TYPE_NESTED_TYPES_FIELD (type, i) = fi.nested_types_list[i];
883fd55a 16555 }
c906108c 16556 }
63d06c5c 16557
bb5ed363 16558 quirk_gcc_member_function_pointer (type, objfile);
c9317f21
TT
16559 if (cu->language == language_rust && die->tag == DW_TAG_union_type)
16560 cu->rust_unions.push_back (type);
57567375
TT
16561 else if (cu->language == language_ada)
16562 quirk_ada_thick_pointer_struct (die, cu, type);
0b92b5bb 16563
90aeadfc
DC
16564 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
16565 snapshots) has been known to create a die giving a declaration
16566 for a class that has, as a child, a die giving a definition for a
16567 nested class. So we have to process our children even if the
16568 current die is a declaration. Normally, of course, a declaration
16569 won't have any children at all. */
134d01f1 16570
ca040673
DE
16571 child_die = die->child;
16572
90aeadfc
DC
16573 while (child_die != NULL && child_die->tag)
16574 {
16575 if (child_die->tag == DW_TAG_member
16576 || child_die->tag == DW_TAG_variable
34eaf542
TT
16577 || child_die->tag == DW_TAG_inheritance
16578 || child_die->tag == DW_TAG_template_value_param
16579 || child_die->tag == DW_TAG_template_type_param)
134d01f1 16580 {
90aeadfc 16581 /* Do nothing. */
134d01f1 16582 }
90aeadfc
DC
16583 else
16584 process_die (child_die, cu);
134d01f1 16585
436c571c 16586 child_die = child_die->sibling;
134d01f1
DJ
16587 }
16588
fa4028e9
JB
16589 /* Do not consider external references. According to the DWARF standard,
16590 these DIEs are identified by the fact that they have no byte_size
16591 attribute, and a declaration attribute. */
16592 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
15cd93d0
TV
16593 || !die_is_declaration (die, cu)
16594 || dwarf2_attr (die, DW_AT_signature, cu) != NULL)
3e1d3d8c
TT
16595 {
16596 struct symbol *sym = new_symbol (die, type, cu);
16597
16598 if (has_template_parameters)
16599 {
a776957c
TT
16600 struct symtab *symtab;
16601 if (sym != nullptr)
16602 symtab = symbol_symtab (sym);
16603 else if (cu->line_header != nullptr)
16604 {
16605 /* Any related symtab will do. */
16606 symtab
7ba99d21 16607 = cu->line_header->file_names ()[0].symtab;
a776957c
TT
16608 }
16609 else
16610 {
16611 symtab = nullptr;
16612 complaint (_("could not find suitable "
16613 "symtab for template parameter"
16614 " - DIE at %s [in module %s]"),
16615 sect_offset_str (die->sect_off),
16616 objfile_name (objfile));
16617 }
16618
16619 if (symtab != nullptr)
16620 {
16621 /* Make sure that the symtab is set on the new symbols.
16622 Even though they don't appear in this symtab directly,
16623 other parts of gdb assume that symbols do, and this is
16624 reasonably true. */
16625 for (int i = 0; i < TYPE_N_TEMPLATE_ARGUMENTS (type); ++i)
16626 symbol_set_symtab (TYPE_TEMPLATE_ARGUMENT (type, i), symtab);
16627 }
3e1d3d8c
TT
16628 }
16629 }
134d01f1
DJ
16630}
16631
ed6acedd
TT
16632/* Assuming DIE is an enumeration type, and TYPE is its associated
16633 type, update TYPE using some information only available in DIE's
16634 children. In particular, the fields are computed. */
55426c9d
JB
16635
16636static void
16637update_enumeration_type_from_children (struct die_info *die,
16638 struct type *type,
16639 struct dwarf2_cu *cu)
16640{
60f7655a 16641 struct die_info *child_die;
55426c9d
JB
16642 int unsigned_enum = 1;
16643 int flag_enum = 1;
55426c9d 16644
8268c778 16645 auto_obstack obstack;
ed6acedd 16646 std::vector<struct field> fields;
55426c9d 16647
60f7655a
DE
16648 for (child_die = die->child;
16649 child_die != NULL && child_die->tag;
436c571c 16650 child_die = child_die->sibling)
55426c9d
JB
16651 {
16652 struct attribute *attr;
16653 LONGEST value;
16654 const gdb_byte *bytes;
16655 struct dwarf2_locexpr_baton *baton;
16656 const char *name;
60f7655a 16657
55426c9d
JB
16658 if (child_die->tag != DW_TAG_enumerator)
16659 continue;
16660
16661 attr = dwarf2_attr (child_die, DW_AT_const_value, cu);
16662 if (attr == NULL)
16663 continue;
16664
16665 name = dwarf2_name (child_die, cu);
16666 if (name == NULL)
16667 name = "<anonymous enumerator>";
16668
16669 dwarf2_const_value_attr (attr, type, name, &obstack, cu,
16670 &value, &bytes, &baton);
16671 if (value < 0)
16672 {
16673 unsigned_enum = 0;
16674 flag_enum = 0;
16675 }
55426c9d 16676 else
edd45eb0
SM
16677 {
16678 if (count_one_bits_ll (value) >= 2)
16679 flag_enum = 0;
edd45eb0 16680 }
55426c9d 16681
ed6acedd
TT
16682 fields.emplace_back ();
16683 struct field &field = fields.back ();
16684 FIELD_NAME (field) = dwarf2_physname (name, child_die, cu);
16685 SET_FIELD_ENUMVAL (field, value);
16686 }
16687
16688 if (!fields.empty ())
16689 {
5e33d5f4 16690 type->set_num_fields (fields.size ());
3cabb6b0
SM
16691 type->set_fields
16692 ((struct field *)
16693 TYPE_ALLOC (type, sizeof (struct field) * fields.size ()));
80fc5e77 16694 memcpy (type->fields (), fields.data (),
ed6acedd 16695 sizeof (struct field) * fields.size ());
55426c9d
JB
16696 }
16697
16698 if (unsigned_enum)
653223d3
SM
16699 type->set_is_unsigned (true);
16700
55426c9d
JB
16701 if (flag_enum)
16702 TYPE_FLAG_ENUM (type) = 1;
55426c9d
JB
16703}
16704
134d01f1
DJ
16705/* Given a DW_AT_enumeration_type die, set its type. We do not
16706 complete the type's fields yet, or create any symbols. */
c906108c 16707
f792889a 16708static struct type *
134d01f1 16709read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 16710{
5e22e966 16711 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 16712 struct type *type;
c906108c 16713 struct attribute *attr;
0114d602 16714 const char *name;
134d01f1 16715
348e048f
DE
16716 /* If the definition of this type lives in .debug_types, read that type.
16717 Don't follow DW_AT_specification though, that will take us back up
16718 the chain and we want to go down. */
052c8bb8 16719 attr = die->attr (DW_AT_signature);
435d3d88 16720 if (attr != nullptr)
348e048f 16721 {
ac9ec31b 16722 type = get_DW_AT_signature_type (die, attr, cu);
9dc481d3 16723
ac9ec31b 16724 /* The type's CU may not be the same as CU.
02142a6c 16725 Ensure TYPE is recorded with CU in die_type_hash. */
348e048f
DE
16726 return set_die_type (die, type, cu);
16727 }
16728
c906108c
SS
16729 type = alloc_type (objfile);
16730
67607e24 16731 type->set_code (TYPE_CODE_ENUM);
94af9270 16732 name = dwarf2_full_name (NULL, die, cu);
39cbfefa 16733 if (name != NULL)
d0e39ea2 16734 type->set_name (name);
c906108c 16735
0626fc76
TT
16736 attr = dwarf2_attr (die, DW_AT_type, cu);
16737 if (attr != NULL)
16738 {
16739 struct type *underlying_type = die_type (die, cu);
16740
16741 TYPE_TARGET_TYPE (type) = underlying_type;
16742 }
16743
e142c38c 16744 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 16745 if (attr != nullptr)
c906108c 16746 {
529908cb 16747 TYPE_LENGTH (type) = attr->constant_value (0);
c906108c
SS
16748 }
16749 else
16750 {
16751 TYPE_LENGTH (type) = 0;
16752 }
16753
2b4424c3
TT
16754 maybe_set_alignment (cu, die, type);
16755
137033e9
JB
16756 /* The enumeration DIE can be incomplete. In Ada, any type can be
16757 declared as private in the package spec, and then defined only
16758 inside the package body. Such types are known as Taft Amendment
16759 Types. When another package uses such a type, an incomplete DIE
16760 may be generated by the compiler. */
02eb380e 16761 if (die_is_declaration (die, cu))
b4b73759 16762 type->set_is_stub (true);
02eb380e 16763
0626fc76
TT
16764 /* If this type has an underlying type that is not a stub, then we
16765 may use its attributes. We always use the "unsigned" attribute
16766 in this situation, because ordinarily we guess whether the type
16767 is unsigned -- but the guess can be wrong and the underlying type
16768 can tell us the reality. However, we defer to a local size
16769 attribute if one exists, because this lets the compiler override
16770 the underlying type if needed. */
e46d3488 16771 if (TYPE_TARGET_TYPE (type) != NULL && !TYPE_TARGET_TYPE (type)->is_stub ())
0626fc76 16772 {
9e7c9a03
HD
16773 struct type *underlying_type = TYPE_TARGET_TYPE (type);
16774 underlying_type = check_typedef (underlying_type);
653223d3
SM
16775
16776 type->set_is_unsigned (underlying_type->is_unsigned ());
16777
0626fc76 16778 if (TYPE_LENGTH (type) == 0)
9e7c9a03 16779 TYPE_LENGTH (type) = TYPE_LENGTH (underlying_type);
653223d3 16780
2b4424c3 16781 if (TYPE_RAW_ALIGN (type) == 0
9e7c9a03
HD
16782 && TYPE_RAW_ALIGN (underlying_type) != 0)
16783 set_type_align (type, TYPE_RAW_ALIGN (underlying_type));
0626fc76
TT
16784 }
16785
3d567982
TT
16786 TYPE_DECLARED_CLASS (type) = dwarf2_flag_true_p (die, DW_AT_enum_class, cu);
16787
ed6acedd
TT
16788 set_die_type (die, type, cu);
16789
16790 /* Finish the creation of this type by using the enum's children.
16791 Note that, as usual, this must come after set_die_type to avoid
16792 infinite recursion when trying to compute the names of the
16793 enumerators. */
16794 update_enumeration_type_from_children (die, type, cu);
16795
16796 return type;
134d01f1
DJ
16797}
16798
16799/* Given a pointer to a die which begins an enumeration, process all
16800 the dies that define the members of the enumeration, and create the
16801 symbol for the enumeration type.
16802
16803 NOTE: We reverse the order of the element list. */
16804
16805static void
16806process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
16807{
f792889a 16808 struct type *this_type;
134d01f1 16809
f792889a
DJ
16810 this_type = get_die_type (die, cu);
16811 if (this_type == NULL)
16812 this_type = read_enumeration_type (die, cu);
9dc481d3 16813
639d11d3 16814 if (die->child != NULL)
c906108c 16815 {
9dc481d3 16816 struct die_info *child_die;
15d034d0 16817 const char *name;
9dc481d3 16818
639d11d3 16819 child_die = die->child;
c906108c
SS
16820 while (child_die && child_die->tag)
16821 {
16822 if (child_die->tag != DW_TAG_enumerator)
16823 {
e7c27a73 16824 process_die (child_die, cu);
c906108c
SS
16825 }
16826 else
16827 {
39cbfefa
DJ
16828 name = dwarf2_name (child_die, cu);
16829 if (name)
ed6acedd 16830 new_symbol (child_die, this_type, cu);
c906108c
SS
16831 }
16832
436c571c 16833 child_die = child_die->sibling;
c906108c 16834 }
c906108c 16835 }
134d01f1 16836
6c83ed52
TT
16837 /* If we are reading an enum from a .debug_types unit, and the enum
16838 is a declaration, and the enum is not the signatured type in the
16839 unit, then we do not want to add a symbol for it. Adding a
16840 symbol would in some cases obscure the true definition of the
16841 enum, giving users an incomplete type when the definition is
16842 actually available. Note that we do not want to do this for all
16843 enums which are just declarations, because C++0x allows forward
16844 enum declarations. */
3019eac3 16845 if (cu->per_cu->is_debug_types
6c83ed52
TT
16846 && die_is_declaration (die, cu))
16847 {
52dc124a 16848 struct signatured_type *sig_type;
6c83ed52 16849
c0f78cd4 16850 sig_type = (struct signatured_type *) cu->per_cu;
9c541725
PA
16851 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
16852 if (sig_type->type_offset_in_section != die->sect_off)
6c83ed52
TT
16853 return;
16854 }
16855
f792889a 16856 new_symbol (die, this_type, cu);
c906108c
SS
16857}
16858
57567375
TT
16859/* Helper function for quirk_ada_thick_pointer that examines a bounds
16860 expression for an index type and finds the corresponding field
16861 offset in the hidden "P_BOUNDS" structure. Returns true on success
16862 and updates *FIELD, false if it fails to recognize an
16863 expression. */
16864
16865static bool
16866recognize_bound_expression (struct die_info *die, enum dwarf_attribute name,
16867 int *bounds_offset, struct field *field,
16868 struct dwarf2_cu *cu)
16869{
16870 struct attribute *attr = dwarf2_attr (die, name, cu);
16871 if (attr == nullptr || !attr->form_is_block ())
16872 return false;
16873
16874 const struct dwarf_block *block = attr->as_block ();
16875 const gdb_byte *start = block->data;
16876 const gdb_byte *end = block->data + block->size;
16877
16878 /* The expression to recognize generally looks like:
16879
16880 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16881 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16882
16883 However, the second "plus_uconst" may be missing:
16884
16885 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16886 DW_OP_deref_size: 4)
16887
16888 This happens when the field is at the start of the structure.
16889
16890 Also, the final deref may not be sized:
16891
16892 (DW_OP_push_object_address; DW_OP_plus_uconst: 4; DW_OP_deref;
16893 DW_OP_deref)
16894
16895 This happens when the size of the index type happens to be the
16896 same as the architecture's word size. This can occur with or
16897 without the second plus_uconst. */
16898
16899 if (end - start < 2)
16900 return false;
16901 if (*start++ != DW_OP_push_object_address)
16902 return false;
16903 if (*start++ != DW_OP_plus_uconst)
16904 return false;
16905
16906 uint64_t this_bound_off;
16907 start = gdb_read_uleb128 (start, end, &this_bound_off);
16908 if (start == nullptr || (int) this_bound_off != this_bound_off)
16909 return false;
16910 /* Update *BOUNDS_OFFSET if needed, or alternatively verify that it
16911 is consistent among all bounds. */
16912 if (*bounds_offset == -1)
16913 *bounds_offset = this_bound_off;
16914 else if (*bounds_offset != this_bound_off)
16915 return false;
16916
16917 if (start == end || *start++ != DW_OP_deref)
16918 return false;
16919
16920 int offset = 0;
16921 if (start ==end)
16922 return false;
16923 else if (*start == DW_OP_deref_size || *start == DW_OP_deref)
16924 {
16925 /* This means an offset of 0. */
16926 }
16927 else if (*start++ != DW_OP_plus_uconst)
16928 return false;
16929 else
16930 {
16931 /* The size is the parameter to DW_OP_plus_uconst. */
16932 uint64_t val;
16933 start = gdb_read_uleb128 (start, end, &val);
16934 if (start == nullptr)
16935 return false;
16936 if ((int) val != val)
16937 return false;
16938 offset = val;
16939 }
16940
16941 if (start == end)
16942 return false;
16943
16944 uint64_t size;
16945 if (*start == DW_OP_deref_size)
16946 {
16947 start = gdb_read_uleb128 (start + 1, end, &size);
16948 if (start == nullptr)
16949 return false;
16950 }
16951 else if (*start == DW_OP_deref)
16952 {
16953 size = cu->header.addr_size;
16954 ++start;
16955 }
16956 else
16957 return false;
16958
16959 SET_FIELD_BITPOS (*field, 8 * offset);
16960 if (size != TYPE_LENGTH (field->type ()))
16961 FIELD_BITSIZE (*field) = 8 * size;
16962
16963 return true;
16964}
16965
16966/* With -fgnat-encodings=minimal, gcc will emit some unusual DWARF for
16967 some kinds of Ada arrays:
16968
16969 <1><11db>: Abbrev Number: 7 (DW_TAG_array_type)
16970 <11dc> DW_AT_name : (indirect string, offset: 0x1bb8): string
16971 <11e0> DW_AT_data_location: 2 byte block: 97 6
16972 (DW_OP_push_object_address; DW_OP_deref)
16973 <11e3> DW_AT_type : <0x1173>
16974 <11e7> DW_AT_sibling : <0x1201>
16975 <2><11eb>: Abbrev Number: 8 (DW_TAG_subrange_type)
16976 <11ec> DW_AT_type : <0x1206>
16977 <11f0> DW_AT_lower_bound : 6 byte block: 97 23 8 6 94 4
16978 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16979 DW_OP_deref_size: 4)
16980 <11f7> DW_AT_upper_bound : 8 byte block: 97 23 8 6 23 4 94 4
16981 (DW_OP_push_object_address; DW_OP_plus_uconst: 8; DW_OP_deref;
16982 DW_OP_plus_uconst: 4; DW_OP_deref_size: 4)
16983
16984 This actually represents a "thick pointer", which is a structure
16985 with two elements: one that is a pointer to the array data, and one
16986 that is a pointer to another structure; this second structure holds
16987 the array bounds.
16988
16989 This returns a new type on success, or nullptr if this didn't
16990 recognize the type. */
16991
16992static struct type *
16993quirk_ada_thick_pointer (struct die_info *die, struct dwarf2_cu *cu,
16994 struct type *type)
16995{
16996 struct attribute *attr = dwarf2_attr (die, DW_AT_data_location, cu);
16997 /* So far we've only seen this with block form. */
16998 if (attr == nullptr || !attr->form_is_block ())
16999 return nullptr;
17000
17001 /* Note that this will fail if the structure layout is changed by
17002 the compiler. However, we have no good way to recognize some
17003 other layout, because we don't know what expression the compiler
17004 might choose to emit should this happen. */
17005 struct dwarf_block *blk = attr->as_block ();
17006 if (blk->size != 2
17007 || blk->data[0] != DW_OP_push_object_address
17008 || blk->data[1] != DW_OP_deref)
17009 return nullptr;
17010
17011 int bounds_offset = -1;
17012 int max_align = -1;
17013 std::vector<struct field> range_fields;
17014 for (struct die_info *child_die = die->child;
17015 child_die;
17016 child_die = child_die->sibling)
17017 {
17018 if (child_die->tag == DW_TAG_subrange_type)
17019 {
17020 struct type *underlying = read_subrange_index_type (child_die, cu);
17021
17022 int this_align = type_align (underlying);
17023 if (this_align > max_align)
17024 max_align = this_align;
17025
17026 range_fields.emplace_back ();
17027 range_fields.emplace_back ();
17028
17029 struct field &lower = range_fields[range_fields.size () - 2];
17030 struct field &upper = range_fields[range_fields.size () - 1];
17031
17032 lower.set_type (underlying);
17033 FIELD_ARTIFICIAL (lower) = 1;
17034
17035 upper.set_type (underlying);
17036 FIELD_ARTIFICIAL (upper) = 1;
17037
17038 if (!recognize_bound_expression (child_die, DW_AT_lower_bound,
17039 &bounds_offset, &lower, cu)
17040 || !recognize_bound_expression (child_die, DW_AT_upper_bound,
17041 &bounds_offset, &upper, cu))
17042 return nullptr;
17043 }
17044 }
17045
17046 /* This shouldn't really happen, but double-check that we found
17047 where the bounds are stored. */
17048 if (bounds_offset == -1)
17049 return nullptr;
17050
17051 struct objfile *objfile = cu->per_objfile->objfile;
17052 for (int i = 0; i < range_fields.size (); i += 2)
17053 {
17054 char name[20];
17055
17056 /* Set the name of each field in the bounds. */
17057 xsnprintf (name, sizeof (name), "LB%d", i / 2);
17058 FIELD_NAME (range_fields[i]) = objfile->intern (name);
17059 xsnprintf (name, sizeof (name), "UB%d", i / 2);
17060 FIELD_NAME (range_fields[i + 1]) = objfile->intern (name);
17061 }
17062
17063 struct type *bounds = alloc_type (objfile);
17064 bounds->set_code (TYPE_CODE_STRUCT);
17065
17066 bounds->set_num_fields (range_fields.size ());
17067 bounds->set_fields
17068 ((struct field *) TYPE_ALLOC (bounds, (bounds->num_fields ()
17069 * sizeof (struct field))));
17070 memcpy (bounds->fields (), range_fields.data (),
17071 bounds->num_fields () * sizeof (struct field));
17072
17073 int last_fieldno = range_fields.size () - 1;
17074 int bounds_size = (TYPE_FIELD_BITPOS (bounds, last_fieldno) / 8
17075 + TYPE_LENGTH (bounds->field (last_fieldno).type ()));
17076 TYPE_LENGTH (bounds) = align_up (bounds_size, max_align);
17077
17078 /* Rewrite the existing array type in place. Specifically, we
17079 remove any dynamic properties we might have read, and we replace
17080 the index types. */
17081 struct type *iter = type;
17082 for (int i = 0; i < range_fields.size (); i += 2)
17083 {
17084 gdb_assert (iter->code () == TYPE_CODE_ARRAY);
17085 iter->main_type->dyn_prop_list = nullptr;
17086 iter->set_index_type
17087 (create_static_range_type (NULL, bounds->field (i).type (), 1, 0));
17088 iter = TYPE_TARGET_TYPE (iter);
17089 }
17090
17091 struct type *result = alloc_type (objfile);
17092 result->set_code (TYPE_CODE_STRUCT);
17093
17094 result->set_num_fields (2);
17095 result->set_fields
17096 ((struct field *) TYPE_ZALLOC (result, (result->num_fields ()
17097 * sizeof (struct field))));
17098
17099 /* The names are chosen to coincide with what the compiler does with
17100 -fgnat-encodings=all, which the Ada code in gdb already
17101 understands. */
17102 TYPE_FIELD_NAME (result, 0) = "P_ARRAY";
17103 result->field (0).set_type (lookup_pointer_type (type));
17104
17105 TYPE_FIELD_NAME (result, 1) = "P_BOUNDS";
17106 result->field (1).set_type (lookup_pointer_type (bounds));
17107 SET_FIELD_BITPOS (result->field (1), 8 * bounds_offset);
17108
17109 result->set_name (type->name ());
17110 TYPE_LENGTH (result) = (TYPE_LENGTH (result->field (0).type ())
17111 + TYPE_LENGTH (result->field (1).type ()));
17112
17113 return result;
17114}
17115
c906108c
SS
17116/* Extract all information from a DW_TAG_array_type DIE and put it in
17117 the DIE's type field. For now, this only handles one dimensional
17118 arrays. */
17119
f792889a 17120static struct type *
e7c27a73 17121read_array_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17122{
5e22e966 17123 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 17124 struct die_info *child_die;
7e314c57 17125 struct type *type;
c906108c 17126 struct type *element_type, *range_type, *index_type;
c906108c 17127 struct attribute *attr;
15d034d0 17128 const char *name;
a405673c 17129 struct dynamic_prop *byte_stride_prop = NULL;
dc53a7ad 17130 unsigned int bit_stride = 0;
c906108c 17131
e7c27a73 17132 element_type = die_type (die, cu);
c906108c 17133
7e314c57
JK
17134 /* The die_type call above may have already set the type for this DIE. */
17135 type = get_die_type (die, cu);
17136 if (type)
17137 return type;
17138
dc53a7ad
JB
17139 attr = dwarf2_attr (die, DW_AT_byte_stride, cu);
17140 if (attr != NULL)
a405673c
JB
17141 {
17142 int stride_ok;
293e7e51 17143 struct type *prop_type = cu->addr_sized_int_type (false);
a405673c
JB
17144
17145 byte_stride_prop
17146 = (struct dynamic_prop *) alloca (sizeof (struct dynamic_prop));
9a49df9d
AB
17147 stride_ok = attr_to_dynamic_prop (attr, die, cu, byte_stride_prop,
17148 prop_type);
a405673c
JB
17149 if (!stride_ok)
17150 {
b98664d3 17151 complaint (_("unable to read array DW_AT_byte_stride "
9d8780f0
SM
17152 " - DIE at %s [in module %s]"),
17153 sect_offset_str (die->sect_off),
5e22e966 17154 objfile_name (cu->per_objfile->objfile));
a405673c
JB
17155 /* Ignore this attribute. We will likely not be able to print
17156 arrays of this type correctly, but there is little we can do
17157 to help if we cannot read the attribute's value. */
17158 byte_stride_prop = NULL;
17159 }
17160 }
dc53a7ad
JB
17161
17162 attr = dwarf2_attr (die, DW_AT_bit_stride, cu);
17163 if (attr != NULL)
529908cb 17164 bit_stride = attr->constant_value (0);
dc53a7ad 17165
c906108c
SS
17166 /* Irix 6.2 native cc creates array types without children for
17167 arrays with unspecified length. */
639d11d3 17168 if (die->child == NULL)
c906108c 17169 {
46bf5051 17170 index_type = objfile_type (objfile)->builtin_int;
0c9c3474 17171 range_type = create_static_range_type (NULL, index_type, 0, -1);
dc53a7ad 17172 type = create_array_type_with_stride (NULL, element_type, range_type,
a405673c 17173 byte_stride_prop, bit_stride);
f792889a 17174 return set_die_type (die, type, cu);
c906108c
SS
17175 }
17176
791afaa2 17177 std::vector<struct type *> range_types;
639d11d3 17178 child_die = die->child;
c906108c
SS
17179 while (child_die && child_die->tag)
17180 {
17181 if (child_die->tag == DW_TAG_subrange_type)
17182 {
f792889a 17183 struct type *child_type = read_type_die (child_die, cu);
9a619af0 17184
dda83cd7
SM
17185 if (child_type != NULL)
17186 {
0963b4bd 17187 /* The range type was succesfully read. Save it for the
dda83cd7 17188 array type creation. */
791afaa2 17189 range_types.push_back (child_type);
dda83cd7 17190 }
c906108c 17191 }
436c571c 17192 child_die = child_die->sibling;
c906108c
SS
17193 }
17194
17195 /* Dwarf2 dimensions are output from left to right, create the
17196 necessary array types in backwards order. */
7ca2d3a3 17197
c906108c 17198 type = element_type;
7ca2d3a3
DL
17199
17200 if (read_array_order (die, cu) == DW_ORD_col_major)
17201 {
17202 int i = 0;
9a619af0 17203
791afaa2 17204 while (i < range_types.size ())
10f6a3ad
TT
17205 {
17206 type = create_array_type_with_stride (NULL, type, range_types[i++],
17207 byte_stride_prop, bit_stride);
17208 bit_stride = 0;
17209 byte_stride_prop = nullptr;
17210 }
7ca2d3a3
DL
17211 }
17212 else
17213 {
791afaa2 17214 size_t ndim = range_types.size ();
7ca2d3a3 17215 while (ndim-- > 0)
10f6a3ad
TT
17216 {
17217 type = create_array_type_with_stride (NULL, type, range_types[ndim],
17218 byte_stride_prop, bit_stride);
17219 bit_stride = 0;
17220 byte_stride_prop = nullptr;
17221 }
7ca2d3a3 17222 }
c906108c 17223
f5f8a009
EZ
17224 /* Understand Dwarf2 support for vector types (like they occur on
17225 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
17226 array type. This is not part of the Dwarf2/3 standard yet, but a
17227 custom vendor extension. The main difference between a regular
17228 array and the vector variant is that vectors are passed by value
17229 to functions. */
e142c38c 17230 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
435d3d88 17231 if (attr != nullptr)
ea37ba09 17232 make_vector_type (type);
f5f8a009 17233
dbc98a8b
KW
17234 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
17235 implementation may choose to implement triple vectors using this
17236 attribute. */
17237 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb 17238 if (attr != nullptr && attr->form_is_unsigned ())
dbc98a8b 17239 {
529908cb
TT
17240 if (attr->as_unsigned () >= TYPE_LENGTH (type))
17241 TYPE_LENGTH (type) = attr->as_unsigned ();
dbc98a8b 17242 else
b98664d3 17243 complaint (_("DW_AT_byte_size for array type smaller "
3e43a32a 17244 "than the total size of elements"));
dbc98a8b
KW
17245 }
17246
39cbfefa
DJ
17247 name = dwarf2_name (die, cu);
17248 if (name)
d0e39ea2 17249 type->set_name (name);
6e70227d 17250
2b4424c3
TT
17251 maybe_set_alignment (cu, die, type);
17252
57567375
TT
17253 struct type *replacement_type = nullptr;
17254 if (cu->language == language_ada)
17255 {
17256 replacement_type = quirk_ada_thick_pointer (die, cu, type);
17257 if (replacement_type != nullptr)
17258 type = replacement_type;
17259 }
17260
0963b4bd 17261 /* Install the type in the die. */
57567375 17262 set_die_type (die, type, cu, replacement_type != nullptr);
7e314c57
JK
17263
17264 /* set_die_type should be already done. */
b4ba55a1
JB
17265 set_descriptive_type (type, die, cu);
17266
7e314c57 17267 return type;
c906108c
SS
17268}
17269
7ca2d3a3 17270static enum dwarf_array_dim_ordering
6e70227d 17271read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7ca2d3a3
DL
17272{
17273 struct attribute *attr;
17274
17275 attr = dwarf2_attr (die, DW_AT_ordering, cu);
17276
435d3d88 17277 if (attr != nullptr)
1bc397c5
TT
17278 {
17279 LONGEST val = attr->constant_value (-1);
17280 if (val == DW_ORD_row_major || val == DW_ORD_col_major)
17281 return (enum dwarf_array_dim_ordering) val;
17282 }
7ca2d3a3 17283
0963b4bd
MS
17284 /* GNU F77 is a special case, as at 08/2004 array type info is the
17285 opposite order to the dwarf2 specification, but data is still
17286 laid out as per normal fortran.
7ca2d3a3 17287
0963b4bd
MS
17288 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
17289 version checking. */
7ca2d3a3 17290
905e0470
PM
17291 if (cu->language == language_fortran
17292 && cu->producer && strstr (cu->producer, "GNU F77"))
7ca2d3a3
DL
17293 {
17294 return DW_ORD_row_major;
17295 }
17296
3a3440fb 17297 switch (cu->language_defn->array_ordering ())
7ca2d3a3
DL
17298 {
17299 case array_column_major:
17300 return DW_ORD_col_major;
17301 case array_row_major:
17302 default:
17303 return DW_ORD_row_major;
17304 };
17305}
17306
72019c9c 17307/* Extract all information from a DW_TAG_set_type DIE and put it in
0963b4bd 17308 the DIE's type field. */
72019c9c 17309
f792889a 17310static struct type *
72019c9c
GM
17311read_set_type (struct die_info *die, struct dwarf2_cu *cu)
17312{
7e314c57
JK
17313 struct type *domain_type, *set_type;
17314 struct attribute *attr;
f792889a 17315
7e314c57
JK
17316 domain_type = die_type (die, cu);
17317
17318 /* The die_type call above may have already set the type for this DIE. */
17319 set_type = get_die_type (die, cu);
17320 if (set_type)
17321 return set_type;
17322
17323 set_type = create_set_type (NULL, domain_type);
17324
17325 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
529908cb
TT
17326 if (attr != nullptr && attr->form_is_unsigned ())
17327 TYPE_LENGTH (set_type) = attr->as_unsigned ();
7e314c57 17328
2b4424c3
TT
17329 maybe_set_alignment (cu, die, set_type);
17330
f792889a 17331 return set_die_type (die, set_type, cu);
72019c9c 17332}
7ca2d3a3 17333
0971de02
TT
17334/* A helper for read_common_block that creates a locexpr baton.
17335 SYM is the symbol which we are marking as computed.
17336 COMMON_DIE is the DIE for the common block.
17337 COMMON_LOC is the location expression attribute for the common
17338 block itself.
17339 MEMBER_LOC is the location expression attribute for the particular
17340 member of the common block that we are processing.
17341 CU is the CU from which the above come. */
17342
17343static void
17344mark_common_block_symbol_computed (struct symbol *sym,
17345 struct die_info *common_die,
17346 struct attribute *common_loc,
17347 struct attribute *member_loc,
17348 struct dwarf2_cu *cu)
17349{
5e22e966 17350 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 17351 struct objfile *objfile = per_objfile->objfile;
0971de02
TT
17352 struct dwarf2_locexpr_baton *baton;
17353 gdb_byte *ptr;
17354 unsigned int cu_off;
08feed99 17355 enum bfd_endian byte_order = gdbarch_byte_order (objfile->arch ());
0971de02
TT
17356 LONGEST offset = 0;
17357
17358 gdb_assert (common_loc && member_loc);
4fc6c0d5
TT
17359 gdb_assert (common_loc->form_is_block ());
17360 gdb_assert (member_loc->form_is_block ()
cd6c91b4 17361 || member_loc->form_is_constant ());
0971de02 17362
8d749320 17363 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
a50264ba 17364 baton->per_objfile = per_objfile;
0971de02
TT
17365 baton->per_cu = cu->per_cu;
17366 gdb_assert (baton->per_cu);
17367
17368 baton->size = 5 /* DW_OP_call4 */ + 1 /* DW_OP_plus */;
17369
cd6c91b4 17370 if (member_loc->form_is_constant ())
0971de02 17371 {
0826b30a 17372 offset = member_loc->constant_value (0);
0971de02
TT
17373 baton->size += 1 /* DW_OP_addr */ + cu->header.addr_size;
17374 }
17375 else
9d2246fc 17376 baton->size += member_loc->as_block ()->size;
0971de02 17377
224c3ddb 17378 ptr = (gdb_byte *) obstack_alloc (&objfile->objfile_obstack, baton->size);
0971de02
TT
17379 baton->data = ptr;
17380
17381 *ptr++ = DW_OP_call4;
9c541725 17382 cu_off = common_die->sect_off - cu->per_cu->sect_off;
0971de02
TT
17383 store_unsigned_integer (ptr, 4, byte_order, cu_off);
17384 ptr += 4;
17385
cd6c91b4 17386 if (member_loc->form_is_constant ())
0971de02
TT
17387 {
17388 *ptr++ = DW_OP_addr;
17389 store_unsigned_integer (ptr, cu->header.addr_size, byte_order, offset);
17390 ptr += cu->header.addr_size;
17391 }
17392 else
17393 {
17394 /* We have to copy the data here, because DW_OP_call4 will only
17395 use a DW_AT_location attribute. */
9d2246fc
TT
17396 struct dwarf_block *block = member_loc->as_block ();
17397 memcpy (ptr, block->data, block->size);
17398 ptr += block->size;
0971de02
TT
17399 }
17400
17401 *ptr++ = DW_OP_plus;
17402 gdb_assert (ptr - baton->data == baton->size);
17403
0971de02 17404 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 17405 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
0971de02
TT
17406}
17407
4357ac6c
TT
17408/* Create appropriate locally-scoped variables for all the
17409 DW_TAG_common_block entries. Also create a struct common_block
17410 listing all such variables for `info common'. COMMON_BLOCK_DOMAIN
85102364 17411 is used to separate the common blocks name namespace from regular
4357ac6c 17412 variable names. */
c906108c
SS
17413
17414static void
e7c27a73 17415read_common_block (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17416{
0971de02
TT
17417 struct attribute *attr;
17418
17419 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 17420 if (attr != nullptr)
0971de02
TT
17421 {
17422 /* Support the .debug_loc offsets. */
4fc6c0d5 17423 if (attr->form_is_block ())
dda83cd7 17424 {
0971de02 17425 /* Ok. */
dda83cd7 17426 }
cd6c91b4 17427 else if (attr->form_is_section_offset ())
dda83cd7 17428 {
0971de02
TT
17429 dwarf2_complex_location_expr_complaint ();
17430 attr = NULL;
dda83cd7 17431 }
0971de02 17432 else
dda83cd7 17433 {
0971de02
TT
17434 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
17435 "common block member");
17436 attr = NULL;
dda83cd7 17437 }
0971de02
TT
17438 }
17439
639d11d3 17440 if (die->child != NULL)
c906108c 17441 {
5e22e966 17442 struct objfile *objfile = cu->per_objfile->objfile;
4357ac6c
TT
17443 struct die_info *child_die;
17444 size_t n_entries = 0, size;
17445 struct common_block *common_block;
17446 struct symbol *sym;
74ac6d43 17447
4357ac6c
TT
17448 for (child_die = die->child;
17449 child_die && child_die->tag;
436c571c 17450 child_die = child_die->sibling)
4357ac6c
TT
17451 ++n_entries;
17452
17453 size = (sizeof (struct common_block)
17454 + (n_entries - 1) * sizeof (struct symbol *));
224c3ddb
SM
17455 common_block
17456 = (struct common_block *) obstack_alloc (&objfile->objfile_obstack,
17457 size);
4357ac6c
TT
17458 memset (common_block->contents, 0, n_entries * sizeof (struct symbol *));
17459 common_block->n_entries = 0;
17460
17461 for (child_die = die->child;
17462 child_die && child_die->tag;
436c571c 17463 child_die = child_die->sibling)
4357ac6c
TT
17464 {
17465 /* Create the symbol in the DW_TAG_common_block block in the current
17466 symbol scope. */
e7c27a73 17467 sym = new_symbol (child_die, NULL, cu);
0971de02
TT
17468 if (sym != NULL)
17469 {
17470 struct attribute *member_loc;
17471
17472 common_block->contents[common_block->n_entries++] = sym;
17473
17474 member_loc = dwarf2_attr (child_die, DW_AT_data_member_location,
17475 cu);
17476 if (member_loc)
17477 {
17478 /* GDB has handled this for a long time, but it is
17479 not specified by DWARF. It seems to have been
17480 emitted by gfortran at least as recently as:
17481 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=23057. */
b98664d3 17482 complaint (_("Variable in common block has "
0971de02 17483 "DW_AT_data_member_location "
9d8780f0
SM
17484 "- DIE at %s [in module %s]"),
17485 sect_offset_str (child_die->sect_off),
518817b3 17486 objfile_name (objfile));
0971de02 17487
cd6c91b4 17488 if (member_loc->form_is_section_offset ())
0971de02 17489 dwarf2_complex_location_expr_complaint ();
cd6c91b4 17490 else if (member_loc->form_is_constant ()
4fc6c0d5 17491 || member_loc->form_is_block ())
0971de02 17492 {
435d3d88 17493 if (attr != nullptr)
0971de02
TT
17494 mark_common_block_symbol_computed (sym, die, attr,
17495 member_loc, cu);
17496 }
17497 else
17498 dwarf2_complex_location_expr_complaint ();
17499 }
17500 }
c906108c 17501 }
4357ac6c
TT
17502
17503 sym = new_symbol (die, objfile_type (objfile)->builtin_void, cu);
17504 SYMBOL_VALUE_COMMON_BLOCK (sym) = common_block;
c906108c
SS
17505 }
17506}
17507
0114d602 17508/* Create a type for a C++ namespace. */
d9fa45fe 17509
0114d602
DJ
17510static struct type *
17511read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
d9fa45fe 17512{
5e22e966 17513 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17514 const char *previous_prefix, *name;
9219021c 17515 int is_anonymous;
0114d602
DJ
17516 struct type *type;
17517
17518 /* For extensions, reuse the type of the original namespace. */
17519 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
17520 {
17521 struct die_info *ext_die;
17522 struct dwarf2_cu *ext_cu = cu;
9a619af0 17523
0114d602
DJ
17524 ext_die = dwarf2_extension (die, &ext_cu);
17525 type = read_type_die (ext_die, ext_cu);
9dc481d3
DE
17526
17527 /* EXT_CU may not be the same as CU.
02142a6c 17528 Ensure TYPE is recorded with CU in die_type_hash. */
0114d602
DJ
17529 return set_die_type (die, type, cu);
17530 }
9219021c 17531
e142c38c 17532 name = namespace_name (die, &is_anonymous, cu);
9219021c
DC
17533
17534 /* Now build the name of the current namespace. */
17535
0114d602
DJ
17536 previous_prefix = determine_prefix (die, cu);
17537 if (previous_prefix[0] != '\0')
17538 name = typename_concat (&objfile->objfile_obstack,
f55ee35c 17539 previous_prefix, name, 0, cu);
0114d602
DJ
17540
17541 /* Create the type. */
19f392bc 17542 type = init_type (objfile, TYPE_CODE_NAMESPACE, 0, name);
0114d602 17543
60531b24 17544 return set_die_type (die, type, cu);
0114d602
DJ
17545}
17546
22cee43f 17547/* Read a namespace scope. */
0114d602
DJ
17548
17549static void
17550read_namespace (struct die_info *die, struct dwarf2_cu *cu)
17551{
5e22e966 17552 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 17553 int is_anonymous;
9219021c 17554
5c4e30ca
DC
17555 /* Add a symbol associated to this if we haven't seen the namespace
17556 before. Also, add a using directive if it's an anonymous
17557 namespace. */
9219021c 17558
f2f0e013 17559 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
5c4e30ca
DC
17560 {
17561 struct type *type;
17562
0114d602 17563 type = read_type_die (die, cu);
e7c27a73 17564 new_symbol (die, type, cu);
5c4e30ca 17565
e8e80198 17566 namespace_name (die, &is_anonymous, cu);
5c4e30ca 17567 if (is_anonymous)
0114d602
DJ
17568 {
17569 const char *previous_prefix = determine_prefix (die, cu);
9a619af0 17570
eb1e02fd 17571 std::vector<const char *> excludes;
804d2729 17572 add_using_directive (using_directives (cu),
7d93a1e0 17573 previous_prefix, type->name (), NULL,
eb1e02fd 17574 NULL, excludes, 0, &objfile->objfile_obstack);
0114d602 17575 }
5c4e30ca 17576 }
9219021c 17577
639d11d3 17578 if (die->child != NULL)
d9fa45fe 17579 {
639d11d3 17580 struct die_info *child_die = die->child;
6e70227d 17581
d9fa45fe
DC
17582 while (child_die && child_die->tag)
17583 {
e7c27a73 17584 process_die (child_die, cu);
436c571c 17585 child_die = child_die->sibling;
d9fa45fe
DC
17586 }
17587 }
38d518c9
EZ
17588}
17589
f55ee35c
JK
17590/* Read a Fortran module as type. This DIE can be only a declaration used for
17591 imported module. Still we need that type as local Fortran "use ... only"
17592 declaration imports depend on the created type in determine_prefix. */
17593
17594static struct type *
17595read_module_type (struct die_info *die, struct dwarf2_cu *cu)
17596{
5e22e966 17597 struct objfile *objfile = cu->per_objfile->objfile;
15d034d0 17598 const char *module_name;
f55ee35c
JK
17599 struct type *type;
17600
17601 module_name = dwarf2_name (die, cu);
19f392bc 17602 type = init_type (objfile, TYPE_CODE_MODULE, 0, module_name);
f55ee35c 17603
f55ee35c
JK
17604 return set_die_type (die, type, cu);
17605}
17606
5d7cb8df
JK
17607/* Read a Fortran module. */
17608
17609static void
17610read_module (struct die_info *die, struct dwarf2_cu *cu)
17611{
17612 struct die_info *child_die = die->child;
530e8392
KB
17613 struct type *type;
17614
17615 type = read_type_die (die, cu);
17616 new_symbol (die, type, cu);
5d7cb8df 17617
5d7cb8df
JK
17618 while (child_die && child_die->tag)
17619 {
17620 process_die (child_die, cu);
436c571c 17621 child_die = child_die->sibling;
5d7cb8df
JK
17622 }
17623}
17624
38d518c9
EZ
17625/* Return the name of the namespace represented by DIE. Set
17626 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
17627 namespace. */
17628
17629static const char *
e142c38c 17630namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
38d518c9
EZ
17631{
17632 struct die_info *current_die;
17633 const char *name = NULL;
17634
17635 /* Loop through the extensions until we find a name. */
17636
17637 for (current_die = die;
17638 current_die != NULL;
f2f0e013 17639 current_die = dwarf2_extension (die, &cu))
38d518c9 17640 {
96553a0c
DE
17641 /* We don't use dwarf2_name here so that we can detect the absence
17642 of a name -> anonymous namespace. */
7d45c7c3 17643 name = dwarf2_string_attr (die, DW_AT_name, cu);
96553a0c 17644
38d518c9
EZ
17645 if (name != NULL)
17646 break;
17647 }
17648
17649 /* Is it an anonymous namespace? */
17650
17651 *is_anonymous = (name == NULL);
17652 if (*is_anonymous)
2b1dbab0 17653 name = CP_ANONYMOUS_NAMESPACE_STR;
38d518c9
EZ
17654
17655 return name;
d9fa45fe
DC
17656}
17657
c906108c
SS
17658/* Extract all information from a DW_TAG_pointer_type DIE and add to
17659 the user defined type vector. */
17660
f792889a 17661static struct type *
e7c27a73 17662read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17663{
5e22e966 17664 struct gdbarch *gdbarch = cu->per_objfile->objfile->arch ();
e7c27a73 17665 struct comp_unit_head *cu_header = &cu->header;
c906108c 17666 struct type *type;
8b2dbe47
KB
17667 struct attribute *attr_byte_size;
17668 struct attribute *attr_address_class;
17669 int byte_size, addr_class;
7e314c57
JK
17670 struct type *target_type;
17671
17672 target_type = die_type (die, cu);
c906108c 17673
7e314c57
JK
17674 /* The die_type call above may have already set the type for this DIE. */
17675 type = get_die_type (die, cu);
17676 if (type)
17677 return type;
17678
17679 type = lookup_pointer_type (target_type);
8b2dbe47 17680
e142c38c 17681 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8b2dbe47 17682 if (attr_byte_size)
529908cb 17683 byte_size = attr_byte_size->constant_value (cu_header->addr_size);
c906108c 17684 else
8b2dbe47
KB
17685 byte_size = cu_header->addr_size;
17686
e142c38c 17687 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8b2dbe47 17688 if (attr_address_class)
529908cb 17689 addr_class = attr_address_class->constant_value (DW_ADDR_none);
8b2dbe47
KB
17690 else
17691 addr_class = DW_ADDR_none;
17692
2b4424c3
TT
17693 ULONGEST alignment = get_alignment (cu, die);
17694
17695 /* If the pointer size, alignment, or address class is different
17696 than the default, create a type variant marked as such and set
17697 the length accordingly. */
17698 if (TYPE_LENGTH (type) != byte_size
17699 || (alignment != 0 && TYPE_RAW_ALIGN (type) != 0
17700 && alignment != TYPE_RAW_ALIGN (type))
17701 || addr_class != DW_ADDR_none)
c906108c 17702 {
5e2b427d 17703 if (gdbarch_address_class_type_flags_p (gdbarch))
8b2dbe47 17704 {
314ad88d
PA
17705 type_instance_flags type_flags
17706 = gdbarch_address_class_type_flags (gdbarch, byte_size,
17707 addr_class);
876cecd0
TT
17708 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
17709 == 0);
8b2dbe47
KB
17710 type = make_type_with_address_space (type, type_flags);
17711 }
17712 else if (TYPE_LENGTH (type) != byte_size)
17713 {
b98664d3 17714 complaint (_("invalid pointer size %d"), byte_size);
8b2dbe47 17715 }
2b4424c3
TT
17716 else if (TYPE_RAW_ALIGN (type) != alignment)
17717 {
b98664d3 17718 complaint (_("Invalid DW_AT_alignment"
2b4424c3
TT
17719 " - DIE at %s [in module %s]"),
17720 sect_offset_str (die->sect_off),
5e22e966 17721 objfile_name (cu->per_objfile->objfile));
2b4424c3 17722 }
6e70227d 17723 else
9a619af0
MS
17724 {
17725 /* Should we also complain about unhandled address classes? */
17726 }
c906108c 17727 }
8b2dbe47
KB
17728
17729 TYPE_LENGTH (type) = byte_size;
2b4424c3 17730 set_type_align (type, alignment);
f792889a 17731 return set_die_type (die, type, cu);
c906108c
SS
17732}
17733
17734/* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
17735 the user defined type vector. */
17736
f792889a 17737static struct type *
e7c27a73 17738read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c
SS
17739{
17740 struct type *type;
17741 struct type *to_type;
17742 struct type *domain;
17743
e7c27a73
DJ
17744 to_type = die_type (die, cu);
17745 domain = die_containing_type (die, cu);
0d5de010 17746
7e314c57
JK
17747 /* The calls above may have already set the type for this DIE. */
17748 type = get_die_type (die, cu);
17749 if (type)
17750 return type;
17751
78134374 17752 if (check_typedef (to_type)->code () == TYPE_CODE_METHOD)
0d5de010 17753 type = lookup_methodptr_type (to_type);
78134374 17754 else if (check_typedef (to_type)->code () == TYPE_CODE_FUNC)
7078baeb 17755 {
5e22e966 17756 struct type *new_type = alloc_type (cu->per_objfile->objfile);
7078baeb
TT
17757
17758 smash_to_method_type (new_type, domain, TYPE_TARGET_TYPE (to_type),
80fc5e77 17759 to_type->fields (), to_type->num_fields (),
a409645d 17760 to_type->has_varargs ());
7078baeb
TT
17761 type = lookup_methodptr_type (new_type);
17762 }
0d5de010
DJ
17763 else
17764 type = lookup_memberptr_type (to_type, domain);
c906108c 17765
f792889a 17766 return set_die_type (die, type, cu);
c906108c
SS
17767}
17768
4297a3f0 17769/* Extract all information from a DW_TAG_{rvalue_,}reference_type DIE and add to
c906108c
SS
17770 the user defined type vector. */
17771
f792889a 17772static struct type *
4297a3f0 17773read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu,
dda83cd7 17774 enum type_code refcode)
c906108c 17775{
e7c27a73 17776 struct comp_unit_head *cu_header = &cu->header;
7e314c57 17777 struct type *type, *target_type;
c906108c
SS
17778 struct attribute *attr;
17779
4297a3f0
AV
17780 gdb_assert (refcode == TYPE_CODE_REF || refcode == TYPE_CODE_RVALUE_REF);
17781
7e314c57
JK
17782 target_type = die_type (die, cu);
17783
17784 /* The die_type call above may have already set the type for this DIE. */
17785 type = get_die_type (die, cu);
17786 if (type)
17787 return type;
17788
4297a3f0 17789 type = lookup_reference_type (target_type, refcode);
e142c38c 17790 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 17791 if (attr != nullptr)
c906108c 17792 {
529908cb 17793 TYPE_LENGTH (type) = attr->constant_value (cu_header->addr_size);
c906108c
SS
17794 }
17795 else
17796 {
107d2387 17797 TYPE_LENGTH (type) = cu_header->addr_size;
c906108c 17798 }
2b4424c3 17799 maybe_set_alignment (cu, die, type);
f792889a 17800 return set_die_type (die, type, cu);
c906108c
SS
17801}
17802
cf363f18
MW
17803/* Add the given cv-qualifiers to the element type of the array. GCC
17804 outputs DWARF type qualifiers that apply to an array, not the
17805 element type. But GDB relies on the array element type to carry
17806 the cv-qualifiers. This mimics section 6.7.3 of the C99
17807 specification. */
17808
17809static struct type *
17810add_array_cv_type (struct die_info *die, struct dwarf2_cu *cu,
17811 struct type *base_type, int cnst, int voltl)
17812{
17813 struct type *el_type, *inner_array;
17814
17815 base_type = copy_type (base_type);
17816 inner_array = base_type;
17817
78134374 17818 while (TYPE_TARGET_TYPE (inner_array)->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17819 {
17820 TYPE_TARGET_TYPE (inner_array) =
17821 copy_type (TYPE_TARGET_TYPE (inner_array));
17822 inner_array = TYPE_TARGET_TYPE (inner_array);
17823 }
17824
17825 el_type = TYPE_TARGET_TYPE (inner_array);
17826 cnst |= TYPE_CONST (el_type);
17827 voltl |= TYPE_VOLATILE (el_type);
17828 TYPE_TARGET_TYPE (inner_array) = make_cv_type (cnst, voltl, el_type, NULL);
17829
17830 return set_die_type (die, base_type, cu);
17831}
17832
f792889a 17833static struct type *
e7c27a73 17834read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17835{
f792889a 17836 struct type *base_type, *cv_type;
c906108c 17837
e7c27a73 17838 base_type = die_type (die, cu);
7e314c57
JK
17839
17840 /* The die_type call above may have already set the type for this DIE. */
17841 cv_type = get_die_type (die, cu);
17842 if (cv_type)
17843 return cv_type;
17844
2f608a3a
KW
17845 /* In case the const qualifier is applied to an array type, the element type
17846 is so qualified, not the array type (section 6.7.3 of C99). */
78134374 17847 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18 17848 return add_array_cv_type (die, cu, base_type, 1, 0);
2f608a3a 17849
f792889a
DJ
17850 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
17851 return set_die_type (die, cv_type, cu);
c906108c
SS
17852}
17853
f792889a 17854static struct type *
e7c27a73 17855read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17856{
f792889a 17857 struct type *base_type, *cv_type;
c906108c 17858
e7c27a73 17859 base_type = die_type (die, cu);
7e314c57
JK
17860
17861 /* The die_type call above may have already set the type for this DIE. */
17862 cv_type = get_die_type (die, cu);
17863 if (cv_type)
17864 return cv_type;
17865
cf363f18
MW
17866 /* In case the volatile qualifier is applied to an array type, the
17867 element type is so qualified, not the array type (section 6.7.3
17868 of C99). */
78134374 17869 if (base_type->code () == TYPE_CODE_ARRAY)
cf363f18
MW
17870 return add_array_cv_type (die, cu, base_type, 0, 1);
17871
f792889a
DJ
17872 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
17873 return set_die_type (die, cv_type, cu);
c906108c
SS
17874}
17875
06d66ee9
TT
17876/* Handle DW_TAG_restrict_type. */
17877
17878static struct type *
17879read_tag_restrict_type (struct die_info *die, struct dwarf2_cu *cu)
17880{
17881 struct type *base_type, *cv_type;
17882
17883 base_type = die_type (die, cu);
17884
17885 /* The die_type call above may have already set the type for this DIE. */
17886 cv_type = get_die_type (die, cu);
17887 if (cv_type)
17888 return cv_type;
17889
17890 cv_type = make_restrict_type (base_type);
17891 return set_die_type (die, cv_type, cu);
17892}
17893
a2c2acaf
MW
17894/* Handle DW_TAG_atomic_type. */
17895
17896static struct type *
17897read_tag_atomic_type (struct die_info *die, struct dwarf2_cu *cu)
17898{
17899 struct type *base_type, *cv_type;
17900
17901 base_type = die_type (die, cu);
17902
17903 /* The die_type call above may have already set the type for this DIE. */
17904 cv_type = get_die_type (die, cu);
17905 if (cv_type)
17906 return cv_type;
17907
17908 cv_type = make_atomic_type (base_type);
17909 return set_die_type (die, cv_type, cu);
17910}
17911
c906108c
SS
17912/* Extract all information from a DW_TAG_string_type DIE and add to
17913 the user defined type vector. It isn't really a user defined type,
17914 but it behaves like one, with other DIE's using an AT_user_def_type
17915 attribute to reference it. */
17916
f792889a 17917static struct type *
e7c27a73 17918read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 17919{
5e22e966 17920 struct objfile *objfile = cu->per_objfile->objfile;
08feed99 17921 struct gdbarch *gdbarch = objfile->arch ();
c906108c
SS
17922 struct type *type, *range_type, *index_type, *char_type;
17923 struct attribute *attr;
216a7e6b
AB
17924 struct dynamic_prop prop;
17925 bool length_is_constant = true;
17926 LONGEST length;
17927
17928 /* There are a couple of places where bit sizes might be made use of
17929 when parsing a DW_TAG_string_type, however, no producer that we know
17930 of make use of these. Handling bit sizes that are a multiple of the
17931 byte size is easy enough, but what about other bit sizes? Lets deal
17932 with that problem when we have to. Warn about these attributes being
17933 unsupported, then parse the type and ignore them like we always
17934 have. */
17935 if (dwarf2_attr (die, DW_AT_bit_size, cu) != nullptr
17936 || dwarf2_attr (die, DW_AT_string_length_bit_size, cu) != nullptr)
17937 {
17938 static bool warning_printed = false;
17939 if (!warning_printed)
17940 {
17941 warning (_("DW_AT_bit_size and DW_AT_string_length_bit_size not "
17942 "currently supported on DW_TAG_string_type."));
17943 warning_printed = true;
17944 }
17945 }
c906108c 17946
e142c38c 17947 attr = dwarf2_attr (die, DW_AT_string_length, cu);
cd6c91b4 17948 if (attr != nullptr && !attr->form_is_constant ())
216a7e6b
AB
17949 {
17950 /* The string length describes the location at which the length of
17951 the string can be found. The size of the length field can be
17952 specified with one of the attributes below. */
17953 struct type *prop_type;
17954 struct attribute *len
17955 = dwarf2_attr (die, DW_AT_string_length_byte_size, cu);
17956 if (len == nullptr)
17957 len = dwarf2_attr (die, DW_AT_byte_size, cu);
cd6c91b4 17958 if (len != nullptr && len->form_is_constant ())
216a7e6b
AB
17959 {
17960 /* Pass 0 as the default as we know this attribute is constant
17961 and the default value will not be returned. */
0826b30a 17962 LONGEST sz = len->constant_value (0);
293e7e51 17963 prop_type = cu->per_objfile->int_type (sz, true);
216a7e6b
AB
17964 }
17965 else
17966 {
17967 /* If the size is not specified then we assume it is the size of
17968 an address on this target. */
293e7e51 17969 prop_type = cu->addr_sized_int_type (true);
216a7e6b
AB
17970 }
17971
17972 /* Convert the attribute into a dynamic property. */
17973 if (!attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
17974 length = 1;
17975 else
17976 length_is_constant = false;
17977 }
17978 else if (attr != nullptr)
17979 {
17980 /* This DW_AT_string_length just contains the length with no
17981 indirection. There's no need to create a dynamic property in this
17982 case. Pass 0 for the default value as we know it will not be
17983 returned in this case. */
0826b30a 17984 length = attr->constant_value (0);
216a7e6b
AB
17985 }
17986 else if ((attr = dwarf2_attr (die, DW_AT_byte_size, cu)) != nullptr)
c906108c 17987 {
216a7e6b 17988 /* We don't currently support non-constant byte sizes for strings. */
0826b30a 17989 length = attr->constant_value (1);
c906108c
SS
17990 }
17991 else
17992 {
216a7e6b
AB
17993 /* Use 1 as a fallback length if we have nothing else. */
17994 length = 1;
c906108c 17995 }
6ccb9162 17996
46bf5051 17997 index_type = objfile_type (objfile)->builtin_int;
216a7e6b
AB
17998 if (length_is_constant)
17999 range_type = create_static_range_type (NULL, index_type, 1, length);
18000 else
18001 {
18002 struct dynamic_prop low_bound;
18003
8c2e4e06 18004 low_bound.set_const_val (1);
216a7e6b
AB
18005 range_type = create_range_type (NULL, index_type, &low_bound, &prop, 0);
18006 }
3b7538c0
UW
18007 char_type = language_string_char_type (cu->language_defn, gdbarch);
18008 type = create_string_type (NULL, char_type, range_type);
6ccb9162 18009
f792889a 18010 return set_die_type (die, type, cu);
c906108c
SS
18011}
18012
4d804846
JB
18013/* Assuming that DIE corresponds to a function, returns nonzero
18014 if the function is prototyped. */
18015
18016static int
18017prototyped_function_p (struct die_info *die, struct dwarf2_cu *cu)
18018{
18019 struct attribute *attr;
18020
18021 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
c45bc3f8 18022 if (attr && attr->as_boolean ())
4d804846
JB
18023 return 1;
18024
18025 /* The DWARF standard implies that the DW_AT_prototyped attribute
85102364 18026 is only meaningful for C, but the concept also extends to other
4d804846
JB
18027 languages that allow unprototyped functions (Eg: Objective C).
18028 For all other languages, assume that functions are always
18029 prototyped. */
18030 if (cu->language != language_c
18031 && cu->language != language_objc
18032 && cu->language != language_opencl)
18033 return 1;
18034
18035 /* RealView does not emit DW_AT_prototyped. We can not distinguish
18036 prototyped and unprototyped functions; default to prototyped,
18037 since that is more common in modern code (and RealView warns
18038 about unprototyped functions). */
18039 if (producer_is_realview (cu->producer))
18040 return 1;
18041
18042 return 0;
18043}
18044
c906108c
SS
18045/* Handle DIES due to C code like:
18046
18047 struct foo
c5aa993b
JM
18048 {
18049 int (*funcp)(int a, long l);
18050 int b;
18051 };
c906108c 18052
0963b4bd 18053 ('funcp' generates a DW_TAG_subroutine_type DIE). */
c906108c 18054
f792889a 18055static struct type *
e7c27a73 18056read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18057{
5e22e966 18058 struct objfile *objfile = cu->per_objfile->objfile;
0963b4bd
MS
18059 struct type *type; /* Type that this function returns. */
18060 struct type *ftype; /* Function that returns above type. */
c906108c
SS
18061 struct attribute *attr;
18062
e7c27a73 18063 type = die_type (die, cu);
7e314c57
JK
18064
18065 /* The die_type call above may have already set the type for this DIE. */
18066 ftype = get_die_type (die, cu);
18067 if (ftype)
18068 return ftype;
18069
0c8b41f1 18070 ftype = lookup_function_type (type);
c906108c 18071
4d804846 18072 if (prototyped_function_p (die, cu))
27e69b7a 18073 ftype->set_is_prototyped (true);
c906108c 18074
c055b101
CV
18075 /* Store the calling convention in the type if it's available in
18076 the subroutine die. Otherwise set the calling convention to
18077 the default value DW_CC_normal. */
18078 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
d0922fcf 18079 if (attr != nullptr
529908cb 18080 && is_valid_DW_AT_calling_convention_for_subroutine (attr->constant_value (0)))
d0922fcf 18081 TYPE_CALLING_CONVENTION (ftype)
529908cb 18082 = (enum dwarf_calling_convention) attr->constant_value (0);
54fcddd0
UW
18083 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
18084 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
18085 else
18086 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
76c10ea2 18087
743649fd
MW
18088 /* Record whether the function returns normally to its caller or not
18089 if the DWARF producer set that information. */
18090 attr = dwarf2_attr (die, DW_AT_noreturn, cu);
c45bc3f8 18091 if (attr && attr->as_boolean ())
743649fd
MW
18092 TYPE_NO_RETURN (ftype) = 1;
18093
76c10ea2
GM
18094 /* We need to add the subroutine type to the die immediately so
18095 we don't infinitely recurse when dealing with parameters
0963b4bd 18096 declared as the same subroutine type. */
76c10ea2 18097 set_die_type (die, ftype, cu);
6e70227d 18098
639d11d3 18099 if (die->child != NULL)
c906108c 18100 {
bb5ed363 18101 struct type *void_type = objfile_type (objfile)->builtin_void;
c906108c 18102 struct die_info *child_die;
8072405b 18103 int nparams, iparams;
c906108c
SS
18104
18105 /* Count the number of parameters.
dda83cd7
SM
18106 FIXME: GDB currently ignores vararg functions, but knows about
18107 vararg member functions. */
8072405b 18108 nparams = 0;
639d11d3 18109 child_die = die->child;
c906108c
SS
18110 while (child_die && child_die->tag)
18111 {
18112 if (child_die->tag == DW_TAG_formal_parameter)
18113 nparams++;
18114 else if (child_die->tag == DW_TAG_unspecified_parameters)
1d6286ed
SM
18115 ftype->set_has_varargs (true);
18116
436c571c 18117 child_die = child_die->sibling;
c906108c
SS
18118 }
18119
18120 /* Allocate storage for parameters and fill them in. */
5e33d5f4 18121 ftype->set_num_fields (nparams);
3cabb6b0
SM
18122 ftype->set_fields
18123 ((struct field *) TYPE_ZALLOC (ftype, nparams * sizeof (struct field)));
c906108c 18124
8072405b
JK
18125 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
18126 even if we error out during the parameters reading below. */
18127 for (iparams = 0; iparams < nparams; iparams++)
5d14b6e5 18128 ftype->field (iparams).set_type (void_type);
8072405b
JK
18129
18130 iparams = 0;
639d11d3 18131 child_die = die->child;
c906108c
SS
18132 while (child_die && child_die->tag)
18133 {
18134 if (child_die->tag == DW_TAG_formal_parameter)
18135 {
3ce3b1ba
PA
18136 struct type *arg_type;
18137
18138 /* DWARF version 2 has no clean way to discern C++
18139 static and non-static member functions. G++ helps
18140 GDB by marking the first parameter for non-static
18141 member functions (which is the this pointer) as
18142 artificial. We pass this information to
18143 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
18144
18145 DWARF version 3 added DW_AT_object_pointer, which GCC
18146 4.5 does not yet generate. */
e142c38c 18147 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
435d3d88 18148 if (attr != nullptr)
c45bc3f8 18149 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = attr->as_boolean ();
c906108c 18150 else
9c37b5ae 18151 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
3ce3b1ba
PA
18152 arg_type = die_type (child_die, cu);
18153
18154 /* RealView does not mark THIS as const, which the testsuite
18155 expects. GCC marks THIS as const in method definitions,
18156 but not in the class specifications (GCC PR 43053). */
18157 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
18158 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
18159 {
18160 int is_this = 0;
18161 struct dwarf2_cu *arg_cu = cu;
18162 const char *name = dwarf2_name (child_die, cu);
18163
18164 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
435d3d88 18165 if (attr != nullptr)
3ce3b1ba
PA
18166 {
18167 /* If the compiler emits this, use it. */
18168 if (follow_die_ref (die, attr, &arg_cu) == child_die)
18169 is_this = 1;
18170 }
18171 else if (name && strcmp (name, "this") == 0)
18172 /* Function definitions will have the argument names. */
18173 is_this = 1;
18174 else if (name == NULL && iparams == 0)
18175 /* Declarations may not have the names, so like
18176 elsewhere in GDB, assume an artificial first
18177 argument is "this". */
18178 is_this = 1;
18179
18180 if (is_this)
18181 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
18182 arg_type, 0);
18183 }
18184
5d14b6e5 18185 ftype->field (iparams).set_type (arg_type);
c906108c
SS
18186 iparams++;
18187 }
436c571c 18188 child_die = child_die->sibling;
c906108c
SS
18189 }
18190 }
18191
76c10ea2 18192 return ftype;
c906108c
SS
18193}
18194
f792889a 18195static struct type *
e7c27a73 18196read_typedef (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18197{
5e22e966 18198 struct objfile *objfile = cu->per_objfile->objfile;
0114d602 18199 const char *name = NULL;
3c8e0968 18200 struct type *this_type, *target_type;
c906108c 18201
94af9270 18202 name = dwarf2_full_name (NULL, die, cu);
19f392bc 18203 this_type = init_type (objfile, TYPE_CODE_TYPEDEF, 0, name);
8f53807e 18204 this_type->set_target_is_stub (true);
f792889a 18205 set_die_type (die, this_type, cu);
3c8e0968
DE
18206 target_type = die_type (die, cu);
18207 if (target_type != this_type)
18208 TYPE_TARGET_TYPE (this_type) = target_type;
18209 else
18210 {
18211 /* Self-referential typedefs are, it seems, not allowed by the DWARF
18212 spec and cause infinite loops in GDB. */
b98664d3 18213 complaint (_("Self-referential DW_TAG_typedef "
9d8780f0
SM
18214 "- DIE at %s [in module %s]"),
18215 sect_offset_str (die->sect_off), objfile_name (objfile));
3c8e0968
DE
18216 TYPE_TARGET_TYPE (this_type) = NULL;
18217 }
e4003a34
TV
18218 if (name == NULL)
18219 {
18220 /* Gcc-7 and before supports -feliminate-dwarf2-dups, which generates
18221 anonymous typedefs, which is, strictly speaking, invalid DWARF.
18222 Handle these by just returning the target type, rather than
18223 constructing an anonymous typedef type and trying to handle this
18224 elsewhere. */
18225 set_die_type (die, target_type, cu);
18226 return target_type;
18227 }
f792889a 18228 return this_type;
c906108c
SS
18229}
18230
a625a8c9
TT
18231/* Helper for get_dwarf2_rational_constant that computes the value of
18232 a given gmp_mpz given an attribute. */
18233
18234static void
18235get_mpz (struct dwarf2_cu *cu, gdb_mpz *value, struct attribute *attr)
18236{
18237 /* GCC will sometimes emit a 16-byte constant value as a DWARF
18238 location expression that pushes an implicit value. */
18239 if (attr->form == DW_FORM_exprloc)
18240 {
18241 dwarf_block *blk = attr->as_block ();
18242 if (blk->size > 0 && blk->data[0] == DW_OP_implicit_value)
18243 {
18244 uint64_t len;
18245 const gdb_byte *ptr = safe_read_uleb128 (blk->data + 1,
18246 blk->data + blk->size,
18247 &len);
18248 if (ptr - blk->data + len <= blk->size)
18249 {
18250 mpz_import (value->val, len,
18251 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18252 1, 0, 0, ptr);
18253 return;
18254 }
18255 }
18256
18257 /* On failure set it to 1. */
18258 *value = gdb_mpz (1);
18259 }
18260 else if (attr->form_is_block ())
18261 {
18262 dwarf_block *blk = attr->as_block ();
18263 mpz_import (value->val, blk->size,
18264 bfd_big_endian (cu->per_objfile->objfile->obfd) ? 1 : -1,
18265 1, 0, 0, blk->data);
18266 }
18267 else
18268 *value = gdb_mpz (attr->constant_value (1));
18269}
18270
09584414
JB
18271/* Assuming DIE is a rational DW_TAG_constant, read the DIE's
18272 numerator and denominator into NUMERATOR and DENOMINATOR (resp).
18273
18274 If the numerator and/or numerator attribute is missing,
18275 a complaint is filed, and NUMERATOR and DENOMINATOR are left
18276 untouched. */
18277
18278static void
18279get_dwarf2_rational_constant (struct die_info *die, struct dwarf2_cu *cu,
5cde1d82 18280 gdb_mpz *numerator, gdb_mpz *denominator)
09584414
JB
18281{
18282 struct attribute *num_attr, *denom_attr;
18283
18284 num_attr = dwarf2_attr (die, DW_AT_GNU_numerator, cu);
18285 if (num_attr == nullptr)
18286 complaint (_("DW_AT_GNU_numerator missing in %s DIE at %s"),
18287 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18288
18289 denom_attr = dwarf2_attr (die, DW_AT_GNU_denominator, cu);
18290 if (denom_attr == nullptr)
18291 complaint (_("DW_AT_GNU_denominator missing in %s DIE at %s"),
18292 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18293
18294 if (num_attr == nullptr || denom_attr == nullptr)
18295 return;
18296
a625a8c9
TT
18297 get_mpz (cu, numerator, num_attr);
18298 get_mpz (cu, denominator, denom_attr);
09584414
JB
18299}
18300
18301/* Same as get_dwarf2_rational_constant, but extracting an unsigned
18302 rational constant, rather than a signed one.
18303
18304 If the rational constant has a negative value, a complaint
18305 is filed, and NUMERATOR and DENOMINATOR are left untouched. */
18306
18307static void
18308get_dwarf2_unsigned_rational_constant (struct die_info *die,
18309 struct dwarf2_cu *cu,
5cde1d82
TT
18310 gdb_mpz *numerator,
18311 gdb_mpz *denominator)
09584414 18312{
5cde1d82
TT
18313 gdb_mpz num (1);
18314 gdb_mpz denom (1);
09584414
JB
18315
18316 get_dwarf2_rational_constant (die, cu, &num, &denom);
5cde1d82 18317 if (mpz_sgn (num.val) == -1 && mpz_sgn (denom.val) == -1)
09584414 18318 {
5cde1d82
TT
18319 mpz_neg (num.val, num.val);
18320 mpz_neg (denom.val, denom.val);
09584414 18321 }
5cde1d82 18322 else if (mpz_sgn (num.val) == -1)
09584414
JB
18323 {
18324 complaint (_("unexpected negative value for DW_AT_GNU_numerator"
18325 " in DIE at %s"),
18326 sect_offset_str (die->sect_off));
18327 return;
18328 }
5cde1d82 18329 else if (mpz_sgn (denom.val) == -1)
09584414
JB
18330 {
18331 complaint (_("unexpected negative value for DW_AT_GNU_denominator"
18332 " in DIE at %s"),
18333 sect_offset_str (die->sect_off));
18334 return;
18335 }
18336
5cde1d82
TT
18337 *numerator = std::move (num);
18338 *denominator = std::move (denom);
09584414
JB
18339}
18340
18341/* Assuming DIE corresponds to a fixed point type, finish the creation
2a12c336 18342 of the corresponding TYPE by setting its type-specific data.
09584414
JB
18343 CU is the DIE's CU. */
18344
18345static void
18346finish_fixed_point_type (struct type *type, struct die_info *die,
18347 struct dwarf2_cu *cu)
18348{
18349 struct attribute *attr;
09584414
JB
18350
18351 gdb_assert (type->code () == TYPE_CODE_FIXED_POINT
18352 && TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_FIXED_POINT);
18353
18354 attr = dwarf2_attr (die, DW_AT_binary_scale, cu);
18355 if (!attr)
18356 attr = dwarf2_attr (die, DW_AT_decimal_scale, cu);
18357 if (!attr)
18358 attr = dwarf2_attr (die, DW_AT_small, cu);
18359
5cde1d82
TT
18360 /* Numerator and denominator of our fixed-point type's scaling factor.
18361 The default is a scaling factor of 1, which we use as a fallback
18362 when we are not able to decode it (problem with the debugging info,
18363 unsupported forms, bug in GDB, etc...). Using that as the default
18364 allows us to at least print the unscaled value, which might still
18365 be useful to a user. */
18366 gdb_mpz scale_num (1);
18367 gdb_mpz scale_denom (1);
18368
09584414
JB
18369 if (attr == nullptr)
18370 {
18371 /* Scaling factor not found. Assume a scaling factor of 1,
18372 and hope for the best. At least the user will be able to see
18373 the encoded value. */
18374 complaint (_("no scale found for fixed-point type (DIE at %s)"),
18375 sect_offset_str (die->sect_off));
18376 }
18377 else if (attr->name == DW_AT_binary_scale)
18378 {
18379 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18380 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18381
5cde1d82 18382 mpz_mul_2exp (num_or_denom->val, num_or_denom->val, std::abs (scale_exp));
09584414
JB
18383 }
18384 else if (attr->name == DW_AT_decimal_scale)
18385 {
18386 LONGEST scale_exp = attr->constant_value (0);
5cde1d82 18387 gdb_mpz *num_or_denom = scale_exp > 0 ? &scale_num : &scale_denom;
09584414 18388
5cde1d82 18389 mpz_ui_pow_ui (num_or_denom->val, 10, std::abs (scale_exp));
09584414
JB
18390 }
18391 else if (attr->name == DW_AT_small)
18392 {
18393 struct die_info *scale_die;
18394 struct dwarf2_cu *scale_cu = cu;
18395
18396 scale_die = follow_die_ref (die, attr, &scale_cu);
18397 if (scale_die->tag == DW_TAG_constant)
18398 get_dwarf2_unsigned_rational_constant (scale_die, scale_cu,
18399 &scale_num, &scale_denom);
18400 else
18401 complaint (_("%s DIE not supported as target of DW_AT_small attribute"
18402 " (DIE at %s)"),
18403 dwarf_tag_name (die->tag), sect_offset_str (die->sect_off));
18404 }
18405 else
18406 {
18407 complaint (_("unsupported scale attribute %s for fixed-point type"
18408 " (DIE at %s)"),
18409 dwarf_attr_name (attr->name),
18410 sect_offset_str (die->sect_off));
18411 }
18412
2a12c336 18413 gdb_mpq &scaling_factor = type->fixed_point_info ().scaling_factor;
5cde1d82
TT
18414 mpz_set (mpq_numref (scaling_factor.val), scale_num.val);
18415 mpz_set (mpq_denref (scaling_factor.val), scale_denom.val);
09584414
JB
18416 mpq_canonicalize (scaling_factor.val);
18417}
18418
9b790ce7
UW
18419/* Allocate a floating-point type of size BITS and name NAME. Pass NAME_HINT
18420 (which may be different from NAME) to the architecture back-end to allow
18421 it to guess the correct format if necessary. */
18422
18423static struct type *
18424dwarf2_init_float_type (struct objfile *objfile, int bits, const char *name,
103a685e 18425 const char *name_hint, enum bfd_endian byte_order)
9b790ce7 18426{
08feed99 18427 struct gdbarch *gdbarch = objfile->arch ();
9b790ce7
UW
18428 const struct floatformat **format;
18429 struct type *type;
18430
18431 format = gdbarch_floatformat_for_type (gdbarch, name_hint, bits);
18432 if (format)
103a685e 18433 type = init_float_type (objfile, bits, name, format, byte_order);
9b790ce7 18434 else
77b7c781 18435 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
9b790ce7
UW
18436
18437 return type;
18438}
18439
eb77c9df
AB
18440/* Allocate an integer type of size BITS and name NAME. */
18441
18442static struct type *
18443dwarf2_init_integer_type (struct dwarf2_cu *cu, struct objfile *objfile,
18444 int bits, int unsigned_p, const char *name)
18445{
18446 struct type *type;
18447
18448 /* Versions of Intel's C Compiler generate an integer type called "void"
18449 instead of using DW_TAG_unspecified_type. This has been seen on
18450 at least versions 14, 17, and 18. */
35ee2dc2
AB
18451 if (bits == 0 && producer_is_icc (cu) && name != nullptr
18452 && strcmp (name, "void") == 0)
eb77c9df
AB
18453 type = objfile_type (objfile)->builtin_void;
18454 else
18455 type = init_integer_type (objfile, bits, unsigned_p, name);
18456
18457 return type;
18458}
18459
09584414
JB
18460/* Return true if DIE has a DW_AT_small attribute whose value is
18461 a constant rational, where both the numerator and denominator
18462 are equal to zero.
18463
18464 CU is the DIE's Compilation Unit. */
18465
18466static bool
18467has_zero_over_zero_small_attribute (struct die_info *die,
18468 struct dwarf2_cu *cu)
18469{
18470 struct attribute *attr = dwarf2_attr (die, DW_AT_small, cu);
18471 if (attr == nullptr)
18472 return false;
18473
18474 struct dwarf2_cu *scale_cu = cu;
18475 struct die_info *scale_die
18476 = follow_die_ref (die, attr, &scale_cu);
18477
18478 if (scale_die->tag != DW_TAG_constant)
18479 return false;
18480
5cde1d82 18481 gdb_mpz num (1), denom (1);
09584414 18482 get_dwarf2_rational_constant (scale_die, cu, &num, &denom);
5cde1d82 18483 return mpz_sgn (num.val) == 0 && mpz_sgn (denom.val) == 0;
09584414
JB
18484}
18485
8bdc1658
AB
18486/* Initialise and return a floating point type of size BITS suitable for
18487 use as a component of a complex number. The NAME_HINT is passed through
18488 when initialising the floating point type and is the name of the complex
18489 type.
18490
18491 As DWARF doesn't currently provide an explicit name for the components
18492 of a complex number, but it can be helpful to have these components
18493 named, we try to select a suitable name based on the size of the
18494 component. */
18495static struct type *
18496dwarf2_init_complex_target_type (struct dwarf2_cu *cu,
18497 struct objfile *objfile,
103a685e
TT
18498 int bits, const char *name_hint,
18499 enum bfd_endian byte_order)
8bdc1658 18500{
08feed99 18501 gdbarch *gdbarch = objfile->arch ();
8bdc1658
AB
18502 struct type *tt = nullptr;
18503
35add35e
AB
18504 /* Try to find a suitable floating point builtin type of size BITS.
18505 We're going to use the name of this type as the name for the complex
18506 target type that we are about to create. */
1db455a7 18507 switch (cu->language)
8bdc1658 18508 {
1db455a7
AB
18509 case language_fortran:
18510 switch (bits)
18511 {
18512 case 32:
18513 tt = builtin_f_type (gdbarch)->builtin_real;
18514 break;
18515 case 64:
18516 tt = builtin_f_type (gdbarch)->builtin_real_s8;
18517 break;
18518 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18519 case 128:
18520 tt = builtin_f_type (gdbarch)->builtin_real_s16;
18521 break;
18522 }
8bdc1658 18523 break;
1db455a7
AB
18524 default:
18525 switch (bits)
18526 {
18527 case 32:
18528 tt = builtin_type (gdbarch)->builtin_float;
18529 break;
18530 case 64:
18531 tt = builtin_type (gdbarch)->builtin_double;
18532 break;
18533 case 96: /* The x86-32 ABI specifies 96-bit long double. */
18534 case 128:
18535 tt = builtin_type (gdbarch)->builtin_long_double;
18536 break;
18537 }
8bdc1658
AB
18538 break;
18539 }
18540
35add35e
AB
18541 /* If the type we found doesn't match the size we were looking for, then
18542 pretend we didn't find a type at all, the complex target type we
18543 create will then be nameless. */
a12e5744 18544 if (tt != nullptr && TYPE_LENGTH (tt) * TARGET_CHAR_BIT != bits)
35add35e
AB
18545 tt = nullptr;
18546
7d93a1e0 18547 const char *name = (tt == nullptr) ? nullptr : tt->name ();
103a685e 18548 return dwarf2_init_float_type (objfile, bits, name, name_hint, byte_order);
8bdc1658
AB
18549}
18550
c906108c
SS
18551/* Find a representation of a given base type and install
18552 it in the TYPE field of the die. */
18553
f792889a 18554static struct type *
e7c27a73 18555read_base_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 18556{
5e22e966 18557 struct objfile *objfile = cu->per_objfile->objfile;
c906108c
SS
18558 struct type *type;
18559 struct attribute *attr;
19f392bc 18560 int encoding = 0, bits = 0;
15d034d0 18561 const char *name;
34877895 18562 gdbarch *arch;
c906108c 18563
e142c38c 18564 attr = dwarf2_attr (die, DW_AT_encoding, cu);
529908cb
TT
18565 if (attr != nullptr && attr->form_is_constant ())
18566 encoding = attr->constant_value (0);
e142c38c 18567 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 18568 if (attr != nullptr)
529908cb 18569 bits = attr->constant_value (0) * TARGET_CHAR_BIT;
39cbfefa 18570 name = dwarf2_name (die, cu);
6ccb9162 18571 if (!name)
34877895 18572 complaint (_("DW_AT_name missing from DW_TAG_base_type"));
103a685e 18573
08feed99 18574 arch = objfile->arch ();
103a685e
TT
18575 enum bfd_endian byte_order = gdbarch_byte_order (arch);
18576
34877895 18577 attr = dwarf2_attr (die, DW_AT_endianity, cu);
529908cb 18578 if (attr != nullptr && attr->form_is_constant ())
103a685e 18579 {
529908cb 18580 int endianity = attr->constant_value (0);
103a685e
TT
18581
18582 switch (endianity)
18583 {
18584 case DW_END_big:
18585 byte_order = BFD_ENDIAN_BIG;
18586 break;
18587 case DW_END_little:
18588 byte_order = BFD_ENDIAN_LITTLE;
18589 break;
18590 default:
18591 complaint (_("DW_AT_endianity has unrecognized value %d"), endianity);
18592 break;
18593 }
18594 }
6ccb9162 18595
09584414
JB
18596 if ((encoding == DW_ATE_signed_fixed || encoding == DW_ATE_unsigned_fixed)
18597 && cu->language == language_ada
18598 && has_zero_over_zero_small_attribute (die, cu))
18599 {
18600 /* brobecker/2018-02-24: This is a fixed point type for which
18601 the scaling factor is represented as fraction whose value
18602 does not make sense (zero divided by zero), so we should
18603 normally never see these. However, there is a small category
18604 of fixed point types for which GNAT is unable to provide
18605 the scaling factor via the standard DWARF mechanisms, and
18606 for which the info is provided via the GNAT encodings instead.
18607 This is likely what this DIE is about.
18608
18609 Ideally, GNAT should be declaring this type the same way
18610 it declares other fixed point types when using the legacy
18611 GNAT encoding, which is to use a simple signed or unsigned
18612 base type. A report to the GNAT team has been created to
18613 look into it. In the meantime, pretend this type is a simple
18614 signed or unsigned integral, rather than a fixed point type,
18615 to avoid any confusion later on as to how to process this type. */
18616 encoding = (encoding == DW_ATE_signed_fixed
18617 ? DW_ATE_signed
18618 : DW_ATE_unsigned);
18619 }
18620
6ccb9162 18621 switch (encoding)
c906108c 18622 {
6ccb9162
UW
18623 case DW_ATE_address:
18624 /* Turn DW_ATE_address into a void * pointer. */
77b7c781 18625 type = init_type (objfile, TYPE_CODE_VOID, TARGET_CHAR_BIT, NULL);
19f392bc 18626 type = init_pointer_type (objfile, bits, name, type);
6ccb9162
UW
18627 break;
18628 case DW_ATE_boolean:
19f392bc 18629 type = init_boolean_type (objfile, bits, 1, name);
6ccb9162
UW
18630 break;
18631 case DW_ATE_complex_float:
103a685e
TT
18632 type = dwarf2_init_complex_target_type (cu, objfile, bits / 2, name,
18633 byte_order);
78134374 18634 if (type->code () == TYPE_CODE_ERROR)
93689ce9
TT
18635 {
18636 if (name == nullptr)
18637 {
18638 struct obstack *obstack
5e22e966 18639 = &cu->per_objfile->objfile->objfile_obstack;
7d93a1e0 18640 name = obconcat (obstack, "_Complex ", type->name (),
93689ce9
TT
18641 nullptr);
18642 }
18643 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
18644 }
18645 else
18646 type = init_complex_type (name, type);
6ccb9162
UW
18647 break;
18648 case DW_ATE_decimal_float:
19f392bc 18649 type = init_decfloat_type (objfile, bits, name);
6ccb9162
UW
18650 break;
18651 case DW_ATE_float:
103a685e 18652 type = dwarf2_init_float_type (objfile, bits, name, name, byte_order);
6ccb9162
UW
18653 break;
18654 case DW_ATE_signed:
eb77c9df 18655 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18656 break;
18657 case DW_ATE_unsigned:
3b2b8fea
TT
18658 if (cu->language == language_fortran
18659 && name
61012eef 18660 && startswith (name, "character("))
19f392bc
UW
18661 type = init_character_type (objfile, bits, 1, name);
18662 else
eb77c9df 18663 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162
UW
18664 break;
18665 case DW_ATE_signed_char:
6e70227d 18666 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea
TT
18667 || cu->language == language_pascal
18668 || cu->language == language_fortran)
19f392bc
UW
18669 type = init_character_type (objfile, bits, 0, name);
18670 else
eb77c9df 18671 type = dwarf2_init_integer_type (cu, objfile, bits, 0, name);
6ccb9162
UW
18672 break;
18673 case DW_ATE_unsigned_char:
868a0084 18674 if (cu->language == language_ada || cu->language == language_m2
3b2b8fea 18675 || cu->language == language_pascal
c44af4eb
TT
18676 || cu->language == language_fortran
18677 || cu->language == language_rust)
19f392bc
UW
18678 type = init_character_type (objfile, bits, 1, name);
18679 else
eb77c9df 18680 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
6ccb9162 18681 break;
75079b2b 18682 case DW_ATE_UTF:
53e710ac 18683 {
53e710ac
PA
18684 if (bits == 16)
18685 type = builtin_type (arch)->builtin_char16;
18686 else if (bits == 32)
18687 type = builtin_type (arch)->builtin_char32;
18688 else
18689 {
b98664d3 18690 complaint (_("unsupported DW_ATE_UTF bit size: '%d'"),
53e710ac 18691 bits);
eb77c9df 18692 type = dwarf2_init_integer_type (cu, objfile, bits, 1, name);
53e710ac
PA
18693 }
18694 return set_die_type (die, type, cu);
18695 }
75079b2b 18696 break;
09584414
JB
18697 case DW_ATE_signed_fixed:
18698 type = init_fixed_point_type (objfile, bits, 0, name);
18699 finish_fixed_point_type (type, die, cu);
18700 break;
18701 case DW_ATE_unsigned_fixed:
18702 type = init_fixed_point_type (objfile, bits, 1, name);
18703 finish_fixed_point_type (type, die, cu);
18704 break;
75079b2b 18705
6ccb9162 18706 default:
b98664d3 18707 complaint (_("unsupported DW_AT_encoding: '%s'"),
6ccb9162 18708 dwarf_type_encoding_name (encoding));
77b7c781 18709 type = init_type (objfile, TYPE_CODE_ERROR, bits, name);
6ccb9162 18710 break;
c906108c 18711 }
6ccb9162 18712
0114d602 18713 if (name && strcmp (name, "char") == 0)
15152a54 18714 type->set_has_no_signedness (true);
0114d602 18715
2b4424c3
TT
18716 maybe_set_alignment (cu, die, type);
18717
db558e34 18718 type->set_endianity_is_not_default (gdbarch_byte_order (arch) != byte_order);
34877895 18719
20a5fcbd
TT
18720 if (TYPE_SPECIFIC_FIELD (type) == TYPE_SPECIFIC_INT)
18721 {
18722 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
529908cb 18723 if (attr != nullptr && attr->as_unsigned () <= 8 * TYPE_LENGTH (type))
20a5fcbd 18724 {
529908cb 18725 unsigned real_bit_size = attr->as_unsigned ();
20a5fcbd
TT
18726 attr = dwarf2_attr (die, DW_AT_data_bit_offset, cu);
18727 /* Only use the attributes if they make sense together. */
18728 if (attr == nullptr
529908cb
TT
18729 || (attr->as_unsigned () + real_bit_size
18730 <= 8 * TYPE_LENGTH (type)))
20a5fcbd
TT
18731 {
18732 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_size
18733 = real_bit_size;
18734 if (attr != nullptr)
18735 TYPE_MAIN_TYPE (type)->type_specific.int_stuff.bit_offset
529908cb 18736 = attr->as_unsigned ();
20a5fcbd
TT
18737 }
18738 }
18739 }
18740
f792889a 18741 return set_die_type (die, type, cu);
c906108c
SS
18742}
18743
80180f79
SA
18744/* Parse dwarf attribute if it's a block, reference or constant and put the
18745 resulting value of the attribute into struct bound_prop.
18746 Returns 1 if ATTR could be resolved into PROP, 0 otherwise. */
18747
18748static int
18749attr_to_dynamic_prop (const struct attribute *attr, struct die_info *die,
9a49df9d
AB
18750 struct dwarf2_cu *cu, struct dynamic_prop *prop,
18751 struct type *default_type)
80180f79
SA
18752{
18753 struct dwarf2_property_baton *baton;
5e22e966 18754 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba
TT
18755 struct objfile *objfile = per_objfile->objfile;
18756 struct obstack *obstack = &objfile->objfile_obstack;
80180f79 18757
9a49df9d
AB
18758 gdb_assert (default_type != NULL);
18759
80180f79
SA
18760 if (attr == NULL || prop == NULL)
18761 return 0;
18762
4fc6c0d5 18763 if (attr->form_is_block ())
80180f79 18764 {
8d749320 18765 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18766 baton->property_type = default_type;
80180f79 18767 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18768 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18769
18770 struct dwarf_block *block = attr->as_block ();
18771 baton->locexpr.size = block->size;
18772 baton->locexpr.data = block->data;
216a7e6b
AB
18773 switch (attr->name)
18774 {
18775 case DW_AT_string_length:
18776 baton->locexpr.is_reference = true;
18777 break;
18778 default:
18779 baton->locexpr.is_reference = false;
18780 break;
18781 }
8c2e4e06
SM
18782
18783 prop->set_locexpr (baton);
18784 gdb_assert (prop->baton () != NULL);
80180f79 18785 }
cd6c91b4 18786 else if (attr->form_is_ref ())
80180f79
SA
18787 {
18788 struct dwarf2_cu *target_cu = cu;
18789 struct die_info *target_die;
18790 struct attribute *target_attr;
18791
18792 target_die = follow_die_ref (die, attr, &target_cu);
18793 target_attr = dwarf2_attr (target_die, DW_AT_location, target_cu);
df25ebbd
JB
18794 if (target_attr == NULL)
18795 target_attr = dwarf2_attr (target_die, DW_AT_data_member_location,
18796 target_cu);
80180f79
SA
18797 if (target_attr == NULL)
18798 return 0;
18799
df25ebbd 18800 switch (target_attr->name)
80180f79 18801 {
df25ebbd 18802 case DW_AT_location:
cd6c91b4 18803 if (target_attr->form_is_section_offset ())
df25ebbd 18804 {
8d749320 18805 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18806 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18807 fill_in_loclist_baton (cu, &baton->loclist, target_attr);
8c2e4e06
SM
18808 prop->set_loclist (baton);
18809 gdb_assert (prop->baton () != NULL);
df25ebbd 18810 }
4fc6c0d5 18811 else if (target_attr->form_is_block ())
df25ebbd 18812 {
8d749320 18813 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18814 baton->property_type = die_type (target_die, target_cu);
df25ebbd 18815 baton->locexpr.per_cu = cu->per_cu;
a50264ba 18816 baton->locexpr.per_objfile = per_objfile;
9d2246fc
TT
18817 struct dwarf_block *block = target_attr->as_block ();
18818 baton->locexpr.size = block->size;
18819 baton->locexpr.data = block->data;
9a49df9d 18820 baton->locexpr.is_reference = true;
8c2e4e06
SM
18821 prop->set_locexpr (baton);
18822 gdb_assert (prop->baton () != NULL);
df25ebbd
JB
18823 }
18824 else
18825 {
18826 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
18827 "dynamic property");
18828 return 0;
18829 }
18830 break;
18831 case DW_AT_data_member_location:
18832 {
18833 LONGEST offset;
18834
18835 if (!handle_data_member_location (target_die, target_cu,
18836 &offset))
18837 return 0;
18838
8d749320 18839 baton = XOBNEW (obstack, struct dwarf2_property_baton);
9a49df9d 18840 baton->property_type = read_type_die (target_die->parent,
6ad395a7 18841 target_cu);
df25ebbd
JB
18842 baton->offset_info.offset = offset;
18843 baton->offset_info.type = die_type (target_die, target_cu);
8c2e4e06 18844 prop->set_addr_offset (baton);
df25ebbd
JB
18845 break;
18846 }
80180f79
SA
18847 }
18848 }
cd6c91b4 18849 else if (attr->form_is_constant ())
8c2e4e06 18850 prop->set_const_val (attr->constant_value (0));
80180f79
SA
18851 else
18852 {
18853 dwarf2_invalid_attrib_class_complaint (dwarf_form_name (attr->form),
18854 dwarf2_name (die, cu));
18855 return 0;
18856 }
18857
18858 return 1;
18859}
18860
09ba997f 18861/* See read.h. */
9a49df9d 18862
09ba997f 18863struct type *
293e7e51 18864dwarf2_per_objfile::int_type (int size_in_bytes, bool unsigned_p) const
9a49df9d 18865{
9a49df9d
AB
18866 struct type *int_type;
18867
18868 /* Helper macro to examine the various builtin types. */
11a8b164
AB
18869#define TRY_TYPE(F) \
18870 int_type = (unsigned_p \
18871 ? objfile_type (objfile)->builtin_unsigned_ ## F \
18872 : objfile_type (objfile)->builtin_ ## F); \
18873 if (int_type != NULL && TYPE_LENGTH (int_type) == size_in_bytes) \
9a49df9d
AB
18874 return int_type
18875
18876 TRY_TYPE (char);
18877 TRY_TYPE (short);
18878 TRY_TYPE (int);
18879 TRY_TYPE (long);
18880 TRY_TYPE (long_long);
18881
18882#undef TRY_TYPE
18883
18884 gdb_assert_not_reached ("unable to find suitable integer type");
18885}
18886
09ba997f 18887/* See read.h. */
11a8b164 18888
09ba997f 18889struct type *
293e7e51 18890dwarf2_cu::addr_sized_int_type (bool unsigned_p) const
11a8b164 18891{
293e7e51
SM
18892 int addr_size = this->per_cu->addr_size ();
18893 return this->per_objfile->int_type (addr_size, unsigned_p);
11a8b164
AB
18894}
18895
b86352cf
AB
18896/* Read the DW_AT_type attribute for a sub-range. If this attribute is not
18897 present (which is valid) then compute the default type based on the
18898 compilation units address size. */
18899
18900static struct type *
18901read_subrange_index_type (struct die_info *die, struct dwarf2_cu *cu)
18902{
18903 struct type *index_type = die_type (die, cu);
18904
18905 /* Dwarf-2 specifications explicitly allows to create subrange types
18906 without specifying a base type.
18907 In that case, the base type must be set to the type of
18908 the lower bound, upper bound or count, in that order, if any of these
18909 three attributes references an object that has a type.
18910 If no base type is found, the Dwarf-2 specifications say that
18911 a signed integer type of size equal to the size of an address should
18912 be used.
18913 For the following C code: `extern char gdb_int [];'
18914 GCC produces an empty range DIE.
18915 FIXME: muller/2010-05-28: Possible references to object for low bound,
18916 high bound or count are not yet handled by this code. */
78134374 18917 if (index_type->code () == TYPE_CODE_VOID)
293e7e51 18918 index_type = cu->addr_sized_int_type (false);
b86352cf
AB
18919
18920 return index_type;
18921}
18922
a02abb62
JB
18923/* Read the given DW_AT_subrange DIE. */
18924
f792889a 18925static struct type *
a02abb62
JB
18926read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
18927{
4c9ad8c2 18928 struct type *base_type, *orig_base_type;
a02abb62
JB
18929 struct type *range_type;
18930 struct attribute *attr;
729efb13 18931 struct dynamic_prop low, high;
4fae6e18 18932 int low_default_is_valid;
c451ebe5 18933 int high_bound_is_count = 0;
15d034d0 18934 const char *name;
d359392f 18935 ULONGEST negative_mask;
e77813c8 18936
b86352cf
AB
18937 orig_base_type = read_subrange_index_type (die, cu);
18938
4c9ad8c2
TT
18939 /* If ORIG_BASE_TYPE is a typedef, it will not be TYPE_UNSIGNED,
18940 whereas the real type might be. So, we use ORIG_BASE_TYPE when
18941 creating the range type, but we use the result of check_typedef
18942 when examining properties of the type. */
18943 base_type = check_typedef (orig_base_type);
a02abb62 18944
7e314c57
JK
18945 /* The die_type call above may have already set the type for this DIE. */
18946 range_type = get_die_type (die, cu);
18947 if (range_type)
18948 return range_type;
18949
8c2e4e06 18950 high.set_const_val (0);
729efb13 18951
4fae6e18
JK
18952 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
18953 omitting DW_AT_lower_bound. */
18954 switch (cu->language)
6e70227d 18955 {
4fae6e18
JK
18956 case language_c:
18957 case language_cplus:
8c2e4e06 18958 low.set_const_val (0);
4fae6e18
JK
18959 low_default_is_valid = 1;
18960 break;
18961 case language_fortran:
8c2e4e06 18962 low.set_const_val (1);
4fae6e18
JK
18963 low_default_is_valid = 1;
18964 break;
18965 case language_d:
4fae6e18 18966 case language_objc:
c44af4eb 18967 case language_rust:
8c2e4e06 18968 low.set_const_val (0);
4fae6e18
JK
18969 low_default_is_valid = (cu->header.version >= 4);
18970 break;
18971 case language_ada:
18972 case language_m2:
18973 case language_pascal:
8c2e4e06 18974 low.set_const_val (1);
4fae6e18
JK
18975 low_default_is_valid = (cu->header.version >= 4);
18976 break;
18977 default:
8c2e4e06 18978 low.set_const_val (0);
4fae6e18
JK
18979 low_default_is_valid = 0;
18980 break;
a02abb62
JB
18981 }
18982
e142c38c 18983 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
435d3d88 18984 if (attr != nullptr)
9a49df9d 18985 attr_to_dynamic_prop (attr, die, cu, &low, base_type);
4fae6e18 18986 else if (!low_default_is_valid)
b98664d3 18987 complaint (_("Missing DW_AT_lower_bound "
9d8780f0
SM
18988 "- DIE at %s [in module %s]"),
18989 sect_offset_str (die->sect_off),
5e22e966 18990 objfile_name (cu->per_objfile->objfile));
a02abb62 18991
506f5c41
TV
18992 struct attribute *attr_ub, *attr_count;
18993 attr = attr_ub = dwarf2_attr (die, DW_AT_upper_bound, cu);
9a49df9d 18994 if (!attr_to_dynamic_prop (attr, die, cu, &high, base_type))
e77813c8 18995 {
506f5c41 18996 attr = attr_count = dwarf2_attr (die, DW_AT_count, cu);
9a49df9d 18997 if (attr_to_dynamic_prop (attr, die, cu, &high, base_type))
6b662e19 18998 {
c451ebe5 18999 /* If bounds are constant do the final calculation here. */
8c2e4e06
SM
19000 if (low.kind () == PROP_CONST && high.kind () == PROP_CONST)
19001 high.set_const_val (low.const_val () + high.const_val () - 1);
c451ebe5
SA
19002 else
19003 high_bound_is_count = 1;
c2ff108b 19004 }
506f5c41
TV
19005 else
19006 {
19007 if (attr_ub != NULL)
19008 complaint (_("Unresolved DW_AT_upper_bound "
19009 "- DIE at %s [in module %s]"),
19010 sect_offset_str (die->sect_off),
5e22e966 19011 objfile_name (cu->per_objfile->objfile));
506f5c41
TV
19012 if (attr_count != NULL)
19013 complaint (_("Unresolved DW_AT_count "
19014 "- DIE at %s [in module %s]"),
19015 sect_offset_str (die->sect_off),
5e22e966 19016 objfile_name (cu->per_objfile->objfile));
506f5c41 19017 }
e77813c8 19018 }
a02abb62 19019
4e962e74
TT
19020 LONGEST bias = 0;
19021 struct attribute *bias_attr = dwarf2_attr (die, DW_AT_GNU_bias, cu);
cd6c91b4 19022 if (bias_attr != nullptr && bias_attr->form_is_constant ())
0826b30a 19023 bias = bias_attr->constant_value (0);
4e962e74 19024
dbb9c2b1
JB
19025 /* Normally, the DWARF producers are expected to use a signed
19026 constant form (Eg. DW_FORM_sdata) to express negative bounds.
19027 But this is unfortunately not always the case, as witnessed
19028 with GCC, for instance, where the ambiguous DW_FORM_dataN form
19029 is used instead. To work around that ambiguity, we treat
19030 the bounds as signed, and thus sign-extend their values, when
19031 the base type is signed. */
6e70227d 19032 negative_mask =
d359392f 19033 -((ULONGEST) 1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1));
8c2e4e06 19034 if (low.kind () == PROP_CONST
c6d940a9 19035 && !base_type->is_unsigned () && (low.const_val () & negative_mask))
8c2e4e06
SM
19036 low.set_const_val (low.const_val () | negative_mask);
19037 if (high.kind () == PROP_CONST
c6d940a9 19038 && !base_type->is_unsigned () && (high.const_val () & negative_mask))
8c2e4e06 19039 high.set_const_val (high.const_val () | negative_mask);
43bbcdc2 19040
5bbd8269
AB
19041 /* Check for bit and byte strides. */
19042 struct dynamic_prop byte_stride_prop;
19043 attribute *attr_byte_stride = dwarf2_attr (die, DW_AT_byte_stride, cu);
19044 if (attr_byte_stride != nullptr)
19045 {
293e7e51 19046 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19047 attr_to_dynamic_prop (attr_byte_stride, die, cu, &byte_stride_prop,
19048 prop_type);
19049 }
19050
19051 struct dynamic_prop bit_stride_prop;
19052 attribute *attr_bit_stride = dwarf2_attr (die, DW_AT_bit_stride, cu);
19053 if (attr_bit_stride != nullptr)
19054 {
19055 /* It only makes sense to have either a bit or byte stride. */
19056 if (attr_byte_stride != nullptr)
19057 {
19058 complaint (_("Found DW_AT_bit_stride and DW_AT_byte_stride "
19059 "- DIE at %s [in module %s]"),
19060 sect_offset_str (die->sect_off),
5e22e966 19061 objfile_name (cu->per_objfile->objfile));
5bbd8269
AB
19062 attr_bit_stride = nullptr;
19063 }
19064 else
19065 {
293e7e51 19066 struct type *prop_type = cu->addr_sized_int_type (false);
5bbd8269
AB
19067 attr_to_dynamic_prop (attr_bit_stride, die, cu, &bit_stride_prop,
19068 prop_type);
19069 }
19070 }
19071
19072 if (attr_byte_stride != nullptr
19073 || attr_bit_stride != nullptr)
19074 {
19075 bool byte_stride_p = (attr_byte_stride != nullptr);
19076 struct dynamic_prop *stride
19077 = byte_stride_p ? &byte_stride_prop : &bit_stride_prop;
19078
19079 range_type
19080 = create_range_type_with_stride (NULL, orig_base_type, &low,
19081 &high, bias, stride, byte_stride_p);
19082 }
19083 else
19084 range_type = create_range_type (NULL, orig_base_type, &low, &high, bias);
a02abb62 19085
c451ebe5 19086 if (high_bound_is_count)
599088e3 19087 range_type->bounds ()->flag_upper_bound_is_count = 1;
c451ebe5 19088
c2ff108b
JK
19089 /* Ada expects an empty array on no boundary attributes. */
19090 if (attr == NULL && cu->language != language_ada)
8c2e4e06 19091 range_type->bounds ()->high.set_undefined ();
c2ff108b 19092
39cbfefa
DJ
19093 name = dwarf2_name (die, cu);
19094 if (name)
d0e39ea2 19095 range_type->set_name (name);
6e70227d 19096
e142c38c 19097 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
435d3d88 19098 if (attr != nullptr)
529908cb 19099 TYPE_LENGTH (range_type) = attr->constant_value (0);
a02abb62 19100
2b4424c3
TT
19101 maybe_set_alignment (cu, die, range_type);
19102
7e314c57
JK
19103 set_die_type (die, range_type, cu);
19104
19105 /* set_die_type should be already done. */
b4ba55a1
JB
19106 set_descriptive_type (range_type, die, cu);
19107
7e314c57 19108 return range_type;
a02abb62 19109}
6e70227d 19110
f792889a 19111static struct type *
81a17f79
JB
19112read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
19113{
19114 struct type *type;
81a17f79 19115
5e22e966 19116 type = init_type (cu->per_objfile->objfile, TYPE_CODE_VOID, 0, NULL);
d0e39ea2 19117 type->set_name (dwarf2_name (die, cu));
81a17f79 19118
74a2f8ff 19119 /* In Ada, an unspecified type is typically used when the description
85102364 19120 of the type is deferred to a different unit. When encountering
74a2f8ff
JB
19121 such a type, we treat it as a stub, and try to resolve it later on,
19122 when needed. */
19123 if (cu->language == language_ada)
b4b73759 19124 type->set_is_stub (true);
74a2f8ff 19125
f792889a 19126 return set_die_type (die, type, cu);
81a17f79 19127}
a02abb62 19128
639d11d3
DC
19129/* Read a single die and all its descendents. Set the die's sibling
19130 field to NULL; set other fields in the die correctly, and set all
19131 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
19132 location of the info_ptr after reading all of those dies. PARENT
19133 is the parent of the die in question. */
19134
19135static struct die_info *
dee91e82 19136read_die_and_children (const struct die_reader_specs *reader,
d521ce57
TT
19137 const gdb_byte *info_ptr,
19138 const gdb_byte **new_info_ptr,
dee91e82 19139 struct die_info *parent)
639d11d3
DC
19140{
19141 struct die_info *die;
d521ce57 19142 const gdb_byte *cur_ptr;
639d11d3 19143
3e225074 19144 cur_ptr = read_full_die_1 (reader, &die, info_ptr, 0);
1d325ec1
DJ
19145 if (die == NULL)
19146 {
19147 *new_info_ptr = cur_ptr;
19148 return NULL;
19149 }
93311388 19150 store_in_ref_table (die, reader->cu);
639d11d3 19151
3e225074 19152 if (die->has_children)
bf6af496 19153 die->child = read_die_and_siblings_1 (reader, cur_ptr, new_info_ptr, die);
639d11d3
DC
19154 else
19155 {
19156 die->child = NULL;
19157 *new_info_ptr = cur_ptr;
19158 }
19159
19160 die->sibling = NULL;
19161 die->parent = parent;
19162 return die;
19163}
19164
19165/* Read a die, all of its descendents, and all of its siblings; set
19166 all of the fields of all of the dies correctly. Arguments are as
19167 in read_die_and_children. */
19168
19169static struct die_info *
bf6af496 19170read_die_and_siblings_1 (const struct die_reader_specs *reader,
d521ce57
TT
19171 const gdb_byte *info_ptr,
19172 const gdb_byte **new_info_ptr,
bf6af496 19173 struct die_info *parent)
639d11d3
DC
19174{
19175 struct die_info *first_die, *last_sibling;
d521ce57 19176 const gdb_byte *cur_ptr;
639d11d3 19177
c906108c 19178 cur_ptr = info_ptr;
639d11d3
DC
19179 first_die = last_sibling = NULL;
19180
19181 while (1)
c906108c 19182 {
639d11d3 19183 struct die_info *die
dee91e82 19184 = read_die_and_children (reader, cur_ptr, &cur_ptr, parent);
639d11d3 19185
1d325ec1 19186 if (die == NULL)
c906108c 19187 {
639d11d3
DC
19188 *new_info_ptr = cur_ptr;
19189 return first_die;
c906108c 19190 }
1d325ec1
DJ
19191
19192 if (!first_die)
19193 first_die = die;
c906108c 19194 else
1d325ec1
DJ
19195 last_sibling->sibling = die;
19196
19197 last_sibling = die;
c906108c 19198 }
c906108c
SS
19199}
19200
bf6af496
DE
19201/* Read a die, all of its descendents, and all of its siblings; set
19202 all of the fields of all of the dies correctly. Arguments are as
19203 in read_die_and_children.
19204 This the main entry point for reading a DIE and all its children. */
19205
19206static struct die_info *
19207read_die_and_siblings (const struct die_reader_specs *reader,
d521ce57
TT
19208 const gdb_byte *info_ptr,
19209 const gdb_byte **new_info_ptr,
bf6af496
DE
19210 struct die_info *parent)
19211{
19212 struct die_info *die = read_die_and_siblings_1 (reader, info_ptr,
19213 new_info_ptr, parent);
19214
b4f54984 19215 if (dwarf_die_debug)
bf6af496
DE
19216 {
19217 fprintf_unfiltered (gdb_stdlog,
19218 "Read die from %s@0x%x of %s:\n",
96b79293 19219 reader->die_section->get_name (),
bf6af496
DE
19220 (unsigned) (info_ptr - reader->die_section->buffer),
19221 bfd_get_filename (reader->abfd));
b4f54984 19222 dump_die (die, dwarf_die_debug);
bf6af496
DE
19223 }
19224
19225 return die;
19226}
19227
3019eac3
DE
19228/* Read a die and all its attributes, leave space for NUM_EXTRA_ATTRS
19229 attributes.
19230 The caller is responsible for filling in the extra attributes
19231 and updating (*DIEP)->num_attrs.
19232 Set DIEP to point to a newly allocated die with its information,
3e225074 19233 except for its child, sibling, and parent fields. */
93311388 19234
d521ce57 19235static const gdb_byte *
3019eac3 19236read_full_die_1 (const struct die_reader_specs *reader,
d521ce57 19237 struct die_info **diep, const gdb_byte *info_ptr,
3e225074 19238 int num_extra_attrs)
93311388 19239{
b64f50a1 19240 unsigned int abbrev_number, bytes_read, i;
93311388
DE
19241 struct abbrev_info *abbrev;
19242 struct die_info *die;
19243 struct dwarf2_cu *cu = reader->cu;
19244 bfd *abfd = reader->abfd;
19245
9c541725 19246 sect_offset sect_off = (sect_offset) (info_ptr - reader->buffer);
93311388
DE
19247 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
19248 info_ptr += bytes_read;
19249 if (!abbrev_number)
19250 {
19251 *diep = NULL;
93311388
DE
19252 return info_ptr;
19253 }
19254
685af9cd 19255 abbrev = reader->abbrev_table->lookup_abbrev (abbrev_number);
93311388 19256 if (!abbrev)
348e048f
DE
19257 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
19258 abbrev_number,
19259 bfd_get_filename (abfd));
19260
3019eac3 19261 die = dwarf_alloc_die (cu, abbrev->num_attrs + num_extra_attrs);
9c541725 19262 die->sect_off = sect_off;
93311388
DE
19263 die->tag = abbrev->tag;
19264 die->abbrev = abbrev_number;
3e225074 19265 die->has_children = abbrev->has_children;
93311388 19266
3019eac3
DE
19267 /* Make the result usable.
19268 The caller needs to update num_attrs after adding the extra
19269 attributes. */
93311388
DE
19270 die->num_attrs = abbrev->num_attrs;
19271
7a5f294d 19272 bool any_need_reprocess = false;
93311388 19273 for (i = 0; i < abbrev->num_attrs; ++i)
18a8505e 19274 {
7a5f294d
TT
19275 info_ptr = read_attribute (reader, &die->attrs[i], &abbrev->attrs[i],
19276 info_ptr);
19277 if (die->attrs[i].requires_reprocessing_p ())
19278 any_need_reprocess = true;
18a8505e
AT
19279 }
19280
052c8bb8 19281 struct attribute *attr = die->attr (DW_AT_str_offsets_base);
529908cb
TT
19282 if (attr != nullptr && attr->form_is_unsigned ())
19283 cu->str_offsets_base = attr->as_unsigned ();
93311388 19284
41144253 19285 attr = die->attr (DW_AT_loclists_base);
19286 if (attr != nullptr)
529908cb 19287 cu->loclist_base = attr->as_unsigned ();
41144253 19288
a39fdb41 19289 auto maybe_addr_base = die->addr_base ();
18a8505e
AT
19290 if (maybe_addr_base.has_value ())
19291 cu->addr_base = *maybe_addr_base;
d0ce17d8
CT
19292
19293 attr = die->attr (DW_AT_rnglists_base);
19294 if (attr != nullptr)
529908cb 19295 cu->ranges_base = attr->as_unsigned ();
d0ce17d8 19296
7a5f294d
TT
19297 if (any_need_reprocess)
19298 {
19299 for (i = 0; i < abbrev->num_attrs; ++i)
19300 {
19301 if (die->attrs[i].requires_reprocessing_p ())
19302 read_attribute_reprocess (reader, &die->attrs[i], die->tag);
19303 }
19304 }
93311388 19305 *diep = die;
93311388
DE
19306 return info_ptr;
19307}
19308
3019eac3
DE
19309/* Read a die and all its attributes.
19310 Set DIEP to point to a newly allocated die with its information,
3e225074 19311 except for its child, sibling, and parent fields. */
3019eac3 19312
d521ce57 19313static const gdb_byte *
3019eac3 19314read_full_die (const struct die_reader_specs *reader,
3e225074 19315 struct die_info **diep, const gdb_byte *info_ptr)
3019eac3 19316{
d521ce57 19317 const gdb_byte *result;
bf6af496 19318
3e225074 19319 result = read_full_die_1 (reader, diep, info_ptr, 0);
bf6af496 19320
b4f54984 19321 if (dwarf_die_debug)
bf6af496
DE
19322 {
19323 fprintf_unfiltered (gdb_stdlog,
19324 "Read die from %s@0x%x of %s:\n",
96b79293 19325 reader->die_section->get_name (),
bf6af496
DE
19326 (unsigned) (info_ptr - reader->die_section->buffer),
19327 bfd_get_filename (reader->abfd));
b4f54984 19328 dump_die (*diep, dwarf_die_debug);
bf6af496
DE
19329 }
19330
19331 return result;
3019eac3 19332}
433df2d4 19333\f
c906108c 19334
72bf9492
DJ
19335/* Returns nonzero if TAG represents a type that we might generate a partial
19336 symbol for. */
19337
19338static int
d8f62e84 19339is_type_tag_for_partial (int tag, enum language lang)
72bf9492
DJ
19340{
19341 switch (tag)
19342 {
19343#if 0
19344 /* Some types that would be reasonable to generate partial symbols for,
d8f62e84
TT
19345 that we don't at present. Note that normally this does not
19346 matter, mainly because C compilers don't give names to these
19347 types, but instead emit DW_TAG_typedef. */
72bf9492
DJ
19348 case DW_TAG_file_type:
19349 case DW_TAG_ptr_to_member_type:
19350 case DW_TAG_set_type:
19351 case DW_TAG_string_type:
19352 case DW_TAG_subroutine_type:
19353#endif
d8f62e84
TT
19354
19355 /* GNAT may emit an array with a name, but no typedef, so we
19356 need to make a symbol in this case. */
19357 case DW_TAG_array_type:
19358 return lang == language_ada;
19359
72bf9492
DJ
19360 case DW_TAG_base_type:
19361 case DW_TAG_class_type:
680b30c7 19362 case DW_TAG_interface_type:
72bf9492
DJ
19363 case DW_TAG_enumeration_type:
19364 case DW_TAG_structure_type:
19365 case DW_TAG_subrange_type:
19366 case DW_TAG_typedef:
19367 case DW_TAG_union_type:
19368 return 1;
19369 default:
19370 return 0;
19371 }
19372}
19373
19374/* Load all DIEs that are interesting for partial symbols into memory. */
19375
19376static struct partial_die_info *
dee91e82 19377load_partial_dies (const struct die_reader_specs *reader,
d521ce57 19378 const gdb_byte *info_ptr, int building_psymtab)
72bf9492 19379{
dee91e82 19380 struct dwarf2_cu *cu = reader->cu;
5e22e966 19381 struct objfile *objfile = cu->per_objfile->objfile;
72bf9492 19382 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
72bf9492 19383 unsigned int bytes_read;
5afb4e99 19384 unsigned int load_all = 0;
72bf9492
DJ
19385 int nesting_level = 1;
19386
19387 parent_die = NULL;
19388 last_die = NULL;
19389
7adf1e79
DE
19390 gdb_assert (cu->per_cu != NULL);
19391 if (cu->per_cu->load_all_dies)
5afb4e99
DJ
19392 load_all = 1;
19393
72bf9492
DJ
19394 cu->partial_dies
19395 = htab_create_alloc_ex (cu->header.length / 12,
19396 partial_die_hash,
19397 partial_die_eq,
19398 NULL,
19399 &cu->comp_unit_obstack,
19400 hashtab_obstack_allocate,
19401 dummy_obstack_deallocate);
19402
72bf9492
DJ
19403 while (1)
19404 {
685af9cd 19405 abbrev_info *abbrev = peek_die_abbrev (*reader, info_ptr, &bytes_read);
72bf9492
DJ
19406
19407 /* A NULL abbrev means the end of a series of children. */
19408 if (abbrev == NULL)
19409 {
19410 if (--nesting_level == 0)
cd9983dd
YQ
19411 return first_die;
19412
72bf9492
DJ
19413 info_ptr += bytes_read;
19414 last_die = parent_die;
19415 parent_die = parent_die->die_parent;
19416 continue;
19417 }
19418
98bfdba5
PA
19419 /* Check for template arguments. We never save these; if
19420 they're seen, we just mark the parent, and go on our way. */
19421 if (parent_die != NULL
19422 && cu->language == language_cplus
19423 && (abbrev->tag == DW_TAG_template_type_param
19424 || abbrev->tag == DW_TAG_template_value_param))
19425 {
19426 parent_die->has_template_arguments = 1;
19427
19428 if (!load_all)
19429 {
19430 /* We don't need a partial DIE for the template argument. */
dee91e82 19431 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19432 continue;
19433 }
19434 }
19435
0d99eb77 19436 /* We only recurse into c++ subprograms looking for template arguments.
98bfdba5
PA
19437 Skip their other children. */
19438 if (!load_all
19439 && cu->language == language_cplus
19440 && parent_die != NULL
f9b5d5ea
TV
19441 && parent_die->tag == DW_TAG_subprogram
19442 && abbrev->tag != DW_TAG_inlined_subroutine)
98bfdba5 19443 {
dee91e82 19444 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
98bfdba5
PA
19445 continue;
19446 }
19447
5afb4e99
DJ
19448 /* Check whether this DIE is interesting enough to save. Normally
19449 we would not be interested in members here, but there may be
19450 later variables referencing them via DW_AT_specification (for
19451 static members). */
19452 if (!load_all
d8f62e84 19453 && !is_type_tag_for_partial (abbrev->tag, cu->language)
72929c62 19454 && abbrev->tag != DW_TAG_constant
72bf9492
DJ
19455 && abbrev->tag != DW_TAG_enumerator
19456 && abbrev->tag != DW_TAG_subprogram
b1dc1806 19457 && abbrev->tag != DW_TAG_inlined_subroutine
bc30ff58 19458 && abbrev->tag != DW_TAG_lexical_block
72bf9492 19459 && abbrev->tag != DW_TAG_variable
5afb4e99 19460 && abbrev->tag != DW_TAG_namespace
f55ee35c 19461 && abbrev->tag != DW_TAG_module
95554aad 19462 && abbrev->tag != DW_TAG_member
74921315
KS
19463 && abbrev->tag != DW_TAG_imported_unit
19464 && abbrev->tag != DW_TAG_imported_declaration)
72bf9492
DJ
19465 {
19466 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19467 info_ptr = skip_one_die (reader, info_ptr + bytes_read, abbrev);
72bf9492
DJ
19468 continue;
19469 }
19470
6f06d47b
YQ
19471 struct partial_die_info pdi ((sect_offset) (info_ptr - reader->buffer),
19472 abbrev);
cd9983dd 19473
48fbe735 19474 info_ptr = pdi.read (reader, *abbrev, info_ptr + bytes_read);
72bf9492
DJ
19475
19476 /* This two-pass algorithm for processing partial symbols has a
19477 high cost in cache pressure. Thus, handle some simple cases
19478 here which cover the majority of C partial symbols. DIEs
19479 which neither have specification tags in them, nor could have
19480 specification tags elsewhere pointing at them, can simply be
19481 processed and discarded.
19482
19483 This segment is also optional; scan_partial_symbols and
19484 add_partial_symbol will handle these DIEs if we chain
19485 them in normally. When compilers which do not emit large
19486 quantities of duplicate debug information are more common,
19487 this code can probably be removed. */
19488
19489 /* Any complete simple types at the top level (pretty much all
19490 of them, for a language without namespaces), can be processed
19491 directly. */
19492 if (parent_die == NULL
cd9983dd
YQ
19493 && pdi.has_specification == 0
19494 && pdi.is_declaration == 0
19495 && ((pdi.tag == DW_TAG_typedef && !pdi.has_children)
19496 || pdi.tag == DW_TAG_base_type
d8f62e84 19497 || pdi.tag == DW_TAG_array_type
cd9983dd 19498 || pdi.tag == DW_TAG_subrange_type))
72bf9492 19499 {
7d00ffec 19500 if (building_psymtab && pdi.raw_name != NULL)
f0fbb768
TT
19501 add_partial_symbol (&pdi, cu);
19502
cd9983dd 19503 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19504 continue;
19505 }
19506
d8228535
JK
19507 /* The exception for DW_TAG_typedef with has_children above is
19508 a workaround of GCC PR debug/47510. In the case of this complaint
a737d952 19509 type_name_or_error will error on such types later.
d8228535
JK
19510
19511 GDB skipped children of DW_TAG_typedef by the shortcut above and then
19512 it could not find the child DIEs referenced later, this is checked
19513 above. In correct DWARF DW_TAG_typedef should have no children. */
19514
cd9983dd 19515 if (pdi.tag == DW_TAG_typedef && pdi.has_children)
b98664d3 19516 complaint (_("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9d8780f0 19517 "- DIE at %s [in module %s]"),
cd9983dd 19518 sect_offset_str (pdi.sect_off), objfile_name (objfile));
d8228535 19519
72bf9492
DJ
19520 /* If we're at the second level, and we're an enumerator, and
19521 our parent has no specification (meaning possibly lives in a
19522 namespace elsewhere), then we can add the partial symbol now
19523 instead of queueing it. */
cd9983dd 19524 if (pdi.tag == DW_TAG_enumerator
72bf9492
DJ
19525 && parent_die != NULL
19526 && parent_die->die_parent == NULL
19527 && parent_die->tag == DW_TAG_enumeration_type
19528 && parent_die->has_specification == 0)
19529 {
7d00ffec 19530 if (pdi.raw_name == NULL)
b98664d3 19531 complaint (_("malformed enumerator DIE ignored"));
72bf9492 19532 else if (building_psymtab)
f0fbb768 19533 add_partial_symbol (&pdi, cu);
72bf9492 19534
cd9983dd 19535 info_ptr = locate_pdi_sibling (reader, &pdi, info_ptr);
72bf9492
DJ
19536 continue;
19537 }
19538
cd9983dd 19539 struct partial_die_info *part_die
6f06d47b 19540 = new (&cu->comp_unit_obstack) partial_die_info (pdi);
cd9983dd 19541
72bf9492
DJ
19542 /* We'll save this DIE so link it in. */
19543 part_die->die_parent = parent_die;
19544 part_die->die_sibling = NULL;
19545 part_die->die_child = NULL;
19546
19547 if (last_die && last_die == parent_die)
19548 last_die->die_child = part_die;
19549 else if (last_die)
19550 last_die->die_sibling = part_die;
19551
19552 last_die = part_die;
19553
19554 if (first_die == NULL)
19555 first_die = part_die;
19556
19557 /* Maybe add the DIE to the hash table. Not all DIEs that we
19558 find interesting need to be in the hash table, because we
19559 also have the parent/sibling/child chains; only those that we
19560 might refer to by offset later during partial symbol reading.
19561
19562 For now this means things that might have be the target of a
19563 DW_AT_specification, DW_AT_abstract_origin, or
19564 DW_AT_extension. DW_AT_extension will refer only to
19565 namespaces; DW_AT_abstract_origin refers to functions (and
19566 many things under the function DIE, but we do not recurse
19567 into function DIEs during partial symbol reading) and
19568 possibly variables as well; DW_AT_specification refers to
19569 declarations. Declarations ought to have the DW_AT_declaration
19570 flag. It happens that GCC forgets to put it in sometimes, but
19571 only for functions, not for types.
19572
19573 Adding more things than necessary to the hash table is harmless
19574 except for the performance cost. Adding too few will result in
5afb4e99
DJ
19575 wasted time in find_partial_die, when we reread the compilation
19576 unit with load_all_dies set. */
72bf9492 19577
5afb4e99 19578 if (load_all
72929c62 19579 || abbrev->tag == DW_TAG_constant
5afb4e99 19580 || abbrev->tag == DW_TAG_subprogram
72bf9492
DJ
19581 || abbrev->tag == DW_TAG_variable
19582 || abbrev->tag == DW_TAG_namespace
19583 || part_die->is_declaration)
19584 {
19585 void **slot;
19586
19587 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9c541725
PA
19588 to_underlying (part_die->sect_off),
19589 INSERT);
72bf9492
DJ
19590 *slot = part_die;
19591 }
19592
72bf9492 19593 /* For some DIEs we want to follow their children (if any). For C
bc30ff58 19594 we have no reason to follow the children of structures; for other
98bfdba5
PA
19595 languages we have to, so that we can get at method physnames
19596 to infer fully qualified class names, for DW_AT_specification,
19597 and for C++ template arguments. For C++, we also look one level
19598 inside functions to find template arguments (if the name of the
19599 function does not already contain the template arguments).
bc30ff58 19600
0a4b0913
AB
19601 For Ada and Fortran, we need to scan the children of subprograms
19602 and lexical blocks as well because these languages allow the
19603 definition of nested entities that could be interesting for the
19604 debugger, such as nested subprograms for instance. */
72bf9492 19605 if (last_die->has_children
5afb4e99
DJ
19606 && (load_all
19607 || last_die->tag == DW_TAG_namespace
f55ee35c 19608 || last_die->tag == DW_TAG_module
72bf9492 19609 || last_die->tag == DW_TAG_enumeration_type
98bfdba5
PA
19610 || (cu->language == language_cplus
19611 && last_die->tag == DW_TAG_subprogram
7d00ffec
TT
19612 && (last_die->raw_name == NULL
19613 || strchr (last_die->raw_name, '<') == NULL))
72bf9492
DJ
19614 || (cu->language != language_c
19615 && (last_die->tag == DW_TAG_class_type
680b30c7 19616 || last_die->tag == DW_TAG_interface_type
72bf9492 19617 || last_die->tag == DW_TAG_structure_type
bc30ff58 19618 || last_die->tag == DW_TAG_union_type))
0a4b0913
AB
19619 || ((cu->language == language_ada
19620 || cu->language == language_fortran)
bc30ff58
JB
19621 && (last_die->tag == DW_TAG_subprogram
19622 || last_die->tag == DW_TAG_lexical_block))))
72bf9492
DJ
19623 {
19624 nesting_level++;
19625 parent_die = last_die;
19626 continue;
19627 }
19628
19629 /* Otherwise we skip to the next sibling, if any. */
dee91e82 19630 info_ptr = locate_pdi_sibling (reader, last_die, info_ptr);
72bf9492
DJ
19631
19632 /* Back to the top, do it again. */
19633 }
19634}
19635
6f06d47b
YQ
19636partial_die_info::partial_die_info (sect_offset sect_off_,
19637 struct abbrev_info *abbrev)
19638 : partial_die_info (sect_off_, abbrev->tag, abbrev->has_children)
19639{
19640}
19641
7d00ffec
TT
19642/* See class definition. */
19643
19644const char *
19645partial_die_info::name (dwarf2_cu *cu)
19646{
19647 if (!canonical_name && raw_name != nullptr)
19648 {
19649 struct objfile *objfile = cu->per_objfile->objfile;
19650 raw_name = dwarf2_canonicalize_name (raw_name, cu, objfile);
19651 canonical_name = 1;
19652 }
19653
19654 return raw_name;
19655}
19656
35cc7ed7
YQ
19657/* Read a minimal amount of information into the minimal die structure.
19658 INFO_PTR should point just after the initial uleb128 of a DIE. */
c906108c 19659
48fbe735
YQ
19660const gdb_byte *
19661partial_die_info::read (const struct die_reader_specs *reader,
19662 const struct abbrev_info &abbrev, const gdb_byte *info_ptr)
c906108c 19663{
dee91e82 19664 struct dwarf2_cu *cu = reader->cu;
976ca316 19665 dwarf2_per_objfile *per_objfile = cu->per_objfile;
fa238c03 19666 unsigned int i;
c5aa993b 19667 int has_low_pc_attr = 0;
c906108c 19668 int has_high_pc_attr = 0;
91da1414 19669 int high_pc_relative = 0;
c906108c 19670
fd0a254f 19671 for (i = 0; i < abbrev.num_attrs; ++i)
c906108c 19672 {
e7da7f8f 19673 attribute attr;
7a5f294d 19674 info_ptr = read_attribute (reader, &attr, &abbrev.attrs[i], info_ptr);
18a8505e 19675 /* String and address offsets that need to do the reprocessing have
dda83cd7 19676 already been read at this point, so there is no need to wait until
18a8505e 19677 the loop terminates to do the reprocessing. */
7a5f294d 19678 if (attr.requires_reprocessing_p ())
d0ce17d8 19679 read_attribute_reprocess (reader, &attr, tag);
c906108c 19680 /* Store the data if it is of an attribute we want to keep in a
dda83cd7 19681 partial symbol table. */
c906108c
SS
19682 switch (attr.name)
19683 {
19684 case DW_AT_name:
48fbe735 19685 switch (tag)
71c25dea
TT
19686 {
19687 case DW_TAG_compile_unit:
95554aad 19688 case DW_TAG_partial_unit:
348e048f 19689 case DW_TAG_type_unit:
71c25dea
TT
19690 /* Compilation units have a DW_AT_name that is a filename, not
19691 a source language identifier. */
19692 case DW_TAG_enumeration_type:
19693 case DW_TAG_enumerator:
19694 /* These tags always have simple identifiers already; no need
19695 to canonicalize them. */
7d00ffec 19696 canonical_name = 1;
2c830f54 19697 raw_name = attr.as_string ();
71c25dea
TT
19698 break;
19699 default:
7d00ffec 19700 canonical_name = 0;
2c830f54 19701 raw_name = attr.as_string ();
71c25dea
TT
19702 break;
19703 }
c906108c 19704 break;
31ef98ae 19705 case DW_AT_linkage_name:
c906108c 19706 case DW_AT_MIPS_linkage_name:
31ef98ae
TT
19707 /* Note that both forms of linkage name might appear. We
19708 assume they will be the same, and we only store the last
19709 one we see. */
95f982e5 19710 linkage_name = attr.as_string ();
c906108c
SS
19711 break;
19712 case DW_AT_low_pc:
19713 has_low_pc_attr = 1;
95f982e5 19714 lowpc = attr.as_address ();
c906108c
SS
19715 break;
19716 case DW_AT_high_pc:
19717 has_high_pc_attr = 1;
95f982e5 19718 highpc = attr.as_address ();
cd6c91b4 19719 if (cu->header.version >= 4 && attr.form_is_constant ())
31aa7e4e 19720 high_pc_relative = 1;
c906108c
SS
19721 break;
19722 case DW_AT_location:
dda83cd7
SM
19723 /* Support the .debug_loc offsets. */
19724 if (attr.form_is_block ())
19725 {
9d2246fc 19726 d.locdesc = attr.as_block ();
dda83cd7
SM
19727 }
19728 else if (attr.form_is_section_offset ())
19729 {
4d3c2250 19730 dwarf2_complex_location_expr_complaint ();
dda83cd7
SM
19731 }
19732 else
19733 {
4d3c2250
KB
19734 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
19735 "partial symbol information");
dda83cd7 19736 }
c906108c 19737 break;
c906108c 19738 case DW_AT_external:
c45bc3f8 19739 is_external = attr.as_boolean ();
c906108c
SS
19740 break;
19741 case DW_AT_declaration:
c45bc3f8 19742 is_declaration = attr.as_boolean ();
c906108c
SS
19743 break;
19744 case DW_AT_type:
48fbe735 19745 has_type = 1;
c906108c
SS
19746 break;
19747 case DW_AT_abstract_origin:
19748 case DW_AT_specification:
72bf9492 19749 case DW_AT_extension:
48fbe735 19750 has_specification = 1;
0826b30a 19751 spec_offset = attr.get_ref_die_offset ();
48fbe735 19752 spec_is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728 19753 || cu->per_cu->is_dwz);
c906108c
SS
19754 break;
19755 case DW_AT_sibling:
19756 /* Ignore absolute siblings, they might point outside of
19757 the current compile unit. */
19758 if (attr.form == DW_FORM_ref_addr)
b98664d3 19759 complaint (_("ignoring absolute DW_AT_sibling"));
c906108c 19760 else
b9502d3f 19761 {
48fbe735 19762 const gdb_byte *buffer = reader->buffer;
0826b30a 19763 sect_offset off = attr.get_ref_die_offset ();
9c541725 19764 const gdb_byte *sibling_ptr = buffer + to_underlying (off);
b9502d3f
WN
19765
19766 if (sibling_ptr < info_ptr)
b98664d3 19767 complaint (_("DW_AT_sibling points backwards"));
22869d73 19768 else if (sibling_ptr > reader->buffer_end)
a0194fa8 19769 reader->die_section->overflow_complaint ();
b9502d3f 19770 else
48fbe735 19771 sibling = sibling_ptr;
b9502d3f 19772 }
c906108c 19773 break;
dda83cd7
SM
19774 case DW_AT_byte_size:
19775 has_byte_size = 1;
19776 break;
19777 case DW_AT_const_value:
19778 has_const_value = 1;
19779 break;
68511cec
CES
19780 case DW_AT_calling_convention:
19781 /* DWARF doesn't provide a way to identify a program's source-level
19782 entry point. DW_AT_calling_convention attributes are only meant
19783 to describe functions' calling conventions.
19784
19785 However, because it's a necessary piece of information in
0c1b455e
TT
19786 Fortran, and before DWARF 4 DW_CC_program was the only
19787 piece of debugging information whose definition refers to
19788 a 'main program' at all, several compilers marked Fortran
19789 main programs with DW_CC_program --- even when those
19790 functions use the standard calling conventions.
19791
19792 Although DWARF now specifies a way to provide this
19793 information, we support this practice for backward
19794 compatibility. */
529908cb 19795 if (attr.constant_value (0) == DW_CC_program
0c1b455e 19796 && cu->language == language_fortran)
48fbe735 19797 main_subprogram = 1;
68511cec 19798 break;
481860b3 19799 case DW_AT_inline:
529908cb
TT
19800 {
19801 LONGEST value = attr.constant_value (-1);
19802 if (value == DW_INL_inlined
19803 || value == DW_INL_declared_inlined)
19804 may_be_inlined = 1;
19805 }
481860b3 19806 break;
95554aad
TT
19807
19808 case DW_AT_import:
48fbe735 19809 if (tag == DW_TAG_imported_unit)
36586728 19810 {
0826b30a 19811 d.sect_off = attr.get_ref_die_offset ();
48fbe735 19812 is_dwz = (attr.form == DW_FORM_GNU_ref_alt
36586728
TT
19813 || cu->per_cu->is_dwz);
19814 }
95554aad
TT
19815 break;
19816
0c1b455e 19817 case DW_AT_main_subprogram:
c45bc3f8 19818 main_subprogram = attr.as_boolean ();
0c1b455e
TT
19819 break;
19820
05caa1d2
TT
19821 case DW_AT_ranges:
19822 {
d0ce17d8
CT
19823 /* DW_AT_rnglists_base does not apply to DIEs from the DWO
19824 skeleton. We take advantage of the fact the DW_AT_ranges
19825 does not appear in DW_TAG_compile_unit of DWO files.
19826
19827 Attributes of the form DW_FORM_rnglistx have already had
dda83cd7 19828 their value changed by read_rnglist_index and already
d0ce17d8
CT
19829 include DW_AT_rnglists_base, so don't need to add the ranges
19830 base, either. */
19831 int need_ranges_base = (tag != DW_TAG_compile_unit
19832 && attr.form != DW_FORM_rnglistx);
529908cb
TT
19833 /* It would be nice to reuse dwarf2_get_pc_bounds here,
19834 but that requires a full DIE, so instead we just
19835 reimplement it. */
9f6c202e 19836 unsigned int ranges_offset = (attr.as_unsigned ()
05caa1d2
TT
19837 + (need_ranges_base
19838 ? cu->ranges_base
19839 : 0));
19840
19841 /* Value of the DW_AT_ranges attribute is the offset in the
19842 .debug_ranges section. */
19843 if (dwarf2_ranges_read (ranges_offset, &lowpc, &highpc, cu,
d0ce17d8 19844 nullptr, tag))
05caa1d2
TT
19845 has_pc_info = 1;
19846 }
19847 break;
19848
c906108c
SS
19849 default:
19850 break;
19851 }
19852 }
19853
10d06d82
TT
19854 /* For Ada, if both the name and the linkage name appear, we prefer
19855 the latter. This lets "catch exception" work better, regardless
19856 of the order in which the name and linkage name were emitted.
19857 Really, though, this is just a workaround for the fact that gdb
19858 doesn't store both the name and the linkage name. */
19859 if (cu->language == language_ada && linkage_name != nullptr)
7d00ffec 19860 raw_name = linkage_name;
10d06d82 19861
91da1414 19862 if (high_pc_relative)
48fbe735 19863 highpc += lowpc;
91da1414 19864
9373cf26
JK
19865 if (has_low_pc_attr && has_high_pc_attr)
19866 {
19867 /* When using the GNU linker, .gnu.linkonce. sections are used to
19868 eliminate duplicate copies of functions and vtables and such.
19869 The linker will arbitrarily choose one and discard the others.
19870 The AT_*_pc values for such functions refer to local labels in
19871 these sections. If the section from that file was discarded, the
19872 labels are not in the output, so the relocs get a value of 0.
19873 If this is a discarded function, mark the pc bounds as invalid,
19874 so that GDB will ignore it. */
976ca316 19875 if (lowpc == 0 && !per_objfile->per_bfd->has_section_at_zero)
9373cf26 19876 {
976ca316 19877 struct objfile *objfile = per_objfile->objfile;
08feed99 19878 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19879
b98664d3 19880 complaint (_("DW_AT_low_pc %s is zero "
9d8780f0 19881 "for DIE at %s [in module %s]"),
48fbe735
YQ
19882 paddress (gdbarch, lowpc),
19883 sect_offset_str (sect_off),
9d8780f0 19884 objfile_name (objfile));
9373cf26
JK
19885 }
19886 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
48fbe735 19887 else if (lowpc >= highpc)
9373cf26 19888 {
976ca316 19889 struct objfile *objfile = per_objfile->objfile;
08feed99 19890 struct gdbarch *gdbarch = objfile->arch ();
9373cf26 19891
b98664d3 19892 complaint (_("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9d8780f0 19893 "for DIE at %s [in module %s]"),
48fbe735
YQ
19894 paddress (gdbarch, lowpc),
19895 paddress (gdbarch, highpc),
19896 sect_offset_str (sect_off),
9c541725 19897 objfile_name (objfile));
9373cf26
JK
19898 }
19899 else
48fbe735 19900 has_pc_info = 1;
9373cf26 19901 }
85cbf3d3 19902
c906108c
SS
19903 return info_ptr;
19904}
19905
72bf9492
DJ
19906/* Find a cached partial DIE at OFFSET in CU. */
19907
d590ff25
YQ
19908struct partial_die_info *
19909dwarf2_cu::find_partial_die (sect_offset sect_off)
72bf9492
DJ
19910{
19911 struct partial_die_info *lookup_die = NULL;
6f06d47b 19912 struct partial_die_info part_die (sect_off);
72bf9492 19913
9a3c8263 19914 lookup_die = ((struct partial_die_info *)
d590ff25 19915 htab_find_with_hash (partial_dies, &part_die,
9c541725 19916 to_underlying (sect_off)));
72bf9492 19917
72bf9492
DJ
19918 return lookup_die;
19919}
19920
348e048f
DE
19921/* Find a partial DIE at OFFSET, which may or may not be in CU,
19922 except in the case of .debug_types DIEs which do not reference
19923 outside their CU (they do however referencing other types via
55f1336d 19924 DW_FORM_ref_sig8). */
72bf9492 19925
122cf0f2 19926static const struct cu_partial_die_info
9c541725 19927find_partial_die (sect_offset sect_off, int offset_in_dwz, struct dwarf2_cu *cu)
72bf9492 19928{
976ca316
SM
19929 dwarf2_per_objfile *per_objfile = cu->per_objfile;
19930 struct objfile *objfile = per_objfile->objfile;
5afb4e99 19931 struct partial_die_info *pd = NULL;
72bf9492 19932
36586728 19933 if (offset_in_dwz == cu->per_cu->is_dwz
4057dfde 19934 && cu->header.offset_in_cu_p (sect_off))
5afb4e99 19935 {
d590ff25 19936 pd = cu->find_partial_die (sect_off);
5afb4e99 19937 if (pd != NULL)
fb816e8b 19938 return { cu, pd };
0d99eb77
DE
19939 /* We missed recording what we needed.
19940 Load all dies and try again. */
5afb4e99 19941 }
0d99eb77
DE
19942 else
19943 {
19944 /* TUs don't reference other CUs/TUs (except via type signatures). */
3019eac3 19945 if (cu->per_cu->is_debug_types)
0d99eb77 19946 {
9d8780f0
SM
19947 error (_("Dwarf Error: Type Unit at offset %s contains"
19948 " external reference to offset %s [in module %s].\n"),
19949 sect_offset_str (cu->header.sect_off), sect_offset_str (sect_off),
0d99eb77
DE
19950 bfd_get_filename (objfile->obfd));
19951 }
7188ed02
SM
19952 dwarf2_per_cu_data *per_cu
19953 = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 19954 per_objfile);
72bf9492 19955
976ca316 19956 cu = per_objfile->get_cu (per_cu);
7188ed02 19957 if (cu == NULL || cu->partial_dies == NULL)
976ca316 19958 load_partial_comp_unit (per_cu, per_objfile, nullptr);
ae038cb0 19959
976ca316 19960 cu = per_objfile->get_cu (per_cu);
7188ed02
SM
19961
19962 cu->last_used = 0;
19963 pd = cu->find_partial_die (sect_off);
0d99eb77 19964 }
5afb4e99 19965
dee91e82
DE
19966 /* If we didn't find it, and not all dies have been loaded,
19967 load them all and try again. */
19968
7188ed02 19969 if (pd == NULL && cu->per_cu->load_all_dies == 0)
5afb4e99 19970 {
7188ed02 19971 cu->per_cu->load_all_dies = 1;
fd820528
DE
19972
19973 /* This is nasty. When we reread the DIEs, somewhere up the call chain
19974 THIS_CU->cu may already be in use. So we can't just free it and
19975 replace its DIEs with the ones we read in. Instead, we leave those
19976 DIEs alone (which can still be in use, e.g. in scan_partial_symbols),
19977 and clobber THIS_CU->cu->partial_dies with the hash table for the new
19978 set. */
976ca316 19979 load_partial_comp_unit (cu->per_cu, per_objfile, cu);
5afb4e99 19980
7188ed02 19981 pd = cu->find_partial_die (sect_off);
5afb4e99
DJ
19982 }
19983
19984 if (pd == NULL)
521894aa 19985 error (_("Dwarf Error: Cannot not find DIE at %s [from module %s]\n"),
9d8780f0 19986 sect_offset_str (sect_off), bfd_get_filename (objfile->obfd));
7188ed02 19987 return { cu, pd };
72bf9492
DJ
19988}
19989
abc72ce4
DE
19990/* See if we can figure out if the class lives in a namespace. We do
19991 this by looking for a member function; its demangled name will
19992 contain namespace info, if there is any. */
19993
19994static void
19995guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
19996 struct dwarf2_cu *cu)
19997{
19998 /* NOTE: carlton/2003-10-07: Getting the info this way changes
19999 what template types look like, because the demangler
20000 frequently doesn't give the same name as the debug info. We
20001 could fix this by only using the demangled name to get the
20002 prefix (but see comment in read_structure_type). */
20003
20004 struct partial_die_info *real_pdi;
20005 struct partial_die_info *child_pdi;
20006
20007 /* If this DIE (this DIE's specification, if any) has a parent, then
20008 we should not do this. We'll prepend the parent's fully qualified
20009 name when we create the partial symbol. */
20010
20011 real_pdi = struct_pdi;
20012 while (real_pdi->has_specification)
fb816e8b 20013 {
122cf0f2
AB
20014 auto res = find_partial_die (real_pdi->spec_offset,
20015 real_pdi->spec_is_dwz, cu);
fb816e8b
TV
20016 real_pdi = res.pdi;
20017 cu = res.cu;
20018 }
abc72ce4
DE
20019
20020 if (real_pdi->die_parent != NULL)
20021 return;
20022
20023 for (child_pdi = struct_pdi->die_child;
20024 child_pdi != NULL;
20025 child_pdi = child_pdi->die_sibling)
20026 {
20027 if (child_pdi->tag == DW_TAG_subprogram
20028 && child_pdi->linkage_name != NULL)
20029 {
43816ebc 20030 gdb::unique_xmalloc_ptr<char> actual_class_name
eff93b4d
AB
20031 (cu->language_defn->class_name_from_physname
20032 (child_pdi->linkage_name));
abc72ce4
DE
20033 if (actual_class_name != NULL)
20034 {
5e22e966 20035 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20036 struct_pdi->raw_name = objfile->intern (actual_class_name.get ());
20037 struct_pdi->canonical_name = 1;
abc72ce4
DE
20038 }
20039 break;
20040 }
20041 }
20042}
20043
25c11aca
TV
20044/* Return true if a DIE with TAG may have the DW_AT_const_value
20045 attribute. */
20046
20047static bool
20048can_have_DW_AT_const_value_p (enum dwarf_tag tag)
20049{
20050 switch (tag)
20051 {
20052 case DW_TAG_constant:
20053 case DW_TAG_enumerator:
20054 case DW_TAG_formal_parameter:
20055 case DW_TAG_template_value_param:
20056 case DW_TAG_variable:
20057 return true;
20058 }
20059
20060 return false;
20061}
20062
52356b79
YQ
20063void
20064partial_die_info::fixup (struct dwarf2_cu *cu)
72bf9492 20065{
abc72ce4
DE
20066 /* Once we've fixed up a die, there's no point in doing so again.
20067 This also avoids a memory leak if we were to call
20068 guess_partial_die_structure_name multiple times. */
52356b79 20069 if (fixup_called)
abc72ce4
DE
20070 return;
20071
72bf9492
DJ
20072 /* If we found a reference attribute and the DIE has no name, try
20073 to find a name in the referred to DIE. */
20074
7d00ffec 20075 if (raw_name == NULL && has_specification)
72bf9492
DJ
20076 {
20077 struct partial_die_info *spec_die;
72bf9492 20078
122cf0f2 20079 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
fb816e8b
TV
20080 spec_die = res.pdi;
20081 cu = res.cu;
72bf9492 20082
52356b79 20083 spec_die->fixup (cu);
72bf9492 20084
7d00ffec 20085 if (spec_die->raw_name)
72bf9492 20086 {
7d00ffec
TT
20087 raw_name = spec_die->raw_name;
20088 canonical_name = spec_die->canonical_name;
72bf9492
DJ
20089
20090 /* Copy DW_AT_external attribute if it is set. */
20091 if (spec_die->is_external)
52356b79 20092 is_external = spec_die->is_external;
72bf9492
DJ
20093 }
20094 }
20095
25c11aca
TV
20096 if (!has_const_value && has_specification
20097 && can_have_DW_AT_const_value_p (tag))
20098 {
20099 struct partial_die_info *spec_die;
20100
20101 auto res = find_partial_die (spec_offset, spec_is_dwz, cu);
20102 spec_die = res.pdi;
20103 cu = res.cu;
20104
20105 spec_die->fixup (cu);
20106
20107 if (spec_die->has_const_value)
20108 {
20109 /* Copy DW_AT_const_value attribute if it is set. */
20110 has_const_value = spec_die->has_const_value;
20111 }
20112 }
20113
72bf9492 20114 /* Set default names for some unnamed DIEs. */
72bf9492 20115
7d00ffec
TT
20116 if (raw_name == NULL && tag == DW_TAG_namespace)
20117 {
20118 raw_name = CP_ANONYMOUS_NAMESPACE_STR;
20119 canonical_name = 1;
20120 }
72bf9492 20121
abc72ce4
DE
20122 /* If there is no parent die to provide a namespace, and there are
20123 children, see if we can determine the namespace from their linkage
122d1940 20124 name. */
abc72ce4 20125 if (cu->language == language_cplus
5e22e966 20126 && !cu->per_objfile->per_bfd->types.empty ()
52356b79
YQ
20127 && die_parent == NULL
20128 && has_children
20129 && (tag == DW_TAG_class_type
20130 || tag == DW_TAG_structure_type
20131 || tag == DW_TAG_union_type))
20132 guess_partial_die_structure_name (this, cu);
abc72ce4 20133
53832f31
TT
20134 /* GCC might emit a nameless struct or union that has a linkage
20135 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
7d00ffec 20136 if (raw_name == NULL
52356b79
YQ
20137 && (tag == DW_TAG_class_type
20138 || tag == DW_TAG_interface_type
20139 || tag == DW_TAG_structure_type
20140 || tag == DW_TAG_union_type)
20141 && linkage_name != NULL)
53832f31 20142 {
43816ebc
TT
20143 gdb::unique_xmalloc_ptr<char> demangled
20144 (gdb_demangle (linkage_name, DMGL_TYPES));
20145 if (demangled != nullptr)
53832f31 20146 {
96408a79
SA
20147 const char *base;
20148
20149 /* Strip any leading namespaces/classes, keep only the base name.
20150 DW_AT_name for named DIEs does not contain the prefixes. */
43816ebc
TT
20151 base = strrchr (demangled.get (), ':');
20152 if (base && base > demangled.get () && base[-1] == ':')
96408a79
SA
20153 base++;
20154 else
43816ebc 20155 base = demangled.get ();
96408a79 20156
5e22e966 20157 struct objfile *objfile = cu->per_objfile->objfile;
7d00ffec
TT
20158 raw_name = objfile->intern (base);
20159 canonical_name = 1;
53832f31
TT
20160 }
20161 }
20162
52356b79 20163 fixup_called = 1;
72bf9492
DJ
20164}
20165
d0ce17d8 20166/* Read the .debug_loclists or .debug_rnglists header (they are the same format)
a1c40103
SM
20167 contents from the given SECTION in the HEADER.
20168
20169 HEADER_OFFSET is the offset of the header in the section. */
41144253 20170static void
d0ce17d8 20171read_loclists_rnglists_header (struct loclists_rnglists_header *header,
a1c40103
SM
20172 struct dwarf2_section_info *section,
20173 sect_offset header_offset)
41144253 20174{
20175 unsigned int bytes_read;
20176 bfd *abfd = section->get_bfd_owner ();
a1c40103
SM
20177 const gdb_byte *info_ptr = section->buffer + to_underlying (header_offset);
20178
41144253 20179 header->length = read_initial_length (abfd, info_ptr, &bytes_read);
20180 info_ptr += bytes_read;
a1c40103 20181
41144253 20182 header->version = read_2_bytes (abfd, info_ptr);
20183 info_ptr += 2;
a1c40103 20184
41144253 20185 header->addr_size = read_1_byte (abfd, info_ptr);
20186 info_ptr += 1;
a1c40103 20187
41144253 20188 header->segment_collector_size = read_1_byte (abfd, info_ptr);
20189 info_ptr += 1;
a1c40103 20190
41144253 20191 header->offset_entry_count = read_4_bytes (abfd, info_ptr);
20192}
20193
20194/* Return the DW_AT_loclists_base value for the CU. */
20195static ULONGEST
20196lookup_loclist_base (struct dwarf2_cu *cu)
20197{
20198 /* For the .dwo unit, the loclist_base points to the first offset following
20199 the header. The header consists of the following entities-
20200 1. Unit Length (4 bytes for 32 bit DWARF format, and 12 bytes for the 64
20201 bit format)
20202 2. version (2 bytes)
20203 3. address size (1 byte)
20204 4. segment selector size (1 byte)
20205 5. offset entry count (4 bytes)
20206 These sizes are derived as per the DWARFv5 standard. */
20207 if (cu->dwo_unit != nullptr)
20208 {
20209 if (cu->header.initial_length_size == 4)
20210 return LOCLIST_HEADER_SIZE32;
20211 return LOCLIST_HEADER_SIZE64;
20212 }
20213 return cu->loclist_base;
20214}
20215
20216/* Given a DW_FORM_loclistx value LOCLIST_INDEX, fetch the offset from the
20217 array of offsets in the .debug_loclists section. */
20218static CORE_ADDR
20219read_loclist_index (struct dwarf2_cu *cu, ULONGEST loclist_index)
20220{
976ca316
SM
20221 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20222 struct objfile *objfile = per_objfile->objfile;
41144253 20223 bfd *abfd = objfile->obfd;
a1c40103
SM
20224 ULONGEST loclist_header_size =
20225 (cu->header.initial_length_size == 4 ? LOCLIST_HEADER_SIZE32
20226 : LOCLIST_HEADER_SIZE64);
41144253 20227 ULONGEST loclist_base = lookup_loclist_base (cu);
05787bad
SM
20228
20229 /* Offset in .debug_loclists of the offset for LOCLIST_INDEX. */
20230 ULONGEST start_offset =
20231 loclist_base + loclist_index * cu->header.offset_size;
20232
a1c40103 20233 /* Get loclists section. */
41144253 20234 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
20235
a1c40103 20236 /* Read the loclists section content. */
41144253 20237 section->read (objfile);
20238 if (section->buffer == NULL)
a0c1eeba
SM
20239 error (_("DW_FORM_loclistx used without .debug_loclists "
20240 "section [in module %s]"), objfile_name (objfile));
20241
a1c40103
SM
20242 /* DW_AT_loclists_base points after the .debug_loclists contribution header,
20243 so if loclist_base is smaller than the header size, we have a problem. */
20244 if (loclist_base < loclist_header_size)
20245 error (_("DW_AT_loclists_base is smaller than header size [in module %s]"),
20246 objfile_name (objfile));
20247
20248 /* Read the header of the loclists contribution. */
d0ce17d8 20249 struct loclists_rnglists_header header;
a1c40103
SM
20250 read_loclists_rnglists_header (&header, section,
20251 (sect_offset) (loclist_base - loclist_header_size));
20252
20253 /* Verify the loclist index is valid. */
41144253 20254 if (loclist_index >= header.offset_entry_count)
a0c1eeba
SM
20255 error (_("DW_FORM_loclistx pointing outside of "
20256 ".debug_loclists offset array [in module %s]"),
20257 objfile_name (objfile));
20258
05787bad
SM
20259 /* Validate that reading won't go beyond the end of the section. */
20260 if (start_offset + cu->header.offset_size > section->size)
20261 error (_("Reading DW_FORM_loclistx index beyond end of"
20262 ".debug_loclists section [in module %s]"),
20263 objfile_name (objfile));
20264
20265 const gdb_byte *info_ptr = section->buffer + start_offset;
41144253 20266
20267 if (cu->header.offset_size == 4)
20268 return bfd_get_32 (abfd, info_ptr) + loclist_base;
20269 else
20270 return bfd_get_64 (abfd, info_ptr) + loclist_base;
20271}
20272
d0ce17d8
CT
20273/* Given a DW_FORM_rnglistx value RNGLIST_INDEX, fetch the offset from the
20274 array of offsets in the .debug_rnglists section. */
20275static CORE_ADDR
20276read_rnglist_index (struct dwarf2_cu *cu, ULONGEST rnglist_index,
20277 dwarf_tag tag)
20278{
20279 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
20280 struct objfile *objfile = dwarf2_per_objfile->objfile;
20281 bfd *abfd = objfile->obfd;
20282 ULONGEST rnglist_header_size =
20283 (cu->header.initial_length_size == 4 ? RNGLIST_HEADER_SIZE32
20284 : RNGLIST_HEADER_SIZE64);
20285 ULONGEST rnglist_base =
20286 (cu->dwo_unit != nullptr) ? rnglist_header_size : cu->ranges_base;
5e4d9bbc
SM
20287
20288 /* Offset in .debug_rnglists of the offset for RNGLIST_INDEX. */
d0ce17d8
CT
20289 ULONGEST start_offset =
20290 rnglist_base + rnglist_index * cu->header.offset_size;
20291
20292 /* Get rnglists section. */
20293 struct dwarf2_section_info *section = cu_debug_rnglists_section (cu, tag);
20294
20295 /* Read the rnglists section content. */
20296 section->read (objfile);
20297 if (section->buffer == nullptr)
20298 error (_("DW_FORM_rnglistx used without .debug_rnglists section "
20299 "[in module %s]"),
20300 objfile_name (objfile));
20301
a1c40103
SM
20302 /* DW_AT_rnglists_base points after the .debug_rnglists contribution header,
20303 so if rnglist_base is smaller than the header size, we have a problem. */
20304 if (rnglist_base < rnglist_header_size)
20305 error (_("DW_AT_rnglists_base is smaller than header size [in module %s]"),
20306 objfile_name (objfile));
20307
20308 /* Read the header of the rnglists contribution. */
d0ce17d8 20309 struct loclists_rnglists_header header;
a1c40103
SM
20310 read_loclists_rnglists_header (&header, section,
20311 (sect_offset) (rnglist_base - rnglist_header_size));
20312
20313 /* Verify the rnglist index is valid. */
d0ce17d8
CT
20314 if (rnglist_index >= header.offset_entry_count)
20315 error (_("DW_FORM_rnglistx index pointing outside of "
20316 ".debug_rnglists offset array [in module %s]"),
20317 objfile_name (objfile));
20318
d0ce17d8 20319 /* Validate that reading won't go beyond the end of the section. */
5e4d9bbc 20320 if (start_offset + cu->header.offset_size > section->size)
d0ce17d8
CT
20321 error (_("Reading DW_FORM_rnglistx index beyond end of"
20322 ".debug_rnglists section [in module %s]"),
20323 objfile_name (objfile));
20324
20325 const gdb_byte *info_ptr = section->buffer + start_offset;
20326
20327 if (cu->header.offset_size == 4)
20328 return read_4_bytes (abfd, info_ptr) + rnglist_base;
20329 else
20330 return read_8_bytes (abfd, info_ptr) + rnglist_base;
20331}
20332
18a8505e
AT
20333/* Process the attributes that had to be skipped in the first round. These
20334 attributes are the ones that need str_offsets_base or addr_base attributes.
20335 They could not have been processed in the first round, because at the time
20336 the values of str_offsets_base or addr_base may not have been known. */
f1749218
TT
20337static void
20338read_attribute_reprocess (const struct die_reader_specs *reader,
d0ce17d8 20339 struct attribute *attr, dwarf_tag tag)
18a8505e
AT
20340{
20341 struct dwarf2_cu *cu = reader->cu;
20342 switch (attr->form)
20343 {
20344 case DW_FORM_addrx:
20345 case DW_FORM_GNU_addr_index:
36d378cf
TT
20346 attr->set_address (read_addr_index (cu,
20347 attr->as_unsigned_reprocess ()));
dda83cd7 20348 break;
41144253 20349 case DW_FORM_loclistx:
b1829e1b
SM
20350 {
20351 CORE_ADDR loclists_sect_off
20352 = read_loclist_index (cu, attr->as_unsigned_reprocess ());
20353
20354 attr->set_unsigned (loclists_sect_off);
20355 }
20356 break;
d0ce17d8 20357 case DW_FORM_rnglistx:
b1829e1b
SM
20358 {
20359 CORE_ADDR rnglists_sect_off
20360 = read_rnglist_index (cu, attr->as_unsigned_reprocess (), tag);
20361
20362 attr->set_unsigned (rnglists_sect_off);
20363 }
dda83cd7 20364 break;
18a8505e
AT
20365 case DW_FORM_strx:
20366 case DW_FORM_strx1:
20367 case DW_FORM_strx2:
20368 case DW_FORM_strx3:
20369 case DW_FORM_strx4:
20370 case DW_FORM_GNU_str_index:
20371 {
fe56917a 20372 unsigned int str_index = attr->as_unsigned_reprocess ();
c6481205 20373 gdb_assert (!attr->canonical_string_p ());
18a8505e 20374 if (reader->dwo_file != NULL)
c6481205
TT
20375 attr->set_string_noncanonical (read_dwo_str_index (reader,
20376 str_index));
18a8505e 20377 else
c6481205
TT
20378 attr->set_string_noncanonical (read_stub_str_index (cu,
20379 str_index));
18a8505e
AT
20380 break;
20381 }
20382 default:
20383 gdb_assert_not_reached (_("Unexpected DWARF form."));
20384 }
20385}
20386
a8329558 20387/* Read an attribute value described by an attribute form. */
c906108c 20388
d521ce57 20389static const gdb_byte *
dee91e82
DE
20390read_attribute_value (const struct die_reader_specs *reader,
20391 struct attribute *attr, unsigned form,
7a5f294d 20392 LONGEST implicit_const, const gdb_byte *info_ptr)
c906108c 20393{
dee91e82 20394 struct dwarf2_cu *cu = reader->cu;
976ca316
SM
20395 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20396 struct objfile *objfile = per_objfile->objfile;
dee91e82 20397 bfd *abfd = reader->abfd;
e7c27a73 20398 struct comp_unit_head *cu_header = &cu->header;
c906108c
SS
20399 unsigned int bytes_read;
20400 struct dwarf_block *blk;
20401
aead7601 20402 attr->form = (enum dwarf_form) form;
a8329558 20403 switch (form)
c906108c 20404 {
c906108c 20405 case DW_FORM_ref_addr:
ae411497 20406 if (cu->header.version == 2)
414ad644
TT
20407 attr->set_unsigned (cu->header.read_address (abfd, info_ptr,
20408 &bytes_read));
ae411497 20409 else
414ad644
TT
20410 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20411 &bytes_read));
ae411497
TT
20412 info_ptr += bytes_read;
20413 break;
36586728 20414 case DW_FORM_GNU_ref_alt:
414ad644
TT
20415 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20416 &bytes_read));
36586728
TT
20417 info_ptr += bytes_read;
20418 break;
ae411497 20419 case DW_FORM_addr:
08feed99
TT
20420 {
20421 struct gdbarch *gdbarch = objfile->arch ();
36d378cf
TT
20422 CORE_ADDR addr = cu->header.read_address (abfd, info_ptr, &bytes_read);
20423 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr);
20424 attr->set_address (addr);
08feed99
TT
20425 info_ptr += bytes_read;
20426 }
c906108c
SS
20427 break;
20428 case DW_FORM_block2:
7b5a2f43 20429 blk = dwarf_alloc_block (cu);
c906108c
SS
20430 blk->size = read_2_bytes (abfd, info_ptr);
20431 info_ptr += 2;
20432 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20433 info_ptr += blk->size;
9d2246fc 20434 attr->set_block (blk);
c906108c
SS
20435 break;
20436 case DW_FORM_block4:
7b5a2f43 20437 blk = dwarf_alloc_block (cu);
c906108c
SS
20438 blk->size = read_4_bytes (abfd, info_ptr);
20439 info_ptr += 4;
20440 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20441 info_ptr += blk->size;
9d2246fc 20442 attr->set_block (blk);
c906108c
SS
20443 break;
20444 case DW_FORM_data2:
414ad644 20445 attr->set_unsigned (read_2_bytes (abfd, info_ptr));
c906108c
SS
20446 info_ptr += 2;
20447 break;
20448 case DW_FORM_data4:
414ad644 20449 attr->set_unsigned (read_4_bytes (abfd, info_ptr));
c906108c
SS
20450 info_ptr += 4;
20451 break;
20452 case DW_FORM_data8:
414ad644 20453 attr->set_unsigned (read_8_bytes (abfd, info_ptr));
c906108c
SS
20454 info_ptr += 8;
20455 break;
0224619f
JK
20456 case DW_FORM_data16:
20457 blk = dwarf_alloc_block (cu);
20458 blk->size = 16;
20459 blk->data = read_n_bytes (abfd, info_ptr, 16);
20460 info_ptr += 16;
9d2246fc 20461 attr->set_block (blk);
0224619f 20462 break;
2dc7f7b3 20463 case DW_FORM_sec_offset:
414ad644
TT
20464 attr->set_unsigned (cu->header.read_offset (abfd, info_ptr,
20465 &bytes_read));
2dc7f7b3
TT
20466 info_ptr += bytes_read;
20467 break;
41144253 20468 case DW_FORM_loclistx:
20469 {
7a5f294d
TT
20470 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20471 &bytes_read));
20472 info_ptr += bytes_read;
41144253 20473 }
20474 break;
c906108c 20475 case DW_FORM_string:
c6481205
TT
20476 attr->set_string_noncanonical (read_direct_string (abfd, info_ptr,
20477 &bytes_read));
c906108c
SS
20478 info_ptr += bytes_read;
20479 break;
4bdf3d34 20480 case DW_FORM_strp:
36586728
TT
20481 if (!cu->per_cu->is_dwz)
20482 {
c6481205
TT
20483 attr->set_string_noncanonical
20484 (read_indirect_string (per_objfile,
20485 abfd, info_ptr, cu_header,
20486 &bytes_read));
36586728
TT
20487 info_ptr += bytes_read;
20488 break;
20489 }
20490 /* FALLTHROUGH */
43988095
JK
20491 case DW_FORM_line_strp:
20492 if (!cu->per_cu->is_dwz)
20493 {
c6481205
TT
20494 attr->set_string_noncanonical
20495 (per_objfile->read_line_string (info_ptr, cu_header,
20496 &bytes_read));
43988095
JK
20497 info_ptr += bytes_read;
20498 break;
20499 }
20500 /* FALLTHROUGH */
36586728
TT
20501 case DW_FORM_GNU_strp_alt:
20502 {
976ca316 20503 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
8266302d
TT
20504 LONGEST str_offset = cu_header->read_offset (abfd, info_ptr,
20505 &bytes_read);
36586728 20506
c6481205
TT
20507 attr->set_string_noncanonical
20508 (dwz->read_string (objfile, str_offset));
36586728
TT
20509 info_ptr += bytes_read;
20510 }
4bdf3d34 20511 break;
2dc7f7b3 20512 case DW_FORM_exprloc:
c906108c 20513 case DW_FORM_block:
7b5a2f43 20514 blk = dwarf_alloc_block (cu);
c906108c
SS
20515 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20516 info_ptr += bytes_read;
20517 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20518 info_ptr += blk->size;
9d2246fc 20519 attr->set_block (blk);
c906108c
SS
20520 break;
20521 case DW_FORM_block1:
7b5a2f43 20522 blk = dwarf_alloc_block (cu);
c906108c
SS
20523 blk->size = read_1_byte (abfd, info_ptr);
20524 info_ptr += 1;
20525 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
20526 info_ptr += blk->size;
9d2246fc 20527 attr->set_block (blk);
c906108c
SS
20528 break;
20529 case DW_FORM_data1:
c906108c 20530 case DW_FORM_flag:
414ad644 20531 attr->set_unsigned (read_1_byte (abfd, info_ptr));
c906108c
SS
20532 info_ptr += 1;
20533 break;
2dc7f7b3 20534 case DW_FORM_flag_present:
414ad644 20535 attr->set_unsigned (1);
2dc7f7b3 20536 break;
c906108c 20537 case DW_FORM_sdata:
1bc397c5 20538 attr->set_signed (read_signed_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20539 info_ptr += bytes_read;
20540 break;
18a8505e 20541 case DW_FORM_rnglistx:
7a5f294d
TT
20542 {
20543 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20544 &bytes_read));
20545 info_ptr += bytes_read;
20546 }
20547 break;
d0ce17d8 20548 case DW_FORM_udata:
414ad644 20549 attr->set_unsigned (read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
c906108c
SS
20550 info_ptr += bytes_read;
20551 break;
20552 case DW_FORM_ref1:
414ad644
TT
20553 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20554 + read_1_byte (abfd, info_ptr)));
c906108c
SS
20555 info_ptr += 1;
20556 break;
20557 case DW_FORM_ref2:
414ad644
TT
20558 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20559 + read_2_bytes (abfd, info_ptr)));
c906108c
SS
20560 info_ptr += 2;
20561 break;
20562 case DW_FORM_ref4:
414ad644
TT
20563 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20564 + read_4_bytes (abfd, info_ptr)));
c906108c
SS
20565 info_ptr += 4;
20566 break;
613e1657 20567 case DW_FORM_ref8:
414ad644
TT
20568 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20569 + read_8_bytes (abfd, info_ptr)));
613e1657
KB
20570 info_ptr += 8;
20571 break;
55f1336d 20572 case DW_FORM_ref_sig8:
630ed6b9 20573 attr->set_signature (read_8_bytes (abfd, info_ptr));
348e048f
DE
20574 info_ptr += 8;
20575 break;
c906108c 20576 case DW_FORM_ref_udata:
414ad644
TT
20577 attr->set_unsigned ((to_underlying (cu->header.sect_off)
20578 + read_unsigned_leb128 (abfd, info_ptr,
20579 &bytes_read)));
c906108c
SS
20580 info_ptr += bytes_read;
20581 break;
c906108c 20582 case DW_FORM_indirect:
a8329558
KW
20583 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20584 info_ptr += bytes_read;
43988095
JK
20585 if (form == DW_FORM_implicit_const)
20586 {
20587 implicit_const = read_signed_leb128 (abfd, info_ptr, &bytes_read);
20588 info_ptr += bytes_read;
20589 }
20590 info_ptr = read_attribute_value (reader, attr, form, implicit_const,
7a5f294d 20591 info_ptr);
43988095
JK
20592 break;
20593 case DW_FORM_implicit_const:
1bc397c5 20594 attr->set_signed (implicit_const);
a8329558 20595 break;
336d760d 20596 case DW_FORM_addrx:
3019eac3 20597 case DW_FORM_GNU_addr_index:
fe56917a
TT
20598 attr->set_unsigned_reprocess (read_unsigned_leb128 (abfd, info_ptr,
20599 &bytes_read));
3019eac3
DE
20600 info_ptr += bytes_read;
20601 break;
cf532bd1 20602 case DW_FORM_strx:
15f18d14
AT
20603 case DW_FORM_strx1:
20604 case DW_FORM_strx2:
20605 case DW_FORM_strx3:
20606 case DW_FORM_strx4:
3019eac3 20607 case DW_FORM_GNU_str_index:
3019eac3 20608 {
15f18d14
AT
20609 ULONGEST str_index;
20610 if (form == DW_FORM_strx1)
20611 {
20612 str_index = read_1_byte (abfd, info_ptr);
20613 info_ptr += 1;
20614 }
20615 else if (form == DW_FORM_strx2)
20616 {
20617 str_index = read_2_bytes (abfd, info_ptr);
20618 info_ptr += 2;
20619 }
20620 else if (form == DW_FORM_strx3)
20621 {
20622 str_index = read_3_bytes (abfd, info_ptr);
20623 info_ptr += 3;
20624 }
20625 else if (form == DW_FORM_strx4)
20626 {
20627 str_index = read_4_bytes (abfd, info_ptr);
20628 info_ptr += 4;
20629 }
20630 else
20631 {
20632 str_index = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
20633 info_ptr += bytes_read;
20634 }
fe56917a 20635 attr->set_unsigned_reprocess (str_index);
7a5f294d 20636 }
3019eac3 20637 break;
c906108c 20638 default:
8a3fe4f8 20639 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
659b0389
ML
20640 dwarf_form_name (form),
20641 bfd_get_filename (abfd));
c906108c 20642 }
28e94949 20643
36586728 20644 /* Super hack. */
cd6c91b4 20645 if (cu->per_cu->is_dwz && attr->form_is_ref ())
36586728
TT
20646 attr->form = DW_FORM_GNU_ref_alt;
20647
28e94949
JB
20648 /* We have seen instances where the compiler tried to emit a byte
20649 size attribute of -1 which ended up being encoded as an unsigned
20650 0xffffffff. Although 0xffffffff is technically a valid size value,
20651 an object of this size seems pretty unlikely so we can relatively
20652 safely treat these cases as if the size attribute was invalid and
20653 treat them as zero by default. */
20654 if (attr->name == DW_AT_byte_size
20655 && form == DW_FORM_data4
529908cb 20656 && attr->as_unsigned () >= 0xffffffff)
01c66ae6
JB
20657 {
20658 complaint
dda83cd7
SM
20659 (_("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
20660 hex_string (attr->as_unsigned ()));
414ad644 20661 attr->set_unsigned (0);
01c66ae6 20662 }
28e94949 20663
c906108c
SS
20664 return info_ptr;
20665}
20666
a8329558
KW
20667/* Read an attribute described by an abbreviated attribute. */
20668
d521ce57 20669static const gdb_byte *
dee91e82
DE
20670read_attribute (const struct die_reader_specs *reader,
20671 struct attribute *attr, struct attr_abbrev *abbrev,
7a5f294d 20672 const gdb_byte *info_ptr)
a8329558
KW
20673{
20674 attr->name = abbrev->name;
c6481205 20675 attr->string_is_canonical = 0;
fe56917a 20676 attr->requires_reprocessing = 0;
43988095 20677 return read_attribute_value (reader, attr, abbrev->form,
7a5f294d 20678 abbrev->implicit_const, info_ptr);
a8329558
KW
20679}
20680
43988095
JK
20681/* Return pointer to string at .debug_str offset STR_OFFSET. */
20682
20683static const char *
976ca316 20684read_indirect_string_at_offset (dwarf2_per_objfile *per_objfile,
4f44ae6c 20685 LONGEST str_offset)
43988095 20686{
976ca316
SM
20687 return per_objfile->per_bfd->str.read_string (per_objfile->objfile,
20688 str_offset, "DW_FORM_strp");
c906108c
SS
20689}
20690
43988095
JK
20691/* Return pointer to string at .debug_str offset as read from BUF.
20692 BUF is assumed to be in a compilation unit described by CU_HEADER.
20693 Return *BYTES_READ_PTR count of bytes read from BUF. */
20694
d521ce57 20695static const char *
976ca316 20696read_indirect_string (dwarf2_per_objfile *per_objfile, bfd *abfd,
ed2dc618 20697 const gdb_byte *buf,
cf2c3c16
TT
20698 const struct comp_unit_head *cu_header,
20699 unsigned int *bytes_read_ptr)
20700{
8266302d 20701 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
cf2c3c16 20702
976ca316 20703 return read_indirect_string_at_offset (per_objfile, str_offset);
cf2c3c16
TT
20704}
20705
86c0bb4c 20706/* See read.h. */
43988095 20707
86c0bb4c
TT
20708const char *
20709dwarf2_per_objfile::read_line_string (const gdb_byte *buf,
5989a64e
SM
20710 const struct comp_unit_head *cu_header,
20711 unsigned int *bytes_read_ptr)
43988095 20712{
86c0bb4c 20713 bfd *abfd = objfile->obfd;
8266302d 20714 LONGEST str_offset = cu_header->read_offset (abfd, buf, bytes_read_ptr);
43988095 20715
5989a64e 20716 return per_bfd->line_str.read_string (objfile, str_offset, "DW_FORM_line_strp");
43988095
JK
20717}
20718
3019eac3 20719/* Given index ADDR_INDEX in .debug_addr, fetch the value.
18a8505e 20720 ADDR_BASE is the DW_AT_addr_base (DW_AT_GNU_addr_base) attribute or zero.
3019eac3
DE
20721 ADDR_SIZE is the size of addresses from the CU header. */
20722
20723static CORE_ADDR
976ca316
SM
20724read_addr_index_1 (dwarf2_per_objfile *per_objfile, unsigned int addr_index,
20725 gdb::optional<ULONGEST> addr_base, int addr_size)
3019eac3 20726{
976ca316 20727 struct objfile *objfile = per_objfile->objfile;
3019eac3
DE
20728 bfd *abfd = objfile->obfd;
20729 const gdb_byte *info_ptr;
18a8505e 20730 ULONGEST addr_base_or_zero = addr_base.has_value () ? *addr_base : 0;
3019eac3 20731
976ca316
SM
20732 per_objfile->per_bfd->addr.read (objfile);
20733 if (per_objfile->per_bfd->addr.buffer == NULL)
3019eac3 20734 error (_("DW_FORM_addr_index used without .debug_addr section [in module %s]"),
4262abfb 20735 objfile_name (objfile));
18a8505e 20736 if (addr_base_or_zero + addr_index * addr_size
976ca316 20737 >= per_objfile->per_bfd->addr.size)
3019eac3
DE
20738 error (_("DW_FORM_addr_index pointing outside of "
20739 ".debug_addr section [in module %s]"),
4262abfb 20740 objfile_name (objfile));
976ca316
SM
20741 info_ptr = (per_objfile->per_bfd->addr.buffer + addr_base_or_zero
20742 + addr_index * addr_size);
3019eac3
DE
20743 if (addr_size == 4)
20744 return bfd_get_32 (abfd, info_ptr);
20745 else
20746 return bfd_get_64 (abfd, info_ptr);
20747}
20748
20749/* Given index ADDR_INDEX in .debug_addr, fetch the value. */
20750
20751static CORE_ADDR
20752read_addr_index (struct dwarf2_cu *cu, unsigned int addr_index)
20753{
5e22e966 20754 return read_addr_index_1 (cu->per_objfile, addr_index,
518817b3 20755 cu->addr_base, cu->header.addr_size);
3019eac3
DE
20756}
20757
20758/* Given a pointer to an leb128 value, fetch the value from .debug_addr. */
20759
20760static CORE_ADDR
d521ce57 20761read_addr_index_from_leb128 (struct dwarf2_cu *cu, const gdb_byte *info_ptr,
3019eac3
DE
20762 unsigned int *bytes_read)
20763{
5e22e966 20764 bfd *abfd = cu->per_objfile->objfile->obfd;
3019eac3
DE
20765 unsigned int addr_index = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
20766
20767 return read_addr_index (cu, addr_index);
20768}
20769
450a1bfc 20770/* See read.h. */
3019eac3
DE
20771
20772CORE_ADDR
82ca3f51 20773dwarf2_read_addr_index (dwarf2_per_cu_data *per_cu,
976ca316 20774 dwarf2_per_objfile *per_objfile,
82ca3f51 20775 unsigned int addr_index)
3019eac3 20776{
976ca316 20777 struct dwarf2_cu *cu = per_objfile->get_cu (per_cu);
18a8505e 20778 gdb::optional<ULONGEST> addr_base;
3019eac3
DE
20779 int addr_size;
20780
3019eac3
DE
20781 /* We need addr_base and addr_size.
20782 If we don't have PER_CU->cu, we have to get it.
20783 Nasty, but the alternative is storing the needed info in PER_CU,
20784 which at this point doesn't seem justified: it's not clear how frequently
20785 it would get used and it would increase the size of every PER_CU.
20786 Entry points like dwarf2_per_cu_addr_size do a similar thing
20787 so we're not in uncharted territory here.
20788 Alas we need to be a bit more complicated as addr_base is contained
20789 in the DIE.
20790
20791 We don't need to read the entire CU(/TU).
20792 We just need the header and top level die.
a1b64ce1 20793
3019eac3 20794 IWBN to use the aging mechanism to let us lazily later discard the CU.
a1b64ce1 20795 For now we skip this optimization. */
3019eac3
DE
20796
20797 if (cu != NULL)
20798 {
20799 addr_base = cu->addr_base;
20800 addr_size = cu->header.addr_size;
20801 }
20802 else
20803 {
976ca316 20804 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
20805 addr_base = reader.cu->addr_base;
20806 addr_size = reader.cu->header.addr_size;
3019eac3
DE
20807 }
20808
976ca316 20809 return read_addr_index_1 (per_objfile, addr_index, addr_base, addr_size);
3019eac3
DE
20810}
20811
18a8505e
AT
20812/* Given a DW_FORM_GNU_str_index value STR_INDEX, fetch the string.
20813 STR_SECTION, STR_OFFSETS_SECTION can be from a Fission stub or a
20814 DWO file. */
3019eac3 20815
d521ce57 20816static const char *
18a8505e
AT
20817read_str_index (struct dwarf2_cu *cu,
20818 struct dwarf2_section_info *str_section,
20819 struct dwarf2_section_info *str_offsets_section,
20820 ULONGEST str_offsets_base, ULONGEST str_index)
3019eac3 20821{
976ca316
SM
20822 dwarf2_per_objfile *per_objfile = cu->per_objfile;
20823 struct objfile *objfile = per_objfile->objfile;
c5164cbc 20824 const char *objf_name = objfile_name (objfile);
3019eac3 20825 bfd *abfd = objfile->obfd;
d521ce57 20826 const gdb_byte *info_ptr;
3019eac3 20827 ULONGEST str_offset;
cf532bd1 20828 static const char form_name[] = "DW_FORM_GNU_str_index or DW_FORM_strx";
3019eac3 20829
96b79293
TT
20830 str_section->read (objfile);
20831 str_offsets_section->read (objfile);
73869dc2 20832 if (str_section->buffer == NULL)
18a8505e 20833 error (_("%s used without %s section"
9d8780f0 20834 " in CU at offset %s [in module %s]"),
96b79293 20835 form_name, str_section->get_name (),
dda83cd7 20836 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20837 if (str_offsets_section->buffer == NULL)
18a8505e 20838 error (_("%s used without %s section"
9d8780f0 20839 " in CU at offset %s [in module %s]"),
96b79293 20840 form_name, str_section->get_name (),
dda83cd7 20841 sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20842 info_ptr = (str_offsets_section->buffer
18a8505e 20843 + str_offsets_base
3019eac3
DE
20844 + str_index * cu->header.offset_size);
20845 if (cu->header.offset_size == 4)
20846 str_offset = bfd_get_32 (abfd, info_ptr);
20847 else
20848 str_offset = bfd_get_64 (abfd, info_ptr);
73869dc2 20849 if (str_offset >= str_section->size)
57d63ce2 20850 error (_("Offset from %s pointing outside of"
9d8780f0
SM
20851 " .debug_str.dwo section in CU at offset %s [in module %s]"),
20852 form_name, sect_offset_str (cu->header.sect_off), objf_name);
73869dc2 20853 return (const char *) (str_section->buffer + str_offset);
3019eac3
DE
20854}
20855
18a8505e
AT
20856/* Given a DW_FORM_GNU_str_index from a DWO file, fetch the string. */
20857
20858static const char *
20859read_dwo_str_index (const struct die_reader_specs *reader, ULONGEST str_index)
20860{
20861 ULONGEST str_offsets_base = reader->cu->header.version >= 5
20862 ? reader->cu->header.addr_size : 0;
20863 return read_str_index (reader->cu,
20864 &reader->dwo_file->sections.str,
20865 &reader->dwo_file->sections.str_offsets,
20866 str_offsets_base, str_index);
20867}
20868
20869/* Given a DW_FORM_GNU_str_index from a Fission stub, fetch the string. */
20870
20871static const char *
20872read_stub_str_index (struct dwarf2_cu *cu, ULONGEST str_index)
20873{
5e22e966 20874 struct objfile *objfile = cu->per_objfile->objfile;
18a8505e
AT
20875 const char *objf_name = objfile_name (objfile);
20876 static const char form_name[] = "DW_FORM_GNU_str_index";
20877 static const char str_offsets_attr_name[] = "DW_AT_str_offsets";
20878
20879 if (!cu->str_offsets_base.has_value ())
20880 error (_("%s used in Fission stub without %s"
20881 " in CU at offset 0x%lx [in module %s]"),
20882 form_name, str_offsets_attr_name,
20883 (long) cu->header.offset_size, objf_name);
20884
20885 return read_str_index (cu,
5e22e966
SM
20886 &cu->per_objfile->per_bfd->str,
20887 &cu->per_objfile->per_bfd->str_offsets,
18a8505e
AT
20888 *cu->str_offsets_base, str_index);
20889}
20890
3019eac3
DE
20891/* Return the length of an LEB128 number in BUF. */
20892
20893static int
20894leb128_size (const gdb_byte *buf)
20895{
20896 const gdb_byte *begin = buf;
20897 gdb_byte byte;
20898
20899 while (1)
20900 {
20901 byte = *buf++;
20902 if ((byte & 128) == 0)
20903 return buf - begin;
20904 }
20905}
20906
c906108c 20907static void
e142c38c 20908set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
c906108c
SS
20909{
20910 switch (lang)
20911 {
20912 case DW_LANG_C89:
76bee0cc 20913 case DW_LANG_C99:
0cfd832f 20914 case DW_LANG_C11:
c906108c 20915 case DW_LANG_C:
d1be3247 20916 case DW_LANG_UPC:
e142c38c 20917 cu->language = language_c;
c906108c 20918 break;
9c37b5ae 20919 case DW_LANG_Java:
c906108c 20920 case DW_LANG_C_plus_plus:
0cfd832f
MW
20921 case DW_LANG_C_plus_plus_11:
20922 case DW_LANG_C_plus_plus_14:
e142c38c 20923 cu->language = language_cplus;
c906108c 20924 break;
6aecb9c2
JB
20925 case DW_LANG_D:
20926 cu->language = language_d;
20927 break;
c906108c
SS
20928 case DW_LANG_Fortran77:
20929 case DW_LANG_Fortran90:
b21b22e0 20930 case DW_LANG_Fortran95:
f7de9aab
MW
20931 case DW_LANG_Fortran03:
20932 case DW_LANG_Fortran08:
e142c38c 20933 cu->language = language_fortran;
c906108c 20934 break;
a766d390
DE
20935 case DW_LANG_Go:
20936 cu->language = language_go;
20937 break;
c906108c 20938 case DW_LANG_Mips_Assembler:
e142c38c 20939 cu->language = language_asm;
c906108c
SS
20940 break;
20941 case DW_LANG_Ada83:
8aaf0b47 20942 case DW_LANG_Ada95:
bc5f45f8
JB
20943 cu->language = language_ada;
20944 break;
72019c9c
GM
20945 case DW_LANG_Modula2:
20946 cu->language = language_m2;
20947 break;
fe8e67fd
PM
20948 case DW_LANG_Pascal83:
20949 cu->language = language_pascal;
20950 break;
22566fbd
DJ
20951 case DW_LANG_ObjC:
20952 cu->language = language_objc;
20953 break;
c44af4eb
TT
20954 case DW_LANG_Rust:
20955 case DW_LANG_Rust_old:
20956 cu->language = language_rust;
20957 break;
c906108c
SS
20958 case DW_LANG_Cobol74:
20959 case DW_LANG_Cobol85:
c906108c 20960 default:
e142c38c 20961 cu->language = language_minimal;
c906108c
SS
20962 break;
20963 }
e142c38c 20964 cu->language_defn = language_def (cu->language);
c906108c
SS
20965}
20966
20967/* Return the named attribute or NULL if not there. */
20968
20969static struct attribute *
e142c38c 20970dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
c906108c 20971{
a48e046c 20972 for (;;)
c906108c 20973 {
a48e046c
TT
20974 unsigned int i;
20975 struct attribute *spec = NULL;
20976
20977 for (i = 0; i < die->num_attrs; ++i)
20978 {
20979 if (die->attrs[i].name == name)
20980 return &die->attrs[i];
20981 if (die->attrs[i].name == DW_AT_specification
20982 || die->attrs[i].name == DW_AT_abstract_origin)
20983 spec = &die->attrs[i];
20984 }
20985
20986 if (!spec)
20987 break;
c906108c 20988
f2f0e013 20989 die = follow_die_ref (die, spec, &cu);
f2f0e013 20990 }
c5aa993b 20991
c906108c
SS
20992 return NULL;
20993}
20994
7d45c7c3
KB
20995/* Return the string associated with a string-typed attribute, or NULL if it
20996 is either not found or is of an incorrect type. */
20997
20998static const char *
20999dwarf2_string_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
21000{
21001 struct attribute *attr;
21002 const char *str = NULL;
21003
21004 attr = dwarf2_attr (die, name, cu);
21005
21006 if (attr != NULL)
21007 {
95f982e5 21008 str = attr->as_string ();
e61108c9 21009 if (str == nullptr)
dda83cd7 21010 complaint (_("string type expected for attribute %s for "
9d8780f0
SM
21011 "DIE at %s in module %s"),
21012 dwarf_attr_name (name), sect_offset_str (die->sect_off),
5e22e966 21013 objfile_name (cu->per_objfile->objfile));
7d45c7c3
KB
21014 }
21015
21016 return str;
21017}
21018
a084a2a6 21019/* Return the dwo name or NULL if not present. If present, it is in either
85102364 21020 DW_AT_GNU_dwo_name or DW_AT_dwo_name attribute. */
a084a2a6
AT
21021static const char *
21022dwarf2_dwo_name (struct die_info *die, struct dwarf2_cu *cu)
21023{
21024 const char *dwo_name = dwarf2_string_attr (die, DW_AT_GNU_dwo_name, cu);
21025 if (dwo_name == nullptr)
21026 dwo_name = dwarf2_string_attr (die, DW_AT_dwo_name, cu);
21027 return dwo_name;
21028}
21029
05cf31d1
JB
21030/* Return non-zero iff the attribute NAME is defined for the given DIE,
21031 and holds a non-zero value. This function should only be used for
2dc7f7b3 21032 DW_FORM_flag or DW_FORM_flag_present attributes. */
05cf31d1
JB
21033
21034static int
21035dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
21036{
21037 struct attribute *attr = dwarf2_attr (die, name, cu);
21038
c45bc3f8 21039 return attr != nullptr && attr->as_boolean ();
05cf31d1
JB
21040}
21041
3ca72b44 21042static int
e142c38c 21043die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
3ca72b44 21044{
05cf31d1
JB
21045 /* A DIE is a declaration if it has a DW_AT_declaration attribute
21046 which value is non-zero. However, we have to be careful with
21047 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
21048 (via dwarf2_flag_true_p) follows this attribute. So we may
21049 end up accidently finding a declaration attribute that belongs
21050 to a different DIE referenced by the specification attribute,
21051 even though the given DIE does not have a declaration attribute. */
21052 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
21053 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
3ca72b44
AC
21054}
21055
63d06c5c 21056/* Return the die giving the specification for DIE, if there is
f2f0e013 21057 one. *SPEC_CU is the CU containing DIE on input, and the CU
edb3359d
DJ
21058 containing the return value on output. If there is no
21059 specification, but there is an abstract origin, that is
21060 returned. */
63d06c5c
DC
21061
21062static struct die_info *
f2f0e013 21063die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
63d06c5c 21064{
f2f0e013
DJ
21065 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
21066 *spec_cu);
63d06c5c 21067
edb3359d
DJ
21068 if (spec_attr == NULL)
21069 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
21070
63d06c5c
DC
21071 if (spec_attr == NULL)
21072 return NULL;
21073 else
f2f0e013 21074 return follow_die_ref (die, spec_attr, spec_cu);
63d06c5c 21075}
c906108c 21076
527f3840
JK
21077/* Stub for free_line_header to match void * callback types. */
21078
21079static void
21080free_line_header_voidp (void *arg)
21081{
9a3c8263 21082 struct line_header *lh = (struct line_header *) arg;
527f3840 21083
fff8551c 21084 delete lh;
527f3840
JK
21085}
21086
83769d0b 21087/* A convenience function to find the proper .debug_line section for a CU. */
36586728
TT
21088
21089static struct dwarf2_section_info *
21090get_debug_line_section (struct dwarf2_cu *cu)
21091{
21092 struct dwarf2_section_info *section;
976ca316 21093 dwarf2_per_objfile *per_objfile = cu->per_objfile;
36586728
TT
21094
21095 /* For TUs in DWO files, the DW_AT_stmt_list attribute lives in the
21096 DWO file. */
21097 if (cu->dwo_unit && cu->per_cu->is_debug_types)
21098 section = &cu->dwo_unit->dwo_file->sections.line;
21099 else if (cu->per_cu->is_dwz)
21100 {
976ca316 21101 dwz_file *dwz = dwarf2_get_dwz_file (per_objfile->per_bfd);
36586728
TT
21102
21103 section = &dwz->line;
21104 }
21105 else
976ca316 21106 section = &per_objfile->per_bfd->line;
36586728
TT
21107
21108 return section;
21109}
21110
debd256d 21111/* Read the statement program header starting at OFFSET in
3019eac3 21112 .debug_line, or .debug_line.dwo. Return a pointer
6502dd73 21113 to a struct line_header, allocated using xmalloc.
cd366ee8
DE
21114 Returns NULL if there is a problem reading the header, e.g., if it
21115 has a version we don't understand.
debd256d
JB
21116
21117 NOTE: the strings in the include directory and file name tables of
3019eac3
DE
21118 the returned object point into the dwarf line section buffer,
21119 and must not be freed. */
ae2de4f8 21120
fff8551c 21121static line_header_up
9c541725 21122dwarf_decode_line_header (sect_offset sect_off, struct dwarf2_cu *cu)
debd256d 21123{
3019eac3 21124 struct dwarf2_section_info *section;
976ca316 21125 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3 21126
36586728 21127 section = get_debug_line_section (cu);
976ca316 21128 section->read (per_objfile->objfile);
3019eac3 21129 if (section->buffer == NULL)
debd256d 21130 {
3019eac3 21131 if (cu->dwo_unit && cu->per_cu->is_debug_types)
b98664d3 21132 complaint (_("missing .debug_line.dwo section"));
3019eac3 21133 else
b98664d3 21134 complaint (_("missing .debug_line section"));
debd256d
JB
21135 return 0;
21136 }
21137
0df7ad3a 21138 return dwarf_decode_line_header (sect_off, cu->per_cu->is_dwz,
976ca316 21139 per_objfile, section, &cu->header);
debd256d 21140}
c906108c 21141
c6da4cef 21142/* Subroutine of dwarf_decode_lines to simplify it.
7ba99d21 21143 Return the file name of the psymtab for the given file_entry.
c6da4cef 21144 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
c89b44cd
TT
21145 If space for the result is malloc'd, *NAME_HOLDER will be set.
21146 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
c6da4cef 21147
d521ce57 21148static const char *
7ba99d21 21149psymtab_include_file_name (const struct line_header *lh, const file_entry &fe,
891813be 21150 const dwarf2_psymtab *pst,
c89b44cd
TT
21151 const char *comp_dir,
21152 gdb::unique_xmalloc_ptr<char> *name_holder)
c6da4cef 21153{
d521ce57
TT
21154 const char *include_name = fe.name;
21155 const char *include_name_to_compare = include_name;
72b9f47f 21156 const char *pst_filename;
c6da4cef
DE
21157 int file_is_pst;
21158
8c43009f 21159 const char *dir_name = fe.include_dir (lh);
c6da4cef 21160
c89b44cd 21161 gdb::unique_xmalloc_ptr<char> hold_compare;
c6da4cef
DE
21162 if (!IS_ABSOLUTE_PATH (include_name)
21163 && (dir_name != NULL || comp_dir != NULL))
21164 {
21165 /* Avoid creating a duplicate psymtab for PST.
21166 We do this by comparing INCLUDE_NAME and PST_FILENAME.
21167 Before we do the comparison, however, we need to account
21168 for DIR_NAME and COMP_DIR.
21169 First prepend dir_name (if non-NULL). If we still don't
21170 have an absolute path prepend comp_dir (if non-NULL).
21171 However, the directory we record in the include-file's
21172 psymtab does not contain COMP_DIR (to match the
21173 corresponding symtab(s)).
21174
21175 Example:
21176
21177 bash$ cd /tmp
21178 bash$ gcc -g ./hello.c
21179 include_name = "hello.c"
21180 dir_name = "."
21181 DW_AT_comp_dir = comp_dir = "/tmp"
5f52445b
YQ
21182 DW_AT_name = "./hello.c"
21183
21184 */
c6da4cef
DE
21185
21186 if (dir_name != NULL)
21187 {
c89b44cd
TT
21188 name_holder->reset (concat (dir_name, SLASH_STRING,
21189 include_name, (char *) NULL));
21190 include_name = name_holder->get ();
c6da4cef 21191 include_name_to_compare = include_name;
c6da4cef
DE
21192 }
21193 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
21194 {
c89b44cd
TT
21195 hold_compare.reset (concat (comp_dir, SLASH_STRING,
21196 include_name, (char *) NULL));
21197 include_name_to_compare = hold_compare.get ();
c6da4cef
DE
21198 }
21199 }
21200
21201 pst_filename = pst->filename;
c89b44cd 21202 gdb::unique_xmalloc_ptr<char> copied_name;
c6da4cef
DE
21203 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
21204 {
c89b44cd
TT
21205 copied_name.reset (concat (pst->dirname, SLASH_STRING,
21206 pst_filename, (char *) NULL));
21207 pst_filename = copied_name.get ();
c6da4cef
DE
21208 }
21209
1e3fad37 21210 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
c6da4cef 21211
c6da4cef
DE
21212 if (file_is_pst)
21213 return NULL;
21214 return include_name;
21215}
21216
d9b3de22
DE
21217/* State machine to track the state of the line number program. */
21218
6f77053d 21219class lnp_state_machine
d9b3de22 21220{
6f77053d
PA
21221public:
21222 /* Initialize a machine state for the start of a line number
21223 program. */
804d2729
TT
21224 lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch, line_header *lh,
21225 bool record_lines_p);
6f77053d 21226
8c43009f
PA
21227 file_entry *current_file ()
21228 {
21229 /* lh->file_names is 0-based, but the file name numbers in the
21230 statement program are 1-based. */
6f77053d
PA
21231 return m_line_header->file_name_at (m_file);
21232 }
21233
21234 /* Record the line in the state machine. END_SEQUENCE is true if
21235 we're processing the end of a sequence. */
21236 void record_line (bool end_sequence);
21237
a8caed5d 21238 /* Check ADDRESS is -1, or zero and less than UNRELOCATED_LOWPC, and if true
7ab6656f 21239 nop-out rest of the lines in this sequence. */
6f77053d
PA
21240 void check_line_address (struct dwarf2_cu *cu,
21241 const gdb_byte *line_ptr,
7ab6656f 21242 CORE_ADDR unrelocated_lowpc, CORE_ADDR address);
6f77053d
PA
21243
21244 void handle_set_discriminator (unsigned int discriminator)
21245 {
21246 m_discriminator = discriminator;
21247 m_line_has_non_zero_discriminator |= discriminator != 0;
21248 }
21249
21250 /* Handle DW_LNE_set_address. */
21251 void handle_set_address (CORE_ADDR baseaddr, CORE_ADDR address)
21252 {
21253 m_op_index = 0;
21254 address += baseaddr;
21255 m_address = gdbarch_adjust_dwarf2_line (m_gdbarch, address, false);
21256 }
21257
21258 /* Handle DW_LNS_advance_pc. */
21259 void handle_advance_pc (CORE_ADDR adjust);
21260
21261 /* Handle a special opcode. */
21262 void handle_special_opcode (unsigned char op_code);
21263
21264 /* Handle DW_LNS_advance_line. */
21265 void handle_advance_line (int line_delta)
21266 {
21267 advance_line (line_delta);
21268 }
21269
21270 /* Handle DW_LNS_set_file. */
21271 void handle_set_file (file_name_index file);
21272
21273 /* Handle DW_LNS_negate_stmt. */
21274 void handle_negate_stmt ()
21275 {
21276 m_is_stmt = !m_is_stmt;
21277 }
21278
21279 /* Handle DW_LNS_const_add_pc. */
21280 void handle_const_add_pc ();
21281
21282 /* Handle DW_LNS_fixed_advance_pc. */
21283 void handle_fixed_advance_pc (CORE_ADDR addr_adj)
21284 {
21285 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21286 m_op_index = 0;
21287 }
21288
21289 /* Handle DW_LNS_copy. */
21290 void handle_copy ()
21291 {
21292 record_line (false);
21293 m_discriminator = 0;
21294 }
21295
21296 /* Handle DW_LNE_end_sequence. */
21297 void handle_end_sequence ()
21298 {
804d2729 21299 m_currently_recording_lines = true;
6f77053d
PA
21300 }
21301
21302private:
21303 /* Advance the line by LINE_DELTA. */
21304 void advance_line (int line_delta)
21305 {
21306 m_line += line_delta;
21307
21308 if (line_delta != 0)
21309 m_line_has_non_zero_discriminator = m_discriminator != 0;
8c43009f
PA
21310 }
21311
804d2729
TT
21312 struct dwarf2_cu *m_cu;
21313
6f77053d
PA
21314 gdbarch *m_gdbarch;
21315
21316 /* True if we're recording lines.
21317 Otherwise we're building partial symtabs and are just interested in
21318 finding include files mentioned by the line number program. */
21319 bool m_record_lines_p;
21320
8c43009f 21321 /* The line number header. */
6f77053d 21322 line_header *m_line_header;
8c43009f 21323
6f77053d
PA
21324 /* These are part of the standard DWARF line number state machine,
21325 and initialized according to the DWARF spec. */
d9b3de22 21326
6f77053d 21327 unsigned char m_op_index = 0;
7ba99d21
AT
21328 /* The line table index of the current file. */
21329 file_name_index m_file = 1;
6f77053d
PA
21330 unsigned int m_line = 1;
21331
21332 /* These are initialized in the constructor. */
21333
21334 CORE_ADDR m_address;
21335 bool m_is_stmt;
21336 unsigned int m_discriminator;
d9b3de22
DE
21337
21338 /* Additional bits of state we need to track. */
21339
21340 /* The last file that we called dwarf2_start_subfile for.
21341 This is only used for TLLs. */
6f77053d 21342 unsigned int m_last_file = 0;
d9b3de22 21343 /* The last file a line number was recorded for. */
6f77053d 21344 struct subfile *m_last_subfile = NULL;
d9b3de22 21345
1313c56e
AB
21346 /* The address of the last line entry. */
21347 CORE_ADDR m_last_address;
21348
21349 /* Set to true when a previous line at the same address (using
21350 m_last_address) had m_is_stmt true. This is reset to false when a
21351 line entry at a new address (m_address different to m_last_address) is
21352 processed. */
21353 bool m_stmt_at_address = false;
21354
804d2729
TT
21355 /* When true, record the lines we decode. */
21356 bool m_currently_recording_lines = false;
d9b3de22
DE
21357
21358 /* The last line number that was recorded, used to coalesce
21359 consecutive entries for the same line. This can happen, for
21360 example, when discriminators are present. PR 17276. */
6f77053d
PA
21361 unsigned int m_last_line = 0;
21362 bool m_line_has_non_zero_discriminator = false;
8c43009f 21363};
d9b3de22 21364
6f77053d
PA
21365void
21366lnp_state_machine::handle_advance_pc (CORE_ADDR adjust)
21367{
21368 CORE_ADDR addr_adj = (((m_op_index + adjust)
21369 / m_line_header->maximum_ops_per_instruction)
21370 * m_line_header->minimum_instruction_length);
21371 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21372 m_op_index = ((m_op_index + adjust)
21373 % m_line_header->maximum_ops_per_instruction);
21374}
d9b3de22 21375
6f77053d
PA
21376void
21377lnp_state_machine::handle_special_opcode (unsigned char op_code)
d9b3de22 21378{
6f77053d 21379 unsigned char adj_opcode = op_code - m_line_header->opcode_base;
258bf0ee
RB
21380 unsigned char adj_opcode_d = adj_opcode / m_line_header->line_range;
21381 unsigned char adj_opcode_r = adj_opcode % m_line_header->line_range;
21382 CORE_ADDR addr_adj = (((m_op_index + adj_opcode_d)
6f77053d
PA
21383 / m_line_header->maximum_ops_per_instruction)
21384 * m_line_header->minimum_instruction_length);
21385 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
258bf0ee 21386 m_op_index = ((m_op_index + adj_opcode_d)
6f77053d 21387 % m_line_header->maximum_ops_per_instruction);
d9b3de22 21388
258bf0ee 21389 int line_delta = m_line_header->line_base + adj_opcode_r;
6f77053d
PA
21390 advance_line (line_delta);
21391 record_line (false);
21392 m_discriminator = 0;
21393}
d9b3de22 21394
6f77053d
PA
21395void
21396lnp_state_machine::handle_set_file (file_name_index file)
21397{
21398 m_file = file;
21399
21400 const file_entry *fe = current_file ();
21401 if (fe == NULL)
21402 dwarf2_debug_line_missing_file_complaint ();
21403 else if (m_record_lines_p)
21404 {
21405 const char *dir = fe->include_dir (m_line_header);
21406
c24bdb02 21407 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21408 m_line_has_non_zero_discriminator = m_discriminator != 0;
804d2729 21409 dwarf2_start_subfile (m_cu, fe->name, dir);
6f77053d
PA
21410 }
21411}
21412
21413void
21414lnp_state_machine::handle_const_add_pc ()
21415{
21416 CORE_ADDR adjust
21417 = (255 - m_line_header->opcode_base) / m_line_header->line_range;
21418
21419 CORE_ADDR addr_adj
21420 = (((m_op_index + adjust)
21421 / m_line_header->maximum_ops_per_instruction)
21422 * m_line_header->minimum_instruction_length);
21423
21424 m_address += gdbarch_adjust_dwarf2_line (m_gdbarch, addr_adj, true);
21425 m_op_index = ((m_op_index + adjust)
21426 % m_line_header->maximum_ops_per_instruction);
21427}
d9b3de22 21428
a05a36a5
DE
21429/* Return non-zero if we should add LINE to the line number table.
21430 LINE is the line to add, LAST_LINE is the last line that was added,
21431 LAST_SUBFILE is the subfile for LAST_LINE.
21432 LINE_HAS_NON_ZERO_DISCRIMINATOR is non-zero if LINE has ever
21433 had a non-zero discriminator.
21434
21435 We have to be careful in the presence of discriminators.
21436 E.g., for this line:
21437
21438 for (i = 0; i < 100000; i++);
21439
21440 clang can emit four line number entries for that one line,
21441 each with a different discriminator.
21442 See gdb.dwarf2/dw2-single-line-discriminators.exp for an example.
21443
21444 However, we want gdb to coalesce all four entries into one.
21445 Otherwise the user could stepi into the middle of the line and
21446 gdb would get confused about whether the pc really was in the
21447 middle of the line.
21448
21449 Things are further complicated by the fact that two consecutive
21450 line number entries for the same line is a heuristic used by gcc
21451 to denote the end of the prologue. So we can't just discard duplicate
21452 entries, we have to be selective about it. The heuristic we use is
21453 that we only collapse consecutive entries for the same line if at least
21454 one of those entries has a non-zero discriminator. PR 17276.
21455
21456 Note: Addresses in the line number state machine can never go backwards
21457 within one sequence, thus this coalescing is ok. */
21458
21459static int
804d2729
TT
21460dwarf_record_line_p (struct dwarf2_cu *cu,
21461 unsigned int line, unsigned int last_line,
a05a36a5
DE
21462 int line_has_non_zero_discriminator,
21463 struct subfile *last_subfile)
21464{
c24bdb02 21465 if (cu->get_builder ()->get_current_subfile () != last_subfile)
a05a36a5
DE
21466 return 1;
21467 if (line != last_line)
21468 return 1;
21469 /* Same line for the same file that we've seen already.
21470 As a last check, for pr 17276, only record the line if the line
21471 has never had a non-zero discriminator. */
21472 if (!line_has_non_zero_discriminator)
21473 return 1;
21474 return 0;
21475}
21476
804d2729
TT
21477/* Use the CU's builder to record line number LINE beginning at
21478 address ADDRESS in the line table of subfile SUBFILE. */
252a6764
DE
21479
21480static void
d9b3de22 21481dwarf_record_line_1 (struct gdbarch *gdbarch, struct subfile *subfile,
8c95582d 21482 unsigned int line, CORE_ADDR address, bool is_stmt,
804d2729 21483 struct dwarf2_cu *cu)
252a6764
DE
21484{
21485 CORE_ADDR addr = gdbarch_addr_bits_remove (gdbarch, address);
21486
27e0867f
DE
21487 if (dwarf_line_debug)
21488 {
21489 fprintf_unfiltered (gdb_stdlog,
21490 "Recording line %u, file %s, address %s\n",
21491 line, lbasename (subfile->name),
21492 paddress (gdbarch, address));
21493 }
21494
804d2729 21495 if (cu != nullptr)
8c95582d 21496 cu->get_builder ()->record_line (subfile, line, addr, is_stmt);
252a6764
DE
21497}
21498
21499/* Subroutine of dwarf_decode_lines_1 to simplify it.
21500 Mark the end of a set of line number records.
d9b3de22 21501 The arguments are the same as for dwarf_record_line_1.
252a6764
DE
21502 If SUBFILE is NULL the request is ignored. */
21503
21504static void
21505dwarf_finish_line (struct gdbarch *gdbarch, struct subfile *subfile,
804d2729 21506 CORE_ADDR address, struct dwarf2_cu *cu)
252a6764 21507{
27e0867f
DE
21508 if (subfile == NULL)
21509 return;
21510
21511 if (dwarf_line_debug)
21512 {
21513 fprintf_unfiltered (gdb_stdlog,
21514 "Finishing current line, file %s, address %s\n",
21515 lbasename (subfile->name),
21516 paddress (gdbarch, address));
21517 }
21518
8c95582d 21519 dwarf_record_line_1 (gdbarch, subfile, 0, address, true, cu);
d9b3de22
DE
21520}
21521
6f77053d
PA
21522void
21523lnp_state_machine::record_line (bool end_sequence)
d9b3de22 21524{
d9b3de22
DE
21525 if (dwarf_line_debug)
21526 {
21527 fprintf_unfiltered (gdb_stdlog,
21528 "Processing actual line %u: file %u,"
94a72be7 21529 " address %s, is_stmt %u, discrim %u%s\n",
7ba99d21 21530 m_line, m_file,
6f77053d 21531 paddress (m_gdbarch, m_address),
94a72be7
AB
21532 m_is_stmt, m_discriminator,
21533 (end_sequence ? "\t(end sequence)" : ""));
d9b3de22
DE
21534 }
21535
6f77053d 21536 file_entry *fe = current_file ();
8c43009f
PA
21537
21538 if (fe == NULL)
d9b3de22
DE
21539 dwarf2_debug_line_missing_file_complaint ();
21540 /* For now we ignore lines not starting on an instruction boundary.
21541 But not when processing end_sequence for compatibility with the
21542 previous version of the code. */
6f77053d 21543 else if (m_op_index == 0 || end_sequence)
d9b3de22 21544 {
8c43009f 21545 fe->included_p = 1;
8c95582d 21546 if (m_record_lines_p)
d9b3de22 21547 {
1313c56e
AB
21548 /* When we switch files we insert an end maker in the first file,
21549 switch to the second file and add a new line entry. The
21550 problem is that the end marker inserted in the first file will
21551 discard any previous line entries at the same address. If the
21552 line entries in the first file are marked as is-stmt, while
21553 the new line in the second file is non-stmt, then this means
21554 the end marker will discard is-stmt lines so we can have a
21555 non-stmt line. This means that there are less addresses at
21556 which the user can insert a breakpoint.
21557
21558 To improve this we track the last address in m_last_address,
21559 and whether we have seen an is-stmt at this address. Then
21560 when switching files, if we have seen a stmt at the current
21561 address, and we are switching to create a non-stmt line, then
21562 discard the new line. */
21563 bool file_changed
21564 = m_last_subfile != m_cu->get_builder ()->get_current_subfile ();
21565 bool ignore_this_line
dda83cd7
SM
21566 = ((file_changed && !end_sequence && m_last_address == m_address
21567 && !m_is_stmt && m_stmt_at_address)
21568 || (!end_sequence && m_line == 0));
1313c56e
AB
21569
21570 if ((file_changed && !ignore_this_line) || end_sequence)
d9b3de22 21571 {
804d2729
TT
21572 dwarf_finish_line (m_gdbarch, m_last_subfile, m_address,
21573 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22
DE
21574 }
21575
1313c56e 21576 if (!end_sequence && !ignore_this_line)
d9b3de22 21577 {
8c95582d
AB
21578 bool is_stmt = producer_is_codewarrior (m_cu) || m_is_stmt;
21579
804d2729 21580 if (dwarf_record_line_p (m_cu, m_line, m_last_line,
6f77053d
PA
21581 m_line_has_non_zero_discriminator,
21582 m_last_subfile))
d9b3de22 21583 {
c24bdb02 21584 buildsym_compunit *builder = m_cu->get_builder ();
804d2729 21585 dwarf_record_line_1 (m_gdbarch,
c24bdb02 21586 builder->get_current_subfile (),
8c95582d 21587 m_line, m_address, is_stmt,
804d2729 21588 m_currently_recording_lines ? m_cu : nullptr);
d9b3de22 21589 }
c24bdb02 21590 m_last_subfile = m_cu->get_builder ()->get_current_subfile ();
6f77053d 21591 m_last_line = m_line;
d9b3de22
DE
21592 }
21593 }
21594 }
1313c56e
AB
21595
21596 /* Track whether we have seen any m_is_stmt true at m_address in case we
21597 have multiple line table entries all at m_address. */
21598 if (m_last_address != m_address)
21599 {
21600 m_stmt_at_address = false;
21601 m_last_address = m_address;
21602 }
21603 m_stmt_at_address |= m_is_stmt;
d9b3de22
DE
21604}
21605
804d2729
TT
21606lnp_state_machine::lnp_state_machine (struct dwarf2_cu *cu, gdbarch *arch,
21607 line_header *lh, bool record_lines_p)
d9b3de22 21608{
804d2729 21609 m_cu = cu;
6f77053d
PA
21610 m_gdbarch = arch;
21611 m_record_lines_p = record_lines_p;
21612 m_line_header = lh;
d9b3de22 21613
804d2729 21614 m_currently_recording_lines = true;
d9b3de22 21615
d9b3de22
DE
21616 /* Call `gdbarch_adjust_dwarf2_line' on the initial 0 address as if there
21617 was a line entry for it so that the backend has a chance to adjust it
21618 and also record it in case it needs it. This is currently used by MIPS
21619 code, cf. `mips_adjust_dwarf2_line'. */
6f77053d
PA
21620 m_address = gdbarch_adjust_dwarf2_line (arch, 0, 0);
21621 m_is_stmt = lh->default_is_stmt;
21622 m_discriminator = 0;
1313c56e
AB
21623
21624 m_last_address = m_address;
21625 m_stmt_at_address = false;
252a6764
DE
21626}
21627
6f77053d
PA
21628void
21629lnp_state_machine::check_line_address (struct dwarf2_cu *cu,
21630 const gdb_byte *line_ptr,
7ab6656f 21631 CORE_ADDR unrelocated_lowpc, CORE_ADDR address)
924c2928 21632{
a8caed5d
FS
21633 /* Linkers resolve a symbolic relocation referencing a GC'd function to 0 or
21634 -1. If ADDRESS is 0, ignoring the opcode will err if the text section is
21635 located at 0x0. In this case, additionally check that if
21636 ADDRESS < UNRELOCATED_LOWPC. */
924c2928 21637
a8caed5d
FS
21638 if ((address == 0 && address < unrelocated_lowpc)
21639 || address == (CORE_ADDR) -1)
924c2928
DE
21640 {
21641 /* This line table is for a function which has been
21642 GCd by the linker. Ignore it. PR gdb/12528 */
21643
5e22e966 21644 struct objfile *objfile = cu->per_objfile->objfile;
924c2928
DE
21645 long line_offset = line_ptr - get_debug_line_section (cu)->buffer;
21646
b98664d3 21647 complaint (_(".debug_line address at offset 0x%lx is 0 [in module %s]"),
924c2928 21648 line_offset, objfile_name (objfile));
804d2729
TT
21649 m_currently_recording_lines = false;
21650 /* Note: m_currently_recording_lines is left as false until we see
21651 DW_LNE_end_sequence. */
924c2928
DE
21652 }
21653}
21654
f3f5162e 21655/* Subroutine of dwarf_decode_lines to simplify it.
d9b3de22
DE
21656 Process the line number information in LH.
21657 If DECODE_FOR_PST_P is non-zero, all we do is process the line number
21658 program in order to set included_p for every referenced header. */
debd256d 21659
c906108c 21660static void
43f3e411
DE
21661dwarf_decode_lines_1 (struct line_header *lh, struct dwarf2_cu *cu,
21662 const int decode_for_pst_p, CORE_ADDR lowpc)
c906108c 21663{
d521ce57
TT
21664 const gdb_byte *line_ptr, *extended_end;
21665 const gdb_byte *line_end;
a8c50c1f 21666 unsigned int bytes_read, extended_len;
699ca60a 21667 unsigned char op_code, extended_op;
e142c38c 21668 CORE_ADDR baseaddr;
5e22e966 21669 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21670 bfd *abfd = objfile->obfd;
08feed99 21671 struct gdbarch *gdbarch = objfile->arch ();
6f77053d
PA
21672 /* True if we're recording line info (as opposed to building partial
21673 symtabs and just interested in finding include files mentioned by
21674 the line number program). */
21675 bool record_lines_p = !decode_for_pst_p;
e142c38c 21676
b3b3bada 21677 baseaddr = objfile->text_section_offset ();
c906108c 21678
debd256d
JB
21679 line_ptr = lh->statement_program_start;
21680 line_end = lh->statement_program_end;
c906108c
SS
21681
21682 /* Read the statement sequences until there's nothing left. */
21683 while (line_ptr < line_end)
21684 {
6f77053d
PA
21685 /* The DWARF line number program state machine. Reset the state
21686 machine at the start of each sequence. */
804d2729 21687 lnp_state_machine state_machine (cu, gdbarch, lh, record_lines_p);
6f77053d 21688 bool end_sequence = false;
d9b3de22 21689
8c43009f 21690 if (record_lines_p)
c906108c 21691 {
8c43009f
PA
21692 /* Start a subfile for the current file of the state
21693 machine. */
21694 const file_entry *fe = state_machine.current_file ();
21695
21696 if (fe != NULL)
804d2729 21697 dwarf2_start_subfile (cu, fe->name, fe->include_dir (lh));
c906108c
SS
21698 }
21699
a738430d 21700 /* Decode the table. */
d9b3de22 21701 while (line_ptr < line_end && !end_sequence)
c906108c
SS
21702 {
21703 op_code = read_1_byte (abfd, line_ptr);
21704 line_ptr += 1;
9aa1fe7e 21705
debd256d 21706 if (op_code >= lh->opcode_base)
6e70227d 21707 {
8e07a239 21708 /* Special opcode. */
6f77053d 21709 state_machine.handle_special_opcode (op_code);
9aa1fe7e
GK
21710 }
21711 else switch (op_code)
c906108c
SS
21712 {
21713 case DW_LNS_extended_op:
3e43a32a
MS
21714 extended_len = read_unsigned_leb128 (abfd, line_ptr,
21715 &bytes_read);
473b7be6 21716 line_ptr += bytes_read;
a8c50c1f 21717 extended_end = line_ptr + extended_len;
c906108c
SS
21718 extended_op = read_1_byte (abfd, line_ptr);
21719 line_ptr += 1;
8f34b746
TV
21720 if (DW_LNE_lo_user <= extended_op
21721 && extended_op <= DW_LNE_hi_user)
21722 {
21723 /* Vendor extension, ignore. */
21724 line_ptr = extended_end;
21725 break;
21726 }
c906108c
SS
21727 switch (extended_op)
21728 {
21729 case DW_LNE_end_sequence:
6f77053d
PA
21730 state_machine.handle_end_sequence ();
21731 end_sequence = true;
c906108c
SS
21732 break;
21733 case DW_LNE_set_address:
d9b3de22
DE
21734 {
21735 CORE_ADDR address
c8a7a66f 21736 = cu->header.read_address (abfd, line_ptr, &bytes_read);
d9b3de22 21737 line_ptr += bytes_read;
6f77053d
PA
21738
21739 state_machine.check_line_address (cu, line_ptr,
7ab6656f 21740 lowpc - baseaddr, address);
6f77053d 21741 state_machine.handle_set_address (baseaddr, address);
d9b3de22 21742 }
c906108c
SS
21743 break;
21744 case DW_LNE_define_file:
dda83cd7
SM
21745 {
21746 const char *cur_file;
ecfb656c
PA
21747 unsigned int mod_time, length;
21748 dir_index dindex;
6e70227d 21749
dda83cd7 21750 cur_file = read_direct_string (abfd, line_ptr,
3e43a32a 21751 &bytes_read);
dda83cd7
SM
21752 line_ptr += bytes_read;
21753 dindex = (dir_index)
21754 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21755 line_ptr += bytes_read;
21756 mod_time =
21757 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21758 line_ptr += bytes_read;
21759 length =
21760 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21761 line_ptr += bytes_read;
21762 lh->add_file_name (cur_file, dindex, mod_time, length);
21763 }
c906108c 21764 break;
d0c6ba3d 21765 case DW_LNE_set_discriminator:
6f77053d
PA
21766 {
21767 /* The discriminator is not interesting to the
21768 debugger; just ignore it. We still need to
21769 check its value though:
21770 if there are consecutive entries for the same
21771 (non-prologue) line we want to coalesce them.
21772 PR 17276. */
21773 unsigned int discr
21774 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21775 line_ptr += bytes_read;
21776
21777 state_machine.handle_set_discriminator (discr);
21778 }
d0c6ba3d 21779 break;
c906108c 21780 default:
b98664d3 21781 complaint (_("mangled .debug_line section"));
debd256d 21782 return;
c906108c 21783 }
a8c50c1f
DJ
21784 /* Make sure that we parsed the extended op correctly. If e.g.
21785 we expected a different address size than the producer used,
21786 we may have read the wrong number of bytes. */
21787 if (line_ptr != extended_end)
21788 {
b98664d3 21789 complaint (_("mangled .debug_line section"));
a8c50c1f
DJ
21790 return;
21791 }
c906108c
SS
21792 break;
21793 case DW_LNS_copy:
6f77053d 21794 state_machine.handle_copy ();
c906108c
SS
21795 break;
21796 case DW_LNS_advance_pc:
2dc7f7b3
TT
21797 {
21798 CORE_ADDR adjust
21799 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
2dc7f7b3 21800 line_ptr += bytes_read;
6f77053d
PA
21801
21802 state_machine.handle_advance_pc (adjust);
2dc7f7b3 21803 }
c906108c
SS
21804 break;
21805 case DW_LNS_advance_line:
a05a36a5
DE
21806 {
21807 int line_delta
21808 = read_signed_leb128 (abfd, line_ptr, &bytes_read);
a05a36a5 21809 line_ptr += bytes_read;
6f77053d
PA
21810
21811 state_machine.handle_advance_line (line_delta);
a05a36a5 21812 }
c906108c
SS
21813 break;
21814 case DW_LNS_set_file:
d9b3de22 21815 {
6f77053d 21816 file_name_index file
ecfb656c
PA
21817 = (file_name_index) read_unsigned_leb128 (abfd, line_ptr,
21818 &bytes_read);
d9b3de22 21819 line_ptr += bytes_read;
8c43009f 21820
6f77053d 21821 state_machine.handle_set_file (file);
d9b3de22 21822 }
c906108c
SS
21823 break;
21824 case DW_LNS_set_column:
0ad93d4f 21825 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
c906108c
SS
21826 line_ptr += bytes_read;
21827 break;
21828 case DW_LNS_negate_stmt:
6f77053d 21829 state_machine.handle_negate_stmt ();
c906108c
SS
21830 break;
21831 case DW_LNS_set_basic_block:
c906108c 21832 break;
c2c6d25f
JM
21833 /* Add to the address register of the state machine the
21834 address increment value corresponding to special opcode
a738430d
MK
21835 255. I.e., this value is scaled by the minimum
21836 instruction length since special opcode 255 would have
b021a221 21837 scaled the increment. */
c906108c 21838 case DW_LNS_const_add_pc:
6f77053d 21839 state_machine.handle_const_add_pc ();
c906108c
SS
21840 break;
21841 case DW_LNS_fixed_advance_pc:
3e29f34a 21842 {
6f77053d 21843 CORE_ADDR addr_adj = read_2_bytes (abfd, line_ptr);
3e29f34a 21844 line_ptr += 2;
6f77053d
PA
21845
21846 state_machine.handle_fixed_advance_pc (addr_adj);
3e29f34a 21847 }
c906108c 21848 break;
9aa1fe7e 21849 default:
a738430d
MK
21850 {
21851 /* Unknown standard opcode, ignore it. */
9aa1fe7e 21852 int i;
a738430d 21853
debd256d 21854 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
9aa1fe7e
GK
21855 {
21856 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
21857 line_ptr += bytes_read;
21858 }
21859 }
c906108c
SS
21860 }
21861 }
d9b3de22
DE
21862
21863 if (!end_sequence)
21864 dwarf2_debug_line_missing_end_sequence_complaint ();
21865
21866 /* We got a DW_LNE_end_sequence (or we ran off the end of the buffer,
21867 in which case we still finish recording the last line). */
6f77053d 21868 state_machine.record_line (true);
c906108c 21869 }
f3f5162e
DE
21870}
21871
21872/* Decode the Line Number Program (LNP) for the given line_header
21873 structure and CU. The actual information extracted and the type
21874 of structures created from the LNP depends on the value of PST.
21875
21876 1. If PST is NULL, then this procedure uses the data from the program
21877 to create all necessary symbol tables, and their linetables.
21878
21879 2. If PST is not NULL, this procedure reads the program to determine
21880 the list of files included by the unit represented by PST, and
21881 builds all the associated partial symbol tables.
21882
21883 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
21884 It is used for relative paths in the line table.
21885 NOTE: When processing partial symtabs (pst != NULL),
21886 comp_dir == pst->dirname.
21887
21888 NOTE: It is important that psymtabs have the same file name (via strcmp)
21889 as the corresponding symtab. Since COMP_DIR is not used in the name of the
21890 symtab we don't use it in the name of the psymtabs we create.
21891 E.g. expand_line_sal requires this when finding psymtabs to expand.
c3b7b696
YQ
21892 A good testcase for this is mb-inline.exp.
21893
527f3840
JK
21894 LOWPC is the lowest address in CU (or 0 if not known).
21895
21896 Boolean DECODE_MAPPING specifies we need to fully decode .debug_line
21897 for its PC<->lines mapping information. Otherwise only the filename
21898 table is read in. */
f3f5162e
DE
21899
21900static void
21901dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
891813be 21902 struct dwarf2_cu *cu, dwarf2_psymtab *pst,
527f3840 21903 CORE_ADDR lowpc, int decode_mapping)
f3f5162e 21904{
5e22e966 21905 struct objfile *objfile = cu->per_objfile->objfile;
f3f5162e 21906 const int decode_for_pst_p = (pst != NULL);
f3f5162e 21907
527f3840
JK
21908 if (decode_mapping)
21909 dwarf_decode_lines_1 (lh, cu, decode_for_pst_p, lowpc);
aaa75496
JB
21910
21911 if (decode_for_pst_p)
21912 {
aaa75496 21913 /* Now that we're done scanning the Line Header Program, we can
dda83cd7 21914 create the psymtab of each included file. */
7ba99d21 21915 for (auto &file_entry : lh->file_names ())
dda83cd7
SM
21916 if (file_entry.included_p == 1)
21917 {
c89b44cd 21918 gdb::unique_xmalloc_ptr<char> name_holder;
d521ce57 21919 const char *include_name =
7ba99d21
AT
21920 psymtab_include_file_name (lh, file_entry, pst,
21921 comp_dir, &name_holder);
c6da4cef 21922 if (include_name != NULL)
dda83cd7
SM
21923 dwarf2_create_include_psymtab (include_name, pst, objfile);
21924 }
aaa75496 21925 }
cb1df416
DJ
21926 else
21927 {
21928 /* Make sure a symtab is created for every file, even files
21929 which contain only variables (i.e. no code with associated
21930 line numbers). */
c24bdb02
KS
21931 buildsym_compunit *builder = cu->get_builder ();
21932 struct compunit_symtab *cust = builder->get_compunit_symtab ();
cb1df416 21933
7ba99d21 21934 for (auto &fe : lh->file_names ())
cb1df416 21935 {
804d2729 21936 dwarf2_start_subfile (cu, fe.name, fe.include_dir (lh));
c24bdb02 21937 if (builder->get_current_subfile ()->symtab == NULL)
43f3e411 21938 {
c24bdb02 21939 builder->get_current_subfile ()->symtab
804d2729 21940 = allocate_symtab (cust,
c24bdb02 21941 builder->get_current_subfile ()->name);
43f3e411 21942 }
c24bdb02 21943 fe.symtab = builder->get_current_subfile ()->symtab;
cb1df416
DJ
21944 }
21945 }
c906108c
SS
21946}
21947
21948/* Start a subfile for DWARF. FILENAME is the name of the file and
21949 DIRNAME the name of the source directory which contains FILENAME
4d663531 21950 or NULL if not known.
c906108c
SS
21951 This routine tries to keep line numbers from identical absolute and
21952 relative file names in a common subfile.
21953
21954 Using the `list' example from the GDB testsuite, which resides in
21955 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
21956 of /srcdir/list0.c yields the following debugging information for list0.c:
21957
c5aa993b 21958 DW_AT_name: /srcdir/list0.c
4d663531 21959 DW_AT_comp_dir: /compdir
357e46e7 21960 files.files[0].name: list0.h
c5aa993b 21961 files.files[0].dir: /srcdir
357e46e7 21962 files.files[1].name: list0.c
c5aa993b 21963 files.files[1].dir: /srcdir
c906108c
SS
21964
21965 The line number information for list0.c has to end up in a single
4f1520fb
FR
21966 subfile, so that `break /srcdir/list0.c:1' works as expected.
21967 start_subfile will ensure that this happens provided that we pass the
21968 concatenation of files.files[1].dir and files.files[1].name as the
21969 subfile's name. */
c906108c
SS
21970
21971static void
804d2729
TT
21972dwarf2_start_subfile (struct dwarf2_cu *cu, const char *filename,
21973 const char *dirname)
c906108c 21974{
43816ebc 21975 gdb::unique_xmalloc_ptr<char> copy;
4f1520fb 21976
4d663531 21977 /* In order not to lose the line information directory,
4f1520fb
FR
21978 we concatenate it to the filename when it makes sense.
21979 Note that the Dwarf3 standard says (speaking of filenames in line
21980 information): ``The directory index is ignored for file names
21981 that represent full path names''. Thus ignoring dirname in the
21982 `else' branch below isn't an issue. */
c906108c 21983
d5166ae1 21984 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
d521ce57 21985 {
43816ebc
TT
21986 copy.reset (concat (dirname, SLASH_STRING, filename, (char *) NULL));
21987 filename = copy.get ();
d521ce57 21988 }
c906108c 21989
c24bdb02 21990 cu->get_builder ()->start_subfile (filename);
c906108c
SS
21991}
21992
804d2729
TT
21993/* Start a symtab for DWARF. NAME, COMP_DIR, LOW_PC are passed to the
21994 buildsym_compunit constructor. */
f4dc4d17 21995
c24bdb02
KS
21996struct compunit_symtab *
21997dwarf2_cu::start_symtab (const char *name, const char *comp_dir,
21998 CORE_ADDR low_pc)
f4dc4d17 21999{
c24bdb02 22000 gdb_assert (m_builder == nullptr);
43f3e411 22001
c24bdb02 22002 m_builder.reset (new struct buildsym_compunit
f6e649dd 22003 (this->per_objfile->objfile,
c24bdb02 22004 name, comp_dir, language, low_pc));
93b8bea4 22005
c24bdb02 22006 list_in_scope = get_builder ()->get_file_symbols ();
804d2729 22007
c24bdb02
KS
22008 get_builder ()->record_debugformat ("DWARF 2");
22009 get_builder ()->record_producer (producer);
f4dc4d17 22010
c24bdb02 22011 processing_has_namespace_info = false;
43f3e411 22012
c24bdb02 22013 return get_builder ()->get_compunit_symtab ();
f4dc4d17
DE
22014}
22015
4c2df51b
DJ
22016static void
22017var_decode_location (struct attribute *attr, struct symbol *sym,
e7c27a73 22018 struct dwarf2_cu *cu)
4c2df51b 22019{
5e22e966 22020 struct objfile *objfile = cu->per_objfile->objfile;
e7c27a73
DJ
22021 struct comp_unit_head *cu_header = &cu->header;
22022
4c2df51b
DJ
22023 /* NOTE drow/2003-01-30: There used to be a comment and some special
22024 code here to turn a symbol with DW_AT_external and a
22025 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
22026 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
22027 with some versions of binutils) where shared libraries could have
22028 relocations against symbols in their debug information - the
22029 minimal symbol would have the right address, but the debug info
22030 would not. It's no longer necessary, because we will explicitly
22031 apply relocations when we read in the debug information now. */
22032
22033 /* A DW_AT_location attribute with no contents indicates that a
22034 variable has been optimized away. */
9d2246fc 22035 if (attr->form_is_block () && attr->as_block ()->size == 0)
4c2df51b 22036 {
f1e6e072 22037 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
4c2df51b
DJ
22038 return;
22039 }
22040
22041 /* Handle one degenerate form of location expression specially, to
22042 preserve GDB's previous behavior when section offsets are
336d760d
AT
22043 specified. If this is just a DW_OP_addr, DW_OP_addrx, or
22044 DW_OP_GNU_addr_index then mark this symbol as LOC_STATIC. */
4c2df51b 22045
9d2246fc
TT
22046 if (attr->form_is_block ())
22047 {
22048 struct dwarf_block *block = attr->as_block ();
22049
22050 if ((block->data[0] == DW_OP_addr
22051 && block->size == 1 + cu_header->addr_size)
22052 || ((block->data[0] == DW_OP_GNU_addr_index
dda83cd7 22053 || block->data[0] == DW_OP_addrx)
9d2246fc
TT
22054 && (block->size
22055 == 1 + leb128_size (&block->data[1]))))
22056 {
22057 unsigned int dummy;
22058
22059 if (block->data[0] == DW_OP_addr)
22060 SET_SYMBOL_VALUE_ADDRESS
22061 (sym, cu->header.read_address (objfile->obfd,
22062 block->data + 1,
38583298 22063 &dummy));
9d2246fc
TT
22064 else
22065 SET_SYMBOL_VALUE_ADDRESS
22066 (sym, read_addr_index_from_leb128 (cu, block->data + 1,
22067 &dummy));
22068 SYMBOL_ACLASS_INDEX (sym) = LOC_STATIC;
22069 fixup_symbol_section (sym, objfile);
22070 SET_SYMBOL_VALUE_ADDRESS
22071 (sym,
22072 SYMBOL_VALUE_ADDRESS (sym)
22073 + objfile->section_offsets[SYMBOL_SECTION (sym)]);
22074 return;
22075 }
4c2df51b
DJ
22076 }
22077
22078 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
22079 expression evaluator, and use LOC_COMPUTED only when necessary
22080 (i.e. when the value of a register or memory location is
22081 referenced, or a thread-local block, etc.). Then again, it might
22082 not be worthwhile. I'm assuming that it isn't unless performance
22083 or memory numbers show me otherwise. */
22084
f1e6e072 22085 dwarf2_symbol_mark_computed (attr, sym, cu, 0);
8be455d7 22086
f1e6e072 22087 if (SYMBOL_COMPUTED_OPS (sym)->location_has_loclist)
9068261f 22088 cu->has_loclist = true;
4c2df51b
DJ
22089}
22090
c906108c
SS
22091/* Given a pointer to a DWARF information entry, figure out if we need
22092 to make a symbol table entry for it, and if so, create a new entry
22093 and return a pointer to it.
22094 If TYPE is NULL, determine symbol type from the die, otherwise
34eaf542
TT
22095 used the passed type.
22096 If SPACE is not NULL, use it to hold the new symbol. If it is
22097 NULL, allocate a new symbol on the objfile's obstack. */
c906108c
SS
22098
22099static struct symbol *
5e2db402
TT
22100new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
22101 struct symbol *space)
c906108c 22102{
976ca316
SM
22103 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22104 struct objfile *objfile = per_objfile->objfile;
08feed99 22105 struct gdbarch *gdbarch = objfile->arch ();
c906108c 22106 struct symbol *sym = NULL;
15d034d0 22107 const char *name;
c906108c
SS
22108 struct attribute *attr = NULL;
22109 struct attribute *attr2 = NULL;
e142c38c 22110 CORE_ADDR baseaddr;
e37fd15a
SW
22111 struct pending **list_to_add = NULL;
22112
edb3359d 22113 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
e142c38c 22114
b3b3bada 22115 baseaddr = objfile->text_section_offset ();
c906108c 22116
94af9270 22117 name = dwarf2_name (die, cu);
c906108c
SS
22118 if (name)
22119 {
34eaf542 22120 int suppress_add = 0;
94af9270 22121
34eaf542
TT
22122 if (space)
22123 sym = space;
22124 else
8c14c3a3 22125 sym = new (&objfile->objfile_obstack) symbol;
c906108c 22126 OBJSTAT (objfile, n_syms++);
2de7ced7
DJ
22127
22128 /* Cache this symbol's name and the name's demangled form (if any). */
d3ecddab 22129 sym->set_language (cu->language, &objfile->objfile_obstack);
f55ee35c
JK
22130 /* Fortran does not have mangling standard and the mangling does differ
22131 between gfortran, iFort etc. */
bcfe6157
TT
22132 const char *physname
22133 = (cu->language == language_fortran
22134 ? dwarf2_full_name (name, die, cu)
22135 : dwarf2_physname (name, die, cu));
22136 const char *linkagename = dw2_linkage_name (die, cu);
22137
22138 if (linkagename == nullptr || cu->language == language_ada)
22139 sym->set_linkage_name (physname);
22140 else
22141 {
22142 sym->set_demangled_name (physname, &objfile->objfile_obstack);
22143 sym->set_linkage_name (linkagename);
22144 }
f55ee35c 22145
c906108c 22146 /* Default assumptions.
dda83cd7 22147 Use the passed type or decode it from the die. */
176620f1 22148 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
f1e6e072 22149 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
c906108c
SS
22150 if (type != NULL)
22151 SYMBOL_TYPE (sym) = type;
22152 else
e7c27a73 22153 SYMBOL_TYPE (sym) = die_type (die, cu);
edb3359d
DJ
22154 attr = dwarf2_attr (die,
22155 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
22156 cu);
435d3d88 22157 if (attr != nullptr)
529908cb 22158 SYMBOL_LINE (sym) = attr->constant_value (0);
cb1df416 22159
edb3359d
DJ
22160 attr = dwarf2_attr (die,
22161 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
22162 cu);
529908cb 22163 if (attr != nullptr && attr->form_is_unsigned ())
cb1df416 22164 {
529908cb
TT
22165 file_name_index file_index
22166 = (file_name_index) attr->as_unsigned ();
8c43009f 22167 struct file_entry *fe;
9a619af0 22168
ecfb656c
PA
22169 if (cu->line_header != NULL)
22170 fe = cu->line_header->file_name_at (file_index);
8c43009f
PA
22171 else
22172 fe = NULL;
22173
22174 if (fe == NULL)
b98664d3 22175 complaint (_("file index out of range"));
8c43009f
PA
22176 else
22177 symbol_set_symtab (sym, fe->symtab);
cb1df416
DJ
22178 }
22179
c906108c
SS
22180 switch (die->tag)
22181 {
22182 case DW_TAG_label:
e142c38c 22183 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
435d3d88 22184 if (attr != nullptr)
3e29f34a
MR
22185 {
22186 CORE_ADDR addr;
22187
95f982e5 22188 addr = attr->as_address ();
3e29f34a 22189 addr = gdbarch_adjust_dwarf2_addr (gdbarch, addr + baseaddr);
38583298 22190 SET_SYMBOL_VALUE_ADDRESS (sym, addr);
8f5c6526 22191 SYMBOL_ACLASS_INDEX (sym) = LOC_LABEL;
3e29f34a 22192 }
8f5c6526
TV
22193 else
22194 SYMBOL_ACLASS_INDEX (sym) = LOC_OPTIMIZED_OUT;
0f5238ed
TT
22195 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
22196 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
d3cb6808 22197 add_symbol_to_list (sym, cu->list_in_scope);
c906108c
SS
22198 break;
22199 case DW_TAG_subprogram:
22200 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22201 finish_block. */
f1e6e072 22202 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
e142c38c 22203 attr2 = dwarf2_attr (die, DW_AT_external, cu);
c45bc3f8 22204 if ((attr2 != nullptr && attr2->as_boolean ())
0a4b0913
AB
22205 || cu->language == language_ada
22206 || cu->language == language_fortran)
c906108c 22207 {
dda83cd7
SM
22208 /* Subprograms marked external are stored as a global symbol.
22209 Ada and Fortran subprograms, whether marked external or
22210 not, are always stored as a global symbol, because we want
22211 to be able to access them globally. For instance, we want
22212 to be able to break on a nested subprogram without having
22213 to specify the context. */
c24bdb02 22214 list_to_add = cu->get_builder ()->get_global_symbols ();
c906108c
SS
22215 }
22216 else
22217 {
e37fd15a 22218 list_to_add = cu->list_in_scope;
c906108c
SS
22219 }
22220 break;
edb3359d
DJ
22221 case DW_TAG_inlined_subroutine:
22222 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
22223 finish_block. */
f1e6e072 22224 SYMBOL_ACLASS_INDEX (sym) = LOC_BLOCK;
edb3359d 22225 SYMBOL_INLINED (sym) = 1;
481860b3 22226 list_to_add = cu->list_in_scope;
edb3359d 22227 break;
34eaf542
TT
22228 case DW_TAG_template_value_param:
22229 suppress_add = 1;
22230 /* Fall through. */
72929c62 22231 case DW_TAG_constant:
c906108c 22232 case DW_TAG_variable:
254e6b9e 22233 case DW_TAG_member:
0963b4bd
MS
22234 /* Compilation with minimal debug info may result in
22235 variables with missing type entries. Change the
22236 misleading `void' type to something sensible. */
78134374 22237 if (SYMBOL_TYPE (sym)->code () == TYPE_CODE_VOID)
46a4882b 22238 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_int;
64c50499 22239
e142c38c 22240 attr = dwarf2_attr (die, DW_AT_const_value, cu);
254e6b9e
DE
22241 /* In the case of DW_TAG_member, we should only be called for
22242 static const members. */
22243 if (die->tag == DW_TAG_member)
22244 {
3863f96c
DE
22245 /* dwarf2_add_field uses die_is_declaration,
22246 so we do the same. */
254e6b9e
DE
22247 gdb_assert (die_is_declaration (die, cu));
22248 gdb_assert (attr);
22249 }
435d3d88 22250 if (attr != nullptr)
c906108c 22251 {
e7c27a73 22252 dwarf2_const_value (attr, sym, cu);
e142c38c 22253 attr2 = dwarf2_attr (die, DW_AT_external, cu);
e37fd15a 22254 if (!suppress_add)
34eaf542 22255 {
c45bc3f8 22256 if (attr2 != nullptr && attr2->as_boolean ())
c24bdb02 22257 list_to_add = cu->get_builder ()->get_global_symbols ();
34eaf542 22258 else
e37fd15a 22259 list_to_add = cu->list_in_scope;
34eaf542 22260 }
c906108c
SS
22261 break;
22262 }
e142c38c 22263 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22264 if (attr != nullptr)
c906108c 22265 {
e7c27a73 22266 var_decode_location (attr, sym, cu);
e142c38c 22267 attr2 = dwarf2_attr (die, DW_AT_external, cu);
4357ac6c
TT
22268
22269 /* Fortran explicitly imports any global symbols to the local
22270 scope by DW_TAG_common_block. */
22271 if (cu->language == language_fortran && die->parent
22272 && die->parent->tag == DW_TAG_common_block)
22273 attr2 = NULL;
22274
caac4577
JG
22275 if (SYMBOL_CLASS (sym) == LOC_STATIC
22276 && SYMBOL_VALUE_ADDRESS (sym) == 0
976ca316 22277 && !per_objfile->per_bfd->has_section_at_zero)
caac4577
JG
22278 {
22279 /* When a static variable is eliminated by the linker,
22280 the corresponding debug information is not stripped
22281 out, but the variable address is set to null;
22282 do not add such variables into symbol table. */
22283 }
c45bc3f8 22284 else if (attr2 != nullptr && attr2->as_boolean ())
1c809c68 22285 {
4b610737
TT
22286 if (SYMBOL_CLASS (sym) == LOC_STATIC
22287 && (objfile->flags & OBJF_MAINLINE) == 0
976ca316 22288 && per_objfile->per_bfd->can_copy)
4b610737
TT
22289 {
22290 /* A global static variable might be subject to
22291 copy relocation. We first check for a local
22292 minsym, though, because maybe the symbol was
22293 marked hidden, in which case this would not
22294 apply. */
22295 bound_minimal_symbol found
22296 = (lookup_minimal_symbol_linkage
987012b8 22297 (sym->linkage_name (), objfile));
4b610737
TT
22298 if (found.minsym != nullptr)
22299 sym->maybe_copied = 1;
22300 }
f55ee35c 22301
1c809c68
TT
22302 /* A variable with DW_AT_external is never static,
22303 but it may be block-scoped. */
804d2729 22304 list_to_add
c24bdb02
KS
22305 = ((cu->list_in_scope
22306 == cu->get_builder ()->get_file_symbols ())
22307 ? cu->get_builder ()->get_global_symbols ()
804d2729 22308 : cu->list_in_scope);
1c809c68 22309 }
c906108c 22310 else
e37fd15a 22311 list_to_add = cu->list_in_scope;
c906108c
SS
22312 }
22313 else
22314 {
22315 /* We do not know the address of this symbol.
dda83cd7
SM
22316 If it is an external symbol and we have type information
22317 for it, enter the symbol as a LOC_UNRESOLVED symbol.
22318 The address of the variable will then be determined from
22319 the minimal symbol table whenever the variable is
22320 referenced. */
e142c38c 22321 attr2 = dwarf2_attr (die, DW_AT_external, cu);
0971de02
TT
22322
22323 /* Fortran explicitly imports any global symbols to the local
22324 scope by DW_TAG_common_block. */
22325 if (cu->language == language_fortran && die->parent
22326 && die->parent->tag == DW_TAG_common_block)
22327 {
22328 /* SYMBOL_CLASS doesn't matter here because
22329 read_common_block is going to reset it. */
22330 if (!suppress_add)
22331 list_to_add = cu->list_in_scope;
22332 }
c45bc3f8 22333 else if (attr2 != nullptr && attr2->as_boolean ()
0971de02 22334 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
c906108c 22335 {
0fe7935b
DJ
22336 /* A variable with DW_AT_external is never static, but it
22337 may be block-scoped. */
804d2729 22338 list_to_add
c24bdb02
KS
22339 = ((cu->list_in_scope
22340 == cu->get_builder ()->get_file_symbols ())
22341 ? cu->get_builder ()->get_global_symbols ()
804d2729 22342 : cu->list_in_scope);
0fe7935b 22343
f1e6e072 22344 SYMBOL_ACLASS_INDEX (sym) = LOC_UNRESOLVED;
c906108c 22345 }
442ddf59
JK
22346 else if (!die_is_declaration (die, cu))
22347 {
22348 /* Use the default LOC_OPTIMIZED_OUT class. */
22349 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
e37fd15a
SW
22350 if (!suppress_add)
22351 list_to_add = cu->list_in_scope;
442ddf59 22352 }
c906108c
SS
22353 }
22354 break;
22355 case DW_TAG_formal_parameter:
a60f3166
TT
22356 {
22357 /* If we are inside a function, mark this as an argument. If
22358 not, we might be looking at an argument to an inlined function
22359 when we do not have enough information to show inlined frames;
22360 pretend it's a local variable in that case so that the user can
22361 still see it. */
804d2729 22362 struct context_stack *curr
c24bdb02 22363 = cu->get_builder ()->get_current_context_stack ();
a60f3166
TT
22364 if (curr != nullptr && curr->name != nullptr)
22365 SYMBOL_IS_ARGUMENT (sym) = 1;
22366 attr = dwarf2_attr (die, DW_AT_location, cu);
435d3d88 22367 if (attr != nullptr)
a60f3166
TT
22368 {
22369 var_decode_location (attr, sym, cu);
22370 }
22371 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22372 if (attr != nullptr)
a60f3166
TT
22373 {
22374 dwarf2_const_value (attr, sym, cu);
22375 }
f346a30d 22376
a60f3166
TT
22377 list_to_add = cu->list_in_scope;
22378 }
c906108c
SS
22379 break;
22380 case DW_TAG_unspecified_parameters:
22381 /* From varargs functions; gdb doesn't seem to have any
22382 interest in this information, so just ignore it for now.
22383 (FIXME?) */
22384 break;
34eaf542
TT
22385 case DW_TAG_template_type_param:
22386 suppress_add = 1;
22387 /* Fall through. */
c906108c 22388 case DW_TAG_class_type:
680b30c7 22389 case DW_TAG_interface_type:
c906108c
SS
22390 case DW_TAG_structure_type:
22391 case DW_TAG_union_type:
72019c9c 22392 case DW_TAG_set_type:
c906108c 22393 case DW_TAG_enumeration_type:
f1e6e072 22394 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22395 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
c906108c 22396
63d06c5c 22397 {
9c37b5ae 22398 /* NOTE: carlton/2003-11-10: C++ class symbols shouldn't
63d06c5c
DC
22399 really ever be static objects: otherwise, if you try
22400 to, say, break of a class's method and you're in a file
22401 which doesn't mention that class, it won't work unless
22402 the check for all static symbols in lookup_symbol_aux
22403 saves you. See the OtherFileClass tests in
22404 gdb.c++/namespace.exp. */
22405
e37fd15a 22406 if (!suppress_add)
34eaf542 22407 {
c24bdb02 22408 buildsym_compunit *builder = cu->get_builder ();
804d2729 22409 list_to_add
c24bdb02 22410 = (cu->list_in_scope == builder->get_file_symbols ()
804d2729 22411 && cu->language == language_cplus
c24bdb02 22412 ? builder->get_global_symbols ()
804d2729 22413 : cu->list_in_scope);
63d06c5c 22414
64382290 22415 /* The semantics of C++ state that "struct foo {
9c37b5ae 22416 ... }" also defines a typedef for "foo". */
64382290 22417 if (cu->language == language_cplus
45280282 22418 || cu->language == language_ada
c44af4eb
TT
22419 || cu->language == language_d
22420 || cu->language == language_rust)
64382290
TT
22421 {
22422 /* The symbol's name is already allocated along
22423 with this objfile, so we don't need to
22424 duplicate it for the type. */
7d93a1e0 22425 if (SYMBOL_TYPE (sym)->name () == 0)
d0e39ea2 22426 SYMBOL_TYPE (sym)->set_name (sym->search_name ());
64382290 22427 }
63d06c5c
DC
22428 }
22429 }
c906108c
SS
22430 break;
22431 case DW_TAG_typedef:
f1e6e072 22432 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
63d06c5c 22433 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22434 list_to_add = cu->list_in_scope;
63d06c5c 22435 break;
d8f62e84 22436 case DW_TAG_array_type:
c906108c 22437 case DW_TAG_base_type:
dda83cd7 22438 case DW_TAG_subrange_type:
f1e6e072 22439 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
176620f1 22440 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
e37fd15a 22441 list_to_add = cu->list_in_scope;
c906108c
SS
22442 break;
22443 case DW_TAG_enumerator:
e142c38c 22444 attr = dwarf2_attr (die, DW_AT_const_value, cu);
435d3d88 22445 if (attr != nullptr)
c906108c 22446 {
e7c27a73 22447 dwarf2_const_value (attr, sym, cu);
c906108c 22448 }
63d06c5c
DC
22449 {
22450 /* NOTE: carlton/2003-11-10: See comment above in the
22451 DW_TAG_class_type, etc. block. */
22452
804d2729 22453 list_to_add
c24bdb02 22454 = (cu->list_in_scope == cu->get_builder ()->get_file_symbols ()
804d2729 22455 && cu->language == language_cplus
c24bdb02 22456 ? cu->get_builder ()->get_global_symbols ()
804d2729 22457 : cu->list_in_scope);
63d06c5c 22458 }
c906108c 22459 break;
74921315 22460 case DW_TAG_imported_declaration:
5c4e30ca 22461 case DW_TAG_namespace:
f1e6e072 22462 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
c24bdb02 22463 list_to_add = cu->get_builder ()->get_global_symbols ();
5c4e30ca 22464 break;
530e8392
KB
22465 case DW_TAG_module:
22466 SYMBOL_ACLASS_INDEX (sym) = LOC_TYPEDEF;
22467 SYMBOL_DOMAIN (sym) = MODULE_DOMAIN;
c24bdb02 22468 list_to_add = cu->get_builder ()->get_global_symbols ();
530e8392 22469 break;
4357ac6c 22470 case DW_TAG_common_block:
f1e6e072 22471 SYMBOL_ACLASS_INDEX (sym) = LOC_COMMON_BLOCK;
4357ac6c 22472 SYMBOL_DOMAIN (sym) = COMMON_BLOCK_DOMAIN;
d3cb6808 22473 add_symbol_to_list (sym, cu->list_in_scope);
4357ac6c 22474 break;
c906108c
SS
22475 default:
22476 /* Not a tag we recognize. Hopefully we aren't processing
22477 trash data, but since we must specifically ignore things
22478 we don't recognize, there is nothing else we should do at
0963b4bd 22479 this point. */
b98664d3 22480 complaint (_("unsupported tag: '%s'"),
4d3c2250 22481 dwarf_tag_name (die->tag));
c906108c
SS
22482 break;
22483 }
df8a16a1 22484
e37fd15a
SW
22485 if (suppress_add)
22486 {
22487 sym->hash_next = objfile->template_symbols;
22488 objfile->template_symbols = sym;
22489 list_to_add = NULL;
22490 }
22491
22492 if (list_to_add != NULL)
d3cb6808 22493 add_symbol_to_list (sym, list_to_add);
e37fd15a 22494
df8a16a1
DJ
22495 /* For the benefit of old versions of GCC, check for anonymous
22496 namespaces based on the demangled name. */
4d4ec4e5 22497 if (!cu->processing_has_namespace_info
94af9270 22498 && cu->language == language_cplus)
c24bdb02 22499 cp_scan_for_anonymous_namespaces (cu->get_builder (), sym, objfile);
c906108c
SS
22500 }
22501 return (sym);
22502}
22503
98bfdba5
PA
22504/* Given an attr with a DW_FORM_dataN value in host byte order,
22505 zero-extend it as appropriate for the symbol's type. The DWARF
22506 standard (v4) is not entirely clear about the meaning of using
22507 DW_FORM_dataN for a constant with a signed type, where the type is
22508 wider than the data. The conclusion of a discussion on the DWARF
22509 list was that this is unspecified. We choose to always zero-extend
22510 because that is the interpretation long in use by GCC. */
c906108c 22511
98bfdba5 22512static gdb_byte *
ff39bb5e 22513dwarf2_const_value_data (const struct attribute *attr, struct obstack *obstack,
12df843f 22514 struct dwarf2_cu *cu, LONGEST *value, int bits)
c906108c 22515{
5e22e966 22516 struct objfile *objfile = cu->per_objfile->objfile;
e17a4113
UW
22517 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
22518 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
529908cb 22519 LONGEST l = attr->constant_value (0);
98bfdba5
PA
22520
22521 if (bits < sizeof (*value) * 8)
22522 {
22523 l &= ((LONGEST) 1 << bits) - 1;
22524 *value = l;
22525 }
22526 else if (bits == sizeof (*value) * 8)
22527 *value = l;
22528 else
22529 {
224c3ddb 22530 gdb_byte *bytes = (gdb_byte *) obstack_alloc (obstack, bits / 8);
98bfdba5
PA
22531 store_unsigned_integer (bytes, bits / 8, byte_order, l);
22532 return bytes;
22533 }
22534
22535 return NULL;
22536}
22537
22538/* Read a constant value from an attribute. Either set *VALUE, or if
22539 the value does not fit in *VALUE, set *BYTES - either already
22540 allocated on the objfile obstack, or newly allocated on OBSTACK,
22541 or, set *BATON, if we translated the constant to a location
22542 expression. */
22543
22544static void
ff39bb5e 22545dwarf2_const_value_attr (const struct attribute *attr, struct type *type,
98bfdba5
PA
22546 const char *name, struct obstack *obstack,
22547 struct dwarf2_cu *cu,
d521ce57 22548 LONGEST *value, const gdb_byte **bytes,
98bfdba5
PA
22549 struct dwarf2_locexpr_baton **baton)
22550{
5e22e966 22551 dwarf2_per_objfile *per_objfile = cu->per_objfile;
a50264ba 22552 struct objfile *objfile = per_objfile->objfile;
98bfdba5 22553 struct comp_unit_head *cu_header = &cu->header;
c906108c 22554 struct dwarf_block *blk;
98bfdba5
PA
22555 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
22556 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
22557
22558 *value = 0;
22559 *bytes = NULL;
22560 *baton = NULL;
c906108c
SS
22561
22562 switch (attr->form)
22563 {
22564 case DW_FORM_addr:
336d760d 22565 case DW_FORM_addrx:
3019eac3 22566 case DW_FORM_GNU_addr_index:
ac56253d 22567 {
ac56253d
TT
22568 gdb_byte *data;
22569
98bfdba5
PA
22570 if (TYPE_LENGTH (type) != cu_header->addr_size)
22571 dwarf2_const_value_length_mismatch_complaint (name,
ac56253d 22572 cu_header->addr_size,
98bfdba5 22573 TYPE_LENGTH (type));
ac56253d
TT
22574 /* Symbols of this form are reasonably rare, so we just
22575 piggyback on the existing location code rather than writing
22576 a new implementation of symbol_computed_ops. */
8d749320 22577 *baton = XOBNEW (obstack, struct dwarf2_locexpr_baton);
a50264ba 22578 (*baton)->per_objfile = per_objfile;
98bfdba5
PA
22579 (*baton)->per_cu = cu->per_cu;
22580 gdb_assert ((*baton)->per_cu);
ac56253d 22581
98bfdba5 22582 (*baton)->size = 2 + cu_header->addr_size;
224c3ddb 22583 data = (gdb_byte *) obstack_alloc (obstack, (*baton)->size);
98bfdba5 22584 (*baton)->data = data;
ac56253d
TT
22585
22586 data[0] = DW_OP_addr;
22587 store_unsigned_integer (&data[1], cu_header->addr_size,
36d378cf 22588 byte_order, attr->as_address ());
ac56253d 22589 data[cu_header->addr_size + 1] = DW_OP_stack_value;
ac56253d 22590 }
c906108c 22591 break;
4ac36638 22592 case DW_FORM_string:
93b5768b 22593 case DW_FORM_strp:
cf532bd1 22594 case DW_FORM_strx:
3019eac3 22595 case DW_FORM_GNU_str_index:
36586728 22596 case DW_FORM_GNU_strp_alt:
c6481205 22597 /* The string is already allocated on the objfile obstack, point
98bfdba5 22598 directly to it. */
2c830f54 22599 *bytes = (const gdb_byte *) attr->as_string ();
93b5768b 22600 break;
c906108c
SS
22601 case DW_FORM_block1:
22602 case DW_FORM_block2:
22603 case DW_FORM_block4:
22604 case DW_FORM_block:
2dc7f7b3 22605 case DW_FORM_exprloc:
0224619f 22606 case DW_FORM_data16:
9d2246fc 22607 blk = attr->as_block ();
98bfdba5
PA
22608 if (TYPE_LENGTH (type) != blk->size)
22609 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
22610 TYPE_LENGTH (type));
22611 *bytes = blk->data;
c906108c 22612 break;
2df3850c
JM
22613
22614 /* The DW_AT_const_value attributes are supposed to carry the
22615 symbol's value "represented as it would be on the target
22616 architecture." By the time we get here, it's already been
22617 converted to host endianness, so we just need to sign- or
22618 zero-extend it as appropriate. */
22619 case DW_FORM_data1:
3aef2284 22620 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 8);
2df3850c 22621 break;
c906108c 22622 case DW_FORM_data2:
3aef2284 22623 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 16);
2df3850c 22624 break;
c906108c 22625 case DW_FORM_data4:
3aef2284 22626 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 32);
2df3850c 22627 break;
c906108c 22628 case DW_FORM_data8:
3aef2284 22629 *bytes = dwarf2_const_value_data (attr, obstack, cu, value, 64);
2df3850c
JM
22630 break;
22631
c906108c 22632 case DW_FORM_sdata:
663c44ac 22633 case DW_FORM_implicit_const:
1bc397c5 22634 *value = attr->as_signed ();
2df3850c
JM
22635 break;
22636
c906108c 22637 case DW_FORM_udata:
529908cb 22638 *value = attr->as_unsigned ();
c906108c 22639 break;
2df3850c 22640
c906108c 22641 default:
b98664d3 22642 complaint (_("unsupported const value attribute form: '%s'"),
4d3c2250 22643 dwarf_form_name (attr->form));
98bfdba5 22644 *value = 0;
c906108c
SS
22645 break;
22646 }
22647}
22648
2df3850c 22649
98bfdba5
PA
22650/* Copy constant value from an attribute to a symbol. */
22651
2df3850c 22652static void
ff39bb5e 22653dwarf2_const_value (const struct attribute *attr, struct symbol *sym,
98bfdba5 22654 struct dwarf2_cu *cu)
2df3850c 22655{
5e22e966 22656 struct objfile *objfile = cu->per_objfile->objfile;
12df843f 22657 LONGEST value;
d521ce57 22658 const gdb_byte *bytes;
98bfdba5 22659 struct dwarf2_locexpr_baton *baton;
2df3850c 22660
98bfdba5 22661 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
987012b8 22662 sym->print_name (),
98bfdba5
PA
22663 &objfile->objfile_obstack, cu,
22664 &value, &bytes, &baton);
2df3850c 22665
98bfdba5
PA
22666 if (baton != NULL)
22667 {
98bfdba5 22668 SYMBOL_LOCATION_BATON (sym) = baton;
f1e6e072 22669 SYMBOL_ACLASS_INDEX (sym) = dwarf2_locexpr_index;
98bfdba5
PA
22670 }
22671 else if (bytes != NULL)
22672 {
22673 SYMBOL_VALUE_BYTES (sym) = bytes;
f1e6e072 22674 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST_BYTES;
98bfdba5
PA
22675 }
22676 else
22677 {
22678 SYMBOL_VALUE (sym) = value;
f1e6e072 22679 SYMBOL_ACLASS_INDEX (sym) = LOC_CONST;
98bfdba5 22680 }
2df3850c
JM
22681}
22682
c906108c
SS
22683/* Return the type of the die in question using its DW_AT_type attribute. */
22684
22685static struct type *
e7c27a73 22686die_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22687{
c906108c 22688 struct attribute *type_attr;
c906108c 22689
e142c38c 22690 type_attr = dwarf2_attr (die, DW_AT_type, cu);
c906108c
SS
22691 if (!type_attr)
22692 {
5e22e966 22693 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22694 /* A missing DW_AT_type represents a void type. */
518817b3 22695 return objfile_type (objfile)->builtin_void;
c906108c 22696 }
348e048f 22697
673bfd45 22698 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22699}
22700
b4ba55a1
JB
22701/* True iff CU's producer generates GNAT Ada auxiliary information
22702 that allows to find parallel types through that information instead
22703 of having to do expensive parallel lookups by type name. */
22704
22705static int
22706need_gnat_info (struct dwarf2_cu *cu)
22707{
de4cb04a
JB
22708 /* Assume that the Ada compiler was GNAT, which always produces
22709 the auxiliary information. */
22710 return (cu->language == language_ada);
b4ba55a1
JB
22711}
22712
b4ba55a1
JB
22713/* Return the auxiliary type of the die in question using its
22714 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
22715 attribute is not present. */
22716
22717static struct type *
22718die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
22719{
b4ba55a1 22720 struct attribute *type_attr;
b4ba55a1
JB
22721
22722 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
22723 if (!type_attr)
22724 return NULL;
22725
673bfd45 22726 return lookup_die_type (die, type_attr, cu);
b4ba55a1
JB
22727}
22728
22729/* If DIE has a descriptive_type attribute, then set the TYPE's
22730 descriptive type accordingly. */
22731
22732static void
22733set_descriptive_type (struct type *type, struct die_info *die,
22734 struct dwarf2_cu *cu)
22735{
22736 struct type *descriptive_type = die_descriptive_type (die, cu);
22737
22738 if (descriptive_type)
22739 {
22740 ALLOCATE_GNAT_AUX_TYPE (type);
22741 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
22742 }
22743}
22744
c906108c
SS
22745/* Return the containing type of the die in question using its
22746 DW_AT_containing_type attribute. */
22747
22748static struct type *
e7c27a73 22749die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22750{
c906108c 22751 struct attribute *type_attr;
5e22e966 22752 struct objfile *objfile = cu->per_objfile->objfile;
c906108c 22753
e142c38c 22754 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
33ac96f0
JK
22755 if (!type_attr)
22756 error (_("Dwarf Error: Problem turning containing type into gdb type "
518817b3 22757 "[in module %s]"), objfile_name (objfile));
33ac96f0 22758
673bfd45 22759 return lookup_die_type (die, type_attr, cu);
c906108c
SS
22760}
22761
ac9ec31b
DE
22762/* Return an error marker type to use for the ill formed type in DIE/CU. */
22763
22764static struct type *
22765build_error_marker_type (struct dwarf2_cu *cu, struct die_info *die)
22766{
976ca316
SM
22767 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22768 struct objfile *objfile = per_objfile->objfile;
528e1572 22769 char *saved;
ac9ec31b 22770
528e1572
SM
22771 std::string message
22772 = string_printf (_("<unknown type in %s, CU %s, DIE %s>"),
22773 objfile_name (objfile),
22774 sect_offset_str (cu->header.sect_off),
22775 sect_offset_str (die->sect_off));
efba19b0 22776 saved = obstack_strdup (&objfile->objfile_obstack, message);
ac9ec31b 22777
19f392bc 22778 return init_type (objfile, TYPE_CODE_ERROR, 0, saved);
ac9ec31b
DE
22779}
22780
673bfd45 22781/* Look up the type of DIE in CU using its type attribute ATTR.
ac9ec31b
DE
22782 ATTR must be one of: DW_AT_type, DW_AT_GNAT_descriptive_type,
22783 DW_AT_containing_type.
673bfd45
DE
22784 If there is no type substitute an error marker. */
22785
c906108c 22786static struct type *
ff39bb5e 22787lookup_die_type (struct die_info *die, const struct attribute *attr,
673bfd45 22788 struct dwarf2_cu *cu)
c906108c 22789{
976ca316
SM
22790 dwarf2_per_objfile *per_objfile = cu->per_objfile;
22791 struct objfile *objfile = per_objfile->objfile;
f792889a
DJ
22792 struct type *this_type;
22793
ac9ec31b
DE
22794 gdb_assert (attr->name == DW_AT_type
22795 || attr->name == DW_AT_GNAT_descriptive_type
22796 || attr->name == DW_AT_containing_type);
22797
673bfd45
DE
22798 /* First see if we have it cached. */
22799
36586728
TT
22800 if (attr->form == DW_FORM_GNU_ref_alt)
22801 {
22802 struct dwarf2_per_cu_data *per_cu;
0826b30a 22803 sect_offset sect_off = attr->get_ref_die_offset ();
36586728 22804
976ca316
SM
22805 per_cu = dwarf2_find_containing_comp_unit (sect_off, 1, per_objfile);
22806 this_type = get_die_type_at_offset (sect_off, per_cu, per_objfile);
36586728 22807 }
cd6c91b4 22808 else if (attr->form_is_ref ())
673bfd45 22809 {
0826b30a 22810 sect_offset sect_off = attr->get_ref_die_offset ();
673bfd45 22811
976ca316 22812 this_type = get_die_type_at_offset (sect_off, cu->per_cu, per_objfile);
673bfd45 22813 }
55f1336d 22814 else if (attr->form == DW_FORM_ref_sig8)
673bfd45 22815 {
630ed6b9 22816 ULONGEST signature = attr->as_signature ();
673bfd45 22817
ac9ec31b 22818 return get_signatured_type (die, signature, cu);
673bfd45
DE
22819 }
22820 else
22821 {
b98664d3 22822 complaint (_("Dwarf Error: Bad type attribute %s in DIE"
9d8780f0
SM
22823 " at %s [in module %s]"),
22824 dwarf_attr_name (attr->name), sect_offset_str (die->sect_off),
4262abfb 22825 objfile_name (objfile));
ac9ec31b 22826 return build_error_marker_type (cu, die);
673bfd45
DE
22827 }
22828
22829 /* If not cached we need to read it in. */
22830
22831 if (this_type == NULL)
22832 {
ac9ec31b 22833 struct die_info *type_die = NULL;
673bfd45
DE
22834 struct dwarf2_cu *type_cu = cu;
22835
cd6c91b4 22836 if (attr->form_is_ref ())
ac9ec31b
DE
22837 type_die = follow_die_ref (die, attr, &type_cu);
22838 if (type_die == NULL)
22839 return build_error_marker_type (cu, die);
22840 /* If we find the type now, it's probably because the type came
3019eac3
DE
22841 from an inter-CU reference and the type's CU got expanded before
22842 ours. */
ac9ec31b 22843 this_type = read_type_die (type_die, type_cu);
673bfd45
DE
22844 }
22845
22846 /* If we still don't have a type use an error marker. */
22847
22848 if (this_type == NULL)
ac9ec31b 22849 return build_error_marker_type (cu, die);
673bfd45 22850
f792889a 22851 return this_type;
c906108c
SS
22852}
22853
673bfd45
DE
22854/* Return the type in DIE, CU.
22855 Returns NULL for invalid types.
22856
02142a6c 22857 This first does a lookup in die_type_hash,
673bfd45
DE
22858 and only reads the die in if necessary.
22859
22860 NOTE: This can be called when reading in partial or full symbols. */
22861
f792889a 22862static struct type *
e7c27a73 22863read_type_die (struct die_info *die, struct dwarf2_cu *cu)
c906108c 22864{
f792889a
DJ
22865 struct type *this_type;
22866
22867 this_type = get_die_type (die, cu);
22868 if (this_type)
22869 return this_type;
22870
673bfd45
DE
22871 return read_type_die_1 (die, cu);
22872}
22873
22874/* Read the type in DIE, CU.
22875 Returns NULL for invalid types. */
22876
22877static struct type *
22878read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
22879{
22880 struct type *this_type = NULL;
22881
c906108c
SS
22882 switch (die->tag)
22883 {
22884 case DW_TAG_class_type:
680b30c7 22885 case DW_TAG_interface_type:
c906108c
SS
22886 case DW_TAG_structure_type:
22887 case DW_TAG_union_type:
f792889a 22888 this_type = read_structure_type (die, cu);
c906108c
SS
22889 break;
22890 case DW_TAG_enumeration_type:
f792889a 22891 this_type = read_enumeration_type (die, cu);
c906108c
SS
22892 break;
22893 case DW_TAG_subprogram:
22894 case DW_TAG_subroutine_type:
edb3359d 22895 case DW_TAG_inlined_subroutine:
f792889a 22896 this_type = read_subroutine_type (die, cu);
c906108c
SS
22897 break;
22898 case DW_TAG_array_type:
f792889a 22899 this_type = read_array_type (die, cu);
c906108c 22900 break;
72019c9c 22901 case DW_TAG_set_type:
f792889a 22902 this_type = read_set_type (die, cu);
72019c9c 22903 break;
c906108c 22904 case DW_TAG_pointer_type:
f792889a 22905 this_type = read_tag_pointer_type (die, cu);
c906108c
SS
22906 break;
22907 case DW_TAG_ptr_to_member_type:
f792889a 22908 this_type = read_tag_ptr_to_member_type (die, cu);
c906108c
SS
22909 break;
22910 case DW_TAG_reference_type:
4297a3f0
AV
22911 this_type = read_tag_reference_type (die, cu, TYPE_CODE_REF);
22912 break;
22913 case DW_TAG_rvalue_reference_type:
22914 this_type = read_tag_reference_type (die, cu, TYPE_CODE_RVALUE_REF);
c906108c
SS
22915 break;
22916 case DW_TAG_const_type:
f792889a 22917 this_type = read_tag_const_type (die, cu);
c906108c
SS
22918 break;
22919 case DW_TAG_volatile_type:
f792889a 22920 this_type = read_tag_volatile_type (die, cu);
c906108c 22921 break;
06d66ee9
TT
22922 case DW_TAG_restrict_type:
22923 this_type = read_tag_restrict_type (die, cu);
22924 break;
c906108c 22925 case DW_TAG_string_type:
f792889a 22926 this_type = read_tag_string_type (die, cu);
c906108c
SS
22927 break;
22928 case DW_TAG_typedef:
f792889a 22929 this_type = read_typedef (die, cu);
c906108c 22930 break;
a02abb62 22931 case DW_TAG_subrange_type:
f792889a 22932 this_type = read_subrange_type (die, cu);
a02abb62 22933 break;
c906108c 22934 case DW_TAG_base_type:
f792889a 22935 this_type = read_base_type (die, cu);
c906108c 22936 break;
81a17f79 22937 case DW_TAG_unspecified_type:
f792889a 22938 this_type = read_unspecified_type (die, cu);
81a17f79 22939 break;
0114d602
DJ
22940 case DW_TAG_namespace:
22941 this_type = read_namespace_type (die, cu);
22942 break;
f55ee35c
JK
22943 case DW_TAG_module:
22944 this_type = read_module_type (die, cu);
22945 break;
a2c2acaf
MW
22946 case DW_TAG_atomic_type:
22947 this_type = read_tag_atomic_type (die, cu);
22948 break;
c906108c 22949 default:
b98664d3 22950 complaint (_("unexpected tag in read_type_die: '%s'"),
4d3c2250 22951 dwarf_tag_name (die->tag));
c906108c
SS
22952 break;
22953 }
63d06c5c 22954
f792889a 22955 return this_type;
63d06c5c
DC
22956}
22957
abc72ce4
DE
22958/* See if we can figure out if the class lives in a namespace. We do
22959 this by looking for a member function; its demangled name will
22960 contain namespace info, if there is any.
22961 Return the computed name or NULL.
22962 Space for the result is allocated on the objfile's obstack.
22963 This is the full-die version of guess_partial_die_structure_name.
22964 In this case we know DIE has no useful parent. */
22965
43816ebc 22966static const char *
abc72ce4
DE
22967guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
22968{
22969 struct die_info *spec_die;
22970 struct dwarf2_cu *spec_cu;
22971 struct die_info *child;
5e22e966 22972 struct objfile *objfile = cu->per_objfile->objfile;
abc72ce4
DE
22973
22974 spec_cu = cu;
22975 spec_die = die_specification (die, &spec_cu);
22976 if (spec_die != NULL)
22977 {
22978 die = spec_die;
22979 cu = spec_cu;
22980 }
22981
22982 for (child = die->child;
22983 child != NULL;
22984 child = child->sibling)
22985 {
22986 if (child->tag == DW_TAG_subprogram)
22987 {
73b9be8b 22988 const char *linkage_name = dw2_linkage_name (child, cu);
abc72ce4 22989
7d45c7c3 22990 if (linkage_name != NULL)
abc72ce4 22991 {
43816ebc 22992 gdb::unique_xmalloc_ptr<char> actual_name
eff93b4d 22993 (cu->language_defn->class_name_from_physname (linkage_name));
43816ebc 22994 const char *name = NULL;
abc72ce4
DE
22995
22996 if (actual_name != NULL)
22997 {
15d034d0 22998 const char *die_name = dwarf2_name (die, cu);
abc72ce4
DE
22999
23000 if (die_name != NULL
43816ebc 23001 && strcmp (die_name, actual_name.get ()) != 0)
abc72ce4
DE
23002 {
23003 /* Strip off the class name from the full name.
23004 We want the prefix. */
23005 int die_name_len = strlen (die_name);
43816ebc
TT
23006 int actual_name_len = strlen (actual_name.get ());
23007 const char *ptr = actual_name.get ();
abc72ce4
DE
23008
23009 /* Test for '::' as a sanity check. */
23010 if (actual_name_len > die_name_len + 2
43816ebc 23011 && ptr[actual_name_len - die_name_len - 1] == ':')
0cf9feb9 23012 name = obstack_strndup (
e3b94546 23013 &objfile->per_bfd->storage_obstack,
43816ebc 23014 ptr, actual_name_len - die_name_len - 2);
abc72ce4
DE
23015 }
23016 }
abc72ce4
DE
23017 return name;
23018 }
23019 }
23020 }
23021
23022 return NULL;
23023}
23024
96408a79
SA
23025/* GCC might emit a nameless typedef that has a linkage name. Determine the
23026 prefix part in such case. See
23027 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
23028
a121b7c1 23029static const char *
96408a79
SA
23030anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
23031{
23032 struct attribute *attr;
e6a959d6 23033 const char *base;
96408a79
SA
23034
23035 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
23036 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
23037 return NULL;
23038
7d45c7c3 23039 if (dwarf2_string_attr (die, DW_AT_name, cu) != NULL)
96408a79
SA
23040 return NULL;
23041
73b9be8b 23042 attr = dw2_linkage_name_attr (die, cu);
2c830f54
TT
23043 const char *attr_name = attr->as_string ();
23044 if (attr == NULL || attr_name == NULL)
96408a79
SA
23045 return NULL;
23046
23047 /* dwarf2_name had to be already called. */
3b64bf15 23048 gdb_assert (attr->canonical_string_p ());
96408a79
SA
23049
23050 /* Strip the base name, keep any leading namespaces/classes. */
2c830f54
TT
23051 base = strrchr (attr_name, ':');
23052 if (base == NULL || base == attr_name || base[-1] != ':')
96408a79
SA
23053 return "";
23054
5e22e966 23055 struct objfile *objfile = cu->per_objfile->objfile;
0cf9feb9 23056 return obstack_strndup (&objfile->per_bfd->storage_obstack,
2c830f54
TT
23057 attr_name,
23058 &base[-1] - attr_name);
96408a79
SA
23059}
23060
fdde2d81 23061/* Return the name of the namespace/class that DIE is defined within,
0114d602 23062 or "" if we can't tell. The caller should not xfree the result.
fdde2d81 23063
0114d602
DJ
23064 For example, if we're within the method foo() in the following
23065 code:
23066
23067 namespace N {
23068 class C {
23069 void foo () {
23070 }
23071 };
23072 }
23073
23074 then determine_prefix on foo's die will return "N::C". */
fdde2d81 23075
0d5cff50 23076static const char *
e142c38c 23077determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
63d06c5c 23078{
976ca316 23079 dwarf2_per_objfile *per_objfile = cu->per_objfile;
0114d602
DJ
23080 struct die_info *parent, *spec_die;
23081 struct dwarf2_cu *spec_cu;
23082 struct type *parent_type;
a121b7c1 23083 const char *retval;
63d06c5c 23084
9c37b5ae 23085 if (cu->language != language_cplus
c44af4eb
TT
23086 && cu->language != language_fortran && cu->language != language_d
23087 && cu->language != language_rust)
0114d602
DJ
23088 return "";
23089
96408a79
SA
23090 retval = anonymous_struct_prefix (die, cu);
23091 if (retval)
23092 return retval;
23093
0114d602
DJ
23094 /* We have to be careful in the presence of DW_AT_specification.
23095 For example, with GCC 3.4, given the code
23096
23097 namespace N {
23098 void foo() {
23099 // Definition of N::foo.
23100 }
23101 }
23102
23103 then we'll have a tree of DIEs like this:
23104
23105 1: DW_TAG_compile_unit
23106 2: DW_TAG_namespace // N
23107 3: DW_TAG_subprogram // declaration of N::foo
23108 4: DW_TAG_subprogram // definition of N::foo
23109 DW_AT_specification // refers to die #3
23110
23111 Thus, when processing die #4, we have to pretend that we're in
23112 the context of its DW_AT_specification, namely the contex of die
23113 #3. */
23114 spec_cu = cu;
23115 spec_die = die_specification (die, &spec_cu);
23116 if (spec_die == NULL)
23117 parent = die->parent;
23118 else
63d06c5c 23119 {
0114d602
DJ
23120 parent = spec_die->parent;
23121 cu = spec_cu;
63d06c5c 23122 }
0114d602
DJ
23123
23124 if (parent == NULL)
23125 return "";
98bfdba5
PA
23126 else if (parent->building_fullname)
23127 {
23128 const char *name;
23129 const char *parent_name;
23130
23131 /* It has been seen on RealView 2.2 built binaries,
23132 DW_TAG_template_type_param types actually _defined_ as
23133 children of the parent class:
23134
23135 enum E {};
23136 template class <class Enum> Class{};
23137 Class<enum E> class_e;
23138
dda83cd7
SM
23139 1: DW_TAG_class_type (Class)
23140 2: DW_TAG_enumeration_type (E)
23141 3: DW_TAG_enumerator (enum1:0)
23142 3: DW_TAG_enumerator (enum2:1)
23143 ...
23144 2: DW_TAG_template_type_param
23145 DW_AT_type DW_FORM_ref_udata (E)
98bfdba5
PA
23146
23147 Besides being broken debug info, it can put GDB into an
23148 infinite loop. Consider:
23149
23150 When we're building the full name for Class<E>, we'll start
23151 at Class, and go look over its template type parameters,
23152 finding E. We'll then try to build the full name of E, and
23153 reach here. We're now trying to build the full name of E,
23154 and look over the parent DIE for containing scope. In the
23155 broken case, if we followed the parent DIE of E, we'd again
23156 find Class, and once again go look at its template type
23157 arguments, etc., etc. Simply don't consider such parent die
23158 as source-level parent of this die (it can't be, the language
23159 doesn't allow it), and break the loop here. */
23160 name = dwarf2_name (die, cu);
23161 parent_name = dwarf2_name (parent, cu);
b98664d3 23162 complaint (_("template param type '%s' defined within parent '%s'"),
98bfdba5
PA
23163 name ? name : "<unknown>",
23164 parent_name ? parent_name : "<unknown>");
23165 return "";
23166 }
63d06c5c 23167 else
0114d602
DJ
23168 switch (parent->tag)
23169 {
63d06c5c 23170 case DW_TAG_namespace:
0114d602 23171 parent_type = read_type_die (parent, cu);
acebe513
UW
23172 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
23173 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
23174 Work around this problem here. */
23175 if (cu->language == language_cplus
7d93a1e0 23176 && strcmp (parent_type->name (), "::") == 0)
acebe513 23177 return "";
0114d602 23178 /* We give a name to even anonymous namespaces. */
7d93a1e0 23179 return parent_type->name ();
63d06c5c 23180 case DW_TAG_class_type:
680b30c7 23181 case DW_TAG_interface_type:
63d06c5c 23182 case DW_TAG_structure_type:
0114d602 23183 case DW_TAG_union_type:
f55ee35c 23184 case DW_TAG_module:
0114d602 23185 parent_type = read_type_die (parent, cu);
7d93a1e0
SM
23186 if (parent_type->name () != NULL)
23187 return parent_type->name ();
0114d602
DJ
23188 else
23189 /* An anonymous structure is only allowed non-static data
23190 members; no typedefs, no member functions, et cetera.
23191 So it does not need a prefix. */
23192 return "";
abc72ce4 23193 case DW_TAG_compile_unit:
95554aad 23194 case DW_TAG_partial_unit:
abc72ce4
DE
23195 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
23196 if (cu->language == language_cplus
976ca316 23197 && !per_objfile->per_bfd->types.empty ()
abc72ce4
DE
23198 && die->child != NULL
23199 && (die->tag == DW_TAG_class_type
23200 || die->tag == DW_TAG_structure_type
23201 || die->tag == DW_TAG_union_type))
23202 {
43816ebc 23203 const char *name = guess_full_die_structure_name (die, cu);
abc72ce4
DE
23204 if (name != NULL)
23205 return name;
23206 }
23207 return "";
0a4b0913
AB
23208 case DW_TAG_subprogram:
23209 /* Nested subroutines in Fortran get a prefix with the name
23210 of the parent's subroutine. */
23211 if (cu->language == language_fortran)
23212 {
23213 if ((die->tag == DW_TAG_subprogram)
23214 && (dwarf2_name (parent, cu) != NULL))
23215 return dwarf2_name (parent, cu);
23216 }
23217 return determine_prefix (parent, cu);
3d567982
TT
23218 case DW_TAG_enumeration_type:
23219 parent_type = read_type_die (parent, cu);
23220 if (TYPE_DECLARED_CLASS (parent_type))
23221 {
7d93a1e0
SM
23222 if (parent_type->name () != NULL)
23223 return parent_type->name ();
3d567982
TT
23224 return "";
23225 }
23226 /* Fall through. */
63d06c5c 23227 default:
8176b9b8 23228 return determine_prefix (parent, cu);
63d06c5c 23229 }
63d06c5c
DC
23230}
23231
3e43a32a
MS
23232/* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
23233 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
23234 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
23235 an obconcat, otherwise allocate storage for the result. The CU argument is
23236 used to determine the language and hence, the appropriate separator. */
987504bb 23237
f55ee35c 23238#define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
63d06c5c
DC
23239
23240static char *
f55ee35c 23241typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
dda83cd7 23242 int physname, struct dwarf2_cu *cu)
63d06c5c 23243{
f55ee35c 23244 const char *lead = "";
5c315b68 23245 const char *sep;
63d06c5c 23246
3e43a32a
MS
23247 if (suffix == NULL || suffix[0] == '\0'
23248 || prefix == NULL || prefix[0] == '\0')
987504bb 23249 sep = "";
45280282
IB
23250 else if (cu->language == language_d)
23251 {
23252 /* For D, the 'main' function could be defined in any module, but it
23253 should never be prefixed. */
23254 if (strcmp (suffix, "D main") == 0)
23255 {
23256 prefix = "";
23257 sep = "";
23258 }
23259 else
23260 sep = ".";
23261 }
f55ee35c
JK
23262 else if (cu->language == language_fortran && physname)
23263 {
23264 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
23265 DW_AT_MIPS_linkage_name is preferred and used instead. */
23266
23267 lead = "__";
23268 sep = "_MOD_";
23269 }
987504bb
JJ
23270 else
23271 sep = "::";
63d06c5c 23272
6dd47d34
DE
23273 if (prefix == NULL)
23274 prefix = "";
23275 if (suffix == NULL)
23276 suffix = "";
23277
987504bb
JJ
23278 if (obs == NULL)
23279 {
3e43a32a 23280 char *retval
224c3ddb
SM
23281 = ((char *)
23282 xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1));
9a619af0 23283
f55ee35c
JK
23284 strcpy (retval, lead);
23285 strcat (retval, prefix);
6dd47d34
DE
23286 strcat (retval, sep);
23287 strcat (retval, suffix);
63d06c5c
DC
23288 return retval;
23289 }
987504bb
JJ
23290 else
23291 {
23292 /* We have an obstack. */
f55ee35c 23293 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
987504bb 23294 }
63d06c5c
DC
23295}
23296
71c25dea
TT
23297/* Get name of a die, return NULL if not found. */
23298
15d034d0
TT
23299static const char *
23300dwarf2_canonicalize_name (const char *name, struct dwarf2_cu *cu,
be1e3d3e 23301 struct objfile *objfile)
71c25dea
TT
23302{
23303 if (name && cu->language == language_cplus)
23304 {
596dc4ad
TT
23305 gdb::unique_xmalloc_ptr<char> canon_name
23306 = cp_canonicalize_string (name);
71c25dea 23307
596dc4ad
TT
23308 if (canon_name != nullptr)
23309 name = objfile->intern (canon_name.get ());
71c25dea
TT
23310 }
23311
23312 return name;
c906108c
SS
23313}
23314
96553a0c
DE
23315/* Get name of a die, return NULL if not found.
23316 Anonymous namespaces are converted to their magic string. */
9219021c 23317
15d034d0 23318static const char *
e142c38c 23319dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
9219021c
DC
23320{
23321 struct attribute *attr;
5e22e966 23322 struct objfile *objfile = cu->per_objfile->objfile;
9219021c 23323
e142c38c 23324 attr = dwarf2_attr (die, DW_AT_name, cu);
2c830f54
TT
23325 const char *attr_name = attr == nullptr ? nullptr : attr->as_string ();
23326 if (attr_name == nullptr
96553a0c 23327 && die->tag != DW_TAG_namespace
53832f31
TT
23328 && die->tag != DW_TAG_class_type
23329 && die->tag != DW_TAG_interface_type
23330 && die->tag != DW_TAG_structure_type
23331 && die->tag != DW_TAG_union_type)
71c25dea
TT
23332 return NULL;
23333
23334 switch (die->tag)
23335 {
23336 case DW_TAG_compile_unit:
95554aad 23337 case DW_TAG_partial_unit:
71c25dea
TT
23338 /* Compilation units have a DW_AT_name that is a filename, not
23339 a source language identifier. */
23340 case DW_TAG_enumeration_type:
23341 case DW_TAG_enumerator:
23342 /* These tags always have simple identifiers already; no need
23343 to canonicalize them. */
2c830f54 23344 return attr_name;
907af001 23345
96553a0c 23346 case DW_TAG_namespace:
2c830f54
TT
23347 if (attr_name != nullptr)
23348 return attr_name;
96553a0c
DE
23349 return CP_ANONYMOUS_NAMESPACE_STR;
23350
907af001
UW
23351 case DW_TAG_class_type:
23352 case DW_TAG_interface_type:
23353 case DW_TAG_structure_type:
23354 case DW_TAG_union_type:
23355 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
23356 structures or unions. These were of the form "._%d" in GCC 4.1,
23357 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
23358 and GCC 4.4. We work around this problem by ignoring these. */
2c830f54
TT
23359 if (attr_name != nullptr
23360 && (startswith (attr_name, "._")
23361 || startswith (attr_name, "<anonymous")))
907af001 23362 return NULL;
53832f31
TT
23363
23364 /* GCC might emit a nameless typedef that has a linkage name. See
23365 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
2c830f54 23366 if (!attr || attr_name == NULL)
53832f31 23367 {
73b9be8b 23368 attr = dw2_linkage_name_attr (die, cu);
95eb9e54 23369 attr_name = attr == nullptr ? nullptr : attr->as_string ();
2c830f54 23370 if (attr == NULL || attr_name == NULL)
53832f31
TT
23371 return NULL;
23372
2c830f54 23373 /* Avoid demangling attr_name the second time on a second
df5c6c50 23374 call for the same DIE. */
3b64bf15 23375 if (!attr->canonical_string_p ())
53832f31 23376 {
43816ebc 23377 gdb::unique_xmalloc_ptr<char> demangled
2c830f54 23378 (gdb_demangle (attr_name, DMGL_TYPES));
4f180d53
AT
23379 if (demangled == nullptr)
23380 return nullptr;
43816ebc 23381
c6481205 23382 attr->set_string_canonical (objfile->intern (demangled.get ()));
95eb9e54 23383 attr_name = attr->as_string ();
53832f31 23384 }
67430cd0 23385
2c830f54
TT
23386 /* Strip any leading namespaces/classes, keep only the
23387 base name. DW_AT_name for named DIEs does not
23388 contain the prefixes. */
23389 const char *base = strrchr (attr_name, ':');
23390 if (base && base > attr_name && base[-1] == ':')
67430cd0
TT
23391 return &base[1];
23392 else
2c830f54 23393 return attr_name;
53832f31 23394 }
907af001
UW
23395 break;
23396
71c25dea 23397 default:
907af001
UW
23398 break;
23399 }
23400
3b64bf15 23401 if (!attr->canonical_string_p ())
c6481205
TT
23402 attr->set_string_canonical (dwarf2_canonicalize_name (attr_name, cu,
23403 objfile));
2c830f54 23404 return attr->as_string ();
9219021c
DC
23405}
23406
23407/* Return the die that this die in an extension of, or NULL if there
f2f0e013
DJ
23408 is none. *EXT_CU is the CU containing DIE on input, and the CU
23409 containing the return value on output. */
9219021c
DC
23410
23411static struct die_info *
f2f0e013 23412dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
9219021c
DC
23413{
23414 struct attribute *attr;
9219021c 23415
f2f0e013 23416 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
9219021c
DC
23417 if (attr == NULL)
23418 return NULL;
23419
f2f0e013 23420 return follow_die_ref (die, attr, ext_cu);
9219021c
DC
23421}
23422
f9aca02d 23423static void
d97bc12b 23424dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
c906108c
SS
23425{
23426 unsigned int i;
23427
d97bc12b 23428 print_spaces (indent, f);
9d8780f0 23429 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset %s)\n",
9c541725 23430 dwarf_tag_name (die->tag), die->abbrev,
9d8780f0 23431 sect_offset_str (die->sect_off));
d97bc12b
DE
23432
23433 if (die->parent != NULL)
23434 {
23435 print_spaces (indent, f);
9d8780f0
SM
23436 fprintf_unfiltered (f, " parent at offset: %s\n",
23437 sect_offset_str (die->parent->sect_off));
d97bc12b
DE
23438 }
23439
23440 print_spaces (indent, f);
23441 fprintf_unfiltered (f, " has children: %s\n",
639d11d3 23442 dwarf_bool_name (die->child != NULL));
c906108c 23443
d97bc12b
DE
23444 print_spaces (indent, f);
23445 fprintf_unfiltered (f, " attributes:\n");
23446
c906108c
SS
23447 for (i = 0; i < die->num_attrs; ++i)
23448 {
d97bc12b
DE
23449 print_spaces (indent, f);
23450 fprintf_unfiltered (f, " %s (%s) ",
c906108c
SS
23451 dwarf_attr_name (die->attrs[i].name),
23452 dwarf_form_name (die->attrs[i].form));
d97bc12b 23453
c906108c
SS
23454 switch (die->attrs[i].form)
23455 {
c906108c 23456 case DW_FORM_addr:
336d760d 23457 case DW_FORM_addrx:
3019eac3 23458 case DW_FORM_GNU_addr_index:
d97bc12b 23459 fprintf_unfiltered (f, "address: ");
36d378cf 23460 fputs_filtered (hex_string (die->attrs[i].as_address ()), f);
c906108c
SS
23461 break;
23462 case DW_FORM_block2:
23463 case DW_FORM_block4:
23464 case DW_FORM_block:
23465 case DW_FORM_block1:
56eb65bd 23466 fprintf_unfiltered (f, "block: size %s",
9d2246fc 23467 pulongest (die->attrs[i].as_block ()->size));
c906108c 23468 break;
2dc7f7b3 23469 case DW_FORM_exprloc:
56eb65bd 23470 fprintf_unfiltered (f, "expression: size %s",
9d2246fc 23471 pulongest (die->attrs[i].as_block ()->size));
2dc7f7b3 23472 break;
0224619f
JK
23473 case DW_FORM_data16:
23474 fprintf_unfiltered (f, "constant of 16 bytes");
23475 break;
4568ecf9
DE
23476 case DW_FORM_ref_addr:
23477 fprintf_unfiltered (f, "ref address: ");
529908cb 23478 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
4568ecf9 23479 break;
36586728
TT
23480 case DW_FORM_GNU_ref_alt:
23481 fprintf_unfiltered (f, "alt ref address: ");
529908cb 23482 fputs_filtered (hex_string (die->attrs[i].as_unsigned ()), f);
36586728 23483 break;
10b3939b
DJ
23484 case DW_FORM_ref1:
23485 case DW_FORM_ref2:
23486 case DW_FORM_ref4:
4568ecf9
DE
23487 case DW_FORM_ref8:
23488 case DW_FORM_ref_udata:
d97bc12b 23489 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
529908cb 23490 (long) (die->attrs[i].as_unsigned ()));
10b3939b 23491 break;
c906108c
SS
23492 case DW_FORM_data1:
23493 case DW_FORM_data2:
23494 case DW_FORM_data4:
ce5d95e1 23495 case DW_FORM_data8:
c906108c 23496 case DW_FORM_udata:
43bbcdc2 23497 fprintf_unfiltered (f, "constant: %s",
529908cb 23498 pulongest (die->attrs[i].as_unsigned ()));
c906108c 23499 break;
2dc7f7b3
TT
23500 case DW_FORM_sec_offset:
23501 fprintf_unfiltered (f, "section offset: %s",
529908cb 23502 pulongest (die->attrs[i].as_unsigned ()));
2dc7f7b3 23503 break;
55f1336d 23504 case DW_FORM_ref_sig8:
ac9ec31b 23505 fprintf_unfiltered (f, "signature: %s",
630ed6b9 23506 hex_string (die->attrs[i].as_signature ()));
348e048f 23507 break;
c906108c 23508 case DW_FORM_string:
4bdf3d34 23509 case DW_FORM_strp:
43988095 23510 case DW_FORM_line_strp:
cf532bd1 23511 case DW_FORM_strx:
3019eac3 23512 case DW_FORM_GNU_str_index:
36586728 23513 case DW_FORM_GNU_strp_alt:
8285870a 23514 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
c6481205
TT
23515 die->attrs[i].as_string ()
23516 ? die->attrs[i].as_string () : "",
23517 die->attrs[i].canonical_string_p () ? "is" : "not");
c906108c
SS
23518 break;
23519 case DW_FORM_flag:
c45bc3f8 23520 if (die->attrs[i].as_boolean ())
d97bc12b 23521 fprintf_unfiltered (f, "flag: TRUE");
c906108c 23522 else
d97bc12b 23523 fprintf_unfiltered (f, "flag: FALSE");
c906108c 23524 break;
2dc7f7b3
TT
23525 case DW_FORM_flag_present:
23526 fprintf_unfiltered (f, "flag: TRUE");
23527 break;
a8329558 23528 case DW_FORM_indirect:
0963b4bd
MS
23529 /* The reader will have reduced the indirect form to
23530 the "base form" so this form should not occur. */
5f48f8f3 23531 fprintf_unfiltered (f,
3e43a32a 23532 "unexpected attribute form: DW_FORM_indirect");
a8329558 23533 break;
1bc397c5 23534 case DW_FORM_sdata:
663c44ac
JK
23535 case DW_FORM_implicit_const:
23536 fprintf_unfiltered (f, "constant: %s",
1bc397c5 23537 plongest (die->attrs[i].as_signed ()));
663c44ac 23538 break;
c906108c 23539 default:
d97bc12b 23540 fprintf_unfiltered (f, "unsupported attribute form: %d.",
c5aa993b 23541 die->attrs[i].form);
d97bc12b 23542 break;
c906108c 23543 }
d97bc12b 23544 fprintf_unfiltered (f, "\n");
c906108c
SS
23545 }
23546}
23547
f9aca02d 23548static void
d97bc12b 23549dump_die_for_error (struct die_info *die)
c906108c 23550{
d97bc12b
DE
23551 dump_die_shallow (gdb_stderr, 0, die);
23552}
23553
23554static void
23555dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
23556{
23557 int indent = level * 4;
23558
23559 gdb_assert (die != NULL);
23560
23561 if (level >= max_level)
23562 return;
23563
23564 dump_die_shallow (f, indent, die);
23565
23566 if (die->child != NULL)
c906108c 23567 {
d97bc12b
DE
23568 print_spaces (indent, f);
23569 fprintf_unfiltered (f, " Children:");
23570 if (level + 1 < max_level)
23571 {
23572 fprintf_unfiltered (f, "\n");
23573 dump_die_1 (f, level + 1, max_level, die->child);
23574 }
23575 else
23576 {
3e43a32a
MS
23577 fprintf_unfiltered (f,
23578 " [not printed, max nesting level reached]\n");
d97bc12b
DE
23579 }
23580 }
23581
23582 if (die->sibling != NULL && level > 0)
23583 {
23584 dump_die_1 (f, level, max_level, die->sibling);
c906108c
SS
23585 }
23586}
23587
d97bc12b
DE
23588/* This is called from the pdie macro in gdbinit.in.
23589 It's not static so gcc will keep a copy callable from gdb. */
23590
23591void
23592dump_die (struct die_info *die, int max_level)
23593{
23594 dump_die_1 (gdb_stdlog, 0, max_level, die);
23595}
23596
f9aca02d 23597static void
51545339 23598store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
c906108c 23599{
51545339 23600 void **slot;
c906108c 23601
9c541725
PA
23602 slot = htab_find_slot_with_hash (cu->die_hash, die,
23603 to_underlying (die->sect_off),
b64f50a1 23604 INSERT);
51545339
DJ
23605
23606 *slot = die;
c906108c
SS
23607}
23608
348e048f
DE
23609/* Follow reference or signature attribute ATTR of SRC_DIE.
23610 On entry *REF_CU is the CU of SRC_DIE.
23611 On exit *REF_CU is the CU of the result. */
23612
23613static struct die_info *
ff39bb5e 23614follow_die_ref_or_sig (struct die_info *src_die, const struct attribute *attr,
348e048f
DE
23615 struct dwarf2_cu **ref_cu)
23616{
23617 struct die_info *die;
23618
cd6c91b4 23619 if (attr->form_is_ref ())
348e048f 23620 die = follow_die_ref (src_die, attr, ref_cu);
55f1336d 23621 else if (attr->form == DW_FORM_ref_sig8)
348e048f
DE
23622 die = follow_die_sig (src_die, attr, ref_cu);
23623 else
23624 {
23625 dump_die_for_error (src_die);
23626 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
5e22e966 23627 objfile_name ((*ref_cu)->per_objfile->objfile));
348e048f
DE
23628 }
23629
23630 return die;
03dd20cc
DJ
23631}
23632
5c631832 23633/* Follow reference OFFSET.
673bfd45
DE
23634 On entry *REF_CU is the CU of the source die referencing OFFSET.
23635 On exit *REF_CU is the CU of the result.
23636 Returns NULL if OFFSET is invalid. */
f504f079 23637
f9aca02d 23638static struct die_info *
9c541725 23639follow_die_offset (sect_offset sect_off, int offset_in_dwz,
36586728 23640 struct dwarf2_cu **ref_cu)
c906108c 23641{
10b3939b 23642 struct die_info temp_die;
f2f0e013 23643 struct dwarf2_cu *target_cu, *cu = *ref_cu;
976ca316 23644 dwarf2_per_objfile *per_objfile = cu->per_objfile;
10b3939b 23645
348e048f
DE
23646 gdb_assert (cu->per_cu != NULL);
23647
98bfdba5
PA
23648 target_cu = cu;
23649
17e593e9
SM
23650 dwarf_read_debug_printf_v ("source CU offset: %s, target offset: %s, "
23651 "source CU contains target offset: %d",
23652 sect_offset_str (cu->per_cu->sect_off),
23653 sect_offset_str (sect_off),
23654 cu->header.offset_in_cu_p (sect_off));
23655
3019eac3 23656 if (cu->per_cu->is_debug_types)
348e048f
DE
23657 {
23658 /* .debug_types CUs cannot reference anything outside their CU.
23659 If they need to, they have to reference a signatured type via
55f1336d 23660 DW_FORM_ref_sig8. */
4057dfde 23661 if (!cu->header.offset_in_cu_p (sect_off))
5c631832 23662 return NULL;
348e048f 23663 }
36586728 23664 else if (offset_in_dwz != cu->per_cu->is_dwz
4057dfde 23665 || !cu->header.offset_in_cu_p (sect_off))
10b3939b
DJ
23666 {
23667 struct dwarf2_per_cu_data *per_cu;
9a619af0 23668
9c541725 23669 per_cu = dwarf2_find_containing_comp_unit (sect_off, offset_in_dwz,
976ca316 23670 per_objfile);
03dd20cc 23671
17e593e9
SM
23672 dwarf_read_debug_printf_v ("target CU offset: %s, "
23673 "target CU DIEs loaded: %d",
23674 sect_offset_str (per_cu->sect_off),
23675 per_objfile->get_cu (per_cu) != nullptr);
23676
03dd20cc 23677 /* If necessary, add it to the queue and load its DIEs. */
976ca316 23678 if (maybe_queue_comp_unit (cu, per_cu, per_objfile, cu->language))
4a636814
SM
23679 load_full_comp_unit (per_cu, per_objfile, per_objfile->get_cu (per_cu),
23680 false, cu->language);
03dd20cc 23681
976ca316 23682 target_cu = per_objfile->get_cu (per_cu);
10b3939b 23683 }
98bfdba5
PA
23684 else if (cu->dies == NULL)
23685 {
23686 /* We're loading full DIEs during partial symbol reading. */
976ca316 23687 gdb_assert (per_objfile->per_bfd->reading_partial_symbols);
4a636814
SM
23688 load_full_comp_unit (cu->per_cu, per_objfile, cu, false,
23689 language_minimal);
98bfdba5 23690 }
c906108c 23691
f2f0e013 23692 *ref_cu = target_cu;
9c541725 23693 temp_die.sect_off = sect_off;
c24bdb02
KS
23694
23695 if (target_cu != cu)
23696 target_cu->ancestor = cu;
23697
9a3c8263 23698 return (struct die_info *) htab_find_with_hash (target_cu->die_hash,
9c541725
PA
23699 &temp_die,
23700 to_underlying (sect_off));
5c631832 23701}
10b3939b 23702
5c631832
JK
23703/* Follow reference attribute ATTR of SRC_DIE.
23704 On entry *REF_CU is the CU of SRC_DIE.
23705 On exit *REF_CU is the CU of the result. */
23706
23707static struct die_info *
ff39bb5e 23708follow_die_ref (struct die_info *src_die, const struct attribute *attr,
5c631832
JK
23709 struct dwarf2_cu **ref_cu)
23710{
0826b30a 23711 sect_offset sect_off = attr->get_ref_die_offset ();
5c631832
JK
23712 struct dwarf2_cu *cu = *ref_cu;
23713 struct die_info *die;
23714
9c541725 23715 die = follow_die_offset (sect_off,
36586728
TT
23716 (attr->form == DW_FORM_GNU_ref_alt
23717 || cu->per_cu->is_dwz),
23718 ref_cu);
5c631832 23719 if (!die)
9d8780f0
SM
23720 error (_("Dwarf Error: Cannot find DIE at %s referenced from DIE "
23721 "at %s [in module %s]"),
23722 sect_offset_str (sect_off), sect_offset_str (src_die->sect_off),
5e22e966 23723 objfile_name (cu->per_objfile->objfile));
348e048f 23724
5c631832
JK
23725 return die;
23726}
23727
d4c9a4f8 23728/* See read.h. */
5c631832
JK
23729
23730struct dwarf2_locexpr_baton
9c541725 23731dwarf2_fetch_die_loc_sect_off (sect_offset sect_off,
d4c9a4f8 23732 dwarf2_per_cu_data *per_cu,
976ca316 23733 dwarf2_per_objfile *per_objfile,
041d9819
SM
23734 gdb::function_view<CORE_ADDR ()> get_frame_pc,
23735 bool resolve_abstract_p)
5c631832 23736{
5c631832
JK
23737 struct die_info *die;
23738 struct attribute *attr;
23739 struct dwarf2_locexpr_baton retval;
976ca316 23740 struct objfile *objfile = per_objfile->objfile;
8cf6f0b1 23741
976ca316 23742 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17 23743 if (cu == nullptr)
976ca316 23744 cu = load_cu (per_cu, per_objfile, false);
1b555f17
SM
23745
23746 if (cu == nullptr)
cc12ce38
DE
23747 {
23748 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23749 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23750 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23751 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23752 }
918dd910 23753
9c541725 23754 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
5c631832 23755 if (!die)
9d8780f0
SM
23756 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23757 sect_offset_str (sect_off), objfile_name (objfile));
5c631832
JK
23758
23759 attr = dwarf2_attr (die, DW_AT_location, cu);
e4a62c65 23760 if (!attr && resolve_abstract_p
976ca316
SM
23761 && (per_objfile->per_bfd->abstract_to_concrete.find (die->sect_off)
23762 != per_objfile->per_bfd->abstract_to_concrete.end ()))
e4a62c65 23763 {
041d9819 23764 CORE_ADDR pc = get_frame_pc ();
b3b3bada 23765 CORE_ADDR baseaddr = objfile->text_section_offset ();
08feed99 23766 struct gdbarch *gdbarch = objfile->arch ();
e4a62c65 23767
3360b6e7 23768 for (const auto &cand_off
976ca316 23769 : per_objfile->per_bfd->abstract_to_concrete[die->sect_off])
e4a62c65 23770 {
3360b6e7
TV
23771 struct dwarf2_cu *cand_cu = cu;
23772 struct die_info *cand
23773 = follow_die_offset (cand_off, per_cu->is_dwz, &cand_cu);
23774 if (!cand
23775 || !cand->parent
e4a62c65
TV
23776 || cand->parent->tag != DW_TAG_subprogram)
23777 continue;
23778
23779 CORE_ADDR pc_low, pc_high;
23780 get_scope_pc_bounds (cand->parent, &pc_low, &pc_high, cu);
eba4caf2
TV
23781 if (pc_low == ((CORE_ADDR) -1))
23782 continue;
23783 pc_low = gdbarch_adjust_dwarf2_addr (gdbarch, pc_low + baseaddr);
23784 pc_high = gdbarch_adjust_dwarf2_addr (gdbarch, pc_high + baseaddr);
23785 if (!(pc_low <= pc && pc < pc_high))
e4a62c65
TV
23786 continue;
23787
23788 die = cand;
23789 attr = dwarf2_attr (die, DW_AT_location, cu);
23790 break;
23791 }
23792 }
23793
5c631832
JK
23794 if (!attr)
23795 {
e103e986
JK
23796 /* DWARF: "If there is no such attribute, then there is no effect.".
23797 DATA is ignored if SIZE is 0. */
5c631832 23798
e103e986 23799 retval.data = NULL;
5c631832
JK
23800 retval.size = 0;
23801 }
cd6c91b4 23802 else if (attr->form_is_section_offset ())
8cf6f0b1
TT
23803 {
23804 struct dwarf2_loclist_baton loclist_baton;
041d9819 23805 CORE_ADDR pc = get_frame_pc ();
8cf6f0b1
TT
23806 size_t size;
23807
23808 fill_in_loclist_baton (cu, &loclist_baton, attr);
23809
23810 retval.data = dwarf2_find_location_expression (&loclist_baton,
23811 &size, pc);
23812 retval.size = size;
23813 }
5c631832
JK
23814 else
23815 {
4fc6c0d5 23816 if (!attr->form_is_block ())
9d8780f0 23817 error (_("Dwarf Error: DIE at %s referenced in module %s "
5c631832 23818 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
9d8780f0 23819 sect_offset_str (sect_off), objfile_name (objfile));
5c631832 23820
9d2246fc
TT
23821 struct dwarf_block *block = attr->as_block ();
23822 retval.data = block->data;
23823 retval.size = block->size;
5c631832 23824 }
976ca316 23825 retval.per_objfile = per_objfile;
5c631832 23826 retval.per_cu = cu->per_cu;
918dd910 23827
976ca316 23828 per_objfile->age_comp_units ();
918dd910 23829
5c631832 23830 return retval;
348e048f
DE
23831}
23832
d4c9a4f8 23833/* See read.h. */
8b9737bf
TT
23834
23835struct dwarf2_locexpr_baton
23836dwarf2_fetch_die_loc_cu_off (cu_offset offset_in_cu,
d4c9a4f8 23837 dwarf2_per_cu_data *per_cu,
14095eb3 23838 dwarf2_per_objfile *per_objfile,
041d9819 23839 gdb::function_view<CORE_ADDR ()> get_frame_pc)
8b9737bf 23840{
9c541725 23841 sect_offset sect_off = per_cu->sect_off + to_underlying (offset_in_cu);
8b9737bf 23842
14095eb3 23843 return dwarf2_fetch_die_loc_sect_off (sect_off, per_cu, per_objfile,
041d9819 23844 get_frame_pc);
8b9737bf
TT
23845}
23846
b6807d98
TT
23847/* Write a constant of a given type as target-ordered bytes into
23848 OBSTACK. */
23849
23850static const gdb_byte *
23851write_constant_as_bytes (struct obstack *obstack,
23852 enum bfd_endian byte_order,
23853 struct type *type,
23854 ULONGEST value,
23855 LONGEST *len)
23856{
23857 gdb_byte *result;
23858
23859 *len = TYPE_LENGTH (type);
224c3ddb 23860 result = (gdb_byte *) obstack_alloc (obstack, *len);
b6807d98
TT
23861 store_unsigned_integer (result, *len, byte_order, value);
23862
23863 return result;
23864}
23865
d4c9a4f8 23866/* See read.h. */
b6807d98
TT
23867
23868const gdb_byte *
9c541725 23869dwarf2_fetch_constant_bytes (sect_offset sect_off,
d4c9a4f8 23870 dwarf2_per_cu_data *per_cu,
14095eb3 23871 dwarf2_per_objfile *per_objfile,
d4c9a4f8 23872 obstack *obstack,
b6807d98
TT
23873 LONGEST *len)
23874{
b6807d98
TT
23875 struct die_info *die;
23876 struct attribute *attr;
23877 const gdb_byte *result = NULL;
23878 struct type *type;
23879 LONGEST value;
23880 enum bfd_endian byte_order;
14095eb3 23881 struct objfile *objfile = per_objfile->objfile;
b6807d98 23882
7188ed02 23883 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
23884 if (cu == nullptr)
23885 cu = load_cu (per_cu, per_objfile, false);
23886
23887 if (cu == nullptr)
cc12ce38
DE
23888 {
23889 /* We shouldn't get here for a dummy CU, but don't crash on the user.
23890 Instead just throw an error, not much else we can do. */
9d8780f0
SM
23891 error (_("Dwarf Error: Dummy CU at %s referenced in module %s"),
23892 sect_offset_str (sect_off), objfile_name (objfile));
cc12ce38 23893 }
b6807d98 23894
9c541725 23895 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
b6807d98 23896 if (!die)
9d8780f0
SM
23897 error (_("Dwarf Error: Cannot find DIE at %s referenced in module %s"),
23898 sect_offset_str (sect_off), objfile_name (objfile));
b6807d98
TT
23899
23900 attr = dwarf2_attr (die, DW_AT_const_value, cu);
23901 if (attr == NULL)
23902 return NULL;
23903
e3b94546 23904 byte_order = (bfd_big_endian (objfile->obfd)
b6807d98
TT
23905 ? BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
23906
23907 switch (attr->form)
23908 {
23909 case DW_FORM_addr:
336d760d 23910 case DW_FORM_addrx:
b6807d98
TT
23911 case DW_FORM_GNU_addr_index:
23912 {
23913 gdb_byte *tem;
23914
23915 *len = cu->header.addr_size;
224c3ddb 23916 tem = (gdb_byte *) obstack_alloc (obstack, *len);
36d378cf 23917 store_unsigned_integer (tem, *len, byte_order, attr->as_address ());
b6807d98
TT
23918 result = tem;
23919 }
23920 break;
23921 case DW_FORM_string:
23922 case DW_FORM_strp:
cf532bd1 23923 case DW_FORM_strx:
b6807d98
TT
23924 case DW_FORM_GNU_str_index:
23925 case DW_FORM_GNU_strp_alt:
c6481205 23926 /* The string is already allocated on the objfile obstack, point
b6807d98 23927 directly to it. */
2c830f54
TT
23928 {
23929 const char *attr_name = attr->as_string ();
23930 result = (const gdb_byte *) attr_name;
23931 *len = strlen (attr_name);
23932 }
b6807d98
TT
23933 break;
23934 case DW_FORM_block1:
23935 case DW_FORM_block2:
23936 case DW_FORM_block4:
23937 case DW_FORM_block:
23938 case DW_FORM_exprloc:
0224619f 23939 case DW_FORM_data16:
9d2246fc
TT
23940 {
23941 struct dwarf_block *block = attr->as_block ();
23942 result = block->data;
23943 *len = block->size;
23944 }
b6807d98
TT
23945 break;
23946
23947 /* The DW_AT_const_value attributes are supposed to carry the
23948 symbol's value "represented as it would be on the target
23949 architecture." By the time we get here, it's already been
23950 converted to host endianness, so we just need to sign- or
23951 zero-extend it as appropriate. */
23952 case DW_FORM_data1:
23953 type = die_type (die, cu);
23954 result = dwarf2_const_value_data (attr, obstack, cu, &value, 8);
23955 if (result == NULL)
23956 result = write_constant_as_bytes (obstack, byte_order,
23957 type, value, len);
23958 break;
23959 case DW_FORM_data2:
23960 type = die_type (die, cu);
23961 result = dwarf2_const_value_data (attr, obstack, cu, &value, 16);
23962 if (result == NULL)
23963 result = write_constant_as_bytes (obstack, byte_order,
23964 type, value, len);
23965 break;
23966 case DW_FORM_data4:
23967 type = die_type (die, cu);
23968 result = dwarf2_const_value_data (attr, obstack, cu, &value, 32);
23969 if (result == NULL)
23970 result = write_constant_as_bytes (obstack, byte_order,
23971 type, value, len);
23972 break;
23973 case DW_FORM_data8:
23974 type = die_type (die, cu);
23975 result = dwarf2_const_value_data (attr, obstack, cu, &value, 64);
23976 if (result == NULL)
23977 result = write_constant_as_bytes (obstack, byte_order,
23978 type, value, len);
23979 break;
23980
23981 case DW_FORM_sdata:
663c44ac 23982 case DW_FORM_implicit_const:
b6807d98
TT
23983 type = die_type (die, cu);
23984 result = write_constant_as_bytes (obstack, byte_order,
1bc397c5 23985 type, attr->as_signed (), len);
b6807d98
TT
23986 break;
23987
23988 case DW_FORM_udata:
23989 type = die_type (die, cu);
23990 result = write_constant_as_bytes (obstack, byte_order,
529908cb 23991 type, attr->as_unsigned (), len);
b6807d98
TT
23992 break;
23993
23994 default:
b98664d3 23995 complaint (_("unsupported const value attribute form: '%s'"),
b6807d98
TT
23996 dwarf_form_name (attr->form));
23997 break;
23998 }
23999
24000 return result;
24001}
24002
d4c9a4f8 24003/* See read.h. */
7942e96e
AA
24004
24005struct type *
9c541725 24006dwarf2_fetch_die_type_sect_off (sect_offset sect_off,
14095eb3
SM
24007 dwarf2_per_cu_data *per_cu,
24008 dwarf2_per_objfile *per_objfile)
7942e96e 24009{
7942e96e
AA
24010 struct die_info *die;
24011
7188ed02 24012 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
1b555f17
SM
24013 if (cu == nullptr)
24014 cu = load_cu (per_cu, per_objfile, false);
24015
24016 if (cu == nullptr)
24017 return nullptr;
7942e96e 24018
9c541725 24019 die = follow_die_offset (sect_off, per_cu->is_dwz, &cu);
7942e96e
AA
24020 if (!die)
24021 return NULL;
24022
24023 return die_type (die, cu);
24024}
24025
8cb5117c 24026/* See read.h. */
8a9b8146
TT
24027
24028struct type *
b64f50a1 24029dwarf2_get_die_type (cu_offset die_offset,
aa66c379
SM
24030 dwarf2_per_cu_data *per_cu,
24031 dwarf2_per_objfile *per_objfile)
8a9b8146 24032{
9c541725 24033 sect_offset die_offset_sect = per_cu->sect_off + to_underlying (die_offset);
aa66c379 24034 return get_die_type_at_offset (die_offset_sect, per_cu, per_objfile);
8a9b8146
TT
24035}
24036
ac9ec31b 24037/* Follow type unit SIG_TYPE referenced by SRC_DIE.
348e048f 24038 On entry *REF_CU is the CU of SRC_DIE.
ac9ec31b
DE
24039 On exit *REF_CU is the CU of the result.
24040 Returns NULL if the referenced DIE isn't found. */
348e048f
DE
24041
24042static struct die_info *
ac9ec31b
DE
24043follow_die_sig_1 (struct die_info *src_die, struct signatured_type *sig_type,
24044 struct dwarf2_cu **ref_cu)
348e048f 24045{
348e048f 24046 struct die_info temp_die;
c24bdb02 24047 struct dwarf2_cu *sig_cu, *cu = *ref_cu;
348e048f 24048 struct die_info *die;
976ca316 24049 dwarf2_per_objfile *per_objfile = (*ref_cu)->per_objfile;
120ce1b5 24050
348e048f 24051
ac9ec31b
DE
24052 /* While it might be nice to assert sig_type->type == NULL here,
24053 we can get here for DW_AT_imported_declaration where we need
24054 the DIE not the type. */
348e048f
DE
24055
24056 /* If necessary, add it to the queue and load its DIEs. */
24057
976ca316 24058 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu, per_objfile,
120ce1b5 24059 language_minimal))
976ca316 24060 read_signatured_type (sig_type, per_objfile);
348e048f 24061
976ca316 24062 sig_cu = per_objfile->get_cu (&sig_type->per_cu);
69d751e3 24063 gdb_assert (sig_cu != NULL);
9c541725
PA
24064 gdb_assert (to_underlying (sig_type->type_offset_in_section) != 0);
24065 temp_die.sect_off = sig_type->type_offset_in_section;
9a3c8263 24066 die = (struct die_info *) htab_find_with_hash (sig_cu->die_hash, &temp_die,
9c541725 24067 to_underlying (temp_die.sect_off));
348e048f
DE
24068 if (die)
24069 {
796a7ff8
DE
24070 /* For .gdb_index version 7 keep track of included TUs.
24071 http://sourceware.org/bugzilla/show_bug.cgi?id=15021. */
976ca316
SM
24072 if (per_objfile->per_bfd->index_table != NULL
24073 && per_objfile->per_bfd->index_table->version <= 7)
796a7ff8 24074 {
ae640021 24075 (*ref_cu)->per_cu->imported_symtabs_push (sig_cu->per_cu);
796a7ff8
DE
24076 }
24077
348e048f 24078 *ref_cu = sig_cu;
c24bdb02
KS
24079 if (sig_cu != cu)
24080 sig_cu->ancestor = cu;
24081
348e048f
DE
24082 return die;
24083 }
24084
ac9ec31b
DE
24085 return NULL;
24086}
24087
24088/* Follow signatured type referenced by ATTR in SRC_DIE.
24089 On entry *REF_CU is the CU of SRC_DIE.
24090 On exit *REF_CU is the CU of the result.
24091 The result is the DIE of the type.
24092 If the referenced type cannot be found an error is thrown. */
24093
24094static struct die_info *
ff39bb5e 24095follow_die_sig (struct die_info *src_die, const struct attribute *attr,
ac9ec31b
DE
24096 struct dwarf2_cu **ref_cu)
24097{
630ed6b9 24098 ULONGEST signature = attr->as_signature ();
ac9ec31b
DE
24099 struct signatured_type *sig_type;
24100 struct die_info *die;
24101
24102 gdb_assert (attr->form == DW_FORM_ref_sig8);
24103
a2ce51a0 24104 sig_type = lookup_signatured_type (*ref_cu, signature);
ac9ec31b
DE
24105 /* sig_type will be NULL if the signatured type is missing from
24106 the debug info. */
24107 if (sig_type == NULL)
24108 {
24109 error (_("Dwarf Error: Cannot find signatured DIE %s referenced"
dda83cd7
SM
24110 " from DIE at %s [in module %s]"),
24111 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24112 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24113 }
24114
24115 die = follow_die_sig_1 (src_die, sig_type, ref_cu);
24116 if (die == NULL)
24117 {
24118 dump_die_for_error (src_die);
24119 error (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24120 " from DIE at %s [in module %s]"),
24121 hex_string (signature), sect_offset_str (src_die->sect_off),
5e22e966 24122 objfile_name ((*ref_cu)->per_objfile->objfile));
ac9ec31b
DE
24123 }
24124
24125 return die;
24126}
24127
24128/* Get the type specified by SIGNATURE referenced in DIE/CU,
24129 reading in and processing the type unit if necessary. */
24130
24131static struct type *
24132get_signatured_type (struct die_info *die, ULONGEST signature,
24133 struct dwarf2_cu *cu)
24134{
976ca316 24135 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ac9ec31b
DE
24136 struct signatured_type *sig_type;
24137 struct dwarf2_cu *type_cu;
24138 struct die_info *type_die;
24139 struct type *type;
24140
a2ce51a0 24141 sig_type = lookup_signatured_type (cu, signature);
ac9ec31b
DE
24142 /* sig_type will be NULL if the signatured type is missing from
24143 the debug info. */
24144 if (sig_type == NULL)
24145 {
b98664d3 24146 complaint (_("Dwarf Error: Cannot find signatured DIE %s referenced"
9d8780f0
SM
24147 " from DIE at %s [in module %s]"),
24148 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24149 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24150 return build_error_marker_type (cu, die);
24151 }
24152
24153 /* If we already know the type we're done. */
976ca316 24154 type = per_objfile->get_type_for_signatured_type (sig_type);
e286671b
TT
24155 if (type != nullptr)
24156 return type;
ac9ec31b
DE
24157
24158 type_cu = cu;
24159 type_die = follow_die_sig_1 (die, sig_type, &type_cu);
24160 if (type_die != NULL)
24161 {
24162 /* N.B. We need to call get_die_type to ensure only one type for this DIE
24163 is created. This is important, for example, because for c++ classes
24164 we need TYPE_NAME set which is only done by new_symbol. Blech. */
24165 type = read_type_die (type_die, type_cu);
24166 if (type == NULL)
24167 {
b98664d3 24168 complaint (_("Dwarf Error: Cannot build signatured type %s"
9d8780f0
SM
24169 " referenced from DIE at %s [in module %s]"),
24170 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24171 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24172 type = build_error_marker_type (cu, die);
24173 }
24174 }
24175 else
24176 {
b98664d3 24177 complaint (_("Dwarf Error: Problem reading signatured DIE %s referenced"
9d8780f0
SM
24178 " from DIE at %s [in module %s]"),
24179 hex_string (signature), sect_offset_str (die->sect_off),
976ca316 24180 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24181 type = build_error_marker_type (cu, die);
24182 }
e286671b 24183
976ca316 24184 per_objfile->set_type_for_signatured_type (sig_type, type);
ac9ec31b
DE
24185
24186 return type;
24187}
24188
24189/* Get the type specified by the DW_AT_signature ATTR in DIE/CU,
24190 reading in and processing the type unit if necessary. */
24191
24192static struct type *
ff39bb5e 24193get_DW_AT_signature_type (struct die_info *die, const struct attribute *attr,
b385a60d 24194 struct dwarf2_cu *cu) /* ARI: editCase function */
ac9ec31b
DE
24195{
24196 /* Yes, DW_AT_signature can use a non-ref_sig8 reference. */
cd6c91b4 24197 if (attr->form_is_ref ())
ac9ec31b
DE
24198 {
24199 struct dwarf2_cu *type_cu = cu;
24200 struct die_info *type_die = follow_die_ref (die, attr, &type_cu);
24201
24202 return read_type_die (type_die, type_cu);
24203 }
24204 else if (attr->form == DW_FORM_ref_sig8)
24205 {
630ed6b9 24206 return get_signatured_type (die, attr->as_signature (), cu);
ac9ec31b
DE
24207 }
24208 else
24209 {
976ca316 24210 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24211
b98664d3 24212 complaint (_("Dwarf Error: DW_AT_signature has bad form %s in DIE"
9d8780f0
SM
24213 " at %s [in module %s]"),
24214 dwarf_form_name (attr->form), sect_offset_str (die->sect_off),
976ca316 24215 objfile_name (per_objfile->objfile));
ac9ec31b
DE
24216 return build_error_marker_type (cu, die);
24217 }
348e048f
DE
24218}
24219
e5fe5e75 24220/* Load the DIEs associated with type unit PER_CU into memory. */
348e048f
DE
24221
24222static void
ab432490
SM
24223load_full_type_unit (dwarf2_per_cu_data *per_cu,
24224 dwarf2_per_objfile *per_objfile)
348e048f 24225{
52dc124a 24226 struct signatured_type *sig_type;
348e048f 24227
f4dc4d17 24228 /* Caller is responsible for ensuring type_unit_groups don't get here. */
197400e8 24229 gdb_assert (! per_cu->type_unit_group_p ());
f4dc4d17 24230
6721b2ec
DE
24231 /* We have the per_cu, but we need the signatured_type.
24232 Fortunately this is an easy translation. */
24233 gdb_assert (per_cu->is_debug_types);
24234 sig_type = (struct signatured_type *) per_cu;
348e048f 24235
7188ed02 24236 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24237
ab432490 24238 read_signatured_type (sig_type, per_objfile);
348e048f 24239
7188ed02 24240 gdb_assert (per_objfile->get_cu (per_cu) != nullptr);
348e048f
DE
24241}
24242
3019eac3
DE
24243/* Read in a signatured type and build its CU and DIEs.
24244 If the type is a stub for the real type in a DWO file,
24245 read in the real type from the DWO file as well. */
dee91e82
DE
24246
24247static void
ab432490
SM
24248read_signatured_type (signatured_type *sig_type,
24249 dwarf2_per_objfile *per_objfile)
dee91e82
DE
24250{
24251 struct dwarf2_per_cu_data *per_cu = &sig_type->per_cu;
348e048f 24252
3019eac3 24253 gdb_assert (per_cu->is_debug_types);
7188ed02 24254 gdb_assert (per_objfile->get_cu (per_cu) == nullptr);
348e048f 24255
2e671100 24256 cutu_reader reader (per_cu, per_objfile, nullptr, nullptr, false);
c0ab21c2
TT
24257
24258 if (!reader.dummy_p)
24259 {
24260 struct dwarf2_cu *cu = reader.cu;
24261 const gdb_byte *info_ptr = reader.info_ptr;
24262
24263 gdb_assert (cu->die_hash == NULL);
24264 cu->die_hash =
24265 htab_create_alloc_ex (cu->header.length / 12,
24266 die_hash,
24267 die_eq,
24268 NULL,
24269 &cu->comp_unit_obstack,
24270 hashtab_obstack_allocate,
24271 dummy_obstack_deallocate);
24272
3e225074 24273 if (reader.comp_unit_die->has_children)
c0ab21c2
TT
24274 reader.comp_unit_die->child
24275 = read_die_and_siblings (&reader, info_ptr, &info_ptr,
24276 reader.comp_unit_die);
24277 cu->dies = reader.comp_unit_die;
24278 /* comp_unit_die is not stored in die_hash, no need. */
24279
24280 /* We try not to read any attributes in this function, because
24281 not all CUs needed for references have been loaded yet, and
24282 symbol table processing isn't initialized. But we have to
24283 set the CU language, or we won't be able to build types
24284 correctly. Similarly, if we do not read the producer, we can
24285 not apply producer-specific interpretation. */
24286 prepare_one_comp_unit (cu, cu->dies, language_minimal);
6751ebae
TT
24287
24288 reader.keep ();
c0ab21c2
TT
24289 }
24290
7ee85ab1 24291 sig_type->per_cu.tu_read = 1;
c906108c
SS
24292}
24293
c906108c
SS
24294/* Decode simple location descriptions.
24295 Given a pointer to a dwarf block that defines a location, compute
7d79de9a
TT
24296 the location and return the value. If COMPUTED is non-null, it is
24297 set to true to indicate that decoding was successful, and false
24298 otherwise. If COMPUTED is null, then this function may emit a
24299 complaint. */
c906108c
SS
24300
24301static CORE_ADDR
7d79de9a 24302decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu, bool *computed)
c906108c 24303{
5e22e966 24304 struct objfile *objfile = cu->per_objfile->objfile;
56eb65bd
SP
24305 size_t i;
24306 size_t size = blk->size;
d521ce57 24307 const gdb_byte *data = blk->data;
21ae7a4d
JK
24308 CORE_ADDR stack[64];
24309 int stacki;
24310 unsigned int bytes_read, unsnd;
24311 gdb_byte op;
c906108c 24312
7d79de9a
TT
24313 if (computed != nullptr)
24314 *computed = false;
24315
21ae7a4d
JK
24316 i = 0;
24317 stacki = 0;
24318 stack[stacki] = 0;
24319 stack[++stacki] = 0;
24320
24321 while (i < size)
24322 {
24323 op = data[i++];
24324 switch (op)
24325 {
24326 case DW_OP_lit0:
24327 case DW_OP_lit1:
24328 case DW_OP_lit2:
24329 case DW_OP_lit3:
24330 case DW_OP_lit4:
24331 case DW_OP_lit5:
24332 case DW_OP_lit6:
24333 case DW_OP_lit7:
24334 case DW_OP_lit8:
24335 case DW_OP_lit9:
24336 case DW_OP_lit10:
24337 case DW_OP_lit11:
24338 case DW_OP_lit12:
24339 case DW_OP_lit13:
24340 case DW_OP_lit14:
24341 case DW_OP_lit15:
24342 case DW_OP_lit16:
24343 case DW_OP_lit17:
24344 case DW_OP_lit18:
24345 case DW_OP_lit19:
24346 case DW_OP_lit20:
24347 case DW_OP_lit21:
24348 case DW_OP_lit22:
24349 case DW_OP_lit23:
24350 case DW_OP_lit24:
24351 case DW_OP_lit25:
24352 case DW_OP_lit26:
24353 case DW_OP_lit27:
24354 case DW_OP_lit28:
24355 case DW_OP_lit29:
24356 case DW_OP_lit30:
24357 case DW_OP_lit31:
24358 stack[++stacki] = op - DW_OP_lit0;
24359 break;
f1bea926 24360
21ae7a4d
JK
24361 case DW_OP_reg0:
24362 case DW_OP_reg1:
24363 case DW_OP_reg2:
24364 case DW_OP_reg3:
24365 case DW_OP_reg4:
24366 case DW_OP_reg5:
24367 case DW_OP_reg6:
24368 case DW_OP_reg7:
24369 case DW_OP_reg8:
24370 case DW_OP_reg9:
24371 case DW_OP_reg10:
24372 case DW_OP_reg11:
24373 case DW_OP_reg12:
24374 case DW_OP_reg13:
24375 case DW_OP_reg14:
24376 case DW_OP_reg15:
24377 case DW_OP_reg16:
24378 case DW_OP_reg17:
24379 case DW_OP_reg18:
24380 case DW_OP_reg19:
24381 case DW_OP_reg20:
24382 case DW_OP_reg21:
24383 case DW_OP_reg22:
24384 case DW_OP_reg23:
24385 case DW_OP_reg24:
24386 case DW_OP_reg25:
24387 case DW_OP_reg26:
24388 case DW_OP_reg27:
24389 case DW_OP_reg28:
24390 case DW_OP_reg29:
24391 case DW_OP_reg30:
24392 case DW_OP_reg31:
24393 stack[++stacki] = op - DW_OP_reg0;
24394 if (i < size)
7d79de9a
TT
24395 {
24396 if (computed == nullptr)
24397 dwarf2_complex_location_expr_complaint ();
24398 else
24399 return 0;
24400 }
21ae7a4d 24401 break;
c906108c 24402
21ae7a4d
JK
24403 case DW_OP_regx:
24404 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
24405 i += bytes_read;
24406 stack[++stacki] = unsnd;
24407 if (i < size)
7d79de9a
TT
24408 {
24409 if (computed == nullptr)
24410 dwarf2_complex_location_expr_complaint ();
24411 else
24412 return 0;
24413 }
21ae7a4d 24414 break;
c906108c 24415
21ae7a4d 24416 case DW_OP_addr:
c8a7a66f
TT
24417 stack[++stacki] = cu->header.read_address (objfile->obfd, &data[i],
24418 &bytes_read);
21ae7a4d
JK
24419 i += bytes_read;
24420 break;
d53d4ac5 24421
21ae7a4d
JK
24422 case DW_OP_const1u:
24423 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
24424 i += 1;
24425 break;
24426
24427 case DW_OP_const1s:
24428 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
24429 i += 1;
24430 break;
24431
24432 case DW_OP_const2u:
24433 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
24434 i += 2;
24435 break;
24436
24437 case DW_OP_const2s:
24438 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
24439 i += 2;
24440 break;
d53d4ac5 24441
21ae7a4d
JK
24442 case DW_OP_const4u:
24443 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
24444 i += 4;
24445 break;
24446
24447 case DW_OP_const4s:
24448 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
24449 i += 4;
24450 break;
24451
585861ea
JK
24452 case DW_OP_const8u:
24453 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
24454 i += 8;
24455 break;
24456
21ae7a4d
JK
24457 case DW_OP_constu:
24458 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
24459 &bytes_read);
24460 i += bytes_read;
24461 break;
24462
24463 case DW_OP_consts:
24464 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
24465 i += bytes_read;
24466 break;
24467
24468 case DW_OP_dup:
24469 stack[stacki + 1] = stack[stacki];
24470 stacki++;
24471 break;
24472
24473 case DW_OP_plus:
24474 stack[stacki - 1] += stack[stacki];
24475 stacki--;
24476 break;
24477
24478 case DW_OP_plus_uconst:
24479 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
24480 &bytes_read);
24481 i += bytes_read;
24482 break;
24483
24484 case DW_OP_minus:
24485 stack[stacki - 1] -= stack[stacki];
24486 stacki--;
24487 break;
24488
24489 case DW_OP_deref:
24490 /* If we're not the last op, then we definitely can't encode
24491 this using GDB's address_class enum. This is valid for partial
24492 global symbols, although the variable's address will be bogus
24493 in the psymtab. */
24494 if (i < size)
7d79de9a
TT
24495 {
24496 if (computed == nullptr)
24497 dwarf2_complex_location_expr_complaint ();
24498 else
24499 return 0;
24500 }
21ae7a4d
JK
24501 break;
24502
dda83cd7 24503 case DW_OP_GNU_push_tls_address:
4aa4e28b 24504 case DW_OP_form_tls_address:
21ae7a4d
JK
24505 /* The top of the stack has the offset from the beginning
24506 of the thread control block at which the variable is located. */
24507 /* Nothing should follow this operator, so the top of stack would
24508 be returned. */
24509 /* This is valid for partial global symbols, but the variable's
585861ea
JK
24510 address will be bogus in the psymtab. Make it always at least
24511 non-zero to not look as a variable garbage collected by linker
24512 which have DW_OP_addr 0. */
21ae7a4d 24513 if (i < size)
7d79de9a
TT
24514 {
24515 if (computed == nullptr)
24516 dwarf2_complex_location_expr_complaint ();
24517 else
24518 return 0;
24519 }
585861ea 24520 stack[stacki]++;
dda83cd7 24521 break;
21ae7a4d
JK
24522
24523 case DW_OP_GNU_uninit:
7d79de9a
TT
24524 if (computed != nullptr)
24525 return 0;
21ae7a4d
JK
24526 break;
24527
336d760d 24528 case DW_OP_addrx:
3019eac3 24529 case DW_OP_GNU_addr_index:
49f6c839 24530 case DW_OP_GNU_const_index:
3019eac3
DE
24531 stack[++stacki] = read_addr_index_from_leb128 (cu, &data[i],
24532 &bytes_read);
24533 i += bytes_read;
24534 break;
24535
21ae7a4d 24536 default:
7d79de9a
TT
24537 if (computed == nullptr)
24538 {
24539 const char *name = get_DW_OP_name (op);
21ae7a4d 24540
7d79de9a
TT
24541 if (name)
24542 complaint (_("unsupported stack op: '%s'"),
24543 name);
24544 else
24545 complaint (_("unsupported stack op: '%02x'"),
24546 op);
24547 }
21ae7a4d
JK
24548
24549 return (stack[stacki]);
d53d4ac5 24550 }
3c6e0cb3 24551
21ae7a4d 24552 /* Enforce maximum stack depth of SIZE-1 to avoid writing
dda83cd7 24553 outside of the allocated space. Also enforce minimum>0. */
21ae7a4d
JK
24554 if (stacki >= ARRAY_SIZE (stack) - 1)
24555 {
7d79de9a
TT
24556 if (computed == nullptr)
24557 complaint (_("location description stack overflow"));
21ae7a4d
JK
24558 return 0;
24559 }
24560
24561 if (stacki <= 0)
24562 {
7d79de9a
TT
24563 if (computed == nullptr)
24564 complaint (_("location description stack underflow"));
21ae7a4d
JK
24565 return 0;
24566 }
24567 }
7d79de9a
TT
24568
24569 if (computed != nullptr)
24570 *computed = true;
21ae7a4d 24571 return (stack[stacki]);
c906108c
SS
24572}
24573
24574/* memory allocation interface */
24575
c906108c 24576static struct dwarf_block *
7b5a2f43 24577dwarf_alloc_block (struct dwarf2_cu *cu)
c906108c 24578{
8d749320 24579 return XOBNEW (&cu->comp_unit_obstack, struct dwarf_block);
c906108c
SS
24580}
24581
c906108c 24582static struct die_info *
b60c80d6 24583dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
c906108c
SS
24584{
24585 struct die_info *die;
b60c80d6
DJ
24586 size_t size = sizeof (struct die_info);
24587
24588 if (num_attrs > 1)
24589 size += (num_attrs - 1) * sizeof (struct attribute);
c906108c 24590
b60c80d6 24591 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
c906108c
SS
24592 memset (die, 0, sizeof (struct die_info));
24593 return (die);
24594}
2e276125
JB
24595
24596\f
a036ba48 24597
c90ec28a 24598/* Macro support. */
cf2c3c16 24599
9eac9650
TT
24600/* An overload of dwarf_decode_macros that finds the correct section
24601 and ensures it is read in before calling the other overload. */
24602
24603static void
24604dwarf_decode_macros (struct dwarf2_cu *cu, unsigned int offset,
24605 int section_is_gnu)
24606{
976ca316
SM
24607 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24608 struct objfile *objfile = per_objfile->objfile;
5a0e026f 24609 const struct line_header *lh = cu->line_header;
9eac9650
TT
24610 unsigned int offset_size = cu->header.offset_size;
24611 struct dwarf2_section_info *section;
24612 const char *section_name;
24613
24614 if (cu->dwo_unit != nullptr)
24615 {
24616 if (section_is_gnu)
24617 {
24618 section = &cu->dwo_unit->dwo_file->sections.macro;
24619 section_name = ".debug_macro.dwo";
24620 }
24621 else
24622 {
24623 section = &cu->dwo_unit->dwo_file->sections.macinfo;
24624 section_name = ".debug_macinfo.dwo";
24625 }
24626 }
24627 else
24628 {
24629 if (section_is_gnu)
24630 {
976ca316 24631 section = &per_objfile->per_bfd->macro;
9eac9650
TT
24632 section_name = ".debug_macro";
24633 }
24634 else
24635 {
976ca316 24636 section = &per_objfile->per_bfd->macinfo;
9eac9650
TT
24637 section_name = ".debug_macinfo";
24638 }
24639 }
24640
24641 section->read (objfile);
24642 if (section->buffer == nullptr)
24643 {
24644 complaint (_("missing %s section"), section_name);
24645 return;
24646 }
24647
24648 buildsym_compunit *builder = cu->get_builder ();
24649
048fde1e 24650 struct dwarf2_section_info *str_offsets_section;
24651 struct dwarf2_section_info *str_section;
24652 ULONGEST str_offsets_base;
24653
24654 if (cu->dwo_unit != nullptr)
24655 {
24656 str_offsets_section = &cu->dwo_unit->dwo_file
24657 ->sections.str_offsets;
24658 str_section = &cu->dwo_unit->dwo_file->sections.str;
24659 str_offsets_base = cu->header.addr_size;
24660 }
24661 else
24662 {
24663 str_offsets_section = &per_objfile->per_bfd->str_offsets;
24664 str_section = &per_objfile->per_bfd->str;
24665 str_offsets_base = *cu->str_offsets_base;
24666 }
24667
976ca316 24668 dwarf_decode_macros (per_objfile, builder, section, lh,
048fde1e 24669 offset_size, offset, str_section, str_offsets_section,
24670 str_offsets_base, section_is_gnu);
9eac9650
TT
24671}
24672
3019eac3
DE
24673/* Return the .debug_loc section to use for CU.
24674 For DWO files use .debug_loc.dwo. */
24675
24676static struct dwarf2_section_info *
24677cu_debug_loc_section (struct dwarf2_cu *cu)
24678{
976ca316 24679 dwarf2_per_objfile *per_objfile = cu->per_objfile;
ed2dc618 24680
3019eac3 24681 if (cu->dwo_unit)
43988095
JK
24682 {
24683 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
5f48f8f3 24684
43988095
JK
24685 return cu->header.version >= 5 ? &sections->loclists : &sections->loc;
24686 }
976ca316
SM
24687 return (cu->header.version >= 5 ? &per_objfile->per_bfd->loclists
24688 : &per_objfile->per_bfd->loc);
3019eac3
DE
24689}
24690
d0ce17d8
CT
24691/* Return the .debug_rnglists section to use for CU. */
24692static struct dwarf2_section_info *
24693cu_debug_rnglists_section (struct dwarf2_cu *cu, dwarf_tag tag)
24694{
24695 if (cu->header.version < 5)
24696 error (_(".debug_rnglists section cannot be used in DWARF %d"),
24697 cu->header.version);
24698 struct dwarf2_per_objfile *dwarf2_per_objfile = cu->per_objfile;
24699
24700 /* Make sure we read the .debug_rnglists section from the file that
24701 contains the DW_AT_ranges attribute we are reading. Normally that
24702 would be the .dwo file, if there is one. However for DW_TAG_compile_unit
24703 or DW_TAG_skeleton unit, we always want to read from objfile/linked
24704 program. */
24705 if (cu->dwo_unit != nullptr
24706 && tag != DW_TAG_compile_unit
24707 && tag != DW_TAG_skeleton_unit)
24708 {
24709 struct dwo_sections *sections = &cu->dwo_unit->dwo_file->sections;
24710
24711 if (sections->rnglists.size > 0)
24712 return &sections->rnglists;
24713 else
24714 error (_(".debug_rnglists section is missing from .dwo file."));
24715 }
24716 return &dwarf2_per_objfile->per_bfd->rnglists;
24717}
24718
8cf6f0b1
TT
24719/* A helper function that fills in a dwarf2_loclist_baton. */
24720
24721static void
24722fill_in_loclist_baton (struct dwarf2_cu *cu,
24723 struct dwarf2_loclist_baton *baton,
ff39bb5e 24724 const struct attribute *attr)
8cf6f0b1 24725{
976ca316 24726 dwarf2_per_objfile *per_objfile = cu->per_objfile;
3019eac3
DE
24727 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
24728
976ca316 24729 section->read (per_objfile->objfile);
8cf6f0b1 24730
976ca316 24731 baton->per_objfile = per_objfile;
8cf6f0b1
TT
24732 baton->per_cu = cu->per_cu;
24733 gdb_assert (baton->per_cu);
24734 /* We don't know how long the location list is, but make sure we
24735 don't run off the edge of the section. */
d4df075e
TT
24736 baton->size = section->size - attr->as_unsigned ();
24737 baton->data = section->buffer + attr->as_unsigned ();
2b24b6e4
TT
24738 if (cu->base_address.has_value ())
24739 baton->base_address = *cu->base_address;
24740 else
24741 baton->base_address = 0;
f664829e 24742 baton->from_dwo = cu->dwo_unit != NULL;
8cf6f0b1
TT
24743}
24744
4c2df51b 24745static void
ff39bb5e 24746dwarf2_symbol_mark_computed (const struct attribute *attr, struct symbol *sym,
f1e6e072 24747 struct dwarf2_cu *cu, int is_block)
4c2df51b 24748{
976ca316
SM
24749 dwarf2_per_objfile *per_objfile = cu->per_objfile;
24750 struct objfile *objfile = per_objfile->objfile;
3019eac3 24751 struct dwarf2_section_info *section = cu_debug_loc_section (cu);
bb5ed363 24752
cd6c91b4 24753 if (attr->form_is_section_offset ()
3019eac3 24754 /* .debug_loc{,.dwo} may not exist at all, or the offset may be outside
99bcc461
DJ
24755 the section. If so, fall through to the complaint in the
24756 other branch. */
d4df075e 24757 && attr->as_unsigned () < section->get_size (objfile))
4c2df51b 24758 {
0d53c4c4 24759 struct dwarf2_loclist_baton *baton;
4c2df51b 24760
8d749320 24761 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_loclist_baton);
4c2df51b 24762
8cf6f0b1 24763 fill_in_loclist_baton (cu, baton, attr);
be391dca 24764
2b24b6e4 24765 if (!cu->base_address.has_value ())
b98664d3 24766 complaint (_("Location list used without "
3e43a32a 24767 "specifying the CU base address."));
4c2df51b 24768
f1e6e072
TT
24769 SYMBOL_ACLASS_INDEX (sym) = (is_block
24770 ? dwarf2_loclist_block_index
24771 : dwarf2_loclist_index);
0d53c4c4
DJ
24772 SYMBOL_LOCATION_BATON (sym) = baton;
24773 }
24774 else
24775 {
24776 struct dwarf2_locexpr_baton *baton;
24777
8d749320 24778 baton = XOBNEW (&objfile->objfile_obstack, struct dwarf2_locexpr_baton);
976ca316 24779 baton->per_objfile = per_objfile;
ae0d2f24
UW
24780 baton->per_cu = cu->per_cu;
24781 gdb_assert (baton->per_cu);
0d53c4c4 24782
4fc6c0d5 24783 if (attr->form_is_block ())
0d53c4c4
DJ
24784 {
24785 /* Note that we're just copying the block's data pointer
24786 here, not the actual data. We're still pointing into the
6502dd73
DJ
24787 info_buffer for SYM's objfile; right now we never release
24788 that buffer, but when we do clean up properly this may
24789 need to change. */
9d2246fc
TT
24790 struct dwarf_block *block = attr->as_block ();
24791 baton->size = block->size;
24792 baton->data = block->data;
0d53c4c4
DJ
24793 }
24794 else
24795 {
24796 dwarf2_invalid_attrib_class_complaint ("location description",
987012b8 24797 sym->natural_name ());
0d53c4c4 24798 baton->size = 0;
0d53c4c4 24799 }
6e70227d 24800
f1e6e072
TT
24801 SYMBOL_ACLASS_INDEX (sym) = (is_block
24802 ? dwarf2_locexpr_block_index
24803 : dwarf2_locexpr_index);
0d53c4c4
DJ
24804 SYMBOL_LOCATION_BATON (sym) = baton;
24805 }
4c2df51b 24806}
6502dd73 24807
2e6a9f79 24808/* See read.h. */
96408a79 24809
2e6a9f79
SM
24810const comp_unit_head *
24811dwarf2_per_cu_data::get_header () const
96408a79 24812{
2e6a9f79
SM
24813 if (!m_header_read_in)
24814 {
24815 const gdb_byte *info_ptr
24816 = this->section->buffer + to_underlying (this->sect_off);
96408a79 24817
2e6a9f79 24818 memset (&m_header, 0, sizeof (m_header));
96408a79 24819
2e6a9f79
SM
24820 read_comp_unit_head (&m_header, info_ptr, this->section,
24821 rcuh_kind::COMPILE);
24822 }
96408a79 24823
2e6a9f79 24824 return &m_header;
96408a79
SA
24825}
24826
09ba997f 24827/* See read.h. */
ae0d2f24 24828
98714339 24829int
09ba997f 24830dwarf2_per_cu_data::addr_size () const
ae0d2f24 24831{
2e6a9f79 24832 return this->get_header ()->addr_size;
ae0d2f24
UW
24833}
24834
09ba997f 24835/* See read.h. */
9eae7c52
TT
24836
24837int
09ba997f 24838dwarf2_per_cu_data::offset_size () const
9eae7c52 24839{
2e6a9f79 24840 return this->get_header ()->offset_size;
96408a79
SA
24841}
24842
09ba997f 24843/* See read.h. */
96408a79
SA
24844
24845int
09ba997f 24846dwarf2_per_cu_data::ref_addr_size () const
96408a79 24847{
2e6a9f79 24848 const comp_unit_head *header = this->get_header ();
96408a79 24849
2e6a9f79
SM
24850 if (header->version == 2)
24851 return header->addr_size;
96408a79 24852 else
2e6a9f79 24853 return header->offset_size;
181cebd4
JK
24854}
24855
09ba997f 24856/* See read.h. */
9aa1f1e3 24857
09ba997f 24858struct type *
293e7e51 24859dwarf2_cu::addr_type () const
9a49df9d 24860{
293e7e51 24861 struct objfile *objfile = this->per_objfile->objfile;
9a49df9d
AB
24862 struct type *void_type = objfile_type (objfile)->builtin_void;
24863 struct type *addr_type = lookup_pointer_type (void_type);
293e7e51 24864 int addr_size = this->per_cu->addr_size ();
9a49df9d
AB
24865
24866 if (TYPE_LENGTH (addr_type) == addr_size)
24867 return addr_type;
24868
c6d940a9 24869 addr_type = addr_sized_int_type (addr_type->is_unsigned ());
9a49df9d
AB
24870 return addr_type;
24871}
24872
22b6cd70
TT
24873/* A helper function for dwarf2_find_containing_comp_unit that returns
24874 the index of the result, and that searches a vector. It will
24875 return a result even if the offset in question does not actually
24876 occur in any CU. This is separate so that it can be unit
24877 tested. */
ae038cb0 24878
22b6cd70
TT
24879static int
24880dwarf2_find_containing_comp_unit
24881 (sect_offset sect_off,
24882 unsigned int offset_in_dwz,
24883 const std::vector<dwarf2_per_cu_data *> &all_comp_units)
ae038cb0 24884{
ae038cb0
DJ
24885 int low, high;
24886
ae038cb0 24887 low = 0;
22b6cd70 24888 high = all_comp_units.size () - 1;
ae038cb0
DJ
24889 while (high > low)
24890 {
36586728 24891 struct dwarf2_per_cu_data *mid_cu;
ae038cb0 24892 int mid = low + (high - low) / 2;
9a619af0 24893
22b6cd70 24894 mid_cu = all_comp_units[mid];
36586728 24895 if (mid_cu->is_dwz > offset_in_dwz
81fbbaf9 24896 || (mid_cu->is_dwz == offset_in_dwz
22b6cd70 24897 && mid_cu->sect_off + mid_cu->length > sect_off))
ae038cb0
DJ
24898 high = mid;
24899 else
24900 low = mid + 1;
24901 }
24902 gdb_assert (low == high);
22b6cd70
TT
24903 return low;
24904}
24905
24906/* Locate the .debug_info compilation unit from CU's objfile which contains
24907 the DIE at OFFSET. Raises an error on failure. */
24908
24909static struct dwarf2_per_cu_data *
24910dwarf2_find_containing_comp_unit (sect_offset sect_off,
24911 unsigned int offset_in_dwz,
976ca316 24912 dwarf2_per_objfile *per_objfile)
22b6cd70 24913{
976ca316
SM
24914 int low = dwarf2_find_containing_comp_unit
24915 (sect_off, offset_in_dwz, per_objfile->per_bfd->all_comp_units);
24916 dwarf2_per_cu_data *this_cu = per_objfile->per_bfd->all_comp_units[low];
22b6cd70 24917
45b8ae0c 24918 if (this_cu->is_dwz != offset_in_dwz || this_cu->sect_off > sect_off)
ae038cb0 24919 {
36586728 24920 if (low == 0 || this_cu->is_dwz != offset_in_dwz)
8a3fe4f8 24921 error (_("Dwarf Error: could not find partial DIE containing "
9d8780f0
SM
24922 "offset %s [in module %s]"),
24923 sect_offset_str (sect_off),
976ca316 24924 bfd_get_filename (per_objfile->objfile->obfd));
10b3939b 24925
976ca316 24926 gdb_assert (per_objfile->per_bfd->all_comp_units[low-1]->sect_off
9c541725 24927 <= sect_off);
976ca316 24928 return per_objfile->per_bfd->all_comp_units[low-1];
ae038cb0
DJ
24929 }
24930 else
24931 {
976ca316 24932 if (low == per_objfile->per_bfd->all_comp_units.size () - 1
9c541725 24933 && sect_off >= this_cu->sect_off + this_cu->length)
9d8780f0 24934 error (_("invalid dwarf2 offset %s"), sect_offset_str (sect_off));
9c541725 24935 gdb_assert (sect_off < this_cu->sect_off + this_cu->length);
ae038cb0
DJ
24936 return this_cu;
24937 }
24938}
24939
22b6cd70
TT
24940#if GDB_SELF_TEST
24941
24942namespace selftests {
24943namespace find_containing_comp_unit {
24944
24945static void
24946run_test ()
24947{
24948 struct dwarf2_per_cu_data one {};
24949 struct dwarf2_per_cu_data two {};
24950 struct dwarf2_per_cu_data three {};
24951 struct dwarf2_per_cu_data four {};
24952
24953 one.length = 5;
24954 two.sect_off = sect_offset (one.length);
24955 two.length = 7;
24956
24957 three.length = 5;
24958 three.is_dwz = 1;
24959 four.sect_off = sect_offset (three.length);
24960 four.length = 7;
24961 four.is_dwz = 1;
24962
24963 std::vector<dwarf2_per_cu_data *> units;
24964 units.push_back (&one);
24965 units.push_back (&two);
24966 units.push_back (&three);
24967 units.push_back (&four);
24968
24969 int result;
24970
24971 result = dwarf2_find_containing_comp_unit (sect_offset (0), 0, units);
24972 SELF_CHECK (units[result] == &one);
24973 result = dwarf2_find_containing_comp_unit (sect_offset (3), 0, units);
24974 SELF_CHECK (units[result] == &one);
24975 result = dwarf2_find_containing_comp_unit (sect_offset (5), 0, units);
24976 SELF_CHECK (units[result] == &two);
24977
24978 result = dwarf2_find_containing_comp_unit (sect_offset (0), 1, units);
24979 SELF_CHECK (units[result] == &three);
24980 result = dwarf2_find_containing_comp_unit (sect_offset (3), 1, units);
24981 SELF_CHECK (units[result] == &three);
24982 result = dwarf2_find_containing_comp_unit (sect_offset (5), 1, units);
24983 SELF_CHECK (units[result] == &four);
24984}
24985
24986}
24987}
24988
24989#endif /* GDB_SELF_TEST */
24990
9e021579 24991/* Initialize dwarf2_cu to read PER_CU, in the context of PER_OBJFILE. */
93311388 24992
9e021579
SM
24993dwarf2_cu::dwarf2_cu (dwarf2_per_cu_data *per_cu,
24994 dwarf2_per_objfile *per_objfile)
24995 : per_cu (per_cu),
24996 per_objfile (per_objfile),
9068261f
AB
24997 mark (false),
24998 has_loclist (false),
24999 checked_producer (false),
25000 producer_is_gxx_lt_4_6 (false),
25001 producer_is_gcc_lt_4_3 (false),
eb77c9df 25002 producer_is_icc (false),
9068261f 25003 producer_is_icc_lt_14 (false),
c258c396 25004 producer_is_codewarrior (false),
9068261f 25005 processing_has_namespace_info (false)
93311388 25006{
9816fde3
JK
25007}
25008
25009/* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
25010
25011static void
95554aad
TT
25012prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die,
25013 enum language pretend_language)
9816fde3
JK
25014{
25015 struct attribute *attr;
25016
25017 /* Set the language we're debugging. */
25018 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
435d3d88 25019 if (attr != nullptr)
529908cb 25020 set_cu_language (attr->constant_value (0), cu);
9816fde3 25021 else
9cded63f 25022 {
95554aad 25023 cu->language = pretend_language;
9cded63f
TT
25024 cu->language_defn = language_def (cu->language);
25025 }
dee91e82 25026
7d45c7c3 25027 cu->producer = dwarf2_string_attr (comp_unit_die, DW_AT_producer, cu);
93311388
DE
25028}
25029
7188ed02 25030/* See read.h. */
ae038cb0 25031
7188ed02
SM
25032dwarf2_cu *
25033dwarf2_per_objfile::get_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25034{
7188ed02
SM
25035 auto it = m_dwarf2_cus.find (per_cu);
25036 if (it == m_dwarf2_cus.end ())
25037 return nullptr;
ae038cb0 25038
7188ed02
SM
25039 return it->second;
25040}
25041
25042/* See read.h. */
25043
25044void
25045dwarf2_per_objfile::set_cu (dwarf2_per_cu_data *per_cu, dwarf2_cu *cu)
25046{
25047 gdb_assert (this->get_cu (per_cu) == nullptr);
25048
25049 m_dwarf2_cus[per_cu] = cu;
25050}
25051
25052/* See read.h. */
25053
25054void
25055dwarf2_per_objfile::age_comp_units ()
25056{
17e593e9
SM
25057 dwarf_read_debug_printf_v ("running");
25058
7188ed02
SM
25059 /* Start by clearing all marks. */
25060 for (auto pair : m_dwarf2_cus)
25061 pair.second->mark = false;
25062
25063 /* Traverse all CUs, mark them and their dependencies if used recently
25064 enough. */
25065 for (auto pair : m_dwarf2_cus)
ae038cb0 25066 {
7188ed02
SM
25067 dwarf2_cu *cu = pair.second;
25068
25069 cu->last_used++;
25070 if (cu->last_used <= dwarf_max_cache_age)
25071 dwarf2_mark (cu);
ae038cb0
DJ
25072 }
25073
7188ed02
SM
25074 /* Delete all CUs still not marked. */
25075 for (auto it = m_dwarf2_cus.begin (); it != m_dwarf2_cus.end ();)
ae038cb0 25076 {
7188ed02 25077 dwarf2_cu *cu = it->second;
ae038cb0 25078
7188ed02 25079 if (!cu->mark)
ae038cb0 25080 {
17e593e9
SM
25081 dwarf_read_debug_printf_v ("deleting old CU %s",
25082 sect_offset_str (cu->per_cu->sect_off));
7188ed02
SM
25083 delete cu;
25084 it = m_dwarf2_cus.erase (it);
ae038cb0
DJ
25085 }
25086 else
7188ed02 25087 it++;
ae038cb0
DJ
25088 }
25089}
25090
7188ed02 25091/* See read.h. */
ae038cb0 25092
7188ed02
SM
25093void
25094dwarf2_per_objfile::remove_cu (dwarf2_per_cu_data *per_cu)
ae038cb0 25095{
7188ed02
SM
25096 auto it = m_dwarf2_cus.find (per_cu);
25097 if (it == m_dwarf2_cus.end ())
25098 return;
ae038cb0 25099
7188ed02 25100 delete it->second;
ae038cb0 25101
7188ed02
SM
25102 m_dwarf2_cus.erase (it);
25103}
ae038cb0 25104
7188ed02
SM
25105dwarf2_per_objfile::~dwarf2_per_objfile ()
25106{
25107 remove_all_cus ();
ae038cb0
DJ
25108}
25109
dee91e82
DE
25110/* A set of CU "per_cu" pointer, DIE offset, and GDB type pointer.
25111 We store these in a hash table separate from the DIEs, and preserve them
25112 when the DIEs are flushed out of cache.
25113
25114 The CU "per_cu" pointer is needed because offset alone is not enough to
3019eac3 25115 uniquely identify the type. A file may have multiple .debug_types sections,
c88ee1f0
DE
25116 or the type may come from a DWO file. Furthermore, while it's more logical
25117 to use per_cu->section+offset, with Fission the section with the data is in
25118 the DWO file but we don't know that section at the point we need it.
25119 We have to use something in dwarf2_per_cu_data (or the pointer to it)
25120 because we can enter the lookup routine, get_die_type_at_offset, from
25121 outside this file, and thus won't necessarily have PER_CU->cu.
25122 Fortunately, PER_CU is stable for the life of the objfile. */
1c379e20 25123
dee91e82 25124struct dwarf2_per_cu_offset_and_type
1c379e20 25125{
dee91e82 25126 const struct dwarf2_per_cu_data *per_cu;
9c541725 25127 sect_offset sect_off;
1c379e20
DJ
25128 struct type *type;
25129};
25130
dee91e82 25131/* Hash function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25132
25133static hashval_t
dee91e82 25134per_cu_offset_and_type_hash (const void *item)
1c379e20 25135{
9a3c8263
SM
25136 const struct dwarf2_per_cu_offset_and_type *ofs
25137 = (const struct dwarf2_per_cu_offset_and_type *) item;
9a619af0 25138
9c541725 25139 return (uintptr_t) ofs->per_cu + to_underlying (ofs->sect_off);
1c379e20
DJ
25140}
25141
dee91e82 25142/* Equality function for a dwarf2_per_cu_offset_and_type. */
1c379e20
DJ
25143
25144static int
dee91e82 25145per_cu_offset_and_type_eq (const void *item_lhs, const void *item_rhs)
1c379e20 25146{
9a3c8263
SM
25147 const struct dwarf2_per_cu_offset_and_type *ofs_lhs
25148 = (const struct dwarf2_per_cu_offset_and_type *) item_lhs;
25149 const struct dwarf2_per_cu_offset_and_type *ofs_rhs
25150 = (const struct dwarf2_per_cu_offset_and_type *) item_rhs;
9a619af0 25151
dee91e82 25152 return (ofs_lhs->per_cu == ofs_rhs->per_cu
9c541725 25153 && ofs_lhs->sect_off == ofs_rhs->sect_off);
1c379e20
DJ
25154}
25155
25156/* Set the type associated with DIE to TYPE. Save it in CU's hash
7e314c57
JK
25157 table if necessary. For convenience, return TYPE.
25158
25159 The DIEs reading must have careful ordering to:
85102364 25160 * Not cause infinite loops trying to read in DIEs as a prerequisite for
7e314c57
JK
25161 reading current DIE.
25162 * Not trying to dereference contents of still incompletely read in types
25163 while reading in other DIEs.
25164 * Enable referencing still incompletely read in types just by a pointer to
25165 the type without accessing its fields.
25166
25167 Therefore caller should follow these rules:
25168 * Try to fetch any prerequisite types we may need to build this DIE type
25169 before building the type and calling set_die_type.
e71ec853 25170 * After building type call set_die_type for current DIE as soon as
7e314c57
JK
25171 possible before fetching more types to complete the current type.
25172 * Make the type as complete as possible before fetching more types. */
1c379e20 25173
f792889a 25174static struct type *
57567375
TT
25175set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
25176 bool skip_data_location)
1c379e20 25177{
976ca316 25178 dwarf2_per_objfile *per_objfile = cu->per_objfile;
dee91e82 25179 struct dwarf2_per_cu_offset_and_type **slot, ofs;
976ca316 25180 struct objfile *objfile = per_objfile->objfile;
3cdcd0ce
JB
25181 struct attribute *attr;
25182 struct dynamic_prop prop;
1c379e20 25183
b4ba55a1
JB
25184 /* For Ada types, make sure that the gnat-specific data is always
25185 initialized (if not already set). There are a few types where
25186 we should not be doing so, because the type-specific area is
25187 already used to hold some other piece of info (eg: TYPE_CODE_FLT
25188 where the type-specific area is used to store the floatformat).
25189 But this is not a problem, because the gnat-specific information
25190 is actually not needed for these types. */
25191 if (need_gnat_info (cu)
78134374
SM
25192 && type->code () != TYPE_CODE_FUNC
25193 && type->code () != TYPE_CODE_FLT
25194 && type->code () != TYPE_CODE_METHODPTR
25195 && type->code () != TYPE_CODE_MEMBERPTR
25196 && type->code () != TYPE_CODE_METHOD
09584414 25197 && type->code () != TYPE_CODE_FIXED_POINT
b4ba55a1
JB
25198 && !HAVE_GNAT_AUX_INFO (type))
25199 INIT_GNAT_SPECIFIC (type);
25200
3f2f83dd
KB
25201 /* Read DW_AT_allocated and set in type. */
25202 attr = dwarf2_attr (die, DW_AT_allocated, cu);
9cdf9820 25203 if (attr != NULL)
3f2f83dd 25204 {
293e7e51 25205 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25206 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25207 type->add_dyn_prop (DYN_PROP_ALLOCATED, prop);
3f2f83dd 25208 }
3f2f83dd
KB
25209
25210 /* Read DW_AT_associated and set in type. */
25211 attr = dwarf2_attr (die, DW_AT_associated, cu);
9cdf9820 25212 if (attr != NULL)
3f2f83dd 25213 {
293e7e51 25214 struct type *prop_type = cu->addr_sized_int_type (false);
9a49df9d 25215 if (attr_to_dynamic_prop (attr, die, cu, &prop, prop_type))
dda83cd7 25216 type->add_dyn_prop (DYN_PROP_ASSOCIATED, prop);
3f2f83dd 25217 }
3f2f83dd 25218
3cdcd0ce 25219 /* Read DW_AT_data_location and set in type. */
57567375
TT
25220 if (!skip_data_location)
25221 {
25222 attr = dwarf2_attr (die, DW_AT_data_location, cu);
25223 if (attr_to_dynamic_prop (attr, die, cu, &prop, cu->addr_type ()))
25224 type->add_dyn_prop (DYN_PROP_DATA_LOCATION, prop);
25225 }
3cdcd0ce 25226
976ca316
SM
25227 if (per_objfile->die_type_hash == NULL)
25228 per_objfile->die_type_hash
0335378b
TT
25229 = htab_up (htab_create_alloc (127,
25230 per_cu_offset_and_type_hash,
25231 per_cu_offset_and_type_eq,
25232 NULL, xcalloc, xfree));
1c379e20 25233
dee91e82 25234 ofs.per_cu = cu->per_cu;
9c541725 25235 ofs.sect_off = die->sect_off;
1c379e20 25236 ofs.type = type;
dee91e82 25237 slot = (struct dwarf2_per_cu_offset_and_type **)
976ca316 25238 htab_find_slot (per_objfile->die_type_hash.get (), &ofs, INSERT);
7e314c57 25239 if (*slot)
b98664d3 25240 complaint (_("A problem internal to GDB: DIE %s has type already set"),
9d8780f0 25241 sect_offset_str (die->sect_off));
8d749320
SM
25242 *slot = XOBNEW (&objfile->objfile_obstack,
25243 struct dwarf2_per_cu_offset_and_type);
1c379e20 25244 **slot = ofs;
f792889a 25245 return type;
1c379e20
DJ
25246}
25247
9c541725 25248/* Look up the type for the die at SECT_OFF in PER_CU in die_type_hash,
02142a6c 25249 or return NULL if the die does not have a saved type. */
1c379e20
DJ
25250
25251static struct type *
9c541725 25252get_die_type_at_offset (sect_offset sect_off,
aa66c379 25253 dwarf2_per_cu_data *per_cu,
976ca316 25254 dwarf2_per_objfile *per_objfile)
1c379e20 25255{
dee91e82 25256 struct dwarf2_per_cu_offset_and_type *slot, ofs;
f792889a 25257
976ca316 25258 if (per_objfile->die_type_hash == NULL)
f792889a 25259 return NULL;
1c379e20 25260
dee91e82 25261 ofs.per_cu = per_cu;
9c541725 25262 ofs.sect_off = sect_off;
9a3c8263 25263 slot = ((struct dwarf2_per_cu_offset_and_type *)
976ca316 25264 htab_find (per_objfile->die_type_hash.get (), &ofs));
1c379e20
DJ
25265 if (slot)
25266 return slot->type;
25267 else
25268 return NULL;
25269}
25270
02142a6c 25271/* Look up the type for DIE in CU in die_type_hash,
673bfd45
DE
25272 or return NULL if DIE does not have a saved type. */
25273
25274static struct type *
25275get_die_type (struct die_info *die, struct dwarf2_cu *cu)
25276{
aa66c379 25277 return get_die_type_at_offset (die->sect_off, cu->per_cu, cu->per_objfile);
673bfd45
DE
25278}
25279
10b3939b
DJ
25280/* Add a dependence relationship from CU to REF_PER_CU. */
25281
25282static void
25283dwarf2_add_dependence (struct dwarf2_cu *cu,
25284 struct dwarf2_per_cu_data *ref_per_cu)
25285{
25286 void **slot;
25287
25288 if (cu->dependencies == NULL)
25289 cu->dependencies
25290 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
25291 NULL, &cu->comp_unit_obstack,
25292 hashtab_obstack_allocate,
25293 dummy_obstack_deallocate);
25294
25295 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
25296 if (*slot == NULL)
25297 *slot = ref_per_cu;
25298}
1c379e20 25299
f504f079
DE
25300/* Subroutine of dwarf2_mark to pass to htab_traverse.
25301 Set the mark field in every compilation unit in the
7188ed02
SM
25302 cache that we must keep because we are keeping CU.
25303
25304 DATA is the dwarf2_per_objfile object in which to look up CUs. */
ae038cb0 25305
10b3939b
DJ
25306static int
25307dwarf2_mark_helper (void **slot, void *data)
25308{
7188ed02
SM
25309 dwarf2_per_cu_data *per_cu = (dwarf2_per_cu_data *) *slot;
25310 dwarf2_per_objfile *per_objfile = (dwarf2_per_objfile *) data;
25311 dwarf2_cu *cu = per_objfile->get_cu (per_cu);
d07ed419
JK
25312
25313 /* cu->dependencies references may not yet have been ever read if QUIT aborts
25314 reading of the chain. As such dependencies remain valid it is not much
25315 useful to track and undo them during QUIT cleanups. */
7188ed02 25316 if (cu == nullptr)
d07ed419
JK
25317 return 1;
25318
7188ed02 25319 if (cu->mark)
10b3939b 25320 return 1;
10b3939b 25321
7188ed02
SM
25322 cu->mark = true;
25323
25324 if (cu->dependencies != nullptr)
25325 htab_traverse (cu->dependencies, dwarf2_mark_helper, per_objfile);
10b3939b
DJ
25326
25327 return 1;
25328}
25329
f504f079
DE
25330/* Set the mark field in CU and in every other compilation unit in the
25331 cache that we must keep because we are keeping CU. */
25332
ae038cb0
DJ
25333static void
25334dwarf2_mark (struct dwarf2_cu *cu)
25335{
25336 if (cu->mark)
25337 return;
7188ed02 25338
9068261f 25339 cu->mark = true;
ae038cb0 25340
7188ed02
SM
25341 if (cu->dependencies != nullptr)
25342 htab_traverse (cu->dependencies, dwarf2_mark_helper, cu->per_objfile);
72bf9492
DJ
25343}
25344
72bf9492
DJ
25345/* Trivial hash function for partial_die_info: the hash value of a DIE
25346 is its offset in .debug_info for this objfile. */
25347
25348static hashval_t
25349partial_die_hash (const void *item)
25350{
9a3c8263
SM
25351 const struct partial_die_info *part_die
25352 = (const struct partial_die_info *) item;
9a619af0 25353
9c541725 25354 return to_underlying (part_die->sect_off);
72bf9492
DJ
25355}
25356
25357/* Trivial comparison function for partial_die_info structures: two DIEs
25358 are equal if they have the same offset. */
25359
25360static int
25361partial_die_eq (const void *item_lhs, const void *item_rhs)
25362{
9a3c8263
SM
25363 const struct partial_die_info *part_die_lhs
25364 = (const struct partial_die_info *) item_lhs;
25365 const struct partial_die_info *part_die_rhs
25366 = (const struct partial_die_info *) item_rhs;
9a619af0 25367
9c541725 25368 return part_die_lhs->sect_off == part_die_rhs->sect_off;
72bf9492
DJ
25369}
25370
3c3bb058
AB
25371struct cmd_list_element *set_dwarf_cmdlist;
25372struct cmd_list_element *show_dwarf_cmdlist;
ae038cb0 25373
9291a0cd 25374static void
cd4fb1b2
SM
25375show_check_physname (struct ui_file *file, int from_tty,
25376 struct cmd_list_element *c, const char *value)
9291a0cd 25377{
cd4fb1b2
SM
25378 fprintf_filtered (file,
25379 _("Whether to check \"physname\" is %s.\n"),
25380 value);
9291a0cd
TT
25381}
25382
6c265988 25383void _initialize_dwarf2_read ();
cd4fb1b2 25384void
6c265988 25385_initialize_dwarf2_read ()
9291a0cd 25386{
0743fc83 25387 add_basic_prefix_cmd ("dwarf", class_maintenance, _("\
cd4fb1b2 25388Set DWARF specific variables.\n\
590042fc 25389Configure DWARF variables such as the cache size."),
0743fc83
TT
25390 &set_dwarf_cmdlist, "maintenance set dwarf ",
25391 0/*allow-unknown*/, &maintenance_set_cmdlist);
156942c7 25392
0743fc83 25393 add_show_prefix_cmd ("dwarf", class_maintenance, _("\
590042fc
PW
25394Show DWARF specific variables.\n\
25395Show DWARF variables such as the cache size."),
0743fc83
TT
25396 &show_dwarf_cmdlist, "maintenance show dwarf ",
25397 0/*allow-unknown*/, &maintenance_show_cmdlist);
156942c7 25398
cd4fb1b2
SM
25399 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
25400 &dwarf_max_cache_age, _("\
25401Set the upper bound on the age of cached DWARF compilation units."), _("\
25402Show the upper bound on the age of cached DWARF compilation units."), _("\
25403A higher limit means that cached compilation units will be stored\n\
25404in memory longer, and more total memory will be used. Zero disables\n\
25405caching, which can slow down startup."),
25406 NULL,
25407 show_dwarf_max_cache_age,
25408 &set_dwarf_cmdlist,
25409 &show_dwarf_cmdlist);
156942c7 25410
cd4fb1b2
SM
25411 add_setshow_zuinteger_cmd ("dwarf-read", no_class, &dwarf_read_debug, _("\
25412Set debugging of the DWARF reader."), _("\
25413Show debugging of the DWARF reader."), _("\
25414When enabled (non-zero), debugging messages are printed during DWARF\n\
25415reading and symtab expansion. A value of 1 (one) provides basic\n\
25416information. A value greater than 1 provides more verbose information."),
25417 NULL,
25418 NULL,
25419 &setdebuglist, &showdebuglist);
9291a0cd 25420
cd4fb1b2
SM
25421 add_setshow_zuinteger_cmd ("dwarf-die", no_class, &dwarf_die_debug, _("\
25422Set debugging of the DWARF DIE reader."), _("\
25423Show debugging of the DWARF DIE reader."), _("\
25424When enabled (non-zero), DIEs are dumped after they are read in.\n\
25425The value is the maximum depth to print."),
25426 NULL,
25427 NULL,
25428 &setdebuglist, &showdebuglist);
9291a0cd 25429
cd4fb1b2
SM
25430 add_setshow_zuinteger_cmd ("dwarf-line", no_class, &dwarf_line_debug, _("\
25431Set debugging of the dwarf line reader."), _("\
25432Show debugging of the dwarf line reader."), _("\
25433When enabled (non-zero), line number entries are dumped as they are read in.\n\
25434A value of 1 (one) provides basic information.\n\
25435A value greater than 1 provides more verbose information."),
25436 NULL,
25437 NULL,
25438 &setdebuglist, &showdebuglist);
437afbb8 25439
cd4fb1b2
SM
25440 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
25441Set cross-checking of \"physname\" code against demangler."), _("\
25442Show cross-checking of \"physname\" code against demangler."), _("\
25443When enabled, GDB's internal \"physname\" code is checked against\n\
25444the demangler."),
25445 NULL, show_check_physname,
25446 &setdebuglist, &showdebuglist);
900e11f9 25447
e615022a
DE
25448 add_setshow_boolean_cmd ("use-deprecated-index-sections",
25449 no_class, &use_deprecated_index_sections, _("\
25450Set whether to use deprecated gdb_index sections."), _("\
25451Show whether to use deprecated gdb_index sections."), _("\
25452When enabled, deprecated .gdb_index sections are used anyway.\n\
25453Normally they are ignored either because of a missing feature or\n\
25454performance issue.\n\
25455Warning: This option must be enabled before gdb reads the file."),
25456 NULL,
25457 NULL,
25458 &setlist, &showlist);
25459
f1e6e072
TT
25460 dwarf2_locexpr_index = register_symbol_computed_impl (LOC_COMPUTED,
25461 &dwarf2_locexpr_funcs);
25462 dwarf2_loclist_index = register_symbol_computed_impl (LOC_COMPUTED,
25463 &dwarf2_loclist_funcs);
25464
25465 dwarf2_locexpr_block_index = register_symbol_block_impl (LOC_BLOCK,
25466 &dwarf2_block_frame_base_locexpr_funcs);
25467 dwarf2_loclist_block_index = register_symbol_block_impl (LOC_BLOCK,
25468 &dwarf2_block_frame_base_loclist_funcs);
c62446b1
PA
25469
25470#if GDB_SELF_TEST
25471 selftests::register_test ("dw2_expand_symtabs_matching",
25472 selftests::dw2_expand_symtabs_matching::run_test);
22b6cd70
TT
25473 selftests::register_test ("dwarf2_find_containing_comp_unit",
25474 selftests::find_containing_comp_unit::run_test);
c62446b1 25475#endif
6502dd73 25476}
This page took 6.766257 seconds and 4 git commands to generate.